openfoam/bin/tools/foamCreateModuleInclude
Mark Olesen fe17c8ad5f CONFIG: improve prefix matching for system libraries (#1607)
- missed detection of system libraries when installed with multiarch
  paths like /usr/lib/x86_64-linux-gnu

CONFIG: improve handling of group/user config files (#928)

- changed bashrc handling of FOAM_CONFIG_NOUSER to use
  FOAM_CONFIG_MODE instead. Propagate into foamEtcFile to make this
  a stickier control.

  This change allows better control, but also enables cluster
  installations to define their own value within the OpenFOAM prefs.sh
  file to prevent users accidentally mis-configuring things if
  necessary.

- remove undocumented handling of an (a)ll mode in foamEtcFile to
  avoid potential pitfalls.

- add support for FOAM_CONFIG_ETC handling.
  This allows injection of an extra search layer when finding
  project etc files

ENH: improvements to foamConfigurePaths (#928)

- handle FOAM_CONFIG_ETC implicitly, or explicitly with the new
  -etc option.

STYLE: more explicit wording in foamConfigurePaths usage (#1602)

- document that an absolute path (eg, -scotch-path) overrides/ignores
  the equivalent ThirdParty setting (eg, -scotch)

- longer options -system-compiler and -third-compiler for -system
  and -third, respectively. Clearer as to their purpose.

- adjust the location sanity check to look for META-INFO directory.
2020-03-16 12:03:58 +01:00

397 lines
11 KiB
Bash
Executable File

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