authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-16 12:46:24+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-08-17 18:50:10-04:00
log65cbdefe4d923efd8fd2cfb555cc02a52c5635fc
tree4d317a1c0c0916e52eeee3e7daee5e844020ba0b
parent5a8780838fb3fc18f7c7ebfbf164b37032c2f829

tools: add CBE option to incr-check


1 files changed, 120 insertions(+), 31 deletions(-)

tools/incr-check.zig+120-31
......@@ -2,7 +2,13 @@ const std = @import("std");
22const fatal = std.process.fatal;
33const Allocator = std.mem.Allocator;
44
5const usage = "usage: incr-check <zig binary path> <input file> [-fno-emit-bin] [--zig-lib-dir lib] [--debug-zcu]";
5const usage = "usage: incr-check <zig binary path> <input file> [--zig-lib-dir lib] [--debug-zcu] [--emit none|bin|c] [--zig-cc-binary /path/to/zig]";
6
7const EmitMode = enum {
8 none,
9 bin,
10 c,
11};
612
713pub fn main() !void {
814 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
......@@ -12,19 +18,24 @@ pub fn main() !void {
1218 var opt_zig_exe: ?[]const u8 = null;
1319 var opt_input_file_name: ?[]const u8 = null;
1420 var opt_lib_dir: ?[]const u8 = null;
15 var no_bin = false;
21 var opt_cc_zig: ?[]const u8 = null;
22 var emit: EmitMode = .bin;
1623 var debug_zcu = false;
1724
1825 var arg_it = try std.process.argsWithAllocator(arena);
1926 _ = arg_it.skip();
2027 while (arg_it.next()) |arg| {
2128 if (arg.len > 0 and arg[0] == '-') {
22 if (std.mem.eql(u8, arg, "-fno-emit-bin")) {
23 no_bin = true;
24 } else if (std.mem.eql(u8, arg, "--debug-zcu")) {
25 debug_zcu = true;
29 if (std.mem.eql(u8, arg, "--emit")) {
30 const emit_str = arg_it.next() orelse fatal("expected arg after '--emit'\n{s}", .{usage});
31 emit = std.meta.stringToEnum(EmitMode, emit_str) orelse
32 fatal("invalid emit mode '{s}'\n{s}", .{ emit_str, usage });
2633 } else if (std.mem.eql(u8, arg, "--zig-lib-dir")) {
2734 opt_lib_dir = arg_it.next() orelse fatal("expected arg after '--zig-lib-dir'\n{s}", .{usage});
35 } else if (std.mem.eql(u8, arg, "--debug-zcu")) {
36 debug_zcu = true;
37 } else if (std.mem.eql(u8, arg, "--zig-cc-binary")) {
38 opt_cc_zig = arg_it.next() orelse fatal("expect arg after '--zig-cc-binary'\n{s}", .{usage});
2839 } else {
2940 fatal("unknown option '{s}'\n{s}", .{ arg, usage });
3041 }
......@@ -51,20 +62,19 @@ pub fn main() !void {
5162 const tmp_dir_path = "tmp_" ++ std.fmt.hex(rand_int);
5263 const tmp_dir = try std.fs.cwd().makeOpenPath(tmp_dir_path, .{});
5364
54 if (opt_lib_dir) |lib_dir| {
55 if (!std.fs.path.isAbsolute(lib_dir)) {
56 // The cwd of the subprocess is within the tmp dir, so prepend `..` to the path.
57 opt_lib_dir = try std.fs.path.join(arena, &.{ "..", lib_dir });
58 }
59 }
60
6165 const child_prog_node = prog_node.start("zig build-exe", 0);
6266 defer child_prog_node.end();
6367
68 // Convert paths to be relative to the cwd of the subprocess.
69 const resolved_zig_exe = try std.fs.path.relative(arena, tmp_dir_path, zig_exe);
70 const opt_resolved_lib_dir = if (opt_lib_dir) |lib_dir|
71 try std.fs.path.relative(arena, tmp_dir_path, lib_dir)
72 else
73 null;
74
6475 var child_args: std.ArrayListUnmanaged([]const u8) = .{};
6576 try child_args.appendSlice(arena, &.{
66 // Convert incr-check-relative path to subprocess-relative path.
67 try std.fs.path.relative(arena, tmp_dir_path, zig_exe),
77 resolved_zig_exe,
6878 "build-exe",
6979 case.root_source_file,
7080 "-fincremental",
......@@ -76,13 +86,13 @@ pub fn main() !void {
7686 ".global_cache",
7787 "--listen=-",
7888 });
79 if (opt_lib_dir) |lib_dir| {
80 try child_args.appendSlice(arena, &.{ "--zig-lib-dir", lib_dir });
89 if (opt_resolved_lib_dir) |resolved_lib_dir| {
90 try child_args.appendSlice(arena, &.{ "--zig-lib-dir", resolved_lib_dir });
8191 }
82 if (no_bin) {
83 try child_args.append(arena, "-fno-emit-bin");
84 } else {
85 try child_args.appendSlice(arena, &.{ "-fno-llvm", "-fno-lld" });
92 switch (emit) {
93 .bin => try child_args.appendSlice(arena, &.{ "-fno-llvm", "-fno-lld" }),
94 .none => try child_args.append(arena, "-fno-emit-bin"),
95 .c => try child_args.appendSlice(arena, &.{ "-ofmt=c", "-lc" }),
8696 }
8797 if (debug_zcu) {
8898 try child_args.appendSlice(arena, &.{ "--debug-log", "zcu" });
......@@ -96,6 +106,24 @@ pub fn main() !void {
96106 child.cwd_dir = tmp_dir;
97107 child.cwd = tmp_dir_path;
98108
109 var cc_child_args: std.ArrayListUnmanaged([]const u8) = .{};
110 if (emit == .c) {
111 const resolved_cc_zig_exe = if (opt_cc_zig) |cc_zig_exe|
112 try std.fs.path.relative(arena, tmp_dir_path, cc_zig_exe)
113 else
114 resolved_zig_exe;
115
116 try cc_child_args.appendSlice(arena, &.{
117 resolved_cc_zig_exe,
118 "cc",
119 "-target",
120 case.target_query,
121 "-I",
122 opt_resolved_lib_dir orelse fatal("'--zig-lib-dir' required when using '--emit c'", .{}),
123 "-o",
124 });
125 }
126
99127 var eval: Eval = .{
100128 .arena = arena,
101129 .case = case,
......@@ -103,6 +131,8 @@ pub fn main() !void {
103131 .tmp_dir_path = tmp_dir_path,
104132 .child = &child,
105133 .allow_stderr = debug_zcu,
134 .emit = emit,
135 .cc_child_args = &cc_child_args,
106136 };
107137
108138 try child.spawn();
......@@ -123,7 +153,7 @@ pub fn main() !void {
123153
124154 eval.write(update);
125155 try eval.requestUpdate();
126 try eval.check(&poller, update);
156 try eval.check(&poller, update, update_node);
127157 }
128158
129159 try eval.end(&poller);
......@@ -138,6 +168,10 @@ const Eval = struct {
138168 tmp_dir_path: []const u8,
139169 child: *std.process.Child,
140170 allow_stderr: bool,
171 emit: EmitMode,
172 /// When `emit == .c`, this contains the first few arguments to `zig cc` to build the generated binary.
173 /// The arguments `out.c in.c` must be appended before spawning the subprocess.
174 cc_child_args: *std.ArrayListUnmanaged([]const u8),
141175
142176 const StreamEnum = enum { stdout, stderr };
143177 const Poller = std.io.Poller(StreamEnum);
......@@ -159,7 +193,7 @@ const Eval = struct {
159193 }
160194 }
161195
162 fn check(eval: *Eval, poller: *Poller, update: Case.Update) !void {
196 fn check(eval: *Eval, poller: *Poller, update: Case.Update, prog_node: std.Progress.Node) !void {
163197 const arena = eval.arena;
164198 const Header = std.zig.Server.Message.Header;
165199 const stdout = poller.fifo(.stdout);
......@@ -201,12 +235,7 @@ const Eval = struct {
201235 }
202236 if (result_error_bundle.errorMessageCount() == 0) {
203237 // Empty bundle indicates successful update in a `-fno-emit-bin` build.
204 // We can't do a full success check since we don't have a binary, but let's
205 // at least check that no errors were expected.
206 switch (update.outcome) {
207 .unknown, .stdout, .exit_code => {},
208 .compile_errors => fatal("expected compile errors but compilation incorrectly succeeded", .{}),
209 }
238 try eval.checkSuccessOutcome(update, null, prog_node);
210239 } else {
211240 try eval.checkErrorOutcome(update, result_error_bundle);
212241 }
......@@ -227,7 +256,7 @@ const Eval = struct {
227256 fatal("emit_bin_path included unexpected stderr:\n{s}", .{stderr_data});
228257 }
229258 }
230 try eval.checkSuccessOutcome(update, result_binary);
259 try eval.checkSuccessOutcome(update, result_binary, prog_node);
231260 // This message indicates the end of the update.
232261 stdout.discard(body.len);
233262 return;
......@@ -270,12 +299,28 @@ const Eval = struct {
270299 }
271300 }
272301
273 fn checkSuccessOutcome(eval: *Eval, update: Case.Update, binary_path: []const u8) !void {
302 fn checkSuccessOutcome(eval: *Eval, update: Case.Update, opt_emitted_path: ?[]const u8, prog_node: std.Progress.Node) !void {
274303 switch (update.outcome) {
275304 .unknown => return,
276305 .compile_errors => fatal("expected compile errors but compilation incorrectly succeeded", .{}),
277306 .stdout, .exit_code => {},
278307 }
308 const emitted_path = opt_emitted_path orelse {
309 std.debug.assert(eval.emit == .none);
310 return;
311 };
312
313 const binary_path = switch (eval.emit) {
314 .none => unreachable,
315 .bin => emitted_path,
316 .c => bin: {
317 const rand_int = std.crypto.random.int(u64);
318 const out_bin_name = "./out_" ++ std.fmt.hex(rand_int);
319 try eval.buildCOutput(update, emitted_path, out_bin_name, prog_node);
320 break :bin out_bin_name;
321 },
322 };
323
279324 const result = std.process.Child.run(.{
280325 .allocator = eval.arena,
281326 .argv = &.{binary_path},
......@@ -345,6 +390,50 @@ const Eval = struct {
345390 fatal("unexpected stderr:\n{s}", .{stderr_data});
346391 }
347392 }
393
394 fn buildCOutput(eval: *Eval, update: Case.Update, c_path: []const u8, out_path: []const u8, prog_node: std.Progress.Node) !void {
395 std.debug.assert(eval.cc_child_args.items.len > 0);
396
397 const child_prog_node = prog_node.start("build cbe output", 0);
398 defer child_prog_node.end();
399
400 try eval.cc_child_args.appendSlice(eval.arena, &.{ out_path, c_path });
401 defer eval.cc_child_args.items.len -= 2;
402
403 const result = std.process.Child.run(.{
404 .allocator = eval.arena,
405 .argv = eval.cc_child_args.items,
406 .cwd_dir = eval.tmp_dir,
407 .cwd = eval.tmp_dir_path,
408 .progress_node = child_prog_node,
409 }) catch |err| {
410 fatal("update '{s}': failed to spawn zig cc for '{s}': {s}", .{
411 update.name, c_path, @errorName(err),
412 });
413 };
414 switch (result.term) {
415 .Exited => |code| if (code != 0) {
416 if (result.stderr.len != 0) {
417 std.log.err("update '{s}': zig cc stderr:\n{s}", .{
418 update.name, result.stderr,
419 });
420 }
421 fatal("update '{s}': zig cc for '{s}' failed with code {d}", .{
422 update.name, c_path, code,
423 });
424 },
425 .Signal, .Stopped, .Unknown => {
426 if (result.stderr.len != 0) {
427 std.log.err("update '{s}': zig cc stderr:\n{s}", .{
428 update.name, result.stderr,
429 });
430 }
431 fatal("update '{s}': zig cc for '{s}' terminated unexpectedly", .{
432 update.name, c_path,
433 });
434 },
435 }
436 }
348437};
349438
350439const Case = struct {