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