Skip to content

Commit

Permalink
Merge pull request #15245 from ziglang/zig-build-install-artifact
Browse files Browse the repository at this point in the history
fix broken and confusing artifact installation logic
  • Loading branch information
andrewrk authored Apr 11, 2023
2 parents 23d7921 + 406706f commit 1728d92
Show file tree
Hide file tree
Showing 46 changed files with 76 additions and 89 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn build(b: *std.Build) !void {
exe.sanitize_thread = sanitize_thread;
exe.build_id = b.option(bool, "build-id", "Include a build id note") orelse false;
exe.entitlements = entitlements;
exe.install();
b.installArtifact(exe);

const compile_step = b.step("compile", "Build the self-hosted compiler");
compile_step.dependOn(&exe.step);
Expand Down
13 changes: 8 additions & 5 deletions lib/init-exe/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ pub fn build(b: *std.Build) void {
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
exe.install();
b.installArtifact(exe);

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

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

// Creates a step for unit testing.
const exe_tests = b.addTest(.{
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const run_unit_tests = b.addRunArtifact(unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
test_step.dependOn(&run_unit_tests.step);
}
9 changes: 6 additions & 3 deletions lib/init-lib/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ pub fn build(b: *std.Build) void {
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
lib.install();
b.installArtifact(lib);

// Creates a step for unit testing.
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const run_main_tests = b.addRunArtifact(main_tests);

// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
test_step.dependOn(&run_main_tests.step);
}
20 changes: 7 additions & 13 deletions lib/std/Build/CompileStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ vcpkg_bin_path: ?[]const u8 = null,
/// This may be set in order to override the default install directory
override_dest_dir: ?InstallDir,
installed_path: ?[]const u8,
install_step: ?*InstallArtifactStep,

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

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

pub fn install(self: *CompileStep) void {
const b = self.step.owner;
b.installArtifact(self);
}

pub fn installHeader(cs: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void {
const b = cs.step.owner;
const install_file = b.addInstallHeaderFile(src_path, dest_rel_path);
Expand Down Expand Up @@ -533,7 +526,7 @@ pub fn installLibraryHeaders(cs: *CompileStep, l: *CompileStep) void {
const T = id.Type();
const ptr = b.allocator.create(T) catch @panic("OOM");
ptr.* = step.cast(T).?.*;
ptr.dest_builder = b;
ptr.step.owner = b;
break :blk &ptr.step;
},
else => unreachable,
Expand All @@ -557,12 +550,13 @@ pub fn addObjCopy(cs: *CompileStep, options: ObjCopyStep.Options) *ObjCopyStep {
return b.addObjCopy(cs.getOutputSource(), copy);
}

/// Deprecated: use `std.Build.addRunArtifact`
/// This function will run in the context of the package that created the executable,
/// This function would run in the context of the package that created the executable,
/// which is undesirable when running an executable provided by a dependency package.
pub fn run(cs: *CompileStep) *RunStep {
return cs.step.owner.addRunArtifact(cs);
}
pub const run = @compileError("deprecated; use std.Build.addRunArtifact");

/// This function would install in the context of the package that created the artifact,
/// which is undesirable when installing an artifact provided by a dependency package.
pub const install = @compileError("deprecated; use std.Build.installArtifact");

pub fn checkObject(self: *CompileStep) *CheckObjectStep {
return CheckObjectStep.create(self.step.owner, self.getOutputSource(), self.target_info.target.ofmt);
Expand Down
9 changes: 2 additions & 7 deletions lib/std/Build/InstallArtifactStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const fs = std.fs;
pub const base_id = .install_artifact;

step: Step,
dest_builder: *std.Build,
artifact: *CompileStep,
dest_dir: InstallDir,
pdb_dir: ?InstallDir,
Expand All @@ -18,8 +17,6 @@ h_dir: ?InstallDir,
dest_sub_path: ?[]const u8,

pub fn create(owner: *std.Build, artifact: *CompileStep) *InstallArtifactStep {
if (artifact.install_step) |s| return s;

const self = owner.allocator.create(InstallArtifactStep) catch @panic("OOM");
self.* = InstallArtifactStep{
.step = Step.init(.{
Expand All @@ -28,7 +25,6 @@ pub fn create(owner: *std.Build, artifact: *CompileStep) *InstallArtifactStep {
.owner = owner,
.makeFn = make,
}),
.dest_builder = owner,
.artifact = artifact,
.dest_dir = artifact.override_dest_dir orelse switch (artifact.kind) {
.obj => @panic("Cannot install a .obj build artifact."),
Expand All @@ -46,7 +42,6 @@ pub fn create(owner: *std.Build, artifact: *CompileStep) *InstallArtifactStep {
.dest_sub_path = null,
};
self.step.dependOn(&artifact.step);
artifact.install_step = self;

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

fn make(step: *Step, prog_node: *std.Progress.Node) !void {
_ = prog_node;
const src_builder = step.owner;
const self = @fieldParentPtr(InstallArtifactStep, "step", step);
const dest_builder = self.dest_builder;
const src_builder = self.artifact.step.owner;
const dest_builder = step.owner;

const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename;
const full_dest_path = dest_builder.getInstallPath(self.dest_dir, dest_sub_path);
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Build/RunStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -866,9 +866,9 @@ fn spawnChildAndCollect(
child.request_resource_usage_statistics = true;

child.stdin_behavior = switch (self.stdio) {
.infer_from_args => if (has_side_effects) .Inherit else .Close,
.infer_from_args => if (has_side_effects) .Inherit else .Ignore,
.inherit => .Inherit,
.check => .Close,
.check => .Ignore,
.zig_test => .Pipe,
};
child.stdout_behavior = switch (self.stdio) {
Expand Down
2 changes: 1 addition & 1 deletion test/link/bss/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void {
.optimize = .Debug,
});

const run = exe.run();
const run = b.addRunArtifact(exe);
run.expectStdOutEqual("0, 1, 0\n");

test_step.dependOn(&run.step);
Expand Down
2 changes: 1 addition & 1 deletion test/link/common_symbols/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
});
test_exe.linkLibrary(lib_a);

test_step.dependOn(&test_exe.run().step);
test_step.dependOn(&b.addRunArtifact(test_exe).step);
}
2 changes: 1 addition & 1 deletion test/link/common_symbols_alignment/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
});
test_exe.linkLibrary(lib_a);

test_step.dependOn(&test_exe.run().step);
test_step.dependOn(&b.addRunArtifact(test_exe).step);
}
2 changes: 1 addition & 1 deletion test/link/interdependent_static_c_libs/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
test_exe.linkLibrary(lib_b);
test_exe.addIncludePath(".");

test_step.dependOn(&test_exe.run().step);
test_step.dependOn(&b.addRunArtifact(test_exe).step);
}
2 changes: 1 addition & 1 deletion test/link/macho/bugs/13056/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
});
exe.addObjectFile(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/lib/libc++.tbd" }) catch unreachable);

const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
run_cmd.expectStdErrEqual("x: 5\n");

test_step.dependOn(&run_cmd.step);
Expand Down
2 changes: 1 addition & 1 deletion test/link/macho/dead_strip_dylibs/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize

test_step.dependOn(&check.step);

const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
test_step.dependOn(&run_cmd.step);
}

Expand Down
2 changes: 1 addition & 1 deletion test/link/macho/entry_in_archive/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
exe.linkLibrary(lib);
exe.linkLibC();

const run = exe.run();
const run = b.addRunArtifact(exe);
run.skip_foreign_checks = true;
run.expectExitCode(0);
test_step.dependOn(&run.step);
Expand Down
8 changes: 4 additions & 4 deletions test/link/macho/headerpad/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize

test_step.dependOn(&check.step);

const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

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

test_step.dependOn(&check.step);

const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

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

test_step.dependOn(&check.step);

const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

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

test_step.dependOn(&check.step);

const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/link/macho/needed_framework/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
check.checkNext("name {*}Cocoa");
test_step.dependOn(&check.step);

const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
test_step.dependOn(&run_cmd.step);
}
2 changes: 1 addition & 1 deletion test/link/macho/objcpp/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
// populate paths to the sysroot here.
exe.linkFramework("Foundation");

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

test_step.dependOn(&run_cmd.step);
Expand Down
2 changes: 1 addition & 1 deletion test/link/macho/tls/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
test_exe.linkLibrary(lib);
test_exe.linkLibC();

const run = test_exe.run();
const run = b.addRunArtifact(test_exe);
run.skip_foreign_checks = true;

test_step.dependOn(&run.step);
Expand Down
2 changes: 1 addition & 1 deletion test/link/macho/weak_framework/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
check.checkNext("name {*}Cocoa");
test_step.dependOn(&check.step);

const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
test_step.dependOn(&run_cmd.step);
}
2 changes: 1 addition & 1 deletion test/link/macho/weak_library/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
});
dylib.addCSourceFile("a.c", &.{});
dylib.linkLibC();
dylib.install();
b.installArtifact(dylib);

const exe = b.addExecutable(.{
.name = "test",
Expand Down
2 changes: 1 addition & 1 deletion test/link/wasm/producers/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
lib.use_llvm = false;
lib.use_lld = false;
lib.strip = false;
lib.install();
b.installArtifact(lib);

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

Expand Down
2 changes: 1 addition & 1 deletion test/link/wasm/segments/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
lib.use_llvm = false;
lib.use_lld = false;
lib.strip = false;
lib.install();
b.installArtifact(lib);

const check_lib = lib.checkObject();
check_lib.checkStart("Section data");
Expand Down
2 changes: 1 addition & 1 deletion test/link/wasm/stack_pointer/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
lib.use_lld = false;
lib.strip = false;
lib.stack_size = std.wasm.page_size * 2; // set an explicit stack size
lib.install();
b.installArtifact(lib);

const check_lib = lib.checkObject();

Expand Down
2 changes: 1 addition & 1 deletion test/link/wasm/type/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
lib.use_llvm = false;
lib.use_lld = false;
lib.strip = false;
lib.install();
b.installArtifact(lib);

const check_lib = lib.checkObject();
check_lib.checkStart("Section type");
Expand Down
6 changes: 3 additions & 3 deletions test/src/CompareOutput.zig
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
});
exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?);

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

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

const run = exe.run();
const run = b.addRunArtifact(exe);
run.setName(annotated_case_name);
run.addArgs(case.cli_args);
run.expectExitCode(126);
Expand Down
2 changes: 1 addition & 1 deletion test/src/run_translated_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub const RunTranslatedCContext = struct {
const exe = translate_c.addExecutable(.{});
exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name});
exe.linkLibC();
const run = exe.run();
const run = b.addRunArtifact(exe);
run.step.name = b.fmt("{s} run", .{annotated_case_name});
if (!case.allow_warnings) {
run.expectStdErrEqual("");
Expand Down
Loading

0 comments on commit 1728d92

Please sign in to comment.