Skip to content

Instantly share code, notes, and snippets.

@lopesivan
Created November 13, 2025 04:56
Show Gist options
  • Select an option

  • Save lopesivan/0352ba632b1d5dfc57633ac47e6423a3 to your computer and use it in GitHub Desktop.

Select an option

Save lopesivan/0352ba632b1d5dfc57633ac47e6423a3 to your computer and use it in GitHub Desktop.
Lua-build
#!/usr/bin/env bash
#
# build-lua-5.1.5.sh - Build Lua from source with static and shared libraries
# Author: Ivan Lopes
# Description: Downloads, compiles and installs Lua 5.1.5 into ~/.luaenv/versions/
#
set -e # Exit on error
# ============================================================================
# Configuration
# ============================================================================
readonly LUA_VERSION="5.1.5"
readonly LUA_MAJOR_VERSION="${LUA_VERSION%.?}"
readonly LUA_ARCHIVE="lua-${LUA_VERSION}.tar.gz"
readonly LUA_DIR="lua-${LUA_VERSION}"
readonly LUA_URL="https://www.lua.org/ftp/${LUA_ARCHIVE}"
readonly INSTALL_PREFIX="${HOME}/.luaenv/versions/${LUA_VERSION}"
readonly PWD_LOCAL="$PWD"
# Compiler flags
readonly CFLAGS_STATIC="-O2 -Wall -DLUA_USE_LINUX"
readonly CFLAGS_SHARED="-fPIC -O2 -Wall -DLUA_USE_LINUX"
readonly LDFLAGS="-lm -Wl,-E -ldl -lreadline -lhistory -lncurses"
# Core Lua source files (order matters for linking)
readonly LUA_CORE_FILES=(
lapi lcode ldebug ldo ldump lfunc lgc llex lmem lobject
lopcodes lparser lstate lstring ltable ltm lundump lvm lzio
)
# Lua library source files
readonly LUA_LIB_FILES=(
lauxlib lbaselib ldblib liolib lmathlib loslib
ltablib lstrlib loadlib linit
)
# Combine all object files
readonly ALL_SOURCE_FILES=("${LUA_CORE_FILES[@]}" "${LUA_LIB_FILES[@]}")
# ============================================================================
# Helper Functions
# ============================================================================
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}
error() {
echo "[ERROR] $*" >&2
exit 1
}
# ============================================================================
# Build Functions
# ============================================================================
download_lua() {
log "Checking for Lua source archive..."
if [[ -f "$LUA_ARCHIVE" ]]; then
log "Archive already exists: $LUA_ARCHIVE"
return 0
fi
log "Downloading Lua ${LUA_VERSION}..."
wget "$LUA_URL" || error "Failed to download Lua"
}
extract_lua() {
log "Cleaning up old source directory..."
[[ -d "$LUA_DIR" ]] && rm -rf "$LUA_DIR"
log "Extracting source archive..."
tar xzf "$LUA_ARCHIVE" || error "Failed to extract archive"
}
compile_objects() {
local cflags="$1"
local build_type="$2"
log "Compiling Lua objects ($build_type)..."
cd "${PWD_LOCAL}/${LUA_DIR}/src" || error "Failed to enter source directory"
# Compile all source files
for src in "${ALL_SOURCE_FILES[@]}"; do
gcc ${cflags} -c -o "${src}.o" "${src}.c" || error "Failed to compile ${src}.c"
done
}
build_static_library() {
log "Building static library..."
compile_objects "$CFLAGS_STATIC" "static"
# Create list of object files
local obj_files=()
for src in "${ALL_SOURCE_FILES[@]}"; do
obj_files+=("${src}.o")
done
# Create static library
ar rcu liblua.a "${obj_files[@]}" || error "Failed to create static library"
ranlib liblua.a || error "Failed to index static library"
# Build lua interpreter
log "Building lua interpreter..."
gcc ${CFLAGS_STATIC} -c -o lua.o lua.c || error "Failed to compile lua.c"
gcc -o lua lua.o liblua.a ${LDFLAGS} || error "Failed to link lua"
# Build luac compiler
log "Building luac compiler..."
gcc ${CFLAGS_STATIC} -c -o luac.o luac.c || error "Failed to compile luac.c"
gcc ${CFLAGS_STATIC} -c -o print.o print.c || error "Failed to compile print.c"
gcc -o luac luac.o print.o liblua.a ${LDFLAGS} || error "Failed to link luac"
}
build_shared_library() {
log "Building shared library..."
compile_objects "$CFLAGS_SHARED" "shared"
# Create list of object files
local obj_files=()
for src in "${ALL_SOURCE_FILES[@]}"; do
obj_files+=("${src}.o")
done
# Create shared library
gcc -shared -o "liblua.so.${LUA_VERSION}" "${obj_files[@]}" ${LDFLAGS} \
|| error "Failed to create shared library"
}
create_install_directories() {
log "Creating installation directories..."
[[ -d "$INSTALL_PREFIX" ]] && rm -rf "$INSTALL_PREFIX"
local dirs=(
"${INSTALL_PREFIX}/bin"
"${INSTALL_PREFIX}/include"
"${INSTALL_PREFIX}/lib"
"${INSTALL_PREFIX}/man/man1"
"${INSTALL_PREFIX}/share/lua/${LUA_MAJOR_VERSION}"
"${INSTALL_PREFIX}/lib/lua/${LUA_MAJOR_VERSION}"
)
for dir in "${dirs[@]}"; do
mkdir -p "$dir" || error "Failed to create directory: $dir"
done
}
install_static_build() {
log "Installing static build..."
cd "${PWD_LOCAL}/${LUA_DIR}/src" || error "Failed to enter source directory"
# Install binaries
install -p -m 0755 lua luac "${INSTALL_PREFIX}/bin/" \
|| error "Failed to install binaries"
# Install headers
install -p -m 0644 lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp \
"${INSTALL_PREFIX}/include/" || error "Failed to install headers"
# Install static library
install -p -m 0644 liblua.a "${INSTALL_PREFIX}/lib/" \
|| error "Failed to install static library"
# Install man pages
cd "${PWD_LOCAL}/${LUA_DIR}/doc" || error "Failed to enter doc directory"
install -p -m 0644 lua.1 luac.1 "${INSTALL_PREFIX}/man/man1/" \
|| error "Failed to install man pages"
}
install_shared_library() {
log "Installing shared library..."
cd "${PWD_LOCAL}/${LUA_DIR}/src" || error "Failed to enter source directory"
# Install shared library
install -p -m 0644 "liblua.so.${LUA_VERSION}" "${INSTALL_PREFIX}/lib/" \
|| error "Failed to install shared library"
# Create symbolic links
cd "${INSTALL_PREFIX}/lib" || error "Failed to enter lib directory"
ln -sf "liblua.so.${LUA_VERSION}" "liblua.so" \
|| error "Failed to create symlink liblua.so"
ln -sf "liblua.so.${LUA_VERSION}" "liblua.so.${LUA_MAJOR_VERSION}" \
|| error "Failed to create symlink liblua.so.${LUA_MAJOR_VERSION}"
}
# ============================================================================
# Main Execution
# ============================================================================
main() {
log "Starting Lua ${LUA_VERSION} build process..."
# Download and extract
download_lua
extract_lua
# Build static version
build_static_library
# Create installation directories
create_install_directories
# Install static build
install_static_build
# Build and install shared library
build_shared_library
install_shared_library
log "Build completed successfully!"
log "Lua ${LUA_VERSION} installed to: ${INSTALL_PREFIX}"
log ""
log "To use this version, add to your PATH:"
log " export PATH=\"${INSTALL_PREFIX}/bin:\$PATH\""
}
# Run main function
main
exit 0
#!/usr/bin/env bash
#
# build-lua-5.4.6.sh - Build Lua 5.4.6 from source with static and shared libraries
# Author: Ivan Lopes
# Description: Downloads, compiles and installs Lua 5.4.6 into ~/.luaenv/versions/
# Includes pkg-config support
#
set -e # Exit on error
# ============================================================================
# Configuration
# ============================================================================
readonly LUA_VERSION="5.4.6"
readonly LUA_MAJOR_VERSION="${LUA_VERSION%.?}"
readonly LUA_ARCHIVE="lua-${LUA_VERSION}.tar.gz"
readonly LUA_DIR="lua-${LUA_VERSION}"
readonly LUA_URL="https://www.lua.org/ftp/${LUA_ARCHIVE}"
readonly INSTALL_PREFIX="${HOME}/.luaenv/versions/${LUA_VERSION}"
readonly PWD_LOCAL="$PWD"
# Compiler flags
readonly CFLAGS_STATIC="-O2 -Wall -Wextra -DLUA_COMPAT_5_3 -DLUA_USE_LINUX"
readonly CFLAGS_SHARED="-fPIC -O2 -Wall -Wextra -DLUA_COMPAT_5_3 -DLUA_USE_LINUX"
readonly LDFLAGS="-lm -Wl,-E -ldl"
# Lua 5.4 core source files (order matters for linking)
readonly LUA_CORE_FILES=(
lapi lcode lctype ldebug ldo ldump lfunc lgc llex lmem
lobject lopcodes lparser lstate lstring ltable ltm lundump lvm lzio
)
# Lua 5.4 library source files (note: new lcorolib and lutf8lib)
readonly LUA_LIB_FILES=(
lauxlib lbaselib lcorolib ldblib liolib lmathlib
loadlib loslib lstrlib ltablib lutf8lib linit
)
# Combine all object files
readonly ALL_SOURCE_FILES=("${LUA_CORE_FILES[@]}" "${LUA_LIB_FILES[@]}")
# ============================================================================
# Helper Functions
# ============================================================================
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}
error() {
echo "[ERROR] $*" >&2
exit 1
}
# ============================================================================
# Build Functions
# ============================================================================
download_lua() {
log "Checking for Lua source archive..."
if [[ -f "$LUA_ARCHIVE" ]]; then
log "Archive already exists: $LUA_ARCHIVE"
return 0
fi
log "Downloading Lua ${LUA_VERSION}..."
wget "$LUA_URL" || error "Failed to download Lua"
}
extract_lua() {
log "Cleaning up old source directory..."
[[ -d "$LUA_DIR" ]] && rm -rf "$LUA_DIR"
log "Extracting source archive..."
tar xzf "$LUA_ARCHIVE" || error "Failed to extract archive"
}
compile_objects() {
local cflags="$1"
local build_type="$2"
log "Compiling Lua objects ($build_type)..."
cd "${PWD_LOCAL}/${LUA_DIR}/src" || error "Failed to enter source directory"
# Compile all source files
for src in "${ALL_SOURCE_FILES[@]}"; do
gcc ${cflags} -c -o "${src}.o" "${src}.c" || error "Failed to compile ${src}.c"
done
}
build_static_library() {
log "Building static library..."
compile_objects "$CFLAGS_STATIC" "static"
# Create list of object files
local obj_files=()
for src in "${ALL_SOURCE_FILES[@]}"; do
obj_files+=("${src}.o")
done
# Create static library
ar rcu liblua.a "${obj_files[@]}" || error "Failed to create static library"
ranlib liblua.a || error "Failed to index static library"
# Build lua interpreter
log "Building lua interpreter..."
gcc ${CFLAGS_STATIC} -c -o lua.o lua.c || error "Failed to compile lua.c"
gcc -o lua lua.o liblua.a ${LDFLAGS} || error "Failed to link lua"
# Build luac compiler
log "Building luac compiler..."
gcc ${CFLAGS_STATIC} -c -o luac.o luac.c || error "Failed to compile luac.c"
gcc -o luac luac.o liblua.a ${LDFLAGS} || error "Failed to link luac"
}
build_shared_library() {
log "Building shared library..."
compile_objects "$CFLAGS_SHARED" "shared"
# Create list of object files
local obj_files=()
for src in "${ALL_SOURCE_FILES[@]}"; do
obj_files+=("${src}.o")
done
# Create shared library
gcc -shared -o "liblua.so.${LUA_VERSION}" "${obj_files[@]}" ${LDFLAGS} \
|| error "Failed to create shared library"
}
create_install_directories() {
log "Creating installation directories..."
[[ -d "$INSTALL_PREFIX" ]] && rm -rf "$INSTALL_PREFIX"
local dirs=(
"${INSTALL_PREFIX}/bin"
"${INSTALL_PREFIX}/include"
"${INSTALL_PREFIX}/lib"
"${INSTALL_PREFIX}/man/man1"
"${INSTALL_PREFIX}/share/lua/${LUA_MAJOR_VERSION}"
"${INSTALL_PREFIX}/lib/lua/${LUA_MAJOR_VERSION}"
"${INSTALL_PREFIX}/pkgconfig"
)
for dir in "${dirs[@]}"; do
mkdir -p "$dir" || error "Failed to create directory: $dir"
done
}
install_static_build() {
log "Installing static build..."
cd "${PWD_LOCAL}/${LUA_DIR}/src" || error "Failed to enter source directory"
# Install binaries
install -p -m 0755 lua luac "${INSTALL_PREFIX}/bin/" \
|| error "Failed to install binaries"
# Install headers (note: lua.hpp instead of ../etc/lua.hpp in 5.4)
install -p -m 0644 lua.h luaconf.h lualib.h lauxlib.h lua.hpp \
"${INSTALL_PREFIX}/include/" || error "Failed to install headers"
# Install static library
install -p -m 0644 liblua.a "${INSTALL_PREFIX}/lib/" \
|| error "Failed to install static library"
# Install man pages
cd "${PWD_LOCAL}/${LUA_DIR}/doc" || error "Failed to enter doc directory"
install -p -m 0644 lua.1 luac.1 "${INSTALL_PREFIX}/man/man1/" \
|| error "Failed to install man pages"
}
install_shared_library() {
log "Installing shared library..."
cd "${PWD_LOCAL}/${LUA_DIR}/src" || error "Failed to enter source directory"
# Install shared library
install -p -m 0644 "liblua.so.${LUA_VERSION}" "${INSTALL_PREFIX}/lib/" \
|| error "Failed to install shared library"
# Create symbolic links
cd "${INSTALL_PREFIX}/lib" || error "Failed to enter lib directory"
ln -sf "liblua.so.${LUA_VERSION}" "liblua.so" \
|| error "Failed to create symlink liblua.so"
ln -sf "liblua.so.${LUA_VERSION}" "liblua.so.${LUA_MAJOR_VERSION}" \
|| error "Failed to create symlink liblua.so.${LUA_MAJOR_VERSION}"
}
create_pkgconfig() {
log "Creating pkg-config file..."
local pkgconfig_file="${INSTALL_PREFIX}/pkgconfig/lua.pc"
cat > "$pkgconfig_file" <<EOF
# lua.pc -- pkg-config data for Lua
# vars from install Makefile
# grep '^V=' ../Makefile
V=${LUA_MAJOR_VERSION}
# grep '^R=' ../Makefile
R=${LUA_VERSION}
# grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/'
prefix=${INSTALL_PREFIX}
INSTALL_BIN=\${prefix}/bin
INSTALL_INC=\${prefix}/include
INSTALL_LIB=\${prefix}/lib
INSTALL_MAN=\${prefix}/man/man1
INSTALL_LMOD=\${prefix}/share/lua/\${V}
INSTALL_CMOD=\${prefix}/lib/lua/\${V}
# canonical vars
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include
Name: Lua
Description: An Extensible Extension Language
Version: \${R}
Requires:
Libs: -L\${libdir} -llua -lm
Cflags: -I\${includedir}
# (end of lua.pc)
EOF
log "pkg-config file created at: $pkgconfig_file"
}
# ============================================================================
# Main Execution
# ============================================================================
main() {
log "Starting Lua ${LUA_VERSION} build process..."
# Download and extract
download_lua
extract_lua
# Build static version
build_static_library
# Create installation directories
create_install_directories
# Install static build
install_static_build
# Build and install shared library
build_shared_library
install_shared_library
# Create pkg-config file
create_pkgconfig
log "Build completed successfully!"
log "Lua ${LUA_VERSION} installed to: ${INSTALL_PREFIX}"
log ""
log "To use this version, add to your environment:"
log " export PATH=\"${INSTALL_PREFIX}/bin:\$PATH\""
log " export PKG_CONFIG_PATH=\"${INSTALL_PREFIX}/pkgconfig:\$PKG_CONFIG_PATH\""
log ""
log "Test installation:"
log " ${INSTALL_PREFIX}/bin/lua -v"
log " pkg-config --modversion lua"
}
# Run main function
main
exit 0
#!/usr/bin/env bash
#
# build-luajit-2.1-dev.sh - Build LuaJIT from source
# Author: Ivan Lopes
# Description: Clones, compiles and installs LuaJIT 2.1 development version
# into ~/.luaenv/versions/ with pkg-config support
#
set -e # Exit on error
# ============================================================================
# Configuration
# ============================================================================
readonly LUAJIT_VERSION="luajit-2.1-dev"
readonly LUAJIT_DIR="LuaJIT"
readonly LUAJIT_ARCHIVE="${LUAJIT_VERSION}.tgz"
readonly LUAJIT_REPO="https://github.com/LuaJIT/LuaJIT.git"
readonly INSTALL_PREFIX="${HOME}/.luaenv/versions/${LUAJIT_VERSION}"
readonly PWD_LOCAL="$PWD"
# Build configuration
readonly BUILD_TARGET="linux"
readonly BUILD_FLAGS="MYCFLAGS=\"-fPIC\""
# pkg-config metadata
readonly LUAJIT_MAJOR_VERSION="2"
readonly LUAJIT_MINOR_VERSION="1"
readonly LUAJIT_RELEASE="ROLLING"
readonly LUAJIT_ABI_VERSION="5.1"
# ============================================================================
# Helper Functions
# ============================================================================
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}
error() {
echo "[ERROR] $*" >&2
exit 1
}
# ============================================================================
# Build Functions
# ============================================================================
clone_luajit() {
log "Checking for LuaJIT source..."
# Check if archive already exists
if [[ -f "$LUAJIT_ARCHIVE" ]]; then
log "Archive already exists: $LUAJIT_ARCHIVE"
return 0
fi
# Clone repository
log "Cloning LuaJIT repository..."
if [[ -d "$LUAJIT_DIR" ]]; then
log "Removing existing directory: $LUAJIT_DIR"
rm -rf "$LUAJIT_DIR"
fi
git clone "$LUAJIT_REPO" || error "Failed to clone LuaJIT repository"
# Create archive for future use
log "Creating archive: $LUAJIT_ARCHIVE"
tar czf "$LUAJIT_ARCHIVE" "$LUAJIT_DIR" || error "Failed to create archive"
}
extract_luajit() {
log "Cleaning up old source directory..."
[[ -d "$LUAJIT_DIR" ]] && rm -rf "$LUAJIT_DIR"
log "Extracting source archive..."
tar xzf "$LUAJIT_ARCHIVE" || error "Failed to extract archive"
}
build_luajit() {
log "Building LuaJIT..."
cd "${PWD_LOCAL}/${LUAJIT_DIR}" || error "Failed to enter LuaJIT directory"
# Build with position-independent code flag
eval make ${BUILD_FLAGS} ${BUILD_TARGET} || error "Failed to build LuaJIT"
}
install_luajit() {
log "Installing LuaJIT..."
cd "${PWD_LOCAL}/${LUAJIT_DIR}" || error "Failed to enter LuaJIT directory"
# Clean up existing installation
if [[ -d "$INSTALL_PREFIX" ]]; then
log "Removing existing installation: $INSTALL_PREFIX"
rm -rf "$INSTALL_PREFIX"
fi
# Install to custom prefix
make PREFIX="$INSTALL_PREFIX" install || error "Failed to install LuaJIT"
}
create_pkgconfig_directory() {
log "Creating pkg-config directory..."
local pkgconfig_dir="${INSTALL_PREFIX}/pkgconfig"
mkdir -p "$pkgconfig_dir" || error "Failed to create pkg-config directory"
}
create_pkgconfig() {
log "Creating pkg-config file..."
local pkgconfig_file="${INSTALL_PREFIX}/pkgconfig/lua.pc"
cat > "$pkgconfig_file" <<EOF
# Package information for LuaJIT to be used by pkg-config.
majver=${LUAJIT_MAJOR_VERSION}
minver=${LUAJIT_MINOR_VERSION}
relver=${LUAJIT_RELEASE}
version=\${majver}.\${minver}.\${relver}
abiver=${LUAJIT_ABI_VERSION}
prefix=${INSTALL_PREFIX}
multilib=lib
exec_prefix=\${prefix}
libdir=\${exec_prefix}/\${multilib}
libname=luajit-\${abiver}
includedir=\${prefix}/include/luajit-\${majver}.\${minver}
INSTALL_LMOD=\${prefix}/share/lua/\${abiver}
INSTALL_CMOD=\${prefix}/\${multilib}/lua/\${abiver}
Name: LuaJIT
Description: Just-in-time compiler for Lua
URL: https://luajit.org
Version: \${version}
Requires:
Libs: -L\${libdir} -l\${libname}
Libs.private: -Wl,-E -lm -ldl
Cflags: -I\${includedir}
EOF
log "pkg-config file created at: $pkgconfig_file"
}
verify_installation() {
log "Verifying installation..."
local luajit_binary="${INSTALL_PREFIX}/bin/luajit"
if [[ ! -x "$luajit_binary" ]]; then
error "LuaJIT binary not found or not executable: $luajit_binary"
fi
# Get version information
local version_output
version_output=$("$luajit_binary" -v 2>&1 | head -n1)
log "Installed version: $version_output"
}
# ============================================================================
# Main Execution
# ============================================================================
main() {
log "Starting LuaJIT ${LUAJIT_VERSION} build process..."
# Clone or use cached archive
clone_luajit
# Extract source
extract_luajit
# Build LuaJIT
build_luajit
# Install to custom prefix
install_luajit
# Create pkg-config support
create_pkgconfig_directory
create_pkgconfig
# Verify installation
verify_installation
log "Build completed successfully!"
log "LuaJIT ${LUAJIT_VERSION} installed to: ${INSTALL_PREFIX}"
log ""
log "To use this version, add to your environment:"
log " export PATH=\"${INSTALL_PREFIX}/bin:\$PATH\""
log " export LD_LIBRARY_PATH=\"${INSTALL_PREFIX}/lib:\$LD_LIBRARY_PATH\""
log " export PKG_CONFIG_PATH=\"${INSTALL_PREFIX}/pkgconfig:\$PKG_CONFIG_PATH\""
log ""
log "Test installation:"
log " ${INSTALL_PREFIX}/bin/luajit -v"
log " pkg-config --modversion lua"
log ""
log "Note: This is a development version (${LUAJIT_RELEASE})"
}
# Run main function
main
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment