-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_key4hep.sh
254 lines (233 loc) · 10.7 KB
/
init_key4hep.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
## MEssage for CENTOS7 users (gluon!!!)
#cd gCentos/RHEL 7 detected
#This OS will reach the end of its maintenance support soon and won't have Key4hep builds in the future, consider upgrading to Alma 9
#Setting up the latest Key4hep software stack from CVMFS
#Note that you are using the latest stack, which may point to a newer stack in the future
#Use the following command to reproduce the current environment:
#
# source /cvmfs/sw.hsf.org/key4hep/setup.sh -r 2024-04-12
# This script sets up the Key4hep software stack from CVMFS for the stable releases
function usage() {
echo "Usage: source /cvmfs/sw.hsf.org/key4hep/setup.sh [-r <release>] [--list-releases [distribution]] [--list-packages [distribution]]"
echo " -r <release> : setup a specific release, if not specified the latest release will be used (also used for --list-packages)"
echo " --help, -h : print this help message"
echo " --list-releases [distribution] : list available releases for the specified distribution (almalinux, centos, ubuntu). By default (no OS is specified) it will list the releases for the detected distribution"
echo " --list-packages [distribution] : list available packages and their versions for the specified distribution (almalinux, centos, ubuntu). By default (no OS is specified) it will list the packages for the detected distribution"
echo "In addition, after sourcing, the command k4_local_repo can be used to add the current repository to the environment"
echo "It will delete all the existing paths containing the repository name and add some predefined paths to the environment"
}
function check_release() {
if [[ "$1" = "-r" && -n "$2" && (! -d "/cvmfs/sw.hsf.org/key4hep/releases/$2" || -z "$(/usr/bin/ls "/cvmfs/sw.hsf.org/key4hep/releases/$2" | grep $3)") ]]; then
echo "Release $2 not found, this is a list of the available releases:"
find /cvmfs/sw.hsf.org/key4hep/releases/ -maxdepth 2 -type d -name "*$3*" |
\awk -F/ '{print $(NF-1)}' | sort
echo "Aborting..."
return 1
fi
return 0
}
function list_release() {
os=$1
if [ "$os" = "almalinux" ] || [ "$os" = "almalinux9" ]; then
name="almalinux9"
elif [ "$os" = "centos" ] || [ "$os" = "centos7" ]; then
name="centos7"
elif [ "$os" = "ubuntu" ] || [ "$os" = "ubuntu22" ]; then
name="ubuntu22"
else
echo "Unsupported OS, aborting..."
usage
return 1
fi
find /cvmfs/sw.hsf.org/key4hep/releases/ -maxdepth 2 -type d -name "*$name*" |
\awk -F/ '{print $(NF-1)}' | sort
}
function list_packages() {
local os=$1
if [ "$os" = "almalinux" ] || [ "$os" = "almalinux9" ]; then
name="almalinux9"
elif [ "$os" = "centos" ] || [ "$os" = "centos7" ]; then
name="centos7"
elif [ "$os" = "ubuntu" ] || [ "$os" = "ubuntu22" ]; then
name="ubuntu22"
else
echo "Unsupported OS, aborting..."
usage
return 1
fi
find /cvmfs/sw.hsf.org/key4hep/releases/$rel/*$name*/ -maxdepth 2 -mindepth 2 -not -path '*/\.*' -type d | awk -F/ '{if ($NF ~ /develop/) printf "%s develop", $(NF-1); else {split($(NF),arr,"-"); printf "%s ", $(NF-1); printf "%s", arr[1]; for (i=2; i<length(arr); i++) printf "-%s", arr[i] } printf "\n" }'
}
rel="latest"
if [[ "$1" = "-r" && -n "$2" ]]; then
rel="$2"
fi
if [[ "$(grep -E '^ID=' /etc/os-release)" = 'ID="centos"' && "$(grep -E 'VERSION_ID' /etc/os-release)" = 'VERSION_ID="7"' ]] ||
[[ "$(grep -E '^ID=' /etc/os-release)" = 'ID="rhel"' && "$(grep -E 'VERSION_ID' /etc/os-release)" = VERSION_ID=\"7* ]]; then
os="centos7"
k4path=$(/usr/bin/ls -rd /cvmfs/sw.hsf.org/key4hep/releases/$rel/*centos7* | head -n1)
elif [[ "$(grep -E '^ID=' /etc/os-release)" = 'ID="almalinux"' && "$(grep -E 'VERSION_ID' /etc/os-release)" = VERSION_ID=\"9* ]] ||
[[ "$(grep -E '^ID=' /etc/os-release)" = 'ID="rhel"' && "$(grep -E 'VERSION_ID' /etc/os-release)" = VERSION_ID=\"9* ]] ||
[[ "$(grep -E '^ID=' /etc/os-release)" = 'ID="rocky"' && "$(grep -E 'VERSION_ID' /etc/os-release)" = VERSION_ID=\"9* ]]; then
os="almalinux9"
k4path=$(/usr/bin/ls -rd /cvmfs/sw.hsf.org/key4hep/releases/$rel/*almalinux9* | head -n1)
elif [[ "$(grep -E '^ID=' /etc/os-release)" = 'ID=ubuntu' && "$(grep -E 'VERSION_ID' /etc/os-release)" = 'VERSION_ID="22.04"' ]]; then
os="ubuntu22.04"
k4path=$(/usr/bin/ls -rd /cvmfs/sw.hsf.org/key4hep/releases/$rel/*ubuntu22* | head -n1)
else
echo "Unsupported OS or OS couldn't be correctly detected, aborting..."
echo "Supported OSes are: CentOS/RHEL 7, AlmaLinux/RockyLinux/RHEL 9, Ubuntu 22.04"
return 1
fi
check_release $1 $2 $os
if [ $? -ne 0 ]; then
return 1
fi
for ((i=1; i<=$#; i++)); do
eval arg=\$$i
case $arg in
-h|--help)
usage
return 0
;;
*)
;;
esac
done
for ((i=1; i<=$#; i++)); do
eval arg=\$$i
eval "argn=\${$((i+1))}"
case $arg in
--help|-h)
usage
return 0
;;
--list-releases)
if [ ! -n "$argn" ]; then
list_release $os
return 0
elif [ -n "$argn" ] && [[ "$argn" =~ ^(almalinux|centos|ubuntu) ]]; then
list_release $argn
return 0
else
echo "Unsupported OS $argn, aborting..."
usage
return 1
fi
;;
--list-packages)
if [ ! -n "$argn" ]; then
list_packages $os
return 0
elif [ -n "$argn" ] && [[ "$argn" =~ ^(almalinux|centos|ubuntu) ]]; then
list_packages $argn
return 0
else
echo "Unsupported OS $argn, aborting..."
usage
return 1
fi
;;
-r)
;;
*)
eval "prev=\${$((i-1))}"
if [ "$prev" != "-r" ]; then
echo "Unknown argument $arg, it will be ignored"
# usage
# return 1
fi
;;
esac
done
if [ -n "$KEY4HEP_STACK" ]; then
echo "The Key4hep software stack is already set up, please start a new shell to avoid conflicts"
return 1
fi
if [ "$os" = "centos7" ]; then
echo "Centos/RHEL 7 detected"
if [ "$rel" = "latest" ]; then
echo "This OS will reach the end of its maintenance support soon and won't have Key4hep builds in the future, consider upgrading to Alma 9"
fi
elif [ "$os" = "almalinux9" ]; then
echo "AlmaLinux/RockyLinux/RHEL 9 detected"
elif [ "$os" = "ubuntu22.04" ]; then
echo "Ubuntu 22.04 detected"
fi
_replace_marlin_dll() {
# replace the library on MARLIN_DLL with the local one (if any)
local pkg_name=${1}
local install_prefix=${2}
if echo ${MARLIN_DLL} | grep -qE "/${pkg_name}/"; then
local old_lib=$(echo ${MARLIN_DLL} | tr ":" "\n" | grep -E "/${pkg_name}/")
local lib_name=$(basename ${old_lib})
local new_lib=$(pwd)/${install_prefix}/lib/${lib_name}
export MARLIN_DLL=$(echo ${MARLIN_DLL%:} | tr ":" "\n" | grep -Ev "/${pkg_name}/" | tr "\n" ":")${new_lib}
echo "Replaced library on MARLIN_DLL: old: '${old_lib}'"
echo " new: '${new_lib}'"
fi
}
k4_local_repo() {
for arg in "$@"; do
case $arg in
-h|--help)
echo "Usage: k4_local_repo [install]"
echo " install : the directory where the software is installed (default: ./install)"
echo " -h : print this help message"
echo "Run the function from the directory where the repository is located."
return 0
;;
*)
;;
esac
done
if [ -n "$1" ]; then
install=$1
else
install=install
fi
current_repo=$(basename $PWD | tr '[:upper:]' '[:lower:]' | tr -d -)
export PATH=$(echo $PATH | tr ":" "\n" | grep -Ev "/${current_repo}/" | tr "\n" ":")
export LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | tr ":" "\n" | grep -Ev "/${current_repo}/" | tr "\n" ":")
export PYTHONPATH=$(echo $PYTHONPATH | tr ":" "\n" | grep -Ev "/${current_repo}/" | tr "\n" ":")
export CMAKE_PREFIX_PATH=$(echo $CMAKE_PREFIX_PATH | tr ":" "\n" | grep -Ev "/${current_repo}/" | tr "\n" ":")
export PKG_CONFIG_PATH=$(echo $PKG_CONFIG_PATH | tr ":" "\n" | grep -Ev "/${current_repo}/" | tr "\n" ":")
export ROOT_INCLUDE_PATH=$(echo $ROOT_INCLUDE_PATH | tr ":" "\n" | grep -Ev "/${current_repo}/" | tr "\n" ":")
_replace_marlin_dll ${current_repo} ${install}
export PATH=$PWD/$install/bin:$PATH
export LD_LIBRARY_PATH=$PWD/$install/lib:$PWD/$install/lib64:$LD_LIBRARY_PATH
# Get the python site-packages directory
libpythondir=$(python -c "import site; print('/'.join(site.getsitepackages()[0].split('/')[-3:]))")
export PYTHONPATH=$PWD/$install/python:$PWD/$install/$libpythondir:$PYTHONPATH
export PYTHONPATH=$PWD/$install/python:$PYTHONPATH
export CMAKE_PREFIX_PATH=$PWD/$install:$CMAKE_PREFIX_PATH
export PKG_CONFIG_PATH=$PWD/$install/lib/pkgconfig:$PKG_CONFIG_PATH
export ROOT_INCLUDE_PATH=$PWD/$install/include:$ROOT_INCLUDE_PATH
if [ "$current_repo" = "k4geo" ]; then
export K4GEO=$PWD
echo "Added K4GEO=$PWD"
fi
echo "Added $PWD/$install to the environment and removed any paths containing /${current_repo}/"
echo "Some variables may have to be updated manually to point to the local installation"
}
setup_script_path=$(/usr/bin/ls -t1 $k4path/key4hep-stack/*/setup.sh | head -1)
setup_actual=$(readlink -f $setup_script_path)
export key4hep_stack_version=$(echo "$setup_actual"| grep -Po '(?<=key4hep-stack/)(.*)(?=-[[:alnum:]]{6}/)')
# For SWAN
if [ -n "$LCG_VERSION" ]; then
echo "A LCG stack has been sourced, unsetting the following variables to avoid conflicts:"
echo "CMAKE_PREFIX_PATH CPPYY_BACKEND_LIBRARY LD_LIBRARY_PATH PKG_CONFIG_PATH PYTHONHOME PYTHONPATH"
unset CMAKE_PREFIX_PATH CPPYY_BACKEND_LIBRARY LD_LIBRARY_PATH PKG_CONFIG_PATH PYTHONHOME PYTHONPATH
fi
if [ "${rel}" = "latest" ]; then
echo "Setting up the latest Key4hep software stack from CVMFS"
echo "Note that you are using the latest stack, which may point to a newer stack in the future"
else
echo "Setting up the Key4hep software stack release ${rel} from CVMFS"
fi
echo "Use the following command to reproduce the current environment: "
echo ""
echo " source /cvmfs/sw.hsf.org/key4hep/setup.sh -r $(basename $(dirname $(dirname $(dirname $(dirname $setup_actual)))))"
echo ""
echo "If you have any issues, comments or requests, open an issue at https://github.com/key4hep/key4hep-spack/issues"
source ${setup_actual}