Created
August 12, 2026 16:22
-
-
Save xsnpdngv/556b26a6a61b47e621f15268993e983c to your computer and use it in GitHub Desktop.
Build a self-contained luacheck binary on the target machine from the latest sources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Build a single-file, self-contained luacheck ELF binary from source. | |
| # No root, no package manager, no `make install`; everything under $DIR. | |
| # | |
| # Prereqs on the host (system defaults are enough on RHEL 8+): | |
| # curl git gcc make ar ranlib strip lua | |
| # | |
| # Mirrors the versions upstream's own release build uses | |
| # (luacheck/scripts/build-binaries.sh + luacheck/build/Makefile), minus | |
| # LuaLanes (optional; adds --jobs N parallelism, not needed for lint). | |
| set -euo pipefail | |
| # ---- 0. Choose your prefix ---- | |
| DIR=$(pwd)/luacheck-build | |
| # Pinned versions (match upstream's build/Makefile). | |
| LUA_VERSION=5.4.4 | |
| LFS_VERSION=1_8_0 | |
| ARGPARSE_VERSION=0.7.1 | |
| LUACHECK_TAG=v1.2.0 | |
| BUILD_DIR="$DIR/build" | |
| DIST_DIR="$DIR/dist" | |
| LUACHECK_SRC="$DIR/luacheck" | |
| echo "== Prep build tree under $BUILD_DIR" | |
| rm -rf "$BUILD_DIR" | |
| mkdir -p "$BUILD_DIR" "$DIST_DIR" | |
| cd "$BUILD_DIR" | |
| # ---- 1. Fetch the pinned sources ---- | |
| echo "== Fetch Lua $LUA_VERSION" | |
| curl -sL "https://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz" | tar xz | |
| echo "== Fetch LuaFileSystem $LFS_VERSION" | |
| curl -sL "https://github.com/lunarmodules/luafilesystem/archive/v${LFS_VERSION}.tar.gz" | tar xz | |
| echo "== Fetch argparse $ARGPARSE_VERSION" | |
| curl -sL "https://github.com/luarocks/argparse/archive/${ARGPARSE_VERSION}.tar.gz" | tar xz | |
| echo "== Fetch luastatic (bundler)" | |
| git clone --depth 1 https://github.com/ers35/luastatic.git | |
| if [ ! -d "$LUACHECK_SRC" ]; then | |
| echo "== Fetch luacheck $LUACHECK_TAG" | |
| git clone --depth 1 --branch "$LUACHECK_TAG" \ | |
| https://github.com/lunarmodules/luacheck.git "$LUACHECK_SRC" | |
| fi | |
| # ---- 2. Build liblua.a (static Lua interpreter library, no install) ---- | |
| echo "== Build liblua.a" | |
| ( cd "lua-${LUA_VERSION}" && make posix -j"$(nproc)" >/dev/null ) | |
| LUA_INC="$BUILD_DIR/lua-${LUA_VERSION}/src" | |
| LUA_A="$BUILD_DIR/lua-${LUA_VERSION}/src/liblua.a" | |
| # ---- 3. Build lfs.a (static LuaFileSystem archive) ---- | |
| echo "== Build lfs.a" | |
| LFS_SRC_DIR="$BUILD_DIR/luafilesystem-${LFS_VERSION}/src" | |
| gcc -c -O2 -Wall -I "$LUA_INC" \ | |
| -o "$LFS_SRC_DIR/lfs.o" \ | |
| "$LFS_SRC_DIR/lfs.c" | |
| ar rcs "$LFS_SRC_DIR/lfs.a" "$LFS_SRC_DIR/lfs.o" | |
| ranlib "$LFS_SRC_DIR/lfs.a" | |
| LFS_A="$LFS_SRC_DIR/lfs.a" | |
| # ---- 4. Stage the flat layout luastatic wants ---- | |
| # - luacheck modules under ./luacheck/ (copied from the git clone) | |
| # - argparse.lua at the top level | |
| # - main script copied to ./luacheck_bin.lua so it does NOT collide | |
| # with the module namespace called 'luacheck' (upstream Makefile | |
| # does exactly the same thing). | |
| echo "== Stage sources for bundler" | |
| cp -r "$LUACHECK_SRC/src/luacheck" . | |
| cp "$LUACHECK_SRC/bin/luacheck.lua" ./luacheck_bin.lua | |
| cp "argparse-${ARGPARSE_VERSION}/src/argparse.lua" . | |
| # ---- 5. Bundle: one gcc pass -> single ELF ---- | |
| echo "== Bundle with luastatic" | |
| CC=gcc NM=nm RANLIB=ranlib \ | |
| lua luastatic/luastatic.lua \ | |
| luacheck_bin.lua \ | |
| luacheck/*.lua luacheck/*/*.lua luacheck/*/*/*.lua \ | |
| argparse.lua \ | |
| "$LUA_A" "$LFS_A" \ | |
| -lm -lpthread -ldl \ | |
| -I "$LUA_INC" | |
| # luastatic writes ./luacheck_bin in CWD. | |
| test -f luacheck_bin | |
| # ---- 6. Strip and install to $DIST_DIR ---- | |
| echo "== Strip and install to $DIST_DIR/luacheck" | |
| mv luacheck_bin "$DIST_DIR/luacheck" | |
| strip "$DIST_DIR/luacheck" | |
| # ---- 7. Verify ---- | |
| echo "== Verify" | |
| "$DIST_DIR/luacheck" --version | |
| echo | |
| echo "OK: $DIST_DIR/luacheck" | |
| ls -la "$DIST_DIR/luacheck" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment