]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
cmake: Make gcc builds use similar compiler warnings to autotools
authorRalf Habacker <ralf.habacker@freenet.de>
Wed, 1 Dec 2021 14:38:04 +0000 (15:38 +0100)
committerSimon McVittie <smcv@collabora.com>
Fri, 10 Dec 2021 13:36:28 +0000 (13:36 +0000)
Fixes #356

CMakeLists.txt
cmake/modules/Macros.cmake

index 965aedc82d45a63d7d94f3a85f090e87057d1ee5..9ba1a39715198bb037ae8e4bfb074cb18d87cf1d 100644 (file)
@@ -228,6 +228,11 @@ endif()
 #
 # setup warnings
 #
+# We're treating -fno-common like a warning: it makes the linker more
+# strict, because on some systems the linker is *always* this strict
+string(APPEND CMAKE_C_FLAGS " -fno-common")
+string(APPEND CMAKE_CXX_FLAGS " -fno-common")
+
 if(MSVC)
     # Use the highest warning level
     if(WALL)
@@ -250,12 +255,12 @@ if(MSVC)
 
     # see https://msdn.microsoft.com/en-us/library/z78503e6.aspx
     # 4018 'expression' : signed/unsigned mismatch
-    set(WARNINGS_C "4018")
+    set(WARNINGS "4018")
     # 4090 'operation' : different 'modifier' qualifiers
     # 4101 'identifier' : unreferenced local variable
     # 4127 conditional expression is constant
     # 4244 'argument' : conversion from 'type1' to 'type2', possible loss of data
-    set(WARNINGS_C_DISABLED "4090 4101 4127 4244")
+    set(WARNINGS_DISABLED "4090 4101 4127 4244")
     # 4002 too many actual parameters for macro 'identifier'
     # 4003 not enough actual parameters for macro 'identifier'
     # 4013 'function' undefined; assuming extern returning int
@@ -264,26 +269,92 @@ if(MSVC)
     # 4047 operator' : 'identifier1' differs in levels of indirection from 'identifier2'
     # 4114 same type qualifier used more than once
     # 4133 'type' : incompatible types - from 'type1' to 'type2'
-    set(WARNINGS_C_ERRORS "4002 4003 4013 4028 4031 4047 4114 4133")
+    set(WARNINGS_ERRORS "4002 4003 4013 4028 4031 4047 4114 4133")
     if(DBUS_MSVC_ANALYZE AND MSVC_VERSION GREATER 1600)
         string(APPEND CMAKE_C_FLAGS " /analyze")
     endif()
 else()
-    set(WARNINGS_C "sign-compare")
-    set(WARNINGS_C_DISABLED "")
-    set(WARNINGS_C_ERRORS
-        missing-prototypes
-        strict-prototypes
+    set(WARNINGS
+        all
+        array-bounds
+        cast-align
+        char-subscripts
         declaration-after-statement
+        double-promotion
+        duplicated-branches
+        duplicated-cond
+        extra
+        float-equal
+        format-nonliteral
+        format-security
+        format=2
         implicit-function-declaration
+        init-self
+        inline
+        jump-misses-init
+        logical-op
+        missing-declarations
+        missing-format-attribute
+        missing-include-dirs
+        missing-noreturn
+        missing-prototypes
+        nested-externs
+        no-error=missing-field-initializers
+        no-error=unused-label
+        no-error=unused-parameter
+        no-missing-field-initializers
+        no-unused-label
+        no-unused-parameter
+        null-dereference
+        old-style-definition
+        packed
+        pointer-arith
+        pointer-sign
+        redundant-decls
+        restrict
+        return-type
+        shadow
+        sign-compare
+        strict-aliasing
+        strict-prototypes
+        switch-default
+        switch-enum
         undef
+        unused-but-set-variable
+        write-strings
     )
-    set(WARNINGS_CXX_ERRORS
-        undef
+    set(WARNINGS_DISABLED
+        error=overloaded-virtual
+        error=missing-field-initializers
+        error=unused-parameter
+        unused-parameter
+    )
+    set(WARNINGS_ERRORS
     )
 endif()
-generate_warning_cflags(WARNINGS_CFLAGS "${WARNINGS_C}" "${WARNINGS_C_DISABLED}" "${WARNINGS_C_ERRORS}")
-generate_warning_cflags(WARNINGS_CXXFLAGS "${WARNINGS_CXX}" "${WARNINGS_CXX_DISABLED}" "${WARNINGS_CXX_ERRORS}")
+
+generate_compiler_warning_flags(
+    RESULTVAR
+        WARNINGS_CFLAGS
+    WARNINGS
+        ${WARNINGS}
+        pointer-sign
+    DISABLED
+        ${WARNINGS_DISABLED}
+    ERRORS
+        ${WARNINGS_ERRORS}
+)
+generate_compiler_warning_flags(
+    RESULTVAR
+        WARNINGS_CXXFLAGS
+    WARNINGS
+        ${WARNINGS}
+    DISABLED
+        ${WARNINGS_DISABLED}
+    ERRORS
+        ${WARNINGS_ERRORS}
+)
+
 string(APPEND CMAKE_C_FLAGS " ${WARNINGS_CFLAGS}")
 string(APPEND CMAKE_CXX_FLAGS " ${WARNINGS_CFLAGS}")
 
index 6d8e5809dee15a6a6b4d4a879d5f25bae01f917c..92fe9d816779c731e52d65cecd39ae7607bcd614 100644 (file)
@@ -120,18 +120,25 @@ macro(add_session_test_executable _target _source)
     )
 endmacro()
 
-#
 # 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()
+# Options:
+#   [DISABLED <list>] list of warnings to disable
+#   [ERRORS <list>] list of warnings to report as error
+#   RESULTVAR <var> variable name to get results
+#   WARNINGS <list> list of warnings to add
+#
+macro(generate_compiler_warning_flags)
+    # Support if() IN_LIST operator
+    cmake_policy(SET CMP0057 NEW)
+    set(options)
+    set(oneValueArgs RESULTVAR)
+    set(multiValueArgs WARNINGS DISABLED ERRORS)
+    cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
+    unset(USED)
+    unset(USED_WARNINGS)
+    unset(USED_DISABLED)
+
     if(MSVC)
         # level 1 is default
         set(enabled_prefix "/w1")
@@ -144,33 +151,40 @@ macro(generate_warning_cflags target warnings disabled_warnings error_warnings)
     endif()
 
     set(temp)
-    string(REPLACE " " ";" warnings_list "${warnings}")
-    foreach(warning ${warnings_list})
-        string(STRIP ${warning} _warning)
-        if(_warning)
-            set(temp "${temp} ${enabled_prefix}${_warning}")
+    foreach(warning ${ARGS_ERRORS})
+        set(temp "${temp} ${error_prefix}${warning}")
+        list(APPEND USED ${warning})
+    endforeach()
+    foreach(warning ${ARGS_WARNINGS})
+        if(warning IN_LIST ARGS_ERRORS)
+            message(WARNING "warning '${warning}' already specified as error, ignored")
+        elseif(warning IN_LIST ARGS_DISABLED)
+            message(WARNING "warning '${warning}' already specified as disabled, ignored")
+        elseif(NOT warning IN_LIST USED)
+            set(temp "${temp} ${enabled_prefix}${warning}")
+            list(APPEND USED_WARNINGS ${warning})
+            list(APPEND USED ${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}")
+    foreach(warning ${ARGS_DISABLED})
+        if(warning IN_LIST ARGS_ERRORS)
+            message(WARNING "disabled warning '${warning}' already specified as error, ignored")
+        elseif(warning IN_LIST ARGS_WARNINGS)
+            message(WARNING "disabled warning '${warning}' already specified as warning, ignored")
+        elseif(NOT warning IN_LIST USED)
+            set(temp "${temp} ${disabled_prefix}${warning}")
+            list(APPEND USED_DISABLED ${warning})
+            list(APPEND USED ${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()
+    foreach(warning ${ARGS_ERRORS})
+        set(temp "${temp} ${error_prefix}${warning}")
     endforeach()
-    set(${target} "${temp}")
-    if(DEBUG_MACROS)
-        message("generate_warning_cflags return: ${${target}}")
-    endif()
+    set(${ARGS_RESULTVAR} "${temp}")
+    message(STATUS "effectively used warnings for '${ARGS_RESULTVAR}': ${USED_WARNINGS}")
+    message(STATUS "effectively used disabled warnings for '${ARGS_RESULTVAR}': ${USED_DISABLED}")
 endmacro()
 
 #