View Issue Details

IDProjectCategoryView StatusLast Update
0000293OpenFOAMBugpublic2015-01-01 14:46
Reporterbgschaid Assigned Tohenry  
PrioritynormalSeverityfeatureReproducibilityhave not tried
Status closedResolutionno change required 
PlatformLinuxOSCentOSOS Version5.5
Summary0000293: Introduce preprocessor defines that allow to distinguish different Foam versions
DescriptionPreamble: I now that #ifdef is BAAAAD and makes the code Ugly (with a capital U) and should be avoided

Some third-party developments only need minor modifications to run under various versions of OF (one example is http://openfoamwiki.net/index.php/Contrib_equationReader where only mathematicalConstants has to be replaced with constants::mathematical to make the source code compile on 2.0). In these cases the maintainance of two different source trees is highly impractical.

Introduction of preprocessorsymbols (I'm modelling this after the example of gcc/__GCC__) like this

#define FOAM_VERSION 2
#define FOAM_VERSION_MiNOR 0
#define FOAM_VERSION_PATCHLEVEL 1

// only in the version that is pulled from git
#define FOAM_VERSION_GIT

in such a way that it is included in every OpenFOAM-H-file (or for instance in foamVersion.H) would help to distinguish different OF-versions during compilation.

Sorry if this already exists (I didn't find it. Only the generated info in global.Cver which is only available at run-time)

BTW: of course I don't propose to generate FOAM_VERSION automatically as this would most likely trigger a complete recompilation of OF every time
TagsNo tags attached.

Activities

wyldckat

2011-09-18 16:04

updater  

version_tag_for_third_party_apps (4,877 bytes)   
diff --git a/src/Allwmake b/src/Allwmake
index aa17b41..9251277 100755
--- a/src/Allwmake
+++ b/src/Allwmake
@@ -22,6 +22,8 @@ wmakePrintBuild -check || /bin/rm -f OpenFOAM/Make/*/global.? 2>/dev/null
 
 wmakeLnInclude OpenFOAM
 wmakeLnInclude OSspecific/${WM_OSTYPE:-POSIX}
+wmakeVersionTag #generate version tag, after OpenFOAM/lnInclude has been created
+
 Pstream/Allwmake $*
 
 OSspecific/${WM_OSTYPE:-POSIX}/Allwmake
diff --git a/wmake/wmakeVersionTag b/wmake/wmakeVersionTag
new file mode 100755
index 0000000..6b7b782
--- /dev/null
+++ b/wmake/wmakeVersionTag
@@ -0,0 +1,152 @@
+#!/bin/sh
+#------------------------------------------------------------------------------
+#
+# License
+#     This file was designed to work with OpenFOAM. It's also bound to the same
+#     license as 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.
+#
+#     OpenFOAM: The Open Source CFD Toolbox
+#     Copyright (C) 2011 OpenFOAM Foundation
+#
+#     You should have received a copy of the GNU General Public License
+#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Script
+#     wmakeVersionTag
+#
+# Description
+#     Print C DEFINES the version used when building the project.
+#
+#------------------------------------------------------------------------------
+# default version tag file for code
+versiontag="$FOAM_SRC/OpenFOAM/lnInclude/foamVersionTag.H"
+
+usage() {
+    exec 1>&2
+
+    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
+    cat<<USAGE
+usage: ${0##*/} [OPTION]
+options:
+  -tagfile FILE       Change the file tag where to save the detailed OpenFOAM
+                      version information. If FILE is a hyphen (-), then the
+                      tag is shown on the standard output.
+
+Generate the version tag file that can be used by third party codes:
+  * Default tag file: $versiontag
+  * Relies on OpenFOAM's "wmakePrintBuild" and "foamNewSource" scripts.
+
+USAGE
+    exit 1
+}
+#------------------------------------------------------------------------------
+
+unset printOpt
+
+# parse options
+while [ "$#" -gt 0 ]
+do
+    case "$1" in
+    -h | -help)
+        usage
+        ;;
+    -t | -tagfile)
+        # for a file name starting with '-' simply display the code
+        if [ "${2#-}" != "${2}" ]
+        then
+            printOpt=true
+        else
+            tagfile=$2
+        fi
+        shift 2
+        ;;
+    *)
+        usage "unknown option/argument: '$*'"
+        ;;
+    esac
+done
+
+#------------------------------------------------------------------------------
+
+#
+# genCoreTag - output the core version tag file
+#
+genCoreTag()
+{
+    #retrieve and parse version information
+    toolbox_version=`wmakePrintBuild -short`
+    toolbox_major=`echo $toolbox_version | sed -e 's=\([0-9]*\).*=\1='`
+    toolbox_minor=`echo $toolbox_version | sed -e 's=[0-9]*\.\([0-9]*\).*=\1='`
+    toolbox_patch=`echo $toolbox_version | sed -e 's=[0-9]*\.[0-9]*\.\([0-9a-z]*\).*=\1='`
+    toolbox_gitcommit=`echo $toolbox_version | sed -e 's=.*\-\([0-9a-z]*\).*=\1='`
+    if [ "$toolbox_gitcommit" = "$toolbox_version" ]
+    then
+      toolbox_gitcommit=""
+    fi
+
+    #generate header inclusion limiter
+    cat << GENCORETAG
+
+#ifndef FOAMVERSIONTAG_H
+#define FOAMVERSIONTAG_H
+
+GENCORETAG
+
+    #generate core version information
+    cat << GENCORETAG
+//TIP: use #FOAM_PROJECT_NAME to get the defined symbol in string form.
+
+#define FOAM_PROJECT_NAME $WM_PROJECT
+#define FOAM_VERSION_MAJOR $toolbox_major
+#define FOAM_VERSION_MINOR $toolbox_minor
+#define FOAM_VERSION_PATCHLEVEL $toolbox_patch
+GENCORETAG
+
+    #generate git related version information
+    if [ -n "$toolbox_gitcommit" ]
+    then
+    cat << GENCORETAG
+
+#define FOAM_VERSION_GIT_COMMIT $toolbox_gitcommit
+GENCORETAG
+    fi
+
+    #finish header inclusion limiter
+    cat << GENCORETAG
+
+#endif
+
+// ************************************************************************* //
+
+GENCORETAG
+}
+
+#
+# genTag - output the version tag file
+#
+genTag()
+{
+    #Get the standard header file
+    $WM_PROJECT_DIR/etc/codeTemplates/source/foamNewSource I - | grep -B 100 '\\\*'
+
+    #generate the code with the version information
+    genCoreTag
+}
+
+if [ -n "$printOpt" ]; then
+  genTag
+else
+  genTag > $versiontag
+fi
+
+#------------------------------------------------------------------------------

wyldckat

2011-09-18 16:15

updater   ~0000657

In case direct coding of the version onto "foamVersion.H" is out of the question, then attached is the file "version_tag_for_third_party_apps" with a proposition patch for a "military dog tag" style header file generation.

The proposed patch works well for most scenarios:
* Automatically generates the "foamVersionTag.H" file with the defines necessary for third party codes.
* Can also be used in case the tag file hasn't been generated yet, so the file can be generated locally by the 3rd party code simply by running:
   wmakeVersionTag -tagfile foamVersionTag.H

* Doesn't affect OpenFOAM's core code, since it's not included.
* It's as simple as using in the 3rd party code:
   #include "foamVersionTag.H"

Notes:
* The attached patch is also being tracked here: https://github.com/wyldckat/OpenFOAM-2.0.x/commits/issue293
* Changes to bgschaid's proposal:
  * Added "FOAM_PROJECT_NAME" to reflect "$WM_PROJECT".
  * Modified "FOAM_VERSION_GIT" to "FOAM_VERSION_GIT_COMMIT", useful for corner cases and keeping track in built 3rd party code versions.

* Rant: Had to re-write this note on this bug tracker because it wouldn't accept the previous form cookie... mental note: gotta remember to copy-paste a backup...

user19

2012-06-11 06:40

  ~0001368

Just as a side-note: You can define such a symbol yourself easily in Make/options. I used the following:

  ifndef FOAM_HEX_VERSION
  FOAM_HEX_VERSION=0x$(subst .,,$(WM_PROJECT_VERSION:.x=.0))
  endif
  
  EXE_INC = -DFOAM_HEX_VERSION=$(FOAM_HEX_VERSION)

E.g. for OpenFOAM 2.1.1 this will define FOAM_HEX_VERSION=0x211. If every any of the numbers becomes larger than 9, you'd need more sophistication than that, but I don't think that's going to happen any time soon... Notice that I chose to ignore git versions, but it should be fairly straight forward to change that, depending on your needs.

Issue History

Date Modified Username Field Change
2011-09-15 13:30 bgschaid New Issue
2011-09-18 16:04 wyldckat File Added: version_tag_for_third_party_apps
2011-09-18 16:15 wyldckat Note Added: 0000657
2012-06-11 06:40 user19 Note Added: 0001368
2015-01-01 14:46 henry Status new => closed
2015-01-01 14:46 henry Assigned To => henry
2015-01-01 14:46 henry Resolution open => no change required