Skip to content

Commit

Permalink
Rewrite of LuaJIT build/install
Browse files Browse the repository at this point in the history
- Supports LuaJIT from git and openresty
- Build Windows as documented on LuaJIT. The root Makefile officially
does not support Windows, so do not attempt to use it. This installs
the luajit JIT library in the sanctioned path (bin\lua\jit) on Windows.
Luarocks can pick it up downstream if it chooses to.
  • Loading branch information
jkl1337 committed Feb 20, 2024
1 parent 35bcb06 commit 13678a4
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
luaVersion: ["5.1.5", "5.2.4", "5.3.5", "5.4.1", "luajit-2.0.5", "luajit-2.1.0-beta3", "luajit-openresty", "5.1"]
luaVersion: ["5.1.5", "5.2.4", "5.3.5", "5.4.1", "luajit-2.0.5", "luajit-2.1.0-beta3", "luajit-openresty", "luajit-git", "5.1"]


runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
luaVersion: ["5.1.5", "5.2.4", "5.3.6", "5.4.4", "luajit-2.0.5", "luajit-2.1.0-beta3", "luajit-openresty", "5.1", "5.4"]
luaVersion: ["5.1.5", "5.2.4", "5.3.6", "5.4.4", "luajit-2.0.5", "luajit-2.1.0-beta3", "luajit-openresty", "luajit-git", "5.1", "5.4"]
machineTag: ["ubuntu-latest", "macos-latest", "windows-latest"]

runs-on: ${{ matrix.machineTag }}
Expand All @@ -32,7 +32,7 @@ jobs:
strategy:
fail-fast: false
matrix:
luaVersion: ["5.1.5", "5.2.4", "5.3.6", "5.4.4", "luajit-2.0.5", "luajit-2.1.0-beta3", "luajit-openresty", "5.1", "5.4"]
luaVersion: ["5.1.5", "5.2.4", "5.3.6", "5.4.4", "luajit-2.0.5", "luajit-2.1.0-beta3", "luajit-openresty", "luajit-git", "5.1", "5.4"]
machineTag: ["ubuntu-latest", "macos-latest", "windows-latest"]

runs-on: ${{ matrix.machineTag }}
Expand Down
181 changes: 122 additions & 59 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const VERSION_ALIASES = {
"5.2": "5.2.4",
"5.3": "5.3.6",
"5.4": "5.4.4",
"luajit": "luajit-2.1.0-beta3",
"luajit": "luajit-openresty",
}

const isMacOS = () => (process.platform || "").startsWith("darwin")
Expand All @@ -35,79 +35,128 @@ const processCwd = () => {
return process.cwd().split(path.sep).join(path.posix.sep);
}

async function finish_luajit_install(src, dst, luajit) {
if (isWindows()) {
await fsp.copyFile(pathJoin(src, "lua51.dll"), pathJoin(dst, "bin", "lua51.dll"))

await exec.exec(`ln -s ${luajit} lua.exe`, undefined, {
cwd: pathJoin(dst, "bin")
})
} else {
await exec.exec(`ln -s ${luajit} lua`, undefined, {
cwd: pathJoin(dst, "bin")
})
async function install_files(dstDir, srcDir, files) {
await io.mkdirP(dstDir);
for (const file of files) {
await fsp.copyFile(
pathJoin(srcDir, file),
pathJoin(dstDir, path.posix.basename(file)),
);
}
}

async function install_luajit_openresty(luaInstallPath) {
const buildPath = path.join(process.env["RUNNER_TEMP"], BUILD_PREFIX)
const luaCompileFlags = core.getInput('luaCompileFlags')
async function fetch_luajit(buildPath, luajitVersion) {
const luaExtractPath = pathJoin(buildPath, `LuaJIT-${luajitVersion}`);

await io.mkdirP(buildPath)
const luaSourceTar = await tc.downloadTool(
`https://luajit.org/download/LuaJIT-${luajitVersion}.tar.gz`,
);

await exec.exec("git clone https://github.com/openresty/luajit2.git", undefined, {
cwd: buildPath
})
await io.mkdirP(luaExtractPath);
await tc.extractTar(luaSourceTar, buildPath);

return luaExtractPath;
}

async function fetch_luajit_openresty(buildPath) {
await exec.exec(
"git clone https://github.com/openresty/luajit2.git luajit",
undefined,
{ cwd: buildPath },
);
return pathJoin(buildPath, "luajit");
}

async function fetch_luajit_git(buildPath) {
await exec.exec(
"git clone https://github.com/LuaJIT/LuaJIT luajit",
undefined,
{ cwd: buildPath },
);
return pathJoin(buildPath, "luajit");
}

let finalCompileFlags = "-j"
async function build_luajit_posix(srcPath) {
const luaCompileFlags = core.getInput("luaCompileFlags");
let finalCompileFlags = "-j";

if (isMacOS()) {
finalCompileFlags += " MACOSX_DEPLOYMENT_TARGET=10.15"
finalCompileFlags += " MACOSX_DEPLOYMENT_TARGET=10.15";
}

if (luaCompileFlags) {
finalCompileFlags += ` ${luaCompileFlags}`
finalCompileFlags += ` ${luaCompileFlags}`;
}

await exec.exec(`make ${finalCompileFlags}`, undefined, {
cwd: pathJoin(buildPath, "luajit2")
})
cwd: srcPath,
});
}

async function install_luajit_posix(srcPath, luaInstallPath, exeName) {
await exec.exec(`make -j install PREFIX="${luaInstallPath}"`, undefined, {
cwd: pathJoin(buildPath, "luajit2")
})
cwd: srcPath,
});

await finish_luajit_install(pathJoin(buildPath, "luajit2", "src"), luaInstallPath, "luajit-2.1.0-beta3")
if (exeName === undefined) {
exeName = await fsp.readlink(pathJoin(luaInstallPath, "bin", "luajit"));
}

await fsp.symlink(exeName, pathJoin(luaInstallPath, "bin", "lua"));
}

async function install_luajit(luaInstallPath, luajitVersion) {
const luaExtractPath = pathJoin(process.env["RUNNER_TEMP"], BUILD_PREFIX, `LuaJIT-${luajitVersion}`)
async function build_luajit_windows(srcPath) {
const luaCompileFlags = core.getInput("luaCompileFlags");
let finalCompileFlags = "-j";

const luaCompileFlags = core.getInput('luaCompileFlags')
if (luaCompileFlags) {
finalCompileFlags += ` ${luaCompileFlags}`;
}

const luaSourceTar = await tc.downloadTool(`https://luajit.org/download/LuaJIT-${luajitVersion}.tar.gz`)
await io.mkdirP(luaExtractPath)
await tc.extractTar(luaSourceTar, path.join(process.env["RUNNER_TEMP"], BUILD_PREFIX))
await exec.exec(`make SHELL=cmd.exe ${finalCompileFlags}`, undefined, {
cwd: pathJoin(srcPath, "src"),
});
}

let finalCompileFlags = "-j"
async function install_luajit_windows(srcPath, luaInstallPath) {
const srcDir = pathJoin(srcPath, "src");
const binDir = pathJoin(luaInstallPath, "bin");

if (isMacOS()) {
finalCompileFlags += " MACOSX_DEPLOYMENT_TARGET=10.15"
{
// install bin files
await install_files(binDir, srcDir, ["luajit.exe", "lua51.dll"]);
await fsp.symlink("luajit.exe", pathJoin(binDir, "lua.exe"));
}

if (luaCompileFlags) {
finalCompileFlags += ` ${luaCompileFlags}`
}

await exec.exec(`make ${finalCompileFlags}`, undefined, {
cwd: luaExtractPath
})
{
// install jit library
const jitLibSrcDir = pathJoin(srcDir, "jit");
const jitLibDstDir = pathJoin(binDir, "lua", "jit");

await exec.exec(`make -j install PREFIX="${luaInstallPath}"`, undefined, {
cwd: luaExtractPath
})
await install_files(
jitLibDstDir,
jitLibSrcDir,
(await fsp.readdir(jitLibSrcDir)).filter((file) => !file.startsWith(".")),
);
}

await finish_luajit_install(pathJoin(luaExtractPath, "src"), luaInstallPath, `luajit-${luajitVersion}`)
{
const incDir = pathJoin(luaInstallPath, "include", "luajit-2.1");
const incDirCompat = pathJoin(luaInstallPath, "include", "5.1");
await io.mkdirP(incDir);
await io.mkdirP(incDirCompat);

const incFiles = [
"lua.h",
"lua.hpp",
"lauxlib.h",
"luaconf.h",
"lualib.h",
"luajit.h",
];
await install_files(incDir, srcDir, incFiles);
await install_files(incDirCompat, srcDir, incFiles);
}
}

async function msvc_link(luaExtractPath, linkCmd, outFile, objs) {
Expand All @@ -123,13 +172,6 @@ async function msvc_link(luaExtractPath, linkCmd, outFile, objs) {
}
}

async function install_files(dstDir, srcDir, files) {
await io.mkdirP(dstDir)
for (let file of files) {
await fsp.copyFile(pathJoin(srcDir, file), pathJoin(dstDir, path.posix.basename(file)))
}
}

async function install_plain_lua_windows(luaExtractPath, luaInstallPath, luaVersion) {
const luaCompileFlags = core.getInput('luaCompileFlags')

Expand Down Expand Up @@ -229,14 +271,35 @@ async function install_plain_lua(luaInstallPath, luaVersion) {
})
}

async function install(luaInstallPath, luaVersion) {
if (luaVersion == "luajit-openresty") {
return await install_luajit_openresty(luaInstallPath)
async function install_luajit(luaInstallPath, luajitVersion) {
const buildPath = pathJoin(process.env["RUNNER_TEMP"], BUILD_PREFIX);

await io.mkdirP(buildPath);

let exeName;
let srcPath;
if (luajitVersion === "openresty") {
srcPath = await fetch_luajit_openresty(buildPath);
} else if (luajitVersion === "git") {
srcPath = await fetch_luajit_git(buildPath);
} else {
srcPath = await fetch_luajit(buildPath, luajitVersion);
exeName = `luajit-${luajitVersion}`;
}

if (isWindows()) {
await build_luajit_windows(srcPath);
await install_luajit_windows(srcPath, luaInstallPath);
} else {
await build_luajit_posix(srcPath);
await install_luajit_posix(srcPath, luaInstallPath, exeName);
}
}

async function install(luaInstallPath, luaVersion) {
if (luaVersion.startsWith("luajit-")) {
const luajitVersion = luaVersion.substr("luajit-".length)
return await install_luajit(luaInstallPath, luajitVersion)
const luajitVersion = luaVersion.substr("luajit-".length);
return await install_luajit(luaInstallPath, luajitVersion);
}

return await install_plain_lua(luaInstallPath, luaVersion)
Expand Down

0 comments on commit 13678a4

Please sign in to comment.