Skip to content

Commit fd2239b

Browse files
Jan Philipp Haferandrewrk
authored andcommitted
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.
1 parent d8c0679 commit fd2239b

File tree

14 files changed

+57
-59
lines changed

14 files changed

+57
-59
lines changed

build.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const InitializedDepContext = struct {
172172
}
173173
};
174174

175-
pub const ExecError = error{
175+
pub 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

1630-
pub fn execAllowFail(
1630+
pub 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.
1674-
pub fn exec(b: *Build, argv: []const []const u8) []u8 {
1674+
pub 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const NativeTargetInfo = std.zig.system.NativeTargetInfo;
1414
const LazyPath = std.Build.LazyPath;
1515
const PkgConfigPkg = std.Build.PkgConfigPkg;
1616
const PkgConfigError = std.Build.PkgConfigError;
17-
const ExecError = std.Build.ExecError;
17+
const RunError = std.Build.RunError;
1818
const Module = std.Build.Module;
1919
const VcpkgRoot = std.Build.VcpkgRoot;
2020
const 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

2266-
fn 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);
2266+
fn 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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const mem = std.mem;
77
const process = std.process;
88
const ArrayList = std.ArrayList;
99
const EnvMap = process.EnvMap;
10-
const Allocator = mem.Allocator;
11-
const ExecError = std.Build.ExecError;
1210
const assert = std.debug.assert;
1311

1412
const Run = @This();

lib/std/child_process.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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,22 +317,22 @@ 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,
331331
cwd_dir: ?fs.Dir = null,
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1515
pub 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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn testExec(allocator: std.mem.Allocator, command: []const u8, expected_stdout:
158158
}
159159

160160
fn 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,

0 commit comments

Comments
 (0)