Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-05-12 07:57:27

0001 #!/bin/bash
0002 set -u
0003 set -e
0004 set -o pipefail
0005 
0006 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
0007 
0008 export SPACK_COLOR=always
0009 
0010 function start_section() {
0011     local section_name="$1"
0012     if [ -n "${GITHUB_ACTIONS:-}" ]; then
0013         echo "::group::${section_name}"
0014     else
0015         echo "${section_name}"
0016     fi
0017 }
0018 
0019 function end_section() {
0020     if [ -n "${GITHUB_ACTIONS:-}" ]; then
0021         echo "::endgroup::"
0022     fi
0023 }
0024 
0025 # Parse command line arguments
0026 while getopts "c:t:d:e:h" opt; do
0027   case ${opt} in
0028     c )
0029       compiler=$OPTARG
0030       ;;
0031     t )
0032       tag=$OPTARG
0033       ;;
0034     d )
0035       destination=$OPTARG
0036       ;;
0037     e )
0038       env_file=$OPTARG
0039       ;;
0040     h )
0041       echo "Usage: $0 [-c compiler] [-t tag] [-d destination]"
0042       echo "Options:"
0043       echo "  -c <compiler>    Specify compiler (defaults to CXX env var)"
0044       echo "  -t <tag>         Specify dependency tag (defaults to DEPENDENCY_TAG env var)"
0045       echo "  -d <destination> Specify install destination (defaults based on CI environment)"
0046       echo "  -e <env_file>    Specify environment file to output environments to"
0047       echo "  -h              Show this help message"
0048       exit 0
0049       ;;
0050     \? )
0051       echo "Invalid option: -$OPTARG" 1>&2
0052       exit 1
0053       ;;
0054     : )
0055       echo "Option -$OPTARG requires an argument" 1>&2
0056       exit 1
0057       ;;
0058   esac
0059 done
0060 
0061 # Set defaults if not specified
0062 if [ -z "${compiler:-}" ]; then
0063   compiler="${CXX:-default}"
0064 fi
0065 
0066 if [ -z "${tag:-}" ]; then
0067   tag="${DEPENDENCY_TAG:-}"
0068   if [ -z "${tag:-}" ]; then
0069     echo "No tag specified via -t or DEPENDENCY_TAG environment variable"
0070     exit 1
0071   fi
0072 fi
0073 
0074 if [ -z "${destination:-}" ]; then
0075   if [ -n "${GITHUB_ACTIONS:-}" ]; then
0076     destination="${GITHUB_WORKSPACE}/dependencies"
0077   elif [ -n "${GITLAB_CI:-}" ]; then
0078     destination="${CI_PROJECT_DIR}/dependencies"
0079   else
0080     echo "No destination specified via -d and not running in CI"
0081     exit 1
0082   fi
0083 fi
0084 
0085 if [ -z "${env_file:-}" ]; then
0086   if [ -n "${GITHUB_ACTIONS:-}" ]; then
0087     env_file="${GITHUB_ENV}"
0088   else
0089     echo "No environment file specified via -e and not running in GitHub Actions"
0090     exit 1
0091   fi
0092 fi
0093 
0094 export env_file
0095 
0096 function set_env {
0097   key="$1"
0098   value="$2"
0099 
0100   echo "=> ${key}=${value}"
0101 
0102   if [ -n "${GITHUB_ACTIONS:-}" ]; then
0103     echo "${key}=${value}" >> "$env_file"
0104   else
0105     echo "export ${key}=${value}" >> "$env_file"
0106   fi
0107 }
0108 
0109 
0110 
0111 
0112 echo "Install tag: $tag"
0113 echo "Install destination: $destination"
0114 
0115 mkdir -p ${destination}
0116 
0117 if [ -n "${GITLAB_CI:-}" ]; then
0118     _spack_folder=${CI_PROJECT_DIR}/spack
0119 else
0120     _spack_folder=${PWD}/spack
0121 fi
0122 
0123 start_section "Install spack if not already installed"
0124 if ! command -v spack &> /dev/null; then
0125   "${SCRIPT_DIR}/setup_spack.sh" "${_spack_folder}"
0126   source "${_spack_folder}/share/spack/setup-env.sh"
0127 fi
0128 end_section
0129 
0130 if [ -n "${GITLAB_CI:-}" ]; then
0131   # Use the project spack config for GitLab CI so we can cache it
0132   mkdir -p ${CI_PROJECT_DIR}/.spack
0133   ln -s ${CI_PROJECT_DIR}/.spack ${HOME}/.spack
0134 fi
0135 
0136 
0137 
0138 if [ -n "${CI:-}" ]; then
0139   start_section "Add buildcache mirror"
0140   mirror_name="acts-spack-buildcache"
0141   mirror_url="oci://ghcr.io/acts-project/spack-buildcache"
0142   if [ -n "${GITLAB_CI:-}" ]; then
0143   # Use CERN mirror for non-Github Actions
0144     mirror_url="oci://registry.cern.ch/ghcr.io/acts-project/spack-buildcache"
0145   fi
0146 
0147   # Check if this buildcache is already configured
0148   if ! spack mirror list | grep -q ${mirror_name}; then
0149     echo "Adding buildcache ${mirror_name}"
0150     spack mirror add ${mirror_name} ${mirror_url} --unsigned
0151   fi
0152   end_section
0153 
0154   start_section "Locate OpenGL"
0155   "${SCRIPT_DIR}/opengl.sh"
0156   end_section
0157 fi
0158 
0159 start_section "Get spack lock file"
0160 arch=$(spack arch --family)
0161 
0162 env_dir="${destination}/env"
0163 view_dir="${destination}/view"
0164 mkdir -p ${env_dir}
0165 
0166 lock_file_path="${destination}/spack.lock"
0167 cmd=(
0168     "${SCRIPT_DIR}/select_lockfile.py"
0169     "--tag" "${tag}"
0170     "--arch" "${arch}"
0171     "--output" "${lock_file_path}"
0172 )
0173 
0174 if [ "${compiler}" != "default" ]; then
0175     cmd+=("--compiler-binary" "${compiler}")
0176 fi
0177 
0178 "${cmd[@]}"
0179 
0180 end_section
0181 
0182 
0183 
0184 start_section "Create spack environment"
0185 time spack env create -d "${env_dir}" "${lock_file_path}" --with-view "$view_dir"
0186 time spack -e "${env_dir}" spec -l
0187 time spack -e "${env_dir}" find
0188 end_section
0189 
0190 start_section "Install spack packages"
0191 NCPUS=4 # set fixes low-ish number to avoid deadlocks
0192 time "${SCRIPT_DIR}"/parallel.sh "$NCPUS" spack -e "${env_dir}" install --fail-fast --use-buildcache only \
0193   | tee install.log \
0194   | grep -v "^Waiting\|^\[+\]"
0195 end_section
0196 
0197 start_section "Patch up Geant4 data directory"
0198 # ${SCRIPT_DIR}/with_spack_env.sh ${env_dir} geant4-config --install-datasets
0199 geant4_dir=$(spack -e "${env_dir}" location -i geant4)
0200 # Prepare the folder for G4 data, and symlink it to where G4 will look for it
0201 mkdir -p "${geant4_dir}/share/Geant4"
0202 ln -s "${geant4_dir}/share/Geant4/data" "${view_dir}/share/Geant4/data"
0203 end_section
0204 
0205 
0206 start_section "Prepare python environment"
0207 ls -al
0208 venv_dir="${view_dir}/venv"
0209 "${view_dir}"/bin/python3 -m venv \
0210   --system-site-packages \
0211   "$venv_dir"
0212 
0213 "${venv_dir}/bin/python3" -m pip install pyyaml jinja2
0214 
0215 end_section
0216 
0217 start_section "Set environment variables"
0218 if [ -n "${GITHUB_ACTIONS:-}" ]; then
0219   echo "${view_dir}/bin" >> "$GITHUB_PATH"
0220   echo "${venv_dir}/bin" >> "$GITHUB_PATH"
0221 fi
0222 set_env PATH "${venv_dir}/bin:${view_dir}/bin/:${PATH}"
0223 set_env ROOT_SETUP_SCRIPT "${view_dir}/bin/thisroot.sh"
0224 set_env CMAKE_PREFIX_PATH "${venv_dir}:${view_dir}"
0225 set_env LD_LIBRARY_PATH "${view_dir}/lib"
0226 set_env ROOT_INCLUDE_PATH "${view_dir}/include"
0227 end_section