From 55042ac72443a1cb9c456ffd94ddc5f504835c33 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Thu, 26 Nov 2015 15:00:59 +0100 Subject: [PATCH 1/3] Refactored cmake part dealing with compiler warnings to use warnings identifier. It is now possible to use msvc warnings identifiers (e.g. '4114') or gcc warnings keys (e.g. 'pointer-sign'). Bug: https://bugs.freedesktop.org/show_bug.cgi?id=93069 --- cmake/CMakeLists.txt | 92 +++++++++++++++++++++++----------------------- cmake/modules/Macros.cmake | 53 ++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 45 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 4bde279..ca49088 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -153,44 +153,53 @@ if(VCS) set(DBUS_VERBOSE_C_S 1 CACHE STRING "verbose mode" FORCE) endif(VCS) -if(WIN32) - if(MSVC) - # controll folders in msvc projects - include(ProjectSourceGroup) - if(NOT GROUP_CODE) - #set(GROUP_CODE split) #cmake default - set(GROUP_CODE flat) - endif(NOT GROUP_CODE) - ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE) - - - # Use the highest warning level - if (WALL) - set(WALL 1 CACHE STRING "all warnings" FORCE) - set(CMAKE_CXX_WARNING_LEVEL 4 CACHE STRING "warning level" FORCE) - - if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") - STRING(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - else(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") - endif(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") - - if(CMAKE_C_FLAGS MATCHES "/W[0-4]") - STRING(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") - else(CMAKE_C_FLAGS MATCHES "/W[0-4]") - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4") - endif(CMAKE_C_FLAGS MATCHES "/W[0-4]") - else (WALL) - set(CMAKE_CXX_WARNING_LEVEL 3 CACHE STRING "warning level" FORCE) - endif (WALL) - - SET(MSVC_W_ERROR " /we4028 /we4013 /we4133 /we4047 /we4031 /we4002 /we4003 /we4114") - SET(MSVC_W_DISABLE " /wd4127 /wd4090 /wd4101 /wd4244") - - SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /FIconfig.h ${MSVC_W_ERROR} ${MSVC_W_DISABLE}") - SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /FIconfig.h ${MSVC_W_ERROR} ${MSVC_W_DISABLE}") - endif(MSVC) -endif(WIN32) +if(MSVC) + # controll folders in msvc projects + include(ProjectSourceGroup) + if(NOT GROUP_CODE) + #set(GROUP_CODE split) #cmake default + set(GROUP_CODE flat) + endif(NOT GROUP_CODE) + ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE) + SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /FIconfig.h") + SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /FIconfig.h") +endif() + +# +# setup warnings +# +if(MSVC) + # Use the highest warning level + if(WALL) + set(WALL 1 CACHE STRING "all warnings" FORCE) + set(CMAKE_CXX_WARNING_LEVEL 4 CACHE STRING "warning level" FORCE) + if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() + + if(CMAKE_C_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4") + endif() + else() + set(CMAKE_CXX_WARNING_LEVEL 3 CACHE STRING "warning level" FORCE) + endif() + + set(WARNINGS "14018") + set(WARNINGS_DISABLED "4127 4090 4101 4244") + set(WARNINGS_ERRORS "4028 4013 4133 4047 4031 4002 4003 4114") +else() + set(WARNINGS "sign-compare") + set(WARNINGS_DISABLED "") + set(WARNINGS_ERRORS "") +endif() +generate_warning_cflags(WARNINGS_CFLAGS "${WARNINGS}" "${WARNINGS_DISABLED}" "${WARNINGS_ERRORS}") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNINGS_CFLAGS}") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNINGS_CFLAGS}") + if (UNIX AND NOT DBUS_DISABLE_ASSERT) # required for backtrace @@ -205,13 +214,6 @@ if(DBUS_WITH_GLIB) endif() SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG") -if(MSVC) - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /w14018") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w14018") -else() - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wsign-compare") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare") -endif() ######################################################################### # Windows CE (>= 5.0.0) diff --git a/cmake/modules/Macros.cmake b/cmake/modules/Macros.cmake index fd3f62c..5062b38 100644 --- a/cmake/modules/Macros.cmake +++ b/cmake/modules/Macros.cmake @@ -60,3 +60,56 @@ macro(add_helper_executable _target _source) add_executable(${_target} ${_source}) target_link_libraries(${_target} ${ARGN}) endmacro(add_helper_executable) + + +# +# generate compiler flags from MSVC warning identifiers (e.g. '4114') or gcc warning keys (e.g. 'pointer-sign') +# +# @param target the variable name which will contain the warnings flags +# @param warnings a string with space delimited warnings +# @param disabled_warnings a string with space delimited disabled warnings +# @param error_warnings a string with space delimited warnings which should result into compile errors +# +macro(generate_warning_cflags target warnings disabled_warnings error_warnings) + if(DEBUG_MACROS) + message("generate_warning_cflags got: ${warnings} - ${disabled_warnings} - ${error_warnings}") + endif() + if(MSVC) + set(enabled_prefix "/w") + set(error_prefix "/we") + set(disabled_prefix "/wd") + else() + set(enabled_prefix "-W") + set(error_prefix "-Werror=") + set(disabled_prefix "-Wno-") + endif() + + set(temp) + string(REPLACE " " ";" warnings_list "${warnings}") + foreach(warning ${warnings_list}) + string(STRIP ${warning} _warning) + if(_warning) + set(temp "${temp} ${enabled_prefix}${_warning}") + endif() + endforeach() + + string(REPLACE " " ";" disabled_warnings_list "${disabled_warnings}") + foreach(warning ${disabled_warnings_list}) + string(STRIP ${warning} _warning) + if(_warning) + set(temp "${temp} ${disabled_prefix}${_warning}") + endif() + endforeach() + + string(REPLACE " " ";" error_warnings_list "${error_warnings}") + foreach(warning ${error_warnings_list}) + string(STRIP ${warning} _warning) + if(_warning) + set(temp "${temp} ${error_prefix}${_warning}") + endif() + endforeach() + set(${target} "${temp}") + if(DEBUG_MACROS) + message("generate_warning_cflags return: ${${target}}") + endif() +endmacro() -- 1.8.4.5