From 2a2ec88ac16cca2e41261b19e5673e04db071f66 Mon Sep 17 00:00:00 2001
From: chrfranke <chrfranke@4ea69e1a-61f1-4043-bf83-b5c94c648137>
Date: Fri, 4 Dec 2009 17:05:21 +0000
Subject: [PATCH] configure.in: Add '-fno-strict-aliasing' to CXXFLAGS if
 supported (ticket #23).

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@2992 4ea69e1a-61f1-4043-bf83-b5c94c648137
---
 smartmontools/CHANGELOG    |  5 +++++
 smartmontools/configure.in | 19 +++++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/smartmontools/CHANGELOG b/smartmontools/CHANGELOG
index 9106eb2f9..44c12131f 100644
--- a/smartmontools/CHANGELOG
+++ b/smartmontools/CHANGELOG
@@ -43,6 +43,11 @@ NOTES FOR FUTURE RELEASES: see TODO file.
 
 <DEVELOPERS: ADDITIONS TO THE CHANGE LOG GO JUST BELOW HERE, PLEASE>
 
+  [CF] configure.in: Add '-fno-strict-aliasing' to CXXFLAGS if supported.
+       This suppresses gcc 4.4.1 warnings on Linux and avoids possible
+       unsafe optimizations (ticket #23).
+       Patch was provided by Manfred Schwarb.
+
   [CF] Avoid truncation of configure arguments in '-V' output.
 
   [AS] Added USB IDs of WD Passport USB Portable
diff --git a/smartmontools/configure.in b/smartmontools/configure.in
index e33fa827e..b0d9056fb 100644
--- a/smartmontools/configure.in
+++ b/smartmontools/configure.in
@@ -310,8 +310,8 @@ AM_CONDITIONAL(OS_SOLARIS, [echo $host_os | grep '^solaris' > /dev/null])
 AM_CONDITIONAL(OS_WIN32_MINGW, [echo $host_os | grep '^mingw' > /dev/null])
 AM_CONDITIONAL(OS_FREEBSD, [echo $host_os | grep '^freebsd' > /dev/null])
 
-dnl Add -Wall and -W if using gcc and its not already specified.
-if test "x$GCC" = "xyes"; then
+dnl Add -Wall and -W if using g++ and its not already specified.
+if test "$GXX" = "yes"; then
   if test -z "`echo "$CXXFLAGS" | grep "\-Wall" 2> /dev/null`" ; then
       CXXFLAGS="$CXXFLAGS -Wall"
   fi
@@ -324,6 +324,21 @@ if test "x$GCC" = "xyes"; then
       # MinGW uses MSVCRT.DLL which uses printf format "%I64d" and not "%lld" for int64_t
       CXXFLAGS="$CXXFLAGS -Wno-format";;
   esac
+
+  # Disable strict aliasing rules by default (see ticket #23).
+  if test -z "`echo "$CXXFLAGS" | grep "\-f[[no-]]*strict-aliasing" 2> /dev/null`" ; then
+    AC_MSG_CHECKING([whether g++ supports -fno-strict-aliasing])
+    ac_save_CXXFLAGS="$CXXFLAGS"
+    CXXFLAGS="-fno-strict-aliasing"
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
+      [gcc_have_fno_strict_aliasing=yes], [gcc_have_fno_strict_aliasing=no])
+    CXXFLAGS="$ac_save_CXXFLAGS"
+    if test "$gcc_have_fno_strict_aliasing" = "yes"; then
+      CXXFLAGS="$CXXFLAGS -fno-strict-aliasing"
+    fi
+    AC_MSG_RESULT([$gcc_have_fno_strict_aliasing])
+  fi
+
 else
  dnl We are NOT using gcc, so enable host-specific compiler flags
  case "${host}" in
-- 
GitLab