std.build.InstallRawStep: allow custom dest_dir

I'm working on a build.zig file where I'm leveraging InstallRawStep but I'd like to change the install dir.  This allows the install dir to be changd and also enhances InstallRawStep to add more options in the future by putting them into a struct with default values.  This also removes the need for an extra addInstallStepWithFormat function in build.zig.
This commit is contained in:
Jonathan Marler 2021-10-18 11:05:47 -06:00 committed by Andrew Kelley
parent 1cac99c90a
commit 7659229edc
3 changed files with 16 additions and 23 deletions

View File

@ -1018,12 +1018,8 @@ pub const Builder = struct {
}
/// Output format (BIN vs Intel HEX) determined by filename
pub fn installRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) void {
self.getInstallStep().dependOn(&self.addInstallRaw(artifact, dest_filename).step);
}
pub fn installRawWithFormat(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) void {
self.getInstallStep().dependOn(&self.addInstallRawWithFormat(artifact, dest_filename, format).step);
pub fn installRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) void {
self.getInstallStep().dependOn(&self.addInstallRaw(artifact, dest_filename, options).step);
}
///`dest_rel_path` is relative to install prefix path
@ -1041,12 +1037,8 @@ pub const Builder = struct {
return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);
}
pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *InstallRawStep {
return InstallRawStep.create(self, artifact, dest_filename, null);
}
pub fn addInstallRawWithFormat(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) *InstallRawStep {
return InstallRawStep.create(self, artifact, dest_filename, format);
pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) *InstallRawStep {
return InstallRawStep.create(self, artifact, dest_filename, options);
}
pub fn addInstallFileWithDir(
@ -1740,12 +1732,8 @@ pub const LibExeObjStep = struct {
self.builder.installArtifact(self);
}
pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8) void {
self.builder.installRaw(self, dest_filename);
}
pub fn installRawWithFormat(self: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) void {
self.builder.installRawWithFormat(self, dest_filename, format);
pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) void {
self.builder.installRaw(self, dest_filename, options);
}
/// Creates a `RunStep` with an executable built with `addExecutable`.

View File

@ -355,20 +355,25 @@ fn detectFormat(filename: []const u8) RawFormat {
return .bin;
}
pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: ?RawFormat) *InstallRawStep {
pub const CreateOptions = struct {
format: ?RawFormat = null,
dest_dir: ?InstallDir = null,
};
pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: CreateOptions) *InstallRawStep {
const self = builder.allocator.create(InstallRawStep) catch unreachable;
self.* = InstallRawStep{
.step = Step.init(.install_raw, builder.fmt("install raw binary {s}", .{artifact.step.name}), builder.allocator, make),
.builder = builder,
.artifact = artifact,
.dest_dir = switch (artifact.kind) {
.dest_dir = if (options.dest_dir) |d| d else switch (artifact.kind) {
.obj => unreachable,
.@"test" => unreachable,
.exe => .bin,
.lib => unreachable,
},
.dest_filename = dest_filename,
.format = format orelse detectFormat(dest_filename),
.format = if (options.format) |f| f else detectFormat(dest_filename),
.output_file = std.build.GeneratedFile{ .step = &self.step },
};
self.step.dependOn(&artifact.step);

View File

@ -20,10 +20,10 @@ pub fn build(b: *Builder) void {
const test_step = b.step("test", "Test the program");
b.default_step.dependOn(test_step);
const hex_step = b.addInstallRaw(elf, "hello.hex");
const hex_step = b.addInstallRaw(elf, "hello.hex", .{});
test_step.dependOn(&hex_step.step);
const explicit_format_hex_step = b.addInstallRawWithFormat(elf, "hello.foo", .hex);
const explicit_format_hex_step = b.addInstallRaw(elf, "hello.foo", .{ .format = .hex });
test_step.dependOn(&explicit_format_hex_step.step);
const expected_hex = &[_][]const u8{