#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
# \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM, licensed under GNU General Public License
# .
#
# Script
# foamConfigurePaths
#
# Description
# Adjust hardcoded installation versions and paths in bashrc and config.sh/
#
#------------------------------------------------------------------------------
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<&2
echo
echo "Error encountered:"
while [ "$#" -ge 1 ]; do echo " $1"; shift; done
echo
echo "See '${0##*/} -help' for usage"
echo
exit 1
}
# -----------------------------------------------------------------------------
# Check that it appears to be an OpenFOAM installation
[ -f etc/bashrc -a -d etc/config.sh ] || \
usage "Please run from top-level directory of installation"
# Check if argument matches the expected input. Respects case.
# Uses sed for consistency with the replacement routines.
#
# _matches [... ]
#
_matches()
{
local input="$1"
shift
local result
for regexp
do
result=$(echo "$input" | sed -n -e "/^$regexp"'$/p')
test -n "$result" && return 0 # successful match
done
return 1
}
# Function to do replacement on file. Checks if any replacement has been done.
# _inlineSed
_inlineSed()
{
local file="$1"
local regexp="$2"
local replacement="$3"
local msg="$4"
local cmd='/^[^#]/s@'"$regexp"'@'"$replacement"'@'
[ -f "$file" ] || {
echo "Missing file: $file"
exit 2 # Fatal
}
grep -q "$regexp" "$file" && sed -i -e "$cmd" "$file" || { \
echo "Failed: ${msg:-replacement} in $file"
return 1
}
[ -n "$msg" ] && echo " $msg ($file)"
return 0
}
# Standard type of replacements.
# replace ..
# looks for KEYWORD=.*
replace()
{
local file="$1"
shift
local key val
while [ "$#" -ge 2 ]
do
key=$1
val=$2
shift 2
_inlineSed \
"$file" \
"$key=.*" \
"$key=$val" \
"Replaced $key setting by '$val'"
done
}
# Get the option's value (argument).
# Die if the argument doesn't exist or is empty
# $1 option
# $2 value
getOptionValue()
{
[ -n "$2" ] || die "'$1' option requires an argument"
echo "$2"
}
# Remove BASH_SOURCE and FOAM_INST_DIR=... magic that looks like this:
# ----
# variable=$BASH_SOURCE
# [ -n "$variable" ] && FOAM_INST_DIR= ...
# FOAM_INST_DIR=...
# ----
removeMagic()
{
local file="$1"
[ -f "$file" ] || {
echo "Missing file: $file"
exit 2 # Fatal
}
echo " Remove default FOAM_INST_DIR setting ($file)"
sed -i \
-e '/^ *#/!{/\(BASH_SOURCE\|FOAM_INST_DIR=\)/s/^/##IGNORE## /}' \
"$file"
}
#------------------------------------------------------------------------------
unset adjusted optMpi
# Parse options
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help* | --help*)
usage
;;
'')
# Discard empty arguments
;;
## Basic ##
-prefix | -foamInstall | --foamInstall)
# Replace WM_PROJECT_INST_DIR, disable FOAM_INST_DIR discovery
optionValue=$(getOptionValue "$@")
removeMagic etc/bashrc
replace etc/bashrc WM_PROJECT_INST_DIR "$optionValue"
adjusted=true
shift
;;
-projectName | --projectName)
# Replace basename part of WM_PROJECT_DIR=...
optionValue=$(getOptionValue "$@")
_inlineSed \
etc/bashrc \
'WM_PROJECT_DIR=.*' \
'WM_PROJECT_DIR=$WM_PROJECT_INST_DIR/'"$optionValue" \
"Replaced WM_PROJECT_DIR basename by $optionValue"
adjusted=true
shift
;;
-version | -foamVersion | --projectVersion)
# Replace WM_PROJECT_VERSION=...
optionValue=$(getOptionValue "$@")
replace etc/bashrc WM_PROJECT_VERSION "$optionValue"
adjusted=true
shift
;;
-archOption | --archOption)
# Replace WM_ARCH_OPTION=...
optionValue=$(getOptionValue "$@")
_matches "$optionValue" 32 64 || \
die "'$1' has bad value: '$optionValue'"
if [ "$optionValue" = "$(sed -ne '/^[^#]/s/^.* WM_ARCH_OPTION=//p' etc/bashrc)" ]
then
echo "WM_ARCH_OPTION already set to $optionValue"
: ${adjusted:=false}
else
replace etc/bashrc WM_ARCH_OPTION "$optionValue"
adjusted=true
fi
shift
;;
-SP | -float32)
# Replace WM_PRECISION_OPTION=...
replace etc/bashrc WM_PRECISION_OPTION "SP"
adjusted=true
;;
-DP | -float64)
# Replace WM_PRECISION_OPTION=...
replace etc/bashrc WM_PRECISION_OPTION "DP"
adjusted=true
;;
-int32 | -int64)
# Replace WM_LABEL_SIZE=...
optionValue="${1#-int}"
replace etc/bashrc WM_LABEL_SIZE "$optionValue"
adjusted=true
;;
## Compiler ##
-clang)
# Replace clang_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/compiler clang_version "$optionValue"
adjusted=true
shift
;;
-gcc)
# Replace gcc_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/compiler gcc_version "$optionValue"
adjusted=true
shift
;;
-system)
# Replace WM_COMPILER_TYPE=... and WM_COMPILER=...
optionValue=$(getOptionValue "$@")
replace etc/bashrc \
WM_COMPILER_TYPE system \
WM_COMPILER "$optionValue"
adjusted=true
shift
;;
-third | -ThirdParty)
# Replace WM_COMPILER_TYPE=... and WM_COMPILER=...
optionValue=$(getOptionValue "$@")
replace etc/bashrc \
WM_COMPILER_TYPE ThirdParty \
WM_COMPILER "$optionValue"
adjusted=true
shift
;;
gmp-[4-9]* | gmp-system)
# gcc-related package
replace etc/config.sh/compiler gmp_version "$1"
adjusted=true
;;
mpfr-[2-9]* | mpfr-system)
# gcc-related package
replace etc/config.sh/compiler mpfr_version "$1"
adjusted=true
;;
mpc-[0-9]* | mpc-system)
# gcc-related package
replace etc/config.sh/compiler mpc_version "$1"
adjusted=true
;;
## MPI ##
-mpi)
# Explicitly set WM_MPLIB=...
optionValue=$(getOptionValue "$@")
replace etc/bashrc WM_MPLIB "$optionValue"
optMpi=system
adjusted=true
shift
;;
-openmpi)
# Replace FOAM_MPI=openmpi-.. and set to use third-party
# The edit is slightly fragile, but works
expected="openmpi-[1-9][.0-9]*"
optMpi=$(getOptionValue "$@")
_matches "$optMpi" "$expected" || \
die "'$1' has bad value: '$optMpi'"
_inlineSed etc/config.sh/mpi \
"FOAM_MPI=$expected" \
"FOAM_MPI=$optMpi" \
"Replaced 'FOAM_MPI=$expected' setting by 'FOAM_MPI=$optMpi'"
replace etc/bashrc WM_MPLIB OPENMPI
adjusted=true
shift
;;
-openmpi-system)
# Explicitly set WM_MPLIB=SYSTEMOPENMPI
replace etc/bashrc WM_MPLIB SYSTEMOPENMPI
optMpi=system
adjusted=true
;;
-openmpi-third)
# Explicitly set WM_MPLIB=OPENMPI, using default setting for openmpi
replace etc/bashrc WM_MPLIB OPENMPI
optMpi=third
adjusted=true
;;
## Components ##
-boost)
# Replace boost_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/CGAL boost_version "$optionValue"
adjusted=true
shift
;;
-boost-path)
# Replace BOOST_ARCH_PATH=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/CGAL BOOST_ARCH_PATH "$optionValue"
adjusted=true
shift
;;
-cgal)
# Replace cgal_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/CGAL cgal_version "$optionValue"
adjusted=true
shift
;;
-cgal-path)
# Replace CGAL_ARCH_PATH=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/CGAL CGAL_ARCH_PATH "$optionValue"
adjusted=true
shift
;;
-fftw)
# Replace fftw_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/FFTW fftw_version "$optionValue"
adjusted=true
shift
;;
-fftw-path)
# Replace FFTW_ARCH_PATH=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/FFTW FFTW_ARCH_PATH "$optionValue"
adjusted=true
shift
;;
-cmake)
# Replace cmake_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/paraview cmake_version "$optionValue"
adjusted=true
shift
;;
-kahip)
# Replace KAHIP_VERSION=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/kahip KAHIP_VERSION "$optionValue"
adjusted=true
shift
;;
-kahip-path)
# Replace KAHIP_ARCH_PATH=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/kahip KAHIP_ARCH_PATH "$optionValue"
adjusted=true
shift
;;
-metis)
# Replace METIS_VERSION=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/metis METIS_VERSION "$optionValue"
adjusted=true
shift
;;
-metis-path)
# Replace METIS_ARCH_PATH=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/metis METIS_ARCH_PATH "$optionValue"
adjusted=true
shift
;;
-scotch | -scotchVersion | --scotchVersion)
# Replace SCOTCH_VERSION=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/scotch SCOTCH_VERSION "$optionValue"
adjusted=true
shift
;;
-scotch-path | -scotchArchPath | --scotchArchPath)
# Replace SCOTCH_ARCH_PATH=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/scotch SCOTCH_ARCH_PATH "$optionValue"
adjusted=true
shift
;;
## Graphics ##
-paraview | -paraviewVersion | --paraviewVersion)
# Replace ParaView_VERSION=...
expected="[5-9][.0-9]*"
optionValue=$(getOptionValue "$@")
_matches "$optionValue" "$expected" || \
die "'$1' has bad value: '$optionValue'"
replace etc/config.sh/paraview ParaView_VERSION "$optionValue"
adjusted=true
shift
;;
-paraview-path | -paraviewInstall | --paraviewInstall)
# Replace ParaView_DIR=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/paraview ParaView_DIR "$optionValue"
adjusted=true
shift
;;
-vtk)
# Replace vtk_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/vtk vtk_version "$optionValue"
adjusted=true
shift
;;
-mesa)
# Replace mesa_version=...
optionValue=$(getOptionValue "$@")
replace etc/config.sh/vtk mesa_version "$optionValue"
adjusted=true
shift
;;
## Misc ##
-no-third)
# Replace WM_THIRD_PARTY_DIR=... with location within the project dir
replace etc/bashrc WM_THIRD_PARTY_DIR '$WM_PROJECT_DIR/ThirdParty'
adjusted=true
shift
;;
-default-third)
# Replace WM_THIRD_PARTY_DIR=... with default location/naming
replace etc/bashrc WM_THIRD_PARTY_DIR \
'$WM_PROJECT_INST_DIR/ThirdParty-$WM_PROJECT_VERSION'
adjusted=true
shift
;;
-third-path)
# Replace WM_THIRD_PARTY_DIR=...
optionValue=$(getOptionValue "$@")
replace etc/bashrc WM_THIRD_PARTY_DIR "$optionValue"
adjusted=true
shift
;;
-no-site)
# Replace fallback value for site within the project dir
_inlineSed \
etc/config.sh/settings \
'^ *siteDir=.*\/site' \
'siteDir=$WM_PROJECT_DIR/site' \
"Setting fallback site-dir '\$WM_PROJECT_DIR/site'"
adjusted=true
shift
;;
-default-site)
# Replace WM_THIRD_PARTY_DIR=... with standard location
_inlineSed \
etc/config.sh/settings \
'^ *siteDir=.*\/site' \
'siteDir=$WM_PROJECT_INST_DIR/site' \
"Setting fallback site-dir '\$WM_PROJECT_INST_DIR/site'"
adjusted=true
shift
;;
-sigfpe | -no-sigfpe)
echo "Enable/disable FOAM_SIGFPE now via controlDict" 1>&2
;;
*)
die "unknown option/argument: '$1'"
;;
esac
shift
done
if [ "$adjusted" = false ]
then
echo "Nothing adjusted"
exit 0
elif [ -z "$adjusted" ]
then
die "Please specify at least one configure option"
fi
#------------------------------------------------------------------------------