|
| 1 | +# Copyright (C) 2024 Intel Corporation |
| 2 | +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +# See LICENSE.TXT |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + |
| 6 | +set(UR_CUDA_LIBRARY "" CACHE FILEPATH "Path of the CUDA library") |
| 7 | +set(UR_CUDA_INCLUDE_DIR "" CACHE FILEPATH "Directory containing the CUDA Headers") |
| 8 | +set(UR_CUDA_REPO "" CACHE STRING "Github repo to get the CUDA sources from") |
| 9 | +set(UR_CUDA_TAG "" CACHE STRING " GIT tag of the CUDA taken from github repo") |
| 10 | + |
| 11 | +# Copy CUDA loader/headers locally to the build to avoid leaking their path. |
| 12 | +set(CUDA_COPY_DIR ${CMAKE_CURRENT_BINARY_DIR}/cuda_headers) |
| 13 | +if (NOT UR_CUDA_LIBRARY STREQUAL "") |
| 14 | + get_filename_component(CUDA_LIB_NAME "${UR_CUDA_LIBRARY}" NAME) |
| 15 | + set(CUDA_LIBRARY ${CUDA_COPY_DIR}/${CUDA_LIB_NAME}) |
| 16 | + message(STATUS "CUDA Adapter: Copying CUDA loader to local build tree") |
| 17 | + file(COPY ${UR_CUDA_LIBRARY} DESTINATION ${CUDA_COPY_DIR} FOLLOW_SYMLINK_CHAIN) |
| 18 | +endif() |
| 19 | +if (NOT UR_CUDA_INCLUDE_DIR STREQUAL "") |
| 20 | + set(CUDA_INCLUDE_DIR ${CUDA_COPY_DIR}) |
| 21 | + message(STATUS "CUDA Adapter: Copying CUDA headers to local build tree") |
| 22 | + file(COPY ${UR_CUDA_INCLUDE_DIR}/ DESTINATION ${CUDA_COPY_DIR}) |
| 23 | +endif() |
| 24 | + |
| 25 | +if (NOT DEFINED CUDA_LIBRARY OR NOT DEFINED CUDA_INCLUDE_DIR) |
| 26 | + message(STATUS "CUDA Adapter: Download CUDA loader and headers from github.com") |
| 27 | + |
| 28 | + if (UR_CUDA_REPO STREQUAL "") |
| 29 | + set(UR_CUDA_REPO "https://gitlab.com/nvidia/headers/cuda-individual/cudart.git") |
| 30 | + endif() |
| 31 | + if (UR_CUDA_TAG STREQUAL "") |
| 32 | + set(UR_CUDA_TAG cuda-12.5.1) |
| 33 | + endif() |
| 34 | + |
| 35 | + message(STATUS "CUDA Adapter: Will fetch CUDA ${UR_CUDA_TAG} from ${UR_CUDA_REPO}") |
| 36 | + include(FetchContent) |
| 37 | + FetchContent_Declare(cuda-content |
| 38 | + GIT_REPOSITORY ${UR_CUDA_REPO} |
| 39 | + GIT_TAG ${UR_CUDA_TAG} |
| 40 | + ) |
| 41 | + FetchContent_MakeAvailable(cuda-content) |
| 42 | + FetchContent_GetProperties(cuda-content) |
| 43 | + |
| 44 | + set(CUDA_LIBRARY cuda) |
| 45 | + set(CUDA_INCLUDE_DIR ${cuda-content_SOURCE_DIR} CACHE PATH "Path to CUDA headers") |
| 46 | + message(STATUS "CUDA include directory: ${CUDA_INCLUDE_DIR}") |
| 47 | +endif() |
| 48 | + |
| 49 | +add_library(cuda INTERFACE) |
| 50 | +# The MSVC linker does not like / at the start of a path, so to work around this |
| 51 | +# we split it into a link library and a library path, where the path is allowed |
| 52 | +# to have leading /. |
| 53 | +get_filename_component(CUDA_LIBRARY_SRC "${CUDA_LIBRARY}" DIRECTORY) |
| 54 | +get_filename_component(CUDA_LIB_NAME "${CUDA_LIBRARY}" NAME) |
| 55 | +target_link_directories(cuda |
| 56 | + INTERFACE "$<BUILD_INTERFACE:${CUDA_LIBRARY_SRC}>" |
| 57 | + "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" |
| 58 | +) |
| 59 | +target_link_libraries(cuda |
| 60 | + INTERFACE "${CUDA_LIB_NAME}" |
| 61 | +) |
| 62 | + |
| 63 | +add_library(cuda-headers INTERFACE) |
| 64 | +target_include_directories(cuda-headers |
| 65 | + INTERFACE "$<BUILD_INTERFACE:${CUDA_INCLUDE_DIR}>" |
| 66 | + "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" |
| 67 | +) |
0 commit comments