CMakeTestHIPCompiler.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_HIP_COMPILER_FORCED)
  4. # The compiler configuration was forced by the user.
  5. # Assume the user has configured all compiler information.
  6. set(CMAKE_HIP_COMPILER_WORKS TRUE)
  7. return()
  8. endif()
  9. set(__CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS}")
  10. if(CMAKE_HIP_COMPILER_ID STREQUAL "Clang")
  11. string(APPEND CMAKE_HIP_FLAGS " --cuda-host-only")
  12. endif()
  13. include(CMakeTestCompilerCommon)
  14. # work around enforced code signing and / or missing executable target type
  15. set(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
  16. if(_CMAKE_FEATURE_DETECTION_TARGET_TYPE)
  17. set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_CMAKE_FEATURE_DETECTION_TARGET_TYPE})
  18. endif()
  19. # Remove any cached result from an older CMake version.
  20. # We now store this in CMakeHIPCompiler.cmake.
  21. unset(CMAKE_HIP_COMPILER_WORKS CACHE)
  22. # Try to identify the ABI and configure it into CMakeHIPCompiler.cmake
  23. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
  24. CMAKE_DETERMINE_COMPILER_ABI(HIP ${CMAKE_ROOT}/Modules/CMakeHIPCompilerABI.hip)
  25. if(CMAKE_HIP_ABI_COMPILED)
  26. # The compiler worked so skip dedicated test below.
  27. set(CMAKE_HIP_COMPILER_WORKS TRUE)
  28. message(STATUS "Check for working HIP compiler: ${CMAKE_HIP_COMPILER} - skipped")
  29. if(CMAKE_HIP_COMPILER_ID STREQUAL "NVIDIA")
  30. include(Internal/CMakeCUDAArchitecturesNative)
  31. # Run the test binary to get:
  32. # - CMAKE_HIP_ARCHITECTURES_NATIVE
  33. cmake_cuda_architectures_native(HIP)
  34. endif()
  35. endif()
  36. # This file is used by EnableLanguage in cmGlobalGenerator to
  37. # determine that the selected C++ compiler can actually compile
  38. # and link the most basic of programs. If not, a fatal error
  39. # is set and cmake stops processing commands and will not generate
  40. # any makefiles or projects.
  41. if(NOT CMAKE_HIP_COMPILER_WORKS)
  42. PrintTestCompilerStatus("HIP")
  43. __TestCompiler_setTryCompileTargetType()
  44. string(CONCAT __TestCompiler_testHIPCompilerSource
  45. "#if !defined(__HIP__) && !defined(__NVCC__)\n"
  46. "# error \"The CMAKE_HIP_COMPILER is set to a C/CXX compiler\"\n"
  47. "#endif\n"
  48. "int main(){return 0;}\n")
  49. # Clear result from normal variable.
  50. unset(CMAKE_HIP_COMPILER_WORKS)
  51. # Puts test result in cache variable.
  52. try_compile(CMAKE_HIP_COMPILER_WORKS
  53. SOURCE_FROM_VAR testHIPCompiler.hip __TestCompiler_testHIPCompilerSource
  54. OUTPUT_VARIABLE __CMAKE_HIP_COMPILER_OUTPUT)
  55. unset(__TestCompiler_testHIPCompilerSource)
  56. # Move result from cache to normal variable.
  57. set(CMAKE_HIP_COMPILER_WORKS ${CMAKE_HIP_COMPILER_WORKS})
  58. unset(CMAKE_HIP_COMPILER_WORKS CACHE)
  59. __TestCompiler_restoreTryCompileTargetType()
  60. if(NOT CMAKE_HIP_COMPILER_WORKS)
  61. PrintTestCompilerResult(CHECK_FAIL "broken")
  62. string(REPLACE "\n" "\n " _output "${__CMAKE_HIP_COMPILER_OUTPUT}")
  63. message(FATAL_ERROR "The HIP compiler\n \"${CMAKE_HIP_COMPILER}\"\n"
  64. "is not able to compile a simple test program.\nIt fails "
  65. "with the following output:\n ${_output}\n\n"
  66. "CMake will not be able to correctly generate this project.")
  67. endif()
  68. PrintTestCompilerResult(CHECK_PASS "works")
  69. endif()
  70. set(CMAKE_HIP_FLAGS "${__CMAKE_HIP_FLAGS}")
  71. unset(__CMAKE_HIP_FLAGS)
  72. # Try to identify the compiler features
  73. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
  74. CMAKE_DETERMINE_COMPILE_FEATURES(HIP)
  75. if(CMAKE_HIP_COMPILER_ID STREQUAL "NVIDIA")
  76. include(Internal/CMakeNVCCFilterImplicitInfo)
  77. # Match arguments with cmake_nvcc_parse_implicit_info call in CMakeDetermineHIPCompiler.
  78. cmake_nvcc_filter_implicit_info(HIP CMAKE_HIP_CUDA_)
  79. include(Internal/CMakeCUDAFilterImplicitLibs)
  80. # Filter out implicit link libraries that should not be passed unconditionally.
  81. cmake_cuda_filter_implicit_libs(CMAKE_HIP_IMPLICIT_LINK_LIBRARIES)
  82. endif()
  83. # Re-configure to save learned information.
  84. configure_file(
  85. ${CMAKE_ROOT}/Modules/CMakeHIPCompiler.cmake.in
  86. ${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPCompiler.cmake
  87. @ONLY
  88. )
  89. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPCompiler.cmake)
  90. if(CMAKE_HIP_SIZEOF_DATA_PTR)
  91. foreach(f ${CMAKE_HIP_ABI_FILES})
  92. include(${f})
  93. endforeach()
  94. unset(CMAKE_HIP_ABI_FILES)
  95. endif()
  96. set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
  97. unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
  98. unset(__CMAKE_HIP_COMPILER_OUTPUT)