From f01ad2f18726a10f628f39d81d262d262568a04f Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 8 Apr 2020 10:37:21 +0200
Subject: [PATCH] CONFIG: improve config files for Darwin (#1667), BSD-csh
 syntax (#1668)

- use Clang instead of Gcc for Darwin since this is its system
  compiler. The user can force use of Gcc by using Gcc92 etc.

- make etc/cshrc sed check more robust.

- replace tcsh (${%var}) syntax with ("${var}" != "")

[Fixes and ideas from Alexey Matveichev]

CONFIG: do not source the gperftools environment by default

- this is now an used feature, but can be re-enabled by advanced users
  if required.
---
 etc/config.csh/functions      | 10 +++++++++-
 etc/config.csh/paraview       | 21 +++++++++------------
 etc/config.csh/settings       |  5 +++--
 etc/config.csh/setup          | 15 ++++++++++++---
 etc/config.csh/unset          |  7 ++++---
 etc/config.sh/functions       |  1 +
 etc/config.sh/gperftools      |  5 ++---
 etc/config.sh/paraview        |  2 +-
 etc/config.sh/paraview-system |  7 +++----
 etc/config.sh/settings        |  5 +++--
 etc/config.sh/setup           |  1 -
 etc/config.sh/unset           |  1 +
 etc/cshrc                     |  4 ++--
 13 files changed, 50 insertions(+), 34 deletions(-)

diff --git a/etc/config.csh/functions b/etc/config.csh/functions
index 685b6462b0d..c7cec4f07b5 100644
--- a/etc/config.csh/functions
+++ b/etc/config.csh/functions
@@ -27,7 +27,14 @@ alias _foamClean 'eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-env=\!*`'
 # Prepend PATH, MANPATH, LD_LIBRARY_PATH
 alias _foamAddPath 'setenv PATH \!*\:${PATH}'
 alias _foamAddMan  'setenv MANPATH \!*\:${MANPATH}'
-alias _foamAddLib  'setenv LD_LIBRARY_PATH \!*\:${LD_LIBRARY_PATH}'
+
+# Special treatment for Darwin
+# - DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH
+if (`uname -s` == "Darwin") then
+    alias _foamAddLib  'setenv DYLD_LIBRARY_PATH \!*\:${DYLD_LIBRARY_PATH}'
+else
+    alias _foamAddLib  'setenv LD_LIBRARY_PATH \!*\:${LD_LIBRARY_PATH}'
+endif
 
 # Prefix to LD_LIBRARY_PATH with additional checking
 # $1 = base directory for 'lib' or 'lib64'
@@ -59,6 +66,7 @@ unsetenv FFTW_ARCH_PATH
 unsetenv GPERFTOOLS_ARCH_PATH
 unsetenv GMP_ARCH_PATH
 unsetenv MPFR_ARCH_PATH
+unsetenv LLVM_ARCH_PATH
 unsetenv MESA_ARCH_PATH
 unsetenv METIS_ARCH_PATH
 unsetenv SCOTCH_ARCH_PATH
diff --git a/etc/config.csh/paraview b/etc/config.csh/paraview
index bc57ed568cf..8f85b808a5d 100644
--- a/etc/config.csh/paraview
+++ b/etc/config.csh/paraview
@@ -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
-#     <http://www.gnu.org/licenses/>.
+#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # File
 #     config.csh/paraview
@@ -65,7 +64,7 @@ if (! $?WM_COMPILER_LIB_ARCH ) setenv WM_COMPILER_LIB_ARCH
 # Compiler-specific location for ThirdParty installations
 set archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"
 
-# Clean PATH and LD_LIBRARY_PATH
+# Clean path and library path
 eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-env=PATH "$ParaView_DIR $archDir/ParaView- $archDir/qt- $archDir/cmake-"`
 eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-env=LD_LIBRARY_PATH "$ParaView_DIR $archDir/ParaView- $archDir/qt-"`
 
@@ -106,13 +105,12 @@ case "none":
 
 case "system":
     # Obtain major.minor from `paraview --version`
-    set pv_api=`paraview --version | \
-        sed -ne 's/^[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p'`
+    set pv_api=`paraview --version | sed -ne 's/^[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p'`
 
-    if ( "${%pv_api}" ) then
-        setenv PV_PLUGIN_PATH "$FOAM_LIBBIN/paraview-$pv_api"
-    else
+    if ("${pv_api}" == "") then
         unsetenv ParaView_DIR PV_PLUGIN_PATH
+    else
+        setenv PV_PLUGIN_PATH "$FOAM_LIBBIN/paraview-$pv_api"
     endif
 
     if ($?FOAM_VERBOSE && $?prompt) then
@@ -138,9 +136,8 @@ default:
         breaksw
 
     case [0-9]*:
-        # Extract API from VERSION
-        set pv_api=`echo "$ParaView_VERSION" | \
-            sed -ne 's/^[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p'`
+        # Extract API from VERSION.
+        set pv_api=`echo "$ParaView_VERSION" | sed -ne 's/^[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p'`
         breaksw
     endsw
 
diff --git a/etc/config.csh/settings b/etc/config.csh/settings
index 196f66ebb01..ca3490032fb 100644
--- a/etc/config.csh/settings
+++ b/etc/config.csh/settings
@@ -90,8 +90,9 @@ case Linux:
     endsw
     breaksw
 
-case Darwin:    # Presumably x86_64
+case Darwin:    # Presume x86_64, with clang (not gcc) as system compiler
     setenv WM_ARCH darwin64
+    if ("$WM_COMPILER" == Gcc) setenv WM_COMPILER Clang
     echo "Darwin support is clang/llvm only"
     breaksw
 
@@ -168,7 +169,7 @@ endif
 # OpenFOAM executables (user, group, standard)
 _foamAddPath "${FOAM_USER_APPBIN}:${FOAM_SITE_APPBIN}:${FOAM_APPBIN}"
 
-# Dummy versions of external libraries. To be found last in LD_LIBRARY_PATH
+# Dummy versions of external libraries. To be found last in library path
 _foamAddLib  "$FOAM_LIBBIN/dummy"
 
 # External (ThirdParty) libraries. Also allowed to be unset
diff --git a/etc/config.csh/setup b/etc/config.csh/setup
index 497fdd795d1..0d43ccb033b 100644
--- a/etc/config.csh/setup
+++ b/etc/config.csh/setup
@@ -67,7 +67,7 @@ foreach foamDir (\
     endif
 end
 
-if ( "${%foundDir}" ) then
+if ("${foundDir}" == true) then
     _foamEcho "Using $WM_THIRD_PARTY_DIR"
 else
     # Dummy fallback value
@@ -141,9 +141,9 @@ endif
 # Clean standard environment variables (PATH, MANPATH, LD_LIBRARY_PATH)
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 # Prevent local variables from shadowing setenv variables
-unset PATH MANPATH LD_LIBRARY_PATH
-if (! $?LD_LIBRARY_PATH ) setenv LD_LIBRARY_PATH
+unset PATH MANPATH LD_LIBRARY_PATH DYLD_LIBRARY_PATH
 if (! $?MANPATH ) setenv MANPATH
+if (! $?LD_LIBRARY_PATH ) setenv LD_LIBRARY_PATH
 
 _foamClean PATH "$foamOldDirs"
 _foamClean MANPATH "$foamOldDirs"
@@ -185,6 +185,15 @@ if ( $?MANPATH ) then
     setenv MANPATH "${MANPATH}:"
 endif
 
+if ($?LD_LIBRARY_PATH) then
+    if ("${LD_LIBRARY_PATH}" == "") unsetenv LD_LIBRARY_PATH
+endif
+
+# Darwin
+if ($?DYLD_LIBRARY_PATH) then
+    if ("${DYLD_LIBRARY_PATH}" == "") unsetenv DYLD_LIBRARY_PATH
+endif
+
 
 # Cleanup temporary information
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/etc/config.csh/unset b/etc/config.csh/unset
index 580fb8c8563..aa1fc04dcd5 100644
--- a/etc/config.csh/unset
+++ b/etc/config.csh/unset
@@ -23,7 +23,7 @@
 
 unset foamClean
 if ( $?WM_PROJECT_DIR ) then
-    set foamClean=$WM_PROJECT_DIR/bin/foamCleanPath
+    set foamClean="$WM_PROJECT_DIR/bin/foamCleanPath"
     if ( ! -f "$foamClean" || ! -x "$foamClean" ) unset foamClean
 endif
 
@@ -141,6 +141,7 @@ unsetenv FFTW_ARCH_PATH
 unsetenv GPERFTOOLS_ARCH_PATH
 unsetenv GMP_ARCH_PATH
 unsetenv MPFR_ARCH_PATH
+unsetenv LLVM_ARCH_PATH
 unsetenv MESA_ARCH_PATH
 unsetenv METIS_ARCH_PATH
 unsetenv SCOTCH_ARCH_PATH
@@ -155,12 +156,12 @@ if ( $?foamClean ) then
 
     if ($?LD_LIBRARY_PATH) then
         eval `$foamClean -csh-env=LD_LIBRARY_PATH "$foamOldDirs"`
-        if ( ${%LD_LIBRARY_PATH} == 0 ) unsetenv LD_LIBRARY_PATH
+        if ("${LD_LIBRARY_PATH}" == "") unsetenv LD_LIBRARY_PATH
     endif
 
     if ($?MANPATH) then
         eval `$foamClean -csh-env=MANPATH "$foamOldDirs"`
-        if ( ${%MANPATH} == 0 ) unsetenv MANPATH
+        if ("${MANPATH}" == "") unsetenv MANPATH
     endif
 
 endif
diff --git a/etc/config.sh/functions b/etc/config.sh/functions
index 30ca8255343..9c55f5cdd8c 100644
--- a/etc/config.sh/functions
+++ b/etc/config.sh/functions
@@ -212,6 +212,7 @@ then
     unset GPERFTOOLS_ARCH_PATH
     unset GMP_ARCH_PATH
     unset MPFR_ARCH_PATH
+    unset LLVM_ARCH_PATH
     unset MESA_ARCH_PATH
     unset METIS_ARCH_PATH
     unset SCOTCH_ARCH_PATH
diff --git a/etc/config.sh/gperftools b/etc/config.sh/gperftools
index 80ea4c175c8..ea58dc58eab 100644
--- a/etc/config.sh/gperftools
+++ b/etc/config.sh/gperftools
@@ -9,12 +9,11 @@
 #     Copyright (C) 2016-2018 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
-#     This file is part of OpenFOAM, licensed under GNU General Public License
-#     <http://www.gnu.org/licenses/>.
+#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # File
 #     etc/config.sh/gperftools
-#     - sourced by OpenFOAM-*/etc/bashrc
+#     Not normally sourced by OpenFOAM-*/etc/bashrc
 #
 # Description
 #     Setup file for GPERFTOOLS binaries/libraries.
diff --git a/etc/config.sh/paraview b/etc/config.sh/paraview
index d850ccf307b..4890bc5c3c9 100644
--- a/etc/config.sh/paraview
+++ b/etc/config.sh/paraview
@@ -64,7 +64,7 @@ pv_api=auto             # Either auto or pair of (major.minor) digits
 # Compiler-specific location for ThirdParty installations
 archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"
 
-# Clean PATH and LD_LIBRARY_PATH
+# Clean path and library path
 eval \
     "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=PATH \
     $ParaView_DIR $archDir/ParaView- $archDir/qt- $archDir/cmake-)"
diff --git a/etc/config.sh/paraview-system b/etc/config.sh/paraview-system
index c972de3df20..1bdec0f30d9 100644
--- a/etc/config.sh/paraview-system
+++ b/etc/config.sh/paraview-system
@@ -5,11 +5,10 @@
 #   \\  /    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
-#     <http://www.gnu.org/licenses/>.
+#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # File
 #     etc/config.sh/paraview-system
@@ -26,7 +25,7 @@
 # Compiler-specific location for ThirdParty installations
 archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"
 
-# Clean PATH and LD_LIBRARY_PATH
+# Clean path and library path
 eval \
     "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=PATH \
     $ParaView_DIR $archDir/ParaView-)"
diff --git a/etc/config.sh/settings b/etc/config.sh/settings
index fefba566bc4..69ad56b3857 100644
--- a/etc/config.sh/settings
+++ b/etc/config.sh/settings
@@ -82,8 +82,9 @@ Linux)
     esac
     ;;
 
-Darwin)     # Presumably x86_64
+Darwin)     # Presume x86_64, with clang (not gcc) as system compiler
     WM_ARCH=darwin64
+    [ "$WM_COMPILER" = Gcc ] && WM_COMPILER=Clang
     echo "Darwin support is clang/llvm only" 1>&2
     ;;
 
@@ -165,7 +166,7 @@ fi
 # OpenFOAM executables (user, group, standard)
 _foamAddPath "$FOAM_USER_APPBIN:$FOAM_SITE_APPBIN:$FOAM_APPBIN"
 
-# Dummy versions of external libraries. To be found last in LD_LIBRARY_PATH
+# Dummy versions of external libraries. To be found last in library path
 _foamAddLib  "$FOAM_LIBBIN/dummy"
 
 # External (ThirdParty) libraries. Also allowed to be unset
diff --git a/etc/config.sh/setup b/etc/config.sh/setup
index 407aa4cce68..48287d4f069 100644
--- a/etc/config.sh/setup
+++ b/etc/config.sh/setup
@@ -147,7 +147,6 @@ _foamEtc -config  settings
 _foamEtc -config  mpi
 _foamEtc -config  paraview -- "$@"  # Pass through for evaluation
 _foamEtc -config  vtk
-_foamEtc -config  gperftools
 _foamEtc -config  adios2
 _foamEtc -config  CGAL
 _foamEtc -config  scotch
diff --git a/etc/config.sh/unset b/etc/config.sh/unset
index 6f05533f1ba..407d1364910 100644
--- a/etc/config.sh/unset
+++ b/etc/config.sh/unset
@@ -137,6 +137,7 @@ unset FFTW_ARCH_PATH
 unset GPERFTOOLS_ARCH_PATH
 unset GMP_ARCH_PATH
 unset MPFR_ARCH_PATH
+unset LLVM_ARCH_PATH
 unset MESA_ARCH_PATH
 unset KAHIP_ARCH_PATH
 unset METIS_ARCH_PATH
diff --git a/etc/cshrc b/etc/cshrc
index 380f9d3d495..d9150ae15cb 100644
--- a/etc/cshrc
+++ b/etc/cshrc
@@ -105,9 +105,9 @@ setenv WM_PROJECT OpenFOAM
 #    later on in this file.
 # --
 # If the directory naming does not match WM_PROJECT, need to change here
+# Important! No line continuation in the lsof evaluation (#1668)
 set projectName="$WM_PROJECT"
-set projectDir=`lsof +p $$ |& \
-    sed -ne 's@^[^/]*@@;\@/'"$projectName"'[^/]*/etc/cshrc@{s@/etc/cshrc.*@@p; q}'`
+set projectDir=`lsof +p $$ |& sed -ne 's#^[^/]*##;\@/'"$projectName"'[^/]*/etc/cshrc@{s#/etc/cshrc.*##p;q; }'`
 
 # set projectDir="$HOME/OpenFOAM/OpenFOAM-$WM_PROJECT_VERSION"
 # set projectDir="/opt/openfoam/OpenFOAM-$WM_PROJECT_VERSION"
-- 
GitLab