bin/mdw-build: Read initial option state from a configuration file.
[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 ### Configuration.
58
59 unset checkout checkoutrev
60 unset setup setupcmd
61 unset dput dputtarget
62 unset build distcheck debian upload clean vpath native
63 for i in \
64 "/etc/mdw-build.conf" \
65 "${XDG_CONFIG_HOME-$HOME/.config}/mdw-build.conf" \
66 "./.mdw-build.conf"
67 do
68 if [ -f "$i" ]; then . "$i"; fi
69 done
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 ###--------------------------------------------------------------------------
82 ### Parse options.
83
84 prog=${0##*/}
85
86 usage () {
87 cat <<EOF
88 Usage: $prog [-vr] BUILDOPT
89
90 Build options:
91
92 [no]checkout[=REV]
93 [no]release
94 [no]setup[=RUNE]
95 [no]distcheck
96 [no]debian
97 [no]upload
98 [no]clean
99 [no]vpath
100 [no]native
101 EOF
102 }
103
104 ## Parse simple options.
105 verbose=no
106 while getopts "hvr" opt; do
107 case "$opt" in
108 h) usage; exit 0 ;;
109 v) verbose=yes ;;
110 *) exit 1 ;;
111 esac
112 done
113 shift $((OPTIND - 1))
114
115 ## Parse the build options.
116 for opt; do
117 case "$opt" in
118 checkout) checkout=yes checkoutrev=HEAD ;;
119 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
120 release) build=release ;;
121 norelease) build=test ;;
122 setup) setup=yes setupcmd=mdw-setup ;;
123 setup=*) setup=yes setupcmd=${opt#*=} ;;
124
125 distcheck | debian | upload | clean | vpath | native)
126 eval "$opt=yes"
127 ;;
128 nocheckout | nosetup | nodistcheck | nodebian | \
129 noupload | noclean | novpath | nonative)
130 eval "${opt#no}=no"
131 ;;
132 *)
133 usage >&2
134 exit 1
135 ;;
136 esac
137 done
138
139 ## Parse DEB_BUILD_OPTIONS.
140 jobs=1
141 set -- $DEB_BUILD_OPTIONS
142 for opt; do
143 case "$opt" in
144 parallel=*) jobs=${opt#*=} ;;
145 esac
146 done
147
148 makeopts=""
149 case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
150
151 ###--------------------------------------------------------------------------
152 ### Utility functions.
153
154 exec 3>&2 4>/dev/null 5>&2
155
156 notify () {
157 colour=$1 message=$2
158 echo $message >&4
159 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
160 }
161
162 fail () {
163 notify 1 "!!! $*"
164 exit 1
165 }
166
167 warn () {
168 case $build in
169 release) fail "$*" ;;
170 *) notify 5 "??? $*" ;;
171 esac
172 }
173
174 info () {
175 notify 6 "--- $*"
176 }
177
178 assign () {
179 info "$1 = $2"
180 eval "$1=$2"
181 }
182
183 runx () {
184 notify 2 "+++ $*"
185 "$@" 2>&3 || fail "$1: exit $?"
186 }
187
188 run () { runx "$@" >&3; }
189
190 yesno () {
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.
205 while [ ! -f configure.ac -a ! -f configure.in -a \
206 ! -f .links -a ! -d .git ]; do
207 case "$(pwd)" in
208 /)
209 fail "couldn't find top-level directory"
210 ;;
211 esac
212 cd ..
213 done
214 assign srcpath $(pwd)
215
216 ## Build any necessary qualifiers.
217 qual= sep=.
218 case ${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" ;;
221 esac
222
223 ## Construct the output directory.
224 assign releasepath $srcpath/dist-$build$qual
225 chmod -R +w $releasepath 2>/dev/null || :
226 rm -rf $releasepath 2>/dev/null || :
227 mkdir $releasepath
228 case $verbose in
229 no)
230 exec 4>$releasepath/mdw-build.log 3>&4 ||
231 fail "Failed to create log."
232 ;;
233 esac
234
235 ## Do we have a Git repository?
236 case "$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."
242 checkout=no gitver=none
243 ;;
244 yes,yes,yes)
245 cd $srcpath
246 [ "$(git ls-files -m)" = "" ] ||
247 warn "working tree has uncommitted changes"
248 ;;
249 esac
250
251 ## Is there Debian build equipment?
252 case "$debian,$(yesno [ -d $srcpath/debian ])" in
253 yes,no)
254 info "No debian directory found."
255 debian=no debver=none
256 ;;
257 no,*)
258 debver=none
259 ;;
260 yes,yes)
261 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p' | tr \~ -)
262 debsrc=$(dpkg-parsechangelog | sed -n 's/^Source: //p')
263 debname=$(git config user.name) debemail=$(git config user.email)
264 ;;
265 esac
266
267 ## Maybe check out a copy of the source.
268 case "$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 ;;
277 esac
278
279 ## Check the version number.
280 hack_dch_p=no
281 case "$gitver,$debver" in
282 none,* | *,none)
283 ;;
284 *)
285 if [ "$gitver" != "$debver" ]; then
286 warn "Git version $gitver doesn't match Debian version $debver"
287 hack_dch=yes
288 fi
289 ;;
290 esac
291
292 ## Maybe refresh the build machinery.
293 case "$setup" in
294 yes)
295 run $setupcmd
296 ;;
297 esac
298
299 ## Initialize the build directory.
300 case "$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 ;;
319 esac
320
321 ## Discover the release name.
322 cat >find-distdir.mk <<'EOF'
323 include Makefile
324 print-distdir:
325 @echo >&3 $(distdir)
326 EOF
327 assign distdir \
328 $({ make -f find-distdir.mk print-distdir >/dev/null 2>&1; } 3>&1)
329
330 ## Get a tarball distribution.
331 case "$distcheck" in
332 yes)
333 run make $makeopts distcheck
334 ;;
335 no)
336 run make $makeopts dist
337 ;;
338 esac
339
340 cd $releasepath
341
342 case $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 ;;
349 esac
350
351 run mv $buildpath/$distdir.tar.gz .
352 case $build in
353 release)
354 run gpg -u$(mdw-conf releasekey) -ab -o$distdir.tar.gz.gpg \
355 $distdir.tar.gz
356 ;;
357 esac
358
359 ## Maybe build the Debian packages.
360 case "$debian" in
361 yes)
362 run tar xvfz $distdir.tar.gz
363 cd $distdir
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
375 EOF
376 mv debian/changelog.new debian/changelog
377 ;;
378 esac
379 run dpkg-buildpackage -k$(mdw-conf releasekey)
380 ;;
381 esac
382
383 ## Maybe upload Debian packages.
384 cd $releasepath
385 case "$upload,$build" in
386 yes,test)
387 info "Test build: not uploading."
388 ;;
389 yes,release)
390 run rsync $distdir.tar.gz $distdir.tar.gz.gpg \
391 $(mdw-conf upload-target ftp.distorted.org.uk:~ftp/pub/mdw/)
392 case "$debian" in
393 yes)
394 run dput -f $(mdw-conf dput-target distorted) *.changes
395 ;;
396 esac
397 esac
398
399 ## Tidy up.
400 case "$clean" in
401 yes)
402 rm -rf $releasepath/$distdir
403 rm -rf $releasepath/_source
404 rm -rf $releasepath/_build
405 ;;
406 esac
407
408 ## Done.
409 info "All OK."
410
411 ###----- That's all, folks --------------------------------------------------