diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | CompilerChecks.cmake | 85 | ||||
-rw-r--r-- | DefineOptions.cmake | 1 | ||||
-rw-r--r-- | example/uptime/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 2 |
6 files changed, 93 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c9283e..c37aab6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,11 +30,11 @@ set(CMAKE_MODULE_PATH # add definitions include(DefineCMakeDefaults) include(DefinePlatformDefaults) -include(DefineCompilerFlags) include(DefineInstallationPaths) include(DefineOptions.cmake) include(CPackConfig.cmake) include(CheckSymbolExists) +include(CompilerChecks.cmake) # disallow in-source build include(MacroEnsureOutOfSourceBuild) diff --git a/CompilerChecks.cmake b/CompilerChecks.cmake new file mode 100644 index 0000000..4d8850c --- /dev/null +++ b/CompilerChecks.cmake @@ -0,0 +1,85 @@ +include(AddCCompilerFlag) +include(CheckCCompilerFlagSSP) + +if (UNIX) + 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) + add_c_compiler_flag("-Wall" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wshadow" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wmissing-prototypes" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wcast-align" SUPPORTED_COMPILER_FLAGS) + #add_c_compiler_flag("-Wcast-qual" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror=address" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wstrict-prototypes" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror=strict-prototypes" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wwrite-strings" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror=write-strings" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror-implicit-function-declaration" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wpointer-arith" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror=pointer-arith" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wdeclaration-after-statement" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror=declaration-after-statement" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wreturn-type" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror=return-type" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wuninitialized" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Werror=uninitialized" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("-Wimplicit-fallthrough" SUPPORTED_COMPILER_FLAGS) + 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) + 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() + + 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)) + add_c_compiler_flag("-Wp,-D_FORTIFY_SOURCE=2" SUPPORTED_COMPILER_FLAGS) + endif() + 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() +endif() + +if (MSVC) + add_c_compiler_flag("/D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("/D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("/D _CRT_NONSTDC_NO_WARNINGS=1" SUPPORTED_COMPILER_FLAGS) + add_c_compiler_flag("/D _CRT_SECURE_NO_WARNINGS=1" SUPPORTED_COMPILER_FLAGS) +endif() + +# This removes this annoying warning +# "warning: 'BN_CTX_free' is deprecated: first deprecated in OS X 10.7 [-Wdeprecated-declarations]" +if (OSX) + add_c_compiler_flag("-Wno-deprecated-declarations" SUPPORTED_COMPILER_FLAGS) +endif() + +if (BSD) + # Allow zero for a variadic macro argument + add_c_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments" SUPPORTED_COMPILER_FLAGS) +endif() + +set(DEFAULT_C_COMPILE_FLAGS ${SUPPORTED_COMPILER_FLAGS} CACHE INTERNAL "Default C Compiler Flags" FORCE) diff --git a/DefineOptions.cmake b/DefineOptions.cmake index 7564a22..bca0848 100644 --- a/DefineOptions.cmake +++ b/DefineOptions.cmake @@ -1,6 +1,7 @@ option(WITH_STATIC_LIB "Build with a static library" OFF) option(WITH_CMOCKERY_SUPPORT "Install a cmockery header" OFF) option(UNIT_TESTING "Build with unit testing" OFF) +option(PICKY_DEVELOPER "Build with picky developer flags" OFF) if (UNIT_TESTING) set(WITH_STATIC_LIB ON) diff --git a/example/uptime/CMakeLists.txt b/example/uptime/CMakeLists.txt index 4f73d88..99924bd 100644 --- a/example/uptime/CMakeLists.txt +++ b/example/uptime/CMakeLists.txt @@ -7,9 +7,11 @@ include_directories( add_library(proc_uptime proc_uptime.c) add_executable(uptime uptime.c) +target_compile_options(uptime PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) target_link_libraries(uptime proc_uptime) add_executable(test_uptime test_uptime.c) +target_compile_options(test_uptime PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) target_link_libraries(test_uptime ${CMOCKA_SHARED_LIBRARY}) set_target_properties(test_uptime diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c1c8c4f..5732232 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -51,7 +51,7 @@ if (CMOCKA_PLATFORM_INCLUDE) endif() add_library(${CMOCKA_SHARED_LIBRARY} SHARED ${cmocka_SRCS}) - +target_compile_options(${CMOCKA_SHARED_LIBRARY} PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) target_link_libraries(${CMOCKA_SHARED_LIBRARY} ${CMOCKA_LINK_LIBRARIES}) set_target_properties( ${CMOCKA_SHARED_LIBRARY} @@ -83,6 +83,7 @@ install( if (WITH_STATIC_LIB) add_library(${CMOCKA_STATIC_LIBRARY} STATIC ${cmocka_SRCS}) + target_compile_options(${CMOCKA_STATIC_LIBRARY} PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) set_target_properties( ${CMOCKA_STATIC_LIBRARY} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7a529d4..3789b1b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,6 +28,7 @@ set(CMOCKA_TESTS foreach(_CMOCKA_TEST ${CMOCKA_TESTS}) add_cmocka_test(${_CMOCKA_TEST} ${_CMOCKA_TEST}.c ${CMOCKA_STATIC_LIBRARY}) + target_compile_options(${_CMOCKA_TEST} PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) endforeach() ### Special Cases @@ -35,6 +36,7 @@ if (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)") set_source_files_properties(test_cmockery.c PROPERTIES COMPILE_FLAGS "-Wno-deprecated-declarations") endif() add_cmocka_test(test_cmockery test_cmockery.c ${CMOCKA_STATIC_LIBRARY}) +target_compile_options(test_cmockery PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) ### Exceptions |