bin/mdw-build: `nocommit' isn't a special case.
[profile] / bin / mdw-build
CommitLineData
7ee12623
MW
1#! /bin/bash
2###
3### Build script for Debian packages
4###
5### (c) 2008 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This program is free software; you can redistribute it and/or modify
11### it under the terms of the GNU General Public License as published by
12### the Free Software Foundation; either version 2 of the License, or
13### (at your option) any later version.
14###
15### This program is distributed in the hope that it will be useful,
16### but WITHOUT ANY WARRANTY; without even the implied warranty of
17### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18### GNU General Public License for more details.
19###
20### You should have received a copy of the GNU General Public License
21### along with this program; if not, write to the Free Software Foundation,
22### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
85b7c21c
MW
24###--------------------------------------------------------------------------
25### Conventions for build systems.
26###
27### This script is designed to work with a variety of `make'-based build
28### systems, but there are a number of conventions which must be followed if
29### this is going to work properly.
30###
31### * There must be a `configure.ac', `configure.in', or `.links' file, or
32### a `.git' directory in the project top-level, so that we can find it.
33###
34### * The following `make' variables must be assigned in the top-level
35### Makefile, after `mdw-build' has constructed it.
36###
37### distdir The name of the top-level project directory in the
38### source distribution, and the base name for
39### distribution archives; should be of the form
40### `PROJECT-VERSION'.
41###
42### The following `make' targets must be available in the top-level
43### Makefile.
44###
45### dist Write to $(distdir).tar.gz a source distribution of
46### the package.
47###
48### distcheck As for `dist', but also build and test the project.
49###
50### * The source distribution constructed by `make dist' must contain a file
51### $(distdir)/RELEASE containing the release name. This isn't currently
52### tested, but it might be later.
53
7ee12623
MW
54set -e
55
56###--------------------------------------------------------------------------
57### Parse options.
58
59usage () {
60 cat <<EOF
61Usage: $0 [-vr] BUILDOPT
62
63Build options:
64
65 [no]checkout[=REV]
66 [no]release
67 [no]setup
68 [no]distcheck
69 [no]debian
70 [no]upload
71 [no]clean
f5b3423e 72 [no]vpath
7ee12623
MW
73EOF
74}
75
76## Parse simple options.
77verbose=no
78while getopts "hvr" opt; do
79 case "$opt" in
80 h) usage; exit 0 ;;
81 v) verbose=yes ;;
82 *) exit 1 ;;
83 esac
84done
85shift $((OPTIND - 1))
86
87## Parse the build options.
88checkout=yes
89checkoutrev=HEAD
90build=test
91setup=yes
92distcheck=yes
93debian=yes
94upload=yes
95clean=yes
f5b3423e 96vpath=yes
7ee12623
MW
97for opt; do
98 case "$opt" in
99 checkout) checkout=yes checkoutrev=HEAD ;;
100 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
7ee12623
MW
101 release) build=release ;;
102 norelease) build=test ;;
103
f5b3423e 104 setup | distcheck | debian | upload | clean | vpath)
7ee12623
MW
105 eval "$opt=yes"
106 ;;
50c72b0f
MW
107 nocheckout | nosetup | nodistcheck | nodebian | \
108 noupload | noclean | novpath)
7ee12623
MW
109 eval "${opt#no}=no"
110 ;;
111 *)
112 usage >&2
113 exit 1
114 ;;
115 esac
116done
117
84dd9069
MW
118## Parse DEB_BUILD_OPTIONS.
119jobs=1
120set -- $DEB_BUILD_OPTIONS
121for opt; do
122 case "$opt" in
123 parallel=*) jobs=${opt#*=} ;;
124 esac
125done
126
127makeopts=""
128case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
129
7ee12623
MW
130###--------------------------------------------------------------------------
131### Utility functions.
132
133exec 3>&2 4>/dev/null 5>&2
134
135notify () {
136 colour=$1 message=$2
137 echo $message >&4
138 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
139}
140
141fail () {
142 notify 1 "!!! $*"
143 exit 1
144}
145
f282ba46
MW
146warn () {
147 case $build in
148 release) fail "$*" ;;
149 *) notify 5 "??? $*" ;;
150 esac
151}
152
7ee12623
MW
153info () {
154 notify 6 "--- $*"
155}
156
157assign () {
158 info "$1 = $2"
159 eval "$1=$2"
160}
161
162runx () {
163 notify 2 "+++ $*"
164 "$@" 2>&3 || fail "$1: exit $?"
165}
166
167run () { runx "$@" >&3; }
168
169yesno () {
170 echo -n "(test $*)" >&4
171 if "$@" >&4 2>&4; then
172 echo "(yes)" >&4
173 echo yes
174 else
175 echo "(no)" >&4
176 echo no
177 fi
178}
179
180###--------------------------------------------------------------------------
181### Do the building.
182
183## Find the top-level package directory.
d43de82b
MW
184while [ ! -f configure.ac -a ! -f configure.in -a \
185 ! -f .links -a ! -d .git ]; do
7ee12623
MW
186 case "$(pwd)" in
187 /)
188 fail "couldn't find top-level directory"
189 ;;
190 esac
191 cd ..
192done
193assign srcpath $(pwd)
194
195## Construct the output directory.
196assign releasepath $srcpath/dist-$build
47539e6a 197chmod -R +w $releasepath 2>/dev/null || :
7ee12623
MW
198rm -rf $releasepath 2>/dev/null || :
199mkdir $releasepath
200case $verbose in
201 no)
202 exec 4>$releasepath/mdw-build.log 3>&4 ||
203 fail "Failed to create log."
204 ;;
205esac
206
f282ba46 207## Do we have a Git repository?
7ee12623
MW
208case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
209 yes,no,*)
210 fail "Inconsistent options: can't check out without setup."
211 ;;
212 yes,yes,no)
213 info "No Git repository found."
f282ba46 214 checkout=no gitver=none
7ee12623
MW
215 ;;
216 yes,yes,yes)
217 cd $srcpath
218 [ "$(git ls-files -m)" = "" ] ||
f282ba46 219 warn "working tree has uncommitted changes"
40196145 220 gitver=$(git describe --abbrev=4)
f282ba46
MW
221esac
222
223## Is there Debian build equipment?
224case "$debian,$(yesno [ -d $srcpath/debian ])" in
225 yes,no)
226 info "No debian directory found."
227 debian=no debver=none
228 ;;
f0905f8c
MW
229 no,*)
230 debver=none
231 ;;
f282ba46 232 yes,yes)
7219881d 233 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p' | tr \~ -)
40196145
MW
234 debsrc=$(dpkg-parsechangelog | sed -n 's/^Source: //p')
235 debname=$(git config user.name) debemail=$(git config user.email)
f282ba46
MW
236 ;;
237esac
238
239## Check the version number.
40196145 240hack_dch_p=no
f282ba46
MW
241case "$gitver,$debver" in
242 none,* | *,none)
243 ;;
244 *)
40196145 245 if [ "$gitver" != "$debver" ]; then
f282ba46 246 warn "Git version $gitver doesn't match Debian version $debver"
40196145
MW
247 hack_dch=yes
248 fi
f282ba46
MW
249 ;;
250esac
251
f5b3423e 252## Maybe check out a copy of the source.
f282ba46
MW
253case "$checkout" in
254 yes)
7ee12623
MW
255 cd $releasepath
256 run git clone -sn $srcpath/.git _source
257 assign srcpath $releasepath/_source
258 cd $srcpath
259 run git checkout -b mdw-build $checkoutrev
260 ;;
261esac
262
263## Maybe refresh the build machinery.
264case "$setup" in
265 yes)
266 run mdw-setup
267 ;;
268esac
269
270## Initialize the build directory.
f5b3423e
MW
271case "$vpath,$(yesno [ -e $srcpath/configure ])" in
272 yes,yes)
273 assign buildpath $releasepath/_build
274 mkdir $buildpath
275 cd $buildpath
276 run $srcpath/configure
277 ;;
278 no,yes)
279 info "VPATH build disabled"
280 assign buildpath $srcpath
281 distcheck=no
282 cd $srcpath
283 run ./configure
284 ;;
285 *,no)
286 info "no configure script"
287 assign buildpath $srcpath
288 cd $srcpath
289 ;;
290esac
7ee12623
MW
291
292## Discover the release name.
293cat >find-distdir.mk <<'EOF'
294include Makefile
295print-distdir:
dd8d9a6c 296 @echo >&3 $(distdir)
7ee12623 297EOF
dd8d9a6c
MW
298assign distdir \
299 $({ make -f find-distdir.mk print-distdir >/dev/null 2>&1; } 3>&1)
7ee12623
MW
300
301## Get a tarball distribution.
302case "$distcheck" in
303 yes)
84dd9069 304 run make $makeopts distcheck
7ee12623
MW
305 ;;
306 no)
84dd9069 307 run make $makeopts dist
7ee12623
MW
308 ;;
309esac
310
311cd $releasepath
312
98a2d95e 313if ! tar tf $buildpath/$distdir.tar.gz 2>/dev/null | grep -q RELEASE; then
7ee12623
MW
314 fail "missing RELEASE file in distribution"
315fi
316
317run mv $buildpath/$distdir.tar.gz .
47f56ea2
MW
318case $build in
319 release)
320 run gpg -u$(mdw-conf releasekey) -ab -o$distdir.tar.gz.gpg \
321 $distdir.tar.gz
322 ;;
323esac
7ee12623
MW
324
325## Maybe build the Debian packages.
f282ba46
MW
326case "$debian" in
327 yes)
7ee12623
MW
328 run tar xvfz $distdir.tar.gz
329 cd $distdir
40196145
MW
330 case $hack_dch in
331 yes)
332 dver=$(echo $gitver | sed 's/-/+/; s/-/./g')
333 now=$(date -R)
334 cat - debian/changelog >debian/changelog.new <<EOF
335$debsrc ($dver) experimental; urgency=low
336
337 * Hacking in process, not intended for release.
338
339 -- $debname <$debemail> $now
340
341EOF
342 mv debian/changelog.new debian/changelog
343 ;;
344 esac
7ee12623
MW
345 run dpkg-buildpackage -k$(mdw-conf releasekey)
346 ;;
347esac
348
349## Maybe upload Debian packages.
350cd $releasepath
351case "$upload,$build" in
352 yes,test)
353 info "Test build: not uploading."
354 ;;
355 yes,release)
25d80caa 356 run rsync $distdir.tar.gz $distdir.tar.gz.gpg \
9a472b9c 357 $(mdw-conf upload-target ftp.distorted.org.uk:~ftp/pub/mdw/)
7ee12623
MW
358 case "$debian" in
359 yes)
9a472b9c 360 run dput -f $(mdw-conf dput-target distorted) *.changes
7ee12623
MW
361 ;;
362 esac
363esac
364
365## Tidy up.
366case "$clean" in
367 yes)
368 rm -rf $releasepath/$distdir
369 rm -rf $releasepath/_source
370 rm -rf $releasepath/_build
371 ;;
372esac
373
374## Done.
375info "All OK."
376
377###----- That's all, folks --------------------------------------------------