authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-06 19:10:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:13-07:00
loga24af8e4005d00cf76755635eb6c661d9c260758
tree3a70bbe7f7f3d63b4109296853be8e0ce28e7e07
parent7bad6958657c6b4a791c73a3f827f7f848cab762

re-integrate stack trace tests with the new std.Build API

* RunStep: ability to set stdin * RunStep: ability to capture stdout and stderr as a FileSource * RunStep: add setName method * RunStep: hash the stdio checks

7 files changed, 637 insertions(+), 727 deletions(-)

build.zig+22-22
...@@ -442,33 +442,33 @@ pub fn build(b: *std.Build) !void {...@@ -442,33 +442,33 @@ pub fn build(b: *std.Build) !void {
442 .skip_stage2 = true, // TODO get all these passing442 .skip_stage2 = true, // TODO get all these passing
443 }));443 }));
444444
445 test_step.dependOn(tests.addCompareOutputTests(b, test_filter, optimization_modes));445 _ = enable_symlinks_windows;
446 test_step.dependOn(tests.addStandaloneTests(446 _ = enable_macos_sdk;
447 b,447 //test_step.dependOn(tests.addCompareOutputTests(b, test_filter, optimization_modes));
448 test_filter,448 //test_step.dependOn(tests.addStandaloneTests(
449 optimization_modes,449 // b,
450 skip_non_native,450 // test_filter,
451 enable_macos_sdk,451 // optimization_modes,
452 target,452 // skip_non_native,
453 skip_stage2_tests,453 // enable_macos_sdk,
454 b.enable_darling,454 // target,
455 b.enable_qemu,455 // skip_stage2_tests,
456 b.enable_rosetta,456 // b.enable_darling,
457 b.enable_wasmtime,457 // b.enable_qemu,
458 b.enable_wine,458 // b.enable_rosetta,
459 enable_symlinks_windows,459 // b.enable_wasmtime,
460 ));460 // b.enable_wine,
461 test_step.dependOn(tests.addCAbiTests(b, skip_non_native, skip_release));461 // enable_symlinks_windows,
462 test_step.dependOn(tests.addLinkTests(b, test_filter, optimization_modes, enable_macos_sdk, skip_stage2_tests, enable_symlinks_windows));462 //));
463 //test_step.dependOn(tests.addCAbiTests(b, skip_non_native, skip_release));
464 //test_step.dependOn(tests.addLinkTests(b, test_filter, optimization_modes, enable_macos_sdk, skip_stage2_tests, enable_symlinks_windows));
463 test_step.dependOn(tests.addStackTraceTests(b, test_filter, optimization_modes));465 test_step.dependOn(tests.addStackTraceTests(b, test_filter, optimization_modes));
464 test_step.dependOn(tests.addCliTests(b, test_filter, optimization_modes));466 //test_step.dependOn(tests.addCliTests(b, test_filter, optimization_modes));
465 test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, optimization_modes));467 //test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, optimization_modes));
466 test_step.dependOn(tests.addTranslateCTests(b, test_filter));468 test_step.dependOn(tests.addTranslateCTests(b, test_filter));
467 if (!skip_run_translated_c) {469 if (!skip_run_translated_c) {
468 test_step.dependOn(tests.addRunTranslatedCTests(b, test_filter, target));470 test_step.dependOn(tests.addRunTranslatedCTests(b, test_filter, target));
469 }471 }
470 // tests for this feature are disabled until we have the self-hosted compiler available
471 // test_step.dependOn(tests.addGenHTests(b, test_filter));
472472
473 test_step.dependOn(tests.addModuleTests(b, .{473 test_step.dependOn(tests.addModuleTests(b, .{
474 .test_filter = test_filter,474 .test_filter = test_filter,
lib/std/Build/RunStep.zig+195-51
...@@ -38,6 +38,8 @@ env_map: ?*EnvMap,...@@ -38,6 +38,8 @@ env_map: ?*EnvMap,
38/// be skipped if all output files are up-to-date and input files are38/// be skipped if all output files are up-to-date and input files are
39/// unchanged.39/// unchanged.
40stdio: StdIo = .infer_from_args,40stdio: StdIo = .infer_from_args,
41/// This field must be `null` if stdio is `inherit`.
42stdin: ?[]const u8 = null,
4143
42/// Additional file paths relative to build.zig that, when modified, indicate44/// Additional file paths relative to build.zig that, when modified, indicate
43/// that the RunStep should be re-executed.45/// that the RunStep should be re-executed.
...@@ -65,6 +67,9 @@ skip_foreign_checks: bool = false,...@@ -65,6 +67,9 @@ skip_foreign_checks: bool = false,
65/// the step fails.67/// the step fails.
66max_stdio_size: usize = 10 * 1024 * 1024,68max_stdio_size: usize = 10 * 1024 * 1024,
6769
70captured_stdout: ?*Output = null,
71captured_stderr: ?*Output = null,
72
68pub const StdIo = union(enum) {73pub const StdIo = union(enum) {
69 /// Whether the RunStep has side-effects will be determined by whether or not one74 /// Whether the RunStep has side-effects will be determined by whether or not one
70 /// of the args is an output file (added with `addOutputFileArg`).75 /// of the args is an output file (added with `addOutputFileArg`).
...@@ -99,12 +104,12 @@ pub const Arg = union(enum) {...@@ -99,12 +104,12 @@ pub const Arg = union(enum) {
99 artifact: *CompileStep,104 artifact: *CompileStep,
100 file_source: std.Build.FileSource,105 file_source: std.Build.FileSource,
101 bytes: []u8,106 bytes: []u8,
102 output: Output,107 output: *Output,
108};
103109
104 pub const Output = struct {110pub const Output = struct {
105 generated_file: *std.Build.GeneratedFile,111 generated_file: std.Build.GeneratedFile,
106 basename: []const u8,112 basename: []const u8,
107 };
108};113};
109114
110pub fn create(owner: *std.Build, name: []const u8) *RunStep {115pub fn create(owner: *std.Build, name: []const u8) *RunStep {
...@@ -119,12 +124,15 @@ pub fn create(owner: *std.Build, name: []const u8) *RunStep {...@@ -119,12 +124,15 @@ pub fn create(owner: *std.Build, name: []const u8) *RunStep {
119 .argv = ArrayList(Arg).init(owner.allocator),124 .argv = ArrayList(Arg).init(owner.allocator),
120 .cwd = null,125 .cwd = null,
121 .env_map = null,126 .env_map = null,
122 .rename_step_with_output_arg = true,
123 .max_stdio_size = 10 * 1024 * 1024,
124 };127 };
125 return self;128 return self;
126}129}
127130
131pub fn setName(self: *RunStep, name: []const u8) void {
132 self.step.name = name;
133 self.rename_step_with_output_arg = false;
134}
135
128pub fn addArtifactArg(self: *RunStep, artifact: *CompileStep) void {136pub fn addArtifactArg(self: *RunStep, artifact: *CompileStep) void {
129 self.argv.append(Arg{ .artifact = artifact }) catch @panic("OOM");137 self.argv.append(Arg{ .artifact = artifact }) catch @panic("OOM");
130 self.step.dependOn(&artifact.step);138 self.step.dependOn(&artifact.step);
...@@ -135,19 +143,19 @@ pub fn addArtifactArg(self: *RunStep, artifact: *CompileStep) void {...@@ -135,19 +143,19 @@ pub fn addArtifactArg(self: *RunStep, artifact: *CompileStep) void {
135/// throughout the build system.143/// throughout the build system.
136pub fn addOutputFileArg(rs: *RunStep, basename: []const u8) std.Build.FileSource {144pub fn addOutputFileArg(rs: *RunStep, basename: []const u8) std.Build.FileSource {
137 const b = rs.step.owner;145 const b = rs.step.owner;
138 const generated_file = b.allocator.create(std.Build.GeneratedFile) catch @panic("OOM");146
139 generated_file.* = .{ .step = &rs.step };147 const output = b.allocator.create(Output) catch @panic("OOM");
140 rs.argv.append(.{ .output = .{148 output.* = .{
141 .generated_file = generated_file,149 .basename = basename,
142 .basename = b.dupe(basename),150 .generated_file = .{ .step = &rs.step },
143 } }) catch @panic("OOM");151 };
152 rs.argv.append(.{ .output = output }) catch @panic("OOM");
144153
145 if (rs.rename_step_with_output_arg) {154 if (rs.rename_step_with_output_arg) {
146 rs.rename_step_with_output_arg = false;155 rs.setName(b.fmt("{s} ({s})", .{ rs.step.name, basename }));
147 rs.step.name = b.fmt("{s} ({s})", .{ rs.step.name, basename });
148 }156 }
149157
150 return .{ .generated = generated_file };158 return .{ .generated = &output.generated_file };
151}159}
152160
153pub fn addFileSourceArg(self: *RunStep, file_source: std.Build.FileSource) void {161pub fn addFileSourceArg(self: *RunStep, file_source: std.Build.FileSource) void {
...@@ -259,6 +267,34 @@ pub fn addCheck(self: *RunStep, new_check: StdIo.Check) void {...@@ -259,6 +267,34 @@ pub fn addCheck(self: *RunStep, new_check: StdIo.Check) void {
259 }267 }
260}268}
261269
270pub fn captureStdErr(self: *RunStep) std.Build.FileSource {
271 assert(self.stdio != .inherit);
272
273 if (self.captured_stderr) |output| return .{ .generated = &output.generated_file };
274
275 const output = self.step.owner.allocator.create(Output) catch @panic("OOM");
276 output.* = .{
277 .basename = "stderr",
278 .generated_file = .{ .step = &self.step },
279 };
280 self.captured_stderr = output;
281 return .{ .generated = &output.generated_file };
282}
283
284pub fn captureStdOut(self: *RunStep) *std.Build.GeneratedFile {
285 assert(self.stdio != .inherit);
286
287 if (self.captured_stdout) |output| return .{ .generated = &output.generated_file };
288
289 const output = self.step.owner.allocator.create(Output) catch @panic("OOM");
290 output.* = .{
291 .basename = "stdout",
292 .generated_file = .{ .step = &self.step },
293 };
294 self.captured_stdout = output;
295 return .{ .generated = &output.generated_file };
296}
297
262/// Returns whether the RunStep has side effects *other than* updating the output arguments.298/// Returns whether the RunStep has side effects *other than* updating the output arguments.
263fn hasSideEffects(self: RunStep) bool {299fn hasSideEffects(self: RunStep) bool {
264 return switch (self.stdio) {300 return switch (self.stdio) {
...@@ -269,6 +305,8 @@ fn hasSideEffects(self: RunStep) bool {...@@ -269,6 +305,8 @@ fn hasSideEffects(self: RunStep) bool {
269}305}
270306
271fn hasAnyOutputArgs(self: RunStep) bool {307fn hasAnyOutputArgs(self: RunStep) bool {
308 if (self.captured_stdout != null) return true;
309 if (self.captured_stderr != null) return true;
272 for (self.argv.items) |arg| switch (arg) {310 for (self.argv.items) |arg| switch (arg) {
273 .output => return true,311 .output => return true,
274 else => continue,312 else => continue,
...@@ -318,7 +356,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -318,7 +356,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
318 var argv_list = ArrayList([]const u8).init(arena);356 var argv_list = ArrayList([]const u8).init(arena);
319 var output_placeholders = ArrayList(struct {357 var output_placeholders = ArrayList(struct {
320 index: usize,358 index: usize,
321 output: Arg.Output,359 output: *Output,
322 }).init(arena);360 }).init(arena);
323361
324 var man = b.cache.obtain();362 var man = b.cache.obtain();
...@@ -361,46 +399,68 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -361,46 +399,68 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
361 }399 }
362 }400 }
363401
364 if (!has_side_effects) {402 if (self.captured_stdout) |output| {
365 for (self.extra_file_dependencies) |file_path| {403 man.hash.addBytes(output.basename);
366 _ = try man.addFile(b.pathFromRoot(file_path), null);404 }
367 }
368405
369 if (try step.cacheHit(&man)) {406 if (self.captured_stderr) |output| {
370 // cache hit, skip running command407 man.hash.addBytes(output.basename);
371 const digest = man.final();408 }
372 for (output_placeholders.items) |placeholder| {
373 placeholder.output.generated_file.path = try b.cache_root.join(
374 arena,
375 &.{ "o", &digest, placeholder.output.basename },
376 );
377 }
378 step.result_cached = true;
379 return;
380 }
381409
382 const digest = man.final();410 hashStdIo(&man.hash, self.stdio);
383411
412 if (has_side_effects) {
413 try runCommand(self, argv_list.items, has_side_effects, null);
414 return;
415 }
416
417 for (self.extra_file_dependencies) |file_path| {
418 _ = try man.addFile(b.pathFromRoot(file_path), null);
419 }
420
421 if (try step.cacheHit(&man)) {
422 // cache hit, skip running command
423 const digest = man.final();
384 for (output_placeholders.items) |placeholder| {424 for (output_placeholders.items) |placeholder| {
385 const output_components = .{ "o", &digest, placeholder.output.basename };425 placeholder.output.generated_file.path = try b.cache_root.join(arena, &.{
386 const output_sub_path = try fs.path.join(arena, &output_components);426 "o", &digest, placeholder.output.basename,
387 const output_sub_dir_path = fs.path.dirname(output_sub_path).?;427 });
388 b.cache_root.handle.makePath(output_sub_dir_path) catch |err| {
389 return step.fail("unable to make path '{}{s}': {s}", .{
390 b.cache_root, output_sub_dir_path, @errorName(err),
391 });
392 };
393 const output_path = try b.cache_root.join(arena, &output_components);
394 placeholder.output.generated_file.path = output_path;
395 argv_list.items[placeholder.index] = output_path;
396 }428 }
429
430 if (self.captured_stdout) |output| {
431 output.generated_file.path = try b.cache_root.join(arena, &.{
432 "o", &digest, output.basename,
433 });
434 }
435
436 if (self.captured_stderr) |output| {
437 output.generated_file.path = try b.cache_root.join(arena, &.{
438 "o", &digest, output.basename,
439 });
440 }
441
442 step.result_cached = true;
443 return;
397 }444 }
398445
399 try runCommand(self, argv_list.items, has_side_effects);446 const digest = man.final();
400447
401 if (!has_side_effects) {448 for (output_placeholders.items) |placeholder| {
402 try man.writeManifest();449 const output_components = .{ "o", &digest, placeholder.output.basename };
450 const output_sub_path = try fs.path.join(arena, &output_components);
451 const output_sub_dir_path = fs.path.dirname(output_sub_path).?;
452 b.cache_root.handle.makePath(output_sub_dir_path) catch |err| {
453 return step.fail("unable to make path '{}{s}': {s}", .{
454 b.cache_root, output_sub_dir_path, @errorName(err),
455 });
456 };
457 const output_path = try b.cache_root.join(arena, &output_components);
458 placeholder.output.generated_file.path = output_path;
459 argv_list.items[placeholder.index] = output_path;
403 }460 }
461
462 try runCommand(self, argv_list.items, has_side_effects, &digest);
463 try man.writeManifest();
404}464}
405465
406fn formatTerm(466fn formatTerm(
...@@ -448,7 +508,12 @@ fn termMatches(expected: ?std.process.Child.Term, actual: std.process.Child.Term...@@ -448,7 +508,12 @@ fn termMatches(expected: ?std.process.Child.Term, actual: std.process.Child.Term
448 };508 };
449}509}
450510
451fn runCommand(self: *RunStep, argv: []const []const u8, has_side_effects: bool) !void {511fn runCommand(
512 self: *RunStep,
513 argv: []const []const u8,
514 has_side_effects: bool,
515 digest: ?*const [std.Build.Cache.hex_digest_len]u8,
516) !void {
452 const step = &self.step;517 const step = &self.step;
453 const b = step.owner;518 const b = step.owner;
454 const arena = b.allocator;519 const arena = b.allocator;
...@@ -584,6 +649,46 @@ fn runCommand(self: *RunStep, argv: []const []const u8, has_side_effects: bool)...@@ -584,6 +649,46 @@ fn runCommand(self: *RunStep, argv: []const []const u8, has_side_effects: bool)
584 step.result_duration_ns = result.elapsed_ns;649 step.result_duration_ns = result.elapsed_ns;
585 step.result_peak_rss = result.peak_rss;650 step.result_peak_rss = result.peak_rss;
586651
652 // Capture stdout and stderr to GeneratedFile objects.
653 const Stream = struct {
654 captured: ?*Output,
655 is_null: bool,
656 bytes: []const u8,
657 };
658 for ([_]Stream{
659 .{
660 .captured = self.captured_stdout,
661 .is_null = result.stdout_null,
662 .bytes = result.stdout,
663 },
664 .{
665 .captured = self.captured_stderr,
666 .is_null = result.stderr_null,
667 .bytes = result.stderr,
668 },
669 }) |stream| {
670 if (stream.captured) |output| {
671 assert(!stream.is_null);
672
673 const output_components = .{ "o", digest.?, output.basename };
674 const output_path = try b.cache_root.join(arena, &output_components);
675 output.generated_file.path = output_path;
676
677 const sub_path = try fs.path.join(arena, &output_components);
678 const sub_path_dirname = fs.path.dirname(sub_path).?;
679 b.cache_root.handle.makePath(sub_path_dirname) catch |err| {
680 return step.fail("unable to make path '{}{s}': {s}", .{
681 b.cache_root, sub_path_dirname, @errorName(err),
682 });
683 };
684 b.cache_root.handle.writeFile(sub_path, stream.bytes) catch |err| {
685 return step.fail("unable to write file '{}{s}': {s}", .{
686 b.cache_root, sub_path, @errorName(err),
687 });
688 };
689 }
690 }
691
587 switch (self.stdio) {692 switch (self.stdio) {
588 .check => |checks| for (checks.items) |check| switch (check) {693 .check => |checks| for (checks.items) |check| switch (check) {
589 .expect_stderr_exact => |expected_bytes| {694 .expect_stderr_exact => |expected_bytes| {
...@@ -705,7 +810,7 @@ fn spawnChildAndCollect(...@@ -705,7 +810,7 @@ fn spawnChildAndCollect(
705 child.request_resource_usage_statistics = true;810 child.request_resource_usage_statistics = true;
706811
707 child.stdin_behavior = switch (self.stdio) {812 child.stdin_behavior = switch (self.stdio) {
708 .infer_from_args => if (has_side_effects) .Inherit else .Ignore,813 .infer_from_args => if (has_side_effects) .Inherit else .Close,
709 .inherit => .Inherit,814 .inherit => .Inherit,
710 .check => .Close,815 .check => .Close,
711 };816 };
...@@ -719,12 +824,26 @@ fn spawnChildAndCollect(...@@ -719,12 +824,26 @@ fn spawnChildAndCollect(
719 .inherit => .Inherit,824 .inherit => .Inherit,
720 .check => .Pipe,825 .check => .Pipe,
721 };826 };
827 if (self.captured_stdout != null) child.stdout_behavior = .Pipe;
828 if (self.captured_stderr != null) child.stderr_behavior = .Pipe;
829 if (self.stdin != null) {
830 assert(child.stdin_behavior != .Inherit);
831 child.stdin_behavior = .Pipe;
832 }
722833
723 child.spawn() catch |err| return self.step.fail("unable to spawn {s}: {s}", .{834 child.spawn() catch |err| return self.step.fail("unable to spawn {s}: {s}", .{
724 argv[0], @errorName(err),835 argv[0], @errorName(err),
725 });836 });
726 var timer = try std.time.Timer.start();837 var timer = try std.time.Timer.start();
727838
839 if (self.stdin) |stdin| {
840 child.stdin.?.writeAll(stdin) catch |err| {
841 return self.step.fail("unable to write stdin: {s}", .{@errorName(err)});
842 };
843 child.stdin.?.close();
844 child.stdin = null;
845 }
846
728 // These are not optionals, as a workaround for847 // These are not optionals, as a workaround for
729 // https://github.com/ziglang/zig/issues/14783848 // https://github.com/ziglang/zig/issues/14783
730 var stdout_bytes: []const u8 = undefined;849 var stdout_bytes: []const u8 = undefined;
...@@ -761,7 +880,8 @@ fn spawnChildAndCollect(...@@ -761,7 +880,8 @@ fn spawnChildAndCollect(
761 }880 }
762881
763 if (!stderr_null and stderr_bytes.len > 0) {882 if (!stderr_null and stderr_bytes.len > 0) {
764 const stderr_is_diagnostic = switch (self.stdio) {883 // Treat stderr as an error message.
884 const stderr_is_diagnostic = self.captured_stderr == null and switch (self.stdio) {
765 .check => |checks| !checksContainStderr(checks.items),885 .check => |checks| !checksContainStderr(checks.items),
766 else => true,886 else => true,
767 };887 };
...@@ -829,3 +949,27 @@ fn failForeign(...@@ -829,3 +949,27 @@ fn failForeign(
829 },949 },
830 }950 }
831}951}
952
953fn hashStdIo(hh: *std.Build.Cache.HashHelper, stdio: StdIo) void {
954 switch (stdio) {
955 .infer_from_args, .inherit => {},
956 .check => |checks| for (checks.items) |check| {
957 hh.add(@as(std.meta.Tag(StdIo.Check), check));
958 switch (check) {
959 .expect_stderr_exact,
960 .expect_stderr_match,
961 .expect_stdout_exact,
962 .expect_stdout_match,
963 => |s| hh.addBytes(s),
964
965 .expect_term => |term| {
966 hh.add(@as(std.meta.Tag(std.process.Child.Term), term));
967 switch (term) {
968 .Exited => |x| hh.add(x),
969 .Signal, .Stopped, .Unknown => |x| hh.add(x),
970 }
971 },
972 }
973 },
974 }
975}
lib/std/Build/WriteFileStep.zig+6-8
...@@ -189,10 +189,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -189,10 +189,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
189 if (try step.cacheHit(&man)) {189 if (try step.cacheHit(&man)) {
190 const digest = man.final();190 const digest = man.final();
191 for (wf.files.items) |file| {191 for (wf.files.items) |file| {
192 file.generated_file.path = try b.cache_root.join(192 file.generated_file.path = try b.cache_root.join(b.allocator, &.{
193 b.allocator,193 "o", &digest, file.sub_path,
194 &.{ "o", &digest, file.sub_path },194 });
195 );
196 }195 }
197 return;196 return;
198 }197 }
...@@ -249,10 +248,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -249,10 +248,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
249 },248 },
250 }249 }
251250
252 file.generated_file.path = try b.cache_root.join(251 file.generated_file.path = try b.cache_root.join(b.allocator, &.{
253 b.allocator,252 cache_path, file.sub_path,
254 &.{ cache_path, file.sub_path },253 });
255 );
256 }254 }
257255
258 try man.writeManifest();256 try man.writeManifest();
test/src/StackTrace.zig created+105
...@@ -0,0 +1,105 @@
1b: *std.Build,
2step: *Step,
3test_index: usize,
4test_filter: ?[]const u8,
5optimize_modes: []const OptimizeMode,
6check_exe: *std.Build.CompileStep,
7
8const Expect = [@typeInfo(OptimizeMode).Enum.fields.len][]const u8;
9
10pub fn addCase(self: *StackTrace, config: anytype) void {
11 if (@hasField(@TypeOf(config), "exclude")) {
12 if (config.exclude.exclude()) return;
13 }
14 if (@hasField(@TypeOf(config), "exclude_arch")) {
15 const exclude_arch: []const std.Target.Cpu.Arch = &config.exclude_arch;
16 for (exclude_arch) |arch| if (arch == builtin.cpu.arch) return;
17 }
18 if (@hasField(@TypeOf(config), "exclude_os")) {
19 const exclude_os: []const std.Target.Os.Tag = &config.exclude_os;
20 for (exclude_os) |os| if (os == builtin.os.tag) return;
21 }
22 for (self.optimize_modes) |optimize_mode| {
23 switch (optimize_mode) {
24 .Debug => {
25 if (@hasField(@TypeOf(config), "Debug")) {
26 self.addExpect(config.name, config.source, optimize_mode, config.Debug);
27 }
28 },
29 .ReleaseSafe => {
30 if (@hasField(@TypeOf(config), "ReleaseSafe")) {
31 self.addExpect(config.name, config.source, optimize_mode, config.ReleaseSafe);
32 }
33 },
34 .ReleaseFast => {
35 if (@hasField(@TypeOf(config), "ReleaseFast")) {
36 self.addExpect(config.name, config.source, optimize_mode, config.ReleaseFast);
37 }
38 },
39 .ReleaseSmall => {
40 if (@hasField(@TypeOf(config), "ReleaseSmall")) {
41 self.addExpect(config.name, config.source, optimize_mode, config.ReleaseSmall);
42 }
43 },
44 }
45 }
46}
47
48fn addExpect(
49 self: *StackTrace,
50 name: []const u8,
51 source: []const u8,
52 optimize_mode: OptimizeMode,
53 mode_config: anytype,
54) void {
55 if (@hasField(@TypeOf(mode_config), "exclude")) {
56 if (mode_config.exclude.exclude()) return;
57 }
58 if (@hasField(@TypeOf(mode_config), "exclude_arch")) {
59 const exclude_arch: []const std.Target.Cpu.Arch = &mode_config.exclude_arch;
60 for (exclude_arch) |arch| if (arch == builtin.cpu.arch) return;
61 }
62 if (@hasField(@TypeOf(mode_config), "exclude_os")) {
63 const exclude_os: []const std.Target.Os.Tag = &mode_config.exclude_os;
64 for (exclude_os) |os| if (os == builtin.os.tag) return;
65 }
66
67 const b = self.b;
68 const annotated_case_name = fmt.allocPrint(b.allocator, "check {s} ({s})", .{
69 name, @tagName(optimize_mode),
70 }) catch @panic("OOM");
71 if (self.test_filter) |filter| {
72 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
73 }
74
75 const src_basename = "source.zig";
76 const write_src = b.addWriteFile(src_basename, source);
77 const exe = b.addExecutable(.{
78 .name = "test",
79 .root_source_file = write_src.getFileSource(src_basename).?,
80 .optimize = optimize_mode,
81 .target = .{},
82 });
83
84 const run = b.addRunArtifact(exe);
85 run.expectExitCode(1);
86 run.expectStdOutEqual("");
87
88 const check_run = b.addRunArtifact(self.check_exe);
89 check_run.setName(annotated_case_name);
90 check_run.addFileSourceArg(run.captureStdErr());
91 check_run.addArgs(&.{
92 @tagName(optimize_mode),
93 });
94 check_run.expectStdOutEqual(mode_config.expect);
95
96 self.step.dependOn(&check_run.step);
97}
98
99const StackTrace = @This();
100const std = @import("std");
101const builtin = @import("builtin");
102const Step = std.Build.Step;
103const OptimizeMode = std.builtin.OptimizeMode;
104const fmt = std.fmt;
105const mem = std.mem;
test/src/Standalone.zig created+141
...@@ -0,0 +1,141 @@
1b: *std.Build,
2step: *Step,
3test_index: usize,
4test_filter: ?[]const u8,
5optimize_modes: []const OptimizeMode,
6skip_non_native: bool,
7enable_macos_sdk: bool,
8target: std.zig.CrossTarget,
9omit_stage2: bool,
10enable_darling: bool = false,
11enable_qemu: bool = false,
12enable_rosetta: bool = false,
13enable_wasmtime: bool = false,
14enable_wine: bool = false,
15enable_symlinks_windows: bool,
16
17pub fn addC(self: *Standalone, root_src: []const u8) void {
18 self.addAllArgs(root_src, true);
19}
20
21pub fn add(self: *Standalone, root_src: []const u8) void {
22 self.addAllArgs(root_src, false);
23}
24
25pub fn addBuildFile(self: *Standalone, build_file: []const u8, features: struct {
26 build_modes: bool = false,
27 cross_targets: bool = false,
28 requires_macos_sdk: bool = false,
29 requires_stage2: bool = false,
30 use_emulation: bool = false,
31 requires_symlinks: bool = false,
32 extra_argv: []const []const u8 = &.{},
33}) void {
34 const b = self.b;
35
36 if (features.requires_macos_sdk and !self.enable_macos_sdk) return;
37 if (features.requires_stage2 and self.omit_stage2) return;
38 if (features.requires_symlinks and !self.enable_symlinks_windows and builtin.os.tag == .windows) return;
39
40 const annotated_case_name = b.fmt("build {s}", .{build_file});
41 if (self.test_filter) |filter| {
42 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
43 }
44
45 var zig_args = ArrayList([]const u8).init(b.allocator);
46 const rel_zig_exe = fs.path.relative(b.allocator, b.build_root.path orelse ".", b.zig_exe) catch unreachable;
47 zig_args.append(rel_zig_exe) catch unreachable;
48 zig_args.append("build") catch unreachable;
49
50 // TODO: fix the various non-concurrency-safe issues in zig's standalone tests,
51 // and then remove this!
52 zig_args.append("-j1") catch @panic("OOM");
53
54 zig_args.append("--build-file") catch unreachable;
55 zig_args.append(b.pathFromRoot(build_file)) catch unreachable;
56
57 zig_args.appendSlice(features.extra_argv) catch unreachable;
58
59 zig_args.append("test") catch unreachable;
60
61 if (b.verbose) {
62 zig_args.append("--verbose") catch unreachable;
63 }
64
65 if (features.cross_targets and !self.target.isNative()) {
66 const target_triple = self.target.zigTriple(b.allocator) catch unreachable;
67 const target_arg = fmt.allocPrint(b.allocator, "-Dtarget={s}", .{target_triple}) catch unreachable;
68 zig_args.append(target_arg) catch unreachable;
69 }
70
71 if (features.use_emulation) {
72 if (self.enable_darling) {
73 zig_args.append("-fdarling") catch unreachable;
74 }
75 if (self.enable_qemu) {
76 zig_args.append("-fqemu") catch unreachable;
77 }
78 if (self.enable_rosetta) {
79 zig_args.append("-frosetta") catch unreachable;
80 }
81 if (self.enable_wasmtime) {
82 zig_args.append("-fwasmtime") catch unreachable;
83 }
84 if (self.enable_wine) {
85 zig_args.append("-fwine") catch unreachable;
86 }
87 }
88
89 const optimize_modes = if (features.build_modes) self.optimize_modes else &[1]OptimizeMode{.Debug};
90 for (optimize_modes) |optimize_mode| {
91 const arg = switch (optimize_mode) {
92 .Debug => "",
93 .ReleaseFast => "-Doptimize=ReleaseFast",
94 .ReleaseSafe => "-Doptimize=ReleaseSafe",
95 .ReleaseSmall => "-Doptimize=ReleaseSmall",
96 };
97 const zig_args_base_len = zig_args.items.len;
98 if (arg.len > 0)
99 zig_args.append(arg) catch unreachable;
100 defer zig_args.resize(zig_args_base_len) catch unreachable;
101
102 const run_cmd = b.addSystemCommand(zig_args.items);
103 self.step.dependOn(&run_cmd.step);
104 }
105}
106
107pub fn addAllArgs(self: *Standalone, root_src: []const u8, link_libc: bool) void {
108 const b = self.b;
109
110 for (self.optimize_modes) |optimize| {
111 const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {s} ({s})", .{
112 root_src,
113 @tagName(optimize),
114 }) catch unreachable;
115 if (self.test_filter) |filter| {
116 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
117 }
118
119 const exe = b.addExecutable(.{
120 .name = "test",
121 .root_source_file = .{ .path = root_src },
122 .optimize = optimize,
123 .target = .{},
124 });
125 if (link_libc) {
126 exe.linkSystemLibrary("c");
127 }
128
129 self.step.dependOn(&exe.step);
130 }
131}
132
133const Standalone = @This();
134const std = @import("std");
135const builtin = @import("builtin");
136const Step = std.Build.Step;
137const OptimizeMode = std.builtin.OptimizeMode;
138const fmt = std.fmt;
139const mem = std.mem;
140const ArrayList = std.ArrayList;
141const fs = std.fs;
test/src/check-stack-trace.zig created+79
...@@ -0,0 +1,79 @@
1const builtin = @import("builtin");
2const std = @import("std");
3const mem = std.mem;
4const fs = std.fs;
5
6pub fn main() !void {
7 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
8 defer arena_instance.deinit();
9 const arena = arena_instance.allocator();
10
11 const args = try std.process.argsAlloc(arena);
12
13 const input_path = args[1];
14 const optimize_mode_text = args[2];
15
16 const input_bytes = try std.fs.cwd().readFileAlloc(arena, input_path, 5 * 1024 * 1024);
17 const optimize_mode = std.meta.stringToEnum(std.builtin.OptimizeMode, optimize_mode_text).?;
18
19 var stderr = input_bytes;
20
21 // process result
22 // - keep only basename of source file path
23 // - replace address with symbolic string
24 // - replace function name with symbolic string when optimize_mode != .Debug
25 // - skip empty lines
26 const got: []const u8 = got_result: {
27 var buf = std.ArrayList(u8).init(arena);
28 defer buf.deinit();
29 if (stderr.len != 0 and stderr[stderr.len - 1] == '\n') stderr = stderr[0 .. stderr.len - 1];
30 var it = mem.split(u8, stderr, "\n");
31 process_lines: while (it.next()) |line| {
32 if (line.len == 0) continue;
33
34 // offset search past `[drive]:` on windows
35 var pos: usize = if (builtin.os.tag == .windows) 2 else 0;
36 // locate delims/anchor
37 const delims = [_][]const u8{ ":", ":", ":", " in ", "(", ")" };
38 var marks = [_]usize{0} ** delims.len;
39 for (delims, 0..) |delim, i| {
40 marks[i] = mem.indexOfPos(u8, line, pos, delim) orelse {
41 // unexpected pattern: emit raw line and cont
42 try buf.appendSlice(line);
43 try buf.appendSlice("\n");
44 continue :process_lines;
45 };
46 pos = marks[i] + delim.len;
47 }
48 // locate source basename
49 pos = mem.lastIndexOfScalar(u8, line[0..marks[0]], fs.path.sep) orelse {
50 // unexpected pattern: emit raw line and cont
51 try buf.appendSlice(line);
52 try buf.appendSlice("\n");
53 continue :process_lines;
54 };
55 // end processing if source basename changes
56 if (!mem.eql(u8, "source.zig", line[pos + 1 .. marks[0]])) break;
57 // emit substituted line
58 try buf.appendSlice(line[pos + 1 .. marks[2] + delims[2].len]);
59 try buf.appendSlice(" [address]");
60 if (optimize_mode == .Debug) {
61 // On certain platforms (windows) or possibly depending on how we choose to link main
62 // the object file extension may be present so we simply strip any extension.
63 if (mem.indexOfScalar(u8, line[marks[4]..marks[5]], '.')) |idot| {
64 try buf.appendSlice(line[marks[3] .. marks[4] + idot]);
65 try buf.appendSlice(line[marks[5]..]);
66 } else {
67 try buf.appendSlice(line[marks[3]..]);
68 }
69 } else {
70 try buf.appendSlice(line[marks[3] .. marks[3] + delims[3].len]);
71 try buf.appendSlice("[function]");
72 }
73 try buf.appendSlice("\n");
74 }
75 break :got_result try buf.toOwnedSlice();
76 };
77
78 try std.io.getStdOut().writeAll(got);
79}
test/tests.zig+89-646
...@@ -20,13 +20,14 @@ const stack_traces = @import("stack_traces.zig");...@@ -20,13 +20,14 @@ const stack_traces = @import("stack_traces.zig");
20const assemble_and_link = @import("assemble_and_link.zig");20const assemble_and_link = @import("assemble_and_link.zig");
21const translate_c = @import("translate_c.zig");21const translate_c = @import("translate_c.zig");
22const run_translated_c = @import("run_translated_c.zig");22const run_translated_c = @import("run_translated_c.zig");
23const gen_h = @import("gen_h.zig");
24const link = @import("link.zig");23const link = @import("link.zig");
2524
26// Implementations25// Implementations
27pub const TranslateCContext = @import("src/translate_c.zig").TranslateCContext;26pub const TranslateCContext = @import("src/translate_c.zig").TranslateCContext;
28pub const RunTranslatedCContext = @import("src/run_translated_c.zig").RunTranslatedCContext;27pub const RunTranslatedCContext = @import("src/run_translated_c.zig").RunTranslatedCContext;
29pub const CompareOutputContext = @import("src/compare_output.zig").CompareOutputContext;28pub const CompareOutputContext = @import("src/compare_output.zig").CompareOutputContext;
29pub const StackTracesContext = @import("src/StackTrace.zig");
30pub const StandaloneContext = @import("src/Standalone.zig");
3031
31const TestTarget = struct {32const TestTarget = struct {
32 target: CrossTarget = @as(CrossTarget, .{}),33 target: CrossTarget = @as(CrossTarget, .{}),
...@@ -460,10 +461,71 @@ const test_targets = blk: {...@@ -460,10 +461,71 @@ const test_targets = blk: {
460 };461 };
461};462};
462463
463const max_stdout_size = 1 * 1024 * 1024; // 1 MB464const c_abi_targets = [_]CrossTarget{
465 .{},
466 .{
467 .cpu_arch = .x86_64,
468 .os_tag = .linux,
469 .abi = .musl,
470 },
471 .{
472 .cpu_arch = .x86,
473 .os_tag = .linux,
474 .abi = .musl,
475 },
476 .{
477 .cpu_arch = .aarch64,
478 .os_tag = .linux,
479 .abi = .musl,
480 },
481 .{
482 .cpu_arch = .arm,
483 .os_tag = .linux,
484 .abi = .musleabihf,
485 },
486 .{
487 .cpu_arch = .mips,
488 .os_tag = .linux,
489 .abi = .musl,
490 },
491 .{
492 .cpu_arch = .riscv64,
493 .os_tag = .linux,
494 .abi = .musl,
495 },
496 .{
497 .cpu_arch = .wasm32,
498 .os_tag = .wasi,
499 .abi = .musl,
500 },
501 .{
502 .cpu_arch = .powerpc,
503 .os_tag = .linux,
504 .abi = .musl,
505 },
506 .{
507 .cpu_arch = .powerpc64le,
508 .os_tag = .linux,
509 .abi = .musl,
510 },
511 .{
512 .cpu_arch = .x86,
513 .os_tag = .windows,
514 .abi = .gnu,
515 },
516 .{
517 .cpu_arch = .x86_64,
518 .os_tag = .windows,
519 .abi = .gnu,
520 },
521};
464522
465pub fn addCompareOutputTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *Step {523pub fn addCompareOutputTests(
466 const cases = b.allocator.create(CompareOutputContext) catch unreachable;524 b: *std.Build,
525 test_filter: ?[]const u8,
526 optimize_modes: []const OptimizeMode,
527) *Step {
528 const cases = b.allocator.create(CompareOutputContext) catch @panic("OOM");
467 cases.* = CompareOutputContext{529 cases.* = CompareOutputContext{
468 .b = b,530 .b = b,
469 .step = b.step("test-compare-output", "Run the compare output tests"),531 .step = b.step("test-compare-output", "Run the compare output tests"),
...@@ -477,14 +539,26 @@ pub fn addCompareOutputTests(b: *std.Build, test_filter: ?[]const u8, optimize_m...@@ -477,14 +539,26 @@ pub fn addCompareOutputTests(b: *std.Build, test_filter: ?[]const u8, optimize_m
477 return cases.step;539 return cases.step;
478}540}
479541
480pub fn addStackTraceTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *Step {542pub fn addStackTraceTests(
481 const cases = b.allocator.create(StackTracesContext) catch unreachable;543 b: *std.Build,
482 cases.* = StackTracesContext{544 test_filter: ?[]const u8,
545 optimize_modes: []const OptimizeMode,
546) *Step {
547 const check_exe = b.addExecutable(.{
548 .name = "check-stack-trace",
549 .root_source_file = .{ .path = "test/src/check-stack-trace.zig" },
550 .target = .{},
551 .optimize = .Debug,
552 });
553
554 const cases = b.allocator.create(StackTracesContext) catch @panic("OOM");
555 cases.* = .{
483 .b = b,556 .b = b,
484 .step = b.step("test-stack-traces", "Run the stack trace tests"),557 .step = b.step("test-stack-traces", "Run the stack trace tests"),
485 .test_index = 0,558 .test_index = 0,
486 .test_filter = test_filter,559 .test_filter = test_filter,
487 .optimize_modes = optimize_modes,560 .optimize_modes = optimize_modes,
561 .check_exe = check_exe,
488 };562 };
489563
490 stack_traces.addCases(cases);564 stack_traces.addCases(cases);
...@@ -507,7 +581,7 @@ pub fn addStandaloneTests(...@@ -507,7 +581,7 @@ pub fn addStandaloneTests(
507 enable_wine: bool,581 enable_wine: bool,
508 enable_symlinks_windows: bool,582 enable_symlinks_windows: bool,
509) *Step {583) *Step {
510 const cases = b.allocator.create(StandaloneContext) catch unreachable;584 const cases = b.allocator.create(StandaloneContext) catch @panic("OOM");
511 cases.* = StandaloneContext{585 cases.* = StandaloneContext{
512 .b = b,586 .b = b,
513 .step = b.step("test-standalone", "Run the standalone tests"),587 .step = b.step("test-standalone", "Run the standalone tests"),
...@@ -539,7 +613,7 @@ pub fn addLinkTests(...@@ -539,7 +613,7 @@ pub fn addLinkTests(
539 omit_stage2: bool,613 omit_stage2: bool,
540 enable_symlinks_windows: bool,614 enable_symlinks_windows: bool,
541) *Step {615) *Step {
542 const cases = b.allocator.create(StandaloneContext) catch unreachable;616 const cases = b.allocator.create(StandaloneContext) catch @panic("OOM");
543 cases.* = StandaloneContext{617 cases.* = StandaloneContext{
544 .b = b,618 .b = b,
545 .step = b.step("test-link", "Run the linker tests"),619 .step = b.step("test-link", "Run the linker tests"),
...@@ -569,7 +643,7 @@ pub fn addCliTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []co...@@ -569,7 +643,7 @@ pub fn addCliTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []co
569 });643 });
570 const run_cmd = exe.run();644 const run_cmd = exe.run();
571 run_cmd.addArgs(&[_][]const u8{645 run_cmd.addArgs(&[_][]const u8{
572 fs.realpathAlloc(b.allocator, b.zig_exe) catch unreachable,646 fs.realpathAlloc(b.allocator, b.zig_exe) catch @panic("OOM"),
573 b.pathFromRoot(b.cache_root.path orelse "."),647 b.pathFromRoot(b.cache_root.path orelse "."),
574 });648 });
575649
...@@ -578,7 +652,7 @@ pub fn addCliTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []co...@@ -578,7 +652,7 @@ pub fn addCliTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []co
578}652}
579653
580pub fn addAssembleAndLinkTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *Step {654pub fn addAssembleAndLinkTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []const OptimizeMode) *Step {
581 const cases = b.allocator.create(CompareOutputContext) catch unreachable;655 const cases = b.allocator.create(CompareOutputContext) catch @panic("OOM");
582 cases.* = CompareOutputContext{656 cases.* = CompareOutputContext{
583 .b = b,657 .b = b,
584 .step = b.step("test-asm-link", "Run the assemble and link tests"),658 .step = b.step("test-asm-link", "Run the assemble and link tests"),
...@@ -593,7 +667,7 @@ pub fn addAssembleAndLinkTests(b: *std.Build, test_filter: ?[]const u8, optimize...@@ -593,7 +667,7 @@ pub fn addAssembleAndLinkTests(b: *std.Build, test_filter: ?[]const u8, optimize
593}667}
594668
595pub fn addTranslateCTests(b: *std.Build, test_filter: ?[]const u8) *Step {669pub fn addTranslateCTests(b: *std.Build, test_filter: ?[]const u8) *Step {
596 const cases = b.allocator.create(TranslateCContext) catch unreachable;670 const cases = b.allocator.create(TranslateCContext) catch @panic("OOM");
597 cases.* = TranslateCContext{671 cases.* = TranslateCContext{
598 .b = b,672 .b = b,
599 .step = b.step("test-translate-c", "Run the C translation tests"),673 .step = b.step("test-translate-c", "Run the C translation tests"),
...@@ -611,7 +685,7 @@ pub fn addRunTranslatedCTests(...@@ -611,7 +685,7 @@ pub fn addRunTranslatedCTests(
611 test_filter: ?[]const u8,685 test_filter: ?[]const u8,
612 target: std.zig.CrossTarget,686 target: std.zig.CrossTarget,
613) *Step {687) *Step {
614 const cases = b.allocator.create(RunTranslatedCContext) catch unreachable;688 const cases = b.allocator.create(RunTranslatedCContext) catch @panic("OOM");
615 cases.* = .{689 cases.* = .{
616 .b = b,690 .b = b,
617 .step = b.step("test-run-translated-c", "Run the Run-Translated-C tests"),691 .step = b.step("test-run-translated-c", "Run the Run-Translated-C tests"),
...@@ -625,20 +699,6 @@ pub fn addRunTranslatedCTests(...@@ -625,20 +699,6 @@ pub fn addRunTranslatedCTests(
625 return cases.step;699 return cases.step;
626}700}
627701
628pub fn addGenHTests(b: *std.Build, test_filter: ?[]const u8) *Step {
629 const cases = b.allocator.create(GenHContext) catch unreachable;
630 cases.* = GenHContext{
631 .b = b,
632 .step = b.step("test-gen-h", "Run the C header file generation tests"),
633 .test_index = 0,
634 .test_filter = test_filter,
635 };
636
637 gen_h.addCases(cases);
638
639 return cases.step;
640}
641
642const ModuleTestOptions = struct {702const ModuleTestOptions = struct {
643 test_filter: ?[]const u8,703 test_filter: ?[]const u8,
644 root_src: []const u8,704 root_src: []const u8,
...@@ -696,7 +756,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {...@@ -696,7 +756,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
696 else756 else
697 "bare";757 "bare";
698758
699 const triple_prefix = test_target.target.zigTriple(b.allocator) catch unreachable;759 const triple_prefix = test_target.target.zigTriple(b.allocator) catch @panic("OOM");
700760
701 // wasm32-wasi builds need more RAM, idk why761 // wasm32-wasi builds need more RAM, idk why
702 const max_rss = if (test_target.target.getOs().tag == .wasi)762 const max_rss = if (test_target.target.getOs().tag == .wasi)
...@@ -750,623 +810,6 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {...@@ -750,623 +810,6 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
750 return step;810 return step;
751}811}
752812
753pub const StackTracesContext = struct {
754 b: *std.Build,
755 step: *Step,
756 test_index: usize,
757 test_filter: ?[]const u8,
758 optimize_modes: []const OptimizeMode,
759
760 const Expect = [@typeInfo(OptimizeMode).Enum.fields.len][]const u8;
761
762 pub fn addCase(self: *StackTracesContext, config: anytype) void {
763 if (@hasField(@TypeOf(config), "exclude")) {
764 if (config.exclude.exclude()) return;
765 }
766 if (@hasField(@TypeOf(config), "exclude_arch")) {
767 const exclude_arch: []const std.Target.Cpu.Arch = &config.exclude_arch;
768 for (exclude_arch) |arch| if (arch == builtin.cpu.arch) return;
769 }
770 if (@hasField(@TypeOf(config), "exclude_os")) {
771 const exclude_os: []const std.Target.Os.Tag = &config.exclude_os;
772 for (exclude_os) |os| if (os == builtin.os.tag) return;
773 }
774 for (self.optimize_modes) |optimize_mode| {
775 switch (optimize_mode) {
776 .Debug => {
777 if (@hasField(@TypeOf(config), "Debug")) {
778 self.addExpect(config.name, config.source, optimize_mode, config.Debug);
779 }
780 },
781 .ReleaseSafe => {
782 if (@hasField(@TypeOf(config), "ReleaseSafe")) {
783 self.addExpect(config.name, config.source, optimize_mode, config.ReleaseSafe);
784 }
785 },
786 .ReleaseFast => {
787 if (@hasField(@TypeOf(config), "ReleaseFast")) {
788 self.addExpect(config.name, config.source, optimize_mode, config.ReleaseFast);
789 }
790 },
791 .ReleaseSmall => {
792 if (@hasField(@TypeOf(config), "ReleaseSmall")) {
793 self.addExpect(config.name, config.source, optimize_mode, config.ReleaseSmall);
794 }
795 },
796 }
797 }
798 }
799
800 fn addExpect(
801 self: *StackTracesContext,
802 name: []const u8,
803 source: []const u8,
804 optimize_mode: OptimizeMode,
805 mode_config: anytype,
806 ) void {
807 if (@hasField(@TypeOf(mode_config), "exclude")) {
808 if (mode_config.exclude.exclude()) return;
809 }
810 if (@hasField(@TypeOf(mode_config), "exclude_arch")) {
811 const exclude_arch: []const std.Target.Cpu.Arch = &mode_config.exclude_arch;
812 for (exclude_arch) |arch| if (arch == builtin.cpu.arch) return;
813 }
814 if (@hasField(@TypeOf(mode_config), "exclude_os")) {
815 const exclude_os: []const std.Target.Os.Tag = &mode_config.exclude_os;
816 for (exclude_os) |os| if (os == builtin.os.tag) return;
817 }
818
819 const annotated_case_name = fmt.allocPrint(self.b.allocator, "{s} {s} ({s})", .{
820 "stack-trace",
821 name,
822 @tagName(optimize_mode),
823 }) catch unreachable;
824 if (self.test_filter) |filter| {
825 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
826 }
827
828 const b = self.b;
829 const src_basename = "source.zig";
830 const write_src = b.addWriteFile(src_basename, source);
831 const exe = b.addExecutable(.{
832 .name = "test",
833 .root_source_file = write_src.getFileSource(src_basename).?,
834 .optimize = optimize_mode,
835 .target = .{},
836 });
837
838 const run_and_compare = RunAndCompareStep.create(
839 self,
840 exe,
841 annotated_case_name,
842 optimize_mode,
843 mode_config.expect,
844 );
845
846 self.step.dependOn(&run_and_compare.step);
847 }
848
849 const RunAndCompareStep = struct {
850 pub const base_id = .custom;
851
852 step: Step,
853 context: *StackTracesContext,
854 exe: *CompileStep,
855 name: []const u8,
856 optimize_mode: OptimizeMode,
857 expect_output: []const u8,
858 test_index: usize,
859
860 pub fn create(
861 context: *StackTracesContext,
862 exe: *CompileStep,
863 name: []const u8,
864 optimize_mode: OptimizeMode,
865 expect_output: []const u8,
866 ) *RunAndCompareStep {
867 const allocator = context.b.allocator;
868 const ptr = allocator.create(RunAndCompareStep) catch unreachable;
869 ptr.* = RunAndCompareStep{
870 .step = Step.init(.{
871 .id = .custom,
872 .name = "StackTraceCompareOutputStep",
873 .makeFn = make,
874 .owner = context.b,
875 }),
876 .context = context,
877 .exe = exe,
878 .name = name,
879 .optimize_mode = optimize_mode,
880 .expect_output = expect_output,
881 .test_index = context.test_index,
882 };
883 ptr.step.dependOn(&exe.step);
884 context.test_index += 1;
885 return ptr;
886 }
887
888 fn make(step: *Step, prog_node: *std.Progress.Node) !void {
889 _ = prog_node;
890 const self = @fieldParentPtr(RunAndCompareStep, "step", step);
891 const b = self.context.b;
892
893 const full_exe_path = self.exe.getOutputSource().getPath(b);
894 var args = ArrayList([]const u8).init(b.allocator);
895 defer args.deinit();
896 args.append(full_exe_path) catch unreachable;
897
898 std.debug.print("Test {d}/{d} {s}...", .{ self.test_index + 1, self.context.test_index, self.name });
899
900 if (!std.process.can_spawn) {
901 const cmd = try std.mem.join(b.allocator, " ", args.items);
902 std.debug.print("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd });
903 b.allocator.free(cmd);
904 return ExecError.ExecNotSupported;
905 }
906
907 var child = std.ChildProcess.init(args.items, b.allocator);
908 child.stdin_behavior = .Ignore;
909 child.stdout_behavior = .Pipe;
910 child.stderr_behavior = .Pipe;
911 child.env_map = b.env_map;
912
913 if (b.verbose) {
914 printInvocation(args.items);
915 }
916 child.spawn() catch |err| debug.panic("Unable to spawn {s}: {s}\n", .{ full_exe_path, @errorName(err) });
917
918 const stdout = child.stdout.?.reader().readAllAlloc(b.allocator, max_stdout_size) catch unreachable;
919 defer b.allocator.free(stdout);
920 const stderrFull = child.stderr.?.reader().readAllAlloc(b.allocator, max_stdout_size) catch unreachable;
921 defer b.allocator.free(stderrFull);
922 var stderr = stderrFull;
923
924 const term = child.wait() catch |err| {
925 debug.panic("Unable to spawn {s}: {s}\n", .{ full_exe_path, @errorName(err) });
926 };
927
928 switch (term) {
929 .Exited => |code| {
930 const expect_code: u32 = 1;
931 if (code != expect_code) {
932 std.debug.print("Process {s} exited with error code {d} but expected code {d}\n", .{
933 full_exe_path,
934 code,
935 expect_code,
936 });
937 printInvocation(args.items);
938 return error.TestFailed;
939 }
940 },
941 .Signal => |signum| {
942 std.debug.print("Process {s} terminated on signal {d}\n", .{ full_exe_path, signum });
943 printInvocation(args.items);
944 return error.TestFailed;
945 },
946 .Stopped => |signum| {
947 std.debug.print("Process {s} stopped on signal {d}\n", .{ full_exe_path, signum });
948 printInvocation(args.items);
949 return error.TestFailed;
950 },
951 .Unknown => |code| {
952 std.debug.print("Process {s} terminated unexpectedly with error code {d}\n", .{ full_exe_path, code });
953 printInvocation(args.items);
954 return error.TestFailed;
955 },
956 }
957
958 // process result
959 // - keep only basename of source file path
960 // - replace address with symbolic string
961 // - replace function name with symbolic string when optimize_mode != .Debug
962 // - skip empty lines
963 const got: []const u8 = got_result: {
964 var buf = ArrayList(u8).init(b.allocator);
965 defer buf.deinit();
966 if (stderr.len != 0 and stderr[stderr.len - 1] == '\n') stderr = stderr[0 .. stderr.len - 1];
967 var it = mem.split(u8, stderr, "\n");
968 process_lines: while (it.next()) |line| {
969 if (line.len == 0) continue;
970
971 // offset search past `[drive]:` on windows
972 var pos: usize = if (builtin.os.tag == .windows) 2 else 0;
973 // locate delims/anchor
974 const delims = [_][]const u8{ ":", ":", ":", " in ", "(", ")" };
975 var marks = [_]usize{0} ** delims.len;
976 for (delims, 0..) |delim, i| {
977 marks[i] = mem.indexOfPos(u8, line, pos, delim) orelse {
978 // unexpected pattern: emit raw line and cont
979 try buf.appendSlice(line);
980 try buf.appendSlice("\n");
981 continue :process_lines;
982 };
983 pos = marks[i] + delim.len;
984 }
985 // locate source basename
986 pos = mem.lastIndexOfScalar(u8, line[0..marks[0]], fs.path.sep) orelse {
987 // unexpected pattern: emit raw line and cont
988 try buf.appendSlice(line);
989 try buf.appendSlice("\n");
990 continue :process_lines;
991 };
992 // end processing if source basename changes
993 if (!mem.eql(u8, "source.zig", line[pos + 1 .. marks[0]])) break;
994 // emit substituted line
995 try buf.appendSlice(line[pos + 1 .. marks[2] + delims[2].len]);
996 try buf.appendSlice(" [address]");
997 if (self.optimize_mode == .Debug) {
998 // On certain platforms (windows) or possibly depending on how we choose to link main
999 // the object file extension may be present so we simply strip any extension.
1000 if (mem.indexOfScalar(u8, line[marks[4]..marks[5]], '.')) |idot| {
1001 try buf.appendSlice(line[marks[3] .. marks[4] + idot]);
1002 try buf.appendSlice(line[marks[5]..]);
1003 } else {
1004 try buf.appendSlice(line[marks[3]..]);
1005 }
1006 } else {
1007 try buf.appendSlice(line[marks[3] .. marks[3] + delims[3].len]);
1008 try buf.appendSlice("[function]");
1009 }
1010 try buf.appendSlice("\n");
1011 }
1012 break :got_result try buf.toOwnedSlice();
1013 };
1014
1015 if (!mem.eql(u8, self.expect_output, got)) {
1016 std.debug.print(
1017 \\
1018 \\========= Expected this output: =========
1019 \\{s}
1020 \\================================================
1021 \\{s}
1022 \\
1023 , .{ self.expect_output, got });
1024 return error.TestFailed;
1025 }
1026 std.debug.print("OK\n", .{});
1027 }
1028 };
1029};
1030
1031pub const StandaloneContext = struct {
1032 b: *std.Build,
1033 step: *Step,
1034 test_index: usize,
1035 test_filter: ?[]const u8,
1036 optimize_modes: []const OptimizeMode,
1037 skip_non_native: bool,
1038 enable_macos_sdk: bool,
1039 target: std.zig.CrossTarget,
1040 omit_stage2: bool,
1041 enable_darling: bool = false,
1042 enable_qemu: bool = false,
1043 enable_rosetta: bool = false,
1044 enable_wasmtime: bool = false,
1045 enable_wine: bool = false,
1046 enable_symlinks_windows: bool,
1047
1048 pub fn addC(self: *StandaloneContext, root_src: []const u8) void {
1049 self.addAllArgs(root_src, true);
1050 }
1051
1052 pub fn add(self: *StandaloneContext, root_src: []const u8) void {
1053 self.addAllArgs(root_src, false);
1054 }
1055
1056 pub fn addBuildFile(self: *StandaloneContext, build_file: []const u8, features: struct {
1057 build_modes: bool = false,
1058 cross_targets: bool = false,
1059 requires_macos_sdk: bool = false,
1060 requires_stage2: bool = false,
1061 use_emulation: bool = false,
1062 requires_symlinks: bool = false,
1063 extra_argv: []const []const u8 = &.{},
1064 }) void {
1065 const b = self.b;
1066
1067 if (features.requires_macos_sdk and !self.enable_macos_sdk) return;
1068 if (features.requires_stage2 and self.omit_stage2) return;
1069 if (features.requires_symlinks and !self.enable_symlinks_windows and builtin.os.tag == .windows) return;
1070
1071 const annotated_case_name = b.fmt("build {s}", .{build_file});
1072 if (self.test_filter) |filter| {
1073 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
1074 }
1075
1076 var zig_args = ArrayList([]const u8).init(b.allocator);
1077 const rel_zig_exe = fs.path.relative(b.allocator, b.build_root.path orelse ".", b.zig_exe) catch unreachable;
1078 zig_args.append(rel_zig_exe) catch unreachable;
1079 zig_args.append("build") catch unreachable;
1080
1081 // TODO: fix the various non-concurrency-safe issues in zig's standalone tests,
1082 // and then remove this!
1083 zig_args.append("-j1") catch @panic("OOM");
1084
1085 zig_args.append("--build-file") catch unreachable;
1086 zig_args.append(b.pathFromRoot(build_file)) catch unreachable;
1087
1088 zig_args.appendSlice(features.extra_argv) catch unreachable;
1089
1090 zig_args.append("test") catch unreachable;
1091
1092 if (b.verbose) {
1093 zig_args.append("--verbose") catch unreachable;
1094 }
1095
1096 if (features.cross_targets and !self.target.isNative()) {
1097 const target_triple = self.target.zigTriple(b.allocator) catch unreachable;
1098 const target_arg = fmt.allocPrint(b.allocator, "-Dtarget={s}", .{target_triple}) catch unreachable;
1099 zig_args.append(target_arg) catch unreachable;
1100 }
1101
1102 if (features.use_emulation) {
1103 if (self.enable_darling) {
1104 zig_args.append("-fdarling") catch unreachable;
1105 }
1106 if (self.enable_qemu) {
1107 zig_args.append("-fqemu") catch unreachable;
1108 }
1109 if (self.enable_rosetta) {
1110 zig_args.append("-frosetta") catch unreachable;
1111 }
1112 if (self.enable_wasmtime) {
1113 zig_args.append("-fwasmtime") catch unreachable;
1114 }
1115 if (self.enable_wine) {
1116 zig_args.append("-fwine") catch unreachable;
1117 }
1118 }
1119
1120 const optimize_modes = if (features.build_modes) self.optimize_modes else &[1]OptimizeMode{.Debug};
1121 for (optimize_modes) |optimize_mode| {
1122 const arg = switch (optimize_mode) {
1123 .Debug => "",
1124 .ReleaseFast => "-Doptimize=ReleaseFast",
1125 .ReleaseSafe => "-Doptimize=ReleaseSafe",
1126 .ReleaseSmall => "-Doptimize=ReleaseSmall",
1127 };
1128 const zig_args_base_len = zig_args.items.len;
1129 if (arg.len > 0)
1130 zig_args.append(arg) catch unreachable;
1131 defer zig_args.resize(zig_args_base_len) catch unreachable;
1132
1133 const run_cmd = b.addSystemCommand(zig_args.items);
1134 self.step.dependOn(&run_cmd.step);
1135 }
1136 }
1137
1138 pub fn addAllArgs(self: *StandaloneContext, root_src: []const u8, link_libc: bool) void {
1139 const b = self.b;
1140
1141 for (self.optimize_modes) |optimize| {
1142 const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {s} ({s})", .{
1143 root_src,
1144 @tagName(optimize),
1145 }) catch unreachable;
1146 if (self.test_filter) |filter| {
1147 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
1148 }
1149
1150 const exe = b.addExecutable(.{
1151 .name = "test",
1152 .root_source_file = .{ .path = root_src },
1153 .optimize = optimize,
1154 .target = .{},
1155 });
1156 if (link_libc) {
1157 exe.linkSystemLibrary("c");
1158 }
1159
1160 self.step.dependOn(&exe.step);
1161 }
1162 }
1163};
1164
1165pub const GenHContext = struct {
1166 b: *std.Build,
1167 step: *Step,
1168 test_index: usize,
1169 test_filter: ?[]const u8,
1170
1171 const TestCase = struct {
1172 name: []const u8,
1173 sources: ArrayList(SourceFile),
1174 expected_lines: ArrayList([]const u8),
1175
1176 const SourceFile = struct {
1177 filename: []const u8,
1178 source: []const u8,
1179 };
1180
1181 pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
1182 self.sources.append(SourceFile{
1183 .filename = filename,
1184 .source = source,
1185 }) catch unreachable;
1186 }
1187
1188 pub fn addExpectedLine(self: *TestCase, text: []const u8) void {
1189 self.expected_lines.append(text) catch unreachable;
1190 }
1191 };
1192
1193 const GenHCmpOutputStep = struct {
1194 step: Step,
1195 context: *GenHContext,
1196 obj: *CompileStep,
1197 name: []const u8,
1198 test_index: usize,
1199 case: *const TestCase,
1200
1201 pub fn create(
1202 context: *GenHContext,
1203 obj: *CompileStep,
1204 name: []const u8,
1205 case: *const TestCase,
1206 ) *GenHCmpOutputStep {
1207 const allocator = context.b.allocator;
1208 const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
1209 ptr.* = GenHCmpOutputStep{
1210 .step = Step.init(.{
1211 .id = .custom,
1212 .name = "ParseCCmpOutput",
1213 .owner = context.b,
1214 .makeFn = make,
1215 }),
1216 .context = context,
1217 .obj = obj,
1218 .name = name,
1219 .test_index = context.test_index,
1220 .case = case,
1221 };
1222 ptr.step.dependOn(&obj.step);
1223 context.test_index += 1;
1224 return ptr;
1225 }
1226
1227 fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1228 _ = prog_node;
1229 const self = @fieldParentPtr(GenHCmpOutputStep, "step", step);
1230 const b = self.context.b;
1231
1232 std.debug.print("Test {d}/{d} {s}...", .{ self.test_index + 1, self.context.test_index, self.name });
1233
1234 const full_h_path = self.obj.getOutputHPath();
1235 const actual_h = try io.readFileAlloc(b.allocator, full_h_path);
1236
1237 for (self.case.expected_lines.items) |expected_line| {
1238 if (mem.indexOf(u8, actual_h, expected_line) == null) {
1239 std.debug.print(
1240 \\
1241 \\========= Expected this output: ================
1242 \\{s}
1243 \\========= But found: ===========================
1244 \\{s}
1245 \\
1246 , .{ expected_line, actual_h });
1247 return error.TestFailed;
1248 }
1249 }
1250 std.debug.print("OK\n", .{});
1251 }
1252 };
1253
1254 pub fn create(
1255 self: *GenHContext,
1256 filename: []const u8,
1257 name: []const u8,
1258 source: []const u8,
1259 expected_lines: []const []const u8,
1260 ) *TestCase {
1261 const tc = self.b.allocator.create(TestCase) catch unreachable;
1262 tc.* = TestCase{
1263 .name = name,
1264 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
1265 .expected_lines = ArrayList([]const u8).init(self.b.allocator),
1266 };
1267
1268 tc.addSourceFile(filename, source);
1269 var arg_i: usize = 0;
1270 while (arg_i < expected_lines.len) : (arg_i += 1) {
1271 tc.addExpectedLine(expected_lines[arg_i]);
1272 }
1273 return tc;
1274 }
1275
1276 pub fn add(self: *GenHContext, name: []const u8, source: []const u8, expected_lines: []const []const u8) void {
1277 const tc = self.create("test.zig", name, source, expected_lines);
1278 self.addCase(tc);
1279 }
1280
1281 pub fn addCase(self: *GenHContext, case: *const TestCase) void {
1282 const b = self.b;
1283
1284 const optimize_mode = std.builtin.OptimizeMode.Debug;
1285 const annotated_case_name = fmt.allocPrint(self.b.allocator, "gen-h {s} ({s})", .{ case.name, @tagName(optimize_mode) }) catch unreachable;
1286 if (self.test_filter) |filter| {
1287 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
1288 }
1289
1290 const write_src = b.addWriteFiles();
1291 for (case.sources.items) |src_file| {
1292 write_src.add(src_file.filename, src_file.source);
1293 }
1294
1295 const obj = b.addObjectFromWriteFileStep("test", write_src, case.sources.items[0].filename);
1296 obj.setBuildMode(optimize_mode);
1297
1298 const cmp_h = GenHCmpOutputStep.create(self, obj, annotated_case_name, case);
1299
1300 self.step.dependOn(&cmp_h.step);
1301 }
1302};
1303
1304fn printInvocation(args: []const []const u8) void {
1305 for (args) |arg| {
1306 std.debug.print("{s} ", .{arg});
1307 }
1308 std.debug.print("\n", .{});
1309}
1310
1311const c_abi_targets = [_]CrossTarget{
1312 .{},
1313 .{
1314 .cpu_arch = .x86_64,
1315 .os_tag = .linux,
1316 .abi = .musl,
1317 },
1318 .{
1319 .cpu_arch = .x86,
1320 .os_tag = .linux,
1321 .abi = .musl,
1322 },
1323 .{
1324 .cpu_arch = .aarch64,
1325 .os_tag = .linux,
1326 .abi = .musl,
1327 },
1328 .{
1329 .cpu_arch = .arm,
1330 .os_tag = .linux,
1331 .abi = .musleabihf,
1332 },
1333 .{
1334 .cpu_arch = .mips,
1335 .os_tag = .linux,
1336 .abi = .musl,
1337 },
1338 .{
1339 .cpu_arch = .riscv64,
1340 .os_tag = .linux,
1341 .abi = .musl,
1342 },
1343 .{
1344 .cpu_arch = .wasm32,
1345 .os_tag = .wasi,
1346 .abi = .musl,
1347 },
1348 .{
1349 .cpu_arch = .powerpc,
1350 .os_tag = .linux,
1351 .abi = .musl,
1352 },
1353 .{
1354 .cpu_arch = .powerpc64le,
1355 .os_tag = .linux,
1356 .abi = .musl,
1357 },
1358 .{
1359 .cpu_arch = .x86,
1360 .os_tag = .windows,
1361 .abi = .gnu,
1362 },
1363 .{
1364 .cpu_arch = .x86_64,
1365 .os_tag = .windows,
1366 .abi = .gnu,
1367 },
1368};
1369
1370pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *Step {813pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *Step {
1371 const step = b.step("test-c-abi", "Run the C ABI tests");814 const step = b.step("test-c-abi", "Run the C ABI tests");
1372815
...@@ -1395,7 +838,7 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S...@@ -1395,7 +838,7 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
1395 test_step.want_lto = false;838 test_step.want_lto = false;
1396 }839 }
1397840
1398 const triple_prefix = c_abi_target.zigTriple(b.allocator) catch unreachable;841 const triple_prefix = c_abi_target.zigTriple(b.allocator) catch @panic("OOM");
1399 test_step.setNamePrefix(b.fmt("{s}-{s}-{s} ", .{842 test_step.setNamePrefix(b.fmt("{s}-{s}-{s} ", .{
1400 "test-c-abi",843 "test-c-abi",
1401 triple_prefix,844 triple_prefix,