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 | |
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')
-rw-r--r-- | Source/cmCoreTryCompile.cxx | 11 | ||||
-rw-r--r-- | Source/cmLocalGenerator.cxx | 37 | ||||
-rw-r--r-- | Source/cmPolicies.h | 5 | ||||
-rw-r--r-- | Source/cmTarget.cxx | 1 |
4 files changed, 51 insertions, 3 deletions
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index dcb1ff5..897f7a8 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -20,6 +20,7 @@ #include "cmSystemTools.h" #include "cmTarget.h" #include "cmVersion.h" +#include "cm_static_string_view.hxx" #include "cmake.h" static std::string const kCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN = @@ -42,6 +43,8 @@ static std::string const kCMAKE_LINK_SEARCH_END_STATIC = "CMAKE_LINK_SEARCH_END_STATIC"; static std::string const kCMAKE_LINK_SEARCH_START_STATIC = "CMAKE_LINK_SEARCH_START_STATIC"; +static std::string const kCMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT = + "CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT"; static std::string const kCMAKE_OSX_ARCHITECTURES = "CMAKE_OSX_ARCHITECTURES"; static std::string const kCMAKE_OSX_DEPLOYMENT_TARGET = "CMAKE_OSX_DEPLOYMENT_TARGET"; @@ -500,6 +503,13 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, fprintf(fout, "set(CMAKE_MODULE_PATH \"%s\")\n", def); } + /* Set MSVC runtime library policy to match our selection. */ + if (const char* msvcRuntimeLibraryDefault = + this->Makefile->GetDefinition(kCMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT)) { + fprintf(fout, "cmake_policy(SET CMP0091 %s)\n", + *msvcRuntimeLibraryDefault ? "NEW" : "OLD"); + } + std::string projectLangs; for (std::string const& li : testLangs) { projectLangs += " " + li; @@ -660,6 +670,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, vars.insert(kCMAKE_SYSROOT_COMPILE); vars.insert(kCMAKE_SYSROOT_LINK); vars.insert(kCMAKE_WARN_DEPRECATED); + vars.emplace("CMAKE_MSVC_RUNTIME_LIBRARY"_s); if (const char* varListStr = this->Makefile->GetDefinition( kCMAKE_TRY_COMPILE_PLATFORM_VARIABLES)) { 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( diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 02a6295..113dd35 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -267,7 +267,10 @@ class cmMakefile; 15, 0, cmPolicies::WARN) \ SELECT(POLICY, CMP0090, \ "export(PACKAGE) does not populate package registry by default.", 3, \ - 15, 0, cmPolicies::WARN) + 15, 0, cmPolicies::WARN) \ + SELECT(POLICY, CMP0091, \ + "MSVC runtime library flags are selected by an abstraction.", 3, 15, \ + 0, cmPolicies::WARN) #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1) #define CM_FOR_EACH_POLICY_ID(POLICY) \ diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index dc9b6d2..9598a3f 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -304,6 +304,7 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, InitProperty("AUTORCC_OPTIONS", nullptr); InitProperty("LINK_DEPENDS_NO_SHARED", nullptr); InitProperty("LINK_INTERFACE_LIBRARIES", nullptr); + InitProperty("MSVC_RUNTIME_LIBRARY", nullptr); InitProperty("WIN32_EXECUTABLE", nullptr); InitProperty("MACOSX_BUNDLE", nullptr); InitProperty("MACOSX_RPATH", nullptr); |