File: wmakePrintBuild

package info (click to toggle)
openfoam 4.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 163,028 kB
  • ctags: 58,990
  • sloc: cpp: 830,760; sh: 10,227; ansic: 8,215; xml: 745; lex: 437; awk: 194; sed: 91; makefile: 77; python: 18
file content (237 lines) | stat: -rwxr-xr-x 6,846 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 2011-2015 OpenFOAM Foundation
#    \\/     M anipulation  |
#-------------------------------------------------------------------------------
# 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/>.
#
# Script
#     wmakePrintBuild
#
# Usage
#     wmakePrintBuild [OPTION]
#
# Description
#     Print the version used when building the project
#
#------------------------------------------------------------------------------
Script=${0##*/}

usage() {
    exec 1>&2

    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE
Usage: $Script [OPTION]
options:
  -check          check the git head commit vs. \$WM_PROJECT_DIR/.build
                  (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)
  -short          report short version information (ie, without pkg tag)
  -version VER    specify an alternative version

Print the version used when building the project, in this order of precedence:
  * the git head commit (prefixed with \$WM_PROJECT_VERSION)
  * \$WM_PROJECT_DIR/.build
  * \$WM_PROJECT_VERSION

USAGE
    exit 1
}


#------------------------------------------------------------------------------
# Parse arguments and options
#------------------------------------------------------------------------------
unset checkOnly update package version shortOpt

while [ "$#" -gt 0 ]
do
    case "$1" in
    -h | -help)
        usage
        ;;
    -c | -check)
        checkOnly=true
        shift
        ;;
    -major)
        echo ${WM_PROJECT_VERSION:-unknown}
        exit 0
        ;;
    -u | -update)
        update=true
        shift
        ;;
    -pkg | -package)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        # Mark empty as 'none', disallow '!' in string
        package=$(echo "${2:-none}" | sed -e 's/!//g')
        shift 2
        ;;
    -short)
        shortOpt=true
        shift
        ;;
    -v | -version)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        version="$2"
        shift 2
        ;;
    *)
        usage "unknown option/argument: '$*'"
        ;;
    esac
done

#------------------------------------------------------------------------------

# Persistent build tag
build="$WM_PROJECT_DIR/.build"


#------------------------------------------------------------------------------
# Retrieve old values from the $WM_PROJECT_DIR/.build cache, stored as
#     version [packager]
#------------------------------------------------------------------------------
unset oldPackage oldVersion
getOldValues()
{
    set -- $(tail -1 $build 2>/dev/null)
    oldVersion="$1"
    [ "$#" -gt 0 ] && 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
}


#------------------------------------------------------------------------------
# Get the version
#------------------------------------------------------------------------------

if [ -n "$version" ]
then
    # Specified a version - no error possible
    rc=0
else
    # Get the head SHA1 when building under git
    # if there are multiple values (eg, HEAD, origin/HEAD, ...)
    # only take the first one, which is 'HEAD'
    version=$(
        cd $WM_PROJECT_DIR 2>/dev/null && \
        git show-ref --hash=12 --head HEAD 2>/dev/null | head -1
    )

    if [ -n "$version" ]
    then
        # Mark as success and prefix with WM_PROJECT_VERSION
        rc=0
        version="${WM_PROJECT_VERSION}-$version"
    else
        # Mark as failure
        rc=1
    fi
fi


# Retrieve old values
getOldValues

if [ "$shortOpt" = true ]
then
    unset package oldPackage
fi


#------------------------------------------------------------------------------
# Update persistent build tag if possible
#------------------------------------------------------------------------------
if [ $rc -eq 0 -a -n "$update" ]
then
    if [ "$version:$package" != "$oldVersion:$oldPackage" ]
    then
        if [ -w "$build" -o \( -w "$WM_PROJECT_DIR" -a ! -e "$build" \) ]
        then
            printTag >| "$build" 2>/dev/null
        fi
    fi
fi


#------------------------------------------------------------------------------
# Check git vs. persistent build tag
#------------------------------------------------------------------------------
if [ -n "$checkOnly" ]
then
    if [ $rc -eq 0 ]
    then
        test "$version:${package:-$oldPackage}" = "$oldVersion:$oldPackage"
        rc=$?
        if [ $rc -eq 0 ]
        then
            echo "same version as previous build" 1>&2
        else
            echo "version changed from previous build" 1>&2
        fi
    else
        echo "no git description found" 1>&2
    fi
    exit $rc
fi


#------------------------------------------------------------------------------
# Cannot get git information or  -version version
#------------------------------------------------------------------------------
if [ $rc -ne 0 ]
then
    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


#------------------------------------------------------------------------------