From a2fe746899c6b6fa2a03da2ed67041bc7230d072 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@Germany>
Date: Fri, 28 Jan 2011 13:52:36 +0100
Subject: [PATCH] ENH: allow -pkg|-package option for wmakePrintBuild

- improves the chances of tracking who built OpenFOAM, where, etc

ENH: make it easier to parse version/build from OpenFOAM -help output

- For example,
    foamListTimes -help | awk '{ if (/^Using:/) print $2}'
    foamListTimes -help | awk '{ if (/^Build:/) print $2}'
---
 src/OpenFOAM/global/argList/argList.C | 19 ++++--
 wmake/rules/General/version           |  4 +-
 wmake/wmakePrintBuild                 | 84 ++++++++++++++++++++-------
 3 files changed, 81 insertions(+), 26 deletions(-)

diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C
index 370f42b2fa3..1d23a86c9cb 100644
--- a/src/OpenFOAM/global/argList/argList.C
+++ b/src/OpenFOAM/global/argList/argList.C
@@ -507,13 +507,23 @@ Foam::argList::argList
     jobInfo.add("startTime", timeString);
     jobInfo.add("userName", userName());
     jobInfo.add("foamVersion", word(FOAMversion));
-    jobInfo.add("foamBuild", Foam::FOAMbuild);
     jobInfo.add("code", executable_);
     jobInfo.add("argList", argListString);
     jobInfo.add("currentDir", cwd());
     jobInfo.add("PPID", ppid());
     jobInfo.add("PGID", pgid());
 
+    // add build information - only use the first word
+    {
+        std::string build(Foam::FOAMbuild);
+        std::string::size_type found = build.find(' ');
+        if (found != std::string::npos)
+        {
+            build.resize(found);
+        }
+        jobInfo.add("foamBuild", build);
+    }
+
 
     // Case is a single processor run unless it is running parallel
     int nProcs = 1;
@@ -883,9 +893,10 @@ void Foam::argList::printUsage() const
     printNotes();
 
     Info<< nl
-        <<"Using OpenFOAM-" << Foam::FOAMversion
-        <<" (build: " << Foam::FOAMbuild << ") - see www.OpenFOAM.com"
-        << nl << endl;
+        <<"Using: OpenFOAM-" << Foam::FOAMversion
+        << " (see www.OpenFOAM.com)" << nl
+        <<"Build: " << Foam::FOAMbuild << nl
+        << endl;
 }
 
 
diff --git a/wmake/rules/General/version b/wmake/rules/General/version
index 11f83c7b746..19633ffc9dc 100644
--- a/wmake/rules/General/version
+++ b/wmake/rules/General/version
@@ -4,8 +4,8 @@
 # update version strings in C++ file and in $WM_PROJECT_DIR/.build file
 #
 Cvertoo = \
-    sed -e 's/VERSION_STRING/$(shell wmakePrintBuild -major)/' \
-        -e 's/BUILD_STRING/$(shell wmakePrintBuild -update)/' \
+    sed -e 's!VERSION_STRING!$(shell wmakePrintBuild -major)!' \
+        -e 's!BUILD_STRING!$(shell wmakePrintBuild -update)!' \
         $$SOURCE > $*.C; \
     $(CC) $(c++FLAGS) -c $*.C -o $@
 
diff --git a/wmake/wmakePrintBuild b/wmake/wmakePrintBuild
index 4b705036875..3812bece3e9 100755
--- a/wmake/wmakePrintBuild
+++ b/wmake/wmakePrintBuild
@@ -3,7 +3,7 @@
 # =========                 |
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
-#   \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
+#   \\  /    A nd           | Copyright (C) 2008-2011 OpenCFD Ltd.
 #    \\/     M anipulation  |
 #-------------------------------------------------------------------------------
 # License
@@ -30,6 +30,8 @@
 #
 #------------------------------------------------------------------------------
 usage() {
+    exec 1>&2
+
     while [ "$#" -ge 1 ]; do echo "$1"; shift; done
     cat<<USAGE
 usage: ${0##*/} [OPTION]
@@ -38,6 +40,7 @@ options:
                   (exit code 0 for no changes)
   -major          report \$WM_PROJECT_VERSION only and exit
   -update         update \$WM_PROJECT_DIR/.build from the git information
+  -pkg TAG        specify packager/release tag ('none' marks an empty packager)
   -version VER    specify an alternative version
 
 Print the version used when building the project, in this order of precedence:
@@ -50,7 +53,7 @@ USAGE
 }
 #------------------------------------------------------------------------------
 
-unset checkOnly update version
+unset checkOnly update package version oldPackage oldVersion
 
 # parse options
 while [ "$#" -gt 0 ]
@@ -71,6 +74,12 @@ do
         update=true
         shift
         ;;
+    -pkg | -package)
+        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
+        # mark empty as 'none', disallow '!' and spaces in string
+        package=$(echo "${2:-none}" | sed -e 's/!//g' -e 's/ //g')
+        shift 2
+        ;;
     -v | -version)
         [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
         version="$2"
@@ -88,7 +97,31 @@ done
 # persistent build tag
 #
 build="$WM_PROJECT_DIR/.build"
-previous=$(tail -1 $build 2>/dev/null)
+
+# retrieve old values from the $WM_PROJECT_DIR/.build cache, stored as
+#     version [packager]
+set -- $(tail -1 $build 2>/dev/null)
+
+oldVersion="$1"; shift
+oldPackage="$@"
+[ "${oldPackage:-none}" = none ] && unset oldPackage
+
+
+#
+# printTag - output the build tag
+# reuses the old -package tag if needed
+#
+printTag()
+{
+    if [ "${package:-${oldPackage:-none}}" = none ]
+    then
+        echo "$version"
+    else
+        echo "$version ${package:-$oldPackage}"
+    fi
+}
+
+
 
 if [ -n "$version" ]
 then
@@ -115,12 +148,17 @@ else
 fi
 
 
+#
 # update persistent build tag if possible
-if [ $rc -eq 0 -a -n "$update" -a "$version" != "$previous" ]
+#
+if [ $rc -eq 0 -a -n "$update" ]
 then
-    if [ -w "$build" -o \( -w "$WM_PROJECT_DIR" -a ! -e "$build" \) ]
+    if [ "$version:$package" != "$oldVersion:$oldPackage" ]
     then
-        echo $version >| "$build" 2>/dev/null
+        if [ -w "$build" -o \( -w "$WM_PROJECT_DIR" -a ! -e "$build" \) ]
+        then
+            printTag >| "$build" 2>/dev/null
+        fi
     fi
 fi
 
@@ -130,32 +168,38 @@ if [ -n "$checkOnly" ]
 then
     if [ $rc -eq 0 ]
     then
-        test "$version" = "$previous"
+        test "$version:${package:-$oldPackage}" = "$oldVersion:$oldPackage"
         rc=$?
         if [ $rc -eq 0 ]
         then
-            echo "same version as previous build"
+            echo "same version as previous build" 1>&2
         else
-            echo "version changed from previous build"
+            echo "version changed from previous build" 1>&2
         fi
     else
-        echo "no git description found"
+        echo "no git description found" 1>&2
     fi
     exit $rc
 fi
 
 
-if [ $rc -eq 0 ]
-then
-    # output the git information or the -version version
-    echo $version
-elif [ -n "$previous" ]
+#
+# cannot get git information or  -version version
+#
+if [ $rc -ne 0 ]
 then
-    # use previous build tag
-    echo $previous
-else
-    # fallback to WM_PROJECT_VERSION alone
-    echo ${WM_PROJECT_VERSION:-unknown}
+    if [ -n "$oldVersion" ]
+    then
+        # use previous version info
+        version="$oldVersion"
+    else
+        # fallback to WM_PROJECT_VERSION alone
+        version="${WM_PROJECT_VERSION:-unknown}"
+    fi
 fi
 
+
+# output the tag
+printTag
+
 #------------------------------------------------------------------------------
-- 
GitLab