Repository navigation
Expand file tree
/
Copy pathbuild.lua
More file actions
355 lines (320 loc) · 12 KB
/
Copy pathbuild.lua
File metadata and controls
355 lines (320 loc) · 12 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
-- build.lua — native artifacts for lua-sys.
--
-- high: bridge.c -> bridge.<ext>. bridge.c has no undefined lua* symbols
-- on any platform: it resolves them at load time from the LuaJIT
-- that loads it (see bridge_resolve_symbols), so it links with no
-- LuaJIT at all and binds to the right one even inside a library
-- built with `lde compile --shared` whose host has its own Lua.
--
-- bundled: the chosen runtime (luajit/lua51/lua54/luau) is built into
-- bundled.<ext>, a shared library exporting its full C API (module
-- lua-sys.bundled). LuaJIT links the lde-org/luajit prebuilt dist
-- whole-archive; Lua 5.1/5.4 compile the official lua.org sources;
-- Luau compiles the pinned release as C++17. Downloads and builds
-- are cached in the output directory.
--
-- vendored: nothing to build (the host process provides the runtime).
--
-- The output follows build.target, not the host, so cross-compiling (e.g.
-- `lde compile --target windows-x86-64`) produces bridge.dll/bundled.dll.
local build = require("lde-build")
local out = assert(build.outDir, "build.outDir is not set")
local target = build.target or "unknown"
-- ── Target classification ──────────────────────────────────────────────────
local function classifyTarget()
local platform, arch, libc
if target:find("windows", 1, true) or target:find("mingw", 1, true) then
platform = "windows"
elseif target:find("apple", 1, true) or target:find("darwin", 1, true) then
platform = "macos"
elseif target:find("freebsd", 1, true) then
platform = "freebsd"
elseif target:find("android", 1, true) or target:find("linux", 1, true) then
platform = "linux"
end
const machine = target:match("^([^-]+)")
if machine == "x86_64" or machine == "x86-64" or machine == "amd64" then
arch = "x86-64"
elseif machine == "aarch64" or machine == "arm64" then
arch = "aarch64"
end
if target:find("android", 1, true) then
libc = "android"
elseif target:find("musl", 1, true) then
libc = "musl"
elseif target:find("gnu", 1, true) then
libc = "gnu"
end
-- build.target is the host triple unless cross-compiling, so this fallback
-- only covers a driver that did not report one.
if not platform then
local os = jit and jit.os
platform = os == "Windows" and "windows" or os == "OSX" and "macos" or os == "BSD" and "freebsd" or "linux"
end
if not arch then
arch = jit and jit.arch == "arm64" and "aarch64" or "x86-64"
end
if platform == "macos" or platform == "freebsd" then
libc = nil
elseif platform == "windows" and not libc then
libc = "gnu"
elseif platform == "linux" and not libc then
libc = "gnu"
end
return platform, arch, libc
end
local platform, arch, libc = classifyTarget()
local isWindows = platform == "windows"
local isMacos = platform == "macos"
local isElf = platform == "linux" or platform == "freebsd"
local sharedFlag = isMacos and "-dynamiclib" or "-shared"
---@param list string[]
---@param extra string[]
local function addAll(list, extra)
for _, value in ipairs(extra) do
list[#list + 1] = value
end
end
-- ── Feature validation ─────────────────────────────────────────────────────
--
-- One Lua version and at most one source. `lde` is the LuaJIT lde itself
-- links, so it implies (and only accepts) the luajit version. Platform flags
-- are ignored here.
const VERSION_FLAGS = { "luajit", "lua51", "lua54", "luau" }
const SOURCE_FLAGS = { "lde", "vendored", "bundled" }
---@param flags string[]
local function enabledFlags(flags)
local found = {}
for _, flag in ipairs(flags) do
if build:feature(flag) then found[#found + 1] = flag end
end
return found
end
---@param message string
local function fail(message)
error("lua-sys: " .. message, 0)
end
local versions = enabledFlags(VERSION_FLAGS)
local sources = enabledFlags(SOURCE_FLAGS)
if #versions > 1 then
fail(
"more than one Lua version feature is enabled (" .. table.concat(versions, ", ") .. "); enable exactly one of luajit, lua51, lua54, luau"
)
end
if #sources > 1 then
fail(
"more than one runtime source feature is enabled (" .. table.concat(sources, ", ") .. "); enable at most one of lde, vendored, bundled"
)
end
local version = versions[1]
local source = sources[1]
if source == "lde" then
if version ~= nil and version ~= "luajit" then
fail("'lde' uses the LuaJIT that lde links; it cannot be combined " .. "with the '" .. version .. "' version feature")
end
version = version or "luajit" -- lde alone implies luajit
end
if source == "bundled" and version == nil then
fail("'bundled' needs a version feature: one of luajit, lua51, lua54, luau")
end
-- ── Bridge ─────────────────────────────────────────────────────────────────
if build:feature("high") then
local ext, args
if isWindows then
ext = "dll"
args = { "-shared", "-O2", "-I", out, "-o", out .. "/bridge." .. ext, out .. "/bridge.c" }
elseif isMacos then
ext = "dylib"
-- macOS dyld dedupes dylibs by install name (LC_ID_DYLIB): if a copy of
-- this dylib is already loaded (e.g. the one embedded in the lde binary,
-- whose install name defaults to its build path), dlopen'ing a fresh
-- build at that same path returns the stale image — e.g. bridge.new_state
-- coming back nil on macOS. A content-derived install name means identical
-- copies share a name (harmless) but different versions never shadow each
-- other. The require path is unchanged.
local hash = 5381
do
local f = io.open(out .. "/bridge.c", "rb")
if f then
local data = f:read("*a")
f:close()
for i = 1, #data do
hash = bit.band(hash * 33 + string.byte(data, i), 0xFFFFFFFF)
end
end
end
args = {
"-dynamiclib",
"-undefined",
"dynamic_lookup",
"-O2",
"-install_name",
"@rpath/bridge-" .. bit.tohex(hash, 8) .. ".dylib",
"-I",
out,
"-o",
out .. "/bridge." .. ext,
out .. "/bridge.c"
}
else
ext = "so"
args = { "-shared", "-fPIC", "-O2", "-I", out, "-o", out .. "/bridge." .. ext, out .. "/bridge.c" }
-- dlopen/dladdr1 live in libdl before glibc 2.34; FreeBSD has them in libc.
if platform == "linux" then
args[#args + 1] = "-ldl"
end
end
build:cc(args)
end
-- ── Bundled runtime ────────────────────────────────────────────────────────
if source == "bundled" then
const ext = isWindows and "dll" or (isMacos and "dylib" or "so")
const output = "bundled." .. ext
const stampFile = "bundled.stamp"
const stampValue = version .. "\n" .. target .. "\n"
const cache = "bundled-cache"
local isFresh = build:exists(output) and build:exists(stampFile) and build:read(stampFile) == stampValue
if not isFresh then
const tmp = "bundled.tmp." .. ext
const tmpPath = out .. "/" .. tmp
---@param cacheRel string # archive path relative to the output dir
---@param url string
---@param root string # directory the archive extracts to, below cache
---@param marker string # file that proves the extraction is complete
local function ensureExtracted(cacheRel, url, root, marker)
if build:exists(root .. "/" .. marker) then return end
if not build:exists(cacheRel) then
build:write(cacheRel, build:fetch(url))
end
build:extract(cacheRel, cache)
if not build:exists(root .. "/" .. marker) then
fail("could not extract " .. cacheRel)
end
end
if version == "luajit" then
-- Same dist naming as packages/sea/src/target.lua.
const distName = "libluajit-" .. platform .. "-" .. arch .. (libc and ("-" .. libc) or "")
const root = cache .. "/" .. distName
ensureExtracted(
cache .. "/" .. distName .. ".tar.gz",
"https://github.com/lde-org/luajit/releases/download/latest/" .. distName .. ".tar.gz",
root,
"lib/libluajit.a"
)
local lib = out .. "/" .. root .. "/lib/libluajit.a"
local args
if isMacos then
-- ld64 spells whole-archive "-all_load".
args = { sharedFlag, "-O2", "-Wl,-all_load", lib }
else
args = { sharedFlag, "-O2", "-Wl,--whole-archive", lib, "-Wl,--no-whole-archive" }
end
if isElf then
addAll(args, { "-Wl,-Bsymbolic", "-lm", "-ldl" })
elseif isWindows then
addAll(args, { "-Wl,--export-all-symbols" })
end
addAll(args, { "-o", tmpPath })
build:cc(args)
elseif version == "lua51" or version == "lua54" then
-- Pinned official sources. lua54 tracks the latest 5.4 release.
const releases = {
lua51 = { root = "lua-5.1.5", url = "https://www.lua.org/ftp/lua-5.1.5.tar.gz" },
lua54 = { root = "lua-5.4.8", url = "https://www.lua.org/ftp/lua-5.4.8.tar.gz" }
}
const release = releases[version]
const root = cache .. "/" .. release.root
ensureExtracted(cache .. "/" .. release.root .. ".tar.gz", release.url, root, "src/lua.h")
-- Every library translation unit; lua.c/luac.c are the command-line
-- programs and onelua.c is the generated single-file amalgamation.
local sources = {}
---@type string[]
local cSources = build:scan(root .. "/src", "*.c")
for _, rel in ipairs(cSources) do
const name = rel:match("([^/\\]+)$")
if name ~= "lua.c" and name ~= "luac.c" and name ~= "onelua.c" then
sources[#sources + 1] = out .. "/" .. rel
end
end
table.sort(sources)
local args = { sharedFlag }
if not isWindows then addAll(args, { "-fPIC" }) end
addAll(args, { "-O2" })
if isWindows then
addAll(args, { "-DLUA_BUILD_AS_DLL", "-DLUA_CORE" })
elseif isMacos then
addAll(args, { "-DLUA_USE_MACOSX" })
elseif version == "lua51" and platform == "linux" then
addAll(args, { "-DLUA_USE_LINUX" })
else
addAll(args, { "-DLUA_USE_POSIX", "-DLUA_USE_DLOPEN" })
end
if isElf then addAll(args, { "-Wl,-Bsymbolic" }) end
addAll(args, sources)
addAll(args, { "-o", tmpPath })
if isElf then addAll(args, { "-lm", "-ldl" }) end
build:cc(args)
elseif version == "luau" then
-- Same tag as tests/fixtures/luau/build.sh. CMake's LUAU_EXTERN_C=ON
-- is spelled out here as the two API macros plus LUA_USE_LONGJMP.
const tag = "0.748"
const root = cache .. "/luau-" .. tag
ensureExtracted(
cache .. "/luau-" .. tag .. ".tar.gz",
"https://github.com/luau-lang/luau/archive/refs/tags/" .. tag .. ".tar.gz",
root,
"VM/include/lua.h"
)
local sources = {}
for _, dir in ipairs({ "VM/src", "Compiler/src", "Ast/src", "Common/src", "Bytecode/src" }) do
---@type string[]
local found = build:scan(root .. "/" .. dir, "*.cpp")
for _, rel in ipairs(found) do
sources[#sources + 1] = out .. "/" .. rel
end
end
table.sort(sources)
local args = { sharedFlag }
if not isWindows then addAll(args, { "-fPIC" }) end
addAll(args, {
"-x",
"c++",
"-std=c++17",
"-O2",
"-fno-math-errno",
"-DLUA_USE_LONGJMP=1",
"-DLUA_API=extern \"C\"",
"-DLUACODE_API=extern \"C\"",
"-I",
out .. "/" .. root .. "/VM/include",
"-I",
out .. "/" .. root .. "/Compiler/include",
"-I",
out .. "/" .. root .. "/Ast/include",
"-I",
out .. "/" .. root .. "/Common/include",
"-I",
out .. "/" .. root .. "/Bytecode/include",
"-I",
out .. "/" .. root .. "/VM/src"
})
if isElf then addAll(args, { "-Wl,-Bsymbolic" }) end
addAll(args, sources)
addAll(args, { "-x", "none" })
if isWindows then
addAll(args, { "-lstdc++", "-static-libstdc++", "-static-libgcc", "-Wl,--export-all-symbols" })
elseif platform == "linux" then
addAll(args, { "-lstdc++" })
else
addAll(args, { "-lc++" })
end
addAll(args, { "-o", tmpPath })
build:cc(args)
else
fail("unsupported bundled version '" .. tostring(version) .. "'")
end
if build:exists(output) then build:delete(output) end
build:move(tmp, output)
build:write(stampFile, stampValue)
end
end