aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/libghostty-vt/pkg/macos/build.zig
blob: 5569baad0bfbd593324507cd9647ea4d0c3307ec (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const std = @import("std");
const builtin = @import("builtin");
const apple_sdk = @import("apple_sdk");

const Framework = struct {
    const Tag = enum { all, macos };

    tag: Tag,
    name: []const u8,
    headers: []const []const u8,
};

const frameworks = [_]Framework{
    .{ .tag = .all, .name = "CoreFoundation", .headers = &.{"CoreFoundation.h"} },
    .{ .tag = .all, .name = "CoreGraphics", .headers = &.{"CoreGraphics.h"} },
    .{ .tag = .all, .name = "CoreText", .headers = &.{"CoreText.h"} },
    .{ .tag = .all, .name = "CoreVideo", .headers = &.{ "CoreVideo.h", "CVPixelBuffer.h" } },
    .{ .tag = .all, .name = "QuartzCore", .headers = &.{"CALayer.h"} },
    .{ .tag = .all, .name = "IOSurface", .headers = &.{"IOSurfaceRef.h"} },
    .{ .tag = .macos, .name = "Carbon", .headers = &.{"Carbon.h"} },
};

const extra_headers = [_][]const u8{
    "dispatch/dispatch.h",
    "os/log.h",
    "os/signpost.h",
};

const framework_header_fmt = "#include <{s}/{s}>\n";
const extra_header_fmt = "#include <{s}>\n";

fn cSourceLen(tag: Framework.Tag) usize {
    var len: usize = 0;
    for (frameworks) |framework| {
        if (tag != .macos and framework.tag == .macos) continue;
        for (framework.headers) |h| len += std.fmt.count(framework_header_fmt, .{ framework.name, h });
    }
    for (extra_headers) |h| len += std.fmt.count(extra_header_fmt, .{h});
    return len;
}

fn genCSource(comptime tag: Framework.Tag) [cSourceLen(tag):0]u8 {
    const len = cSourceLen(tag);
    var buf: [len:0]u8 = undefined;
    var writer: std.Io.Writer = .fixed(&buf);
    for (frameworks) |framework| {
        if (tag != .macos and framework.tag == .macos) continue;
        for (framework.headers) |h| writer.print(framework_header_fmt, .{ framework.name, h }) catch unreachable;
    }
    for (extra_headers) |h| writer.print(extra_header_fmt, .{h}) catch unreachable;
    buf[len] = 0;
    return buf;
}

const c_source_macos = genCSource(.macos);
const c_source_other = genCSource(.all);

fn linkFrameworks(tag: Framework.Tag, module: *std.Build.Module) !void {
    for (frameworks) |framework| {
        if (tag != .macos and framework.tag == .macos) continue;
        module.linkFramework(framework.name, .{});
    }
}

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

    const module = b.addModule("macos", .{
        .root_source_file = b.path("main.zig"),
        .target = target,
        .optimize = optimize,
    });

    translate: {
        const translate_c = b.lazyImport(@This(), "translate_c") orelse break :translate;
        const translate_c_dep = b.lazyDependency("translate_c", .{}) orelse break :translate;
        const macos_c: translate_c.Translator = .init(translate_c_dep, .{
            .c_source_file = b.addWriteFiles().add(
                "macos_c.h",
                if (target.result.os.tag == .macos) &c_source_macos else &c_source_other,
            ),
            .target = target,
            .optimize = optimize,
            .libc_file = if (target.result.os.tag.isDarwin()) libc_file: {
                switch (try apple_sdk.pathsForTarget(b, target.result)) {
                    inline else => |paths| break :libc_file paths.libc,
                }
            } else null,
        });

        // Blocks need to be enabled to use MacOS headers
        macos_c.run.addArg("-fblocks");

        module.addImport("macos_c", macos_c.mod);
    }

    const lib = b.addLibrary(.{
        .name = "macos",
        .root_module = b.createModule(.{
            .target = target,
            .optimize = optimize,
        }),
        .linkage = .static,
    });

    lib.root_module.addCSourceFile(.{
        .file = b.path("os/zig_macos.c"),
        .flags = &.{"-std=c99"},
    });
    lib.root_module.addCSourceFile(.{
        .file = b.path("text/ext.c"),
    });

    inline for (.{ lib.root_module, module }) |mod| {
        try linkFrameworks(if (target.result.os.tag == .macos) .macos else .all, mod);
    }
    try apple_sdk.addPaths(b, lib);
    b.installArtifact(lib);

    {
        const test_exe = b.addTest(.{
            .name = "test",
            .root_module = b.createModule(.{
                .root_source_file = b.path("main.zig"),
                .target = target,
                .optimize = optimize,
            }),
        });
        if (target.result.os.tag.isDarwin()) {
            try apple_sdk.addPaths(b, test_exe);
        }
        test_exe.root_module.linkLibrary(lib);

        var it = module.import_table.iterator();
        while (it.next()) |entry| {
            test_exe.root_module.addImport(
                entry.key_ptr.*,
                entry.value_ptr.*,
            );
        }

        b.installArtifact(test_exe);

        const tests_run = b.addRunArtifact(test_exe);
        const test_step = b.step("test", "Run tests");
        test_step.dependOn(&tests_run.step);
    }
}