Skip to content

Commit

Permalink
child_process + Build: rename exec to run + all related code
Browse files Browse the repository at this point in the history
Justification: exec, execv etc are unix concepts and portable version
should be called differently.

Do no touch non-Zig code. Adjust error names as well, if associated.
Closes #5853.
  • Loading branch information
Jan Philipp Hafer authored and andrewrk committed Oct 22, 2023
1 parent d8c0679 commit fd2239b
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 59 deletions.
6 changes: 3 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pub fn build(b: *std.Build) !void {
const version_string = b.fmt("{d}.{d}.{d}", .{ zig_version.major, zig_version.minor, zig_version.patch });

var code: u8 = undefined;
const git_describe_untrimmed = b.execAllowFail(&[_][]const u8{
const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{
"git", "-C", b.build_root.path orelse ".", "describe", "--match", "*.*.*", "--tags",
}, &code, .Ignore) catch {
break :v version_string;
Expand Down Expand Up @@ -738,9 +738,9 @@ fn addCxxKnownPath(
return error.RequiredLibraryNotFound;

const path_padded = if (ctx.cxx_compiler_arg1.len > 0)
b.exec(&.{ ctx.cxx_compiler, ctx.cxx_compiler_arg1, b.fmt("-print-file-name={s}", .{objname}) })
b.run(&.{ ctx.cxx_compiler, ctx.cxx_compiler_arg1, b.fmt("-print-file-name={s}", .{objname}) })
else
b.exec(&.{ ctx.cxx_compiler, b.fmt("-print-file-name={s}", .{objname}) });
b.run(&.{ ctx.cxx_compiler, b.fmt("-print-file-name={s}", .{objname}) });
var tokenizer = mem.tokenizeAny(u8, path_padded, "\r\n");
const path_unpadded = tokenizer.next().?;
if (mem.eql(u8, path_unpadded, objname)) {
Expand Down
10 changes: 5 additions & 5 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const InitializedDepContext = struct {
}
};

pub const ExecError = error{
pub const RunError = error{
ReadFailure,
ExitCodeFailure,
ProcessTerminated,
Expand Down Expand Up @@ -1627,12 +1627,12 @@ pub fn findProgram(self: *Build, names: []const []const u8, paths: []const []con
return error.FileNotFound;
}

pub fn execAllowFail(
pub fn runAllowFail(
self: *Build,
argv: []const []const u8,
out_code: *u8,
stderr_behavior: std.ChildProcess.StdIo,
) ExecError![]u8 {
) RunError![]u8 {
assert(argv.len != 0);

if (!process.can_spawn)
Expand Down Expand Up @@ -1671,7 +1671,7 @@ pub fn execAllowFail(
/// This is a helper function to be called from build.zig scripts, *not* from
/// inside step make() functions. If any errors occur, it fails the build with
/// a helpful message.
pub fn exec(b: *Build, argv: []const []const u8) []u8 {
pub fn run(b: *Build, argv: []const []const u8) []u8 {
if (!process.can_spawn) {
std.debug.print("unable to spawn the following command: cannot spawn child process\n{s}\n", .{
try allocPrintCmd(b.allocator, null, argv),
Expand All @@ -1680,7 +1680,7 @@ pub fn exec(b: *Build, argv: []const []const u8) []u8 {
}

var code: u8 = undefined;
return b.execAllowFail(argv, &code, .Inherit) catch |err| {
return b.runAllowFail(argv, &code, .Inherit) catch |err| {
const printed_cmd = allocPrintCmd(b.allocator, null, argv) catch @panic("OOM");
std.debug.print("unable to spawn the following command: {s}\n{s}\n", .{
@errorName(err), printed_cmd,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step.zig
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub fn evalChildProcess(s: *Step, argv: []const []const u8) !void {
try handleChildProcUnsupported(s, null, argv);
try handleVerbose(s.owner, null, argv);

const result = std.ChildProcess.exec(.{
const result = std.ChildProcess.run(.{
.allocator = arena,
.argv = argv,
}) catch |err| return s.fail("unable to spawn {s}: {s}", .{ argv[0], @errorName(err) });
Expand Down
8 changes: 4 additions & 4 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const NativeTargetInfo = std.zig.system.NativeTargetInfo;
const LazyPath = std.Build.LazyPath;
const PkgConfigPkg = std.Build.PkgConfigPkg;
const PkgConfigError = std.Build.PkgConfigError;
const ExecError = std.Build.ExecError;
const RunError = std.Build.RunError;
const Module = std.Build.Module;
const VcpkgRoot = std.Build.VcpkgRoot;
const InstallDir = std.Build.InstallDir;
Expand Down Expand Up @@ -854,7 +854,7 @@ fn runPkgConfig(self: *Compile, lib_name: []const u8) ![]const []const u8 {
};

var code: u8 = undefined;
const stdout = if (b.execAllowFail(&[_][]const u8{
const stdout = if (b.runAllowFail(&[_][]const u8{
"pkg-config",
pkg_name,
"--cflags",
Expand Down Expand Up @@ -2263,8 +2263,8 @@ pub fn doAtomicSymLinks(
};
}

fn execPkgConfigList(self: *std.Build, out_code: *u8) (PkgConfigError || ExecError)![]const PkgConfigPkg {
const stdout = try self.execAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
fn execPkgConfigList(self: *std.Build, out_code: *u8) (PkgConfigError || RunError)![]const PkgConfigPkg {
const stdout = try self.runAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
var list = ArrayList(PkgConfigPkg).init(self.allocator);
errdefer list.deinit();
var line_it = mem.tokenizeAny(u8, stdout, "\r\n");
Expand Down
2 changes: 0 additions & 2 deletions lib/std/Build/Step/Run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const mem = std.mem;
const process = std.process;
const ArrayList = std.ArrayList;
const EnvMap = process.EnvMap;
const Allocator = mem.Allocator;
const ExecError = std.Build.ExecError;
const assert = std.debug.assert;

const Run = @This();
Expand Down
14 changes: 7 additions & 7 deletions lib/std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub const ChildProcess = struct {
return term;
}

pub const ExecResult = struct {
pub const RunResult = struct {
term: Term,
stdout: []u8,
stderr: []u8,
Expand Down Expand Up @@ -317,22 +317,22 @@ pub const ChildProcess = struct {
stderr.* = fifoToOwnedArrayList(poller.fifo(.stderr));
}

pub const ExecError = os.GetCwdError || os.ReadError || SpawnError || os.PollError || error{
pub const RunError = os.GetCwdError || os.ReadError || SpawnError || os.PollError || error{
StdoutStreamTooLong,
StderrStreamTooLong,
};

/// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
/// If it succeeds, the caller owns result.stdout and result.stderr memory.
pub fn exec(args: struct {
pub fn run(args: struct {
allocator: mem.Allocator,
argv: []const []const u8,
cwd: ?[]const u8 = null,
cwd_dir: ?fs.Dir = null,
env_map: ?*const EnvMap = null,
max_output_bytes: usize = 50 * 1024,
expand_arg0: Arg0Expand = .no_expand,
}) ExecError!ExecResult {
}) RunError!RunResult {
var child = ChildProcess.init(args.argv, args.allocator);
child.stdin_behavior = .Ignore;
child.stdout_behavior = .Pipe;
Expand All @@ -352,7 +352,7 @@ pub const ChildProcess = struct {
try child.spawn();
try child.collectOutput(&stdout, &stderr, args.max_output_bytes);

return ExecResult{
return RunResult{
.term = try child.wait(),
.stdout = try stdout.toOwnedSlice(),
.stderr = try stderr.toOwnedSlice(),
Expand Down Expand Up @@ -821,7 +821,7 @@ pub const ChildProcess = struct {
const cmd_line_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, cmd_line);
defer self.allocator.free(cmd_line_w);

exec: {
run: {
const PATH: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATH")) orelse &[_:0]u16{};
const PATHEXT: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATHEXT")) orelse &[_:0]u16{};

Expand Down Expand Up @@ -873,7 +873,7 @@ pub const ChildProcess = struct {
dir_buf.shrinkRetainingCapacity(normalized_len);

if (windowsCreateProcessPathExt(self.allocator, &dir_buf, &app_buf, PATHEXT, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) {
break :exec;
break :run;
} else |err| switch (err) {
error.FileNotFound, error.AccessDenied, error.InvalidExe => continue,
error.UnrecoverableInvalidExe => return error.InvalidExe,
Expand Down
4 changes: 2 additions & 2 deletions lib/std/zig/system/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub const macos = @import("darwin/macos.zig");
/// stderr from xcode-select is ignored.
/// If error.OutOfMemory occurs in Allocator, this function returns null.
pub fn isSdkInstalled(allocator: Allocator) bool {
const result = std.process.Child.exec(.{
const result = std.process.Child.run(.{
.allocator = allocator,
.argv = &.{ "/usr/bin/xcode-select", "--print-path" },
}) catch return false;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn getSdk(allocator: Allocator, target: Target) ?[]const u8 {
else => return null,
};
const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" };
const result = std.process.Child.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
const result = std.process.Child.run(.{ .allocator = allocator, .argv = argv }) catch return null;
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
Expand Down
28 changes: 14 additions & 14 deletions src/libc_installation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub const LibCInstallation = struct {
dev_null,
});

const exec_res = std.ChildProcess.exec(.{
const run_res = std.ChildProcess.run(.{
.allocator = allocator,
.argv = argv.items,
.max_output_bytes = 1024 * 1024,
Expand All @@ -292,21 +292,21 @@ pub const LibCInstallation = struct {
},
};
defer {
allocator.free(exec_res.stdout);
allocator.free(exec_res.stderr);
allocator.free(run_res.stdout);
allocator.free(run_res.stderr);
}
switch (exec_res.term) {
switch (run_res.term) {
.Exited => |code| if (code != 0) {
printVerboseInvocation(argv.items, null, args.verbose, exec_res.stderr);
printVerboseInvocation(argv.items, null, args.verbose, run_res.stderr);
return error.CCompilerExitCode;
},
else => {
printVerboseInvocation(argv.items, null, args.verbose, exec_res.stderr);
printVerboseInvocation(argv.items, null, args.verbose, run_res.stderr);
return error.CCompilerCrashed;
},
}

var it = std.mem.tokenizeAny(u8, exec_res.stderr, "\n\r");
var it = std.mem.tokenizeAny(u8, run_res.stderr, "\n\r");
var search_paths = std.ArrayList([]const u8).init(allocator);
defer search_paths.deinit();
while (it.next()) |line| {
Expand Down Expand Up @@ -596,7 +596,7 @@ fn ccPrintFileName(args: CCPrintFileNameOptions) ![:0]u8 {
try appendCcExe(&argv, skip_cc_env_var);
try argv.append(arg1);

const exec_res = std.ChildProcess.exec(.{
const run_res = std.ChildProcess.run(.{
.allocator = allocator,
.argv = argv.items,
.max_output_bytes = 1024 * 1024,
Expand All @@ -611,21 +611,21 @@ fn ccPrintFileName(args: CCPrintFileNameOptions) ![:0]u8 {
else => return error.UnableToSpawnCCompiler,
};
defer {
allocator.free(exec_res.stdout);
allocator.free(exec_res.stderr);
allocator.free(run_res.stdout);
allocator.free(run_res.stderr);
}
switch (exec_res.term) {
switch (run_res.term) {
.Exited => |code| if (code != 0) {
printVerboseInvocation(argv.items, args.search_basename, args.verbose, exec_res.stderr);
printVerboseInvocation(argv.items, args.search_basename, args.verbose, run_res.stderr);
return error.CCompilerExitCode;
},
else => {
printVerboseInvocation(argv.items, args.search_basename, args.verbose, exec_res.stderr);
printVerboseInvocation(argv.items, args.search_basename, args.verbose, run_res.stderr);
return error.CCompilerCrashed;
},
}

var it = std.mem.tokenizeAny(u8, exec_res.stdout, "\n\r");
var it = std.mem.tokenizeAny(u8, run_res.stdout, "\n\r");
const line = it.next() orelse return error.LibCRuntimeNotFound;
// When this command fails, it returns exit code 0 and duplicates the input file name.
// So we detect failure by checking if the output matches exactly the input.
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4472,7 +4472,7 @@ fn cmdRc(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
}

if (process.can_spawn) {
var result = std.ChildProcess.exec(.{
var result = std.ChildProcess.run(.{
.allocator = gpa,
.argv = argv.items,
.max_output_bytes = std.math.maxInt(u32),
Expand Down
2 changes: 1 addition & 1 deletion test/standalone/windows_spawn/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn testExec(allocator: std.mem.Allocator, command: []const u8, expected_stdout:
}

fn testExecWithCwd(allocator: std.mem.Allocator, command: []const u8, cwd: ?[]const u8, expected_stdout: []const u8) !void {
var result = try std.ChildProcess.exec(.{
var result = try std.ChildProcess.run(.{
.allocator = allocator,
.argv = &[_][]const u8{command},
.cwd = cwd,
Expand Down

0 comments on commit fd2239b

Please sign in to comment.