-
Notifications
You must be signed in to change notification settings - Fork 40
/
luastatic.lua
executable file
·468 lines (421 loc) · 12 KB
/
luastatic.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
#!/usr/bin/env lua
-- The author disclaims copyright to this source code.
-- The C compiler used to compile and link the generated C source file.
local CC = os.getenv("CC") or "cc"
-- The nm used to determine whether a library is liblua or a Lua binary module.
local NM = os.getenv("NM") or "nm"
local function file_exists(name)
local file = io.open(name, "r")
if file then
file:close()
return true
end
return false
end
--[[
Run a shell command, wait for it to finish, and return a string containing stdout.
--]]
local function shellout(command)
local file = io.popen(command)
local stdout = file:read("*all")
local ok = file:close()
if ok then
return stdout
end
return nil
end
--[[
Use execute() when stdout isn't needed instead of shellout() because io.popen() does
not return the status code in Lua 5.1.
--]]
local function execute(cmd)
local ok = os.execute(cmd)
return (ok == true or ok == 0)
end
--[[
Return a comma separated hex string suitable for a C array definition.
--]]
local function string_to_c_hex_literal(characters)
local hex = {}
for character in characters:gmatch(".") do
table.insert(hex, ("0x%02x"):format(string.byte(character)))
end
return table.concat(hex, ", ")
end
assert(string_to_c_hex_literal("hello") == "0x68, 0x65, 0x6c, 0x6c, 0x6f")
--[[
Strip the directory from a filename.
--]]
local function basename(path)
local name = path:gsub([[(.*[\/])(.*)]], "%2")
return name
end
assert(basename("/path/to/file.lua") == "file.lua")
assert(basename([[C:\path\to\file.lua]]) == "file.lua")
local function is_source_file(extension)
return
-- Source file.
extension == "lua" or
-- Precompiled chunk.
extension == "luac"
end
local function is_binary_library(extension)
return
-- Object file.
extension == "o" or
-- Static library.
extension == "a" or
-- Shared library.
extension == "so" or
-- Mach-O dynamic library.
extension == "dylib"
end
-- Required Lua source files.
local lua_source_files = {}
-- Libraries for required Lua binary modules.
local module_library_files = {}
local module_link_libraries = {}
-- Libraries other than Lua binary modules, including liblua.
local dep_library_files = {}
-- Additional arguments are passed to the C compiler.
local other_arguments = {}
-- Get the operating system name.
local UNAME = (shellout("uname -s") or "Unknown"):match("%a+") or "Unknown"
local link_with_libdl = ""
--[[
Parse command line arguments. main.lua must be the first argument. Static libraries are
passed to the compiler in the order they appear and may be interspersed with arguments to
the compiler. Arguments to the compiler are passed to the compiler in the order they
appear.
--]]
for i, name in ipairs(arg) do
local extension = name:match("%.(%a+)$")
if i == 1 or (is_source_file(extension) or is_binary_library(extension)) then
if not file_exists(name) then
io.stderr:write("file does not exist: " .. name .. "\n")
os.exit(1)
end
local info = {}
info.path = name
info.basename = basename(info.path)
info.basename_noextension = info.basename:match("(.+)%.") or info.basename
--[[
Handle the common case of "./path/to/file.lua".
This won't work in all cases.
--]]
info.dotpath = info.path:gsub("^%.%/", "")
info.dotpath = info.dotpath:gsub("[\\/]", ".")
info.dotpath_noextension = info.dotpath:match("(.+)%.") or info.dotpath
info.dotpath_underscore = info.dotpath_noextension:gsub("[.-]", "_")
if i == 1 or is_source_file(extension) then
table.insert(lua_source_files, info)
elseif is_binary_library(extension) then
-- The library is either a Lua module or a library dependency.
local nmout = shellout(NM .. " " .. info.path)
if not nmout then
io.stderr:write("nm not found\n")
os.exit(1)
end
local is_module = false
if nmout:find("T _?luaL_newstate") then
if nmout:find("U _?dlopen") then
if UNAME == "Linux" or UNAME == "SunOS" or UNAME == "Darwin" then
--[[
Link with libdl because liblua was built with support loading
shared objects and the operating system depends on it.
--]]
link_with_libdl = "-ldl"
end
end
else
for luaopen in nmout:gmatch("[^dD] _?luaopen_([%a%p%d]+)") do
local modinfo = {}
modinfo.path = info.path
modinfo.dotpath_underscore = luaopen
modinfo.dotpath = modinfo.dotpath_underscore:gsub("_", ".")
modinfo.dotpath_noextension = modinfo.dotpath
is_module = true
table.insert(module_library_files, modinfo)
end
end
if is_module then
table.insert(module_link_libraries, info.path)
else
table.insert(dep_library_files, info.path)
end
end
else
-- Forward the remaining arguments to the C compiler.
table.insert(other_arguments, name)
end
end
if #lua_source_files == 0 then
local version = "0.0.12"
print("luastatic " .. version)
print([[
usage: luastatic main.lua[1] require.lua[2] liblua.a[3] library.a[4] -I/include/lua[5] [6]
[1]: The entry point to the Lua program
[2]: One or more required Lua source files
[3]: The path to the Lua interpreter static library
[4]: One or more static libraries for a required Lua binary module
[5]: The path to the directory containing lua.h
[6]: Additional arguments are passed to the C compiler]])
os.exit(1)
end
-- The entry point to the Lua program.
local mainlua = lua_source_files[1]
--[[
Generate a C program containing the Lua source files that uses the Lua C API to
initialize any Lua libraries and run the program.
--]]
local outfilename = mainlua.basename_noextension .. ".luastatic.c"
local outfile = io.open(outfilename, "w+")
local function out(...)
outfile:write(...)
end
local function outhex(str)
outfile:write(string_to_c_hex_literal(str), ", ")
end
--[[
Embed Lua program source code.
--]]
local function out_lua_source(file)
local f = io.open(file.path, "r")
local prefix = f:read(4)
if prefix then
if prefix:match("\xef\xbb\xbf") then
-- Strip the UTF-8 byte order mark.
prefix = prefix:sub(4)
end
if prefix:match("#") then
-- Strip the shebang.
f:read("*line")
prefix = "\n"
end
out(string_to_c_hex_literal(prefix), ", ")
end
while true do
local strdata = f:read(4096)
if strdata then
out(string_to_c_hex_literal(strdata), ", ")
else
break
end
end
f:close()
end
out([[
#ifdef __cplusplus
extern "C" {
#endif
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#ifdef __cplusplus
}
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if LUA_VERSION_NUM == 501
#define LUA_OK 0
#endif
/* Copied from lua.c */
static lua_State *globalL = NULL;
static void lstop (lua_State *L, lua_Debug *ar) {
(void)ar; /* unused arg. */
lua_sethook(L, NULL, 0, 0); /* reset hook */
luaL_error(L, "interrupted!");
}
static void laction (int i) {
signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
}
static void createargtable (lua_State *L, char **argv, int argc, int script) {
int i, narg;
if (script == argc) script = 0; /* no script name? */
narg = argc - (script + 1); /* number of positive indices */
lua_createtable(L, narg, script + 1);
for (i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i - script);
}
lua_setglobal(L, "arg");
}
static int msghandler (lua_State *L) {
const char *msg = lua_tostring(L, 1);
if (msg == NULL) { /* is error object not a string? */
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
return 1; /* that is the message */
else
msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1));
}
/* Call debug.traceback() instead of luaL_traceback() for Lua 5.1 compatibility. */
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
/* debug */
lua_remove(L, -2);
lua_pushstring(L, msg);
/* original msg */
lua_remove(L, -3);
lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */
return 1; /* return the traceback */
}
static int docall (lua_State *L, int narg, int nres) {
int status;
int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, msghandler); /* push message handler */
lua_insert(L, base); /* put it under function and args */
globalL = L; /* to be available to 'laction' */
signal(SIGINT, laction); /* set C-signal handler */
status = lua_pcall(L, narg, nres, base);
signal(SIGINT, SIG_DFL); /* reset C-signal handler */
lua_remove(L, base); /* remove message handler from the stack */
return status;
}
#ifdef __cplusplus
extern "C" {
#endif
]])
for _, library in ipairs(module_library_files) do
out((' int luaopen_%s(lua_State *L);\n'):format(library.dotpath_underscore))
end
out([[
#ifdef __cplusplus
}
#endif
int main(int argc, char *argv[])
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
createargtable(L, argv, argc, 0);
static const unsigned char lua_loader_program[] = {
]])
outhex([[
local args = {...}
local lua_bundle = args[1]
local function load_string(str, name)
if _VERSION == "Lua 5.1" then
return loadstring(str, name)
else
return load(str, name)
end
end
local function lua_loader(name)
local separator = package.config:sub(1, 1)
name = name:gsub(separator, ".")
local mod = lua_bundle[name] or lua_bundle[name .. ".init"]
if mod then
if type(mod) == "string" then
local chunk, errstr = load_string(mod, name)
if chunk then
return chunk
else
error(
("error loading module '%s' from luastatic bundle:\n\t%s"):format(name, errstr),
0
)
end
elseif type(mod) == "function" then
return mod
end
else
return ("\n\tno module '%s' in luastatic bundle"):format(name)
end
end
table.insert(package.loaders or package.searchers, 2, lua_loader)
-- Lua 5.1 has unpack(). Lua 5.2+ has table.unpack().
local unpack = unpack or table.unpack
]])
outhex(([[
local func = lua_loader("%s")
if type(func) == "function" then
-- Run the main Lua program.
func(unpack(arg))
else
error(func, 0)
end
]]):format(mainlua.dotpath_noextension))
out(([[
};
/*printf("%%.*s", (int)sizeof(lua_loader_program), lua_loader_program);*/
if
(
luaL_loadbuffer(L, (const char*)lua_loader_program, sizeof(lua_loader_program), "%s")
!= LUA_OK
)
{
fprintf(stderr, "luaL_loadbuffer: %%s\n", lua_tostring(L, -1));
lua_close(L);
return 1;
}
/* lua_bundle */
lua_newtable(L);
]]):format(mainlua.basename_noextension));
for i, file in ipairs(lua_source_files) do
out((' static const unsigned char lua_require_%i[] = {\n '):format(i))
out_lua_source(file);
out("\n };\n")
out(([[
lua_pushlstring(L, (const char*)lua_require_%i, sizeof(lua_require_%i));
]]):format(i, i))
out((' lua_setfield(L, -2, "%s");\n\n'):format(file.dotpath_noextension))
end
for _, library in ipairs(module_library_files) do
out((' lua_pushcfunction(L, luaopen_%s);\n'):format(library.dotpath_underscore))
out((' lua_setfield(L, -2, "%s");\n\n'):format(library.dotpath_noextension))
end
out([[
if (docall(L, 1, LUA_MULTRET))
{
const char *errmsg = lua_tostring(L, 1);
if (errmsg)
{
fprintf(stderr, "%s\n", errmsg);
}
lua_close(L);
return 1;
}
lua_close(L);
return 0;
}
]])
outfile:close()
if os.getenv("CC") == "" then
-- Disable compiling and exit with a success code.
os.exit(0)
end
if not execute(CC .. " --version 1>/dev/null 2>/dev/null") then
io.stderr:write("C compiler not found.\n")
os.exit(1)
end
-- http://lua-users.org/lists/lua-l/2009-05/msg00147.html
local rdynamic = "-rdynamic"
local binary_extension = ""
if shellout(CC .. " -dumpmachine"):match("mingw") then
rdynamic = ""
binary_extension = ".exe"
end
local compile_command = table.concat({
CC,
"-Os",
outfilename,
-- Link with Lua modules first to avoid linking errors.
table.concat(module_link_libraries, " "),
table.concat(dep_library_files, " "),
rdynamic,
"-lm",
link_with_libdl,
"-o " .. mainlua.basename_noextension .. binary_extension,
table.concat(other_arguments, " "),
}, " ")
print(compile_command)
local ok = execute(compile_command)
if ok then
os.exit(0)
else
os.exit(1)
end