diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -1,1 +1,2 @@
 src
+patches/upstream
diff --git a/.hgtags b/.hgtags
--- a/.hgtags
+++ b/.hgtags
@@ -3,3 +3,4 @@
 c24b1e8f4ba1fce31bb2ea48bb069bedaebe3d32 mpfr-2.4.2.p2
 ad149c890ee193fbe7d2fdbebf94dee20eb6a612 mpfr-3.1.0
 834f2c4e0347c2e4249d94188203d93cea44d92a mpfr-3.1.0.p0
+b9faebc37a62ab557543b26b27e3747429672552 mpfr-3.1.0.p1
diff --git a/SPKG.txt b/SPKG.txt
--- a/SPKG.txt
+++ b/SPKG.txt
@@ -34,19 +34,58 @@
 
 The MPFR website is located at http://mpfr.org/
 
-The MPFR team can be contact via the MPFR mailing list: mpfr@loria.fr
+The MPFR team can be contacted via the MPFR mailing list: mpfr@loria.fr
 
 == Dependencies ==
 
- * MPIR
+ * GMP/MPIR
+ * GNU patch
 
-== IMPORTANT ==
-When building Sage binaries for widespead distribution, set the variable
-INCLUDE_MPFR_PATCH to 1, to include a patch, which can cause failures
-on sun4v machines.
+== Special Update/Build Instructions ==
+
+ * When building Sage binaries for wide-spread distribution, set the variable
+   `INCLUDE_MPFR_PATCH` to 1, to include a patch which avoids failures on sun4v
+   machines.  (This applies to at least SunOS / Solaris.)
+ * Check whether there are new "official" upstream patches, i.e., such put into
+   http://www.mpfr.org/mpfr-current/allpatches (if our version is the latest
+   released one); see also http://www.mpfr.org/mpfr-current/#bugs .
+   (As of April 13th 2012, our current version is 3.1.0-p8; the sources in
+   `src/` are kept vanilla, the cumulative upstream patch is in
+   `patches/upstream/` and gets applied by `spkg-install`.)
+ * Make sure the patches still apply.
+ * Make sure MPFR's settings of `CC` and `CFLAGS` still get properly extracted,
+   currently from its `config.log` in the `src/` directory.
+ * We should remove the `configure` option `--disable-thread-safe` in case
+   the issues without that have meanwhile been fixed.  (Then we should
+   actually pass `--enable-thread-safe`.)
+ * Check whether the work-around for GCC 4.7 on ia64 is still appropriate, i.e.
+   adapt it in case the issue has been fixed in later GCC versions of the 4.7
+   release series.  (Although the bug currently affects only GCC 4.7.0, it
+   seems unlikely that it will be fixed soon, hence we apply the work-around
+   for any GCC 4.7.x version on ia64.)
+   See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48496 for details and the
+   current status of that bug. 
 
 == Changelog ==
 
+=== mpfr-3.1.0.p1 (Leif Leonhardy, April 13th 2012) ===
+ * #12837: Work around GCC 4.7.0 bug on ia64 (Itanium), unfortunately by almost
+   completely disabling optimization on that platform.  (MPFR is also a pre-
+   requisite for building the GCC spkg; after that has been built, MPFR will
+   get rebuilt with normal optimization.)
+ * Use `patch` to apply patches.
+ * Apply all recent "official" upstream patches; the resulting MPFR version
+   then is 3.1.0-p8.
+ * Properly quote the parameter to the `configure` option `--libdir`.
+ * Export `CFLAGS`! Previously MPFR would not even have used "its own" flags
+   (extracted from `config.status` after configuring it with `CC` and `CFLAGS`
+   unset) if `CFLAGS`, set in its `spkg-install`, weren't already exported.
+   (Also export `CPPFLAGS` and `LDFLAGS`, to which `$CFLAG64` is now also added
+   if `SAGE64` is `yes`.)
+ * Add `$CFLAG64` to *required* `CFLAGS` if `SAGE64=yes`.
+ * Clean up the spkg; redirect all warnings and error messages to stderr; add
+   some messages.
+
 === mpfr-3.1.0.p0 (Jean-Pierre Flori, February 22nd, 2012) ===
  * #11666: Further cleanups and rebase on #12131, #12366 and #12548.
  * Let configure get the MPIR flags.
diff --git a/patches/mpn_exp.c b/patches/mpn_exp.c
deleted file mode 100644
--- a/patches/mpn_exp.c
+++ /dev/null
@@ -1,185 +0,0 @@
-/* mpfr_mpn_exp -- auxiliary function for mpfr_get_str and mpfr_set_str
-
-Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
-Contributed by the Arenaire and Caramel projects, INRIA.
-
-This file is part of the GNU MPFR Library.
-
-The GNU MPFR Library is free software; you can redistribute it and/or modify
-it under the terms of the GNU Lesser General Public License as published by
-the Free Software Foundation; either version 3 of the License, or (at your
-option) any later version.
-
-The GNU MPFR Library is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
-License for more details.
-
-You should have received a copy of the GNU Lesser General Public License
-along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
-http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
-51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
-
-
-#define MPFR_NEED_LONGLONG_H
-#include "mpfr-impl.h"
-
-/* this function computes an approximation of b^e in {a, n}, with exponent
-   stored in exp_r. The computed value is rounded toward zero (truncated).
-   It returns an integer f such that the final error is bounded by 2^f ulps,
-   that is:
-   a*2^exp_r <= b^e <= 2^exp_r (a + 2^f),
-   where a represents {a, n}, i.e. the integer
-   a[0] + a[1]*B + ... + a[n-1]*B^(n-1) where B=2^GMP_NUMB_BITS
-
-   Return -1 is the result is exact.
-   Return -2 if an overflow occurred in the computation of exp_r.
-*/
-
-long
-mpfr_mpn_exp (mp_limb_t *a, mpfr_exp_t *exp_r, int b, mpfr_exp_t e, size_t n)
-{
-  mp_limb_t *c, B;
-  mpfr_exp_t f, h;
-  int i;
-  unsigned long t; /* number of bits in e */
-  unsigned long bits;
-  size_t n1;
-  unsigned int error;           /* (number - 1) of loop a^2b inexact */
-                                 /* error == t means no error */
-  int err_s_a2 = 0;
-  int err_s_ab = 0;              /* number of error when shift A^2, AB */
-  MPFR_TMP_DECL(marker);
-
-  MPFR_ASSERTN(e > 0);
-  MPFR_ASSERTN((2 <= b) && (b <= 62));
-
-  MPFR_TMP_MARK(marker);
-
-  /* initialization of a, b, f, h */
-
-  /* normalize the base */
-  B = (mp_limb_t) b;
-  count_leading_zeros (h, B);
-
-  bits = GMP_NUMB_BITS - h;
-
-  B = B << h;
-  h = - h;
-
-  /* allocate space for A and set it to B */
-  c = MPFR_TMP_LIMBS_ALLOC (2 * n);
-  a [n - 1] = B;
-  /* The following couple of lines work around a bug seen so far only on
-  a Sun T5240 with a pair of T2+ processors. It only happens with some versions
-  of gcc, so the patch will only be used on sun4v machines by default, but
-  and enviroment variable will allow the patch to be included or not. 
-  There would be an advantage in including it even on sun4u machines, 
-  if it was felt the binary might be used on sun4v.
-
-  The patch to the C code was developed by Paul Zimmermann */
-
-  if (n > 1)
-    MPN_ZERO (a, n - 1);
-
-  /* initial exponent for A: invariant is A = {a, n} * 2^f */
-  f = h - (n - 1) * GMP_NUMB_BITS;
-
-  /* determine number of bits in e */
-  count_leading_zeros (t, (mp_limb_t) e);
-
-  t = GMP_NUMB_BITS - t; /* number of bits of exponent e */
-
-  error = t; /* error <= GMP_NUMB_BITS */
-
-  MPN_ZERO (c, 2 * n);
-
-  for (i = t - 2; i >= 0; i--)
-    {
-
-      /* determine precision needed */
-      bits = n * GMP_NUMB_BITS - mpn_scan1 (a, 0);
-      n1 = (n * GMP_NUMB_BITS - bits) / GMP_NUMB_BITS;
-
-      /* square of A : {c+2n1, 2(n-n1)} = {a+n1, n-n1}^2 */
-      mpn_sqr_n (c + 2 * n1, a + n1, n - n1);
-
-      /* set {c+n, 2n1-n} to 0 : {c, n} = {a, n}^2*K^n */
-
-      /* check overflow on f */
-      if (MPFR_UNLIKELY(f < MPFR_EXP_MIN/2 || f > MPFR_EXP_MAX/2))
-        {
-        overflow:
-          MPFR_TMP_FREE(marker);
-          return -2;
-        }
-      /* FIXME: Could f = 2*f + n * GMP_NUMB_BITS be used? */
-      f = 2*f;
-      MPFR_SADD_OVERFLOW (f, f, n * GMP_NUMB_BITS,
-                          mpfr_exp_t, mpfr_uexp_t,
-                          MPFR_EXP_MIN, MPFR_EXP_MAX,
-                          goto overflow, goto overflow);
-      if ((c[2*n - 1] & MPFR_LIMB_HIGHBIT) == 0)
-        {
-          /* shift A by one bit to the left */
-          mpn_lshift (a, c + n, n, 1);
-          a[0] |= mpn_lshift (c + n - 1, c + n - 1, 1, 1);
-          f --;
-          if (error != t)
-            err_s_a2 ++;
-        }
-      else
-        MPN_COPY (a, c + n, n);
-
-      if ((error == t) && (2 * n1 <= n) &&
-          (mpn_scan1 (c + 2 * n1, 0) < (n - 2 * n1) * GMP_NUMB_BITS))
-        error = i;
-
-      if (e & ((mpfr_exp_t) 1 << i))
-        {
-          /* multiply A by B */
-          c[2 * n - 1] = mpn_mul_1 (c + n - 1, a, n, B);
-          f += h + GMP_NUMB_BITS;
-          if ((c[2 * n - 1] & MPFR_LIMB_HIGHBIT) == 0)
-            { /* shift A by one bit to the left */
-              mpn_lshift (a, c + n, n, 1);
-              a[0] |= mpn_lshift (c + n - 1, c + n - 1, 1, 1);
-              f --;
-            }
-          else
-            {
-              MPN_COPY (a, c + n, n);
-              if (error != t)
-                err_s_ab ++;
-            }
-          if ((error == t) && (c[n - 1] != 0))
-            error = i;
-        }
-    }
-
-  MPFR_TMP_FREE(marker);
-
-  *exp_r = f;
-
-  if (error == t)
-    return -1; /* result is exact */
-  else /* error <= t-2 <= GMP_NUMB_BITS-2
-          err_s_ab, err_s_a2 <= t-1       */
-    {
-      /* if there are p loops after the first inexact result, with
-         j shifts in a^2 and l shifts in a*b, then the final error is
-         at most 2^(p+ceil((j+1)/2)+l+1)*ulp(res).
-         This is bounded by 2^(5/2*t-1/2) where t is the number of bits of e.
-      */
-      error = error + err_s_ab + err_s_a2 / 2 + 3; /* <= 5t/2-1/2 */
-#if 0
-      if ((error - 1) >= ((n * GMP_NUMB_BITS - 1) / 2))
-        error = n * GMP_NUMB_BITS; /* result is completely wrong:
-                                         this is very unlikely since error is
-                                         at most 5/2*log_2(e), and
-                                         n * GMP_NUMB_BITS is at least
-                                         3*log_2(e) */
-#endif
-      return error;
-    }
-}
diff --git a/patches/mpn_exp.c.patch b/patches/mpn_exp.c.patch
--- a/patches/mpn_exp.c.patch
+++ b/patches/mpn_exp.c.patch
@@ -1,5 +1,5 @@
---- ../src/src/mpn_exp.c	2011-10-03 09:17:09.000000000 +0100
-+++ mpn_exp.c	2011-12-17 09:57:30.000000000 +0000
+--- src/src/mpn_exp.c	2011-10-03 10:17:09.000000000 +0200
++++ patches/mpn_exp.c	2012-02-25 15:48:28.000000000 +0100
 @@ -70,7 +70,18 @@
    /* allocate space for A and set it to B */
    c = MPFR_TMP_LIMBS_ALLOC (2 * n);
diff --git a/spkg-check b/spkg-check
--- a/spkg-check
+++ b/spkg-check
@@ -1,18 +1,25 @@
 #!/usr/bin/env bash
 
 if [ -z "$SAGE_LOCAL" ]; then
-    echo "Error: SAGE_LOCAL undefined - exiting..."
-    echo "Maybe run 'sage -sh'?"
+    echo >&2 "Error: SAGE_LOCAL undefined - exiting..."
+    echo >&2 "Maybe run 'sage -sh'?"
     exit 1
 fi
 
 # unset RM since it messes with libtool
 unset RM
 
+# We don't have to (again) set up CFLAGS etc. here, as 'configure' puts
+# them into the Makefiles.
+
 cd src
 
+echo
+echo "Now building and running MPFR's test suite..."
 $MAKE check
 if [ $? -ne 0 ]; then
-  echo "Error: the MPFR test suite failed."
-  exit 1
+    echo >&2 "Error building or running MPFR's test suite."
+    exit 1
 fi
+echo
+echo "MPFR's test suite passed without errors."
diff --git a/spkg-install b/spkg-install
--- a/spkg-install
+++ b/spkg-install
@@ -1,8 +1,8 @@
 #!/usr/bin/env bash
 
 if [ -z "$SAGE_LOCAL" ]; then
-    echo "Error: SAGE_LOCAL undefined - exiting..."
-    echo "Maybe run 'sage -sh'?"
+    echo >&2 "Error: SAGE_LOCAL undefined - exiting..."
+    echo >&2 "Maybe run 'sage -sh'?"
     exit 1
 fi
 
@@ -12,93 +12,126 @@
 mpfr_patch()
 {
     # Apply a patch on Solaris, but only on the sun4v architecture
-    # by default. On other Solaris systems, it can be overridden
+    # by default. On other Solaris systems, it can be overridden.
+    # The patch to the C code was developed by Paul Zimmermann, the patch
+    # to the spkg-install by David Kirkby. See Ticket #6453.
+    if [ "$UNAME" = SunOS ]; then
+        # Add a patch to MPFR on Solaris if either:
+        # 1) The machine is a sun4v architecture.
+        # 2) The environment variable INCLUDE_MPFR_PATCH is set to 1 or "yes".
+        #    This will allow creating of binaries on a machine other than
+        #    sun4v, which will run on sun4v.
+        echo
+        echo "This computer is running Solaris on which some problems have been observed" 
+        echo "with the MPFR library running on a T2+ processor (sun4v architecture)."
+        echo "See http://sagetrac.org/sage_trac/ticket/6453  A patch can be applied"
+        echo "to bypass this problem.  The problem has NEVER been seen on Solaris on x86"
+        echo "or on sun4u (UltraSPARC II etc.).  To determine the architecture run:"
+        echo "    /usr/bin/arch -k"
+        echo "If you experience MPFR test failures, or you wish to ensure the binary will run"
+        echo "on a sun4v system, set the environment variable INCLUDE_MPFR_PATCH to 1, with:"
+        echo "    export INCLUDE_MPFR_PATCH=1"
+        echo "If you specifically wish to exclude the patch, set INCLUDE_MPFR_PATCH to 0 with:"
+        echo "    export INCLUDE_MPFR_PATCH=0"
+        echo "By default, the patch will only be applied on sun4v systems (T1, T2 and T2+"
+        echo "processors), but this can be overridden on other systems.  Binaries for"
+        echo "Solaris SPARC should be built with INCLUDE_MPFR_PATCH set to 1 or \"yes\","
+        echo "unless you are 100% sure they will not be used on a sun4v system."
+        echo
+        # Note: "/usr/bin/arch" will return "sun4" on any SPARC system.
+        # If given the '-k' option, aditional information will be displayed
+        # to indicate if the system is sun4m (very old), sun4u or sun4v.
 
-    # The patch to the C code was developed by Paul Zimmermann, the patch
-    # to the spkg-install by David Kirkby. See Ticket #6453
+        # Set PATCH_MPFR to something other than 0 or 1 if it is unset:
+        PATCH_MPFR="${INCLUDE_MPFR_PATCH-2}"
+        do_apply=false
+        case "$PATCH_MPFR" in
+          1|yes)
+            echo "Since INCLUDE_MPFR_PATCH was set to 1 or \"yes\", the MPFR library will be"
+            echo "patched.  The binaries should be safe on any Solaris system."
+            do_apply=true;;
+          0|no)
+            echo "Since INCLUDE_MPFR_PATCH was set to 0 or \"no\", the MPFR library will not"
+            echo "be specifically patched.  You would be unwise to distribute SPARC binaries"
+            echo "unless you are sure they will not be used on sun4v systems.";;
+          2) # INCLUDE_MPFR_PATCH isn't set at all.
+            if [ "`arch -k`" = sun4v ]; then
+                echo "This is a sun4v system, so the MPFR library will be patched.  The binaries"
+                echo "should run correctly on any SPARC system whose operating system is"
+                echo "no older than the system used to build the binaries."
+                do_apply=true
+            else
+                echo "Since this is not a sun4v system, the MPFR binary will not be patched."
+                # Issue a warning on SPARC, but not (e.g.) x86 systems.
+                if [ "`arch`" = sun4 ]; then
+                    echo >&2 "WARNING: Problems may occur if you try to run this SPARC binary on a sun4v"
+                    echo >&2 "system (T1, T2 or T2+ processors).  Binaries created like this should not"
+                    echo >&2 "be distributed unless you know the end users' systems will not be sun4v."
+                    echo >&2 "Set INCLUDE_MPFR_PATCH to 1, to include the patch, if you are unsure."
+                fi
+            fi;;
+          *)
+            echo >&2 "Warning: The environment variable INCLUDE_MPFR_PATCH was incorrectly"
+            echo >&2 "set to \"$PATCH_MPFR\".  The MPFR library will be patched as a precaution."
+            do_apply=true
+        esac
+        if $do_apply && ! patch -p1 < ../patches/mpn_exp.c.patch; then
+            echo >&2 "Error: Patch for Solaris SPARC ('mpn_exp.c.patch') failed to apply."
+            exit 1
+        fi
+        echo
+    fi # $UNAME=SunOS
 
-    if [ `uname` = "SunOS" ]; then
-       # Add a patch to MPFR on Solaris if either:
-       # 1) The machine is a sun4v architecture
-       # 2) The environment variable INCLUDE_MPFR_PATCH is set to 1
-       #    This will allow creating of binaries on a machine other than
-       #    sun4v, which will run on sun4v.
-       echo "\nThis computer is running Solaris on which some problems have been observed" 
-       echo "with the MPFR library running on a T2+ processor (sun4v architecture)."
-       echo "See http://sagetrac.org/sage_trac/ticket/6453 A patch can be applied"
-       echo "to bypass this problem. The problem has NEVER been seen on Solaris running x86"
-       echo "or on sun4u (UltraSPARC II etc). To determine the architecture run:"
-       echo "$ /usr/bin/arch -k\n"
-       echo "If you experience MPFR test failures, or you wish to ensure the binary will run"
-       echo "on a sun4v system, set the environment variable INCLUDE_MPFR_PATCH to 1, with:"
-       echo "$ export INCLUDE_MPFR_PATCH=1\n"
-       echo "If you specifically wish to exclude the patch, set INCLUDE_MPFR_PATCH to 0 with"
-       echo "$ export INCLUDE_MPFR_PATCH=0\n"
-       echo "By default, the patch will only be applied on sun4v systems (T1, T2 and T2+)"
-       echo "processors but this can be over-ridden on other systems. Binaries for"
-       echo "Solaris SPARC binaries should be built with INCLUDE_MPFR_PATCH set to 1"
-       echo "unless you are 100% sure they will not be used on a sun4v system.\n"
+    # Apply official upstream patches (if any):
+    ls ../patches/upstream/*.patch &>/dev/null &&
+    echo "Applying official upstream patches..." &&
+    for patch in ../patches/upstream/*.patch; do
+        basename "$patch"
+        # We don't need '-N' and '-Z' here, options suggested by upstream:
+        if ! patch -p1 <"$patch"; then
+            echo >&2 "Error: '$patch' failed to apply."
+            exit 1
+        fi
+    done &&
+    echo
 
-       # Note, "/usr/bin/arch" will return "sun4" on any SPARC system
-       # If given the '-k' option, aditional information will be displayed
-       # to indicate if the system is sun4m (very old), sun4u or sun4v
-
-       # Set the variable PATCH_MPFR to something other than 0 or 1"
-       PATCH_MPFR="${INCLUDE_MPFR_PATCH-2}"
-       if [ $PATCH_MPFR = "1" ] ; then
-          echo "Since INCLUDE_MPFR_PATCH was set to 1, the MPFR library will be patched."
-          echo "The binaries should be safe on any Solaris system"
-          cp ../patches/mpn_exp.c src/mpn_exp.c
-       elif [ $PATCH_MPFR = "0" ] ; then
-          echo "Since INCLUDE_MPFR_PATCH was set to 0, the MPFR library will not"
-          echo "be patched. You would be unwise to distribute SPARC binaries"
-          echo "unless you are sure they will not be used on sun4v systems"
-       elif [  $PATCH_MPFR = "2" -a `arch -k` = "sun4v" ] ; then
-          echo "This is a sun4v system, so the MPFR library will be patched. The binaries"
-          echo "should run correctly on any SPARC system whose operating system is"
-          echo "no older than the system used to build the binaries."
-          cp ../patches/mpn_exp.c src/mpn_exp.c
-       elif [ $PATCH_MPFR = "2" -a `arch -k` != "sun4v" ] ; then
-          echo "Since this is not a sun4v system, the MPFR binary will not"
-          echo "be patched"
-          # Issue a warning on SPARC, but not x86 systems.
-          if [ `arch` = "sun4" ]; then
-            echo "WARNING: Problems may occur if you try to run this SPARC"
-            echo "binary on a sun4v system (T1, T2 or T2+ processors). Binaries created like"
-            echo "this should not distributed unless you know the end users system will not"
-            echo "be sun4v. Set INCLUDE_MPFR_PATCH to 1, to include the patch, if you are unsure."
-          fi
-       else
-          echo "The environment variable INCLUDE_MPFR_PATCH was set incorrectly The MPFR"
-          echo "library will be patched as a precaution."
-          cp ../patches/mpn_exp.c src/mpn_exp.c
-       fi
-    fi
-
+    # Further patches for Sage should get applied here, i.e., after all
+    # upstream patches have been applied, which also means they should be
+    # based on the already (upstream-)patched upstream sources.
 }
 
 mpfr_get_upstream_selected_cflags() # Get MPFR's choice on empty CC and CFLAGS.
 {
     if [ $# -ne 1 ]; then
-        echo "Error: mpfr_get_upstream_selected_cflags() requires 'file' parameter."
+        echo >&2 "Error: mpfr_get_upstream_selected_cflags() requires 'file' parameter."
         exit 1
     fi
-    header_file=$1
+    config_file=$1
+    # Note: We currently extract MPFR's settings of CC and CFLAGS from
+    #       'config.status', not 'mpfr.h' (which are both created by 'configure').
     mpfr_cc_pat="s/^CC='\([^']*\)'/\1/p"
     mpfr_cflags_pat="s/^CFLAGS='\([^']*\)'/\1/p"
-    if ! [ -f "$header_file" ]; then
+    if ! [ -f "$config_file" ]; then
         upstream_cc=""
         upstream_cflags=""
         return 1
     fi
-    upstream_cc=`sed -n -e "$mpfr_cc_pat" "$header_file"`
-    upstream_cflags=`sed -n -e "$mpfr_cflags_pat" "$header_file"`
+    upstream_cc=`sed -n -e "$mpfr_cc_pat" "$config_file"`
+    upstream_cflags=`sed -n -e "$mpfr_cflags_pat" "$config_file"`
+    # CFLAGS might probably be empty, CC shouldn't:
+    if [ -z "$upstream_cc" ]; then
+        # A warning will be issued by the code calling this function.
+        return 1
+    fi
     return 0
 }
 
 mpfr_configure()
 {
-    sage_cc=$CC # Save Sage CC, because we need to unset it later.
+    ###########################################################################
+    # Set up environment variables:
+    ###########################################################################
+
     user_cflags=$CFLAGS # Save them. 'sage-env' sets CC, but not CFLAGS.
     required_cflags="" # Additional mandatory settings required by Sage, accumulated below.
     default_cflags="" # Spkg defaults that can and might get overridden.
@@ -106,43 +139,72 @@
     if [ "$SAGE_DEBUG" = yes ]; then
         # Disable optimization, add debug symbols:
         required_cflags="$required_cflags -g -O0"
-        echo "Warning: Building MPFR with SAGE_DEBUG=yes disables optimization."
+        echo >&2 "Warning: Building MPFR with SAGE_DEBUG=yes disables optimization."
     else
         # Add debug symbols by default, enable optimization, but do not (yet)
         # add processor-specific flags (these are eventually added later):
         default_cflags="$default_cflags -g -O3"
     fi
 
-    if [ "$SAGE64" = "yes" ]; then
-        echo "Building a 64-bit version of MPFR."
-        default_cflags="$default_cflags -m64"
-    else
-        default_cflags="$default_cflags"
+    if [ "$SAGE64" = yes ]; then
+        if [ -z "$CFLAG64" ]; then
+            CFLAG64=-m64
+        fi
+        echo "Building a 64-bit version of MPFR (using '$CFLAG64')."
+        required_cflags="$required_cflags $CFLAG64"
+        CPPFLAGS="$CPPFLAGS $CFLAG64"
+        LDFLAGS="$LDFLAGS $CFLAG64"
+        # As MPFR doesn't have C++ files, we don't have to modify CXXFLAGS.
     fi
 
-    SAGE_CONF_OPTS="--libdir=$SAGE_LOCAL/lib --disable-thread-safe"
-    if [ ! -f "$SAGE_LOCAL/include/gmp.h" ]; then
-         SAGE_CONF_OPTS="$SAGE_CONF_OPTS --disable-static --enable-shared"
+    # Work around a bug in GCC 4.7.0 which breaks the build on Itanium CPUs with
+    # '-O3', '-O2' and '-O1' (cf. #12837/#12765, #12751, and the bug URL below.)
+    if [ "`uname -m`" = ia64 ] && [ "`testcc.sh $CC`" = GCC ]; then
+        gcc_version=`$CC -dumpversion`
+        case "$gcc_version" in
+          4.7.*)
+            required_cflags="$required_cflags -O0 -finline-functions -fschedule-insns"
+            echo >&2 "Warning: Disabling almost all optimization due to a bug in (at least)"
+            echo >&2 "         GCC 4.7.0 on Itanium, which otherwise would break the build."
+            echo >&2 "         See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48496"
+            echo >&2 "         for current status and further details."
+            echo >&2 "         (And please report to e.g. sage-devel in case you feel this bug"
+            echo >&2 "          should already be fixed in GCC $gcc_version.)"
+        esac
     fi
 
-    if [ $UNAME = "CYGWIN" ]; then
-         SAGE_CONF_OPTS="$SAGE_CONF_OPTS --disable-static --enable-shared"
+    # Enabling thread-safe (which meanwhile is or at least may be the default)
+    # currently causes problems on a few systems:
+    SAGE_CONF_OPTS="--disable-thread-safe"
 
+    # XXX What is the following supposed to achieve???
+    # (GMP/MPIR is a prerequisite for MPFR, and we always build MPIR with
+    # '--enable-gmpcompat', since MPFR and other packages won't build without
+    # that anyway.) 
+    if [ ! -f "$SAGE_LOCAL/include/gmp.h" ]; then
+        SAGE_CONF_OPTS="$SAGE_CONF_OPTS --disable-static --enable-shared"
     fi
 
-    # Pre-configure MPFR to get the settings it would use if CC and CFLAGS were empty:
+    if [ "$UNAME" = CYGWIN ]; then
+        SAGE_CONF_OPTS="$SAGE_CONF_OPTS --disable-static --enable-shared"
+    fi
+
+    ###########################################################################
+    # Pre-configure MPFR with CC and CFLAGS unset:
+    ###########################################################################
     echo "Checking what CC and CFLAGS MPFR would use if they were empty..."
     if (unset CC CFLAGS CPPFLAGS CXXFLAGS &&
         ./configure --with-gmp="$SAGE_LOCAL" $SAGE_CONF_OPTS $MPFR_EXTRA_OPTS) &>/dev/null;
     then
-        if mpfr_get_upstream_selected_cflags config.status; then
+        mpfr_config_file=config.status
+        if mpfr_get_upstream_selected_cflags "$mpfr_config_file"; then
             mpfr_cflags=$upstream_cflags
             mpfr_cc=$upstream_cc
             echo "Settings chosen by MPFR when configuring with CC and CFLAGS unset:"
             echo "  CC:      $mpfr_cc"
             echo "  CFLAGS:  $mpfr_cflags"
         else
-            echo "Warning: Couldn't determine MPFR-selected CC and CFLAGS from 'mpfr.h'"
+            echo >&2 "Warning: Couldn't determine MPFR-selected CC and CFLAGS from '$mpfr_config_file'."
         fi
     else
         # We ignore errors in the first place, since we redirected all
@@ -151,7 +213,6 @@
         :;
     fi
     find . -name config.cache -o -name config.status -exec rm -f {} \;
-    CC=$sage_cc
 
     echo "Settings required to properly build MPFR, taking into account SAGE_DEBUG etc.:"
     echo "  CFLAGS:  $required_cflags"
@@ -183,6 +244,7 @@
     echo "Finally using the following settings:"
     echo "  CC=$CC"
     echo "  CFLAGS=$CFLAGS"
+    echo "  CPP=$CPP"
     echo "  CPPFLAGS=$CPPFLAGS"
     echo "  CXX=$CXX"
     echo "  CXXFLAGS=$CXXFLAGS"
@@ -190,55 +252,72 @@
     echo "  ABI=$ABI"
     echo "(These settings may still get overridden by 'configure' or Makefiles.)"
 
-    ###############################################################################
+    export CFLAGS CPPFLAGS LDFLAGS # 'sage-env' does *not* export all of them.
+
+    ###########################################################################
     # Now really configure MPFR with proper settings:
-    ###############################################################################
+    ###########################################################################
+    echo
     if [ -z "$MPFR_EXTRA_OPTS" ]; then
         echo "Configuring MPFR with the following options:"
-        echo "    --prefix=\"$SAGE_LOCAL\" --with-gmp=\"$SAGE_LOCAL\" $SAGE_CONF_OPTS"
+    else
+        echo "Configuring MPFR with additional options as specified by" \
+            "MPFR_EXTRA_OPTS:"
+        echo "  $MPFR_EXTRA_OPTS"
+        echo "Finally configuring MPFR with the following options:"
+    fi
+    echo "  --prefix=\"$SAGE_LOCAL\""
+    echo "  --libdir=\"$SAGE_LOCAL/lib\""
+    echo "  --with-gmp=\"$SAGE_LOCAL\""
+    for opt in $SAGE_CONF_OPTS $MPFR_EXTRA_OPTS; do
+        echo "  $opt"
+    done
+    if [ -z "$MPFR_EXTRA_OPTS" ]; then
         echo "You can set MPFR_EXTRA_OPTS to pass additional parameters."
-    else
-        echo "Using additional 'configure' options as specified with" \
-            "MPFR_EXTRA_OPTS:"
-        echo "    $MPFR_EXTRA_OPTS"
-        echo "Configuring MPFR with the following options:"
-        echo "    --prefix=\"$SAGE_LOCAL\" --with-gmp=\"$SAGE_LOCAL\" $SAGE_CONF_OPTS $MPFR_EXTRA_OPTS"
     fi
 
-    ./configure --prefix="$SAGE_LOCAL" --with-gmp="$SAGE_LOCAL" $SAGE_CONF_OPTS $MPFR_EXTRA_OPTS
+    ./configure --prefix="$SAGE_LOCAL" \
+                --libdir="$SAGE_LOCAL/lib" \
+                --with-gmp="$SAGE_LOCAL" \
+                $SAGE_CONF_OPTS $MPFR_EXTRA_OPTS
 }
 
 mpfr_build()
 {
-    echo "Patching MPFR"
+    echo "Patching MPFR..."
     mpfr_patch
-    if [ $? -ne 0 ];  then
+    if [ $? -ne 0 ]; then
         echo >&2 "Error patching MPFR."
         exit 1
     fi
 
-    echo "Configuring MPFR"
+    echo
+    echo "Now configuring MPFR..."
     mpfr_configure
     if [ $? -ne 0 ]; then
-        echo >&2 "Error configuring MPFR."\
-            "(See above for the options passed to it.)"
+        echo >&2 "Error configuring MPFR."
+        echo >&2 "See above for the options passed to it, and the file"
+        echo >&2 "  `pwd`/config.log"
+        echo >&2 "for details."
         exit 1
     fi
 
-    echo "Building MPFR"
+    echo
+    echo "Now building MPFR..."
     $MAKE
-    if [ $? -ne 0 ];  then
+    if [ $? -ne 0 ]; then
         echo >&2 "Error building MPFR."
         exit 1
     fi
 
-    echo "Deleting old headers"
+    echo
+    echo "Building MPFR succeeded.  Now deleting old headers..."
     rm -f "$SAGE_LOCAL"/include/*mpfr*
     # Do not delete old libraries as this causes gcc to break during
     # parallel builds.
     # rm -f "$SAGE_LOCAL"/lib/libmpfr.*
 
-    echo "Installing MPFR"
+    echo "Now installing MPFR..."
     $MAKE install
     if [ $? -ne 0 ];  then
         echo >&2 "Error installing MPFR."
@@ -249,3 +328,4 @@
 cd src
 
 mpfr_build
+# All potential errors catched in the above function.
