summaryrefslogtreecommitdiffstats
path: root/Modules/CMakeHIPRuntime.cmake.in
blob: ade26bb9b17015e45ee14dc00652d5ab8674e442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.


function(_CMAKE_FIND_HIP_RUNTIME )
  # Determined when hipcc is the HIP compiler
  set(_CMAKE_HIP_COMPILER_ROCM_ROOT "@_CMAKE_HIP_COMPILER_ROCM_ROOT@")

  # Forward facing value that can be provided by the user
  set(CMAKE_HIP_COMPILER_TOOLKIT_ROOT @CMAKE_HIP_COMPILER_TOOLKIT_ROOT@)

  if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
    set(message_on_found TRUE)
  endif()

  set(explicit_search_only FALSE)
  set(rocm_root_dirs )
  if(DEFINED CMAKE_HIP_COMPILER_TOOLKIT_ROOT)
    set(rocm_root_dirs "${CMAKE_HIP_COMPILER_TOOLKIT_ROOT}")
    set(explicit_search_only TRUE)
    set(error_message_location "the variable CMAKE_HIP_COMPILER_TOOLKIT_ROOT [\"${CMAKE_HIP_COMPILER_TOOLKIT_ROOT}\"]")
  elseif(DEFINED ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT})
    set(rocm_root_dirs "$ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT}")
    set(explicit_search_only TRUE)
    set(error_message_location "CMAKE_HIP_COMPILER_TOOLKIT_ROOT")
    set(error_message_location "the environment variable CMAKE_HIP_COMPILER_TOOLKIT_ROOT [\"$ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT}\"]")
  elseif(DEFINED _CMAKE_HIP_COMPILER_ROCM_ROOT)
    set(rocm_root_dirs "${_CMAKE_HIP_COMPILER_ROCM_ROOT}")
    set(explicit_search_only TRUE)
    set(error_message_location "the associated hipconfig --rocmpath [\"${_CMAKE_HIP_COMPILER_ROCM_ROOT}\"]")
  endif()

  # Guess on where rocm is installed
  if(NOT rocm_root_dirs AND (UNIX AND NOT APPLE))
    set(platform_base "/opt/rocm-")

    # Finad all default rocm installations
    file(GLOB possible_paths "${platform_base}*")

    set(versions)
    foreach(p ${possible_paths})
      # Extract version number from end of string
      string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+$" p_version ${p})
      if(IS_DIRECTORY ${p} AND p_version)
        list(APPEND versions ${p_version})
      endif()
    endforeach()

    # Sort numerically in descending order, so we try the newest versions first.
    list(SORT versions COMPARE NATURAL ORDER DESCENDING)

    # With a descending list of versions, populate possible paths to search.
    set(rocm_root_dirs "/opt/rocm")
    foreach(v IN LISTS versions)
      list(APPEND rocm_root_dirs "${platform_base}${v}")
    endforeach()
  endif()

  set(search_rel_path "/lib/cmake/hip-lang/")
  list(TRANSFORM rocm_root_dirs APPEND "${search_rel_path}")

  find_package(hip-lang
    CONFIG
    PATHS ${rocm_root_dirs}
    QUIET
    NO_DEFAULT_PATH
  )
  if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET AND NOT explicit_search_only)
    find_package(hip-lang CONFIG QUIET)
  endif()

  if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
    set(CMAKE_HIP_RUNTIME_LIBRARIES_STATIC
      ${CMAKE_HIP_RUNTIME_LIBRARIES_STATIC}
      ${_CMAKE_HIP_DEVICE_RUNTIME_TARGET} PARENT_SCOPE)
    set(CMAKE_HIP_RUNTIME_LIBRARIES_SHARED
      ${CMAKE_HIP_RUNTIME_LIBRARIES_SHARED}
      ${_CMAKE_HIP_DEVICE_RUNTIME_TARGET} PARENT_SCOPE)
  endif()

  if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET AND message_on_found)
    message(STATUS "Found HIP runtime: ${hip-lang_DIR}")
  elseif(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
    if(explicit_search_only)
      set(error_message "Failed to find the HIP runtime, Could not find hip-lang-config.cmake at the following location(s):\n")
      foreach(p IN LISTS rocm_root_dirs)
        string(APPEND error_message "\t${p}\n")
      endforeach()
      string(APPEND "which are computed from the location specified by ${error_message_location}. \
        Please specify CMAKE_HIP_COMPILER_TOOLKIT_ROOT to the location of")
      message(FATAL_ERROR "${error_message}")
    else()
      message(FATAL_ERROR
        "Failed to find the HIP runtime, Could not find hip-lang-config.cmake.\
        Try setting CMAKE_HIP_COMPILER_TOOLKIT_ROOT")
    endif()
  endif()

endfunction()