Skip to content

Commit ac67aaf

Browse files
committed
feat: support cuda warnings in project_warnings
1 parent 6780bff commit ac67aaf

4 files changed

Lines changed: 44 additions & 29 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ It gets the following named parameters (each accepting multiple values):
9999
- `MSVC_WARNINGS`: Override the defaults for the MSVC warnings
100100
- `CLANG_WARNINGS`: Override the defaults for the CLANG warnings
101101
- `GCC_WARNINGS`: Override the defaults for the GCC warnings
102+
- `CUDA_WARNINGS`: Override the defaults for the CUDA warnings
102103
- `CONAN_OPTIONS`: Extra Conan options
103104

104105
## `run_vcpkg` function
@@ -122,15 +123,15 @@ A very useful function that accepts the same arguments as `target_link_libraries
122123

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

125-
## `find_and_link_cuda` function
126+
## `target_link_cuda` function
126127

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+
A function that links Cuda to the given target.
128129

129130
```cmake
130131
add_executable(main_cuda main.cu)
131132
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)
133+
target_link_libraries(main_cuda PRIVATE project_options project_warnings)
134+
target_link_cuda(main_cuda)
134135
```
135136

136137
## Changing the project_options parameters dynamically

src/CompilerWarnings.cmake

Lines changed: 31 additions & 9 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(NOT ${MSVC_WARNINGS})
1314
set(MSVC_WARNINGS
1415
/W4 # Baseline reasonable warnings
@@ -69,23 +70,44 @@ function(
6970
)
7071
endif()
7172

73+
if(NOT ${CUDA_WARNINGS})
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 STREQUAL TRUE)
7385
message(AUTHOR_WARNING "NOTE: WARNINGS_AS_ERRORS=${WARNINGS_AS_ERRORS}")
7486
list(APPEND CLANG_WARNINGS -Werror)
7587
list(APPEND GCC_WARNINGS -Werror)
7688
list(APPEND MSVC_WARNINGS /WX)
7789
endif()
7890

79-
if(MSVC)
80-
set(PROJECT_WARNINGS ${MSVC_WARNINGS})
81-
elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
82-
set(PROJECT_WARNINGS ${CLANG_WARNINGS})
83-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
84-
set(PROJECT_WARNINGS ${GCC_WARNINGS})
85-
else()
91+
if(NOT MSVC
92+
AND NOT
93+
CMAKE_CXX_COMPILER_ID
94+
MATCHES
95+
".*Clang"
96+
AND NOT
97+
CMAKE_CXX_COMPILER_ID
98+
STREQUAL
99+
"GNU")
86100
message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
87101
endif()
88102

89-
target_compile_options(${project_name} INTERFACE ${PROJECT_WARNINGS})
103+
target_compile_options(
104+
${project_name}
105+
INTERFACE $<$<COMPILE_LANG_AND_ID:CXX,Clang>:${CLANG_WARNINGS}>
106+
$<$<COMPILE_LANG_AND_ID:C,Clang>:${CLANG_WARNINGS}>
107+
$<$<COMPILE_LANG_AND_ID:CXX,GNU>:${GCC_WARNINGS}>
108+
$<$<COMPILE_LANG_AND_ID:C,GNU>:${GCC_WARNINGS}>
109+
$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:${MSVC_WARNINGS}>
110+
$<$<COMPILE_LANG_AND_ID:C,MSVC>:${MSVC_WARNINGS}>
111+
$<$<COMPILE_LANGUAGE:CUDA>:${CUDA_WARNINGS}>)
90112

91113
endfunction()

src/Cuda.cmake

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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.
1+
# ! target_link_cuda
2+
# A function that links Cuda to the given target
33
#
44
# # Example
55
# add_executable(main_cuda main.cu)
66
# 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)
7+
# target_link_libraries(main_cuda PRIVATE project_options project_warnings)
8+
# target_link_cuda(main_cuda)
99
#
10-
macro(find_and_link_cuda target)
10+
macro(target_link_cuda target)
1111
# optional named CUDA_WARNINGS
1212
set(oneValueArgs CUDA_WARNINGS)
1313
cmake_parse_arguments(
@@ -38,14 +38,4 @@ macro(find_and_link_cuda target)
3838
# so that the static cuda runtime can find it at runtime.
3939
set_property(TARGET ${target} PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
4040
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()
5141
endmacro()

src/Index.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ include("${ProjectOptions_SRC_DIR}/Cuda.cmake")
3636
# - MSVC_WARNINGS: Override the defaults for the MSVC warnings
3737
# - CLANG_WARNINGS: Override the defaults for the CLANG warnings
3838
# - GCC_WARNINGS: Override the defaults for the GCC warnings
39+
# - CUDA_WARNINGS: Override the defaults for the CUDA warnings
3940
# - CONAN_OPTIONS: Extra Conan options
4041
#
4142
# NOTE: cmake-lint [C0103] Invalid macro name "project_options" doesn't match `[0-9A-Z_]+`
@@ -115,7 +116,8 @@ macro(project_options)
115116
"${WARNINGS_AS_ERRORS}"
116117
"${ProjectOptions_MSVC_WARNINGS}"
117118
"${ProjectOptions_CLANG_WARNINGS}"
118-
"${ProjectOptions_GCC_WARNINGS}")
119+
"${ProjectOptions_GCC_WARNINGS}"
120+
"${ProjectOptions_CUDA_WARNINGS}")
119121

120122
include("${ProjectOptions_SRC_DIR}/Tests.cmake")
121123
if(${ProjectOptions_ENABLE_COVERAGE})

0 commit comments

Comments
 (0)