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