CMakeTestCUDACompiler.cmake 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. include(Internal/CMakeCUDAArchitecturesNative)
  21. # Run the test binary to get:
  22. # - CMAKE_CUDA_ARCHITECTURES_NATIVE
  23. cmake_cuda_architectures_native(CUDA)
  24. endif()
  25. # This file is used by EnableLanguage in cmGlobalGenerator to
  26. # determine that the selected cuda compiler can actually compile
  27. # and link the most basic of programs. If not, a fatal error
  28. # is set and cmake stops processing commands and will not generate
  29. # any makefiles or projects.
  30. if(NOT CMAKE_CUDA_COMPILER_WORKS)
  31. PrintTestCompilerStatus("CUDA")
  32. string(CONCAT __TestCompiler_testCudaCompilerSource
  33. "#ifndef __CUDACC__\n"
  34. "# error \"The CMAKE_CUDA_COMPILER is set to an invalid CUDA compiler\"\n"
  35. "#endif\n"
  36. "int main(){return 0;}\n")
  37. # Clear result from normal variable.
  38. unset(CMAKE_CUDA_COMPILER_WORKS)
  39. # Puts test result in cache variable.
  40. try_compile(CMAKE_CUDA_COMPILER_WORKS
  41. SOURCE_FROM_VAR main.cu __TestCompiler_testCudaCompilerSource
  42. OUTPUT_VARIABLE __CMAKE_CUDA_COMPILER_OUTPUT)
  43. unset(__TestCompiler_testCudaCompilerSource)
  44. # Move result from cache to normal variable.
  45. set(CMAKE_CUDA_COMPILER_WORKS ${CMAKE_CUDA_COMPILER_WORKS})
  46. unset(CMAKE_CUDA_COMPILER_WORKS CACHE)
  47. if(NOT CMAKE_CUDA_COMPILER_WORKS)
  48. PrintTestCompilerResult(CHECK_FAIL "broken")
  49. string(REPLACE "\n" "\n " _output "${__CMAKE_CUDA_COMPILER_OUTPUT}")
  50. message(FATAL_ERROR "The CUDA compiler\n \"${CMAKE_CUDA_COMPILER}\"\n"
  51. "is not able to compile a simple test program.\nIt fails "
  52. "with the following output:\n ${_output}\n\n"
  53. "CMake will not be able to correctly generate this project.")
  54. endif()
  55. PrintTestCompilerResult(CHECK_PASS "works")
  56. endif()
  57. # Try to identify the compiler features
  58. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  59. CMAKE_DETERMINE_COMPILE_FEATURES(CUDA)
  60. if("x${CMAKE_CUDA_SIMULATE_ID}" STREQUAL "xMSVC")
  61. set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES}")
  62. set(CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES "${CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES}")
  63. endif()
  64. include(Internal/CMakeCUDAFilterImplicitLibs)
  65. # Filter out implicit link libraries that should not be passed unconditionally.
  66. cmake_cuda_filter_implicit_libs(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES)
  67. if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
  68. include(Internal/CMakeNVCCFilterImplicitInfo)
  69. # Match arguments with cmake_nvcc_parse_implicit_info call in CMakeDetermineCUDACompiler.
  70. cmake_nvcc_filter_implicit_info(CUDA CMAKE_CUDA_)
  71. endif()
  72. # Re-configure to save learned information.
  73. configure_file(
  74. ${CMAKE_ROOT}/Modules/CMakeCUDACompiler.cmake.in
  75. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake
  76. @ONLY
  77. )
  78. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCUDACompiler.cmake)
  79. unset(__CMAKE_CUDA_COMPILER_OUTPUT)