-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.lua
1793 lines (1557 loc) · 53.7 KB
/
build.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
996
997
998
999
1000
section [[
This file is part of luax.
luax is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
luax is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with luax. If not, see <https://www.gnu.org/licenses/>.
For further information about luax you can visit
https://github.com/cdsoft/luax
]]
local F = require "F"
local fs = require "fs"
local sh = require "sh"
local LUAX = {}
local I = F.I(LUAX)
LUAX.AUTHORS = "Christophe Delord"
LUAX.URL = "github.com/cdsoft/luax"
LUAX.VERSION = sh "git describe --tags" : trim()
LUAX.DATE = sh "git show -s --format=%cd --date=format:'%Y-%m-%d'" : trim()
LUAX.COPYRIGHT = I"LuaX $(VERSION) Copyright (C) 2021-$(DATE:split'%-':head()) $(URL), $(AUTHORS)"
local BUILD_CONFIG = {
ZIG_VERSION = "0.13.0",
ZIG_PATH = "~/.local/opt/zig",
ZIG_PATH_WIN = "~\\zig",
}
help.name "LuaX"
help.description(I[[
$(COPYRIGHT)
luax is a Lua interpreter and REPL based on Lua 5.4
augmented with some useful packages.
luax can also produce standalone scripts from Lua scripts.
luax runs on several platforms with no dependency:
- Linux (x86_64, aarch64)
- MacOS (x86_64, aarch64)
- Windows (x86_64)
luax can "compile" scripts executable on all supported platforms
(LuaX must be installed on the target environment).
]])
section [[
WARNING: This file has been generated by bang. DO NOT MODIFY IT.
If you need to update the build system, please modify build.lua
and run bang to regenerate build.ninja.
]]
local gitdir = (function()
local dir = ".git"
if fs.is_file(dir) then
return fs.read(dir) : match "gitdir:%s*(%S*)"
end
return dir
end)()
generator {
implicit_in = gitdir/"refs/tags",
}
-- list of targets used for cross compilation (with Zig only)
local targets = dofile "libluax/sys/targets.lua"
local sys = dofile "libluax/sys/sys.lua"
local usage = I{
title = function(s) return F.unlines {s, ("="):rep(#s)}:rtrim() end,
list = function(t) return t:map(F.prefix" - "):unlines():rtrim() end,
targets = targets:map(F.partial(F.nth, "name")),
}[[
$(title "LuaX bang file usage")
The LuaX bang file can be given options to customize the LuaX compilation.
Without any options, LuaX:
- is compiled with gcc
- is optimized for size
- is not a cross-compiler
- does not use optional modules (lz4, luasocket, luasec)
- does not use OpenSSL
$(title "Compiler")
bang -- gcc Compile LuaX with gcc (default)
bang -- clang Compile LuaX with clang
bang -- zig Compile LuaX with Zig
$(title "Compilation mode")
bang -- fast Code optimized for speed
bang -- small Code optimized for size (default)
bang -- debug Compiled with debug informations and no optimization
bang -- san Compiled with ASan and UBSan (implies clang)
bang -- lax Disable strict compilation options
bang -- strip Remove debug information from precompiled bytecode
bang -- lto Enable LTO optimizations
bang -- nolto Disable LTO optimizations (default)
$(title "Optional features")
bang -- lz4 Add LZ4 support
bang -- nolz4 No LZ4 support (default)
bang -- socket Add socket support via luasocket
bang -- nosocket No socket support via luasocket (default)
bang -- ssl Add SSL support via LuaSec and OpenSSL
bang -- nossl No SSL support via LuaSec and OpenSSL (default)
bang -- cross Generate cross-compilers (implies compilation with zig)
bang -- nocross Do not generate cross-compilers (default)
Zig is downloaded by the ninja file.
gcc and clang must be already installed.
Supported targets:
$(list(targets))
]]
if F.elem("help", arg) then
print(usage)
os.exit(0)
end
local mode = "small" -- fast, small, debug
local compiler = "gcc" -- zig, gcc, clang
local san = false
local strict = true -- strict compilation options and checks
local use_lto = false
local lz4 = false
local socket = false
local ssl = false
local release = false
local cross = false
local bytecode = "-b"
F.foreach(arg, function(a)
local function set_mode() mode = a end
local function set_compiler() compiler = a end
case(a) {
fast = set_mode,
small = set_mode,
debug = set_mode,
zig = set_compiler,
gcc = set_compiler,
clang = set_compiler,
san = function() san = true end,
lax = function() strict = false end,
strip = function() bytecode = "-s" end,
lto = function() use_lto = true end,
nolto = function() use_lto = false end,
lz4 = function() lz4 = true end,
nolz4 = function() lz4 = false end,
socket = function() socket = true end,
nosocket = function() socket = false end,
ssl = function() ssl = true end,
nossl = function() ssl = false end,
release = function() release = true end,
cross = function() cross = true end,
nocross = function() cross = false end,
[Nil] = function()
F.error_without_stack_trace(a..": unknown parameter\n\n"..usage, 1)
end,
} ()
end)
if san then compiler = "clang" end
if release or cross then compiler = "zig"; san = false end
if ssl then socket = true end
local host_targets = F{targets:find(function(t) return t.os==sys.os and t.arch==sys.arch end)}
assert(#host_targets == 1)
local targets_to_compile = (release or cross) and targets or host_targets
BUILD_CONFIG.COMPILER_NAME = compiler
BUILD_CONFIG.COMPILER_FULL_VERSION = F.case(compiler) {
zig = function() return compiler.." version "..BUILD_CONFIG.ZIG_VERSION end,
gcc = function() return assert(sh "gcc --version"):lines():head() end,
clang = function() return assert(sh "clang --version"):lines():head() end,
}()
BUILD_CONFIG.COMPILER_VERSION = F.case(compiler) {
zig = function() return BUILD_CONFIG.ZIG_VERSION end,
gcc = function() return BUILD_CONFIG.COMPILER_FULL_VERSION:find "[%d.]+" end,
clang = function() return BUILD_CONFIG.COMPILER_FULL_VERSION:find "[%d.]+" end,
}()
BUILD_CONFIG.MODE = mode
BUILD_CONFIG.LTO = use_lto
BUILD_CONFIG.SOCKET = socket
BUILD_CONFIG.SSL = ssl
section("Compilation options")
comment(("Compilation mode : %s"):format(F{mode, use_lto and "+ LTO" or {}}:flatten():unwords()))
comment(("Compiler : %s"):format(compiler))
comment(("Sanitizers : %s"):format(san and "ASan and UBSan" or "none"))
comment(("Compilation checks: %s"):format(strict and "strict" or "lax"))
comment(("Lua code : %s"):format(case(bytecode) { ["-b"] = "bytecode",
["-s"] = "stripped bytecode",
[Nil] = "source code" }))
comment(("Compression : %s"):format(F.flatten{"Lzip", lz4 and "LZ4" or {}} : str " + "))
comment(("Socket support : %s"):format(F.flatten{socket and "LuaSocket" or "none", ssl and {"LuaSec", "OpenSSL"} or {}} : str " + "))
local function is_dynamic(target) return target.libc~="musl" and not san end
local function has_partial_ld(target)
if compiler=="clang" and use_lto then return false end
return target.os=="linux" or target.os=="macos"
end
local function optional(cond)
return cond and F.id or F.const{}
end
local function zig_target(t)
return {"-target", F{t.arch, t.os, t.libc}:str"-"}
end
--===================================================================
section "Build environment"
---------------------------------------------------------------------
var "builddir" ".build"
var "bin" "$builddir/bin"
var "lib" "$builddir/lib"
var "doc" "$builddir/doc"
var "tmp" "$builddir/tmp"
var "test" "$builddir/test"
var "dist" "$builddir/dist"
local compile = {}
local test = {}
local doc = {}
local promotion = {}
local compile_flags = file "compile_flags.txt"
local function once(f)
local cache = {}
return F.curry(function(x, y)
local hash = F.show{x, y}
local val = cache[hash]
if val ~= nil then return val end
val = f(x)(y)
cache[hash] = val
return val
end)
end
local build_once = once(build)
local openssl_libs = targets : map2t(function(target) return target.name, {} end)
local function add_openssl_rules()
--===================================================================
section "OpenSSL"
---------------------------------------------------------------------
if not ssl then return end
var "openssl" "$builddir/openssl"
var "openssl_src" (fs.realpath "ext/opt/openssl")
local openssl_options = {
case(mode) {
fast = "--release",
small = "--release",
debug = "--debug",
},
"--static",
"no-apps",
--"no-asm",
"no-async",
"no-atexit", -- atexit crashes tests with luax shared libraries
"no-docs",
"no-dso",
"no-tests",
"no-threads",
"no-shared",
-- https://github.com/openssl/openssl/discussions/23542
"no-afalgeng",
"no-autoalginit",
"no-autoerrinit",
case(mode) {
fast = {},
small = "no-cached-fetch",
debug = "no-cached-fetch",
},
"no-dgram",
"no-dynamic-engine",
"no-filenames",
"no-fips-securitychecks",
"no-gost",
"no-module",
"no-nextprotoneg",
"no-pinshared",
"no-srtp",
"no-sse2",
"no-ssl-trace",
"no-static-engine",
"no-quic",
"no-ui-console",
"no-uplink",
"no-argon2",
"no-scrypt",
"-DOPENSSL_NO_APPLE_CRYPTO_RANDOM",
case(mode) {
fast = {},
small = "-DOPENSSL_SMALL_FOOTPRINT",
debug = {},
},
}
local nproc = (sh "getconf _NPROCESSORS_ONLN" or "8"):trim()
rule "make_openssl" {
description = "compile OpenSSL for $target",
command = {
"set -e;",
"mkdir -p $openssl/$target;",
"cd $openssl/$target;",
optional(compiler=="zig") {
'export AR="$zig ar";',
'export CC="$zig cc $zig_target";',
'export CXX="$zig c++ $zig_target";',
'export LD="$zig ld $zig_target";',
'export RANLIB="$zig ranlib";',
'export RC="$zig rc";',
},
case(mode) {
fast = 'export CFLAGS="-pipe -O3";',
small = 'export CFLAGS="-pipe -Os";',
debug = 'export CFLAGS="-pipe -Og -g";',
},
"$lto",
"$openssl_src/Configure", "$openssl_target", openssl_options, "$additional_flags", ";",
"make", "-j", nproc,
},
implicit_in = {
build "$openssl_src/Configure" {
description = "download OpenSSL",
command = "git submodule sync && git submodule update --init --recursive",
pool = "console",
},
},
pool = "console",
}
targets_to_compile:foreach(function(target)
local lto_opt = case(compiler) {
zig = 'export CFLAGS="$$CFLAGS -flto=thin";',
gcc = 'export CFLAGS="$$CFLAGS -flto=auto";',
clang = 'export CFLAGS="$$CFLAGS -flto=thin";',
}
openssl_libs[target.name] = build { "$openssl"/target.name/"libssl.a", "$openssl"/target.name/"libcrypto.a" } { "make_openssl",
target = target.name,
zig_target = zig_target(target),
openssl_target = case(target.name) {
["linux-x86_64"] = F.const"linux-x86_64",
["linux-x86_64-musl"] = F.const"linux-x86_64",
["linux-aarch64"] = F.const"linux-aarch64",
["linux-aarch64-musl"] = F.const"linux-aarch64",
["macos-x86_64"] = F.const"darwin64-x86_64",
["macos-aarch64"] = F.const"darwin64-arm64",
["windows-x86_64"] = F.const"mingw64",
[Nil] = function() error(target.name..": unsupported OpenSSL target") end,
} (),
lto = optional(use_lto) {
case(target.os) {
linux = lto_opt,
macos = {},
windows = lto_opt,
},
},
additional_flags = case(target.os) {
linux = {
"no-asm",
},
macos = {
"no-asm",
},
windows = {},
},
}
end)
end -- add_openssl_rules
--===================================================================
section "Compiler"
---------------------------------------------------------------------
local compiler_deps = {}
case(compiler) {
zig = function()
var "zig_version" (BUILD_CONFIG.ZIG_VERSION)
local zig_path = case(sys.os) {
windows = BUILD_CONFIG.ZIG_PATH_WIN:gsub("^~", os.getenv"LOCALAPPDATA" or "~"),
[Nil] = BUILD_CONFIG.ZIG_PATH:gsub("^~", os.getenv"HOME" or "~"),
}
var "zig" { zig_path/"$zig_version"/"zig" }
build "$zig" {
description = "install zig $zig_version",
command = "tools/install_zig.sh $zig_version $out",
pool = "console",
}
compiler_deps = { "$zig" }
local function zig_rules(target)
local target_opt = zig_target(target)
var("cc-"..target.name) { "$zig cc", target_opt }
var("ar-"..target.name) { "$zig ar" }
var("ld-"..target.name) { "$zig cc", target_opt }
end
targets_to_compile:foreach(zig_rules)
end,
gcc = function()
var("cc-"..sys.name) "gcc"
var("ar-"..sys.name) "ar"
var("ld-"..sys.name) "gcc"
end,
clang = function()
var("cc-"..sys.name) "clang"
var("ar-"..sys.name) "ar"
var("ld-"..sys.name) "clang"
end,
}()
local include_path = F.flatten {
".",
"$tmp",
"lua",
"ext/c/lzlib/lib",
"ext/c/lzlib/lib/inc",
"libluax",
optional(lz4) "ext/opt/lz4/lib",
}
local lto_opt = optional(use_lto) {
case(compiler) {
zig = "-flto=thin",
gcc = "-flto=auto",
clang = "-flto=thin",
}
}
local sanitizer_cflags = optional(san) {
"-g", "-fno-omit-frame-pointer", "-fno-optimize-sibling-calls",
"-fsanitize=address",
"-fsanitize=undefined",
"-fsanitize=float-divide-by-zero",
--"-fsanitize=unsigned-integer-overflow",
"-fsanitize=implicit-conversion",
"-fsanitize=local-bounds",
"-fsanitize=float-cast-overflow",
"-fsanitize=nullability-arg",
"-fsanitize=nullability-assign",
"-fsanitize=nullability-return",
}
local sanitizer_ext_cflags = optional(san) {
"-g", "-fno-omit-frame-pointer", "-fno-optimize-sibling-calls",
"-fsanitize=address",
--"-fsanitize=undefined",
--"-fsanitize=float-divide-by-zero",
--"-fsanitize=unsigned-integer-overflow",
--"-fsanitize=implicit-conversion",
"-fsanitize=local-bounds",
"-fsanitize=float-cast-overflow",
"-fsanitize=nullability-arg",
"-fsanitize=nullability-assign",
"-fsanitize=nullability-return",
}
local sanitizer_ldflags = optional(san) {
"-fsanitize=address",
"-fsanitize=undefined",
}
local sanitizer_options = optional(san) {
"export ASAN_OPTIONS=check_initialization_order=1:detect_stack_use_after_return=1:detect_leaks=1;",
"export UBSAN_OPTIONS=print_stacktrace=1;",
}
local host_cflags = {
"-O2",
"-pipe",
"-fPIC",
include_path:map(F.prefix"-I"),
case(sys.os) {
linux = "-DLUA_USE_LINUX",
macos = "-DLUA_USE_MACOSX",
windows = {},
},
}
local host_ldflags = {
"-pipe",
optional(is_dynamic(sys)) "-rdynamic",
"-s",
"-lm",
}
local cflags = {
"-std=gnu2x",
case(mode) {
fast = "-O3",
small = "-Os",
debug = { "-g", "-Og" },
},
"-pipe",
"-fPIC",
include_path:map(F.prefix"-I"),
optional(lz4) "-DLUAX_USE_LZ4",
optional(socket) "-DLUAX_USE_SOCKET",
optional(ssl) "-DLUAX_USE_SSL",
}
local luax_cflags = F{
cflags,
optional(strict) {
"-Werror",
"-Wall",
"-Wextra",
"-pedantic",
"-Wstrict-prototypes",
"-Wmissing-field-initializers",
"-Wmissing-prototypes",
"-Wmissing-declarations",
"-Werror=switch-enum",
"-Werror=implicit-fallthrough",
"-Werror=missing-prototypes",
case(compiler) {
zig = {
"-Weverything",
"-Wno-padded",
"-Wno-reserved-identifier",
"-Wno-disabled-macro-expansion",
"-Wno-used-but-marked-unused",
"-Wno-documentation-unknown-command",
"-Wno-declaration-after-statement",
"-Wno-unsafe-buffer-usage",
"-Wno-pre-c2x-compat",
},
gcc = {},
clang = {
"-Weverything",
"-Wno-padded",
"-Wno-reserved-identifier",
"-Wno-disabled-macro-expansion",
"-Wno-used-but-marked-unused",
"-Wno-documentation-unknown-command",
"-Wno-declaration-after-statement",
"-Wno-unsafe-buffer-usage",
"-Wno-pre-c2x-compat",
},
},
},
sanitizer_cflags,
}
local ext_cflags = {
cflags,
case(compiler) {
zig = {
"-Wno-constant-logical-operand",
},
gcc = {},
clang = {
"-Wno-constant-logical-operand",
},
},
sanitizer_ext_cflags,
}
compile_flags {
vars.expand(luax_cflags) : flatten() : unlines(),
}
local ldflags = {
"-pipe",
case(mode) {
fast = "-s",
small = "-s",
debug = {},
},
"-lm",
case(compiler) {
zig = {
"-Wno-single-bit-bitfield-constant-conversion",
},
gcc = {
"-Wstringop-overflow=0",
},
clang = {},
},
sanitizer_ldflags,
}
add_openssl_rules()
local cc = {}
local cc_ext = {}
local ar = {}
local ld = {}
local so = {}
local partial_ld = {}
cc.host = rule "cc-host" {
description = "cc $in",
command = { "$cc-"..sys.name, "-c", host_cflags, "-MD -MF $depfile $in -o $out" },
implicit_in = compiler_deps,
depfile = "$out.d",
}
ld.host = rule "ld-host" {
description = "ld $out",
command = { "$ld-"..sys.name, "$in -o $out", host_ldflags },
implicit_in = compiler_deps,
}
targets_to_compile:foreach(function(target)
local lto = case(target.os) {
linux = lto_opt,
macos = {},
windows = lto_opt,
}
local target_flags = {
"-DLUAX_ARCH='\""..target.arch.."\"'",
"-DLUAX_OS='\""..target.os.."\"'",
"-DLUAX_LIBC='\""..target.libc.."\"'",
"-DLUAX_EXE='\""..target.exe.."\"'",
"-DLUAX_SO='\""..target.so.."\"'",
"-DLUAX_NAME='\""..target.name.."\"'",
}
local lua_flags = {
case(target.os) {
linux = "-DLUA_USE_LINUX",
macos = "-DLUA_USE_MACOSX",
windows = {},
},
optional(san) {
"-DLUAI_ASSERT",
"-DLUA_USE_APICHECK",
},
}
local opt_include_path = F.flatten {
optional(lz4) "ext/opt/lz4/lib",
optional(ssl) {
"$openssl"/target.name/"include",
"ext/opt/openssl/include",
},
}
local opt_flags = {
F.map(F.prefix"-I", opt_include_path),
}
if target.name == sys.name then
compile_flags {
F{target_flags, lua_flags}
: flatten()
: map(function(s) return s:gsub("'", "") end)
: unlines()
}
end
local target_ld_flags = {
optional(is_dynamic(target)) "-rdynamic",
case(target.os) {
linux = {},
macos = {},
windows = {
"-lshlwapi",
optional(socket) "-lws2_32",
optional(ssl) "-lcrypt32",
},
},
}
local target_so_flags = {
"-shared",
}
local target_opt = case(compiler) {
zig = zig_target(target),
gcc = {},
clang = {},
}
cc[target.name] = rule("cc-"..target.name) {
description = "cc $in",
command = {
"$cc-"..target.name, target_opt, "-c", lto, luax_cflags, lua_flags, target_flags, opt_flags, "$additional_flags", "-MD -MF $depfile $in -o $out",
case(target.os) {
linux = {},
macos = {},
windows = "$build_as_dll",
},
},
implicit_in = {
compiler_deps,
},
depfile = "$out.d",
}
cc_ext[target.name] = rule("cc_ext-"..target.name) {
description = "cc $in",
command = {
"$cc-"..target.name, target_opt, "-c", lto, ext_cflags, lua_flags, opt_flags, "$additional_flags", "-MD -MF $depfile $in -o $out",
},
implicit_in = {
compiler_deps,
},
depfile = "$out.d",
}
ld[target.name] = rule("ld-"..target.name) {
description = "ld $out",
command = {
"$ld-"..target.name, target_opt, lto, "$in -o $out", ldflags, target_ld_flags,
},
implicit_in = compiler_deps,
}
so[target.name] = is_dynamic(target) and rule("so-"..target.name) {
description = "so $out",
command = {
"$cc-"..target.name, target_opt, lto, ldflags, target_ld_flags, target_so_flags, "$in -o $out",
},
implicit_in = compiler_deps,
}
partial_ld[target.name] = has_partial_ld(target) and rule("partial-ld-"..target.name) {
description = "ld $out",
command = {
"$ld-"..target.name, target_opt, "-r", "$in -o $out",
},
implicit_in = compiler_deps,
}
ar[target.name] = rule("ar-"..target.name) {
description = "ar $out",
command = {
"$ar-"..target.name, "-crs $out $in",
},
implicit_in = compiler_deps,
}
end)
--===================================================================
section "Third-party modules update"
---------------------------------------------------------------------
build "update_modules" {
description = "update third-party modules",
command = "tools/update-third-party-modules.sh $builddir/update",
pool = "console",
}
--===================================================================
section "LuaX sources"
---------------------------------------------------------------------
local linux_only = F.flatten {
"ext/c/linenoise/linenoise.c",
"ext/c/linenoise/utf8.c",
optional(socket) {
"ext/opt/luasocket/serial.c",
"ext/opt/luasocket/unixdgram.c",
"ext/opt/luasocket/unixstream.c",
"ext/opt/luasocket/usocket.c",
"ext/opt/luasocket/unix.c",
},
}
local windows_only = F.flatten {
optional(socket) {
"ext/opt/luasocket/wsocket.c",
},
}
local ignored_sources = {
"ext/c/lqmath/src/imath.c",
}
local sources = F{
lua_c_files = ls "lua/*.c"
: filter(function(name) return F.not_elem(name:basename(), {"lua.c", "luac.c"}) end),
lua_main_c_files = F{ "lua/lua.c" },
luax_main_c_files = F{ "luax/luax.c" },
libluax_main_c_files = F{ "luax/libluax.c" },
luax_c_files = ls "libluax/**.c"
: difference(lz4 and {} or ls "libluax/lz4/**.c")
: difference(socket and {} or ls "libluax/socket/**.c")
: difference(ssl and {} or ls "libluax/sec/**.c"),
third_party_c_files = F.flatten {
ls "ext/c/**.c"
: filter(function(name) return not name:match "lzlib/lib/inc" end)
: filter(function(name) return not name:match "lzlib/programs" end),
optional(lz4) { ls "ext/opt/lz4/lib/*.c" },
optional(socket) { ls "ext/opt/luasocket/*.c" },
optional(ssl) { ls "ext/opt/luasec/**.c" },
}
: difference(linux_only)
: difference(windows_only)
: difference(ignored_sources),
linux_third_party_c_files = linux_only,
windows_third_party_c_files = windows_only,
}
--===================================================================
section "Native Lua interpreter"
---------------------------------------------------------------------
var "lua" "$bin/lua"
var "lua_path" (
F{
"$tmp",
ls "libluax/*" : filter(fs.is_dir),
"ext/lua/argparse",
"ext/lua/cbor",
"luax",
}
: flatten()
: map(function(path) return path / "?.lua" end)
: str ";"
)
build "$lua" { ld.host,
(sources.lua_c_files .. sources.lua_main_c_files) : map(function(src)
return build("$tmp/obj/lua"/src:chext".o") { cc.host, src }
end),
}
--===================================================================
section "lzip cli"
---------------------------------------------------------------------
var "lzip" "$bin/lzip"
build "$lzip" { ld.host,
( ls "ext/c/lzlib/programs/*.c" .. ls "ext/c/lzlib/lib/*.c" )
: map(function(src)
return build("$tmp/obj/lzip"/src:chext".o") { cc.host, src }
end),
}
--===================================================================
section "LuaX configuration"
---------------------------------------------------------------------
comment [[
The LuaX configuration files (luax_config.h and luax_config.lua)
are created in `$tmp`
]]
var "luax_config_h" "$tmp/luax_config.h"
var "luax_config_lua" "$tmp/luax_config.lua"
rule "ypp-config" {
description = "ypp $out",
command = { "$lua tools/luax.lua tools/ypp.luax", build.ypp_vars(LUAX), "$in -o $out" },
implicit_in = {
"$lua",
"tools/luax.lua",
"tools/ypp.luax",
},
}
build "$luax_config_h" { "ypp-config", "libluax/luax_config.h.in" }
build "$luax_config_lua" { "ypp-config", "libluax/luax_config.lua.in" }
comment [[
The LuaX build configuration file (luax_build_config.lua)
is created in `$tmp`
]]
var "luax_build_config_lua" "$tmp/luax_build_config.lua"
rule "ypp-build-config" {
description = "ypp $out",
command = { "$lua tools/luax.lua tools/ypp.luax", build.ypp_vars(BUILD_CONFIG), "$in -o $out" },
implicit_in = {
"$lua",
"tools/luax.lua",
"tools/ypp.luax",
},
}
build "$luax_build_config_lua" { "ypp-build-config", "luax/luax_build_config.lua.in" }
--===================================================================
section "Lua runtime"
---------------------------------------------------------------------
rule "bundle" {
description = "bundle $out",
command = {
"PATH=$bin:$$PATH",
"LUA_PATH=\"$lua_path\"",
"LUAX_LIB=$lib",
"$lua tools/bundle.lua $args $in -o $out",
},
implicit_in = {
"$lua",
"$lzip",
"tools/bundle.lua",
"luax/luax_bundle.lua",
"$luax_config_lua",
"$luax_build_config_lua",
},
}
local luax_runtime, lua_runtime = {}, {}
local function rt(t)
acc(luax_runtime) { t.luax }
acc(lua_runtime) { t.lua }
end
rt { luax="libluax/F/F.lua", lua="libluax/F/F.lua" }
rt { lua="libluax/complex/complex.lua" }
rt { luax="libluax/crypt/crypt.lua", lua={"libluax/crypt/crypt.lua", "libluax/crypt/_crypt.lua"} }
rt { luax="libluax/fs/fs.lua", lua={"libluax/fs/fs.lua", "libluax/fs/_fs.lua"} }
rt { lua="libluax/imath/imath.lua" }
rt { luax="libluax/import/import.lua", lua="libluax/import/import.lua" }
rt { lua="libluax/linenoise/linenoise.lua" }
rt { luax="libluax/lar/lar.lua", lua="libluax/lar/lar.lua" }
rt { luax="libluax/lzip/lzip.lua", lua={"libluax/lzip/lzip.lua", "libluax/lzip/_lzip.lua"} }
rt { lua="libluax/mathx/mathx.lua" }
rt { lua="libluax/ps/ps.lua" }
rt { luax="libluax/qmath/qmath.lua", lua={"libluax/qmath/qmath.lua", "libluax/qmath/_qmath.lua"} }
rt { luax="libluax/sh/sh.lua", lua="libluax/sh/sh.lua" }
rt { luax="libluax/sys/targets.lua", lua={"libluax/sys/sys.lua", "libluax/sys/targets.lua"} }
rt { luax="libluax/tar/tar.lua", lua="libluax/tar/tar.lua" }
rt { luax="libluax/term/term.lua", lua={"libluax/term/term.lua", "libluax/term/_term.lua"} }
rt { luax="libluax/package/package_hook.lua", lua="libluax/package/package_hook.lua" }
rt { luax="libluax/debug/debug_hook.lua", lua="libluax/debug/debug_hook.lua" }
rt { luax={ls "ext/c/**.lua", ls "ext/lua/**.lua"}, lua={ls "ext/lua/**.lua"} }
if lz4 then
rt { luax="libluax/lz4/lz4.lua", lua={"libluax/lz4/lz4.lua", "libluax/lz4/_lz4.lua"} }
end
if socket then
rt { luax={ls "ext/opt/luasocket/**.lua"}, lua={} }
end
if ssl then
rt { luax={ls "ext/opt/luasec/**.lua"}, lua={} }
end
-- Ensures all Lua scripts are in the runtime
local used_scripts = F.flatten{luax_runtime, lua_runtime} : nub()
local expected_scripts = F.flatten{
ls "libluax/**.lua"
: difference(optional(not lz4)(ls "libluax/lz4/**.lua" )),
ls "ext/c/**.lua",
ls "ext/lua/**.lua",
optional(lz4) { ls "libluax/lz4/**.lua" },
optional(socket) { ls "ext/opt/luasocket/**.lua" },
optional(ssl) { ls "ext/opt/luasec/**.lua" },
} : nub()
local unused_scripts = expected_scripts : difference(used_scripts)
if not unused_scripts:null()
then
error("Some Lua scripts are not in the runtime:\n"..unused_scripts:sort():map(F.prefix" "):unlines())
end
local luax_runtime_bundle = build "$tmp/lua_runtime_bundle.c" {
"bundle", luax_runtime,
args = {
"-e lib",
"-t c",
bytecode,
"-n luax",
},
}