2016-05-12 04:22:25 +00:00
|
|
|
##############################################################################
|
2017-09-07 03:44:16 +00:00
|
|
|
# Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC.
|
2014-07-08 08:56:32 +00:00
|
|
|
# Produced at the Lawrence Livermore National Laboratory.
|
|
|
|
#
|
|
|
|
# This file is part of Spack.
|
2016-05-12 04:22:25 +00:00
|
|
|
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
|
2014-07-08 08:56:32 +00:00
|
|
|
# LLNL-CODE-647188
|
|
|
|
#
|
2015-12-09 09:24:15 +00:00
|
|
|
# For details, see https://github.com/llnl/spack
|
2017-06-25 05:22:55 +00:00
|
|
|
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
2016-05-12 04:22:25 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License (as
|
|
|
|
# published by the Free Software Foundation) version 2.1, February 1999.
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
|
2016-05-12 04:22:25 +00:00
|
|
|
# conditions of the GNU Lesser General Public License for more details.
|
2014-07-08 08:56:32 +00:00
|
|
|
#
|
2016-05-12 04:22:25 +00:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
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
|
|
|
|
|
2014-08-16 21:53:57 +00:00
|
|
|
_sp_subcommand=$1; shift
|
|
|
|
_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")
|
2014-09-30 05:39:36 +00:00
|
|
|
_sp_arg="$1"; shift
|
|
|
|
if [ "$_sp_arg" = "-h" ]; then
|
|
|
|
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
|
|
|
|
;;
|
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
|
|
|
|
|
|
|
|
_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")
|
2016-07-21 11:15:53 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module loads --input-only $_sp_subcommand_args --module-type dotkit $_sp_spec); then
|
2014-08-22 18:00:19 +00:00
|
|
|
use $_sp_module_args $_sp_full_spec
|
|
|
|
fi ;;
|
|
|
|
"unuse")
|
2016-07-21 11:15:53 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module loads --input-only $_sp_subcommand_args --module-type dotkit $_sp_spec); then
|
2014-08-22 18:00:19 +00:00
|
|
|
unuse $_sp_module_args $_sp_full_spec
|
|
|
|
fi ;;
|
|
|
|
"load")
|
2016-07-21 11:15:53 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module loads --input-only $_sp_subcommand_args --module-type tcl $_sp_spec); then
|
2014-08-22 18:00:19 +00:00
|
|
|
module load $_sp_module_args $_sp_full_spec
|
|
|
|
fi ;;
|
|
|
|
"unload")
|
2016-07-21 11:15:53 +00:00
|
|
|
if _sp_full_spec=$(command spack $_sp_flags module loads --input-only $_sp_subcommand_args --module-type tcl $_sp_spec); then
|
2014-08-22 18:00:19 +00:00
|
|
|
module unload $_sp_module_args $_sp_full_spec
|
|
|
|
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.
|
|
|
|
#
|
|
|
|
_sp_source_file="${BASH_SOURCE[0]}" # Bash's location of last sourced file.
|
|
|
|
if [ -z "$_sp_source_file" ]; then
|
|
|
|
_sp_source_file="$0:A" # zsh way to do it
|
|
|
|
if [[ "$_sp_source_file" == *":A" ]]; then
|
|
|
|
# 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() {
|
2017-09-12 19:17:20 +00:00
|
|
|
ps -p $$ | tail -n 1 | awk '{print $4}' | sed 's/^-//' | xargs basename
|
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;
|
|
|
|
|
|
|
|
#
|
|
|
|
# build and make available environment-modules
|
|
|
|
#
|
|
|
|
if [ "${need_module}" = "yes" ]; then
|
2017-09-11 02:54:23 +00:00
|
|
|
#check if environment-modules is installed
|
2017-09-08 17:15:06 +00:00
|
|
|
module_prefix="$(spack location -i "environment-modules" 2>&1 || echo "not_installed")"
|
2017-09-11 02:54:23 +00:00
|
|
|
module_prefix=$(echo "${module_prefix}" | tail -n 1)
|
2017-09-08 17:15:06 +00:00
|
|
|
if [ "${module_prefix}" != "not_installed" ]; then
|
|
|
|
#activate it!
|
|
|
|
export MODULE_PREFIX=${module_prefix}
|
|
|
|
_spack_pathadd PATH "${MODULE_PREFIX}/Modules/bin"
|
|
|
|
module() { eval `${MODULE_PREFIX}/Modules/bin/modulecmd ${SPACK_SHELL} $*`; }
|
|
|
|
fi;
|
|
|
|
fi;
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set up modules and dotkit search paths in the user environment
|
|
|
|
#
|
2015-12-14 20:36:53 +00:00
|
|
|
_sp_sys_type=$(spack-python -c 'print(spack.architecture.sys_type())')
|
2016-11-04 19:00:01 +00:00
|
|
|
_sp_dotkit_root=$(spack-python -c "print(spack.util.path.canonicalize_path(spack.config.get_config('config').get('module_roots', {}).get('dotkit')))")
|
|
|
|
_sp_tcl_root=$(spack-python -c "print(spack.util.path.canonicalize_path(spack.config.get_config('config').get('module_roots', {}).get('tcl')))")
|
|
|
|
_spack_pathadd DK_NODE "${_sp_dotkit_root%/}/$_sp_sys_type"
|
|
|
|
_spack_pathadd MODULEPATH "${_sp_tcl_root%/}/$_sp_sys_type"
|
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
|