From dbb33d98ebcd1408cd1adb98d92737f3622fd4dd Mon Sep 17 00:00:00 2001
From: "Fabian.Jakub" <fabian@jakub.com>
Date: Wed, 22 Mar 2023 16:35:25 +0100
Subject: [PATCH] ff

---
 conf/real-from-ideal/proj.yaml  |  9 ++---
 platforms/common/spack_utils.sh | 60 ++++++++++++++++++++++-----------
 templates/common/build_icon.sh  |  2 +-
 3 files changed, 46 insertions(+), 25 deletions(-)

diff --git a/conf/real-from-ideal/proj.yaml b/conf/real-from-ideal/proj.yaml
index 8e23cfd..c6d79d6 100644
--- a/conf/real-from-ideal/proj.yaml
+++ b/conf/real-from-ideal/proj.yaml
@@ -2,10 +2,11 @@ spack:
   #url: git@gitlab.physik.uni-muenchen.de:LDAP_rbg/spack.git
   #branch: lmu/ubuntu20.04-icon
   #compiler: gcc@11.3.0
-  url: https://github.com/spack/spack.git
-  branch: develop
-  compiler: gcc@12.2.0
-  root: "%HPCROOTDIR%/spack" # path to a spack install, will be cloned with git if not present
+  init: # command to load spack environment, e.g. module load spack, use spack/setup-env.sh if empty
+  url: https://github.com/spack/spack.git # url to download spack if necessary
+  branch: develop # if downloaded, branch name to use
+  compiler: gcc@12.2.0 # desired compiler for spack
+  root: "%HPCROOTDIR%/spack" # path to a spack install, will be downloaded to if not present
   user_cache_path: "%HPCROOTDIR%/spack_user_cache_path" # spack puts data here when bootstrapping, leave empty to use home folder
   disable_local_config: true # if false, spack install into home folder
 
diff --git a/platforms/common/spack_utils.sh b/platforms/common/spack_utils.sh
index fab55d4..aec1794 100644
--- a/platforms/common/spack_utils.sh
+++ b/platforms/common/spack_utils.sh
@@ -1,16 +1,10 @@
-# static settings when using spack
-function spack_env() {
-export SPACK_SETUP_ENV=$SPACK_ROOT/share/spack/setup-env.sh
-}
-
 function _install_spack() {
-spack_env
-
-if [ ! -f ${SPACK_ROOT} ]; then
+if [ ! -e ${SPACK_ROOT} ]; then
+  echo "Cloning to ${SPACK_ROOT}"
   git clone ${SPACK_URL} -b ${SPACK_BRANCH} ${SPACK_ROOT}
 fi
 
-. ${SPACK_SETUP_ENV}
+_init_spack
 
 if [[ $(spack compiler info ${SPACK_COMPILER}) ]]; then
   echo "Found Compiler $(spack compiler info ${SPACK_COMPILER})"
@@ -21,18 +15,44 @@ else
 fi
 }
 
+function _init_spack() {
+SPACK_INIT_CMD=$1
+SPACK_ROOT=$2
+if [ -z "$SPACK_INIT_CMD" ] && [ ! -z "${SPACK_ROOT}" ]; then
+  echo "Empty SPACK_INIT_CMD -> trying to source config file of spack root: $SPACK_ROOT/share/spack/setup-env.sh"
+  . $SPACK_ROOT/share/spack/setup-env.sh
+else
+  echo "Executing SPACK_INIT_CMD: $SPACK_INIT_CMD"
+  $SPACK_INIT_CMD
+fi
+}
+
 function load_spack() {
-export SPACK_ROOT=$1
-export SPACK_URL=$2
-export SPACK_BRANCH=$3
-export SPACK_COMPILER=$4
-export SPACK_DISABLE_LOCAL_CONFIG=$5
-export SPACK_USER_CACHE_PATH=$6
-if [ ! -z "$SPACK_USER_CACHE_PATH" ]; then
-  echo "setting SPACK_USER_CACHE_PATH=$SPACK_USER_CACHE_PATH"
+export SPACK_INIT_CMD=$1
+export SPACK_ROOT=${2:-spack}
+export SPACK_URL=${3:-https://github.com/spack/spack.git}
+export SPACK_BRANCH=${4:-develop}
+export SPACK_COMPILER=${5:-gcc@12.2.0}
+export SPACK_DISABLE_LOCAL_CONFIG=${6:-true}
+export SPACK_USER_CACHE_PATH=${7:-${SPACK_ROOT}/spack_user_cache_path}
+
+echo "SPACK_INIT_CMD                = $SPACK_INIT_CMD"
+echo "SPACK_ROOT                    = $SPACK_ROOT"
+echo "SPACK_URL                     = $SPACK_URL"
+echo "SPACK_BRANCH                  = $SPACK_BRANCH"
+echo "SPACK_COMPILER                = $SPACK_COMPILER"
+echo "SPACK_DISABLE_LOCAL_CONFIG    = $SPACK_DISABLE_LOCAL_CONFIG"
+echo "SPACK_USER_CACHE_PATH         = $SPACK_USER_CACHE_PATH"
+
+if [ -z "$SPACK_USER_CACHE_PATH" ]; then
+  unset SPACK_USER_CACHE_PATH
 fi
-spack_env
-if [ ! -f ${SPACK_ROOT} ]; then _install_spack; fi
-. ${SPACK_SETUP_ENV}
+
+
+if [ ! -e ${SPACK_ROOT} ] && [ ! -z "${SPACK_ROOT}" ] ; then _install_spack; fi
+
+_init_spack "$SPACK_INIT_CMD" "$SPACK_ROOT"
+
+echo "Using spack from $(which spack)"
 }
 
diff --git a/templates/common/build_icon.sh b/templates/common/build_icon.sh
index 75789e7..40052bf 100644
--- a/templates/common/build_icon.sh
+++ b/templates/common/build_icon.sh
@@ -12,7 +12,7 @@ fi
 cd ${WORKDIR}
 
 . ${WORKDIR}/proj/platforms/common/spack_utils.sh
-load_spack "%spack.root%" "%spack.url%" "%spack.branch%" "%spack.compiler%" "%spack.disable_local_config%" "%spack.user_cache_path%"
+load_spack "%spack.init%" "%spack.root%" "%spack.url%" "%spack.branch%" "%spack.compiler%" "%spack.disable_local_config%" "%spack.user_cache_path%"
 
 SPACK_BUILD_ICON=%icon.spack_cmd%
 if [ ! -z $SPACK_BUILD_ICON ]; then
-- 
GitLab