Browse Source

VS: Select the CUDA runtime library

Parse the `-cudart=` option and add a corresponding `CudaRuntime`
field to the generated project file.  Also add a matching `.lib`
to the list of libraries linked.
Brad King 8 years ago
parent
commit
253594d0ae

+ 9 - 0
Source/cmVS10CudaFlagTable.h

@@ -5,5 +5,14 @@ static cmVS7FlagTable cmVS10CudaFlagTable[] = {
   { "AdditionalCompilerOptions", "Xcompiler", "Host compiler options", "",
     cmVS7FlagTable::UserFollowing | cmVS7FlagTable::SpaceAppendable },
 
+  // Select the CUDA runtime library.
+  { "CudaRuntime", "cudart=none", "No CUDA runtime library", "None", 0 },
+  { "CudaRuntime", "cudart=shared", "Shared/dynamic CUDA runtime library",
+    "Shared", 0 },
+  { "CudaRuntime", "cudart=static", "Static CUDA runtime library", "Static",
+    0 },
+  { "CudaRuntime", "cudart", "CUDA runtime library", "",
+    cmVS7FlagTable::UserFollowing },
+
   { 0, 0, 0, 0, 0 }
 };

+ 17 - 2
Source/cmVisualStudio10TargetGenerator.cxx

@@ -2855,8 +2855,10 @@ bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
     this->LocalGenerator, Options::Linker, gg->GetLinkFlagTable(), 0, this));
   Options& linkOptions = *pOptions;
 
-  const std::string& linkLanguage =
-    this->GeneratorTarget->GetLinkerLanguage(config.c_str());
+  cmGeneratorTarget::LinkClosure const* linkClosure =
+    this->GeneratorTarget->GetLinkClosure(config);
+
+  const std::string& linkLanguage = linkClosure->LinkerLanguage;
   if (linkLanguage.empty()) {
     cmSystemTools::Error(
       "CMake can not determine linker language for target: ",
@@ -2911,6 +2913,19 @@ bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
   std::vector<std::string> libVec;
   std::vector<std::string> vsTargetVec;
   this->AddLibraries(cli, libVec, vsTargetVec);
+  if (std::find(linkClosure->Languages.begin(), linkClosure->Languages.end(),
+                "CUDA") != linkClosure->Languages.end()) {
+    switch (this->CudaOptions[config]->GetCudaRuntime()) {
+      case cmVisualStudioGeneratorOptions::CudaRuntimeStatic:
+        libVec.push_back("cudart_static.lib");
+        break;
+      case cmVisualStudioGeneratorOptions::CudaRuntimeShared:
+        libVec.push_back("cudart.lib");
+        break;
+      case cmVisualStudioGeneratorOptions::CudaRuntimeNone:
+        break;
+    }
+  }
   std::string standardLibsVar = "CMAKE_";
   standardLibsVar += linkLanguage;
   standardLibsVar += "_STANDARD_LIBRARIES";

+ 36 - 0
Source/cmVisualStudioGeneratorOptions.cxx

@@ -187,6 +187,27 @@ bool cmVisualStudioGeneratorOptions::UsingSBCS() const
   return false;
 }
 
+cmVisualStudioGeneratorOptions::CudaRuntime
+cmVisualStudioGeneratorOptions::GetCudaRuntime() const
+{
+  std::map<std::string, FlagValue>::const_iterator i =
+    this->FlagMap.find("CudaRuntime");
+  if (i != this->FlagMap.end() && i->second.size() == 1) {
+    std::string const& cudaRuntime = i->second[0];
+    if (cudaRuntime == "Static") {
+      return CudaRuntimeStatic;
+    }
+    if (cudaRuntime == "Shared") {
+      return CudaRuntimeShared;
+    }
+    if (cudaRuntime == "None") {
+      return CudaRuntimeNone;
+    }
+  }
+  // nvcc default is static
+  return CudaRuntimeStatic;
+}
+
 void cmVisualStudioGeneratorOptions::Parse(const char* flags)
 {
   // Parse the input string as a windows command line since the string
@@ -220,6 +241,21 @@ void cmVisualStudioGeneratorOptions::ParseFinish()
     rl += this->FortranRuntimeDLL ? "DLL" : "";
     this->FlagMap["RuntimeLibrary"] = rl;
   }
+
+  if (this->CurrentTool == CudaCompiler) {
+    std::map<std::string, FlagValue>::iterator i =
+      this->FlagMap.find("CudaRuntime");
+    if (i != this->FlagMap.end() && i->second.size() == 1) {
+      std::string& cudaRuntime = i->second[0];
+      if (cudaRuntime == "static") {
+        cudaRuntime = "Static";
+      } else if (cudaRuntime == "shared") {
+        cudaRuntime = "Shared";
+      } else if (cudaRuntime == "none") {
+        cudaRuntime = "None";
+      }
+    }
+  }
 }
 
 void cmVisualStudioGeneratorOptions::PrependInheritedString(

+ 8 - 0
Source/cmVisualStudioGeneratorOptions.h

@@ -67,6 +67,14 @@ public:
   bool UsingUnicode() const;
   bool UsingSBCS() const;
 
+  enum CudaRuntime
+  {
+    CudaRuntimeStatic,
+    CudaRuntimeShared,
+    CudaRuntimeNone
+  };
+  CudaRuntime GetCudaRuntime() const;
+
   bool IsDebug() const;
   bool IsWinRt() const;
   bool IsManaged() const;