diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d1ced66a40..6f19459c14b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,23 @@ if(APPLE) endif() endif() +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 + # 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() + # Figure out the version set(_s "[\\t ]*") # CMake doesn't support \s* file(STRINGS "setup.cfg" _version REGEX "^version${_s}=${_s}")