Repository navigation
Expand file tree
/
Copy pathninja.lua
More file actions
1106 lines (927 loc) · 38.4 KB
/
Copy pathninja.lua
File metadata and controls
1106 lines (927 loc) · 38.4 KB
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
--
-- Name: premake-ninja/ninja.lua
-- Purpose: Define the ninja action.
-- Author: Dmitry Ivanov
-- Modified by: Jan "GamesTrap" Schürkamp
-- Created: 2015/07/04
-- Updated: 2026/10/09
-- Copyright: (c) 2015 Dmitry Ivanov, (c) 2023-2026 Jan "GamesTrap" Schürkamp
--
local p = premake
local tree = p.tree
local project = p.project
local config = p.config
local fileconfig = p.fileconfig
-- Some toolset fixes/helper
p.tools.clang.objectextension = ".o"
p.tools.gcc.objectextension = ".o"
p.tools.msc.objectextension = ".obj"
p.tools.clang.tools.rc = p.tools.clang.tools.rc or "windres"
p.tools.msc.gettoolname = function(cfg, name)
local map = {cc = "cl", cxx = "cl", ar = "lib", rc = "rc"}
return map[name]
end
-- Ninja module
premake.modules.ninja = {}
local ninja = p.modules.ninja
ninja.handlers = {}
local function registerHandler(kind, c_cpp_compilationRules, target_rules)
ninja.handlers[kind] = {c_cpp_compilationRules = c_cpp_compilationRules, target_rules = target_rules}
end
local function getKey(cfg)
local name = name or cfg.project.name
if cfg.platform then
return string.format('%s_%s_%s', name, cfg.buildcfg, cfg.platform)
end
return string.format('%s_%s', name, cfg.buildcfg)
end
local function list(value)
if #value > 0 then
return " " .. table.concat(value, " ")
else
return ""
end
end
local function escape(value)
value = value:gsub("%$", "$$") -- TODO maybe there is better way
value = value:gsub(":", "$:")
value = value:gsub("\n", "$\n")
value = value:gsub(" ", "$ ")
return value
end
local build_cache = {}
local function addBuild(cfg, out, implicit_outputs, command, inputs, implicit_inputs, dependencies, vars)
implicit_outputs = list(table.translate(implicit_outputs, escape))
if #implicit_outputs > 0 then
implicit_outputs = " |" .. implicit_outputs
else
implicit_outputs = ""
end
inputs = list(table.translate(inputs, escape))
implicit_inputs = list(table.translate(implicit_inputs, escape))
if #implicit_inputs > 0 then
implicit_inputs = " |" .. implicit_inputs
else
implicit_inputs = ""
end
dependencies = list(table.translate(dependencies, escape))
if #dependencies > 0 then
dependencies = " ||" .. dependencies
else
dependencies = ""
end
build_line = "build " .. escape(out) .. implicit_outputs .. ": " .. command .. inputs .. implicit_inputs .. dependencies
local cached = build_cache[out]
if cached ~= nil then
if build_line == cached.build_line and table.equals(vars or {}, cached.vars or {}) then
-- custom_command/copy rules are identical for each configuration (contrary to other rules)
-- So we can compare extra parameter
if command == "custom_command" or command == "copy" then
p.outln("# INFO: Rule ignored, same as " .. cached.cfg_key)
else
local cfg_key = cfg and getKey(cfg) or "Global scope"
p.warn(cached.cfg_key .. " and " .. cfg_key .. " both generate (differently?) " .. out .. ". Ignoring " .. cfg_key)
p.outln("# WARNING: Rule ignored, using the one from .." .. cached.cfg_key)
end
else
local cfg_key = cfg and getKey(cfg) or "Global scope"
p.warn(cached.cfg_key .. " and " .. cfg_key .. " both generate differently " .. out .. ". Ignoring " .. cfg_key)
p.outln("# ERROR: Rule ignored, using the one from " .. cached.cfg_key)
end
p.outln("# " .. build_line)
for i, var in ipairs(vars or {}) do
p.outln("# " .. var)
end
return
end
p.outln(build_line)
for i, var in ipairs(vars or {}) do
p.outln(" " .. var)
end
build_cache[out] =
{
cfg_key = cfg and getKey(cfg) or "Global scope",
build_line = build_line,
vars = vars
}
end
local function emitRule(name, cmds, description, opts)
opts = opts or {}
p.outln("rule " .. name)
p.outln(" command = " .. table.concat(cmds, " &&$\n "))
p.outln(" description = " .. description)
for key, value in pairs(opts) do
p.outln(" " .. key .. " = " .. value)
end
p.outln("")
end
local function emitFlags(name, value)
p.outln(name .. " =" .. value)
end
local function quote(value)
value = value:gsub("\\", "\\\\")
value = value:gsub("'", "\\'")
value = value:gsub('"', '\\"')
return '"' .. value .. '"'
end
-- In some cases we write file names in rule commands directly
-- so we need to propely escape them
local function shellEscape(value)
if type(value) == "table" then
return table.translate(value, shellEscape)
end
if value:find(" ") or value:find('"') or value:find('(', 1, true) or value:find(')') or value:find('|') or value:find('&') then
return quote(value)
end
return value
end
local function canGenerate(prj)
return p.action.supports(prj.kind) and prj.kind ~= p.NONE
end
-- return name of build file for configuration
local function projectCfgFilename(cfg, relative)
if relative ~= nil then
relative = project.getrelative(cfg.workspace, cfg.location) .. "/"
else
relative = ""
end
return relative .. getKey(cfg, cfg.project.filename) .. ".ninja"
end
-- Generate solution that will call ninja for projects
function ninja.generateWorkspace(wks)
local oldGetDefaultSeparator = path.getDefaultSeparator
path.getDefaultSeparator = function()
return "/"
end
p.outln("# solution build file")
p.outln("# generated with premake ninja")
p.outln("")
p.outln("# build projects")
local cfgs = {} -- key is concatenated name or variant name, value is string of outputs names
local key = ""
local cfgOrder = {} -- build configuration names in declaration order
local cfg_first = nil
local cfg_first_lib = nil
local cfg_start = nil
local subninjas = {}
for prj in p.workspace.eachproject(wks) do
if canGenerate(prj) then
for cfg in p.project.eachconfig(prj) do
key = getKey(cfg)
if not cfgs[cfg.buildcfg] then
cfgs[cfg.buildcfg] = {}
table.insert(cfgOrder, cfg.buildcfg)
end
table.insert(cfgs[cfg.buildcfg], key)
-- set first configuration name (the start project takes precedence)
if cfg_start == nil and prj.name == wks.startproject then
if wks.defaultplatform == nil or cfg.platform == wks.defaultplatform then
cfg_start = key
end
end
if wks.defaultplatform == nil then
if (cfg_first == nil) and (cfg.kind == p.CONSOLEAPP or cfg.kind == p.WINDOWEDAPP) then
cfg_first = key
end
end
if (cfg_first_lib == nil) and (cfg.kind == p.STATICLIB or cfg.kind == p.SHAREDLIB) then
cfg_first_lib = key
end
-- include other ninja file
table.insert(subninjas, escape(projectCfgFilename(cfg, true)))
p.outln("subninja " .. escape(projectCfgFilename(cfg, true)))
end
end
end
cfg_first = cfg_start or cfg_first or cfg_first_lib
p.outln("")
p.outln("# targets")
for _, cfg in ipairs(cfgOrder) do
local outputs = cfgs[cfg]
p.outln("build " .. escape(cfg) .. ": phony" .. list(table.translate(outputs, escape)))
end
p.outln("")
if wks.editorintegration then
-- we need to filter out the 'file' argument, since we already output the script separately
local args = {}
for _, arg in ipairs(_ARGV) do
if not (arg:startswith('--file') or arg:startswith('/file')) then
table.insert(args, arg)
end
end
table.sort(args)
p.outln("# Rule")
emitRule('premake', {shellEscape(p.workspace.getrelative(wks, _PREMAKE_COMMAND)) .. ' --file=$in ' .. table.concat(shellEscape(args), ' ')}, 'run premake', {generator = 'true', restat = 'true'})
addBuild(nil, 'build.ninja', subninjas, 'premake', {p.workspace.getrelative(wks, _MAIN_SCRIPT_DIR)}, {}, {}, {})
p.outln('')
end
if cfg_first then
p.outln("# default target")
p.outln("default " .. escape(cfg_first))
p.outln("")
end
path.getDefaultSeparator = oldGetDefaultSeparator
end
local function shouldCompileAsC(filecfg)
if filecfg.compileas and filecfg.compileas ~= "Default" then
return p.languages.isc(filecfg.compileas)
end
return path.iscfile(filecfg.abspath)
end
local function shouldCompileAsCpp(filecfg)
if filecfg.compileas and filecfg.compileas ~= "Default" then
return p.languages.iscpp(filecfg.compileas)
end
return path.iscppfile(filecfg.abspath)
end
local function getFileDependencies(cfg)
local dependencies = {}
if #cfg.prebuildcommands > 0 or cfg.prebuildmessage then
dependencies = {"prebuild_" .. getKey(cfg)}
end
for i = 1, #cfg.dependson do
local dependpostfix = ''
if cfg.platform then
dependpostfix = '_' .. cfg.platform
end
table.insert(dependencies, cfg.dependson[i] .. "_" .. cfg.buildcfg .. dependpostfix)
end
return dependencies
end
-- Ninja requires that all files are relative to the build directory (premakes workspace location).
-- Premakes toolset functions resolve paths relative to the project location, so we have
-- to temporarily point it at the workspace location
local function relativeToWorkspace(cfg, func)
local prjLocation = cfg.project.location
cfg.project.location = cfg.workspace.location
local a, b = func()
cfg.project.locaton = projectLocation
return a, b
end
local function getCFlags(toolset, cfg, filecfg)
p.escaper(shellEscape)
local buildopt = list(filecfg.buildoptions)
local cppflags = list(toolset.getcppflags(filecfg))
local cflags = list(toolset.getcflags(filecfg))
local defines = list(table.join(toolset.getdefines(filecfg.defines, filecfg), toolset.getundefines(filecfg.undefines)))
local includes = relativeToWorkspace(cfg, function() return list(toolset.getincludedirs(cfg, filecfg.includedirs, filecfg.externalincludedirs, filecfg.frameworkdirs, filecfg.includedirsafter)) end)
local forceincludes = relativeToWorkspace(cfg, function() return list(toolset.getforceincludes(cfg)) end)
p.escaper(nil)
return buildopt .. cppflags .. cflags .. defines .. includes .. forceincludes
end
local function getCXXFlags(toolset, cfg, filecfg)
p.escaper(shellEscape)
local buildopt = list(filecfg.buildoptions)
local cppflags = list(toolset.getcppflags(filecfg))
local cxxflags = list(toolset.getcxxflags(filecfg))
local defines = list(table.join(toolset.getdefines(filecfg.defines, filecfg), toolset.getundefines(filecfg.undefines)))
local includes = relativeToWorkspace(cfg, function() return list(toolset.getincludedirs(cfg, filecfg.includedirs, filecfg.externalincludedirs, filecfg.frameworkdirs, filecfg.includedirsafter)) end)
local forceincludes = relativeToWorkspace(cfg, function() return list(toolset.getforceincludes(cfg)) end)
p.escaper(nil)
return buildopt .. cppflags .. cxxflags .. defines .. includes .. forceincludes
end
local function getLDFlags(toolset, cfg)
-- Ninja requires that all files are relative to the build dir
local oldCfgProjectDir = cfg.project.location
cfg.project.location = cfg.workspace.location
local libdirs = toolset.getLibraryDirectories(cfg)
cfg.project.location = oldCfgProjectDir
local ldflags = list(table.join(libdirs, toolset.getrunpathdirs(cfg, table.join(cfg.runpathdirs, config.getsiblingtargetdirs(cfg))), toolset.getldflags(cfg), cfg.linkoptions))
-- Make sure every '$' (e.g. $ORIGIN in rpaths) is escaped exactly once for ninja,
-- otherwise ninja expands it to an empty string (-Wl,-rpath,'') which swallows the next linker argument.
ldflags = ldflags:gsub("%$%$", "$"):gsub("%$", "$$")
if cfg.entrypoint ~= nil and toolset == p.tools.msc then
ldflags = ldflags .. " /ENTRY:" .. cfg.entrypoint
end
-- experimental feature, change install_name of shared libs
--if toolset == p.tools.clang and cfg.kind == p.SHAREDLIB and cfg.buildtarget.name:endsWith(".dylib") then
-- ldflags = ldflags .. " -install_name " .. cfg.buildtarget.name
--end
return ldflags
end
local function getResFlags(toolset, cfg, filecfg)
p.escaper(shellEscape)
local defines = list(toolset.getdefines(table.join(filecfg.defines, filecfg.resdefines), filecfg))
local includes = relativeToWorkspace(cfg, function() return list(toolset.getincludedirs(cfg, table.join(filecfg.externalincludedirs, filecfg.includedirsafter, filecfg.includedirs, filecfg.resincludedirs), {}, {}, {})) end)
local options = list(cfg.resoptions)
p.escaper(nil)
return defines .. includes .. options
end
local strMagic = "([%^%$%(%)%%%.%[%]%*%+%-%?])" -- UTF-8 replacement for "(%W)"
-- Replace is plain text version of string.gsub()
local function stringReplace(strTxt, strOld, strNew, intNum)
strOld = tostring(strOld or ""):gsub(strMagic,"%%%1") -- Hide magic pattern symbols
return tostring(strTxt or ""):gsub(strOld,function() return strNew end,tonumber(intNum)) -- Hide % capture symbols
end
local function pretranslatePaths(cmds, cfg)
for i = 1, #cmds do
local cmdCopy = cmds[i]
for v in string.gmatch(cmds[i], "%.%./[%w-._/\\]+") do
local correctedPath = path.getabsolute(v, cfg.project.location)
correctedPath = path.getrelative(cfg.workspace.location, correctedPath)
cmdCopy = stringReplace(cmdCopy, v, correctedPath, 1) --Replace path with path based on build dir
end
cmds[i] = cmdCopy
end
return cmds
end
local function prebuildRule(cfg)
if #cfg.prebuildcommands > 0 or cfg.prebuildmessage then
local commands = {}
if cfg.prebuildmessage then
commands = {os.translateCommandsAndPaths("{ECHO} " .. cfg.prebuildmessage, cfg.workspace.basedir, cfg.workspace.location)}
end
commands = table.join(commands, os.translateCommandsAndPaths(pretranslatePaths(cfg.prebuildcommands, cfg), cfg.workspace.basedir, cfg.workspace.location))
if (#commands > 1) then
if p.tools.canonical(cfg.toolset) == p.tools.msc then
local joined = table.concat(commands, " && ")
joind = joined:gsub('"', '""')
commands = 'cmd /C "' .. joined .. '"'
else
commands = "sh -c '" .. table.concat(commands, " && ") .. "'"
end
else
commands = commands[1]
end
emitRule('run_prebuild', {commands}, 'prebuild')
end
end
local function prelinkRule(cfg)
if #cfg.prelinkcommands > 0 or cfg.prelinkmessage then
local commands = {}
if cfg.prelinkmessage then
commands = {os.translateCommandsAndPaths("{ECHO} " .. cfg.prelinkmessage, cfg.workspace.basedir, cfg.workspace.location)}
end
commands = table.join(commands, os.translateCommandsAndPaths(pretranslatePaths(cfg.prelinkcommands, cfg), cfg.workspace.basedir, cfg.workspace.location))
if (#commands > 1) then
if p.tools.canonical(cfg.toolset) == p.tools.msc then
local joinde = table.concat(commands, " && ")
joined = joined:gsub('"', '""')
commands = 'cmd /C "' .. joined .. '"'
else
commands = "sh -c '" .. table.cocnat(commands, " && ") .. "'"
end
else
commands = commands[1]
end
emitRule('run_prelink', {commands}, 'prelink')
end
end
local function postbuildRule(cfg)
if #cfg.postbuildcommands > 0 or cfg.postbuildmessage then
local commands = {}
if cfg.postbuildmessage then
commands = {os.translateCommandsAndPaths("{ECHO} " .. cfg.postbuildmessage, cfg.workspace.basedir, cfg.workspace.location)}
end
local oldGetDefaultSeparator = path.getDefaultSeparator
if p.tools.canonical(cfg.toolset) == p.tools.msc then
path.getDefaultSeparator = function()
return '\\'
end
end
commands = table.join(commands, os.translateCommandsAndPaths(pretranslatePaths(cfg.postbuildcommands, cfg), cfg.workspace.basedir, cfg.workspace.location))
path.getDefaultSeparator = oldGetDefaultSeparator
if (#commands > 1) then
if p.tools.canonical(cfg.toolset) == p.tools.msc then
local joined = table.concat(commands, " && ")
joined = joined:gsub('"', '""')
commands = 'cmd /C "' .. joined .. '"'
else
commands = "sh -c '" .. table.concat(commands, " && ") .. "'"
end
else
commands = commands[1]
end
emitRule('run_postbuild', {commands}, 'postbuild')
end
end
local function c_cpp_compilationRules(cfg, toolset, pch)
---------------------------------------------------- figure out toolset executables
local cc = toolset.gettoolname(cfg, "cc")
local cxx = toolset.gettoolname(cfg, "cxx")
local ar = toolset.gettoolname(cfg, "ar")
local link = toolset.gettoolname(cfg, iif(cfg.language == "C", "cc", "cxx"))
local rc = toolset.gettoolname(cfg, "rc")
-- all paths need to be relative to the workspace output location,
-- and not relative to the project output location.
-- override the toolset getrelative function to achieve this
local getrelative = p.tools.getrelative
p.tools.getrelative = function(cfg, value)
return p.workspace.getrelative(cfg.workspace, value)
end
local all_cflags = getCFlags(toolset, cfg, cfg)
local all_cxxflags = getCXXFlags(toolset, cfg, cfg)
local all_ldflags = getLDFlags(toolset, cfg)
local all_resflags = getResFlags(toolset, cfg, cfg)
if toolset == p.tools.msc then
local force_include_pch = ""
if pch then
force_include_pch = " /Yu" .. shellEscape(path.getname(pch.input)) .. " /Fp" .. shellEscape(pch.pch)
end
emitFlags('CFLAGS', all_cflags)
emitRule('cc', {cc .. ' $CFLAGS' .. force_include_pch .. ' /nologo /showIncludes -c /Tc$in /Fo$out'}, 'cc $out', {deps = 'msvc'})
emitFlags('CXXFLAGS', all_cxxflags)
emitRule('cxx', {cxx .. ' $CXXFLAGS' .. force_include_pch .. ' /nologo /showIncludes -c /Tp$in /Fo$out'}, 'cxx $out', {deps = 'msvc'})
if pch then
emitRule('build_pch', {iif(cfg.language == "C", cc .. " $CFLAGS", cxx .. " $CXXFLAGS") .. ' /Yc' .. shellEscape(path.getname(pch.input)) .. ' /Fp' .. shellEscape(pch.pch) .. ' /nologo /showIncludes -c /Tp$in /Fo$out'}, 'build_pch $out', {deps = 'msvc'})
end
emitRule('cxx_module', {cxx .. ' $CXXFLAGS' .. force_include_pch .. ' /nologo /showIncludes @$DYNDEP_MODULE_MAP_FILE /FS -c /Tp$in /Fo$out'}, 'cxx_module $out', {deps = 'msvc'})
emitFlags('RESFLAGS', all_resflags)
emitRule('rc', {rc .. ' /nologo /fo$out $in $RESFLAGS'}, 'rc $out')
if cfg.kind == p.STATICLIB then
emitRule('ar', {ar .. ' $in /nologo -OUT:$out'}, 'ar $out')
else
emitRule('link', {link .. ' $in ' .. list(shellEscape(pretranslatePaths(toolset.getlinks(cfg, true), cfg))) .. ' /link ' .. all_ldflags .. ' /nologo /out:$out'}, 'link $out')
end
elseif toolset == p.tools.clang or toolset == p.tools.gcc then
local force_include_pch = ""
if pch then
force_include_pch = " -include " .. shellEscape(pch.placeholder)
end
emitFlags('CFLAGS', all_cflags)
emitRule('cc', {cc .. ' $CFLAGS' .. force_include_pch .. ' -x c -MF $out.d -c -o $out $in'}, 'cc $out', {depfile = '$out.d', deps = 'gcc'})
emitFlags('CXXFLAGS', all_cxxflags)
emitRule('cxx', {cxx .. ' $CXXFLAGS' .. force_include_pch .. ' -x c++ -MF $out.d -c -o $out $in'}, 'cxx $out', {depfile = '$out.d', deps = 'gcc'})
if pch then
emitRule('build_pch', {iif(cfg.language == "C", cc .. ' $CFLAGS -x c-header', cxx .. ' $CXXFLAGS -x c++-header') .. ' -MF $out.d -c -o $out $in'}, 'build_pch $out', {depfile = '$out.d', deps = 'gcc'})
end
if toolset == p.tools.gcc then
emitRule('cxx_module', {cxx .. ' $CXXFLAGS -fmodules-ts -fmodule-mapper=$DYNDEP_MODULE_MAP_FILE -fdeps-format=p1689r5 -x c++' .. force_include_pch .. ' -MT $out -MF $out.d -c -o $out $in'}, 'cxx_module $out', {depfile = '$out.d', deps = 'gcc'})
elseif toolset == p.tools.clang then
emitRule('cxx_module', {cxx .. ' $CXXFLAGS' .. force_include_pch .. ' -MT $out -MF $out.d @$DYNDEP_MODULE_MAP_FILE -c -o $out $in'}, 'cxx_module $out', {depfile = '$out.d', deps = 'gcc'})
end
emitFlags('RESFLAGS', all_resflags)
if rc then
emitRule('rc', {rc .. ' -i $in -o $out $RESFLAGS'}, 'rc $out')
end
if cfg.kind == p.STATICLIB then
emitRule('ar', {ar .. ' rcs $out $in'}, 'ar $out')
else
local groups = iif(cfg.linkgroups == premake.ON, {'-Wl,--start-group ', ' -Wl,--end-group'}, {'', ''})
emitRule('link', {link .. ' -o $out ' .. groups[1] .. '$in' .. list(shellEscape(pretranslatePaths(toolset.getlinks(cfg, true, true), cfg))) .. all_ldflags .. groups[2]}, 'link $out')
end
end
p.tools.getrelative = getrelative
end
local function customCommandRule()
emitRule('custom_command', {'$CUSTOM_COMMAND'}, '$CUSTOM_DESCRIPTION')
end
local function copyRule()
emitRule('copy', {os.translateCommands('{COPYFILE} $in $out')}, 'copy $in $out')
end
local function collectGeneratedFiles(prj, cfg)
local generated_files = {}
tree.traverse(project.getsourcetree(prj), {
onleaf = function(node, depth)
function append_to_generated_files(filecfg)
local outputs = project.getrelative(prj.workspace, filecfg.buildoutputs)
generated_files = table.join(generated_files, outputs)
end
local filecfg = fileconfig.getconfig(node, cfg)
if not filecfg or (filecfg.flags and filecfg.flags.ExcludeFromBuild) then
return
end
local rule = p.global.getRuleForFile(node.name, prj.rules)
if fileconfig.hasCustomBuildRule(filecfg) then
append_to_generated_files(filecfg)
elseif rule then
local environ = table.shallowcopy(filecfg.environ)
if rule.propertydefinition then
p.rule.prepareEnvironment(rule, environ, cfg)
p.rule.prepareEnvironment(rule, environ, filecfg)
end
local rulecfg = p.context.extent(rule, environ)
append_to_generated_files(rulecfg)
end
end,
}, false, 1)
return generated_files
end
local function pchBuild(cfg, pch, toolset)
local pch_dependency = {}
if pch then
if toolset == p.tools.msc then
pch_dependency = { pch.pch }
addBuild(cfg, pch.outObj, {pch.pch}, "build_pch", {pch.inputSrc}, {}, {}, {})
else
pch_dependency = {pch.gch}
addBuild(cfg, pch.gch, {}, "build_pch", {pch.input}, {}, {}, {})
end
end
return pch_dependency
end
local function customCommandBuild(prj, cfg, filecfg, filename, file_dependencies)
local outputs = project.getrelative(prj.workspace, filecfg.buildoutputs)
local output = outputs[1]
table.remove(outputs, 1)
local commands = {}
if filecfg.buildmessage then
commands = {os.translateCommandsAndPaths("{ECHO} " .. filecfg.buildmessage, prj.workspace.basedir, prj.workspace.location)}
end
commands = table.join(commands, os.translateCommandsAndPaths(pretranslatePaths(filecfg.buildcommands, cfg), prj.workspace.basedir, prj.workspace.location))
if (#commands > 1) then
if p.tools.canonical(cfg.toolset) == p.tools.msc then
local joined = table.concat(commands, " && ")
joined = joined:gsub('"', '""')
commands = 'cmd /C "' .. joined .. '"'
else
commands = "sh -c '" .. table.concat(commands, " && ") .. "'"
end
else
commands = commands[1]
end
addBuild(cfg, output, outputs, "custom_command", {filename}, project.getrelative(prj.workspace, filecfg.buildinputs), file_dependencies, {"CUSTOM_COMMAND = " .. commands, "CUSTOM_DESCRIPTION = custom build " .. shellEscape(output)})
end
local function isCXXModuleFile(file)
if _OPTIONS["experimental-modules-scan-all"] then
return true
end
local fileEnding = path.getextension(file)
if fileEnding == ".cxx" or fileEnding == ".cxxm" or fileEnding == ".ixx" or
fileEnding == ".cppm" or fileEnding == ".c++m" or fileEnding == ".ccm" or
fileEnding == ".mpp" then
return true
end
return false
end
local function compileFileBuild(cfg, filecfg, toolset, pch_dependency, regular_file_dependencies, objfiles, extrafiles)
local obj_file = filecfg.objname .. (toolset.objectextension or '.o')
local obj_dir = project.getrelative(cfg.workspace, cfg.objdir)
local filepath = project.getrelative(cfg.workspace, filecfg.abspath)
local has_custom_settings = fileconfig.hasFileSettings(filecfg)
if filecfg.buildaction == 'None' then
return
elseif filecfg.buildaction == 'Copy' then
local target = project.getrelative(cfg.workspace, path.join(cfg.targetdir, filecfg.name))
addBuild(cfg, target, {}, 'copy', {filepath}, {}, {}, {})
extrafiles[#extrafiles + 1] = target
elseif shouldCompileAsC(filecfg) then
local objfilename = obj_dir .. '/' .. obj_file
objfiles[#objfiles + 1] = objfilename
local vars = {}
if has_custom_settings then
vars = {'CFLAGS = $CFLAGS ' .. getCFlags(toolset, cfg, filecfg)}
end
addBuild(cfg, objfilename, {}, 'cc', {filepath}, pch_dependency, regular_file_dependencies, vars)
elseif shouldCompileAsCpp(filecfg) then
local objfilename = obj_dir .. '/' .. obj_file
objfiles[#objfiles + 1] = objfilename
local vars = {}
if has_custom_settings then
vars = { 'CXXFLAGS = $CXXFLAGS ' .. getCXXFlags(toolset, cfg, filecfg)}
end
if _OPTIONS["experimental-enable-cxx-modules"] and isCXXModuleFile(filecfg.name) then
local regFileDeps = table.arraycopy(regular_file_dependencies)
local dynDepModMapFile = objfilename .. ".modmap"
local dynDepFile = path.join(obj_dir, "CXX.dd")
table.insert(vars, "DYNDEP_MODULE_MAP_FILE = " .. dynDepModMapFile)
table.insert(vars, "dyndep = " .. dynDepFile)
table.insert(regFileDeps, dynDepFile)
table.insert(regFileDeps, dynDepModMapFile)
addBuild(cfg, objfilename, {}, 'cxx_module', {filepath}, pch_dependency, regFileDeps, vars)
else
addBuild(cfg, objfilename, {}, 'cxx', {filepath}, pch_dependency, regular_file_dependencies, vars)
end
elseif path.isresourcefile(filecfg.abspath) then
local objfilename = obj_dir .. '/' .. filecfg.basename .. '.res'
objfiles[#objfiles + 1] = objfilename
local resflags = {}
if has_custom_settings then
resflags = {'RESFLAGS = $RESFLAGS ' .. getResFlags(toolset, cfg, filecfg)}
end
local rc = toolset.gettoolname(cfg, 'rc')
if rc then
addBuild(cfg, objfilename, {}, 'rc', {filepath}, {}, {}, resflags)
else
p.warnOnce(filepath, string.format('Ignored resource: "%s"', filepath))
end
end
end
local function filesBuild(prj, cfg, toolset, pch_dependency, regular_file_dependencies, file_dependencies)
local objfiles = {}
local extrafiles = {}
local obj_dir = project.getrelative(cfg.workspace, cfg.objdir)
tree.traverse(project.getsourcetree(prj), {
onleaf = function(node, depth)
local filecfg = fileconfig.getconfig(node, cfg)
if not filecfg or (filecfg.flags and filecfg.flags.ExcludeFromBuild) then
return
end
-- Compiling PCH is handled via build_pch build rule
if cfg.pchsource and cfg.pchsource == node.abspath then
if toolset == p.tools.msc then -- MSVC emits an object file for the PCH, while GCC and Clang dont
local objfilename = obj_dir .. "/" .. path.getname(node.path) .. (toolset.objectextension or ".o")
objfiles[#objfiles + 1] = objfilename
end
return
end
local rule = p.global.getRuleForFile(node.name, prj.rules)
local filepath = project.getrelative(cfg.workspace, node.abspath)
if fileconfig.hasCustomBuildRule(filecfg) then
customCommandBuild(prj, cfg, filecfg, filepath, file_dependencies)
elseif rule then
local environ = table.shallowcopy(filecfg.environ)
if rule.propertydefinition then
p.rule.prepareEnvironment(rule, environ, cfg)
p.rule.prepareEnvironment(rule, environ, filecfg)
end
local rulecfg = p.context.extent(rule, environ)
customCommandBuild(prj, cfg, rulecfg, filepath, file_dependencies)
else
compileFileBuild(cfg, filecfg, toolset, pch_dependency, regular_file_dependencies, objfiles, extrafiles)
end
end,
}, false, 1)
p.outln("")
return objfiles, extrafiles
end
local function generatedFilesBuild(cfg, generated_files, key)
local final_dependency = {}
if #generated_files > 0 then
p.outln("# generated files")
addBuild(cfg, "generated_files_" .. key, {}, "phony", generated_files, {}, {}, {})
final_dependency = {"generated_files_" .. key}
end
return final_dependency
end
-- return name of output binary relative to build folder
local function outputFilename(cfg)
return project.getrelative(cfg.workspace, cfg.buildtarget.directory) .. "/" .. cfg.buildtarget.name
end
local function getCXXModuleScannerName(toolset, toolsetVersion, cfg)
if toolset == p.tools.clang then
local scannerName = "clang-scan-deps"
if toolsetVersion then
scannerName = scannerName .. "-" .. toolsetVersion
end
return scannerNamer
elseif toolset == p.tools.gcc or toolset == p.tools.msc then
return toolset.gettoolname(cfg, "cxx")
end
return nil
end
local function cxxModuleScanRule(cfg, toolset)
local cmd = ""
local scannerName = getCXXModuleScannerName(toolset, toolsetVersion, cfg)
if toolset == p.tools.clang then
local _, toolsetVersion = p.tools.canonical(cfg.toolset)
local compilerName = toolset.gettoolname(cfg, "cxx")
libcppPath, libcppError = os.outputof(compilerName .. " -print-resource-dir")
if libcppError ~= 0 then
term.setTextColor(term.errorColor)
print("Failed to find path to system headers!")
term.setTextColor(term.warningColor)
print("Using automatic system header discovery, this may use incorrect paths")
term.setTextColor(nil)
libcppPath = ""
else
libcppPath = " -resource-dir " .. libcppPath .. " "
end
cmd = scannerName .. " -format=p1689 -- " .. compilerName .. libcppPath .. " $CXXFLAGS -x c++ $in -c -o $OBJ_FILE -MT $DYNDEP_INTERMEDIATE_FILE -MD -MF $DEP_FILE > $DYNDEP_INTERMEDIATE_FILE.tmp && mv $DYNDEP_INTERMEDIATE_FILE.tmp $DYNDEP_INTERMEDIATE_FILE"
elseif toolset == p.tools.gcc then
cmd = scannerName .. " $CXXFLAGS -E -x c++ $in -MT $DYNDEP_INTERMEDIATE_FILE -MD -MF $DEP_FILE -fmodules-ts -fdeps-file=$DYNDEP_INTERMEDIATE_FILE -fdeps-target=$OBJ_FILE -fdeps-format=p1689r5 -o $PREPROCESSED_OUTPUT_FILE"
elseif toolset == p.tools.msc then
cmd = scannerName .. " $CXXFLAGS $in -nologo -TP -showIncludes -scanDependencies $DYNDEP_INTERMEDIATE_FILE -Fo$OBJ_FILE"
else
term.setTextColor(term.errorColor)
print("C++20 Modules are only supported with Clang, GCC and MSC!")
term.setTextColor(nil)
os.exit()
end
local deps = {}
if toolset == p.tools.msc then
deps = {deps = 'msvc'}
else
deps = {depfile = '$DEP_FILE', deps = 'gcc'}
end
emitRule("__module_scan", {cmd}, 'Scanning $in for C++ dependencies', deps)
end
local collateModuleScript = nil
local function cxxModuleCollateRule(cfg, toolset)
if not collateModuleScript then
local collateModuleScripts = os.matchfiles(_MAIN_SCRIPT_DIR .. "/.modules/**/collate_modules/collate_modules.lua")
if collateModuleScripts == nil or collateModuleScripts[1] == nil then
term.setTextColor(term.errorColor)
print("Unable to find collate_modules.lua script!")
term.setTextColor(nil)
os.exit()
else
collateModuleScript = collateModuleScripts[1]
end
end
local cmd = _PREMAKE_COMMAND .. " --file=" .. collateModuleScript .. " collate_modules "
if toolset == p.tools.clang then
cmd = cmd .. "--modmapfmt=clang "
elseif toolset == p.tools.gcc then
cmd = cmd .. "--modmapfmt=gcc "
elseif toolset == p.tools.msc then
cmd = cmd .. "--modmapfmt=msvc "
else
term.setTextColor(term.errorColor)
print("C++20 Modules are only supported with Clang, GCC and MSC!")
term.setTextColor(nil)
os.exit()
end
cmd = cmd .. "--dd=$out --ddi=\"$in\" --deps=$MODULE_DEPS @$out.rsp"
emitRule('__module_collate', {cmd}, 'Generating C++ dyndep file $out', {rspfile = '$out.rsp', rspfile_content = '$in'})
end
local function scanCXXModuleFileBuild(cfg, filecfg, toolset, modulefiles)
local obj_dir = project.getrelative(cfg.workspace, cfg.objdir)
local filepath = project.getrelative(cfg.workspace, filecfg.abspath)
local outputFilebase = obj_dir .. "/" .. filecfg.name
local dyndepfilename = outputFilebase .. toolset.objectextension .. ".ddi"
modulefiles[#modulefiles + 1] = dyndepfilename
local vars = {}
table.insert(vars, "DEP_FILE = " .. outputFilebase .. toolset.objectextension .. ".ddi.d")
table.insert(vars, "DYNDEP_INTERMEDIATE_FILE = " .. dyndepfilename)
table.insert(vars, "OBJ_FILE = " .. outputFilebase .. toolset.objectextension)
table.insert(vars, "PREPROCESSED_OUTPUT_FILE = " .. outputFilebase .. toolset.objectextension .. ".ddi.i")
addBuild(cfg, dyndepfilename, {}, "__module_scan", {filepath}, {}, {}, vars)
end
local function filesScanCXXModules(prj, cfg, toolset)
local modulefiles = {}
tree.traverse(project.getsourcetree(prj), {
onleaf = function(node, depth)
if not isCXXModuleFile(node.name) then
return
end
local filecfg = fileconfig.getconfig(node, cfg)
if not filecfg or (filecfg.flags and filecfg.flags.ExcludeFromBuild) then
return
end
scanCXXModuleFileBuild(cfg, filecfg, toolset, modulefiles)
end,
}, false, 1)
p.outln("")
return modulefiles
end
-- generate project + config build file
local function generateProjectCfg(cfg)
local oldGetDefaultSeparator = path.getDefaultSeparator
path.getDefaultSeparator = function()
return '/'
end
local prj = cfg.project
local key = getKey(cfg)
local toolset, toolset_version = p.tools.canonical(cfg.toolset)
if not toolset then
p.error("Unknown toolset " .. cfg.toolset)
end
-- Some toolset fixes
cfg.gccprefix = cfg.gccprefix or ""
if os.target() == "windows" and cfg.externalwarnings == nil then
cfg.externalwarnings = "Default"
end
if cfg.characterset == nil then
cfg.characterset = "Default"
end
if cfg.entrypoint == nil then
if cfg.kind == "WindowedApp" then -- Use WinMain()
cfg.entrypoint = "WinMainCRTStartup"
elseif cfg.kind == "SharedLib" then -- Use DllMain()
cfg.entrypoint = "_DllMainCRTStartup"
elseif cfg.kind == "ConsoleApp" then -- Use main()
cfg.entrypoint = "mainCRTStartup"
end
end
p.outln("# project build file")
p.outln("# generated with premake ninja")
p.outln("")
-- premake-ninja relies on scoped rules and dyndep
p.outln("ninja_required_version = 1.11.1")
p.outln("")
local isCOrCPP = cfg.language == p.C or cfg.language == p.CPP
---------------------------------------------------- figure out settings
local pch = nil
if isCOrCPP then
pch = p.tools.gcc.getpch(cfg)
if pch then
pch = {
input = project.getrelative(cfg.workspace, path.join(cfg.location, pch)),
inputSrc = project.getrelative(cfg.workspace, path.join(cfg.location, cfg.pchsource)),
placeholder = project.getrelative(cfg.workspace, path.join(cfg.objdir, path.getname(pch))),
gch = project.getrelative(cfg.workspace, path.join(cfg.objdir, path.getname(pch) .. ".gch")),
pch = project.getrelative(cfg.workspace, path.join(cfg.objdir, path.getname(pch) .. ".pch")),
outObj = path.join(project.getrelative(cfg.workspace, cfg.objdir), path.getname(project.getrelative(cfg.workspace, path.join(cfg.location, cfg.pchsource))) .. (toolset.objectextension or p.tools.msc.objectextension))
}
end
end
---------------------------------------------------- write rules
p.outln("# core rules for " .. cfg.name)
prebuildRule(cfg)
prelinkRule(cfg)
postbuildRule(cfg)
if isCOrCPP then
c_cpp_compilationRules(cfg, toolset, pch)
if _OPTIONS["experimental-enable-cxx-modules"] then
cxxModuleScanRule(cfg, toolset)
cxxModuleCollateRule(cfg, toolset)
---------------------------------------------------- scan all module files
p.outln("# scan modules")
local modulefiles = filesScanCXXModules(prj, cfg, toolset)
if modulefiles then
---------------------------------------------------- collate all scanned module files
p.outln("# collate modules")
local obj_dir = project.getrelative(cfg.workspace, cfg.objdir)
local outputFile = obj_dir .. "/CXX.dd"
local implicitOutputs = {obj_dir .. "/CXXModules.json"}
for k,v in pairs(modulefiles) do
table.insert(implicitOutputs, path.replaceextension(v, "modmap"))
end
local implicit_inputs = {}
local vars = {}
local dependencies = {}
for _, v in pairs(p.config.getlinks(cfg, "dependencies", "object")) do
local relDepObjDir = project.getrelative(cfg.workspace, v.objdir)
table.insert(dependencies, path.join(relDepObjDir, "CXXModules.json"))
end