This repository was archived by the owner on Jan 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
233 lines (199 loc) · 7.63 KB
/
main.lua
File metadata and controls
233 lines (199 loc) · 7.63 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
pcall(require, "luarocks.loader")
require "global"
local lfs = require "lfs"
local system = require "system"
local tools = require "tools"
local config = require "neld.config"
local repos = require "repos"
local self = {}
local rootfs = {}
for _, package in ipairs(config.bootstrap) do
rootfs[package] = package
end
if stage > 4 then
for _, package in ipairs(config.rootfs) do
rootfs[package] = package
end
end
local cwd = lfs.currentdir()
local base_build_path = "neld/.build"
local base_stage_path = ".stage" .. stage
local mnt_path = cwd .. "/" .. base_build_path .. "/work/mnt"
local root_path = mnt_path .. "/root"
local overlay_path = root_path .. "/usr"
local ro_path = mnt_path .. "/ro"
local mountpoints = {}
local function prepare_mounts(overlay, packages)
for _, package in ipairs(packages) do
local is_string = type(package) == "string"
local mount_name
if is_string then
mount_name = package
local i = package:find("%.")
if i then
package = pkg(package:sub(1, i - 1))
else
package = pkg(package)
end
else
mount_name = package.name
end
if not rootfs[mount_name] then
local mountpoint = mnt_path .. "/" .. mount_name
overlay[mount_name] = mountpoint
local suffix = "/" .. tools.get_file(mount_name, package.version)
local mnt = cwd .. "/" .. config.repository .. "/" .. package.name .. "/" .. base_stage_path .. suffix
local prebuilt_mnt = cwd .. "/" .. base_build_path .. suffix
if not mountpoints[mount_name] then
lfs.mkdir(mountpoint)
if os.execute("squashfuse " .. mnt .. " " .. mountpoint) or os.execute("squashfuse " .. prebuilt_mnt .. " " .. mountpoint) then
mountpoints[mount_name] = mountpoint
else
print("FAILED MOUNT: " .. prebuilt_mnt)
end
end
if package.dev_dependencies then
prepare_mounts(overlay, package.dev_dependencies)
end
if package.dependencies then
prepare_mounts(overlay, package.dependencies)
end
end
end
end
function self.close()
lfs.chdir(cwd)
if stage ~= 1 then
os.execute("umount " .. root_path)
end
for _, m in pairs(mountpoints) do
os.execute("umount " .. m)
end
end
function self.build(name, skip_dependencies)
if name:find("%.") then
return
end
local package, process_main, build_suffix = pkg(name), true, config.repository .. "/" .. name
if lfs.attributes(cwd .. "/" .. build_suffix .. "/" .. base_stage_path .. "/" .. tools.get_file(name, package.version)) then
if not package.variants then
return
end
process_main = false
end
if not skip_dependencies then
local function prepare_package(package)
local name = package.name
if stage < 5 or not rootfs[name] then
if stage > 4
and repos.available_packages[name] ~= nil
and not lfs.attributes(cwd .. "/" .. config.repository .. "/" .. name .. "/" .. base_stage_path .. "/" .. tools.get_file(name, package.version)) then
local old_cwd = lfs.currentdir()
lfs.mkdir(cwd .. "/" .. base_build_path)
lfs.chdir(cwd .. "/" .. base_build_path)
repos.download(name)
lfs.chdir(old_cwd)
else
self.build(name)
end
end
end
if stage ~= 1 and package.dev_dependencies then
for _, package in ipairs(package.dev_dependencies) do
prepare_package(package)
end
end
if package.dependencies then
for _, package in ipairs(package.dependencies) do
prepare_package(package)
end
end
end
local overlay = {}
if stage ~= 1 then
prepare_mounts(overlay, config.development)
if package.dev_dependencies then
prepare_mounts(overlay, package.dev_dependencies)
end
if package.dependencies then
prepare_mounts(overlay, package.dependencies)
end
local lowerdir = ro_path .. ":"
for _, m in pairs(overlay) do
lowerdir = lowerdir .. m .. ":"
end
os.execute("fuse-overlayfs -o lowerdir=" .. lowerdir:sub(1, -2) .. " " .. overlay_path)
end
lfs.chdir(build_suffix)
local package_path = lfs.currentdir()
lfs.mkdir(base_stage_path)
lfs.chdir(base_stage_path)
build_suffix = build_suffix .. "/" .. base_stage_path
local build_path = lfs.currentdir()
if process_main and package.sources then
for _, source in ipairs(package.sources) do
local path, url = source[1], source[2]
if not lfs.attributes(path) then
os.execute("curl -sSLo S" .. path .. " " .. url)
os.execute("rm -rf " .. path)
lfs.mkdir(path)
local extension = string.sub(url, -4)
if extension == ".zip" or extension == ".whl" then
os.execute("unzip S" .. path .. " -d " .. path)
else
os.execute("tar xf S" .. path .. " --strip-components=1 -C " .. path)
end
local patch_dir = package_path .. "/" .. path
if not lfs.attributes(patch_dir) then
if path == "source" then
patch_dir = nil
else
patch_dir = package_path .. "/source"
if not lfs.attributes(patch_dir) then
patch_dir = nil
end
end
end
if patch_dir then
lfs.chdir(path)
for file in lfs.dir(patch_dir) do
if file ~= "." and file ~= ".." then
os.execute("patch -p 1 -i " .. patch_dir .. "/" .. file)
end
end
lfs.chdir(build_path)
end
end
end
end
lfs.chdir(cwd)
-- especially required in stage 2 and for some packages that cannot find system c++ headers
local cpp_paths = "/include/c++"
cpp_paths = ":" .. cpp_paths .. ":" .. cpp_paths .. "/" .. system.target
local process_main_option = " 0"
if process_main then
process_main_option = " 1"
end
os.execute(
"bwrap --unshare-ipc --unshare-pid --unshare-net --unshare-uts --unshare-cgroup-try --clearenv --setenv PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin --setenv C_INCLUDE_PATH /include --setenv CPLUS_INCLUDE_PATH /include" ..
cpp_paths .. " --chdir /root --ro-bind " ..
root_path .. " / --dev /dev --tmpfs /tmp " ..
"--ro-bind " .. cwd .. " /root --bind " .. build_path .. " /root/" .. build_suffix ..
" /bin/env lua untrusted_build.lua " .. name .. process_main_option .. " " .. stage)
if stage ~= 1 then
os.execute("umount " .. overlay_path)
end
end
if stage == 1 then
root_path = "/"
else
lfs.mkdir(base_build_path)
lfs.mkdir(base_build_path .. "/work")
lfs.mkdir(mnt_path)
lfs.mkdir(root_path)
os.execute("squashfuse " .. base_build_path .. "/work/rootfs.sqsh " .. root_path)
lfs.mkdir(ro_path)
lfs.mkdir(ro_path .. "/bin")
lfs.link("../../bin/env", ro_path .. "/bin/env", true)
end
return self