Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ add_executable(main main.cpp)
target_check_warning(main)
```

### Ignoring Specific Warnings
### Ignoring Specific Warnings on a Target

You can pass `FLAGS` to the `target_check_warning` function to ignore specific warnings on a target.
You can call the `target_compile_options` function to ignore specific warnings on a target.

```cmake
target_check_warning(main FLAGS -Wno-unused-variable)
target_check_warning(main)
target_compile_options(main PRIVATE -Wno-unused-variable)
```

## License
Expand Down
6 changes: 2 additions & 4 deletions cmake/target_check_warning.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
function(target_check_warning TARGET)
cmake_parse_arguments(ARG "" "" "FLAGS" ${ARGN})

# Determine if the target is an interface library or not
get_target_property(TARGET_TYPE ${TARGET} TYPE)
if(${TARGET_TYPE} STREQUAL INTERFACE_LIBRARY)
Expand All @@ -11,8 +9,8 @@ function(target_check_warning TARGET)

# Append warning flags to the compile options
if(MSVC)
target_compile_options(${TARGET} ${TYPE} /WX /permissive- /W4 /EHsc ${ARG_FLAGS})
target_compile_options(${TARGET} ${TYPE} /WX /permissive- /W4 /EHsc)
else()
target_compile_options(${TARGET} ${TYPE} -Werror -Wall -Wextra -Wpedantic ${ARG_FLAGS})
target_compile_options(${TARGET} ${TYPE} -Werror -Wall -Wextra -Wpedantic)
endif()
endfunction()
12 changes: 11 additions & 1 deletion test/sample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ add_executable(with_parameters EXCLUDE_FROM_ALL main.cpp)
target_link_libraries(with_parameters PRIVATE lib)
target_compile_definitions(with_parameters PRIVATE WITH_UNUSED)
target_check_warning(with_parameters FLAGS $<IF:$<BOOL:${MSVC}>,/wd4101,-Wno-unused-variable>)
if(MSVC)
target_compile_options(with_parameters PRIVATE /wd4101)
else()
target_compile_options(with_parameters PRIVATE -Wno-unused-variable)
endif()

add_executable(correct_c EXCLUDE_FROM_ALL main.c)
target_check_warning(correct_c)
Expand All @@ -31,4 +36,9 @@ target_check_warning(incorrect_c)

add_executable(with_parameters_c EXCLUDE_FROM_ALL main.c)
target_compile_definitions(with_parameters_c PRIVATE WITH_UNUSED)
target_check_warning(with_parameters_c FLAGS $<IF:$<BOOL:${MSVC}>,/wd4101,-Wno-unused-variable>)
target_check_warning(with_parameters_c)
if(MSVC)
target_compile_options(with_parameters_c PRIVATE /wd4101)
else()
target_compile_options(with_parameters_c PRIVATE -Wno-unused-variable)
endif()