forked from eyalroz/cuda-api-wrappers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
166 lines (139 loc) · 5.47 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Does your system only have an older version of CMake? Not to worry!
# CMake offers download-and-use binary packages, with no installation
# necessary... Visit https://cmake.org/download/ and grab one for
# your platform. They are not finicky with library dependencies, so
# compatability is very likely. Also, the package's CMake binary will
# not mistake any other local CMake-related files for its own.
cmake_minimum_required(VERSION 3.25 FATAL_ERROR)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
# -----------------------------------
# Project name, version & build type
# -----------------------------------
if(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# Avoid a build failure when the BUILD_SHARED_LIBS variable is set to true.
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
PROJECT(cuda-api-wrappers
VERSION 0.6.4
DESCRIPTION "Thin C++-flavored wrappers for the CUDA Runtime API"
HOMEPAGE_URL https://github.com/eyalroz/cuda-api-wrappers
LANGUAGES CUDA CXX)
include(GNUInstallDirs)
set(caw_namespace "cuda-api-wrappers")
find_package(CUDAToolkit 9.0 REQUIRED)
find_package(Threads REQUIRED)
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.1)
_CUDAToolkit_find_and_add_import_lib(nvptxcompiler)
_CUDAToolkit_find_and_add_import_lib(nvptxcompiler_static)
endif()
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib/")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib/")
# -------------------
# Our library targets
# -------------------
set(targets runtime-and-driver nvtx rtc)
set(prefixed-targets "")
foreach(wrapper_lib ${targets})
# First ugly hack to facilitate FetchContent use:
# Prefix target names with something project-specific
set(caw_lib "caw_${wrapper_lib}")
add_library(${caw_lib} INTERFACE)
target_compile_features(${caw_lib} INTERFACE cxx_std_11) # This means _at least_ C++11
target_include_directories(
${caw_lib}
INTERFACE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_link_libraries(${caw_lib} INTERFACE CUDA::cudart CUDA::cuda_driver)
# Targets using these libraries should be compiled with C++11 _at least_,
# but it doesn't seem there's a way to express that at the CMake level
# Additional hacks for facilitating FetchContent use,
# which in particular will let you use the same target names as if you had
# imported them with find_package
add_library("${caw_namespace}::${wrapper_lib}" ALIAS ${caw_lib})
list(APPEND prefixed-targets ${caw_lib})
set_target_properties(${caw_lib}
PROPERTIES
EXPORT_NAME ${wrapper_lib}
OUTPUT_NAME ${wrapper_lib}
)
endforeach()
target_link_libraries(caw_rtc INTERFACE cuda-api-wrappers::runtime-and-driver CUDA::nvrtc)
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 11.1)
if (TARGET CUDA::nvptxcompiler)
target_link_libraries(caw_rtc INTERFACE CUDA::nvptxcompiler)
else()
target_link_libraries(caw_rtc INTERFACE CUDA::nvptxcompiler_static)
endif()
target_link_libraries(CUDA::nvptxcompiler_static INTERFACE Threads::Threads) # Because the NVIDIA PTX compiler itself uses threads
endif()
target_link_libraries(caw_nvtx INTERFACE cuda-api-wrappers::runtime-and-driver ${CMAKE_DL_LIBS}) # libnvToolsExt uses dlclose
if(CUDAToolkit_VERSION VERSION_GREATER_EQUAL 10.0)
target_link_libraries(caw_nvtx INTERFACE CUDA::nvtx3)
else()
target_link_libraries(caw_nvtx INTERFACE CUDA::nvToolsExt)
endif()
# Note: This is a bit like a poor man's configure.h file;
# but for two settings I won't bother creating one of those
if(DEFINED CMAKE_USE_PTHREADS_INIT)
target_compile_definitions(caw_nvtx INTERFACE "" "CUDA_API_WRAPPERS_USE_PTHREADS")
elseif(DEFINED CMAKE_USE_WIN32_THREADS_INIT)
target_compile_definitions(caw_nvtx INTERFACE "" "CUDA_API_WRAPPERS_USE_WIN32_THREADS")
endif()
# --------
# Examples
# --------
option(CAW_BUILD_EXAMPLES "Build example programs" OFF)
if (CAW_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
# ------------------------
# Installing the libraries
# ------------------------
configure_file("${PROJECT_SOURCE_DIR}/cmake/cuda-api-wrappers-config.cmake.in"
"${PROJECT_BINARY_DIR}/cuda-api-wrappers-config.cmake" @ONLY)
install(FILES "${PROJECT_BINARY_DIR}/cuda-api-wrappers-config.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cuda-api-wrappers")
install(
TARGETS ${prefixed-targets}
EXPORT cuda-api-wrappers_export
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
export(
EXPORT cuda-api-wrappers_export
NAMESPACE "${caw_namespace}::"
FILE "${PROJECT_BINARY_DIR}/cuda-api-wrappers-targets.cmake"
)
install(
DIRECTORY src/cuda
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING REGEX "\\.(h|hpp|cuh)$"
)
install(
EXPORT cuda-api-wrappers_export
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cuda-api-wrappers"
NAMESPACE "${caw_namespace}::"
FILE "cuda-api-wrappers-targets.cmake"
)
include(CMakePackageConfigHelpers)
# The SameMinorVersion parameter requires CMake 3.11.
# If not supported, fall back to SameMajorVersion.
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.11)
set(COMPAT_SETTING SameMinorVersion)
else()
set(COMPAT_SETTING SameMajorVersion)
endif()
write_basic_package_version_file(
"cuda-api-wrappers-config-version.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY ${COMPAT_SETTING}
)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/cuda-api-wrappers-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cuda-api-wrappers"
)