Set warnings with target_compile_options

The use of target_compile_options makes it easy to append warnings
to a target. The use of generator expressions also more succinctly
handles a few edge cases for compiler bugs and platform-specific
oddities.

This should be easy to read and maintain than the variable-based
solution it replaces.
This commit is contained in:
Chris Thrasher 2022-08-13 13:33:07 -06:00 committed by Lukas Dürrenberger
parent 142ccf57bd
commit aa82ea132b

View File

@ -6,7 +6,9 @@
function(set_target_warnings target) function(set_target_warnings target)
option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE) option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE)
set(MSVC_WARNINGS if(SFML_COMPILER_MSVC)
target_compile_options(${target} PRIVATE
$<$<BOOL:${WARNINGS_AS_ERRORS}>:/WX>
/W4 # Baseline reasonable warnings /W4 # Baseline reasonable warnings
/w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data /w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data
/w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data /w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data
@ -35,8 +37,11 @@ function(set_target_warnings target)
/wd4505 # disable warnings about unused functions that might be platform-specific /wd4505 # disable warnings about unused functions that might be platform-specific
/wd4800 # disable warnings regarding implicit conversions to bool /wd4800 # disable warnings regarding implicit conversions to bool
) )
endif()
set(CLANG_AND_GCC_WARNINGS if(SFML_COMPILER_GCC OR SFML_COMPILER_CLANG)
target_compile_options(${target} PRIVATE
$<$<BOOL:${WARNINGS_AS_ERRORS}>:-Werror>
-Wall -Wall
-Wextra # reasonable and standard -Wextra # reasonable and standard
-Wshadow # warn the user if a variable declaration shadows one from a parent context -Wshadow # warn the user if a variable declaration shadows one from a parent context
@ -53,48 +58,25 @@ function(set_target_warnings target)
-Wnull-dereference # warn if a null dereference is detected -Wnull-dereference # warn if a null dereference is detected
-Wold-style-cast # warn for c-style casts -Wold-style-cast # warn for c-style casts
-Wpedantic # warn if non-standard C++ is used -Wpedantic # warn if non-standard C++ is used
$<$<BOOL:${SFML_OS_ANDROID}>:-Wno-main> # allow main() to be called
) )
if(ANDROID)
set(CLANG_AND_GCC_WARNINGS ${CLANG_AND_GCC_WARNINGS} -Wno-main) # allow main() to be called
endif() endif()
if(WARNINGS_AS_ERRORS) if(SFML_COMPILER_GCC)
set(CLANG_AND_GCC_WARNINGS ${CLANG_AND_GCC_WARNINGS} -Werror) # Don't enable -Wduplicated-branches for GCC < 8.1 since it will lead to false positives
set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX) # https://github.com/gcc-mirror/gcc/commit/6bebae75035889a4844eb4d32a695bebf412bcd7
endif() target_compile_options(${target} PRIVATE
set(CLANG_WARNINGS
${CLANG_AND_GCC_WARNINGS}
-Wno-unknown-warning-option # do not warn on GCC-specific warning diagnostic pragmas
)
set(GCC_WARNINGS
${CLANG_AND_GCC_WARNINGS}
-Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist -Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist
-Wduplicated-cond # warn if if / else chain has duplicated conditions -Wduplicated-cond # warn if if / else chain has duplicated conditions
-Wlogical-op # warn about logical operations being used where bitwise were probably wanted -Wlogical-op # warn about logical operations being used where bitwise were probably wanted
# -Wuseless-cast # warn if you perform a cast to the same type (disabled because it is not portable as some type aliases might vary between platforms) # -Wuseless-cast # warn if you perform a cast to the same type (disabled because it is not portable as some type aliases might vary between platforms)
) $<$<VERSION_GREATER_EQUAL:${CMAKE_CXX_COMPILER_VERSION},8.1>:-Wduplicated-branches> # warn if if / else branches have duplicated code
# Don't enable -Wduplicated-branches for GCC < 8.1 since it will lead to false positives
# https://github.com/gcc-mirror/gcc/commit/6bebae75035889a4844eb4d32a695bebf412bcd7
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.1)
set(GCC_WARNINGS
${GCC_WARNINGS}
-Wduplicated-branches # warn if if / else branches have duplicated code
) )
endif() endif()
if(MSVC) if(SFML_COMPILER_CLANG)
set(FILE_WARNINGS ${MSVC_WARNINGS}) target_compile_options(${target} PRIVATE
elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") -Wno-unknown-warning-option # do not warn on GCC-specific warning diagnostic pragmas
set(FILE_WARNINGS ${CLANG_WARNINGS}) )
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(FILE_WARNINGS ${GCC_WARNINGS})
else()
message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
endif() endif()
target_compile_options(${target} PRIVATE ${FILE_WARNINGS})
endfunction() endfunction()