CMakeTestCUDACompiler.cmake 4.1 KB

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