aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/libghostty-vt/src/build/combine_archives.zig
blob: 3df5ed0e426f5c226c02b1bf0fa09587ff37ec6f (plain) (blame)
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
//! Build tool that combines multiple static archives into a single fat
//! archive using an MRI script piped to `zig ar -M`.
//!
//! MRI scripts require stdin piping (`ar -M < script`), which can't be
//! expressed as a single command in the zig build system's RunStep. The
//! previous approach used `/bin/sh -c` to do the piping, but that isn't
//! available on Windows. This tool handles both the script generation
//! and the piping in a single cross-platform executable.
//!
//! Usage: combine_archives <zig_exe> <output.a> <input1.a> [input2.a ...]

const std = @import("std");

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

    const args = try init.minimal.args.toSlice(alloc);
    if (args.len < 4) {
        std.log.err("usage: combine_archives <zig_exe> <output> <input...>", .{});
        std.process.exit(1);
    }

    const zig_exe = args[1];
    const output_path = args[2];
    const inputs = args[3..];

    // Build the MRI script.
    var script: std.ArrayListUnmanaged(u8) = .empty;
    try script.appendSlice(alloc, "CREATE ");
    try script.appendSlice(alloc, output_path);
    try script.append(alloc, '\n');
    for (inputs) |input| {
        try script.appendSlice(alloc, "ADDLIB ");
        try script.appendSlice(alloc, input);
        try script.append(alloc, '\n');
    }
    try script.appendSlice(alloc, "SAVE\nEND\n");

    var child = try std.process.spawn(init.io, .{
        .argv = &.{ zig_exe, "ar", "-M" },
        .stdin = .pipe,
        .stdout = .inherit,
        .stderr = .inherit,
    });

    try child.stdin.?.writeStreamingAll(init.io, script.items);
    child.stdin.?.close(init.io);
    child.stdin = null;

    const term = try child.wait(init.io);
    if (term.exited != 0) {
        std.log.err("zig ar -M exited with code {d}", .{term.exited});
        std.process.exit(1);
    }
}