summaryrefslogtreecommitdiff
path: root/build.zig
blob: 7d64f995363ba1eac9b0da94384a166627ab9f3c (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const std = @import("std");

pub fn build(b: *std.Build) void {
    const optimize = b.standardOptimizeOption(.{});
    const target = b.standardTargetOptions(.{});

    // ghostty-vt and vaxis both depend on uucode, but Zig 0.16 forbids the
    // same source file from belonging to two modules, so they can't each have
    // their own uucode instance. We build a single uucode module here (with our
    // own uucode_config.zig) and hand it to both.
    //
    // Our config matches ghostty's exactly except the tables are `.unpacked`;
    // see uucode_config.zig for why that's required on Zig 0.16.
    const uucode_config = b.path("uucode_config.zig");
    const uucode_tables = b.dependency("uucode", .{
        .target = target,
        .optimize = optimize,
        .build_config_path = uucode_config,
    }).namedLazyPath("tables.zig");
    const uucode_mod = b.dependency("uucode", .{
        .target = target,
        .optimize = optimize,
        .build_config_path = uucode_config,
        .tables_path = uucode_tables,
    }).module("uucode");

    // vaxis is built with `external_uucode = true` so it skips its built-in
    // uucode dependency and lets us wire one in.
    const vaxis_dep = b.dependency("vaxis", .{
        .target = target,
        .optimize = optimize,
        .external_uucode = true,
    });
    const vaxis_mod = vaxis_dep.module("vaxis");

    // Accessing ghostty's modules runs its build(), which (because vaxis is a
    // shared dependency, deduplicated to the same module) wires ghostty's own
    // uucode into vaxis. We override both with ours below; order matters, so
    // ghostty must be touched before the final vaxis uucode override.
    const ghostty_vt: ?*std.Build.Module = if (b.lazyDependency("ghostty", .{
        // Setting simd to false will force a pure static build that doesn't
        // even require libc, but it has a significant performance penalty. If
        // your embedding app requires libc anyway, you should always keep simd
        // enabled. We need libc anyway (forkpty), so we keep it on.
        // .simd = false,
    })) |dep| blk: {
        const mod = dep.module("ghostty-vt");
        mod.addImport("uucode", uucode_mod);
        break :blk mod;
    } else null;

    // Override uucode on vaxis last, so it wins over ghostty's build() above.
    vaxis_mod.addImport("uucode", uucode_mod);

    // Wires the shared imports onto a root module. We link libc for `forkpty`
    // (see pty.zig).
    const wire = struct {
        fn apply(
            mod: *std.Build.Module,
            vaxis_m: *std.Build.Module,
            ghostty_m: ?*std.Build.Module,
        ) void {
            mod.link_libc = true;
            mod.addImport("vaxis", vaxis_m);
            if (ghostty_m) |g| mod.addImport("ghostty-vt", g);
        }
    }.apply;

    // The app.
    const exe_mod = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("main.zig"),
    });
    wire(exe_mod, vaxis_mod, ghostty_vt);
    const exe = b.addExecutable(.{ .name = "pardes", .root_module = exe_mod });
    b.installArtifact(exe);

    const run_exe = b.addRunArtifact(exe);
    const run = b.step("run", "descend to the pardes");
    run.dependOn(&run_exe.step);

    // Deterministic pipeline tests (pty -> ghostty-vt -> grid + input encode).
    const test_mod = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("tests.zig"),
    });
    wire(test_mod, vaxis_mod, ghostty_vt);
    const tests = b.addTest(.{ .root_module = test_mod });
    const run_tests = b.addRunArtifact(tests);
    const test_step = b.step("test", "run the pipeline + smoke tests");
    test_step.dependOn(&run_tests.step);

    // End-to-end smoke: drives the real `pardes` binary under a pty. Only needs
    // libc (forkpty); the pardes path is passed in as argv[1].
    const smoke_mod = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("smoke.zig"),
    });
    smoke_mod.link_libc = true;
    const smoke = b.addExecutable(.{ .name = "smoke", .root_module = smoke_mod });
    const run_smoke = b.addRunArtifact(smoke);
    run_smoke.addArtifactArg(exe); // builds pardes first, passes its path
    test_step.dependOn(&run_smoke.step);
}