summaryrefslogtreecommitdiffstats
path: root/autoconf/api.lua
blob: 064ea795c8f1da132b68c7db220f0847338d77e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
---
-- Autoconfiguration.
-- Copyright (c) 2016 Blizzard Entertainment
-- Enhanced by re3
---
local p = premake
local autoconf = p.modules.autoconf
autoconf.cache = {}
autoconf.parameters = ""


---
-- register autoconfigure api.
---
p.api.register {
	name = "autoconfigure",
	scope = "config",
	kind = "table"
}

---
-- Check for a particular include file.
--
-- @cfg      : Current config.
-- @variable : The variable to store the result, such as 'HAVE_STDINT_H'.
-- @filename : The header file to check for.
---
function check_include(cfg, variable, filename)
	local res = autoconf.cache_compile(cfg, variable, function ()
		p.outln('#include <' .. filename .. '>')
		p.outln('int main(void) { return 0; }')
	end)

	if res.value then
		autoconf.set_value(cfg, variable, 1)
	end
end


---
-- Check for size of a particular type.
--
-- @cfg      : Current config.
-- @variable : The variable to use, such as 'SIZEOF_SIZE_T', this method will also add "'HAVE_' .. variable".
-- @type     : The type to check.
-- @headers  : An optional array of header files to include.
-- @defines  : An optional array of defines to define.
---
function check_type_size(cfg, variable, type, headers, defines)
	check_include(cfg, 'HAVE_SYS_TYPES_H', 'sys/types.h')
	check_include(cfg, 'HAVE_STDINT_H', 'stdint.h')
	check_include(cfg, 'HAVE_STDDEF_H', 'stddef.h')

	local res = autoconf.cache_compile(cfg, variable .. cfg.platform,
		function ()
			if cfg.autoconf['HAVE_SYS_TYPES_H'] then
				p.outln('#include <sys/types.h>')
			end

			if cfg.autoconf['HAVE_STDINT_H'] then
				p.outln('#include <stdint.h>')
			end

			if cfg.autoconf['HAVE_STDDEF_H'] then
				p.outln('#include <stddef.h>')
			end

			autoconf.include_defines(defines)
			autoconf.include_headers(headers)
			p.outln("")
			p.outln("#define SIZE (sizeof(" .. type .. "))")
			p.outln("char info_size[] =  {'I', 'N', 'F', 'O', ':', 's','i','z','e','[',")
			p.outln("  ('0' + ((SIZE / 10000)%10)),")
			p.outln("  ('0' + ((SIZE / 1000)%10)),")
			p.outln("  ('0' + ((SIZE / 100)%10)),")
			p.outln("  ('0' + ((SIZE / 10)%10)),")
			p.outln("  ('0' +  (SIZE     %10)),")
			p.outln("  ']', '\\0'};")
			p.outln("")
			p.outln("int main(int argc, char *argv[]) {")
			p.outln("  int require = 0;")
			p.outln("  require += info_size[argc];")
			p.outln("  (void)argv;")
			p.outln("  return require;")
			p.outln("}")
		end,
		function (e)
			-- if the compile step succeeded, we should have a binary with 'INFO:size[*****]'
			-- somewhere in there.
			local content = io.readfile(e.binary)
			if content then
				local size = string.find(content, 'INFO:size')
				if size then
					e.size = tonumber(string.sub(content, size+10, size+14))
				end
			end
		end
	)

	if res.size then
		autoconf.set_value(cfg, 'HAVE_' .. variable, 1)
		autoconf.set_value(cfg, variable, res.size)
	end
end


---
-- Check if the given struct or class has the specified member variable
--
-- @cfg      : current config.
-- @variable : variable to store the result.
-- @type     : the name of the struct or class you are interested in
-- @member   : the member which existence you want to check
-- @headers  : an optional array of header files to include.
-- @defines  : An optional array of defines to define.
---
function check_struct_has_member(cfg, variable, type, member, headers, defines)
	local res = autoconf.cache_compile(cfg, variable, function ()
		autoconf.include_defines(defines)
		autoconf.include_headers(headers)
		p.outln('int main(void) {')
		p.outln('  (void)sizeof(((' .. type .. '*)0)->' .. member ..');')
		p.outln('  return 0;')
		p.outln('}')
	end)

	if res.value then
		autoconf.set_value(cfg, variable, 1)
	end
end


---
-- Check if a symbol exists as a function, variable, or macro
--
-- @cfg      : current config.
-- @variable : variable to store the result.
-- @symbol   : The symbol to check for.
-- @headers  : an optional array of header files to include.
-- @defines  : An optional array of defines to define.
---
function check_symbol_exists(cfg, variable, symbol, headers, defines)
	local h = headers
	local res = autoconf.cache_compile(cfg, variable, function ()
		autoconf.include_defines(defines)
		autoconf.include_headers(headers)
		p.outln('int main(int argc, char** argv) {')
		p.outln('  (void)argv;')
		p.outln('#ifndef ' .. symbol)
		p.outln('  return ((int*)(&' .. symbol .. '))[argc];')
		p.outln('#else')
		p.outln('  (void)argc;')
		p.outln('  return 0;')
		p.outln('#endif')
		p.outln('}')
	end)

	if res.value then
		autoconf.set_value(cfg, variable, 1)
	end
end


---
-- try compiling a piece of c/c++
---
function autoconf.try_compile(cfg, cpp)
	local ts = autoconf.toolset(cfg)
	if ts then
		return ts.try_compile(cfg, cpp, autoconf.parameters)
	else
		p.warnOnce('autoconf', 'no toolset found, autoconf always failing.')
	end
end


function autoconf.cache_compile(cfg, entry, func, post)
	if not autoconf.cache[entry] then
		local cpp = p.capture(func)
		local res = autoconf.try_compile(cfg, cpp)
		if res then
			local e = { binary = res, value = true }
			if post then
				post(e)
			end
			autoconf.cache[entry] = e
		else
			autoconf.cache[entry] = { }
		end
	end
	return autoconf.cache[entry]
end


---
-- get the current configured toolset, or the default.
---
function autoconf.toolset(cfg)
	local ts = p.config.toolset(cfg)
	if not ts then
		local tools = {
			-- Actually we always return nil on msc. see msc.lua
			['vs2010']   = p.tools.msc,
			['vs2012']   = p.tools.msc,
			['vs2013']   = p.tools.msc,
			['vs2015']   = p.tools.msc,
			['vs2017']   = p.tools.msc,
			['vs2019']   = p.tools.msc,
			['gmake']    = premake.tools.gcc,
			['gmake2']    = premake.tools.gcc,
			['codelite'] = premake.tools.gcc,
			['xcode4']    = premake.tools.clang,
		}
		ts = tools[_ACTION]
	end
	return ts
end


---
-- store the value of the variable in the configuration
---
function autoconf.set_value(cfg, variable, value)
	cfg.autoconf[variable] = value
end


---
-- write the cfg.autoconf table to the file
---
function autoconf.writefile(cfg, filename)
	if cfg.autoconf then
		local file = io.open(filename, "w+")
		for variable, value in pairs(cfg.autoconf) do
			file:write('#define ' .. variable .. ' ' .. tostring(value) .. (_eol or '\n'))
		end
		file:close()
	end
end


---
-- Utility method to add a table of headers.
---
function autoconf.include_headers(headers)
	if headers ~= nil then
		if type(headers) == "table" then
			for _, v in ipairs(headers) do
				p.outln('#include <' .. v .. '>')
			end
		else
			p.outln('#include <' .. headers .. '>')
		end
	end
end

function autoconf.include_defines(defines)
	if defines ~= nil then
		if type(defines) == "table" then
			for _, v in ipairs(defines) do
				p.outln('#define ' .. v)
			end
		else
			p.outln('#define ' .. defines)
		end
	end
end

---
-- attach ourselfs to the running action.
---
p.override(p.action, 'call', function (base, name)
	local a = p.action.get(name)

	-- store the old callback.
	local onBaseProject = a.onProject or a.onproject

	-- override it with our own.
	a.onProject = function(prj)
		-- go through each configuration, and call the setup configuration methods.
		for cfg in p.project.eachconfig(prj) do
			cfg.autoconf = {}
			if cfg.autoconfigure then
				verbosef('Running auto config steps for "%s/%s".', prj.name, cfg.name)
				for file, func in pairs(cfg.autoconfigure) do
					func(cfg)

					if not (file ~= "dontWrite") then
						os.mkdir(cfg.objdir)
						local filename = path.join(cfg.objdir, file)
						autoconf.writefile(cfg, filename)
					end
				end
			end
		end

		-- then call the old onProject.
		if onBaseProject then
			onBaseProject(prj)
		end
	end

	-- now call the original action.call methods
	base(name)
end)