Re:Show HN: Luastatic – Build an executable from a Lua program
by camperman on Monday October 26, 2015 @10:23AM
(#10451611)
You can do this in Luajit with the following trick:
for f in *.lua; do
luajit -b $f `basename $f .lua`.o
done
ar rcus libmylib.a *.o
gcc -o myexe main-stub.c -I/usr/local/include/luajit-2.0 -L/usr/local/lib -lluajit-5.1 -Wl,--whole-archive libmylib.a -Wl,--no-whole-archive -Wl,-E
Where main-stub.c looks like this:
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
int
main(void)
{
int status, result, i;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
/* load your lua entry point here */
status = luaL_loadfile(L, "mainluafile.lua");
if (status) {
fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
exit(1);
}
result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
exit(1);
}
lua_close(L);
return 0;
}
The executables produced are very small because you dynamically link to Luajit and statically link to your own Lua code. I have a full media scheduler running at 100 sites which is only 40k. And it runs like a demon on ARM too.
Re:Show HN: Luastatic – Build an executable from a Lua program
by 0x01 on Monday October 26, 2015 @11:48AM
(#10452177)
That's a neat trick! Is there any reason you can't do this with luac? I tried the equivalent to what you're doing with luac, but gcc complains that "member libmylib.a(luafunc.o) in archive is not an object".
What's the difference between the bytecode generated by luajit compared to luac?
Re:Show HN: Luastatic – Build an executable from a Lua program
by camperman on Monday October 26, 2015 @01:40PM
(#10452946)
Luajit detects the kind of output file passed to the -b option, in this case .o, and writes the data accordingly. It's actually writing compiled architecture-specific object code here which can be linked into a static library. You can also pass it a .c or a .h extension to get c source or headers.
Re:Show HN: Luastatic – Build an executable from a Lua program
by 0x01 on Tuesday October 27, 2015 @06:25AM
(#10457163)
Ah, this explains why running a file on the .o file said it was lua bytecode. This feature of luajit makes it much more appealing. Thanks for the info!
Re:Show HN: Luastatic – Build an executable from a Lua program
by hbbio on Monday October 26, 2015 @05:32AM
(#10450277)
This is an interesting project, thanks for sharing.
Did you explore the possibility to use LuaJIT instead? The ability to do this and also integrate with Terra http://terralang.org/ would create a great project!
Re:Show HN: Luastatic – Build an executable from a Lua program
by Toenex on Monday October 26, 2015 @06:12AM
(#10450417)
I think this is targeted at the scenario where the developer works in Lua but is deploying to platform without any Lua dependencies. Not sure if it will pull in any used packages though.
Re:Show HN: Luastatic – Build an executable from a Lua program
by ers35 on Monday October 26, 2015 @07:03AM
(#10450599)
Lua binary modules are supported by referencing the path to the module's static library and any dependent libraries. Used packages are not automatically pulled in. LuaRocks integration would be interesting: https://github.com/keplerproject/luarocks/pull/264
What's the difference between the bytecode generated by luajit compared to luac?
I think luac compiles to bytecode only.
[0]: https://github.com/LuaDist/squish
Bundle [1], using LuaJIT
Luvi [2], which is also able to generate a single file executable and you can include static resources.
[1] https://luapower.com/bundle
[2] https://github.com/luvit/luvi
Did you explore the possibility to use LuaJIT instead? The ability to do this and also integrate with Terra http://terralang.org/ would create a great project!
I'm so excited to see you're still active!