forked from Teelevision/zcatch
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bam.lua
547 lines (454 loc) · 17.2 KB
/
bam.lua
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
CheckVersion("0.5")
Import("configure.lua")
Import("other/sdl/sdl.lua")
Import("other/freetype/freetype.lua")
--- Setup Config -------
config = NewConfig()
config:Add(OptCCompiler("compiler"))
config:Add(OptTestCompileC("stackprotector", "int main(){return 0;}", "-fstack-protector -fstack-protector-all"))
config:Add(OptTestCompileC("minmacosxsdk", "int main(){return 0;}", "-mmacosx-version-min=10.7 -isysroot /Developer/SDKs/MacOSX10.7.sdk"))
config:Add(OptTestCompileC("buildwithoutsseflag", "#include <immintrin.h>\nint main(){_mm_pause();return 0;}", ""))
config:Add(OptLibrary("zlib", "zlib.h", false))
config:Add(SDL.OptFind("sdl", true))
config:Add(FreeType.OptFind("freetype", true))
config:Finalize("config.lua")
generated_src_dir = "build/src"
generated_icon_dir = "build/icons"
builddir = "build/%(arch)s/%(conf)s"
content_src_dir = "datasrc/"
python_in_path = ExecuteSilent("python -V") == 0
-- data compiler
function Python(name)
if family == "windows" then
name = str_replace(name, "/", "\\")
if not python_in_path then
-- Python is usually registered for .py files in Windows
return name
end
end
return "python " .. name
end
function CHash(output, ...)
local inputs = TableFlatten({...})
output = PathJoin(generated_src_dir, Path(output))
-- compile all the files
local cmd = Python("scripts/cmd5.py") .. " "
for index, inname in ipairs(inputs) do
cmd = cmd .. Path(inname) .. " "
end
cmd = cmd .. " > " .. output
AddJob(output, "cmd5 " .. output, cmd)
for index, inname in ipairs(inputs) do
AddDependency(output, inname)
end
AddDependency(output, "scripts/cmd5.py")
return output
end
function ResCompile(scriptfile, compiler)
scriptfile = Path(scriptfile)
local output = nil
if compiler == "cl" then
output = PathJoin(generated_icon_dir, PathBase(PathFilename(scriptfile)) .. ".res")
AddJob(output, "rc " .. scriptfile, "rc /fo " .. output .. " " .. scriptfile)
elseif compiler == "gcc" or compiler == "clang" then
output = PathJoin(generated_icon_dir, PathBase(PathFilename(scriptfile)) .. ".coff")
AddJob(output, "windres " .. scriptfile, "windres -i " .. scriptfile .. " -o " .. output)
end
AddDependency(output, scriptfile)
return output
end
function ContentCompile(action, output)
output = PathJoin(generated_src_dir, Path(output))
AddJob(
output,
action .. " > " .. output,
Python("datasrc/compile.py") .. " " .. action .. " > " .. output
)
AddDependency(output, "datasrc/compile.py")
AddDependency("datasrc/compile.py", "datasrc/content.py", "datasrc/network.py", "datasrc/datatypes.py")
return output
end
function GenerateCommonSettings(settings, conf, arch, compiler)
if compiler == "gcc" or compiler == "clang" then
settings.cc.flags:Add("-Wall", "-fno-exceptions")
end
-- Compile zlib if needed
local zlib = nil
if config.zlib.value == 1 then
settings.link.libs:Add("z")
if config.zlib.include_path then
settings.cc.includes:Add(config.zlib.include_path)
end
else
settings.cc.includes:Add("src/engine/external/zlib")
zlib = Compile(settings, Collect("src/engine/external/zlib/*.c"))
end
local md5 = Compile(settings, Collect("src/engine/external/md5/*.c"))
local wavpack = Compile(settings, Collect("src/engine/external/wavpack/*.c"))
local png = Compile(settings, Collect("src/engine/external/pnglite/*.c"))
local json = Compile(settings, Collect("src/engine/external/json-parser/*.c"))
-- globally available libs
libs = {zlib=zlib, wavpack=wavpack, png=png, md5=md5, json=json}
end
function GenerateMacOSXSettings(settings, conf, arch, compiler)
if arch == "x86" then
settings.cc.flags:Add("-arch i386")
settings.link.flags:Add("-arch i386")
elseif arch == "x86_64" then
settings.cc.flags:Add("-arch x86_64")
settings.link.flags:Add("-arch x86_64")
elseif arch == "ppc" then
settings.cc.flags:Add("-arch ppc")
settings.link.flags:Add("-arch ppc")
elseif arch == "ppc64" then
settings.cc.flags:Add("-arch ppc64")
settings.link.flags:Add("-arch ppc64")
else
print("Unknown Architecture '" .. arch .. "'. Supported: x86, x86_64, ppc, ppc64")
os.exit(1)
end
-- c++ stdlib needed
settings.cc.flags:Add("--stdlib=libc++")
settings.link.flags:Add("--stdlib=libc++")
-- this also needs the macOS min SDK version to be at least 10.7
settings.cc.flags:Add("-mmacosx-version-min=10.7")
settings.link.flags:Add("-mmacosx-version-min=10.7")
if config.minmacosxsdk.value == 1 then
settings.cc.flags:Add("-isysroot /Developer/SDKs/MacOSX10.7.sdk")
settings.link.flags:Add("-isysroot /Developer/SDKs/MacOSX10.7.sdk")
end
settings.link.frameworks:Add("Carbon")
settings.link.frameworks:Add("AppKit")
GenerateCommonSettings(settings, conf, arch, compiler)
-- Build server launcher before adding game stuff
local serverlaunch = Link(settings, "serverlaunch", Compile(settings, "src/osxlaunch/server.m"))
-- Master server, version server and tools
BuildEngineCommon(settings)
BuildMasterserver(settings)
BuildVersionserver(settings)
BuildTools(settings)
-- Add requirements for Server & Client
BuildGameCommon(settings)
-- Server
settings.link.frameworks:Add("Cocoa")
local server_exe = BuildServer(settings)
AddDependency(server_exe, serverlaunch)
-- Client
settings.link.frameworks:Add("OpenGL")
settings.link.frameworks:Add("AGL")
-- FIXME: the SDL config is applied in BuildClient too but is needed here before so the launcher will compile
config.sdl:Apply(settings)
settings.link.extrafiles:Merge(Compile(settings, "src/osxlaunch/client.m"))
BuildClient(settings)
-- Content
BuildContent(settings, arch, conf)
end
function GenerateLinuxSettings(settings, conf, arch, compiler)
if arch == "x86" then
if config.buildwithoutsseflag.value == false then
settings.cc.flags:Add("-msse2") -- for the _mm_pause call
end
settings.cc.flags:Add("-m32")
settings.link.flags:Add("-m32")
elseif arch == "x86_64" then
settings.cc.flags:Add("-m64")
settings.link.flags:Add("-m64")
elseif arch == "armv7l" then
-- arm 32 bit
else
print("Unknown Architecture '" .. arch .. "'. Supported: x86, x86_64")
os.exit(1)
end
settings.link.libs:Add("pthread")
GenerateCommonSettings(settings, conf, arch, compiler)
-- Master server, version server and tools
BuildEngineCommon(settings)
BuildTools(settings)
BuildMasterserver(settings)
BuildVersionserver(settings)
-- Add requirements for Server & Client
BuildGameCommon(settings)
-- Server
BuildServer(settings)
-- Client
settings.link.libs:Add("X11")
settings.link.libs:Add("GL")
settings.link.libs:Add("GLU")
BuildClient(settings)
-- Content
BuildContent(settings, arch, conf)
end
function GenerateSolarisSettings(settings, conf, arch, compiler)
settings.link.libs:Add("socket")
settings.link.libs:Add("nsl")
GenerateLinuxSettings(settings, conf, arch, compiler)
end
function GenerateWindowsSettings(settings, conf, target_arch, compiler)
if compiler == "cl" then
if (target_arch == "x86" and arch ~= "ia32") or
(target_arch == "x86_64" and arch ~= "ia64" and arch ~= "amd64") then
print("Cross compiling is unsupported on Windows.")
os.exit(1)
end
settings.cc.flags:Add("/wd4244", "/wd4577")
elseif compiler == "gcc" or config.compiler.driver == "clang" then
if target_arch ~= "x86" and target_arch ~= "x86_64" then
print("Unknown Architecture '" .. arch .. "'. Supported: x86, x86_64")
os.exit(1)
end
-- disable visibility attribute support for gcc on windows
settings.cc.defines:Add("NO_VIZ")
settings.cc.defines:Add("_WIN32_WINNT=0x0501")
end
local icons = SharedIcons(compiler)
local manifests = SharedManifests(compiler)
-- Required libs
settings.link.libs:Add("gdi32")
settings.link.libs:Add("user32")
settings.link.libs:Add("ws2_32")
settings.link.libs:Add("ole32")
settings.link.libs:Add("shell32")
settings.link.libs:Add("advapi32")
GenerateCommonSettings(settings, conf, target_arch, compiler)
-- Master server, version server and tools
BuildEngineCommon(settings)
BuildMasterserver(settings)
BuildVersionserver(settings)
BuildTools(settings)
-- Add requirements for Server & Client
BuildGameCommon(settings)
-- Server
local server_settings = settings:Copy()
server_settings.link.extrafiles:Add(icons.server)
BuildServer(server_settings)
-- Client
settings.link.extrafiles:Add(icons.client)
settings.link.extrafiles:Add(manifests.client)
settings.link.libs:Add("opengl32")
settings.link.libs:Add("glu32")
settings.link.libs:Add("winmm")
BuildClient(settings)
-- Content
BuildContent(settings, target_arch, conf)
end
function SharedCommonFiles()
-- Shared game files, generate only once
if not shared_common_files then
local network_source = ContentCompile("network_source", "generated/protocol.cpp")
local network_header = ContentCompile("network_header", "generated/protocol.h")
AddDependency(network_source, network_header, "src/engine/shared/protocol.h")
local nethash = CHash("generated/nethash.cpp", "src/engine/shared/protocol.h", "src/game/tuning.h", "src/game/gamecore.cpp", network_header)
shared_common_files = {network_source, nethash}
end
return shared_common_files
end
function SharedServerFiles()
-- Shared server files, generate only once
if not shared_server_files then
local server_content_source = ContentCompile("server_content_source", "generated/server_data.cpp")
local server_content_header = ContentCompile("server_content_header", "generated/server_data.h")
AddDependency(server_content_source, server_content_header)
shared_server_files = {server_content_source}
end
return shared_server_files
end
function SharedClientFiles()
-- Shared client files, generate only once
if not shared_client_files then
local client_content_source = ContentCompile("client_content_source", "generated/client_data.cpp")
local client_content_header = ContentCompile("client_content_header", "generated/client_data.h")
AddDependency(client_content_source, client_content_header)
shared_client_files = {client_content_source}
end
return shared_client_files
end
shared_icons = {}
function SharedIcons(compiler)
if not shared_icons[compiler] then
local server_icon = ResCompile("other/icons/teeworlds_srv_" .. compiler .. ".rc", compiler)
local client_icon = ResCompile("other/icons/teeworlds_" .. compiler .. ".rc", compiler)
shared_icons[compiler] = {server=server_icon, client=client_icon}
end
return shared_icons[compiler]
end
function SharedManifests(compiler)
if not shared_manifests then
local client_manifest = ResCompile("other/manifest/teeworlds.rc", compiler)
shared_manifests = {client=client_manifest}
end
return shared_manifests
end
function BuildEngineCommon(settings)
settings.link.extrafiles:Merge(Compile(settings, Collect("src/engine/shared/*.cpp", "src/base/*.c")))
end
function BuildGameCommon(settings)
settings.link.extrafiles:Merge(Compile(settings, Collect("src/game/*.cpp"), SharedCommonFiles()))
end
function BuildClient(settings, family, platform)
config.sdl:Apply(settings)
config.freetype:Apply(settings)
local client = Compile(settings, Collect("src/engine/client/*.cpp"))
local game_client = Compile(settings, CollectRecursive("src/game/client/*.cpp"), SharedClientFiles())
local game_editor = Compile(settings, Collect("src/game/editor/*.cpp"))
Link(settings, "teeworlds", libs["zlib"], libs["md5"], libs["wavpack"], libs["png"], libs["json"], client, game_client, game_editor)
end
function BuildServer(settings, family, platform)
local server = Compile(settings, Collect("src/engine/server/*.cpp"))
local game_server = Compile(settings, CollectRecursive("src/game/server/*.cpp"), SharedServerFiles())
return Link(settings, "teeworlds_srv", libs["zlib"], libs["md5"], server, game_server)
end
function BuildTools(settings)
local tools = {}
for i,v in ipairs(Collect("src/tools/*.cpp", "src/tools/*.c")) do
local toolname = PathFilename(PathBase(v))
tools[i] = Link(settings, toolname, Compile(settings, v), libs["zlib"], libs["md5"], libs["wavpack"], libs["png"])
end
PseudoTarget(settings.link.Output(settings, "pseudo_tools") .. settings.link.extension, tools)
end
function BuildMasterserver(settings)
return Link(settings, "mastersrv", Compile(settings, Collect("src/mastersrv/*.cpp")), libs["zlib"], libs["md5"])
end
function BuildVersionserver(settings)
return Link(settings, "versionsrv", Compile(settings, Collect("src/versionsrv/*.cpp")), libs["zlib"], libs["md5"])
end
function BuildContent(settings, arch, conf)
local content = {}
table.insert(content, CopyToDir(settings.link.Output(settings, "data"), CollectRecursive(content_src_dir .. "*.png", content_src_dir .. "*.wv", content_src_dir .. "*.ttf", content_src_dir .. "*.txt", content_src_dir .. "*.map", content_src_dir .. "*.rules", content_src_dir .. "*.json")))
if family == "windows" then
if arch == "x86_64" then
_arch = "64"
else
_arch = "32"
end
-- dependencies
dl = Python("scripts/download.py")
AddJob({
"other/freetype/include/ft2build.h", "other/freetype/windows/lib" .. _arch .. "/freetype.dll",
"other/sdl/include/SDL.h", "other/sdl/windows/lib" .. _arch .. "/SDL2.dll"
}, "Downloading freetype and SDL2", dl .. " freetype sdl"
)
table.insert(content, CopyFile(settings.link.Output(settings, "") .. "/SDL2.dll", "other/sdl/windows/lib" .. _arch .. "/SDL2.dll"))
table.insert(content, CopyFile(settings.link.Output(settings, "") .. "/freetype.dll", "other/freetype/windows/lib" .. _arch .. "/freetype.dll"))
AddDependency(settings.link.Output(settings, "") .. "/SDL2.dll", "other/sdl/include/SDL.h")
AddDependency(settings.link.Output(settings, "") .. "/freetype.dll", "other/freetype/include/ft2build.h")
end
PseudoTarget(settings.link.Output(settings, "content") .. settings.link.extension, content)
end
-- create all targets for specified configuration & architecture
function GenerateSettings(conf, arch, builddir, compiler)
local settings = NewSettings()
-- Set compiler if explicitly requested
if compiler == "gcc" then
SetDriversGCC(settings)
elseif compiler == "clang" then
SetDriversClang(settings)
elseif compiler == "cl" then
SetDriversCL(settings)
else
-- apply compiler settings
config.compiler:Apply(settings)
compiler = config.compiler.driver
end
if conf == "debug" then
settings.debug = 1
settings.optimize = 0
settings.cc.defines:Add("CONF_DEBUG")
else
settings.debug = 0
settings.optimize = 1
settings.cc.defines:Add("CONF_RELEASE")
end
-- Generate object files in {builddir}/objs/
settings.cc.Output = function (settings_, input)
-- strip
input = input:gsub("^src/", "")
input = input:gsub("^" .. generated_src_dir .. "/", "")
return PathJoin(PathJoin(builddir, "objs"), PathBase(input))
end
-- Build output files in {builddir}
settings.link.Output = function (settings_, input)
return PathJoin(builddir, PathBase(input) .. settings_.config_ext)
end
settings.cc.includes:Add("src")
settings.cc.includes:Add("src/engine/external/pnglite")
settings.cc.includes:Add("src/engine/external/wavpack")
settings.cc.includes:Add(generated_src_dir)
if family == "windows" then
GenerateWindowsSettings(settings, conf, arch, compiler)
elseif family == "unix" then
if platform == "macosx" then
GenerateMacOSXSettings(settings, conf, arch, compiler)
elseif platform == "solaris" then
GenerateSolarisSettings(settings, conf, arch, compiler)
else -- Linux, BSD
GenerateLinuxSettings(settings, conf, arch, compiler)
end
end
return settings
end
-- String formatting with named parameters, by RiciLake http://lua-users.org/wiki/StringInterpolation
function interp(s, tab)
return (s:gsub('%%%((%a%w*)%)([-0-9%.]*[cdeEfgGiouxXsq])',
function(k, fmt)
return tab[k] and ("%"..fmt):format(tab[k]) or '%('..k..')'..fmt
end))
end
function CopyToDir(dst, ...)
local output = {}
for filename in TableWalk({...}) do
table.insert(output, CopyFile(PathJoin(dst, string.sub(filename, string.len(content_src_dir)+1)), filename))
end
return output
end
function split(str, sep)
local vals = {}
str:gsub("([^,]+)", function(val) table.insert(vals, val) end)
return vals
end
-- Supported archtitectures: x86, amd64, ppc, ppc64
if ScriptArgs['arch'] then
archs = split(ScriptArgs['arch'])
else
if arch == "ia32" then
archs = {"x86"}
elseif arch == "ia64" or arch == "amd64" then
archs = {"x86_64"}
else
archs = {arch}
end
end
if ScriptArgs['conf'] then
confs = split(ScriptArgs['conf'])
else
confs = {"debug"}
end
if ScriptArgs['compiler'] then
compiler = ScriptArgs['compiler']
else
compiler = nil
end
if ScriptArgs['builddir'] then
builddir = ScriptArgs['builddir']
end
targets = {client="teeworlds", server="teeworlds_srv",
versionserver="versionsrv", masterserver="mastersrv",
tools="pseudo_tools", content="content"}
subtargets = {}
for t, cur_target in pairs(targets) do
subtargets[cur_target] = {}
end
for a, cur_arch in ipairs(archs) do
for c, cur_conf in ipairs(confs) do
cur_builddir = interp(builddir, {platform=family, arch=cur_arch, target=cur_target, conf=cur_conf, compiler=compiler})
local settings = GenerateSettings(cur_conf, cur_arch, cur_builddir, compiler)
for t, cur_target in pairs(targets) do
table.insert(subtargets[cur_target], PathJoin(cur_builddir, cur_target .. settings.link.extension))
end
end
end
for cur_name, cur_target in pairs(targets) do
-- Supertarget for all configurations and architectures of that target
PseudoTarget(cur_name, subtargets[cur_target])
end
PseudoTarget("game", "client", "server", "content")
DefaultTarget("game")