summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/genie/src/actions/fastbuild/fastbuild_project.lua
blob: eec65d584f5a8017f7d738b5342b726112effdbd (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
-- Generates a FASTBuild config file for a project.

-- Note that table order iteration should be deterministic, so the .bff file content is not
-- arbitrarily changed each time it's generated. There are several places in this file
-- where sorts are done for that reason.

-- Remaining flags to handle:
--		NoImportLib = 1,
--		NoIncrementalLink = 1,
--		NoManifest = 1,
--		NoPCH = 1,
--		SingleOutputDir = 1,
--		StaticATL = 1,
--		Symbols = 1,
--		Unicode = 1,
--		Unsafe = 1,
--		WinMain = 1,
-- API todo:
--		custombuildtask
--		dependency

local function add_trailing_backslash(dir)
	if dir:len() > 0 and dir:sub(-1) ~= "\\" then
		return dir.."\\"
	end
	return dir
end

local function compile(indentlevel, prj, cfg, commonbasepath)

	local firstflag = true
	for _, cfgexclude in ipairs(cfg.excludes) do
		if path.issourcefile(cfgexclude) then
			if firstflag then
				_p(indentlevel, '// Excluded files:')
				firstflag = false
			end
			_p(indentlevel, ".CompilerInputFiles - '%s'", cfgexclude)
		end
	end
	if not firstflag then
		_p('')
	end

	_p(indentlevel, ".CompilerOutputPath = '%s'", add_trailing_backslash(cfg.objectsdir))

	_p(indentlevel, ".Defines = ''")
	for _, define in ipairs(cfg.defines) do
		_p(indentlevel+1, "+ ' /D%s'", define)
	end

	if cfg.kind == 'SharedLib' then
		_p(indentlevel+1, "+ ' /D_WINDLL'")
	end

	_p(indentlevel, ".IncludeDirs = ''")
	local sortedincdirs = {}
	for _, includedir in ipairs(cfg.userincludedirs) do
		if includedir ~= nil then
			table.insert(sortedincdirs, includedir)
		end
	end
	for _, includedir in ipairs(cfg.includedirs) do
		if includedir ~= nil then
			table.insert(sortedincdirs, includedir)
		end
	end

	-- Setup for special include dir sort to ensure that 'nearby' dirs get precedence over others.
	-- Gets the relative path from commonbasepath and counts the steps in that path.
	local function getpathnodecount(p)
		local nodefinder = string.gmatch(p, "[^\\/]+")
		local result = 0
		local node = nodefinder()
		while node do
			result = result + ((node ~= '.' and 1) or 0)
			node = nodefinder()
		end
		return result
	end

	local stepsfrombase = {}

	for _, includedir in ipairs(sortedincdirs) do
		stepsfrombase[includedir] = getpathnodecount(path.getrelative(commonbasepath, includedir))
	end

	local function includesort(a, b)
		if stepsfrombase[a] == stepsfrombase[b] then
			return a < b
		else
			return stepsfrombase[a] < stepsfrombase[b]
		end
	end

	table.sort(sortedincdirs, includesort)
	for _, includedir in ipairs(sortedincdirs) do
		_p(indentlevel+1, "+ ' /I\"%s\"'", includedir)
	end

	_p(indentlevel+1, "+ .MSVCIncludes")

	local compileroptions = {
		'"%1"',
		'/nologo',
		'/c',
		'/Gy',
		'/Gm-',
		'/Zc:inline',
		'/errorReport:prompt',
		'/FS',
		}

	if cfg.options.ForceCPP then
		table.insert(compileroptions, '/TP')
	end

	if cfg.flags.PedanticWarnings
	or cfg.flags.ExtraWarnings
	then
		table.insert(compileroptions, '/W4')
	end

	if (cfg.flags.NativeWChar == false or
		cfg.flags.NoNativeWChar) then
		table.insert(compileroptions, '/Zc:wchar_t-')
	end

	if (cfg.flags.EnableMinimalRebuild or
		cfg.flags.NoMultiProcessorCompilation) then
		-- Not compatible with FastBuild
	end

	if cfg.flags.FloatFast then
		table.insert(compileroptions, '/fp:fast')
	elseif cfg.flags.FloatStrict then
		table.insert(compileroptions, '/fp:strict')
	else
		table.insert(compileroptions, '/fp:precise')
	end

	if cfg.flags.FastCall then
		table.insert(compileroptions, '/Gr')
	elseif cfg.flags.StdCall then
		table.insert(compileroptions, '/Gd')
	end

	if cfg.flags.UnsignedChar then
		table.insert(compileroptions, '/J')
	end

	if premake.config.isdebugbuild(cfg) then
		if cfg.flags.StaticRuntime then
			table.insert(compileroptions, '/MTd')
		else
			table.insert(compileroptions, '/MDd')
		end
	else
		if cfg.flags.StaticRuntime then
			table.insert(compileroptions, '/MT')
		else
			table.insert(compileroptions, '/MD')
		end
	end

	if cfg.flags.Symbols then
		if (cfg.flags.C7DebugInfo) then
			table.insert(compileroptions, '/Z7')
		else
			if (premake.config.isoptimizedbuild(cfg.flags)
				or cfg.flags.NoEditAndContinue) then
				table.insert(compileroptions, '/Zi')
			else
				table.insert(compileroptions, '/ZI')
			end
			local targetdir = add_trailing_backslash(cfg.buildtarget.directory)
			table.insert(compileroptions, string.format("/Fd\"%s%s.pdb\"", targetdir, cfg.buildtarget.basename))
		end
	end

	local isoptimised = true
	if (cfg.flags.Optimize) then
		table.insert(compileroptions, '/Ox')
	elseif (cfg.flags.OptimizeSize) then
		table.insert(compileroptions, '/O1')
	elseif (cfg.flags.OptimizeSpeed) then
		table.insert(compileroptions, '/O2')
	else
		isoptimised = false
	end

	if isoptimised then
		table.insert(compileroptions, '/GF')
	else
		table.insert(compileroptions, '/Od')
		table.insert(compileroptions, '/RTC1')
	end

	if cfg.flags.FatalWarnings then
		table.insert(compileroptions, '/WX')
	else
		table.insert(compileroptions, '/WX-')
	end

	if cfg.platform == 'x32' then
		if cfg.flags.EnableSSE2 then
			table.insert(compileroptions, '/arch:SSE2')
		elseif cfg.flags.EnableSSE then
			table.insert(compileroptions, '/arch:SSE')
		end
	end

	if cfg.flags.NoExceptions then
	else
		if cfg.flags.SEH then
			table.insert(compileroptions, '/EHa')
		else
			table.insert(compileroptions, '/EHsc')
		end
	end

	if cfg.flags.NoRTTI then
		table.insert(compileroptions, '/GR-')
	else
		table.insert(compileroptions, '/GR')
	end

	if cfg.flags.NoFramePointer then
		table.insert(compileroptions, '/Oy-')
	else
		table.insert(compileroptions, '/Oy')
	end

	for _, addloption in ipairs(cfg.buildoptions) do
		table.insert(compileroptions, addloption)
	end

	_p(indentlevel, ".CompilerOptions = ''")
	for _, option in ipairs(compileroptions) do
		_p(indentlevel+1, "+ ' %s'", option)
	end

	_p(indentlevel+1, "+ .IncludeDirs")
	_p(indentlevel+1, "+ .Defines")

	if not cfg.flags.NoPCH and cfg.pchheader then
		_p(indentlevel, ".CompilerInputFiles - '%s'", cfg.pchsource)
		_p(indentlevel, ".PCHInputFile = '%s'", cfg.pchsource)
		_p(indentlevel, ".PCHOutputFile = .CompilerOutputPath + '%s.pch'", prj.name)
		_p(indentlevel, ".CompilerOptions + ' /Fp\"' + .CompilerOutputPath + '%s.pch\"'", prj.name)
		_p(indentlevel, ".PCHOptions = .CompilerOptions")
		_p(indentlevel+1, "+ ' /Yc\"%s\"'", cfg.pchheader)
		_p(indentlevel+1, "+ ' /Fo\"%%3\"'")
		_p(indentlevel, ".CompilerOptions + ' /Yu\"%s\"'", cfg.pchheader)
	end
	_p(indentlevel+1, "+ ' /Fo\"%%2\"'") -- make sure the previous property is .CompilerOptions
end

local function library(prj, cfg, useconfig, commonbasepath)
	_p(1, "Library('%s-%s-%s')", prj.name, cfg.name, cfg.platform)
	_p(1, '{')

	useconfig(2)
	compile(2, prj, cfg, commonbasepath)

	local librarianoptions = {
		'"%1"',
		'/OUT:"%2"',
		'/NOLOGO',
		}

	if cfg.platform == 'x64' then
		table.insert(librarianoptions, '/MACHINE:X64')
	else
		table.insert(librarianoptions, '/MACHINE:X86')
	end

	_p(2, ".LibrarianOptions = ''")
	for _, option in ipairs(librarianoptions) do
		_p(3, "+ ' %s'", option)
	end

	_p(2, ".LibrarianOutput = '%s'", cfg.buildtarget.fullpath)

	_p(1, '}')
	_p('')
end

local function binary(prj, cfg, useconfig, bintype, commonbasepath)
	_p(1, "ObjectList('%s_obj-%s-%s')", prj.name, cfg.name, cfg.platform)
	_p(1, '{')

	useconfig(2)
	compile(2, prj, cfg, commonbasepath)

	_p(1, '}')
	_p('')

	_p(1, "%s('%s-%s-%s')", bintype, prj.name, cfg.name, cfg.platform)
	_p(1, '{')

	useconfig(2)
	_p(2, '.Libraries = {')
	_p(3, "'%s_obj-%s-%s',", prj.name, cfg.name, cfg.platform) -- Refer to the ObjectList

	local sorteddeplibs = {}
	for _, deplib in ipairs(premake.getlinks(cfg, "dependencies", "basename")) do
		table.insert(sorteddeplibs, string.format("'%s-%s-%s',", deplib, cfg.name, cfg.platform))
	end
	table.sort(sorteddeplibs)
	for _, deplib in ipairs(sorteddeplibs) do
		_p(3, deplib)
	end
	_p(3, '}')

	_p(2, '.LinkerLinkObjects = false')

	local linkeroptions = {
		'"%1"',
		'/OUT:"%2"',
		'/NOLOGO',
		'/errorReport:prompt',
		}

	local subsystemversion = '",5.02"'
	if cfg.platform == 'x32' then
		subsystemversion = '",5.01"'
	end

	if cfg.kind == 'ConsoleApp' then
		table.insert(linkeroptions, '/SUBSYSTEM:CONSOLE'..subsystemversion)
	else
		table.insert(linkeroptions, '/SUBSYSTEM:WINDOWS'..subsystemversion)
	end

	if cfg.kind == 'SharedLib' then
		table.insert(linkeroptions, '/DLL')
	end

	if cfg.platform == 'x64' then
		table.insert(linkeroptions, '/MACHINE:X64')
	else
		table.insert(linkeroptions, '/MACHINE:X86')
	end

	for _, libdir in ipairs(cfg.libdirs) do
		table.insert(linkeroptions, string.format('/LIBPATH:%s', path.translate(libdir, '\\')))
	end

	if not cfg.flags.WinMain and (cfg.kind == 'ConsoleApp' or cfg.kind == 'WindowedApp') then
		if cfg.flags.Unicode then
			table.insert(linkeroptions, '/ENTRY:wmainCRTStartup')
		else
			table.insert(linkeroptions, '/ENTRY:mainCRTStartup')
		end
	end

	local deffile = premake.findfile(cfg, ".def")
	if deffile then
		table.insert(linkeroptions, '/DEF:%s', deffile)
	end

	if (cfg.flags.Symbols ~= nil) then
		table.insert(linkeroptions, '/DEBUG')
	end

	if premake.config.isoptimizedbuild(cfg.flags) then
		table.insert(linkeroptions, '/OPT:REF')
		table.insert(linkeroptions, '/OPT:ICF')
	end

	table.insert(linkeroptions, '/INCREMENTAL'..((premake.config.isincrementallink(cfg) and '') or ':NO'))

	for _, addloption in ipairs(cfg.linkoptions) do
		table.insert(linkeroptions, addloption)
	end

	local linklibs = premake.getlinks(cfg, "all", "fullpath")

	for _, linklib in ipairs(linklibs) do
		table.insert(linkeroptions, path.getname(linklib))
	end

	table.sort(linkeroptions)

	_p(2, ".LinkerOptions = ''")
	for _, option in ipairs(linkeroptions) do
		_p(3, "+ ' %s'", option)
	end
	_p(3, "+ .MSVCLibPaths")

	_p(2, ".LinkerOutput = '%s'", cfg.buildtarget.fullpath)

	_p(1, '}')
	_p('')
end

local function executable(prj, cfg, useconfig, commonbasepath)
	binary(prj, cfg, useconfig, 'Executable', commonbasepath)
end

local function dll(prj, cfg, useconfig, commonbasepath)
	binary(prj, cfg, useconfig, 'DLL', commonbasepath)
end

local function alias(prj, cfg, target)
	_p(1, "Alias('%s-%s-%s')", prj.name, cfg.name, cfg.platform)
	_p(1, '{')
	_p(2, ".Targets = '%s'", target)
	_p(1, '}')
	_p('')
end

function premake.fastbuild.project(prj)
	io.indent = '    '
	_p('// FASTBuild project configuration file autogenerated by GENie.')
	_p('')

	_p('{')

	local cppfiles = {}
	for file in premake.project.eachfile(prj) do
		if path.issourcefile(file.name) then
			table.insert(cppfiles, file.name)
		end
	end

	local commonbasepath = cppfiles[1]
	for _, file in ipairs(cppfiles) do
		commonbasepath = path.getcommonbasedir(commonbasepath..'/', file)
	end

	_p(1, ".CompilerInputFilesRoot = '%s/'", commonbasepath)
	_p(1, '.CompilerInputFiles = {')
	for _, file in ipairs(cppfiles) do
		_p(2, "'%s',", file)
	end
	_p(1, '}')
	_p('')

	local targetkindmap = {
		ConsoleApp = executable,
		WindowedApp = executable,
		SharedLib = dll,
		StaticLib = library,
		}

	local useconfigmap = {
		x32 = function(indentlevel)
				_p(indentlevel, 'Using(.MSVCx86Config)')
				_p('')
			end,
		x64 = function(indentlevel)
				_p(indentlevel, 'Using(.MSVCx64Config)')
				_p('')
			end,
		}

	local nativeplatform = (os.is64bit() and 'x64') or 'x32'

	for _, platform in ipairs(prj.solution.platforms) do
		for cfg in premake.eachconfig(prj, platform) do
			if cfg.platform == 'Native' then
				alias(prj, cfg, string.format('%s-%s-%s', prj.name, cfg.name, nativeplatform))
			else
				targetkindmap[prj.kind](prj, cfg, useconfigmap[cfg.platform], commonbasepath)
			end
		end
	end
	_p('}')

end