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 {...@@ -539,6 +539,8 @@ pub const TestOptions = struct {
539 optimize: std.builtin.Mode = .Debug,539 optimize: std.builtin.Mode = .Debug,
540 version: ?std.builtin.Version = null,540 version: ?std.builtin.Version = null,
541 max_rss: usize = 0,541 max_rss: usize = 0,
542 filter: ?[]const u8 = null,
543 test_runner: ?[]const u8 = null,
542};544};
543545
544pub fn addTest(b: *Build, options: TestOptions) *CompileStep {546pub fn addTest(b: *Build, options: TestOptions) *CompileStep {
...@@ -549,6 +551,8 @@ pub fn addTest(b: *Build, options: TestOptions) *CompileStep {...@@ -549,6 +551,8 @@ pub fn addTest(b: *Build, options: TestOptions) *CompileStep {
549 .target = options.target,551 .target = options.target,
550 .optimize = options.optimize,552 .optimize = options.optimize,
551 .max_rss = options.max_rss,553 .max_rss = options.max_rss,
554 .filter = options.filter,
555 .test_runner = options.test_runner,
552 });556 });
553}557}
554558
lib/std/Build/CompileStep.zig+37-58
...@@ -97,8 +97,6 @@ out_lib_filename: []const u8,...@@ -97,8 +97,6 @@ out_lib_filename: []const u8,
97out_pdb_filename: []const u8,97out_pdb_filename: []const u8,
98modules: std.StringArrayHashMap(*Module),98modules: std.StringArrayHashMap(*Module),
9999
100object_src: []const u8,
101
102link_objects: ArrayList(LinkObject),100link_objects: ArrayList(LinkObject),
103include_dirs: ArrayList(IncludeDir),101include_dirs: ArrayList(IncludeDir),
104c_macros: ArrayList([]const u8),102c_macros: ArrayList([]const u8),
...@@ -287,6 +285,8 @@ pub const Options = struct {...@@ -287,6 +285,8 @@ pub const Options = struct {
287 linkage: ?Linkage = null,285 linkage: ?Linkage = null,
288 version: ?std.builtin.Version = null,286 version: ?std.builtin.Version = null,
289 max_rss: usize = 0,287 max_rss: usize = 0,
288 filter: ?[]const u8 = null,
289 test_runner: ?[]const u8 = null,
290};290};
291291
292pub const Kind = enum {292pub const Kind = enum {
...@@ -339,6 +339,23 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {...@@ -339,6 +339,23 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
339 options.target.zigTriple(owner.allocator) catch @panic("OOM"),339 options.target.zigTriple(owner.allocator) catch @panic("OOM"),
340 });340 });
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
342 const self = owner.allocator.create(CompileStep) catch @panic("OOM");359 const self = owner.allocator.create(CompileStep) catch @panic("OOM");
343 self.* = CompileStep{360 self.* = CompileStep{
344 .strip = null,361 .strip = null,
...@@ -360,7 +377,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {...@@ -360,7 +377,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
360 .max_rss = options.max_rss,377 .max_rss = options.max_rss,
361 }),378 }),
362 .version = options.version,379 .version = options.version,
363 .out_filename = undefined,380 .out_filename = out_filename,
364 .out_h_filename = owner.fmt("{s}.h", .{name}),381 .out_h_filename = owner.fmt("{s}.h", .{name}),
365 .out_lib_filename = undefined,382 .out_lib_filename = undefined,
366 .out_pdb_filename = owner.fmt("{s}.pdb", .{name}),383 .out_pdb_filename = owner.fmt("{s}.pdb", .{name}),
...@@ -374,13 +391,12 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {...@@ -374,13 +391,12 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
374 .rpaths = ArrayList(FileSource).init(owner.allocator),391 .rpaths = ArrayList(FileSource).init(owner.allocator),
375 .framework_dirs = ArrayList(FileSource).init(owner.allocator),392 .framework_dirs = ArrayList(FileSource).init(owner.allocator),
376 .installed_headers = ArrayList(*Step).init(owner.allocator),393 .installed_headers = ArrayList(*Step).init(owner.allocator),
377 .object_src = undefined,
378 .c_std = std.Build.CStd.C99,394 .c_std = std.Build.CStd.C99,
379 .zig_lib_dir = null,395 .zig_lib_dir = null,
380 .main_pkg_path = null,396 .main_pkg_path = null,
381 .exec_cmd_args = null,397 .exec_cmd_args = null,
382 .filter = null,398 .filter = options.filter,
383 .test_runner = null,399 .test_runner = options.test_runner,
384 .disable_stack_probing = false,400 .disable_stack_probing = false,
385 .disable_sanitize_c = false,401 .disable_sanitize_c = false,
386 .sanitize_thread = false,402 .sanitize_thread = false,
...@@ -395,60 +411,41 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {...@@ -395,60 +411,41 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
395 .output_pdb_path_source = GeneratedFile{ .step = &self.step },411 .output_pdb_path_source = GeneratedFile{ .step = &self.step },
396 .output_dirname_source = GeneratedFile{ .step = &self.step },412 .output_dirname_source = GeneratedFile{ .step = &self.step },
397413
398 .target_info = NativeTargetInfo.detect(self.target) catch @panic("unhandled error"),414 .target_info = target_info,
399 };415 };
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
424 if (self.kind == .lib) {417 if (self.kind == .lib) {
425 if (self.linkage != null and self.linkage.? == .static) {418 if (self.linkage != null and self.linkage.? == .static) {
426 self.out_lib_filename = self.out_filename;419 self.out_lib_filename = self.out_filename;
427 } else if (self.version) |version| {420 } else if (self.version) |version| {
428 if (target.isDarwin()) {421 if (target_info.target.isDarwin()) {
429 self.major_only_filename = b.fmt("lib{s}.{d}.dylib", .{422 self.major_only_filename = owner.fmt("lib{s}.{d}.dylib", .{
430 self.name,423 self.name,
431 version.major,424 version.major,
432 });425 });
433 self.name_only_filename = b.fmt("lib{s}.dylib", .{self.name});426 self.name_only_filename = owner.fmt("lib{s}.dylib", .{self.name});
434 self.out_lib_filename = self.out_filename;427 self.out_lib_filename = self.out_filename;
435 } else if (target.os.tag == .windows) {428 } else if (target_info.target.os.tag == .windows) {
436 self.out_lib_filename = b.fmt("{s}.lib", .{self.name});429 self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
437 } else {430 } else {
438 self.major_only_filename = b.fmt("lib{s}.so.{d}", .{ self.name, version.major });431 self.major_only_filename = owner.fmt("lib{s}.so.{d}", .{ self.name, version.major });
439 self.name_only_filename = b.fmt("lib{s}.so", .{self.name});432 self.name_only_filename = owner.fmt("lib{s}.so", .{self.name});
440 self.out_lib_filename = self.out_filename;433 self.out_lib_filename = self.out_filename;
441 }434 }
442 } else {435 } else {
443 if (target.isDarwin()) {436 if (target_info.target.isDarwin()) {
444 self.out_lib_filename = self.out_filename;437 self.out_lib_filename = self.out_filename;
445 } else if (target.os.tag == .windows) {438 } else if (target_info.target.os.tag == .windows) {
446 self.out_lib_filename = b.fmt("{s}.lib", .{self.name});439 self.out_lib_filename = owner.fmt("{s}.lib", .{self.name});
447 } else {440 } else {
448 self.out_lib_filename = self.out_filename;441 self.out_lib_filename = self.out_filename;
449 }442 }
450 }443 }
451 }444 }
445
446 if (root_src) |rs| rs.addStepDependencies(&self.step);
447
448 return self;
452}449}
453450
454pub fn installHeader(cs: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void {451pub 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 {...@@ -841,24 +838,6 @@ fn linkSystemLibraryInner(self: *CompileStep, name: []const u8, opts: struct {
841 }) catch @panic("OOM");838 }) catch @panic("OOM");
842}839}
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
862/// Handy when you have many C/C++ source files and want them all to have the same flags.841/// Handy when you have many C/C++ source files and want them all to have the same flags.
863pub fn addCSourceFiles(self: *CompileStep, files: []const []const u8, flags: []const []const u8) void {842pub fn addCSourceFiles(self: *CompileStep, files: []const []const u8, flags: []const []const u8) void {
864 const b = self.step.owner;843 const b = self.step.owner;
test/standalone/issue_13970/build.zig+3-3
...@@ -6,16 +6,16 @@ pub fn build(b: *std.Build) void {...@@ -6,16 +6,16 @@ pub fn build(b: *std.Build) void {
66
7 const test1 = b.addTest(.{7 const test1 = b.addTest(.{
8 .root_source_file = .{ .path = "test_root/empty.zig" },8 .root_source_file = .{ .path = "test_root/empty.zig" },
9 .test_runner = "src/main.zig",
9 });10 });
10 const test2 = b.addTest(.{11 const test2 = b.addTest(.{
11 .root_source_file = .{ .path = "src/empty.zig" },12 .root_source_file = .{ .path = "src/empty.zig" },
13 .test_runner = "src/main.zig",
12 });14 });
13 const test3 = b.addTest(.{15 const test3 = b.addTest(.{
14 .root_source_file = .{ .path = "empty.zig" },16 .root_source_file = .{ .path = "empty.zig" },
17 .test_runner = "src/main.zig",
15 });18 });
16 test1.setTestRunner("src/main.zig");
17 test2.setTestRunner("src/main.zig");
18 test3.setTestRunner("src/main.zig");
1919
20 test_step.dependOn(&b.addRunArtifact(test1).step);20 test_step.dependOn(&b.addRunArtifact(test1).step);
21 test_step.dependOn(&b.addRunArtifact(test2).step);21 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");...@@ -3,8 +3,8 @@ const std = @import("std");
3pub fn build(b: *std.Build) void {3pub fn build(b: *std.Build) void {
4 const t = b.addTest(.{4 const t = b.addTest(.{
5 .root_source_file = .{ .path = "src/main.zig" },5 .root_source_file = .{ .path = "src/main.zig" },
6 .test_runner = "test_runner/main.zig",
6 });7 });
7 t.setTestRunner("test_runner/main.zig");
88
9 const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } });9 const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } });
10 const module2 = b.createModule(.{10 const module2 = b.createModule(.{
test/tests.zig+6-6
...@@ -977,11 +977,11 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {...@@ -977,11 +977,11 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
977 .optimize = test_target.optimize_mode,977 .optimize = test_target.optimize_mode,
978 .target = test_target.target,978 .target = test_target.target,
979 .max_rss = max_rss,979 .max_rss = max_rss,
980 .filter = options.test_filter,
980 });981 });
981 const single_threaded_txt = if (test_target.single_threaded) "single" else "multi";982 const single_threaded_txt = if (test_target.single_threaded) "single" else "multi";
982 const backend_txt = if (test_target.backend) |backend| @tagName(backend) else "default";983 const backend_txt = if (test_target.backend) |backend| @tagName(backend) else "default";
983 these_tests.single_threaded = test_target.single_threaded;984 these_tests.single_threaded = test_target.single_threaded;
984 these_tests.setFilter(options.test_filter);
985 if (test_target.link_libc) {985 if (test_target.link_libc) {
986 these_tests.linkSystemLibrary("c");986 these_tests.linkSystemLibrary("c");
987 }987 }
...@@ -1037,10 +1037,15 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S...@@ -1037,10 +1037,15 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
1037 continue;1037 continue;
1038 }1038 }
10391039
1040 const triple_prefix = c_abi_target.zigTriple(b.allocator) catch @panic("OOM");
1041
1040 const test_step = b.addTest(.{1042 const test_step = b.addTest(.{
1041 .root_source_file = .{ .path = "test/c_abi/main.zig" },1043 .root_source_file = .{ .path = "test/c_abi/main.zig" },
1042 .optimize = optimize_mode,1044 .optimize = optimize_mode,
1043 .target = c_abi_target,1045 .target = c_abi_target,
1046 .name = b.fmt("test-c-abi-{s}-{s}", .{
1047 triple_prefix, @tagName(optimize_mode),
1048 }),
1044 });1049 });
1045 if (c_abi_target.abi != null and c_abi_target.abi.?.isMusl()) {1050 if (c_abi_target.abi != null and c_abi_target.abi.?.isMusl()) {
1046 // TODO NativeTargetInfo insists on dynamically linking musl1051 // 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...@@ -1057,11 +1062,6 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
1057 test_step.want_lto = false;1062 test_step.want_lto = false;
1058 }1063 }
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
1065 const run = b.addRunArtifact(test_step);1065 const run = b.addRunArtifact(test_step);
1066 run.skip_foreign_checks = true;1066 run.skip_foreign_checks = true;
1067 step.dependOn(&run.step);1067 step.dependOn(&run.step);