1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4
5if(CMAKE_HIP_COMPILER_FORCED)
6  # The compiler configuration was forced by the user.
7  # Assume the user has configured all compiler information.
8  set(CMAKE_HIP_COMPILER_WORKS TRUE)
9  return()
10endif()
11
12set(__CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS}")
13string(APPEND CMAKE_HIP_FLAGS " --cuda-host-only")
14
15include(CMakeTestCompilerCommon)
16
17# work around enforced code signing and / or missing executable target type
18set(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
19if(_CMAKE_FEATURE_DETECTION_TARGET_TYPE)
20  set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_CMAKE_FEATURE_DETECTION_TARGET_TYPE})
21endif()
22
23# Remove any cached result from an older CMake version.
24# We now store this in CMakeHIPCompiler.cmake.
25unset(CMAKE_HIP_COMPILER_WORKS CACHE)
26
27# Try to identify the ABI and configure it into CMakeHIPCompiler.cmake
28include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
29CMAKE_DETERMINE_COMPILER_ABI(HIP ${CMAKE_ROOT}/Modules/CMakeHIPCompilerABI.hip)
30if(CMAKE_HIP_ABI_COMPILED)
31  # The compiler worked so skip dedicated test below.
32  set(CMAKE_HIP_COMPILER_WORKS TRUE)
33  message(STATUS "Check for working HIP compiler: ${CMAKE_HIP_COMPILER} - skipped")
34endif()
35
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.
41if(NOT CMAKE_HIP_COMPILER_WORKS)
42  PrintTestCompilerStatus("HIP")
43  __TestCompiler_setTryCompileTargetType()
44  file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testHIPCompiler.hip
45    "#ifndef __HIP__\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 ${CMAKE_BINARY_DIR}
53    ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testHIPCompiler.hip
54    OUTPUT_VARIABLE __CMAKE_HIP_COMPILER_OUTPUT)
55  # Move result from cache to normal variable.
56  set(CMAKE_HIP_COMPILER_WORKS ${CMAKE_HIP_COMPILER_WORKS})
57  unset(CMAKE_HIP_COMPILER_WORKS CACHE)
58  __TestCompiler_restoreTryCompileTargetType()
59  if(NOT CMAKE_HIP_COMPILER_WORKS)
60    PrintTestCompilerResult(CHECK_FAIL "broken")
61    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
62      "Determining if the HIP compiler works failed with "
63      "the following output:\n${__CMAKE_HIP_COMPILER_OUTPUT}\n\n")
64    string(REPLACE "\n" "\n  " _output "${__CMAKE_HIP_COMPILER_OUTPUT}")
65    message(FATAL_ERROR "The HIP compiler\n  \"${CMAKE_HIP_COMPILER}\"\n"
66      "is not able to compile a simple test program.\nIt fails "
67      "with the following output:\n  ${_output}\n\n"
68      "CMake will not be able to correctly generate this project.")
69  endif()
70  PrintTestCompilerResult(CHECK_PASS "works")
71  file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
72    "Determining if the HIP compiler works passed with "
73    "the following output:\n${__CMAKE_HIP_COMPILER_OUTPUT}\n\n")
74endif()
75
76set(CMAKE_HIP_FLAGS "${__CMAKE_HIP_FLAGS}")
77unset(__CMAKE_HIP_FLAGS)
78
79
80# Try to identify the compiler features
81include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
82CMAKE_DETERMINE_COMPILE_FEATURES(HIP)
83
84# Re-configure to save learned information.
85configure_file(
86  ${CMAKE_ROOT}/Modules/CMakeHIPCompiler.cmake.in
87  ${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPCompiler.cmake
88  @ONLY
89  )
90include(${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPCompiler.cmake)
91
92if(CMAKE_HIP_SIZEOF_DATA_PTR)
93  foreach(f ${CMAKE_HIP_ABI_FILES})
94    include(${f})
95  endforeach()
96  unset(CMAKE_HIP_ABI_FILES)
97endif()
98
99set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
100unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
101unset(__CMAKE_HIP_COMPILER_OUTPUT)
102