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) {
126126};
127127
128128pub const Arg = union(enum) {
129 artifact: *Step.Compile,
129 artifact: PrefixedArtifact,
130130 lazy_path: PrefixedLazyPath,
131131 directory_source: PrefixedLazyPath,
132132 bytes: []u8,
......@@ -134,6 +134,11 @@ pub const Arg = union(enum) {
134134 output_directory: *Output,
135135};
136136
137pub const PrefixedArtifact = struct {
138 prefix: []const u8,
139 artifact: *Step.Compile,
140};
141
137142pub const PrefixedLazyPath = struct {
138143 prefix: []const u8,
139144 lazy_path: std.Build.LazyPath,
......@@ -185,10 +190,20 @@ pub fn enableTestRunnerMode(run: *Run) void {
185190}
186191
187192pub 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 {
188197 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
189205 const bin_file = artifact.getEmittedBin();
190206 bin_file.addStepDependencies(&run.step);
191 run.argv.append(b.allocator, Arg{ .artifact = artifact }) catch @panic("OOM");
192207}
193208
194209/// 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 {
610625 man.hash.addBytes(file.prefix);
611626 man.hash.addBytes(file_path);
612627 },
613 .artifact => |artifact| {
628 .artifact => |pa| {
629 const artifact = pa.artifact;
630
614631 if (artifact.rootModuleTarget().os.tag == .windows) {
615632 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
616633 run.addPathForDynLibs(artifact);
617634 }
618635 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
622639 _ = try man.addFile(file_path, null);
623640 },
......@@ -912,7 +929,7 @@ fn runCommand(
912929 // work even for the edge case that the binary was produced by a
913930 // third party.
914931 const exe = switch (run.argv.items[0]) {
915 .artifact => |exe| exe,
932 .artifact => |exe| exe.artifact,
916933 else => break :interpret,
917934 };
918935 switch (exe.kind) {