diff --git a/lib/std/build.zig b/lib/std/build.zig index 6575e057e8d3..4cbf6ea67aae 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -45,6 +45,7 @@ pub const Builder = struct { verbose_llvm_ir: bool, verbose_cimport: bool, verbose_llvm_cpu_features: bool, + color: enum { auto, on, off } = .auto, invalid_user_input: bool, zig_exe: []const u8, default_step: *Step, @@ -1946,6 +1947,11 @@ pub const LibExeObjStep = struct { }; zig_args.append(cmd) catch unreachable; + if (builder.color != .auto) { + try zig_args.append("--color"); + try zig_args.append(@tagName(builder.color)); + } + if (self.root_src) |root_src| try zig_args.append(root_src.getPath(builder)); var prev_has_extra_flags = false; diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 43d6b9653647..f861d3159b88 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -82,6 +82,15 @@ pub fn main() !void { return usageAndErr(builder, false, stderr_stream); }; builder.addSearchPrefix(search_prefix); + } else if (mem.eql(u8, arg, "--color")) { + const next_arg = nextArg(args, &arg_idx) orelse { + warn("expected [auto|on|off] after --color", .{}); + return usageAndErr(builder, false, stderr_stream); + }; + builder.color = std.meta.stringToEnum(@TypeOf(builder.color), next_arg) orelse { + warn("expected [auto|on|off] after --color, found '{}'", .{next_arg}); + return usageAndErr(builder, false, stderr_stream); + }; } else if (mem.eql(u8, arg, "--override-lib-dir")) { builder.override_lib_dir = nextArg(args, &arg_idx) orelse { 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 \\ --verbose Print commands before executing them \\ --prefix [path] Override default install prefix \\ --search-prefix [path] Add a path to look for binaries, libraries, headers + \\ --color [auto|off|on] Enable or disable colored error messages \\ \\Project-Specific Options: \\ diff --git a/src/Compilation.zig b/src/Compilation.zig index 1fddda42f91c..2070e0fc8a0e 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -103,7 +103,7 @@ owned_link_dir: ?std.fs.Dir, /// This is for stage1 and should be deleted upon completion of self-hosting. /// Don't use this for anything other than stage1 compatibility. -color: @import("main.zig").Color = .Auto, +color: @import("main.zig").Color = .auto, test_filter: ?[]const u8, test_name_prefix: ?[]const u8, @@ -385,7 +385,7 @@ pub const InitOptions = struct { machine_code_model: std.builtin.CodeModel = .default, clang_preprocessor_mode: ClangPreprocessorMode = .no, /// This is for stage1 and should be deleted upon completion of self-hosting. - color: @import("main.zig").Color = .Auto, + color: @import("main.zig").Color = .auto, test_filter: ?[]const u8 = null, test_name_prefix: ?[]const u8 = null, subsystem: ?std.Target.SubSystem = null, @@ -1179,7 +1179,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor var progress: std.Progress = .{}; var main_progress_node = try progress.start("", null); defer main_progress_node.end(); - if (self.color == .Off) progress.terminal = null; + if (self.color == .off) progress.terminal = null; var c_comp_progress_node = main_progress_node.start("Compile C Objects", self.c_source_files.len); defer c_comp_progress_node.end(); diff --git a/src/main.zig b/src/main.zig index 8a3fb7210242..f2036c31ddcc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,9 +28,9 @@ pub fn fatal(comptime format: []const u8, args: anytype) noreturn { pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB pub const Color = enum { - Auto, - Off, - On, + auto, + off, + on, }; const usage = @@ -380,7 +380,7 @@ fn buildOutputType( run, }, ) !void { - var color: Color = .Auto; + var color: Color = .auto; var optimize_mode: std.builtin.Mode = .Debug; var provided_name: ?[]const u8 = null; var link_mode: ?std.builtin.LinkMode = null; @@ -585,15 +585,9 @@ fn buildOutputType( } i += 1; const next_arg = args[i]; - if (mem.eql(u8, next_arg, "auto")) { - color = .Auto; - } else if (mem.eql(u8, next_arg, "on")) { - color = .On; - } else if (mem.eql(u8, next_arg, "off")) { - color = .Off; - } else { + color = std.meta.stringToEnum(Color, next_arg) orelse { fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg}); - } + }; } else if (mem.eql(u8, arg, "--subsystem")) { if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg}); i += 1; @@ -2374,7 +2368,7 @@ const Fmt = struct { pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { const stderr_file = io.getStdErr(); - var color: Color = .Auto; + var color: Color = .auto; var stdin_flag: bool = false; var check_flag: bool = false; var input_files = ArrayList([]const u8).init(gpa); @@ -2394,15 +2388,9 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { } i += 1; const next_arg = args[i]; - if (mem.eql(u8, next_arg, "auto")) { - color = .Auto; - } else if (mem.eql(u8, next_arg, "on")) { - color = .On; - } else if (mem.eql(u8, next_arg, "off")) { - color = .Off; - } else { + color = std.meta.stringToEnum(Color, next_arg) orelse { fatal("expected [auto|on|off] after --color, found '{}'", .{next_arg}); - } + }; } else if (mem.eql(u8, arg, "--stdin")) { stdin_flag = true; } else if (mem.eql(u8, arg, "--check")) { @@ -2626,9 +2614,9 @@ fn printErrMsgToFile( color: Color, ) !void { const color_on = switch (color) { - .Auto => file.isTty(), - .On => true, - .Off => false, + .auto => file.isTty(), + .on => true, + .off => false, }; const lok_token = parse_error.loc(); const span_first = lok_token;