-
-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathninja_cpp.lua
More file actions
1358 lines (1113 loc) · 39.7 KB
/
Copy pathninja_cpp.lua
File metadata and controls
1358 lines (1113 loc) · 39.7 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
--
-- ninja_cpp.lua
-- Define the ninja cpp functionality
-- Author: Nick Clark
-- Copyright (c) 2025 Jess Perkins and the Premake project
--
local p = premake
local ninja = p.modules.ninja
local tree = p.tree
local project = p.project
local config = p.config
local fileconfig = p.fileconfig
p.modules.ninja.cpp = {}
local m = p.modules.ninja.cpp
m.elements = {}
m.elements.project = function(prj)
return {
ninja.header,
m.rules,
m.configurations,
m.buildTargets,
m.projectPhonies,
}
end
function m.generate(prj)
p.callArray(m.elements.project, prj)
_p("") -- Empty line at end of file
end
function m.rules(prj)
local rulesDone = {}
for cfg in project.eachconfig(prj) do
local toolset = ninja.gettoolset(cfg)
local toolsetKey = tostring(toolset)
if not rulesDone[toolsetKey] then
m.ccrule(cfg, toolset)
m.cxxrule(cfg, toolset)
m.resourcerule(cfg, toolset)
m.linkrule(cfg, toolset)
m.pchrule(cfg, toolset)
m.copyrule(cfg, toolset)
m.prebuildcommandrule(cfg, toolset)
m.prelinkcommandrule(cfg, toolset)
m.postbuildcommandrule(cfg, toolset)
m.customcommand(cfg, toolset)
m.customrule(cfg, toolset, prj)
rulesDone[toolsetKey] = true
end
end
end
function m.ccrule(cfg, toolset)
toolset = toolset or ninja.gettoolset(cfg)
local ccname = toolset.gettoolname(cfg, "cc")
_p("rule cc_%s", cfg.toolset)
if toolset == p.tools.msc then
_p(" command = %s $cflags /nologo /showIncludes -c /Tc$in /Fo$out", ccname)
_p(" deps = msvc")
else
_p(" command = %s $cflags -c $in -o $out -MD -MF $out.d", ccname)
_p(" deps = gcc")
_p(" depfile = $out.d")
end
_p(" description = Compiling C source $in")
_p("")
end
function m.cxxrule(cfg, toolset)
toolset = toolset or ninja.gettoolset(cfg)
local cxxname = toolset.gettoolname(cfg, "cxx")
_p("rule cxx_%s", cfg.toolset)
if toolset == p.tools.msc then
_p(" command = %s $cxxflags /nologo /showIncludes -c /Tp$in /Fo$out", cxxname)
_p(" deps = msvc")
else
_p(" command = %s $cxxflags -c $in -o $out -MD -MF $out.d", cxxname)
_p(" deps = gcc")
_p(" depfile = $out.d")
end
_p(" description = Compiling C++ source $in")
_p("")
end
function m.resourcerule(cfg, toolset)
toolset = toolset or ninja.gettoolset(cfg)
local rcname = toolset.gettoolname(cfg, "rc")
_p("rule rc_%s", cfg.toolset)
if toolset == p.tools.msc then
_p(" command = %s /nologo /fo$out $in $resflags", rcname)
else
_p(" command = %s -i $in -o $out $resflags", rcname)
end
_p(" description = Compiling resource $in")
_p("")
end
function m.linkrule(cfg, toolset)
toolset = toolset or ninja.gettoolset(cfg)
if toolset == p.tools.msc then
local arname = toolset.gettoolname(cfg, "ar")
_p("rule ar_%s", cfg.toolset)
_p(" command = %s $in /nologo -OUT:$out", arname)
_p(" description = Archiving static library $out")
_p("")
local ldname = toolset.gettoolname(cfg, iif(cfg.language == "C", "cc", "cxx"))
_p("rule link_%s", cfg.toolset)
_p(" command = %s $in $links /link $ldflags /nologo /out:$out", ldname)
_p(" description = Linking target $out")
_p("")
else
local arname = toolset.gettoolname(cfg, "ar")
_p("rule ar_%s", cfg.toolset)
_p(" command = %s -rcs $out $in", arname)
_p(" description = Archiving static library $out")
_p("")
local ldname = toolset.gettoolname(cfg, iif(cfg.language == "C", "cc", "cxx"))
local commands = string.format("command = %s -o $out $in $links $ldflags", ldname);
commands = commands:gsub("(.-)%s*$", "%1")
commands = commands:gsub("%s+", " ")
_p("rule link_%s", cfg.toolset)
_p(" %s", commands)
_p(" description = Linking target $out")
_p("")
end
end
function m.pchrule(cfg, toolset)
toolset = toolset or ninja.gettoolset(cfg)
local pchname = toolset.gettoolname(cfg, cfg.language == "C" and "cc" or "cxx")
_p("rule pch_%s", cfg.toolset)
if toolset == p.tools.msc then
-- MSVC: /Yc creates the PCH, /Fp specifies output, /Fo specifies obj output
_p(" command = %s /nologo /Yc$pchheader /Fp$out /Fo$objdir/ $cflags /c $in", pchname)
_p(" description = Generating precompiled header $pchheader")
else
-- GCC/Clang: compile header as C or C++ header
local headerType = iif(cfg.language == "C", "c-header", "c++-header")
_p(" command = %s -x %s $cflags -o $out -MD -MF $out.d -c $in", pchname, headerType)
_p(" description = Generating precompiled header $in")
_p(" depfile = $out.d")
end
_p("")
end
function m.copyrule(cfg, toolset)
_p("rule copy")
_p(os.translateCommands(" command = {COPYFILE} $in $out"))
_p(" description = Copying file $in to $out")
_p("")
end
function m.prebuildcommandrule(cfg, toolset)
_p("rule prebuild")
_p(" command = $prebuildcommands")
_p(" description = Running pre-build commands")
_p("")
end
function m.prelinkcommandrule(cfg, toolset)
_p("rule prelink")
_p(" command = $prelinkcommands")
_p(" description = Running pre-link commands")
_p("")
end
function m.postbuildcommandrule(cfg, toolset)
_p("rule postbuild")
_p(" command = $postbuildcommands")
_p(" description = Running post-build commands")
_p("")
end
function m.customcommand(cfg, toolset)
_p("rule custom")
_p(" command = $customcommand")
_p(" description = Running custom command: $customcommand")
_p("")
end
function m.customrule(cfg, toolset, prj)
if not prj.rules or #prj.rules == 0 then
return
end
for _, ruleName in ipairs(prj.rules) do
local rule = p.global.getRule(ruleName)
if rule then
local ruleNameEscaped = ruleName:gsub("[^%w_]", "_"):lower()
_p("rule %s", ruleNameEscaped)
_p(" command = $%s_command", ruleNameEscaped)
_p(" description = $%s_description", ruleNameEscaped)
_p("")
end
end
end
local function kindHasOutputFile(kind)
return kind == p.STATICLIB or kind == p.SHAREDLIB or kind == p.CONSOLEAPP or kind == p.WINDOWSAPP
end
local function gatherDepTargets(cfg)
local depTargets = {}
for _, depname in ipairs(cfg.dependson) do
local depprj = p.workspace.findproject(cfg.workspace, depname)
if depprj then
local depcfg = project.getconfig(depprj, cfg.buildcfg, cfg.platform)
if depcfg then
-- Dependency hierarchy is as follows:
-- 1) If the dependent config has post-build commands, depend on the post-build target
-- 2) If the dependent config produces an output file (exe/dll/lib), depend on that
-- 3) If the dependent config has pre-link commands, depend on the pre-link target
-- 4) If the dependent config has pre-build commands, depend on the pre-build target
local depTargetEventRoot = path.getrelative(cfg.workspace.location, depcfg.objdir) .. "/" .. depcfg.project.name
if depcfg.postbuildcommands and #depcfg.postbuildcommands > 0 then
table.insert(depTargets, depTargetEventRoot .. ".postbuild")
elseif kindHasOutputFile(depcfg.kind) then
local depTarget = path.getrelative(cfg.workspace.location, depcfg.buildtarget.directory) .. "/" .. depcfg.buildtarget.name
table.insert(depTargets, depTarget)
elseif depcfg.prelinkcommands and #depcfg.prelinkcommands > 0 then
table.insert(depTargets, depTargetEventRoot .. ".prelinkevents")
elseif depcfg.prebuildcommands and #depcfg.prebuildcommands > 0 then
table.insert(depTargets, depTargetEventRoot .. ".prebuild")
end
end
end
end
return depTargets
end
function m.buildDependsOnTarget(cfg)
if not cfg.dependson or #cfg.dependson == 0 then
return nil
end
local depTargets = gatherDepTargets(cfg)
if #depTargets == 0 then
return nil
end
return depTargets
end
function m.configurations(prj)
for cfg in project.eachconfig(prj) do
_p("# Configuration: %s", ninja.key(cfg))
_p("")
m.configurationVariables(cfg)
local depsTargets = m.buildDependsOnTarget(cfg)
cfg._dependsOnTargets = depsTargets
m.buildFiles(cfg)
m.linkTarget(cfg)
_p("")
end
end
function m.configurationVariables(cfg)
local toolset = ninja.gettoolset(cfg)
local cflags = m.getCFlags(cfg, toolset)
if #cflags > 0 then
_p("cflags_%s = %s", ninja.key(cfg), table.concat(cflags, " "))
end
local cxxflags = m.getCxxFlags(cfg, toolset)
if #cxxflags > 0 then
_p("cxxflags_%s = %s", ninja.key(cfg), table.concat(cxxflags, " "))
end
local ldflags = m.getLdFlags(cfg, toolset)
if #ldflags > 0 then
_p("ldflags_%s = %s", ninja.key(cfg), table.concat(ldflags, " "))
end
local links = toolset.getlinks(cfg)
if #links > 0 then
_p("links_%s = %s", ninja.key(cfg), table.concat(links, " "))
end
_p("objdir_%s = %s", ninja.key(cfg), project.getrelative(cfg.project, cfg.objdir))
_p("targetdir_%s = %s", ninja.key(cfg), project.getrelative(cfg.project, cfg.buildtarget.directory))
_p("target_%s = %s", ninja.key(cfg), cfg.buildtarget.name)
_p("")
end
function m.getCFlags(cfg, toolset)
local flags = {}
toolset = toolset or ninja.gettoolset(cfg)
local toolFlags = toolset.getcflags(cfg)
flags = table.join(flags, toolFlags)
local escaper = p.escaper(p.quote)
local defines = toolset.getdefines(cfg.defines)
flags = table.join(flags, defines)
local undefines = toolset.getundefines(cfg.undefines)
flags = table.join(flags, undefines)
local includedirs = toolset.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs, cfg.frameworkdirs, cfg.includedirsafter)
flags = table.join(flags, includedirs)
local forceincludes = toolset.getforceincludes(cfg)
flags = table.join(flags, forceincludes)
local buildopts = cfg.buildoptions or {}
flags = table.join(flags, buildopts)
p.escaper(escaper)
return flags
end
function m.getFileCFlags(cfg, filecfg, toolset)
local flags = {}
if filecfg.cdialect and filecfg.cdialect ~= cfg.cdialect then
local toolFlags = toolset.getcflags(filecfg)
flags = table.join(flags, toolFlags)
else
local toolFlags = toolset.getcflags(cfg)
flags = table.join(flags, toolFlags)
end
local allDefines = table.join(cfg.defines or {}, filecfg.defines or {})
local escaper = p.escaper(p.quote)
local defines = toolset.getdefines(allDefines)
flags = table.join(flags, defines)
local allUndefines = table.join(cfg.undefines or {}, filecfg.undefines or {})
local undefines = toolset.getundefines(allUndefines)
flags = table.join(flags, undefines)
local allIncludedirs = table.join(cfg.includedirs or {}, filecfg.includedirs or {})
local allExternalIncludedirs = table.join(cfg.externalincludedirs or {}, filecfg.externalincludedirs or {})
local allFrameworkdirs = table.join(cfg.frameworkdirs or {}, filecfg.frameworkdirs or {})
local allIncludedirsafter = table.join(cfg.includedirsafter or {}, filecfg.includedirsafter or {})
local includedirs = toolset.getincludedirs(cfg, allIncludedirs, allExternalIncludedirs, allFrameworkdirs, allIncludedirsafter)
flags = table.join(flags, includedirs)
local forceincludes = toolset.getforceincludes(filecfg)
flags = table.join(flags, forceincludes)
local allBuildopts = table.join(cfg.buildoptions or {}, filecfg.buildoptions or {})
flags = table.join(flags, allBuildopts)
p.escaper(escaper)
return flags
end
function m.getCxxFlags(cfg, toolset)
local flags = {}
toolset = toolset or ninja.gettoolset(cfg)
local toolFlags = toolset.getcxxflags(cfg)
flags = table.join(flags, toolFlags)
local escaper = p.escaper(p.quote)
local defines = toolset.getdefines(cfg.defines)
flags = table.join(flags, defines)
local undefines = toolset.getundefines(cfg.undefines)
flags = table.join(flags, undefines)
local includedirs = toolset.getincludedirs(cfg, cfg.includedirs, cfg.externalincludedirs, cfg.frameworkdirs, cfg.includedirsafter)
flags = table.join(flags, includedirs)
local forceincludes = toolset.getforceincludes(cfg)
flags = table.join(flags, forceincludes)
local buildopts = cfg.buildoptions or {}
flags = table.join(flags, buildopts)
p.escaper(escaper)
return flags
end
function m.getFileCxxFlags(cfg, filecfg, toolset)
local flags = {}
if filecfg.cppdialect and filecfg.cppdialect ~= cfg.cppdialect then
local toolFlags = toolset.getcxxflags(filecfg)
flags = table.join(flags, toolFlags)
else
local toolFlags = toolset.getcxxflags(cfg)
flags = table.join(flags, toolFlags)
end
local allDefines = table.join(cfg.defines or {}, filecfg.defines or {})
local escaper = p.escaper(p.quote)
local defines = toolset.getdefines(allDefines)
flags = table.join(flags, defines)
local allUndefines = table.join(cfg.undefines or {}, filecfg.undefines or {})
local undefines = toolset.getundefines(allUndefines)
flags = table.join(flags, undefines)
local allIncludedirs = table.join(cfg.includedirs or {}, filecfg.includedirs or {})
local allExternalIncludedirs = table.join(cfg.externalincludedirs or {}, filecfg.externalincludedirs or {})
local allFrameworkdirs = table.join(cfg.frameworkdirs or {}, filecfg.frameworkdirs or {})
local allIncludedirsafter = table.join(cfg.includedirsafter or {}, filecfg.includedirsafter or {})
local includedirs = toolset.getincludedirs(cfg, allIncludedirs, allExternalIncludedirs, allFrameworkdirs, allIncludedirsafter)
flags = table.join(flags, includedirs)
local forceincludes = toolset.getforceincludes(filecfg)
flags = table.join(flags, forceincludes)
local allBuildopts = table.join(cfg.buildoptions or {}, filecfg.buildoptions or {})
flags = table.join(flags, allBuildopts)
p.escaper(escaper)
return flags
end
function m.hasPerFileConfiguration(cfg, filecfg)
if filecfg.defines and #filecfg.defines > 0 then
local parentDefines = cfg.defines or {}
if #filecfg.defines ~= #parentDefines then
return true
end
for i, define in ipairs(filecfg.defines) do
if define ~= parentDefines[i] then
return true
end
end
end
if filecfg.undefines and #filecfg.undefines > 0 then
local parentUndefines = cfg.undefines or {}
if #filecfg.undefines ~= #parentUndefines then
return true
end
for i, undefine in ipairs(filecfg.undefines) do
if undefine ~= parentUndefines[i] then
return true
end
end
end
if filecfg.buildoptions and #filecfg.buildoptions > 0 then
local parentBuildopts = cfg.buildoptions or {}
if #filecfg.buildoptions ~= #parentBuildopts then
return true
end
for i, opt in ipairs(filecfg.buildoptions) do
if opt ~= parentBuildopts[i] then
return true
end
end
end
if filecfg.includedirs and #filecfg.includedirs > 0 then
local parentIncludedirs = cfg.includedirs or {}
if #filecfg.includedirs ~= #parentIncludedirs then
return true
end
for i, dir in ipairs(filecfg.includedirs) do
if dir ~= parentIncludedirs[i] then
return true
end
end
end
if filecfg.externalincludedirs and #filecfg.externalincludedirs > 0 then
local parentExternalIncludedirs = cfg.externalincludedirs or {}
if #filecfg.externalincludedirs ~= #parentExternalIncludedirs then
return true
end
for i, dir in ipairs(filecfg.externalincludedirs) do
if dir ~= parentExternalIncludedirs[i] then
return true
end
end
end
if filecfg.cdialect and filecfg.cdialect ~= cfg.cdialect then
return true
end
if filecfg.cppdialect and filecfg.cppdialect ~= cfg.cppdialect then
return true
end
return false
end
function m.getLdFlags(cfg, toolset)
local flags = {}
toolset = toolset or ninja.gettoolset(cfg)
local toolFlags = toolset.getldflags(cfg)
flags = table.join(flags, toolFlags)
local linkopts = cfg.linkoptions or {}
flags = table.join(flags, linkopts)
local libdirs = toolset.getLibraryDirectories(cfg)
flags = table.join(flags, libdirs)
local sections = toolset.getSections(cfg)
flags = table.join(flags, sections)
if toolset.getrunpathdirs then
local runpathdirs = table.join(cfg.runpathdirs, config.getsiblingtargetdirs(cfg))
local rpaths = toolset.getrunpathdirs(cfg, runpathdirs)
flags = table.join(flags, rpaths)
end
return flags
end
function m.getPchPath(cfg)
if not cfg.pchheader or cfg.enablepch == p.OFF then
return nil
end
local toolset = ninja.gettoolset(cfg)
local objdir = path.getrelative(cfg.project.location, cfg.objdir)
if toolset == p.tools.msc then
return objdir .. "/" .. path.getbasename(cfg.pchheader) .. ".pch"
else
local pch = toolset.getpch and toolset.getpch(cfg)
if pch then
return objdir .. "/" .. path.getname(pch) .. ".gch"
else
return objdir .. "/" .. path.getbasename(cfg.pchheader) .. ".h.gch"
end
end
end
function m.buildPch(cfg)
if not cfg.pchheader or cfg.enablepch == p.OFF then
return nil
end
local toolset = ninja.gettoolset(cfg)
local pchPath = m.getPchPath(cfg)
local depfile = pchPath .. ".d"
if not pchPath then
return nil
end
local implicitDeps = ""
if cfg._dependsOnTargets then
implicitDeps = " | " .. table.concat(cfg._dependsOnTargets, " ")
end
if toolset == p.tools.msc then
local pchSource = cfg.pchsource
if not pchSource then
return nil
end
local relPath = path.getrelative(cfg.workspace.location, pchSource)
local objdir = path.getrelative(cfg.workspace.location, cfg.objdir)
local objFile = objdir .. "/" .. path.getbasename(pchSource) .. ".obj"
local wksRelPchPath = path.getrelative(cfg.workspace.location, path.join(cfg.project.location, pchPath))
_p("build %s | %s: pch_%s %s%s", wksRelPchPath, objFile, cfg.toolset, relPath, implicitDeps)
_p(" pchheader = %s", cfg.pchheader)
_p(" objdir = %s", objdir)
if cfg.language == "C" then
_p(" cflags = $cflags_%s", ninja.key(cfg))
else
_p(" cflags = $cxxflags_%s", ninja.key(cfg))
end
return wksRelPchPath
else
local pch = toolset.getpch(cfg)
local relPath
if pch then
-- pch is workspace-relative because ninja.getrelative is active when this runs
relPath = pch
else
relPath = cfg.pchheader
end
local wksRelPchPath = path.getrelative(cfg.workspace.location, path.join(cfg.project.location, pchPath))
local wksRelPchDepPath = path.getrelative(cfg.workspace.location, path.join(cfg.project.location, depfile))
_p("build %s | %s: pch_%s %s%s", wksRelPchPath, wksRelPchDepPath, cfg.toolset, relPath, implicitDeps)
if cfg.language == "C" then
_p(" cflags = $cflags_%s", ninja.key(cfg))
_p(" depfile = %s", wksRelPchDepPath)
else
_p(" cflags = $cxxflags_%s", ninja.key(cfg))
_p(" depfile = %s", wksRelPchDepPath)
end
return wksRelPchPath
end
end
function m.buildFiles(cfg)
local tr = project.getsourcetree(cfg.project)
local objList = {}
local copyList = {}
local pchFile = m.buildPch(cfg)
local prebuildTarget = m.buildPreBuildEvents(cfg)
cfg._hasPrebuild = (prebuildTarget ~= nil)
if not cfg.project._ninja_output_tracking then
cfg.project._ninja_output_tracking = {}
end
local outputTracking = cfg.project._ninja_output_tracking
if not cfg._customRuleOutputs then
cfg._customRuleOutputs = {}
end
tree.traverse(tr, {
onleaf = function(node, depth)
local filecfg = fileconfig.getconfig(node, cfg)
if filecfg then
if filecfg.buildaction == "None" or filecfg.excludefrombuild then
return
end
local toolset = ninja.gettoolset(cfg)
if not (cfg.pchsource and node.abspath == cfg.pchsource and toolset == p.tools.msc) then
local customRuleFile = m.checkCustomRuleFile(cfg, node, filecfg, outputTracking)
if customRuleFile and customRuleFile.outputs then
for _, output in ipairs(customRuleFile.outputs) do
table.insert(cfg._customRuleOutputs, output)
if path.islinkable(output) then
table.insert(objList, output)
end
end
elseif filecfg.buildcommands and #filecfg.buildcommands > 0 and
filecfg.buildoutputs and #filecfg.buildoutputs > 0 then
local outputs = m.buildCustomFile(cfg, node, filecfg, outputTracking)
if outputs then
for _, output in ipairs(outputs) do
table.insert(cfg._customRuleOutputs, output)
end
local shouldLink = true
if filecfg.linkbuildoutputs ~= nil then
shouldLink = filecfg.linkbuildoutputs
end
if shouldLink then
for _, output in ipairs(outputs) do
if path.islinkable(output) then
table.insert(objList, output)
end
end
end
end
end
end
end
end
}, false, 1)
tree.traverse(tr, {
onleaf = function(node, depth)
local filecfg = fileconfig.getconfig(node, cfg)
if filecfg then
if filecfg.buildaction == "None" or filecfg.excludefrombuild then
return
end
local toolset = ninja.gettoolset(cfg)
if not (cfg.pchsource and node.abspath == cfg.pchsource and toolset == p.tools.msc) then
local rule = p.global.getRuleForFile(node.abspath, cfg.project.rules or {})
local hasCustomBuild = filecfg.buildcommands and #filecfg.buildcommands > 0 and
filecfg.buildoutputs and #filecfg.buildoutputs > 0
if not rule and not hasCustomBuild then
if filecfg.buildaction == "Copy" then
local output = m.buildCopyFile(cfg, node, filecfg)
if output then
table.insert(copyList, output)
local shouldLink = (filecfg.linkbuildoutputs ~= false)
if shouldLink and path.islinkable(output) then
table.insert(objList, output)
end
end
else
local objFile = m.objectFile(cfg, node, filecfg)
if objFile then
table.insert(objList, objFile)
m.buildFile(cfg, node, filecfg, objFile, pchFile, prebuildTarget)
end
end
end
end
end
end
}, false, 1)
-- If there is a PCH and this is MSVC, make sure the PCH obj is in the object list
if pchFile and ninja.gettoolset(cfg) == p.tools.msc then
local objdir = path.getrelative(cfg.workspace.location, cfg.objdir)
local pchObjFile = objdir .. "/" .. path.getbasename(cfg.pchsource) .. ".obj"
table.insert(objList, 1, pchObjFile)
end
cfg._objectFiles = objList
cfg._copyFiles = copyList
end
function m.objectFile(cfg, node, filecfg)
local objdir = path.getrelative(cfg.workspace.location, cfg.objdir)
local ext = path.getextension(node.abspath):lower()
local shouldCompile = false
if filecfg and filecfg.buildaction == "Compile" then
shouldCompile = true
elseif filecfg and filecfg.compileas and filecfg.compileas ~= "Default" then
if p.languages.isc(filecfg.compileas) or p.languages.iscpp(filecfg.compileas) then
shouldCompile = true
end
else
if path.iscppfile(node.abspath) or path.iscfile(node.abspath) then
shouldCompile = true
end
end
if shouldCompile then
local objname = filecfg.objname or path.getbasename(node.abspath)
local toolset = ninja.gettoolset(cfg)
local objext = toolset.gettooloutputext("cc")
return objdir .. "/" .. objname .. objext
end
return nil
end
function m.buildFile(cfg, node, filecfg, objFile, pchFile, prebuildTarget)
local ext = path.getextension(node.abspath):lower()
local rule = nil
local flags = ""
local extraFlags = {}
local toolset = ninja.gettoolset(cfg)
local hasPerFileConfig = m.hasPerFileConfiguration(cfg, filecfg)
if filecfg and filecfg.compileas and filecfg.compileas ~= "Default" then
if p.languages.isc(filecfg.compileas) then
rule = "cc"
flags = "cflags_" .. ninja.key(cfg)
if toolset.shared and toolset.shared.compileas and toolset.shared.compileas["C"] then
table.insert(extraFlags, toolset.shared.compileas["C"])
end
elseif p.languages.iscpp(filecfg.compileas) then
rule = "cxx"
flags = "cxxflags_" .. ninja.key(cfg)
if toolset.shared and toolset.shared.compileas and toolset.shared.compileas["C++"] then
table.insert(extraFlags, toolset.shared.compileas["C++"])
end
end
elseif filecfg and filecfg.buildaction == "Compile" then
if p.languages.isc(cfg.language) then
rule = "cc"
flags = "cflags_" .. ninja.key(cfg)
else
rule = "cxx"
flags = "cxxflags_" .. ninja.key(cfg)
end
else
if path.iscfile(node.abspath) then
rule = "cc"
flags = "cflags_" .. ninja.key(cfg)
elseif path.iscppfile(node.abspath) then
rule = "cxx"
flags = "cxxflags_" .. ninja.key(cfg)
end
end
if rule then
local relPath = path.getrelative(cfg.workspace.location, node.abspath)
local implicitDeps = ""
local usePch = cfg.pchheader and cfg.enablepch ~= p.OFF and (not filecfg or filecfg.enablepch ~= p.OFF)
if pchFile and usePch then
implicitDeps = implicitDeps .. " | " .. pchFile
end
if prebuildTarget then
if implicitDeps == "" then
implicitDeps = " |"
end
implicitDeps = implicitDeps .. " " .. prebuildTarget
end
if cfg._customRuleOutputs and #cfg._customRuleOutputs > 0 then
if implicitDeps == "" then
implicitDeps = " |"
end
for _, output in ipairs(cfg._customRuleOutputs) do
implicitDeps = implicitDeps .. " " .. output
end
end
-- Check if there are any dependson targets and add them as implicit dependencies
if cfg._dependsOnTargets then
if implicitDeps == "" then
implicitDeps = " |"
end
implicitDeps = implicitDeps .. " " .. table.concat(cfg._dependsOnTargets, " ")
end
_p("build %s: %s_%s %s%s", objFile, rule, cfg.toolset, relPath, implicitDeps)
if usePch then
if toolset == p.tools.msc then
table.insert(extraFlags, "/Yu" .. cfg.pchheader)
local pchPath = m.getPchPath(cfg)
if pchPath then
local wksRelPchPath = path.getrelative(cfg.workspace.location, path.join(cfg.project.location, pchPath))
table.insert(extraFlags, "/Fp" .. wksRelPchPath)
end
else
local pch = toolset.getpch(cfg)
if pch then
local objdir = path.getrelative(cfg.workspace.location, cfg.objdir)
local pchPlaceholder = objdir .. "/" .. path.getname(pch)
-- Add the directory containing the PCH header to the search path so
-- #include "pch.h" in source files can be resolved when building from
-- the workspace root (where the project subdirectory is not in the path).
local pchDir = path.getdirectory(pch)
if pchDir and pchDir ~= "" and pchDir ~= "." then
table.insert(extraFlags, "-I " .. pchDir)
end
table.insert(extraFlags, "-include " .. pchPlaceholder)
end
end
end
-- If the file has per-file configuration, generate flags directly rather than using variables
if hasPerFileConfig then
local fileFlags = {}
if rule == "cc" then
fileFlags = m.getFileCFlags(cfg, filecfg, toolset)
else
fileFlags = m.getFileCxxFlags(cfg, filecfg, toolset)
end
table.insertflat(fileFlags, extraFlags)
if #fileFlags > 0 then
if rule == "cc" then
_p(" cflags = %s", table.concat(fileFlags, " "))
else
_p(" cxxflags = %s", table.concat(fileFlags, " "))
end
end
else
if rule == "cc" then
if #extraFlags > 0 then
_p(" cflags = $%s %s", flags, table.concat(extraFlags, " "))
else
_p(" cflags = $%s", flags)
end
else
if #extraFlags > 0 then
_p(" cxxflags = $%s %s", flags, table.concat(extraFlags, " "))
else
_p(" cxxflags = $%s", flags)
end
end
end
end
end
local function trackOutput(outputTracking, cfg, outputs)
local alreadyGenerated = false
if outputTracking then
for _, output in ipairs(outputs) do
if outputTracking[output] then
alreadyGenerated = true
break
end
end
end
if alreadyGenerated then
return true
end
if outputTracking then
for _, output in ipairs(outputs) do
outputTracking[output] = outputTracking[output] or {}
table.insert(outputTracking[output], ninja.key(cfg))
end
end
return alreadyGenerated
end
function m.checkCustomRuleFile(cfg, node, filecfg, outputTracking)
if not cfg.project.rules or #cfg.project.rules == 0 then
return nil
end
local rule = p.global.getRuleForFile(node.abspath, cfg.project.rules)
if not rule then
return nil
end
local environ = table.shallowcopy(filecfg.environ)
if rule.propertydefinition then
p.rule.prepareEnvironment(rule, environ, cfg)
p.rule.prepareEnvironment(rule, environ, filecfg)
end
local shadowContext = p.context.extent(rule, environ)
local buildoutputs = shadowContext.buildoutputs
local buildmessage = shadowContext.buildmessage
local buildcommands = shadowContext.buildcommands
local buildinputs = shadowContext.buildinputs
if not buildoutputs or #buildoutputs == 0 then
return nil
end
local outputs = {}
for _, output in ipairs(buildoutputs) do
local absOutput = path.getabsolute(path.join(cfg.project.basedir, output))
local relOutput = path.getrelative(cfg.workspace.location, absOutput)
table.insert(outputs, relOutput)
end
local alreadyGenerated = trackOutput(outputTracking, cfg, outputs)
if alreadyGenerated then
return { outputs = outputs }
end
local relPath = path.getrelative(cfg.workspace.location, node.abspath)
local ruleNameEscaped = rule.name:gsub("[^%w_]", "_"):lower()
local deps = ""
if buildinputs and #buildinputs > 0 then
local depList = {}
for _, dep in ipairs(buildinputs) do
local absDep = path.getabsolute(path.join(cfg.project.basedir, dep))
local relDep = path.getrelative(cfg.workspace.location, absDep)
table.insert(depList, relDep)
end
if #depList > 0 then
deps = " | " .. table.concat(depList, " ")
end
end
if cfg._dependsOnTargets and not cfg._hasPrebuild then
if deps == "" then
deps = " |"
end
deps = deps .. " " .. table.concat(cfg._dependsOnTargets, " ")
end
local commands = {}
if buildcommands then