File: foamCreateModuleInclude

package info (click to toggle)
openfoam 1912.200626-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 238,956 kB
  • sloc: cpp: 1,159,641; sh: 15,902; ansic: 5,195; lex: 660; xml: 387; python: 282; awk: 212; makefile: 103; sed: 88; csh: 3
file content (396 lines) | stat: -rwxr-xr-x 10,907 bytes parent folder | download | duplicates (2)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
387
388
389
390
391
392
393
394
395
396
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2016-2017 CINECA
#     Copyright (C) 2017-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# 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)
  -prefs=file       A preferences file (OpenFOAM) to load.
  -preload=file     Specify a shell 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 optPrefs preloads projectDir
unset moduleOutput moduleTmpDir

# Parse some options
while [ "$#" -gt 0 ]
do
    case "$1" in
    '')
        # Ignore empty args
        ;;
    -h | -help*)
        usage
        ;;
    -debug)
        optDebug=true
        ;;
    -prefs=*)
        optPrefs="${1#*=}"
        ;;
    -preload=*)
        preloads="$preloads${preloads:+ }${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 preloads
    if [ -n "$preloads" ]
    then
        for file in $preloads
        do
            [ -f "$file" ] || echo "No such file to preload: $file" 1>&2
        done
    fi

    # Check that it appears to be an OpenFOAM installation
    # could also check [ -d "$projectDir/META-INFO" ]
    if [ -d "$projectDir" ] && [ -f "etc/bashrc" ]
    then
        echo "Appears to be an OpenFOAM installation" 1>&2
    else
        die "Incorrect OpenFOAM projectDir?" \
        "    $projectDir"
    fi

    # 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:" \
        LD_LIBRARY_PATH=":MOD_PREPEND:$(syspath $LD_LIBRARY_PATH):MOD_APPEND:" \
        $0 \
        --recursive-backend-- \
        "${optDebug:+-debug}" \
        "${optPrefs:+-prefs=$optPrefs}" \
        "${moduleOutput:+-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 "$optPrefs"

echo "Using openfoam: $WM_PROJECT_DIR" 1>&2
echo "==> $moduleOutput" 1>&2

# Remove some cruft
unset FOAM_JOB_DIR FOAM_RUN FOAM_SETTINGS FOAM_CONTROLDICT
unset FOAM_INST_DIR WM_PROJECT_INST_DIR
unset WM_PROJECT_USER_DIR WM_THIRD_PARTY_DIR
unset SCOTCH_VERSION

# Probably don't want these either
unset FOAM_CONFIG_MODE


# Also remove user directories as being unreliable

foamOldDirs="$BOOST_ARCH_PATH $CGAL_ARCH_PATH $FFTW_ARCH_PATH \
$MESA_ARCH_PATH $LLVM_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")
        unset FOAM_EXT_LIBBIN
    fi
fi

# 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 wmSPDP      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.
# Needs rethinking, it largely duplicates logic from 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' \
    -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
# --------------------------------------------------

# --------------------------------------------------
# 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

# -----------------------------------------------------------------------------