-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.zig
More file actions
223 lines (199 loc) · 8.67 KB
/
Copy pathbuild.zig
File metadata and controls
223 lines (199 loc) · 8.67 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
const std = @import("std");
const TestTarget = enum {
all,
root,
types,
file,
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.option(
std.builtin.OptimizeMode,
"optimize",
"Prioritize performance, safety, or binary size (default: ReleaseSafe)",
) orelse .ReleaseSafe;
const package_name = "zig_coding_agent";
const exe_name = "zig-coding-agent";
const test_target_raw = b.option([]const u8, "test-target", "Test target: all|root|types|file") orelse "all";
const test_file = b.option([]const u8, "test-file", "Path to Zig file when -Dtest-target=file");
const test_filter = b.option([]const u8, "test-filter", "Only run tests whose names contain this text");
const test_target = std.meta.stringToEnum(TestTarget, test_target_raw) orelse {
@panic("Invalid -Dtest-target value. Use all, root, types, or file.");
};
// Public package module (import as @import("zig_coding_agent")).
const mod = b.addModule(package_name, .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// CLI executable entrypoint.
const app = b.addExecutable(.{
.name = exe_name,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = optimize != .Debug,
.imports = &.{
.{ .name = package_name, .module = mod },
},
}),
});
// Linux native build avoids linkLibC (glibc/GCC 16 .sframe linker issues on Zig 0.15.2).
// Windows needs libc for socket APIs (recvfrom, etc.).
if (target.result.os.tag == .windows) {
app.linkLibC();
}
b.installArtifact(app);
// Cross-compile for Windows: `zig build windows`
const windows_target = b.resolveTargetQuery(.{
.cpu_arch = .x86_64,
.os_tag = .windows,
.abi = .gnu,
});
const windows_app = b.addExecutable(.{
.name = exe_name,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = windows_target,
.optimize = optimize,
.strip = optimize != .Debug,
.imports = &.{
.{ .name = package_name, .module = mod },
},
}),
});
windows_app.linkLibC();
const windows_step = b.step("windows", "Cross-compile ReleaseSafe binary for x86_64-windows-gnu");
windows_step.dependOn(&b.addInstallArtifact(windows_app, .{}).step);
// Sibling eval runner: `zig build zig_evals` (requires ../zig_eval and a running harness).
const zig_eval_root = b.option([]const u8, "eval-root", "Path to sibling zig_eval checkout") orelse "../zig_eval";
const eval_registry = b.option([]const u8, "eval-registry", "Registry directory inside zig_eval") orelse "registry";
const eval_service = b.option([]const u8, "eval-service", "Service name from registry/services.json") orelse "local-openai-compat";
const harness_base = b.option([]const u8, "eval-harness-url", "Harness base URL for readiness checks") orelse "http://127.0.0.1:8081";
const eval_wait_seconds = b.option(u32, "eval-wait-seconds", "Seconds to wait for harness readiness before failing") orelse 0;
const eval_provider = b.option([]const u8, "eval-provider", "Provider to use for the eval readiness chat probe");
const preflight_script = b.fmt(
\\if [ -f ".env" ]; then set -a; . ./.env; set +a; fi
\\if [ -n "${{ZIG_EVAL_PROVIDER:-}}" ]; then PROVIDER="$ZIG_EVAL_PROVIDER"; else PROVIDER="${{LLM_ROUTER_PROVIDER:-ollama}}"; fi
\\for i in $(seq 0 {d}); do
\\ if curl -sf "{s}/health" >/dev/null; then
\\ PAYLOAD=$(printf '{{"messages":[{{"role":"user","content":"Reply OK"}}],"provider":"%s","model":"auto"}}' "$PROVIDER")
\\ if curl -sf -X POST "{s}/v1/chat/completions" \
\\ -H "Content-Type: application/json" \
\\ ${{LLM_ROUTER_API_KEY:+-H "Authorization: Bearer $LLM_ROUTER_API_KEY"}} \
\\ -d "$PAYLOAD" >/dev/null; then
\\ exit 0
\\ fi
\\ echo "error: harness chat probe failed at {s}/v1/chat/completions (provider=$PROVIDER; is the LLM configured?)" >&2
\\ exit 1
\\ fi
\\ if [ "$i" -eq {d} ]; then break; fi
\\ sleep 1
\\done
\\echo "error: harness not reachable at {s}/health (start it with: zig build run -- --use-env)" >&2
\\exit 1
,
.{ eval_wait_seconds, harness_base, harness_base, harness_base, eval_wait_seconds, harness_base },
);
const eval_preflight = b.addSystemCommand(&.{ "sh", "-c", preflight_script });
if (eval_provider) |provider| {
eval_preflight.setEnvironmentVariable("ZIG_EVAL_PROVIDER", provider);
}
const build_zig_eval = b.addSystemCommand(&.{ "zig", "build", "-Doptimize=ReleaseSafe" });
build_zig_eval.setCwd(b.path(zig_eval_root));
build_zig_eval.step.dependOn(&eval_preflight.step);
const run_evals_script = b.fmt(
\\if [ -f ".env" ]; then set -a; . ./.env; set +a; fi
\\cd "{s}" && zig build run -Doptimize=ReleaseSafe -- run --registry {s} --service {s} "$@"
,
.{ zig_eval_root, eval_registry, eval_service },
);
const run_evals = b.addSystemCommand(&.{ "sh", "-c", run_evals_script, "zig_evals" });
run_evals.step.dependOn(&build_zig_eval.step);
if (b.args) |args| {
run_evals.addArgs(args);
}
const zig_evals_step = b.step(
"zig_evals",
"Run zig_eval registry against a live harness (requires ../zig_eval; start server first)",
);
zig_evals_step.dependOn(&run_evals.step);
// Run command: `zig build run -- [args]`.
const run_step = b.step("run", "Run the Zig Coding Agent server");
const run_cmd = b.addRunArtifact(app);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
// Tests from the package root module.
const mod_tests = b.addTest(.{
.root_module = mod,
});
// Tests from the executable root module.
const exe_tests = b.addTest(.{
.root_module = app.root_module,
});
// Focused tests for the frequently-edited shared types module.
const types_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/types.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_mod_tests = b.addRunArtifact(mod_tests);
const run_exe_tests = b.addRunArtifact(exe_tests);
const run_types_unit_tests = b.addRunArtifact(types_unit_tests);
if (test_filter) |filter| {
run_mod_tests.addArg("--test-filter");
run_mod_tests.addArg(filter);
run_exe_tests.addArg("--test-filter");
run_exe_tests.addArg(filter);
run_types_unit_tests.addArg("--test-filter");
run_types_unit_tests.addArg(filter);
}
var run_file_unit_tests: ?*std.Build.Step.Run = null;
if (test_target == .file) {
const file_path = test_file orelse @panic("-Dtest-target=file requires -Dtest-file=src/path.zig");
const file_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path(file_path),
.target = target,
.optimize = optimize,
}),
});
const run_file = b.addRunArtifact(file_unit_tests);
if (test_filter) |filter| {
run_file.addArg("--test-filter");
run_file.addArg(filter);
}
run_file_unit_tests = run_file;
}
// Targetable test command.
const test_step = b.step("test", "Run tests (use -Dtest-target=all|root|types|file)");
switch (test_target) {
.all => {
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_exe_tests.step);
test_step.dependOn(&run_types_unit_tests.step);
},
.root => {
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_exe_tests.step);
},
.types => {
test_step.dependOn(&run_types_unit_tests.step);
},
.file => {
test_step.dependOn(&run_file_unit_tests.?.step);
},
}
// Compile-only verification for app and built-in test modules.
const check_step = b.step("check", "Compile app and built-in test modules without running");
check_step.dependOn(&app.step);
check_step.dependOn(&mod_tests.step);
check_step.dependOn(&exe_tests.step);
check_step.dependOn(&types_unit_tests.step);
}