llvm-project/offload/cmake/Modules/LibomptargetGetDependencies.cmake
Joel E. Denny 5709506de0
[offload] Fix finding amdgpu/nvptx-arch to generate tests (#135072)
PR #134713, which landed as 79cb6f05da37, causes this on my test
systems:

```
-- Building AMDGPU plugin for dlopened libhsa
-- Not generating AMDGPU tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_AMDGPU_TESTS' to override.
-- Building CUDA plugin for dlopened libcuda
-- Not generating NVIDIA tests, no supported devices detected. Use 'LIBOMPTARGET_FORCE_NVIDIA_TESTS' to override.
```

The problem is it cannot locate amdgpu-arch and nvptx-arch. This patch
enables it to.

I suspect there is more cleanup to do here. amdgpu-arch and nvptx-arch
do not appear to exist as cmake targets anymore, but there is still
cmake code here that looks for those targets.
2025-04-09 15:54:29 -04:00

93 lines
3.5 KiB
CMake

# Try to detect in the system several dependencies required by the different
# components of libomptarget. These are the dependencies we have:
#
# libffi : required to launch target kernels given function and argument
# pointers.
include (FindPackageHandleStandardArgs)
################################################################################
# Looking for LLVM...
################################################################################
if (OPENMP_STANDALONE_BUILD)
# Complete LLVM package is required for building libomptarget
# in an out-of-tree mode.
find_package(LLVM REQUIRED)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVM in: ${LLVM_DIR}")
list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR})
include(AddLLVM)
if(TARGET omptarget)
message(FATAL_ERROR "CMake target 'omptarget' already exists. "
"Use an LLVM installation that doesn't expose its 'omptarget' target.")
endif()
else()
# Note that OPENMP_STANDALONE_BUILD is FALSE, when
# openmp is built with -DLLVM_ENABLE_RUNTIMES="openmp" vs
# -DLLVM_ENABLE_PROJECTS="openmp", but openmp build
# is actually done as a standalone project build with many
# LLVM CMake variables propagated to it.
list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS
${LLVM_MAIN_INCLUDE_DIR} ${LLVM_BINARY_DIR}/include
)
message(STATUS
"Using LLVM include directories: ${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
endif()
################################################################################
# Looking for libffi...
################################################################################
find_package(FFI QUIET)
set(LIBOMPTARGET_DEP_LIBFFI_FOUND ${FFI_FOUND})
################################################################################
# Looking for NVIDIA GPUs...
################################################################################
set(LIBOMPTARGET_DEP_CUDA_ARCH "sm_35")
if(TARGET nvptx-arch)
get_property(LIBOMPTARGET_NVPTX_ARCH TARGET nvptx-arch PROPERTY LOCATION)
else()
find_program(LIBOMPTARGET_NVPTX_ARCH NAMES nvptx-arch
PATHS ${LLVM_TOOLS_BINARY_DIR})
endif()
if(LIBOMPTARGET_NVPTX_ARCH)
execute_process(COMMAND ${LIBOMPTARGET_NVPTX_ARCH}
OUTPUT_VARIABLE LIBOMPTARGET_NVPTX_ARCH_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "\n" ";" nvptx_arch_list "${LIBOMPTARGET_NVPTX_ARCH_OUTPUT}")
if(nvptx_arch_list)
set(LIBOMPTARGET_FOUND_NVIDIA_GPU TRUE)
set(LIBOMPTARGET_NVPTX_DETECTED_ARCH_LIST "${nvptx_arch_list}")
list(GET nvptx_arch_list 0 LIBOMPTARGET_DEP_CUDA_ARCH)
endif()
endif()
################################################################################
# Looking for AMD GPUs...
################################################################################
if(TARGET amdgpu-arch)
get_property(LIBOMPTARGET_AMDGPU_ARCH TARGET amdgpu-arch PROPERTY LOCATION)
else()
find_program(LIBOMPTARGET_AMDGPU_ARCH NAMES amdgpu-arch
PATHS ${LLVM_TOOLS_BINARY_DIR})
endif()
if(LIBOMPTARGET_AMDGPU_ARCH)
execute_process(COMMAND ${LIBOMPTARGET_AMDGPU_ARCH}
OUTPUT_VARIABLE LIBOMPTARGET_AMDGPU_ARCH_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "\n" ";" amdgpu_arch_list "${LIBOMPTARGET_AMDGPU_ARCH_OUTPUT}")
if(amdgpu_arch_list)
set(LIBOMPTARGET_FOUND_AMDGPU_GPU TRUE)
set(LIBOMPTARGET_AMDGPU_DETECTED_ARCH_LIST "${amdgpu_arch_list}")
endif()
endif()
set(OPENMP_PTHREAD_LIB ${LLVM_PTHREAD_LIB})