slimpack/lib/build.lua
2025-03-08 23:22:10 +02:00

51 lines
1.2 KiB
Lua

local function unresolve_require(paths, f)
for _, pattern in ipairs(paths) do
local path = f:match(pattern);
if path then
return path:gsub("%/", ".");
end
end
error("path " .. f .. " couldn't be resolved to any valid module");
end
local function all_loader(paths, ...)
local files = { ... };
local function coro_entry()
for _, f in ipairs(files) do
local name = unresolve_require(paths, f);
local bytecode = string.dump(assert(load(io.lines(f, 1024), "@" .. f, "t", nil)));
coroutine.yield(("package.preload[%q] = load(%q, %q, 'b');"):format(name, bytecode, f));
end
coroutine.yield("require 'init' (...);");
end
return coroutine.wrap(coro_entry);
end
local function main(root, out, ...)
local paths = { root .. "/(.+).lua", root .. "/(.+)/init.lua" };
for el in package.path:gmatch "[^;]+" do
paths[#paths + 1] = el:gsub("?", "(.+)");
end
local res = string.dump(assert(load(all_loader(paths, ...), "@<entry>", "t", nil)));
local f = assert(io.open(out, "w"));
f:write "#include <stdint.h>\n";
f:write "#define BYTECODE entry_bytecode\n";
f:write "static const uint8_t entry_bytecode[] = { ";
for i = 1, #res do
f:write(string.byte(res, i), ", ");
end
f:write "};";
assert(f:close());
end
main(...);