From 884d9de8b7d7201a4461711335ac10aae743777d Mon Sep 17 00:00:00 2001 From: Semyon Kolton Date: Tue, 15 Feb 2022 18:17:24 +0300 Subject: [PATCH] color: Introduce CMAKE_COLOR_DIAGNOSTICS variable Add a variable to control both makefile color messages and compiler color diagnostics. Fixes: #15502 --- Auxiliary/vim/syntax/cmake.vim | 1 + Help/manual/cmake-variables.7.rst | 1 + Help/release/dev/color-diagnostics.rst | 7 +++++ Help/variable/CMAKE_COLOR_DIAGNOSTICS.rst | 35 +++++++++++++++++++++++ Modules/CMakeGenericSystem.cmake | 7 +++-- Modules/Compiler/Clang.cmake | 6 ++++ Modules/Compiler/GNU.cmake | 7 +++++ Source/cmGhsMultiTargetGenerator.cxx | 1 + Source/cmLocalGenerator.cxx | 24 ++++++++++++++++ Source/cmLocalGenerator.h | 1 + Source/cmLocalUnixMakefileGenerator3.cxx | 6 +++- 11 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 Help/release/dev/color-diagnostics.rst create mode 100644 Help/variable/CMAKE_COLOR_DIAGNOSTICS.rst diff --git a/Auxiliary/vim/syntax/cmake.vim b/Auxiliary/vim/syntax/cmake.vim index 381d97c213..703144bbe4 100644 --- a/Auxiliary/vim/syntax/cmake.vim +++ b/Auxiliary/vim/syntax/cmake.vim @@ -705,6 +705,7 @@ syn keyword cmakeVariable contained \ CMAKE_CODEBLOCKS_COMPILER_ID \ CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES \ CMAKE_CODELITE_USE_TARGETS + \ CMAKE_COLOR_DIAGNOSTICS \ CMAKE_COLOR_MAKEFILE \ CMAKE_COMMAND \ CMAKE_COMPILER_2005 diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index 86e4d4c265..5f56f0d359 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -172,6 +172,7 @@ Variables that Change Behavior /variable/CMAKE_CODEBLOCKS_COMPILER_ID /variable/CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES /variable/CMAKE_CODELITE_USE_TARGETS + /variable/CMAKE_COLOR_DIAGNOSTICS /variable/CMAKE_COLOR_MAKEFILE /variable/CMAKE_CONFIGURATION_TYPES /variable/CMAKE_DEPENDS_IN_PROJECT_ONLY diff --git a/Help/release/dev/color-diagnostics.rst b/Help/release/dev/color-diagnostics.rst new file mode 100644 index 0000000000..7f64ba3687 --- /dev/null +++ b/Help/release/dev/color-diagnostics.rst @@ -0,0 +1,7 @@ +color-diagnostics +----------------- + +* The :variable:`CMAKE_COLOR_DIAGNOSTICS` variable was added to control + color diagnostics generated by compilers. This variable also controls + color buildsystem messages with :ref:`Makefile Generators`, replacing + :variable:`CMAKE_COLOR_MAKEFILE`. diff --git a/Help/variable/CMAKE_COLOR_DIAGNOSTICS.rst b/Help/variable/CMAKE_COLOR_DIAGNOSTICS.rst new file mode 100644 index 0000000000..805467a866 --- /dev/null +++ b/Help/variable/CMAKE_COLOR_DIAGNOSTICS.rst @@ -0,0 +1,35 @@ +CMAKE_COLOR_DIAGNOSTICS +----------------------- + +.. versionadded:: 3.24 + +Enable color diagnostics throughout. + +This variable uses three states: ``ON``, ``OFF`` and not defined. + +When not defined: + +* :ref:`Makefile Generators` initialize the :variable:`CMAKE_COLOR_MAKEFILE` + variable to ``ON``. It controls color buildsystem messages. + +* GNU/Clang compilers are not invoked with any color diagnostics flag. + +When ``ON``: + +* :ref:`Makefile Generators` produce color buildsystem messages by default. + :variable:`CMAKE_COLOR_MAKEFILE` is not initialized, but may be + explicitly set to ``OFF`` to disable color buildsystem messages. + +* GNU/Clang compilers are invoked with a flag enabling color diagnostics + (``-fcolor-diagnostics``). + +When ``OFF``: + +* :ref:`Makefile Generators` do not produce color buildsystem messages by + default. :variable:`CMAKE_COLOR_MAKEFILE` is not initialized, but may be + explicitly set to ``ON`` to enable color buildsystem messages. + +* GNU/Clang compilers are invoked with a flag disabling color diagnostics + (``-fno-color-diagnostics``). + +``CMAKE_COLOR_DIAGNOSTICS`` is not defined by default. diff --git a/Modules/CMakeGenericSystem.cmake b/Modules/CMakeGenericSystem.cmake index 9ae68c483c..e54a0287ee 100644 --- a/Modules/CMakeGenericSystem.cmake +++ b/Modules/CMakeGenericSystem.cmake @@ -48,10 +48,11 @@ set (CMAKE_SKIP_INSTALL_RPATH "NO" CACHE BOOL set(CMAKE_VERBOSE_MAKEFILE FALSE CACHE BOOL "If this value is on, makefiles will be generated without the .SILENT directive, and all commands will be echoed to the console during the make. This is useful for debugging only. With Visual Studio IDE projects all commands are done without /nologo.") if(CMAKE_GENERATOR MATCHES "Make") - set(CMAKE_COLOR_MAKEFILE ON CACHE BOOL - "Enable/Disable color output during build." - ) + if(NOT DEFINED CMAKE_COLOR_DIAGNOSTICS) + set(CMAKE_COLOR_MAKEFILE ON CACHE BOOL "Enable/Disable color output during build.") + endif() mark_as_advanced(CMAKE_COLOR_MAKEFILE) + if(DEFINED CMAKE_RULE_MESSAGES) set_property(GLOBAL PROPERTY RULE_MESSAGES ${CMAKE_RULE_MESSAGES}) endif() diff --git a/Modules/Compiler/Clang.cmake b/Modules/Compiler/Clang.cmake index f885eb0234..df115d3aac 100644 --- a/Modules/Compiler/Clang.cmake +++ b/Modules/Compiler/Clang.cmake @@ -114,6 +114,12 @@ else() endif() set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Xclang -include-pch -Xclang -Xclang -include -Xclang ) set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Xclang -emit-pch -Xclang -include -Xclang -x ${__pch_header_${lang}}) + + # '-fcolor-diagnostics' introduced since Clang 2.6 + if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 2.6) + set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS "-fcolor-diagnostics") + set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF "-fno-color-diagnostics") + endif() endmacro() endif() diff --git a/Modules/Compiler/GNU.cmake b/Modules/Compiler/GNU.cmake index 928e7263fc..2eef5323a0 100644 --- a/Modules/Compiler/GNU.cmake +++ b/Modules/Compiler/GNU.cmake @@ -119,4 +119,11 @@ macro(__compiler_gnu lang) set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -include ) set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -x ${__pch_header_${lang}} -include ) endif() + + # '-fdiagnostics-color=always' introduced since GCC 4.9 + # https://gcc.gnu.org/gcc-4.9/changes.html + if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 4.9) + set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS "-fdiagnostics-color=always") + set(CMAKE_${lang}_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF "-fno-diagnostics-color") + endif() endmacro() diff --git a/Source/cmGhsMultiTargetGenerator.cxx b/Source/cmGhsMultiTargetGenerator.cxx index 47cefaeaa4..85ce57f610 100644 --- a/Source/cmGhsMultiTargetGenerator.cxx +++ b/Source/cmGhsMultiTargetGenerator.cxx @@ -187,6 +187,7 @@ void cmGhsMultiTargetGenerator::SetCompilerFlags(std::string const& config, language, config); this->LocalGenerator->AddVisibilityPresetFlags( flags, this->GeneratorTarget, language); + this->LocalGenerator->AddColorDiagnosticsFlags(flags, language); // Append old-style preprocessor definition flags. if (this->Makefile->GetDefineFlags() != " ") { diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 3976c4214e..4fa7d4f546 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1609,6 +1609,7 @@ std::vector> cmLocalGenerator::GetTargetCompileFlags( this->AddCMP0018Flags(compileFlags, target, lang, config); this->AddVisibilityPresetFlags(compileFlags, target, lang); + this->AddColorDiagnosticsFlags(compileFlags, lang); this->AppendFlags(compileFlags, mf->GetDefineFlags()); this->AppendFlags(compileFlags, this->GetFrameworkFlags(lang, config, target)); @@ -2354,6 +2355,29 @@ void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags, } } +void cmLocalGenerator::AddColorDiagnosticsFlags(std::string& flags, + const std::string& lang) +{ + cmValue diag = this->Makefile->GetDefinition("CMAKE_COLOR_DIAGNOSTICS"); + if (diag.IsSet()) { + std::string colorFlagName; + if (diag.IsOn()) { + colorFlagName = + cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_COLOR_DIAGNOSTICS"); + } else { + colorFlagName = + cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF"); + } + + std::vector options; + this->Makefile->GetDefExpandList(colorFlagName, options); + + for (std::string const& option : options) { + this->AppendFlagEscape(flags, option); + } + } +} + void cmLocalGenerator::AddConfigVariableFlags(std::string& flags, const std::string& var, const std::string& config) diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index 115a54a5fc..436a29e932 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -159,6 +159,7 @@ public: cmGeneratorTarget const* target, const std::string& lang, const std::string& config); + void AddColorDiagnosticsFlags(std::string& flags, const std::string& lang); //! Append flags to a string. virtual void AppendFlags(std::string& flags, const std::string& newFlags) const; diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index 0f8cdcabb4..ca5d41c8cc 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -133,7 +133,11 @@ void cmLocalUnixMakefileGenerator3::Generate() // Record whether some options are enabled to avoid checking many // times later. if (!this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) { - this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE"); + if (this->Makefile->IsSet("CMAKE_COLOR_MAKEFILE")) { + this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE"); + } else { + this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_DIAGNOSTICS"); + } } this->SkipPreprocessedSourceRules = this->Makefile->IsOn("CMAKE_SKIP_PREPROCESSED_SOURCE_RULES");