Use -D_FORTIFY_SOURCE=2 only if the gc optimization level is greater than 0.
authorwmeier <wmeier@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 7 Jul 2012 22:35:55 +0000 (22:35 +0000)
committerwmeier <wmeier@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 7 Jul 2012 22:35:55 +0000 (22:35 +0000)
Fixes Bug #7449: https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=7449

-----
Issue:

Building Wireshark with '-O0 -D_FORTIFY_SOURCE=2 ...' fails

The warning [error] message:

/usr/include/features.h:314:4: error: #warning _FORTIFY_SOURCE requires
compiling with optimization (-O) [-Werror=cpp]`

A bit of research shows that this warning was added to a recent version of
glibc (on at least Fedora).

 See: http://sourceware.org/bugzilla/show_bug.cgi?id=13979

The warning message occurs if -D_FORTIFY_SOURCE=... is used and the gcc
'optimization level' == 0 (-O0).

Unfortunately when building with -O0 this warning message:

1. Causes compiles to fail (if -Werror [stop  on warning])
2. Causes ./configure to fail with an (incorrect) message
   about the pcap header being older than the libpcap version.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@43601 f5534014-38df-0310-8fa8-9805f1628bb7

acinclude.m4
configure.in

index 1aa56a9f14b6476503861dc70b13b869cc7bd962..81e365784a6838fa707d4c35873055120d45c3ce 100644 (file)
@@ -1674,6 +1674,52 @@ else
 fi
 ])
 
+# AC_WIRESHARK_GCC_FORTIFY_SOURCE_CHECK
+#
+# Checks if '-D_FORTIFY_SOURCE=...' is OK to use in CPPFLAGS.
+#  Use '-D_FORTIFY_SOURCE=...' in CPPFLAGS only if the GCC 'optimization level' is > 0.
+#  The use of '-D_FORTIFY_SOURCE=...' will cause a warning with at least some versions
+#    of glibc if the  GCC "optimization level" is 0 (default or -O or -O0)
+#    when using GCC to compile a source file which references the macro definition.
+#
+# See: http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
+# See: http://sourceware.org/bugzilla/show_bug.cgi?id=13979
+#
+#   We'll use '-D_FORTIFY_SOURCE=2' only if there's no warning; Among other things this means
+#    that the  use of '-D_FORTIFY_SOURCE=2' with '-Werror' and '-O0' won't cause
+#    the compiler to stop on error.
+#   Assumption: CFLAGS already contains whatever optimization option including none) is
+#    to be used.
+#
+
+AC_DEFUN([AC_WIRESHARK_GCC_FORTIFY_SOURCE_CHECK],
+[
+if test "x$GCC" = "xyes" -o "x$CC" = "xclang" ; then
+  AC_MSG_CHECKING([whether -D_FORTIFY_SOURCE=... can be used (without generating a warning)])
+  CFLAGS_saved="$CFLAGS"
+  CPPFLAGS_saved="$CPPFLAGS"
+  CFLAGS="$CFLAGS -Werror"
+  CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2"
+  AC_COMPILE_IFELSE([
+    AC_LANG_SOURCE([[
+                  #include <stdio.h>
+                      int foo;
+                  ]])],
+                  [
+                    AC_MSG_RESULT(yes)
+                    #
+                    # (CPPFLAGS contains _D_FORTIFY_SOURCE=2)
+                    #
+                  ],
+                  [
+                    AC_MSG_RESULT(no)
+                    # Remove -D_FORTIFY_SOURCE=2
+                    CPPFLAGS="$CPPFLAGS_saved"
+                  ])
+  CFLAGS="$CFLAGS_saved"
+fi
+])
+
 #
 # AC_WIRESHARK_OSX_INTEGRATION_CHECK
 #
index b6f9eeadb01cd24644784514733d389f32e20f3f..490571bed27c2c8083e36fe810ef76cc23cc0b32 100644 (file)
@@ -436,12 +436,14 @@ else
   CPPFLAGS="-D_U_=\"\" $CPPFLAGS"
 fi
 
-# If we're running GCC or CLang, always use FORTIFY_SOURCE=2
+# If we're running GCC or CLang, use FORTIFY_SOURCE=2
+#  (only if the GCC 'optimization level' > 0).
+#
 # See: http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
-# Note: FORTIFY_SOURCE is only effective for gcc -O2 (and -O1 ?)
-if test "x$GCC" = "xyes" -o "x$CC" = "xclang" ; then
-  CPPFLAGS="-D_FORTIFY_SOURCE=2 $CPPFLAGS"
-fi
+# See: http://sourceware.org/bugzilla/show_bug.cgi?id=13979
+#
+# Note: FORTIFY_SOURCE is only effective for gcc optimization level > 0 (-O1, etc)
+AC_WIRESHARK_GCC_FORTIFY_SOURCE_CHECK
 
 #
 # If the compiler supports GCC-style flags, enable a barrier "stop on