authorgravatar for jan.hafer@rwth-aachen.deJan Philipp Hafer <jan.hafer@rwth-aachen.de> 2023-10-19 22:36:11+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-22 14:47:20-04:00
logfd2239bde9e2051a32fb25c50c732a40d4afccd0
tree4adce729cd1ed961e617e6a02413f179a2236885
parentd8c067966fe88e15f9de5c42b83f1c5fbed550e4

child_process + Build: rename exec to run + all related code

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.

14 files changed, 57 insertions(+), 59 deletions(-)

build.zig+3-3
......@@ -255,7 +255,7 @@ pub fn build(b: *std.Build) !void {
255255 const version_string = b.fmt("{d}.{d}.{d}", .{ zig_version.major, zig_version.minor, zig_version.patch });
256256
257257 var code: u8 = undefined;
258 const git_describe_untrimmed = b.execAllowFail(&[_][]const u8{
258 const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{
259259 "git", "-C", b.build_root.path orelse ".", "describe", "--match", "*.*.*", "--tags",
260260 }, &code, .Ignore) catch {
261261 break :v version_string;
......@@ -738,9 +738,9 @@ fn addCxxKnownPath(
738738 return error.RequiredLibraryNotFound;
739739
740740 const path_padded = if (ctx.cxx_compiler_arg1.len > 0)
741 b.exec(&.{ ctx.cxx_compiler, ctx.cxx_compiler_arg1, b.fmt("-print-file-name={s}", .{objname}) })
741 b.run(&.{ ctx.cxx_compiler, ctx.cxx_compiler_arg1, b.fmt("-print-file-name={s}", .{objname}) })
742742 else
743 b.exec(&.{ ctx.cxx_compiler, b.fmt("-print-file-name={s}", .{objname}) });
743 b.run(&.{ ctx.cxx_compiler, b.fmt("-print-file-name={s}", .{objname}) });
744744 var tokenizer = mem.tokenizeAny(u8, path_padded, "\r\n");
745745 const path_unpadded = tokenizer.next().?;
746746 if (mem.eql(u8, path_unpadded, objname)) {
lib/std/Build.zig+5-5
......@@ -172,7 +172,7 @@ const InitializedDepContext = struct {
172172 }
173173};
174174
175pub const ExecError = error{
175pub const RunError = error{
176176 ReadFailure,
177177 ExitCodeFailure,
178178 ProcessTerminated,
......@@ -1627,12 +1627,12 @@ pub fn findProgram(self: *Build, names: []const []const u8, paths: []const []con
16271627 return error.FileNotFound;
16281628}
16291629
1630pub fn execAllowFail(
1630pub fn runAllowFail(
16311631 self: *Build,
16321632 argv: []const []const u8,
16331633 out_code: *u8,
16341634 stderr_behavior: std.ChildProcess.StdIo,
1635) ExecError![]u8 {
1635) RunError![]u8 {
16361636 assert(argv.len != 0);
16371637
16381638 if (!process.can_spawn)
......@@ -1671,7 +1671,7 @@ pub fn execAllowFail(
16711671/// This is a helper function to be called from build.zig scripts, *not* from
16721672/// inside step make() functions. If any errors occur, it fails the build with
16731673/// a helpful message.
1674pub fn exec(b: *Build, argv: []const []const u8) []u8 {
1674pub fn run(b: *Build, argv: []const []const u8) []u8 {
16751675 if (!process.can_spawn) {
16761676 std.debug.print("unable to spawn the following command: cannot spawn child process\n{s}\n", .{
16771677 try allocPrintCmd(b.allocator, null, argv),
......@@ -1680,7 +1680,7 @@ pub fn exec(b: *Build, argv: []const []const u8) []u8 {
16801680 }
16811681
16821682 var code: u8 = undefined;
1683 return b.execAllowFail(argv, &code, .Inherit) catch |err| {
1683 return b.runAllowFail(argv, &code, .Inherit) catch |err| {
16841684 const printed_cmd = allocPrintCmd(b.allocator, null, argv) catch @panic("OOM");
16851685 std.debug.print("unable to spawn the following command: {s}\n{s}\n", .{
16861686 @errorName(err), printed_cmd,
lib/std/Build/Step.zig+1-1
......@@ -273,7 +273,7 @@ pub fn evalChildProcess(s: *Step, argv: []const []const u8) !void {
273273 try handleChildProcUnsupported(s, null, argv);
274274 try handleVerbose(s.owner, null, argv);
275275
276 const result = std.ChildProcess.exec(.{
276 const result = std.ChildProcess.run(.{
277277 .allocator = arena,
278278 .argv = argv,
279279 }) catch |err| return s.fail("unable to spawn {s}: {s}", .{ argv[0], @errorName(err) });
lib/std/Build/Step/Compile.zig+4-4
......@@ -14,7 +14,7 @@ const NativeTargetInfo = std.zig.system.NativeTargetInfo;
1414const LazyPath = std.Build.LazyPath;
1515const PkgConfigPkg = std.Build.PkgConfigPkg;
1616const PkgConfigError = std.Build.PkgConfigError;
17const ExecError = std.Build.ExecError;
17const RunError = std.Build.RunError;
1818const Module = std.Build.Module;
1919const VcpkgRoot = std.Build.VcpkgRoot;
2020const InstallDir = std.Build.InstallDir;
......@@ -854,7 +854,7 @@ fn runPkgConfig(self: *Compile, lib_name: []const u8) ![]const []const u8 {
854854 };
855855
856856 var code: u8 = undefined;
857 const stdout = if (b.execAllowFail(&[_][]const u8{
857 const stdout = if (b.runAllowFail(&[_][]const u8{
858858 "pkg-config",
859859 pkg_name,
860860 "--cflags",
......@@ -2263,8 +2263,8 @@ pub fn doAtomicSymLinks(
22632263 };
22642264}
22652265
2266fn execPkgConfigList(self: *std.Build, out_code: *u8) (PkgConfigError || ExecError)![]const PkgConfigPkg {
2267 const stdout = try self.execAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
2266fn execPkgConfigList(self: *std.Build, out_code: *u8) (PkgConfigError || RunError)![]const PkgConfigPkg {
2267 const stdout = try self.runAllowFail(&[_][]const u8{ "pkg-config", "--list-all" }, out_code, .Ignore);
22682268 var list = ArrayList(PkgConfigPkg).init(self.allocator);
22692269 errdefer list.deinit();
22702270 var line_it = mem.tokenizeAny(u8, stdout, "\r\n");
lib/std/Build/Step/Run.zig-2
......@@ -7,8 +7,6 @@ const mem = std.mem;
77const process = std.process;
88const ArrayList = std.ArrayList;
99const EnvMap = process.EnvMap;
10const Allocator = mem.Allocator;
11const ExecError = std.Build.ExecError;
1210const assert = std.debug.assert;
1311
1412const Run = @This();
lib/std/child_process.zig+7-7
......@@ -262,7 +262,7 @@ pub const ChildProcess = struct {
262262 return term;
263263 }
264264
265 pub const ExecResult = struct {
265 pub const RunResult = struct {
266266 term: Term,
267267 stdout: []u8,
268268 stderr: []u8,
......@@ -317,14 +317,14 @@ pub const ChildProcess = struct {
317317 stderr.* = fifoToOwnedArrayList(poller.fifo(.stderr));
318318 }
319319
320 pub const ExecError = os.GetCwdError || os.ReadError || SpawnError || os.PollError || error{
320 pub const RunError = os.GetCwdError || os.ReadError || SpawnError || os.PollError || error{
321321 StdoutStreamTooLong,
322322 StderrStreamTooLong,
323323 };
324324
325325 /// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
326326 /// If it succeeds, the caller owns result.stdout and result.stderr memory.
327 pub fn exec(args: struct {
327 pub fn run(args: struct {
328328 allocator: mem.Allocator,
329329 argv: []const []const u8,
330330 cwd: ?[]const u8 = null,
......@@ -332,7 +332,7 @@ pub const ChildProcess = struct {
332332 env_map: ?*const EnvMap = null,
333333 max_output_bytes: usize = 50 * 1024,
334334 expand_arg0: Arg0Expand = .no_expand,
335 }) ExecError!ExecResult {
335 }) RunError!RunResult {
336336 var child = ChildProcess.init(args.argv, args.allocator);
337337 child.stdin_behavior = .Ignore;
338338 child.stdout_behavior = .Pipe;
......@@ -352,7 +352,7 @@ pub const ChildProcess = struct {
352352 try child.spawn();
353353 try child.collectOutput(&stdout, &stderr, args.max_output_bytes);
354354
355 return ExecResult{
355 return RunResult{
356356 .term = try child.wait(),
357357 .stdout = try stdout.toOwnedSlice(),
358358 .stderr = try stderr.toOwnedSlice(),
......@@ -821,7 +821,7 @@ pub const ChildProcess = struct {
821821 const cmd_line_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, cmd_line);
822822 defer self.allocator.free(cmd_line_w);
823823
824 exec: {
824 run: {
825825 const PATH: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATH")) orelse &[_:0]u16{};
826826 const PATHEXT: [:0]const u16 = std.os.getenvW(unicode.utf8ToUtf16LeStringLiteral("PATHEXT")) orelse &[_:0]u16{};
827827
......@@ -873,7 +873,7 @@ pub const ChildProcess = struct {
873873 dir_buf.shrinkRetainingCapacity(normalized_len);
874874
875875 if (windowsCreateProcessPathExt(self.allocator, &dir_buf, &app_buf, PATHEXT, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) {
876 break :exec;
876 break :run;
877877 } else |err| switch (err) {
878878 error.FileNotFound, error.AccessDenied, error.InvalidExe => continue,
879879 error.UnrecoverableInvalidExe => return error.InvalidExe,
lib/std/zig/system/darwin.zig+2-2
......@@ -13,7 +13,7 @@ pub const macos = @import("darwin/macos.zig");
1313/// stderr from xcode-select is ignored.
1414/// If error.OutOfMemory occurs in Allocator, this function returns null.
1515pub fn isSdkInstalled(allocator: Allocator) bool {
16 const result = std.process.Child.exec(.{
16 const result = std.process.Child.run(.{
1717 .allocator = allocator,
1818 .argv = &.{ "/usr/bin/xcode-select", "--print-path" },
1919 }) catch return false;
......@@ -44,7 +44,7 @@ pub fn getSdk(allocator: Allocator, target: Target) ?[]const u8 {
4444 else => return null,
4545 };
4646 const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" };
47 const result = std.process.Child.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
47 const result = std.process.Child.run(.{ .allocator = allocator, .argv = argv }) catch return null;
4848 defer {
4949 allocator.free(result.stderr);
5050 allocator.free(result.stdout);
src/libc_installation.zig+14-14
......@@ -274,7 +274,7 @@ pub const LibCInstallation = struct {
274274 dev_null,
275275 });
276276
277 const exec_res = std.ChildProcess.exec(.{
277 const run_res = std.ChildProcess.run(.{
278278 .allocator = allocator,
279279 .argv = argv.items,
280280 .max_output_bytes = 1024 * 1024,
......@@ -292,21 +292,21 @@ pub const LibCInstallation = struct {
292292 },
293293 };
294294 defer {
295 allocator.free(exec_res.stdout);
296 allocator.free(exec_res.stderr);
295 allocator.free(run_res.stdout);
296 allocator.free(run_res.stderr);
297297 }
298 switch (exec_res.term) {
298 switch (run_res.term) {
299299 .Exited => |code| if (code != 0) {
300 printVerboseInvocation(argv.items, null, args.verbose, exec_res.stderr);
300 printVerboseInvocation(argv.items, null, args.verbose, run_res.stderr);
301301 return error.CCompilerExitCode;
302302 },
303303 else => {
304 printVerboseInvocation(argv.items, null, args.verbose, exec_res.stderr);
304 printVerboseInvocation(argv.items, null, args.verbose, run_res.stderr);
305305 return error.CCompilerCrashed;
306306 },
307307 }
308308
309 var it = std.mem.tokenizeAny(u8, exec_res.stderr, "\n\r");
309 var it = std.mem.tokenizeAny(u8, run_res.stderr, "\n\r");
310310 var search_paths = std.ArrayList([]const u8).init(allocator);
311311 defer search_paths.deinit();
312312 while (it.next()) |line| {
......@@ -596,7 +596,7 @@ fn ccPrintFileName(args: CCPrintFileNameOptions) ![:0]u8 {
596596 try appendCcExe(&argv, skip_cc_env_var);
597597 try argv.append(arg1);
598598
599 const exec_res = std.ChildProcess.exec(.{
599 const run_res = std.ChildProcess.run(.{
600600 .allocator = allocator,
601601 .argv = argv.items,
602602 .max_output_bytes = 1024 * 1024,
......@@ -611,21 +611,21 @@ fn ccPrintFileName(args: CCPrintFileNameOptions) ![:0]u8 {
611611 else => return error.UnableToSpawnCCompiler,
612612 };
613613 defer {
614 allocator.free(exec_res.stdout);
615 allocator.free(exec_res.stderr);
614 allocator.free(run_res.stdout);
615 allocator.free(run_res.stderr);
616616 }
617 switch (exec_res.term) {
617 switch (run_res.term) {
618618 .Exited => |code| if (code != 0) {
619 printVerboseInvocation(argv.items, args.search_basename, args.verbose, exec_res.stderr);
619 printVerboseInvocation(argv.items, args.search_basename, args.verbose, run_res.stderr);
620620 return error.CCompilerExitCode;
621621 },
622622 else => {
623 printVerboseInvocation(argv.items, args.search_basename, args.verbose, exec_res.stderr);
623 printVerboseInvocation(argv.items, args.search_basename, args.verbose, run_res.stderr);
624624 return error.CCompilerCrashed;
625625 },
626626 }
627627
628 var it = std.mem.tokenizeAny(u8, exec_res.stdout, "\n\r");
628 var it = std.mem.tokenizeAny(u8, run_res.stdout, "\n\r");
629629 const line = it.next() orelse return error.LibCRuntimeNotFound;
630630 // When this command fails, it returns exit code 0 and duplicates the input file name.
631631 // So we detect failure by checking if the output matches exactly the input.
src/main.zig+1-1
......@@ -4472,7 +4472,7 @@ fn cmdRc(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
44724472 }
44734473
44744474 if (process.can_spawn) {
4475 var result = std.ChildProcess.exec(.{
4475 var result = std.ChildProcess.run(.{
44764476 .allocator = gpa,
44774477 .argv = argv.items,
44784478 .max_output_bytes = std.math.maxInt(u32),
test/standalone/windows_spawn/main.zig+1-1
......@@ -158,7 +158,7 @@ fn testExec(allocator: std.mem.Allocator, command: []const u8, expected_stdout:
158158}
159159
160160fn testExecWithCwd(allocator: std.mem.Allocator, command: []const u8, cwd: ?[]const u8, expected_stdout: []const u8) !void {
161 var result = try std.ChildProcess.exec(.{
161 var result = try std.ChildProcess.run(.{
162162 .allocator = allocator,
163163 .argv = &[_][]const u8{command},
164164 .cwd = cwd,
tools/docgen.zig+15-15
......@@ -1443,7 +1443,7 @@ fn genHtml(
14431443 try shell_out.print("\n", .{});
14441444
14451445 if (expected_outcome == .build_fail) {
1446 const result = try ChildProcess.exec(.{
1446 const result = try ChildProcess.run(.{
14471447 .allocator = allocator,
14481448 .argv = build_args.items,
14491449 .cwd = tmp_dir_name,
......@@ -1471,7 +1471,7 @@ fn genHtml(
14711471 try shell_out.writeAll(colored_stderr);
14721472 break :code_block;
14731473 }
1474 const exec_result = exec(allocator, &env_map, tmp_dir_name, build_args.items) catch
1474 const exec_result = run(allocator, &env_map, tmp_dir_name, build_args.items) catch
14751475 return parseError(tokenizer, code.source_token, "example failed to compile", .{});
14761476
14771477 if (code.verbose_cimport) {
......@@ -1499,7 +1499,7 @@ fn genHtml(
14991499 var exited_with_signal = false;
15001500
15011501 const result = if (expected_outcome == .fail) blk: {
1502 const result = try ChildProcess.exec(.{
1502 const result = try ChildProcess.run(.{
15031503 .allocator = allocator,
15041504 .argv = run_args,
15051505 .env_map = &env_map,
......@@ -1520,7 +1520,7 @@ fn genHtml(
15201520 }
15211521 break :blk result;
15221522 } else blk: {
1523 break :blk exec(allocator, &env_map, tmp_dir_name, run_args) catch return parseError(tokenizer, code.source_token, "example crashed", .{});
1523 break :blk run(allocator, &env_map, tmp_dir_name, run_args) catch return parseError(tokenizer, code.source_token, "example crashed", .{});
15241524 };
15251525
15261526 const escaped_stderr = try escapeHtml(allocator, result.stderr);
......@@ -1581,7 +1581,7 @@ fn genHtml(
15811581 },
15821582 }
15831583 }
1584 const result = exec(allocator, &env_map, null, test_args.items) catch
1584 const result = run(allocator, &env_map, null, test_args.items) catch
15851585 return parseError(tokenizer, code.source_token, "test failed", .{});
15861586 const escaped_stderr = try escapeHtml(allocator, result.stderr);
15871587 const escaped_stdout = try escapeHtml(allocator, result.stdout);
......@@ -1612,7 +1612,7 @@ fn genHtml(
16121612 try test_args.append("-lc");
16131613 try shell_out.print("-lc ", .{});
16141614 }
1615 const result = try ChildProcess.exec(.{
1615 const result = try ChildProcess.run(.{
16161616 .allocator = allocator,
16171617 .argv = test_args.items,
16181618 .env_map = &env_map,
......@@ -1671,7 +1671,7 @@ fn genHtml(
16711671 },
16721672 }
16731673
1674 const result = try ChildProcess.exec(.{
1674 const result = try ChildProcess.run(.{
16751675 .allocator = allocator,
16761676 .argv = test_args.items,
16771677 .env_map = &env_map,
......@@ -1744,7 +1744,7 @@ fn genHtml(
17441744 }
17451745
17461746 if (maybe_error_match) |error_match| {
1747 const result = try ChildProcess.exec(.{
1747 const result = try ChildProcess.run(.{
17481748 .allocator = allocator,
17491749 .argv = build_args.items,
17501750 .env_map = &env_map,
......@@ -1775,7 +1775,7 @@ fn genHtml(
17751775 const colored_stderr = try termColor(allocator, escaped_stderr);
17761776 try shell_out.print("\n{s} ", .{colored_stderr});
17771777 } else {
1778 _ = exec(allocator, &env_map, null, build_args.items) catch return parseError(tokenizer, code.source_token, "example failed to compile", .{});
1778 _ = run(allocator, &env_map, null, build_args.items) catch return parseError(tokenizer, code.source_token, "example failed to compile", .{});
17791779 }
17801780 try shell_out.writeAll("\n");
17811781 },
......@@ -1828,7 +1828,7 @@ fn genHtml(
18281828 try test_args.append(option);
18291829 try shell_out.print("{s} ", .{option});
18301830 }
1831 const result = exec(allocator, &env_map, null, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
1831 const result = run(allocator, &env_map, null, test_args.items) catch return parseError(tokenizer, code.source_token, "test failed", .{});
18321832 const escaped_stderr = try escapeHtml(allocator, result.stderr);
18331833 const escaped_stdout = try escapeHtml(allocator, result.stdout);
18341834 try shell_out.print("\n{s}{s}\n", .{ escaped_stderr, escaped_stdout });
......@@ -1843,13 +1843,13 @@ fn genHtml(
18431843 }
18441844}
18451845
1846fn exec(
1846fn run(
18471847 allocator: Allocator,
18481848 env_map: *process.EnvMap,
18491849 cwd: ?[]const u8,
18501850 args: []const []const u8,
1851) !ChildProcess.ExecResult {
1852 const result = try ChildProcess.exec(.{
1851) !ChildProcess.RunResult {
1852 const result = try ChildProcess.run(.{
18531853 .allocator = allocator,
18541854 .argv = args,
18551855 .env_map = env_map,
......@@ -1880,12 +1880,12 @@ fn getBuiltinCode(
18801880 opt_zig_lib_dir: ?[]const u8,
18811881) ![]const u8 {
18821882 if (opt_zig_lib_dir) |zig_lib_dir| {
1883 const result = try exec(allocator, env_map, null, &.{
1883 const result = try run(allocator, env_map, null, &.{
18841884 zig_exe, "build-obj", "--show-builtin", "--zig-lib-dir", zig_lib_dir,
18851885 });
18861886 return result.stdout;
18871887 } else {
1888 const result = try exec(allocator, env_map, null, &.{
1888 const result = try run(allocator, env_map, null, &.{
18891889 zig_exe, "build-obj", "--show-builtin",
18901890 });
18911891 return result.stdout;
tools/generate_linux_syscalls.zig+2-2
......@@ -257,7 +257,7 @@ pub fn main() !void {
257257 "arch/arm64/include/uapi/asm/unistd.h",
258258 };
259259
260 const child_result = try std.ChildProcess.exec(.{
260 const child_result = try std.ChildProcess.run(.{
261261 .allocator = allocator,
262262 .argv = &child_args,
263263 .cwd = linux_path,
......@@ -318,7 +318,7 @@ pub fn main() !void {
318318 "arch/riscv/include/uapi/asm/unistd.h",
319319 };
320320
321 const child_result = try std.ChildProcess.exec(.{
321 const child_result = try std.ChildProcess.run(.{
322322 .allocator = allocator,
323323 .argv = &child_args,
324324 .cwd = linux_path,
tools/update_clang_options.zig+1-1
......@@ -613,7 +613,7 @@ pub fn main() anyerror!void {
613613 try std.fmt.allocPrint(allocator, "-I={s}/clang/include/clang/Driver", .{llvm_src_root}),
614614 };
615615
616 const child_result = try std.ChildProcess.exec(.{
616 const child_result = try std.ChildProcess.run(.{
617617 .allocator = allocator,
618618 .argv = &child_args,
619619 .max_output_bytes = 100 * 1024 * 1024,
tools/update_cpu_features.zig+1-1
......@@ -1064,7 +1064,7 @@ fn processOneTarget(job: Job) anyerror!void {
10641064 }),
10651065 };
10661066
1067 const child_result = try std.ChildProcess.exec(.{
1067 const child_result = try std.ChildProcess.run(.{
10681068 .allocator = arena,
10691069 .argv = &child_args,
10701070 .max_output_bytes = 400 * 1024 * 1024,