Newer
Older
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
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
# \\/ M anipulation | Copyright (C) 2016-2017 CINECA
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM.
#
# OpenFOAM is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
#
# Script
# foamCreateModuleInclude
#
# Description
# Script to create module settings.
# This is still incomplete, but can be a useful basis when using a module
# system.
#------------------------------------------------------------------------------
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<<USAGE
usage: ${0##*/} [OPTION] projectDir
options:
-output=file The output name (default: ModuleInclude.tcl)
-preload=file Specify (module) file to preload. Can use multiple times
-tmpdir=file The tmp directory to use.
-help Print the usage
Create module settings for inclusion in a top-level openfoam module.
USAGE
exit 1
}
# Report error and exit
die()
{
exec 1>&2
echo
echo "Error encountered:"
while [ "$#" -ge 1 ]; do echo " $1"; shift; done
echo
echo "See '${0##*/} -help' for usage"
echo
exit 1
}
#------------------------------------------------------------------------------
unset optBackend optDebug preload projectDir
unset moduleOutput moduleTmpDir
# Parse some options
while [ "$#" -gt 0 ]
do
case "$1" in
'')
# Ignore empty args
;;
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
usage
;;
-debug)
optDebug=true
;;
-preload=*)
preload="$preload${preload:+ }${1#*=}"
;;
--recursive-backend--)
optBackend=true
;;
--output=*)
moduleOutput="${1#*=}"
;;
--tmpdir=*)
moduleTmpDir="${1#*=}"
;;
-*)
die "unknown option: $1"
;;
*)
break
;;
esac
shift
done
[ "$#" -eq 1 ] || die "missing projectDir, or too many arguments"
projectDir="${1%/}"
#------------------------------------------------------------------------------
# Filter accepts system paths only
syspath() {
local path
set -- $(echo "${1}" | tr ':' '\n' )
for i in $@
do
case $i in
/bin | /usr/bin | /usr/lib*)
path="${path}${path:+:}$i";;
esac
done
echo "$path"
}
# Frontend: do all basic sanity checks in the front-end only
if [ -z "$optBackend" ]
then
# Check that it appears to be an OpenFOAM installation
[ -d "$projectDir" -a -f "$projectDir/etc/bashrc" ] || \
die "Incorrect projectDir? $projectDir"
# Check preloads
for file in $preloads
do
[ -f "$file" ] || echo "No such file to preload: $file" 1>&2
done
# Call itself with clean environment.
# Tag the start/end of the original PATH, MANPATH, LD_LIBRARY_PATH
exec env -i \
HOME=$HOME \
USER=$USER \
PATH=":MOD_PREPEND:$(syspath $PATH):MOD_APPEND:" \
MANPATH=":MOD_PREPEND:$(syspath $MANPATH):MOD_APPEND:" \
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
LD_LIBRARY_PATH=":MOD_PREPEND:$(syspath $LD_LIBRARY_PATH):MOD_APPEND:" \
$0 \
--recursive-backend-- \
"${optDebug:+-debug}" \
"${output:+-output=$moduleOutput}" \
"${preloads:+-preload=$preloads}" \
"${moduleTmpDir:+-tmpdir=$moduleTmpDir}" \
$projectDir
exitCode=$? # exec failed?
echo "exec somehow failed?" 1>&2
exit $exitCode
fi
#------------------------------------------------------------------------------
# Backend
: ${moduleOutput:=ModuleInclude.tcl}
: ${moduleTmpDir:=${TMPDIR:-/tmp}}
# Preload any/all modules
for file in $preloads
do
[ -f "$file" ] && . "$file" ''
done
# Temporary files
tmpFiles="$moduleTmpDir/modules-$USER.$$"
if [ -n "$optDebug" ]
then
echo "Preserving intermediate files: $tmpFiles.*" 1>&2
else
trap 'rm -f $tmpFiles.* 2>/dev/null; exit 0' EXIT TERM INT
fi
# Snapshot of aliases - sorted
printAlias()
{
alias | sort -f
}
# Snapshot of environment - without functions
# Sorted as non-OpenFOAM, WM_*, FOAM_*
printEnv()
{
# Non FOAM_*, WM_* settings
echo "# non-OpenFOAM"
env | sed -n -e '\@^FOAM_@d' -e '\@^WM_@d' -e '\@^[^ {}]@p' \
| sort -f
# WM_* settings
echo "# OpenFOAM"
env | sed -n -e '\@^WM_@p' \
| sort -f
# FOAM_* settings
echo "# OpenFOAM"
env | sed -n -e '\@^FOAM_@p' \
| sort -f
}
#
# Initial snapshot of the environment (without functions)
#
printEnv > $tmpFiles.env.pre.log
printAlias > $tmpFiles.alias.pre.log
# OpenFOAM settings
. $projectDir/etc/bashrc ''
echo "Using openfoam: $WM_PROJECT_DIR" 1>&2
echo "==> $moduleOutput" 1>&2
# Remove some cruft
unset FOAM_JOB_DIR FOAM_RUN FOAM_SETTINGS FOAM_INST_DIR
unset WM_PROJECT_USER_DIR WM_THIRD_PARTY_DIR
unset SCOTCH_VERSION
# Also remove user directories as being unreliable
foamOldDirs="$CEI_HOME $BOOST_ARCH_PATH $CGAL_ARCH_PATH $FFTW_ARCH_PATH $MPI_ARCH_PATH $SCOTCH_ARCH_PATH \
$FOAM_SITE_APPBIN $FOAM_SITE_LIBBIN $WM_PROJECT_SITE \
$FOAM_USER_APPBIN $FOAM_USER_LIBBIN"
foamClean=$WM_PROJECT_DIR/bin/foamCleanPath
if [ -x "$foamClean" ]
then
eval $($foamClean -sh-env=PATH "$foamOldDirs")
eval $($foamClean -sh-env=MANPATH "$foamOldDirs")
eval $($foamClean -sh-env=LD_LIBRARY_PATH "$foamOldDirs")
# May not have/need any third party at all
if [ -n "$FOAM_EXT_LIBBIN" -a ! -d "$FOAM_EXT_LIBBIN" ]
then
eval $($foamClean -sh-env=LD_LIBRARY_PATH "$FOAM_EXT_LIBBIN")
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
unset FOAM_EXT_LIBBIN
fi
fi
unset CEI_HOME
unset ENSIGHT9_INPUT ENSIGHT9_READER
unset ENSIGHT10_INPUT ENSIGHT10_READER
# Always consider site to be bogus. If needed, should be done elsewhere.
unset FOAM_SITE_APPBIN FOAM_SITE_LIBBIN WM_PROJECT_SITE
# Remove non-essential aliases
unalias wmDP 2>/dev/null
unalias wmInt32 2>/dev/null
unalias wmInt64 2>/dev/null
unalias wmSP 2>/dev/null
unalias wmSchedOff 2>/dev/null
unalias wmSchedOn 2>/dev/null
unalias wmSet 2>/dev/null
unalias wmUnset 2>/dev/null
unalias app 2>/dev/null
unalias foamSite 2>/dev/null
unalias lib 2>/dev/null
unalias run 2>/dev/null
unalias sol 2>/dev/null
unalias src 2>/dev/null
unalias tut 2>/dev/null
unalias util 2>/dev/null
#------------------------------------------------
# Generalize environment.
# This needs rethinking since it largely duplicates logic from the etc/config.sh/settings
rewriteEnv()
{
sed \
-e 's@^\(FOAM_USER_APPBIN=\).*@\1$env(HOME)/OpenFOAM/$env(USER)-$env(WM_PROJECT_VERSION)/platforms/$env(WM_OPTIONS)/bin@' \
-e 's@^\(FOAM_USER_LIBBIN=\).*@\1$env(HOME)/OpenFOAM/$env(USER)-$env(WM_PROJECT_VERSION)/platforms/$env(WM_OPTIONS)/lib@' \
;
}
#
# Updated snapshot of the environment (without functions)
#
printEnv | rewriteEnv > $tmpFiles.env.post.log
printAlias > $tmpFiles.alias.post.log
# Create a diff of the environment and aliases
diff $tmpFiles.env.pre.log $tmpFiles.env.post.log > $tmpFiles.env.diff.log
diff $tmpFiles.alias.pre.log $tmpFiles.alias.post.log > $tmpFiles.alias.diff.log
echo "# -------------------" > $moduleOutput
# --------------------------------------------------
# Environment other than PATH, MANPATH, LD_LIBRARY_PATH
echo "# Environment" >> $moduleOutput
grep '> ' $tmpFiles.env.diff.log | \
sed \
-e '/^> *PATH=/d' \
-e '/^> *MANPATH=/d' \
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
-e '/^> *LD_LIBRARY_PATH=/d' \
-e 's/^>/setenv /' \
-e 's/=/ "/' -e 's/$/\"/' >> $moduleOutput
# --------------------------------------------------
# Changes in PATH - without junk and system directories
echo "# PATH" >> $moduleOutput
sed -ne 's/^< *PATH=//p' $tmpFiles.env.diff.log | tr ':' '\n' > $tmpFiles.path.pre.log
sed -ne 's/^> *PATH=//p' $tmpFiles.env.diff.log | tr ':' '\n' > $tmpFiles.path.post.log
grep -vxFf $tmpFiles.path.pre.log $tmpFiles.path.post.log | \
sed \
-e '\@^/bin$@d' \
-e '\@^/usr/bin$@d' \
-e '\@^/usr/local/bin$@d' \
-e '\@^[.]$@d' \
-e '\@^$@d' \
> $tmpFiles.path.diff.log
sed \
-e 's/^/append-path PATH "/' \
-e 's/$/\"/' \
$tmpFiles.path.diff.log >> $moduleOutput
# --------------------------------------------------
# --------------------------------------------------
# Changes in MANPATH - without junk and system directories
echo "# MANPATH" >> $moduleOutput
sed -ne 's/^< *MANPATH=//p' $tmpFiles.env.diff.log | tr ':' '\n' > $tmpFiles.manpath.pre.log
sed -ne 's/^> *MANPATH=//p' $tmpFiles.env.diff.log | tr ':' '\n' > $tmpFiles.manpath.post.log
grep -vxFf $tmpFiles.manpath.pre.log $tmpFiles.manpath.post.log | \
sed \
-e '\@^/usr/share/@d' \
-e '\@^/usr/local/@d' \
-e '\@^/usr/lib@d' \
-e '\@^[.]$@d' \
-e '\@^$@d' \
> $tmpFiles.manpath.diff.log
sed \
-e 's/^/append-path MANPATH "/' \
-e 's/$/\"/' \
$tmpFiles.manpath.diff.log >> $moduleOutput
# --------------------------------------------------
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# --------------------------------------------------
# Changes in LD_LIBRARY_PATH - without junk and system directories
echo "# LD_LIBRARY_PATH" >> $moduleOutput
sed -ne 's/^< *LD_LIBRARY_PATH=//p' $tmpFiles.env.diff.log | tr ':' '\n' > $tmpFiles.ldpath.pre.log
sed -ne 's/^> *LD_LIBRARY_PATH=//p' $tmpFiles.env.diff.log | tr ':' '\n' > $tmpFiles.ldpath.post.log
grep -vxFf $tmpFiles.ldpath.pre.log $tmpFiles.ldpath.post.log | \
sed \
-e '\@^/lib.*$@d' \
-e '\@^/usr/lib.*$@d' \
-e '\@^/usr/local/lib.*$@d' \
-e '\@^[.]$@d' \
-e '\@^$@d' \
> $tmpFiles.ldpath.diff.log
sed \
-e 's/^/append-path LD_LIBRARY_PATH "/' \
-e 's/$/\"/' \
$tmpFiles.ldpath.diff.log >> $moduleOutput
# -------------------
# aliases
# Some diff give "> alias". Others give ">", needed extended regular expressions '-r'
echo "# aliases" >> $moduleOutput
cat $tmpFiles.alias.diff.log | \
sed -r -n -e 's/^> (alias)?/set-alias /p' | \
sed -e "s/='/ \"/" -e "s/'/\"/g" >> $moduleOutput
echo "# -------------------" >> $moduleOutput
# -----------------------------------------------------------------------------