-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminilde.lua
More file actions
298 lines (258 loc) · 9.06 KB
/
Copy pathminilde.lua
File metadata and controls
298 lines (258 loc) · 9.06 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
-- This file implements a 'mini' lde for the sake of bootstrapping an initial lde binary for a platform without requiring lde.
-- Dependencies:
-- - All: curl, tar
-- - Windows: Developer Mode or Administrator
local separator = package.config:sub(1, 1)
local function join(...)
return table.concat({ ... }, separator)
end
local isWindows = separator == '\\'
-- On Windows, os.execute uses cmd.exe which doesn't understand Unix commands.
-- Use _spawnlp to invoke bash directly, bypassing cmd.exe entirely.
-- _P_WAIT = 2 (suspends caller until child exits, returns child exit code).
local function sh(cmd)
if isWindows then
local ffi = require("ffi")
pcall(ffi.cdef, [[int _spawnlp(int mode, const char *cmdname, const char *arg0, ...);]])
-- bash uses forward slashes; convert backslashes in the command string
cmd = cmd:gsub("\\", "/")
local _P_WAIT = 2
return ffi.C._spawnlp(_P_WAIT, "bash", "bash", "-c", cmd, ffi.cast("char *", nil))
end
return os.execute(cmd)
end
---@param path string
local function exists(path)
local ok, _, code = os.rename(path, path)
if not ok then
return code == 13 -- permission denied means it exists
end
return true
end
---@param dir string
local function mkdir(dir)
if exists(dir) then return end
sh('mkdir -p "' .. dir .. '"')
end
---@type fun(src: string, dest: string)
local function mklink(src, dest)
if exists(dest) then return end
sh("ln -sf '" .. src .. "' '" .. dest .. "'")
end
---@type fun(handle: file*?): string?
local function readhandle(handle)
if not handle then return end
local content = handle:read("*a")
handle:close()
return content
end
---@type fun(path: string): string?
local function read(path)
return readhandle(io.open(path, "rb"))
end
---@type fun(path: string, content: string)
local function write(path, content)
local file = io.open(path, "wb")
if not file then return end
file:write(content)
file:close()
end
---@type fun(path: string)
local function rm(path)
sh('rm -rf "' .. path .. '"')
end
---@type fun(src: string, dest: string) # Recursive copy
local function copy(src, dest)
if not exists(src) then return end
sh('cp -rL "' .. src .. '" "' .. dest .. '"')
end
---@type fun(b: string): any? # Tiny json decoder with very basic support for what we will use in lde.json files
local function jsonDecode(b)
local c = 0; local function d(e)
local f, g, m = b:find("^%s*", c)
if f then c = g + 1 end; f, g, m = b:find(e, c)
if f then
c = g + 1; return m or true
end
end; local h, i; local function j()
local n = d("^(%d+%.?%d*)"); if n then return tonumber(n) end
return d("^\"([^\"]*)\"") or h() or i() or d("^true") or (d("^false") and false)
end; function h()
if not d("^{") then return end; local k = {}
while not d("^}") do
local l = d("^\"([^\"]*)\"")
d("^:"); k[l] = j(); d("^,")
end; return k
end; function i()
if not d("^%[") then return end; local k = {}
while not d("^%]") do
k[#k + 1] = j()
d("^,")
end; return k
end; return h()
end
local args = { ... }
local function pop() return table.remove(args, 1) end
---@alias minilde.dep
--- | { path: string }
--- | { git: string }
local tmpBase = os.getenv("TEMP") or os.getenv("TMPDIR") or "/tmp"
local tmpLDEDir = join(tmpBase, "lde")
local ffi = require("ffi")
local setenv ---@type fun(name: string, value: string)
local chdir ---@type fun(dir: string)
local getcwd ---@type fun(): string
ffi.cdef [[void _exit(int status);]]
if isWindows then
ffi.cdef [[int _putenv_s(const char *name, const char *value);]]
setenv = function(name, value) ffi.C._putenv_s(name, value) end
ffi.cdef [[int _chdir(const char *dirname);]]
chdir = function(dir) ffi.C._chdir(dir) end
ffi.cdef [[int _getcwd(char *buffer, size_t size);]]
getcwd = function()
local buffer = ffi.new("char[?]", 1024)
ffi.C._getcwd(buffer, 1024)
return ffi.string(buffer)
end
else
ffi.cdef [[int setenv(const char *name, const char *value, int overwrite);]]
setenv = function(name, value) ffi.C.setenv(name, value, 1) end
ffi.cdef [[int chdir(const char *path);]]
chdir = function(dir) ffi.C.chdir(dir) end
ffi.cdef [[char *getcwd(char *buf, size_t size);]]
getcwd = function()
local buffer = ffi.new("char[?]", 1024)
ffi.C.getcwd(buffer, 1024)
return ffi.string(buffer)
end
end
---@param packagePath string
---@param targetDir string
local function buildPackage(packagePath, targetDir)
local config = jsonDecode(assert(read(join(packagePath, "lde.json")) or read(join(packagePath, "lpm.json")),
"No lde.json at " .. packagePath)) --[[@as { name: string, dependencies: { [string]: minilde.dep } }]]
mkdir(targetDir)
if exists(join(packagePath, "build.lua")) then
local outputDir = join(targetDir, config.name)
copy(join(packagePath, "src"), outputDir)
setenv("LDE_OUTPUT_DIR", outputDir)
setenv("LPM_OUTPUT_DIR", outputDir)
---@alias minilde.build { outDir: string }
---@class minilde.build
local build = {}
build.__index = build
---@format disable-next
do
function build:fetch(url)
local tmp = join(tmpLDEDir, "fetch-" .. tostring(os.time()))
sh('curl -fsSL "' .. url .. '" -o "' .. tmp .. '"')
local content = assert(read(tmp), "failed to read fetched file from " .. tmp)
sh('rm -f "' .. tmp .. '"')
return content
end
function build:write(rel, content) write(join(outputDir, rel), content) end
function build:read(rel) return read(join(outputDir, rel)) end
function build:extract(rel, dest)
mkdir(join(outputDir, dest))
local src = join(outputDir, rel)
local dst = join(outputDir, dest)
sh('tar -xzf "' .. src .. '" -C "' .. dst .. '"')
end
function build:copy(rel, dest) copy(join(outputDir, rel), join(outputDir, dest)) end
function build:delete(rel) rm(join(outputDir, rel)) end
function build:move(rel, dest) os.rename(join(outputDir, rel), join(outputDir, dest)) end
function build:exists(rel) return exists(join(outputDir, rel)) end
function build:sh(cmd)
local res = sh(cmd)
assert(res == 0 or res == true, "failed to execute " .. cmd)
end
function build:cc(args)
local compiler = os.getenv("SEA_CC") or os.getenv("CC") or "gcc"
local cmd = compiler .. " " .. table.concat(args, " ")
local res = sh(cmd)
assert(res == 0 or res == true, "cc failed: " .. cmd)
end
end
package.loaded["lde-build"] = setmetatable({ outDir = outputDir }, build)
local oldDir = getcwd()
chdir(packagePath)
dofile(join(packagePath, "build.lua"))
chdir(oldDir)
else
mklink(join(packagePath, "src"), join(targetDir, config.name))
end
if not config.dependencies then return end
for name, dep in pairs(config.dependencies) do
---@format disable-next
if dep.path then
buildPackage(join(packagePath, dep.path), targetDir)
elseif dep.git then -- downloads to tmpLDEDir/<name> then build to target
local finalDir = join(tmpLDEDir, "git", name)
if not exists(finalDir) then
local tarballUrl = dep.git .. "/archive/master.tar.gz"
local tarball = join(tmpLDEDir, "tar", name)
local curlOk = sh('curl -fsSL "' .. tarballUrl .. '" -o "' .. tarball .. '"')
assert(curlOk == 0 or curlOk == true,
"failed to download " .. tarballUrl)
mkdir(finalDir)
-- On Windows, bsdtar misparses drive letters (C:) as remote hosts.
-- Use pushd to cd into the dest dir so neither -f nor -C see a drive letter path.
local tarOk = sh('tar -xzf "' .. tarball .. '" --strip-components=1 -C "' .. finalDir .. '"')
assert(tarOk == 0 or tarOk == true,
"failed to extract tarball for " .. name .. " — repo may use submodules (not supported in bootstrap mode)")
end
buildPackage(finalDir, targetDir)
else
error("Unknown dependency type: " .. name)
end
end
return config
end
local function build()
mkdir(tmpLDEDir)
mkdir(join(tmpLDEDir, "tar"))
mkdir(join(tmpLDEDir, "git"))
local cwd = getcwd()
return buildPackage(cwd, join(cwd, "target"))
end
if #args == 0 then
print("Usage: minilde [-C <dir>] <command>")
print("Commands:")
print(" run: build and run the package")
return
end
-- -C <dir>: change working directory before doing anything
if args[1] == "-C" then
table.remove(args, 1)
local dir = assert(table.remove(args, 1), "minilde: -C requires a directory argument")
pcall(ffi.cdef, isWindows and "int _chdir(const char *path);" or "int chdir(const char *path);")
local chdir = isWindows and ffi.C._chdir or ffi.C.chdir
assert(chdir(dir) == 0, "minilde: -C: cannot chdir to '" .. dir .. "'")
end
if pop() == "run" then
local config = assert(build())
local cwd = getcwd()
package.path = join(cwd, "target", "?.lua") .. ";" ..
join(cwd, "target", "?", "init.lua") .. ";" ..
package.path
package.cpath = join(cwd, "target", "?.so") .. ";" ..
join(cwd, "target", "?.dll") .. ";" ..
join(cwd, "target", "?.dylib") .. ";" ..
package.cpath
local extraArgs = {}
local foundSep = false
for _, v in ipairs(args) do
if foundSep then
extraArgs[#extraArgs + 1] = v
elseif v == "--" then
foundSep = true
end
end
_G.arg = extraArgs
local chunk = loadfile(join(cwd, "target", config.name, "init.lua"))
if chunk then
chunk(unpack(extraArgs))
end
-- TODO: Figure out why luajit cleanup causes a segfault without this
ffi.C._exit(0)
end