CMakeHIPRuntime.cmake.in 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. function(_CMAKE_FIND_HIP_RUNTIME )
  4. # Determined when hipcc is the HIP compiler
  5. set(CMAKE_HIP_COMPILER_ROCM_ROOT "@CMAKE_HIP_COMPILER_ROCM_ROOT@")
  6. # Forward facing value that can be provided by the user
  7. set(CMAKE_HIP_COMPILER_TOOLKIT_ROOT @CMAKE_HIP_COMPILER_TOOLKIT_ROOT@)
  8. if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
  9. set(message_on_found TRUE)
  10. endif()
  11. set(explicit_search_only FALSE)
  12. set(rocm_root_dirs )
  13. if(DEFINED CMAKE_HIP_COMPILER_TOOLKIT_ROOT)
  14. set(rocm_root_dirs "${CMAKE_HIP_COMPILER_TOOLKIT_ROOT}")
  15. set(explicit_search_only TRUE)
  16. set(error_message_location "the variable CMAKE_HIP_COMPILER_TOOLKIT_ROOT [\"${CMAKE_HIP_COMPILER_TOOLKIT_ROOT}\"]")
  17. elseif(DEFINED ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT})
  18. set(rocm_root_dirs "$ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT}")
  19. set(explicit_search_only TRUE)
  20. set(error_message_location "CMAKE_HIP_COMPILER_TOOLKIT_ROOT")
  21. set(error_message_location "the environment variable CMAKE_HIP_COMPILER_TOOLKIT_ROOT [\"$ENV{CMAKE_HIP_COMPILER_TOOLKIT_ROOT}\"]")
  22. elseif(DEFINED CMAKE_HIP_COMPILER_ROCM_ROOT)
  23. set(rocm_root_dirs "${CMAKE_HIP_COMPILER_ROCM_ROOT}")
  24. set(explicit_search_only TRUE)
  25. set(error_message_location "the associated hipconfig --rocmpath [\"${CMAKE_HIP_COMPILER_ROCM_ROOT}\"]")
  26. endif()
  27. # Guess on where rocm is installed
  28. if(NOT rocm_root_dirs AND (UNIX AND NOT APPLE))
  29. set(platform_base "/opt/rocm-")
  30. # Finad all default rocm installations
  31. file(GLOB possible_paths "${platform_base}*")
  32. set(versions)
  33. foreach(p ${possible_paths})
  34. # Extract version number from end of string
  35. string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+$" p_version ${p})
  36. if(IS_DIRECTORY ${p} AND p_version)
  37. list(APPEND versions ${p_version})
  38. endif()
  39. endforeach()
  40. # Sort numerically in descending order, so we try the newest versions first.
  41. list(SORT versions COMPARE NATURAL ORDER DESCENDING)
  42. # With a descending list of versions, populate possible paths to search.
  43. set(rocm_root_dirs "/opt/rocm")
  44. foreach(v IN LISTS versions)
  45. list(APPEND rocm_root_dirs "${platform_base}${v}")
  46. endforeach()
  47. endif()
  48. set(search_rel_path "/lib/cmake/hip-lang/")
  49. list(TRANSFORM rocm_root_dirs APPEND "${search_rel_path}")
  50. find_package(hip-lang
  51. CONFIG
  52. PATHS ${rocm_root_dirs}
  53. QUIET
  54. NO_DEFAULT_PATH
  55. )
  56. if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET AND NOT explicit_search_only)
  57. find_package(hip-lang CONFIG QUIET)
  58. endif()
  59. if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
  60. set(CMAKE_HIP_RUNTIME_LIBRARIES_STATIC
  61. ${CMAKE_HIP_RUNTIME_LIBRARIES_STATIC}
  62. ${_CMAKE_HIP_DEVICE_RUNTIME_TARGET} PARENT_SCOPE)
  63. set(CMAKE_HIP_RUNTIME_LIBRARIES_SHARED
  64. ${CMAKE_HIP_RUNTIME_LIBRARIES_SHARED}
  65. ${_CMAKE_HIP_DEVICE_RUNTIME_TARGET} PARENT_SCOPE)
  66. endif()
  67. if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET AND message_on_found)
  68. message(STATUS "Found HIP runtime: ${hip-lang_DIR}")
  69. elseif(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
  70. if(explicit_search_only)
  71. set(error_message "Failed to find the HIP runtime, Could not find hip-lang-config.cmake at the following location(s):\n")
  72. foreach(p IN LISTS rocm_root_dirs)
  73. string(APPEND error_message "\t${p}\n")
  74. endforeach()
  75. string(APPEND "which are computed from the location specified by ${error_message_location}. \
  76. Please specify CMAKE_HIP_COMPILER_TOOLKIT_ROOT to the location of")
  77. message(FATAL_ERROR "${error_message}")
  78. else()
  79. message(FATAL_ERROR
  80. "Failed to find the HIP runtime, Could not find hip-lang-config.cmake.\
  81. Try setting CMAKE_HIP_COMPILER_TOOLKIT_ROOT")
  82. endif()
  83. endif()
  84. endfunction()