-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmkversion.sh
executable file
·143 lines (119 loc) · 3.81 KB
/
mkversion.sh
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
#!/bin/sh
# Copyright 2014-2016 dunnhumby Germany GmbH.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
# Defaults
rev_file=src/Version.d
author="`git config user.name`"
# Get the current date (might be overridden by command-line options later)
date=$(date -u +"%Y-%m-%d %H:%M:%S %Z")
print_usage()
{
echo "\
Generates a Version.d file.
Usage: $0 [OPTIONS] [TEMPLATE] [LIB1] [LIB2] ...
Options:
-o FILE Where to write the output (Version.d) file (default: $rev_file)
-a AUTHOR Author of the build (default: detected, currently $author)
-d DATE Build date string (default now: $date)
-m MODULE Module name to use in the module declaration (default: built from -o)
-F DFLAGS D compiler flags used to compile the binary (default: taken from
\$DFLAGS, now $DFLAGS)
-f FLAVOUR MakeD flavour used to compile the binary (default: taken from \$F,
now '$F')
-v Be more verbose (print a message if the file was updated)
-p Only print this repository version and exit
-h Shows this help and exit
TEMPLATE is template file to use
LIB1 ... are the name of the libraries this program depends on (to get the
libraries versions).
NOTE: All these options are replace in the template using sed s// command and
this script doesn't get care of quoting, so if you use any 'special'
character (like '/') you need to quote it yourself.
"
}
# Parse arguments
verbose=0
module=
print_only=
while getopts o:L:a:t:d:m:F:f:vph flag
do
case $flag in
o) rev_file="$OPTARG";;
a) author="$OPTARG";;
d) date="$OPTARG";;
m) module="$OPTARG";;
F) DFLAGS="$OPTARG";;
f) F="$OPTARG";;
v) verbose=1;;
p) print_only=1;;
h) print_usage ; exit 0;;
\?) echo >&2; print_usage >&2; exit 2;;
esac
done
shift `expr $OPTIND - 1`
version=`git describe --dirty --always --abbrev=4`
# Add branch name if we only got a hash
echo "$version" | grep -Eq '^[0-9a-f]{4,}(-dirty)?$' &&
version=`git rev-parse --abbrev-ref HEAD`-0-g"$version"
# Check if the user only wanted to print the version number
if [ "$print_only" = 1 ]
then
echo $version
exit 0
fi
# Get compiler version
compiler="`${DC:-dmd} | head -1`"
template="$1"; shift
tmp=`mktemp mkversion.XXXXXXXXXX`
trap "rm -f '$tmp'; exit 1" INT TERM QUIT
# Generate the file (in a temporary) based on a template
module=${module:-`echo "$rev_file" | sed -e 's|/|.|g' -e 's|.d||g'`}
# Generate the libraries info
libs=''
for lib in "$@"
do
lib_base=`basename $lib`
ver_desc=`cd $lib && git describe --dirty --always --abbrev=4`
# Add branch name if we only got a hash
echo "$ver_desc" | grep -Eq '^[0-9a-f]{4,}(-dirty)?$' &&
ver_desc=`cd $lib && git rev-parse --abbrev-ref HEAD`-g"$ver_desc"
libs="${libs} version_info[\"lib_${lib_base}\"] = \"${ver_desc}\";\\n"
done
awk \
-v module="$module" \
-v version="$version" \
-v date="$date" \
-v author="$author" \
-v compiler="$compiler" \
-v dflags="$DFLAGS" \
-v flavour="$F" \
-v libraries="$libs" \
'{
gsub("@MODULE@", module);
gsub("@VERSION@", version);
gsub("@DATE@", date);
gsub("@AUTHOR@", author);
gsub("@COMPILER@", compiler);
gsub("@DFLAGS@", dflags);
gsub("@FLAVOUR@", flavour);
gsub("@LIBRARIES@", libraries);
print $0;
}' "$template" > "$tmp"
# Check if anything has changed
if [ -e "$rev_file" ]
then
sum1=`md5sum "$tmp" | cut -d' ' -f1`
sum2=`md5sum "$rev_file" | cut -d' ' -f1`
if [ $sum1 = $sum2 ]
then
rm "$tmp"
exit 0
fi
fi
mv "$tmp" "$rev_file"
if test "$verbose" -gt 0
then
echo "$rev_file updated"
fi
# vim: set et sw=4 sts=4 :