# It is best to unset these environment variables, as they might confuse
# the Python installer.
unset PYTHONHOME
unset PYTHONPATH
unset SETUPTOOLS_USE_DISTUTILS

# Prevent use of the system hg and svn as it might make the installation fail
export HAS_HG=no
export SVNVERSION=no

cd src

if [ "$SAGE_DEBUG" = "yes" ]; then
    echo "Building Python with pydebug"
    PYTHON_CONFIGURE="$PYTHON_CONFIGURE --with-pydebug"
fi

# pymalloc screws with valgrind, so let's disable it
if [ "$SAGE_VALGRIND" = "yes" ]; then
    echo "Building Python without pymalloc"
    PYTHON_CONFIGURE="$PYTHON_CONFIGURE --without-pymalloc"
fi

# We will use EXTRA_CFLAGS for user-defined CFLAGS below.
OLD_CFLAGS="$CFLAGS"
unset CFLAGS

if [ "$UNAME" = Darwin ]; then
    PYTHON_CONFIGURE="--disable-toolbox-glue $PYTHON_CONFIGURE"

    mkdir "../include"
    if [ -n "$OPENSSL_INCLUDE" ];  then
        # If the user explicitely states where to get the openssl
        # includes, use that.
        cp -rp "$OPENSSL_INCLUDE" "../include"
        export CFLAGS="-I../include"
    else
        # Otherwise try using homebrew version
        brew_openssl="/usr/local/opt/openssl/include"
        if [ -d "$brew_openssl" ]; then
            export LDFLAGS="$LDFLAGS -L/usr/local/opt/openssl/lib"
            export CPPFLAGS="$CPPFLAGS -I/usr/local/opt/openssl/include"
            export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/opt/openssl/lib/pkgconfig"
        fi
    fi

    if [ $MACOSX_VERSION -ge 16 ]; then
        echo "OS X 10.$[$MACOSX_VERSION-4] Building with clang."
        CC=clang
    fi
fi

# Use EXTRA_CFLAGS for user-defined CFLAGS since Python puts its own
# default flags like -O3 after CFLAGS but before EXTRA_CFLAGS.
# We also disable warnings about unused variables/functions which are
# common in Cython-generated code.
export EXTRA_CFLAGS="`testcflags.sh -Wno-unused $OLD_CFLAGS`"

    # Note: --without-ensurepip ensures that setuptools+pip are *not* installed
    # automatically when installing python3. They will be installed instead by
    # the separate setuptools and pip packages; see
    # https://github.com/sagemath/sage/issues/23398
    PYTHON_CONFIGURE="$PYTHON_CONFIGURE --without-ensurepip"

sdh_configure --enable-shared $PYTHON_CONFIGURE

# Make sure -L. is placed before -L$SAGE_LOCAL/lib so that python and extension
# modules are linked with the right libpython; we pass this in at make time
# only, since we don't want -L. to be saved as one of the default LDFLAGS
# used for building third-party extension modules.
#
# Issue #32442: As we now install python in SAGE_VENV, not SAGE_LOCAL,
# we need to provide paths into $SAGE_LOCAL, so that setup.py finds
# the libraries needed for the extension modules - in particular sqlite3.
# (The search code there does not know about CPATH and LIBRARY_PATH.)

if [ "$UNAME" = "Darwin" ]; then
    make_LDFLAGS="-L$SAGE_LOCAL/lib -L$SAGE_INST_LOCAL/lib $LDFLAGS"
    # This enables python to find libpython
    export DYLD_LIBRARY_PATH="."
fi
if [ "$UNAME" = "Linux" ]; then
    make_LDFLAGS="-L. -Wl,-rpath,. -L$SAGE_LOCAL/lib $LDFLAGS"
    make_CPPFLAGS="-I$SAGE_LOCAL/include $CPPFLAGS"
    # Also, we need to add an rpath, like we do for SAGE_LOCAL in src/bin/sage-env.
    # SAGE_INST_LOCAL is the installation hierarchy for the current package
    # -- for python3, this is SAGE_VENV.
    make_LDFLAGS="-Wl,-rpath,$SAGE_INST_LOCAL/lib $make_LDFLAGS"
    make_LDFLAGS="-Wl,-rpath-link,$SAGE_INST_LOCAL/lib $make_LDFLAGS"
    # This enables python to find libpython
    export LD_LIBRARY_PATH="."
fi

sdh_make LDFLAGS="$make_LDFLAGS" CPPFLAGS="$make_CPPFLAGS"

# When building on a case-insensitive filesystem (on any OS, not just Windows)
# the Python executable is output to the build directory as 'python.exe'
if [ -f "python.exe" ]; then
    PYTHON="./python.exe"
else
    PYTHON="./python"
fi

# Make sure extension modules were built correctly.
# All these modules are important and if any one
# fails to build, Sage will not work.

echo "Testing importing of various modules..."
import_errors=false
# Issue #31160: We no longer check for readline here.
# https://github.com/passagemath/passagemath/issues/813: _posixsubprocess is missing on ubuntu-{xenial,bionic}
test_modules="ctypes math hashlib socket zlib sqlite3 ssl _posixsubprocess"
if [ "$UNAME" = "Darwin" ]; then
    test_modules="$test_modules _scproxy"
fi

for module in $test_modules; do
    if $PYTHON -c "import $module"; then
        echo "$module module imported OK"
    else
        echo >&2 "$module module failed to import"
        import_errors=true
    fi
done

if $import_errors; then
    echo >&2 "Error: One or more modules failed to import."
    exit 1
fi
