2 #//===----------------------------------------------------------------------===//
4 #// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 #// See https://llvm.org/LICENSE.txt for license information.
6 #// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 #//===----------------------------------------------------------------------===//
11 # Try to detect in the system several dependencies required by the different
12 # components of libomptarget. These are the dependencies we have:
14 # libffi : required to launch target kernels given function and argument
16 # CUDA : required to control offloading to NVIDIA GPUs.
18 include (FindPackageHandleStandardArgs)
20 ################################################################################
22 ################################################################################
24 if (OPENMP_STANDALONE_BUILD)
25 # Complete LLVM package is required for building libomptarget
26 # in an out-of-tree mode.
27 find_package(LLVM REQUIRED)
28 message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
29 message(STATUS "Using LLVM in: ${LLVM_DIR}")
30 list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
31 list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR})
34 message(FATAL_ERROR "CMake target 'omptarget' already exists. "
35 "Use an LLVM installation that doesn't expose its 'omptarget' target.")
38 # Note that OPENMP_STANDALONE_BUILD is FALSE, when
39 # openmp is built with -DLLVM_ENABLE_RUNTIMES="openmp" vs
40 # -DLLVM_ENABLE_PROJECTS="openmp", but openmp build
41 # is actually done as a standalone project build with many
42 # LLVM CMake variables propagated to it.
43 list(APPEND LIBOMPTARGET_LLVM_INCLUDE_DIRS
44 ${LLVM_MAIN_INCLUDE_DIR} ${LLVM_BINARY_DIR}/include
47 "Using LLVM include directories: ${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
50 ################################################################################
51 # Looking for libffi...
52 ################################################################################
53 find_package(PkgConfig)
55 pkg_check_modules(LIBOMPTARGET_SEARCH_LIBFFI QUIET libffi)
58 LIBOMPTARGET_DEP_LIBFFI_INCLUDE_DIR
62 ${LIBOMPTARGET_SEARCH_LIBFFI_INCLUDEDIR}
63 ${LIBOMPTARGET_SEARCH_LIBFFI_INCLUDE_DIRS}
71 # Don't bother look for the library if the header files were not found.
72 if (LIBOMPTARGET_DEP_LIBFFI_INCLUDE_DIR)
74 LIBOMPTARGET_DEP_LIBFFI_LIBRARIES
78 ${LIBOMPTARGET_SEARCH_LIBFFI_LIBDIR}
79 ${LIBOMPTARGET_SEARCH_LIBFFI_LIBRARY_DIRS}
89 set(LIBOMPTARGET_DEP_LIBFFI_INCLUDE_DIRS ${LIBOMPTARGET_DEP_LIBFFI_INCLUDE_DIR})
90 find_package_handle_standard_args(
91 LIBOMPTARGET_DEP_LIBFFI
93 LIBOMPTARGET_DEP_LIBFFI_LIBRARIES
94 LIBOMPTARGET_DEP_LIBFFI_INCLUDE_DIRS)
97 LIBOMPTARGET_DEP_LIBFFI_INCLUDE_DIRS
98 LIBOMPTARGET_DEP_LIBFFI_LIBRARIES)
100 ################################################################################
101 # Looking for CUDA...
102 ################################################################################
104 find_package(CUDAToolkit QUIET)
105 set(LIBOMPTARGET_DEP_CUDA_FOUND ${CUDAToolkit_FOUND})
107 ################################################################################
108 # Looking for NVIDIA GPUs...
109 ################################################################################
110 set(LIBOMPTARGET_DEP_CUDA_ARCH "sm_35")
112 if(TARGET nvptx-arch)
113 get_property(LIBOMPTARGET_NVPTX_ARCH TARGET nvptx-arch PROPERTY LOCATION)
115 find_program(LIBOMPTARGET_NVPTX_ARCH NAMES nvptx-arch
116 PATHS ${LLVM_TOOLS_BINARY_DIR}/bin)
119 if(LIBOMPTARGET_NVPTX_ARCH)
120 execute_process(COMMAND ${LIBOMPTARGET_NVPTX_ARCH}
121 OUTPUT_VARIABLE LIBOMPTARGET_NVPTX_ARCH_OUTPUT
122 OUTPUT_STRIP_TRAILING_WHITESPACE)
123 string(REPLACE "\n" ";" nvptx_arch_list "${LIBOMPTARGET_NVPTX_ARCH_OUTPUT}")
125 set(LIBOMPTARGET_FOUND_NVIDIA_GPU TRUE)
126 set(LIBOMPTARGET_NVPTX_DETECTED_ARCH_LIST "${nvptx_arch_list}")
127 list(GET nvptx_arch_list 0 LIBOMPTARGET_DEP_CUDA_ARCH)
132 ################################################################################
133 # Looking for AMD GPUs...
134 ################################################################################
136 if(TARGET amdgpu-arch)
137 get_property(LIBOMPTARGET_AMDGPU_ARCH TARGET amdgpu-arch PROPERTY LOCATION)
139 find_program(LIBOMPTARGET_AMDGPU_ARCH NAMES amdgpu-arch
140 PATHS ${LLVM_TOOLS_BINARY_DIR}/bin)
143 if(LIBOMPTARGET_AMDGPU_ARCH)
144 execute_process(COMMAND ${LIBOMPTARGET_AMDGPU_ARCH}
145 OUTPUT_VARIABLE LIBOMPTARGET_AMDGPU_ARCH_OUTPUT
146 OUTPUT_STRIP_TRAILING_WHITESPACE)
147 string(REPLACE "\n" ";" amdgpu_arch_list "${LIBOMPTARGET_AMDGPU_ARCH_OUTPUT}")
149 set(LIBOMPTARGET_FOUND_AMDGPU_GPU TRUE)
150 set(LIBOMPTARGET_AMDGPU_DETECTED_ARCH_LIST "${amdgpu_arch_list}")
154 set(OPENMP_PTHREAD_LIB ${LLVM_PTHREAD_LIB})