From e827c117e3c3ed44c2e698a593bf88616e928a82 Mon Sep 17 00:00:00 2001
From: Alexey Matveichev <alexey@matveichev.com>
Date: Tue, 16 Aug 2022 15:50:12 +0200
Subject: [PATCH] CONFIG: fixes for MacOS (#2555)

- introduce a FOAM_LD_LIBRARY_PATH variable to shadow
  DYLD_LIBRARY_PATH on MacOS.

  The DYLD_LIBRARY_PATH and LD_LIBRARY_PATH cannot be modified via sub
  shells etc when SIP is active. This helps circumvent these
  restrictions, which is obviously a hack, but seems to be required.

COMP: disable -ftrapping-math in geompack for MacOS
---
 bin/foamCleanPath                             | 103 ++++++++++--------
 bin/tools/RunFunctions                        |   4 +-
 bin/tools/change-sitedir.sh                   |   4 +-
 bin/tools/change-userdir.sh                   |   6 +-
 etc/config.csh/functions                      |   8 +-
 etc/config.csh/paraview                       |  12 +-
 etc/config.csh/setup                          |  18 ++-
 etc/config.csh/unset                          |  10 +-
 etc/config.sh/functions                       |  14 ++-
 etc/config.sh/paraview                        |  14 ++-
 etc/config.sh/paraview-system                 |   8 +-
 etc/config.sh/setup                           |  29 ++++-
 etc/config.sh/unset                           |   8 +-
 .../triSurfaceTools/geompack/geompack.C       |   4 +
 14 files changed, 158 insertions(+), 84 deletions(-)

diff --git a/bin/foamCleanPath b/bin/foamCleanPath
index 03d75bad592..aa9e13856be 100755
--- a/bin/foamCleanPath
+++ b/bin/foamCleanPath
@@ -7,7 +7,7 @@
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 #     Copyright (C) 2011-2016 OpenFOAM Foundation
-#     Copyright (C) 2017-2021 OpenCFD Ltd.
+#     Copyright (C) 2017-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -48,26 +48,29 @@
 #     - Similarly for c-shell
 #       eval `foamCleanPath -csh-path dir1:dir2`
 #
-# For library paths, it is suggested to use -sh-lib, or -csh-lib.
-# The will use DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH on Darwin.
+# For library paths, it is suggested to use -sh-lib, -env=-lib etc.
+#
+# On Darwin it uses FOAM_LD_LIBRARY_PATH instead of LD_LIBRARY_PATH.
+# This should actually be DYLD_LIBRARY_PATH on Darwin, but setting that
+# or LD_LIBRARY_PATH via a shell-script is disallowed when SIP is active.
 #
 #------------------------------------------------------------------------------
 printHelp() {
     cat<<USAGE
 
-Usage: foamCleanPath [OPTION] ENV [filter] .. [filter]
+Usage: foamCleanPath [OPTION] ENVNAME [filter] .. [filter]
        foamCleanPath [OPTION] -env=name [filter] .. [filter]
 options:
   -env=NAME         Evaluate NAME to obtain initial content,
                     Accepts "-env=-path", "-env=-lib" shortcuts for PATH
-                    and LD_LIBRARY_PATH (DYLD_LIBRARY_PATH on Darwin)
+                    and LD_LIBRARY_PATH (FOAM_LD_LIBRARY_PATH on Darwin)
   -sh=NAME          Produce 'NAME=...' output for sh eval
   -csh=NAME         Produce 'setenv NAME ...' output for csh eval
   -sh-env=NAME      Same as -sh=NAME -env=NAME
   -csh-env=NAME     Same as -csh=NAME -env=NAME
   -sh-path | -csh-path  Same as -[c]sh-env=PATH
   -sh-lib  | -csh-lib   Same as -[c]sh-env=LD_LIBRARY_PATH
-                        or DYLD_LIBRARY_PATH on Darwin
+                        (FOAM_LD_LIBRARY_PATH on Darwin)
   -debug            Print debug information to stderr
   -strip            Remove inaccessible directories
   -verbose          Report some progress (input, output, ...)
@@ -81,7 +84,7 @@ Prints its argument (which should be a ':' separated list) cleansed from
 Exit status
     0  on success
     1  general error
-    2  initial value of 'path' is empty
+    2  initial value of ENVNAME is empty
 
 USAGE
     exit 0  # A clean exit
@@ -103,63 +106,69 @@ die()
 #-------------------------------------------------------------------------------
 
 # Input and outputs
-unset dirList shellOutput shellFlavour
+unset dirList shellFlavour shellOutput
 unset optDebug optEnvName optStrip optVerbose
 
 # Parse options
 while [ "$#" -gt 0 ]
 do
     case "$1" in
-    -h | -help*)
+    (-h | -help*)
         printHelp
         ;;
 
-    -env=*)
-        name="${1#*=}"
-        [ -n "$name" ] || die "Option '$1' missing an ENVNAME"
-
-        # Accept aliases
-        case "$name" in
-        (-path) name='PATH';;
-        (-lib)
-            case "$(uname -s 2>/dev/null)" in
-                (Darwin) name='DYLD_LIBRARY_PATH';;
-                (*) name='LD_LIBRARY_PATH';;
-            esac
-            ;;
-         esac
-         optEnvName="$name"     # Use for input evaluation
-         ;;
-
-    -csh-path | -sh-path)
+    (-csh-lib | -csh-path | -sh-lib | -sh-path)
         shellFlavour="$1"
-        name='PATH'
+        case "$1" in
+        (*-lib)
+            name='LD_LIBRARY_PATH'
+            if [ "$(uname -s 2>/dev/null)" = Darwin ]
+            then
+                name='FOAM_LD_LIBRARY_PATH' # Shadow DYLD_LIBRARY_PATH
+            fi
+            ;;
+        (*-path)
+            name='PATH'
+            ;;
+        esac
         optEnvName="$name"      # Use for input evaluation
         shellOutput="$name"     # Use for output
         ;;
 
-    -csh-lib | -sh-lib)
-        shellFlavour="$1"
-        case "$(uname -s 2>/dev/null)" in
-            (Darwin) name='DYLD_LIBRARY_PATH';;
-            (*) name='LD_LIBRARY_PATH';;
+    (-env=*)
+        name="${1#*=}"
+        [ -n "$name" ] || die "Option '$1' missing an ENVNAME"
+        # Handle (-lib | -path) aliases
+        case "$1" in
+        (*=-lib)
+            name='LD_LIBRARY_PATH'
+            if [ "$(uname -s 2>/dev/null)" = Darwin ]
+            then
+                name='FOAM_LD_LIBRARY_PATH' # Shadow DYLD_LIBRARY_PATH
+            fi
+            ;;
+        (*=-path)
+            name='PATH'
+            ;;
         esac
         optEnvName="$name"      # Use for input evaluation
-        shellOutput="$name"     # Use for output
         ;;
 
-    -csh=* | -sh=* | -csh-env=* | -sh-env=*)
+    (-csh=* | -csh-env=* | -sh=* | -sh-env=*)
         shellFlavour="$1"
         name="${1#*=}"
         [ -n "$name" ] || die "Option '$1' missing an ENVNAME"
-        # Accept aliases
-        case "$name" in
-        (-path) name='PATH';;
-        (-lib)
-            case "$(uname -s 2>/dev/null)" in
-                (Darwin) name='DYLD_LIBRARY_PATH';;
-                (*) name='LD_LIBRARY_PATH';;
-            esac
+        # Handle (-lib | -path) aliases
+        case "$1" in
+        (*=-lib)
+            name='LD_LIBRARY_PATH'
+            if [ "$(uname -s 2>/dev/null)" = Darwin ]
+            then
+                name='FOAM_LD_LIBRARY_PATH' # Shadow DYLD_LIBRARY_PATH
+            fi
+            ;;
+        (*=-path)
+            name='PATH'
             ;;
         esac
         shellOutput="$name"     # Use for output
@@ -167,16 +176,16 @@ do
         case "$1" in (*-env=*) optEnvName="$name";; esac
         ;;
 
-    -debug)
+    (-debug)
         optDebug=true
         ;;
-    -strip)
+    (-strip)
         optStrip=true
         ;;
-    -verbose)
+    (-verbose)
         optVerbose=true
         ;;
-    *)
+    (*)
         break
         ;;
     esac
diff --git a/bin/tools/RunFunctions b/bin/tools/RunFunctions
index 5d05ae82369..5ef1c3c23a9 100644
--- a/bin/tools/RunFunctions
+++ b/bin/tools/RunFunctions
@@ -26,9 +26,9 @@
 [ -d "$FOAM_TUTORIALS" ] || echo "No OpenFOAM tutorials? : $FOAM_TUTORIALS" 1>&2
 
 # Darwin workaround - SIP clearing DYLD_LIBRARY_PATH variable
-if [ -n "$FOAM_DYLD_LIBRARY_PATH" ] && [ -z "$DYLD_LIBRARY_PATH" ]
+if [ -n "$FOAM_LD_LIBRARY_PATH" ] && [ -z "$DYLD_LIBRARY_PATH" ]
 then
-    export DYLD_LIBRARY_PATH="$FOAM_DYLD_LIBRARY_PATH"
+    export DYLD_LIBRARY_PATH="$FOAM_LD_LIBRARY_PATH"
 fi
 
 
diff --git a/bin/tools/change-sitedir.sh b/bin/tools/change-sitedir.sh
index ad3d31e2d0b..7498370bfae 100644
--- a/bin/tools/change-sitedir.sh
+++ b/bin/tools/change-sitedir.sh
@@ -49,8 +49,8 @@ then
     foamClean="$WM_PROJECT_DIR/bin/foamCleanPath"
     if [ -x "$foamClean" ]
     then
-        cleaned=$($foamClean "$PATH" "$foamOldDirs") && PATH="$cleaned"
-        cleaned=$($foamClean "$LD_LIBRARY_PATH" "$foamOldDirs") \
+        cleaned=$($foamClean -env=PATH "$foamOldDirs") && PATH="$cleaned"
+        cleaned=$($foamClean -env=LD_LIBRARY_PATH "$foamOldDirs") \
             && LD_LIBRARY_PATH="$cleaned"
     fi
 
diff --git a/bin/tools/change-userdir.sh b/bin/tools/change-userdir.sh
index bb0dc25dd11..ab28479c52c 100644
--- a/bin/tools/change-userdir.sh
+++ b/bin/tools/change-userdir.sh
@@ -5,7 +5,7 @@
 #   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
-#     Copyright (C) 2017-2020 OpenCFD Ltd.
+#     Copyright (C) 2017-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -49,8 +49,8 @@ then
     foamClean="$WM_PROJECT_DIR/bin/foamCleanPath"
     if [ -x "$foamClean" ]
     then
-        cleaned=$($foamClean "$PATH" "$foamOldDirs") && PATH="$cleaned"
-        cleaned=$($foamClean "$LD_LIBRARY_PATH" "$foamOldDirs") \
+        cleaned=$($foamClean -env=PATH "$foamOldDirs") && PATH="$cleaned"
+        cleaned=$($foamClean -env=LD_LIBRARY_PATH "$foamOldDirs") \
             && LD_LIBRARY_PATH="$cleaned"
     fi
 
diff --git a/etc/config.csh/functions b/etc/config.csh/functions
index eb0b6bf0546..bf4f2347ccb 100644
--- a/etc/config.csh/functions
+++ b/etc/config.csh/functions
@@ -5,7 +5,7 @@
 #   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
-#     Copyright (C) 2018-2020 OpenCFD Ltd.
+#     Copyright (C) 2018-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -46,10 +46,10 @@ alias _foamAddMan  'setenv MANPATH \!*\:${MANPATH}'
 
 # Special treatment for Darwin
 # - DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH
-if ("${_foam_uname_s}" == "Darwin") then
-    alias _foamAddLib  'setenv DYLD_LIBRARY_PATH \!*\:${DYLD_LIBRARY_PATH}'
+if ("${_foam_uname_s}" == 'Darwin') then
+    alias _foamAddLib 'if (-e \!*) setenv DYLD_LIBRARY_PATH \!*\:${DYLD_LIBRARY_PATH}; if (-e \!*) setenv FOAM_LD_LIBRARY_PATH \!*\:${FOAM_LD_LIBRARY_PATH}'
 else
-    alias _foamAddLib  'setenv LD_LIBRARY_PATH \!*\:${LD_LIBRARY_PATH}'
+    alias _foamAddLib 'setenv LD_LIBRARY_PATH \!*\:${LD_LIBRARY_PATH}'
 endif
 
 # Prefix to LD_LIBRARY_PATH with additional checking
diff --git a/etc/config.csh/paraview b/etc/config.csh/paraview
index f9f801d1863..94a2b83cc88 100644
--- a/etc/config.csh/paraview
+++ b/etc/config.csh/paraview
@@ -69,6 +69,13 @@ set archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"
 eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-path "$ParaView_DIR $archDir/ParaView- $archDir/qt-"`
 eval `$WM_PROJECT_DIR/bin/foamCleanPath -csh-lib "$ParaView_DIR $archDir/ParaView- $archDir/qt-"`
 
+# Darwin
+switch ("$WM_ARCH")
+case darwin*:
+    setenv DYLD_LIBRARY_PATH "${FOAM_LD_LIBRARY_PATH}"
+    breaksw
+endsw
+
 # Evaluate command-line parameters for ParaView
 while ( $#argv > 0 )
     switch ($argv[1])
@@ -206,10 +213,11 @@ default:
         if ( "$pv_libdirs" != "" ) then
             switch ("$WM_ARCH")
             case darwin*:
-                setenv DYLD_LIBRARY_PATH "${pv_libdirs}:$DYLD_LIBRARY_PATH"
+                setenv FOAM_LD_LIBRARY_PATH "${pv_libdirs}:${FOAM_LD_LIBRARY_PATH}"
+                setenv DYLD_LIBRARY_PATH "${FOAM_LD_LIBRARY_PATH}"
                 breaksw
             default:
-                setenv LD_LIBRARY_PATH "${pv_libdirs}:$LD_LIBRARY_PATH"
+                setenv LD_LIBRARY_PATH "${pv_libdirs}:${LD_LIBRARY_PATH}"
                 breaksw
             endsw
         endif
diff --git a/etc/config.csh/setup b/etc/config.csh/setup
index 47cd76a91a3..33b39c927fc 100644
--- a/etc/config.csh/setup
+++ b/etc/config.csh/setup
@@ -148,16 +148,21 @@ endif
 # Clean standard environment variables (PATH, MANPATH, [DY]LD_LIBRARY_PATH)
 # - avoid local variables shadowing setenv variables
 
-unset PATH MANPATH LD_LIBRARY_PATH DYLD_LIBRARY_PATH
+unset PATH MANPATH LD_LIBRARY_PATH DYLD_LIBRARY_PATH FOAM_LD_LIBRARY_PATH
 if (! $?MANPATH ) setenv MANPATH
-if (! $?LD_LIBRARY_PATH ) setenv LD_LIBRARY_PATH
-if ("${_foam_uname_s}" == "Darwin" ) then
+if ("${_foam_uname_s}" == 'Darwin') then
     if (! $?DYLD_LIBRARY_PATH ) setenv DYLD_LIBRARY_PATH
+    if (! $?FOAM_LD_LIBRARY_PATH ) setenv FOAM_LD_LIBRARY_PATH
+else
+    if (! $?LD_LIBRARY_PATH ) setenv LD_LIBRARY_PATH
 endif
 
 _foamClean PATH "$foamOldDirs"
 _foamClean MANPATH "$foamOldDirs"
 _foamClean -lib "$foamOldDirs"
+if ("${_foam_uname_s}" == 'Darwin') then
+    setenv DYLD_LIBRARY_PATH "${FOAM_LD_LIBRARY_PATH}"
+endif
 
 
 #------------------------------------------------------------------------------
@@ -213,6 +218,9 @@ endif
 _foamClean PATH
 _foamClean MANPATH
 _foamClean -lib
+if ("${_foam_uname_s}" == 'Darwin') then
+   setenv DYLD_LIBRARY_PATH "${FOAM_LD_LIBRARY_PATH}"
+endif
 
 # Add trailing ':' for system manpages
 if ( $?MANPATH ) then
@@ -224,8 +232,8 @@ if ($?LD_LIBRARY_PATH) then
 endif
 
 # Darwin
-if ($?DYLD_LIBRARY_PATH) then
-    if ("${DYLD_LIBRARY_PATH}" == "") unsetenv DYLD_LIBRARY_PATH
+if ($?FOAM_LD_LIBRARY_PATH) then
+    if ("${FOAM_LD_LIBRARY_PATH}" == "") unsetenv DYLD_LIBRARY_PATH FOAM_LD_LIBRARY_PATH
 endif
 
 
diff --git a/etc/config.csh/unset b/etc/config.csh/unset
index ce5a2e818ac..2c17414c68e 100644
--- a/etc/config.csh/unset
+++ b/etc/config.csh/unset
@@ -6,7 +6,7 @@
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 #     Copyright (C) 2011-2016 OpenFOAM Foundation
-#     Copyright (C) 2016-2021 OpenCFD Ltd.
+#     Copyright (C) 2016-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -172,8 +172,10 @@ if ( $?foamClean ) then
     if ($?LD_LIBRARY_PATH) then
         eval `$foamClean -csh-env=LD_LIBRARY_PATH "$foamOldDirs"`
     endif
-    if ($?DYLD_LIBRARY_PATH) then
-        eval `$foamClean -csh-env=DYLD_LIBRARY_PATH "$foamOldDirs"`
+    # Darwin
+    if ($?FOAM_LD_LIBRARY_PATH) then
+        eval `$foamClean -csh-env=FOAM_LD_LIBRARY_PATH "$foamOldDirs"`
+        setenv DYLD_LIBRARY_PATH ${FOAM_LD_LIBRARY_PATH}
     endif
 endif
 
@@ -188,7 +190,7 @@ if ($?DYLD_LIBRARY_PATH) then
 endif
 
 # Remove any shadow env variables
-unsetenv FOAM_DYLD_LIBRARY_PATH
+unsetenv FOAM_DYLD_LIBRARY_PATH FOAM_LD_LIBRARY_PATH
 
 #------------------------------------------------------------------------------
 # Cleanup aliases
diff --git a/etc/config.sh/functions b/etc/config.sh/functions
index 2707f3a54c0..521582f848e 100644
--- a/etc/config.sh/functions
+++ b/etc/config.sh/functions
@@ -6,7 +6,7 @@
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 #     Copyright (C) 2011-2016 OpenFOAM Foundation
-#     Copyright (C) 2017-2021 OpenCFD Ltd.
+#     Copyright (C) 2017-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -31,7 +31,7 @@
 #     _foamEtc       : resolve etc files (silent or verbose)
 #     _foamAddPath   : prepend to PATH
 #     _foamAddMan    : prepend to MANPATH
-#     _foamAddLib    : prepend to [DY]LD_LIBRARY_PATH
+#     _foamAddLib    : prepend to {DY,FOAM_}LD_LIBRARY_PATH
 #     _foamAddLibAuto: prepend to lib64/lib resolved name
 #
 #------------------------------------------------------------------------------
@@ -56,7 +56,7 @@ then
          foamVar_name="$1"
          shift
          eval "$($foamClean -sh-env="$foamVar_name" "$@")"
-         unset "foamVar_name"
+         unset foamVar_name
     }
 
     # Echo values to stderr when FOAM_VERBOSE is on, no-op otherwise
@@ -94,14 +94,18 @@ then
         _foamAddLib()
         {
             case "$1" in (/?*)
-                export DYLD_LIBRARY_PATH="${1}${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH}" ;;
+            if [ -e "$1" ]
+            then
+                export FOAM_LD_LIBRARY_PATH="${1}${FOAM_LD_LIBRARY_PATH:+:}${FOAM_LD_LIBRARY_PATH}"
+                export DYLD_LIBRARY_PATH="$FOAM_LD_LIBRARY_PATH"
+            fi
             esac
         }
     else
         _foamAddLib()
         {
             case "$1" in (/?*)
-                export LD_LIBRARY_PATH="${1}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}" ;;
+                export LD_LIBRARY_PATH="${1}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}"
             esac
         }
     fi
diff --git a/etc/config.sh/paraview b/etc/config.sh/paraview
index cf9e43812bf..8a933e4dc98 100644
--- a/etc/config.sh/paraview
+++ b/etc/config.sh/paraview
@@ -76,6 +76,12 @@ eval \
     "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-lib \
     $ParaView_DIR $archDir/ParaView- $archDir/qt-)"
 
+# Darwin
+case "$WM_ARCH" in
+(darwin*)
+    export DYLD_LIBRARY_PATH="${FOAM_LD_LIBRARY_PATH}" ;;
+esac
+
 # Evaluate command-line parameters for ParaView
 for i
 do
@@ -186,8 +192,12 @@ case "$ParaView_VERSION" in
         then
             case "$WM_ARCH" in
             (darwin*)
-                export DYLD_LIBRARY_PATH="${pv_libdirs}:$DYLD_LIBRARY_PATH" ;;
-            (*) export LD_LIBRARY_PATH="${pv_libdirs}:$LD_LIBRARY_PATH" ;;
+                export FOAM_LD_LIBRARY_PATH="${pv_libdirs}:${FOAM_LD_LIBRARY_PATH}"
+                export DYLD_LIBRARY_PATH="${FOAM_LD_LIBRARY_PATH}"
+                ;;
+            (*)
+                export LD_LIBRARY_PATH="${pv_libdirs}:${LD_LIBRARY_PATH}"
+                ;;
             esac
         fi
 
diff --git a/etc/config.sh/paraview-system b/etc/config.sh/paraview-system
index 56b29377eaa..dd06b7546fe 100644
--- a/etc/config.sh/paraview-system
+++ b/etc/config.sh/paraview-system
@@ -5,7 +5,7 @@
 #   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
-#     Copyright (C) 2019-2021 OpenCFD Ltd.
+#     Copyright (C) 2019-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -34,6 +34,12 @@ eval \
     "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-lib \
     $ParaView_DIR $archDir/ParaView-)"
 
+# Darwin
+case "$WM_ARCH" in
+(darwin*)
+    export DYLD_LIBRARY_PATH="${FOAM_LD_LIBRARY_PATH}" ;;
+esac
+
 #------------------------------------------------------------------------------
 
 ParaView_DIR="$(command -v paraview 2>/dev/null)"
diff --git a/etc/config.sh/setup b/etc/config.sh/setup
index e7576329663..837eb90de42 100644
--- a/etc/config.sh/setup
+++ b/etc/config.sh/setup
@@ -5,7 +5,7 @@
 #   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
-#     Copyright (C) 2018-2021 OpenCFD Ltd.
+#     Copyright (C) 2018-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -178,12 +178,23 @@ else
 fi
 
 
-# Clean standard environment variables
+# Clean standard environment variables (PATH, MANPATH, [DY]LD_LIBRARY_PATH)
+
+export PATH MANPATH
+if [ "${_foam_uname_s}" = Darwin ]
+then
+    export DYLD_LIBRARY_PATH FOAM_LD_LIBRARY_PATH
+else
+    export LD_LIBRARY_PATH
+fi
 
-export PATH MANPATH LD_LIBRARY_PATH
 _foamClean PATH "$foamOldDirs"
 _foamClean MANPATH "$foamOldDirs"
 _foamClean -lib "$foamOldDirs"
+if [ "${_foam_uname_s}" = Darwin ]
+then
+    export DYLD_LIBRARY_PATH="$FOAM_LD_LIBRARY_PATH"
+fi
 
 #------------------------------------------------------------------------------
 # Base setup (OpenFOAM compilation), MPI and third-party packages
@@ -237,11 +248,21 @@ fi
 #------------------------------------------------------------------------------
 
 # Remove duplicates from environment paths
-export PATH MANPATH LD_LIBRARY_PATH
+export PATH MANPATH
+if [ "${_foam_uname_s}" = Darwin ]
+then
+    export DYLD_LIBRARY_PATH FOAM_LD_LIBRARY_PATH
+else
+    export LD_LIBRARY_PATH
+fi
 
 _foamClean PATH
 _foamClean MANPATH
 _foamClean -lib
+if [ "${_foam_uname_s}" = Darwin ]
+then
+    export DYLD_LIBRARY_PATH="$FOAM_LD_LIBRARY_PATH"
+fi
 
 # Add trailing ':' for system manpages
 if [ -n "$MANPATH" ]
diff --git a/etc/config.sh/unset b/etc/config.sh/unset
index ebc9c38c7cb..318291b0d25 100644
--- a/etc/config.sh/unset
+++ b/etc/config.sh/unset
@@ -6,7 +6,7 @@
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 #     Copyright (C) 2011-2016 OpenFOAM Foundation
-#     Copyright (C) 2016-2021 OpenCFD Ltd.
+#     Copyright (C) 2016-2022 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -164,7 +164,9 @@ then
     eval "$($foamClean -sh-env=PATH $foamOldDirs)"
     eval "$($foamClean -sh-env=MANPATH $foamOldDirs)"
     eval "$($foamClean -sh-env=LD_LIBRARY_PATH $foamOldDirs)"
-    eval "$($foamClean -sh-env=DYLD_LIBRARY_PATH $foamOldDirs)"
+    # Darwin
+    eval "$($foamClean -sh-env=FOAM_LD_LIBRARY_PATH $foamOldDirs)"
+    export DYLD_LIBRARY_PATH="$FOAM_LD_LIBRARY_PATH"
 fi
 
 [ -n "$MANPATH" ] || unset MANPATH
@@ -172,7 +174,7 @@ fi
 [ -n "$DYLD_LIBRARY_PATH" ] || unset DYLD_LIBRARY_PATH
 
 # Remove any shadow env variables
-unset FOAM_DYLD_LIBRARY_PATH
+unset FOAM_DYLD_LIBRARY_PATH FOAM_LD_LIBRARY_PATH
 
 #------------------------------------------------------------------------------
 # Cleanup aliases and functions
diff --git a/src/meshTools/triSurface/triSurfaceTools/geompack/geompack.C b/src/meshTools/triSurface/triSurfaceTools/geompack/geompack.C
index 7298db6060b..b586baf85d1 100644
--- a/src/meshTools/triSurface/triSurfaceTools/geompack/geompack.C
+++ b/src/meshTools/triSurface/triSurfaceTools/geompack/geompack.C
@@ -6,6 +6,10 @@
 # include <ctime>
 # include <cstring>
 
+#if defined(__APPLE__) && defined(__clang__)
+#pragma clang fp exceptions(ignore)
+#endif
+
 using namespace std;
 
 # include "geompack.H"
-- 
GitLab