authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-26 04:21:58+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-10-05 20:52:26+01:00
logdfc0a27090b1f15aae429801cdded14ab0a80595
treef37b49f99b77fde2d80fba12fb81ded57c7b38d7
parent5ce962eb690aa094521b0ec5f3f466192c42318a
signaturelock-open Commit is signed but in an unrecognized format.

incr-check: clean up temporary directory by default

The new `--preserve-tmp` flag can be used to preserve the temporary directory for debugging purposes.

1 files changed, 64 insertions(+), 30 deletions(-)

tools/incr-check.zig+64-30
......@@ -1,11 +1,12 @@
11const std = @import("std");
2const fatal = std.process.fatal;
32const Allocator = std.mem.Allocator;
43const Cache = std.Build.Cache;
54
6const usage = "usage: incr-check <zig binary path> <input file> [--zig-lib-dir lib] [--debug-zcu] [--debug-link] [--zig-cc-binary /path/to/zig]";
5const usage = "usage: incr-check <zig binary path> <input file> [--zig-lib-dir lib] [--debug-zcu] [--debug-link] [--preserve-tmp] [--zig-cc-binary /path/to/zig]";
76
87pub fn main() !void {
8 const fatal = std.process.fatal;
9
910 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
1011 defer arena_instance.deinit();
1112 const arena = arena_instance.allocator();
......@@ -16,6 +17,7 @@ pub fn main() !void {
1617 var opt_cc_zig: ?[]const u8 = null;
1718 var debug_zcu = false;
1819 var debug_link = false;
20 var preserve_tmp = false;
1921
2022 var arg_it = try std.process.argsWithAllocator(arena);
2123 _ = arg_it.skip();
......@@ -27,6 +29,8 @@ pub fn main() !void {
2729 debug_zcu = true;
2830 } else if (std.mem.eql(u8, arg, "--debug-link")) {
2931 debug_link = true;
32 } else if (std.mem.eql(u8, arg, "--preserve-tmp")) {
33 preserve_tmp = true;
3034 } else if (std.mem.eql(u8, arg, "--zig-cc-binary")) {
3135 opt_cc_zig = arg_it.next() orelse fatal("expect arg after '--zig-cc-binary'\n{s}", .{usage});
3236 } else {
......@@ -48,12 +52,29 @@ pub fn main() !void {
4852 const input_file_bytes = try std.fs.cwd().readFileAlloc(arena, input_file_name, std.math.maxInt(u32));
4953 const case = try Case.parse(arena, input_file_bytes);
5054
55 // Check now: if there are any targets using the `cbe` backend, we need the lib dir.
56 if (opt_lib_dir == null) {
57 for (case.targets) |target| {
58 if (target.backend == .cbe) {
59 fatal("'--zig-lib-dir' requried when using backend 'cbe'", .{});
60 }
61 }
62 }
63
5164 const prog_node = std.Progress.start(.{});
5265 defer prog_node.end();
5366
5467 const rand_int = std.crypto.random.int(u64);
5568 const tmp_dir_path = "tmp_" ++ std.fmt.hex(rand_int);
56 const tmp_dir = try std.fs.cwd().makeOpenPath(tmp_dir_path, .{});
69 var tmp_dir = try std.fs.cwd().makeOpenPath(tmp_dir_path, .{});
70 defer {
71 tmp_dir.close();
72 if (!preserve_tmp) {
73 std.fs.cwd().deleteTree(tmp_dir_path) catch |err| {
74 std.log.warn("failed to delete tree '{s}': {s}", .{ tmp_dir_path, @errorName(err) });
75 };
76 }
77 }
5778
5879 // Convert paths to be relative to the cwd of the subprocess.
5980 const resolved_zig_exe = try std.fs.path.relative(arena, tmp_dir_path, zig_exe);
......@@ -132,7 +153,7 @@ pub fn main() !void {
132153 "-target",
133154 target.query,
134155 "-I",
135 opt_resolved_lib_dir orelse fatal("'--zig-lib-dir' required when using backend 'cbe'", .{}),
156 opt_resolved_lib_dir.?, // verified earlier
136157 "-o",
137158 });
138159 }
......@@ -146,6 +167,7 @@ pub fn main() !void {
146167 .tmp_dir_path = tmp_dir_path,
147168 .child = &child,
148169 .allow_stderr = debug_log_verbose,
170 .preserve_tmp_on_fatal = preserve_tmp,
149171 .cc_child_args = &cc_child_args,
150172 };
151173
......@@ -172,7 +194,7 @@ pub fn main() !void {
172194
173195 try eval.end(&poller);
174196
175 waitChild(&child);
197 waitChild(&child, &eval);
176198 }
177199}
178200
......@@ -185,6 +207,7 @@ const Eval = struct {
185207 tmp_dir_path: []const u8,
186208 child: *std.process.Child,
187209 allow_stderr: bool,
210 preserve_tmp_on_fatal: bool,
188211 /// When `target.backend == .cbe`, this contains the first few arguments to `zig cc` to build the generated binary.
189212 /// The arguments `out.c in.c` must be appended before spawning the subprocess.
190213 cc_child_args: *std.ArrayListUnmanaged([]const u8),
......@@ -199,12 +222,12 @@ const Eval = struct {
199222 .sub_path = full_contents.name,
200223 .data = full_contents.bytes,
201224 }) catch |err| {
202 fatal("failed to update '{s}': {s}", .{ full_contents.name, @errorName(err) });
225 eval.fatal("failed to update '{s}': {s}", .{ full_contents.name, @errorName(err) });
203226 };
204227 }
205228 for (update.deletes) |doomed_name| {
206229 eval.tmp_dir.deleteFile(doomed_name) catch |err| {
207 fatal("failed to delete '{s}': {s}", .{ doomed_name, @errorName(err) });
230 eval.fatal("failed to delete '{s}': {s}", .{ doomed_name, @errorName(err) });
208231 };
209232 }
210233 }
......@@ -246,7 +269,7 @@ const Eval = struct {
246269 if (eval.allow_stderr) {
247270 std.log.info("error_bundle included stderr:\n{s}", .{stderr_data});
248271 } else {
249 fatal("error_bundle included unexpected stderr:\n{s}", .{stderr_data});
272 eval.fatal("error_bundle included unexpected stderr:\n{s}", .{stderr_data});
250273 }
251274 }
252275 if (result_error_bundle.errorMessageCount() != 0) {
......@@ -265,7 +288,7 @@ const Eval = struct {
265288 if (eval.allow_stderr) {
266289 std.log.info("emit_digest included stderr:\n{s}", .{stderr_data});
267290 } else {
268 fatal("emit_digest included unexpected stderr:\n{s}", .{stderr_data});
291 eval.fatal("emit_digest included unexpected stderr:\n{s}", .{stderr_data});
269292 }
270293 }
271294
......@@ -302,16 +325,15 @@ const Eval = struct {
302325 if (eval.allow_stderr) {
303326 std.log.info("update '{s}' included stderr:\n{s}", .{ update.name, stderr_data });
304327 } else {
305 fatal("update '{s}' failed:\n{s}", .{ update.name, stderr_data });
328 eval.fatal("update '{s}' failed:\n{s}", .{ update.name, stderr_data });
306329 }
307330 }
308331
309 waitChild(eval.child);
310 fatal("update '{s}': compiler failed to send error_bundle or emit_bin_path", .{update.name});
332 waitChild(eval.child, eval);
333 eval.fatal("update '{s}': compiler failed to send error_bundle or emit_bin_path", .{update.name});
311334 }
312335
313336 fn checkErrorOutcome(eval: *Eval, update: Case.Update, error_bundle: std.zig.ErrorBundle) !void {
314 _ = eval;
315337 switch (update.outcome) {
316338 .unknown => return,
317339 .compile_errors => |expected_errors| {
......@@ -323,7 +345,7 @@ const Eval = struct {
323345 .stdout, .exit_code => {
324346 const color: std.zig.Color = .auto;
325347 error_bundle.renderToStdErr(color.renderOptions());
326 fatal("update '{s}': unexpected compile errors", .{update.name});
348 eval.fatal("update '{s}': unexpected compile errors", .{update.name});
327349 },
328350 }
329351 }
......@@ -331,7 +353,7 @@ const Eval = struct {
331353 fn checkSuccessOutcome(eval: *Eval, update: Case.Update, opt_emitted_path: ?[]const u8, prog_node: std.Progress.Node) !void {
332354 switch (update.outcome) {
333355 .unknown => return,
334 .compile_errors => fatal("expected compile errors but compilation incorrectly succeeded", .{}),
356 .compile_errors => eval.fatal("expected compile errors but compilation incorrectly succeeded", .{}),
335357 .stdout, .exit_code => {},
336358 }
337359 const emitted_path = opt_emitted_path orelse {
......@@ -388,7 +410,7 @@ const Eval = struct {
388410 .cwd_dir = eval.tmp_dir,
389411 .cwd = eval.tmp_dir_path,
390412 }) catch |err| {
391 fatal("update '{s}': failed to run the generated executable '{s}': {s}", .{
413 eval.fatal("update '{s}': failed to run the generated executable '{s}': {s}", .{
392414 update.name, binary_path, @errorName(err),
393415 });
394416 };
......@@ -402,7 +424,7 @@ const Eval = struct {
402424 .unknown, .compile_errors => unreachable,
403425 .stdout => |expected_stdout| {
404426 if (code != 0) {
405 fatal("update '{s}': generated executable '{s}' failed with code {d}", .{
427 eval.fatal("update '{s}': generated executable '{s}' failed with code {d}", .{
406428 update.name, binary_path, code,
407429 });
408430 }
......@@ -411,7 +433,7 @@ const Eval = struct {
411433 .exit_code => |expected_code| try std.testing.expectEqual(expected_code, result.term.Exited),
412434 },
413435 .Signal, .Stopped, .Unknown => {
414 fatal("update '{s}': generated executable '{s}' terminated unexpectedly", .{
436 eval.fatal("update '{s}': generated executable '{s}' terminated unexpectedly", .{
415437 update.name, binary_path,
416438 });
417439 },
......@@ -428,7 +450,7 @@ const Eval = struct {
428450 }
429451
430452 fn end(eval: *Eval, poller: *Poller) !void {
431 requestExit(eval.child);
453 requestExit(eval.child, eval);
432454
433455 const Header = std.zig.Server.Message.Header;
434456 const stdout = poller.fifo(.stdout);
......@@ -448,7 +470,7 @@ const Eval = struct {
448470
449471 if (stderr.readableLength() > 0) {
450472 const stderr_data = try stderr.toOwnedSlice();
451 fatal("unexpected stderr:\n{s}", .{stderr_data});
473 eval.fatal("unexpected stderr:\n{s}", .{stderr_data});
452474 }
453475 }
454476
......@@ -468,7 +490,7 @@ const Eval = struct {
468490 .cwd = eval.tmp_dir_path,
469491 .progress_node = child_prog_node,
470492 }) catch |err| {
471 fatal("update '{s}': failed to spawn zig cc for '{s}': {s}", .{
493 eval.fatal("update '{s}': failed to spawn zig cc for '{s}': {s}", .{
472494 update.name, c_path, @errorName(err),
473495 });
474496 };
......@@ -479,7 +501,7 @@ const Eval = struct {
479501 update.name, result.stderr,
480502 });
481503 }
482 fatal("update '{s}': zig cc for '{s}' failed with code {d}", .{
504 eval.fatal("update '{s}': zig cc for '{s}' failed with code {d}", .{
483505 update.name, c_path, code,
484506 });
485507 },
......@@ -489,12 +511,22 @@ const Eval = struct {
489511 update.name, result.stderr,
490512 });
491513 }
492 fatal("update '{s}': zig cc for '{s}' terminated unexpectedly", .{
514 eval.fatal("update '{s}': zig cc for '{s}' terminated unexpectedly", .{
493515 update.name, c_path,
494516 });
495517 },
496518 }
497519 }
520
521 fn fatal(eval: *Eval, comptime fmt: []const u8, args: anytype) noreturn {
522 eval.tmp_dir.close();
523 if (!eval.preserve_tmp_on_fatal) {
524 std.fs.cwd().deleteTree(eval.tmp_dir_path) catch |err| {
525 std.log.warn("failed to delete tree '{s}': {s}", .{ eval.tmp_dir_path, @errorName(err) });
526 };
527 }
528 std.process.fatal(fmt, args);
529 }
498530};
499531
500532const Case = struct {
......@@ -550,6 +582,8 @@ const Case = struct {
550582 };
551583
552584 fn parse(arena: Allocator, bytes: []const u8) !Case {
585 const fatal = std.process.fatal;
586
553587 var targets: std.ArrayListUnmanaged(Target) = .empty;
554588 var updates: std.ArrayListUnmanaged(Update) = .empty;
555589 var changes: std.ArrayListUnmanaged(FullContents) = .empty;
......@@ -656,7 +690,7 @@ const Case = struct {
656690 }
657691};
658692
659fn requestExit(child: *std.process.Child) void {
693fn requestExit(child: *std.process.Child, eval: *Eval) void {
660694 if (child.stdin == null) return;
661695
662696 const header: std.zig.Client.Message.Header = .{
......@@ -665,7 +699,7 @@ fn requestExit(child: *std.process.Child) void {
665699 };
666700 child.stdin.?.writeAll(std.mem.asBytes(&header)) catch |err| switch (err) {
667701 error.BrokenPipe => {},
668 else => fatal("failed to send exit: {s}", .{@errorName(err)}),
702 else => eval.fatal("failed to send exit: {s}", .{@errorName(err)}),
669703 };
670704
671705 // Send EOF to stdin.
......@@ -673,11 +707,11 @@ fn requestExit(child: *std.process.Child) void {
673707 child.stdin = null;
674708}
675709
676fn waitChild(child: *std.process.Child) void {
677 requestExit(child);
678 const term = child.wait() catch |err| fatal("child process failed: {s}", .{@errorName(err)});
710fn waitChild(child: *std.process.Child, eval: *Eval) void {
711 requestExit(child, eval);
712 const term = child.wait() catch |err| eval.fatal("child process failed: {s}", .{@errorName(err)});
679713 switch (term) {
680 .Exited => |code| if (code != 0) fatal("compiler failed with code {d}", .{code}),
681 .Signal, .Stopped, .Unknown => fatal("compiler terminated unexpectedly", .{}),
714 .Exited => |code| if (code != 0) eval.fatal("compiler failed with code {d}", .{code}),
715 .Signal, .Stopped, .Unknown => eval.fatal("compiler terminated unexpectedly", .{}),
682716 }
683717}