luastatic

repository·master·Indexed 21 days ago

https://github.com/ers35/luastatic

A command line tool that builds standalone executables from Lua programs, allowing distribution as single binaries without a pre-installed Lua interpreter. It supports Lua 5.1, 5.2, 5.3, 5.4, and LuaJIT, and provides capabilities for embedding Lua files, including C libraries, cross-compiling for Windows, and static linking with musl libc.

Tokens
1.2K
Snippets
12
Records
12
Agent score
25%

What's inside luastatic

  1. Build using Homebrew (macOS)

    master

    If you installed Lua and LuaRocks via Homebrew, use brew --prefix to dynamically locate the correct library and include paths.

    # Install Lua and LuaRocks from Homebrew.
    brew install lua luarocks
    # Install luastatic from LuaRocks.
    luarocks install luastatic
    # Build using the Homebrew installation path.
    luastatic main.lua $(brew --prefix lua)/lib/liblua.a -I$(brew --prefix lua)/include/lua
    brew install lua luarocks
    luarocks install luastatic
    luastatic main.lua $(brew --prefix lua)/lib/liblua.a -I$(brew --prefix lua)/include/lua
  2. Statically link with musl libc

    master

    To create a highly portable binary using musl-gcc and static linking, set the CC environment variable and pass the -static flag.

    CC=musl-gcc luastatic main.lua /usr/lib/x86_64-linux-musl/liblua5.2.a -I/usr/include/lua5.2 -static
    CC=musl-gcc luastatic main.lua /usr/lib/x86_64-linux-musl/liblua5.2.a -I/usr/include/lua5.2 -static
  3. Build with LuaJIT on macOS

    master

    When using LuaJIT on macOS, you may need to specify -pagezero_size and -image_base for the linker.

    luastatic main.lua /opt/local/lib/libluajit-5.1.a -I/opt/local/include/luajit-2.0 -pagezero_size 10000 -image_base 100000000
    luastatic main.lua /opt/local/lib/libluajit-5.1.a -I/opt/local/include/luajit-2.0 -pagezero_size 10000 -image_base 100000000
  4. Include C libraries containing luaopen_()

    master

    To include a Lua binary module (a C library), pass the static library file to luastatic.

    luastatic main.lua library.a /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2
    luastatic main.lua library.a /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2
  5. Dynamically link with Lua

    master

    If you prefer to link against a shared Lua library instead of a static one, use the -l flag to pass linking instructions to the C compiler.

    luastatic main.lua -llua5.2 -I/usr/include/lua5.2
    luastatic main.lua -llua5.2 -I/usr/include/lua5.2
  6. Build a single Lua file executable

    master

    To build a standalone executable from a single Lua file, provide the entry point, the path to the Lua static library, and the include directory for lua.h.

    luastatic main.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2
    luastatic main.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2
  7. Build with LuaJIT on Ubuntu 16.10

    master

    When using LuaJIT on Ubuntu, you may need to pass specific flags like -no-pie to the compiler.

    luastatic main.lua /usr/lib/x86_64-linux-gnu/libluajit-5.1.a -I/usr/include/luajit-2.0 -no-pie
    luastatic main.lua /usr/lib/x86_64-linux-gnu/libluajit-5.1.a -I/usr/include/luajit-2.0 -no-pie
  8. Cross compile for Windows

    master

    You can cross-compile for Windows by setting the CC environment variable to a MinGW compiler and providing the Windows-compatible Lua static library and include paths.

    CC=x86_64-w64-mingw32-gcc luastatic main.lua /usr/x86_64-w64-mingw32/lib/liblua5.2.a -I/usr/x86_64-w64-mingw32/include/lua5.2/
    CC=x86_64-w64-mingw32-gcc luastatic main.lua /usr/x86_64-w64-mingw32/lib/liblua5.2.a -I/usr/x86_64-w64-mingw32/include/lua5.2/
  9. Embed Lua files for require()

    master

    If your main script uses require("library"), you can embed library.lua directly into the executable so it is available at runtime.

    luastatic main.lua library.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2
    luastatic main.lua library.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2
  10. Use the luastatic CLI

    master

    The luastatic command line tool builds standalone executables from Lua programs. The resulting executable can run on systems that do not have Lua installed. It supports Lua 5.1, 5.2, 5.3, 5.4, and LuaJIT.

    Command Syntax:

    luastatic main.lua [required_lua_files] [lua_interpreter_static_lib] [binary_module_static_libs] -I/include/lua [additional_c_compiler_args]

    Arguments:

    • main.lua: The entry point to the Lua program.
    • required.lua: One or more required Lua source files to be embedded.
    • liblua.a: The path to the Lua interpreter static library.
    • library.a: One or more static libraries for a required Lua binary module.
    • -I/include/lua: The path to the directory containing lua.h.
    • [additional_args]: Any additional arguments are passed directly to the C compiler.
    luastatic main.lua require.lua liblua.a library.a -I/include/lua