-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.lua
executable file
·309 lines (285 loc) · 6.94 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
-- Main build file. Only tested on Linux - may not work on other platforms.
local scriptDir;
if arg[0] == "build.lua" then
scriptDir = ".";
else
scriptDir = arg[0]:sub(1, -(2 + ("build.lua"):len()));
end
local scriptDirLen = scriptDir:len();
local osType = jit.os == "Windows" and "win" or "posix";
local sep = jit.os == "Windows" and "\\" or "/";
local function abs(fp)
return scriptDir .. sep .. fp;
end
local luaScripts = {
{ "ntb.c", abs("main/c.lua") },
{ "ntb.common", abs("main/common.lua") },
{ "ntb.importer", abs("main/importer.lua") },
{ "ntb.main", abs("main/main.lua") },
{ "ntb.misc", abs("main/misc.lua") },
{ "ntb.ninjaFileBuilder", abs("main/ninjaFileBuilder.lua") },
{ "ntb.ninjaFileWrite", abs("main/ninjaFileWrite.lua") },
};
local function pathSplit(fp)
local dot;
local slash;
for i = fp:len(), 0, -1 do
if dot and slash then
break;
end
local c = fp:sub(i, i);
if dot == nil and c == "." then
dot = i;
elseif slash == nil and c == sep then
slash = i;
end
end
if slash == nil then
if dot == nil then
return "", fp, "";
end
return "", fp:sub(1, dot - 1), fp:sub(dot);
end
if dot == nil or dot <= slash then
return fp:sub(1, slash - 1), fp:sub(slash + 1), nil;
end
return fp:sub(1, slash - 1), fp:sub(slash + 1, dot - 1), fp:sub(dot);
end
local function luaLib(name, files)
if type(files) == "string" then
files = { files };
end
for _, file in ipairs(files) do
local dir, base, ext = pathSplit(file);
if (base == "init" and ext == ".lua") or base == name then
table.insert(luaScripts, { name, file });
else
table.insert(luaScripts, { name .. "." .. base, file });
end
end
end
local function pl(names)
local ret = {};
for _, name in ipairs(names) do
table.insert(ret, abs("main" .. sep .. "lib" .. sep .. "pl" .. sep .. name .. ".lua"));
end
return ret;
end
luaLib("pl", pl{
"app",
"array2d",
"class",
"compat",
"comprehension",
"config",
"data",
"Date",
"dir",
"file",
"func",
"import_into",
"init",
"input",
"lapp",
"lexer",
"List",
"luabalanced",
"Map",
"MultiMap",
"operator",
"OrderedMap",
"path",
"permute",
"pretty",
"seq",
"Set",
"sip",
"strict",
"stringio",
"stringx",
"tablex",
"template",
"test",
"text",
"types",
"url",
"utils",
"xml",
});
local sources = {
abs("main/main.c"),
abs("main/lib/lfs/lfs.c"),
};
local luaCHeaders = {
'lauxlib.h',
'luaconf.h',
'lua.h',
'luajit.h',
'lualib.h',
};
local buildBase = abs('build');
local function libObj(lib)
local ext = jit.os == "Windows" and ".obj" or ".o";
local p = lib:sub(scriptDirLen + 1);
return buildBase .. sep .. 'obj' .. p .. ext;
end
local function getMainSourceFiles()
local ret = {};
for _, src in ipairs(sources) do
table.insert(ret, src);
end
for _, lib in ipairs(luaScripts) do
table.insert(ret, libObj(lib[2]));
end
return ret;
end
local function exec(cmd)
print("Executing: " .. cmd);
if os.execute(cmd) == nil then
error("Command '" .. cmd .. "' failed.");
end
end
local function quote(argument)
if jit.os == "Windows" then
if argument == "" or argument:find('[ \f\t\v]') then
argument = '"' .. argument:gsub([[(\*)"]], [[%1%1\"]]):gsub([[\+$]], "%0%0") .. '"';
end
return (argument:gsub('["^<>!|&%%]', "^%0"));
else
if argument == "" or argument:find('[^a-zA-Z0-9_@%+=:,./-]') then
argument = "'" .. argument:gsub("'", [['\'']]) .. "'";
end
return argument;
end
end
local function getLuajitIncludeFile()
local function tryIncludeDir(input, output)
local compileCmd;
if jit.os == "Windows" then
compileCmd = 'cl.exe -TC -c ' .. quote(input) .. ' -Fo' .. quote(output) .. ' >nul 2>nul';
else
compileCmd = 'clang -I/usr/local/include -x c -c ' .. quote(input) .. ' -o ' .. quote(output) .. ' > /dev/null 2>&1';
end
return os.execute(compileCmd);
end
local function getCCode(prefix)
local pref = '';
if prefix and prefix ~= "" then
pref = prefix .. '/';
end
local ret = {};
for _, ch in ipairs(luaCHeaders) do
table.insert(ret, '#include <' .. pref .. ch .. '>');
end
return table.concat(ret, '\n');
end
local prefixes = {'luajit-2.1', 'luajit-2.0', 'luajit', ''};
for _, prefix in ipairs(prefixes) do
local ccode = getCCode(prefix);
local input = os.tmpname();
local output = os.tmpname();
local inp = io.open(input, 'w');
inp:write(ccode);
inp:close();
local code = tryIncludeDir(input, output);
os.remove(output);
if code == 0 then
return input;
end
os.remove(input);
end
return nil;
end
local function checkSourceFiles()
local files = {
"main/lib/lfs/lfs.c",
"main/lib/lfs/lfs.h",
"main/vendor/xxhash/xxhash.c",
"main/vendor/xxhash/xxhash.h",
"main/main.c",
};
for _, f in ipairs(files) do
f = abs(f);
local fh = io.open(f, "rb");
local str = fh:read("*all");
fh:close();
for _, ch in ipairs(luaCHeaders) do
if str:find(ch, 1, true) then
return f;
end
end
end
return nil;
end
local function mkdirp(dir)
if jit.os == "Windows" then
os.execute('mkdir ' .. quote(dir));
else
os.execute('mkdir -p ' .. quote(dir));
end
end
local function doPrebuildCommands()
local f = checkSourceFiles();
if f then
error("File '" .. f .. "' must not include lua headers because they will be automatically inserted.");
end
for _, lib in ipairs(luaScripts) do
local l = libObj(lib[2]);
local name = lib[1];
local dir = pathSplit(l);
mkdirp(dir);
exec('luajit -O3 -b -g -n ' .. quote(name) .. ' ' .. quote(lib[2]) .. ' ' .. quote(l));
end
end
local function doBuildCommands()
local inc = getLuajitIncludeFile();
if not inc then
error("Could not find luajit.");
end
local src = getMainSourceFiles();
local buildBinDir = buildBase .. sep .. "bin";
mkdirp(buildBinDir);
if jit.os == "Windows" then
local out = buildBinDir .. sep .. "ntb.exe";
exec(
'cl.exe'
.. ' /Ox /FI ' .. quote(inc) .. ' /nologo /W3 /DNDEBUG /D_CRT_SECURE_NO_WARNINGS /DSODIUM_STATIC=1'
.. ' ' .. table.concat(src, " ")
.. ' lua51.lib'
.. ' /Fe' .. quote(out)
);
else
local out = buildBinDir .. sep .. "ntb";
if jit.os == "Linux" then
exec(
'clang'
.. ' -include ' .. quote(inc) .. ' -O3 -s -DNDEBUG -D_GNU_SOURCE=1 -std=gnu99 -Wall -Wextra -flto'
.. ' ' .. table.concat(src, " ")
.. ' -Wl,-Bstatic,-lluajit-5.1,-Bdynamic -Wl,-E -ldl -lm'
.. ' -o ' .. quote(out)
);
elseif jit.os == "OSX" then
exec(
'clang'
.. ' -include ' .. quote(inc) .. ' -O3 -DNDEBUG -D_GNU_SOURCE=1 -std=gnu99 -Wall -Wextra -flto'
.. ' ' .. table.concat(src, " ")
.. ' -lluajit-5.1 -ldl -lm -pagezero_size 10000 -image_base 100000000'
.. ' -o ' .. quote(out)
);
else
exec(
'clang'
.. ' -include ' .. quote(inc) .. ' -O3 -s -DNDEBUG -D_GNU_SOURCE=1 -std=gnu99 -Wall -Wextra -flto -fuse-ld=lld -I/usr/local/include -L/usr/local/lib'
.. ' ' .. table.concat(src, " ")
.. ' -Wl,-Bstatic,-lluajit-5.1,-Bdynamic -Wl,-E -lm'
.. ' -o ' .. quote(out)
);
end
end
os.remove(inc);
end
local function compile()
doPrebuildCommands();
doBuildCommands();
end
compile();