authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2021-10-18 11:05:47-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-03 12:56:49-08:00
log7659229edcc9b0455010188fafdce26ac26de9b2
tree4aa38b58957acdf747dd2289f4a867f5dc5b5e8e
parent1cac99c90a34bc2cab96d75017a337ddd3479b50

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.

3 files changed, 16 insertions(+), 23 deletions(-)

lib/std/build.zig+6-18
......@@ -1018,12 +1018,8 @@ pub const Builder = struct {
10181018 }
10191019
10201020 /// Output format (BIN vs Intel HEX) determined by filename
1021 pub fn installRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) void {
1022 self.getInstallStep().dependOn(&self.addInstallRaw(artifact, dest_filename).step);
1023 }
1024
1025 pub fn installRawWithFormat(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) void {
1026 self.getInstallStep().dependOn(&self.addInstallRawWithFormat(artifact, dest_filename, format).step);
1021 pub fn installRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) void {
1022 self.getInstallStep().dependOn(&self.addInstallRaw(artifact, dest_filename, options).step);
10271023 }
10281024
10291025 ///`dest_rel_path` is relative to install prefix path
......@@ -1041,12 +1037,8 @@ pub const Builder = struct {
10411037 return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);
10421038 }
10431039
1044 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *InstallRawStep {
1045 return InstallRawStep.create(self, artifact, dest_filename, null);
1046 }
1047
1048 pub fn addInstallRawWithFormat(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) *InstallRawStep {
1049 return InstallRawStep.create(self, artifact, dest_filename, format);
1040 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) *InstallRawStep {
1041 return InstallRawStep.create(self, artifact, dest_filename, options);
10501042 }
10511043
10521044 pub fn addInstallFileWithDir(
......@@ -1740,12 +1732,8 @@ pub const LibExeObjStep = struct {
17401732 self.builder.installArtifact(self);
17411733 }
17421734
1743 pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8) void {
1744 self.builder.installRaw(self, dest_filename);
1745 }
1746
1747 pub fn installRawWithFormat(self: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) void {
1748 self.builder.installRawWithFormat(self, dest_filename, format);
1735 pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) void {
1736 self.builder.installRaw(self, dest_filename, options);
17491737 }
17501738
17511739 /// Creates a `RunStep` with an executable built with `addExecutable`.
lib/std/build/InstallRawStep.zig+8-3
......@@ -355,20 +355,25 @@ fn detectFormat(filename: []const u8) RawFormat {
355355 return .bin;
356356}
357357
358pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, format: ?RawFormat) *InstallRawStep {
358pub const CreateOptions = struct {
359 format: ?RawFormat = null,
360 dest_dir: ?InstallDir = null,
361};
362
363pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: CreateOptions) *InstallRawStep {
359364 const self = builder.allocator.create(InstallRawStep) catch unreachable;
360365 self.* = InstallRawStep{
361366 .step = Step.init(.install_raw, builder.fmt("install raw binary {s}", .{artifact.step.name}), builder.allocator, make),
362367 .builder = builder,
363368 .artifact = artifact,
364 .dest_dir = switch (artifact.kind) {
369 .dest_dir = if (options.dest_dir) |d| d else switch (artifact.kind) {
365370 .obj => unreachable,
366371 .@"test" => unreachable,
367372 .exe => .bin,
368373 .lib => unreachable,
369374 },
370375 .dest_filename = dest_filename,
371 .format = format orelse detectFormat(dest_filename),
376 .format = if (options.format) |f| f else detectFormat(dest_filename),
372377 .output_file = std.build.GeneratedFile{ .step = &self.step },
373378 };
374379 self.step.dependOn(&artifact.step);
test/standalone/install_raw_hex/build.zig+2-2
......@@ -20,10 +20,10 @@ pub fn build(b: *Builder) void {
2020 const test_step = b.step("test", "Test the program");
2121 b.default_step.dependOn(test_step);
2222
23 const hex_step = b.addInstallRaw(elf, "hello.hex");
23 const hex_step = b.addInstallRaw(elf, "hello.hex", .{});
2424 test_step.dependOn(&hex_step.step);
2525
26 const explicit_format_hex_step = b.addInstallRawWithFormat(elf, "hello.foo", .hex);
26 const explicit_format_hex_step = b.addInstallRaw(elf, "hello.foo", .{ .format = .hex });
2727 test_step.dependOn(&explicit_format_hex_step.step);
2828
2929 const expected_hex = &[_][]const u8{