Merge commit 'd3b269b7c6ffa0cd68845adfecdfb849316dba71' into nextRelease
[foam-extend-3.2.git] / wmake / wmakePrintBuild
blob12e3361a06c2b6a48509f7e5c4b1a312acfc5e55
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | foam-extend: Open Source CFD
5 # \\ / O peration |
6 # \\ / A nd | For copyright notice see file Copyright
7 # \\/ M anipulation |
8 #------------------------------------------------------------------------------
9 # License
10 # This file is part of foam-extend.
12 # foam-extend is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by the
14 # Free Software Foundation, either version 3 of the License, or (at your
15 # option) any later version.
17 # foam-extend is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with foam-extend. 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 Script=${0##*/}
34 usage() {
35 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
36 cat<<USAGE
37 usage: $Script [OPTION]
38 options:
39 -check check the git head commit vs. \$WM_PROJECT_DIR/.build
40 (exit code 0 for no changes)
41 -major report \$WM_PROJECT_VERSION only and exit
42 -update update \$WM_PROJECT_DIR/.build from the git information
43 -version VER specify an alternative version
45 Print the version used when building the project, in this order of precedence:
46 * the git head commit (prefixed with \$WM_PROJECT_VERSION)
47 * \$WM_PROJECT_DIR/.build
48 * \$WM_PROJECT_VERSION
50 USAGE
51 exit 1
53 #------------------------------------------------------------------------------
55 unset checkOnly update version
57 # parse options
58 while [ "$#" -gt 0 ]
60 case "$1" in
61 -h | -help)
62 usage
64 -c | -check)
65 checkOnly=true
66 shift
68 -major)
69 echo ${WM_PROJECT_VERSION:-unknown}
70 exit 0
72 -u | -update)
73 update=true
74 shift
76 -v | -version)
77 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
78 version=$2
79 shift 2
82 usage "unknown option/argument: '$*'"
84 esac
85 done
87 #------------------------------------------------------------------------------
90 # persistent build tag
92 build="$WM_PROJECT_DIR/.build"
93 previous=$(tail -1 $build 2>/dev/null)
95 if [ -n "$version" ]
96 then
97 # specified a version - no error possible
98 rc=0
99 else
100 # get the head SHA1 when building under git
101 # if there are multiple values (eg, HEAD, origin/HEAD, ...)
102 # only take the first one, which is 'HEAD'
103 version=$(git show-ref --hash=12 --head HEAD 2>/dev/null | head -1)
105 if [ -n "$version" ]
106 then
107 # mark as success and prefix with WM_PROJECT_VERSION
108 rc=0
109 version="${WM_PROJECT_VERSION}-$version"
110 else
111 # mark as failure
112 rc=1
117 # update persistent build tag if possible
118 if [ $rc -eq 0 -a -n "$update" -a "$version" != "$previous" ]
119 then
120 if [ -w "$build" -o \( -w "$WM_PROJECT_DIR" -a ! -e "$build" \) ]
121 then
122 echo $version >| "$build" 2>/dev/null
127 # check git vs. persistent build tag
128 if [ -n "$checkOnly" ]
129 then
130 if [ $rc -eq 0 ]
131 then
132 test "$version" = "$previous"
133 rc=$?
134 if [ $rc -eq 0 ]
135 then
136 echo "same version as previous build"
137 else
138 echo "version changed from previous build"
140 else
141 echo "no git description found"
143 exit $rc
147 if [ $rc -eq 0 ]
148 then
149 # output the git information or the -version version
150 echo $version
151 elif [ -n "$previous" ]
152 then
153 # use previous build tag
154 echo $previous
155 else
156 # fallback to WM_PROJECT_VERSION alone
157 echo ${WM_PROJECT_VERSION:-unknown}
160 #------------------------------------------------------------------------------