From 4083bae6906c385f6dde81f78edef6e15d91d1bc Mon Sep 17 00:00:00 2001 From: Nicolas Sterchele Date: Fri, 17 Mar 2023 23:51:39 +0100 Subject: [PATCH 1/3] build: fix tty conf retrieval As part of https://github.com/ziglang/zig/pull/14647 and more specifically this zig commit https://github.com/ziglang/zig/commit/bf73620cbdb4b9ae6088d44bb88daa4a7d84ed70, the color is now communicated via env vars. Signed-off-by: Nicolas Sterchele --- build.zig | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/build.zig b/build.zig index 4f9f0461..23bf488a 100644 --- a/build.zig +++ b/build.zig @@ -532,31 +532,30 @@ pub fn build(b: *Builder) void { std.os.exit(0); } - use_color_escapes = false; - switch (b.color) { - .on => use_color_escapes = true, - .off => use_color_escapes = false, - .auto => { - if (std.io.getStdErr().supportsAnsiEscapeCodes()) { - use_color_escapes = true; - } else if (builtin.os.tag == .windows) { - const w32 = struct { - const WINAPI = std.os.windows.WINAPI; - const DWORD = std.os.windows.DWORD; - const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; - const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12)); - extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque; - extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32; - extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32; - }; - const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE); - var mode: w32.DWORD = 0; - if (w32.GetConsoleMode(handle, &mode) != 0) { - mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING; - use_color_escapes = w32.SetConsoleMode(handle, mode) != 0; - } + if (b.env_map.get("NO_COLOR") != null) { + use_color_escapes = false; + } else if (b.env_map.get("ZIG_DEBUG_COLOR") != null) { + use_color_escapes = true; + } else { + if (std.io.getStdErr().supportsAnsiEscapeCodes()) { + use_color_escapes = true; + } else if (builtin.os.tag == .windows) { + const w32 = struct { + const WINAPI = std.os.windows.WINAPI; + const DWORD = std.os.windows.DWORD; + const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; + const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12)); + extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque; + extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32; + extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32; + }; + const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE); + var mode: w32.DWORD = 0; + if (w32.GetConsoleMode(handle, &mode) != 0) { + mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING; + use_color_escapes = w32.SetConsoleMode(handle, mode) != 0; } - }, + } } if (use_color_escapes) { @@ -756,11 +755,6 @@ const ZiglingStep = struct { zig_args.append("-lc") catch unreachable; } - if (builder.color != .auto) { - zig_args.append("--color") catch unreachable; - zig_args.append(@tagName(builder.color)) catch unreachable; - } - const zig_file = std.fs.path.join(builder.allocator, &[_][]const u8{ if (self.use_healed) "patches/healed" else "exercises", self.exercise.main_file }) catch unreachable; zig_args.append(builder.pathFromRoot(zig_file)) catch unreachable; From 3cc3d03c146ea29e2a9c2b0af4cad58a8807062b Mon Sep 17 00:00:00 2001 From: Nicolas Sterchele Date: Sat, 18 Mar 2023 07:39:05 +0100 Subject: [PATCH 2/3] build: use step options to init step Step's Options introduced from the following PR https://github.com/ziglang/zig/pull/14647 and more precisely this commit https://github.com/ziglang/zig/commit/02381c037221937e941e03c5e7439383dde8a2a1 Also, MakeFn now takes a std.Progress.Node introduces by https://github.com/ziglang/zig/commit/0e078790feaf49964d7a0da3042117ebd10de13b --- build.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/build.zig b/build.zig index 23bf488a..d8c99169 100644 --- a/build.zig +++ b/build.zig @@ -609,7 +609,8 @@ pub fn build(b: *Builder) void { named_verify.dependOn(&verify_step.step); const chain_verify = b.allocator.create(Step) catch unreachable; - chain_verify.* = Step.initNoOp(.custom, b.fmt("chain {s}", .{key}), b.allocator); + const step_options = Step.Options{ .id = .custom, .name = b.fmt("chain {s}", .{key}), .owner = b }; + chain_verify.* = Step.init(step_options); chain_verify.dependOn(&verify_step.step); const named_chain = b.step(b.fmt("{s}_start", .{key}), b.fmt("Check all solutions starting at {s}", .{ex.main_file})); @@ -635,8 +636,9 @@ const ZiglingStep = struct { pub fn create(builder: *Builder, exercise: Exercise, use_healed: bool) *@This() { const self = builder.allocator.create(@This()) catch unreachable; + const step_options = Step.Options{ .id = .custom, .name = exercise.main_file, .owner = builder, .makeFn = make }; self.* = .{ - .step = Step.init(.custom, exercise.main_file, builder.allocator, make), + .step = Step.init(step_options), .exercise = exercise, .builder = builder, .use_healed = use_healed, @@ -644,7 +646,8 @@ const ZiglingStep = struct { return self; } - fn make(step: *Step) anyerror!void { + fn make(step: *Step, prog_node: *std.Progress.Node) anyerror!void { + _ = prog_node; const self = @fieldParentPtr(@This(), "step", step); self.makeInternal() catch { if (self.exercise.hint.len > 0) { From 133dee1bb7f2f98e55ac294f894e7a415920b66a Mon Sep 17 00:00:00 2001 From: Nicolas Sterchele Date: Sat, 18 Mar 2023 14:11:55 +0100 Subject: [PATCH 3/3] build: remove unused header_step --- build.zig | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/build.zig b/build.zig index d8c99169..a4835ab7 100644 --- a/build.zig +++ b/build.zig @@ -565,20 +565,7 @@ pub fn build(b: *Builder) void { reset_text = "\x1b[0m"; } - const header_step = b.addLog( - \\ - \\ _ _ _ - \\ ___(_) __ _| (_)_ __ __ _ ___ - \\ |_ | |/ _' | | | '_ \ / _' / __| - \\ / /| | (_| | | | | | | (_| \__ \ - \\ /___|_|\__, |_|_|_| |_|\__, |___/ - \\ |___/ |___/ - \\ - \\ - , .{}); - const verify_all = b.step("ziglings", "Check all ziglings"); - verify_all.dependOn(&header_step.step); b.default_step = verify_all; var prev_chain_verify = verify_all; @@ -614,7 +601,6 @@ pub fn build(b: *Builder) void { chain_verify.dependOn(&verify_step.step); const named_chain = b.step(b.fmt("{s}_start", .{key}), b.fmt("Check all solutions starting at {s}", .{ex.main_file})); - named_chain.dependOn(&header_step.step); named_chain.dependOn(chain_verify); prev_chain_verify.dependOn(chain_verify);