Skip to content

Commit

Permalink
std.childprocess: 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 ziglang#5853.
  • Loading branch information
matu3ba committed Jan 4, 2023
1 parent ed23615 commit 606444b
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 105 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ fn addCxxKnownPath(
) !void {
if (!std.process.can_spawn)
return error.RequiredLibraryNotFound;
const path_padded = try b.exec(&[_][]const u8{
const path_padded = try b.run(&[_][]const u8{
ctx.cxx_compiler,
b.fmt("-print-file-name={s}", .{objname}),
});
Expand Down
26 changes: 13 additions & 13 deletions doc/docgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ fn genHtml(
try shell_out.print("\n", .{});

if (expected_outcome == .BuildFail) {
const result = try ChildProcess.exec(.{
const result = try ChildProcess.run(.{
.allocator = allocator,
.argv = build_args.items,
.env_map = &env_map,
Expand All @@ -1405,7 +1405,7 @@ fn genHtml(
try shell_out.writeAll(colored_stderr);
break :code_block;
}
const exec_result = exec(allocator, &env_map, build_args.items) catch
const exec_result = run(allocator, &env_map, build_args.items) catch
return parseError(tokenizer, code.source_token, "example failed to compile", .{});

if (code.verbose_cimport) {
Expand Down Expand Up @@ -1438,7 +1438,7 @@ fn genHtml(
var exited_with_signal = false;

const result = if (expected_outcome == ExpectedOutcome.Fail) blk: {
const result = try ChildProcess.exec(.{
const result = try ChildProcess.run(.{
.allocator = allocator,
.argv = run_args,
.env_map = &env_map,
Expand All @@ -1458,7 +1458,7 @@ fn genHtml(
}
break :blk result;
} else blk: {
break :blk exec(allocator, &env_map, run_args) catch return parseError(tokenizer, code.source_token, "example crashed", .{});
break :blk run(allocator, &env_map, run_args) catch return parseError(tokenizer, code.source_token, "example crashed", .{});
};

const escaped_stderr = try escapeHtml(allocator, result.stderr);
Expand Down Expand Up @@ -1519,7 +1519,7 @@ fn genHtml(
},
}
}
const result = exec(allocator, &env_map, test_args.items) catch
const result = run(allocator, &env_map, test_args.items) catch
return parseError(tokenizer, code.source_token, "test failed", .{});
const escaped_stderr = try escapeHtml(allocator, result.stderr);
const escaped_stdout = try escapeHtml(allocator, result.stdout);
Expand Down Expand Up @@ -1553,7 +1553,7 @@ fn genHtml(
try test_args.append("-fstage1");
try shell_out.print("-fstage1", .{});
}
const result = try ChildProcess.exec(.{
const result = try ChildProcess.run(.{
.allocator = allocator,
.argv = test_args.items,
.env_map = &env_map,
Expand Down Expand Up @@ -1609,7 +1609,7 @@ fn genHtml(
},
}

const result = try ChildProcess.exec(.{
const result = try ChildProcess.run(.{
.allocator = allocator,
.argv = test_args.items,
.env_map = &env_map,
Expand Down Expand Up @@ -1678,7 +1678,7 @@ fn genHtml(
}

if (maybe_error_match) |error_match| {
const result = try ChildProcess.exec(.{
const result = try ChildProcess.run(.{
.allocator = allocator,
.argv = build_args.items,
.env_map = &env_map,
Expand Down Expand Up @@ -1709,7 +1709,7 @@ fn genHtml(
const colored_stderr = try termColor(allocator, escaped_stderr);
try shell_out.print("\n{s} ", .{colored_stderr});
} else {
_ = exec(allocator, &env_map, build_args.items) catch return parseError(tokenizer, code.source_token, "example failed to compile", .{});
_ = run(allocator, &env_map, build_args.items) catch return parseError(tokenizer, code.source_token, "example failed to compile", .{});
}
try shell_out.writeAll("\n");
},
Expand Down Expand Up @@ -1756,7 +1756,7 @@ fn genHtml(
},
}
}
const result = exec(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
const result = run(allocator, &env_map, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
const escaped_stderr = try escapeHtml(allocator, result.stderr);
const escaped_stdout = try escapeHtml(allocator, result.stdout);
try shell_out.print("\n{s}{s}\n", .{ escaped_stderr, escaped_stdout });
Expand All @@ -1771,8 +1771,8 @@ fn genHtml(
}
}

fn exec(allocator: Allocator, env_map: *process.EnvMap, args: []const []const u8) !ChildProcess.ExecResult {
const result = try ChildProcess.exec(.{
fn run(allocator: Allocator, env_map: *process.EnvMap, args: []const []const u8) !ChildProcess.RunResult {
const result = try ChildProcess.run(.{
.allocator = allocator,
.argv = args,
.env_map = env_map,
Expand All @@ -1796,7 +1796,7 @@ fn exec(allocator: Allocator, env_map: *process.EnvMap, args: []const []const u8
}

fn getBuiltinCode(allocator: Allocator, env_map: *process.EnvMap, zig_exe: []const u8) ![]const u8 {
const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });
const result = try run(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });
return result.stdout;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub const Builder = struct {
/// Information about the native target. Computed before build() is invoked.
host: NativeTargetInfo,

pub const ExecError = error{
pub const RunError = error{
ReadFailure,
ExitCodeFailure,
ProcessTerminated,
Expand Down Expand Up @@ -1181,7 +1181,7 @@ pub const Builder = struct {
argv: []const []const u8,
out_code: *u8,
stderr_behavior: std.ChildProcess.StdIo,
) ExecError![]u8 {
) RunError![]u8 {
assert(argv.len != 0);

if (!std.process.can_spawn)
Expand Down Expand Up @@ -1217,7 +1217,7 @@ pub const Builder = struct {
}
}

pub fn execFromStep(self: *Builder, argv: []const []const u8, src_step: ?*Step) ![]u8 {
pub fn runFromStep(self: *Builder, argv: []const []const u8, src_step: ?*Step) ![]u8 {
assert(argv.len != 0);

if (self.verbose) {
Expand Down Expand Up @@ -1266,8 +1266,8 @@ pub const Builder = struct {
};
}

pub fn exec(self: *Builder, argv: []const []const u8) ![]u8 {
return self.execFromStep(argv, null);
pub fn run(self: *Builder, argv: []const []const u8) ![]u8 {
return self.runFromStep(argv, null);
}

pub fn addSearchPrefix(self: *Builder, search_prefix: []const u8) void {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/build/InstallRawStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn make(step: *Step) !void {
};

try argv_list.appendSlice(&.{ full_src_path, full_dest_path });
_ = try self.builder.execFromStep(argv_list.items, &self.step);
_ = try self.builder.runFromStep(argv_list.items, &self.step);
}

test {
Expand Down
6 changes: 3 additions & 3 deletions lib/std/build/LibExeObjStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const NativeTargetInfo = std.zig.system.NativeTargetInfo;
const FileSource = std.build.FileSource;
const PkgConfigPkg = Builder.PkgConfigPkg;
const PkgConfigError = Builder.PkgConfigError;
const ExecError = Builder.ExecError;
const RunError = Builder.RunError;
const Pkg = std.build.Pkg;
const VcpkgRoot = std.build.VcpkgRoot;
const InstallDir = std.build.InstallDir;
Expand Down Expand Up @@ -1875,7 +1875,7 @@ fn make(step: *Step) !void {
try zig_args.append(try std.mem.concat(builder.allocator, u8, &[_][]const u8{ "@", args_file }));
}

const output_dir_nl = try builder.execFromStep(zig_args.items, &self.step);
const output_dir_nl = try builder.runFromStep(zig_args.items, &self.step);
const build_output_dir = mem.trimRight(u8, output_dir_nl, "\r\n");

if (self.output_dir) |output_dir| {
Expand Down Expand Up @@ -1991,7 +1991,7 @@ pub fn doAtomicSymLinks(allocator: Allocator, output_path: []const u8, filename_
};
}

fn execPkgConfigList(self: *Builder, out_code: *u8) (PkgConfigError || ExecError)![]const PkgConfigPkg {
fn execPkgConfigList(self: *Builder, out_code: *u8) (PkgConfigError || RunError)![]const PkgConfigPkg {
const stdout = try self.execAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
var list = ArrayList(PkgConfigPkg).init(self.allocator);
errdefer list.deinit();
Expand Down
4 changes: 2 additions & 2 deletions lib/std/build/RunStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const process = std.process;
const ArrayList = std.ArrayList;
const EnvMap = process.EnvMap;
const Allocator = mem.Allocator;
const ExecError = build.Builder.ExecError;
const RunError = build.Builder.RunError;

const max_stdout_size = 1 * 1024 * 1024; // 1 MiB

Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn runCommand(
const cmd = try std.mem.join(builder.allocator, " ", argv);
std.debug.print("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd });
builder.allocator.free(cmd);
return ExecError.ExecNotSupported;
return RunError.ExecNotSupported;
}

var child = std.ChildProcess.init(argv, builder.allocator);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/build/TranslateCStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn make(step: *Step) !void {

try argv_list.append(self.source.getPath(self.builder));

const output_path_nl = try self.builder.execFromStep(argv_list.items, &self.step);
const output_path_nl = try self.builder.runFromStep(argv_list.items, &self.step);
const output_path = mem.trimRight(u8, output_path_nl, "\r\n");

self.out_basename = fs.path.basename(output_path);
Expand Down
16 changes: 8 additions & 8 deletions lib/std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub const ChildProcess = struct {
}
}

pub const ExecResult = struct {
pub const RunResult = struct {
term: Term,
stdout: []u8,
stderr: []u8,
Expand Down Expand Up @@ -373,15 +373,15 @@ pub const ChildProcess = struct {

/// 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,
}) !ExecResult {
}) !RunResult {
var child = ChildProcess.init(args.argv, args.allocator);
child.stdin_behavior = .Ignore;
child.stdout_behavior = .Pipe;
Expand All @@ -402,7 +402,7 @@ pub const ChildProcess = struct {
const stderr = try stderr_in.readAllAlloc(args.allocator, args.max_output_bytes);
errdefer args.allocator.free(stderr);

return ExecResult{
return RunResult{
.term = try child.wait(),
.stdout = stdout,
.stderr = stderr,
Expand All @@ -422,7 +422,7 @@ pub const ChildProcess = struct {
try collectOutputPosix(child, &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 @@ -989,7 +989,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 @@ -1041,7 +1041,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 Expand Up @@ -1246,7 +1246,7 @@ fn windowsCreateProcessPathExt(
};

// Now we know that at least *a* file matching the wildcard exists, we can loop
// through PATHEXT in order and exec any that exist
// through PATHEXT in order and run any that exist

var ext_it = mem.tokenize(u16, pathext, &[_]u16{';'});
while (ext_it.next()) |ext| {
Expand Down
6 changes: 3 additions & 3 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");
/// https://github.com/Homebrew/brew/blob/e119bdc571dcb000305411bc1e26678b132afb98/Library/Homebrew/brew.sh#L630
pub fn isDarwinSDKInstalled(allocator: Allocator) bool {
const argv = &[_][]const u8{ "/usr/bin/xcode-select", "--print-path" };
const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return false;
const result = std.ChildProcess.run(.{ .allocator = allocator, .argv = argv }) catch return false;
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
Expand All @@ -40,7 +40,7 @@ pub fn getDarwinSDK(allocator: Allocator, target: Target) ?DarwinSDK {
};
const path = path: {
const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" };
const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
const result = std.ChildProcess.run(.{ .allocator = allocator, .argv = argv }) catch return null;
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
Expand All @@ -55,7 +55,7 @@ pub fn getDarwinSDK(allocator: Allocator, target: Target) ?DarwinSDK {
};
const version = version: {
const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-version" };
const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
const result = std.ChildProcess.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 @@ -261,7 +261,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 @@ -279,21 +279,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.tokenize(u8, exec_res.stderr, "\n\r");
var it = std.mem.tokenize(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 @@ -584,7 +584,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 @@ -599,21 +599,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.tokenize(u8, exec_res.stdout, "\n\r");
var it = std.mem.tokenize(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

0 comments on commit 606444b

Please sign in to comment.