authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-05 16:44:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-13 06:42:25-07:00
logdad6039092d02460d5e993855b129da9949e9c5c
tree69eeb85b13e7dae4d96be0e28ed1e38c3c0ccfcf
parent27317eaff08453792ae2d3bd0c96f81be8a26bfc

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.

2 files changed, 27 insertions(+), 20 deletions(-)

lib/std/Build.zig+23-1
......@@ -348,7 +348,7 @@ fn applyArgs(b: *Build, args: anytype) !void {
348348 .used = false,
349349 });
350350 },
351 .Enum => {
351 .Enum, .EnumLiteral => {
352352 try b.user_input_options.put(field.name, .{
353353 .name = field.name,
354354 .value = .{ .scalar = @tagName(v) },
......@@ -599,6 +599,28 @@ pub fn addSystemCommand(self: *Build, argv: []const []const u8) *RunStep {
599599 return run_step;
600600}
601601
602/// Creates a `RunStep` with an executable built with `addExecutable`.
603/// Add command line arguments with methods of `RunStep`.
604pub fn addRunArtifact(b: *Build, exe: *CompileStep) *RunStep {
605 assert(exe.kind == .exe or exe.kind == .test_exe);
606
607 // It doesn't have to be native. We catch that if you actually try to run it.
608 // Consider that this is declarative; the run step may not be run unless a user
609 // option is supplied.
610 const run_step = RunStep.create(b, b.fmt("run {s}", .{exe.step.name}));
611 run_step.addArtifactArg(exe);
612
613 if (exe.kind == .test_exe) {
614 run_step.addArg(b.zig_exe);
615 }
616
617 if (exe.vcpkg_bin_path) |path| {
618 run_step.addPathDir(path);
619 }
620
621 return run_step;
622}
623
602624/// Using the `values` provided, produces a C header file, possibly based on a
603625/// template input file (e.g. config.h.in).
604626/// When an input template file is provided, this function will fail the build
lib/std/Build/CompileStep.zig+4-19
......@@ -506,26 +506,11 @@ pub fn installLibraryHeaders(a: *CompileStep, l: *CompileStep) void {
506506 a.installed_headers.appendSlice(l.installed_headers.items) catch @panic("OOM");
507507}
508508
509/// Creates a `RunStep` with an executable built with `addExecutable`.
510/// Add command line arguments with `addArg`.
509/// Deprecated: use `std.Build.addRunArtifact`
510/// This function will run in the context of the package that created the executable,
511/// which is undesirable when running an executable provided by a dependency package.
511512pub fn run(exe: *CompileStep) *RunStep {
512 assert(exe.kind == .exe or exe.kind == .test_exe);
513
514 // It doesn't have to be native. We catch that if you actually try to run it.
515 // Consider that this is declarative; the run step may not be run unless a user
516 // option is supplied.
517 const run_step = RunStep.create(exe.builder, exe.builder.fmt("run {s}", .{exe.step.name}));
518 run_step.addArtifactArg(exe);
519
520 if (exe.kind == .test_exe) {
521 run_step.addArg(exe.builder.zig_exe);
522 }
523
524 if (exe.vcpkg_bin_path) |path| {
525 run_step.addPathDir(path);
526 }
527
528 return run_step;
513 return exe.builder.addRunArtifact(exe);
529514}
530515
531516/// Creates an `EmulatableRunStep` with an executable built with `addExecutable`.