bin/mdw-build: Allow configuration of the `setup' command.
[profile] / bin / mdw-build
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
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
54 set -e
55
56 ###--------------------------------------------------------------------------
57 ### Parse options.
58
59 usage () {
60 cat <<EOF
61 Usage: $0 [-vr] BUILDOPT
62
63 Build options:
64
65 [no]checkout[=REV]
66 [no]release
67 [no]setup[=RUNE]
68 [no]distcheck
69 [no]debian
70 [no]upload
71 [no]clean
72 [no]vpath
73 EOF
74 }
75
76 ## Parse simple options.
77 verbose=no
78 while getopts "hvr" opt; do
79 case "$opt" in
80 h) usage; exit 0 ;;
81 v) verbose=yes ;;
82 *) exit 1 ;;
83 esac
84 done
85 shift $((OPTIND - 1))
86
87 ## Parse the build options.
88 checkout=yes
89 checkoutrev=HEAD
90 build=test
91 setup=yes
92 setupcmd=mdw-setup
93 distcheck=yes
94 debian=yes
95 upload=yes
96 clean=yes
97 vpath=yes
98 for opt; do
99 case "$opt" in
100 checkout) checkout=yes checkoutrev=HEAD ;;
101 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
102 release) build=release ;;
103 norelease) build=test ;;
104 setup) setup=yes setupcmd=mdw-setup ;;
105 setup=*) setup=yes setupcmd=${opt#*=} ;;
106
107 distcheck | debian | upload | clean | vpath)
108 eval "$opt=yes"
109 ;;
110 nocheckout | nosetup | nodistcheck | nodebian | \
111 noupload | noclean | novpath)
112 eval "${opt#no}=no"
113 ;;
114 *)
115 usage >&2
116 exit 1
117 ;;
118 esac
119 done
120
121 ## Parse DEB_BUILD_OPTIONS.
122 jobs=1
123 set -- $DEB_BUILD_OPTIONS
124 for opt; do
125 case "$opt" in
126 parallel=*) jobs=${opt#*=} ;;
127 esac
128 done
129
130 makeopts=""
131 case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
132
133 ###--------------------------------------------------------------------------
134 ### Utility functions.
135
136 exec 3>&2 4>/dev/null 5>&2
137
138 notify () {
139 colour=$1 message=$2
140 echo $message >&4
141 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
142 }
143
144 fail () {
145 notify 1 "!!! $*"
146 exit 1
147 }
148
149 warn () {
150 case $build in
151 release) fail "$*" ;;
152 *) notify 5 "??? $*" ;;
153 esac
154 }
155
156 info () {
157 notify 6 "--- $*"
158 }
159
160 assign () {
161 info "$1 = $2"
162 eval "$1=$2"
163 }
164
165 runx () {
166 notify 2 "+++ $*"
167 "$@" 2>&3 || fail "$1: exit $?"
168 }
169
170 run () { runx "$@" >&3; }
171
172 yesno () {
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.
187 while [ ! -f configure.ac -a ! -f configure.in -a \
188 ! -f .links -a ! -d .git ]; do
189 case "$(pwd)" in
190 /)
191 fail "couldn't find top-level directory"
192 ;;
193 esac
194 cd ..
195 done
196 assign srcpath $(pwd)
197
198 ## Construct the output directory.
199 assign releasepath $srcpath/dist-$build
200 chmod -R +w $releasepath 2>/dev/null || :
201 rm -rf $releasepath 2>/dev/null || :
202 mkdir $releasepath
203 case $verbose in
204 no)
205 exec 4>$releasepath/mdw-build.log 3>&4 ||
206 fail "Failed to create log."
207 ;;
208 esac
209
210 ## Do we have a Git repository?
211 case "$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."
217 checkout=no gitver=none
218 ;;
219 yes,yes,yes)
220 cd $srcpath
221 [ "$(git ls-files -m)" = "" ] ||
222 warn "working tree has uncommitted changes"
223 gitver=$(git describe --abbrev=4)
224 esac
225
226 ## Is there Debian build equipment?
227 case "$debian,$(yesno [ -d $srcpath/debian ])" in
228 yes,no)
229 info "No debian directory found."
230 debian=no debver=none
231 ;;
232 no,*)
233 debver=none
234 ;;
235 yes,yes)
236 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p' | tr \~ -)
237 debsrc=$(dpkg-parsechangelog | sed -n 's/^Source: //p')
238 debname=$(git config user.name) debemail=$(git config user.email)
239 ;;
240 esac
241
242 ## Check the version number.
243 hack_dch_p=no
244 case "$gitver,$debver" in
245 none,* | *,none)
246 ;;
247 *)
248 if [ "$gitver" != "$debver" ]; then
249 warn "Git version $gitver doesn't match Debian version $debver"
250 hack_dch=yes
251 fi
252 ;;
253 esac
254
255 ## Maybe check out a copy of the source.
256 case "$checkout" in
257 yes)
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 ;;
264 esac
265
266 ## Maybe refresh the build machinery.
267 case "$setup" in
268 yes)
269 run $setupcmd
270 ;;
271 esac
272
273 ## Initialize the build directory.
274 case "$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 ;;
293 esac
294
295 ## Discover the release name.
296 cat >find-distdir.mk <<'EOF'
297 include Makefile
298 print-distdir:
299 @echo >&3 $(distdir)
300 EOF
301 assign distdir \
302 $({ make -f find-distdir.mk print-distdir >/dev/null 2>&1; } 3>&1)
303
304 ## Get a tarball distribution.
305 case "$distcheck" in
306 yes)
307 run make $makeopts distcheck
308 ;;
309 no)
310 run make $makeopts dist
311 ;;
312 esac
313
314 cd $releasepath
315
316 if ! tar tf $buildpath/$distdir.tar.gz 2>/dev/null | grep -q RELEASE; then
317 fail "missing RELEASE file in distribution"
318 fi
319
320 run mv $buildpath/$distdir.tar.gz .
321 case $build in
322 release)
323 run gpg -u$(mdw-conf releasekey) -ab -o$distdir.tar.gz.gpg \
324 $distdir.tar.gz
325 ;;
326 esac
327
328 ## Maybe build the Debian packages.
329 case "$debian" in
330 yes)
331 run tar xvfz $distdir.tar.gz
332 cd $distdir
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
344 EOF
345 mv debian/changelog.new debian/changelog
346 ;;
347 esac
348 run dpkg-buildpackage -k$(mdw-conf releasekey)
349 ;;
350 esac
351
352 ## Maybe upload Debian packages.
353 cd $releasepath
354 case "$upload,$build" in
355 yes,test)
356 info "Test build: not uploading."
357 ;;
358 yes,release)
359 run rsync $distdir.tar.gz $distdir.tar.gz.gpg \
360 $(mdw-conf upload-target ftp.distorted.org.uk:~ftp/pub/mdw/)
361 case "$debian" in
362 yes)
363 run dput -f $(mdw-conf dput-target distorted) *.changes
364 ;;
365 esac
366 esac
367
368 ## Tidy up.
369 case "$clean" in
370 yes)
371 rm -rf $releasepath/$distdir
372 rm -rf $releasepath/_source
373 rm -rf $releasepath/_build
374 ;;
375 esac
376
377 ## Done.
378 info "All OK."
379
380 ###----- That's all, folks --------------------------------------------------