summaryrefslogtreecommitdiffstats
path: root/premake5.lua
blob: 0998a338f92ba84f3fd83b2b21a9b2d4c4e4bfab (plain)
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
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
-- premake5.lua

-- This file describes the build configuration for Slang so
-- that premake can generate platform-specific build files
-- using Premake 5 (https://premake.github.io/).
--
-- To update the build files that are checked in to the Slang repository,
-- run a `premake5` binary and specify the appropriate action, e.g.:
--
--      premake5.exe --os=windows vs2015
--
-- If you are trying to build Slang on another platform, then you
-- can try invoking `premake5` for your desired OS and build format
-- and see what happens.
--
-- If you are going to modify this file to change/customize the Slang
-- build, then you may need to read up on Premake's approach and
-- how it uses/abuses Lua syntax. A few important things to note:
--
-- * Everything that *looks* like a declarative (e.g., `kind "SharedLib"`)
-- is actually a Lua function call (e.g., `kind("SharedLib")`) that
-- modifies the behind-the-scenes state that describes the build.
--
-- * Many of these function calls are "sticky" and affect subsequent
-- calls, so ordering matters a *lot*. This file uses indentation to
-- represent some of the flow of state, but it is important to recognize
-- that the indentation is not semantically significant.
--
-- * Because the configuration logic is just executable Lua code, we
-- can capture and re-use bits of configuration logic in ordinary
-- Lua subroutines.
--
-- Now let's move on to the actual build:

-- The "workspace" represents the overall build (the "solution" in
-- Visual Studio terms). It sets up basic build settings that will
-- apply across all projects.
--

-- To output linux will output to linux
-- % premake5 --os=linux gmake2 --build-location="build.linux"
--
-- % cd build.linux
-- % make config=release_x64
-- or
-- % make config=debug_x64
--
-- From in the build directory you can use
-- % premake5 --file=../premake5.lua --os=linux gmake2

-- Fail if we try to use the deprecated 'gmake' generator
if _ACTION == "gmake" then
    premake.error "Please use the 'gmake2' generator instead of 'gmake'"
end

--
-- Add the package path for slang-pack/slang-util
-- The question mark is there the name of the module is inserted.
---

local modulePath = "external/slang-binaries/lua-modules/?.lua"

package.path = package.path .. ";" .. modulePath

-- Load the slack package manager module
slangPack = require("slang-pack")
slangUtil = require("slang-util")

-- Load the dependencies from the json file
deps = slangPack.loadDependencies("deps/target-deps.json")

newoption {
    trigger     = "override-module",
    description = "(Optional) Specify a lua file that can override functions",
    value       = "path"
}

newoption {
    trigger     = "build-location",
    description = "(Optional) Specifiy the location to place solution on root Makefile",
    value       = "path"
}

newoption {
    trigger     = "execute-binary",
    description = "(Optional) If true binaries used in build will be executed (disable on cross compilation)",
    value       = "bool",
    default     = "true",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "build-glslang",
    description = "(Optional) If true glslang and spirv-opt will be built",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "enable-cuda",
    description = "(Optional) If true will enable cuda tests, if CUDA is found via CUDA_PATH",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "enable-nvapi",
    description = "(Optional) If true will enable NVAPI, if NVAPI is found via external/nvapi",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "cuda-sdk-path",
    description = "(Optional) Path to the root of CUDA SDK. If set will enable CUDA in build (ie in effect sets enable-cuda=true too)",
    value       = "path"
}

newoption {
    trigger     = "enable-optix",
    description = "(Optional) If true will enable OptiX build/ tests (also implicitly enables CUDA)",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "optix-sdk-path",
    description = "(Optional) Path to the root of OptiX SDK. (Implicitly enabled OptiX and CUDA)",
    value       = "path"
}

newoption {
    trigger     = "enable-profile",
    description = "(Optional) If true will enable slang-profile tool - suitable for gprof usage on linux",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "enable-embed-stdlib",
    description = "(Optional) If true build slang with an embedded version of the stdlib",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "enable-xlib",
    description = "(Optional) If true build `gfx` and `platform` with xlib to support windowed apps on linux.",
    value       = "bool",
    default     = "true",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "enable-experimental-projects",
    description = "(Optional) If true include experimental projects in build.",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "disable-stdlib-source",
    description = "(Optional) If true stdlib source will not be included in binary.",
    value       = "bool",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "skip-source-generation",
    description = "(Optional) If true will skip source generation steps.",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "deploy-slang-llvm",
    description = "(Optional) If true will copy slang-llvm to output directory.",
    value       = "bool",
    default     = "true",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "deploy-slang-glslang",
    description = "(Optional) If true will copy slang-glslang to output directory.",
    value       = "bool",
    default     = "true",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "full-debug-validation",
    description = "(Optional) If true will enable full IR validation in debug build. (SLOW!)",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

newoption {
    trigger     = "enable-asan",
    description = "(Optional) If true will enable ASAN (address santizier).",
    value       = "bool",
    default     = "false",
    allowed     = { { "true", "True"}, { "false", "False" } }
}

buildLocation = _OPTIONS["build-location"]
executeBinary = (_OPTIONS["execute-binary"] == "true")
buildGlslang = (_OPTIONS["build-glslang"] == "true")
enableCuda = not not (_OPTIONS["enable-cuda"] == "true" or _OPTIONS["cuda-sdk-path"])
enableProfile = (_OPTIONS["enable-profile"] == "true")
optixPath = _OPTIONS["optix-sdk-path"]
enableOptix = not not (_OPTIONS["enable-optix"] == "true" or optixPath)
enableProfile = (_OPTIONS["enable-profile"] == "true")
enableEmbedStdLib = (_OPTIONS["enable-embed-stdlib"] == "true")
enableXlib = (_OPTIONS["enable-xlib"] == "true")
skipSourceGeneration = (_OPTIONS["skip-source-generation"] == "true")
deployLLVM = (_OPTIONS["deploy-slang-llvm"] == "true")
deployGLSLang = (_OPTIONS["deploy-slang-glslang"] == "true")
fullDebugValidation = (_OPTIONS["full-debug-validation"] == "true")
enableAsan = (_OPTIONS["enable-asan"] == "true")

-- If stdlib embedding is enabled, disable stdlib source embedding by default
disableStdlibSource = enableEmbedStdLib

-- If embedding is enabled, and the setting `disable-stdlib-source` setting is set, use it's value
if enableEmbedStdLib and _OPTIONS["disable-stdlib-source"] ~= nil then
    disableStdlibSource = (_OPTIONS["disable-stdlib-source"] == "true")   
end

-- Determine the target info

targetInfo = slangUtil.getTargetInfo()

--
-- Update the dependencies for the target
--

deps:update(targetInfo.name)

-- Get the target name that can be used as paths that generate for different configurations (ie contains premake Tokens)

targetName = targetInfo.tokenName

-- This is the path where nvapi is expected to be found

nvapiPath = "external/nvapi"

if enableOptix then
    optixPath = optixPath or "C:/ProgramData/NVIDIA Corporation/OptiX SDK 7.0.0/"
    enableCuda = true
end

-- cudaPath is only set if cuda is enabled, and CUDA_PATH enviromental variable is set
cudaPath = nil
if enableCuda then
    -- Get the CUDA path. Use the value set on cuda-sdk-path by default, if not set use the environment variable.
    cudaPath = (_OPTIONS["cuda-sdk-path"] or os.getenv("CUDA_PATH"))
end

-- TODO(JS): What's the point in the enable-xlib command line option if it's just overridden here?

if targetInfo.isWindows or os.target() == "macosx" then
    enableXlib = false
end

-- Even if we have the nvapi path, we only want to currently enable on windows targets

enableNvapi = not not (os.isdir(nvapiPath) and targetInfo.isWindows and _OPTIONS["enable-nvapi"] == "true")

if enableNvapi then
    printf("Enabled NVAPI")
end

overrideModule = {}
local overrideModulePath = _OPTIONS["override-module"]
if overrideModulePath then
    overrideModule = require(overrideModulePath)
end


-- This is needed for gcc, for the 'fileno' functions on cygwin
-- _GNU_SOURCE makes realpath available in gcc
if targetInfo.os == "cygwin" then
    buildoptions { "-D_POSIX_SOURCE" }
    filter { "toolset:gcc*" }
        buildoptions { "-D_GNU_SOURCE" }
end

function getPlatforms(targetInfo)
    return { "x86", "x64", "aarch64" }
end

workspace "slang"
    -- We will support debug/release configuration and x86/x64 builds.
    configurations { "Debug", "Release" }
    platforms(getPlatforms(targetInfo))

    if buildLocation then
        location(buildLocation)
    end

    flags "MultiProcessorCompile"

    --
    -- Make slang-test the startup project.
    --
    -- https://premake.github.io/docs/startproject
    startproject "slang-test"

    -- The output binary directory will be derived from the OS
    -- and configuration options, e.g. `bin/windows-x64/debug/`
    targetdir("bin/" .. targetName .. "/%{cfg.buildcfg:lower()}")

    cppdialect "C++17"
    -- Statically link to the C/C++ runtime rather than create a DLL dependency.
    staticruntime "On"
    -- Turn off edit and continue for all projects. This is needed to avoid
    -- linking warnings.
    editandcontinue "Off"

    -- Once we've set up the common settings, we will make some tweaks
    -- that only apply in a subset of cases. Each call to `filter()`
    -- changes the "active" filter for subsequent commands. In
    -- effect, those commands iwll be ignored when the conditions of
    -- the filter aren't satisfied.

    -- Our `x64` platform should (obviously) target the x64
    -- architecture and similarly for x86.
    --
    -- https://premake.github.io/docs/architecture/
    -- 
    filter { "platforms:x64" }
        architecture "x64"
    filter { "platforms:x86" }
        architecture "x86"
    filter { "platforms:aarch64" }
        architecture "ARM64"
    filter { "platforms:aarch64", "toolset:clang" }
        buildoptions { "-arch arm64" }
        linkoptions { "-arch arm64" }

    filter { "toolset:clang or gcc*" }
        -- Makes all symbols hidden by default unless explicitly 'exported'
        buildoptions { "-fvisibility=hidden" } 
        -- Warnings
        buildoptions { "-Wno-unused-but-set-variable", "-Wno-unused-parameter", "-Wno-type-limits", "-Wno-sign-compare", "-Wno-unused-variable", "-Wno-switch", "-Wno-return-type", "-Wno-unused-local-typedefs", "-Wno-parentheses" }
    filter { "toolset:clang or gcc*", "language:C++" }
        buildoptions { "-Wno-reorder", "-Wno-class-memaccess", "-Wno-invalid-offsetof" }

    filter { "files:source/compiler-core/slang-dxc-compiler.cpp", "toolset:clang or gcc" }
        -- For the DXC headers
        buildoptions { "-fms-extensions" }

    filter { "toolset:gcc*" }
        buildoptions { "-Wno-implicit-fallthrough", "-Wno-maybe-uninitialized" }


    filter { "toolset:clang" }
        buildoptions { "-Wno-deprecated-register", "-Wno-tautological-compare", "-Wno-missing-braces", "-Wno-undefined-var-template", "-Wno-unused-function", "-Wno-return-std-move", "-Wno-ignored-optimization-argument", "-Wno-unknown-warning-option" }

    -- When compiling the debug configuration, we want to turn
    -- optimization off, make sure debug symbols are output,
    -- and add the same preprocessor definition that VS
    -- would add by default.
    filter { "configurations:debug" }
        optimize "Off"
        symbols "On"
        defines { "_DEBUG" }
        -- staticruntime "Off"
    -- For the release configuration we will turn optimizations on
    -- (we do not yet micro-manage the optimization settings)
    -- and set the preprocessor definition that VS would add by default.
    filter { "configurations:release" }
        optimize "On"
        defines { "NDEBUG" }

    filter { "system:linux" }
        links { "dl" }
        --
        -- `--no-undefined` - by default if a symbol is not found in a link it will assume it will be resolved at runtime (!)
        --                    this option ensures that all the referenced symbols exist
        --
        linkoptions{ "-Wl,-rpath,'$$ORIGIN',--no-as-needed,--no-undefined" }
        -- allow libraries to be listed in any order (do not require dependency order)
        linkgroups "On"

function dump(o)
    if type(o) == 'table' then
        local s = '{ '
        for k,v in pairs(o) do
            if type(k) ~= 'number' then k = '"'..k..'"' end
            s = s .. '['..k..'] = ' .. dump(v) .. ','
        end
        return s .. '} '
    else
        return tostring(o)
    end
end

function dumpTable(o)
    local s = '{ '
    for k,v in pairs(o) do
        if type(k) ~= 'number' then k = '"'..k..'"' end
        s = s .. '['..k..'] = ' .. tostring(v) .. ',\n'
    end
    return s .. '} '
end

function getExecutableSuffix()
    if(os.target() == "windows") then
        return ".exe"
    end
    return ""
end
--
-- We are now going to start defining the projects, where
-- each project builds some binary artifact (an executable,
-- library, etc.).
--
-- All of our projects follow a common structure, so rather
-- than reiterate a bunch of build settings, we define
-- some subroutines that make the configuration as concise
-- as possible.
--
-- First, we will define a helper routine for adding all
-- the relevant files from a given directory path:
--
-- Note that this does not work recursively
-- so projects that spread their source over multiple
-- directories will need to take more steps.
function addSourceDir(path)
    files
    {
        path .. "/*.cpp",       -- C++ source files
        path .. "/*.slang",     -- Slang files (for our stdlib)
        path .. "/*.h",         -- Header files
        path .. "/*.hpp",       -- C++ style headers (for glslang)
        path .. "/*.natvis",    -- Visual Studio debugger visualization files
    }
    removefiles
    {
        "**/*.meta.slang.h",
        "**/slang-*generated*.h",
        "**/gfx-unit-test/test-tmp*"
    }
end

-- Adds CUDA dependency to a project
function addCUDAIfEnabled()
    if type(cudaPath) == "string" and targetInfo.isWindows then
        filter {}
        includedirs { cudaPath .. "/include" }
        includedirs { cudaPath .. "/include", cudaPath .. "/common/inc" }
        links { "cuda", "cudart" }
        if optixPath then
            defines { "RENDER_TEST_OPTIX" }
            includedirs { optixPath .. "include/" }
        end

        filter { "platforms:x86" }
            libdirs { cudaPath .. "/lib/Win32/" }

        filter { "platforms:x64" }
            libdirs { cudaPath .. "/lib/x64/" }
        filter {}
        return true
    elseif enableCuda then
        filter {}
        if type(cudaPath) == "string" then
            includedirs { cudaPath .. "/include" }
            includedirs { cudaPath .. "/include" }
            if optixPath then
                defines { "GFX_OPTIX" }
                includedirs { optixPath .. "include/" }
            end
            filter { "platforms:x86" }
            libdirs { cudaPath .. "/lib32/" }
            filter { "platforms:x64" }
            libdirs { cudaPath .. "/lib64/" }
            filter {}
            links { "cuda", "cudart" }
        else
            print "Error: CUDA is enabled but --cuda-sdk-path is not specified."
        end
        return true
    end
    return false
end

--
-- Next we will define a helper routine that all of our
-- projects will bottleneck through. Here `name` is
-- the name for the project (and the base name for
-- whatever output file it produces), while `sourceDir`
-- is the directory that holds the source.
--
-- E.g., for the `slangc` project, the source code
-- is nested in `source/`, so we'd (indirectly) call:
--
--      baseSlangProject("slangc", "source/slangc")
--
-- NOTE! This function will add any source from the sourceDir, *if* it's specified.
-- Pass nil if adding files is not wanted.
function baseSlangProject(name, sourceDir)

    -- Start a new project in premake. This switches
    -- the "current" project over to the newly created
    -- one, so that subsequent commands affect this project.
    --
    project(name)

    -- We need every project to have a stable UUID for
    -- output formats (like Visual Studio and XCode projects)
    -- that use UUIDs rather than names to uniquely identify
    -- projects. If we don't have a stable UUID, then the
    -- output files might have spurious diffs whenever we
    -- re-run premake generation.

    if sourceDir then
        uuid(os.uuid(name .. '|' .. sourceDir))
    else
        -- If we don't have a sourceDir, the name will have to be enough
        uuid(os.uuid(name))
    end

    -- Location could do with a better name than 'other' - but it seems as if %{cfg.buildcfg:lower()} and similar variables
    -- is not available for location to expand.
    location("build/" .. slangUtil.getBuildLocationName(targetInfo) .. "/" .. name)


    -- The intermediate ("object") directory will use a similar
    -- naming scheme to the output directory, but will also use
    -- the project name to avoid cases where multiple projects
    -- have source files with the same name.
    --
    objdir("intermediate/" .. targetName .. "/%{cfg.buildcfg:lower()}/%{prj.name}")

    -- Treat C++ as the default language, projects in other languages can
    -- override this later
    --
    language "C++"

    -- By default, Premake generates VS project files that
    -- reflect the directory structure of the source code.
    -- While this is nice in principle, it creates messy
    -- results in practice for our projects.
    --
    -- Instead, we will use the `vpaths` feature to imitate
    -- the default VS behavior of grouping files into
    -- virtual subdirectories (VS calls them "filters") for
    -- header and source files respectively.
    --
    -- Note: We are setting `vpaths` using a list of key/value
    -- tables instead of just a key/value table, since this
    -- appears to be an (undocumented) way to fix the order
    -- in which the filters are tested. Otherwise we have
    -- issues where premake will nondeterministically decide
    -- the check something against the `**.cpp` filter first,
    -- and decide that a `foo.cpp.h` file should go into
    -- the `"Source Files"` vpath. That behavior seems buggy,
    -- but at least we appear to have a workaround.
    --
    vpaths {
        { ["Header Files"] = { "**.h", "**.hpp"} },
        { ["Source Files"] = { "**.cpp", "**.slang", "**.natvis" } },
    }

    -- Override default options for a project if necessary

    if overrideModule.addBaseProjectOptions then
        overrideModule.addBaseProjectOptions()
    end

    --
    -- Add the files in the sourceDir
    -- NOTE! This doesn't recursively add files in subdirectories
    --

    if not not sourceDir then
        addSourceDir(sourceDir)
    end

    --
    -- Enable ASAN (address sanitizer) if requested.
    --

    if enableAsan then
        if (targetInfo.isWindows) then
            buildoptions { "/fsanitize=address" }
            flags { "NoIncrementalLink" }
        end
    end

end


-- We can now use the `baseSlangProject()` subroutine to
-- define helpers for the different categories of project
-- in our source tree.
--
-- For example, the Slang project has several tools that
-- are used during building/testing, but don't need to
-- be distributed. These always have their source code in
-- `tools/<project-name>/`.
--
function tool(name)
    -- We use the `group` command here to specify that the
    -- next project we create shold be placed into a group
    -- named "tools" in a generated IDE solution/workspace.
    --
    -- This is used in the generated Visual Studio solution
    -- to group all the tools projects together in a logical
    -- sub-directory of the solution.
    --
    group "tools"

    -- Now we invoke our shared project configuration logic,
    -- specifying that the project lives under the `tools/` path.
    --
    baseSlangProject(name, "tools/" .. name)

    -- Finally, we set the project "kind" to produce a console
    -- application. This is a reasonable default for tools,
    -- and it can be overriden because Premake is stateful,
    -- and a subsequent call to `kind()` would overwrite this
    -- default.
    --
    kind "ConsoleApp"
end

-- "Standard" projects will be those that go to make the binary
-- packages for slang: the shared libraries and executables.
--
function standardProject(name, sourceDir)
    -- Because Premake is stateful, any `group()` call by another
    -- project would still be in effect when we create a project
    -- here (e.g., if somebody had called `tool()` before
    -- `standardProject()`), so we are careful here to set the
    -- group to an emptry string, which Premake treats as "no group."
    --
    group ""

    baseSlangProject(name, sourceDir)
end


function toolSharedLibrary(name)
    group "test-tool"
    -- specifying that the project lives under the `tools/` path.
    --
    baseSlangProject(name .. "-tool", "tools/" .. name)

    defines { "SLANG_SHARED_LIBRARY_TOOL" }

    kind "SharedLib"
end

function exampleLibrary(name)
    group "examples"
    baseSlangProject(name, "examples/"..name)
    kind "StaticLib"
    includedirs { ".", "tools" }
    links { "gfx", "slang", "platform", "gfx-util", "core"}
    addCUDAIfEnabled();
end

exampleLibrary "example-base"

-- Finally we have the example programs that show how to use Slang.
--
function example(name)
    -- Example programs go into an "example" group
    group "examples"

    -- They have their source code under `examples/<project-name>/`
    baseSlangProject(name, "examples/" .. name)

    -- Set up working directory to be the source directory
    debugdir("examples/" .. name)

    -- By default, all of our examples are GUI applications. One some
    -- platforms there is no meaningful distinction between GUI and
    -- command-line applications, but it is significant on Windows and MacOS
    --
    kind "WindowedApp"

    -- Every example needs to be able to include the `slang.h` header
    -- if it is going to use Slang, so we might as well set up a suitable
    -- include path here rather than make each example do it.
    --
    -- Most of the examples also need the `gfx` library,
    -- which lives under `tools/`, so we will add that to the path as well.
    --
    includedirs { ".", "tools" }

    -- The examples also need to link against the slang library,
    -- and the `gfx` abstraction layer (which in turn
    -- depends on the `core` library). We specify all of that here,
    -- rather than in each example.
    links { "example-base", "slang", "gfx", "gfx-util", "platform", "core" }

    if targetInfo.isWindows then
    else
        if enableXlib then
            defines { "SLANG_ENABLE_XLIB" }
            libdirs { "/usr/X11/lib" }
            links {"X11"}
        end
    end

    addCUDAIfEnabled();
end

--
-- Create a project that is used as a build step, typically to
-- build items needed for other dependencies
---

function generatorProject(name, sourcePath, projectKind)
    -- We use the `group` command here to specify that the
    -- next project we create shold be placed into a group
    -- named "generator" in a generated IDE solution/workspace.
    --
    -- This is used in the generated Visual Studio solution
    -- to group all the tools projects together in a logical
    -- sub-directory of the solution.
    --
    group "generator"

    -- Set up the project, but do NOT add any source files.
    baseSlangProject(name, sourcePath)

    -- By default the generator projects run a custom tool and don't
    -- require any premake machinery to compile a binary for them.
    if projectKind == nil then
        kind "Utility"
    else
        kind (projectKind)
    end
end

--
-- With all of these helper routines defined, we can now define the
-- actual projects quite simply. For example, here is the entire
-- declaration of the "Hello, World" example project:
--
example "hello-world"
    kind "ConsoleApp"
    includedirs {"external/vulkan/include"}
--
-- Note how we are calling our custom `example()` subroutine with
-- the same syntax sugar that Premake usually advocates for their
-- `project()` function. This allows us to treat `example` as
-- a kind of specialized "subclass" of `project`
--

-- Let's go ahead and set up the projects for our other example now.
example "triangle"

example "ray-tracing"
example "ray-tracing-pipeline"

example "autodiff-texture"

example "gpu-printing"
    kind "ConsoleApp"

example "shader-toy"

example "model-viewer"

example "shader-object"
    kind "ConsoleApp"

example "cpu-com-example"
    kind "ConsoleApp"

example "cpu-hello-world"
    kind "ConsoleApp"

-- Most of the other projects have more interesting configuration going
-- on, so let's walk through them in order of increasing complexity.
--
-- The `core` project is a static library that has all the basic types
-- and routines that get shared across both the Slang compiler/runtime
-- and the various tool projects. It's build is pretty simple:
--

standardProject("core", "source/core")
    uuid "F9BE7957-8399-899E-0C49-E714FDDD4B65"
    kind "StaticLib"
    -- We need the core library to be relocatable to be able to link with slang.so
    pic "On"

    -- For our core implementation, we want to use the most
    -- aggressive warning level supported by the target, and
    -- to treat every warning as an error to make sure we
    -- keep our code free of warnings.
    --
    warnings "Extra"

    if targetInfo.isWindows then
        addSourceDir "source/core/windows"
    else
        addSourceDir "source/core/unix"
    end

standardProject("compiler-core", "source/compiler-core")
    uuid "12C1E89D-F5D0-41D3-8E8D-FB3F358F8126"
    kind "StaticLib"
    -- We need the compiler-core library to be relocatable to be able to link with slang.so
    pic "On"

    links { "core" }

    -- For our core implementation, we want to use the most
    -- aggressive warning level supported by the target, and
    -- to treat every warning as an error to make sure we
    -- keep our code free of warnings.
    --
    warnings "Extra"

    if targetInfo.isWindows then
        addSourceDir "source/compiler-core/windows"
    else
        addSourceDir "source/compiler-core/unix"
    end

standardProject("slang-rt", "source/slang-rt")
    uuid "DFC79D72-91DE-434C-871B-B3943B488BEB"
    kind "SharedLib"
    pic "On"
    warnings "Extra"
    links {"miniz", "lz4"}
    defines { "SLANG_RT_DYNAMIC", "SLANG_RT_DYNAMIC_EXPORT" }
    addSourceDir "source/core"
    if targetInfo.isWindows then
        addSourceDir "source/core/windows"
    else
        addSourceDir "source/core/unix"
    end

--
-- The cpp extractor is a tool that scans C++ header files to extract
-- reflection like information, and generate files to handle
-- RTTI fast/simply
---

tool "slang-cpp-extractor"
    uuid "CA8A30D1-8FA9-4330-B7F7-84709246D8DC"
    includedirs { "." }

    links { "compiler-core", "core" }

tool "slang-lookup-generator"
    uuid "3242baa7-fc4c-4f76-83bc-e4403099dc1d"
    includedirs { "." }

    links { "compiler-core", "core" }

tool "test-process"
    uuid "BE412850-4BB9-429A-877C-BFBC4B34186C"
    includedirs { "." }

    links { "compiler-core", "core" }

tool "test-server"
    uuid "23149706-C12F-4329-B6AA-8266407C32D3"
    includedirs { "." }

    links { "compiler-core", "core", "slang" }
    
tool "slangd"
    uuid "B2D63B45-92B0-40F7-B242-CCA4DFD64341"
    includedirs { "." }
    links { "compiler-core", "core", "slang" }

--
-- `slang-generate` is a tool we use for source code generation on
-- the compiler. It depends on the `core` library, so we need to
-- declare that:
--

tool "slang-generate"
    uuid "66174227-8541-41FC-A6DF-4764FC66F78E"
    links { "core" }

tool "slang-embed"
    uuid "7F773DD9-EB8F-2403-B43C-B49C2014B99C"
    links { "core" }

--
-- The `slang-test` test driver also uses the `core` library, and it
-- currently relies on include paths being set up so that it can find
-- the core headers:
--

tool "slang-test"
    uuid "0C768A18-1D25-4000-9F37-DA5FE99E3B64"
    includedirs { "." }
    links { "compiler-core", "slang", "core", "miniz", "lz4" }
    dependson { "slang-reflection-test-tool", "render-test-tool", "slang-unit-test-tool", "gfx-unit-test-tool" }
    -- We want to set to the root of the project, but that doesn't seem to work with '.'.
    -- So set a path that resolves to the same place.
    debugdir("source/..")
    if not targetInfo.isWindows then
        links { "pthread" }
    end
    
--
-- The reflection test harness `slang-reflection-test` is pretty
-- simple, in that it only needs to link against the slang library
-- to do its job:
--

toolSharedLibrary "slang-reflection-test"
    uuid "C5ACCA6E-C04D-4B36-8516-3752B3C13C2F"

    includedirs { "." }

    kind "SharedLib"
    links { "core", "slang" }

--
-- The most complex testing tool we have is `render-test`, but from
-- a build perspective the most interesting thing about it is that for
-- our Windows build it requires a Windows 10 SDK.
--
-- TODO: Try to make the build not require a fixed version of the Windows SDK.
-- Ideally we should just specify a *minimum* version.
--
-- This test also requires Vulkan headers which we've placed in the
-- `external/` directory, and it also includes files from the `core`
-- library in ways that require us to set up `source/` as an include path.
--
-- TODO: Fix that requirement.
--

toolSharedLibrary "render-test"
    uuid "61F7EB00-7281-4BF3-9470-7C2EA92620C3"

    includedirs { ".", "external", "source", "tools/gfx", "tools/platform" }
    links { "core", "compiler-core", "slang", "gfx", "gfx-util", "platform" }
    if targetInfo.isWindows then
        addSourceDir "tools/render-test/windows"

        systemversion "latest"

        -- For Windows targets, we want to copy
        -- dxcompiler.dll, and dxil.dll from the Windows SDK redistributable
        -- directory into the output directory.
        -- d3dcompiler_47.dll is copied from the external/slang-binaries submodule.
        postbuildcommands { '"$(SolutionDir)tools\\copy-hlsl-libs.bat" "$(WindowsSdkDir)Redist/D3D/%{cfg.platform:lower()}/" "%{cfg.targetdir}/" "windows-%{cfg.platform:lower()}"'}
        if (type(cudaPath) == "string") then
            addSourceDir "tools/render-test/cuda"
        end
    end
    if addCUDAIfEnabled() then
        defines { "RENDER_TEST_CUDA" }
    end

--
-- `gfx` is a abstraction layer for different GPU platforms.
--

tool "gfx"
    uuid "222F7498-B40C-4F3F-A704-DDEB91A4484A"
    -- Unlike most of the code under `tools/`, this is a library
    -- rather than a stand-alone executable.
    kind "SharedLib"
    links { "core", "slang" }
    pic "On"

    defines { "SLANG_GFX_DYNAMIC", "SLANG_GFX_DYNAMIC_EXPORT" }

    includedirs { ".", "external", "source" }

    files {"slang-gfx.h"}

    -- Will compile across targets
    addSourceDir "tools/gfx/cpu"
    addSourceDir "tools/gfx/nvapi"
    addSourceDir "tools/gfx/cuda"
    addSourceDir "tools/gfx/debug-layer"
    if targetInfo.isWindows then
        postbuildcommands {
            '{COPY} "$(SolutionDir)tools/gfx/gfx.slang" "%{cfg.targetdir}"',
            '{COPY} "$(SolutionDir)tools/gfx/slang.slang" "%{cfg.targetdir}"'
        }
    else
        postbuildcommands {
            '{COPY} "' .. path.getabsolute("tools/gfx/gfx.slang") .. '" "%{cfg.targetdir}"',
            '{COPY} "' .. path.getabsolute("tools/gfx/slang.slang") .. '" "%{cfg.targetdir}"',
        }
    end
    -- To special case that we may be building using cygwin on windows. If 'true windows' we build for dx12/vk and run the script
    -- If not we assume it's a cygwin/mingw type situation and remove files that aren't appropriate
    if targetInfo.isWindows then
        systemversion "latest"

        -- For Windows targets, we want to copy
        -- dxcompiler.dll, and dxil.dll from the Windows SDK redistributable
        -- directory into the output directory.
        -- d3dcompiler_47.dll is copied from the external/slang-binaries submodule.
        postbuildcommands { '"$(SolutionDir)tools\\copy-hlsl-libs.bat" "$(WindowsSdkDir)Redist/D3D/%{cfg.platform:lower()}/" "%{cfg.targetdir}/"'}

        addSourceDir "tools/gfx/vulkan"
        addSourceDir "tools/gfx/open-gl"
        addSourceDir "tools/gfx/d3d"
        addSourceDir "tools/gfx/d3d11"
        addSourceDir "tools/gfx/d3d12"
    elseif targetInfo.os == "mingw" or targetInfo.os == "cygwin" then
        -- Don't support any render techs...
    elseif os.target() == "macosx" then
        --addSourceDir "tools/gfx/open-gl"
    else
        -- Linux like
        addSourceDir "tools/gfx/vulkan"
        --addSourceDir "tools/gfx/open-gl"
    end

    if enableXlib then
        defines { "SLANG_ENABLE_XLIB" }
        libdirs { "/usr/X11/lib" }
        links {"X11"}
    end

    -- If NVAPI is enabled
    if enableNvapi then
        -- Add the include path
        includedirs { nvapiPath }

        -- Add a define so that render-test code can check if nvapi is available
        defines { "GFX_NVAPI" }

        -- Set the nvapi libs directory
        filter { "platforms:x86" }
            libdirs { nvapiPath .. "/x86" }
            links { "nvapi" }

        filter { "platforms:x64" }
            libdirs { nvapiPath .. "/amd64" }
            links { "nvapi64" }

    end
    if addCUDAIfEnabled() then
        defines { "GFX_ENABLE_CUDA" }
    end
    
--
-- `gfx-util` is a static library containing utilities and helpers for using
-- the `gfx` library.
--
tool "gfx-util"
    uuid "F5ADB74E-02A7-44FB-AA3B-FC02F8AC7A4B"
    kind "StaticLib"
    pic "On"

    includedirs { ".", "source" }

    addSourceDir "tools/gfx-util"
--
-- `platform` contains all the platform abstractions for a GUI application.
--
tool "platform"
    uuid "3565fe5e-4fa3-11eb-ae93-0242ac130002"
    kind "SharedLib"
    pic "On"
    links {"core", "slang", "gfx" }
    defines { "SLANG_PLATFORM_DYNAMIC", "SLANG_PLATFORM_DYNAMIC_EXPORT" }
    includedirs { ".", "external", "source", "external/imgui", "tools/gfx" }
    addSourceDir "tools/platform"
    addSourceDir "tools/platform/linux"
    addSourceDir "tools/platform/windows"
    addSourceDir "tools/platform/placeholder"
    -- Include windowing support on Windows.
    if targetInfo.isWindows then
        systemversion "latest"
    else
        if enableXlib then
            defines { "SLANG_ENABLE_XLIB" }
            libdirs { "/usr/X11/lib" }
            links {"X11"}
        end
    end

--
-- The `slangc` command-line application is just a very thin wrapper
-- around the Slang dynamic library, so its build is extermely simple.
-- One windows `slangc` uses the the `core` library for some UTF-16
-- to UTF-8 string conversion before calling into `slang.dll`, so
-- it also depends on `core`:
--

standardProject("slangc", "source/slangc")
    uuid "D56CBCEB-1EB5-4CA8-AEC4-48EA35ED61C7"
    kind "ConsoleApp"
    links { "core", "slang" }

function getBuildDir(isArm64)
    return "%{cfg.targetdir}"
end

function astReflectGenerator(isArm64)
    local builddir = getBuildDir()

    filter("files:**/slang-ast-reflect.h")

    buildmessage "C++ Extractor %{file.relpath}"

    local sourcePath = "%{file.directory}"

    -- Work out the output files

    local outputTypes = { "obj", "ast", "value" };

    local outputTable = {}

    for key, outputType in ipairs(outputTypes) do
        table.insert(outputTable, sourcePath .. "/slang-generated-" .. outputType .. ".h")
        table.insert(outputTable, sourcePath .. "/slang-generated-" .. outputType .. "-macro.h")
    end

    -- List all of the input files to be scanned

    local inputFiles = { "slang-ast-support-types.h", "slang-ast-base.h", "slang-ast-decl.h", "slang-ast-expr.h", "slang-ast-modifier.h", "slang-ast-stmt.h", "slang-ast-type.h", "slang-ast-val.h" }

    local options = { "-strip-prefix", "slang-", "-o", "slang-generated", "-output-fields", "-mark-suffix", "_CLASS"}

    -- Specify the actual command to run for this action.
    --
    -- Note that we use a single-quoted Lua string and wrap the path
    -- to the `slang-cpp-extractor` command in double quotes to avoid
    -- confusing the Windows shell. It seems that Premake outputs that
    -- path with forward slashes, which confused the shell if we don't
    -- quote the executable path.

    local buildcmd = '"' .. builddir .. '/slang-cpp-extractor" -d ' .. sourcePath .. " " .. table.concat(inputFiles, " ") .. " " .. table.concat(options, " ")

    buildcommands { buildcmd }

    -- Specify the files output by the extactor - so custom action will run when these files are needed.
    --
    buildoutputs(outputTable)

    -- Make it depend on the extractor tool itself
    local buildInputTable = { builddir .. "/slang-cpp-extractor" .. getExecutableSuffix() }
    for key, inputFile in ipairs(inputFiles) do
        table.insert(buildInputTable, sourcePath .. "/" .. inputFile)
    end

    --
    buildinputs(buildInputTable)
end

function metaSlangGenerator()
    local builddir = getBuildDir()

    filter("files:**.meta.slang")

    -- Specify the "friendly" message that should print to the build log for the action
    buildmessage "slang-generate %{file.relpath}"

    -- Specify the actual command to run for this action.
    --
    -- Note that we use a single-quoted Lua string and wrap the path
    -- to the `slang-generate` command in double quotes to avoid
    -- confusing the Windows shell. It seems that Premake outputs that
    -- path with forward slashes, which confused the shell if we don't
    -- quote the executable path.
    --
    buildcommands { '"' .. builddir .. '/slang-generate" %{file.relpath}' }

    -- Given `foo.meta.slang` we woutput `foo.meta.slang.h`.
    -- This needs to be specified because the custom action will only
    -- run when this file needs to be generated.
    --
    -- Note the use of abspath here, this ensures windows tests the correct file, otherwise
    -- triggering doesn't work. The problem still remains on linux, because abspath *isn't* an
    -- absolute path, it remains relative.
    --
    -- TODO(JS):
    -- It's not clear how to determine how to create the absolute path on linux, using
    -- path.absolutepath, requires knowing the path to be relative to, and it's neither
    -- the current path, the source path or the targetpath.
    buildoutputs { "%{file.abspath}.h" }

    -- We will specify an additional build input dependency on the `slang-generate`
    -- tool itself, so that changes to the code for the tool cause the generation
    -- step to be re-run.
    --
    -- In order to get the file name right, we need to know the executable suffix
    -- that the target platform will use. Premake might have a built-in way to
    -- query this, but I couldn't find it, so I am just winging it for now:
    --
    --
    buildinputs { builddir .. "/slang-generate" .. getExecutableSuffix() }
end

function preludeGenerator()
    local builddir = getBuildDir()

    filter("files:prelude/*-prelude.h")

    buildmessage "slang-embed %{file.relpath}"
    buildcommands { '"' .. builddir .. '/slang-embed" %{file.relpath}' }
    buildoutputs { "%{file.abspath}.cpp" }
    buildinputs { builddir .. "/slang-embed" .. getExecutableSuffix() }
end

if not skipSourceGeneration then

generatorProject("run-generators", nil)

    -- We make 'source/slang' the location of the source, to make paths to source
    -- relative to that

    -- We include these, even though they are not really part of the dummy
    -- build, so that the filters below can pick up the appropriate locations.

    files
    {
        "source/slang/*.meta.slang",                -- The stdlib files
        "source/slang/slang-ast-reflect.h",         -- C++ reflection
        "prelude/*.h",                              -- The prelude files

        --
        -- To build we need to have some source! It has to be a source file that
        -- does not depend on anything that is generated, so we take something
        -- from core that will compile without any generation.
        --

        "source/core/slang-string.cpp",
    }

    -- First, we need to ensure that various source-generation tools
    -- get built before `slang`, so we declare a non-linking dependency between
    -- the projects here:
    dependson { "slang-cpp-extractor", "slang-generate", "slang-embed" }

    local executableSuffix = getExecutableSuffix()

    -- We need to run the C++ extractor to generate some include files
    if executeBinary then
        astReflectGenerator()
    end

    -- Next, we want to add a custom build rule for each of the
    -- files that makes up the standard library. Those are
    -- always named `*.meta.slang`, so we can select for them
    -- using a `filter` and then use Premake's support for
    -- defining custom build commands:
    --
    if executeBinary then
        metaSlangGenerator()
        preludeGenerator()
    end

    filter { }

generatorProject("generate-lookup-tables")
    tables = {
        {
            json = "external/spirv-headers/include/spirv/unified1/extinst.glsl.std.450.grammar.json",
            header = "spirv/unified1/GLSL.std.450.h",
            prefix = "GLSLstd450",
            type = "GLSLstd450"
        },
        {
            json = "external/spirv-headers/include/spirv/unified1/spirv.core.grammar.json",
            header = "spirv/unified1/spirv.h",
            prefix = "Spv",
            type = "SpvOp"
        }
    }
    for _, t in pairs(tables) do
        files {t.json}
    end

    dependson { "slang-lookup-generator" }

    local builddir = getBuildDir()
    if executeBinary then
        for _, t in pairs(tables) do
            filter("files:" .. t.json)

            local inJson = "%{file.abspath}"
            local cppFilename = "slang-lookup-" .. t.type:lower() .. ".cpp"
            local cppPath = "%{wks.location}/source/slang/" .. cppFilename
            local buildcmd = '"' .. builddir .. '/slang-lookup-generator" '
                .. inJson .. " "
                .. cppPath .. " "
                .. t.type .. " "
                .. t.prefix .. " "
                .. t.header

            buildmessage ("slang-lookup-generator for " .. cppFilename)
            buildcommands { buildcmd }
            buildinputs { inJson, builddir .. "/slang-lookup-generator" .. getExecutableSuffix() }
            buildoutputs (cppPath)
        end
    end

    filter { }

if enableEmbedStdLib then
    standardProject("slangc-bootstrap", "source/slangc")
        uuid "6339BF31-AC99-4819-B719-679B63451EF0"
        kind "ConsoleApp"
        links { "core", "compiler-core", "miniz", "lz4" }

        -- We need to run all the generators to be able to build the main
        -- slang source in source/slang

        dependson { "run-generators" }

        defines {
            -- We are going statically link Slang compiler with the slangc command line
            "SLANG_STATIC",
            -- This is the bootstrap to produce the embedded stdlib, so we disable to be able to bootstrap
            "SLANG_WITHOUT_EMBEDDED_STD_LIB"
        }

        includedirs { "external/spirv-headers/include" }

        -- Add all of the slang source
        addSourceDir "source/slang"

        -- On some tests with MSBuild disabling these made build work.
        -- flags { "NoIncrementalLink", "NoPCH", "NoMinimalRebuild" }

        -- The `standardProject` operation already added all the code in
        -- `source/slang/*`, but we also want to incldue the umbrella
        -- `slang.h` header in this prject, so we do that manually here.
        files { "slang.h" }

        files { "source/core/core.natvis" }

        -- We explicitly name the prelude file(s) that we need to
        -- compile for their embedded code, since they will not
        -- exist at the time projects/makefiles are generated,
        -- and thus a glob would not match anything.
        files {
            "prelude/slang-cuda-prelude.h.cpp",
            "prelude/slang-hlsl-prelude.h.cpp",
            "prelude/slang-cpp-prelude.h.cpp",
            "prelude/slang-cpp-host-prelude.h.cpp",
            "prelude/slang-torch-prelude.h.cpp"
        }
end

if enableEmbedStdLib then
    generatorProject("embed-stdlib-generator", nil, "SharedLib")

        -- We include these, even though they are not really part of the dummy
        -- build, so that the filters below can pick up the appropriate locations.

        files
        {
            --
            -- To build we need to have some source! It has to be a source file that
            -- does not depend on anything that is generated, so we take something
            -- from core that will compile without any generation.
            --

            "source/slang/slang-stdlib-api.cpp",
        }

        -- Only produce the embedded stdlib if that option is enabled

        local executableSuffix = getExecutableSuffix()

        -- We need slangc-bootstrap to build the embedded stdlib
        dependson { "slangc-bootstrap" }

        local absDirectory = path.getabsolute("source/slang")
        local absOutputPath = absDirectory .. "/slang-stdlib-generated.h"

        -- I don't know why I need a filter, but without it nothing works (!)
        filter("files:source/slang/slang-stdlib-api.cpp")
        -- Note! Has to be an absolute path else doesn't work(!)
        buildoutputs { absOutputPath }
        filter("files:source/slang/slang-stdlib-api.cpp")
    
        filter(f)
            buildinputs { "%{cfg.targetdir}/slangc-bootstrap" .. executableSuffix }
            buildcommands { '"%{cfg.targetdir}/slangc-bootstrap" -archive-type riff-lz4 -save-stdlib-bin-source "%{file.directory}/slang-stdlib-generated.h"' }
end
end -- not skipSourceGeneration

--
-- TODO: Slang's current `Makefile` build does some careful incantations
-- to make sure that the binaries it generates use a "relative `RPATH`"
-- for loading shared libraries, so that Slang is not dependent on
-- being installed to a fixed path on end-user machines. Before we
-- can use Premake for the Linux build (or eventually MacOS) we would
-- need to figure out how to replicate this incantation in premake.
--

--
-- Now that we've gotten all the simple projects out of the way, it is time
-- to get into the more serious build steps.
--
-- First up is the `slang` dynamic library project:
--

standardProject("slang", "source/slang")
    uuid "DB00DA62-0533-4AFD-B59F-A67D5B3A0808"
    kind "SharedLib"
    links { "core", "compiler-core", "miniz", "lz4"}
    warnings "Extra"
    pic "On"

    -- The way that we currently configure things through `slang.h`,
    -- we need to set a preprocessor definitions to ensure that
    -- we declare the Slang API functions for *export* and not *import*.
    --
    defines { "SLANG_DYNAMIC_EXPORT" }

    if disableStdlibSource then
        defines { "SLANG_DISABLE_STDLIB_SOURCE" }
    end

    if fullDebugValidation then
        defines { "SLANG_ENABLE_FULL_IR_VALIDATION" }
    end

    if enableEmbedStdLib then
        -- We only have this dependency if we are embedding stdlib
        if not skipSourceGeneration then
            dependson { "embed-stdlib-generator" }
        end
    else
        -- Disable StdLib embedding
        defines { "SLANG_WITHOUT_EMBEDDED_STD_LIB" }
    end

    includedirs { "external/spirv-headers/include" }

    -- On some tests with MSBuild disabling these made build work.
    -- flags { "NoIncrementalLink", "NoPCH", "NoMinimalRebuild" }

    -- The `standardProject` operation already added all the code in
    -- `source/slang/*`, but we also want to incldue the umbrella
    -- `slang.h` header in this prject, so we do that manually here.
    files { "slang.h" }

    files { "source/core/core.natvis" }

    -- We explicitly name the prelude file(s) that we need to
    -- compile for their embedded code, since they will not
    -- exist at the time projects/makefiles are generated,
    -- and thus a glob would not match anything.
    files {
        "prelude/slang-cuda-prelude.h.cpp",
        "prelude/slang-hlsl-prelude.h.cpp",
        "prelude/slang-cpp-prelude.h.cpp",
        "prelude/slang-cpp-host-prelude.h.cpp",
        "prelude/slang-torch-prelude.h.cpp"
    }

    -- Similarly for any generated lookup tables
    files {
        "source/slang/slang-lookup-spvop.cpp",
        "source/slang/slang-lookup-glslstd450.cpp"
    }

    --
    -- The most challenging part of building `slang` is that we need
    -- to invoke generators such as slang-cpp-extractor and slang-generate
    -- to generate. We do this by executing the run-generators 'dummy' project
    -- which produces the appropriate source

    if not skipSourceGeneration then
        dependson { "run-generators" }
        dependson { "generate-lookup-tables" }
    end

    -- If we have slang-llvm copy it
    local slangLLVMPath = deps:getProjectRelativePath("slang-llvm", "../../..")
    
    if slangLLVMPath and deployLLVM then
        filter { "system:linux or macosx or windows" }
            local sharedLibName = slangUtil.getSharedLibraryFileName(targetInfo, "slang-llvm")            
            postbuildcommands {
                "{COPY} " .. slangLLVMPath .."/bin/" .. targetName .. "/release/" .. sharedLibName .. " %{cfg.targetdir}"
            }
    end

    local slangGlslangPath = deps:getProjectRelativePath("slang-glslang", "../../..")

    -- If we are not building glslang from source, then be
    -- sure to copy a binary copy over to the output directory
    if not buildGlslang and slangGlslangPath~=nil and deployGLSLang then
        filter { "system:linux or macosx or windows" }
            local sharedLibName = slangUtil.getSharedLibraryFileName(targetInfo, "slang-glslang")            
            postbuildcommands {
                "{COPY} " .. slangGlslangPath .. "/bin/" .. targetName .. "/release/" .. sharedLibName .. " %{cfg.targetdir}"
            }        
    end

    filter {"configurations:debug"}
        defines { "SLANG_ENABLE_IR_BREAK_ALLOC=1" }
    filter {}

toolSharedLibrary "gfx-unit-test"
    uuid "092DAB9F-1DA5-4538-ADD7-1A8D1DBFD519"
    includedirs { "." }
    addSourceDir "tools/unit-test"
    links {  "core", "slang", "gfx", "gfx-util", "platform" }

toolSharedLibrary "slang-unit-test"
    uuid "0162864E-7651-4B5E-9105-C571105276EA"
    includedirs { "." }
    addSourceDir "tools/unit-test"
    links { "lz4", "miniz", "core", "compiler-core",  "slang" }
    if not targetInfo.isWindows then
        links { "pthread" }
    end

if enableProfile then
    tool "slang-profile"
        uuid "375CC87D-F34A-4DF1-9607-C5C990FD6227"

        -- gprof needs symbols
        symbols "On"

        dependson { "slang" }

        includedirs { "external/spirv-headers/include" }

        defines { "SLANG_STATIC",
            -- Disable StdLib embedding
            "SLANG_WITHOUT_EMBEDDED_STD_LIB"
        }

        -- The `standardProject` operation already added all the code in
        -- `source/slang/*`, but we also want to incldue the umbrella
        -- `slang.h` header in this prject, so we do that manually here.
        files { "slang.h" }

        files { "source/core/core.natvis" }

        -- We explicitly name the prelude file(s) that we need to
        -- compile for their embedded code, since they will not
        -- exist at the time projects/makefiles are generated,
        -- and thus a glob would not match anything.
        files {
            "prelude/slang-cuda-prelude.h.cpp",
            "prelude/slang-hlsl-prelude.h.cpp",
            "prelude/slang-cpp-prelude.h.cpp",
            "prelude/slang-cpp-host-prelude.h.cpp",
            "prelude/slang-torch-prelude.h.cpp"
        }

        -- Add the slang source
        addSourceDir "source/slang"

        includedirs { "." }
        links { "core", "compiler-core", "miniz", "lz4"}

        filter { "system:linux" }
            linkoptions{  "-pg" }
            buildoptions{ "-pg" }

end

standardProject("miniz", nil)
    uuid "E76ACB11-4A12-4F0A-BE1E-CE0B8836EB7F"
    kind "StaticLib"
    pic "On"

    -- Add the files explicitly
    language "C"
    files
    {
        "external/miniz/miniz.c",
        "external/miniz/miniz_tdef.c",
        "external/miniz/miniz_tinfl.c",
        "external/miniz/miniz_zip.c"
    }
    filter { "system:linux" }
        defines { "_LARGEFILE64_SOURCE" }

    filter { "system:linux or macosx" }
        links { "dl"}

standardProject("lz4", nil)
    uuid "E1EC8075-823E-46E5-BC38-C124CCCDF878"
    kind "StaticLib"
    pic "On"

    -- Add the files explicitly
    language "C"
    files
    {
        "external/lz4/lib/lz4.c",
        "external/lz4/lib/lz4.h",
    }

    filter { "system:linux or macosx" }
        links { "dl"}

if buildGlslang then

standardProject("slang-spirv-tools", nil)
    uuid "C36F6185-49B3-467E-8388-D0E9BF5F7BB8"
    kind "StaticLib"
    pic "On"

    includedirs { "external/spirv-tools", "external/spirv-tools/include", "external/spirv-headers/include",  "external/spirv-tools-generated"}

    addSourceDir("external/spirv-tools/source")
    addSourceDir("external/spirv-tools/source/opt")
    addSourceDir("external/spirv-tools/source/util")
    addSourceDir("external/spirv-tools/source/val")

    filter { "system:linux or macosx" }
        links { "dl"}
--
-- The single most complicated part of our build is our custom version of glslang.
-- Is not really set up to produce a shared library with a usable API, so we have
-- our own custom shim API around it to invoke GLSL->SPIRV compilation.
--
-- Glslang normally relies on a CMake-based build process, and its code is spread
-- across multiple directories with implicit dependencies on certain command-line
-- definitions.
--
-- The following is a tailored build of glslang that pulls in the pieces we care
-- about whle trying to leave out the rest:
--
standardProject("slang-glslang", "source/slang-glslang")
    uuid "C495878A-832C-485B-B347-0998A90CC936"
    kind "SharedLib"
    pic "On"

    includedirs { "external/glslang", "external/spirv-tools", "external/spirv-tools/include", "external/spirv-headers/include",  "external/spirv-tools-generated", "external/glslang-generated" }

    defines
    {
        -- `ENABLE_OPT` must be defined (to either zero or one) for glslang to compile at all
        "ENABLE_OPT=1",

        -- We want to build a version of glslang that supports every feature possible,
        -- so we will enable all of the supported vendor-specific extensions so
        -- that they can be used in Slang-generated GLSL code when required.
        --
        "AMD_EXTENSIONS",
        "NV_EXTENSIONS",
    }

    -- We will add source code from every directory that is required to get a
    -- minimal GLSL->SPIR-V compilation path working.
    addSourceDir("external/glslang/glslang/GenericCodeGen")
    addSourceDir("external/glslang/glslang/MachineIndependent")
    addSourceDir("external/glslang/glslang/MachineIndependent/preprocessor")
    addSourceDir("external/glslang/OGLCompilersDLL")
    addSourceDir("external/glslang/SPIRV")
    addSourceDir("external/glslang/StandAlone")

    -- Unfortunately, blindly adding files like that also pulled in a declaration
    -- of a main entry point that we do *not* want, so we will specifically
    -- exclude that file from our build.
    removefiles { "external/glslang/StandAlone/StandAlone.cpp" }

    -- Glslang includes some platform-specific code around DLL setup/teardown
    -- and handling of thread-local storage for its multi-threaded mode. We
    -- don't really care about *any* of that, but we can't remove it from the
    -- build so we need to include the appropriate platform-specific sources.

    links { "slang-spirv-tools"}

    filter { "system:windows" }
        -- On Windows we need to add the platform-specific sources and then
        -- remove the `main.cpp` file since it tries to define a `DllMain`
        -- and we don't want the default glslang one.
        addSourceDir( "external/glslang/glslang/OSDependent/Windows")
        removefiles { "external/glslang/glslang/OSDependent/Windows/main.cpp" }

    filter { "system:linux or macosx" }
        addSourceDir( "external/glslang/glslang/OSDependent/Unix")
        links { "dl", "pthread" }

--
-- With glslang's build out of the way, we've now covered everything we have
-- to build to get Slang and its tools/examples built.
--
-- What is not included in this file yet is support for any custom `make`
-- targets for:
--
-- * Invoking the test runner
-- * Packaging up binaries
-- * "Installing" Slang on a user's machine
--

end