Merge branch 'master' of github.com:OpenFOAM/OpenFOAM-2.0.x
[OpenFOAM-2.0.x.git] / wmake / wmakePrintBuild
blob26cd5ef6ce6e86ebfef6bdb14b2dc48fd68b72db
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 # \\/ M anipulation |
8 #-------------------------------------------------------------------------------
9 # License
10 # This file is part of OpenFOAM.
12 # OpenFOAM is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 # for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 # Script
26 # wmakePrintBuild
28 # Description
29 # Print the version used when building the project
31 #------------------------------------------------------------------------------
32 # persistent build tag
33 build="$WM_PROJECT_DIR/.build"
35 usage() {
36 exec 1>&2
38 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
39 cat<<USAGE
40 usage: ${0##*/} [OPTION]
41 options:
42 -check check the git head commit vs. \$WM_PROJECT_DIR/.build
43 (exit code 0 for no changes)
44 -major report \$WM_PROJECT_VERSION only and exit
45 -update update \$WM_PROJECT_DIR/.build from the git information
46 -pkg TAG specify packager/release tag ('none' marks an empty packager)
47 -short report short version information (ie, without pkg tag)
48 -version VER specify an alternative version
50 Print the version used when building the project, in this order of precedence:
51 * the git head commit (prefixed with \$WM_PROJECT_VERSION)
52 * \$WM_PROJECT_DIR/.build
53 * \$WM_PROJECT_VERSION
55 USAGE
56 exit 1
58 #------------------------------------------------------------------------------
60 unset checkOnly update package version shortOpt
62 # parse options
63 while [ "$#" -gt 0 ]
65 case "$1" in
66 -h | -help)
67 usage
69 -c | -check)
70 checkOnly=true
71 shift
73 -major)
74 echo ${WM_PROJECT_VERSION:-unknown}
75 exit 0
77 -u | -update)
78 update=true
79 shift
81 -pkg | -package)
82 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
83 # mark empty as 'none', disallow '!' in string
84 package=$(echo "${2:-none}" | sed -e 's/!//g')
85 shift 2
87 -short)
88 shortOpt=true
89 shift
91 -v | -version)
92 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
93 version="$2"
94 shift 2
97 usage "unknown option/argument: '$*'"
99 esac
100 done
102 #------------------------------------------------------------------------------
105 # retrieve old values from the $WM_PROJECT_DIR/.build cache, stored as
106 # version [packager]
108 unset oldPackage oldVersion
109 getOldValues()
111 set -- $(tail -1 $build 2>/dev/null)
112 oldVersion="$1"
113 [ "$#" -gt 0 ] && shift
114 oldPackage="$@"
115 [ "${oldPackage:-none}" = none ] && unset oldPackage
119 # printTag - output the build tag
120 # reuses the old -package tag if needed
122 printTag()
124 if [ "${package:-${oldPackage:-none}}" = none ]
125 then
126 echo "$version"
127 else
128 echo "$version ${package:-$oldPackage}"
134 if [ -n "$version" ]
135 then
136 # specified a version - no error possible
137 rc=0
138 else
139 # get the head SHA1 when building under git
140 # if there are multiple values (eg, HEAD, origin/HEAD, ...)
141 # only take the first one, which is 'HEAD'
142 version=$(
143 cd $WM_PROJECT_DIR 2>/dev/null && \
144 git show-ref --hash=12 --head HEAD 2>/dev/null | head -1
147 if [ -n "$version" ]
148 then
149 # mark as success and prefix with WM_PROJECT_VERSION
150 rc=0
151 version="${WM_PROJECT_VERSION}-$version"
152 else
153 # mark as failure
154 rc=1
159 # retrieve old values
160 getOldValues
162 if [ "$shortOpt" = true ]
163 then
164 unset package oldPackage
168 # update persistent build tag if possible
170 if [ $rc -eq 0 -a -n "$update" ]
171 then
172 if [ "$version:$package" != "$oldVersion:$oldPackage" ]
173 then
174 if [ -w "$build" -o \( -w "$WM_PROJECT_DIR" -a ! -e "$build" \) ]
175 then
176 printTag >| "$build" 2>/dev/null
181 # cat<< DEBUG 1>&2
182 # Debug information
183 # version='$version'
184 # package='$package'
185 # oldVersion='$oldVersion'
186 # oldPackage='$oldPackage'
187 # DEBUG
190 # check git vs. persistent build tag
191 if [ -n "$checkOnly" ]
192 then
193 if [ $rc -eq 0 ]
194 then
195 test "$version:${package:-$oldPackage}" = "$oldVersion:$oldPackage"
196 rc=$?
197 if [ $rc -eq 0 ]
198 then
199 echo "same version as previous build" 1>&2
200 else
201 echo "version changed from previous build" 1>&2
203 else
204 echo "no git description found" 1>&2
206 exit $rc
211 # cannot get git information or -version version
213 if [ $rc -ne 0 ]
214 then
215 if [ -n "$oldVersion" ]
216 then
217 # use previous version info
218 version="$oldVersion"
219 else
220 # fallback to WM_PROJECT_VERSION alone
221 version="${WM_PROJECT_VERSION:-unknown}"
226 # output the tag
227 printTag
229 #------------------------------------------------------------------------------