Skip to content

Commit 8195bdf

Browse files
authored
Merge pull request #49 from cpp-best-practices/cuda
2 parents 69949c8 + 7c9120d commit 8195bdf

5 files changed

Lines changed: 108 additions & 9 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ It gets the following named parameters that can have different values in front o
102102
- `MSVC_WARNINGS`: Override the defaults for the MSVC warnings
103103
- `CLANG_WARNINGS`: Override the defaults for the CLANG warnings
104104
- `GCC_WARNINGS`: Override the defaults for the GCC warnings
105+
- `CUDA_WARNINGS`: Override the defaults for the CUDA warnings
105106
- `CONAN_OPTIONS`: Extra Conan options
106107

107108
## `run_vcpkg` function
@@ -125,6 +126,17 @@ A very useful function that accepts the same arguments as `target_link_libraries
125126

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

129+
## `target_link_cuda` function
130+
131+
A function that links Cuda to the given target.
132+
133+
```cmake
134+
add_executable(main_cuda main.cu)
135+
target_compile_features(main_cuda PRIVATE cxx_std_17)
136+
target_link_libraries(main_cuda PRIVATE project_options project_warnings)
137+
target_link_cuda(main_cuda)
138+
```
139+
128140
## Changing the project_options dynamically
129141

130142
During the test and development, it can be useful to change options on the fly. For example, to enable sanitizers when running tests. You can include `DynamicOptions.cmake`, which imports the `dynamic_project_options` function.

src/CompilerWarnings.cmake

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function(
88
WARNINGS_AS_ERRORS
99
MSVC_WARNINGS
1010
CLANG_WARNINGS
11-
GCC_WARNINGS)
11+
GCC_WARNINGS
12+
CUDA_WARNINGS)
1213
if("${MSVC_WARNINGS}" STREQUAL "")
1314
set(MSVC_WARNINGS
1415
/W4 # Baseline reasonable warnings
@@ -69,6 +70,17 @@ function(
6970
)
7071
endif()
7172

73+
if("${CUDA_WARNINGS}" STREQUAL "")
74+
set(CUDA_WARNINGS
75+
-Wall
76+
-Wextra
77+
-Wunused
78+
-Wconversion
79+
-Wshadow
80+
# TODO add more Cuda warnings
81+
)
82+
endif()
83+
7284
if(WARNINGS_AS_ERRORS)
7385
message(TRACE "Warnings are treated as errors")
7486
list(APPEND CLANG_WARNINGS -Werror)
@@ -77,15 +89,27 @@ function(
7789
endif()
7890

7991
if(MSVC)
80-
set(PROJECT_WARNINGS ${MSVC_WARNINGS})
92+
set(PROJECT_WARNINGS_CXX ${MSVC_WARNINGS})
8193
elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
82-
set(PROJECT_WARNINGS ${CLANG_WARNINGS})
94+
set(PROJECT_WARNINGS_CXX ${CLANG_WARNINGS})
8395
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
84-
set(PROJECT_WARNINGS ${GCC_WARNINGS})
96+
set(PROJECT_WARNINGS_CXX ${GCC_WARNINGS})
8597
else()
86-
message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
98+
message(AUTHOR_WARNING "No compiler warnings set for CXX compiler: '${CMAKE_CXX_COMPILER_ID}'")
99+
# TODO support Intel compiler
87100
endif()
88101

89-
target_compile_options(${project_name} INTERFACE ${PROJECT_WARNINGS})
102+
# use the same warning flags for C
103+
set(PROJECT_WARNINGS_C "${PROJECT_WARNINGS_CXX}")
104+
105+
set(PROJECT_WARNINGS_CUDA "${CUDA_WARNINGS}")
90106

107+
target_compile_options(
108+
${project_name}
109+
INTERFACE # C++ warnings
110+
$<$<COMPILE_LANGUAGE:CXX>:${PROJECT_WARNINGS_CXX}>
111+
# C warnings
112+
$<$<COMPILE_LANGUAGE:C>:${PROJECT_WARNINGS_C}>
113+
# Cuda warnings
114+
$<$<COMPILE_LANGUAGE:CUDA>:${PROJECT_WARNINGS_CUDA}>)
91115
endfunction()

src/Cuda.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ! target_link_cuda
2+
# A function that links Cuda to the given 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 project_warnings)
8+
# target_link_cuda(main_cuda)
9+
#
10+
macro(target_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(WIN32 AND "$ENV{VSCMD_VER}" STREQUAL "")
43+
message(
44+
WARNING
45+
"Compiling Cuda on Windows outside the Visual Studio Commant prompt or without running `vcvarsall.bat x64` probably fails"
46+
)
47+
endif()
48+
endmacro()

src/Index.cmake

Lines changed: 5 additions & 1 deletion
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
@@ -35,6 +37,7 @@ include("${ProjectOptions_SRC_DIR}/SystemLink.cmake")
3537
# - MSVC_WARNINGS: Override the defaults for the MSVC warnings
3638
# - CLANG_WARNINGS: Override the defaults for the CLANG warnings
3739
# - GCC_WARNINGS: Override the defaults for the GCC warnings
40+
# - CUDA_WARNINGS: Override the defaults for the CUDA warnings
3841
# - CONAN_OPTIONS: Extra Conan options
3942
#
4043
# NOTE: cmake-lint [C0103] Invalid macro name "project_options" doesn't match `[0-9A-Z_]+`
@@ -118,7 +121,8 @@ macro(project_options)
118121
"${WARNINGS_AS_ERRORS}"
119122
"${ProjectOptions_MSVC_WARNINGS}"
120123
"${ProjectOptions_CLANG_WARNINGS}"
121-
"${ProjectOptions_GCC_WARNINGS}")
124+
"${ProjectOptions_GCC_WARNINGS}"
125+
"${ProjectOptions_CUDA_WARNINGS}")
122126

123127
include("${ProjectOptions_SRC_DIR}/Tests.cmake")
124128
if(${ProjectOptions_ENABLE_COVERAGE})

src/StandardProjectSettings.cmake

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,20 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1919

2020
# Enhance error reporting and compiler messages
2121
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
22-
add_compile_options(-fcolor-diagnostics)
22+
if(WIN32)
23+
# On Windows cuda nvcc uses cl and not clang
24+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-fcolor-diagnostics> $<$<COMPILE_LANGUAGE:CXX>:-fcolor-diagnostics>)
25+
else()
26+
add_compile_options(-fcolor-diagnostics)
27+
endif()
2328
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
24-
add_compile_options(-fdiagnostics-color=always)
29+
if(WIN32)
30+
# On Windows cuda nvcc uses cl and not gcc
31+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-fdiagnostics-color=always>
32+
$<$<COMPILE_LANGUAGE:CXX>:-fdiagnostics-color=always>)
33+
else()
34+
add_compile_options(-fdiagnostics-color=always)
35+
endif()
2536
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER 1900)
2637
add_compile_options(/diagnostics:column)
2738
else()

0 commit comments

Comments
 (0)