authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-09 23:56:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:14-07:00
log7d5bce56e16cd7661b79046d573427972bbb6cf5
treee1ed0d941eaa4a6813fad9ec2815c6d176f3e3f8
parent097bcca069c5c7abdf77f321182eb4a0c54b3a93

build runner: print to stderr in dumb terminals

Terminal progress is suppressed and instead there is an explicit handling of printing to stderr, one line per step make() function call. The output looks very similar to Ninja. A future commit should add a -q to quiet the output.

1 files changed, 34 insertions(+), 9 deletions(-)

lib/build_runner.zig+34-9
......@@ -278,8 +278,12 @@ pub fn main() !void {
278278 .windows_api => {},
279279 }
280280
281 var progress: std.Progress = .{ .dont_print_on_dumb = true };
281 var progress: std.Progress = .{};
282282 const main_progress_node = progress.start("", 0);
283 if (ttyconf == .no_color) {
284 progress.timer = null;
285 progress.terminal = null;
286 }
283287
284288 builder.debug_log_scopes = debug_log_scopes.items;
285289 builder.resolveInstallPrefix(install_prefix, dir_list);
......@@ -302,6 +306,9 @@ pub fn main() !void {
302306 .enable_summary = enable_summary,
303307 .ttyconf = ttyconf,
304308 .stderr = stderr,
309
310 .step_index = 0,
311 .step_count = undefined,
305312 };
306313
307314 if (run.max_rss == 0) {
......@@ -332,6 +339,9 @@ const Run = struct {
332339 enable_summary: ?bool,
333340 ttyconf: std.debug.TTY.Config,
334341 stderr: std.fs.File,
342
343 step_count: usize,
344 step_index: usize,
335345};
336346
337347fn runStepNames(
......@@ -395,7 +405,8 @@ fn runStepNames(
395405 {
396406 defer parent_prog_node.end();
397407
398 var step_prog = parent_prog_node.start("run steps", step_stack.count());
408 run.step_count = step_stack.count();
409 var step_prog = parent_prog_node.start("run steps", run.step_count);
399410 defer step_prog.end();
400411
401412 var wait_group: std.Thread.WaitGroup = .{};
......@@ -739,10 +750,27 @@ fn workerMakeOneStep(
739750 sub_prog_node.activate();
740751 defer sub_prog_node.end();
741752
742 // I suspect we will want to pass `b` to make() in a future modification.
743 // For example, CompileStep does some sus things with modifying the saved
744 // *Build object in install header steps that might be able to be removed
745 // by passing the *Build object through the make() functions.
753 const stderr = run.stderr;
754 const ttyconf = run.ttyconf;
755
756 // If we are unable to print a fancy terminal progress bar, then we resort
757 // to 1 line printed to stderr for each step, similar to Ninja.
758 if (ttyconf == .no_color) {
759 var buf: [120]u8 = undefined;
760 const step_index = @atomicRmw(usize, &run.step_index, .Add, 1, .Monotonic);
761 const text = std.fmt.bufPrint(&buf, "[{d}/{d}] Making {s}{s}\n", .{
762 step_index + 1, run.step_count, s.owner.dep_prefix, s.name,
763 }) catch |err| switch (err) {
764 error.NoSpaceLeft => blk: {
765 buf[buf.len - 4 ..].* = "...\n".*;
766 break :blk &buf;
767 },
768 };
769 std.debug.getStderrMutex().lock();
770 defer std.debug.getStderrMutex().unlock();
771 stderr.writeAll(text) catch {};
772 }
773
746774 const make_result = s.make(&sub_prog_node);
747775
748776 // No matter the result, we want to display error/warning messages.
......@@ -750,9 +778,6 @@ fn workerMakeOneStep(
750778 sub_prog_node.context.lock_stderr();
751779 defer sub_prog_node.context.unlock_stderr();
752780
753 const stderr = run.stderr;
754 const ttyconf = run.ttyconf;
755
756781 for (s.result_error_msgs.items) |msg| {
757782 // Sometimes it feels like you just can't catch a break. Finally,
758783 // with Zig, you can.