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
|
const Ghostty = @This();
const std = @import("std");
const builtin = @import("builtin");
const RunStep = std.Build.Step.Run;
const Config = @import("Config.zig");
const Docs = @import("GhosttyDocs.zig");
const I18n = @import("GhosttyI18n.zig");
const Resources = @import("GhosttyResources.zig");
const XCFramework = @import("GhosttyXCFramework.zig");
build: *std.Build.Step.Run,
open: *std.Build.Step.Run,
copy: *std.Build.Step.Run,
xctest: *std.Build.Step.Run,
pub const Deps = struct {
xcframework: *const XCFramework,
docs: *const Docs,
i18n: ?*const I18n,
resources: *const Resources,
};
pub fn init(
b: *std.Build,
config: *const Config,
deps: Deps,
) !Ghostty {
const xc_config = switch (config.optimize) {
.Debug => "Debug",
.ReleaseSafe,
.ReleaseSmall,
.ReleaseFast,
=> "Release",
};
const xc_arch: ?[]const u8 = switch (deps.xcframework.target) {
// Universal is our default target, so we don't have to
// add anything.
.universal => null,
// Native we need to override the architecture in the Xcode
// project with the -arch flag.
.native => switch (builtin.cpu.arch) {
.aarch64 => "arm64",
.x86_64 => "x86_64",
else => @panic("unsupported macOS arch"),
},
};
const env = try std.process.getEnvMap(b.allocator);
const app_path = b.fmt("macos/build/{s}/Ghostty.app", .{xc_config});
// Our step to build the Ghostty macOS app.
const build = build: {
// External environment variables can mess up xcodebuild, so
// we create a new empty environment.
const env_map = try b.allocator.create(std.process.EnvMap);
env_map.* = .init(b.allocator);
if (env.get("PATH")) |v| try env_map.put("PATH", v);
const step = RunStep.create(b, "xcodebuild");
step.has_side_effects = true;
step.cwd = b.path("macos");
step.env_map = env_map;
step.addArgs(&.{
"xcodebuild",
"-target",
"Ghostty",
"-configuration",
xc_config,
});
// If we have a specific architecture, we need to pass it
// to xcodebuild.
if (xc_arch) |arch| step.addArgs(&.{ "-arch", arch });
// We need the xcframework
deps.xcframework.addStepDependencies(&step.step);
// We also need all these resources because the xcode project
// references them via symlinks.
deps.resources.addStepDependencies(&step.step);
if (deps.i18n) |v| v.addStepDependencies(&step.step);
deps.docs.installDummy(&step.step);
// Expect success
step.expectExitCode(0);
break :build step;
};
const xctest = xctest: {
const env_map = try b.allocator.create(std.process.EnvMap);
env_map.* = .init(b.allocator);
if (env.get("PATH")) |v| try env_map.put("PATH", v);
const step = RunStep.create(b, "xcodebuild test");
step.has_side_effects = true;
step.cwd = b.path("macos");
step.env_map = env_map;
step.addArgs(&.{
"xcodebuild",
"test",
"-scheme",
"Ghostty",
});
if (xc_arch) |arch| step.addArgs(&.{ "-arch", arch });
// We need the xcframework
deps.xcframework.addStepDependencies(&step.step);
// We also need all these resources because the xcode project
// references them via symlinks.
deps.resources.addStepDependencies(&step.step);
if (deps.i18n) |v| v.addStepDependencies(&step.step);
deps.docs.installDummy(&step.step);
// Expect success
step.expectExitCode(0);
break :xctest step;
};
// Our step to open the resulting Ghostty app.
const open = open: {
const disable_save_state = RunStep.create(b, "disable save state");
disable_save_state.has_side_effects = true;
disable_save_state.addArgs(&.{
"/usr/libexec/PlistBuddy",
"-c",
// We'll have to change this to `Set` if we ever put this
// into our Info.plist.
"Add :NSQuitAlwaysKeepsWindows bool false",
b.fmt("{s}/Contents/Info.plist", .{app_path}),
});
disable_save_state.expectExitCode(0);
disable_save_state.step.dependOn(&build.step);
const open = RunStep.create(b, "run Ghostty app");
open.has_side_effects = true;
open.cwd = b.path("");
open.addArgs(&.{b.fmt(
"{s}/Contents/MacOS/ghostty",
.{app_path},
)});
// Open depends on the app
open.step.dependOn(&build.step);
open.step.dependOn(&disable_save_state.step);
// This overrides our default behavior and forces logs to show
// up on stderr (in addition to the centralized macOS log).
open.setEnvironmentVariable("GHOSTTY_LOG", "1");
// Configure how we're launching
open.setEnvironmentVariable("GHOSTTY_MAC_LAUNCH_SOURCE", "zig_run");
if (b.args) |args| {
open.addArgs(args);
}
break :open open;
};
// Our step to copy the app bundle to the install path.
// We have to use `cp -R` because there are symlinks in the
// bundle.
const copy = copy: {
const step = RunStep.create(b, "copy app bundle");
step.addArgs(&.{ "cp", "-R" });
step.addFileArg(b.path(app_path));
step.addArg(b.fmt("{s}", .{b.install_path}));
step.step.dependOn(&build.step);
break :copy step;
};
return .{
.build = build,
.open = open,
.copy = copy,
.xctest = xctest,
};
}
pub fn install(self: *const Ghostty) void {
const b = self.copy.step.owner;
b.getInstallStep().dependOn(&self.copy.step);
}
pub fn installXcframework(self: *const Ghostty) void {
const b = self.build.step.owner;
b.getInstallStep().dependOn(&self.build.step);
}
pub fn addTestStepDependencies(
self: *const Ghostty,
other_step: *std.Build.Step,
) void {
other_step.dependOn(&self.xctest.step);
}
|