2019-01-01 06:04:23 +00:00
|
|
|
# Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
|
2018-10-07 20:52:23 +00:00
|
|
|
# Spack Project Developers. See the top-level COPYRIGHT file for details.
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
2018-10-07 20:52:23 +00:00
|
|
|
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
|
|
|
|
2014-07-08 08:56:32 +00:00
|
|
|
|
2016-05-12 04:22:25 +00:00
|
|
|
########################################################################
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
|
|
|
# This file is part of Spack and sets up the spack environment for
|
2014-08-16 21:53:57 +00:00
|
|
|
# bash and zsh. This includes dotkit support, module support, and
|
2017-09-08 17:15:06 +00:00
|
|
|
# it also puts spack in your path. The script also checks that
|
|
|
|
# at least module support exists, and provides suggestions if it
|
|
|
|
# doesn't. Source it like this:
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
2014-08-16 21:53:57 +00:00
|
|
|
# . /path/to/spack/share/spack/setup-env.sh
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
|
|
|
########################################################################
|
|
|
|
# This is a wrapper around the spack command that forwards calls to
|
|
|
|
# 'spack use' and 'spack unuse' to shell functions. This in turn
|
|
|
|
# allows them to be used to invoke dotkit functions.
|
|
|
|
#
|
|
|
|
# 'spack use' is smarter than just 'use' because it converts its
|
|
|
|
# arguments into a unique spack spec that is then passed to dotkit
|
|
|
|
# commands. This allows the user to use packages without knowing all
|
|
|
|
# their installation details.
|
|
|
|
#
|
2016-04-15 10:21:32 +00:00
|
|
|
# e.g., rather than requiring a full spec for libelf, the user can type:
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
|
|
|
# spack use libelf
|
|
|
|
#
|
|
|
|
# This will first find the available libelf dotkits and use a
|
|
|
|
# matching one. If there are two versions of libelf, the user would
|
|
|
|
# need to be more specific, e.g.:
|
|
|
|
#
|
|
|
|
# spack use libelf@0.8.13
|
|
|
|
#
|
|
|
|
# This is very similar to how regular spack commands work and it
|
|
|
|
# avoids the need to come up with a user-friendly naming scheme for
|
|
|
|
# spack dotfiles.
|
|
|
|
########################################################################
|
2015-12-29 17:04:54 +00:00
|
|
|
|
2014-07-08 08:56:32 +00:00
|
|
|
function spack {
|
2016-07-13 18:33:14 +00:00
|
|
|
# Zsh does not do word splitting by default, this enables it for this function only
|
2016-11-30 01:04:29 +00:00
|
|
|
if [ -n "${ZSH_VERSION:-}" ]; then
|
2016-07-13 18:33:14 +00:00
|
|
|
emulate -L sh
|
|
|
|
fi
|
|
|
|
|
2015-11-25 19:02:51 +00:00
|
|
|
# save raw arguments into an array before butchering them
|
2015-12-30 18:55:52 +00:00
|
|
|
args=( "$@" )
|
2015-12-29 17:04:54 +00:00
|
|
|
|
2014-08-17 08:41:32 +00:00
|
|
|
# accumulate initial flags for main spack command
|
|
|
|
_sp_flags=""
|
|
|
|
while [[ "$1" =~ ^- ]]; do
|
|
|
|
_sp_flags="$_sp_flags $1"
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
# h and V flags don't require further output parsing.
|
2015-06-09 16:07:58 +00:00
|
|
|
if [[ (! -z "$_sp_flags") && ("$_sp_flags" =~ '.*h.*' || "$_sp_flags" =~ '.*V.*') ]]; then
|
2014-08-17 08:41:32 +00:00
|
|
|
command spack $_sp_flags "$@"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2018-07-27 13:14:01 +00:00
|
|
|
_sp_subcommand=""
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
_sp_subcommand="$1"
|
|
|
|
shift
|
|
|
|
fi
|
2018-04-24 20:43:32 +00:00
|
|
|
_sp_spec=("$@")
|
2014-07-08 08:56:32 +00:00
|
|
|
|
|
|
|
# Filter out use and unuse. For any other commands, just run the
|
|
|
|
# command.
|
2014-08-16 21:53:57 +00:00
|
|
|
case $_sp_subcommand in
|
2014-08-22 05:59:39 +00:00
|
|
|
"cd")
|
2018-07-27 13:14:01 +00:00
|
|
|
_sp_arg=""
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
_sp_arg="$1"
|
|
|
|
shift
|
|
|
|
fi
|
2018-12-16 01:47:05 +00:00
|
|
|
if [[ "$_sp_arg" = "-h" || "$_sp_arg" = "--help" ]]; then
|
2014-09-30 05:39:36 +00:00
|
|
|
command spack cd -h
|
|
|
|
else
|
2016-04-29 16:33:54 +00:00
|
|
|
LOC="$(spack location $_sp_arg "$@")"
|
|
|
|
if [[ -d "$LOC" ]] ; then
|
2016-04-28 22:43:05 +00:00
|
|
|
cd "$LOC"
|
2017-06-28 11:41:04 +00:00
|
|
|
else
|
|
|
|
return 1
|
2016-04-29 16:33:54 +00:00
|
|
|
fi
|
2014-09-30 05:39:36 +00:00
|
|
|
fi
|
2014-08-22 05:59:39 +00:00
|
|
|
return
|
|
|
|
;;
|
2018-10-14 22:21:57 +00:00
|
|
|
"env")
|
|
|
|
_sp_arg=""
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
_sp_arg="$1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2018-12-16 01:47:05 +00:00
|
|
|
if [[ "$_sp_arg" = "-h" || "$_sp_arg" = "--help" ]]; then
|
2018-10-14 22:21:57 +00:00
|
|
|
command spack env -h
|
|
|
|
else
|
|
|
|
case $_sp_arg in
|
|
|
|
activate)
|
2018-10-16 06:04:45 +00:00
|
|
|
_a="$@"
|
|
|
|
if [ -z "$1" -o "${_a#*--sh}" != "$_a" -o "${_a#*--csh}" != "$_a" -o "${_a#*-h}" != "$_a" ]; then
|
|
|
|
# no args or args contain -h/--help, --sh, or --csh: just execute
|
2018-10-14 22:21:57 +00:00
|
|
|
command spack "${args[@]}"
|
|
|
|
else
|
|
|
|
# actual call to activate: source the output
|
|
|
|
eval $(command spack $_sp_flags env activate --sh "$@")
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
deactivate)
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
# with args: execute the command
|
|
|
|
command spack "${args[@]}"
|
|
|
|
else
|
|
|
|
# no args: source the output.
|
|
|
|
eval $(command spack $_sp_flags env deactivate --sh)
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
command spack "${args[@]}"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
return
|
|
|
|
;;
|
2014-08-16 21:53:57 +00:00
|
|
|
"use"|"unuse"|"load"|"unload")
|
2014-07-31 20:42:34 +00:00
|
|
|
# Shift any other args for use off before parsing spec.
|
2016-07-13 18:33:14 +00:00
|
|
|
_sp_subcommand_args=""
|
2014-08-16 21:53:57 +00:00
|
|
|
_sp_module_args=""
|
2016-07-13 18:33:14 +00:00
|
|
|
while [[ "$1" =~ ^- ]]; do
|
2016-07-14 18:14:06 +00:00
|
|
|
if [ "$1" = "-r" -o "$1" = "--dependencies" ]; then
|
2016-07-13 18:33:14 +00:00
|
|
|
_sp_subcommand_args="$_sp_subcommand_args $1"
|
|
|
|
else
|
|
|
|
_sp_module_args="$_sp_module_args $1"
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2018-04-24 20:43:32 +00:00
|
|
|
_sp_spec=("$@")
|
2014-07-31 20:42:34 +00:00
|
|
|
|
|
|
|
# Here the user has run use or unuse with a spec. Find a matching
|
2014-08-16 21:53:57 +00:00
|
|
|
# spec using 'spack module find', then use the appropriate module
|
|
|
|
# tool's commands to add/remove the result from the environment.
|
|
|
|
# If spack module command comes back with an error, do nothing.
|
2014-08-22 18:00:19 +00:00
|
|
|
case $_sp_subcommand in
|
|
|
|
"use")
|
2018-07-20 08:30:36 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module dotkit find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
2014-08-22 18:00:19 +00:00
|
|
|
use $_sp_module_args $_sp_full_spec
|
2018-07-10 08:54:55 +00:00
|
|
|
else
|
|
|
|
$(exit 1)
|
2014-08-22 18:00:19 +00:00
|
|
|
fi ;;
|
|
|
|
"unuse")
|
2018-07-20 08:30:36 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module dotkit find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
2014-08-22 18:00:19 +00:00
|
|
|
unuse $_sp_module_args $_sp_full_spec
|
2018-07-10 08:54:55 +00:00
|
|
|
else
|
|
|
|
$(exit 1)
|
2014-08-22 18:00:19 +00:00
|
|
|
fi ;;
|
|
|
|
"load")
|
2018-07-20 08:30:36 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module tcl find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
2014-08-22 18:00:19 +00:00
|
|
|
module load $_sp_module_args $_sp_full_spec
|
2018-07-10 08:54:55 +00:00
|
|
|
else
|
|
|
|
$(exit 1)
|
2014-08-22 18:00:19 +00:00
|
|
|
fi ;;
|
|
|
|
"unload")
|
2018-07-20 08:30:36 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module tcl find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
2014-08-22 18:00:19 +00:00
|
|
|
module unload $_sp_module_args $_sp_full_spec
|
2018-07-10 08:54:55 +00:00
|
|
|
else
|
|
|
|
$(exit 1)
|
2014-08-22 18:00:19 +00:00
|
|
|
fi ;;
|
|
|
|
esac
|
2014-07-31 20:42:34 +00:00
|
|
|
;;
|
2014-07-08 08:56:32 +00:00
|
|
|
*)
|
2015-11-25 19:02:51 +00:00
|
|
|
command spack "${args[@]}"
|
2014-08-22 05:59:39 +00:00
|
|
|
;;
|
2014-07-08 08:56:32 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# Prepends directories to path, if they exist.
|
|
|
|
# pathadd /path/to/dir # add to PATH
|
|
|
|
# or pathadd OTHERPATH /path/to/dir # add to OTHERPATH
|
|
|
|
########################################################################
|
|
|
|
function _spack_pathadd {
|
|
|
|
# If no variable name is supplied, just append to PATH
|
|
|
|
# otherwise append to that variable.
|
2014-08-16 21:53:57 +00:00
|
|
|
_pa_varname=PATH
|
|
|
|
_pa_new_path="$1"
|
2014-07-08 08:56:32 +00:00
|
|
|
if [ -n "$2" ]; then
|
2014-08-16 21:53:57 +00:00
|
|
|
_pa_varname="$1"
|
|
|
|
_pa_new_path="$2"
|
2014-07-08 08:56:32 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Do the actual prepending here.
|
2016-03-19 22:42:06 +00:00
|
|
|
eval "_pa_oldvalue=\${${_pa_varname}:-}"
|
2014-08-16 21:53:57 +00:00
|
|
|
|
|
|
|
if [ -d "$_pa_new_path" ] && [[ ":$_pa_oldvalue:" != *":$_pa_new_path:"* ]]; then
|
|
|
|
if [ -n "$_pa_oldvalue" ]; then
|
|
|
|
eval "export $_pa_varname=\"$_pa_new_path:$_pa_oldvalue\""
|
2014-07-08 08:56:32 +00:00
|
|
|
else
|
2014-08-16 21:53:57 +00:00
|
|
|
export $_pa_varname="$_pa_new_path"
|
2014-07-08 08:56:32 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-01-23 23:29:25 +00:00
|
|
|
# Export spack function so it is available in subshells (only works with bash)
|
|
|
|
if [ -n "${BASH_VERSION:-}" ]; then
|
|
|
|
export -f spack
|
|
|
|
fi
|
|
|
|
|
2014-08-16 21:53:57 +00:00
|
|
|
#
|
|
|
|
# Figure out where this file is. Below code needs to be portable to
|
|
|
|
# bash and zsh.
|
|
|
|
#
|
2019-04-16 19:33:02 +00:00
|
|
|
_sp_source_file="${BASH_SOURCE[0]:-}" # Bash's location of last sourced file.
|
2014-08-16 21:53:57 +00:00
|
|
|
if [ -z "$_sp_source_file" ]; then
|
2019-04-16 19:33:02 +00:00
|
|
|
_sp_source_file="${(%):-%N}" # zsh way to do it
|
|
|
|
if [ -z "$_sp_source_file" ]; then
|
2014-08-16 21:53:57 +00:00
|
|
|
# Not zsh either... bail out with plain old $0,
|
|
|
|
# which WILL NOT work if this is sourced indirectly.
|
|
|
|
_sp_source_file="$0"
|
|
|
|
fi
|
|
|
|
fi
|
2014-07-08 08:56:32 +00:00
|
|
|
|
|
|
|
#
|
2017-09-08 17:15:06 +00:00
|
|
|
# Find root directory and add bin to path.
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
2015-02-07 00:41:43 +00:00
|
|
|
_sp_share_dir=$(cd "$(dirname $_sp_source_file)" && pwd)
|
|
|
|
_sp_prefix=$(cd "$(dirname $(dirname $_sp_share_dir))" && pwd)
|
2015-12-19 05:28:44 +00:00
|
|
|
_spack_pathadd PATH "${_sp_prefix%/}/bin"
|
2017-09-08 17:15:06 +00:00
|
|
|
export SPACK_ROOT=${_sp_prefix}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Determine which shell is being used
|
|
|
|
#
|
|
|
|
function _spack_determine_shell() {
|
2019-02-14 01:52:18 +00:00
|
|
|
# This logic is derived from the cea-hpc/modules profile.sh example at
|
|
|
|
# https://github.com/cea-hpc/modules/blob/master/init/profile.sh.in
|
|
|
|
#
|
|
|
|
# The objective is to correctly detect the shell type even when setup-env
|
|
|
|
# is sourced within a script itself rather than a login terminal.
|
|
|
|
if [ -n "${BASH:-}" ]; then
|
|
|
|
echo ${BASH##*/}
|
|
|
|
elif [ -n "${ZSH_NAME:-}" ]; then
|
|
|
|
echo $ZSH_NAME
|
|
|
|
else
|
|
|
|
PS_FORMAT= ps -p $$ | tail -n 1 | awk '{print $4}' | sed 's/^-//' | xargs basename
|
|
|
|
fi
|
2017-09-08 17:15:06 +00:00
|
|
|
}
|
|
|
|
export SPACK_SHELL=$(_spack_determine_shell)
|
|
|
|
|
|
|
|
#
|
2017-09-11 02:54:23 +00:00
|
|
|
# Check whether a function of the given name is defined
|
2017-09-08 17:15:06 +00:00
|
|
|
#
|
|
|
|
function _spack_fn_exists() {
|
2017-09-26 19:28:50 +00:00
|
|
|
LANG= type $1 2>&1 | grep -q 'function'
|
2017-09-08 17:15:06 +00:00
|
|
|
}
|
2015-12-14 20:36:53 +00:00
|
|
|
|
2017-09-08 17:15:06 +00:00
|
|
|
need_module="no"
|
2017-09-11 02:54:23 +00:00
|
|
|
if ! _spack_fn_exists use && ! _spack_fn_exists module; then
|
2017-09-08 17:15:06 +00:00
|
|
|
need_module="yes"
|
|
|
|
fi;
|
|
|
|
|
2018-07-16 22:43:44 +00:00
|
|
|
|
2017-09-08 17:15:06 +00:00
|
|
|
#
|
2018-07-16 22:43:44 +00:00
|
|
|
# make available environment-modules
|
2017-09-08 17:15:06 +00:00
|
|
|
#
|
|
|
|
if [ "${need_module}" = "yes" ]; then
|
2018-07-16 22:43:44 +00:00
|
|
|
eval `spack --print-shell-vars sh,modules`
|
|
|
|
|
|
|
|
# _sp_module_prefix is set by spack --print-sh-vars
|
|
|
|
if [ "${_sp_module_prefix}" != "not_installed" ]; then
|
2019-02-28 12:22:44 +00:00
|
|
|
# activate it!
|
|
|
|
# environment-modules@4: has a bin directory inside its prefix
|
|
|
|
MODULE_PREFIX_BIN="${_sp_module_prefix}/bin"
|
|
|
|
if [ ! -d "${MODULE_PREFIX_BIN}" ]; then
|
|
|
|
# environment-modules@3 has a nested bin directory
|
|
|
|
MODULE_PREFIX_BIN="${_sp_module_prefix}/Modules/bin"
|
|
|
|
fi
|
|
|
|
export MODULE_PREFIX_BIN
|
|
|
|
_spack_pathadd PATH "${MODULE_PREFIX_BIN}"
|
|
|
|
module() { eval `${MODULE_PREFIX_BIN}/modulecmd ${SPACK_SHELL} $*`; }
|
2017-09-08 17:15:06 +00:00
|
|
|
fi;
|
2018-07-16 22:43:44 +00:00
|
|
|
else
|
|
|
|
eval `spack --print-shell-vars sh`
|
2017-09-08 17:15:06 +00:00
|
|
|
fi;
|
|
|
|
|
|
|
|
#
|
2018-07-16 22:43:44 +00:00
|
|
|
# set module system roots
|
2017-09-08 17:15:06 +00:00
|
|
|
#
|
2019-03-27 20:06:46 +00:00
|
|
|
_sp_multi_pathadd() {
|
|
|
|
local IFS=':'
|
|
|
|
if [[ -n "${ZSH_VERSION:-}" ]]; then
|
|
|
|
setopt sh_word_split
|
|
|
|
fi
|
2019-04-24 02:53:25 +00:00
|
|
|
for pth in $2; do
|
2019-03-27 20:06:46 +00:00
|
|
|
_spack_pathadd "$1" "${pth}/${_sp_sys_type}"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
_sp_multi_pathadd MODULEPATH "$_sp_tcl_roots"
|
|
|
|
_sp_multi_pathadd DK_NODE "$_sp_dotkit_roots"
|
2017-02-06 20:34:35 +00:00
|
|
|
|
|
|
|
# Add programmable tab completion for Bash
|
|
|
|
#
|
|
|
|
if [ -n "${BASH_VERSION:-}" ]; then
|
|
|
|
source $_sp_share_dir/spack-completion.bash
|
|
|
|
fi
|