CONFIG: support openfoam -init=FILE

- useful for supporting a variety of different predefined
  configuration files. Bypasses the regular etc/bashrc entirely.

- minor refactoring in foamCreateModuleInclude
This commit is contained in:
Mark Olesen 2020-06-23 14:11:26 +02:00
parent 31bbca3c38
commit cbcc87549e
2 changed files with 114 additions and 41 deletions

View File

@ -35,6 +35,7 @@ options:
-paraview Retain paraview elements -paraview Retain paraview elements
-sh | -tcl Output flavour (default: -tcl) -sh | -tcl Output flavour (default: -tcl)
-debug Retain intermediate files for debugging purposes -debug Retain intermediate files for debugging purposes
-reduce=NUM Environment reduction level (experimental)
-help Print the usage -help Print the usage
Create module settings for inclusion in a top-level openfoam module. Create module settings for inclusion in a top-level openfoam module.
@ -58,7 +59,7 @@ die()
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
unset optBackend optDebug optAliases optPrefs optFlavour optParaview unset optBackend optDebug optAliases optPrefs optFlavour optReduce optParaview
unset preloads projectDir unset preloads projectDir
unset moduleOutput moduleTmpDir unset moduleOutput moduleTmpDir
@ -81,6 +82,9 @@ do
-paraview) -paraview)
optParaview=true optParaview=true
;; ;;
-reduce=[0-9]*)
optReduce="${1#*=}"
;;
-prefs=*) -prefs=*)
optPrefs="${1#*=}" optPrefs="${1#*=}"
;; ;;
@ -96,12 +100,12 @@ do
--recursive-backend--) --recursive-backend--)
optBackend=true optBackend=true
;; ;;
-tcl)
optFlavour="tcl"
;;
-sh) -sh)
optFlavour="sh" optFlavour="sh"
;; ;;
-tcl)
optFlavour="tcl"
;;
-*) -*)
die "unknown option: $1" die "unknown option: $1"
;; ;;
@ -169,6 +173,7 @@ then
"${optDebug:+-debug}" \ "${optDebug:+-debug}" \
"${optAliases:+-aliases}" \ "${optAliases:+-aliases}" \
"${optParaview:+-paraview}" \ "${optParaview:+-paraview}" \
"${optReduce:+-reduce=$optReduce}" \
"${optPrefs:+-prefs=$optPrefs}" \ "${optPrefs:+-prefs=$optPrefs}" \
"${optFlavour:+-$optFlavour}" \ "${optFlavour:+-$optFlavour}" \
"${moduleOutput:+-output=$moduleOutput}" \ "${moduleOutput:+-output=$moduleOutput}" \
@ -365,6 +370,22 @@ unset FOAM_APP FOAM_SRC FOAM_SOLVERS FOAM_UTILITIES
unset SCOTCH_VERSION unset SCOTCH_VERSION
# More optimization (environment reduction).
# Values are currently arbitrary
: "${optReduce:=0}"
if [ "$optReduce" -gt 0 ]
then
unset FOAM_USER_APPBIN FOAM_USER_LIBBIN
fi
if [ "$optReduce" -gt 1 ]
then
unset FOAM_TUTORIALS
unset FOAM_EXT_LIBBIN
unset FOAM_APPBIN FOAM_LIBBIN
fi
# Remove non-essential aliases # Remove non-essential aliases
unalias wmDP 2>/dev/null unalias wmDP 2>/dev/null
@ -411,29 +432,30 @@ diff "$tmpFiles".alias.pre.log "$tmpFiles".alias.post.log > "$tmpFiles".alias.d
# -------------------------------------------------- # --------------------------------------------------
# Output functions # Output functions
if [ "$foamCreateModule_flavour" = "sh" ] case "$foamCreateModule_flavour" in
then ("sh")
# Shell # Shell
subst_USER_PREFIX='${HOME}/OpenFOAM/${USER}-${WM_PROJECT_VERSION}/platforms/${WM_OPTIONS}' subst_USER_PREFIX='${HOME}/OpenFOAM/${USER}-${WM_PROJECT_VERSION}/platforms/${WM_OPTIONS}'
# Output 'NAME=VAL' as 'export NAME="VAL"' but prune 'NAME='
format_export() { format_export() {
sed -e 's/^\([^=]*\)=\(.*\)$/export \1="\2"/' ; sed -e '/=$/d; s/^\([^=]*\)=\(.*\)$/export \1="\2"/' ;
} }
prepend_path() { prepend_path() {
local envname="$1" local envname="$1"
if [ -n "$envname" ] if [ -z "$envname" ]
then then
echo "Warning: no envname specified" 1>&2
sed -e 'd'
else
sed \ sed \
-e '/""/d' \ -e '/""/d' \
-e '/^$/d' \ -e '/^$/d' \
-e 's/^/export '"$envname"'=/' \ -e 's/^/export '"$envname"'=/' \
-e 's/"$/${'"$envname"':+:}${'"$envname"'}"/' \ -e 's/"$/${'"$envname"':+:}${'"$envname"'}"/' \
; ;
else
echo "Warning: no envname specified" 1>&2
sed -e 'd'
fi fi
} }
@ -476,28 +498,31 @@ then
sed -r -n -e 's/^> (alias)?/alias/p' sed -r -n -e 's/^> (alias)?/alias/p'
} }
else ;;
(*)
# Tcl # Tcl
subst_USER_PREFIX='$env(HOME)/OpenFOAM/$env(USER)-$env(WM_PROJECT_VERSION)/platforms/$env(WM_OPTIONS)' subst_USER_PREFIX='$env(HOME)/OpenFOAM/$env(USER)-$env(WM_PROJECT_VERSION)/platforms/$env(WM_OPTIONS)'
# Output 'NAME=VAL' as 'setenv NAME "VAL"' but prune 'NAME='
format_export() { format_export() {
sed -e 's/^\([^=]*\)=\(.*\)$/setenv \1 "\2"/' ; sed -e '/=$/d; s/^\([^=]*\)=\(.*\)$/setenv \1 "\2"/' ;
} }
prepend_path() { prepend_path() {
local envname="$1" local envname="$1"
if [ -n "$envname" ] if [ -z "$envname" ]
then then
echo "Warning: no envname specified" 1>&2
sed -e 'd'
else
sed \ sed \
-e '/""/d' \ -e '/""/d' \
-e '/^$/d' \ -e '/^$/d' \
-e 's/^/prepend-path '"$envname"' /' \ -e 's/^/prepend-path '"$envname"' /' \
; ;
else
echo "Warning: no envname specified" 1>&2
sed -e 'd'
fi fi
} }
@ -505,6 +530,23 @@ else
unset sedFilter2a sedFilter2b unset sedFilter2a sedFilter2b
unset sedFilter3a sedFilter3b unset sedFilter3a sedFilter3b
if [ -n "$WM_OPTIONS" ]
then
sedFilter1='s@/'"$WM_OPTIONS"'/@/$env(WM_OPTIONS)/@g'
fi
if [ -n "$WM_PROJECT_DIR" ]
then
sedFilter2a='s@^'"$WM_PROJECT_DIR"'/\(platforms\|bin\|wmake\|tutorials\)@$env(WM_PROJECT_DIR)/\1@;'
sedFilter2b='s@='"$WM_PROJECT_DIR"'/\(platforms\|bin\|wmake\|tutorials\)@=$env(WM_PROJECT_DIR)/\1@;'
fi
if [ -n "$WM_THIRD_PARTY_DIR" ]
then
sedFilter3a='s@^'"$WM_THIRD_PARTY_DIR"'/\(platforms\|bin\|wmake\|tutorials\)@$env(WM_THIRD_PARTY_DIR)/\1@;'
sedFilter3b='s@='"$WM_THIRD_PARTY_DIR"'/\(platforms\|bin\|wmake\|tutorials\)@=$env(WM_THIRD_PARTY_DIR)/\1@;'
fi
# Generalize environment. # Generalize environment.
# Needs rethinking, duplicates logic from etc/config.sh/settings # Needs rethinking, duplicates logic from etc/config.sh/settings
rewrite_env() { rewrite_env() {
@ -523,10 +565,14 @@ else
sed -r -n -e 's/^> (alias)?/set-alias /p' | \ sed -r -n -e 's/^> (alias)?/set-alias /p' | \
sed -e "s/='/ \"/" -e "s/'$/\"/" sed -e "s/='/ \"/" -e "s/'$/\"/"
} }
fi
;;
esac
# -------------------------------------------------- # --------------------------------------------------
# Preamble
echo "# --------------------" >| "$moduleOutput" echo "# --------------------" >| "$moduleOutput"
echo "# OpenFOAM environment" >> "$moduleOutput" echo "# OpenFOAM environment" >> "$moduleOutput"
@ -540,21 +586,33 @@ echo "# --------------------" >> "$moduleOutput"
echo >> "$moduleOutput" echo >> "$moduleOutput"
# Known project values first # Project directory
for envname in \ echo "WM_PROJECT_DIR=$WM_PROJECT_DIR" | format_export >> "$moduleOutput"
WM_PROJECT_DIR \
WM_THIRD_PARTY_DIR \ # ThirdParty directory. May be relative to the project directory
WM_PROJECT \ absDir="$WM_THIRD_PARTY_DIR"
WM_PROJECT_VERSION \ relDir="${absDir#${WM_PROJECT_DIR}/}"
WM_OPTIONS \ if [ "$relDir" != "$absDir" ]
; then
do # Ugly but workable
value="$(eval echo \$$envname)" case "$foamCreateModule_flavour" in
if [ -n "$value" ] && [ "$value" != '$' ] ("sh")
then echo "WM_THIRD_PARTY_DIR=\${WM_PROJECT_DIR}/$relDir"
echo "$envname=$value" | format_export >> "$moduleOutput" ;;
fi (*)
done # Tcl
echo "WM_THIRD_PARTY_DIR=\$env(WM_PROJECT_DIR)/$relDir"
;;
esac
else
echo "WM_THIRD_PARTY_DIR=$absDir"
fi | format_export >> "$moduleOutput"
echo "WM_PROJECT=$WM_PROJECT" | format_export >> "$moduleOutput"
echo "WM_PROJECT_VERSION=$WM_PROJECT_VERSION" | format_export >> "$moduleOutput"
echo "WM_OPTIONS=$WM_OPTIONS" | format_export >> "$moduleOutput"
echo >> "$moduleOutput" echo >> "$moduleOutput"
# Environment other than PATH, MANPATH, LD_LIBRARY_PATH # Environment other than PATH, MANPATH, LD_LIBRARY_PATH
@ -645,8 +703,9 @@ sed \
cat "$tmpFiles".ldpath.diff.log | prepend_path LD_LIBRARY_PATH >> "$moduleOutput" cat "$tmpFiles".ldpath.diff.log | prepend_path LD_LIBRARY_PATH >> "$moduleOutput"
# ------------------- # -------------------
# Aliases # Aliases, Finalize
# Some diff give "> alias". Others give ">", needed extended regular expressions '-r' # Some diff give "> alias". Others give ">", needed extended regular expressions '-r'
echo >> "$moduleOutput" echo >> "$moduleOutput"

View File

@ -60,6 +60,7 @@ options:
-show-patch Print META-INFO patch value and exit -show-patch Print META-INFO patch value and exit
-show-prefix Print project directory and exit -show-prefix Print project directory and exit
-test-tutorial Forward arguments to tutorials/AutoTest -test-tutorial Forward arguments to tutorials/AutoTest
-init=FILE Alternative initialization file (expert option)
-verbose Set FOAM_VERBOSE=true (interactive only) -verbose Set FOAM_VERBOSE=true (interactive only)
-help Print the usage -help Print the usage
@ -102,7 +103,7 @@ getApiInfo()
# - No inheritance of FOAM_SETTINGS # - No inheritance of FOAM_SETTINGS
# - No default verbosity (only as command-line option) # - No default verbosity (only as command-line option)
unset FOAM_CONFIG_ETC FOAM_SETTINGS FOAM_VERBOSE unset FOAM_CONFIG_ETC FOAM_SETTINGS FOAM_VERBOSE
unset _foamEtcDir _foamSettings _foamScriptCommand unset _foamEtcDir _foamSettings _foamScriptCommand _foamSourceBashEnv
unset optTestTut unset optTestTut
# Parse options # Parse options
@ -147,7 +148,7 @@ do
fi fi
;; ;;
-sp | -dp | -spdp ) -sp | -dp | -spdp)
# WM_PRECISION_OPTION=(SP|DP|SPDP) # WM_PRECISION_OPTION=(SP|DP|SPDP)
setting=$(echo "${1#-}" | sed -e 's/-//g;y/sdp/SDP/') setting=$(echo "${1#-}" | sed -e 's/-//g;y/sdp/SDP/')
_foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=$setting" _foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=$setting"
@ -167,6 +168,10 @@ do
projectDir="${1#*=}" projectDir="${1#*=}"
;; ;;
-init=*)
_foamSourceBashEnv="${1#*=}"
;;
-verbose) -verbose)
export FOAM_VERBOSE=true export FOAM_VERBOSE=true
;; ;;
@ -209,18 +214,27 @@ then
. "$WM_PROJECT_DIR/etc/config.sh/unset" || true . "$WM_PROJECT_DIR/etc/config.sh/unset" || true
fi fi
unset interactive unset interactive
if [ "$#" -eq 0 ] && [ -z "$_foamScriptCommand" ] && [ -z "$optTestTut" ] if [ "$#" -eq 0 ] && [ -z "$_foamScriptCommand" ] && [ -z "$optTestTut" ]
then then
# Interactive shell, chain off via a file
interactive=true interactive=true
_foamSourceBashEnv="$projectDir/bin/tools/source-bashrc"
else
# Non-interactive shell, use the OPENFOAM etc/bashrc
_foamSourceBashEnv="$projectDir/etc/bashrc"
fi fi
if [ -z "$_foamSourceBashEnv" ]
then
if [ -n "$interactive" ]
then
# Interactive shell, chain off via a file
_foamSourceBashEnv="$projectDir/bin/tools/source-bashrc"
else
# Default: OPENFOAM etc/bashrc
_foamSourceBashEnv="$projectDir/etc/bashrc"
fi
fi
[ -f "$_foamSourceBashEnv" ] || { [ -f "$_foamSourceBashEnv" ] || {
echo "Error: file not found: $_foamSourceBashEnv" 1>&2 echo "Error: file not found: $_foamSourceBashEnv" 1>&2
exit 2 exit 2