Commit dad6039092

Andrew Kelley <andrew@ziglang.org>
2023-02-06 00:44:03
std.Build: support running build artifacts from packages
Deprecate CompileStep.run. The problem with this function is that it does the RunStep with the same build.zig context as the CompileStep, but this is not desirable when running an executable that is provided by a dependency package. Instead, users should use `b.addRunArtifact`. This has the additional benefit of conforming to the existing naming conventions. Additionally, support enum literals in config header options values.
1 parent 27317ea
Changed files (2)
lib/std/Build/CompileStep.zig
@@ -506,26 +506,11 @@ pub fn installLibraryHeaders(a: *CompileStep, l: *CompileStep) void {
     a.installed_headers.appendSlice(l.installed_headers.items) catch @panic("OOM");
 }
 
-/// Creates a `RunStep` with an executable built with `addExecutable`.
-/// Add command line arguments with `addArg`.
+/// Deprecated: use `std.Build.addRunArtifact`
+/// This function will run in the context of the package that created the executable,
+/// which is undesirable when running an executable provided by a dependency package.
 pub fn run(exe: *CompileStep) *RunStep {
-    assert(exe.kind == .exe or exe.kind == .test_exe);
-
-    // It doesn't have to be native. We catch that if you actually try to run it.
-    // Consider that this is declarative; the run step may not be run unless a user
-    // option is supplied.
-    const run_step = RunStep.create(exe.builder, exe.builder.fmt("run {s}", .{exe.step.name}));
-    run_step.addArtifactArg(exe);
-
-    if (exe.kind == .test_exe) {
-        run_step.addArg(exe.builder.zig_exe);
-    }
-
-    if (exe.vcpkg_bin_path) |path| {
-        run_step.addPathDir(path);
-    }
-
-    return run_step;
+    return exe.builder.addRunArtifact(exe);
 }
 
 /// Creates an `EmulatableRunStep` with an executable built with `addExecutable`.
lib/std/Build.zig
@@ -348,7 +348,7 @@ fn applyArgs(b: *Build, args: anytype) !void {
                         .used = false,
                     });
                 },
-                .Enum => {
+                .Enum, .EnumLiteral => {
                     try b.user_input_options.put(field.name, .{
                         .name = field.name,
                         .value = .{ .scalar = @tagName(v) },
@@ -599,6 +599,28 @@ pub fn addSystemCommand(self: *Build, argv: []const []const u8) *RunStep {
     return run_step;
 }
 
+/// Creates a `RunStep` with an executable built with `addExecutable`.
+/// Add command line arguments with methods of `RunStep`.
+pub fn addRunArtifact(b: *Build, exe: *CompileStep) *RunStep {
+    assert(exe.kind == .exe or exe.kind == .test_exe);
+
+    // It doesn't have to be native. We catch that if you actually try to run it.
+    // Consider that this is declarative; the run step may not be run unless a user
+    // option is supplied.
+    const run_step = RunStep.create(b, b.fmt("run {s}", .{exe.step.name}));
+    run_step.addArtifactArg(exe);
+
+    if (exe.kind == .test_exe) {
+        run_step.addArg(b.zig_exe);
+    }
+
+    if (exe.vcpkg_bin_path) |path| {
+        run_step.addPathDir(path);
+    }
+
+    return run_step;
+}
+
 /// Using the `values` provided, produces a C header file, possibly based on a
 /// template input file (e.g. config.h.in).
 /// When an input template file is provided, this function will fail the build