-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.zig
More file actions
319 lines (284 loc) · 10.4 KB
/
Copy pathbuild.zig
File metadata and controls
319 lines (284 loc) · 10.4 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.Build) void {
// GitHub's macOS runners default the deployment target to the host
// (currently 15.x), which makes release binaries fail to start on older
// macOS versions. Pin a lower default; callers can still override with
// -Dtarget.
const default_target: std.Target.Query = if (builtin.os.tag == .macos) .{
.cpu_arch = builtin.cpu.arch,
.os_tag = .macos,
.os_version_min = .{ .semver = .{ .major = 12, .minor = 0, .patch = 0 } },
} else .{
.os_version_min = .{ .semver = .{ .major = 12, .minor = 0, .patch = 0 } },
};
const target = b.standardTargetOptions(.{
.default_target = default_target,
});
const optimize = b.standardOptimizeOption(.{});
const sdk_root: ?[]const u8 = if (target.result.os.tag == .macos)
findSdkRoot(b)
else
null;
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const mcp_mod = b.createModule(.{
.root_source_file = b.path("src/mcp/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const control_mod = b.createModule(.{
.root_source_file = b.path("src/app/control.zig"),
.target = target,
.optimize = optimize,
});
const env_mod = b.createModule(.{
.root_source_file = b.path("src/env.zig"),
.target = target,
.optimize = optimize,
});
const clock_mod = b.createModule(.{
.root_source_file = b.path("src/clock.zig"),
.target = target,
.optimize = optimize,
});
const posix_util_mod = b.createModule(.{
.root_source_file = b.path("src/posix_util.zig"),
.target = target,
.optimize = optimize,
});
const wake_pipe_mod = b.createModule(.{
.root_source_file = b.path("src/wake_pipe.zig"),
.target = target,
.optimize = optimize,
});
wake_pipe_mod.addImport("posix_util.zig", posix_util_mod);
control_mod.addImport("../env.zig", env_mod);
control_mod.addImport("../clock.zig", clock_mod);
control_mod.addImport("../posix_util.zig", posix_util_mod);
control_mod.addImport("../wake_pipe.zig", wake_pipe_mod);
mcp_mod.addImport("control", control_mod);
mcp_mod.addImport("../env.zig", env_mod);
const assets_mod = b.createModule(.{
.root_source_file = b.path("assets/terminfo.zig"),
.target = target,
.optimize = optimize,
});
exe_mod.addImport("assets", assets_mod);
const c_sdl = addTranslateCModule(b, exe_mod, "c_sdl", "src/c/sdl.h", target, optimize);
const pty_header = switch (target.result.os.tag) {
.macos => "src/c/pty_macos.h",
.freebsd => "src/c/pty_freebsd.h",
else => "src/c/pty_linux.h",
};
_ = addTranslateCModule(b, exe_mod, "c_pty", pty_header, target, optimize);
_ = addTranslateCModule(b, exe_mod, "c_stdlib", "src/c/libc_stdlib.h", target, optimize);
_ = addTranslateCModule(b, exe_mod, "c_time", "src/c/libc_time.h", target, optimize);
if (target.result.os.tag == .macos) {
exe_mod.addCSourceFile(.{ .file = b.path("src/c/libproc.c") });
_ = addTranslateCModule(b, exe_mod, "c_libproc", "src/c/libproc.h", target, optimize);
_ = addTranslateCModule(b, exe_mod, "c_sysctl", "src/c/sysctl.h", target, optimize);
}
if (b.lazyDependency("ghostty", .{
.target = target,
.optimize = optimize,
})) |dep| {
exe_mod.addImport(
"ghostty-vt",
dep.module("ghostty-vt"),
);
}
if (b.lazyDependency("libxev", .{
.target = target,
.optimize = optimize,
})) |dep| {
exe_mod.addImport("xev", dep.module("xev"));
}
if (b.lazyDependency("toml", .{
.target = target,
.optimize = optimize,
})) |dep| {
exe_mod.addImport("toml", dep.module("toml"));
}
const exe = b.addExecutable(.{
.name = "architect",
.root_module = exe_mod,
});
const mcp_exe = b.addExecutable(.{
.name = "architect-mcp",
.root_module = mcp_mod,
});
exe_mod.linkSystemLibrary("SDL3", .{});
exe_mod.linkSystemLibrary("SDL3_ttf", .{});
const framework_path: ?[]const u8 = if (sdk_root) |path|
b.fmt("{s}/System/Library/Frameworks", .{path})
else
null;
addSdlPaths(b, exe_mod, c_sdl, framework_path);
if (target.result.os.tag == .macos) {
if (sdk_root) |path| {
exe_mod.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/usr/lib", .{path}) });
}
exe.headerpad_max_install_names = true;
mcp_exe.headerpad_max_install_names = true;
exe_mod.linkSystemLibrary("proc", .{});
exe_mod.linkSystemLibrary("objc", .{});
exe_mod.linkFramework("Carbon", .{});
exe_mod.linkFramework("CoreFoundation", .{});
exe_mod.linkFramework("AppKit", .{});
}
b.installArtifact(exe);
b.installArtifact(mcp_exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_unit_tests = b.addTest(.{
.root_module = exe_mod,
});
const mcp_unit_tests = b.addTest(.{
.root_module = mcp_mod,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const run_mcp_unit_tests = b.addRunArtifact(mcp_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
test_step.dependOn(&run_mcp_unit_tests.step);
// Lint step using Zwanzig. Always build the linter with ReleaseFast: it is a
// build-time tool and Debug-mode safety/allocator overhead makes analysis
// multiple orders of magnitude slower on x86 Linux runners.
const zwanzig = b.dependency("zwanzig", .{
.target = b.graph.host,
.optimize = .ReleaseFast,
});
const run_zwanzig = b.addRunArtifact(zwanzig.artifact("zwanzig"));
const target_triple = b.fmt("{s}-{s}", .{
@tagName(target.result.cpu.arch),
@tagName(target.result.os.tag),
});
run_zwanzig.addArgs(&.{ "--target", target_triple, "src" });
const lint_step = b.step("lint", "Run Zwanzig");
lint_step.dependOn(&run_zwanzig.step);
}
fn addTranslateCModule(
b: *std.Build,
exe_mod: *std.Build.Module,
name: []const u8,
header: []const u8,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) *std.Build.Step.TranslateC {
const translate_c = b.addTranslateC(.{
.root_source_file = b.path(header),
.target = target,
.optimize = optimize,
.link_libc = true,
});
exe_mod.addImport(name, translate_c.createModule());
return translate_c;
}
fn addSdlPaths(
b: *std.Build,
exe_mod: *std.Build.Module,
translate_c: *std.Build.Step.TranslateC,
framework_path: ?[]const u8,
) void {
if (b.graph.environ_map.get("SDL3_INCLUDE_PATH")) |sdl3_include| {
const include_path: std.Build.LazyPath = .{ .cwd_relative = sdl3_include };
exe_mod.addIncludePath(include_path);
translate_c.addIncludePath(include_path);
const lib_path = b.fmt("{s}/../lib", .{sdl3_include});
exe_mod.addLibraryPath(.{ .cwd_relative = lib_path });
}
if (b.graph.environ_map.get("SDL3_TTF_INCLUDE_PATH")) |sdl3_ttf_include| {
const include_path: std.Build.LazyPath = .{ .cwd_relative = sdl3_ttf_include };
exe_mod.addIncludePath(include_path);
translate_c.addIncludePath(include_path);
const lib_path = b.fmt("{s}/../lib", .{sdl3_ttf_include});
exe_mod.addLibraryPath(.{ .cwd_relative = lib_path });
}
if (framework_path) |path| {
const framework_lazy_path: std.Build.LazyPath = .{ .cwd_relative = path };
exe_mod.addFrameworkPath(framework_lazy_path);
translate_c.addFrameworkPath(framework_lazy_path);
}
}
// Prefer the active developer selection over hardcoded SDK locations so
// macOS SDK overrides in the dev shell stay local to the environment.
fn findSdkRoot(b: *std.Build) ?[]const u8 {
if (b.graph.environ_map.get("SDKROOT")) |sdk_root| {
return sdk_root;
}
if (findDeveloperDirSdkRoot(b)) |sdk_root| {
return sdk_root;
}
if (findXcrunSdkRoot(b.allocator, b.graph.io)) |sdk_root| {
return sdk_root;
}
const candidates = [_][]const u8{
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk",
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk",
};
for (candidates) |candidate| {
if (sdkExists(b.graph.io, candidate)) {
return candidate;
}
}
return null;
}
fn findDeveloperDirSdkRoot(b: *std.Build) ?[]const u8 {
const developer_dir = b.graph.environ_map.get("DEVELOPER_DIR") orelse return null;
const candidates = [_][]const u8{
b.fmt("{s}/SDKs/MacOSX.sdk", .{developer_dir}),
b.fmt("{s}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk", .{developer_dir}),
};
for (candidates) |candidate| {
if (sdkExists(b.graph.io, candidate)) {
return candidate;
}
}
return null;
}
fn findXcrunSdkRoot(allocator: std.mem.Allocator, io: std.Io) ?[]const u8 {
const result = std.process.run(allocator, io, .{
.argv = &.{ "xcrun", "--sdk", "macosx", "--show-sdk-path" },
}) catch return null;
defer allocator.free(result.stderr);
switch (result.term) {
.exited => |code| if (code != 0) {
allocator.free(result.stdout);
return null;
},
else => {
allocator.free(result.stdout);
return null;
},
}
const trimmed = std.mem.trimEnd(u8, result.stdout, "\r\n");
if (trimmed.len == 0) {
allocator.free(result.stdout);
return null;
}
if (trimmed.len == result.stdout.len) {
return result.stdout;
}
defer allocator.free(result.stdout);
return allocator.dupe(u8, trimmed) catch null;
}
fn sdkExists(io: std.Io, path: []const u8) bool {
if (std.Io.Dir.openDirAbsolute(io, path, .{})) |dir_const| {
var dir = dir_const;
dir.close(io);
return true;
} else |_| {
return false;
}
}