summaryrefslogtreecommitdiff
path: root/build.zig
blob: e8ad6b37fe4ee900bdc0fb2705d04ec2b8aa2ad5 (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
const std = @import("std");

/// The one place the version is written. `mise run version` rewrites it.
const version = @import("build.zig.zon").version;

/// The oldest macOS the app says it runs on, in the binary and in Info.plist.
const min_macos: std.SemanticVersion = .{ .major = 13, .minor = 0, .patch = 0 };

pub fn build(b: *std.Build) void {
    // Not the native target: that would require whatever macOS the build machine runs.
    const target = b.resolveTargetQuery(.{
        .cpu_arch = .aarch64, // see src/objc.zig
        .os_tag = .macos,
        .os_version_min = .{ .semver = min_macos },
    });
    const optimize = b.standardOptimizeOption(.{});

    const mod = b.createModule(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    // Zig looks for the SDK by itself only when the target is native.
    const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.io, &target.result) orelse
        @panic("no macOS SDK: run xcode-select --install");
    mod.addFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "System/Library/Frameworks" }) });
    mod.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "usr/lib" }) });
    mod.linkFramework("AppKit", .{});
    mod.addImport("panto", b.dependency("libpantograph", .{ .target = target, .optimize = optimize }).module("panto"));
    const exe = b.addExecutable(.{ .name = "write_away", .root_module = mod });

    // AppKit's own About panel reads the version from Info.plist.
    var plist: []const u8 = @embedFile("Info.plist");
    for ([_][2][]const u8{
        .{ "@VERSION@", version },
        .{ "@MIN_MACOS@", b.fmt("{f}", .{min_macos}) },
    }) |sub| plist = std.mem.replaceOwned(u8, b.allocator, plist, sub[0], sub[1]) catch @panic("OOM");

    // Installed as an app bundle: zig-out/Write_Away.app
    const bundle = "Write_Away.app/Contents";
    const install = b.getInstallStep();
    const bin = b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = bundle ++ "/MacOS" } } });
    install.dependOn(&bin.step);
    install.dependOn(&b.addInstallFile(b.addWriteFiles().add("Info.plist", plist), bundle ++ "/Info.plist").step);
    install.dependOn(&b.addInstallFile(b.path("assets/Write_Away.icns"), bundle ++ "/Resources/Write_Away.icns").step);

    // From inside the bundle, or the app has no Info.plist, and so no version or icon.
    const run = b.addSystemCommand(&.{b.getInstallPath(.prefix, bundle ++ "/MacOS/write_away")});
    run.step.dependOn(install);
    if (b.args) |args| run.addArgs(args);
    b.step("run", "Run the editor (zig build run -- file.md)").dependOn(&run.step);

    // Format/model checks plus the real app driven through its commands with no window shown.
    const smoke = b.addRunArtifact(exe);
    smoke.setEnvironmentVariable("WRITE_AWAY_SELFTEST", "1");
    const test_step = b.step("test", "Run the checks");
    for ([_][]const u8{ "src/doc.zig", "src/markdown.zig", "src/lab.zig", "src/lab_ui.zig" }) |path| {
        const unit = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path(path), .target = target }) });
        test_step.dependOn(&b.addRunArtifact(unit).step);
    }
    test_step.dependOn(&smoke.step);
}