CMakeTestCUDACompiler.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. set(CMAKE_CUDA_ARCHITECTURES_NATIVE "${_CUDA_ARCHS_OUTPUT}")
  29. list(REMOVE_DUPLICATES CMAKE_CUDA_ARCHITECTURES_NATIVE)
  30. else()
  31. if(NOT _CUDA_ARCHS_RESULT MATCHES "[0-9]+")
  32. set(_CUDA_ARCHS_STATUS " (${_CUDA_ARCHS_RESULT})")
  33. else()
  34. set(_CUDA_ARCHS_STATUS "")
  35. endif()
  36. string(REPLACE "\n" "\n " _CUDA_ARCHS_OUTPUT " ${_CUDA_ARCHS_OUTPUT}")
  37. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  38. "Detecting the CUDA native architecture(s) failed with "
  39. "the following output:\n${_CUDA_ARCHS_OUTPUT}\n\n")
  40. endif()
  41. unset(_CUDA_ARCHS_EXE)
  42. unset(_CUDA_ARCHS_RESULT)
  43. unset(_CUDA_ARCHS_OUTPUT)
  44. endif()
  45. # This file is used by EnableLanguage in cmGlobalGenerator to
  46. # determine that the selected cuda compiler can actually compile
  47. # and link the most basic of programs. If not, a fatal error
  48. # is set and cmake stops processing commands and will not generate
  49. # any makefiles or projects.
  50. if(NOT CMAKE_CUDA_COMPILER_WORKS)
  51. PrintTestCompilerStatus("CUDA")
  52. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.cu
  53. "#ifndef __CUDACC__\n"
  54. "# error \"The CMAKE_CUDA_COMPILER is set to an invalid CUDA compiler\"\n"
  55. "#endif\n"
  56. "int main(){return 0;}\n")
  57. # Clear result from normal variable.
  58. unset(CMAKE_CUDA_COMPILER_WORKS)
  59. # Puts test result in cache variable.
  60. try_compile(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_BINARY_DIR}
  61. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/main.cu
  62. OUTPUT_VARIABLE __CMAKE_CUDA_COMPILER_OUTPUT)
  63. # Move result from cache to normal variable.
  64. set(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_CUDA_COMPILER_WORKS})
  65. unset(CMAKE_CUDA_COMPILER_WORKS CACHE)
  66. if(NOT CMAKE_CUDA_COMPILER_WORKS)
  67. PrintTestCompilerResult(CHECK_FAIL "broken")
  68. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  69. "Determining if the CUDA compiler works failed with "
  70. "the following output:\n${__CMAKE_CUDA_COMPILER_OUTPUT}\n\n")
  71. string(REPLACE "\n" "\n " _output "${__CMAKE_CUDA_COMPILER_OUTPUT}")
  72. message(FATAL_ERROR "The CUDA compiler\n \"${CMAKE_CUDA_COMPILER}\"\n"
  73. "is not able to compile a simple test program.\nIt fails "
  74. "with the following output:\n ${_output}\n\n"
  75. "CMake will not be able to correctly generate this project.")
  76. endif()
  77. PrintTestCompilerResult(CHECK_PASS "works")
  78. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  79. "Determining if the CUDA compiler works passed with "
  80. "the following output:\n${__CMAKE_CUDA_COMPILER_OUTPUT}\n\n")
  81. endif()
  82. # Try to identify the compiler features
  83. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  84. CMAKE_DETERMINE_COMPILE_FEATURES(CUDA)
  85. if("x${CMAKE_CUDA_SIMULATE_ID}" STREQUAL "xMSVC")
  86. set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES}")
  87. set(CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES}")
  88. endif()
  89. # Filter out implicit link libraries that should not be passed unconditionally.
  90. # See CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE in CMakeDetermineCUDACompiler.
  91. list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES ${CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
  92. if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  93. # Remove the CUDA Toolkit include directories from the set of
  94. # implicit system include directories.
  95. # This resolves the issue that NVCC doesn't specify these
  96. # includes as SYSTEM includes when compiling device code, and sometimes
  97. # they contain headers that generate warnings, so let users mark them
  98. # as SYSTEM explicitly
  99. if(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES)
  100. list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_INCLUDE_DIRECTORIES
  101. ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
  102. )
  103. endif()
  104. endif()
  105. # Re-configure to save learned information.
  106. configure_file(
  107. ${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
  108. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
  109. @ONLY
  110. )
  111. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake)
  112. unset(__CMAKE_CUDA_COMPILER_OUTPUT)