From 3d02c8a5305aaf0cfafecfcc712404807fdb9477 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Fri, 10 Feb 2017 16:13:54 +0100
Subject: [PATCH] ENH: improve isolation of shell variables in wmake scripts

- reduces unexpected interactions between various make elements
---
 .../graphics/PVReaders/PVFoamReader/Allwclean |   5 +-
 .../PVReaders/PVblockMeshReader/Allwclean     |   5 +-
 src/Pstream/Allwmake                          |   7 +-
 .../graphics/runTimePostProcessing/Allwclean  |   5 +-
 src/parallel/decompose/Allwclean              |   2 -
 src/parallel/decompose/Allwmake               |   9 +-
 wmake/scripts/cmakeFunctions                  |  46 ++---
 wmake/scripts/wmakeFunctions                  | 186 ++++++++++--------
 wmake/wclean                                  |  71 ++++---
 wmake/wcleanPlatform                          |   4 +-
 wmake/wdep                                    |   5 +-
 wmake/wmake                                   |  14 +-
 wmake/wrmdep                                  |  19 +-
 wmake/wrmo                                    |  10 +-
 14 files changed, 202 insertions(+), 186 deletions(-)

diff --git a/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/Allwclean b/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/Allwclean
index 4df5195cb12..9b1370c3ff8 100755
--- a/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/Allwclean
+++ b/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/Allwclean
@@ -10,8 +10,7 @@ rm -f $FOAM_LIBBIN/libPVFoamReader* 2>/dev/null
 rm -rf PVFoamReader/Make        # safety: old build location
 wclean libso vtkPVFoam
 
-# Cleanup generated files
-findObjectDir $PWD # remove entire top-level
-rm -rf "$objectsDir" > /dev/null 2>&1
+# Cleanup generated files - remove entire top-level
+removeObjectDir $PWD
 
 #------------------------------------------------------------------------------
diff --git a/applications/utilities/postProcessing/graphics/PVReaders/PVblockMeshReader/Allwclean b/applications/utilities/postProcessing/graphics/PVReaders/PVblockMeshReader/Allwclean
index 8e0fd1a41a0..b49fed7d2d1 100755
--- a/applications/utilities/postProcessing/graphics/PVReaders/PVblockMeshReader/Allwclean
+++ b/applications/utilities/postProcessing/graphics/PVReaders/PVblockMeshReader/Allwclean
@@ -10,8 +10,7 @@ rm -f $FOAM_LIBBIN/libPVblockMeshReader* 2>/dev/null
 rm -rf PVblockMeshReader/Make   # safety: old build location
 wclean libso vtkPVblockMesh
 
-# Cleanup generated files
-findObjectDir $PWD # remove entire top-level
-rm -rf "$objectsDir" > /dev/null 2>&1
+# Cleanup generated files - remove entire top-level
+removeObjectDir $PWD
 
 #------------------------------------------------------------------------------
diff --git a/src/Pstream/Allwmake b/src/Pstream/Allwmake
index d32bc5503d7..c752d8220ff 100755
--- a/src/Pstream/Allwmake
+++ b/src/Pstream/Allwmake
@@ -10,16 +10,17 @@ cd ${0%/*} || exit 1    # Run from this directory
 # use sentinel file to handle version changes
 wmakeMpiLib()
 {
+    local objectsDir
     for libName
     do
     (
         WM_OPTIONS="$WM_OPTIONS$WM_MPLIB"
-        libDir="$WM_PROJECT_DIR/platforms/$WM_OPTIONS/src/Pstream/$libName"
-        whichmpi="$libDir/using:$FOAM_MPI"
+        objectsDir="$WM_PROJECT_DIR/platforms/$WM_OPTIONS/src/Pstream/$libName"
+        whichmpi="$objectsDir/using:$FOAM_MPI"
         [ -e "$whichmpi" ] || wclean $libName
         echo "wmake $targetType $libName"
         wmake $targetType $libName
-        mkdir -p "$libDir"
+        mkdir -p "$objectsDir"
         touch "$whichmpi"
     )
     done
diff --git a/src/functionObjects/graphics/runTimePostProcessing/Allwclean b/src/functionObjects/graphics/runTimePostProcessing/Allwclean
index 00ff12fadbb..7a118a43495 100755
--- a/src/functionObjects/graphics/runTimePostProcessing/Allwclean
+++ b/src/functionObjects/graphics/runTimePostProcessing/Allwclean
@@ -7,8 +7,7 @@ cd ${0%/*} || exit 1    # Run from this directory
 # Cleanup library
 rm -f $FOAM_LIBBIN/librunTimePostProcessing* 2>/dev/null
 
-# Cleanup generated files
-findObjectDir $PWD # remove entire top-level
-rm -rf "$objectsDir" > /dev/null 2>&1
+# Cleanup generated files - remove entire top-level
+removeObjectDir $PWD
 
 #------------------------------------------------------------------------------
diff --git a/src/parallel/decompose/Allwclean b/src/parallel/decompose/Allwclean
index e0acdabc153..ef6dc412124 100755
--- a/src/parallel/decompose/Allwclean
+++ b/src/parallel/decompose/Allwclean
@@ -20,8 +20,6 @@ wcleanMpiLib()
     do
     (
         WM_OPTIONS="$WM_OPTIONS$WM_MPLIB"
-        whichmpi="$WM_PROJECT_DIR/platforms/$WM_OPTIONS/src/parallel/decompose/$libName/using:$FOAM_MPI"
-        whichscotch="$WM_PROJECT_DIR/platforms/$WM_OPTIONS/src/parallel/decompose/$libName/using:$SCOTCH_VERSION"
         wclean $libName
     )
     done
diff --git a/src/parallel/decompose/Allwmake b/src/parallel/decompose/Allwmake
index 6bddc475ea6..82e965e79cf 100755
--- a/src/parallel/decompose/Allwmake
+++ b/src/parallel/decompose/Allwmake
@@ -126,18 +126,19 @@ hasScotch()
 wmakeMpiLib()
 {
     local decompName="$1"
+    local objectsDir
     shift
     for libName
     do
     (
         WM_OPTIONS="$WM_OPTIONS$WM_MPLIB"
-        libDir="$WM_PROJECT_DIR/platforms/$WM_OPTIONS/src/parallel/decompose/$libName"
-        whichmpi="$libDir/using:$FOAM_MPI"
-        whichdecomp="$libDir/using:$decompName"
+        objectsDir="$WM_PROJECT_DIR/platforms/$WM_OPTIONS/src/parallel/decompose/$libName"
+        whichmpi="$objectsDir/using:$FOAM_MPI"
+        whichdecomp="$objectsDir/using:$decompName"
         [ -e "$whichmpi" -a -e "$whichdecomp" ] || wclean $libName
         echo "wmake $targetType $libName"
         wmake $targetType $libName
-        mkdir -p "$libDir"
+        mkdir -p "$objectsDir"
         touch "$whichdecomp" "$whichmpi"
     )
     done
diff --git a/wmake/scripts/cmakeFunctions b/wmake/scripts/cmakeFunctions
index a95e686aea1..b78d7c34aa1 100644
--- a/wmake/scripts/cmakeFunctions
+++ b/wmake/scripts/cmakeFunctions
@@ -44,12 +44,15 @@
 sameDependency()
 {
     local depend="$1"
-    findObjectDir "$2"  # Where generated files are stored
-    local sentinel="$objectsDir/ThirdParty"
+    local sourceDir="$2"
+    local objectsDir sentinel prev
+
+    # Where generated files are stored
+    objectsDir=$(findObjectDir "$sourceDir") || exit 1 # Fatal
+    sentinel="$objectsDir/ThirdParty"
 
     echo $sentinel
 
-    local prev
     if read -r prev 2>/dev/null < $sentinel
     then
         if [ "$prev" = "$depend" ]
@@ -70,35 +73,24 @@ sameDependency()
 
 
 # CMake into objectsDir with external dependency
+# - use sentinel file(s) to handle paraview/vtk version changes
 cmakeVersioned()
 {
     local depend="$1"
     local sourceDir="$2"
-    findObjectDir $sourceDir # Where are generated files stored?
+    local objectsDir sentinel
 
-    local sentinel
+    # Where generated files are stored
+    objectsDir=$(findObjectDir "$sourceDir") || exit 1 # Fatal
 
-    # version changed
-    sentinel=$(sameDependency "$depend" "$sourceDir") \
-        || rm -rf "$objectsDir" > /dev/null 2>&1
-
-    test -f "$objectsDir/CMakeCache.txt"
-    retry=$? # Additional attempt if sources moved
+    # Version changed
+    sentinel=$(sameDependency "$depend" "$sourceDir") || \
+        rm -rf "$objectsDir" > /dev/null 2>&1
 
     mkdir -p $objectsDir && \
     (
-        cd $objectsDir || exit 1
-
-        cmake $sourceDir || {
-            if [ $retry -eq 0 ]
-            then
-                echo "Removing CMakeCache.txt and attempt again" 1>&2
-                rm -f CMakeCache.txt
-                cmake $sourceDir
-            else
-                exit 1
-            fi
-        } && make && { echo "$depend" > $sentinel; }
+        cd $objectsDir && cmake $sourceDir && make \
+     && echo "$depend" > ${sentinel:-/dev/null}
     )
 }
 
@@ -127,9 +119,11 @@ wmakeLibPv()
 
     for libName
     do
-        # version changed
-        sentinel=$(sameDependency "$depend" $libName) || wclean $libName
-        wmake $targetType $libName && { echo "$depend" > $sentinel; }
+        sentinel=$(sameDependency "$depend" $libName) || \
+            wclean $libName
+
+        wmake $targetType $libName \
+        && echo "$depend" > ${sentinel:-/dev/null}
     done
 }
 
diff --git a/wmake/scripts/wmakeFunctions b/wmake/scripts/wmakeFunctions
index 4ed0b7b0f38..ee9a2067bd8 100644
--- a/wmake/scripts/wmakeFunctions
+++ b/wmake/scripts/wmakeFunctions
@@ -28,6 +28,11 @@
 #     Functions to check wmake environment and find .dep and .o files
 #------------------------------------------------------------------------------
 
+# Ensure these variables are always defined
+MakeDir=Make
+: ${Script:=wmakeFunctions}
+
+
 #------------------------------------------------------------------------------
 # Check environment variables
 #------------------------------------------------------------------------------
@@ -52,78 +57,83 @@ checkEnv()
 # expandPath dirName
 # expandPath fileName
 #
-# Sets:
-# - exPath
+# Output:
+# - the expanded path name
 expandPath()
 {
     if [ -d "$1" ]
     then
-        exPath=$(cd "$1" && pwd -P)
+        (cd "$1" && pwd -P)
+    elif [ -n "$1" ]
+    then
+        (cd $(dirname "$1") && pwd -P)
     else
-        exPath=$(cd $(dirname "$1") && pwd -P)
+        pwd -P
     fi
 }
 
-# Find the target directory
-# findTarget dirName
-# findTarget fileName
+# Find the target directory, which contains a Make/ directory
+# search upwards in its parent directories, but stopping
+# when it hits the project root, home, or the file-system root
 #
-# Sets:
-# - dir
+# findTarget dirName
 #
-# Side-effect variables:
-# - sets exPath
-# - sets wmpdir
+# Output:
+# - the relative target directory
 #
 # Global variables used:
 # - WM_PROJECT_DIR, HOME
 findTarget()
 {
-    expandPath $WM_PROJECT_DIR
-    wmpdir=$exPath
-    expandPath $1
-
-    if [ "$exPath" = "$wmpdir" \
-      -o "$exPath" = "$HOME" \
-      -o "$exPath" = "/" \
-    ]
-    then
-        echo "$Script error: could not find Make directory" 1>&2
-        exit 1
-    elif [ -d "$1/Make" ]
-    then
-        dir=$1
-    else
-        findTarget "$1/.."
-    fi
+    local wmpdir=$(expandPath $WM_PROJECT_DIR)
+    local home=$(expandPath $HOME)
+    local reldir="${1:-.}"
+    local absdir=$(expandPath $reldir)
+
+    while [ -n "$absdir" ]
+    do
+        case "$absdir" in
+        ($wmpdir | $home | /)
+            break
+            ;;
+        esac
+
+        if [ -d "$reldir/Make" ]
+        then
+            echo "$reldir"
+            return 0
+        else
+            # Check parent directory
+            absdir="${absdir%/*}"
+            reldir="$reldir/.."
+        fi
+    done
+
+    echo "Error: no Make directory for $(expandPath $1)" 1>&2
+    echo 1>&2
+    return 1
 }
 
-# Change to 'MakeDir'
-# - 'MakeDir' for its input
-#
-# Sets:
-# - dir
+
+# Change to 'MakeDir' parent
+# - uses 'MakeDir' for its input
 #
-# Side-effect variables:
-# - sets exPath
+# Side-effects:
 # - unsets targetType
 cdSource()
 {
-    if [ ! -d $MakeDir ]
+    local dir
+    if [ ! -d "$MakeDir" ]
     then
         echo "$Script: '$MakeDir' directory does not exist in $PWD" 1>&2
         echo "    Searching up directories tree for Make directory" 1>&2
-        unset targetType
-
-        findTarget .
 
-        if [ "$dir" ]
-        then
-            cd $dir 2>/dev/null || {
-                echo "$Script error: could not change to directory '$dir'" 1>&2
-                exit 1
-            }
-        fi
+        dir=$(findTarget .) || exit 1 # Fatal
+        cd $dir 2>/dev/null || {
+            echo "$Script error: could not change to directory '$dir'" 1>&2
+            exit 1
+        }
+        unset targetType
     fi
 
     [ -r $MakeDir/files ] || {
@@ -137,48 +147,58 @@ cdSource()
 # findObjectDir dirName
 # findObjectDir fileName
 #
-# Sets:
-# - dir
-# - path
-# - appDir
-# - objectsDir
-#
-# Side-effect variables:
-# - sets exPath
-# - sets wmpdir
-# - set platformPath
+# Output:
+# - the objectsDir
 #
 # Global variables used:
 # - WM_PROJECT_DIR, WM_OPTIONS
 findObjectDir()
 {
-    expandPath $WM_PROJECT_DIR
-    wmpdir=$exPath
-    expandPath $1
+    local wmpdir=$(expandPath $WM_PROJECT_DIR)
+    local exPath=$(expandPath ${1:-.})
+    local objectsDir
+
+    case "$exPath" in
+    ("$wmpdir"/*)
+        local buildPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
+        objectsDir=$buildPath$(echo $exPath | sed s%$wmpdir%% )
+        ;;
+    (*)
+        local path=$exPath
+        local appDir=.
+        [ -d Make ] || appDir=$(findTarget .) || exit 1 # Fatal
+        exPath=$(expandPath $appDir/.)
+        objectsDir=$appDir/Make/${WM_OPTIONS}$(echo $path | sed s%$exPath%% )
+    ;;
+    esac
+
+    echo "$objectsDir"
+}
+
 
-    if echo $exPath | grep "$wmpdir" > /dev/null
+# Find the object directory and remove it
+# removeObjectDir dirName
+# removeObjectDir fileName
+#
+# Output:
+# - NONE
+#
+# Global variables used:
+# - WM_PROJECT_DIR, WM_OPTIONS
+removeObjectDir()
+{
+    local objectsDir=$(findObjectDir ${1:-.})
+    if [ -d "$objectsDir" ]
     then
-        platformPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
-        objectsDir=$platformPath$(echo $exPath | sed s%$wmpdir%% )
-    else
-        path=$exPath
-        dir=.
-        if [ ! -d Make ]
-        then
-            findTarget .
-        fi
-        appDir=$dir
-        expandPath $appDir/.
-        objectsDir=$appDir/Make/${WM_OPTIONS}$(echo $path | sed s%$exPath%% )
+        rm -rf "$objectsDir" > /dev/null 2>&1
     fi
 }
 
 
-# depToSource
-# - uses 'depFile' for its input
+# depToSource depFile
 #
-# Sets:
-# - sourceFile
+# Output:
+# - the sourceFile corresponding to depFile
 #
 # Global variables used:
 # - WM_OPTIONS
@@ -187,20 +207,24 @@ if [ -n "$BASH_VERSION" ]
 then
     depToSource()
     {
-        sourceFile=${depFile%.dep}
+        local sourceFile=${1%.dep}
         sourceFile="${sourceFile/platforms\/${WM_OPTIONS}\//}"
-        sourceFile="${sourceFile/Make\/${WM_OPTIONS}\//}"
         sourceFile="${sourceFile/platforms\/${WM_OPTIONS}${WM_MPLIB}\//}"
+        sourceFile="${sourceFile/Make\/${WM_OPTIONS}\//}"
         sourceFile="${sourceFile/Make\/${WM_OPTIONS}${WM_MPLIB}\//}"
+
+        echo "$sourceFile"
     }
 else
     depToSource()
     {
-        sourceFile=$(echo ${depFile%.dep} |                 \
+        local sourceFile=$(echo ${1%.dep} |                 \
             sed -e s%platforms/${WM_OPTIONS}/%%             \
-                -e s%Make/${WM_OPTIONS}/%%                  \
                 -e s%platforms/${WM_OPTIONS}${WM_MPLIB}/%%  \
+                -e s%Make/${WM_OPTIONS}/%%                  \
                 -e s%Make/${WM_OPTIONS}${WM_MPLIB}/%% )
+
+        echo "$sourceFile"
     }
 fi
 
diff --git a/wmake/wclean b/wmake/wclean
index 42209bc2bb8..c41f82c2b66 100755
--- a/wmake/wclean
+++ b/wmake/wclean
@@ -47,6 +47,7 @@ Usage: $Script [OPTION] [dir]
        $Script [OPTION] target [dir [MakeDir]]
 
 options:
+  -a | -all         Same as the 'all' target
   -s | -silent      Ignored - for compatibility with wmake
   -help             Print the usage
 
@@ -67,19 +68,23 @@ USAGE
     exit 1
 }
 
-
 #------------------------------------------------------------------------------
 # Parse arguments and options
 #------------------------------------------------------------------------------
 
+unset dir targetType
+MakeDir=Make
+
 while [ "$#" -gt 0 ]
 do
     case "$1" in
     -h | -help)
         usage
         ;;
+    -a | -all | all)
+        targetType=all
+        ;;
     -s | -silent)    # Ignored - for compatibility with wmake
-        shift
         ;;
     -*)
         usage "unknown option: '$1'"
@@ -88,16 +93,14 @@ do
         break
         ;;
     esac
+    shift
 done
 
-
 #------------------------------------------------------------------------------
-# Check arguments and change to the directory in which to run wclean
+# Check arguments and change to the directory in which to run wclean.
+# The variables 'targetType' and 'MakeDir' are considered global
 #------------------------------------------------------------------------------
 
-unset dir targetType
-MakeDir=Make
-
 if [ $# -ge 1 ]
 then
     if [ -d "$1" ]
@@ -132,7 +135,7 @@ fi
 
 #------------------------------------------------------------------------------
 # If target not specified search up the directory tree for the Make
-# sub-directory, check the existance of the 'files' file and clean there if
+# sub-directory, check the existence of the 'files' file and clean there if
 # present
 # ------------------------------------------------------------------------------
 
@@ -159,12 +162,11 @@ then
     # Second pass: clean up object directories with WM_PROJECT_DIR that don't
     # have respective source code folders, along with the respective binaries
 
-    expandPath $PWD
-    if [ "$exPath" = "$WM_PROJECT_DIR" ]
+    if [ "$(expandPath $PWD)" = "$WM_PROJECT_DIR" ]
     then
-        findObjectDir $PWD
+        objectsDir=$(findObjectDir $PWD) || exit 1 # Fatal
 
-        if [ -d $objectsDir ]
+        if [ -d "$objectsDir" ]
         then
             echo "    Removing redundant object directories in $objectsDir"
 
@@ -173,7 +175,7 @@ then
             do
                 # Hack'ish way of getting the original source code path
                 depFile=$(dirname $variablesFile)
-                depToSource $depFile
+                sourceFile=$(depToSource $depFile)
 
                 # Check if the original source code directory exists
                 if [ ! -r "$sourceFile" ]
@@ -227,43 +229,40 @@ then
     then
         ./Allclean
         exit $?
-    else
-        # For all the sub-directories containing a 'Make' directory
-        for dir in `find . \( -type d -a -name Make \)`
-        do
-            dir=${dir%/Make} # Parent directory - trim /Make from the end
-
-            # If Allwclean exists execute otherwise wclean
-            if [ -e "$dir/Allwclean" ]
-            then
-                $dir/Allwclean
-            else
-                $0 $dir
-            fi
-        done
     fi
-fi
 
-# targetType is not needed beyond this point
-unset targetType
+    # For all sub-directories containing a 'Make' directory
+    for dir in $(find . -type d -name Make)
+    do
+        dir=${dir%/*} # Parent directory containing the Make directory
+
+        # Use Allwclean if it exists, otherwise wclean
+        if [ -e "$dir/Allwclean" ]
+        then
+            $dir/Allwclean
+        else
+            $0 $dir
+        fi
+    done
+fi
 
 
 #------------------------------------------------------------------------------
 # Clean the 'Make' directory if present
 #------------------------------------------------------------------------------
 
-if [ -d $MakeDir ]
+if [ -d "$MakeDir" ]
 then
     objectsDir=$MakeDir/$WM_OPTIONS
-    if [ $(echo $PWD | grep "$WM_PROJECT_DIR") ]
-    then
+    case "$PWD" in
+    ("$WM_PROJECT_DIR"/*)
         buildPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
-        objectsDir=$buildPath$(echo $PWD | sed s%$WM_PROJECT_DIR%%)
-    fi
+        objectsDir=$buildPath$(echo $PWD | sed s%$WM_PROJECT_DIR%% )
+        ;;
+    esac
     rm -rf $objectsDir 2>/dev/null
 fi
 
-
 #------------------------------------------------------------------------------
 # Remove the lnInclude directory if present
 #------------------------------------------------------------------------------
diff --git a/wmake/wcleanPlatform b/wmake/wcleanPlatform
index 36605bdf552..18b4d5b2c39 100755
--- a/wmake/wcleanPlatform
+++ b/wmake/wcleanPlatform
@@ -103,10 +103,10 @@ else
             [ -n "$platform" ] || continue
         fi
 
-        if [ -d platforms/${platform} ]
+        if [ -d "platforms/$platform" ]
         then
             echo "Cleaning platform $platform"
-            rm -rf platforms/${platform}*
+            rm -rf "platforms/$platform"*
         else
             echo "Platform $platform not built"
         fi
diff --git a/wmake/wdep b/wmake/wdep
index dd20f21a97a..c7aaa47df7c 100755
--- a/wmake/wdep
+++ b/wmake/wdep
@@ -4,7 +4,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2014-2015 OpenFOAM Foundation
-#    \\/     M anipulation  |
+#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 #-------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
@@ -105,8 +105,7 @@ fi
 # and echo path for the dep file corresponding to the specified source file
 #------------------------------------------------------------------------------
 
-findObjectDir $sourceFile
-echo "$objectsDir/${sourceFile##*/}.dep"
+echo "$(findObjectDir $sourceFile)/${sourceFile##*/}.dep"
 
 exit 0 # clean exit
 
diff --git a/wmake/wmake b/wmake/wmake
index e251b1e9f40..219bbf454e5 100755
--- a/wmake/wmake
+++ b/wmake/wmake
@@ -239,7 +239,8 @@ fi
 
 
 #------------------------------------------------------------------------------
-# Check arguments and change to the directory in which to run wmake
+# Check arguments and change to the directory in which to run wmake.
+# The variables 'targetType' and 'MakeDir' are considered global
 #------------------------------------------------------------------------------
 
 unset dir targetType
@@ -352,7 +353,7 @@ fi
 
 #------------------------------------------------------------------------------
 # Search up the directory tree for the Make sub-directory,
-# check the existance of the 'files' file and build there if present
+# check the existence of the 'files' file and build there if present
 #------------------------------------------------------------------------------
 
 cdSource
@@ -387,11 +388,12 @@ fi
 #------------------------------------------------------------------------------
 
 objectsDir=$MakeDir/$WM_OPTIONS
-if [ $(echo $PWD | grep "$WM_PROJECT_DIR") ]
-then
+case "$PWD" in
+("$WM_PROJECT_DIR"/*)
     buildPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
-    objectsDir=$buildPath$(echo $PWD | sed s%$WM_PROJECT_DIR%%)
-fi
+    objectsDir=$buildPath$(echo $PWD | sed s%$WM_PROJECT_DIR%% )
+    ;;
+esac
 
 (
     unset MAKEFLAGS
diff --git a/wmake/wrmdep b/wmake/wrmdep
index c2f648e74e5..1d37a1564c0 100755
--- a/wmake/wrmdep
+++ b/wmake/wrmdep
@@ -141,7 +141,7 @@ files)
     # Remove the selected .dep files from the object tree
     #-------------------------------------------------------------------------
 
-    findObjectDir .
+    objectsDir=$(findObjectDir .) || exit 1 # Fatal
 
     # With the -a/-all option replace the current platform with a wildcard
     if [ "$all" = all ]
@@ -152,14 +152,15 @@ files)
     if [ "$#" -eq 0 ]
     then
         echo "removing all .dep files ..."
-        find $objectsDir -name '*.dep' -print | xargs -t rm 2>/dev/null
+        find $objectsDir -type f -name '*.dep' -print | xargs -t rm 2>/dev/null
     else
         echo "removing .o files corresponding to"
         echo "    $@ ..."
         for file
         do
-            find $objectsDir -name '*.dep' -exec grep -q "$file" '{}' \; \
-                 -exec rm '{}' \; -print
+            find $objectsDir -type f -name '*.dep' \
+                -exec grep -q "$file" '{}' \; \
+                -exec rm '{}' \; -print
         done
     fi
     ;;
@@ -172,9 +173,9 @@ oldFolders)
 
     for checkDir
     do
-        findObjectDir $checkDir
+        objectsDir=$(findObjectDir $checkDir)
 
-        if [ -d $objectsDir ]
+        if [ -d "$objectsDir" ]
         then
             echo "    searching: $objectsDir"
         else
@@ -182,9 +183,9 @@ oldFolders)
             continue
         fi
 
-        find $objectsDir -name '*.dep' -print | while read depFile
+        find $objectsDir -type f -name '*.dep' -print | while read depFile
         do
-            depToSource $depFile
+            sourceFile=$(depToSource $depFile)
 
             # Check C++ or Flex source file exists
             if [ ! -r "$sourceFile" ]
@@ -204,7 +205,7 @@ updateMode)
     fi
 
     echo "Removing dep files corresponding to source files that no longer exist..."
-    fileNameList=$(find -L src applications -name '*.[CHL]' -type l)
+    fileNameList=$(find -L src applications -type l -name '*.[CHL]')
 
     for filePathAndName in $fileNameList
     do
diff --git a/wmake/wrmo b/wmake/wrmo
index 01610c0f108..489e179736d 100755
--- a/wmake/wrmo
+++ b/wmake/wrmo
@@ -62,7 +62,7 @@ USAGE
 #------------------------------------------------------------------------------
 
 # Default to processing only the current platform
-unset all
+unset platform
 
 while [ "$#" -gt 0 ]
 do
@@ -72,7 +72,7 @@ do
         ;;
     # All platforms
     -a | -all | all)
-        all=all
+        platform=all
         shift
         ;;
     -*)
@@ -92,10 +92,10 @@ checkEnv
 # Remove the selected .o files from the object tree
 #------------------------------------------------------------------------------
 
-findObjectDir .
+objectsDir=$(findObjectDir .) || exit 1 # Fatal
 
 # With the -a/-all option replace the current platform with a wildcard
-if [ "$all" = all ]
+if [ "$platform" = all ]
 then
     objectsDir=$(echo $objectsDir | sed s%$WM_OPTIONS%*% )
 fi
@@ -103,7 +103,7 @@ fi
 if [ "$#" -eq 0 ]
 then
     echo "removing all .o files ..."
-    find $objectsDir -name '*.o' -print | xargs -t rm 2>/dev/null
+    find $objectsDir -type f -name '*.o' -print | xargs -t rm 2>/dev/null
 else
     echo "removing .o files corresponding to"
     echo "    $@ ..."
-- 
GitLab