File: premake5.lua

package info (click to toggle)
0ad 0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 171,928 kB
  • sloc: cpp: 194,011; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,695; perl: 1,575; java: 533; xml: 415; php: 192; makefile: 99
file content (1576 lines) | stat: -rw-r--r-- 46,751 bytes parent folder | download
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
newoption { trigger = "android", description = "Use non-working Android cross-compiling mode" }
newoption { trigger = "coverage", description = "Enable code coverage data collection (GCC only)" }
newoption { trigger = "gles", description = "Use non-working OpenGL ES 2.0 mode" }
newoption { trigger = "icc", description = "Use Intel C++ Compiler (Linux only; should use either \"--cc icc\" or --without-pch too, and then set CXX=icpc before calling make)" }
newoption { trigger = "jenkins-tests", description = "Configure CxxTest to use the XmlPrinter runner which produces Jenkins-compatible output" }
newoption { trigger = "minimal-flags", description = "Only set compiler/linker flags that are really needed. Has no effect on Windows builds" }
newoption { trigger = "outpath", description = "Location for generated project files", default="../workspaces/default" }
newoption { trigger = "with-system-cxxtest", description = "Search standard paths for cxxtest, instead of using bundled copy" }
newoption { trigger = "with-lto", description = "Enable Link Time Optimization (LTO)" }
newoption { trigger = "with-system-mozjs", description = "Search standard paths for libmozjs115, instead of using bundled copy" }
newoption { trigger = "with-system-nvtt", description = "Search standard paths for nvidia-texture-tools library, instead of using bundled copy" }
newoption { trigger = "with-valgrind", description = "Enable Valgrind support (non-Windows only)" }
newoption { trigger = "without-audio", description = "Disable use of OpenAL/Ogg/Vorbis APIs" }
newoption { trigger = "without-atlas", description = "Disable Atlas scenario/map editor and ActorEditor" }
newoption { trigger = "without-lobby", description = "Disable the use of gloox and the multiplayer lobby" }
newoption { trigger = "without-miniupnpc", description = "Disable use of miniupnpc for port forwarding" }
newoption { trigger = "without-nvtt", description = "Disable use of NVTT" }
newoption { trigger = "without-pch", description = "Disable generation and usage of precompiled headers" }
newoption { trigger = "without-tests", description = "Disable generation of test projects" }

-- OS X specific options
newoption { trigger = "macosx-version-min", description = "Set minimum required version of the OS X API, the build will possibly fail if an older SDK is used, while newer API functions will be weakly linked (i.e. resolved at runtime)" }
newoption { trigger = "sysroot", description = "Set compiler system root path, used for building against a non-system SDK. For example /usr/local becomes SYSROOT/user/local" }

-- Install options
newoption { trigger = "bindir", description = "Directory for executables (typically '/usr/games'); default is to be relocatable" }
newoption { trigger = "datadir", description = "Directory for data files (typically '/usr/share/games/0ad'); default is ../data/ relative to executable" }
newoption { trigger = "libdir", description = "Directory for libraries (typically '/usr/lib/games/0ad'); default is ./ relative to executable" }

if _ACTION == "gmake" then
	print("Premake action 'gmake' is no longer supported by pyrogenesis, use 'gmake2'")
	print("Example: 'premake5 --file=build/premake/premake5.lua gmake2'")
	os.exit(1)
end

-- On Windows check if wxWidgets is available, if not disable atlas and emit warning.
-- This is because there are currently not prebuilt binaries provided.
if not _OPTIONS["without-atlas"] and os.istarget("windows") then
	if not os.isfile("../../libraries/win32/wxwidgets/include/wx/wx.h") then
		print("wxWidgets not found, disableing atlas")
		_OPTIONS["without-atlas"] = ""
	end
end

-- Root directory of project checkout relative to this .lua file
rootdir = "../.."

dofile("extern_libs5.lua")

-- detect compiler for non-Windows
if os.istarget("macosx") then
	cc = "clang"
elseif os.istarget("linux") and _OPTIONS["icc"] then
	cc = "icc"
elseif os.istarget("bsd") and os.getversion().description == "FreeBSD" then
	cc = "clang"
elseif not os.istarget("windows") then
	cc = os.getenv("CC")
	if cc == nil or cc == "" then
		local hasgcc = os.execute("which gcc > .gccpath")
		local f = io.open(".gccpath", "r")
		local gccpath = f:read("*line")
		f:close()
		os.execute("rm .gccpath")
		if gccpath == nil then
			cc = "clang"
		else
			cc = "gcc"
		end
	end
end

-- detect CPU architecture (simplistic)
-- The user can target an architecture with HOSTTYPE, but the game still selects some know value.
arch = "x86"
macos_arch = "x86_64"

if _OPTIONS["android"] then
	arch = "arm"
elseif os.istarget("windows") then
	if os.getenv("HOSTTYPE") then
		arch = os.getenv("HOSTTYPE")
	elseif os.getenv("PROCESSOR_ARCHITECTURE") == "amd64" or os.getenv("PROCESSOR_ARCHITEW6432") == "amd64" then
		arch = "amd64"
	end
else
	local machine = "x86_64"
	if os.getenv("HOSTTYPE") and os.getenv("HOSTTYPE") ~= '' then
		machine = os.getenv("HOSTTYPE")
	else
		os.execute(cc .. " -dumpmachine > .gccmachine.tmp")
		local f = io.open(".gccmachine.tmp", "r")
		machine = f:read("*line")
		f:close()
	end
	-- Special handling on mac os where xcode needs special flags.
	-- TODO: We should look into "universal" macOS compilation.
	if os.istarget("macosx") then
		if string.find(machine, "arm64") then
			arch = "aarch64"
			macos_arch = "arm64"
		else
			arch = "amd64"
			macos_arch = "x86_64"
		end
	elseif string.find(machine, "x86_64") == 1 or string.find(machine, "amd64") == 1 then
		arch = "amd64"
	elseif string.find(machine, "i.86") == 1 then
		arch = "x86"
	elseif string.find(machine, "arm") == 1 then
		arch = "arm"
	elseif string.find(machine, "aarch64") == 1 then
		arch = "aarch64"
	elseif string.find(machine, "e2k") == 1 then
		arch = "e2k"
	elseif string.find(machine, "ppc64") == 1 or string.find(machine, "powerpc64") == 1 then
		arch = "ppc64"
	else
		print("WARNING: Cannot determine architecture from GCC, assuming x86")
	end
end

-- Test whether we need to link libexecinfo.
-- This is mostly the case on musl systems, as well as on BSD systems : only glibc provides the
-- backtrace symbols we require in the libc, for other libcs we use the libexecinfo library.
local link_execinfo = false
if os.istarget("bsd") then
	link_execinfo = true
elseif os.istarget("linux") then
	local _, link_errorCode = os.outputof(cc .. " ./tests/execinfo.c -o /dev/null")
	if link_errorCode ~= 0 then
		link_execinfo = true
	end
end

-- Test whether system mozjs is built with --enable-debug
-- The pc file doesn't specify the required -DDEBUG needed in that case
local mozjs_is_debug_build = false
if _OPTIONS["with-system-mozjs"] then
	local _, errorCode = os.outputof(cc .. " $(pkg-config mozjs-115 --cflags) ./tests/mozdebug.c -o /dev/null")
	if errorCode ~= 0 then
		mozjs_is_debug_build = true
	end
end

-- Set up the Workspace
workspace "pyrogenesis"
targetdir(rootdir.."/binaries/system")
libdirs(rootdir.."/binaries/system")
location(_OPTIONS["outpath"])
configurations { "Release", "Debug" }

source_root = rootdir.."/source/" -- default for most projects - overridden by local in others

-- Rationale: projects should not have any additional include paths except for
-- those required by external libraries. Instead, we should always write the
-- full relative path, e.g. #include "maths/Vector3d.h". This avoids confusion
-- ("which file is meant?") and avoids enormous include path lists.


-- projects: engine static libs, main exe, atlas, atlas frontends, test.

--------------------------------------------------------------------------------
-- project helper functions
--------------------------------------------------------------------------------

function project_set_target(project_name)

	-- Note: On Windows, ".exe" is added on the end, on unices the name is used directly

	local obj_dir_prefix = _OPTIONS["outpath"].."/obj/"..project_name.."_"

	filter "Debug"
		objdir(obj_dir_prefix.."Debug")
		targetsuffix("_dbg")

	filter "Release"
		objdir(obj_dir_prefix.."Release")

	filter { }

end


function project_set_build_flags()

	editandcontinue "Off"

	if not _OPTIONS["minimal-flags"] then
		symbols "On"
	end

	if cc ~= "icc" and (os.istarget("windows") or not _OPTIONS["minimal-flags"]) then
		-- adds the -Wall compiler flag
		warnings "Extra" -- this causes far too many warnings/remarks on ICC
	end

	-- disable Windows debug heap, since it makes malloc/free hugely slower when
	-- running inside a debugger
	if os.istarget("windows") then
		debugenvs { "_NO_DEBUG_HEAP=1" }
	end

	filter { "Debug", "action:vs*" }
		defines { "DEBUG" }

	filter "Release"
		if os.istarget("windows") or not _OPTIONS["minimal-flags"] then
			optimize "Speed"
		end
		if _OPTIONS["with-lto"] then
			flags { "LinkTimeOptimization" }
		end
		defines { "NDEBUG", "CONFIG_FINAL=1" }

	filter { }

	if mozjs_is_debug_build then
		defines "DEBUG"
	end

	if _OPTIONS["gles"] then
		defines { "CONFIG2_GLES=1" }
	end

	if _OPTIONS["without-audio"] then
		defines { "CONFIG2_AUDIO=0" }
	end

	if _OPTIONS["without-nvtt"] then
		defines { "CONFIG2_NVTT=0" }
	end

	if _OPTIONS["without-lobby"] then
		defines { "CONFIG2_LOBBY=0" }
	end

	if _OPTIONS["without-miniupnpc"] then
		defines { "CONFIG2_MINIUPNPC=0" }
	end

	-- Enable C++17 standard.
	filter "action:vs*"
		buildoptions { "/std:c++17" }
	filter "action:not vs*"
		buildoptions { "-std=c++17" }
	filter {}

	-- various platform-specific build flags
	if os.istarget("windows") then

		flags { "MultiProcessorCompile" }

		-- Since KB4088875 Windows 7 has a soft requirement for SSE2.
		-- Windows 8+ and Firefox ESR52 make it hard requirement.
		-- Finally since VS2012 it's enabled implicitely when not set.
		vectorextensions "SSE2"

		-- use native wchar_t type (not typedef to unsigned short)
		nativewchar "on"

	else	-- *nix

		-- TODO, FIXME: This check is incorrect because it means that some additional flags will be added inside the "else" branch if the
		-- compiler is ICC and minimal-flags is specified (ticket: #2994)
		if cc == "icc" and not _OPTIONS["minimal-flags"] then
			buildoptions {
				"-w1",
				-- "-Wabi",
				-- "-Wp64", -- complains about OBJECT_TO_JSVAL which is annoying
				"-Wpointer-arith",
				"-Wreturn-type",
				-- "-Wshadow",
				"-Wuninitialized",
				"-Wunknown-pragmas",
				"-Wunused-function",
				"-wd1292" -- avoid lots of 'attribute "__nonnull__" ignored'
			}
			filter "Debug"
				buildoptions { "-O0" } -- ICC defaults to -O2
			filter { }
			if os.istarget("macosx") then
				linkoptions { "-multiply_defined","suppress" }
			end
		else
			-- exclude most non-essential build options for minimal-flags
			if not _OPTIONS["minimal-flags"] then
				buildoptions {
					-- enable most of the standard warnings
					"-Wno-switch",		-- enumeration value not handled in switch (this is sometimes useful, but results in lots of noise)
					"-Wno-reorder",		-- order of initialization list in constructors (lots of noise)
					"-Wno-invalid-offsetof",	-- offsetof on non-POD types (see comment in renderer/PatchRData.cpp)

					"-Wextra",
					"-Wno-missing-field-initializers",	-- (this is common in external headers we can't fix)

					-- add some other useful warnings that need to be enabled explicitly
					"-Wunused-parameter",
					"-Wredundant-decls",	-- (useful for finding some multiply-included header files)
					-- "-Wformat=2",		-- (useful sometimes, but a bit noisy, so skip it by default)
					-- "-Wcast-qual",		-- (useful for checking const-correctness, but a bit noisy, so skip it by default)
					"-Wnon-virtual-dtor",	-- (sometimes noisy but finds real bugs)
					"-Wundef",				-- (useful for finding macro name typos)

					-- enable security features (stack checking etc) that shouldn't have
					-- a significant effect on performance and can catch bugs
					"-fstack-protector-all",

					-- always enable strict aliasing (useful in debug builds because of the warnings)
					"-fstrict-aliasing",

					-- don't omit frame pointers (for now), because performance will be impacted
					-- negatively by the way this breaks profilers more than it will be impacted
					-- positively by the optimisation
					"-fno-omit-frame-pointer"
				}

				filter { "Release" }
					buildoptions {
						-- FORTIFY_SOURCE needs optimizations to be enabled
						"-U_FORTIFY_SOURCE",	-- (avoid redefinition warning if already defined)
						"-D_FORTIFY_SOURCE=2",
					}
				filter {}

				if not _OPTIONS["without-pch"] then
					buildoptions {
						-- do something (?) so that ccache can handle compilation with PCH enabled
						-- (ccache 3.1+ also requires CCACHE_SLOPPINESS=time_macros for this to work)
						"-fpch-preprocess"
					}
				end

				if os.istarget("linux") or os.istarget("bsd") then
					buildoptions { "-fPIC" }
					linkoptions { "-Wl,--no-undefined", "-Wl,--as-needed", "-Wl,-z,relro" }
				end

				if arch == "x86" then
					buildoptions {
						-- To support intrinsics like __sync_bool_compare_and_swap on x86
						-- we need to set -march to something that supports them (i686).
						-- We use pentium3 to also enable other features like mmx and sse,
						-- while tuning for generic to have good performance on every
						-- supported CPU.
						-- Note that all these features are already supported on amd64.
						"-march=pentium3 -mtune=generic",
						-- This allows x86 operating systems to handle the 2GB+ public mod.
						"-D_FILE_OFFSET_BITS=64"
					}
				end
			end

			if arch == "arm" then
				-- disable warnings about va_list ABI change and use
				-- compile-time flags for futher configuration.
				buildoptions { "-Wno-psabi" }
				if _OPTIONS["android"] then
					-- Android uses softfp, so we should too.
					buildoptions { "-mfloat-abi=softfp" }
				end
			end

			if _OPTIONS["coverage"] then
				buildoptions { "-fprofile-arcs", "-ftest-coverage" }
				links { "gcov" }
			end

			-- MacOS 10.12 only supports intel processors with SSE 4.1, so enable that.
			if os.istarget("macosx") and arch == "amd64" then
				buildoptions { "-msse4.1" }
			end

			-- Check if SDK path should be used
			if _OPTIONS["sysroot"] then
				buildoptions { "-isysroot " .. _OPTIONS["sysroot"] }
				linkoptions { "-Wl,-syslibroot," .. _OPTIONS["sysroot"] }
			end

			-- On OS X, sometimes we need to specify the minimum API version to use
			if _OPTIONS["macosx-version-min"] then
				buildoptions { "-mmacosx-version-min=" .. _OPTIONS["macosx-version-min"] }
				-- clang and llvm-gcc look at mmacosx-version-min to determine link target
				-- and CRT version, and use it to set the macosx_version_min linker flag
				linkoptions { "-mmacosx-version-min=" .. _OPTIONS["macosx-version-min"] }
			end

			-- Only libc++ is supported on MacOS
			if os.istarget("macosx") then
				buildoptions { "-stdlib=libc++" }
				linkoptions { "-stdlib=libc++" }
			end
		end

		buildoptions {
			-- Hide symbols in dynamic shared objects by default, for efficiency and for equivalence with
			-- Windows - they should be exported explicitly with __attribute__ ((visibility ("default")))
			"-fvisibility=hidden"
		}

		if _OPTIONS["bindir"] then
			defines { "INSTALLED_BINDIR=" .. _OPTIONS["bindir"] }
		end
		if _OPTIONS["datadir"] then
			defines { "INSTALLED_DATADIR=" .. _OPTIONS["datadir"] }
		end
		if _OPTIONS["libdir"] then
			defines { "INSTALLED_LIBDIR=" .. _OPTIONS["libdir"] }
		end

		if os.istarget("linux") or os.istarget("bsd") then
			-- To use our local shared libraries, they need to be found in the
			-- runtime dynamic linker path. Add their path to -rpath.
			if _OPTIONS["libdir"] then
				linkoptions {"-Wl,-rpath," .. _OPTIONS["libdir"] }
			else
				-- On FreeBSD we need to allow use of $ORIGIN
				if os.istarget("bsd") then
					linkoptions { "-Wl,-z,origin" }
				end

				-- Adding the executable path and taking care of correct escaping
				if _ACTION == "gmake2" then
					linkoptions { "-Wl,-rpath,'$$ORIGIN'" }
				elseif _ACTION == "codeblocks" then
					linkoptions { "-Wl,-R\\\\$$$ORIGIN" }
				end
			end
		end

	end
end

-- create a project and set the attributes that are common to all projects.
function project_create(project_name, target_type)

	project(project_name)
	language "C++"
	kind(target_type)

	filter "action:vs2017"
		toolset "v141_xp"
	filter {}

	filter "action:vs*"
		buildoptions "/utf-8"
		-- disable LNK4221 warning, to avoid spending energy ordering projects in linker invocations
		linkoptions "/ignore:4221"
	filter {}

	project_set_target(project_name)
	project_set_build_flags()
end


-- OSX creates a .app bundle if the project type of the main application is set to "WindowedApp".
-- We don't want this because this bundle would be broken (it lacks all the resources and external dependencies, Info.plist etc...)
-- Windows opens a console in the background if it's set to ConsoleApp, which is not what we want.
-- I didn't check if this setting matters for linux, but WindowedApp works there.
function get_main_project_target_type()
	if _OPTIONS["android"] then
		return "SharedLib"
	elseif os.istarget("macosx") then
		return "ConsoleApp"
	else
		return "WindowedApp"
	end
end


-- source_root: rel_source_dirs and rel_include_dirs are relative to this directory
-- rel_source_dirs: A table of subdirectories. All source files in these directories are added.
-- rel_include_dirs: A table of subdirectories to be included.
-- extra_params: table including zero or more of the following:
-- * no_pch: If specified, no precompiled headers are used for this project.
-- * pch_dir: If specified, this directory will be used for precompiled headers instead of the default
--   <source_root>/pch/<projectname>/.
-- * extra_files: table of filenames (relative to source_root) to add to project
-- * extra_links: table of library names to add to link step
function project_add_contents(source_root, rel_source_dirs, rel_include_dirs, extra_params)

	for i,v in pairs(rel_source_dirs) do
		local prefix = source_root..v.."/"
		files { prefix.."*.cpp", prefix.."*.h", prefix.."*.inl", prefix.."*.js", prefix.."*.asm", prefix.."*.mm" }
	end

	-- Put the project-specific PCH directory at the start of the
	-- include path, so '#include "precompiled.h"' will look in
	-- there first
	local pch_dir
	if not extra_params["pch_dir"] then
		pch_dir = source_root .. "pch/" .. project().name .. "/"
	else
		pch_dir = extra_params["pch_dir"]
	end
	includedirs { pch_dir }

	-- Precompiled Headers
	-- rationale: we need one PCH per static lib, since one global header would
	-- increase dependencies. To that end, we can either include them as
	-- "projectdir/precompiled.h", or add "source/PCH/projectdir" to the
	-- include path and put the PCH there. The latter is better because
	-- many projects contain several dirs and it's unclear where there the
	-- PCH should be stored. This way is also a bit easier to use in that
	-- source files always include "precompiled.h".
	-- Notes:
	-- * Visual Assist manages to use the project include path and can
	--   correctly open these files from the IDE.
	-- * precompiled.cpp (needed to "Create" the PCH) also goes in
	--   the abovementioned dir.
	if (not _OPTIONS["without-pch"] and not extra_params["no_pch"]) then
		filter "action:vs*"
			pchheader("precompiled.h")
		filter "action:xcode*"
			pchheader("../"..pch_dir.."precompiled.h")
		filter { "action:not vs*", "action:not xcode*" }
			pchheader(pch_dir.."precompiled.h")
		filter {}
		pchsource(pch_dir.."precompiled.cpp")
		defines { "CONFIG_ENABLE_PCH=1" }
		files { pch_dir.."precompiled.h", pch_dir.."precompiled.cpp" }
	else
		defines { "CONFIG_ENABLE_PCH=0" }
		flags { "NoPCH" }
	end

	-- next is source root dir, for absolute (nonrelative) includes
	-- (e.g. "lib/precompiled.h")
	includedirs { source_root }

	for i,v in pairs(rel_include_dirs) do
		includedirs { source_root .. v }
	end

	if extra_params["extra_files"] then
		for i,v in pairs(extra_params["extra_files"]) do
			-- .rc files are only needed on Windows
			if path.getextension(v) ~= ".rc" or os.istarget("windows") then
				files { source_root .. v }
			end
		end
	end

	if extra_params["extra_links"] then
		links { extra_params["extra_links"] }
	end
end


-- Add command-line options to set up the manifest dependencies for Windows
function project_add_manifest()
	-- To use XP-style themed controls, we need to use the manifest to specify the
	-- desired version. (This must be set in the game's .exe in order to affect Atlas.)
	-- We can remove it once we remove wxWidgets
	linkoptions { "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='X86' publicKeyToken='6595b64144ccf1df'\"" }
end

--------------------------------------------------------------------------------
-- engine static libraries
--------------------------------------------------------------------------------

-- the engine is split up into several static libraries. this eases separate
-- distribution of those components, reduces dependencies a bit, and can
-- also speed up builds.
-- more to the point, it is necessary to efficiently support a separate
-- test executable that also includes much of the game code.

-- names of all static libs created. automatically added to the
-- main app project later (see explanation at end of this file)
static_lib_names = {}
static_lib_names_debug = {}
static_lib_names_release = {}

-- set up one of the static libraries into which the main engine code is split.
-- extra_params:
--	no_default_link: If specified, linking won't be done by default.
-- 	For the rest of extra_params, see project_add_contents().
-- note: rel_source_dirs and rel_include_dirs are relative to global source_root.
function setup_static_lib_project (project_name, rel_source_dirs, extern_libs, extra_params)

	local target_type = "StaticLib"
	project_create(project_name, target_type)
	project_add_contents(source_root, rel_source_dirs, {}, extra_params)
	project_add_extern_libs(extern_libs, target_type)

	if not extra_params["no_default_link"] then
		table.insert(static_lib_names, project_name)
	end

	-- Deactivate Run Time Type Information. Performance of dynamic_cast is very poor.
	-- The exception to this principle is Atlas UI, which is not a static library.
	rtti "off"

	if os.istarget("macosx") then
		architecture(macos_arch)
		buildoptions { "-arch " .. macos_arch }
		linkoptions { "-arch " .. macos_arch }
		xcodebuildsettings { ARCHS = macos_arch }
		if _OPTIONS["macosx-version-min"] then
			xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = _OPTIONS["macosx-version-min"] }
		end
	end
end

function setup_third_party_static_lib_project (project_name, rel_source_dirs, extern_libs, extra_params)

	setup_static_lib_project(project_name, rel_source_dirs, extern_libs, extra_params)
	includedirs { source_root .. "third_party/" .. project_name .. "/include/" }
end

function setup_shared_lib_project (project_name, rel_source_dirs, extern_libs, extra_params)

	local target_type = "SharedLib"
	project_create(project_name, target_type)
	project_add_contents(source_root, rel_source_dirs, {}, extra_params)
	project_add_extern_libs(extern_libs, target_type)

	if not extra_params["no_default_link"] then
		table.insert(static_lib_names, project_name)
	end

	if os.istarget("windows") then
		links { "delayimp" }
	elseif os.istarget("macosx") then
		architecture(macos_arch)
		buildoptions { "-arch " .. macos_arch }
		linkoptions { "-arch " .. macos_arch }
		xcodebuildsettings { ARCHS = macos_arch }
		if _OPTIONS["macosx-version-min"] then
			xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = _OPTIONS["macosx-version-min"] }
		end
	end
end


-- this is where the source tree is chopped up into static libs.
-- can be changed very easily; just copy+paste a new setup_static_lib_project,
-- or remove existing ones. static libs are automagically added to
-- main_exe link step.
function setup_all_libs ()

	-- relative to global source_root.
	local source_dirs = {}
	-- names of external libraries used (see libraries_dir comment)
	local extern_libs = {}


	source_dirs = {
		"network",
	}
	extern_libs = {
		"spidermonkey",
		"enet",
		"sdl",
		"boost",	-- dragged in via server->simulation.h->random and NetSession.h->lockfree
		"fmt",
	}
	if not _OPTIONS["without-miniupnpc"] then
		table.insert(extern_libs, "miniupnpc")
	end
	setup_static_lib_project("network", source_dirs, extern_libs, {})

	source_dirs = {
		"rlinterface",
	}
	extern_libs = {
		"boost", -- dragged in via simulation.h and scriptinterface.h
		"fmt",
		"spidermonkey",
	}
	setup_static_lib_project("rlinterface", source_dirs, extern_libs, { no_pch = 1 })

	source_dirs = {
		"third_party/tinygettext/src",
	}
	extern_libs = {
		"iconv",
		"boost",
		"fmt",
	}
	setup_third_party_static_lib_project("tinygettext", source_dirs, extern_libs, { } )

	-- it's an external library and we don't want to modify its source to fix warnings, so we just disable them to avoid noise in the compile output
	filter "action:vs*"
		buildoptions {
			"/wd4127",
			"/wd4309",
			"/wd4800",
			"/wd4100",
			"/wd4996",
			"/wd4099",
			"/wd4503"
		}
	filter {}


	if not _OPTIONS["without-lobby"] then
		source_dirs = {
			"lobby",
			"lobby/scripting",
			"i18n",
			"third_party/encryption"
		}

		extern_libs = {
			"spidermonkey",
			"boost",
			"enet",
			"gloox",
			"icu",
			"iconv",
			"libsodium",
			"tinygettext",
			"fmt",
		}
		setup_static_lib_project("lobby", source_dirs, extern_libs, {})
	else
		source_dirs = {
			"lobby/scripting",
			"third_party/encryption"
		}
		extern_libs = {
			"spidermonkey",
			"boost",
			"libsodium",
			"fmt",
		}
		setup_static_lib_project("lobby", source_dirs, extern_libs, {})
		files { source_root.."lobby/Globals.cpp" }
	end


	source_dirs = {
		"simulation2",
		"simulation2/components",
		"simulation2/helpers",
		"simulation2/scripting",
		"simulation2/serialization",
		"simulation2/system",
		"simulation2/testcomponents",
	}
	extern_libs = {
		"boost",
		"spidermonkey",
		"fmt",
	}
	setup_static_lib_project("simulation2", source_dirs, extern_libs, {})


	source_dirs = {
		"scriptinterface",
		"scriptinterface/third_party"
	}
	extern_libs = {
		"boost",
		"spidermonkey",
		"valgrind",
		"sdl",
		"fmt",
	}
	setup_static_lib_project("scriptinterface", source_dirs, extern_libs, {})


	source_dirs = {
		"ps",
		"ps/scripting",
		"network/scripting",
		"ps/GameSetup",
		"ps/XMB",
		"ps/XML",
		"soundmanager",
		"soundmanager/data",
		"soundmanager/items",
		"soundmanager/scripting",
		"maths",
		"maths/scripting",
		"i18n",
		"i18n/scripting",
	}
	extern_libs = {
		"spidermonkey",
		"sdl",	-- key definitions
		"libxml2",
		"zlib",
		"boost",
		"enet",
		"libcurl",
		"tinygettext",
		"icu",
		"iconv",
		"libsodium",
		"fmt",
		"freetype",
	}

	if not _OPTIONS["without-nvtt"] then
		table.insert(extern_libs, "nvtt")
	end
	if not _OPTIONS["without-audio"] then
		table.insert(extern_libs, "openal")
		table.insert(extern_libs, "vorbis")
	end

	setup_static_lib_project("engine", source_dirs, extern_libs, {})


	source_dirs = {
		"graphics",
		"graphics/scripting",
		"renderer",
		"renderer/backend",
		"renderer/backend/dummy",
		"renderer/backend/gl",
		"renderer/backend/vulkan",
		"renderer/scripting",
		"third_party/mikktspace",
		"third_party/ogre3d_preprocessor",
		"third_party/vma"
	}
	extern_libs = {
		"sdl",	-- key definitions
		"spidermonkey",	-- for graphics/scripting
		"boost",
		"fmt",
		"freetype",
		"icu",
	}
	if not _OPTIONS["without-nvtt"] then
		table.insert(extern_libs, "nvtt")
	end
	setup_static_lib_project("graphics", source_dirs, extern_libs, {})


	source_dirs = {
		"tools/atlas/GameInterface",
		"tools/atlas/GameInterface/Handlers"
	}
	extern_libs = {
		"boost",
		"sdl",	-- key definitions
		"spidermonkey",
		"fmt",
	}
	setup_static_lib_project("atlas", source_dirs, extern_libs, {})


	source_dirs = {
		"gui",
		"gui/ObjectTypes",
		"gui/ObjectBases",
		"gui/Scripting",
		"gui/SettingTypes",
		"i18n"
	}
	extern_libs = {
		"spidermonkey",
		"sdl",	-- key definitions
		"boost",
		"enet",
		"tinygettext",
		"icu",
		"iconv",
		"fmt",
	}
	if not _OPTIONS["without-audio"] then
		table.insert(extern_libs, "openal")
	end
	setup_static_lib_project("gui", source_dirs, extern_libs, {})


	source_dirs = {
		"lib",
		"lib/adts",
		"lib/allocators",
		"lib/external_libraries",
		"lib/file",
		"lib/file/archive",
		"lib/file/common",
		"lib/file/io",
		"lib/file/vfs",
		"lib/pch",
		"lib/posix",
		"lib/res",
		"lib/res/graphics",
		"lib/sysdep",
		"lib/tex"
	}
	extern_libs = {
		"boost",
		"sdl",
		"openal",
		"libpng",
		"zlib",
		"valgrind",
		"cxxtest",
		"fmt",
	}

	-- CPU architecture-specific
	if arch == "amd64" then
		table.insert(source_dirs, "lib/sysdep/arch/amd64");
		table.insert(source_dirs, "lib/sysdep/arch/x86_x64");
	elseif arch == "x86" then
		table.insert(source_dirs, "lib/sysdep/arch/ia32");
		table.insert(source_dirs, "lib/sysdep/arch/x86_x64");
	elseif arch == "arm" then
		table.insert(source_dirs, "lib/sysdep/arch/arm");
	elseif arch == "aarch64" then
		table.insert(source_dirs, "lib/sysdep/arch/aarch64");
	elseif arch == "e2k" then
		table.insert(source_dirs, "lib/sysdep/arch/e2k");
	elseif arch == "ppc64" then
		table.insert(source_dirs, "lib/sysdep/arch/ppc64");
	end

	-- OS-specific
	sysdep_dirs = {
		linux = { "lib/sysdep/os/linux", "lib/sysdep/os/unix" },
		-- note: RC file must be added to main_exe project.
		-- note: don't add "lib/sysdep/os/win/aken.cpp" because that must be compiled with the DDK.
		windows = { "lib/sysdep/os/win", "lib/sysdep/os/win/wposix", "lib/sysdep/os/win/whrt" },
		macosx = { "lib/sysdep/os/osx", "lib/sysdep/os/unix" },
		bsd = { "lib/sysdep/os/bsd", "lib/sysdep/os/unix", "lib/sysdep/os/unix/x" },
	}
	for i,v in pairs(sysdep_dirs[os.target()]) do
		table.insert(source_dirs, v);
	end

	if os.istarget("linux") then
		if _OPTIONS["android"] then
			table.insert(source_dirs, "lib/sysdep/os/android")
		else
			table.insert(source_dirs, "lib/sysdep/os/unix/x")
		end
	end

	-- On OSX, disable precompiled headers because C++ files and Objective-C++ files are
	-- mixed in this project. To fix that, we would need per-file basis configuration which
	-- is not yet supported by the gmake action in premake. We should look into using gmake2.
	extra_params = {}
	if os.istarget("macosx") then
		extra_params = { no_pch = 1 }
	end

	-- runtime-library-specific
	if _ACTION == "vs2017" then
		table.insert(source_dirs, "lib/sysdep/rtl/msc");
	else
		table.insert(source_dirs, "lib/sysdep/rtl/gcc");
	end

	setup_static_lib_project("lowlevel", source_dirs, extern_libs, extra_params)


	extern_libs = { "glad" }
	if not os.istarget("windows") and not _OPTIONS["android"] and not os.istarget("macosx") then
		table.insert(extern_libs, "x11")
	end
	setup_static_lib_project("gladwrapper", {}, extern_libs, { no_pch = 1 })
	-- select files to build for the current platform
	glad_path = third_party_source_dir.."glad/"
	files { glad_path.."src/vulkan.cpp" }
	if _OPTIONS["gles"] then
		files { glad_path.."src/gles2.cpp" }
	else
		files { glad_path.."src/gl.cpp" }
		if os.istarget("windows") then
			files { glad_path.."src/wgl.cpp" }
		elseif os.istarget("linux") or os.istarget("bsd") then
			files { glad_path.."src/egl.cpp", glad_path.."src/glx.cpp" }
		end
	end
	-- on Windows, silence a build warning in vulkan.cpp
	filter "action:vs*"
		buildoptions { "/wd4551" }
	filter {}


	-- Third-party libraries that are built as part of the main project,
	-- not built externally and then linked
	source_dirs = {
		"third_party/mongoose",
	}
	extern_libs = {
	}
	setup_static_lib_project("mongoose", source_dirs, extern_libs, { no_pch = 1 })


	-- CxxTest mock function support
	extern_libs = {
		"boost",
		"cxxtest",
	}

	-- 'real' implementations, to be linked against the main executable
	-- (files are added manually and not with setup_static_lib_project
	-- because not all files in the directory are included)
	setup_static_lib_project("mocks_real", {}, extern_libs, { no_default_link = 1, no_pch = 1 })
	files { "mocks/*.h", source_root.."mocks/*_real.cpp" }
	-- 'test' implementations, to be linked against the test executable
	setup_static_lib_project("mocks_test", {}, extern_libs, { no_default_link = 1, no_pch = 1 })
	files { source_root.."mocks/*.h", source_root.."mocks/*_test.cpp" }
end

--------------------------------------------------------------------------------
-- main EXE
--------------------------------------------------------------------------------

-- used for main EXE as well as test
used_extern_libs = {
	"sdl",

	"libpng",
	"zlib",

	"spidermonkey",
	"libxml2",

	"boost",
	"cxxtest",
	"comsuppw",
	"enet",
	"libcurl",
	"tinygettext",
	"icu",
	"iconv",
	"libsodium",
	"fmt",
	"freetype",

	"valgrind",
}

if not os.istarget("windows") and not _OPTIONS["android"] and not os.istarget("macosx") then
	-- X11 should only be linked on *nix
	table.insert(used_extern_libs, "x11")
end

if not _OPTIONS["without-audio"] then
	table.insert(used_extern_libs, "openal")
	table.insert(used_extern_libs, "vorbis")
end

if not _OPTIONS["without-nvtt"] then
	table.insert(used_extern_libs, "nvtt")
end

if not _OPTIONS["without-lobby"] then
	table.insert(used_extern_libs, "gloox")
end

if not _OPTIONS["without-miniupnpc"] then
	table.insert(used_extern_libs, "miniupnpc")
end

-- Bundles static libs together with main.cpp and builds game executable.
function setup_main_exe ()

	local target_type = get_main_project_target_type()
	project_create("pyrogenesis", target_type)

	filter "system:not macosx"
		linkgroups 'On'
	filter {}

	links { "mocks_real" }

	local extra_params = {
		extra_files = { "main.cpp" },
		no_pch = 1
	}
	project_add_contents(source_root, {}, {}, extra_params)
	project_add_extern_libs(used_extern_libs, target_type)

	dependson { "Collada" }

	rtti "off"

	-- Platform Specifics
	if os.istarget("windows") then

		files { source_root.."lib/sysdep/os/win/icon.rc" }
		files { source_root.."lib/sysdep/os/win/pyrogenesis.rc" }
		-- from "lowlevel" static lib; must be added here to be linked in
		files { source_root.."lib/sysdep/os/win/error_dialog.rc" }

		linkoptions {
			-- wraps main thread in a __try block(see wseh.cpp). replace with mainCRTStartup if that's undesired.
			"/ENTRY:wseh_EntryPoint",
		}

		links { "delayimp" }

		if arch == "x86" then
			-- allow the executable to use more than 2GB of RAM.
			linkoptions { "/LARGEADDRESSAWARE" }
		end

		-- see manifest.cpp
		project_add_manifest()

	elseif os.istarget("linux") or os.istarget("bsd") then

		if not _OPTIONS["android"] and not (os.getversion().description == "OpenBSD") then
			links { "rt" }
		end

		if _OPTIONS["android"] then
			-- NDK's STANDALONE-TOOLCHAIN.html says this is required
			linkoptions { "-Wl,--fix-cortex-a8" }

			links { "log" }
		end

		if link_execinfo then
			links {
				"execinfo"
			}
		end

		if os.istarget("linux") or os.getversion().description == "GNU/kFreeBSD" then
			links {
				-- Dynamic libraries (needed for linking for gold)
				"dl",
			}
		end

		-- Threading support
		buildoptions { "-pthread" }
		if not _OPTIONS["android"] then
			linkoptions { "-pthread" }
		end

		-- For debug_resolve_symbol
		filter "Debug"
			linkoptions { "-rdynamic" }
		filter { }

	elseif os.istarget("macosx") then

		links { "pthread" }
		links { "ApplicationServices.framework", "Cocoa.framework", "CoreFoundation.framework" }

		architecture(macos_arch)
		buildoptions { "-arch " .. macos_arch }
		linkoptions { "-arch " .. macos_arch }
		xcodebuildsettings { ARCHS = macos_arch }
		if _OPTIONS["macosx-version-min"] then
			xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = _OPTIONS["macosx-version-min"] }
		end
	end
end


--------------------------------------------------------------------------------
-- atlas
--------------------------------------------------------------------------------

-- setup a typical Atlas component project
-- extra_params, rel_source_dirs and rel_include_dirs: as in project_add_contents;
function setup_atlas_project(project_name, target_type, rel_source_dirs, rel_include_dirs, extern_libs, extra_params)

	local source_root = rootdir.."/source/tools/atlas/" .. project_name .. "/"
	project_create(project_name, target_type)

	-- if not specified, the default for atlas pch files is in the project root.
	if not extra_params["pch_dir"] then
		extra_params["pch_dir"] = source_root
	end

	project_add_contents(source_root, rel_source_dirs, rel_include_dirs, extra_params)
	project_add_extern_libs(extern_libs, target_type)

	-- Platform Specifics
	if os.istarget("windows") then
		-- Link to required libraries
		links { "winmm", "delayimp" }

	elseif os.istarget("macosx") then
		architecture(macos_arch)
		buildoptions { "-arch " .. macos_arch }
		linkoptions { "-arch " .. macos_arch }
		xcodebuildsettings { ARCHS = macos_arch }
		if _OPTIONS["macosx-version-min"] then
			xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = _OPTIONS["macosx-version-min"] }
		end
	elseif os.istarget("linux") or os.istarget("bsd") then
		if os.getversion().description == "FreeBSD" then
			buildoptions { "-fPIC" }
			linkoptions { "-fPIC" }
		else
			buildoptions { "-fPIC" }
			linkoptions { "-fPIC", "-rdynamic" }
		end

		-- warnings triggered by wxWidgets
		buildoptions { "-Wno-unused-local-typedefs" }
	end

end


-- build all Atlas component projects
function setup_atlas_projects()

	setup_atlas_project("AtlasObject", "StaticLib",
	{	-- src
		".",
		"../../../third_party/jsonspirit"

	},{	-- include
		"../../../third_party/jsonspirit"
	},{	-- extern_libs
		"boost",
		"iconv",
		"libxml2"
	},{	-- extra_params
		no_pch = 1
	})

	atlas_src = {
		"ActorEditor",
		"CustomControls/Buttons",
		"CustomControls/Canvas",
		"CustomControls/ColorDialog",
		"CustomControls/DraggableListCtrl",
		"CustomControls/EditableListCtrl",
		"CustomControls/FileHistory",
		"CustomControls/HighResTimer",
		"CustomControls/MapDialog",
		"CustomControls/MapResizeDialog",
		"CustomControls/SnapSplitterWindow",
		"CustomControls/VirtualDirTreeCtrl",
		"CustomControls/Windows",
		"General",
		"General/VideoRecorder",
		"Misc",
		"ScenarioEditor",
		"ScenarioEditor/Sections/Common",
		"ScenarioEditor/Sections/Cinema",
		"ScenarioEditor/Sections/Environment",
		"ScenarioEditor/Sections/Map",
		"ScenarioEditor/Sections/Object",
		"ScenarioEditor/Sections/Player",
		"ScenarioEditor/Sections/Terrain",
		"ScenarioEditor/Tools",
		"ScenarioEditor/Tools/Common",
	}
	atlas_extra_links = {
		"AtlasObject"
	}

	atlas_extern_libs = {
		"boost",
		"comsuppw",
		"iconv",
		"libxml2",
		"sdl",	-- key definitions
		"wxwidgets",
		"zlib",
	}
	if not os.istarget("windows") and not os.istarget("macosx") then
		-- X11 should only be linked on *nix
		table.insert(atlas_extern_libs, "x11")
	end

	setup_atlas_project("AtlasUI", "SharedLib", atlas_src,
	{	-- include
		"..",
		"CustomControls",
		"Misc",
		"../../../third_party/jsonspirit"
	},
	atlas_extern_libs,
	{	-- extra_params
		pch_dir = rootdir.."/source/tools/atlas/AtlasUI/Misc/",
		no_pch = false,
		extra_links = atlas_extra_links,
		extra_files = { "Misc/atlas.rc" }
	})
end


-- Atlas 'frontend' tool-launching projects
function setup_atlas_frontend_project (project_name)

	local target_type = get_main_project_target_type()
	project_create(project_name, target_type)

	local source_root = rootdir.."/source/tools/atlas/AtlasFrontends/"
	files { source_root..project_name..".cpp" }

	if os.istarget("windows") then
		files { source_root..project_name..".rc" }
	end

	includedirs { source_root .. ".." }

	-- Platform Specifics
	if os.istarget("windows") then
		-- see manifest.cpp
		project_add_manifest()

	else -- Non-Windows, = Unix
		links { "AtlasObject" }
		if os.istarget("macosx") then
			architecture(macos_arch)
			buildoptions { "-arch " .. macos_arch }
			linkoptions { "-arch " .. macos_arch }
			xcodebuildsettings { ARCHS = macos_arch }
		end
	end

	links { "AtlasUI" }

end

function setup_atlas_frontends()
	setup_atlas_frontend_project("ActorEditor")
end


--------------------------------------------------------------------------------
-- collada
--------------------------------------------------------------------------------

function setup_collada_project(project_name, target_type, rel_source_dirs, rel_include_dirs, extern_libs, extra_params)

	project_create(project_name, target_type)
	local source_root = source_root.."collada/"
	extra_params["pch_dir"] = source_root
	project_add_contents(source_root, rel_source_dirs, rel_include_dirs, extra_params)
	project_add_extern_libs(extern_libs, target_type)

	-- Platform Specifics
	if os.istarget("windows") then
		characterset "MBCS"
	elseif os.istarget("linux") then
		defines { "LINUX" }

		links {
			"dl",
		}

		-- FCollada is not aliasing-safe, so disallow dangerous optimisations
		-- (TODO: It'd be nice to fix FCollada, but that looks hard)
		buildoptions { "-fno-strict-aliasing" }
		if os.getversion().description ~= "FreeBSD" then
			linkoptions { "-rdynamic" }
		end

	elseif os.istarget("bsd") then
		if os.getversion().description == "OpenBSD" then
			links { "c", }
		end

		if os.getversion().description == "GNU/kFreeBSD" then
			links {
				"dl",
			}
		end

		buildoptions { "-fno-strict-aliasing" }

		linkoptions { "-rdynamic" }

	elseif os.istarget("macosx") then
		-- define MACOS-something?

		buildoptions { "-fno-strict-aliasing" }
		-- On OSX, fcollada uses a few utility functions from coreservices
		links { "CoreServices.framework" }

		architecture(macos_arch)
		buildoptions { "-arch " .. macos_arch }
		linkoptions { "-arch " .. macos_arch }
		xcodebuildsettings { ARCHS = macos_arch }
	end

end

-- build all Collada component projects
function setup_collada_projects()

	setup_collada_project("Collada", "SharedLib",
	{	-- src
		"."
	},{	-- include
	},{	-- extern_libs
		"fcollada",
		"iconv",
		"libxml2"
	},{	-- extra_params
	})

end


--------------------------------------------------------------------------------
-- tests
--------------------------------------------------------------------------------

function setup_tests()

	local cxxtest = require "cxxtest"

	if os.istarget("windows") then
		cxxtest.setpath(rootdir.."/build/bin/cxxtestgen.exe")
	else
		if _OPTIONS["with-system-cxxtest"] then
			local handle = io.popen("command -v cxxtestgen")
			local cxxtestgen = handle:read("*a"):gsub("\n", " ")
			handle:close()
			cxxtest.setpath(cxxtestgen)
		else
			cxxtest.setpath(rootdir.."/libraries/source/cxxtest-4.4/bin/cxxtestgen")
		end
	end

	local runner = "ErrorPrinter"
	if _OPTIONS["jenkins-tests"] then
		runner = "XmlPrinter"
	end

	local include_files = {
		-- Precompiled headers - the header is added to all generated .cpp files
		-- note that the header isn't actually precompiled here, only #included
		-- so that the build stage can use it as a precompiled header.
		"precompiled.h",
	}
	local test_root_include_files = {
		"precompiled.h",
	}
	if os.istarget("windows") then
		-- This is required to build against SDL 2.0.12 (starting from 2.0.4) on Windows.
		-- Refs #3138
		table.insert(test_root_include_files, "lib/external_libraries/libsdl.h")
	end

	cxxtest.init(true, true, runner, include_files, test_root_include_files)

	local target_type = get_main_project_target_type()
	project_create("test", target_type)

	-- Find header files in 'test' subdirectories
	local all_files = os.matchfiles(source_root .. "**/tests/*.h")
	local test_files = {}
	for i,v in pairs(all_files) do
		-- Don't include sysdep tests on the wrong sys
		-- Don't include Atlas tests unless Atlas is being built
		if not (string.find(v, "/sysdep/os/win/") and not os.istarget("windows")) and
		   not (string.find(v, "/tools/atlas/") and _OPTIONS["without-atlas"]) and
		   not (string.find(v, "/sysdep/arch/x86_x64/") and ((arch ~= "amd64") or (arch ~= "x86")))
		then
			table.insert(test_files, v)
		end
	end

	cxxtest.configure_project(test_files)

	filter "system:not macosx"
		linkgroups 'On'
	filter {}

	links { static_lib_names }
	filter "Debug"
		links { static_lib_names_debug }
	filter "Release"
		links { static_lib_names_release }
	filter { }

	links { "mocks_test" }
	if not _OPTIONS["without-atlas"] then
		links { "AtlasObject" }
	end
	extra_params = {
		extra_files = { "test_setup.cpp" },
	}

	project_add_contents(source_root, {}, {}, extra_params)
	project_add_extern_libs(used_extern_libs, target_type)

	dependson { "Collada" }

	rtti "off"

	-- TODO: should fix the duplication between this OS-specific linking
	-- code, and the similar version in setup_main_exe

	if os.istarget("windows") then
		-- from "lowlevel" static lib; must be added here to be linked in
		files { source_root.."lib/sysdep/os/win/error_dialog.rc" }

		-- Enables console for the TEST project on Windows
		linkoptions { "/SUBSYSTEM:CONSOLE" }

		links { "delayimp" }

		project_add_manifest()

	elseif os.istarget("linux") or os.istarget("bsd") then

		if link_execinfo then
			links {
				"execinfo"
			}
		end
		if not _OPTIONS["android"] and not (os.getversion().description == "OpenBSD") then
			links { "rt" }
		end

		if _OPTIONS["android"] then
			-- NDK's STANDALONE-TOOLCHAIN.html says this is required
			linkoptions { "-Wl,--fix-cortex-a8" }
		end

		if os.istarget("linux") or os.getversion().description == "GNU/kFreeBSD" then
			links {
				-- Dynamic libraries (needed for linking for gold)
				"dl",
			}
		end

		-- Threading support
		buildoptions { "-pthread" }
		if not _OPTIONS["android"] then
			linkoptions { "-pthread" }
		end

		-- For debug_resolve_symbol
		filter "Debug"
			linkoptions { "-rdynamic" }
		filter { }

		includedirs { source_root .. "pch/test/" }

	elseif os.istarget("macosx") then
		architecture(macos_arch)
		buildoptions { "-arch " .. macos_arch }
		linkoptions { "-arch " .. macos_arch }
		xcodebuildsettings { ARCHS = macos_arch }
		if _OPTIONS["macosx-version-min"] then
			xcodebuildsettings { MACOSX_DEPLOYMENT_TARGET = _OPTIONS["macosx-version-min"] }
		end
	end
end

-- must come first, so that VC sets it as the default project and therefore
-- allows running via F5 without the "where is the EXE" dialog.
setup_main_exe()

setup_all_libs()

-- add the static libs to the main EXE project. only now (after
-- setup_all_libs has run) are the lib names known. cannot move
-- setup_main_exe to run after setup_all_libs (see comment above).
-- we also don't want to hardcode the names - that would require more
-- work when changing the static lib breakdown.
project("pyrogenesis") -- Set the main project active
	links { static_lib_names }
	filter "Debug"
		links { static_lib_names_debug }
	filter "Release"
		links { static_lib_names_release }
	filter { }

if not _OPTIONS["without-atlas"] then
	setup_atlas_projects()
	setup_atlas_frontends()
end

setup_collada_projects()

if not _OPTIONS["without-tests"] then
	setup_tests()
end