CMakeTestCUDACompiler.cmake 4.2 KB

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