From 7659229edcc9b0455010188fafdce26ac26de9b2 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Mon, 18 Oct 2021 11:05:47 -0600 Subject: [PATCH] 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. --- lib/std/build.zig | 24 ++++++----------------- lib/std/build/InstallRawStep.zig | 11 ++++++++--- test/standalone/install_raw_hex/build.zig | 4 ++-- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index c8ff1e4c87..30296081b7 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -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`. diff --git a/lib/std/build/InstallRawStep.zig b/lib/std/build/InstallRawStep.zig index 0f921d6622..17a23931e2 100644 --- a/lib/std/build/InstallRawStep.zig +++ b/lib/std/build/InstallRawStep.zig @@ -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); diff --git a/test/standalone/install_raw_hex/build.zig b/test/standalone/install_raw_hex/build.zig index 789197b627..9a0cba7ae8 100644 --- a/test/standalone/install_raw_hex/build.zig +++ b/test/standalone/install_raw_hex/build.zig @@ -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{