-
Notifications
You must be signed in to change notification settings - Fork 36
/
writer.lua
995 lines (906 loc) · 30.7 KB
/
writer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
local fs = require "bee.filesystem"
local arguments = require "arguments"
local globals = require "globals"
local fsutil = require "fsutil"
local glob = require "glob"
local pathutil = require "pathutil"
local log = require "log"
local sandbox = require "sandbox"
local workspace = require "workspace"
local ninja
local cc
local m = {}
local loaded_target = {}
local loaded_rule = {}
local loaded_conf = {}
local scripts = {}
local visited = {}
local file_type <const> = {
cxx = "cxx",
cpp = "cxx",
cc = "cxx",
mm = "cxx",
c = "c",
m = "c",
rc = "rc",
s = "asm",
asm = "asm",
def = "raw",
obj = "raw",
}
local function fmtpath(p)
return p:gsub("\\", "/")
end
local function init_enum(attribute, attr_name, default, allow)
local v = attribute[attr_name]
if v == nil then
attribute[attr_name] = default
v = default
end
if allow[v] == nil then
local str = {}
for k in pairs(allow) do
str[#str+1] = " - "..k
end
table.sort(str)
table.insert(str, 1, ("The value `%s` of attribute `%s` is invalid. Allowed values:"):format(v, attr_name))
log.fatal(table.concat(str, "\n"))
end
end
local function get_blob(rootdir, lst)
if type(lst) ~= "table" then
return {}
end
local result = glob(rootdir, lst)
for i, r in ipairs(result) do
result[i] = fsutil.relative(r, WORKDIR)
end
table.sort(result)
return result
end
local function tbl_append(t, a)
table.move(a, 1, #a, #t + 1, t)
end
local function tbl_insert(t, pos, a)
for i = 1, #a do
table.insert(t, pos + i - 1, a[i])
end
end
local function normalize_rootdir(workdir, rootdir)
return pathutil.tostr(workdir, rootdir)
end
local function reslove_attributes(g, l)
local l_rootdir = normalize_rootdir(g.workdir, l.rootdir or g.rootdir)
local t = {}
workspace.push_attributes(t, g)
if l.confs then
for _, conf in ipairs(l.confs) do
local conf_attribute = log.assert(loaded_conf[conf], "unknown conf `%s`", conf)
workspace.push_attributes(t, conf_attribute)
end
end
workspace.resolve_attributes(t, l, l_rootdir)
t.workdir = g.workdir
t.rootdir = l_rootdir
return t
end
local function reslove_attributes_nolink(g, l)
local l_rootdir = normalize_rootdir(g.workdir, l.rootdir or g.rootdir)
local t = {}
workspace.push_attributes(t, g, true)
if l.confs then
for _, conf in ipairs(l.confs) do
local conf_attribute = log.assert(loaded_conf[conf], "unknown conf `%s`", conf)
workspace.push_attributes(t, conf_attribute, true)
end
end
workspace.resolve_attributes(t, l, l_rootdir)
t.workdir = g.workdir
t.rootdir = l_rootdir
return t
end
local function reslove_args(args)
local command = {}
for i, v in ipairs(args) do
command[i] = fsutil.quotearg(v)
end
return table.concat(command, " ")
end
local function array_remove(t, k)
for pos, v in ipairs(t) do
if v == k then
table.remove(t, pos)
return true
end
end
return false
end
local function update_flags(flags, cflags, cxxflags, attribute, name, rule)
local defines = attribute.defines or {}
tbl_append(flags, cc.flags)
flags[#flags+1] = cc.optimize[attribute.optimize]
flags[#flags+1] = cc.warnings[attribute.warnings]
if globals.os ~= "windows" then
if attribute.visibility ~= "default" then
flags[#flags+1] = ("-fvisibility=%s"):format(attribute.visibility)
end
if rule == "shared_library" then
flags[#flags+1] = "-fPIC"
end
end
log.assert(cc.c[attribute.c], "`%s`: unknown std c: `%s`", name, attribute.c)
log.assert(cc.cxx[attribute.cxx], "`%s`: unknown std c++: `%s`", name, attribute.cxx)
cflags[#cflags+1] = cc.c[attribute.c]
cxxflags[#cxxflags+1] = cc.cxx[attribute.cxx]
cc.update_flags(flags, cflags, cxxflags, attribute, name)
if attribute.includes then
for _, inc in ipairs(attribute.includes) do
flags[#flags+1] = cc.includedir(inc)
end
end
if attribute.sysincludes then
for _, inc in ipairs(attribute.sysincludes) do
flags[#flags+1] = cc.sysincludedir(inc)
end
end
if attribute.mode ~= "debug" then
defines[#defines+1] = "NDEBUG"
end
if attribute.undefs then
local undefs = attribute.undefs
local pos = 1
while pos <= #undefs do
local macro = undefs[pos]
if array_remove(defines, macro) then
table.remove(undefs, pos)
else
pos = pos + 1
end
end
for _, macro in ipairs(defines) do
flags[#flags+1] = cc.define(macro)
end
for _, macro in ipairs(undefs) do
flags[#flags+1] = cc.undef(macro)
end
else
for _, macro in ipairs(defines) do
flags[#flags+1] = cc.define(macro)
end
end
if attribute.flags then
tbl_append(flags, attribute.flags)
end
end
local function update_ldflags(ldflags, attribute, name)
tbl_append(ldflags, cc.ldflags)
if attribute.links then
for _, link in ipairs(attribute.links) do
ldflags[#ldflags+1] = cc.link(link)
end
end
if attribute.linkdirs then
for _, linkdir in ipairs(attribute.linkdirs) do
ldflags[#ldflags+1] = cc.linkdir(linkdir)
end
end
if attribute.ldflags then
tbl_append(ldflags, attribute.ldflags)
end
if attribute.deps then
for _, dep in ipairs(attribute.deps) do
local target = loaded_target[dep]
log.assert(target, "`%s`: can`t find deps `%s`", name, dep)
if target.ldflags then
tbl_append(ldflags, target.ldflags)
end
end
end
cc.update_ldflags(ldflags, attribute, name)
end
local enum_onoff <const> = { on = true, off = true }
local enum_mode <const> = { release = true, debug = true }
local enum_crt <const> = { dynamic = true, static = true }
local enum_visibility <const> = { default = true, hidden = true }
local enum_luaversion <const> = { [""] = true, lua53 = true, lua54 = true, lua55 = true }
local function generate(rule, attribute, name)
local target = loaded_target[name]
local input
local ldflags
local deps
do
if target then
if target.rule == "source_set" then
target.rule = rule
input = target.input
ldflags = target.ldflags
deps = target.deps
target.input = nil
target.ldflags = nil
target.deps = nil
else
log.fatal("`%s`: redefinition.", name)
end
else
target = { rule = rule }
input = {}
ldflags = {}
loaded_target[name] = target
end
end
local bindir = attribute.bindir
local sources = get_blob(attribute.rootdir, attribute.sources)
local objargs = attribute.objdeps and { implicit_inputs = attribute.objdeps } or nil
local implicit_inputs = {}
init_enum(attribute, "mode", "release", enum_mode)
init_enum(attribute, "crt", "dynamic", enum_crt)
init_enum(attribute, "c", "", cc.c)
init_enum(attribute, "cxx", "", cc.cxx)
init_enum(attribute, "warnings", "on", cc.warnings)
init_enum(attribute, "rtti", "on", enum_onoff)
init_enum(attribute, "visibility", "hidden", enum_visibility)
init_enum(attribute, "luaversion", "", enum_luaversion)
init_enum(attribute, "optimize", (attribute.mode == "debug" and "off" or "speed"), cc.optimize)
local default_enable_lto = attribute.mode ~= "debug" and globals.compiler == "msvc"
init_enum(attribute, "lto", default_enable_lto and "on" or "off", enum_onoff)
if globals.compiler == "msvc" then
init_enum(attribute, "permissive", "off", enum_onoff)
end
if attribute.luaversion ~= "" then
init_enum(attribute, "export_luaopen", "on", enum_onoff)
require "lua_support" (ninja, loaded_target, rule, name, attribute)
end
if attribute.deps then
if deps then
tbl_append(deps, attribute.deps)
else
deps = attribute.deps
end
end
local flags = {}
local cflags = {}
local cxxflags = {}
update_flags(flags, cflags, cxxflags, attribute, name, rule)
local str_flags = table.concat(flags, " ")
local str_cflags = table.concat(cflags, " ")
local str_cxxflags = table.concat(cxxflags, " ")
for _, source in ipairs(sources) do
local ext = fsutil.extension(source):sub(2):lower()
local type = file_type[ext]
if type == "raw" then
input[#input+1] = source
goto continue
end
local objpath = fsutil.join("$obj", name, fsutil.filename(source))
if type == "c" then
cc.rule_c(ninja, name, str_flags, str_cflags)
input[#input+1] = ninja:build_obj(objpath, source, objargs)
elseif type == "cxx" then
cc.rule_cxx(ninja, name, str_flags, str_cxxflags)
input[#input+1] = ninja:build_obj(objpath, source, objargs)
elseif globals.os == "windows" and type == "rc" then
cc.rule_rc(ninja, name)
input[#input+1] = ninja:build_obj(objpath, source, objargs)
elseif type == "asm" then
cc.rule_asm(ninja, name, str_flags)
input[#input+1] = ninja:build_obj(objpath, source, objargs)
else
log.fatal("`%s`: unknown file extension: `%s` in `%s`", name, ext, source)
end
::continue::
end
if rule == "source_set" then
if attribute.links then
for _, link in ipairs(attribute.links) do
ldflags[#ldflags+1] = cc.link(link)
end
end
if attribute.linkdirs then
for _, linkdir in ipairs(attribute.linkdirs) do
ldflags[#ldflags+1] = cc.linkdir(linkdir)
end
end
if attribute.ldflags then
tbl_append(ldflags, attribute.ldflags)
end
if globals.compiler == "clang" and attribute.frameworks then
for _, framework in ipairs(attribute.frameworks) do
ldflags[#ldflags+1] = "-framework"
ldflags[#ldflags+1] = framework
end
end
target.input = input
target.deps = deps
target.ldflags = ldflags
return
end
if deps then
local mark = { [name] = true }
local i = 1
while i <= #deps do
local dep = deps[i]
if mark[dep] then
table.remove(deps, i)
else
mark[dep] = true
local t = loaded_target[dep]
log.assert(t, "`%s`: deps `%s` undefine.", name, dep)
if t.deps then
tbl_insert(deps, i + 1, t.deps)
end
i = i + 1
end
end
for _, dep in ipairs(deps) do
local t = loaded_target[dep]
if t.input then
tbl_append(input, t.input)
end
implicit_inputs[#implicit_inputs+1] = t.implicit_inputs
end
end
log.assert(#input > 0, "`%s`: no source files found.", name)
local binname
if bindir == globals.bindir then
bindir = "$bin"
end
update_ldflags(ldflags, attribute, name)
local fin_ldflags = table.concat(ldflags, " ")
local basename = attribute.basename or name
if rule == "shared_library" then
cc.rule_dll(ninja, name, fin_ldflags)
if globals.compiler == "msvc" then
binname = bindir.."/"..basename..".dll"
local lib = ("$obj/%s/%s.lib"):format(name, basename)
target.input = { lib }
target.implicit_inputs = binname
ninja:build(binname, input, {
implicit_inputs = implicit_inputs,
implicit_outputs = lib,
variables = {
implib = lib,
},
})
elseif globals.os == "windows" then
binname = bindir.."/"..basename..".dll"
target.input = { binname }
ninja:build(binname, input, {
implicit_inputs = implicit_inputs,
})
else
if globals.compiler == "emcc" then
binname = bindir.."/"..basename..".wasm"
else
binname = bindir.."/"..basename..".so"
end
target.implicit_inputs = binname
ninja:build(binname, input, {
implicit_inputs = implicit_inputs,
})
end
elseif rule == "executable" then
if globals.compiler == "emcc" then
binname = bindir.."/"..basename..".js"
elseif globals.os == "windows" then
binname = bindir.."/"..basename..".exe"
else
binname = bindir.."/"..basename
end
target.implicit_inputs = binname
cc.rule_exe(ninja, name, fin_ldflags)
ninja:build(binname, input, {
implicit_inputs = implicit_inputs,
})
elseif rule == "static_library" then
if globals.os == "windows" then
binname = bindir.."/"..basename..".lib"
else
binname = bindir.."/lib"..basename..".a"
end
target.input = { binname }
cc.rule_lib(ninja, name)
ninja:build(binname, input, {
implicit_inputs = implicit_inputs,
})
end
ninja:phony(name, binname)
end
local function getImplicitInputs(name, attribute)
local res = {}
if attribute.deps then
for _, dep in ipairs(attribute.deps) do
local target = loaded_target[dep]
if not target then
if name then
log.fatal("`%s`: deps `%s` undefine.", name, dep)
else
log.fatal("deps `%s` undefine.", dep)
end
end
if target.input then
tbl_append(res, target.input)
end
res[#res+1] = target.implicit_inputs
end
end
return res
end
local GEN = {}
local NAMEIDX = 0
local function generateTargetName()
NAMEIDX = NAMEIDX + 1
return ("__target_0x%08x__"):format(NAMEIDX)
end
function GEN.phony(attribute, name)
local inputs = get_blob(attribute.rootdir, attribute.inputs)
local outputs = attribute.outputs or {}
local implicit_inputs = getImplicitInputs(name, attribute)
local n = #inputs
for i = 1, #implicit_inputs do
inputs[n + i] = implicit_inputs[i]
end
if name then
if #outputs == 0 then
if #inputs == 0 then
log.fatal("`%s`: no input.", name)
else
ninja:phony(name, inputs)
end
else
ninja:phony(name, outputs)
for _, out in ipairs(outputs) do
ninja:phony(out, inputs)
end
end
loaded_target[name] = {
implicit_inputs = name,
}
else
if #outputs == 0 then
log.fatal("no output.")
else
for _, out in ipairs(outputs) do
ninja:phony(out, inputs)
end
end
end
end
function GEN.runlua(attribute, name)
local tmpName = not name
name = name or generateTargetName()
log.assert(loaded_target[name] == nil, "`%s`: redefinition.", name)
local script = attribute.script
log.assert(script, "`%s`: need attribute `script`.", name)
local inputs = get_blob(attribute.rootdir, attribute.inputs)
local outputs = attribute.outputs or {}
local implicit_inputs = getImplicitInputs(name, attribute)
implicit_inputs[#implicit_inputs+1] = script
if attribute.args then
local command = reslove_args(attribute.args)
ninja:rule("runlua", "$luamake lua $script "..command, {
description = "lua $script "..command
})
else
ninja:rule("runlua", "$luamake lua $script", {
description = "lua $script"
})
end
local outname
if #outputs == 0 then
outname = "$builddir/_/"..name
else
outname = outputs
end
ninja:build(outname, inputs, {
variables = {
script = script,
},
implicit_inputs = implicit_inputs,
})
if not tmpName then
ninja:phony(name, outname)
loaded_target[name] = {
implicit_inputs = name,
}
end
end
function GEN.build(attribute, name)
local tmpName = not name
name = name or generateTargetName()
log.assert(loaded_target[name] == nil, "`%s`: redefinition.", name)
local inputs = get_blob(attribute.rootdir, attribute.inputs)
local outputs = attribute.outputs or {}
local implicit_inputs = getImplicitInputs(name, attribute)
local rule = attribute.rule
local outname
if #outputs == 0 then
outname = "$builddir/_/"..name
else
outname = outputs
end
if rule then
log.assert(loaded_rule[rule], "unknown rule `%s`", rule)
local command
if attribute.args then
command = reslove_args(attribute.args)
end
ninja:set_rule(rule)
ninja:build(outname, inputs, {
implicit_inputs = implicit_inputs,
variables = { args = command },
})
else
local command = reslove_args(attribute.args)
ninja:rule("build_"..name, command)
ninja:build(outname, inputs, {
implicit_inputs = implicit_inputs,
})
end
if not tmpName then
ninja:phony(name, outname)
loaded_target[name] = {
implicit_inputs = name,
}
end
end
local function generate_copy(implicit_inputs, inputs, outputs)
if globals.hostshell == "cmd" then
ninja:rule("copy", "powershell -NonInteractive -Command Copy-Item -Path '$in$input' -Destination '$out' | Out-Null", {
description = "Copy $in$input $out",
restat = 1,
})
elseif globals.hostos == "windows" then
ninja:rule("copy", 'sh -c "cp -fv $in$input $out 1>/dev/null"', {
description = "Copy $in$input $out",
restat = 1,
})
elseif globals.hostos == "macos" then
-- see https://developer.apple.com/documentation/security/updating_mac_software
ninja:rule("copy", "ditto $in$input $out 1>/dev/null", {
description = "Copy $in$input $out",
restat = 1,
})
else
ninja:rule("copy", "cp -fv $in$input $out 1>/dev/null", {
description = "Copy $in$input $out",
restat = 1,
})
end
if #implicit_inputs == 0 then
for i = 1, #inputs do
ninja:build(outputs[i], inputs[i])
end
else
for i = 1, #inputs do
ninja:build(outputs[i], nil, {
implicit_inputs = implicit_inputs,
variables = { input = inputs[i] },
})
end
end
end
function GEN.copy(attribute, name)
local inputs = attribute.inputs or {}
local outputs = attribute.outputs or {}
for i = 1, #inputs do
inputs[i] = pathutil.tostr(attribute.rootdir, inputs[i])
end
local implicit_inputs = getImplicitInputs(name, attribute)
log.assert(#inputs == #outputs, "`%s`: The number of input and output must be the same.", name)
generate_copy(implicit_inputs, inputs, outputs)
if name then
log.assert(loaded_target[name] == nil, "`%s`: redefinition.", name)
ninja:phony(name, outputs)
loaded_target[name] = {
implicit_inputs = name,
}
end
end
local enum_copy_type <const> = { vcrt = true, ucrt = true, asan = true }
function GEN.msvc_copydll(attribute, name)
if globals.compiler ~= "msvc" then
return
end
local inputs = {}
local outputs = {}
local implicit_inputs = getImplicitInputs(name, attribute)
init_enum(attribute, "mode", "release", enum_mode)
init_enum(attribute, "optimize", (attribute.mode == "debug" and "off" or "speed"), cc.optimize)
init_enum(attribute, "type", nil, enum_copy_type)
local msvc = require "env.msvc"
local archalias = msvc.archAlias(attribute.arch)
local attributeOutputs = attribute.outputs
if attribute.type == "vcrt" then
local ignore = attribute.optimize == "off" and "vccorlib140d.dll" or "vccorlib140.dll"
for dll in fs.pairs(msvc.vcrtpath(archalias, attribute.optimize)) do
local filename = dll:filename():string()
if filename:lower() ~= ignore then
for _, outputdir in ipairs(attributeOutputs) do
inputs[#inputs+1] = dll
outputs[#outputs+1] = fsutil.join(outputdir, filename)
end
end
end
elseif attribute.type == "ucrt" then
local redist, bin = msvc.ucrtpath(archalias, attribute.optimize)
if attribute.optimize == "off" then
for dll in fs.pairs(redist) do
local filename = dll:filename():string()
if filename:lower() == "ucrtbase.dll" then
for _, outputdir in ipairs(attributeOutputs) do
inputs[#inputs+1] = fsutil.join(bin, "ucrtbased.dll")
outputs[#outputs+1] = fsutil.join(outputdir, "ucrtbased.dll")
end
else
for _, outputdir in ipairs(attributeOutputs) do
inputs[#inputs+1] = dll
outputs[#outputs+1] = fsutil.join(outputdir, filename)
end
end
end
else
for dll in fs.pairs(redist) do
local filename = dll:filename():string()
for _, outputdir in ipairs(attributeOutputs) do
inputs[#inputs+1] = dll
outputs[#outputs+1] = fsutil.join(outputdir, filename)
end
end
end
elseif attribute.type == "asan" then
local inputdir = msvc.binpath(archalias)
local filename = ("clang_rt.asan_dynamic-%s.dll"):format(
attribute.arch == "x86_64" and "x86_64" or "i386"
)
for _, outputdir in ipairs(attributeOutputs) do
inputs[#inputs+1] = fsutil.join(inputdir, filename)
outputs[#outputs+1] = fsutil.join(outputdir, filename)
end
end
generate_copy(implicit_inputs, inputs, outputs)
if name then
log.assert(loaded_target[name] == nil, "`%s`: redefinition.", name)
ninja:phony(name, outputs)
loaded_target[name] = {
implicit_inputs = name,
}
end
end
local function get_luamake()
return fmtpath(fs.exe_path():string())
end
local function configure_args()
local s = {}
if arguments.C then
s[#s+1] = "-C"
s[#s+1] = arguments.C
end
for _, v in ipairs(arguments.targets) do
s[#s+1] = v
end
for k, v in pairs(arguments.args) do
s[#s+1] = "-"..k
if v ~= "on" then
s[#s+1] = v
end
end
return table.concat(s, " ")
end
function m.init()
ninja = require "ninja_writer" ()
cc = require("compiler."..globals.compiler)
ninja:switch_body()
end
function m.generate()
ninja:switch_head()
local builddir = fsutil.join(WORKDIR, globals.builddir)
fs.create_directories(builddir)
ninja:variable("ninja_required_version", "1.7")
ninja:variable("builddir", fmtpath(globals.builddir))
ninja:variable("bin", fmtpath(globals.bindir))
ninja:variable("obj", fmtpath(globals.objdir))
if globals.os == "android" and globals.hostos ~= "android" then
require "env.ndk"
end
if globals.compiler == "wasi" then
require "env.wasi"
end
if globals.compiler == "msvc" then
if not globals.prebuilt then
local msvc = require "env.msvc"
msvc.createEnvConfig(globals.arch, arguments.what == "rebuild")
ninja:variable("msvc_deps_prefix", globals.cc == "clang-cl"
and "Note: including file:"
or msvc.getPrefix()
)
end
ninja:variable("cc", globals.cc or "cl")
ninja:variable("ml", globals.arch == "x86_64" and "ml64" or "ml")
else
local compiler = globals.cc or globals.compiler
if globals.hostshell == "cmd" then
compiler = "cmd /c "..compiler
end
ninja:variable("cc", compiler)
ninja:variable("ar", globals.ar or "ar")
end
if globals.prebuilt then
ninja:variable("luamake", "luamake")
else
ninja:variable("luamake", get_luamake())
ninja:rule("configure", "$luamake init "..configure_args(), { generator = 1 })
ninja:build("$builddir/build.ninja", scripts)
end
local ninja_script = fsutil.join(builddir, "build.ninja")
ninja:close(ninja_script)
end
local api = {}
local compile_target <const> = {
"executable",
"shared_library",
"static_library",
}
for _, rule in ipairs(compile_target) do
api[rule] = function (global_attribute, name)
log.assert(type(name) == "string", "Name is not a string.")
return function (local_attribute)
local attribute = reslove_attributes(global_attribute, local_attribute)
generate(rule, attribute, name)
end
end
end
local lua_compile_target <const> = {
lua_exe = "executable",
lua_dll = "shared_library",
lua_lib = "static_library",
}
for rule, origin_rule in pairs(lua_compile_target) do
api[rule] = function (global_attribute, name)
log.assert(type(name) == "string", "Name is not a string.")
return function (local_attribute)
local attribute = reslove_attributes(global_attribute, local_attribute)
attribute.luaversion = attribute.luaversion or "lua54"
generate(origin_rule, attribute, name)
end
end
end
function api.source_set(global_attribute, name)
log.assert(type(name) == "string", "Name is not a string.")
return function (local_attribute)
local attribute = reslove_attributes_nolink(global_attribute, local_attribute)
generate("source_set", attribute, name)
end
end
function api.lua_src(global_attribute, name)
log.assert(type(name) == "string", "Name is not a string.")
return function (local_attribute)
local attribute = reslove_attributes_nolink(global_attribute, local_attribute)
attribute.luaversion = attribute.luaversion or "lua54"
generate("source_set", attribute, name)
end
end
local alias <const> = {
exe = "executable",
dll = "shared_library",
lib = "static_library",
src = "source_set",
lua_library = "lua_dll",
lua_source = "lua_src",
}
for to, from in pairs(alias) do
api[to] = api[from]
end
for rule, genfunc in pairs(GEN) do
api[rule] = function (global_attribute, name)
if type(name) == "table" then
local local_attribute = name
local attribute = reslove_attributes(global_attribute, local_attribute)
genfunc(attribute)
else
log.assert(type(name) == "string", "Name is not a string.")
return function (local_attribute)
local attribute = reslove_attributes(global_attribute, local_attribute)
genfunc(attribute, name)
end
end
end
end
function api.rule(global_attribute, name)
log.assert(type(name) == "string", "Name is not a string.")
return function (local_attribute)
local attribute = reslove_attributes(global_attribute, local_attribute)
log.assert(loaded_rule[name] == nil, "rule `%s`: redefinition.", name)
loaded_rule[name] = true
if attribute.deps then
attribute.deps = attribute.deps[#attribute.deps]
end
local command = reslove_args(attribute.args)
ninja:rule(name, command, attribute)
end
end
function api.conf(global_attribute, name)
if type(name) == "table" then
local local_attribute = name
local root = normalize_rootdir(global_attribute.workdir, local_attribute.rootdir or global_attribute.rootdir)
local attribute = {}
workspace.resolve_attributes(attribute, local_attribute, root)
global_attribute("set", attribute)
else
log.assert(type(name) == "string", "Name is not a string.")
return function (local_attribute)
local root = normalize_rootdir(global_attribute.workdir, local_attribute.rootdir or global_attribute.rootdir)
local attribute = {}
workspace.resolve_attributes(attribute, local_attribute, root)
loaded_conf[name] = attribute
end
end
end
function api:has(name)
log.assert(type(name) == "string", "Name is not a string.")
return loaded_target[name] ~= nil
end
function api:path(value)
return pathutil.create(self.workdir, value)
end
function api:required_version(buildVersion)
local function parse_version(v)
local major, minor = v:match "^(%d+)%.(%d+)"
if not major then
log.fastfail("Invalid version string: `%s`.", v)
end
return tonumber(major) * 1000 + tonumber(minor)
end
local luamakeVersion = require "version"
if parse_version(luamakeVersion) < parse_version(buildVersion) then
log.fastfail("luamake version (%s) incompatible with build file required_version (%s).", luamakeVersion, buildVersion)
end
end
local MainWorkspace = workspace.create(globals.workdir, api, globals)
function api:default(attribute)
if self == MainWorkspace then
local deps = {}
workspace.push_strings(deps, attribute)
local implicit_inputs = getImplicitInputs("default", { deps = deps })
ninja:default(implicit_inputs)
end
end
local function openfile(name, mode)
local f, err = io.open(name, mode)
if f and (mode == nil or mode:match "r") then
if not scripts[name] then
scripts[name] = true
scripts[#scripts+1] = fsutil.relative(name, WORKDIR)
end
end
return f, err
end
local function importfile(ws, rootdir, filename)
local subws = ws and workspace.create(rootdir, ws, {}) or MainWorkspace
sandbox {
rootdir = rootdir,
builddir = globals.builddir,
preload = {
luamake = subws,
},
openfile = openfile,
main = filename,
args = {}
}
end
function api:import(path)
local ws = self
local fullpath = pathutil.tostr(ws.workdir, path)
if visited[fullpath] then
return
end
visited[fullpath] = true
local rootdir = fsutil.parent_path(fullpath)
local filename = fsutil.filename(fullpath)
importfile(ws, rootdir, filename)
end
function m.import(path)
importfile(nil, WORKDIR, path or "make.lua")
end
api.pcall = log.pcall
api.xpcall = log.xpcall
return m