summaryrefslogtreecommitdiff
path: root/build/gen_lua_headers_embed.zig
blob: 8c58bf483f5b5ee1a6a2e0656aec26f9eece325d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Build-time codegen: emit a Zig module exposing every Lua public
//! header from the Lua source tarball as embedded bytes. Bootstrap
//! writes these to `<data home>/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 <lua-src-dir> <out-file>
//!
//! `<lua-src-dir>` 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();
}