Improve Sun Studio support.  Patch from Monty Taylor.
diff --git a/configure.ac b/configure.ac
index 42a7fe7..b847f3d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -14,15 +14,20 @@
 # the size of one file name in the dist tarfile over the 99-char limit.)
 AC_INIT([Protocol Buffers],[2.1.1-pre],[protobuf@googlegroups.com],[protobuf])
 
-# Detect whether the user specified their own compilation flags.  If so then
-# we want to respect their decision, otherwise we will twiddle them later.
-AS_IF([test "$CXXFLAGS" = ""],[
-  protobuf_default_cxxflags=yes
-])
 
 AC_CONFIG_SRCDIR(src/google/protobuf/message.cc)
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_MACRO_DIR([m4])
+
+# autoconf's default CXXFLAGS are usually "-g -O2".  These aren't necessarily
+# the best choice for libprotobuf.
+AS_IF([test "x${ac_cv_env_CFLAGS_set}" = "x"],
+      [CFLAGS=""])
+AS_IF([test "x${ac_cv_env_CXXFLAGS_set}" = "x"],
+      [CXXFLAGS=""])
+  
+AC_CANONICAL_TARGET
+
 AM_INIT_AUTOMAKE
 
 AC_ARG_WITH([zlib],
@@ -40,26 +45,15 @@
 AC_PROG_CXX
 AC_LANG([C++])
 ACX_USE_SYSTEM_EXTENSIONS
-AC_PROG_LIBTOOL
 AM_CONDITIONAL(GCC, test "$GCC" = yes)   # let the Makefile know if we're gcc
 
-# autoconf's default CXXFLAGS are usually "-g -O2".  These aren't necessarily
-# the best choice for libprotobuf.
+# test_util.cc takes forever to compile with GCC and optimization turned on.
 AC_MSG_CHECKING([C++ compiler flags...])
-AS_IF([test "$protobuf_default_cxxflags" = "yes"],[
-
-  # test_util.cc takes forever to compile with GCC and optimization turned on.
-  # But we cannot override anything that is part of CXXFLAGS since it is the
-  # last thing added to the command line.  The automake docs insist that you
-  # should never want to override CXXFLAGS because they represent the intent of
-  # the user, and the user knows best.  But if the user actually did not set
-  # any CXXFLAGS, then AC_PROG_CXX sets them to a rather arbitrary default.
-  # That's not user intent at all, but automake still treats it like it is.
-  # Grr.  Anyway, getting back to the point, this hack here strips out the -O
-  # flag from autoconf's defaults and puts it into another variable so that
-  # we can override it.  BTW, m4 escaping sucks.
-  PROTOBUF_OPT_FLAG=`echo "$CXXFLAGS" | grep -o '\-O@<:@0-9@:>@\?'`
-  CXXFLAGS=`echo "$CXXFLAGS" | sed -e 's/ \?-O@<:@0-9@:>@\?//g'`
+AS_IF([test "x${ac_cv_env_CXXFLAGS_set}" = "x"],[
+  AS_IF([test "$GCC" = "yes"],[
+    PROTOBUF_OPT_FLAG="-O2"
+    CXXFLAGS="${CXXFLAGS} -g"
+  ])
 
   # Protocol Buffers contains several checks that are intended to be used only
   # for debugging and which might hurt performance.  Most users are probably
@@ -68,7 +62,6 @@
 
   AC_MSG_RESULT([use default: $PROTOBUF_OPT_FLAG $CXXFLAGS])
 ],[
-  PROTOBUF_OPT_FLAG=
   AC_MSG_RESULT([use user-supplied: $CXXFLAGS])
 ])
 
@@ -76,6 +69,10 @@
 
 ACX_CHECK_SUNCC
 
+# Have to do libtool after SUNCC, other wise it "helpfully" adds Crun Cstd 
+# to the link
+AC_PROG_LIBTOOL
+
 # Checks for header files.
 AC_HEADER_STDC
 AC_CHECK_HEADERS([fcntl.h inttypes.h limits.h stdlib.h unistd.h])
diff --git a/m4/acx_check_suncc.m4 b/m4/acx_check_suncc.m4
index 795a42c..3464a84 100644
--- a/m4/acx_check_suncc.m4
+++ b/m4/acx_check_suncc.m4
@@ -14,22 +14,58 @@
 
   AC_LANG_PUSH([C++])
   AC_CHECK_DECL([__SUNPRO_CC], [SUNCC="yes"], [SUNCC="no"])
-
-  AS_IF([test "$SUNCC" = "yes"],[
-    isainfo_k=`isainfo -k`
-    AS_IF([test "$target_cpu" = "sparc"],[
-      MEMALIGN_FLAGS="-xmemalign=8s"
-      IS_64="-m64"
-      LDFLAGS="${LDFLAGS} -L/usr/lib/${isainfo_k} -L/usr/local/lib/${isainfo_k}"
-    ],[
-      AS_IF([test "$isainfo_k" = "amd64"],[
-        IS_64="-m64"
-        LDFLAGS="${LDFLAGS} -L/usr/lib/${isainfo_k} -L/usr/local/lib/${isainfo_k}"
-      ])
-    ])
-
-    CFLAGS="-g -xO4 -xlibmil -xdepend -Xa -mt -xstrconst ${IS_64} ${MEMALIGN_FLAGS} $CFLAGS"
-    CXXFLAGS="-g -xO4 -xlibmil -mt ${IS_64} ${MEMALIGN_FLAGS} -xlang=c99 -compat=5 -library=stlport4 -template=no%extdef $CXXFLAGS"
-  ])
   AC_LANG_POP()
+
+
+  AC_ARG_ENABLE([64bit-solaris],
+    [AS_HELP_STRING([--disable-64bit-solaris],
+      [Build 64 bit binary on Solaris @<:@default=on@:>@])],
+             [ac_enable_64bit="$enableval"],
+             [ac_enable_64bit="yes"])
+
+  AS_IF([test "$SUNCC" = "yes" -a "x${ac_cv_env_CXXFLAGS_set}" = "x"],[
+    dnl Sun Studio has a crashing bug with -xO4 in some cases. Keep this
+    dnl at -xO3 until a proper test to detect those crashes can be done.
+    CXXFLAGS="-g0 -xO3 -xlibmil -xdepend -xbuiltin -mt -compat=5 -library=stlport4 -template=no%extdef ${CXXFLAGS}"
+  ])
+
+  case $host_os in
+    *solaris*)
+      AC_CHECK_PROGS(ISAINFO, [isainfo], [no])
+      AS_IF([test "x$ISAINFO" != "xno"],
+            [isainfo_b=`${ISAINFO} -b`],
+            [isainfo_b="x"])
+
+      AS_IF([test "$isainfo_b" != "x"],[
+
+        isainfo_k=`${ISAINFO} -k`
+
+        AS_IF([test "x$ac_enable_64bit" = "xyes"],[
+
+          AS_IF([test "x${ac_cv_env_LDFLAGS_set}" = "x"],[
+            LDFLAGS="-L/usr/local/lib/${isainfo_k} ${LDFLAGS}"
+          ])
+
+          AS_IF([test "x$libdir" = "x\${exec_prefix}/lib"],[
+           dnl The user hasn't overridden the default libdir, so we'll
+           dnl the dir suffix to match solaris 32/64-bit policy
+           libdir="${libdir}/${isainfo_k}"
+          ])
+
+          dnl This should just be set in CPPFLAGS and in LDFLAGS, but libtool
+          dnl does the wrong thing if you don't put it into CXXFLAGS. sigh.
+          AS_IF([test "x${ac_cv_env_CXXFLAGS_set}" = "x"],[
+            CXXFLAGS="${CXXFLAGS} -m64"
+            ac_cv_env_CXXFLAGS_set=set
+            ac_cv_env_CXXFLAGS_value='-m64'
+          ])
+
+          AS_IF([test "$target_cpu" = "sparc" -a "x$SUNCC" = "xyes" ],[
+            CXXFLAGS="-xmemalign=8s ${CXXFLAGS}"
+          ])
+        ])
+      ])
+    ;;
+  esac
+
 ])