From 68db5fb7e687b1bd665b2aa2b3b4a5e8e3e967b0 Mon Sep 17 00:00:00 2001 From: maxrdz Date: Wed, 17 Jan 2024 20:20:29 -0700 Subject: [PATCH 1/3] CMake: Force GCC/Clang color diagnostics for Ninja Linux users are recommended to use Ninja when generating the build files through CMake. Ninja buffers output, so tools are writing to a pipe, not directly to the terminal, so auto-detection may turn ANSI colored output off conservatively. This forces color diagnostics. --- CMakeLists.txt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d1ced66a40..fefc5fb770c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,25 @@ if(APPLE) endif() endif() +# Enable 'FORCE_ANSI_COLORED_OUTPUT' option when using Ninja. +# Forces GCC/Clang compilers to enable color diagnostics. Enabled by default. +if(CMAKE_GENERATOR STREQUAL "Ninja") + option(FORCE_ANSI_COLORED_OUTPUT "Always produce ANSI-colored output. (GNU/Clang only)" TRUE) + + if(FORCE_ANSI_COLORED_OUTPUT) + set(CMAKE_COLOR_DIAGNOSTICS ON) + # CMake versions 3.24 and below do not support this option, so we have + # to invoke the color diagnostics flags manually. + if(CMAKE_VERSION VERSION_LESS "3.24.0") + if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + add_compile_options(-fdiagnostics-color=always) + elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + add_compile_options(-fcolor-diagnostics) + endif() + endif() + endif() +endif() + # Figure out the version set(_s "[\\t ]*") # CMake doesn't support \s* file(STRINGS "setup.cfg" _version REGEX "^version${_s}=${_s}") From ec964156de86c123465051dfaab9caf4c0941a0a Mon Sep 17 00:00:00 2001 From: maxrdz Date: Thu, 18 Jan 2024 14:58:56 -0700 Subject: [PATCH 2/3] CMake: Always force color diagnostics with Ninja gen --- CMakeLists.txt | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fefc5fb770c..84962eea096 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,21 +41,17 @@ if(APPLE) endif() endif() -# Enable 'FORCE_ANSI_COLORED_OUTPUT' option when using Ninja. -# Forces GCC/Clang compilers to enable color diagnostics. Enabled by default. +# Forces GCC/Clang compilers to enable color diagnostics if using Ninja. if(CMAKE_GENERATOR STREQUAL "Ninja") - option(FORCE_ANSI_COLORED_OUTPUT "Always produce ANSI-colored output. (GNU/Clang only)" TRUE) - - if(FORCE_ANSI_COLORED_OUTPUT) - set(CMAKE_COLOR_DIAGNOSTICS ON) - # CMake versions 3.24 and below do not support this option, so we have - # to invoke the color diagnostics flags manually. - if(CMAKE_VERSION VERSION_LESS "3.24.0") - if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - add_compile_options(-fdiagnostics-color=always) - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - add_compile_options(-fcolor-diagnostics) - endif() + set(CMAKE_COLOR_DIAGNOSTICS ON) + + # CMake versions 3.24 and below do not support this option, so we have + # to invoke the color diagnostics flags manually. + if(CMAKE_VERSION VERSION_LESS "3.24.0") + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + add_compile_options(-fdiagnostics-color=always) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + add_compile_options(-fcolor-diagnostics) endif() endif() endif() From 99da8a7dffa310d95340abd29e40a3cd0b88228e Mon Sep 17 00:00:00 2001 From: maxrdz Date: Mon, 5 Feb 2024 10:49:46 -0700 Subject: [PATCH 3/3] CMake: Use cache variable for force color diagnostics option --- CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 84962eea096..6f19459c14b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,8 +41,10 @@ if(APPLE) endif() endif() -# Forces GCC/Clang compilers to enable color diagnostics if using Ninja. -if(CMAKE_GENERATOR STREQUAL "Ninja") +set(COMPILER_FORCE_COLOR_DIAGNOSTICS TRUE CACHE BOOL "Force GCC/Clang to output ANSI colors.") + +# Forces GCC/Clang compilers to enable color diagnostics. +if(COMPILER_FORCE_COLOR_DIAGNOSTICS) set(CMAKE_COLOR_DIAGNOSTICS ON) # CMake versions 3.24 and below do not support this option, so we have