Selaa lähdekoodia

Merge topic 'cuda_mark_toolkit_includes_as_system'

ea659b155d CUDA: Always mark cuda toolkit as system include

Acked-by: Kitware Robot <[email protected]>
Merge-request: !7476
Brad King 3 vuotta sitten
vanhempi
sitoutus
7b9757e507

+ 2 - 4
Modules/CMakeTestCUDACompiler.cmake

@@ -126,10 +126,8 @@ list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES ${CMAKE_CUDA_IMPLICIT_LINK_L
 if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
   # Remove the CUDA Toolkit include directories from the set of
   # implicit system include directories.
-  # This resolves the issue that NVCC doesn't specify these
-  # includes as SYSTEM includes when compiling device code, and sometimes
-  # they contain headers that generate warnings, so let users mark them
-  # as SYSTEM explicitly
+  # CMake will explicitly mark these as SYSTEM to NVCC since it implicitly
+  # adds them as user includes and not system
   if(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
     list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_INCLUDE_DIRECTORIES
       ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}

+ 9 - 0
Source/cmGlobalGenerator.cxx

@@ -1854,6 +1854,15 @@ void cmGlobalGenerator::FinalizeTargetCompileInfo()
         cmExpandedList(standardIncludesStr);
       standardIncludesSet.insert(standardIncludesVec.begin(),
                                  standardIncludesVec.end());
+      if (li == "CUDA") {
+        std::string const& cudaSystemIncludeVar =
+          mf->GetSafeDefinition("CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES");
+        std::vector<std::string> cudaToolkitIncludeVec =
+          cmExpandedList(cudaSystemIncludeVar);
+        standardIncludesSet.insert(cudaToolkitIncludeVec.begin(),
+                                   cudaToolkitIncludeVec.end());
+        mf->AddIncludeDirectories(cudaToolkitIncludeVec);
+      }
     }
     mf->AddSystemIncludeDirectories(standardIncludesSet);
   }

+ 4 - 2
Tests/CudaOnly/CMakeLists.txt

@@ -27,10 +27,12 @@ if(CMake_TEST_CUDA AND NOT CMake_TEST_CUDA STREQUAL "Clang")
   add_cuda_test_macro(CudaOnly.GPUDebugFlag CudaOnlyGPUDebugFlag)
 endif()
 
-# The CUDA only ships the shared version of the toolkit libraries
-# on windows
 if(NOT WIN32)
+  # The CUDA only ships the shared version of the toolkit libraries
+  # on windows
   add_cuda_test_macro(CudaOnly.StaticRuntimePlusToolkit CudaOnlyStaticRuntimePlusToolkit)
+  # `isystem` behaves differently on windows with nvcc
+  add_cuda_test_macro(CudaOnly.ToolkitIsSystemInclude CudaOnlySystemInclude)
 endif()
 
 add_cuda_test_macro(CudaOnly.DeviceLTO CudaOnlyDeviceLTO)

+ 7 - 0
Tests/CudaOnly/ToolkitIsSystemInclude/CMakeLists.txt

@@ -0,0 +1,7 @@
+cmake_minimum_required(VERSION 3.24)
+project(ToolkitIsSystemInclude CUDA)
+
+# Verify that the nvrtc.h that is inside `CMAKE_CURRENT_SOURCE_DIR` is still
+# the first include for `.cu` files.
+add_executable(CudaOnlySystemInclude main.cu)
+target_include_directories(CudaOnlySystemInclude SYSTEM PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)

+ 5 - 0
Tests/CudaOnly/ToolkitIsSystemInclude/include/nvrtc.h

@@ -0,0 +1,5 @@
+#define CMAKE_CUDA_TOOLKIT_IS_SYSTEM 1
+
+int main()
+{
+}

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

@@ -0,0 +1,4 @@
+#include "nvrtc.h"
+#ifndef CMAKE_CUDA_TOOLKIT_IS_SYSTEM
+#  error "Failed to specify the CUDA Toolkit includes as system"
+#endif