From a97e227a9dc6f4bc62ac9b7045b6f9bc64537932 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Fri, 24 Aug 2018 07:43:04 +0200 Subject: cmake: Improve compiler flag detection Signed-off-by: Andreas Schneider --- CompilerChecks.cmake | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'CompilerChecks.cmake') diff --git a/CompilerChecks.cmake b/CompilerChecks.cmake index 57afba81..f8a7af14 100644 --- a/CompilerChecks.cmake +++ b/CompilerChecks.cmake @@ -2,6 +2,20 @@ include(AddCCompilerFlag) include(CheckCCompilerFlagSSP) if (UNIX) + # + # Check for -Werror turned on if possible + # + # This will prevent that compiler flags are detected incorrectly. + # + check_c_compiler_flag("-Werror" REQUIRED_FLAGS_WERROR) + if (REQUIRED_FLAGS_WERROR) + set(CMAKE_REQUIRED_FLAGS "-Werror") + + if (PICKY_DEVELOPER) + list(APPEND SUPPORTED_COMPILER_FLAGS "-Werror") + endif() + endif() + add_c_compiler_flag("-std=gnu99" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-pedantic" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-pedantic-errors" SUPPORTED_COMPILER_FLAGS) @@ -28,28 +42,20 @@ if (UNIX) add_c_compiler_flag("-Werror=strict-overflow" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-Wstrict-overflow=2" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-Wno-format-zero-length" SUPPORTED_COMPILER_FLAGS) + + check_c_compiler_flag("-Wformat" REQUIRED_FLAGS_WFORMAT) + if (REQUIRED_FLAGS_WFORMAT) + list(APPEND SUPPORTED_COMPILER_FLAGS "-Wformat") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wformat") + endif() add_c_compiler_flag("-Wformat-security" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-Werror=format-security" SUPPORTED_COMPILER_FLAGS) - # - # Check for -f options with -Werror turned on if possible - # - # This will prevent that compiler flags are detected incorrectly. - # - check_c_compiler_flag("-Werror" REQUIRED_FLAGS_WERROR) - if (REQUIRED_FLAGS_WERROR) - set(CMAKE_REQUIRED_FLAGS "-Werror") - endif() + # Allow zero for a variadic macro argument + add_c_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-fno-common" SUPPORTED_COMPILER_FLAGS) - check_c_compiler_flag_ssp("-fstack-protector" WITH_STACK_PROTECTOR) - if (WITH_STACK_PROTECTOR) - list(APPEND SUPPORTED_COMPILER_FLAGS "-fstack-protector") - endif() - unset(CMAKE_REQUIRED_FLAGS) - - if (CMAKE_BUILD_TYPE) string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) if (CMAKE_BUILD_TYPE_LOWER MATCHES (release|relwithdebinfo|minsizerel)) @@ -57,14 +63,18 @@ if (UNIX) endif() endif() + check_c_compiler_flag_ssp("-fstack-protector" WITH_STACK_PROTECTOR) + if (WITH_STACK_PROTECTOR) + list(APPEND SUPPORTED_COMPILER_FLAGS "-fstack-protector") + endif() + if (PICKY_DEVELOPER) - add_c_compiler_flag("-Werror" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-Wno-error=deprecated-declarations" SUPPORTED_COMPILER_FLAGS) add_c_compiler_flag("-Wno-error=tautological-compare" SUPPORTED_COMPILER_FLAGS) endif() - # Allow zero for a variadic macro argument - add_c_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments" SUPPORTED_COMPILER_FLAGS) + # Unset CMAKE_REQUIRED_FLAGS + unset(CMAKE_REQUIRED_FLAGS) endif() if (MSVC) -- cgit v1.2.3