|
| 1 | +From 73afdf168e49b580c34971dba5ead6acb8c2c5b0 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Peter0x44 <peter0x44@disroot.org> |
| 3 | +Date: Thu, 13 Aug 2026 02:49:23 -0500 |
| 4 | +Subject: [PATCH 1/3] fix: handle diagnostic colors on Windows consoles |
| 5 | + |
| 6 | +Ccache forces supported compilers to produce colored diagnostics because |
| 7 | +their output is captured instead of written directly to the terminal. It |
| 8 | +later decides whether to strip the resulting ANSI sequences before |
| 9 | +replaying the diagnostics. |
| 10 | + |
| 11 | +On Windows, a real console is color-capable even when TERM is unset: it can |
| 12 | +either process ANSI after virtual terminal processing is enabled or use a |
| 13 | +Win32 console fallback. Treat a handle accepted by GetConsoleMode as |
| 14 | +color-capable independently of TERM. Preserve the existing console mode and, |
| 15 | +like GCC, try to enable processed output and virtual terminal processing. |
| 16 | + |
| 17 | +If virtual terminal processing remains unavailable, strip captured colors as |
| 18 | +a safe fallback. This prevents raw ANSI sequences from reaching legacy |
| 19 | +consoles while preserving colors on consoles that support them. |
| 20 | +--- |
| 21 | + src/ccache/argprocessing.cpp | 42 +++++++++++++++++++++++++++++++++--- |
| 22 | + 1 file changed, 39 insertions(+), 3 deletions(-) |
| 23 | + |
| 24 | +diff --git a/src/ccache/argprocessing.cpp b/src/ccache/argprocessing.cpp |
| 25 | +index af9e0e8c..f86b4bfa 100644 |
| 26 | +--- a/src/ccache/argprocessing.cpp |
| 27 | ++++ b/src/ccache/argprocessing.cpp |
| 28 | +@@ -178,9 +178,43 @@ private: |
| 29 | + bool |
| 30 | + color_output_possible() |
| 31 | + { |
| 32 | ++#ifdef _WIN32 |
| 33 | ++ DWORD mode; |
| 34 | ++ HANDLE stderr_handle = |
| 35 | ++ reinterpret_cast<HANDLE>(_get_osfhandle(STDERR_FILENO)); |
| 36 | ++ return GetConsoleMode(stderr_handle, &mode); |
| 37 | ++#else |
| 38 | + const char* term_env = getenv("TERM"); |
| 39 | + return isatty(STDERR_FILENO) && term_env |
| 40 | + && util::to_lowercase(term_env) != "dumb"; |
| 41 | ++#endif |
| 42 | ++} |
| 43 | ++ |
| 44 | ++bool |
| 45 | ++windows_console_needs_color_translation() |
| 46 | ++{ |
| 47 | ++#ifdef _WIN32 |
| 48 | ++ DWORD mode; |
| 49 | ++ HANDLE stderr_handle = |
| 50 | ++ reinterpret_cast<HANDLE>(_get_osfhandle(STDERR_FILENO)); |
| 51 | ++ if (!GetConsoleMode(stderr_handle, &mode)) { |
| 52 | ++ return false; |
| 53 | ++ } |
| 54 | ++ |
| 55 | ++# ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 56 | ++ if (!(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) { |
| 57 | ++ mode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING; |
| 58 | ++ if (!SetConsoleMode(stderr_handle, mode)) { |
| 59 | ++ return true; |
| 60 | ++ } |
| 61 | ++ } |
| 62 | ++ return false; |
| 63 | ++# else |
| 64 | ++ return true; |
| 65 | ++# endif |
| 66 | ++#else |
| 67 | ++ return false; |
| 68 | ++#endif |
| 69 | + } |
| 70 | + |
| 71 | + bool |
| 72 | +@@ -1825,10 +1859,12 @@ process_args(Context& ctx) |
| 73 | + state.add_common_arg(state.explicit_language); |
| 74 | + } |
| 75 | + |
| 76 | +- args_info.strip_diagnostics_colors = |
| 77 | ++ const bool colors_enabled = |
| 78 | + state.color_diagnostics != ColorDiagnostics::automatic |
| 79 | +- ? state.color_diagnostics == ColorDiagnostics::never |
| 80 | +- : !color_output_possible(); |
| 81 | ++ ? state.color_diagnostics == ColorDiagnostics::always |
| 82 | ++ : color_output_possible(); |
| 83 | ++ args_info.strip_diagnostics_colors = |
| 84 | ++ !colors_enabled || windows_console_needs_color_translation(); |
| 85 | + |
| 86 | + // Since output is redirected, compilers will not color their output by |
| 87 | + // default, so force it explicitly. |
| 88 | +-- |
| 89 | +2.54.0.windows.1 |
| 90 | + |
0 commit comments