Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 554282a

Browse files
committed
Trac #30128: enforce sourcing of sage-env-config before src/bin/sage-env.
All callers within sage now source sage-env-config before sage-env, so this commit removes the bashism within sage-env that tries to find and source sage-env-config on its own. Since sage-env still morally depends on sage-env-config for the values of some variables, we now throw an error in sage-env is sage-env-config was not previously sourced. This should prevent future misuse from creeping in.
1 parent 2f141c8 commit 554282a

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

src/bin/sage-env

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# they won't be available in child programs.
1414
# - This script has a version number such that a newer version of
1515
# sage-env can be sourced when upgrading. See below.
16-
# - It uses bash features, so it cannot be used with other shells.
1716
#
1817
# If you want to set all environment variables for your shell like
1918
# they are during the build of Sage packages, type
@@ -27,6 +26,16 @@
2726
#
2827
##########################################################################
2928

29+
if [ "${SAGE_ENV_CONFIG_SOURCED}" -ne 1 ]; then
30+
# This script uses sage-env-config to find the ./configured value
31+
# of SAGE_ROOT, but ironically, it doesn't know how to find
32+
# sage-env-config because it doesn't know SAGE_ROOT yet!
33+
# Fortunately, anyone that knows how to source sage-env must know
34+
# how to source sage-env-config, so the issue is avoided by
35+
# requiring sage-env-config to be sourced before sage-env.
36+
echo 'must source sage-env-config before sage-env' >&2
37+
return 3
38+
fi
3039

3140
# Resolve all symbolic links in a filename. This more or less behaves
3241
# like "readlink -f" except that it does not convert the filename to an
@@ -109,25 +118,6 @@ resolvelinks() {
109118
echo "$out"
110119
}
111120

112-
# See if we can obtain SAGE_SCRIPTS_DIR before determining SAGE_ROOT
113-
if [ -z "$SAGE_SCRIPTS_DIR" ]; then
114-
## Find the directory of this script (sage-env).
115-
## Note that it is *sourced* by various other scripts,
116-
## which is why `dirname $0` would not work ($0 is /bin/bash).
117-
## Instead we use a solution from
118-
## http://stackoverflow.com/questions/59895/can-a-bash-script-tell-which-directory-it-is-stored-in
119-
## It uses a bash feature: BASH_SOURCE[0] gives the source of
120-
## the "currently running function". See
121-
## https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
122-
## (entries for FUNCNAME and BASH_SOURCE).
123-
SAGE_SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
124-
fi
125-
126-
if [ -n "$SAGE_SCRIPTS_DIR" -a -r "$SAGE_SCRIPTS_DIR/sage-env-config" ]; then
127-
# Set environment variables (like SAGE_LOCAL and SAGE_ROOT) depending on ./configure
128-
. "$SAGE_SCRIPTS_DIR/sage-env-config"
129-
fi
130-
131121
# New value for SAGE_ROOT: either SAGE_ROOT (if given)
132122
# or a guessed value based on pwd.
133123
if [ -n "$SAGE_ROOT" ]; then
@@ -137,7 +127,7 @@ elif [ -f sage -a -d build ]; then
137127
elif [ -f ../../sage -a -d ../../build ]; then
138128
NEW_SAGE_ROOT="../.."
139129
else
140-
# No idea what SAGE_ROOT should be...
130+
# No idea what SAGE_ROOT should be... it should have been set by sage-env-config.
141131
echo >&2 "Error: You must set the SAGE_ROOT or SAGE_SCRIPTS_DIR environment variables or run this"
142132
echo >&2 "script from the SAGE_ROOT or SAGE_ROOT/local/bin/ directory."
143133
return 1
@@ -203,15 +193,6 @@ elif [ ! -f "$SAGE_SCRIPTS_DIR/sage-env-config" ]; then
203193
return 1
204194
fi
205195

206-
if [ -n $SAGE_ENV_CONFIG_SOURCED ]; then
207-
# Set environment variables (like SAGE_LOCAL) depending on ./configure
208-
. "$SAGE_SCRIPTS_DIR/sage-env-config"
209-
if [ $? -ne 0 ]; then
210-
echo >&2 "Error: failed to source $SAGE_SCRIPTS_DIR/sage-env-config"
211-
return 1
212-
fi
213-
fi
214-
215196
# The compilers are set in order of priority by
216197
# 1) environment variables
217198
# 2) compiler installed by sage

0 commit comments

Comments
 (0)