-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbitsenv
More file actions
executable file
·313 lines (272 loc) · 7.39 KB
/
bitsenv
File metadata and controls
executable file
·313 lines (272 loc) · 7.39 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
function Help() {
cat<<EOF
Usage: bitsenv [-m <modules directory>] [-p <platform>] [-h]
<checkenv|printenv|enter> <package/version>[,<package/version>...]
setenv> <package/version>[,<package/version>...] -c <command>
<query|q>
EOF
exit 0
}
version=3.2.10
prog=`readlink $0 2>/dev/null`
[[ -z $prog ]] && prog=$0
path=`dirname $prog`
path=`cd "$path";pwd`
[ x$BITSENV_DEBUG == x1 ] && printf "path=$path\nprog=$prog\n" >&2
path=$(cd $(dirname $0) && pwd) # appended to PATH later on
printvar=""
# If using modulecmd from the system, and ver >= 4, this var enables 3.x compat mode
export MODULES_USE_COMPAT_VERSION=1
Eval(){
cmd=$*
ret=`$cmd` || return 1
[ -z "$ret" ] && return 0
eval $ret
}
[ -f .bitsenv ] && source .bitsenv && [ x$BITSENV_DEBUG == x1 ] && printf "found .bitsenv"
argc=$#
argv=("$@")
for (( j=0; j<argc; j++ ))
do
case "${argv[j]}" in
-m|-?modules|-?mdir|-?moduledir)
moduledir="${argv[j+1]}"
;;
-p|-?platform)
platform="${argv[j+1]}"
;;
-h)
Help
;;
*)
;;
esac
done
if [ -z $platform ]
then
if [[ "$BITS_PLATFORM" != "" ]]
then
printf "WARNING: overriding detected platform ($platform) with $BITS_PLATFORM\n" >&2;
platform=$BITS_PLATFORM;
else
platform=$(bits architecture)
fi
fi
if [ -z $moduledir ]
then
if [[ "$BITS_MODULEDIR" != "" ]]; then
moduledir="$BITS_MODULEDIR"
elif [[ `basename $prog` == bitsenv && `basename $path` == bin ]]; then
moduledir=`dirname "$path"`
fi
fi
if [ -z $moduledir ]
then
bits=`which bits`
if [ ! -z $bits ]
then
exec $bits $@
fi
printf "Could not determine module directory, please set BITS_MODULEDIR\n"
exit 1
fi
if [ -w . -a ! -f bits.rc -a ! -f .bitsrc ]
then
printf "moduledir=$moduledir\nplatform=$platform\n" > .bitsenv
fi
modules=$moduledir/$platform/Modules
if [ ! -d $modules ]
then
echo "$modules directory not found"
exit 1
fi
unset MODULESHOME
function modulepath() {
local dir
local colon
local subdir
subdir=$1; shift 1
for dir in $MODULEPATH $*
do
if [ -d $dir/Modules/$subdir ]
then
printf "${colon}${dir}/Modules/${subdir}"
colon=":"
fi
done
printf "\n"
}
function test_toolchain() {
local TMPPREF=/tmp/bitsenv_helloworld
cat > $TMPPREF.cpp <<EOF
#include <iostream>
int main(int argn, char *argv[]) {
std::cout << "hello world" << std::endl;
return 0;
}
EOF
g++ -o $TMPPREF ${TMPPREF}.cpp > ${TMPPREF}.log 2>&1
if [[ `/tmp/bitsenv_helloworld 2> /dev/null` != "hello world" ]]; then
echo "WARNING: We are using GNU C++ compiler at $(which g++ 2> /dev/null)" >&2
echo "WARNING: This compiler is unable to produce valid executables on this platform!" >&2
echo "WARNING: Error from g++ follows:" >&2
while IFS= read LINE; do
echo "WARNING: $LINE" >&2
done < <(cat ${TMPPREF}.log)
else
echo "NOTICE: loaded compiler ($(which g++)) seems to produce valid executables" >&2
fi
rm -f ${TMPPREF}*
}
# Transform a comma-separated packages to modulecmd
# format, i.e.:
#
# Returned list (on stdout) is also sorted: packages with a certain priority
# are moved at the beginning of the list.
function normalize_sort_packages() {
NORM=( $(echo $1 | sed -e 's%::%/%g' -e 's%,% %g') )
[[ $BITSENV_DEBUG == 1 ]] && printf "NOTICE: list of packages normalized to ${NORM[*]}\n" >&2
echo ${NORM[*]}
}
export PATH=$PATH:$path
if [ -d $modules/$version/$distro_dir/$distro_release ]
then
moduleenv="env LD_LIBRARY_PATH=$modules/$version/$distro_dir/$distro_release/lib"
modulecmd="$modules/$version/$distro_dir/$distro_release/bin/modulecmd"
else
moduleenv="env LD_LIBRARY_PATH=$modules/$version/$distro_dir/$distro_xrelease/lib"
modulecmd="$modules/$version/$distro_dir/$distro_xrelease/bin/modulecmd"
fi
if [[ ! -f $modulecmd ]]; then
# Fallback on system-installed
[[ $BITSENV_DEBUG == 1 ]] && printf "NOTICE: using modulecmd from the system\n" >&2
modulecmd=modulecmd
moduleenv=
fi
[[ $BITSENV_DEBUG == 1 ]] && printf "modulecmd=$modulecmd\nmoduleenv=$moduleenv\n" >&2
T=`mktemp`
$moduleenv $modulecmd &> $T
if [[ $? == 127 ]]; then
echo "Unknown distribution release: $distro_name $distro_release"
[[ $BITSENV_DEBUG == 1 ]] && printf "ERROR: full error message is: `cat $T`\n" >&2
rm -f $T
exit 1
fi
rm -f $T
unset T
tclsh <<EOF >/dev/null 2>&1
EOF
[[ $? == 0 ]] && moduleenv=
command=""
# We cannot cross-pick pacakages among different platforms but we have to
# pick all packages consistently from a certain platform tree. When listing
# packages we show them all, when we load a package e define a priority list and
# we always have a fallback for backward compatibility.
ARGS=("$@")
PACKAGES=
EXPECT_PACKAGES=
for ARG in "$@"; do
if [[ $EXPECT_PACKAGES == 1 ]]; then
PACKAGES=$(normalize_sort_packages "$ARG")
break
elif [[ "$ARG" == enter || "$ARG" == printenv || "$ARG" == setenv || "$ARG" == checkenv ]]; then
EXPECT_PACKAGES=1
else
EXPECT_PACKAGES=
fi
done
export MODULEPATH="$moduledir/etc/toolchain/modulefiles/$platform:$moduledir/$platform/Modules/modulefiles"
[ x$BITSENV_DEBUG == x1 ] && printf "MODULEPATH=$MODULEPATH\n" >&2
COMMAND_IN_ENV=()
while [ $# -gt 0 ]
do
case $1 in
enter)
shift 1
args=$(normalize_sort_packages "$1")
before=`printenv`
Eval $moduleenv $modulecmd bash load $args 2>/dev/null || exit 1
after=`printenv | grep -v LS_COLORS=`
_LM_ENV=""
for var in $after
do
if [[ ! "$before" =~ "$var" ]]
then
_LM_ENV="$var;$_LM_ENV"
fi
done
export _LM_ENV
exec env PS1="[$args] \W > " bash --norc -i
;;
setenv)
shift 1
args=$(normalize_sort_packages "$1")
Eval $moduleenv $modulecmd bash load $args 2>/dev/null || exit 1
shift 1
;;
checkenv)
shift 1
args=$(normalize_sort_packages "$1")
Eval $moduleenv $modulecmd bash load $args || exit 1
PREV_PKG=
PREV_VER=
PKG_ERR=
while read LMF; do
VER=${LMF##*/}
PKG=${LMF%/*}
PKG=${PKG##*/}
if [[ $PKG == $PREV_PKG && $VER != $PREV_VER ]]; then
printf "ERROR: attempting to load $PKG $VER when conflicting version $PREV_VER already loaded\n" >&2
PKG_ERR=1
fi
PREV_PKG=$PKG
PREV_VER=$VER
done < <(echo $_LMFILES_ | sed -e 's/:/\n/g' | sort)
[[ $PKG_ERR ]] && exit 1
[[ $BITSENV_DEBUG == 1 ]] && printf "NOTICE: all packages loaded successfully\n" >&2
exit 0
;;
printenv)
shift 1
if [ x$1 = x ]
then
echo $_LM_ENV
fi
args=$(normalize_sort_packages "$1")
$moduleenv $modulecmd bash load $args 2>/dev/null
exit
;;
-m|-?modules|-?mdir|-?moduledir)
shift 2
;;
-p|--platform)
shift 2
;;
-print)
shift 1
var=$1
echo ${!var}
shift 1
;;
-a|--archive)
shift 1
export MODULEPATH=$MODULEPATH:$(modulepath archive $moduledirs)
;;
-c)
shift; COMMAND_IN_ENV=("$@");
exec "${COMMAND_IN_ENV[@]}"
;;
-h)
Help
;;
q|query)
$moduleenv $modulecmd bash -t avail 2>&1 | grep -v -e : -e "^$"
exit $?
;;
*)
$moduleenv $modulecmd bash $*
exit
;;
esac
done