authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-14 21:35:43-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-10-14 21:35:43-04:00
log2f52f95b928c1dcb42afb22243c27fb9661cbd1b
treec47cd6837a9f881ef190f9672aab26ccc3904c26
parent352976ed236030d88d5a7332433b0b37bf4bc41c
parent6ba1fdf7e0d8dfc348a163219b3c3215c3644dd6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6669 from ifreund/color-fixes

std/build: support --color

4 files changed, 31 insertions(+), 27 deletions(-)

lib/std/build.zig+6
......@@ -45,6 +45,7 @@ pub const Builder = struct {
4545 verbose_llvm_ir: bool,
4646 verbose_cimport: bool,
4747 verbose_llvm_cpu_features: bool,
48 color: enum { auto, on, off } = .auto,
4849 invalid_user_input: bool,
4950 zig_exe: []const u8,
5051 default_step: *Step,
......@@ -1946,6 +1947,11 @@ pub const LibExeObjStep = struct {
19461947 };
19471948 zig_args.append(cmd) catch unreachable;
19481949
1950 if (builder.color != .auto) {
1951 try zig_args.append("--color");
1952 try zig_args.append(@tagName(builder.color));
1953 }
1954
19491955 if (self.root_src) |root_src| try zig_args.append(root_src.getPath(builder));
19501956
19511957 var prev_has_extra_flags = false;
lib/std/special/build_runner.zig+10
......@@ -82,6 +82,15 @@ pub fn main() !void {
8282 return usageAndErr(builder, false, stderr_stream);
8383 };
8484 builder.addSearchPrefix(search_prefix);
85 } else if (mem.eql(u8, arg, "--color")) {
86 const next_arg = nextArg(args, &arg_idx) orelse {
87 warn("expected [auto|on|off] after --color", .{});
88 return usageAndErr(builder, false, stderr_stream);
89 };
90 builder.color = std.meta.stringToEnum(@TypeOf(builder.color), next_arg) orelse {
91 warn("expected [auto|on|off] after --color, found '{}'", .{next_arg});
92 return usageAndErr(builder, false, stderr_stream);
93 };
8594 } else if (mem.eql(u8, arg, "--override-lib-dir")) {
8695 builder.override_lib_dir = nextArg(args, &arg_idx) orelse {
8796 warn("Expected argument after --override-lib-dir\n\n", .{});
......@@ -171,6 +180,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
171180 \\ --verbose Print commands before executing them
172181 \\ --prefix [path] Override default install prefix
173182 \\ --search-prefix [path] Add a path to look for binaries, libraries, headers
183 \\ --color [auto|off|on] Enable or disable colored error messages
174184 \\
175185 \\Project-Specific Options:
176186 \\
src/Compilation.zig+3-3
......@@ -103,7 +103,7 @@ owned_link_dir: ?std.fs.Dir,
103103
104104/// This is for stage1 and should be deleted upon completion of self-hosting.
105105/// Don't use this for anything other than stage1 compatibility.
106color: @import("main.zig").Color = .Auto,
106color: @import("main.zig").Color = .auto,
107107
108108test_filter: ?[]const u8,
109109test_name_prefix: ?[]const u8,
......@@ -385,7 +385,7 @@ pub const InitOptions = struct {
385385 machine_code_model: std.builtin.CodeModel = .default,
386386 clang_preprocessor_mode: ClangPreprocessorMode = .no,
387387 /// This is for stage1 and should be deleted upon completion of self-hosting.
388 color: @import("main.zig").Color = .Auto,
388 color: @import("main.zig").Color = .auto,
389389 test_filter: ?[]const u8 = null,
390390 test_name_prefix: ?[]const u8 = null,
391391 subsystem: ?std.Target.SubSystem = null,
......@@ -1179,7 +1179,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
11791179 var progress: std.Progress = .{};
11801180 var main_progress_node = try progress.start("", null);
11811181 defer main_progress_node.end();
1182 if (self.color == .Off) progress.terminal = null;
1182 if (self.color == .off) progress.terminal = null;
11831183
11841184 var c_comp_progress_node = main_progress_node.start("Compile C Objects", self.c_source_files.len);
11851185 defer c_comp_progress_node.end();
src/main.zig+12-24
......@@ -28,9 +28,9 @@ pub fn fatal(comptime format: []const u8, args: anytype) noreturn {
2828pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
2929
3030pub const Color = enum {
31 Auto,
32 Off,
33 On,
31 auto,
32 off,
33 on,
3434};
3535
3636const usage =
......@@ -414,7 +414,7 @@ fn buildOutputType(
414414 run,
415415 },
416416) !void {
417 var color: Color = .Auto;
417 var color: Color = .auto;
418418 var optimize_mode: std.builtin.Mode = .Debug;
419419 var provided_name: ?[]const u8 = null;
420420 var link_mode: ?std.builtin.LinkMode = null;
......@@ -622,15 +622,9 @@ fn buildOutputType(
622622 }
623623 i += 1;
624624 const next_arg = args[i];
625 if (mem.eql(u8, next_arg, "auto")) {
626 color = .Auto;
627 } else if (mem.eql(u8, next_arg, "on")) {
628 color = .On;
629 } else if (mem.eql(u8, next_arg, "off")) {
630 color = .Off;
631 } else {
625 color = std.meta.stringToEnum(Color, next_arg) orelse {
632626 fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg});
633 }
627 };
634628 } else if (mem.eql(u8, arg, "--subsystem")) {
635629 if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
636630 i += 1;
......@@ -2411,7 +2405,7 @@ const Fmt = struct {
24112405
24122406pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
24132407 const stderr_file = io.getStdErr();
2414 var color: Color = .Auto;
2408 var color: Color = .auto;
24152409 var stdin_flag: bool = false;
24162410 var check_flag: bool = false;
24172411 var input_files = ArrayList([]const u8).init(gpa);
......@@ -2431,15 +2425,9 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
24312425 }
24322426 i += 1;
24332427 const next_arg = args[i];
2434 if (mem.eql(u8, next_arg, "auto")) {
2435 color = .Auto;
2436 } else if (mem.eql(u8, next_arg, "on")) {
2437 color = .On;
2438 } else if (mem.eql(u8, next_arg, "off")) {
2439 color = .Off;
2440 } else {
2428 color = std.meta.stringToEnum(Color, next_arg) orelse {
24412429 fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg});
2442 }
2430 };
24432431 } else if (mem.eql(u8, arg, "--stdin")) {
24442432 stdin_flag = true;
24452433 } else if (mem.eql(u8, arg, "--check")) {
......@@ -2663,9 +2651,9 @@ fn printErrMsgToFile(
26632651 color: Color,
26642652) !void {
26652653 const color_on = switch (color) {
2666 .Auto => file.isTty(),
2667 .On => true,
2668 .Off => false,
2654 .auto => file.isTty(),
2655 .on => true,
2656 .off => false,
26692657 };
26702658 const lok_token = parse_error.loc();
26712659 const span_first = lok_token;