From dd1c2edb6b744553b6f4a706b590fd4514e7cf09 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 12 Aug 2020 12:15:27 +0200
Subject: [PATCH] COMP: wmake findObjectDir fails for non-project directories
 (fixes #1807)

- experienced while reusing src/Pstream/Allwmake-mpi to create
  additional mpi-layers after installation. Since the copied sources
  are not located within the OpenFOAM source-tree (and/or the
  source-tree is non-writable), it should not and does not use the
  central build/WM_OPTIONS directory.

  However, when exploring for the appropriate local Make directory, it
  searched for the current '.' directory instead of checking for the
  resolved directory.

  This fails, since there is no src/Pstream/Make directory.
  Must check for src/Pstream/mpi/Make directory first!

- Adjust wclean to always remove a local build directory
  (Make/WM_OPTIONS) for additional safety.
  After which, attempt to remove central build/WM_OPTIONS version too.
---
 wmake/scripts/wmakeFunctions | 17 +++++++++++------
 wmake/wclean                 | 20 +++++++++++---------
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/wmake/scripts/wmakeFunctions b/wmake/scripts/wmakeFunctions
index 72322448f63..89134c835a4 100644
--- a/wmake/scripts/wmakeFunctions
+++ b/wmake/scripts/wmakeFunctions
@@ -173,13 +173,18 @@ findObjectDir()
     # Default (local) build directory
     if [ -z "$objectsDir" ]
     then
-        relativeDir="$absdir"
-        appDir=.
-        [ -d Make ] || appDir=$(findTarget .) || exit 1 # Fatal
-        absdir=$(expandPath "$appDir"/.)
+        if [ -d "$absdir/Make" ]
+        then
+            objectsDir="${absdir}/Make/${WM_OPTIONS}"
+        else
+            relativeDir="$absdir"
+            appDir=.
+            [ -d Make ] || appDir=$(findTarget .) || exit 1 # Fatal
+            absdir=$(expandPath "$appDir"/.)
 
-        relativeDir="${relativeDir#${absdir}}"
-        objectsDir="${appDir}/Make/${WM_OPTIONS}${relativeDir}"
+            relativeDir="${relativeDir#${absdir}}"
+            objectsDir="${appDir}/Make/${WM_OPTIONS}${relativeDir}"
+        fi
     fi
 
     echo "$objectsDir"
diff --git a/wmake/wclean b/wmake/wclean
index 53712bfadd2..ca2fe7f81c0 100755
--- a/wmake/wclean
+++ b/wmake/wclean
@@ -313,16 +313,18 @@ fi
 # Clean the 'Make' directory if present
 #------------------------------------------------------------------------------
 
-if [ -d "$MakeDir" ]
+if [ -d "$MakeDir" ] && [ -n "$WM_OPTIONS" ]
 then
-    objectsDir="$MakeDir/$WM_OPTIONS"
-    case "$PWD" in
-    ("$WM_PROJECT_DIR"/*)
-        buildPath="$WM_PROJECT_DIR/build/${WM_OPTIONS}"
-        objectsDir=$buildPath$(echo $PWD | sed s%$WM_PROJECT_DIR%% )
-        ;;
-    esac
-    rm -rf "$objectsDir" 2>/dev/null
+    # Remove in-source directory (if any)
+    rm -rf "$MakeDir/$WM_OPTIONS" 2>/dev/null
+
+    # Remove out-of-source directory (if applicable)
+    relativeDir="${PWD#${WM_PROJECT_DIR}/}"
+    if [ "$relativeDir" != "$PWD" ]
+    then
+        objectsDir="${WM_PROJECT_DIR}/build/${WM_OPTIONS}/${relativeDir}"
+        rm -rf "$objectsDir" 2>/dev/null
+    fi
 fi
 
 #------------------------------------------------------------------------------
-- 
GitLab