-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
458 lines (429 loc) · 15.6 KB
/
build.zig
File metadata and controls
458 lines (429 loc) · 15.6 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const std = @import("std");
const builtin = @import("builtin");
const DEFAULT_MACOS_SDK_PATH = ".zig-cache/macos-sdk/MacOSX11.3.sdk";
const DEFAULT_MACOS_SDK_URL = "https://github.com/joseluisq/macosx-sdks/releases/download/11.3/MacOSX11.3.sdk.tar.xz";
fn resolveMacosSdkPath(
b: *std.Build,
macos_sdk_option: ?[]const u8,
) ?[]const u8 {
if (macos_sdk_option) |path| {
return path;
}
return b.graph.env_map.get("MACOS_SDK_ROOT");
}
fn absolutizePath(
b: *std.Build,
input_path: []const u8,
) []const u8 {
if (std.fs.path.isAbsolute(input_path)) {
return input_path;
}
const cwd = std.process.getCwdAlloc(b.allocator) catch return input_path;
return std.fs.path.resolve(b.allocator, &.{ cwd, input_path }) catch input_path;
}
fn optimizeModeName(optimize: std.builtin.OptimizeMode) []const u8 {
return switch (optimize) {
.Debug => "Debug",
.ReleaseSafe => "ReleaseSafe",
.ReleaseFast => "ReleaseFast",
.ReleaseSmall => "ReleaseSmall",
};
}
fn appendUniqueCFlag(
b: *std.Build,
flags: []const []const u8,
new_flag: []const u8,
) []const []const u8 {
for (flags) |flag| {
if (std.mem.eql(u8, flag, new_flag)) {
return flags;
}
}
const merged = b.allocator.alloc([]const u8, flags.len + 1) catch @panic("OOM");
@memcpy(merged[0..flags.len], flags);
merged[flags.len] = new_flag;
return merged;
}
fn addEnsureMacosSdkStep(
b: *std.Build,
sdk_root: []const u8,
sdk_url: []const u8,
) *std.Build.Step {
const archive_path = b.fmt("{s}.tar.xz", .{sdk_root});
const script =
\\set -euo pipefail
\\if [ -d "$CM_MACOS_SDK_ROOT" ]; then
\\ exit 0
\\fi
\\mkdir -p "$(dirname "$CM_MACOS_SDK_ROOT")"
\\if [ ! -f "$CM_MACOS_SDK_ARCHIVE" ]; then
\\ if command -v curl >/dev/null 2>&1; then
\\ curl -L --fail "$CM_MACOS_SDK_URL" -o "$CM_MACOS_SDK_ARCHIVE"
\\ elif command -v wget >/dev/null 2>&1; then
\\ wget -O "$CM_MACOS_SDK_ARCHIVE" "$CM_MACOS_SDK_URL"
\\ else
\\ echo "Neither curl nor wget is available to download macOS SDK." >&2
\\ exit 1
\\ fi
\\fi
\\tar -xf "$CM_MACOS_SDK_ARCHIVE" -C "$(dirname "$CM_MACOS_SDK_ROOT")"
\\if [ ! -d "$CM_MACOS_SDK_ROOT" ]; then
\\ echo "macOS SDK extraction failed: $CM_MACOS_SDK_ROOT not found." >&2
\\ exit 1
\\fi
;
const cmd = b.addSystemCommand(&.{ "bash", "-lc", script });
cmd.setEnvironmentVariable("CM_MACOS_SDK_ROOT", sdk_root);
cmd.setEnvironmentVariable("CM_MACOS_SDK_ARCHIVE", archive_path);
cmd.setEnvironmentVariable("CM_MACOS_SDK_URL", sdk_url);
return &cmd.step;
}
fn addMatrixInstallCommand(
b: *std.Build,
step: *std.Build.Step,
target_triple: []const u8,
install_name: []const u8,
maybe_sysroot: ?[]const u8,
ensure_macos_sdk_step: ?*std.Build.Step,
frontend_prebuild_step: ?*std.Build.Step,
optimize: std.builtin.OptimizeMode,
strip_symbols: bool,
) void {
var argv = std.ArrayList([]const u8).empty;
defer argv.deinit(b.allocator);
argv.appendSlice(b.allocator, &.{
"zig",
"build",
"install",
"--prefix",
"zig-out",
}) catch @panic("OOM");
argv.append(b.allocator, b.fmt("-Doptimize={s}", .{optimizeModeName(optimize)})) catch @panic("OOM");
argv.append(b.allocator, b.fmt("-Dstrip={}", .{strip_symbols})) catch @panic("OOM");
argv.appendSlice(b.allocator, &.{"-Dskip_frontend=true"}) catch @panic("OOM");
if (maybe_sysroot) |sysroot| {
argv.appendSlice(b.allocator, &.{ "--sysroot", sysroot }) catch @panic("OOM");
}
argv.append(b.allocator, b.fmt("-Dtarget={s}", .{target_triple})) catch @panic("OOM");
argv.append(b.allocator, b.fmt("-Dmatrix_name={s}", .{install_name})) catch @panic("OOM");
const cmd = b.addSystemCommand(argv.items);
if (ensure_macos_sdk_step) |download_step| {
cmd.step.dependOn(download_step);
}
if (frontend_prebuild_step) |frontend_step| {
cmd.step.dependOn(frontend_step);
}
step.dependOn(&cmd.step);
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const strip_symbols = b.option(bool, "strip", "Strip debug symbols from binaries.") orelse false;
const matrix_name = b.option([]const u8, "matrix_name", "Override installed binary name");
const skip_frontend = b.option(
bool,
"skip_frontend",
"Skip frontend npm typecheck/build steps (for internal matrix sub-builds).",
) orelse false;
const macos_sdk_option = b.option(
[]const u8,
"macos_sdk",
"Path to a macOS SDK for macOS cross-compilation (sysroot).",
);
const macos_sdk_auto_download = b.option(
bool,
"macos_sdk_auto_download",
"Automatically download macOS SDK when needed for macOS cross-compilation.",
) orelse false;
const macos_sdk_url = b.option(
[]const u8,
"macos_sdk_url",
"URL used when automatically downloading the macOS SDK.",
) orelse DEFAULT_MACOS_SDK_URL;
var macos_sdk_path = resolveMacosSdkPath(b, macos_sdk_option);
if (macos_sdk_path == null and macos_sdk_auto_download and builtin.os.tag == .linux) {
macos_sdk_path = DEFAULT_MACOS_SDK_PATH;
}
if (macos_sdk_path) |sdk| {
macos_sdk_path = absolutizePath(b, sdk);
}
var ensure_macos_sdk_step: ?*std.Build.Step = null;
if (macos_sdk_auto_download and builtin.os.tag == .linux) {
if (macos_sdk_path) |sdk| {
ensure_macos_sdk_step = addEnsureMacosSdkStep(b, sdk, macos_sdk_url);
}
}
if (!target.query.isNative() and target.result.os.tag == .macos and b.sysroot == null) {
const fail = b.addFail(
"Cross-compiling to macOS requires --sysroot <sdk>. For matrix builds, use `zig build build-all-targets -Dmacos_sdk_auto_download=true`.",
);
b.default_step.dependOn(&fail.step);
b.getInstallStep().dependOn(&fail.step);
return;
}
var frontend_build_step: ?*std.Build.Step = null;
if (!skip_frontend) {
const frontend_install = b.addSystemCommand(&.{
"bash",
"-lc",
"test -d frontend/node_modules || npm --prefix frontend ci --no-audit --no-fund",
});
const frontend_typecheck = b.addSystemCommand(&.{
"npm",
"--prefix",
"frontend",
"exec",
"--",
"tsc",
"-p",
"frontend/tsconfig.json",
"--noEmit",
});
frontend_typecheck.step.dependOn(&frontend_install.step);
const frontend_build = b.addSystemCommand(&.{
"npm",
"--prefix",
"frontend",
"exec",
"--",
"vite",
"build",
"--config",
"frontend/vite.config.ts",
"--outDir",
"dist",
});
frontend_build.step.dependOn(&frontend_typecheck.step);
frontend_build_step = &frontend_build.step;
}
const webui_dep = b.dependency("webui", .{
.target = target,
.optimize = optimize,
.dynamic = false,
.@"enable-tls" = false,
.@"enable-webui-log" = false,
});
const webui_module = webui_dep.module("webui");
if (target.result.os.tag == .macos) {
if (b.sysroot) |sysroot| {
const frameworks_path = b.fmt("{s}/System/Library/Frameworks", .{sysroot});
const sdk_usr_include = b.fmt("{s}/usr/include", .{sysroot});
webui_module.addFrameworkPath(.{ .cwd_relative = frameworks_path });
webui_module.addSystemIncludePath(.{ .cwd_relative = sdk_usr_include });
for (webui_module.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |linked_compile| {
linked_compile.root_module.addFrameworkPath(.{ .cwd_relative = frameworks_path });
linked_compile.root_module.addSystemIncludePath(.{ .cwd_relative = sdk_usr_include });
for (linked_compile.root_module.link_objects.items) |*compile_link_object| {
switch (compile_link_object.*) {
.c_source_file => |c_source_file| {
c_source_file.flags = appendUniqueCFlag(
b,
c_source_file.flags,
"-Wno-error=gnu-folding-constant",
);
},
.c_source_files => |c_source_files| {
c_source_files.flags = appendUniqueCFlag(
b,
c_source_files.flags,
"-Wno-error=gnu-folding-constant",
);
},
else => {},
}
}
},
else => {},
}
}
}
if (!target.query.isNative()) {
if (ensure_macos_sdk_step) |download_step| {
for (webui_module.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |linked_compile| {
linked_compile.step.dependOn(download_step);
},
else => {},
}
}
}
}
}
const exe = b.addExecutable(.{
.name = "codex-manager",
.root_module = b.createModule(.{
.root_source_file = b.path("main.zig"),
.target = target,
.optimize = optimize,
.strip = strip_symbols,
.imports = &.{
.{ .name = "webui", .module = webui_module },
},
}),
});
if (frontend_build_step) |frontend_step| {
exe.step.dependOn(frontend_step);
}
if (!target.query.isNative() and target.result.os.tag == .macos) {
if (ensure_macos_sdk_step) |download_step| {
exe.step.dependOn(download_step);
}
}
const install_artifact = b.addInstallArtifact(exe, .{
.dest_sub_path = matrix_name,
.pdb_dir = if (matrix_name == null) .default else .disabled,
});
b.getInstallStep().dependOn(&install_artifact.step);
const dev_step = b.step("dev", "Build frontend and run Codex Manager in dev mode");
const installed_exe_name = matrix_name orelse "codex-manager";
const installed_exe_path = b.getInstallPath(.bin, installed_exe_name);
const run_cmd = b.addSystemCommand(&.{installed_exe_path});
run_cmd.step.dependOn(&install_artifact.step);
if (b.args) |args| {
run_cmd.addArgs(args);
}
dev_step.dependOn(&run_cmd.step);
const backend_tests = b.addTest(.{
.root_module = b.createModule(.{
// Keep package root at repo root so @embedFile("../frontend/...") used by
// imported modules remains inside package boundaries during tests.
.root_source_file = b.path("rpc_tests.zig"),
.target = target,
.optimize = optimize,
.strip = strip_symbols,
}),
});
const run_backend_tests = b.addRunArtifact(backend_tests);
const test_step = b.step("test", "Run backend Zig tests");
test_step.dependOn(&run_backend_tests.step);
const build_all_targets_step = b.step(
"build-all-targets",
"Compile release binaries for supported target matrix into zig-out/bin",
);
switch (builtin.os.tag) {
.linux => {
addMatrixInstallCommand(
b,
build_all_targets_step,
"x86_64-linux-gnu",
"codex-manager-linux-x86_64",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
addMatrixInstallCommand(
b,
build_all_targets_step,
"aarch64-linux-gnu",
"codex-manager-linux-aarch64",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
addMatrixInstallCommand(
b,
build_all_targets_step,
"x86_64-windows-gnu",
"codex-manager-windows-x86_64.exe",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
addMatrixInstallCommand(
b,
build_all_targets_step,
"aarch64-windows-gnu",
"codex-manager-windows-aarch64.exe",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
if (macos_sdk_path) |macos_sdk_root| {
if (macos_sdk_root.len > 0) {
addMatrixInstallCommand(
b,
build_all_targets_step,
"x86_64-macos",
"codex-manager-macos-x86_64",
macos_sdk_root,
ensure_macos_sdk_step,
frontend_build_step,
optimize,
strip_symbols,
);
addMatrixInstallCommand(
b,
build_all_targets_step,
"aarch64-macos",
"codex-manager-macos-aarch64",
macos_sdk_root,
ensure_macos_sdk_step,
frontend_build_step,
optimize,
strip_symbols,
);
}
}
},
.macos => {
addMatrixInstallCommand(
b,
build_all_targets_step,
"x86_64-macos",
"codex-manager-macos-x86_64",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
addMatrixInstallCommand(
b,
build_all_targets_step,
"aarch64-macos",
"codex-manager-macos-aarch64",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
},
.windows => {
addMatrixInstallCommand(
b,
build_all_targets_step,
"x86_64-windows-gnu",
"codex-manager-windows-x86_64.exe",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
addMatrixInstallCommand(
b,
build_all_targets_step,
"aarch64-windows-gnu",
"codex-manager-windows-aarch64.exe",
null,
null,
frontend_build_step,
optimize,
strip_symbols,
);
},
else => {},
}
}