diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2020-10-23 18:54:34 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-23 18:54:34 -0700 |
commit | 560beb098efdd5b13f02692317fa5460c9b81141 (patch) | |
tree | 133da36f125b25f8af7488519ff15ad18ab5b379 /share | |
parent | 4cf85ed5f715fc93fe88cabeed08f8dc94fc8e68 (diff) | |
download | spack-560beb098efdd5b13f02692317fa5460c9b81141.tar.gz spack-560beb098efdd5b13f02692317fa5460c9b81141.tar.bz2 spack-560beb098efdd5b13f02692317fa5460c9b81141.tar.xz spack-560beb098efdd5b13f02692317fa5460c9b81141.zip |
csh: don't require SPACK_ROOT for sourcing setup-env.csh (#18225)
Don't require SPACK_ROOT for sourcing setup-env.csh and make output more consistent
Diffstat (limited to 'share')
-rw-r--r-- | share/spack/csh/pathadd.csh | 17 | ||||
-rw-r--r-- | share/spack/csh/spack.csh | 24 | ||||
-rwxr-xr-x | share/spack/qa/run-shell-tests | 4 | ||||
-rwxr-xr-x | share/spack/qa/setup-env-test.csh | 76 | ||||
-rwxr-xr-x | share/spack/setup-env.csh | 79 |
5 files changed, 163 insertions, 37 deletions
diff --git a/share/spack/csh/pathadd.csh b/share/spack/csh/pathadd.csh index 4390a788d0..0f8a04ba62 100644 --- a/share/spack/csh/pathadd.csh +++ b/share/spack/csh/pathadd.csh @@ -12,17 +12,26 @@ # otherwise append to that variable. set _pa_varname = PATH; set _pa_new_path = $_pa_args[1]; -[ $#_pa_args -gt 1 ] && set _pa_varname = $_pa_args[1] && set _pa_new_path = $_pa_args[2]; + +if ($#_pa_args > 1) then + set _pa_varname = $_pa_args[1] + set _pa_new_path = $_pa_args[2] +endif # Check whether the variable is set yet. set _pa_old_value = "" eval set _pa_set = '$?'$_pa_varname -[ $_pa_set -eq 1 ] && eval set _pa_old_value='$'$_pa_varname; +if ($_pa_set == 1) then + eval set _pa_old_value='$'$_pa_varname +endif # Do the actual prepending here, if it is a dir and not already in the path if ( -d $_pa_new_path && \:$_pa_old_value\: !~ *\:$_pa_new_path\:* ) then - [ -n "$_pa_old_value" ] && setenv $_pa_varname $_pa_new_path\:$_pa_old_value - [ -z "$_pa_old_value" ] && setenv $_pa_varname $_pa_new_path + if ("x$_pa_old_value" == "x") then + setenv $_pa_varname $_pa_new_path + else + setenv $_pa_varname $_pa_new_path\:$_pa_old_value + endif endif unset _pa_args _pa_new_path _pa_old_value _pa_set _pa_varname diff --git a/share/spack/csh/spack.csh b/share/spack/csh/spack.csh index 0df547fab6..ce3308067e 100644 --- a/share/spack/csh/spack.csh +++ b/share/spack/csh/spack.csh @@ -57,8 +57,12 @@ endif # Set up args -- we want a subcommand and a spec. set _sp_subcommand="" set _sp_spec="" -[ $#_sp_args -gt 0 ] && set _sp_subcommand = ($_sp_args[1]) -[ $#_sp_args -gt 1 ] && set _sp_spec = ($_sp_args[2-]) +if ($#_sp_args > 0) then + set _sp_subcommand = ($_sp_args[1]) +endif +if ($#_sp_args > 1) then + set _sp_spec = ($_sp_args[2-]) +endif # Run subcommand switch ($_sp_subcommand) @@ -66,7 +70,9 @@ case cd: shift _sp_args # get rid of 'cd' set _sp_arg="" - [ $#_sp_args -gt 0 ] && set _sp_arg = ($_sp_args[1]) + if ($#_sp_args > 0) then + set _sp_arg = ($_sp_args[1]) + endif shift _sp_args if ( "$_sp_arg" == "-h" || "$_sp_args" == "--help" ) then @@ -79,7 +85,9 @@ case env: shift _sp_args # get rid of 'env' set _sp_arg="" - [ $#_sp_args -gt 0 ] && set _sp_arg = ($_sp_args[1]) + if ($#_sp_args > 0) then + set _sp_arg = ($_sp_args[1]) + endif if ( "$_sp_arg" == "-h" || "$_sp_arg" == "--help" ) then \spack env -h @@ -87,7 +95,9 @@ case env: switch ($_sp_arg) case activate: set _sp_env_arg="" - [ $#_sp_args -gt 1 ] && set _sp_env_arg = ($_sp_args[2]) + if ($#_sp_args > 1) then + set _sp_env_arg = ($_sp_args[2]) + endif # Space needed here to differentiate between `-h` # argument and environments with "-h" in the name. @@ -106,7 +116,9 @@ case env: breaksw case deactivate: set _sp_env_arg="" - [ $#_sp_args -gt 1 ] && set _sp_env_arg = ($_sp_args[2]) + if ($#_sp_args > 1) then + set _sp_env_arg = ($_sp_args[2]) + endif # Space needed here to differentiate between `--sh` # argument and environments with "--sh" in the name. diff --git a/share/spack/qa/run-shell-tests b/share/spack/qa/run-shell-tests index c5c3425a8e..9c5302ec89 100755 --- a/share/spack/qa/run-shell-tests +++ b/share/spack/qa/run-shell-tests @@ -47,3 +47,7 @@ dash "$QA_DIR/setup-env-test.sh" # Run fish tests fish "$QA_DIR/setup-env-test.fish" + +# run csh and tcsh tests +csh "$QA_DIR/setup-env-test.csh" +tcsh "$QA_DIR/setup-env-test.csh" diff --git a/share/spack/qa/setup-env-test.csh b/share/spack/qa/setup-env-test.csh new file mode 100755 index 0000000000..02dda30bca --- /dev/null +++ b/share/spack/qa/setup-env-test.csh @@ -0,0 +1,76 @@ +#!/bin/csh +# +# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other +# Spack Project Developers. See the top-level COPYRIGHT file for details. +# +# SPDX-License-Identifier: (Apache-2.0 OR MIT) + +# +# This tests that Spack's setup-env.csh init script works. +# +# There are limited tests here so far, as we haven't ported the unit test +# functions we have for sh/bash/zsh/fish to csh. +# + +# ----------------------------------------------------------------------- +# Setup test environment and do some preliminary checks +# ----------------------------------------------------------------------- + +# find spack but don't call it SPACK_ROOT -- we want to ensure that +# setup-env.csh sets that. +set QA_DIR = `dirname $0` +set SPACK_DIR = `cd $QA_DIR/../../.. && pwd` + +# Make sure no environment is active, and SPACK_ROOT is not set +unsetenv SPACK_ENV +unsetenv SPACK_ROOT + +# Source setup-env.sh before tests +source "$SPACK_DIR/share/spack/setup-env.csh" + +echo -n "SPACK_ROOT is set..." +if (! $?SPACK_ROOT) then + echo "FAIL" + echo "Error: SPACK_ROOT not set by setup-env.csh" + exit 1 +else + echo "SUCCESS" +endif + +echo -n "SPACK_ROOT is set correctly..." +if ("$SPACK_ROOT" != "$SPACK_DIR") then + echo "FAIL" + echo "Error: SPACK_ROOT not set correctly by setup-env.csh" + echo " Expected: '$SPACK_DIR'" + echo " Found: '$SPACK_ROOT'" + exit 1 +else + echo "SUCCESS" +endif + +echo -n "spack is in the path..." +set spack_script = `which \spack` +if ("$spack_script" != "$SPACK_DIR/bin/spack") then + echo "FAIL" + echo "Error: could not find spack after sourcing." + echo " Expected: '$SPACK_DIR/bin/spack'" + echo " Found: '$spack_script'" + exit 1 +else + echo "SUCCESS" +endif + +echo -n "spack is aliased to something after sourcing..." +set spack_alias = `which spack` +if ("$spack_alias" !~ 'spack: aliased to '*) then + echo "FAIL" + echo "Error: spack not aliased after sourcing." + echo " Expected: 'spack: aliased to [...]'" + echo " Found: '$spack_alias'" + exit 1 +else + echo "SUCCESS" +endif + +echo "SUCCESS" +exit 0 diff --git a/share/spack/setup-env.csh b/share/spack/setup-env.csh index 1985e023de..b70036456d 100755 --- a/share/spack/setup-env.csh +++ b/share/spack/setup-env.csh @@ -3,14 +3,12 @@ # # SPDX-License-Identifier: (Apache-2.0 OR MIT) - # # This file is part of Spack and sets up the spack environment for # csh and tcsh. This includes environment modules and lmod support, and # it also puts spack in your path. Source it like this: # -# setenv SPACK_ROOT /path/to/spack -# source $SPACK_ROOT/share/spack/setup-env.csh +# source /path/to/spack/share/spack/setup-env.csh # # prevent infinite recursion when spack shells out (e.g., on cray for modules) @@ -19,32 +17,59 @@ if ($?_sp_initializing) then endif setenv _sp_initializing true -if ($?SPACK_ROOT) then - set _spack_source_file = $SPACK_ROOT/share/spack/setup-env.csh - set _spack_share_dir = $SPACK_ROOT/share/spack - - # Command aliases point at separate source files - alias spack 'set _sp_args = (\!*); source $_spack_share_dir/csh/spack.csh' - alias spacktivate 'spack env activate' - alias _spack_pathadd 'set _pa_args = (\!*) && source $_spack_share_dir/csh/pathadd.csh' - - # Set variables needed by this script - _spack_pathadd PATH "$SPACK_ROOT/bin" - eval `spack --print-shell-vars csh` - - # Set up module search paths in the user environment - set tcl_roots = `echo $_sp_tcl_roots:q | sed 's/:/ /g'` - set compatible_sys_types = `echo $_sp_compatible_sys_types:q | sed 's/:/ /g'` - foreach tcl_root ($tcl_roots:q) - foreach systype ($compatible_sys_types:q) - _spack_pathadd MODULEPATH "$tcl_root/$systype" - end - end +# If SPACK_ROOT is not set, we'll try to find it ourselves. +# csh/tcsh don't have a built-in way to do this, but both keep files +# they are sourcing open. We use /proc on linux and lsof on macs to +# find this script's full path in the current process's open files. +if (! $?SPACK_ROOT) then + # figure out a command to list open files + if (-d /proc/$$/fd) then + set _sp_lsof = "ls -l /proc/$$/fd" + else + which lsof > /dev/null + if ($? == 0) then + set _sp_lsof = "lsof -p $$" + endif + endif + + # filter this script out of list of open files + if ( $?_sp_lsof ) then + set _sp_source_file = `$_sp_lsof | sed -e 's/^[^/]*//' | grep "/setup-env.csh"` + endif + + # This script is in $SPACK_ROOT/share/spack; get the root with dirname + if ($?_sp_source_file) then + set _sp_share_spack = `dirname "$_sp_source_file"` + set _sp_share = `dirname "$_sp_share_spack"` + setenv SPACK_ROOT `dirname "$_sp_share"` + endif -else - echo "ERROR: Sourcing spack setup-env.csh requires setting SPACK_ROOT to " - echo " the root of your spack installation." + if (! $?SPACK_ROOT) then + echo "==> Error: setup-env.csh couldn't figure out where spack lives." + echo " Set SPACK_ROOT to the root of your spack installation and try again." + exit 1 + endif endif +# Command aliases point at separate source files +set _spack_source_file = $SPACK_ROOT/share/spack/setup-env.csh +set _spack_share_dir = $SPACK_ROOT/share/spack +alias spack 'set _sp_args = (\!*); source $_spack_share_dir/csh/spack.csh' +alias spacktivate 'spack env activate' +alias _spack_pathadd 'set _pa_args = (\!*) && source $_spack_share_dir/csh/pathadd.csh' + +# Set variables needed by this script +_spack_pathadd PATH "$SPACK_ROOT/bin" +eval `spack --print-shell-vars csh` + +# Set up module search paths in the user environment +set tcl_roots = `echo $_sp_tcl_roots:q | sed 's/:/ /g'` +set compatible_sys_types = `echo $_sp_compatible_sys_types:q | sed 's/:/ /g'` +foreach tcl_root ($tcl_roots:q) + foreach systype ($compatible_sys_types:q) + _spack_pathadd MODULEPATH "$tcl_root/$systype" + end +end + # done: unset sentinel variable as we're no longer initializing unsetenv _sp_initializing |