//! Build-time codegen: emit a Zig module exposing every Lua public //! header from the Lua source tarball as embedded bytes. Bootstrap //! writes these to `/rocks/lua-X.Y.Z/include/` on first run //! so luarocks can compile C rocks against them. //! //! Invoked from `build.zig`: //! //! gen-lua-headers-embed //! //! `` is the `src/` directory of the Lua tarball. const std = @import("std"); const headers = [_][]const u8{ "lua.h", "luaconf.h", "lualib.h", "lauxlib.h", // hpp is the C++-friendly include shim shipped upstream; some C // rocks reference it. "lua.hpp", }; pub fn main(init: std.process.Init) !void { const arena = init.arena.allocator(); const io = init.io; var args = init.minimal.args.iterate(); defer args.deinit(); _ = args.next(); const embed_prefix = args.next() orelse return error.MissingEmbedPrefix; const src_dir = args.next() orelse return error.MissingSrcDir; const out_path = args.next() orelse return error.MissingOutPath; var out_file = try std.Io.Dir.cwd().createFile(io, out_path, .{}); defer out_file.close(io); var buf: [4096]u8 = undefined; var writer = out_file.writer(io, &buf); const w = &writer.interface; try w.writeAll( \\//! Auto-generated. Do not edit. See build/gen_lua_headers_embed.zig. \\ \\pub const Entry = struct { \\ name: []const u8, \\ contents: []const u8, \\}; \\ \\pub const files: []const Entry = &.{ \\ ); for (headers) |name| { const abs = try std.fs.path.join(arena, &.{ src_dir, name }); // Verify the file exists; emit nothing for missing files (lua.hpp // exists in current Lua releases but we don't want to hard-fail // if the tarball ever drops it). std.Io.Dir.cwd().access(io, abs, .{}) catch continue; const embed_path = try std.fs.path.join(arena, &.{ embed_prefix, name }); try w.print( " .{{ .name = \"{s}\", .contents = @embedFile(\"{s}\") }},\n", .{ name, embed_path }, ); } try w.writeAll("};\n"); try w.flush(); }