浏览代码

CUDA: Factor runtime library lookup into helper method

Brad King 5 年之前
父节点
当前提交
4468acb979

+ 12 - 27
Source/cmComputeLinkInformation.cxx

@@ -12,7 +12,6 @@
 #include <cmext/algorithm>
 
 #include "cmComputeLinkDepends.h"
-#include "cmGeneratorExpression.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalGenerator.h"
 #include "cmListFileCache.h"
@@ -587,32 +586,18 @@ void cmComputeLinkInformation::AddImplicitLinkInfo()
 }
 
 void cmComputeLinkInformation::AddRuntimeLinkLibrary(std::string const& lang)
-{ // Add the lang runtime library flags. This is activated by the presence
-  // of a default selection whether or not it is overridden by a property.
-  std::string defaultVar =
-    cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT");
-  cmProp langRuntimeLibraryDefault = this->Makefile->GetDef(defaultVar);
-  if (langRuntimeLibraryDefault && !langRuntimeLibraryDefault->empty()) {
-    cmProp runtimeLibraryValue =
-      this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
-    if (!runtimeLibraryValue) {
-      runtimeLibraryValue = langRuntimeLibraryDefault;
-    }
-
-    std::string runtimeLibrary =
-      cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
-        *runtimeLibraryValue, this->Target->GetLocalGenerator(), this->Config,
-        this->Target));
-    if (!runtimeLibrary.empty()) {
-      if (const char* runtimeLinkOptions = this->Makefile->GetDefinition(
-            "CMAKE_" + lang + "_RUNTIME_LIBRARY_LINK_OPTIONS_" +
-            runtimeLibrary)) {
-        std::vector<std::string> libsVec = cmExpandedList(runtimeLinkOptions);
-        for (std::string const& i : libsVec) {
-          if (!cm::contains(this->ImplicitLinkLibs, i)) {
-            this->AddItem(i, nullptr);
-          }
-        }
+{
+  std::string const& runtimeLibrary =
+    this->Target->GetRuntimeLinkLibrary(lang, this->Config);
+  if (runtimeLibrary.empty()) {
+    return;
+  }
+  if (const char* runtimeLinkOptions = this->Makefile->GetDefinition(
+        "CMAKE_" + lang + "_RUNTIME_LIBRARY_LINK_OPTIONS_" + runtimeLibrary)) {
+    std::vector<std::string> libsVec = cmExpandedList(runtimeLinkOptions);
+    for (std::string const& i : libsVec) {
+      if (!cm::contains(this->ImplicitLinkLibs, i)) {
+        this->AddItem(i, nullptr);
       }
     }
   }

+ 19 - 0
Source/cmGeneratorTarget.cxx

@@ -5710,6 +5710,25 @@ void cmGeneratorTarget::GetTargetVersion(const std::string& property,
   }
 }
 
+std::string cmGeneratorTarget::GetRuntimeLinkLibrary(
+  std::string const& lang, std::string const& config) const
+{
+  // This is activated by the presence of a default selection whether or
+  // not it is overridden by a property.
+  cmProp runtimeLibraryDefault = this->Makefile->GetDef(
+    cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT"));
+  if (!runtimeLibraryDefault || runtimeLibraryDefault->empty()) {
+    return std::string();
+  }
+  cmProp runtimeLibraryValue =
+    this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
+  if (!runtimeLibraryValue) {
+    runtimeLibraryValue = runtimeLibraryDefault;
+  }
+  return cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
+    *runtimeLibraryValue, this->LocalGenerator, config, this));
+}
+
 std::string cmGeneratorTarget::GetFortranModuleDirectory(
   std::string const& working_dir) const
 {

+ 3 - 0
Source/cmGeneratorTarget.h

@@ -783,6 +783,9 @@ public:
                                 const std::string& fallback_property,
                                 int& major, int& minor, int& patch) const;
 
+  std::string GetRuntimeLinkLibrary(std::string const& lang,
+                                    std::string const& config) const;
+
   std::string GetFortranModuleDirectory(std::string const& working_dir) const;
 
   const std::string& GetSourcesProperty() const;

+ 8 - 15
Source/cmVisualStudioGeneratorOptions.cxx

@@ -157,21 +157,14 @@ void cmVisualStudioGeneratorOptions::FixCudaRuntime(cmGeneratorTarget* target)
     this->FlagMap.find("CudaRuntime");
   if (i == this->FlagMap.end()) {
     // User didn't provide am override so get the property value
-    cmProp runtimeLibraryValue = target->GetProperty("CUDA_RUNTIME_LIBRARY");
-    if (runtimeLibraryValue) {
-      std::string cudaRuntime =
-        cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
-          *runtimeLibraryValue, this->LocalGenerator, this->Configuration,
-          target));
-      if (cudaRuntime == "STATIC") {
-        this->AddFlag("CudaRuntime", "Static");
-      }
-      if (cudaRuntime == "SHARED") {
-        this->AddFlag("CudaRuntime", "Shared");
-      }
-      if (cudaRuntime == "NONE") {
-        this->AddFlag("CudaRuntime", "None");
-      }
+    std::string const& cudaRuntime =
+      target->GetRuntimeLinkLibrary("CUDA", this->Configuration);
+    if (cudaRuntime == "STATIC") {
+      this->AddFlag("CudaRuntime", "Static");
+    } else if (cudaRuntime == "SHARED") {
+      this->AddFlag("CudaRuntime", "Shared");
+    } else if (cudaRuntime == "NONE") {
+      this->AddFlag("CudaRuntime", "None");
     } else {
       // nvcc default is static
       this->AddFlag("CudaRuntime", "Static");