From cc9db3ea3a1cca5ada00860f9af6192d24728197 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 3 Apr 2020 18:52:10 +0200
Subject: [PATCH] BACKPORT: relocate wmake binaries into project
 platforms/tools (#1647)

- can aid when creating source-only or binary-only packages

- replace dirToString binary with shell/awk equivalent for simpler
  maintenance. The utility is very rarely used (auto scanning to
  create Make/files) so there is no performance difference.
---
 wmake/rules/General/general |   2 +-
 wmake/scripts/dirToString   |  92 +++++++++++++++++++++++++++++
 wmake/scripts/makeDepend    |   4 +-
 wmake/scripts/makeFiles     |  16 +++--
 wmake/scripts/makeOptions   |   6 +-
 wmake/scripts/makeTargetDir |   4 +-
 wmake/src/Allmake           |  35 +++++++++--
 wmake/src/Makefile          |  46 ++++++++-------
 wmake/src/dirToString.c     | 113 ------------------------------------
 9 files changed, 171 insertions(+), 147 deletions(-)
 create mode 100755 wmake/scripts/dirToString
 delete mode 100644 wmake/src/dirToString.c

diff --git a/wmake/rules/General/general b/wmake/rules/General/general
index 61a7b1ecc75..bf465c001ec 100644
--- a/wmake/rules/General/general
+++ b/wmake/rules/General/general
@@ -17,7 +17,7 @@ GLIB_LIBS  =
 COMPILER_TYPE   = $(shell echo $(WM_COMPILER) | tr -d [:digit:])
 DEFAULT_RULES   = $(WM_DIR)/rules/$(WM_ARCH)$(COMPILER_TYPE)
 RULES           = $(WM_DIR)/rules/$(WM_ARCH)$(WM_COMPILER)
-WMAKE_BIN       = $(WM_DIR)/platforms/$(WM_ARCH)$(WM_COMPILER)
+WMAKE_BIN       = $(WM_PROJECT_DIR)/platforms/tools/$(WM_ARCH)$(WM_COMPILER)
 
 ifeq ($(WM_SCHEDULER),)
     AND = &&
diff --git a/wmake/scripts/dirToString b/wmake/scripts/dirToString
new file mode 100755
index 00000000000..55d027e044f
--- /dev/null
+++ b/wmake/scripts/dirToString
@@ -0,0 +1,92 @@
+#!/bin/sh
+#------------------------------------------------------------------------------
+# =========                 |
+# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+#  \\    /   O peration     |
+#   \\  /    A nd           | www.openfoam.com
+#    \\/     M anipulation  |
+#------------------------------------------------------------------------------
+#     Copyright (C) 2019 OpenCFD Ltd.
+#------------------------------------------------------------------------------
+# License
+#     This file is part of OpenFOAM, licensed under GNU General Public License
+#     <http://www.gnu.org/licenses/>.
+#
+# Script
+#     dirToString
+#
+# Usage
+#     dirToString path/to/file
+#
+# Description
+#     Converts a directory path into a camelCase string.
+#     Leading [./] characters are stripped by default.
+#
+#     For example,
+#         input:  dir1/dir2/dir3
+#         output: dir1Dir2Dir3
+#
+#------------------------------------------------------------------------------
+usage() {
+    exec 1>&2
+    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
+    cat<<USAGE
+
+Usage: ${0##*/} [OPTION] dir
+
+options:
+  -no-strip         Do not ignore leading [./] characters
+  -strip            Ignore leading [./] characters (default)
+  -h, -help         Print the usage
+
+Converts a directory path into a camelCase string
+
+USAGE
+    exit 1
+}
+
+
+#------------------------------------------------------------------------------
+# Parse arguments and options
+#------------------------------------------------------------------------------
+
+optStrip=true
+
+while [ "$#" -gt 0 ]
+do
+    case "$1" in
+    # Print help
+    (-h | -help*)
+        usage
+        ;;
+    (-s | -strip)
+        optStrip=true
+        ;;
+    (-no-strip)
+        unset optStrip
+        ;;
+    (--)
+        shift
+        break
+        ;;
+    (*)
+        break
+        ;;
+    esac
+    shift
+done
+
+if [ -z "$optStrip" ]
+then
+    dirName="$1"
+else
+    # Ignore any leading ./ characters
+    dirName="$(echo ""$1"" | sed -e 's@^[./]*@@')"
+fi
+
+dirName=$(echo "$dirName" | \
+awk 'BEGIN{FS="";RS="/";ORS=""} {if (FNR>1) {$0=toupper(substr($0,1,1))substr($0,2)}} 1')
+
+echo "$dirName"
+
+#------------------------------------------------------------------------------
diff --git a/wmake/scripts/makeDepend b/wmake/scripts/makeDepend
index 97158eb89d7..1aa1f6f7e7e 100755
--- a/wmake/scripts/makeDepend
+++ b/wmake/scripts/makeDepend
@@ -3,9 +3,11 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+#   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
+#     Copyright (C) 2018 OpenCFD Ltd.
+#------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
 #     <http://www.gnu.org/licenses/>.
diff --git a/wmake/scripts/makeFiles b/wmake/scripts/makeFiles
index 07806c92387..cc48b4f4efb 100755
--- a/wmake/scripts/makeFiles
+++ b/wmake/scripts/makeFiles
@@ -3,9 +3,11 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
+#   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
+#     Copyright (C) 2011-2016 OpenFOAM Foundation
+#------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
 #
@@ -31,7 +33,7 @@
 #     Usage : makeFiles
 #
 #------------------------------------------------------------------------------
-dirToString="$WM_DIR/platforms/$WM_ARCH$WM_COMPILER"/dirToString
+dirToString="$WM_DIR/scripts/dirToString"
 
 if [ -r Make/files ]
 then
@@ -46,8 +48,10 @@ fi
 
 [ -d Make ] || mkdir Make
 rm -f Make/files
+
 #------------------------------------------------------------------------------
-echo "Creating Make/files"
+
+echo "Creating Make/files ..."
 
 for dir in $(find . -mindepth 1 -type d -print)
 do
@@ -56,7 +60,7 @@ do
         # Skip special directories
         ;;
     *)
-        echo "$(echo $dir | $dirToString -strip)  =  ${dir#./}"
+        echo "$($dirToString "$dir")  =  ${dir#./}"
         ;;
     esac
 done > Make/files
@@ -64,11 +68,11 @@ done > Make/files
 
 for file in $(find . -name "*.[cCylLfF]" -type f -print)
 do
-    pathName=$(echo ${file%/*} | $dirToString -strip)
+    pathName="$($dirToString "${file%/*}")"
 
     if [ -n "$pathName" ]
     then
-        echo '$('$pathName')/'"${file##*/}"
+        echo '$('"$pathName"')/'"${file##*/}"
     else
         echo "${file##*/}"
     fi
diff --git a/wmake/scripts/makeOptions b/wmake/scripts/makeOptions
index db1585485dd..3d27c475f04 100755
--- a/wmake/scripts/makeOptions
+++ b/wmake/scripts/makeOptions
@@ -3,9 +3,11 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+#   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
+#     Copyright (C) 2011 OpenFOAM Foundation
+#------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
 #
@@ -40,7 +42,9 @@ fi
 
 [ -d Make ] || mkdir Make
 rm -f Make/options
+
 #------------------------------------------------------------------------------
+
 echo "Creating Make/options"
 
 echo 'EXE_INC = \
diff --git a/wmake/scripts/makeTargetDir b/wmake/scripts/makeTargetDir
index 97e26b71383..44b778f9ba7 100755
--- a/wmake/scripts/makeTargetDir
+++ b/wmake/scripts/makeTargetDir
@@ -3,9 +3,11 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+#   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
+#     Copyright (C) 2011 OpenFOAM Foundation
+#------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, licensed under GNU General Public License
 #     <http://www.gnu.org/licenses/>.
diff --git a/wmake/src/Allmake b/wmake/src/Allmake
index 8130987f832..9e78c5eb528 100755
--- a/wmake/src/Allmake
+++ b/wmake/src/Allmake
@@ -1,12 +1,39 @@
 #!/bin/sh
-cd "${0%/*}" || exit 1                  # Run from this directory
+cd "${0%/*}" || exit            # This directory (/path/project/wmake/src)
 
-if [ -z "$WM_DIR" ]                     # Require WM_DIR
+if [ -z "$WM_DIR" ]             # Require WM_DIR (/path/project/wmake)
 then
-    WM_DIR="$(\cd $(dirname $0)/.. && \pwd -L)"
+    WM_DIR="$(dirname "$(pwd -L)")"
     export WM_DIR
 fi
 
-make                                    # Compile tools for wmake
+if [ -z "$WM_PROJECT_DIR" ]     # Expect WM_PROJECT_DIR (/path/project)
+then
+    echo "Warning (${0##*/}) : No WM_PROJECT_DIR set" 1>&2
+    WM_PROJECT_DIR="${WM_DIR%/*}"
+    export WM_PROJECT_DIR
+fi
+
+if [ -z "$WM_ARCH" ] || [ -z "$WM_COMPILER" ]
+then
+    echo "Error (${0##*/}) : No WM_ARCH or WM_COMPILER set"
+    echo "    Check your OpenFOAM environment and installation"
+    exit 1
+fi
+
+case "$WM_COMPILER" in
+Mingw*)
+    # Host wmake toolchain with system gcc (when cross-compiling)
+    make \
+        WM_COMPILER=Gcc WM_COMPILER_TYPE=system \
+        WMAKE_BIN="${WM_PROJECT_DIR}/platforms/tools/${WM_ARCH}${WM_COMPILER}" \
+        "$@"
+    ;;
+
+*)
+    # Regular wmake toolchain
+    make "$@"
+    ;;
+esac
 
 #------------------------------------------------------------------------------
diff --git a/wmake/src/Makefile b/wmake/src/Makefile
index 53b375b595f..f02ad124c3f 100644
--- a/wmake/src/Makefile
+++ b/wmake/src/Makefile
@@ -2,9 +2,12 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
+#   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
+#     Copyright (C) 2011-2016 OpenFOAM Foundation
+#     Copyright (C) 2017-2020 OpenCFD Ltd.
+#------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
 #
@@ -30,13 +33,13 @@
 #------------------------------------------------------------------------------
 
 #------------------------------------------------------------------------------
-# The Makefile use a POSIX shell
+# Use POSIX shell
 #------------------------------------------------------------------------------
 
-SHELL      = /bin/sh
+SHELL   = /bin/sh
 
 #------------------------------------------------------------------------------
-# Unset suffices list (suffix rules are not used)
+# No default suffix rules used
 #------------------------------------------------------------------------------
 
 .SUFFIXES:
@@ -52,41 +55,44 @@ WM_COMPILE_OPTION = Opt
 GENERAL_RULES   = $(WM_DIR)/rules/General
 include $(GENERAL_RULES)/general
 
+archHost    := $(WM_ARCH)$(WM_COMPILER)
+archTarget  := $(shell basename $(WMAKE_BIN))
+
 
 #------------------------------------------------------------------------------
 # Targets
 #------------------------------------------------------------------------------
 
-.PHONY: all clean
+.PHONY: all clean message old
+
+all: $(WMAKE_BIN)/wmkdepend message
 
-all:  $(WMAKE_BIN)/dirToString $(WMAKE_BIN)/wmkdep $(WMAKE_BIN)/wmkdepend
-	@echo built wmake-bin for $(WM_ARCH)$(WM_COMPILER)
+# Flex-based processing
+old: $(WMAKE_BIN)/wmkdep
+
+message:
+	@echo "built wmake-bin ($(archTarget))"
 
 clean:
-	@echo clean wmake-bin for $(WM_ARCH)$(WM_COMPILER)
-	@rm -rf $(WMAKE_BIN) 2>/dev/null
+	@echo "clean wmake-bin ($(archTarget))"
+	@rm -rf $(WMAKE_BIN)
 	@rmdir $(shell dirname $(WMAKE_BIN)) 2>/dev/null || true
 
-$(WMAKE_BIN)/dirToString: dirToString.c
-	@mkdir -p $(WMAKE_BIN)
-	$(call QUIET_MESSAGE,compile,$(<F))
-	$E $(cc) $(cFLAGS) $(<F) -o $@
-
 $(WMAKE_BIN)/wmkdep: wmkdep.l
 	@mkdir -p $(WMAKE_BIN)
 	$(call QUIET_MESSAGE,flex,$(<F))
 	$E flex -o $@.c $(<F) && $(cc) $(cFLAGS) $@.c -o $@
-	@rm -f $@.c 2>/dev/null
+	@rm -f $@.c
 
 $(WMAKE_BIN)/wmkdepend: wmkdepend.cpp
 	@mkdir -p $(WMAKE_BIN)
 	$(call QUIET_MESSAGE,wmkdepend,$(<F))
 	$E $(CC) $(c++FLAGS) $(c++LESSWARN) $(<F) -o $@
 
-# $(WMAKE_BIN)/wmkdepend: wmkdepend.rl
-# 	@mkdir -p $(WMAKE_BIN)
-# 	$(call QUIET_MESSAGE,ragel,$(<F))
-# 	$E ragel -G2 -o $@.cpp $(<F) && $(CC) $(c++FLAGS) $(c++LESSWARN) $@.cpp -o $@
-# 	@rm -f $@.cpp 2>/dev/null
+#$(WMAKE_BIN)/wmkdepend: wmkdepend.rl
+#	@mkdir -p $(WMAKE_BIN)
+#	$(call QUIET_MESSAGE,ragel,$(<F))
+#	$E ragel -G2 -o $@.cpp $(<F) && $(CC) $(c++FLAGS) $@.cpp -o $@
+#	@rm -f $@.cpp
 
 #------------------------------------------------------------------------------
diff --git a/wmake/src/dirToString.c b/wmake/src/dirToString.c
deleted file mode 100644
index 1655af38549..00000000000
--- a/wmake/src/dirToString.c
+++ /dev/null
@@ -1,113 +0,0 @@
-/*----------------------------------------------------------------------------*\
- =========                   |
- \\      /   F ield          | OpenFOAM: The Open Source CFD Toolbox
-  \\    /    O peration      |
-   \\  /     A nd            | Copyright (C) 2011 OpenFOAM Foundation
-    \\/      M anipulation   | Copyright (C) 2017 OpenCFD Ltd.
--------------------------------------------------------------------------------
-License
-    This file is part of OpenFOAM.
-
-    OpenFOAM is free software: you can redistribute it and/or modify it
-    under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
-    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-    for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
-
-Application
-    dirToString
-
-Description
-    Converts a directory path into a camelCase string.
-    e.g. dir1/dir2/dir3 becomes dir1Dir2Dir3
-
-Usage
-    echo dirName | dirToString
-
-    e.g.
-        using sh
-        baseDirName=$(echo $dir | $bin/dirToString -strip)
-
-        using csh
-        set baseDirName=`echo $dir | $bin/dirToString -strip`
-
-\*----------------------------------------------------------------------------*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
-/* The executable name (for messages), without requiring access to argv[] */
-#define EXENAME  "dirToString"
-
-int main(int argc, char* argv[])
-{
-    int c;
-
-    if (argc > 1)
-    {
-        if (!strncmp(argv[1], "-h", 2))
-        {
-            /* Option: -h, -help */
-
-            fputs
-            (
-                "\nUsage: " EXENAME
-                " [-strip]\n\n"
-                "  -strip    ignore leading [./] characters.\n\n"
-                "Converts a directory path into a camelCase string\n\n",
-                stderr
-            );
-            return 0;
-        }
-
-        if (!strcmp(argv[1], "-s") || !strcmp(argv[1], "-strip"))
-        {
-            /* Option: -s, -strip */
-
-            while ((c=getchar()) != EOF && (c == '.' || c == '/'))
-            {
-                /* nop */
-            }
-
-            if (c == EOF)
-            {
-                return 0;
-            }
-
-            putchar(c);
-        }
-    }
-
-
-    int nextUpper = 0;
-    while ((c = getchar()) != EOF)
-    {
-        if (c == '/')
-        {
-            nextUpper = 1;
-        }
-        else if (nextUpper)
-        {
-            putchar(toupper(c));
-            nextUpper = 0;
-        }
-        else
-        {
-            putchar(c);
-        }
-    }
-
-    return 0;
-}
-
-
-/*****************************************************************************/
-- 
GitLab