Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CMakeTestHIPCompiler.cmake
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
5 if(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()
10 endif()
11
12 set(__CMAKE_HIP_FLAGS "${CMAKE_HIP_FLAGS}")
13 string(APPEND CMAKE_HIP_FLAGS " --cuda-host-only")
14
15 include(CMakeTestCompilerCommon)
16
17 # work around enforced code signing and / or missing executable target type
18 set(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
19 if(_CMAKE_FEATURE_DETECTION_TARGET_TYPE)
20   set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_CMAKE_FEATURE_DETECTION_TARGET_TYPE})
21 endif()
22
23 # Remove any cached result from an older CMake version.
24 # We now store this in CMakeHIPCompiler.cmake.
25 unset(CMAKE_HIP_COMPILER_WORKS CACHE)
26
27 # Try to identify the ABI and configure it into CMakeHIPCompiler.cmake
28 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
29 CMAKE_DETERMINE_COMPILER_ABI(HIP ${CMAKE_ROOT}/Modules/CMakeHIPCompilerABI.hip)
30 if(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")
34 endif()
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.
41 if(NOT CMAKE_HIP_COMPILER_WORKS)
42   PrintTestCompilerStatus("HIP")
43   __TestCompiler_setTryCompileTargetType()
44   string(CONCAT __TestCompiler_testHIPCompilerSource
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
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     file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
63       "Determining if the HIP compiler works failed with "
64       "the following output:\n${__CMAKE_HIP_COMPILER_OUTPUT}\n\n")
65     string(REPLACE "\n" "\n  " _output "${__CMAKE_HIP_COMPILER_OUTPUT}")
66     message(FATAL_ERROR "The HIP compiler\n  \"${CMAKE_HIP_COMPILER}\"\n"
67       "is not able to compile a simple test program.\nIt fails "
68       "with the following output:\n  ${_output}\n\n"
69       "CMake will not be able to correctly generate this project.")
70   endif()
71   PrintTestCompilerResult(CHECK_PASS "works")
72   file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
73     "Determining if the HIP compiler works passed with "
74     "the following output:\n${__CMAKE_HIP_COMPILER_OUTPUT}\n\n")
75 endif()
76
77 set(CMAKE_HIP_FLAGS "${__CMAKE_HIP_FLAGS}")
78 unset(__CMAKE_HIP_FLAGS)
79
80
81 # Try to identify the compiler features
82 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompileFeatures.cmake)
83 CMAKE_DETERMINE_COMPILE_FEATURES(HIP)
84
85 # Re-configure to save learned information.
86 configure_file(
87   ${CMAKE_ROOT}/Modules/CMakeHIPCompiler.cmake.in
88   ${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPCompiler.cmake
89   @ONLY
90   )
91 include(${CMAKE_PLATFORM_INFO_DIR}/CMakeHIPCompiler.cmake)
92
93 if(CMAKE_HIP_SIZEOF_DATA_PTR)
94   foreach(f ${CMAKE_HIP_ABI_FILES})
95     include(${f})
96   endforeach()
97   unset(CMAKE_HIP_ABI_FILES)
98 endif()
99
100 set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE})
101 unset(__CMAKE_SAVED_TRY_COMPILE_TARGET_TYPE)
102 unset(__CMAKE_HIP_COMPILER_OUTPUT)