authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-25 20:21:27+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-25 20:39:31+01:00
log054dbb6798bd81cbea809999b2fdffc1b59e5ca3
treedd07ce8a033b77471bad19011d6e0c5059f531b3
parent1714b74edfdf1dbc52f8dd1555abb0ff43dfdff8
signaturelock-open Commit is signed but in an unrecognized format.

incr-check: specify backend in manifest, allow multiple targets/backends


10 files changed, 155 insertions(+), 121 deletions(-)

test/incremental/add_decl+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const std = @import("std");
test/incremental/add_decl_namespaced+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const std = @import("std");
test/incremental/delete_comptime_decls+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45pub fn main() void {}
test/incremental/hello+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const std = @import("std");
test/incremental/modify_inline_fn+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const std = @import("std");
test/incremental/move_src+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const std = @import("std");
test/incremental/remove_enum_field+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const MyEnum = enum(u8) {
test/incremental/type_becomes_comptime_only+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const SomeType = u32;
test/incremental/unreferenced_error+2-1
......@@ -1,4 +1,5 @@
1#target=x86_64-linux
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
23#update=initial version
34#file=main.zig
45const std = @import("std");
tools/incr-check.zig+137-112
......@@ -3,13 +3,7 @@ const fatal = std.process.fatal;
33const Allocator = std.mem.Allocator;
44const Cache = std.Build.Cache;
55
6const usage = "usage: incr-check <zig binary path> <input file> [--zig-lib-dir lib] [--debug-zcu] [--debug-link] [--emit none|bin|c] [--zig-cc-binary /path/to/zig]";
7
8const EmitMode = enum {
9 none,
10 bin,
11 c,
12};
6const usage = "usage: incr-check <zig binary path> <input file> [--zig-lib-dir lib] [--debug-zcu] [--debug-link] [--zig-cc-binary /path/to/zig]";
137
148pub fn main() !void {
159 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
......@@ -20,7 +14,6 @@ pub fn main() !void {
2014 var opt_input_file_name: ?[]const u8 = null;
2115 var opt_lib_dir: ?[]const u8 = null;
2216 var opt_cc_zig: ?[]const u8 = null;
23 var emit: EmitMode = .bin;
2417 var debug_zcu = false;
2518 var debug_link = false;
2619
......@@ -28,11 +21,7 @@ pub fn main() !void {
2821 _ = arg_it.skip();
2922 while (arg_it.next()) |arg| {
3023 if (arg.len > 0 and arg[0] == '-') {
31 if (std.mem.eql(u8, arg, "--emit")) {
32 const emit_str = arg_it.next() orelse fatal("expected arg after '--emit'\n{s}", .{usage});
33 emit = std.meta.stringToEnum(EmitMode, emit_str) orelse
34 fatal("invalid emit mode '{s}'\n{s}", .{ emit_str, usage });
35 } else if (std.mem.eql(u8, arg, "--zig-lib-dir")) {
24 if (std.mem.eql(u8, arg, "--zig-lib-dir")) {
3625 opt_lib_dir = arg_it.next() orelse fatal("expected arg after '--zig-lib-dir'\n{s}", .{usage});
3726 } else if (std.mem.eql(u8, arg, "--debug-zcu")) {
3827 debug_zcu = true;
......@@ -76,109 +65,114 @@ pub fn main() !void {
7665 else
7766 null;
7867
79 var child_args: std.ArrayListUnmanaged([]const u8) = .empty;
80 try child_args.appendSlice(arena, &.{
81 resolved_zig_exe,
82 "build-exe",
83 case.root_source_file,
84 "-fincremental",
85 "-target",
86 case.target_query,
87 "--cache-dir",
88 ".local-cache",
89 "--global-cache-dir",
90 ".global_cache",
91 "--listen=-",
92 });
93 if (opt_resolved_lib_dir) |resolved_lib_dir| {
94 try child_args.appendSlice(arena, &.{ "--zig-lib-dir", resolved_lib_dir });
95 }
96 switch (emit) {
97 .bin => try child_args.appendSlice(arena, &.{ "-fno-llvm", "-fno-lld" }),
98 .none => try child_args.append(arena, "-fno-emit-bin"),
99 .c => try child_args.appendSlice(arena, &.{ "-ofmt=c", "-lc" }),
100 }
101 if (debug_zcu) {
102 try child_args.appendSlice(arena, &.{ "--debug-log", "zcu" });
103 }
104 if (debug_link) {
105 try child_args.appendSlice(arena, &.{ "--debug-log", "link", "--debug-log", "link_state", "--debug-log", "link_relocs" });
106 }
107
10868 const debug_log_verbose = debug_zcu or debug_link;
10969
110 var child = std.process.Child.init(child_args.items, arena);
111 child.stdin_behavior = .Pipe;
112 child.stdout_behavior = .Pipe;
113 child.stderr_behavior = .Pipe;
114 child.progress_node = child_prog_node;
115 child.cwd_dir = tmp_dir;
116 child.cwd = tmp_dir_path;
117
118 var cc_child_args: std.ArrayListUnmanaged([]const u8) = .empty;
119 if (emit == .c) {
120 const resolved_cc_zig_exe = if (opt_cc_zig) |cc_zig_exe|
121 try std.fs.path.relative(arena, tmp_dir_path, cc_zig_exe)
122 else
123 resolved_zig_exe;
124
125 try cc_child_args.appendSlice(arena, &.{
126 resolved_cc_zig_exe,
127 "cc",
70 for (case.targets) |target| {
71 std.log.scoped(.status).info("target: '{s}-{s}'", .{ target.query, @tagName(target.backend) });
72
73 var child_args: std.ArrayListUnmanaged([]const u8) = .empty;
74 try child_args.appendSlice(arena, &.{
75 resolved_zig_exe,
76 "build-exe",
77 case.root_source_file,
78 "-fincremental",
12879 "-target",
129 case.target_query,
130 "-I",
131 opt_resolved_lib_dir orelse fatal("'--zig-lib-dir' required when using '--emit c'", .{}),
132 "-o",
80 target.query,
81 "--cache-dir",
82 ".local-cache",
83 "--global-cache-dir",
84 ".global_cache",
85 "--listen=-",
13386 });
134 }
87 if (opt_resolved_lib_dir) |resolved_lib_dir| {
88 try child_args.appendSlice(arena, &.{ "--zig-lib-dir", resolved_lib_dir });
89 }
90 switch (target.backend) {
91 .sema => try child_args.append(arena, "-fno-emit-bin"),
92 .selfhosted => try child_args.appendSlice(arena, &.{ "-fno-llvm", "-fno-lld" }),
93 .llvm => try child_args.appendSlice(arena, &.{ "-fllvm", "-flld" }),
94 .cbe => try child_args.appendSlice(arena, &.{ "-ofmt=c", "-lc" }),
95 }
96 if (debug_zcu) {
97 try child_args.appendSlice(arena, &.{ "--debug-log", "zcu" });
98 }
99 if (debug_link) {
100 try child_args.appendSlice(arena, &.{ "--debug-log", "link", "--debug-log", "link_state", "--debug-log", "link_relocs" });
101 }
135102
136 var eval: Eval = .{
137 .arena = arena,
138 .case = case,
139 .tmp_dir = tmp_dir,
140 .tmp_dir_path = tmp_dir_path,
141 .child = &child,
142 .allow_stderr = debug_log_verbose,
143 .emit = emit,
144 .cc_child_args = &cc_child_args,
145 };
103 var child = std.process.Child.init(child_args.items, arena);
104 child.stdin_behavior = .Pipe;
105 child.stdout_behavior = .Pipe;
106 child.stderr_behavior = .Pipe;
107 child.progress_node = child_prog_node;
108 child.cwd_dir = tmp_dir;
109 child.cwd = tmp_dir_path;
110
111 var cc_child_args: std.ArrayListUnmanaged([]const u8) = .empty;
112 if (target.backend == .cbe) {
113 const resolved_cc_zig_exe = if (opt_cc_zig) |cc_zig_exe|
114 try std.fs.path.relative(arena, tmp_dir_path, cc_zig_exe)
115 else
116 resolved_zig_exe;
117
118 try cc_child_args.appendSlice(arena, &.{
119 resolved_cc_zig_exe,
120 "cc",
121 "-target",
122 target.query,
123 "-I",
124 opt_resolved_lib_dir orelse fatal("'--zig-lib-dir' required when using backend 'cbe'", .{}),
125 "-o",
126 });
127 }
146128
147 try child.spawn();
129 var eval: Eval = .{
130 .arena = arena,
131 .case = case,
132 .target = target,
133 .tmp_dir = tmp_dir,
134 .tmp_dir_path = tmp_dir_path,
135 .child = &child,
136 .allow_stderr = debug_log_verbose,
137 .cc_child_args = &cc_child_args,
138 };
148139
149 var poller = std.io.poll(arena, Eval.StreamEnum, .{
150 .stdout = child.stdout.?,
151 .stderr = child.stderr.?,
152 });
153 defer poller.deinit();
140 try child.spawn();
154141
155 for (case.updates) |update| {
156 var update_node = prog_node.start(update.name, 0);
157 defer update_node.end();
142 var poller = std.io.poll(arena, Eval.StreamEnum, .{
143 .stdout = child.stdout.?,
144 .stderr = child.stderr.?,
145 });
146 defer poller.deinit();
158147
159 if (debug_log_verbose) {
160 std.log.info("=== START UPDATE '{s}' ===", .{update.name});
161 }
148 for (case.updates) |update| {
149 var update_node = prog_node.start(update.name, 0);
150 defer update_node.end();
162151
163 eval.write(update);
164 try eval.requestUpdate();
165 try eval.check(&poller, update, update_node);
166 }
152 if (debug_log_verbose) {
153 std.log.scoped(.status).info("update: '{s}'", .{update.name});
154 }
155
156 eval.write(update);
157 try eval.requestUpdate();
158 try eval.check(&poller, update, update_node);
159 }
167160
168 try eval.end(&poller);
161 try eval.end(&poller);
169162
170 waitChild(&child);
163 waitChild(&child);
164 }
171165}
172166
173167const Eval = struct {
174168 arena: Allocator,
175169 case: Case,
170 target: Case.Target,
176171 tmp_dir: std.fs.Dir,
177172 tmp_dir_path: []const u8,
178173 child: *std.process.Child,
179174 allow_stderr: bool,
180 emit: EmitMode,
181 /// When `emit == .c`, this contains the first few arguments to `zig cc` to build the generated binary.
175 /// When `target.backend == .cbe`, this contains the first few arguments to `zig cc` to build the generated binary.
182176 /// The arguments `out.c in.c` must be appended before spawning the subprocess.
183177 cc_child_args: *std.ArrayListUnmanaged([]const u8),
184178
......@@ -262,7 +256,7 @@ const Eval = struct {
262256 }
263257 }
264258
265 if (eval.emit == .none) {
259 if (eval.target.backend == .sema) {
266260 try eval.checkSuccessOutcome(update, null, prog_node);
267261 // This message indicates the end of the update.
268262 stdout.discard(body.len);
......@@ -275,11 +269,11 @@ const Eval = struct {
275269 const bin_name = try std.zig.binNameAlloc(arena, .{
276270 .root_name = name,
277271 .target = try std.zig.system.resolveTargetQuery(try std.Build.parseTargetQuery(.{
278 .arch_os_abi = eval.case.target_query,
279 .object_format = switch (eval.emit) {
280 .none => unreachable,
281 .bin => null,
282 .c => "c",
272 .arch_os_abi = eval.target.query,
273 .object_format = switch (eval.target.backend) {
274 .sema => unreachable,
275 .selfhosted, .llvm => null,
276 .cbe => "c",
283277 },
284278 })),
285279 .output_mode = .Exe,
......@@ -335,14 +329,14 @@ const Eval = struct {
335329 .stdout, .exit_code => {},
336330 }
337331 const emitted_path = opt_emitted_path orelse {
338 std.debug.assert(eval.emit == .none);
332 std.debug.assert(eval.target.backend == .sema);
339333 return;
340334 };
341335
342 const binary_path = switch (eval.emit) {
343 .none => unreachable,
344 .bin => emitted_path,
345 .c => bin: {
336 const binary_path = switch (eval.target.backend) {
337 .sema => unreachable,
338 .selfhosted, .llvm => emitted_path,
339 .cbe => bin: {
346340 const rand_int = std.crypto.random.int(u64);
347341 const out_bin_name = "./out_" ++ std.fmt.hex(rand_int);
348342 try eval.buildCOutput(update, emitted_path, out_bin_name, prog_node);
......@@ -468,7 +462,26 @@ const Eval = struct {
468462const Case = struct {
469463 updates: []Update,
470464 root_source_file: []const u8,
471 target_query: []const u8,
465 targets: []const Target,
466
467 const Target = struct {
468 query: []const u8,
469 backend: Backend,
470 const Backend = enum {
471 /// Run semantic analysis only. Runtime output will not be tested, but we still verify
472 /// that compilation succeeds. Corresponds to `-fno-emit-bin`.
473 sema,
474 /// Use the self-hosted code generation backend for this target.
475 /// Corresponds to `-fno-llvm -fno-lld`.
476 selfhosted,
477 /// Use the LLVM backend.
478 /// Corresponds to `-fllvm -flld`.
479 llvm,
480 /// Use the C backend. The output is compiled with `zig cc`.
481 /// Corresponds to `-ofmt=c`.
482 cbe,
483 };
484 };
472485
473486 const Update = struct {
474487 name: []const u8,
......@@ -498,9 +511,9 @@ const Case = struct {
498511 };
499512
500513 fn parse(arena: Allocator, bytes: []const u8) !Case {
514 var targets: std.ArrayListUnmanaged(Target) = .empty;
501515 var updates: std.ArrayListUnmanaged(Update) = .empty;
502516 var changes: std.ArrayListUnmanaged(FullContents) = .empty;
503 var target_query: ?[]const u8 = null;
504517 var it = std.mem.splitScalar(u8, bytes, '\n');
505518 var line_n: usize = 1;
506519 var root_source_file: ?[]const u8 = null;
......@@ -512,8 +525,16 @@ const Case = struct {
512525 if (val.len == 0) {
513526 fatal("line {d}: missing value", .{line_n});
514527 } else if (std.mem.eql(u8, key, "target")) {
515 if (target_query != null) fatal("line {d}: duplicate target", .{line_n});
516 target_query = val;
528 const split_idx = std.mem.lastIndexOfScalar(u8, val, '-') orelse
529 fatal("line {d}: target does not include backend", .{line_n});
530 const query = val[0..split_idx];
531 const backend_str = val[split_idx + 1 ..];
532 const backend: Target.Backend = std.meta.stringToEnum(Target.Backend, backend_str) orelse
533 fatal("line {d}: invalid backend '{s}'", .{ line_n, backend_str });
534 try targets.append(arena, .{
535 .query = query,
536 .backend = backend,
537 });
517538 } else if (std.mem.eql(u8, key, "update")) {
518539 if (updates.items.len > 0) {
519540 const last_update = &updates.items[updates.items.len - 1];
......@@ -565,15 +586,19 @@ const Case = struct {
565586 }
566587 }
567588
589 if (targets.items.len == 0) {
590 fatal("missing target", .{});
591 }
592
568593 if (changes.items.len > 0) {
569594 const last_update = &updates.items[updates.items.len - 1];
570 last_update.changes = try changes.toOwnedSlice(arena);
595 last_update.changes = changes.items; // arena so no need for toOwnedSlice
571596 }
572597
573598 return .{
574599 .updates = updates.items,
575600 .root_source_file = root_source_file orelse fatal("missing root source file", .{}),
576 .target_query = target_query orelse fatal("missing target", .{}),
601 .targets = targets.items, // arena so no need for toOwnedSlice
577602 };
578603 }
579604};