Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions lib/std/special/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
Expand Down Expand Up @@ -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:
\\
Expand Down
6 changes: 3 additions & 3 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 12 additions & 24 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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")) {
Expand Down Expand Up @@ -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;
Expand Down