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