Skip to content

Instantly share code, notes, and snippets.

@rfl890
Created December 13, 2023 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfl890/d4928b8ff2f3052db456c097b681e63e to your computer and use it in GitHub Desktop.
Save rfl890/d4928b8ff2f3052db456c097b681e63e to your computer and use it in GitHub Desktop.
Internal Script
-- Change to the path of lua51.lib/libluajit-2.1.a/whatever LuaJIT library you link against
local lua_slib = "\"C:\\Users\\user\\bin\\luajit\\lib\\lua51.lib\""
local CC = "clang -O2"
local stub_code = [[#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <luajit.h>
#include <stdlib.h>
#include <windows.h>
int main(void) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_getglobal(L, "require");
lua_pushliteral(L, "main");
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
MessageBox(NULL, lua_tostring(L, -1),"Application Error", MB_OK | MB_ICONERROR);
exit(EXIT_FAILURE);
}
}]]
local function run(cmd)
print(cmd)
os.execute(cmd)
end
local function writeFile(fname, str)
local f, err = io.open(fname, "wb")
if f == nil then error(err) end
f:write(str)
f:close()
end
writeFile("main.c", stub_code)
local modules = {}
for file in io.popen("dir /b src"):lines() do
table.insert(modules, file)
end
for _, modulename in ipairs(modules) do
local outname = modulename:sub(1, -5) .. ".o"
run("copy /Y src\\" .. modulename .. " .")
run("luajit -b " .. modulename .. " " .. outname)
run("del " .. modulename)
modules[_] = outname
end
run(CC .. " main.c " .. lua_slib .. " " .. table.concat(modules, " "))
run("del " .. table.concat(modules, " "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment