diff --git a/lib/std/build.zig b/lib/std/build.zig index 6ba6a08af030f599b77ffd43c9e2d8128bcce60c..5e4f83d2789a1ed42470df5a0118a0af431e45c9 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -242,6 +242,20 @@ pub const Builder = struct { return LibExeObjStep.createObject(self, name, root_src_param); } + pub fn addObjectFromWriteFileStep( + self: *Builder, + name: []const u8, + wfs: *WriteFileStep, + basename: []const u8, + ) *LibExeObjStep { + return LibExeObjStep.createObject(self, name, @as(FileSource, .{ + .write_file = .{ + .step = wfs, + .basename = basename, + }, + })); + } + pub fn addSharedLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8, ver: Version) *LibExeObjStep { const root_src_param = if (root_src) |p| @as(FileSource, .{ .path = p }) else null; return LibExeObjStep.createSharedLibrary(self, name, root_src_param, ver); diff --git a/test/tests.zig b/test/tests.zig index ae9b68c84477da8ea9904dda4e65ef83be1c22d3..7c97e46e0280d7494807eb6bafe1592372552920 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -737,6 +737,7 @@ pub const CompileErrorContext = struct { test_index: usize, case: *const TestCase, build_mode: Mode, + write_src: *build.WriteFileStep, const ErrLineIter = struct { lines: mem.SplitIterator, @@ -756,7 +757,13 @@ pub const CompileErrorContext = struct { } }; - pub fn create(context: *CompileErrorContext, name: []const u8, case: *const TestCase, build_mode: Mode) *CompileCmpOutputStep { + pub fn create( + context: *CompileErrorContext, + name: []const u8, + case: *const TestCase, + build_mode: Mode, + write_src: *build.WriteFileStep, + ) *CompileCmpOutputStep { const allocator = context.b.allocator; const ptr = allocator.create(CompileCmpOutputStep) catch unreachable; ptr.* = CompileCmpOutputStep{ @@ -766,6 +773,7 @@ pub const CompileErrorContext = struct { .test_index = context.test_index, .case = case, .build_mode = build_mode, + .write_src = write_src, }; context.test_index += 1; @@ -776,11 +784,6 @@ pub const CompileErrorContext = struct { const self = @fieldParentPtr(CompileCmpOutputStep, "step", step); const b = self.context.b; - const root_src = fs.path.join( - b.allocator, - &[_][]const u8{ b.cache_root, self.case.sources.items[0].filename }, - ) catch unreachable; - var zig_args = ArrayList([]const u8).init(b.allocator); zig_args.append(b.zig_exe) catch unreachable; @@ -791,7 +794,8 @@ pub const CompileErrorContext = struct { } else { try zig_args.append("build-obj"); } - zig_args.append(b.pathFromRoot(root_src)) catch unreachable; + const root_src_basename = self.case.sources.toSliceConst()[0].filename; + try zig_args.append(self.write_src.getOutputPath(root_src_basename)); zig_args.append("--name") catch unreachable; zig_args.append("test") catch unreachable; @@ -990,18 +994,14 @@ pub const CompileErrorContext = struct { if (self.test_filter) |filter| { if (mem.indexOf(u8, annotated_case_name, filter) == null) return; } + const write_src = b.addWriteFiles(); + for (case.sources.toSliceConst()) |src_file| { + write_src.add(src_file.filename, src_file.source); + } - const compile_and_cmp_errors = CompileCmpOutputStep.create(self, annotated_case_name, case, .Debug); + const compile_and_cmp_errors = CompileCmpOutputStep.create(self, annotated_case_name, case, .Debug, write_src); + compile_and_cmp_errors.step.dependOn(&write_src.step); self.step.dependOn(&compile_and_cmp_errors.step); - - for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = fs.path.join( - b.allocator, - &[_][]const u8{ b.cache_root, src_file.filename }, - ) catch unreachable; - const write_src = b.addWriteFile(expanded_src_path, src_file.source); - compile_and_cmp_errors.step.dependOn(&write_src.step); - } } }; @@ -1195,10 +1195,6 @@ pub const GenHContext = struct { pub fn addCase(self: *GenHContext, case: *const TestCase) void { const b = self.b; - const root_src = fs.path.join( - b.allocator, - &[_][]const u8{ b.cache_root, case.sources.items[0].filename }, - ) catch unreachable; const mode = builtin.Mode.Debug; const annotated_case_name = fmt.allocPrint(self.b.allocator, "gen-h {} ({})", .{ case.name, @tagName(mode) }) catch unreachable; @@ -1206,18 +1202,14 @@ pub const GenHContext = struct { if (mem.indexOf(u8, annotated_case_name, filter) == null) return; } - const obj = b.addObject("test", root_src); + const write_src = b.addWriteFiles(); + for (case.sources.toSliceConst()) |src_file| { + write_src.add(src_file.filename, src_file.source); + } + + const obj = b.addObjectFromWriteFileStep("test", write_src, case.sources.items[0].filename); obj.setBuildMode(mode); - for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = fs.path.join( - b.allocator, - &[_][]const u8{ b.cache_root, src_file.filename }, - ) catch unreachable; - const write_src = b.addWriteFile(expanded_src_path, src_file.source); - obj.step.dependOn(&write_src.step); - } - const cmp_h = GenHCmpOutputStep.create(self, obj, annotated_case_name, case); self.step.dependOn(&cmp_h.step);