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 {...@@ -1018,12 +1018,8 @@ pub const Builder = struct {
1018 }1018 }
10191019
1020 /// Output format (BIN vs Intel HEX) determined by filename1020 /// Output format (BIN vs Intel HEX) determined by filename
1021 pub fn installRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) void {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).step);1022 self.getInstallStep().dependOn(&self.addInstallRaw(artifact, dest_filename, options).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);
1027 }1023 }
10281024
1029 ///`dest_rel_path` is relative to install prefix path1025 ///`dest_rel_path` is relative to install prefix path
...@@ -1041,12 +1037,8 @@ pub const Builder = struct {...@@ -1041,12 +1037,8 @@ pub const Builder = struct {
1041 return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);1037 return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);
1042 }1038 }
10431039
1044 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *InstallRawStep {1040 pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) *InstallRawStep {
1045 return InstallRawStep.create(self, artifact, dest_filename, null);1041 return InstallRawStep.create(self, artifact, dest_filename, options);
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);
1050 }1042 }
10511043
1052 pub fn addInstallFileWithDir(1044 pub fn addInstallFileWithDir(
...@@ -1740,12 +1732,8 @@ pub const LibExeObjStep = struct {...@@ -1740,12 +1732,8 @@ pub const LibExeObjStep = struct {
1740 self.builder.installArtifact(self);1732 self.builder.installArtifact(self);
1741 }1733 }
17421734
1743 pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8) void {1735 pub fn installRaw(self: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) void {
1744 self.builder.installRaw(self, dest_filename);1736 self.builder.installRaw(self, dest_filename, options);
1745 }
1746
1747 pub fn installRawWithFormat(self: *LibExeObjStep, dest_filename: []const u8, format: InstallRawStep.RawFormat) void {
1748 self.builder.installRawWithFormat(self, dest_filename, format);
1749 }1737 }
17501738
1751 /// Creates a `RunStep` with an executable built with `addExecutable`.1739 /// 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 {...@@ -355,20 +355,25 @@ fn detectFormat(filename: []const u8) RawFormat {
355 return .bin;355 return .bin;
356}356}
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 {
359 const self = builder.allocator.create(InstallRawStep) catch unreachable;364 const self = builder.allocator.create(InstallRawStep) catch unreachable;
360 self.* = InstallRawStep{365 self.* = InstallRawStep{
361 .step = Step.init(.install_raw, builder.fmt("install raw binary {s}", .{artifact.step.name}), builder.allocator, make),366 .step = Step.init(.install_raw, builder.fmt("install raw binary {s}", .{artifact.step.name}), builder.allocator, make),
362 .builder = builder,367 .builder = builder,
363 .artifact = artifact,368 .artifact = artifact,
364 .dest_dir = switch (artifact.kind) {369 .dest_dir = if (options.dest_dir) |d| d else switch (artifact.kind) {
365 .obj => unreachable,370 .obj => unreachable,
366 .@"test" => unreachable,371 .@"test" => unreachable,
367 .exe => .bin,372 .exe => .bin,
368 .lib => unreachable,373 .lib => unreachable,
369 },374 },
370 .dest_filename = dest_filename,375 .dest_filename = dest_filename,
371 .format = format orelse detectFormat(dest_filename),376 .format = if (options.format) |f| f else detectFormat(dest_filename),
372 .output_file = std.build.GeneratedFile{ .step = &self.step },377 .output_file = std.build.GeneratedFile{ .step = &self.step },
373 };378 };
374 self.step.dependOn(&artifact.step);379 self.step.dependOn(&artifact.step);
test/standalone/install_raw_hex/build.zig+2-2
...@@ -20,10 +20,10 @@ pub fn build(b: *Builder) void {...@@ -20,10 +20,10 @@ pub fn build(b: *Builder) void {
20 const test_step = b.step("test", "Test the program");20 const test_step = b.step("test", "Test the program");
21 b.default_step.dependOn(test_step);21 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", .{});
24 test_step.dependOn(&hex_step.step);24 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 });
27 test_step.dependOn(&explicit_format_hex_step.step);27 test_step.dependOn(&explicit_format_hex_step.step);
2828
29 const expected_hex = &[_][]const u8{29 const expected_hex = &[_][]const u8{