summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Krafczyk <krafczyk.matthew@gmail.com>2017-02-07 14:21:31 -0600
committerMatthew Krafczyk <krafczyk.matthew@gmail.com>2017-02-08 16:24:54 -0600
commit4eb7b241c67dad1c95bd5d92ff6c954cb3e48ae2 (patch)
treef92e06ef5728110f523e648af2ed41fad41ec47c
parent81424dce5cac5be2bae78e3b737ba7b8c8bc1ea3 (diff)
downloadspack-4eb7b241c67dad1c95bd5d92ff6c954cb3e48ae2.tar.gz
spack-4eb7b241c67dad1c95bd5d92ff6c954cb3e48ae2.tar.bz2
spack-4eb7b241c67dad1c95bd5d92ff6c954cb3e48ae2.tar.xz
spack-4eb7b241c67dad1c95bd5d92ff6c954cb3e48ae2.zip
Add code to automatically build and enable module
If the module command doesn't exist in the shell, automatically build a version which doesn't depend on X windows and create the module function for the appropriate shell.
-rwxr-xr-xshare/spack/setup-env.sh59
1 files changed, 57 insertions, 2 deletions
diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh
index 943db72612..0efc1222e9 100755
--- a/share/spack/setup-env.sh
+++ b/share/spack/setup-env.sh
@@ -27,7 +27,9 @@
#
# This file is part of Spack and sets up the spack environment for
# bash and zsh. This includes dotkit support, module support, and
-# it also puts spack in your path. Source it like this:
+# it also puts spack in your path. The script also checks that
+# at least module support exists, and builds and enables it if it
+# doesn't. Source it like this:
#
# . /path/to/spack/share/spack/setup-env.sh
#
@@ -182,14 +184,67 @@ if [ -z "$_sp_source_file" ]; then
fi
#
-# Set up modules and dotkit search paths in the user environment
+# Find root directory and add bin to path.
#
_sp_share_dir=$(cd "$(dirname $_sp_source_file)" && pwd)
_sp_prefix=$(cd "$(dirname $(dirname $_sp_share_dir))" && pwd)
_spack_pathadd PATH "${_sp_prefix%/}/bin"
+export SPACK_ROOT=${_sp_prefix}
+
+#
+#
+# Determine which shell is being used
+#
+function _spack_determine_shell() {
+ ps -p $$ | tail -n 1 | awk '{print $4}' | xargs basename
+}
+export SPACK_SHELL=$(_spack_determine_shell)
+#
+# Check whether at least one of 'use' and 'module' exists
+#
+function _spack_fn_exists() {
+ type $1 2>&1 | grep -q 'shell function'
+}
+
+need_module="no"
+if [ ! $(_spack_fn_exists use) ]; then
+ if [ ! $(_spack_fn_exists module) ]; then
+ need_module="yes"
+ fi;
+fi;
+
+#
+# build and make available environment-modules
+#
+if [ "${need_module}" = "yes" ]; then
+ #check if environment-modules~X is installed
+ spack location -i environment-modules~X >& /dev/null
+ if [ $? -eq 1 ]; then
+ #install it!
+ spack install environment-modules~X
+ fi;
+ export MODULE_PREFIX=`spack location -i environment-modules~X`
+
+ module() { eval `${MODULE_PREFIX}/Modules/bin/modulecmd ${SPACK_SHELL} $*`; }
+fi;
+
+#
+# Set up modules and dotkit search paths in the user environment
+#
_sp_sys_type=$(spack-python -c 'print(spack.architecture.sys_type())')
_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"
+
+# Export spack function so it is available in subshells (only works with bash)
+if [ -n "${BASH_VERSION:-}" ]; then
+ export -f spack
+fi
+
+# Add programmable tab completion for Bash
+#
+if [ -n "${BASH_VERSION:-}" ]; then
+ source $_sp_share_dir/spack-completion.bash
+fi