diff options
author | Brad King <brad.king@kitware.com> | 2019-04-10 17:38:41 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2019-04-17 15:00:44 (GMT) |
commit | fb3370b6a1681190ffd8daf63975c44ce8fc1c49 (patch) | |
tree | 1e837d6bb039320c6f38c60b69c9439abc720e5f /Source/cmLocalGenerator.cxx | |
parent | f621e7fa5df8d35cc379f9f7825f3d75b8489876 (diff) | |
download | CMake-fb3370b6a1681190ffd8daf63975c44ce8fc1c49.zip CMake-fb3370b6a1681190ffd8daf63975c44ce8fc1c49.tar.gz CMake-fb3370b6a1681190ffd8daf63975c44ce8fc1c49.tar.bz2 |
MSVC: Add abstraction for runtime library selection
Replace our hard-coded defaults for `/MD` and `/MDd` with a first-class
abstraction to select the runtime library from an enumeration of logical
names. We've long hesitated to do this because the idea of "runtime
library selection" touches on related concepts on several platforms.
Avoid that scope creep by simply defining an abstraction that applies
only when targeting the MSVC ABI on Windows.
Removing the old default flags requires a policy because existing
projects may rely on string processing to edit them and choose a runtime
library under the old behavior. Add policy CMP0091 to provide
compatibility.
Fixes: #19108
Diffstat (limited to 'Source/cmLocalGenerator.cxx')
-rw-r--r-- | Source/cmLocalGenerator.cxx | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 67763d4..8b51834 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -6,6 +6,7 @@ #include "cmComputeLinkInformation.h" #include "cmCustomCommandGenerator.h" #include "cmGeneratedFileStream.h" +#include "cmGeneratorExpression.h" #include "cmGeneratorExpressionEvaluationFile.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" @@ -1519,8 +1520,40 @@ void cmLocalGenerator::AddLanguageFlags(std::string& flags, flagsVar += "_FLAGS"; this->AddConfigVariableFlags(flags, flagsVar, config); - // Placeholder for possible future per-target flags. - static_cast<void>(target); + // Add MSVC runtime library flags. This is activated by the presence + // of a default selection whether or not it is overridden by a property. + const char* msvcRuntimeLibraryDefault = + this->Makefile->GetDefinition("CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT"); + if (msvcRuntimeLibraryDefault && *msvcRuntimeLibraryDefault) { + const char* msvcRuntimeLibraryValue = + target->GetProperty("MSVC_RUNTIME_LIBRARY"); + if (!msvcRuntimeLibraryValue) { + msvcRuntimeLibraryValue = msvcRuntimeLibraryDefault; + } + cmGeneratorExpression ge; + std::unique_ptr<cmCompiledGeneratorExpression> cge = + ge.Parse(msvcRuntimeLibraryValue); + std::string const msvcRuntimeLibrary = + cge->Evaluate(this, config, false, target); + if (!msvcRuntimeLibrary.empty()) { + if (const char* msvcRuntimeLibraryOptions = + this->Makefile->GetDefinition( + "CMAKE_" + lang + "_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_" + + msvcRuntimeLibrary)) { + this->AppendCompileOptions(flags, msvcRuntimeLibraryOptions); + } else if ((this->Makefile->GetSafeDefinition( + "CMAKE_" + lang + "_COMPILER_ID") == "MSVC" || + this->Makefile->GetSafeDefinition( + "CMAKE_" + lang + "_SIMULATE_ID") == "MSVC") && + !cmSystemTools::GetErrorOccuredFlag()) { + // The compiler uses the MSVC ABI so it needs a known runtime library. + this->IssueMessage(MessageType::FATAL_ERROR, + "MSVC_RUNTIME_LIBRARY value '" + + msvcRuntimeLibrary + "' not known for this " + + lang + " compiler."); + } + } + } } void cmLocalGenerator::AddLanguageFlagsForLinking( |