| 1 |
cmake_minimum_required(VERSION 3.18) |
| 2 |
project(Complex CXX CUDA) |
| 3 |
#Goal for this example: |
| 4 |
|
| 5 |
#build a cpp dynamic library base |
| 6 |
#build a cuda static library base that uses separable compilation |
| 7 |
|
| 8 |
#build a cuda dynamic library that uses the first dynamic library |
| 9 |
#build a mixed cpp & cuda dynamic library uses all 3 previous libraries |
| 10 |
|
| 11 |
#lastly build a cpp executable that uses this last cuda dynamic library |
| 12 |
|
| 13 |
#this tests that we can properly handle linking cuda and cpp together |
| 14 |
#and also building cpp targets that need cuda implicit libraries |
| 15 |
|
| 16 |
set(CMAKE_CUDA_STANDARD 11) |
| 17 |
set(CMAKE_CXX_STANDARD 11) |
| 18 |
set(CMAKE_CUDA_STANDARD_REQUIRED TRUE) |
| 19 |
set(CMAKE_CXX_STANDARD_REQUIRED TRUE) |
| 20 |
|
| 21 |
add_library(CudaComplexCppBase SHARED dynamic.cpp) |
| 22 |
add_library(CudaComplexSharedLib SHARED dynamic.cu) |
| 23 |
target_link_libraries(CudaComplexSharedLib PUBLIC CudaComplexCppBase) |
| 24 |
|
| 25 |
add_library(CudaComplexSeperableLib STATIC file1.cu file2.cu file3.cu) |
| 26 |
add_library(CudaComplexMixedLib SHARED mixed.cpp mixed.cu) |
| 27 |
target_link_libraries(CudaComplexMixedLib |
| 28 |
PUBLIC CudaComplexSharedLib |
| 29 |
PRIVATE CudaComplexSeperableLib) |
| 30 |
|
| 31 |
add_executable(CudaComplex main.cpp) |
| 32 |
target_link_libraries(CudaComplex PUBLIC CudaComplexMixedLib) |
| 33 |
|
| 34 |
|
| 35 |
set_target_properties(CudaComplexMixedLib |
| 36 |
CudaComplexSeperableLib |
| 37 |
PROPERTIES |
| 38 |
POSITION_INDEPENDENT_CODE ON |
| 39 |
CUDA_SEPARABLE_COMPILATION ON |
| 40 |
) |
| 41 |
set_target_properties(CudaComplexMixedLib |
| 42 |
CudaComplexSharedLib |
| 43 |
PROPERTIES |
| 44 |
CUDA_RUNTIME_LIBRARY shared |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
if(APPLE) |
| 49 |
# Help the static cuda runtime find the driver (libcuda.dyllib) at runtime. |
| 50 |
set_property(TARGET CudaComplex PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}) |
| 51 |
endif() |
| 52 |
|
| 53 |
if(UNIX) |
| 54 |
# Help the shared cuda runtime find libcudart as it is not located |
| 55 |
# in a default system searched location |
| 56 |
find_package(CUDAToolkit REQUIRED) |
| 57 |
set_property(TARGET CudaComplexMixedLib PROPERTY BUILD_RPATH "${CUDAToolkit_LIBRARY_DIR}") |
| 58 |
endif() |