bin/mdw-build: Allow configuration of the `setup' command.
[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
9c586ae1 67 [no]setup[=RUNE]
7ee12623
MW
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
9c586ae1 92setupcmd=mdw-setup
7ee12623
MW
93distcheck=yes
94debian=yes
95upload=yes
96clean=yes
f5b3423e 97vpath=yes
7ee12623
MW
98for opt; do
99 case "$opt" in
100 checkout) checkout=yes checkoutrev=HEAD ;;
101 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
7ee12623
MW
102 release) build=release ;;
103 norelease) build=test ;;
9c586ae1
MW
104 setup) setup=yes setupcmd=mdw-setup ;;
105 setup=*) setup=yes setupcmd=${opt#*=} ;;
7ee12623 106
9c586ae1 107 distcheck | debian | upload | clean | vpath)
7ee12623
MW
108 eval "$opt=yes"
109 ;;
50c72b0f
MW
110 nocheckout | nosetup | nodistcheck | nodebian | \
111 noupload | noclean | novpath)
7ee12623
MW
112 eval "${opt#no}=no"
113 ;;
114 *)
115 usage >&2
116 exit 1
117 ;;
118 esac
119done
120
84dd9069
MW
121## Parse DEB_BUILD_OPTIONS.
122jobs=1
123set -- $DEB_BUILD_OPTIONS
124for opt; do
125 case "$opt" in
126 parallel=*) jobs=${opt#*=} ;;
127 esac
128done
129
130makeopts=""
131case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
132
7ee12623
MW
133###--------------------------------------------------------------------------
134### Utility functions.
135
136exec 3>&2 4>/dev/null 5>&2
137
138notify () {
139 colour=$1 message=$2
140 echo $message >&4
141 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
142}
143
144fail () {
145 notify 1 "!!! $*"
146 exit 1
147}
148
f282ba46
MW
149warn () {
150 case $build in
151 release) fail "$*" ;;
152 *) notify 5 "??? $*" ;;
153 esac
154}
155
7ee12623
MW
156info () {
157 notify 6 "--- $*"
158}
159
160assign () {
161 info "$1 = $2"
162 eval "$1=$2"
163}
164
165runx () {
166 notify 2 "+++ $*"
167 "$@" 2>&3 || fail "$1: exit $?"
168}
169
170run () { runx "$@" >&3; }
171
172yesno () {
173 echo -n "(test $*)" >&4
174 if "$@" >&4 2>&4; then
175 echo "(yes)" >&4
176 echo yes
177 else
178 echo "(no)" >&4
179 echo no
180 fi
181}
182
183###--------------------------------------------------------------------------
184### Do the building.
185
186## Find the top-level package directory.
d43de82b
MW
187while [ ! -f configure.ac -a ! -f configure.in -a \
188 ! -f .links -a ! -d .git ]; do
7ee12623
MW
189 case "$(pwd)" in
190 /)
191 fail "couldn't find top-level directory"
192 ;;
193 esac
194 cd ..
195done
196assign srcpath $(pwd)
197
198## Construct the output directory.
199assign releasepath $srcpath/dist-$build
47539e6a 200chmod -R +w $releasepath 2>/dev/null || :
7ee12623
MW
201rm -rf $releasepath 2>/dev/null || :
202mkdir $releasepath
203case $verbose in
204 no)
205 exec 4>$releasepath/mdw-build.log 3>&4 ||
206 fail "Failed to create log."
207 ;;
208esac
209
f282ba46 210## Do we have a Git repository?
7ee12623
MW
211case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
212 yes,no,*)
213 fail "Inconsistent options: can't check out without setup."
214 ;;
215 yes,yes,no)
216 info "No Git repository found."
f282ba46 217 checkout=no gitver=none
7ee12623
MW
218 ;;
219 yes,yes,yes)
220 cd $srcpath
221 [ "$(git ls-files -m)" = "" ] ||
f282ba46 222 warn "working tree has uncommitted changes"
40196145 223 gitver=$(git describe --abbrev=4)
f282ba46
MW
224esac
225
226## Is there Debian build equipment?
227case "$debian,$(yesno [ -d $srcpath/debian ])" in
228 yes,no)
229 info "No debian directory found."
230 debian=no debver=none
231 ;;
f0905f8c
MW
232 no,*)
233 debver=none
234 ;;
f282ba46 235 yes,yes)
7219881d 236 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p' | tr \~ -)
40196145
MW
237 debsrc=$(dpkg-parsechangelog | sed -n 's/^Source: //p')
238 debname=$(git config user.name) debemail=$(git config user.email)
f282ba46
MW
239 ;;
240esac
241
242## Check the version number.
40196145 243hack_dch_p=no
f282ba46
MW
244case "$gitver,$debver" in
245 none,* | *,none)
246 ;;
247 *)
40196145 248 if [ "$gitver" != "$debver" ]; then
f282ba46 249 warn "Git version $gitver doesn't match Debian version $debver"
40196145
MW
250 hack_dch=yes
251 fi
f282ba46
MW
252 ;;
253esac
254
f5b3423e 255## Maybe check out a copy of the source.
f282ba46
MW
256case "$checkout" in
257 yes)
7ee12623
MW
258 cd $releasepath
259 run git clone -sn $srcpath/.git _source
260 assign srcpath $releasepath/_source
261 cd $srcpath
262 run git checkout -b mdw-build $checkoutrev
263 ;;
264esac
265
266## Maybe refresh the build machinery.
267case "$setup" in
268 yes)
9c586ae1 269 run $setupcmd
7ee12623
MW
270 ;;
271esac
272
273## Initialize the build directory.
f5b3423e
MW
274case "$vpath,$(yesno [ -e $srcpath/configure ])" in
275 yes,yes)
276 assign buildpath $releasepath/_build
277 mkdir $buildpath
278 cd $buildpath
279 run $srcpath/configure
280 ;;
281 no,yes)
282 info "VPATH build disabled"
283 assign buildpath $srcpath
284 distcheck=no
285 cd $srcpath
286 run ./configure
287 ;;
288 *,no)
289 info "no configure script"
290 assign buildpath $srcpath
291 cd $srcpath
292 ;;
293esac
7ee12623
MW
294
295## Discover the release name.
296cat >find-distdir.mk <<'EOF'
297include Makefile
298print-distdir:
dd8d9a6c 299 @echo >&3 $(distdir)
7ee12623 300EOF
dd8d9a6c
MW
301assign distdir \
302 $({ make -f find-distdir.mk print-distdir >/dev/null 2>&1; } 3>&1)
7ee12623
MW
303
304## Get a tarball distribution.
305case "$distcheck" in
306 yes)
84dd9069 307 run make $makeopts distcheck
7ee12623
MW
308 ;;
309 no)
84dd9069 310 run make $makeopts dist
7ee12623
MW
311 ;;
312esac
313
314cd $releasepath
315
98a2d95e 316if ! tar tf $buildpath/$distdir.tar.gz 2>/dev/null | grep -q RELEASE; then
7ee12623
MW
317 fail "missing RELEASE file in distribution"
318fi
319
320run mv $buildpath/$distdir.tar.gz .
47f56ea2
MW
321case $build in
322 release)
323 run gpg -u$(mdw-conf releasekey) -ab -o$distdir.tar.gz.gpg \
324 $distdir.tar.gz
325 ;;
326esac
7ee12623
MW
327
328## Maybe build the Debian packages.
f282ba46
MW
329case "$debian" in
330 yes)
7ee12623
MW
331 run tar xvfz $distdir.tar.gz
332 cd $distdir
40196145
MW
333 case $hack_dch in
334 yes)
335 dver=$(echo $gitver | sed 's/-/+/; s/-/./g')
336 now=$(date -R)
337 cat - debian/changelog >debian/changelog.new <<EOF
338$debsrc ($dver) experimental; urgency=low
339
340 * Hacking in process, not intended for release.
341
342 -- $debname <$debemail> $now
343
344EOF
345 mv debian/changelog.new debian/changelog
346 ;;
347 esac
7ee12623
MW
348 run dpkg-buildpackage -k$(mdw-conf releasekey)
349 ;;
350esac
351
352## Maybe upload Debian packages.
353cd $releasepath
354case "$upload,$build" in
355 yes,test)
356 info "Test build: not uploading."
357 ;;
358 yes,release)
25d80caa 359 run rsync $distdir.tar.gz $distdir.tar.gz.gpg \
9a472b9c 360 $(mdw-conf upload-target ftp.distorted.org.uk:~ftp/pub/mdw/)
7ee12623
MW
361 case "$debian" in
362 yes)
9a472b9c 363 run dput -f $(mdw-conf dput-target distorted) *.changes
7ee12623
MW
364 ;;
365 esac
366esac
367
368## Tidy up.
369case "$clean" in
370 yes)
371 rm -rf $releasepath/$distdir
372 rm -rf $releasepath/_source
373 rm -rf $releasepath/_build
374 ;;
375esac
376
377## Done.
378info "All OK."
379
380###----- That's all, folks --------------------------------------------------