Skip to content

Instantly share code, notes, and snippets.

@greatwolf
Last active September 9, 2024 05:50
Show Gist options
  • Save greatwolf/8d8e8c57549166a779d0d918d9199932 to your computer and use it in GitHub Desktop.
Save greatwolf/8d8e8c57549166a779d0d918d9199932 to your computer and use it in GitHub Desktop.
Building and Packaging Lua from source using MinGW | w64devkit

Compile Lua src

Start MSYS/MinGW32 shell

msys64/msys2_shell.cmd -mingw32

or

w64devkit.exe

Go into base directory and make

cd /<driveletter>/lua-5.<version>
make mingw

Package Lua bin

Check make variables are correct

make echo INSTALL_TOP=../lua-$\(R\) TO_BIN="lua*.exe lua5*.dll"

Put build outputs into proper directory structure suitable for deployment

make install INSTALL_TOP=../lua-$\(R\) TO_BIN="lua*.exe lua5*.dll"

Cleaning build output

make clean LUA_T="lua*.exe lua5*.dll"

Fix defective require search path

Setting up proper require path so scripts can find its own modules. Especially necessary when current working directory differs from where the script's located.

Create lua-5.<version>/bin/lua/env.lua:

-- env.lua
local require_ = require

require = function(...)
  local arg = _G.arg
  local main = arg and arg[0]:match '(.+[/\\])[%w_]+%.lua$'

  if main then
    local dirslash = package.config:sub(1, 1)
    package.path  = main .. "?.lua;"
                  .. main .. "?" .. dirslash .. "init.lua;"
                  .. package.path
    package.cpath = main .. "?.dll;"
                  .. package.cpath
  end

  require = require_
  return require(...)
end

Set up cmd environment

Add Lua location to PATH environment

set PATH=lua-5.<version>\bin;%PATH%

Set up Linux-like alias with doskey

doskey lua=lua -l env $*

Associate Lua extension

assoc .lua=luafile
ftype luafile="lua-5.<version>\bin\lua.exe" -l env %1 %*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment