authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-10 18:09:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-11 08:42:14-07:00
log3c3cee2cfabc5d03bb83cf6cc6a8ed80f13ee1df
treeb440fbaf013ed472c39a60381928c0224e694f5c
parentd5eab33fd23351a8606cb69a846b1b753b4c7c4a

fix build logic due to state mutations and break the API accordingly

* remove setName, setFilter, and setTestRunner. Please set these options directly when creating the CompileStep. * removed unused field * remove computeOutFileNames and inline the logic, making clear the goal of avoiding state mutations after the build step is created.

5 files changed, 51 insertions(+), 68 deletions(-)

lib/std/Build.zig+4
......@@ -539,6 +539,8 @@ pub const TestOptions = struct {
539539 optimize: std.builtin.Mode = .Debug,
540540 version: ?std.builtin.Version = null,
541541 max_rss: usize = 0,
542 filter: ?[]const u8 = null,
543 test_runner: ?[]const u8 = null,
542544};
543545
544546pub fn addTest(b: *Build, options: TestOptions) *CompileStep {
......@@ -549,6 +551,8 @@ pub fn addTest(b: *Build, options: TestOptions) *CompileStep {
549551 .target = options.target,
550552 .optimize = options.optimize,
551553 .max_rss = options.max_rss,
554 .filter = options.filter,
555 .test_runner = options.test_runner,
552556 });
553557}
554558
lib/std/Build/CompileStep.zig+37-58
......@@ -97,8 +97,6 @@ out_lib_filename: []const u8,
9797out_pdb_filename: []const u8,
9898modules: std.StringArrayHashMap(*Module),
9999
100object_src: []const u8,
101
102100link_objects: ArrayList(LinkObject),
103101include_dirs: ArrayList(IncludeDir),
104102c_macros: ArrayList([]const u8),
......@@ -287,6 +285,8 @@ pub const Options = struct {
287285 linkage: ?Linkage = null,
288286 version: ?std.builtin.Version = null,
289287 max_rss: usize = 0,
288 filter: ?[]const u8 = null,
289 test_runner: ?[]const u8 = null,
290290};
291291
292292pub const Kind = enum {
......@@ -339,6 +339,23 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
339339 options.target.zigTriple(owner.allocator) catch @panic("OOM"),
340340 });
341341
342 const target_info = NativeTargetInfo.detect(options.target) catch @panic("unhandled error");
343
344 const out_filename = std.zig.binNameAlloc(owner.allocator, .{
345 .root_name = name,
346 .target = target_info.target,
347 .output_mode = switch (options.kind) {
348 .lib => .Lib,
349 .obj => .Obj,
350 .exe, .@"test" => .Exe,
351 },
352 .link_mode = if (options.linkage) |some| @as(std.builtin.LinkMode, switch (some) {
353 .dynamic => .Dynamic,
354 .static => .Static,
355 }) else null,
356 .version = options.version,
357 }) catch @panic("OOM");
358
342359 const self = owner.allocator.create(CompileStep) catch @panic("OOM");
343360 self.* = CompileStep{
344361 .strip = null,
......@@ -360,7 +377,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
360377 .max_rss = options.max_rss,
361378 }),
362379 .version = options.version,
363 .out_filename = undefined,
380 .out_filename = out_filename,
364381 .out_h_filename = owner.fmt("{s}.h", .{name}),
365382 .out_lib_filename = undefined,
366383 .out_pdb_filename = owner.fmt("{s}.pdb", .{name}),
......@@ -374,13 +391,12 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
374391 .rpaths = ArrayList(FileSource).init(owner.allocator),
375392 .framework_dirs = ArrayList(FileSource).init(owner.allocator),
376393 .installed_headers = ArrayList(*Step).init(owner.allocator),
377 .object_src = undefined,
378394 .c_std = std.Build.CStd.C99,
379395 .zig_lib_dir = null,
380396 .main_pkg_path = null,
381397 .exec_cmd_args = null,
382 .filter = null,
383 .test_runner = null,
398 .filter = options.filter,
399 .test_runner = options.test_runner,
384400 .disable_stack_probing = false,
385401 .disable_sanitize_c = false,
386402 .sanitize_thread = false,
......@@ -395,60 +411,41 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
395411 .output_pdb_path_source = GeneratedFile{ .step = &self.step },
396412 .output_dirname_source = GeneratedFile{ .step = &self.step },
397413
398 .target_info = NativeTargetInfo.detect(self.target) catch @panic("unhandled error"),
414 .target_info = target_info,
399415 };
400 self.computeOutFileNames();
401 if (root_src) |rs| rs.addStepDependencies(&self.step);
402 return self;
403}
404
405fn computeOutFileNames(self: *CompileStep) void {
406 const b = self.step.owner;
407 const target = self.target_info.target;
408
409 self.out_filename = std.zig.binNameAlloc(b.allocator, .{
410 .root_name = self.name,
411 .target = target,
412 .output_mode = switch (self.kind) {
413 .lib => .Lib,
414 .obj => .Obj,
415 .exe, .@"test" => .Exe,
416 },
417 .link_mode = if (self.linkage) |some| @as(std.builtin.LinkMode, switch (some) {
418 .dynamic => .Dynamic,
419 .static => .Static,
420 }) else null,
421 .version = self.version,
422 }) catch @panic("OOM");
423416
424417 if (self.kind == .lib) {
425418 if (self.linkage != null and self.linkage.? == .static) {
426419 self.out_lib_filename = self.out_filename;
427420 } else if (self.version) |version| {
428 if (target.isDarwin()) {
429 self.major_only_filename = b.fmt("lib{s}.{d}.dylib", .{
421 if (target_info.target.isDarwin()) {
422 self.major_only_filename = owner.fmt("lib{s}.{d}.dylib", .{
430423 self.name,
431424 version.major,
432425 });
433 self.name_only_filename = b.fmt("lib{s}.dylib", .{self.name});
426 self.name_only_filename = owner.fmt("lib{s}.dylib", .{self.name});
434427 self.out_lib_filename = self.out_filename;
435 } else if (target.os.tag == .windows) {
436 self.out_lib_filename = b.fmt("{s}.lib", .{self.name});
428 } else if (target_info.target.os.tag == .windows) {
429 self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
437430 } else {
438 self.major_only_filename = b.fmt("lib{s}.so.{d}", .{ self.name, version.major });
439 self.name_only_filename = b.fmt("lib{s}.so", .{self.name});
431 self.major_only_filename = owner.fmt("lib{s}.so.{d}", .{ self.name, version.major });
432 self.name_only_filename = owner.fmt("lib{s}.so", .{self.name});
440433 self.out_lib_filename = self.out_filename;
441434 }
442435 } else {
443 if (target.isDarwin()) {
436 if (target_info.target.isDarwin()) {
444437 self.out_lib_filename = self.out_filename;
445 } else if (target.os.tag == .windows) {
446 self.out_lib_filename = b.fmt("{s}.lib", .{self.name});
438 } else if (target_info.target.os.tag == .windows) {
439 self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
447440 } else {
448441 self.out_lib_filename = self.out_filename;
449442 }
450443 }
451444 }
445
446 if (root_src) |rs| rs.addStepDependencies(&self.step);
447
448 return self;
452449}
453450
454451pub fn installHeader(cs: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void {
......@@ -841,24 +838,6 @@ fn linkSystemLibraryInner(self: *CompileStep, name: []const u8, opts: struct {
841838 }) catch @panic("OOM");
842839}
843840
844pub fn setName(self: *CompileStep, text: []const u8) void {
845 const b = self.step.owner;
846 assert(self.kind == .@"test");
847 self.name = b.dupe(text);
848}
849
850pub fn setFilter(self: *CompileStep, text: ?[]const u8) void {
851 const b = self.step.owner;
852 assert(self.kind == .@"test");
853 self.filter = if (text) |t| b.dupe(t) else null;
854}
855
856pub fn setTestRunner(self: *CompileStep, path: ?[]const u8) void {
857 const b = self.step.owner;
858 assert(self.kind == .@"test");
859 self.test_runner = if (path) |p| b.dupePath(p) else null;
860}
861
862841/// Handy when you have many C/C++ source files and want them all to have the same flags.
863842pub fn addCSourceFiles(self: *CompileStep, files: []const []const u8, flags: []const []const u8) void {
864843 const b = self.step.owner;
test/standalone/issue_13970/build.zig+3-3
......@@ -6,16 +6,16 @@ pub fn build(b: *std.Build) void {
66
77 const test1 = b.addTest(.{
88 .root_source_file = .{ .path = "test_root/empty.zig" },
9 .test_runner = "src/main.zig",
910 });
1011 const test2 = b.addTest(.{
1112 .root_source_file = .{ .path = "src/empty.zig" },
13 .test_runner = "src/main.zig",
1214 });
1315 const test3 = b.addTest(.{
1416 .root_source_file = .{ .path = "empty.zig" },
17 .test_runner = "src/main.zig",
1518 });
16 test1.setTestRunner("src/main.zig");
17 test2.setTestRunner("src/main.zig");
18 test3.setTestRunner("src/main.zig");
1919
2020 test_step.dependOn(&b.addRunArtifact(test1).step);
2121 test_step.dependOn(&b.addRunArtifact(test2).step);
test/standalone/test_runner_module_imports/build.zig+1-1
......@@ -3,8 +3,8 @@ const std = @import("std");
33pub fn build(b: *std.Build) void {
44 const t = b.addTest(.{
55 .root_source_file = .{ .path = "src/main.zig" },
6 .test_runner = "test_runner/main.zig",
67 });
7 t.setTestRunner("test_runner/main.zig");
88
99 const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } });
1010 const module2 = b.createModule(.{
test/tests.zig+6-6
......@@ -977,11 +977,11 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
977977 .optimize = test_target.optimize_mode,
978978 .target = test_target.target,
979979 .max_rss = max_rss,
980 .filter = options.test_filter,
980981 });
981982 const single_threaded_txt = if (test_target.single_threaded) "single" else "multi";
982983 const backend_txt = if (test_target.backend) |backend| @tagName(backend) else "default";
983984 these_tests.single_threaded = test_target.single_threaded;
984 these_tests.setFilter(options.test_filter);
985985 if (test_target.link_libc) {
986986 these_tests.linkSystemLibrary("c");
987987 }
......@@ -1037,10 +1037,15 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
10371037 continue;
10381038 }
10391039
1040 const triple_prefix = c_abi_target.zigTriple(b.allocator) catch @panic("OOM");
1041
10401042 const test_step = b.addTest(.{
10411043 .root_source_file = .{ .path = "test/c_abi/main.zig" },
10421044 .optimize = optimize_mode,
10431045 .target = c_abi_target,
1046 .name = b.fmt("test-c-abi-{s}-{s}", .{
1047 triple_prefix, @tagName(optimize_mode),
1048 }),
10441049 });
10451050 if (c_abi_target.abi != null and c_abi_target.abi.?.isMusl()) {
10461051 // TODO NativeTargetInfo insists on dynamically linking musl
......@@ -1057,11 +1062,6 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
10571062 test_step.want_lto = false;
10581063 }
10591064
1060 const triple_prefix = c_abi_target.zigTriple(b.allocator) catch @panic("OOM");
1061 test_step.setName(b.fmt("test-c-abi-{s}-{s} ", .{
1062 triple_prefix, @tagName(optimize_mode),
1063 }));
1064
10651065 const run = b.addRunArtifact(test_step);
10661066 run.skip_foreign_checks = true;
10671067 step.dependOn(&run.step);