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 a plugin for a CUDA machine if available.
11 ##===----------------------------------------------------------------------===##
12 set(LIBOMPTARGET_BUILD_CUDA_PLUGIN TRUE CACHE BOOL
13 "Whether to build CUDA plugin")
14 if (NOT LIBOMPTARGET_BUILD_CUDA_PLUGIN)
15 libomptarget_say("Not building CUDA offloading plugin: LIBOMPTARGET_BUILD_CUDA_PLUGIN is false")
19 if (NOT(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(ppc64le)|(aarch64)$" AND CMAKE_SYSTEM_NAME MATCHES "Linux"))
20 libomptarget_say("Not building CUDA offloading plugin: only support CUDA in Linux x86_64, ppc64le, or aarch64 hosts.")
22 elseif (NOT LIBOMPTARGET_DEP_LIBELF_FOUND)
23 libomptarget_say("Not building CUDA offloading plugin: libelf dependency not found.")
27 libomptarget_say("Building CUDA offloading plugin.")
29 # Define the suffix for the runtime messaging dumps.
30 add_definitions(-DTARGET_NAME=CUDA)
33 ${LIBOMPTARGET_DEP_LIBELF_INCLUDE_DIRS}
34 ${LIBOMPTARGET_LLVM_INCLUDE_DIRS}
37 set(LIBOMPTARGET_DLOPEN_LIBCUDA OFF)
38 option(LIBOMPTARGET_FORCE_DLOPEN_LIBCUDA "Build with dlopened libcuda" ${LIBOMPTARGET_DLOPEN_LIBCUDA})
40 set(LIBOMPTARGET_CAN_LINK_LIBCUDA FALSE)
41 if (LIBOMPTARGET_DEP_CUDA_FOUND AND LIBOMPTARGET_DEP_CUDA_DRIVER_FOUND)
42 set(LIBOMPTARGET_CAN_LINK_LIBCUDA TRUE)
45 if (LIBOMPTARGET_CAN_LINK_LIBCUDA AND NOT LIBOMPTARGET_FORCE_DLOPEN_LIBCUDA)
46 libomptarget_say("Building CUDA plugin linked against libcuda")
47 include_directories(${LIBOMPTARGET_DEP_CUDA_INCLUDE_DIRS})
48 add_library(omptarget.rtl.cuda SHARED src/rtl.cpp)
49 set (LIBOMPTARGET_DEP_LIBRARIES ${LIBOMPTARGET_DEP_CUDA_DRIVER_LIBRARIES})
51 libomptarget_say("Building CUDA plugin for dlopened libcuda")
52 include_directories(dynamic_cuda)
53 add_library(omptarget.rtl.cuda SHARED src/rtl.cpp dynamic_cuda/cuda.cpp)
54 set (LIBOMPTARGET_DEP_LIBRARIES ${CMAKE_DL_LIBS})
57 # Install plugin under the lib destination folder.
58 install(TARGETS omptarget.rtl.cuda LIBRARY DESTINATION "${OPENMP_INSTALL_LIBDIR}")
60 target_link_libraries(omptarget.rtl.cuda
63 ${LIBOMPTARGET_DEP_LIBRARIES}
64 ${LIBOMPTARGET_DEP_LIBELF_LIBRARIES}
66 "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/../exports"
69 # Report to the parent scope that we are building a plugin for CUDA.
70 # This controls whether tests are run for the nvptx offloading target
71 # Run them if libcuda is available, or if the user explicitly asked for dlopen
72 # Otherwise this plugin is being built speculatively and there may be no cuda available
73 if (LIBOMPTARGET_CAN_LINK_LIBCUDA OR LIBOMPTARGET_FORCE_DLOPEN_LIBCUDA)
74 libomptarget_say("Enable tests using CUDA plugin")
75 set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS} nvptx64-nvidia-cuda nvptx64-nvidia-cuda-newRTL" PARENT_SCOPE)
77 libomptarget_say("Disabling tests using CUDA plugin as cuda may not be available")