Merge pull request #15245 from ziglang/zig-build-install-artifact

fix broken and confusing artifact installation logic
This commit is contained in:
Andrew Kelley 2023-04-11 11:40:53 -04:00 committed by GitHub
commit 1728d92f60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
46 changed files with 76 additions and 89 deletions

View File

@ -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);

View File

@ -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.
@ -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);
}

View File

@ -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);
}

View File

@ -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,
@ -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 },
@ -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);
@ -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,
@ -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);

View File

@ -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,
@ -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(.{
@ -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."),
@ -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()) {
@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}
@ -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);
}
@ -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);
}
@ -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);
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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",

View File

@ -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;

View File

@ -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");

View File

@ -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();

View File

@ -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");

View File

@ -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);
@ -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);
@ -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);

View File

@ -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("");

View File

@ -24,7 +24,6 @@ pub fn build(b: *std.Build) void {
.dependencies = &.{.{ .name = "shared", .module = shared }},
});
const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

View File

@ -22,7 +22,6 @@ pub fn build(b: *std.Build) void {
});
exe.addModule("foo", foo);
const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

View File

@ -18,7 +18,6 @@ pub fn build(b: *std.Build) void {
});
exe.addModule("foo", foo);
const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

View File

@ -15,7 +15,6 @@ pub fn build(b: *std.Build) void {
.source_file = .{ .path = "foo.zig" },
});
const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

View File

@ -21,7 +21,6 @@ pub fn build(b: *std.Build) void {
});
exe.addModule("shared", shared);
const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

View File

@ -11,5 +11,5 @@ pub fn build(b: *std.Build) void {
main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") };
main.emit_bin = .{ .emit_to = b.pathFromRoot("main") };
test_step.dependOn(&main.run().step);
test_step.dependOn(&b.addRunArtifact(main).step);
}

View File

@ -28,5 +28,5 @@ pub fn build(b: *std.Build) void {
main.linkLibrary(obj1);
main.linkLibrary(obj2);
test_step.dependOn(&main.run().step);
test_step.dependOn(&b.addRunArtifact(main).step);
}

View File

@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
exe.install();
b.installArtifact(exe);
const c_sources = [_][]const u8{
"test.c",

View File

@ -17,7 +17,7 @@ pub fn build(b: *std.Build) void {
test2.setTestRunner("src/main.zig");
test3.setTestRunner("src/main.zig");
test_step.dependOn(&test1.run().step);
test_step.dependOn(&test2.run().step);
test_step.dependOn(&test3.run().step);
test_step.dependOn(&b.addRunArtifact(test1).step);
test_step.dependOn(&b.addRunArtifact(test2).step);
test_step.dependOn(&b.addRunArtifact(test3).step);
}

View File

@ -21,7 +21,7 @@ pub fn build(b: *std.Build) !void {
});
kernel.addObjectFile("./boot.S");
kernel.setLinkerScriptPath(.{ .path = "./linker.ld" });
kernel.install();
b.installArtifact(kernel);
test_step.dependOn(&kernel.step);
}

View File

@ -9,5 +9,5 @@ pub fn build(b: *std.Build) void {
});
test_exe.setMainPkgPath(".");
test_step.dependOn(&test_exe.run().step);
test_step.dependOn(&b.addRunArtifact(test_exe).step);
}

View File

@ -25,7 +25,6 @@ pub fn build(b: *std.Build) void {
b.default_step.dependOn(&exe.step);
const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
test_step.dependOn(&run_cmd.step);
}

View File

@ -17,5 +17,5 @@ pub fn build(b: *std.Build) void {
options.addOption([]const u8, "string", b.option([]const u8, "string", "s").?);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&main.run().step);
test_step.dependOn(&b.addRunArtifact(main).step);
}

View File

@ -17,7 +17,7 @@ pub fn build(b: *std.Build) void {
});
main.pie = true;
const run = main.run();
const run = b.addRunArtifact(main);
run.skip_foreign_checks = true;
test_step.dependOn(&run.step);

View File

@ -13,7 +13,6 @@ pub fn build(b: *std.Build) void {
});
exe.addAnonymousModule("my_pkg", .{ .source_file = .{ .path = "pkg.zig" } });
const run = exe.run();
const run = b.addRunArtifact(exe);
test_step.dependOn(&run.step);
}

View File

@ -23,7 +23,6 @@ pub fn build(b: *std.Build) void {
exe.linkLibrary(lib);
exe.linkSystemLibrary("c");
const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
test_step.dependOn(&run_cmd.step);
}

View File

@ -21,5 +21,5 @@ pub fn build(b: *std.Build) void {
test_exe.linkLibrary(foo);
test_exe.addIncludePath(".");
test_step.dependOn(&test_exe.run().step);
test_step.dependOn(&b.addRunArtifact(test_exe).step);
}

View File

@ -15,6 +15,6 @@ pub fn build(b: *std.Build) void {
t.addModule("module2", module2);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&t.run().step);
test_step.dependOn(&b.addRunArtifact(t).step);
b.default_step = test_step;
}

View File

@ -11,7 +11,6 @@ pub fn build(b: *std.Build) void {
});
test_exe.test_runner = "test_runner.zig";
const test_run = test_exe.run();
const test_run = b.addRunArtifact(test_exe);
test_step.dependOn(&test_run.step);
}

View File

@ -12,5 +12,5 @@ pub fn build(b: *std.Build) void {
});
main.addIncludePath(".");
test_step.dependOn(&main.run().step);
test_step.dependOn(&b.addRunArtifact(main).step);
}

View File

@ -596,7 +596,8 @@ pub fn addStandaloneTests(
});
if (case.link_libc) exe.linkLibC();
step.dependOn(&exe.run().step);
const run = b.addRunArtifact(exe);
step.dependOn(&run.step);
}
}
}
@ -1004,7 +1005,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
},
};
const run = these_tests.run();
const run = b.addRunArtifact(these_tests);
run.skip_foreign_checks = true;
run.setName(b.fmt("run test {s}-{s}-{s}-{s}-{s}-{s}", .{
options.name,
@ -1061,7 +1062,7 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S
triple_prefix, @tagName(optimize_mode),
}));
const run = test_step.run();
const run = b.addRunArtifact(test_step);
run.skip_foreign_checks = true;
step.dependOn(&run.step);
}