summaryrefslogtreecommitdiff
path: root/build/gen_panto_so_embed.zig
blob: 37b8329be9e56e6f13695942d667cc5931f17085 (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
//! Build-time codegen: emit a Zig module exposing the compiled
//! `libpanto-lua` shared object (`panto.so`) as embedded bytes. Bootstrap
//! writes these to `<tree>/lib/lua/<short>/panto.so` on first run so the
//! embedded VM's `require('panto')` resolves to the native module on a
//! cold machine with no network — the guaranteed initial module load.
//!
//! Invoked from `build.zig`:
//!
//!     gen-panto-so-embed <embed-name> <out-file>
//!
//! `<embed-name>` is the filename the generated module `@embedFile`s; the
//! actual `.so` is materialized alongside the generated file under that
//! name via `addCopyFile` so `@embedFile` can reach it.

const std = @import("std");

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var args = init.minimal.args.iterate();
    defer args.deinit();
    _ = args.next();
    const embed_name = args.next() orelse return error.MissingEmbedName;
    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.print(
        \\//! Auto-generated. Do not edit. See build/gen_panto_so_embed.zig.
        \\
        \\/// The compiled `libpanto-lua` shared object, embedded so the
        \\/// bootstrap can stage it onto the embedded VM's `package.cpath`.
        \\pub const bytes: []const u8 = @embedFile("{s}");
        \\
    , .{embed_name});

    try w.flush();
}