summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorManlio Perillo <manlio.perillo@gmail.com>2023-05-01 19:27:50 +0200
committerManlio Perillo <manlio.perillo@gmail.com>2023-05-03 18:13:55 +0200
commit11d8468539f489d760098453810ed9b274813169 (patch)
tree7aa4d64e60c9f23e35ee1c3c51ad5901e79d2c2e
parent40cbee8fa207c4b2d5918741493e015078d9b5fb (diff)
build: use Child.exec in ZiglingStep.run
Update the run method to use Child.exec, instead of Child.spawn followed by Child.wait. This simplifies the code.
-rw-r--r--build.zig47
1 files changed, 17 insertions, 30 deletions
diff --git a/build.zig b/build.zig
index 03fad4c..234a58f 100644
--- a/build.zig
+++ b/build.zig
@@ -272,44 +272,31 @@ const ZiglingStep = struct {
resetLine();
print("Checking {s}...\n", .{self.exercise.main_file});
- const cwd = self.builder.build_root.path.?;
-
- const argv = [_][]const u8{exe_path};
-
- var child = std.ChildProcess.init(&argv, self.builder.allocator);
-
- child.cwd = cwd;
- child.env_map = self.builder.env_map;
-
- child.stdin_behavior = .Inherit;
- if (self.exercise.check_stdout) {
- child.stdout_behavior = .Pipe;
- child.stderr_behavior = .Inherit;
- } else {
- child.stdout_behavior = .Inherit;
- child.stderr_behavior = .Pipe;
- }
+ const b = self.step.owner;
- child.spawn() catch |err| {
- print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text });
+ // Allow up to 1 MB of stdout capture.
+ const max_output_bytes = 1 * 1024 * 1024;
+
+ var result = Child.exec(.{
+ .allocator = b.allocator,
+ .argv = &.{exe_path},
+ .cwd = b.build_root.path.?,
+ .cwd_dir = b.build_root.handle,
+ .max_output_bytes = max_output_bytes,
+ }) catch |err| {
+ print("{s}Unable to spawn {s}: {s}{s}\n", .{
+ red_text, exe_path, @errorName(err), reset_text,
+ });
return err;
};
- // Allow up to 1 MB of stdout capture.
- const max_output_len = 1 * 1024 * 1024;
const output = if (self.exercise.check_stdout)
- try child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_output_len)
+ result.stdout
else
- try child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_output_len);
-
- // At this point stdout is closed, wait for the process to terminate.
- const term = child.wait() catch |err| {
- print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text });
- return err;
- };
+ result.stderr;
// Make sure it exited cleanly.
- switch (term) {
+ switch (result.term) {
.Exited => |code| {
if (code != 0) {
print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ red_text, self.exercise.main_file, code, 0, reset_text });