CMakeTestCUDACompiler.cmake 6.0 KB

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