authorgravatar for xtex@astrafall.orgxtex <xtex@astrafall.org> 2024-07-12 17:06:06+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-12 11:06:06+02:00
log0d79aa01768d600a19f7a7493afce417da7e3810
tree034e8614c4fb58fb3e4d7b5f3d246f4505912873
parent3bf0d2e5168c1722656a5eaa56e4bf16272df6d5
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.Build.Step.Run: support prefixed artifact args

Just like how addPrefixedFileArg and addPrefixedDirectoryArg works, we can make it for artifacts. Signed-off-by: Bingwu Zhang <xtexchooser@duck.com>

1 files changed, 22 insertions(+), 5 deletions(-)

lib/std/Build/Step/Run.zig+22-5
...@@ -126,7 +126,7 @@ pub const StdIo = union(enum) {...@@ -126,7 +126,7 @@ pub const StdIo = union(enum) {
126};126};
127127
128pub const Arg = union(enum) {128pub const Arg = union(enum) {
129 artifact: *Step.Compile,129 artifact: PrefixedArtifact,
130 lazy_path: PrefixedLazyPath,130 lazy_path: PrefixedLazyPath,
131 directory_source: PrefixedLazyPath,131 directory_source: PrefixedLazyPath,
132 bytes: []u8,132 bytes: []u8,
...@@ -134,6 +134,11 @@ pub const Arg = union(enum) {...@@ -134,6 +134,11 @@ pub const Arg = union(enum) {
134 output_directory: *Output,134 output_directory: *Output,
135};135};
136136
137pub const PrefixedArtifact = struct {
138 prefix: []const u8,
139 artifact: *Step.Compile,
140};
141
137pub const PrefixedLazyPath = struct {142pub const PrefixedLazyPath = struct {
138 prefix: []const u8,143 prefix: []const u8,
139 lazy_path: std.Build.LazyPath,144 lazy_path: std.Build.LazyPath,
...@@ -185,10 +190,20 @@ pub fn enableTestRunnerMode(run: *Run) void {...@@ -185,10 +190,20 @@ pub fn enableTestRunnerMode(run: *Run) void {
185}190}
186191
187pub fn addArtifactArg(run: *Run, artifact: *Step.Compile) void {192pub fn addArtifactArg(run: *Run, artifact: *Step.Compile) void {
193 run.addPrefixedArtifactArg("", artifact);
194}
195
196pub fn addPrefixedArtifactArg(run: *Run, prefix: []const u8, artifact: *Step.Compile) void {
188 const b = run.step.owner;197 const b = run.step.owner;
198
199 const prefixed_artifact: PrefixedArtifact = .{
200 .prefix = b.dupe(prefix),
201 .artifact = artifact,
202 };
203 run.argv.append(b.allocator, .{ .artifact = prefixed_artifact }) catch @panic("OOM");
204
189 const bin_file = artifact.getEmittedBin();205 const bin_file = artifact.getEmittedBin();
190 bin_file.addStepDependencies(&run.step);206 bin_file.addStepDependencies(&run.step);
191 run.argv.append(b.allocator, Arg{ .artifact = artifact }) catch @panic("OOM");
192}207}
193208
194/// Provides a file path as a command line argument to the command being run.209/// Provides a file path as a command line argument to the command being run.
...@@ -610,14 +625,16 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {...@@ -610,14 +625,16 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
610 man.hash.addBytes(file.prefix);625 man.hash.addBytes(file.prefix);
611 man.hash.addBytes(file_path);626 man.hash.addBytes(file_path);
612 },627 },
613 .artifact => |artifact| {628 .artifact => |pa| {
629 const artifact = pa.artifact;
630
614 if (artifact.rootModuleTarget().os.tag == .windows) {631 if (artifact.rootModuleTarget().os.tag == .windows) {
615 // On Windows we don't have rpaths so we have to add .dll search paths to PATH632 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
616 run.addPathForDynLibs(artifact);633 run.addPathForDynLibs(artifact);
617 }634 }
618 const file_path = artifact.installed_path orelse artifact.generated_bin.?.path.?; // the path is guaranteed to be set635 const file_path = artifact.installed_path orelse artifact.generated_bin.?.path.?; // the path is guaranteed to be set
619636
620 try argv_list.append(file_path);637 try argv_list.append(b.fmt("{s}{s}", .{ pa.prefix, file_path }));
621638
622 _ = try man.addFile(file_path, null);639 _ = try man.addFile(file_path, null);
623 },640 },
...@@ -912,7 +929,7 @@ fn runCommand(...@@ -912,7 +929,7 @@ fn runCommand(
912 // work even for the edge case that the binary was produced by a929 // work even for the edge case that the binary was produced by a
913 // third party.930 // third party.
914 const exe = switch (run.argv.items[0]) {931 const exe = switch (run.argv.items[0]) {
915 .artifact => |exe| exe,932 .artifact => |exe| exe.artifact,
916 else => break :interpret,933 else => break :interpret,
917 };934 };
918 switch (exe.kind) {935 switch (exe.kind) {