color: Introduce CMAKE_COLOR_DIAGNOSTICS variable

Add a variable to control both makefile color messages and compiler
color diagnostics.

Fixes: #15502
This commit is contained in:
Semyon Kolton 2022-02-15 18:17:24 +03:00 committed by Brad King
parent 2ac3db2d42
commit 884d9de8b7
11 changed files with 92 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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`.

View File

@ -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.

View File

@ -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()

View File

@ -114,6 +114,12 @@ else()
endif()
set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Xclang -include-pch -Xclang <PCH_FILE> -Xclang -include -Xclang <PCH_HEADER>)
set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Xclang -emit-pch -Xclang -include -Xclang <PCH_HEADER> -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()

View File

@ -119,4 +119,11 @@ macro(__compiler_gnu lang)
set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -include <PCH_HEADER>)
set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -x ${__pch_header_${lang}} -include <PCH_HEADER>)
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()

View File

@ -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() != " ") {

View File

@ -1609,6 +1609,7 @@ std::vector<BT<std::string>> 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<std::string> 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)

View File

@ -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;

View File

@ -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");