mpfr_get_upstream_selected_cflags() # Get MPFR's choice on empty CC and CFLAGS.
{
    if [ $# -ne 1 ]; then
        echo >&2 "Error: mpfr_get_upstream_selected_cflags() requires 'file' parameter."
        exit 1
    fi
    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 "$config_file" ]; then
        upstream_cc=""
        upstream_cflags=""
        return 1
    fi
    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()
{
    ###########################################################################
    # 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.

    if [ "$SAGE_DEBUG" = yes ]; then
        # Disable optimization, add debug symbols:
        required_cflags="$required_cflags -g -O0"
        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

    # 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"

    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 $SAGE_CONFIGURE_GMP $SAGE_CONF_OPTS $MPFR_CONFIGURE) &>/dev/null;
    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 >&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
        # messages to /dev/null. (The messages can be found in the 'config.log'
        # files if someone really wants to read them.)
        :;
    fi
    find . -name config.cache -o -name config.status -exec rm -f {} \;

    echo "Settings required to properly build MPFR, taking into account SAGE_DEBUG etc.:"
    echo "  CFLAGS:  $required_cflags"
    echo "  LDFLAGS: $LDFLAGS" # Might be empty, or specified by the user.
    echo "  ABI:     $ABI" # Might be empty, or specified by the user.
    echo "Settings from the \"global\" environment:"
    echo "  CC:      $CC" # Set by Sage, maybe overridden by the user.
    echo "  CFLAGS:  $user_cflags"
    echo "  (CPPFLAGS, CXX and CXXFLAGS are listed below; these don't get modified.)"

    if [ -z "$user_cflags" ]; then
        # No CFLAGS specified by user => Use either MPFR's or our default ones,
        # plus those required by Sage for the package to build properly:
        if [ -n "$mpfr_cflags" ]; then
            # Fine. Use upstream settings.
            echo "Using MPFR's settings (plus mandatory ones)."
            CFLAGS="$mpfr_cflags $required_cflags"
        else # Use spkg's defaults.
            echo "Using the spkg's (i.e. Sage's) default (plus mandatory) settings."
            CFLAGS="$default_cflags $required_cflags"
        fi
    else
        # CFLAGS were specified by the user, so don't override them (unless
        # necessary).
        echo "Using user-specified settings (overriding defaults), with some additions."
        CFLAGS="$default_cflags $user_cflags $required_cflags"
    fi

    echo "Finally using the following settings:"
    echo "  CC=$CC"
    echo "  CFLAGS=$CFLAGS"
    echo "  CPP=$CPP"
    echo "  CPPFLAGS=$CPPFLAGS"
    echo "  CXX=$CXX"
    echo "  CXXFLAGS=$CXXFLAGS"
    echo "  LDFLAGS=$LDFLAGS"
    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_CONFIGURE" ]; then
        echo "Configuring MPFR with the following options:"
    else
        echo "Configuring MPFR with additional options as specified by" \
            "MPFR_CONFIGURE:"
        echo "  $MPFR_CONFIGURE"
        echo "Finally configuring MPFR with the following options:"
    fi
    echo "  --prefix=\"$SAGE_LOCAL\""
    echo "  --libdir=\"$SAGE_LOCAL/lib\""
    if [ -n "$SAGE_CONFIGURE_GMP" ]; then
        echo "  --with-gmp=\"$SAGE_LOCAL\""
    fi

    for opt in $SAGE_CONF_OPTS $MPFR_CONFIGURE; do
        echo "  $opt"
    done
    if [ -z "$MPFR_CONFIGURE" ]; then
        echo "You can set MPFR_CONFIGURE to pass additional parameters."
    fi

    sdh_configure $SAGE_CONFIGURE_GMP $SAGE_CONF_OPTS $MPFR_CONFIGURE
}

mpfr_build()
{
    mpfr_configure
    sdh_make

    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.*

    sdh_make_install
}

cd src

mpfr_build
