1 ##===----------------------------------------------------------------------===##
3 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 # See https://llvm.org/LICENSE.txt for license information.
5 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 ##===----------------------------------------------------------------------===##
9 # Build offloading library and related plugins.
11 ##===----------------------------------------------------------------------===##
13 if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
14 message(FATAL_ERROR "Direct configuration not supported, please use parent directory!")
17 # Add cmake directory to search for custom cmake functions.
18 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules ${CMAKE_MODULE_PATH})
20 # Set the path of all resulting libraries to a unified location so that it can
21 # be used for testing.
22 set(LIBOMPTARGET_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
23 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBOMPTARGET_LIBRARY_DIR})
24 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBOMPTARGET_LIBRARY_DIR})
25 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBOMPTARGET_LIBRARY_DIR})
27 if(NOT LLVM_LIBRARY_OUTPUT_INTDIR)
28 set(LIBOMPTARGET_INTDIR ${LIBOMPTARGET_LIBRARY_DIR})
30 set(LIBOMPTARGET_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
34 include(LibomptargetUtils)
36 # Get dependencies for the different components of the project.
37 include(LibomptargetGetDependencies)
39 # LLVM source tree is required at build time for libomptarget
40 if (NOT LIBOMPTARGET_LLVM_INCLUDE_DIRS)
41 message(FATAL_ERROR "Missing definition for LIBOMPTARGET_LLVM_INCLUDE_DIRS")
44 include_directories(${LIBOMPTARGET_LLVM_INCLUDE_DIRS})
46 # This is a list of all the targets that are supported/tested right now.
47 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} aarch64-unknown-linux-gnu")
48 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} aarch64-unknown-linux-gnu-LTO")
49 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} amdgcn-amd-amdhsa")
50 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64le-ibm-linux-gnu")
51 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64le-ibm-linux-gnu-LTO")
52 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64-ibm-linux-gnu")
53 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} powerpc64-ibm-linux-gnu-LTO")
54 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} x86_64-pc-linux-gnu")
55 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} x86_64-pc-linux-gnu-LTO")
56 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} nvptx64-nvidia-cuda")
57 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} nvptx64-nvidia-cuda-LTO")
58 set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} nvptx64-nvidia-cuda-JIT-LTO")
60 # Once the plugins for the different targets are validated, they will be added to
61 # the list of supported targets in the current system.
62 set (LIBOMPTARGET_SYSTEM_TARGETS "")
63 set (LIBOMPTARGET_TESTED_PLUGINS "")
65 # Check whether using debug mode. In debug mode, allow dumping progress
66 # messages at runtime by default. Otherwise, it can be enabled
67 # independently using the LIBOMPTARGET_ENABLE_DEBUG option.
68 string( TOLOWER "${CMAKE_BUILD_TYPE}" LIBOMPTARGET_CMAKE_BUILD_TYPE)
69 if(LIBOMPTARGET_CMAKE_BUILD_TYPE MATCHES debug)
70 option(LIBOMPTARGET_ENABLE_DEBUG "Allow debug output with the environment variable LIBOMPTARGET_DEBUG=1" ON)
72 option(LIBOMPTARGET_ENABLE_DEBUG "Allow debug output with the environment variable LIBOMPTARGET_DEBUG=1" OFF)
74 if(LIBOMPTARGET_ENABLE_DEBUG)
75 add_definitions(-DOMPTARGET_DEBUG)
78 # No exceptions and no RTTI, except if requested.
79 set(offload_compile_flags -fno-exceptions)
80 if(NOT LLVM_ENABLE_RTTI)
81 set(offload_compile_flags ${offload_compile_flags} -fno-rtti)
84 # If LTO is not explicitly disabled we check if we can enable it and do so.
85 set(LIBOMPTARGET_USE_LTO TRUE CACHE BOOL "Use LTO for the offload runtimes if available")
86 if (LIBOMPTARGET_USE_LTO)
87 include(CheckIPOSupported)
88 check_ipo_supported(RESULT use_lto OUTPUT output)
90 set(offload_compile_flags ${offload_compile_flags} -flto)
91 set(offload_link_flags ${offload_link_flags} -flto)
93 message(WARNING "LTO is not supported: ${output}")
97 # OMPT support for libomptarget
98 # Follow host OMPT support and check if host support has been requested.
99 # LIBOMP_HAVE_OMPT_SUPPORT indicates whether host OMPT support has been implemented.
100 # LIBOMP_OMPT_SUPPORT indicates whether host OMPT support has been requested (default is ON).
101 # LIBOMPTARGET_OMPT_SUPPORT indicates whether target OMPT support has been requested (default is ON).
102 set(OMPT_TARGET_DEFAULT FALSE)
103 if ((LIBOMP_HAVE_OMPT_SUPPORT) AND (LIBOMP_OMPT_SUPPORT) AND (NOT WIN32))
104 set (OMPT_TARGET_DEFAULT TRUE)
106 set(LIBOMPTARGET_OMPT_SUPPORT ${OMPT_TARGET_DEFAULT} CACHE BOOL "OMPT-target-support?")
107 if ((OMPT_TARGET_DEFAULT) AND (LIBOMPTARGET_OMPT_SUPPORT))
108 add_definitions(-DOMPT_SUPPORT=1)
109 message(STATUS "OMPT target enabled")
111 set(LIBOMPTARGET_OMPT_SUPPORT FALSE)
112 message(STATUS "OMPT target disabled")
115 pythonize_bool(LIBOMPTARGET_OMPT_SUPPORT)
117 # Check if this build supports the GPU libc.
118 set(LIBC_GPU_SUPPORT FALSE)
119 if("libc" IN_LIST LLVM_ENABLE_RUNTIMES AND (LIBC_GPU_BUILD OR
120 LIBC_GPU_ARCHITECTURES))
121 set(LIBC_GPU_SUPPORT TRUE)
124 set(LIBOMPTARGET_GPU_LIBC_SUPPORT ${LIBC_GPU_SUPPORT} CACHE BOOL
125 "Libomptarget support for the GPU libc")
126 pythonize_bool(LIBOMPTARGET_GPU_LIBC_SUPPORT)
128 set(LIBOMPTARGET_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
129 message(STATUS "OpenMP tools dir in libomptarget: ${LIBOMP_OMP_TOOLS_INCLUDE_DIR}")
130 include_directories(${LIBOMP_OMP_TOOLS_INCLUDE_DIR})
132 # Definitions for testing, for reuse when testing libomptarget-nvptx.
133 set(LIBOMPTARGET_OPENMP_HEADER_FOLDER "${LIBOMP_INCLUDE_DIR}" CACHE STRING
134 "Path to folder containing omp.h")
135 set(LIBOMPTARGET_OPENMP_HOST_RTL_FOLDER "${LIBOMP_LIBRARY_DIR}" CACHE STRING
136 "Path to folder containing libomp.so, and libLLVMSupport.so with profiling enabled")
137 set(LIBOMPTARGET_LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE STRING
138 "Path to folder containing llvm library libomptarget.so")
139 set(LIBOMPTARGET_LLVM_LIBRARY_INTDIR "${LIBOMPTARGET_INTDIR}" CACHE STRING
140 "Path to folder where intermediate libraries will be output")
142 # Build offloading plugins and device RTLs if they are available.
143 add_subdirectory(plugins-nextgen)
144 add_subdirectory(DeviceRTL)
145 add_subdirectory(tools)
147 # Build target agnostic offloading library.
148 set(LIBOMPTARGET_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
149 add_subdirectory(${LIBOMPTARGET_SRC_DIR})
152 add_subdirectory(test)
154 # Add unit tests if GMock/GTest is present
155 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest)
156 if (NOT TARGET llvm_gtest)
157 add_subdirectory(${LLVM_THIRD_PARTY_DIR}/unittest ${CMAKE_CURRENT_BINARY_DIR}/third-party/unittest)
159 add_subdirectory(unittests)