From ba3a31af956e6cec33881e44f6842e518aca5cca Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 12 Feb 2020 17:09:43 +0100 Subject: [PATCH] ENH: openfoam shell session - improved and relocated - '-c' option (as per shell), '-Dkey[=value]' option to provide preferences via the command-line. For example, etc/openfoam -DWM_COMPILER=Clang -int64 ./Allwmake -j -s -l These can also be combined with other options. Eg, etc/openfoam -DWM_COMPILER=Clang \ -c 'wmake -show-path-cxx -show-cxxflags' - relocated from bin/tools/ => etc/ for easier access - bin/tools/openfoam.in : for autoconfig-style installation - Auto-detect if the shell script was executed with openfoam and interpret accordingly. Simple example, -------------- #!/usr/bin/openfoam cd "${0%/*}" || exit # Run -*-sh-*- from this dir blockMesh simpleFoam -------------- Note it is NOT currently possible to provide any other parameters this way. Eg, `#!/usr/bin/openfoam -sp` (NOT) This will either fail to run, or result in infinite recursion. --- bin/tools/openfoam | 193 ---------------------------- bin/tools/openfoam.in | 26 ++++ bin/tools/source-bashrc | 22 ++-- doc/openfoam.1.in | 70 +++++++++++ etc/bashrc | 14 ++- etc/config.sh/setup | 4 +- etc/cshrc | 9 +- etc/openfoam | 272 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 398 insertions(+), 212 deletions(-) delete mode 100755 bin/tools/openfoam create mode 100644 bin/tools/openfoam.in create mode 100644 doc/openfoam.1.in create mode 100755 etc/openfoam diff --git a/bin/tools/openfoam b/bin/tools/openfoam deleted file mode 100755 index bae80e12af..0000000000 --- a/bin/tools/openfoam +++ /dev/null @@ -1,193 +0,0 @@ -#!/bin/bash -#------------------------------------------------------------------------------ -# ========= | -# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox -# \\ / O peration | -# \\ / A nd | www.openfoam.com -# \\/ M anipulation | -#------------------------------------------------------------------------------ -# Copyright (C) 2019 OpenCFD Ltd. -#------------------------------------------------------------------------------ -# License -# This file is part of OpenFOAM, licensed under GNU General Public License -# . -# -# Script -# openfoam [args] -# -# Description -# Open an interactive bash session with an OpenFOAM environment, -# or run an OpenFOAM application (with arguments) after first sourcing -# the OpenFOAM etc/bashrc file from the project directory. -# -# This script normally exists in $WM_PROJECT_DIR/bin/tools but can also -# be modified to use a hard-coded PROJECT_DIR entry and placed elsewhere -# in the filesystem (eg, /usr/bin). -# -#------------------------------------------------------------------------------ -# Hard-coded value (eg, with autoconfig) -projectDir="@PROJECT_DIR@" - -if [ -z "$projectDir" ] || [ "${projectDir#@}" != "$projectDir" ] -then - # Auto-detect from location - toolsDir="${0%/*}" # The bin/tools dir - projectDir="${toolsDir%/bin/tools}" # Project dir - - case "$projectDir" in - (/bin | /usr/bin | /usr/local/bin) - # This shouldn't happen. - # If copied to a system dir, should also be using hard-coded values! - echo "Warning: suspicious looking project dir: $projectDir" 1>&2 - ;; - - ("$toolsDir") - # Eg, called as ./openfoam etc - need to try harder - projectDir="$(\cd $(dirname $0)/../.. && \pwd -L)" || unset projectDir - ;; - esac -fi - -#------------------------------------------------------------------------------ -usage() { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done - cat<&2 - exit 1 - ;; - *) - break - ;; - esac - shift -done - -#------------------------------------------------------------------------------- - -# Remove current OpenFOAM environment -if [ -d "$WM_PROJECT_DIR" ] && [ -f "$WM_PROJECT_DIR/etc/config.sh/unset" ] -then - . "$WM_PROJECT_DIR/etc/config.sh/unset" -fi - -[ -d "$projectDir" ] || { - echo "Error: no project dir: $projectDir" 1>&2 - exit 2 -} - -_foamSourceBashEnv="$projectDir/etc/bashrc" - -if [ "$#" -eq 0 ] -then - # Interactive shell - _foamSourceBashEnv="$projectDir/bin/tools/source-bashrc" -fi - -[ -f "$_foamSourceBashEnv" ] || { - echo "Error: file not found: $_foamSourceBashEnv" 1>&2 - exit 2 -} - -if [ "$#" -eq 0 ] -then - # Source user ~/.bashrc and OpenFOAM etc/bashrc. - # 1) Can either use a tmp file, or 2) chain off to a dedicated file - # We use a dedicated file. - - if [ -n "$_foamSettings" ] - then - export FOAM_SETTINGS="$_foamSettings" - fi - - ## echo "Source with $_foamSourceBashEnv with '$FOAM_SETTINGS'" 1>&2 - - # Interactive shell (newer bash can use --init-file instead of --rcfile) - exec bash --rcfile "$_foamSourceBashEnv" -i - -else - # Non-interactive - - # Source bashrc within a function to preserve command-line arguments - # - this will not have aliases, but working non-interactively anyhow - sourceBashrc() - { - . "$_foamSourceBashEnv" $_foamSettings - } - - sourceBashrc - exec "$@" -fi - -#------------------------------------------------------------------------------ diff --git a/bin/tools/openfoam.in b/bin/tools/openfoam.in new file mode 100644 index 0000000000..39b1fe76fb --- /dev/null +++ b/bin/tools/openfoam.in @@ -0,0 +1,26 @@ +#!/bin/sh +#------------------------------------------------------------------------------ +# ========= | +# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox +# \\ / O peration | +# \\ / A nd | www.openfoam.com +# \\/ M anipulation | +#------------------------------------------------------------------------------ +# Copyright (C) 2020 OpenCFD Ltd. +#------------------------------------------------------------------------------ +# License +# This file is part of OpenFOAM, distributed under GPL-3.0-or-later. +# +# Script +# openfoam [options] [args] +# +# Description +# Forwarding to the OpenFOAM etc/openfoam bash session script. +# +#------------------------------------------------------------------------------ +# Hard-coded directory path (eg, autoconfig) +projectDir="@PROJECT_DIR@" + +exec "$projectDir"/etc/openfoam "$@" + +#------------------------------------------------------------------------------ diff --git a/bin/tools/source-bashrc b/bin/tools/source-bashrc index 13f494785a..e4fec4062b 100644 --- a/bin/tools/source-bashrc +++ b/bin/tools/source-bashrc @@ -5,34 +5,36 @@ # \\ / A nd | www.openfoam.com # \\/ M anipulation | #------------------------------------------------------------------------------ -# Copyright (C) 2019 OpenCFD Ltd. +# Copyright (C) 2019-2020 OpenCFD Ltd. #------------------------------------------------------------------------------ # License -# This file is part of OpenFOAM, licensed under GNU General Public License -# . +# This file is part of OpenFOAM, distributed under GPL-3.0-or-later. # # File # bin/tools/source-bashrc # # Description -# Source user ~/.bashrc and OpenFOAM etc/bashrc +# Source user ~/.bashrc and OpenFOAM etc/bashrc. +# Not normally sourced manually, but from bash with the --rcfile option. # -# This file is normally not sourced manually, -# but from bash with the --rcfile option. #------------------------------------------------------------------------------ -# Hard-coded value (eg, with autoconfig) +# Hard-coded directory path (eg, autoconfig) projectDir="@PROJECT_DIR@" if [ -z "$projectDir" ] || [ "${projectDir#@}" != "$projectDir" ] then - # Auto-detect (as per OpenFOAM etc/bashrc) + # Auto-detect location (as per OpenFOAM etc/bashrc) # -- # Assuming this file is $WM_PROJECT_DIR/bin/tools/source-bashrc, # the next lines should work when sourced by BASH or ZSH shells. # -- projectDir="${BASH_SOURCE:-${ZSH_NAME:+$0}}" - [ -n "$projectDir" ] && projectDir="$(\cd $(dirname $projectDir)/../.. && \pwd -L)" || unset projectDir + if [ -n "$projectDir" ] + then + projectDir="$(\cd "$(dirname "$projectDir")"/../.. && \pwd -L)" || \ + unset projectDir + fi fi #------------------------------------------------------------------------------ @@ -66,7 +68,7 @@ then # Some feedback if [ -n "$PS1" ] && [ -d "$WM_PROJECT_DIR" ] then - info="$(foamEtcFile -show-patch 2>/dev/null)" + info="$("$WM_PROJECT_DIR"/bin/foamEtcFile -show-patch 2>/dev/null)" # echo "Using: OpenFOAM-$WM_PROJECT_VERSION ($FOAM_API${info:+ patch=$info}) - visit www.openfoam.com" 1>&2 echo "Using: OpenFOAM-$WM_PROJECT_VERSION${info:+ (patch=$info)} - visit www.openfoam.com" 1>&2 diff --git a/doc/openfoam.1.in b/doc/openfoam.1.in new file mode 100644 index 0000000000..2dbdac04bb --- /dev/null +++ b/doc/openfoam.1.in @@ -0,0 +1,70 @@ +.TH "OPENFOAM" 1 "OpenFOAM-version" "www.openfoam.com" "OpenFOAM Commands Manual" + +.SH NAME +openfoam \- OpenFOAM bash(1) session + +.SH SYNOPSIS +\fBopenfoam\fR [\fIOPTIONS\fR] [\fIapplication ...\fR] + +.SH DESCRIPTION +Activate an \fBOpenFOAM\fR environment in an interactive or +non-interactive bash(1) session. + +If no application is given, an interactive bash session will be used. +If an application (optionally with arguments) is provided, the +OpenFOAM \fIetc/bashrc\fR file will be sourced from the project directory +prior to running the application. + +.SH OPTIONS +.TP +\fB\-c\fR \fIcommand\fR +Execute shell commands with OpenFOAM environment +.TP +\fB\-D\fR\fIkey=[value]\fR +Define key/value to pass as a preference +.TP +\fB\-sp\fR +Use single precision for scalar-size +.TP +\fB\-dp\fR +Use double precision for scalar-size +.TP +\fB\-spdp\fR +Use single precision for scalar-size, double for solve-scalar size +.TP +\fB\-int32\fR +Use 32-bit label-size +.TP +\fB\-int64\fR +Use 64-bit label-size +.TP +\fB\-prefix=DIR\fR +Specify alternative OpenFOAM project directory +.TP +\fB\-show-api\fR | \fB\-version\fR +Print META-INFO api value and exit +.TP +\fB\-show-patch\fR +Print META-INFO patch value and exit +.TP +\fB\-show-prefix\fR +Print project directory and exit +.TP +\fB\-help\fR +Print the usage + +.SH ARGUMENTS + +If arguments remain after option processing, the first argument is +assumed to be an application with options and arguments. + +.SH FILES + +The \fIetc/bashrc\fR file from the OpenFOAM project directory supplies +the environment settings. + +.SH "SEE ALSO" +Online documentation https://www.openfoam.com/documentation/ + +.SH COPYRIGHT +Copyright \(co 2020 OpenCFD Ltd. diff --git a/etc/bashrc b/etc/bashrc index 9743498f39..30241548cd 100644 --- a/etc/bashrc +++ b/etc/bashrc @@ -6,11 +6,10 @@ # \\/ M anipulation | #------------------------------------------------------------------------------ # Copyright (C) 2011-2016 OpenFOAM Foundation -# Copyright (C) 2016-2019 OpenCFD Ltd. +# Copyright (C) 2016-2020 OpenCFD Ltd. #------------------------------------------------------------------------------ # License -# This file is part of OpenFOAM, licensed under GNU General Public License -# . +# This file is part of OpenFOAM, distributed under GPL-3.0-or-later. # # File # etc/bashrc @@ -30,11 +29,16 @@ # - $WM_PROJECT_SITE/$FOAM_API/etc/prefs.sh # - $WM_PROJECT_SITE/etc/prefs.sh # +# Some settings can also be overridden on the command-line when +# sourcing this file. For example, +# +# . /path/etc/bashrc WM_COMPILER=Clang WM_LABEL_SIZE=64 +# # Environment -# FOAM_VERBOSE (set/unset) -# - add extra verbosity when sourcing files # FOAM_CONFIG_NOUSER (set/unset) # - suppress use of user/group configuration files +# FOAM_VERBOSE (set/unset) +# - add extra verbosity when sourcing files # WM_PROJECT_SITE (optional directory) # - local site-specific directory, uses WM_PROJECT_DIR/site if unset # diff --git a/etc/config.sh/setup b/etc/config.sh/setup index d502e835cc..afead38f9f 100644 --- a/etc/config.sh/setup +++ b/etc/config.sh/setup @@ -132,8 +132,8 @@ then _foamAddMan "$WM_PROJECT_DIR/doc" fi -# Interactive shell -if /usr/bin/tty -s 2>/dev/null +# Interactive shell (use PS1, not tty) +if [ -n "$PS1" ] then _foamEtc -config aliases [ "${BASH_VERSINFO:-0}" -ge 4 ] && _foamEtc -config bash_completion diff --git a/etc/cshrc b/etc/cshrc index 86fb8bb6cf..b63ba3e4ed 100644 --- a/etc/cshrc +++ b/etc/cshrc @@ -29,11 +29,16 @@ # - $WM_PROJECT_SITE/$FOAM_API/etc/prefs.csh # - $WM_PROJECT_SITE/prefs.csh # +# Some settings can also be overridden on the command-line when +# sourcing this file. For example, +# +# source /path/etc/cshrc WM_COMPILER=Clang WM_LABEL_SIZE=64 +# # Environment -# FOAM_VERBOSE (set/unset) -# - add extra verbosity when sourcing files # FOAM_CONFIG_NOUSER (set/unset) # - suppress use of user/group configuration files +# FOAM_VERBOSE (set/unset) +# - add extra verbosity when sourcing files # WM_PROJECT_SITE (optional directory) # - local site-specific directory, uses WM_PROJECT_DIR/site if unset # diff --git a/etc/openfoam b/etc/openfoam new file mode 100755 index 0000000000..e6d67dcb57 --- /dev/null +++ b/etc/openfoam @@ -0,0 +1,272 @@ +#!/bin/bash +#------------------------------------------------------------------------------ +# ========= | +# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox +# \\ / O peration | +# \\ / A nd | www.openfoam.com +# \\/ M anipulation | +#------------------------------------------------------------------------------ +# Copyright (C) 2019-2020 OpenCFD Ltd. +#------------------------------------------------------------------------------ +# License +# This file is part of OpenFOAM, distributed under GPL-3.0-or-later. +# +# Script +# openfoam [options] [args] +# +# Description +# Open an interactive bash session with an OpenFOAM environment, +# or run an OpenFOAM application (with arguments) after first sourcing +# the OpenFOAM etc/bashrc file from the project directory. +# +# Note +# This script normally exists in the $WM_PROJECT_DIR/etc/ directory. +# Do not copy/move/link to other locations. Use instead an edited copy of +# `bin/tools/openfoam.in` with a hard-coded projectDir entry. +# +# See OpenFOAM etc/bashrc for (command-line) preferences. +# Some equivalent settings: +# -sp | -DWM_PRECISION_OPTION=SP +# -dp | -DWM_PRECISION_OPTION=DP +# -int32 | -DWM_LABEL_SIZE=32 +# -int64 | -DWM_LABEL_SIZE=64 +# +# However, the '-D' options grant more flexibility. For example, +# etc/openfoam -DWM_COMPILER=Clang +# +#------------------------------------------------------------------------------ +# Auto-detect from location +projectDir="$(\cd "$(dirname "${0%/*}")" && \pwd -L)" + +#------------------------------------------------------------------------------ +printHelp() { + cat</dev/null)" + + if [ -n "$value" ] + then + echo "$value" + else + echo "Could not determine OPENFOAM '$1' value" 1>&2 + return 1 + fi +} + +#------------------------------------------------------------------------------- + +# No inheritance of FOAM_SETTINGS +unset FOAM_SETTINGS +unset _foamSettings _foamScriptCommand + +# Parse options +while [ "$#" -gt 0 ] +do + case "$1" in + -h | -help* | --help*) + printHelp + ;; + -show-api | -version | --version) # Show API and exit + getApiInfo api + exit $? + ;; + -show-patch) # Show patch level and exit + getApiInfo patch + exit $? + ;; + -show-prefix) # Show project directory and exit + echo "$projectDir" + exit $? + ;; + + -c) # Shell command + _foamScriptCommand="$2" + [ -n "$_foamScriptCommand" ] || { + echo "$0: missing or bad command argument: $2" 1>&2 + exit 1 + } + shift 2 + break + ;; + + -D*) # Define key/value to pass as preference + setting="${1#-D}" + if [ -n "$setting" ] + then + _foamSettings="$_foamSettings${_foamSettings:+ }$setting" + fi + ;; + + -sp | -dp | -spdp ) + # WM_PRECISION_OPTION=(SP|DP|SPDP) + setting=$(echo "${1#-}" | sed -e 's/-//g;y/sdp/SDP/') + _foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=$setting" + ;; + + -int32 | -int64) + # WM_LABEL_SIZE=... + _foamSettings="$_foamSettings${_foamSettings:+ }WM_LABEL_SIZE=${1#-int}" + ;; + + -prefix=*) + projectDir="${1#*=}" + ;; + + -verbose) + export FOAM_VERBOSE=true + ;; + + --) + shift + break + ;; + -*) + echo "$0: unknown option: '$1'" 1>&2 + exit 1 + ;; + *) + break + ;; + esac + shift +done + +#------------------------------------------------------------------------------- + +# Sanity check (installed under /bin, /usr/bin, /usr/local/bin) +# This should not happen. +# If copied to a system dir, should also be using hard-coded values! + +if [ "${projectDir%/bin}" != "$projectDir" ] +then + echo "Warning: suspicious project dir: $projectDir" 1>&2 +fi + +[ -d "$projectDir/META-INFO" ] || { + echo "Warning: missing META-INFO in OpenFOAM directory:" 1>&2 + echo " $projectDir" 1>&2 +} + + +# Remove current OpenFOAM environment +if [ -d "$WM_PROJECT_DIR" ] && [ -f "$WM_PROJECT_DIR/etc/config.sh/unset" ] +then + . "$WM_PROJECT_DIR/etc/config.sh/unset" || true +fi + +unset interactive + +if [ "$#" -eq 0 ] && [ -z "$_foamScriptCommand" ] +then + # Interactive shell, chain off via a file + interactive=true + _foamSourceBashEnv="$projectDir/bin/tools/source-bashrc" +else + # Non-interactive shell, use the OPENFOAM etc/bashrc + _foamSourceBashEnv="$projectDir/etc/bashrc" +fi + +[ -f "$_foamSourceBashEnv" ] || { + echo "Error: file not found: $_foamSourceBashEnv" 1>&2 + exit 2 +} + +if [ -n "$interactive" ] +then + # Interactive shell + # ----------------- + + # Source ~/.bashrc and OpenFOAM etc/bashrc in one of two ways: + # 1) Generate and use a tmp file + # 2) Chain off to a dedicated file [This is what we use] + + if [ -n "$_foamSettings" ] + then + # Pass preferences via the FOAM_SETTINGS mechanism + export FOAM_SETTINGS="$_foamSettings" + fi + + ## echo "Source with $_foamSourceBashEnv with '$FOAM_SETTINGS'" 1>&2 + + # Newer bash can use --init-file instead of --rcfile + exec bash --rcfile "$_foamSourceBashEnv" -i + exit $? # Safety +fi + + +# Non-interactive shell +# --------------------- + +# Source bashrc within a function to preserve command-line arguments +# Suppresses aliases as a side-effect, but non-interactive anyhow. +sourceBashrc() +{ + . "$_foamSourceBashEnv" $_foamSettings +} + + +if [ -n "$_foamScriptCommand" ] +then + # A shell command + + sourceBashrc + exec bash -c "$_foamScriptCommand" "$@" + exit $? # Safety +fi + + +# An application or a shell script + +# It may actually be a script with a '#!/project-path/bin/openfoam', +# so we need to catch this to avoid infinite recursion. +if [ -f "$1" ] \ +&& [ -n "$(sed -ne '1{/^#!.*\/openfoam$/p; q}' "$1" 2>/dev/null)" ] +then + # A shell script + + sourceBashrc + exec bash "$@" + +else + # An application + + sourceBashrc + exec "$@" + +fi + +#------------------------------------------------------------------------------