Skip to content

Commit 6780bff

Browse files
committed
feat: add cuda support
1 parent 3cb79f0 commit 6780bff

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ A very useful function that accepts the same arguments as `target_link_libraries
122122

123123
Similar to `target_include_directories`, but it suppresses the warnings. It is useful if you want to include some external directories directly.
124124

125+
## `find_and_link_cuda` function
126+
127+
A function that links Cuda to the given target. This function automatically links the compiler warnings, so do not link `project_warnings` with your target.
128+
129+
```cmake
130+
add_executable(main_cuda main.cu)
131+
target_compile_features(main_cuda PRIVATE cxx_std_17)
132+
target_link_libraries(main_cuda PRIVATE project_options)
133+
find_and_link_cuda(main_cuda)
134+
```
135+
125136
## Changing the project_options parameters dynamically
126137

127138
It might be useful to change the test and development options on the fly (e.g., to enable sanitizers when running tests). To do this, you can include the `GlobalOptions.cmake`, which adds global options for the arguments of `project_options` function.

src/Cuda.cmake

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# ! find_and_link_cuda
2+
# A function that links Cuda to the given target. This function automatically links the compiler warnings, so do not link `project_warnings` with your target.
3+
#
4+
# # Example
5+
# add_executable(main_cuda main.cu)
6+
# target_compile_features(main_cuda PRIVATE cxx_std_17)
7+
# target_link_libraries(main_cuda PRIVATE project_options)
8+
# find_and_link_cuda(main_cuda)
9+
#
10+
macro(find_and_link_cuda target)
11+
# optional named CUDA_WARNINGS
12+
set(oneValueArgs CUDA_WARNINGS)
13+
cmake_parse_arguments(
14+
_cuda_args
15+
""
16+
"${oneValueArgs}"
17+
""
18+
${ARGN})
19+
20+
# add CUDA to cmake language
21+
enable_language(CUDA)
22+
23+
# use the same C++ standard if not specified
24+
if("${CMAKE_CUDA_STANDARD}" STREQUAL "")
25+
set(CMAKE_CUDA_STANDARD "${CMAKE_CXX_STANDARD}")
26+
endif()
27+
28+
# -fPIC
29+
set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
30+
31+
# We need to explicitly state that we need all CUDA files in the
32+
# ${target} library to be built with -dc as the member functions
33+
# could be called by other libraries and executables
34+
set_target_properties(${target} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
35+
36+
if(APPLE)
37+
# We need to add the path to the driver (libcuda.dylib) as an rpath,
38+
# so that the static cuda runtime can find it at runtime.
39+
set_property(TARGET ${target} PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
40+
endif()
41+
42+
if(${_cuda_args_CUDA_WARNINGS} STREQUAL "")
43+
target_compile_options(
44+
${target}
45+
PRIVATE -Wall
46+
-Wextra
47+
-Wunused
48+
-Wconversion
49+
-Wshadow)
50+
endif()
51+
endmacro()

src/Index.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ include("${ProjectOptions_SRC_DIR}/Vcpkg.cmake")
1010

1111
include("${ProjectOptions_SRC_DIR}/SystemLink.cmake")
1212

13+
include("${ProjectOptions_SRC_DIR}/Cuda.cmake")
14+
1315
#
1416
# Params:
1517
# - WARNINGS_AS_ERRORS: Treat compiler warnings as errors

0 commit comments

Comments
 (0)