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