xref: /llvm-project/offload/cmake/Modules/LibomptargetGetDependencies.cmake (revision c618ae17341315af5fcc97ffd3ed2b19f6d9e412)
1# Try to detect in the system several dependencies required by the different
2# components of libomptarget. These are the dependencies we have:
3#
4# libffi : required to launch target kernels given function and argument
5#          pointers.
6
7include (FindPackageHandleStandardArgs)
8
9################################################################################
10# Looking for LLVM...
11################################################################################
12
13if (OPENMP_STANDALONE_BUILD)
14  # Complete LLVM package is required for building libomptarget
15  # in an out-of-tree mode.
16  find_package(LLVM REQUIRED)
17  message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
18  message(STATUS "Using LLVM in: ${LLVM_DIR}")
19  list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
20  list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR})
21  include(AddLLVM)
22  if(TARGET omptarget)
23    message(FATAL_ERROR "CMake target 'omptarget' already exists. "
24                        "Use an LLVM installation that doesn't expose its 'omptarget' target.")
25  endif()
26else()
27  # Note that OPENMP_STANDALONE_BUILD is FALSE, when
28  # openmp is built with -DLLVM_ENABLE_RUNTIMES="openmp" vs
29  # -DLLVM_ENABLE_PROJECTS="openmp", but openmp build
30  # is actually done as a standalone project build with many
31  # LLVM CMake variables propagated to it.
32  list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS
33    ${LLVM_MAIN_INCLUDE_DIR} ${LLVM_BINARY_DIR}/include
34    )
35  message(STATUS
36    "Using LLVM include directories: ${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
37endif()
38
39################################################################################
40# Looking for libffi...
41################################################################################
42find_package(FFI QUIET)
43set(LIBOMPTARGET_DEP_LIBFFI_FOUND ${FFI_FOUND})
44
45################################################################################
46# Looking for NVIDIA GPUs...
47################################################################################
48set(LIBOMPTARGET_DEP_CUDA_ARCH "sm_35")
49
50if(TARGET nvptx-arch)
51  get_property(LIBOMPTARGET_NVPTX_ARCH TARGET nvptx-arch PROPERTY LOCATION)
52else()
53  find_program(LIBOMPTARGET_NVPTX_ARCH NAMES nvptx-arch
54               PATHS ${LLVM_TOOLS_BINARY_DIR}/bin)
55endif()
56
57if(LIBOMPTARGET_NVPTX_ARCH)
58  execute_process(COMMAND ${LIBOMPTARGET_NVPTX_ARCH}
59                  OUTPUT_VARIABLE LIBOMPTARGET_NVPTX_ARCH_OUTPUT
60                  OUTPUT_STRIP_TRAILING_WHITESPACE)
61  string(REPLACE "\n" ";" nvptx_arch_list "${LIBOMPTARGET_NVPTX_ARCH_OUTPUT}")
62  if(nvptx_arch_list)
63    set(LIBOMPTARGET_FOUND_NVIDIA_GPU TRUE)
64    set(LIBOMPTARGET_NVPTX_DETECTED_ARCH_LIST "${nvptx_arch_list}")
65    list(GET nvptx_arch_list 0 LIBOMPTARGET_DEP_CUDA_ARCH)
66  endif()
67endif()
68
69
70################################################################################
71# Looking for AMD GPUs...
72################################################################################
73
74if(TARGET amdgpu-arch)
75  get_property(LIBOMPTARGET_AMDGPU_ARCH TARGET amdgpu-arch PROPERTY LOCATION)
76else()
77  find_program(LIBOMPTARGET_AMDGPU_ARCH NAMES amdgpu-arch
78               PATHS ${LLVM_TOOLS_BINARY_DIR}/bin)
79endif()
80
81if(LIBOMPTARGET_AMDGPU_ARCH)
82  execute_process(COMMAND ${LIBOMPTARGET_AMDGPU_ARCH}
83                  OUTPUT_VARIABLE LIBOMPTARGET_AMDGPU_ARCH_OUTPUT
84                  OUTPUT_STRIP_TRAILING_WHITESPACE)
85  string(REPLACE "\n" ";" amdgpu_arch_list "${LIBOMPTARGET_AMDGPU_ARCH_OUTPUT}")
86  if(amdgpu_arch_list)
87    set(LIBOMPTARGET_FOUND_AMDGPU_GPU TRUE)
88    set(LIBOMPTARGET_AMDGPU_DETECTED_ARCH_LIST "${amdgpu_arch_list}")
89  endif()
90endif()
91
92set(OPENMP_PTHREAD_LIB ${LLVM_PTHREAD_LIB})
93