1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4if(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()
9endif()
10
11include(CMakeTestCompilerCommon)
12
13# Remove any cached result from an older CMake version.
14# We now store this in CMakeCUDACompiler.cmake.
15unset(CMAKE_CUDA_COMPILER_WORKS CACHE)
16
17# Try to identify the ABI and configure it into CMakeCUDACompiler.cmake
18include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
19CMAKE_DETERMINE_COMPILER_ABI(CUDA ${CMAKE_ROOT}/Modules/CMakeCUDACompilerABI.cu)
20if(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")
24endif()
25
26# This file is used by EnableLanguage in cmGlobalGenerator to
27# determine that the selected cuda compiler can actually compile
28# and link the most basic of programs.   If not, a fatal error
29# is set and cmake stops processing commands and will not generate
30# any makefiles or projects.
31if(NOT CMAKE_CUDA_COMPILER_WORKS)
32  PrintTestCompilerStatus("CUDA")
33  file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.cu
34    "#ifndef __CUDACC__\n"
35    "# error \"The CMAKE_CUDA_COMPILER is set to an invalid CUDA compiler\"\n"
36    "#endif\n"
37    "int main(){return 0;}\n")
38
39  # Clear result from normal variable.
40  unset(CMAKE_CUDA_COMPILER_WORKS)
41
42  # Puts test result in cache variable.
43  try_compile(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_BINARY_DIR}
44    ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.cu
45    OUTPUT_VARIABLE __CMAKE_CUDA_COMPILER_OUTPUT)
46
47  # Move result from cache to normal variable.
48  set(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_CUDA_COMPILER_WORKS})
49  unset(CMAKE_CUDA_COMPILER_WORKS CACHE)
50  if(NOT CMAKE_CUDA_COMPILER_WORKS)
51    PrintTestCompilerResult(CHECK_FAIL "broken")
52    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
53      "Determining if the CUDA compiler works failed with "
54      "the following output:\n${__CMAKE_CUDA_COMPILER_OUTPUT}\n\n")
55    string(REPLACE "\n" "\n  " _output "${__CMAKE_CUDA_COMPILER_OUTPUT}")
56    message(FATAL_ERROR "The CUDA compiler\n  \"${CMAKE_CUDA_COMPILER}\"\n"
57      "is not able to compile a simple test program.\nIt fails "
58      "with the following output:\n  ${_output}\n\n"
59      "CMake will not be able to correctly generate this project.")
60  endif()
61  PrintTestCompilerResult(CHECK_PASS "works")
62  file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
63    "Determining if the CUDA compiler works passed with "
64    "the following output:\n${__CMAKE_CUDA_COMPILER_OUTPUT}\n\n")
65endif()
66
67# Try to identify the compiler features
68include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
69CMAKE_DETERMINE_COMPILE_FEATURES(CUDA)
70
71if("x${CMAKE_CUDA_SIMULATE_ID}" STREQUAL "xMSVC")
72  set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES}")
73  set(CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES}")
74endif()
75
76# Filter out implicit link libraries that should not be passed unconditionally.
77# See CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE in CMakeDetermineCUDACompiler.
78list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES ${CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
79
80if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
81  # Remove the CUDA Toolkit include directories from the set of
82  # implicit system include directories.
83  # This resolves the issue that NVCC doesn't specify these
84  # includes as SYSTEM includes when compiling device code, and sometimes
85  # they contain headers that generate warnings, so let users mark them
86  # as SYSTEM explicitly
87  if(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
88    list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_INCLUDE_DIRECTORIES
89      ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
90      )
91  endif()
92endif()
93
94# Re-configure to save learned information.
95configure_file(
96  ${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
97  ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
98  @ONLY
99  )
100include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake)
101
102unset(__CMAKE_CUDA_COMPILER_OUTPUT)
103