Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CMakeTestCUDACompiler.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 if(CMAKE_CUDA_COMPILER_FORCED)
5   # The compiler configuration was forced by the user.
6   # Assume the user has configured all compiler information.
7   set(CMAKE_CUDA_COMPILER_WORKS TRUE)
8   return()
9 endif()
10
11 include(CMakeTestCompilerCommon)
12
13 # Remove any cached result from an older CMake version.
14 # We now store this in CMakeCUDACompiler.cmake.
15 unset(CMAKE_CUDA_COMPILER_WORKS CACHE)
16
17 # Try to identify the ABI and configure it into CMakeCUDACompiler.cmake
18 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
19 CMAKE_DETERMINE_COMPILER_ABI(CUDA ${CMAKE_ROOT}/Modules/CMakeCUDACompilerABI.cu)
20 if(CMAKE_CUDA_ABI_COMPILED)
21   # The compiler worked so skip dedicated test below.
22   set(CMAKE_CUDA_COMPILER_WORKS TRUE)
23   message(STATUS "Check for working CUDA compiler: ${CMAKE_CUDA_COMPILER} - skipped")
24
25   # Run the test binary to detect the native architectures.
26   execute_process(COMMAND "${CMAKE_PLATFORM_INFO_DIR}/CMakeDetermineCompilerABI_CUDA.bin"
27     RESULT_VARIABLE _CUDA_ARCHS_RESULT
28     OUTPUT_VARIABLE _CUDA_ARCHS_OUTPUT
29     ERROR_VARIABLE  _CUDA_ARCHS_OUTPUT
30     OUTPUT_STRIP_TRAILING_WHITESPACE
31     )
32   if(_CUDA_ARCHS_RESULT EQUAL 0)
33     if("$ENV{CMAKE_CUDA_ARCHITECTURES_NATIVE_CLAMP}")
34       # Undocumented hook used by CMake's CI.
35       # Clamp native architecture to version range supported by this CUDA.
36       list(GET CMAKE_CUDA_ARCHITECTURES_ALL 0  _CUDA_ARCH_MIN)
37       list(GET CMAKE_CUDA_ARCHITECTURES_ALL -1 _CUDA_ARCH_MAX)
38       set(CMAKE_CUDA_ARCHITECTURES_NATIVE "")
39       foreach(_CUDA_ARCH IN LISTS _CUDA_ARCHS_OUTPUT)
40         if(_CUDA_ARCH LESS _CUDA_ARCH_MIN)
41           set(_CUDA_ARCH "${_CUDA_ARCH_MIN}")
42         endif()
43         if(_CUDA_ARCH GREATER _CUDA_ARCH_MAX)
44           set(_CUDA_ARCH "${_CUDA_ARCH_MAX}")
45         endif()
46         list(APPEND CMAKE_CUDA_ARCHITECTURES_NATIVE ${_CUDA_ARCH})
47       endforeach()
48       unset(_CUDA_ARCH)
49       unset(_CUDA_ARCH_MIN)
50       unset(_CUDA_ARCH_MAX)
51     else()
52       set(CMAKE_CUDA_ARCHITECTURES_NATIVE "${_CUDA_ARCHS_OUTPUT}")
53     endif()
54     list(REMOVE_DUPLICATES CMAKE_CUDA_ARCHITECTURES_NATIVE)
55     list(TRANSFORM CMAKE_CUDA_ARCHITECTURES_NATIVE APPEND "-real")
56   else()
57     if(NOT _CUDA_ARCHS_RESULT MATCHES "[0-9]+")
58       set(_CUDA_ARCHS_STATUS " (${_CUDA_ARCHS_RESULT})")
59     else()
60       set(_CUDA_ARCHS_STATUS "")
61     endif()
62     string(REPLACE "\n" "\n  " _CUDA_ARCHS_OUTPUT "  ${_CUDA_ARCHS_OUTPUT}")
63     file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
64       "Detecting the CUDA native architecture(s) failed with "
65       "the following output:\n${_CUDA_ARCHS_OUTPUT}\n\n")
66   endif()
67   unset(_CUDA_ARCHS_EXE)
68   unset(_CUDA_ARCHS_RESULT)
69   unset(_CUDA_ARCHS_OUTPUT)
70 endif()
71
72 # This file is used by EnableLanguage in cmGlobalGenerator to
73 # determine that the selected cuda compiler can actually compile
74 # and link the most basic of programs.   If not, a fatal error
75 # is set and cmake stops processing commands and will not generate
76 # any makefiles or projects.
77 if(NOT CMAKE_CUDA_COMPILER_WORKS)
78   PrintTestCompilerStatus("CUDA")
79   string(CONCAT __TestCompiler_testCudaCompilerSource
80     "#ifndef __CUDACC__\n"
81     "# error \"The CMAKE_CUDA_COMPILER is set to an invalid CUDA compiler\"\n"
82     "#endif\n"
83     "int main(){return 0;}\n")
84
85   # Clear result from normal variable.
86   unset(CMAKE_CUDA_COMPILER_WORKS)
87
88   # Puts test result in cache variable.
89   try_compile(CMAKE_CUDA_COMPILER_WORKS
90     SOURCE_FROM_VAR main.cu __TestCompiler_testCudaCompilerSource
91     OUTPUT_VARIABLE __CMAKE_CUDA_COMPILER_OUTPUT)
92   unset(__TestCompiler_testCudaCompilerSource)
93
94   # Move result from cache to normal variable.
95   set(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_CUDA_COMPILER_WORKS})
96   unset(CMAKE_CUDA_COMPILER_WORKS CACHE)
97   if(NOT CMAKE_CUDA_COMPILER_WORKS)
98     PrintTestCompilerResult(CHECK_FAIL "broken")
99     file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
100       "Determining if the CUDA compiler works failed with "
101       "the following output:\n${__CMAKE_CUDA_COMPILER_OUTPUT}\n\n")
102     string(REPLACE "\n" "\n  " _output "${__CMAKE_CUDA_COMPILER_OUTPUT}")
103     message(FATAL_ERROR "The CUDA compiler\n  \"${CMAKE_CUDA_COMPILER}\"\n"
104       "is not able to compile a simple test program.\nIt fails "
105       "with the following output:\n  ${_output}\n\n"
106       "CMake will not be able to correctly generate this project.")
107   endif()
108   PrintTestCompilerResult(CHECK_PASS "works")
109   file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
110     "Determining if the CUDA compiler works passed with "
111     "the following output:\n${__CMAKE_CUDA_COMPILER_OUTPUT}\n\n")
112 endif()
113
114 # Try to identify the compiler features
115 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
116 CMAKE_DETERMINE_COMPILE_FEATURES(CUDA)
117
118 if("x${CMAKE_CUDA_SIMULATE_ID}" STREQUAL "xMSVC")
119   set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES}")
120   set(CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES}")
121 endif()
122
123 # Filter out implicit link libraries that should not be passed unconditionally.
124 # See CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE in CMakeDetermineCUDACompiler.
125 list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES ${CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
126
127 if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
128   # Remove the CUDA Toolkit include directories from the set of
129   # implicit system include directories.
130   # This resolves the issue that NVCC doesn't specify these
131   # includes as SYSTEM includes when compiling device code, and sometimes
132   # they contain headers that generate warnings, so let users mark them
133   # as SYSTEM explicitly
134   if(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
135     list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_INCLUDE_DIRECTORIES
136       ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
137       )
138   endif()
139 endif()
140
141 # Re-configure to save learned information.
142 configure_file(
143   ${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
144   ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
145   @ONLY
146   )
147 include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake)
148
149 unset(__CMAKE_CUDA_COMPILER_OUTPUT)