diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2022-06-24 13:44:12 (GMT) |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2022-06-28 14:03:22 (GMT) |
commit | 23bbac941a94391548b57303f9ab371fa5a82b96 (patch) | |
tree | 89904fe6d1db1488414b4c5dae54696a8782bec3 /Source | |
parent | 2b6ef864e0906480e2eab4ed90a273fa629aaa62 (diff) | |
download | CMake-23bbac941a94391548b57303f9ab371fa5a82b96.zip CMake-23bbac941a94391548b57303f9ab371fa5a82b96.tar.gz CMake-23bbac941a94391548b57303f9ab371fa5a82b96.tar.bz2 |
Add cmake_language(GET_MESSAGE_LOG_LEVEL) sub command
The new sub-command writes a string representation of the
current log level to the output variable given to the
sub-command.
Given that the log-level might be set either via the --log-level
command line option or via the CMAKE_MESSAGE_LOG_LEVEL
cache / regular variables, the priority for each of the log level
sources is as follows, with the first one being the highest:
1) --log-level
2) CMAKE_MESSAGE_LOG_LEVEL regular variable
3) CMAKE_MESSAGE_LOG_LEVEL cache variable
4) default log level (STATUS)
Fixes: #23572
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmCMakeLanguageCommand.cxx | 27 | ||||
-rw-r--r-- | Source/cmMakefile.cxx | 23 | ||||
-rw-r--r-- | Source/cmMakefile.h | 1 | ||||
-rw-r--r-- | Source/cmMessageCommand.cxx | 13 | ||||
-rw-r--r-- | Source/cmake.cxx | 51 | ||||
-rw-r--r-- | Source/cmake.h | 3 |
6 files changed, 93 insertions, 25 deletions
diff --git a/Source/cmCMakeLanguageCommand.cxx b/Source/cmCMakeLanguageCommand.cxx index a2aaa2a..287f45b 100644 --- a/Source/cmCMakeLanguageCommand.cxx +++ b/Source/cmCMakeLanguageCommand.cxx @@ -19,10 +19,12 @@ #include "cmGlobalGenerator.h" #include "cmListFileCache.h" #include "cmMakefile.h" +#include "cmMessageType.h" #include "cmRange.h" #include "cmState.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" +#include "cmake.h" namespace { @@ -303,6 +305,27 @@ bool cmCMakeLanguageCommandSET_DEPENDENCY_PROVIDER( return true; } + +bool cmCMakeLanguageCommandGET_MESSAGE_LOG_LEVEL( + std::vector<cmListFileArgument> const& args, cmExecutionStatus& status) +{ + cmMakefile& makefile = status.GetMakefile(); + std::vector<std::string> expandedArgs; + makefile.ExpandArguments(args, expandedArgs); + + if (args.size() < 2 || expandedArgs.size() > 2) { + return FatalError( + status, + "sub-command GET_MESSAGE_LOG_LEVEL expects exactly one argument"); + } + + Message::LogLevel logLevel = makefile.GetCurrentLogLevel(); + std::string outputValue = cmake::LogLevelToString(logLevel); + + const std::string& outputVariable = expandedArgs[1]; + makefile.AddDefinition(outputVariable, outputValue); + return true; +} } bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args, @@ -451,5 +474,9 @@ bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args, return cmCMakeLanguageCommandEVAL(args, status); } + if (expArgs[expArg] == "GET_MESSAGE_LOG_LEVEL") { + return cmCMakeLanguageCommandGET_MESSAGE_LOG_LEVEL(args, status); + } + return FatalError(status, "called with unknown meta-operation"); } diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 97a3db9..62ea427 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -149,6 +149,29 @@ void cmMakefile::IssueMessage(MessageType t, std::string const& text) const this->GetCMakeInstance()->IssueMessage(t, text, this->Backtrace); } +Message::LogLevel cmMakefile::GetCurrentLogLevel() const +{ + const cmake* cmakeInstance = this->GetCMakeInstance(); + + const Message::LogLevel logLevelCliOrDefault = cmakeInstance->GetLogLevel(); + assert("Expected a valid log level here" && + logLevelCliOrDefault != Message::LogLevel::LOG_UNDEFINED); + + Message::LogLevel result = logLevelCliOrDefault; + + // If the log-level was set via the command line option, it takes precedence + // over the CMAKE_MESSAGE_LOG_LEVEL variable. + if (!cmakeInstance->WasLogLevelSetViaCLI()) { + const Message::LogLevel logLevelFromVar = cmake::StringToLogLevel( + this->GetSafeDefinition("CMAKE_MESSAGE_LOG_LEVEL")); + if (logLevelFromVar != Message::LogLevel::LOG_UNDEFINED) { + result = logLevelFromVar; + } + } + + return result; +} + bool cmMakefile::CheckCMP0037(std::string const& targetName, cmStateEnums::TargetType targetType) const { diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index e7b9716..df40c82 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -925,6 +925,7 @@ public: }; void IssueMessage(MessageType t, std::string const& text) const; + Message::LogLevel GetCurrentLogLevel() const; /** Set whether or not to report a CMP0000 violation. */ void SetCheckCMP0000(bool b) { this->CheckCMP0000 = b; } diff --git a/Source/cmMessageCommand.cxx b/Source/cmMessageCommand.cxx index 9628124..fa29ec9 100644 --- a/Source/cmMessageCommand.cxx +++ b/Source/cmMessageCommand.cxx @@ -157,18 +157,7 @@ bool cmMessageCommand(std::vector<std::string> const& args, assert("Message log level expected to be set" && level != Message::LogLevel::LOG_UNDEFINED); - auto desiredLevel = mf.GetCMakeInstance()->GetLogLevel(); - assert("Expected a valid log level here" && - desiredLevel != Message::LogLevel::LOG_UNDEFINED); - - // Command line option takes precedence over the cache variable - if (!mf.GetCMakeInstance()->WasLogLevelSetViaCLI()) { - const auto desiredLevelFromCache = - cmake::StringToLogLevel(mf.GetSafeDefinition("CMAKE_MESSAGE_LOG_LEVEL")); - if (desiredLevelFromCache != Message::LogLevel::LOG_UNDEFINED) { - desiredLevel = desiredLevelFromCache; - } - } + Message::LogLevel desiredLevel = mf.GetCurrentLogLevel(); if (desiredLevel < level) { // Suppress the message diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 026c3d4..efb2520 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -3,6 +3,7 @@ #include "cmake.h" #include <algorithm> +#include <array> #include <cstdio> #include <cstdlib> #include <cstring> @@ -1402,21 +1403,32 @@ void cmake::SetArgs(const std::vector<std::string>& args) #endif } -Message::LogLevel cmake::StringToLogLevel(const std::string& levelStr) -{ - using LevelsPair = std::pair<std::string, Message::LogLevel>; - static const std::vector<LevelsPair> levels = { - { "error", Message::LogLevel::LOG_ERROR }, - { "warning", Message::LogLevel::LOG_WARNING }, - { "notice", Message::LogLevel::LOG_NOTICE }, - { "status", Message::LogLevel::LOG_STATUS }, - { "verbose", Message::LogLevel::LOG_VERBOSE }, - { "debug", Message::LogLevel::LOG_DEBUG }, - { "trace", Message::LogLevel::LOG_TRACE } +namespace { +using LevelsPair = std::pair<cm::string_view, Message::LogLevel>; +using LevelsPairArray = std::array<LevelsPair, 7>; +const LevelsPairArray& getStringToLogLevelPairs() +{ + static const LevelsPairArray levels = { + { { "error", Message::LogLevel::LOG_ERROR }, + { "warning", Message::LogLevel::LOG_WARNING }, + { "notice", Message::LogLevel::LOG_NOTICE }, + { "status", Message::LogLevel::LOG_STATUS }, + { "verbose", Message::LogLevel::LOG_VERBOSE }, + { "debug", Message::LogLevel::LOG_DEBUG }, + { "trace", Message::LogLevel::LOG_TRACE } } }; + return levels; +} +} // namespace + +Message::LogLevel cmake::StringToLogLevel(cm::string_view levelStr) +{ + const LevelsPairArray& levels = getStringToLogLevelPairs(); - const auto levelStrLowCase = cmSystemTools::LowerCase(levelStr); + const auto levelStrLowCase = + cmSystemTools::LowerCase(std::string{ levelStr }); + // NOLINTNEXTLINE(readability-qualified-auto) const auto it = std::find_if(levels.cbegin(), levels.cend(), [&levelStrLowCase](const LevelsPair& p) { return p.first == levelStrLowCase; @@ -1424,6 +1436,21 @@ Message::LogLevel cmake::StringToLogLevel(const std::string& levelStr) return (it != levels.cend()) ? it->second : Message::LogLevel::LOG_UNDEFINED; } +std::string cmake::LogLevelToString(Message::LogLevel level) +{ + const LevelsPairArray& levels = getStringToLogLevelPairs(); + + // NOLINTNEXTLINE(readability-qualified-auto) + const auto it = + std::find_if(levels.cbegin(), levels.cend(), + [&level](const LevelsPair& p) { return p.second == level; }); + const cm::string_view levelStrLowerCase = + (it != levels.cend()) ? it->first : "undefined"; + std::string levelStrUpperCase = + cmSystemTools::UpperCase(std::string{ levelStrLowerCase }); + return levelStrUpperCase; +} + cmake::TraceFormat cmake::StringToTraceFormat(const std::string& traceStr) { using TracePair = std::pair<std::string, TraceFormat>; diff --git a/Source/cmake.h b/Source/cmake.h index 9d3348d..a631647 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -458,7 +458,8 @@ public: //! Get the selected log level for `message()` commands during the cmake run. Message::LogLevel GetLogLevel() const { return this->MessageLogLevel; } void SetLogLevel(Message::LogLevel level) { this->MessageLogLevel = level; } - static Message::LogLevel StringToLogLevel(const std::string& levelStr); + static Message::LogLevel StringToLogLevel(cm::string_view levelStr); + static std::string LogLevelToString(Message::LogLevel level); static TraceFormat StringToTraceFormat(const std::string& levelStr); bool HasCheckInProgress() const |