Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/src/bin/sage-env
4052 views
# -*- shell-script -*-

###########################################################################
#
#  Set environment variables for building and/or running Sage.
#
#  NOTES:
#  - You must *source* this script instead of executing.
#  - Use "return" instead of "exit" to signal a failure.  Since this
#    file is sourced, an "exit" here will actually exit src/bin/sage,
#    which is probably not intended.
#  - All environment variables set here should be *exported*, otherwise
#    they won't be available in child programs.
#  - This script has a version number such that a newer version of
#    sage-env can be sourced when upgrading.  See below.
#
#  If you want to set all environment variables for your shell like
#  they are during the build of Sage packages, type
#
#             . src/bin/sage-env
#
#  from the SAGE_ROOT directory.
#
#  AUTHORS: William Stein, David Kirkby, Jeroen Demeyer,
#           J. H. Palmieri, Leif Leonhardy and others.
#
##########################################################################

# Resolve all symbolic links in a filename.  This more or less behaves
# like "readlink -f" except that it does not convert the filename to an
# absolute path (a relative path remains relative), nor does it treat
# "." or ".." specially.
#
# AUTHOR: Jeroen Demeyer (2011-08-23): Github issues #5852 and #11704
#
resolvelinks() {
    # $in is what still needs to be converted (normally has no starting slash)
    in="$1"
    # $out is the part which is converted (normally ends with trailing slash)
    out="./"

    # Move stuff from $in to $out
    while [ -n "$in" ]; do
        # Normalize $in by replacing consecutive slashes by one slash
        in=$(echo "${in}" | sed 's://*:/:g')

        # If $in starts with a slash, remove it and set $out to the root
        in_without_slash=${in#/}
        if [ "$in" != "$in_without_slash" ]; then
            in=$in_without_slash
            out="/"
            continue
        fi

        # Check that the directory $out exists by trying to cd to it.
        # If this fails, then cd will show an error message (unlike
        # test -d "$out"), so no need to be more verbose.
        ( cd "$out" ) || return $?


        # Get the first component of $in
        f=${in%%/*}

        # If it is not a symbolic link, simply move it to $out
        if [ ! -L "$out$f" ]; then
            in=${in#"$f"}
            out="$out$f"

            # If the new $in starts with a slash, move it to $out
            in_without_slash=${in#/}
            if [ "$in" != "$in_without_slash" ]; then
                in=$in_without_slash
                out="$out/"
            fi
            continue
        fi

        # Now resolve the symbolic link "$f"
        f_resolved=`readlink -n "$out$f" 2>/dev/null`
        status=$?
        # status 127 means readlink could not be found.
        if [ $status -eq 127 ]; then
            # We don't have "readlink", try a stupid "ls" hack instead.
            # This will fail if we have filenames like "a -> b".
            fls=`ls -l "$out$f" 2>/dev/null`
            status=$?
            f_resolved=${fls##*-> }

            # If $fls equals $f_resolved, then certainly
            # something is wrong
            if [ $status -eq 0 -a "$fls" = "$f_resolved" ]; then
                echo >&2 "Cannot parse output from ls -l '$out$f'"
                return 1
            fi
        fi
        if [ $status -ne 0 ]; then
            echo >&2 "Cannot read symbolic link '$out$f'"
            return $status
        fi

        # In $in, replace $f by $f_resolved (leave $out alone)
        in="${in#${f}}"
        in="${f_resolved}${in}"
    done

    # Return $out
    echo "$out"
}

# Make sure that SAGE_ROOT is either an absolute physical directory name
# or empty.
if [ -n "$SAGE_ROOT" ]; then
    export SAGE_ROOT=$(cd "$SAGE_ROOT" 2>/dev/null && pwd -P)
fi

# Don't execute the commands more than once for the same version of
# sage-env... for the same combination of SAGE_LOCAL and SAGE_VENV.
# "6" indicates the version of the format of the value of SAGE_ENV_VERSION.
SAGE_ENV_VERSION="6:$SAGE_LOCAL:$SAGE_VENV:$SAGE_SRC"
if [ "$SAGE_ENV_SOURCED" = "$SAGE_ENV_VERSION" ]; then
    # Already sourced, nothing to do.
    return 0
fi
# Set SAGE_ENV_SOURCED to the appropriate value at the end of this file, once
# $SAGE_LOCAL, $SAGE_VENV, $SAGE_SRC have been set.

# The compilers are set in order of priority by
# 1) environment variables
# 2) compiler installed by sage
# 3) compiler set at configuration time
if [ -z "$CC" ]; then
    if [ -n "$SAGE_LOCAL" -a -x "$SAGE_LOCAL/bin/gcc" ]; then
        CC=gcc
    elif [ -n "$CONFIGURED_CC" ]; then
        CC="$CONFIGURED_CC"
    fi
    export CC
fi
if [ -z "$CXX" ]; then
    if [  -n "$SAGE_LOCAL" -a -x "$SAGE_LOCAL/bin/g++" ]; then
        CXX=g++
    elif [ -n "$CONFIGURED_CXX" ]; then
        CXX="$CONFIGURED_CXX"
    fi
    export CXX
fi
if [ -z "$FC" ]; then
    if [ -n "$SAGE_LOCAL" -a -x "$SAGE_LOCAL/bin/gfortran" ]; then
        FC=gfortran
    elif [ -n "$CONFIGURED_FC" ]; then
        FC="$CONFIGURED_FC"
    fi
    export FC
fi
if [ "$UNAME" = "Darwin" ]; then
    if [ -z "$OBJC" ]; then
        OBJC="$CONFIGURED_OBJC"
    fi
    if [ -z "$OBJCXX" ]; then
        OBJCXX="$CONFIGURED_OBJCXX"
    fi
    export OBJC OBJCXX
fi

# Set other Fortran-related compiler variables
export F77="$FC"
export F90="$FC"   # Needed for SciPy
export F95="$FC"

# For ARCHFLAGS (#31227) we need to distinguish unset and empty.
# If the environment defines ARCHFLAGS, even when empty, then take that.
# Otherwise, use the configured value; but if that is "unset", do not set
# the variable at all.
if [ "${ARCHFLAGS-unset}" = "unset" ]; then
    if [ "${SAGE_ARCHFLAGS-unset}" != "unset" ]; then
        export ARCHFLAGS="${SAGE_ARCHFLAGS}"
    fi
fi

# Call with: contains_spaces X${VAR}X
# i.e., WITHOUT quotes but some character(s) around the environment variable to test.
# (This function does return false for empty/unset variables.)
contains_spaces()
{
    if [ $# -ne 1 ]; then
        return 0 # true
    else
        return 1 # false
    fi
}


if contains_spaces X${SAGE_ROOT}X ; then
    echo "Error: The path to the Sage directory (\$SAGE_ROOT) MUST NOT contain spaces."
    echo "It is currently \"$SAGE_ROOT\"."
    echo "Please correct this by moving Sage (or renaming one or more directories) first."
    echo "Exiting now..."
    return 1
fi


if [ 1 = 2 ]; then
    echo "The following environment variables can be set by the user"
    echo "AR          The archiver (e.g. ar, /usr/ccs/bin/ar or /usr/bin/ar)"
    echo "AS          The assembler (e.g. as, /usr/ccs/bin/as or /usr/bin/as)"
    echo "CC          The C compiler (e.g cc, /opt/SUNWspro/bin/cc or /usr/bin/gcc)"
    echo "CFLAGS      Flag(s) for the C compiler (e.g.  -g -Wall -O2)"
    echo "            (You are advised to a some optimisation flag(s), such as -O2 or -xO2 to CFLAGS)"
    echo "CXX         The C++ compiler (e.g g++, /opt/SUNWspro/bin/CC or /usr/local/bin/g++)"
    echo "CXXFLAGS    Flag(s) for the C++ compiler (e.g. -fast -fsimple=1 -x04)"
    echo "LD          The linker (e.g. ld, /usr/ccs/bin/ld or /usr/bin/ld)"
    echo "LDFLAGS     Linker flag(s) (e.g. -D token)"
    echo "LN          Used to make links (e.g. ln, /usr/xpg4/bin/ln or /usr/bin/ln)"
    echo "MAKE        The make program (e.g. make, /usr/bin/make or /usr/local/bin/gmake)"
    echo "MAKEFLAGS   Flag(s) to make (e.g. -j4)."
    echo "RANLIB      Archiver ranlib (e.g. ranlib, /usr/ccs/bin/ranlib etc)"
    echo "SHAREDFLAGS Flag(s) necessary for building a shared library (e.g. -fPIC or -xcode=pic32)"
    echo "We attempt to set this to sensible values, but check below to"
    echo "ensure they are OK. If you wish to override any then please use:"
    echo "setenv NAME_OF_ENVIRONMENT_VARIABLE value_of_environment_variable"
    echo "(if you use tcsh, csh or a similar shell) or"
    echo "NAME_OF_ENVIRONMENT_VARIABLE value_of_environment_variable"
    echo "export NAME_OF_ENVIRONMENT_VARIABLE"
    echo "if you use sh, bash or a similar shell"
fi

# Setting Sage-related location environment variables,
# depending on SAGE_ROOT and SAGE_LOCAL which are already defined.
if [ -n "$SAGE_LOCAL" ]; then
    export SAGE_SHARE="$SAGE_LOCAL/share"
    export SAGE_SPKG_INST="$SAGE_LOCAL/var/lib/sage/installed"  # deprecated
fi
if [ -n "$SAGE_SHARE" ]; then
    export SAGE_DOC="$SAGE_SHARE/doc/sage"
fi
if [ -d "$SAGE_ROOT" ]; then
    export SAGE_LOGS="$SAGE_ROOT/logs/pkgs"
    export SAGE_SRC="$SAGE_ROOT/src"
fi
if [ -n "$SAGE_SRC" ]; then
    export SAGE_DOC_SRC="$SAGE_SRC/doc"
fi

if [ -n "$SAGE_PKG_CONFIG_PATH" ]; then
    # set up external pkg-config to look into SAGE_LOCAL/lib/pkgconfig/
    # (Sage's pkgconf spkg takes care of this, if installed)
    export PKG_CONFIG_PATH="$SAGE_PKG_CONFIG_PATH${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"
fi

if [ -z "${SAGE_ORIG_PATH_SET}" ]; then
    SAGE_ORIG_PATH=$PATH && export SAGE_ORIG_PATH
    SAGE_ORIG_PATH_SET=True && export SAGE_ORIG_PATH_SET
fi
if [ -n "$SAGE_LOCAL" ]; then
    export PATH="$SAGE_LOCAL/bin:$PATH"
fi
if [ -n "$SAGE_VENV" ]; then
    export PATH="$SAGE_VENV/bin:$PATH"
fi
if [ -d "$SAGE_ROOT" ]; then
    export PATH="$SAGE_ROOT/build/bin:$PATH"
fi

# We offer a toolchain option, so if $SAGE_LOCAL/toolchain/toolchain-env exists source it.
# Since the user might do something crazy we do not do any checks, but hope for the best.
if [ -n "$SAGE_LOCAL" -a -f "$SAGE_LOCAL"/toolchain/toolchain-env ]; then
  source "$SAGE_LOCAL"/toolchain/toolchain-env
fi

# setting of the variable UNAME (describing the o.s.)
export UNAME=`uname`

# Mac OS X-specific setup
if [ "$UNAME" = "Darwin" ]; then
    export MACOSX_VERSION=`uname -r | awk -F. '{print $1}'`
    # Work around problems on recent OS X crashing with an error message
    # "... may have been in progress in another thread when fork() was called"
    # when objective-C functions are called after fork(). See Issue #25921.
    # Most likely, these errors are false positives, so we disable them:
    export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
fi

if [ -n "$SAGE_LOCAL" ]; then
    # Compile-time path for libraries.  This is the equivalent of
    # adding the gcc option -L $SAGE_LOCAL/lib.
    [ -z "$LIBRARY_PATH" ] || LIBRARY_PATH=":${LIBRARY_PATH}"
    export LIBRARY_PATH="$SAGE_LOCAL/lib${LIBRARY_PATH}"
    # Compile-time path for include files.  This is the equivalent of
    # adding the gcc option -I $SAGE_LOCAL/include.
    [ -z "$CPATH" ] || CPATH=":${CPATH}"
    export CPATH="$SAGE_LOCAL/include${CPATH}"
fi

if [ -n "$SAGE_LOCAL" ]; then
    # Ensure that there is a colon at the end of $INFOPATH by
    # stripping the existing one (if it exists), and then adding a new
    # one. This forces the "info" program to check various default
    # system locations when the user does not have $INFOPATH set. This
    # is necessary to find some *.info files installed by system
    # packages when "info" from the SPKG is used.
    export INFOPATH="${SAGE_LOCAL}/share/info:${INFOPATH%:}:"
fi

if [ -z "$SAGE_REPO_ANONYMOUS" ]; then
    SAGE_REPO_ANONYMOUS="https://github.com/sagemath/sage.git"
    export SAGE_REPO_ANONYMOUS
fi
if [ -z "$SAGE_REPO_AUTHENTICATED" ]; then
        SAGE_REPO_AUTHENTICATED="https://github.com/sagemath/sage.git"
        export SAGE_REPO_AUTHENTICATED
fi

if [ -d "$SAGE_ROOT" ]; then
    if [ -z "$SAGE_DISTFILES" ]; then
        SAGE_DISTFILES="$SAGE_ROOT/upstream"
        export SAGE_DISTFILES
    fi
fi

# Check that $HOME exists
if [ "$HOME" = "" ]; then
    echo >&2 'Error: environment variable $HOME is not set.'
    return 1
fi
if ! [ -d "$HOME" ]; then
    echo >&2 "Error: HOME directory '$HOME' does not exist."
    return 1
fi

if [ "$DOT_SAGE" = "" ]; then
    # It is *not* an error if this directory does not exist, it will
    # be created in src/bin/sage or src/sage/misc/misc.py.
    # This also works if $HOME/.sage is a symbolic link to a
    # non-existing directory.
    DOT_SAGE=`resolvelinks "$HOME/.sage"`

    # In theory, DOT_SAGE is not required to have a trailing slash.
    # But since there are some issues (#11924, maybe #12221),
    # we add a slash for safety.
    DOT_SAGE="${DOT_SAGE}/"
    export DOT_SAGE
fi

if [ "$SAGE_STARTUP_FILE" = "" ]; then
    SAGE_STARTUP_FILE="$DOT_SAGE/init.sage"
    export SAGE_STARTUP_FILE
fi

if [ "$PYTHON_EGG_CACHE" = "" ]; then
    PYTHON_EGG_CACHE="$DOT_SAGE/.python-eggs"
    export PYTHON_EGG_CACHE
fi

# Set PYTHONUSERBASE to avoid picking up non-Sage versions of
# Matplotlib, numpy, etc. See https://github.com/sagemath/sage/issues/19612.
#
# For more history (it used to be PYTHONNOUSERSITE=yes which killed
# the ability to do "sage -pip install PACKAGE --user"), see
# https://github.com/sagemath/sage/issues/14243 and
# https://github.com/sagemath/sage/issues/18955.

if [ "$PYTHONUSERBASE" = "" ]; then
    PYTHONUSERBASE="$DOT_SAGE/local"
    export PYTHONUSERBASE
fi

if [ -n "$PYTHONHOME" ]; then
    >&2 echo "Warning: PYTHONHOME must not be set when running Sage, clearing env..."
    unset PYTHONHOME
fi

if [ -n "$SAGE_LOCAL" ]; then
    # Construct and export LDFLAGS
    if [ "$UNAME" = "Darwin" ]; then
       LDFLAGS="-L$SAGE_LOCAL/lib $LDFLAGS"
       # On OS X, use the old linker if it is available.
       # if "ld-classic" is present in the selected XCode
       # toolchain, add "-Wl,-ld_classic" to LDFLAGS (see #36599) unless
       # LD is already set, as it will be with conda on macOS.  When the
       # selected toolchain is in the Xcode app the output of "xcode-select -p"
       # is "/Applications/Xcode.app/Contents/Developer", but "ld-classic" is
       # not in the subdirectory "usr/bin/" but rather in the subdirectory
       # "Toolchains/XcodeDefault.xctoolchain/usr/bin/".  (See #37237.)
       if [ -z "$LD" ]; then
	   # Running xcode-select on a system with no toolchain writes an
	   # error message to stderr, so redirect stderr to /dev/null.
	   XCODE_PATH=$(/usr/bin/xcode-select -p 2> /dev/null)
	   if [ -n $XCODE_PATH ]; then
               if [ -x "$XCODE_PATH/usr/bin/ld-classic" -o \
		       -x "$XCODE_PATH/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld-classic" ]; then
                   # Add -ld_classic only if -ld_classic is not deprecated.
                   if [ -z "$(ld -ld_classic 2>&1 | grep 'ld_classic is deprecated')" ]; then
		       LDFLAGS="$LDFLAGS -Wl,-ld_classic"
                   fi
               fi
           else
               # On a macOS system with no toolchain we don't want this script
               # to call gcc because that will also print an error message to
               # stderr.  We can avoid this by setting AS and LD to their
               # default values.
               AS=as
               LD=ld
	   fi
       fi
    fi
    if [ "$UNAME" = "Linux" ]; then
	LDFLAGS="-L$SAGE_LOCAL/lib -Wl,-rpath,$SAGE_LOCAL/lib $LDFLAGS"
        LDFLAGS="-Wl,-rpath-link,$SAGE_LOCAL/lib $LDFLAGS"
    fi
    export LDFLAGS
fi

if [ -z "$IPYTHONDIR" ]; then
    # We hardcode a version number in the directory name. The idea is
    # that we keep using the same version number as long as that is
    # possible. Only when some future IPython version really requires
    # a new structure for the $IPYTHONDIR should this version number be
    # changed to the new IPython version.
    export IPYTHONDIR="$DOT_SAGE/ipython-5.0.0"
fi

if [ -z "$JUPYTER_CONFIG_DIR" ]; then
    # We hardcode a version number in the directory name. The idea is
    # that we keep using the same version number as long as that is
    # possible. Only when some future Jupyter version really requires
    # a new structure for the $JUPYTER_CONFIG_DIR should this version
    # number be changed to the new jupyter_core version.
    export JUPYTER_CONFIG_DIR="$DOT_SAGE/jupyter-4.1"
fi

if [ -z "$MPLCONFIGDIR" ]; then
    # We hardcode a version number in the directory name. The idea is
    # that we keep using the same version number as long as that is
    # possible. Only when some future Matplotlib version really requires
    # a new structure for the $MPLCONFIGDIR should this version
    # number be changed to the new matplotlib version.
    export MPLCONFIGDIR="$DOT_SAGE/matplotlib-1.5.1"
fi

# Make sure that a system-wide R installation does not interfere
unset R_HOME
unset R_PROFILE
# Do not use the global Makevars.site and ~/.R/Makevars when installing R packages
# Provide empty files to appease some R packages' installation scripts.
if [ -n "$SAGE_LOCAL" -a -d "$SAGE_LOCAL/lib/R/share" ] ; then
   R_MAKEVARS_SITE="$SAGE_LOCAL/lib/R/share/Makevars.site" && export R_MAKEVARS_SITE
   if ! [ -f "$R_MAKEVARS_SITE" ] ; then
       if ! [ -e "$R_MAKEVARS_SITE" ] ; then
           echo "## Empty site-wide Makevars file for Sage's R" > "$R_MAKEVARS_SITE"
       else
           >&2 echo "Warning: $R_MAKEVARS_SITE exists and is not a file : trouble ahead..."
       fi
   fi
fi
if [ -d "$DOT_SAGE" ] ; then
    if ! [ -d "$DOT_SAGE/R" ] ; then
        if ! [ -e  "$DOT_SAGE/R" ] ; then
            mkdir -p "$DOT_SAGE/R"
        else
            >&2 echo "Warning: $DOT_SAGE/R exists and is not a directory : trouble ahead..."
        fi
    fi
    R_MAKEVARS_USER="$DOT_SAGE/R/Makevars.user" && export R_MAKEVARS_USER
    if ! [ -f "$R_MAKEVARS_USER" ] ; then
        if ! [ -e "$R_MAKEVARS_USER" ] ; then
            echo "## Empty user-specific Makevars file for Sage's R" > "$R_MAKEVARS_USER"
        else
            >&2 echo "Warning: $R_MAKEVARS_USER exists and is not a file : trouble ahead..."
        fi
    fi
fi

export MAXIMA_USERDIR="$DOT_SAGE/maxima"

if [ -n "$SAGE_LOCAL" ]; then
    PERL5LIB="$SAGE_LOCAL/lib/perl5:$PERL5LIB" && export PERL5LIB
fi

# Allow SAGE_BROWSER to override BROWSER (Issue #22449)
if [ -n "$SAGE_BROWSER" ]; then
    export BROWSER="$SAGE_BROWSER"
fi

############ compilation flags

# Setting Sage-related compilation flags.
# This could be used in code to make special changes only when
# code is being built as part of Sage.
export __sage__=""

# Setup env varariables if ccache is installed
if [ -n "$SAGE_LOCAL" -a -d "$SAGE_LOCAL/libexec/ccache" ]; then
    PATH="$SAGE_LOCAL/libexec/ccache:$PATH"
fi
if [ -d "$SAGE_ROOT" -a -z "$CCACHE_BASEDIR" ]; then
    export CCACHE_BASEDIR="$SAGE_ROOT"
fi

# Set AS to assembler used by $CC ("as" by default)
if [ "$AS" = "" ]; then
    CC_as=`$CC -print-prog-name=as 2>/dev/null`
    if command -v $CC_as >/dev/null 2>/dev/null; then
        AS="$CC_as"
    fi
    if [ "$AS" = "" ]; then
        AS=as
    fi
fi
export AS

# Set LD to linker used by $CC ("ld" by default)
if [ "$LD" = "" ]; then
    CC_ld=`$CC -print-prog-name=ld 2>/dev/null`
    if command -v $CC_ld >/dev/null 2>/dev/null; then
        LD="$CC_ld"
    fi
    if [ "$LD" = "" ]; then
        LD=ld
    fi
fi
export LD


if [ "$AR" = "" ]; then
    AR="ar"  && export AR
fi

if [ "$LDFLAGS" = "" ]; then
    LDFLAGS=""          && export LDFLAGS
fi

if [ -z "$CFLAGS" ]; then
    unset CFLAGS
fi

if [ -z "$CXXFLAGS" ]; then
    unset CXXFLAGS
fi

if [ -n "$CFLAGS" -a -z "$CXXFLAGS" ]; then
    export CXXFLAGS="$CFLAGS"
fi

if [ "$CP" = "" ]; then
    CP="cp"  && export CP
fi

if [ "$MV" = "" ]; then
    MV="mv"  && export MV
fi

if [ "$RANLIB" = "" ]; then
    RANLIB="ranlib"  && export RANLIB
fi

if [ "$LN" = "" ]; then
    LN="ln"  && export LN
fi

if [ "$MKDIR" = "" ]; then
    MKDIR="mkdir"  && export MKDIR
fi

if [ "$CHMOD" = "" ]; then
    CHMOD="chmod"  && export CHMOD
fi

if [ "$TOUCH" = "" ]; then
    TOUCH="touch"  && export TOUCH
fi

# Handle parallel building/testing/...
case "$SAGE_NUM_THREADS,$SAGE_NUM_THREADS_PARALLEL" in
    [1-9][0-9]*,[1-9][0-9]*)
        # Variables are set to positive values already,
        # sage-num-threads.py would just recompute them
        ;;
    *)
        # See Issue Ticket #12016
        # First, figure out the right values for SAGE_NUM_THREADS (default
        # number of threads) and SAGE_NUM_THREADS_PARALLEL (default number of
        # threads when parallel execution is asked explicitly).
        sage_num_threads_array=$(sage-num-threads.py 2>/dev/null || echo 1 2 1)
        sage_num_threads_array="${sage_num_threads_array% *}" # strip third item
        SAGE_NUM_THREADS="${sage_num_threads_array% *}" # keep first item
        SAGE_NUM_THREADS_PARALLEL="${sage_num_threads_array#* }" # keep second item
        export SAGE_NUM_THREADS
        export SAGE_NUM_THREADS_PARALLEL
        ;;
esac

# Multithreading in OpenBLAS does not seem to play well with Sage's attempts to
# spawn new processes, see #26118. Apparently, OpenBLAS sets the thread
# affinity and, e.g., parallel doctest jobs, remain on the same core.
# Disabling that thread-affinity with OPENBLAS_MAIN_FREE=1 leads to hangs in
# some computations.
# So we disable OpenBLAS' threading completely; we might loose some performance
# here but strangely the opposite seems to be the case. Note that callers such
# as LinBox use a single-threaded OpenBLAS anyway.
export OPENBLAS_NUM_THREADS=1

if [ "$MAKE" = "" ]; then
    MAKE="make"
fi

# If MAKEFLAGS exists, assume it got set by make.
# Therefore, remove all flags from $MAKE
if [ "${MAKEFLAGS-__unset__}" != "__unset__" ]; then
    MAKE=`echo "$MAKE" | sed 's/ .*//'`
fi
export MAKE

# Set the cysignals crash logs directory
if [ -z "$CYSIGNALS_CRASH_LOGS" ]; then
    export CYSIGNALS_CRASH_LOGS="$DOT_SAGE/crash_logs"
    export CYSIGNALS_CRASH_DAYS=7  # keep logs for 7 days
fi

# You can set environment variables in $SAGE_RC_FILE
# (by default, this is the file $DOT_SAGE/sagerc).  For example,
# setting PS1 there will set your prompt when you run "sage --sh".
if [ -z "$SAGE_RC_FILE" ]; then
    SAGE_RC_FILE="$DOT_SAGE/sagerc"
fi

if [ -r "$SAGE_RC_FILE" ]; then
    source "$SAGE_RC_FILE"
    if [ $? -ne 0 ]; then
        echo >&2 "Error sourcing $SAGE_RC_FILE"
        exit 1
    fi
fi

if [ -n "$SAGE_LOCAL" ]; then
    # If we move the Sage tree then ncurses cannot find terminfo, hence, we
    # tell it where to find it. See Issue Ticket #15091
    export TERMINFO="$SAGE_LOCAL/share/terminfo"

    # If nodejs is installed, activate the nodeenv containing it.

    nodeenv_activate="$SAGE_LOCAL/share/nodejs/activate"

    if [ -f "$nodeenv_activate" ]; then
        # symlinked into nodeenv for specific version of nodejs installed
        # The activate script needs to be sourced using its actual path.
        nodeenv_activate=`resolvelinks "$nodeenv_activate"`

        # Don't let nodeenv wipe out the sage-sh/sage-buildsh prompt.
        NODE_VIRTUAL_ENV_DISABLE_PROMPT=1 . "$nodeenv_activate"

        if [ $? -ne 0 ]; then
            echo >&2 "Warning: failed to activate the nodeenv containing nodejs"
        fi
    elif [ -L "$nodeenv_activate" ]; then
        echo >&2 "Warning: the nodeenv activation symlink for nodejs is broken"
    fi

fi


# Newer versions of debugpy come with a bundled pydevd that complains
# about >=python-3.11's core modules being frozen (and therefore not
# breakpoint-able). This workaround simply hides the warning to keep
# our doctests predictable (which was the status quo with earlier
# versions of debugpy).
export PYDEVD_DISABLE_FILE_VALIDATION=1

# Finally, set SAGE_ENV_SOURCED as evidence that this script has been
# run successfully.
export SAGE_ENV_SOURCED="6:$SAGE_LOCAL:$SAGE_VENV:$SAGE_SRC"