Skip to content

Commit

Permalink
Trac #25486: Discover SAGE_SCRIPTS_DIR to make $SAGE_LOCAL/bin/sage w…
Browse files Browse the repository at this point in the history
…ork from any directory, in an environment without SAGE_* variables

This ticket makes `$SAGE_LOCAL/bin/sage` work without environment
variables set, or the current working directory to be something
particular.

It does that by
- adding `SAGE_ROOT` to  `$SAGE_LOCAL/bin/sage-env-config` (via `src/bin
/sage-env-config.in`).
- adding code to the top of `sage-env` that determines
`SAGE_SCRIPTS_DIR` from the directory of itself.

------
Previous discussion in https://groups.google.com/d/msg/sage-
devel/oODph9-yjaI/FVeBsCk_CgAJ:

On Wednesday, May 30, 2018 at 9:01:59 AM UTC-7, Nils Bruin wrote:

 Currently, if I run the script $SAGE_LOCAL/bin/sage in my normal
environment (where SAGE_ROOT is not set) then I get:

 Error: You must set the SAGE_ROOT environment variable or run this
 script from the SAGE_ROOT or SAGE_ROOT/local/bin/ directory.
 Error setting environment variables by sourcing '/usr/local/sage/sage-
git/local/bin/sage-env';
 possibly contact sage-devel (see http://groups.google.com/group/sage-
devel).

 If instead I run the script $SAGE_ROOT/sage a value for SAGE_ROOT is
figured out and everything works
 fine when called from my normal environment.

URL: https://trac.sagemath.org/25486
Reported by: mkoeppe
Ticket author(s): Matthias Koeppe
Reviewer(s): Dima Pasechnik
  • Loading branch information
Release Manager committed Jun 26, 2020
2 parents 01ea67f + 4415da5 commit 265e19d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/bin/sage-env
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ resolvelinks() {
echo "$out"
}

# See if we can obtain SAGE_SCRIPTS_DIR before determining SAGE_ROOT
if [ -z "$SAGE_SCRIPTS_DIR" ]; then
## Find the directory of this script (sage-env).
## Note that it is *sourced* by various other scripts,
## which is why `dirname $0` would not work ($0 is /bin/bash).
## Instead we use a solution from
## http://stackoverflow.com/questions/59895/can-a-bash-script-tell-which-directory-it-is-stored-in
## It uses a bash feature: BASH_SOURCE[0] gives the source of
## the "currently running function". See
## https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
## (entries for FUNCNAME and BASH_SOURCE).
SAGE_SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi

if [ -n "$SAGE_SCRIPTS_DIR" -a -r "$SAGE_SCRIPTS_DIR/sage-env-config" ]; then
# Set environment variables (like SAGE_LOCAL and SAGE_ROOT) depending on ./configure
. "$SAGE_SCRIPTS_DIR/sage-env-config"
fi

# New value for SAGE_ROOT: either SAGE_ROOT (if given)
# or a guessed value based on pwd.
Expand All @@ -120,7 +138,7 @@ elif [ -f ../../sage -a -d ../../build ]; then
NEW_SAGE_ROOT="../.."
else
# No idea what SAGE_ROOT should be...
echo >&2 "Error: You must set the SAGE_ROOT environment variable or run this"
echo >&2 "Error: You must set the SAGE_ROOT or SAGE_SCRIPTS_DIR environment variables or run this"
echo >&2 "script from the SAGE_ROOT or SAGE_ROOT/local/bin/ directory."
return 1
fi
Expand Down Expand Up @@ -185,11 +203,13 @@ elif [ ! -f "$SAGE_SCRIPTS_DIR/sage-env-config" ]; then
return 1
fi

# Set environment variables (like SAGE_LOCAL) depending on ./configure
. "$SAGE_SCRIPTS_DIR/sage-env-config"
if [ $? -ne 0 ]; then
echo >&2 "Error: failed to source $SAGE_SCRIPTS_DIR/sage-env-config"
return 1
if [ -n $SAGE_ENV_CONFIG_SOURCED ]; then
# Set environment variables (like SAGE_LOCAL) depending on ./configure
. "$SAGE_SCRIPTS_DIR/sage-env-config"
if [ $? -ne 0 ]; then
echo >&2 "Error: failed to source $SAGE_SCRIPTS_DIR/sage-env-config"
return 1
fi
fi

# The compilers are set in order of priority by
Expand Down
5 changes: 5 additions & 0 deletions src/bin/sage-env-config.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
#
##########################################################################

export SAGE_ENV_CONFIG_SOURCED=1

# SAGE_LOCAL is the installation prefix and can be customized by using
# ./configure --prefix
export SAGE_LOCAL="@prefix@"

# SAGE_ROOT is the location of the Sage source/build tree.
export SAGE_ROOT="@abs_top_srcdir@"

#######################################
# Compilers set at configuration time
#######################################
Expand Down

0 comments on commit 265e19d

Please sign in to comment.