authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-04-14 10:26:35+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-04-16 12:37:08+02:00
log5492b1e3264af7151adb215cdf905a41f93d2713
treed42f9e512f3838fd66c98c93437153e8cf90eda7
parentbde87cfc71a0b9d3d2e35e4b2181d1f956ff5711
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

tests: move incremental target matrix out of manifests

Having the matrix of test targets for incremental compilation in the individual test manifests has turned out to be inconvenient for a few reasons: the tests are almost certain to accidentally get out of sync, disabling targets entirely is annoying to do, and incr-check needs to take care to print the target in all error messages (which currently does not always happen). If I recall correctly, I originally designed it this way because it allows targets to be disabled at the granularity of individual tests, but there's an easier approach to that: just let a test manifest that it should be *skipped* on a certain target! As skipping is the rare case, and also the case you want readers to notice, it makes sense for *it* to be explicitly specified, like how unit tests use `error.SkipZigTest`. So, `incr-check` no longer runs through a list of targets specified in the manifest. Instead, it accepts (and, in fact, requires) a single target on the command line, and runs the test for that specific target. If the file contains a `#skip_target` directive for that target, then `incr-check` exits immediately, so we can still disable targets at individual test granularity, but you can also disable a target for all tests by just commenting it out of the matrix in `test/tests.zig`. As a nice bonus, this also allows the build system to run different incremental test targets in parallel, because the targets are now different steps. This definitely seems like a better way to split the work between the build system and incr-check---sorry for getting this wrong initially!

39 files changed, 258 insertions(+), 425 deletions(-)

test/incremental/add_decl-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/add_decl_namespaced-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/add_remove_struct_fields-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const S = struct { x: u8 };
test/incremental/add_remove_toplevel_fields-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const S = @This();
test/incremental/analysis_error_and_syntax_error-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub fn main() !void {
test/incremental/bad_import-7
......@@ -1,10 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
7
81#update=initial version
92#file=main.zig
103pub fn main() !void {
test/incremental/change_embed_file-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/change_enum_tag_type-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const Tag = u2;
test/incremental/change_exports-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6
71#update=initial version
82#file=main.zig
93export fn foo() void {}
test/incremental/change_fn_type-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub fn main() !void {
test/incremental/change_generic_line_number-4
......@@ -1,7 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-llvm
4//#target=wasm32-wasi-selfhosted
51#update=initial version
62#file=main.zig
73const std = @import("std");
test/incremental/change_line_number-4
......@@ -1,7 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-llvm
4//#target=wasm32-wasi-selfhosted
51#update=initial version
62#file=main.zig
73const std = @import("std");
test/incremental/change_module-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#module=foo=foo.zig
82
93#update=initial version
test/incremental/change_panic_handler-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub fn main() !u8 {
test/incremental/change_panic_handler_explicit-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub fn main() !u8 {
test/incremental/change_shift_op-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub fn main() !void {
test/incremental/change_struct_same_fields-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const S = extern struct { x: u8, y: u8 };
test/incremental/change_zon_file-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/change_zon_file_no_result_type-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/compile_error_then_log-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version with compile error
82#file=main.zig
93pub fn main() void {}
test/incremental/compile_log-7
......@@ -1,10 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
7
81#update=initial version with no compile log
92#file=main.zig
103const std = @import("std");
test/incremental/delete_comptime_decls-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub fn main() void {}
test/incremental/dependency_on_type_of_inferred_global-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const foo = @as(u8, 123);
test/incremental/fix_astgen_failure-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version with error
82#file=main.zig
93pub fn main() !void {
test/incremental/function_becomes_inline-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=non-inline version
82#file=main.zig
93pub fn main() !void {
test/incremental/hello-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/make_decl_pub-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const foo = @import("foo.zig");
test/incremental/modify_inline_fn-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/move_src-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/no_change_preserves_tag_names-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/recursive_function_becomes_non_recursive-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub fn main() !void {
test/incremental/remove_enum_field-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const MyEnum = enum(u8) {
test/incremental/remove_invalid_union_backing_enum-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const E = enum { a, b, c };
test/incremental/temporary_parse_error-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/incremental/type_becomes_comptime_only-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const SomeType = u32;
test/incremental/type_dependency_loop-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93pub const A = struct { b: B };
test/incremental/unreferenced_error-6
......@@ -1,9 +1,3 @@
1#target=x86_64-linux-selfhosted
2//#target=x86_64-windows-selfhosted
3#target=x86_64-linux-cbe
4#target=x86_64-windows-cbe
5#target=x86_64-linux-llvm
6//#target=wasm32-wasi-selfhosted
71#update=initial version
82#file=main.zig
93const std = @import("std");
test/tests.zig+37-11
......@@ -1989,6 +1989,26 @@ const c_abi_targets = blk: {
19891989 };
19901990};
19911991
1992/// Unlike `test_targets` and `c_abi_targets`, these targets are just simple strings which we pass
1993/// directly to `incr-check`. They include the target triple and the compiler backend.
1994///
1995/// If only one specific test is failing on a target, instead of entirely disabling the target here,
1996/// you can skip the target for that specific test only by adding a line like this to the manifest:
1997/// #skip_target=x86_64-linux-selfhosted
1998const incremental_targets: []const []const u8 = &.{
1999 // Avoid adding more CBE or LLVM targets without good reason: they're a lot slower than others
2000 // to run due to the output (C source code or LLVM IR) being built non-incrementally (by Clang
2001 // or LLVM). We just have a couple here to make sure that it works.
2002 "x86_64-linux-cbe",
2003 "x86_64-linux-llvm",
2004
2005 "x86_64-linux-selfhosted",
2006 // https://codeberg.org/ziglang/zig/issues/31773
2007 //"x86_64-windows-selfhosted",
2008 // https://codeberg.org/ziglang/zig/issues/31810
2009 //"wasm32-wasi-selfhosted",
2010};
2011
19922012fn compatible32bitArch(b: *std.Build) ?std.Target.Cpu.Arch {
19932013 const host = b.graph.host.result;
19942014 return switch (host.os.tag) {
......@@ -2935,21 +2955,27 @@ pub fn addIncrementalTests(b: *std.Build, test_step: *Step, test_filters: []cons
29352955 if (std.mem.indexOf(u8, entry.path, test_filter)) |_| break;
29362956 } else if (test_filters.len > 0) continue;
29372957
2938 const run = b.addRunArtifact(incr_check);
2939 run.setName(b.fmt("incr-check '{s}'", .{entry.basename}));
2958 for (incremental_targets) |target_str| {
2959 const run = b.addRunArtifact(incr_check);
2960 run.setName(b.fmt("incr-check {s} '{s}'", .{ target_str, entry.basename }));
29402961
2941 run.addArg(b.graph.zig_exe);
2942 run.addFileArg(b.path("test/incremental/").path(b, entry.path));
2943 run.addArgs(&.{ "--zig-lib-dir", b.fmt("{f}", .{b.graph.zig_lib_directory}) });
2962 run.addArg(b.graph.zig_exe);
2963 run.addFileArg(b.path("test/incremental/").path(b, entry.path));
2964 run.addArgs(&.{
2965 "--zig-lib-dir", b.graph.zig_lib_directory.path orelse ".",
2966 "--target", target_str,
2967 });
29442968
2945 if (b.enable_qemu) run.addArg("-fqemu");
2946 if (b.enable_wine) run.addArg("-fwine");
2947 if (b.enable_wasmtime) run.addArg("-fwasmtime");
2948 if (b.enable_darling) run.addArg("-fdarling");
2969 run.addArg("--quiet"); // don't fill stderr telling us about skipped tests etc
29492970
2950 run.addCheck(.{ .expect_term = .{ .exited = 0 } });
2971 if (b.enable_qemu) run.addArg("-fqemu");
2972 if (b.enable_wine) run.addArg("-fwine");
2973 if (b.enable_wasmtime) run.addArg("-fwasmtime");
2974 if (b.enable_darling) run.addArg("-fdarling");
29512975
2952 test_step.dependOn(&run.step);
2976 run.addCheck(.{ .expect_term = .{ .exited = 0 } });
2977 test_step.dependOn(&run.step);
2978 }
29532979 }
29542980}
29552981
tools/incr-check.zig+221-194
......@@ -4,32 +4,44 @@ const Dir = std.Io.Dir;
44const Allocator = std.mem.Allocator;
55const Cache = std.Build.Cache;
66
7const usage = "usage: incr-check <zig binary path> <input file> [--zig-lib-dir lib] [--debug-log foo] [--preserve-tmp] [--zig-cc-binary /path/to/zig]";
7const usage =
8 \\Usage: incr-check <zig binary path> <input file> [options]
9 \\Options:
10 \\ --target triple-backend
11 \\ --quiet
12 \\ --zig-lib-dir /path/to/zig/lib
13 \\ --zig-cc-binary /path/to/zig
14 \\ -fqemu
15 \\ -fwine
16 \\ -fwasmtime
17 \\Debug Options:
18 \\ --preserve-tmp
19 \\ --debug-log foo
20;
821
922pub const std_options: std.Options = .{
1023 .logFn = logImpl,
1124};
12var log_cur_update: ?struct { *const Case.Target, *const Case.Update } = null;
25var log_cur_update: ?*const Case.Update = null;
1326fn logImpl(
1427 comptime level: std.log.Level,
1528 comptime scope: @EnumLiteral(),
1629 comptime format: []const u8,
1730 args: anytype,
1831) void {
19 const target, const update = log_cur_update orelse {
32 const update = log_cur_update orelse {
2033 return std.log.defaultLog(level, scope, format, args);
2134 };
2235 std.log.defaultLog(
2336 level,
2437 scope,
25 "[{s}-{t} '{s}'] " ++ format,
26 .{ target.query, target.backend, update.name } ++ args,
38 "['{s}'] " ++ format,
39 .{update.name} ++ args,
2740 );
2841}
2942
3043pub fn main(init: std.process.Init) !void {
3144 const gpa = init.gpa;
32 const fatal = std.process.fatal;
3345 const arena = init.arena.allocator();
3446 const io = init.io;
3547 const environ_map = init.environ_map;
......@@ -39,11 +51,13 @@ pub fn main(init: std.process.Init) !void {
3951 var opt_input_file_name: ?[]const u8 = null;
4052 var opt_lib_dir: ?[]const u8 = null;
4153 var opt_cc_zig: ?[]const u8 = null;
54 var opt_target: ?struct { std.Target.Query, Backend } = null;
4255 var preserve_tmp = false;
4356 var enable_qemu: bool = false;
4457 var enable_wine: bool = false;
4558 var enable_wasmtime: bool = false;
4659 var enable_darling: bool = false;
60 var quiet: bool = false;
4761
4862 var debug_log_args: std.ArrayList([]const u8) = .empty;
4963
......@@ -52,11 +66,16 @@ pub fn main(init: std.process.Init) !void {
5266 while (arg_it.next()) |arg| {
5367 if (arg.len > 0 and arg[0] == '-') {
5468 if (std.mem.eql(u8, arg, "--zig-lib-dir")) {
55 opt_lib_dir = arg_it.next() orelse fatal("expected arg after --zig-lib-dir\n{s}", .{usage});
69 opt_lib_dir = arg_it.next() orelse badUsage("expected arg after --zig-lib-dir", .{});
70 } else if (std.mem.eql(u8, arg, "--target")) {
71 const str = arg_it.next() orelse badUsage("expected arg after --zig-cc-binary", .{});
72 opt_target = parseTargetQueryAndBackend(str, "");
73 } else if (std.mem.eql(u8, arg, "--quiet")) {
74 quiet = true;
5675 } else if (std.mem.eql(u8, arg, "--debug-log")) {
5776 try debug_log_args.append(
5877 arena,
59 arg_it.next() orelse fatal("expected arg after --debug-log\n{s}", .{usage}),
78 arg_it.next() orelse badUsage("expected arg after --debug-log", .{}),
6079 );
6180 } else if (std.mem.eql(u8, arg, "--preserve-tmp")) {
6281 preserve_tmp = true;
......@@ -69,9 +88,9 @@ pub fn main(init: std.process.Init) !void {
6988 } else if (std.mem.eql(u8, arg, "-fdarling")) {
7089 enable_darling = true;
7190 } else if (std.mem.eql(u8, arg, "--zig-cc-binary")) {
72 opt_cc_zig = arg_it.next() orelse fatal("expected arg after --zig-cc-binary\n{s}", .{usage});
91 opt_cc_zig = arg_it.next() orelse badUsage("expected arg after --zig-cc-binary", .{});
7392 } else {
74 fatal("unknown option '{s}'\n{s}", .{ arg, usage });
93 badUsage("unknown option '{s}'", .{arg});
7594 }
7695 continue;
7796 }
......@@ -80,24 +99,29 @@ pub fn main(init: std.process.Init) !void {
8099 } else if (opt_input_file_name == null) {
81100 opt_input_file_name = arg;
82101 } else {
83 fatal("unknown argument '{s}'\n{s}", .{ arg, usage });
102 badUsage("unknown argument '{s}'\n{s}", .{ arg, usage });
84103 }
85104 }
86 const zig_exe = opt_zig_exe orelse fatal("missing path to zig\n{s}", .{usage});
87 const input_file_name = opt_input_file_name orelse fatal("missing input file\n{s}", .{usage});
105 const zig_exe = opt_zig_exe orelse badUsage("missing path to zig", .{});
106 const input_file_name = opt_input_file_name orelse badUsage("missing input file", .{});
107 const target_query, const backend = opt_target orelse badUsage("missing required option '--target'", .{});
108
109 if (backend == .cbe and opt_lib_dir == null) {
110 std.process.fatal("'--zig-lib-dir' required when using backend 'cbe'", .{});
111 }
88112
89113 const input_file_bytes = try Dir.cwd().readFileAlloc(io, input_file_name, arena, .limited(std.math.maxInt(u32)));
90 const case = try Case.parse(arena, io, input_file_bytes);
114 const case: Case = try .parse(arena, input_file_bytes);
91115
92 // Check now: if there are any targets using the `cbe` backend, we need the lib dir.
93 if (opt_lib_dir == null) {
94 for (case.targets) |target| {
95 if (target.backend == .cbe) {
96 fatal("'--zig-lib-dir' requried when using backend 'cbe'", .{});
97 }
116 for (case.skip_targets) |skip| {
117 if (target_query.eql(skip.query) and backend == skip.backend) {
118 if (!quiet) std.log.warn("skipping test because of a 'skip_target' match", .{});
119 return;
98120 }
99121 }
100122
123 const target = try std.zig.system.resolveTargetQuery(io, target_query);
124
101125 const prog_node = std.Progress.start(io, .{});
102126 defer prog_node.end();
103127
......@@ -122,143 +146,137 @@ pub fn main(init: std.process.Init) !void {
122146
123147 const host = try std.zig.system.resolveTargetQuery(io, .{});
124148
125 const debug_log_verbose = debug_log_args.items.len != 0;
126
127 for (case.targets) |target| {
128 const target_prog_node = node: {
129 var name_buf: [std.Progress.Node.max_name_len]u8 = undefined;
130 const name = std.fmt.bufPrint(&name_buf, "{s}-{t}", .{ target.query, target.backend }) catch &name_buf;
131 break :node prog_node.start(name, case.updates.len);
132 };
133 defer target_prog_node.end();
134
135 if (debug_log_verbose) {
136 std.log.scoped(.status).info("target: '{s}-{t}'", .{ target.query, target.backend });
137 }
138 var child_args: std.ArrayList([]const u8) = .empty;
139 try child_args.appendSlice(arena, &.{
140 resolved_zig_exe,
141 "build-exe",
142 "-fincremental",
143 "-fno-ubsan-rt",
144 "-target",
145 target.query,
146 "--cache-dir",
147 ".local-cache",
148 "--global-cache-dir",
149 ".global-cache",
150 });
151 try child_args.append(arena, "--listen=-");
152
153 if (opt_resolved_lib_dir) |resolved_lib_dir| {
154 try child_args.appendSlice(arena, &.{ "--zig-lib-dir", resolved_lib_dir });
155 }
156 switch (target.backend) {
157 .sema => try child_args.append(arena, "-fno-emit-bin"),
158 .selfhosted => try child_args.appendSlice(arena, &.{ "-fno-llvm", "-fno-lld" }),
159 .llvm => try child_args.appendSlice(arena, &.{ "-fllvm", "-flld" }),
160 .cbe => try child_args.appendSlice(arena, &.{ "-ofmt=c", "-lc" }),
161 }
162 for (debug_log_args.items) |arg| {
163 try child_args.appendSlice(arena, &.{ "--debug-log", arg });
164 }
165 for (case.modules) |mod| {
166 try child_args.appendSlice(arena, &.{ "--dep", mod.name });
167 }
168 try child_args.append(arena, try std.fmt.allocPrint(arena, "-Mroot={s}", .{case.root_source_file}));
169 for (case.modules) |mod| {
170 try child_args.append(arena, try std.fmt.allocPrint(arena, "-M{s}={s}", .{ mod.name, mod.file }));
171 }
149 var child_args: std.ArrayList([]const u8) = .empty;
150 try child_args.appendSlice(arena, &.{
151 resolved_zig_exe,
152 "build-exe",
153 "-fincremental",
154 "-fno-ubsan-rt",
155 "-target",
156 try target_query.zigTriple(arena),
157 "--cache-dir",
158 ".local-cache",
159 "--global-cache-dir",
160 ".global-cache",
161 });
162 try child_args.append(arena, "--listen=-");
163
164 if (opt_resolved_lib_dir) |resolved_lib_dir| {
165 try child_args.appendSlice(arena, &.{ "--zig-lib-dir", resolved_lib_dir });
166 }
167 switch (backend) {
168 .sema => try child_args.append(arena, "-fno-emit-bin"),
169 .selfhosted => try child_args.appendSlice(arena, &.{ "-fno-llvm", "-fno-lld" }),
170 .llvm => try child_args.appendSlice(arena, &.{ "-fllvm", "-flld" }),
171 .cbe => try child_args.appendSlice(arena, &.{ "-ofmt=c", "-lc" }),
172 }
173 for (debug_log_args.items) |arg| {
174 try child_args.appendSlice(arena, &.{ "--debug-log", arg });
175 }
176 for (case.modules) |mod| {
177 try child_args.appendSlice(arena, &.{ "--dep", mod.name });
178 }
179 try child_args.append(arena, try std.fmt.allocPrint(arena, "-Mroot={s}", .{case.root_source_file}));
180 for (case.modules) |mod| {
181 try child_args.append(arena, try std.fmt.allocPrint(arena, "-M{s}={s}", .{ mod.name, mod.file }));
182 }
172183
173 const zig_prog_node = target_prog_node.start("zig build-exe", 0);
174 defer zig_prog_node.end();
175
176 var cc_child_args: std.ArrayList([]const u8) = .empty;
177 if (target.backend == .cbe) {
178 const resolved_cc_zig_exe = if (opt_cc_zig) |cc_zig_exe|
179 try Dir.path.relative(arena, cwd_path, environ_map, tmp_dir_path, cc_zig_exe)
180 else
181 resolved_zig_exe;
182
183 try cc_child_args.appendSlice(arena, &.{
184 resolved_cc_zig_exe,
185 "cc",
186 "-target",
187 target.query,
188 "-I",
189 opt_resolved_lib_dir.?, // verified earlier
190 });
184 const zig_prog_node = prog_node.start("zig", 0);
185 defer zig_prog_node.end();
191186
192 try cc_child_args.append(arena, "-o");
193 }
187 var cc_child_args: std.ArrayList([]const u8) = .empty;
188 if (backend == .cbe) {
189 const resolved_cc_zig_exe = if (opt_cc_zig) |cc_zig_exe|
190 try Dir.path.relative(arena, cwd_path, environ_map, tmp_dir_path, cc_zig_exe)
191 else
192 resolved_zig_exe;
194193
195 var child = try std.process.spawn(io, .{
196 .argv = child_args.items,
197 .stdin = .pipe,
198 .stdout = .pipe,
199 .stderr = .pipe,
200 .progress_node = zig_prog_node,
201 .cwd = .{ .path = tmp_dir_path },
194 try cc_child_args.appendSlice(arena, &.{
195 resolved_cc_zig_exe,
196 "cc",
197 "-target",
198 try target_query.zigTriple(arena),
199 "-I",
200 opt_resolved_lib_dir.?, // verified earlier
202201 });
203 defer child.kill(io);
204
205 var eval: Eval = .{
206 .arena = arena,
207 .io = io,
208 .case = case,
209 .host = host,
210 .target = target,
211 .tmp_dir = tmp_dir,
212 .tmp_dir_path = tmp_dir_path,
213 .child = &child,
214 .allow_stderr = debug_log_verbose,
215 .preserve_tmp_on_fatal = preserve_tmp,
216 .cc_child_args = &cc_child_args,
217 .enable_qemu = enable_qemu,
218 .enable_wine = enable_wine,
219 .enable_wasmtime = enable_wasmtime,
220 .enable_darling = enable_darling,
221 };
222202
223 var multi_reader_buffer: Io.File.MultiReader.Buffer(2) = undefined;
224 var multi_reader: Io.File.MultiReader = undefined;
225 multi_reader.init(gpa, io, multi_reader_buffer.toStreams(), &.{ child.stdout.?, child.stderr.? });
226 defer multi_reader.deinit();
203 try cc_child_args.append(arena, "-o");
204 }
227205
228 for (case.updates) |update| {
229 var update_node = target_prog_node.start(update.name, 0);
230 defer update_node.end();
206 var child = try std.process.spawn(io, .{
207 .argv = child_args.items,
208 .stdin = .pipe,
209 .stdout = .pipe,
210 .stderr = .pipe,
211 .progress_node = zig_prog_node,
212 .cwd = .{ .path = tmp_dir_path },
213 });
214 defer child.kill(io);
215
216 const updates_prog_node = prog_node.start("updates", case.updates.len);
217 defer updates_prog_node.end();
218
219 var eval: Eval = .{
220 .arena = arena,
221 .io = io,
222 .case = case,
223 .host = host,
224 .target = target,
225 .backend = backend,
226 .tmp_dir = tmp_dir,
227 .tmp_dir_path = tmp_dir_path,
228 .child = &child,
229 .allow_compiler_stderr = debug_log_args.items.len != 0,
230 .quiet = quiet,
231 .preserve_tmp_on_fatal = preserve_tmp,
232 .cc_child_args = &cc_child_args,
233 .enable_qemu = enable_qemu,
234 .enable_wine = enable_wine,
235 .enable_wasmtime = enable_wasmtime,
236 .enable_darling = enable_darling,
237 };
231238
232 if (debug_log_verbose) {
233 std.log.scoped(.status).info("update: '{s}'", .{update.name});
234 }
239 var multi_reader_buffer: Io.File.MultiReader.Buffer(2) = undefined;
240 var multi_reader: Io.File.MultiReader = undefined;
241 multi_reader.init(gpa, io, multi_reader_buffer.toStreams(), &.{ child.stdout.?, child.stderr.? });
242 defer multi_reader.deinit();
235243
236 log_cur_update = .{ &target, &update };
237 defer log_cur_update = null;
244 for (case.updates) |update| {
245 var update_prog_node = updates_prog_node.start(update.name, 0);
246 defer update_prog_node.end();
238247
239 eval.write(update);
240 try eval.requestUpdate();
241 try eval.check(&multi_reader, update, update_node);
248 if (debug_log_args.items.len != 0) {
249 // Print a line separating the debug logs from the compiler in the stderr output.
250 std.log.scoped(.status).info("update: '{s}'", .{update.name});
242251 }
243252
244 try eval.end(&multi_reader);
253 log_cur_update = &update;
254 defer log_cur_update = null;
245255
246 waitChild(&child, &eval);
256 eval.write(update);
257 try eval.requestUpdate();
258 try eval.check(&multi_reader, update, update_prog_node);
247259 }
260
261 try eval.end(&multi_reader);
262
263 waitChild(&child, &eval);
248264}
249265
250266const Eval = struct {
251267 arena: Allocator,
252268 io: Io,
253 host: std.Target,
254269 case: Case,
255 target: Case.Target,
270 host: std.Target,
271 target: std.Target,
272 backend: Backend,
256273 tmp_dir: Dir,
257274 tmp_dir_path: []const u8,
258275 child: *std.process.Child,
259 allow_stderr: bool,
276 allow_compiler_stderr: bool,
277 quiet: bool,
260278 preserve_tmp_on_fatal: bool,
261 /// When `target.backend == .cbe`, this contains the first few arguments to `zig cc` to build the generated binary.
279 /// When `backend == .cbe`, this contains the first few arguments to `zig cc` to build the generated binary.
262280 /// The arguments `out.c in.c` must be appended before spawning the subprocess.
263281 cc_child_args: *std.ArrayList([]const u8),
264282
......@@ -307,7 +325,7 @@ const Eval = struct {
307325 .error_bundle => {
308326 const result_error_bundle = try std.zig.Server.allocErrorBundle(arena, body);
309327 if (stderr.bufferedLen() > 0) {
310 if (eval.allow_stderr) {
328 if (eval.allow_compiler_stderr) {
311329 std.log.info("error_bundle stderr:\n{s}", .{stderr.buffered()});
312330 } else {
313331 eval.fatal("error_bundle unexpected stderr:\n{s}", .{stderr.buffered()});
......@@ -325,14 +343,14 @@ const Eval = struct {
325343 _ = r.takeStruct(std.zig.Server.Message.EmitDigest, .little) catch unreachable;
326344
327345 if (stderr.bufferedLen() > 0) {
328 if (eval.allow_stderr) {
346 if (eval.allow_compiler_stderr) {
329347 std.log.info("emit_digest stderr:\n{s}", .{stderr.buffered()});
330348 } else {
331349 eval.fatal("emit_digest unexpected stderr:\n{s}", .{stderr.buffered()});
332350 }
333351 stderr.tossBuffered();
334352 }
335 if (eval.target.backend == .sema) {
353 if (eval.backend == .sema) {
336354 try eval.checkSuccessOutcome(update, null, prog_node);
337355 continue;
338356 }
......@@ -342,7 +360,7 @@ const Eval = struct {
342360
343361 const bin_name = try std.zig.EmitArtifact.bin.cacheName(arena, .{
344362 .root_name = "root", // corresponds to the module name "root"
345 .target = &eval.target.resolved,
363 .target = &eval.target,
346364 .output_mode = .Exe,
347365 });
348366 const bin_path = try Dir.path.join(arena, &.{ result_dir, bin_name });
......@@ -357,7 +375,7 @@ const Eval = struct {
357375
358376 const buffered_stderr = stderr.buffered();
359377 if (buffered_stderr.len > 0) {
360 if (eval.allow_stderr) {
378 if (eval.allow_compiler_stderr) {
361379 std.log.info("stderr:\n{s}", .{buffered_stderr});
362380 } else {
363381 eval.fatal("unexpected stderr:\n{s}", .{buffered_stderr});
......@@ -450,12 +468,12 @@ const Eval = struct {
450468 .stdout, .exit_code => {},
451469 }
452470 const emitted_path = opt_emitted_path orelse {
453 std.debug.assert(eval.target.backend == .sema);
471 std.debug.assert(eval.backend == .sema);
454472 return;
455473 };
456474 const io = eval.io;
457475
458 const binary_path = switch (eval.target.backend) {
476 const binary_path = switch (eval.backend) {
459477 .sema => unreachable,
460478 .selfhosted, .llvm => emitted_path,
461479 .cbe => bin: {
......@@ -470,15 +488,15 @@ const Eval = struct {
470488 const argv: []const []const u8, const is_foreign: bool = sw: switch (std.zig.system.getExternalExecutor(
471489 io,
472490 &eval.host,
473 &eval.target.resolved,
474 .{ .link_libc = eval.target.backend == .cbe },
491 &eval.target,
492 .{ .link_libc = eval.backend == .cbe },
475493 )) {
476494 .bad_dl, .bad_os_or_cpu => {
477495 // This binary cannot be executed on this host.
478 if (eval.allow_stderr) {
496 if (!eval.quiet) {
479497 std.log.warn("skipping execution because host '{s}' cannot execute binaries for foreign target '{s}'", .{
480498 try eval.host.zigTriple(eval.arena),
481 try eval.target.resolved.zigTriple(eval.arena),
499 try eval.target.zigTriple(eval.arena),
482500 });
483501 }
484502 return;
......@@ -534,10 +552,10 @@ const Eval = struct {
534552 }) catch |err| {
535553 if (is_foreign) {
536554 // Chances are the foreign executor isn't available. Skip this evaluation.
537 if (eval.allow_stderr) {
555 if (!eval.quiet) {
538556 std.log.warn("skipping execution of '{s}' via executor for foreign target '{s}': {t}", .{
539557 binary_path,
540 try eval.target.resolved.zigTriple(eval.arena),
558 try eval.target.zigTriple(eval.arena),
541559 err,
542560 });
543561 }
......@@ -658,30 +676,30 @@ const Eval = struct {
658676 }
659677};
660678
679const Backend = enum {
680 /// Run semantic analysis only. Runtime output will not be tested, but we still verify
681 /// that compilation succeeds. Corresponds to `-fno-emit-bin`.
682 sema,
683 /// Use the self-hosted code generation backend for this target.
684 /// Corresponds to `-fno-llvm -fno-lld`.
685 selfhosted,
686 /// Use the LLVM backend.
687 /// Corresponds to `-fllvm -flld`.
688 llvm,
689 /// Use the C backend. The output is compiled with `zig cc`.
690 /// Corresponds to `-ofmt=c`.
691 cbe,
692};
693
661694const Case = struct {
662695 updates: []Update,
663696 root_source_file: []const u8,
664 targets: []const Target,
697 skip_targets: []const SkipTarget,
665698 modules: []const Module,
666699
667 const Target = struct {
668 query: []const u8,
669 resolved: std.Target,
700 const SkipTarget = struct {
701 query: std.Target.Query,
670702 backend: Backend,
671 const Backend = enum {
672 /// Run semantic analysis only. Runtime output will not be tested, but we still verify
673 /// that compilation succeeds. Corresponds to `-fno-emit-bin`.
674 sema,
675 /// Use the self-hosted code generation backend for this target.
676 /// Corresponds to `-fno-llvm -fno-lld`.
677 selfhosted,
678 /// Use the LLVM backend.
679 /// Corresponds to `-fllvm -flld`.
680 llvm,
681 /// Use the C backend. The output is compiled with `zig cc`.
682 /// Corresponds to `-ofmt=c`.
683 cbe,
684 };
685703 };
686704
687705 const Module = struct {
......@@ -721,10 +739,10 @@ const Case = struct {
721739 },
722740 };
723741
724 fn parse(arena: Allocator, io: Io, bytes: []const u8) !Case {
742 fn parse(arena: Allocator, bytes: []const u8) !Case {
725743 const fatal = std.process.fatal;
726744
727 var targets: std.ArrayList(Target) = .empty;
745 var skip_targets: std.ArrayList(SkipTarget) = .empty;
728746 var modules: std.ArrayList(Module) = .empty;
729747 var updates: std.ArrayList(Update) = .empty;
730748 var changes: std.ArrayList(FullContents) = .empty;
......@@ -739,29 +757,13 @@ const Case = struct {
739757 const val = std.mem.trimEnd(u8, line_it.rest(), "\r"); // windows moment
740758 if (val.len == 0) {
741759 fatal("line {d}: missing value", .{line_n});
742 } else if (std.mem.eql(u8, key, "target")) {
743 const split_idx = std.mem.lastIndexOfScalar(u8, val, '-') orelse
744 fatal("line {d}: target does not include backend", .{line_n});
745
746 const query = val[0..split_idx];
747
748 const backend_str = val[split_idx + 1 ..];
749 const backend: Target.Backend = std.meta.stringToEnum(Target.Backend, backend_str) orelse
750 fatal("line {d}: invalid backend '{s}'", .{ line_n, backend_str });
751
752 const parsed_query = std.Build.parseTargetQuery(.{
753 .arch_os_abi = query,
754 .object_format = switch (backend) {
755 .sema, .selfhosted, .llvm => null,
756 .cbe => "c",
757 },
758 }) catch fatal("line {d}: invalid target query '{s}'", .{ line_n, query });
759
760 const resolved = try std.zig.system.resolveTargetQuery(io, parsed_query);
761
762 try targets.append(arena, .{
760 } else if (std.mem.eql(u8, key, "skip_target")) {
761 const query, const backend = parseTargetQueryAndBackend(
762 val,
763 try std.fmt.allocPrint(arena, "line {d}: ", .{line_n}),
764 );
765 try skip_targets.append(arena, .{
763766 .query = query,
764 .resolved = resolved,
765767 .backend = backend,
766768 });
767769 } else if (std.mem.eql(u8, key, "module")) {
......@@ -872,10 +874,6 @@ const Case = struct {
872874 }
873875 }
874876
875 if (targets.items.len == 0) {
876 fatal("missing target", .{});
877 }
878
879877 if (changes.items.len > 0) {
880878 const last_update = &updates.items[updates.items.len - 1];
881879 last_update.changes = changes.items; // arena so no need for toOwnedSlice
......@@ -885,7 +883,7 @@ const Case = struct {
885883 return .{
886884 .updates = updates.items,
887885 .root_source_file = root_source_file orelse fatal("missing root source file", .{}),
888 .targets = targets.items, // arena so no need for toOwnedSlice
886 .skip_targets = skip_targets.items, // arena so no need for toOwnedSlice
889887 .modules = modules.items,
890888 };
891889 }
......@@ -970,3 +968,32 @@ fn rand64(io: Io) u64 {
970968 io.random(@ptrCast(&x));
971969 return x;
972970}
971
972/// Calls `std.process.fatal` on error. The error messages are prefixed with `err_prefix`.
973fn parseTargetQueryAndBackend(input_str: []const u8, err_prefix: []const u8) struct { std.Target.Query, Backend } {
974 const fatal = std.process.fatal;
975
976 const split_idx = std.mem.lastIndexOfScalar(u8, input_str, '-') orelse
977 fatal("{s}target does not include backend", .{err_prefix});
978
979 const query = input_str[0..split_idx];
980
981 const backend_str = input_str[split_idx + 1 ..];
982 const backend: Backend = std.meta.stringToEnum(Backend, backend_str) orelse
983 fatal("{s}invalid backend '{s}'", .{ err_prefix, backend_str });
984
985 const parsed_query = std.Build.parseTargetQuery(.{
986 .arch_os_abi = query,
987 .object_format = switch (backend) {
988 .sema, .selfhosted, .llvm => null,
989 .cbe => "c",
990 },
991 }) catch fatal("{s}invalid target query '{s}'", .{ err_prefix, query });
992
993 return .{ parsed_query, backend };
994}
995
996fn badUsage(comptime fmt: []const u8, args: anytype) noreturn {
997 std.log.err(fmt ++ "\n{s}", args ++ .{usage});
998 std.process.exit(1);
999}