dot/emacs: Add `face' to `whitespace-style'.
[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
72EOF
73}
74
75## Parse simple options.
76verbose=no
77while getopts "hvr" opt; do
78 case "$opt" in
79 h) usage; exit 0 ;;
80 v) verbose=yes ;;
81 *) exit 1 ;;
82 esac
83done
84shift $((OPTIND - 1))
85
86## Parse the build options.
87checkout=yes
88checkoutrev=HEAD
89build=test
90setup=yes
91distcheck=yes
92debian=yes
93upload=yes
94clean=yes
95for opt; do
96 case "$opt" in
97 checkout) checkout=yes checkoutrev=HEAD ;;
98 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
99 nocheckout) checkout=no ;;
100 release) build=release ;;
101 norelease) build=test ;;
102
103 setup | distcheck | debian | upload | clean)
104 eval "$opt=yes"
105 ;;
106 nosetup | nodistcheck | nodebian | noupload | noclean)
107 eval "${opt#no}=no"
108 ;;
109 *)
110 usage >&2
111 exit 1
112 ;;
113 esac
114done
115
84dd9069
MW
116## Parse DEB_BUILD_OPTIONS.
117jobs=1
118set -- $DEB_BUILD_OPTIONS
119for opt; do
120 case "$opt" in
121 parallel=*) jobs=${opt#*=} ;;
122 esac
123done
124
125makeopts=""
126case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
127
7ee12623
MW
128###--------------------------------------------------------------------------
129### Utility functions.
130
131exec 3>&2 4>/dev/null 5>&2
132
133notify () {
134 colour=$1 message=$2
135 echo $message >&4
136 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
137}
138
139fail () {
140 notify 1 "!!! $*"
141 exit 1
142}
143
f282ba46
MW
144warn () {
145 case $build in
146 release) fail "$*" ;;
147 *) notify 5 "??? $*" ;;
148 esac
149}
150
7ee12623
MW
151info () {
152 notify 6 "--- $*"
153}
154
155assign () {
156 info "$1 = $2"
157 eval "$1=$2"
158}
159
160runx () {
161 notify 2 "+++ $*"
162 "$@" 2>&3 || fail "$1: exit $?"
163}
164
165run () { runx "$@" >&3; }
166
167yesno () {
168 echo -n "(test $*)" >&4
169 if "$@" >&4 2>&4; then
170 echo "(yes)" >&4
171 echo yes
172 else
173 echo "(no)" >&4
174 echo no
175 fi
176}
177
178###--------------------------------------------------------------------------
179### Do the building.
180
181## Find the top-level package directory.
d43de82b
MW
182while [ ! -f configure.ac -a ! -f configure.in -a \
183 ! -f .links -a ! -d .git ]; do
7ee12623
MW
184 case "$(pwd)" in
185 /)
186 fail "couldn't find top-level directory"
187 ;;
188 esac
189 cd ..
190done
191assign srcpath $(pwd)
192
193## Construct the output directory.
194assign releasepath $srcpath/dist-$build
47539e6a 195chmod -R +w $releasepath 2>/dev/null || :
7ee12623
MW
196rm -rf $releasepath 2>/dev/null || :
197mkdir $releasepath
198case $verbose in
199 no)
200 exec 4>$releasepath/mdw-build.log 3>&4 ||
201 fail "Failed to create log."
202 ;;
203esac
204
f282ba46 205## Do we have a Git repository?
7ee12623
MW
206case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
207 yes,no,*)
208 fail "Inconsistent options: can't check out without setup."
209 ;;
210 yes,yes,no)
211 info "No Git repository found."
f282ba46 212 checkout=no gitver=none
7ee12623
MW
213 ;;
214 yes,yes,yes)
215 cd $srcpath
216 [ "$(git ls-files -m)" = "" ] ||
f282ba46
MW
217 warn "working tree has uncommitted changes"
218 gitver=$(git describe)
219esac
220
221## Is there Debian build equipment?
222case "$debian,$(yesno [ -d $srcpath/debian ])" in
223 yes,no)
224 info "No debian directory found."
225 debian=no debver=none
226 ;;
227 yes,yes)
228 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p')
229 ;;
230esac
231
232## Check the version number.
233case "$gitver,$debver" in
234 none,* | *,none)
235 ;;
236 *)
237 [ "$gitver" = "$debver" ] ||
238 warn "Git version $gitver doesn't match Debian version $debver"
239 ;;
240esac
241
242## Maybe check ot a copy of the source.
243case "$checkout" in
244 yes)
7ee12623
MW
245 cd $releasepath
246 run git clone -sn $srcpath/.git _source
247 assign srcpath $releasepath/_source
248 cd $srcpath
249 run git checkout -b mdw-build $checkoutrev
250 ;;
251esac
252
253## Maybe refresh the build machinery.
254case "$setup" in
255 yes)
256 run mdw-setup
257 ;;
258esac
259
260## Initialize the build directory.
261if [ -e $srcpath/configure ]; then
262 assign buildpath $releasepath/_build
263 mkdir $buildpath
264 cd $buildpath
265 run $srcpath/configure
266else
267 info "no configure script"
268 assign buildpath $srcpath
269 cd $srcpath
270fi
271
272## Discover the release name.
273cat >find-distdir.mk <<'EOF'
274include Makefile
275print-distdir:
276 @echo $(distdir)
277EOF
278assign distdir $(make -f find-distdir.mk print-distdir)
279
280## Get a tarball distribution.
281case "$distcheck" in
282 yes)
84dd9069 283 run make $makeopts distcheck
7ee12623
MW
284 ;;
285 no)
84dd9069 286 run make $makeopts dist
7ee12623
MW
287 ;;
288esac
289
290cd $releasepath
291
98a2d95e 292if ! tar tf $buildpath/$distdir.tar.gz 2>/dev/null | grep -q RELEASE; then
7ee12623
MW
293 fail "missing RELEASE file in distribution"
294fi
295
296run mv $buildpath/$distdir.tar.gz .
297
298## Maybe build the Debian packages.
f282ba46
MW
299case "$debian" in
300 yes)
7ee12623
MW
301 run tar xvfz $distdir.tar.gz
302 cd $distdir
303 run dpkg-buildpackage -k$(mdw-conf releasekey)
304 ;;
305esac
306
307## Maybe upload Debian packages.
308cd $releasepath
309case "$upload,$build" in
310 yes,test)
311 info "Test build: not uploading."
312 ;;
313 yes,release)
9978f25a 314 run rsync $distdir.tar.gz \
9a472b9c 315 $(mdw-conf upload-target ftp.distorted.org.uk:~ftp/pub/mdw/)
7ee12623
MW
316 case "$debian" in
317 yes)
9a472b9c 318 run dput -f $(mdw-conf dput-target distorted) *.changes
7ee12623
MW
319 ;;
320 esac
321esac
322
323## Tidy up.
324case "$clean" in
325 yes)
326 rm -rf $releasepath/$distdir
327 rm -rf $releasepath/_source
328 rm -rf $releasepath/_build
329 ;;
330esac
331
332## Done.
333info "All OK."
334
335###----- That's all, folks --------------------------------------------------