Răsfoiți Sursa

VS: Add workaround for CUDA compiler PDB location

The CUDA Toolkit Visual Studio Integration does not honor the
`ClCompile.ProgramDataBaseFileName` field when telling `nvcc` how to
invoke `cl`.  Work around this problem by passing `-Xcompiler=-Fd...`
ourselves through `AdditionalOptions`.

Fixes: #17647
Brad King 7 ani în urmă
părinte
comite
134e795fa9

+ 14 - 0
Source/cmVisualStudio10TargetGenerator.cxx

@@ -2690,6 +2690,20 @@ bool cmVisualStudio10TargetGenerator::ComputeCudaOptions(
     cudaOptions.AppendFlagString("AdditionalOptions", "-x cu");
   }
 
+  // Specify the compiler program database file if configured.
+  std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
+  if (!pdb.empty()) {
+    // CUDA does not have a field for this and does not honor the
+    // ProgramDataBaseFileName field in ClCompile.  Work around this
+    // limitation by creating the directory and passing the flag ourselves.
+    std::string const pdbDir = cmSystemTools::GetFilenamePath(pdb);
+    cmSystemTools::MakeDirectory(pdbDir);
+    pdb = this->ConvertPath(pdb, true);
+    ConvertToWindowsSlash(pdb);
+    std::string const clFd = "-Xcompiler=\"-Fd\\\"" + pdb + "\\\"\"";
+    cudaOptions.AppendFlagString("AdditionalOptions", clFd);
+  }
+
   // CUDA automatically passes the proper '--machine' flag to nvcc
   // for the current architecture, but does not reflect this default
   // in the user-visible IDE settings.  Set it explicitly.

+ 4 - 0
Tests/CudaOnly/CMakeLists.txt

@@ -6,3 +6,7 @@ ADD_TEST_MACRO(CudaOnly.LinkSystemDeviceLibraries CudaOnlyLinkSystemDeviceLibrar
 ADD_TEST_MACRO(CudaOnly.ResolveDeviceSymbols CudaOnlyResolveDeviceSymbols)
 ADD_TEST_MACRO(CudaOnly.SeparateCompilation CudaOnlySeparateCompilation)
 ADD_TEST_MACRO(CudaOnly.WithDefs CudaOnlyWithDefs)
+
+if(MSVC)
+  ADD_TEST_MACRO(CudaOnly.PDB CudaOnlyPDB)
+endif()

+ 19 - 0
Tests/CudaOnly/PDB/CMakeLists.txt

@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 3.11)
+project (CudaOnlyPDB CUDA)
+
+add_executable(CudaOnlyPDB main.cu)
+set_target_properties(CudaOnlyPDB PROPERTIES
+  PDB_NAME LinkPDBName
+  PDB_OUTPUT_DIRECTORY LinkPDBDir
+  COMPILE_PDB_NAME CompPDBName
+  COMPILE_PDB_OUTPUT_DIRECTORY CompPDBDir
+  )
+
+set(pdbs
+  ${CMAKE_CURRENT_BINARY_DIR}/CompPDBDir/${CMAKE_CFG_INTDIR}/CompPDBName.pdb
+  ${CMAKE_CURRENT_BINARY_DIR}/LinkPDBDir/${CMAKE_CFG_INTDIR}/LinkPDBName.pdb
+  )
+add_custom_command(TARGET CudaOnlyPDB POST_BUILD
+  COMMAND ${CMAKE_COMMAND} -Dconfig=$<CONFIG> "-Dpdbs=${pdbs}"
+                           -P ${CMAKE_CURRENT_SOURCE_DIR}/check_pdbs.cmake
+  )

+ 10 - 0
Tests/CudaOnly/PDB/check_pdbs.cmake

@@ -0,0 +1,10 @@
+if(NOT "${config}" MATCHES "[Dd][Ee][Bb]")
+  return()
+endif()
+foreach(pdb ${pdbs})
+  if(EXISTS "${pdb}")
+    message(STATUS "PDB Exists: ${pdb}")
+  else()
+    message(SEND_ERROR "PDB MISSING:\n ${pdb}")
+  endif()
+endforeach()

+ 4 - 0
Tests/CudaOnly/PDB/main.cu

@@ -0,0 +1,4 @@
+int main()
+{
+  return 0;
+}