From d00ca18ac2ac4fe20c9abdba8333fe2fd5e4b553 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 7 Feb 2017 14:51:29 +0100 Subject: [PATCH] CMake: automatic detection of compiler flag compatibility --- CMakeLists.txt | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21e537e..1cee4b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,13 +12,43 @@ set(CJSON_VERSION_SO 1) set(CJSON_UTILS_VERSION_SO 1) set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") + +set(custom_compiler_flags) + +include(CheckCCompilerFlag) option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) - if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat -Wundef -Wswitch-default -Wconversion -fstack-protector-strong") - endif() + list(APPEND custom_compiler_flags + -std=c89 + -pedantic + -Wall + -Wextra + -Werror + -Wstrict-prototypes + -Wwrite-strings + -Wshadow + -Winit-self + -Wcast-align + -Wformat=2 + -Wmissing-prototypes + -Wstrict-overflow=2 + -Wcast-qual + -Wc++-compat + -Wundef + -Wswitch-default + -Wconversion + -fstack-protector-strong + ) endif() +# apply custom compiler flags +foreach(compiler_flag ${custom_compiler_flags}) + CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED-${compiler_flag}") + if (FLAG_SUPPORTED${compiler_flag}) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}") + endif() +endforeach() + #variables for pkg-config set(prefix "${CMAKE_INSTALL_PREFIX}") set(libdir "${CMAKE_INSTALL_LIBDIR}")