Skip to content

Commit 1728d92

Browse files
authored
Merge pull request #15245 from ziglang/zig-build-install-artifact
fix broken and confusing artifact installation logic
2 parents 23d7921 + 406706f commit 1728d92

File tree

46 files changed

+76
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+76
-89
lines changed

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub fn build(b: *std.Build) !void {
181181
exe.sanitize_thread = sanitize_thread;
182182
exe.build_id = b.option(bool, "build-id", "Include a build id note") orelse false;
183183
exe.entitlements = entitlements;
184-
exe.install();
184+
b.installArtifact(exe);
185185

186186
const compile_step = b.step("compile", "Build the self-hosted compiler");
187187
compile_step.dependOn(&exe.step);

lib/init-exe/build.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ pub fn build(b: *std.Build) void {
2727
// This declares intent for the executable to be installed into the
2828
// standard location when the user invokes the "install" step (the default
2929
// step when running `zig build`).
30-
exe.install();
30+
b.installArtifact(exe);
3131

3232
// This *creates* a RunStep in the build graph, to be executed when another
3333
// step is evaluated that depends on it. The next line below will establish
3434
// such a dependency.
35-
const run_cmd = exe.run();
35+
const run_cmd = b.addRunArtifact(exe);
3636

3737
// By making the run step depend on the install step, it will be run from the
3838
// installation directory rather than directly from within the cache directory.
@@ -52,16 +52,19 @@ pub fn build(b: *std.Build) void {
5252
const run_step = b.step("run", "Run the app");
5353
run_step.dependOn(&run_cmd.step);
5454

55-
// Creates a step for unit testing.
56-
const exe_tests = b.addTest(.{
55+
// Creates a step for unit testing. This only builds the test executable
56+
// but does not run it.
57+
const unit_tests = b.addTest(.{
5758
.root_source_file = .{ .path = "src/main.zig" },
5859
.target = target,
5960
.optimize = optimize,
6061
});
6162

63+
const run_unit_tests = b.addRunArtifact(unit_tests);
64+
6265
// Similar to creating the run step earlier, this exposes a `test` step to
6366
// the `zig build --help` menu, providing a way for the user to request
6467
// running the unit tests.
6568
const test_step = b.step("test", "Run unit tests");
66-
test_step.dependOn(&exe_tests.step);
69+
test_step.dependOn(&run_unit_tests.step);
6770
}

lib/init-lib/build.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ pub fn build(b: *std.Build) void {
2727
// This declares intent for the library to be installed into the standard
2828
// location when the user invokes the "install" step (the default step when
2929
// running `zig build`).
30-
lib.install();
30+
b.installArtifact(lib);
3131

32-
// Creates a step for unit testing.
32+
// Creates a step for unit testing. This only builds the test executable
33+
// but does not run it.
3334
const main_tests = b.addTest(.{
3435
.root_source_file = .{ .path = "src/main.zig" },
3536
.target = target,
3637
.optimize = optimize,
3738
});
3839

40+
const run_main_tests = b.addRunArtifact(main_tests);
41+
3942
// This creates a build step. It will be visible in the `zig build --help` menu,
4043
// and can be selected like this: `zig build test`
4144
// This will evaluate the `test` step rather than the default, which is "install".
4245
const test_step = b.step("test", "Run library tests");
43-
test_step.dependOn(&main_tests.step);
46+
test_step.dependOn(&run_main_tests.step);
4447
}

lib/std/Build/CompileStep.zig

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ vcpkg_bin_path: ?[]const u8 = null,
111111
/// This may be set in order to override the default install directory
112112
override_dest_dir: ?InstallDir,
113113
installed_path: ?[]const u8,
114-
install_step: ?*InstallArtifactStep,
115114

116115
/// Base address for an executable image.
117116
image_base: ?u64 = null,
@@ -390,7 +389,6 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep {
390389
.output_dir = null,
391390
.override_dest_dir = null,
392391
.installed_path = null,
393-
.install_step = null,
394392
.force_undefined_symbols = StringHashMap(void).init(owner.allocator),
395393

396394
.output_path_source = GeneratedFile{ .step = &self.step },
@@ -465,11 +463,6 @@ pub fn setOutputDir(self: *CompileStep, dir: []const u8) void {
465463
self.output_dir = b.dupePath(dir);
466464
}
467465

468-
pub fn install(self: *CompileStep) void {
469-
const b = self.step.owner;
470-
b.installArtifact(self);
471-
}
472-
473466
pub fn installHeader(cs: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void {
474467
const b = cs.step.owner;
475468
const install_file = b.addInstallHeaderFile(src_path, dest_rel_path);
@@ -533,7 +526,7 @@ pub fn installLibraryHeaders(cs: *CompileStep, l: *CompileStep) void {
533526
const T = id.Type();
534527
const ptr = b.allocator.create(T) catch @panic("OOM");
535528
ptr.* = step.cast(T).?.*;
536-
ptr.dest_builder = b;
529+
ptr.step.owner = b;
537530
break :blk &ptr.step;
538531
},
539532
else => unreachable,
@@ -557,12 +550,13 @@ pub fn addObjCopy(cs: *CompileStep, options: ObjCopyStep.Options) *ObjCopyStep {
557550
return b.addObjCopy(cs.getOutputSource(), copy);
558551
}
559552

560-
/// Deprecated: use `std.Build.addRunArtifact`
561-
/// This function will run in the context of the package that created the executable,
553+
/// This function would run in the context of the package that created the executable,
562554
/// which is undesirable when running an executable provided by a dependency package.
563-
pub fn run(cs: *CompileStep) *RunStep {
564-
return cs.step.owner.addRunArtifact(cs);
565-
}
555+
pub const run = @compileError("deprecated; use std.Build.addRunArtifact");
556+
557+
/// This function would install in the context of the package that created the artifact,
558+
/// which is undesirable when installing an artifact provided by a dependency package.
559+
pub const install = @compileError("deprecated; use std.Build.installArtifact");
566560

567561
pub fn checkObject(self: *CompileStep) *CheckObjectStep {
568562
return CheckObjectStep.create(self.step.owner, self.getOutputSource(), self.target_info.target.ofmt);

lib/std/Build/InstallArtifactStep.zig

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const fs = std.fs;
88
pub const base_id = .install_artifact;
99

1010
step: Step,
11-
dest_builder: *std.Build,
1211
artifact: *CompileStep,
1312
dest_dir: InstallDir,
1413
pdb_dir: ?InstallDir,
@@ -18,8 +17,6 @@ h_dir: ?InstallDir,
1817
dest_sub_path: ?[]const u8,
1918

2019
pub fn create(owner: *std.Build, artifact: *CompileStep) *InstallArtifactStep {
21-
if (artifact.install_step) |s| return s;
22-
2320
const self = owner.allocator.create(InstallArtifactStep) catch @panic("OOM");
2421
self.* = InstallArtifactStep{
2522
.step = Step.init(.{
@@ -28,7 +25,6 @@ pub fn create(owner: *std.Build, artifact: *CompileStep) *InstallArtifactStep {
2825
.owner = owner,
2926
.makeFn = make,
3027
}),
31-
.dest_builder = owner,
3228
.artifact = artifact,
3329
.dest_dir = artifact.override_dest_dir orelse switch (artifact.kind) {
3430
.obj => @panic("Cannot install a .obj build artifact."),
@@ -46,7 +42,6 @@ pub fn create(owner: *std.Build, artifact: *CompileStep) *InstallArtifactStep {
4642
.dest_sub_path = null,
4743
};
4844
self.step.dependOn(&artifact.step);
49-
artifact.install_step = self;
5045

5146
owner.pushInstalledFile(self.dest_dir, artifact.out_filename);
5247
if (self.artifact.isDynamicLibrary()) {
@@ -71,9 +66,9 @@ pub fn create(owner: *std.Build, artifact: *CompileStep) *InstallArtifactStep {
7166

7267
fn make(step: *Step, prog_node: *std.Progress.Node) !void {
7368
_ = prog_node;
74-
const src_builder = step.owner;
7569
const self = @fieldParentPtr(InstallArtifactStep, "step", step);
76-
const dest_builder = self.dest_builder;
70+
const src_builder = self.artifact.step.owner;
71+
const dest_builder = step.owner;
7772

7873
const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename;
7974
const full_dest_path = dest_builder.getInstallPath(self.dest_dir, dest_sub_path);

lib/std/Build/RunStep.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,9 @@ fn spawnChildAndCollect(
866866
child.request_resource_usage_statistics = true;
867867

868868
child.stdin_behavior = switch (self.stdio) {
869-
.infer_from_args => if (has_side_effects) .Inherit else .Close,
869+
.infer_from_args => if (has_side_effects) .Inherit else .Ignore,
870870
.inherit => .Inherit,
871-
.check => .Close,
871+
.check => .Ignore,
872872
.zig_test => .Pipe,
873873
};
874874
child.stdout_behavior = switch (self.stdio) {

test/link/bss/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void {
1010
.optimize = .Debug,
1111
});
1212

13-
const run = exe.run();
13+
const run = b.addRunArtifact(exe);
1414
run.expectStdOutEqual("0, 1, 0\n");
1515

1616
test_step.dependOn(&run.step);

test/link/common_symbols/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2424
});
2525
test_exe.linkLibrary(lib_a);
2626

27-
test_step.dependOn(&test_exe.run().step);
27+
test_step.dependOn(&b.addRunArtifact(test_exe).step);
2828
}

test/link/common_symbols_alignment/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2424
});
2525
test_exe.linkLibrary(lib_a);
2626

27-
test_step.dependOn(&test_exe.run().step);
27+
test_step.dependOn(&b.addRunArtifact(test_exe).step);
2828
}

test/link/interdependent_static_c_libs/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3535
test_exe.linkLibrary(lib_b);
3636
test_exe.addIncludePath(".");
3737

38-
test_step.dependOn(&test_exe.run().step);
38+
test_step.dependOn(&b.addRunArtifact(test_exe).step);
3939
}

test/link/macho/bugs/13056/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3131
});
3232
exe.addObjectFile(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/lib/libc++.tbd" }) catch unreachable);
3333

34-
const run_cmd = exe.run();
34+
const run_cmd = b.addRunArtifact(exe);
3535
run_cmd.expectStdErrEqual("x: 5\n");
3636

3737
test_step.dependOn(&run_cmd.step);

test/link/macho/dead_strip_dylibs/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2727

2828
test_step.dependOn(&check.step);
2929

30-
const run_cmd = exe.run();
30+
const run_cmd = b.addRunArtifact(exe);
3131
test_step.dependOn(&run_cmd.step);
3232
}
3333

test/link/macho/entry_in_archive/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2929
exe.linkLibrary(lib);
3030
exe.linkLibC();
3131

32-
const run = exe.run();
32+
const run = b.addRunArtifact(exe);
3333
run.skip_foreign_checks = true;
3434
run.expectExitCode(0);
3535
test_step.dependOn(&run.step);

test/link/macho/headerpad/build.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3636

3737
test_step.dependOn(&check.step);
3838

39-
const run = exe.run();
39+
const run = b.addRunArtifact(exe);
4040
test_step.dependOn(&run.step);
4141
}
4242

@@ -52,7 +52,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
5252

5353
test_step.dependOn(&check.step);
5454

55-
const run = exe.run();
55+
const run = b.addRunArtifact(exe);
5656
test_step.dependOn(&run.step);
5757
}
5858

@@ -69,7 +69,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
6969

7070
test_step.dependOn(&check.step);
7171

72-
const run = exe.run();
72+
const run = b.addRunArtifact(exe);
7373
test_step.dependOn(&run.step);
7474
}
7575

@@ -95,7 +95,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
9595

9696
test_step.dependOn(&check.step);
9797

98-
const run = exe.run();
98+
const run = b.addRunArtifact(exe);
9999
test_step.dependOn(&run.step);
100100
}
101101
}

test/link/macho/needed_framework/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3030
check.checkNext("name {*}Cocoa");
3131
test_step.dependOn(&check.step);
3232

33-
const run_cmd = exe.run();
33+
const run_cmd = b.addRunArtifact(exe);
3434
test_step.dependOn(&run_cmd.step);
3535
}

test/link/macho/objcpp/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2727
// populate paths to the sysroot here.
2828
exe.linkFramework("Foundation");
2929

30-
const run_cmd = exe.run();
30+
const run_cmd = b.addRunArtifact(exe);
3131
run_cmd.expectStdOutEqual("Hello from C++ and Zig");
3232

3333
test_step.dependOn(&run_cmd.step);

test/link/macho/tls/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3232
test_exe.linkLibrary(lib);
3333
test_exe.linkLibC();
3434

35-
const run = test_exe.run();
35+
const run = b.addRunArtifact(test_exe);
3636
run.skip_foreign_checks = true;
3737

3838
test_step.dependOn(&run.step);

test/link/macho/weak_framework/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2727
check.checkNext("name {*}Cocoa");
2828
test_step.dependOn(&check.step);
2929

30-
const run_cmd = exe.run();
30+
const run_cmd = b.addRunArtifact(exe);
3131
test_step.dependOn(&run_cmd.step);
3232
}

test/link/macho/weak_library/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2323
});
2424
dylib.addCSourceFile("a.c", &.{});
2525
dylib.linkLibC();
26-
dylib.install();
26+
b.installArtifact(dylib);
2727

2828
const exe = b.addExecutable(.{
2929
.name = "test",

test/link/wasm/producers/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2323
lib.use_llvm = false;
2424
lib.use_lld = false;
2525
lib.strip = false;
26-
lib.install();
26+
b.installArtifact(lib);
2727

2828
const version_fmt = "version " ++ builtin.zig_version_string;
2929

test/link/wasm/segments/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2222
lib.use_llvm = false;
2323
lib.use_lld = false;
2424
lib.strip = false;
25-
lib.install();
25+
b.installArtifact(lib);
2626

2727
const check_lib = lib.checkObject();
2828
check_lib.checkStart("Section data");

test/link/wasm/stack_pointer/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2323
lib.use_lld = false;
2424
lib.strip = false;
2525
lib.stack_size = std.wasm.page_size * 2; // set an explicit stack size
26-
lib.install();
26+
b.installArtifact(lib);
2727

2828
const check_lib = lib.checkObject();
2929

test/link/wasm/type/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2222
lib.use_llvm = false;
2323
lib.use_lld = false;
2424
lib.strip = false;
25-
lib.install();
25+
b.installArtifact(lib);
2626

2727
const check_lib = lib.checkObject();
2828
check_lib.checkStart("Section type");

test/src/CompareOutput.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
101101
});
102102
exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?);
103103

104-
const run = exe.run();
104+
const run = b.addRunArtifact(exe);
105105
run.setName(annotated_case_name);
106106
run.addArgs(case.cli_args);
107107
run.expectStdOutEqual(case.expected_output);
@@ -128,7 +128,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
128128
exe.linkSystemLibrary("c");
129129
}
130130

131-
const run = exe.run();
131+
const run = b.addRunArtifact(exe);
132132
run.setName(annotated_case_name);
133133
run.addArgs(case.cli_args);
134134
run.expectStdOutEqual(case.expected_output);
@@ -155,7 +155,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
155155
exe.linkSystemLibrary("c");
156156
}
157157

158-
const run = exe.run();
158+
const run = b.addRunArtifact(exe);
159159
run.setName(annotated_case_name);
160160
run.addArgs(case.cli_args);
161161
run.expectExitCode(126);

test/src/run_translated_c.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub const RunTranslatedCContext = struct {
9494
const exe = translate_c.addExecutable(.{});
9595
exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name});
9696
exe.linkLibC();
97-
const run = exe.run();
97+
const run = b.addRunArtifact(exe);
9898
run.step.name = b.fmt("{s} run", .{annotated_case_name});
9999
if (!case.allow_warnings) {
100100
run.expectStdErrEqual("");

0 commit comments

Comments
 (0)