mirror of
https://github.com/SFML/SFML.git
synced 2025-02-19 06:39:59 +08:00
Only enable compiler warnings in developer builds
Compiler warnings are a tool we enable as developers to detect issues with our source code that go beyond the basic requirements of compiling C++ code. For example, many implicit conversions are perfectly correct, and defined behavior in C++ but we choose to disallow them with -Wconversion. This is something only us devs have to care about. Someone using SFML wants the library to work well but does not need to concern themselves with such low level details about its implementation. By disabling warnings in non-developer builds, we ensure that non- developer users of SFML are not going to see console noise due to warnings and it prevents issues like GCC's bug 82952 which causes compilation to halt indefitely due to a bug in how a compiler warning is implemented. This also improves compatibility with older compilers. If we enable warnings in all builds no matter what, we have to protect against compilers we don't expect. Perhaps someone uses an older version of GCC that we haven't tested with. This compiler is perfectly fine compiling our C++ but we happen to use a warning flag it does not recognize so the build fails. This is not a good reason for a build to fail but nevertheless it's something we have to deal with so long as we always enable warnings. SFML_WARNINGS_AS_ERRORS is now named somewhat incorrectly but I retained the name for compatibility's sake. We can rename it to SFML_ENABLE_WARNINGS or something similar in SFML 4.
This commit is contained in:
parent
0eb92fc27d
commit
c91973fdd1
@ -4,11 +4,28 @@
|
||||
|
||||
# Helper function to enable compiler warnings for a specific target
|
||||
function(set_target_warnings target)
|
||||
option(SFML_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF)
|
||||
option(SFML_WARNINGS_AS_ERRORS "Enable and enforce compiler warnings" OFF)
|
||||
|
||||
# Disable certain deprecation warnings
|
||||
if(SFML_OS_WINDOWS)
|
||||
target_compile_definitions(${target} PRIVATE -D_CRT_SECURE_NO_WARNINGS)
|
||||
target_compile_definitions(${target} PRIVATE -D_WINSOCK_DEPRECATED_NO_WARNINGS)
|
||||
endif()
|
||||
|
||||
if(SFML_COMPILER_CLANG OR SFML_COMPILER_CLANG_CL)
|
||||
target_compile_options(${target} PRIVATE
|
||||
-Wno-unknown-warning-option # do not warn on GCC-specific warning diagnostic pragmas
|
||||
)
|
||||
endif()
|
||||
|
||||
# Short-circuit function to prevent adding any warnings
|
||||
if(NOT SFML_WARNINGS_AS_ERRORS)
|
||||
return()
|
||||
endif()
|
||||
|
||||
if(SFML_COMPILER_MSVC)
|
||||
target_compile_options(${target} PRIVATE
|
||||
$<$<BOOL:${SFML_WARNINGS_AS_ERRORS}>:/WX>
|
||||
/WX
|
||||
/W4 # Baseline reasonable warnings
|
||||
/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
|
||||
@ -41,7 +58,7 @@ function(set_target_warnings target)
|
||||
|
||||
if(SFML_COMPILER_GCC OR SFML_COMPILER_CLANG)
|
||||
target_compile_options(${target} PRIVATE
|
||||
$<$<BOOL:${SFML_WARNINGS_AS_ERRORS}>:-Werror>
|
||||
-Werror
|
||||
-Wall
|
||||
-Wextra # reasonable and standard
|
||||
-Wshadow # warn the user if a variable declaration shadows one from a parent context
|
||||
@ -73,16 +90,4 @@ function(set_target_warnings target)
|
||||
$<$<VERSION_GREATER_EQUAL:${CMAKE_CXX_COMPILER_VERSION},8.1>:-Wduplicated-branches> # warn if if / else branches have duplicated code
|
||||
)
|
||||
endif()
|
||||
|
||||
if(SFML_COMPILER_CLANG OR SFML_COMPILER_CLANG_CL)
|
||||
target_compile_options(${target} PRIVATE
|
||||
-Wno-unknown-warning-option # do not warn on GCC-specific warning diagnostic pragmas
|
||||
)
|
||||
endif()
|
||||
|
||||
# Disable certain deprecation warnings
|
||||
if(SFML_OS_WINDOWS)
|
||||
target_compile_definitions(${target} PRIVATE -D_CRT_SECURE_NO_WARNINGS)
|
||||
target_compile_definitions(${target} PRIVATE -D_WINSOCK_DEPRECATED_NO_WARNINGS)
|
||||
endif()
|
||||
endfunction()
|
||||
|
Loading…
x
Reference in New Issue
Block a user