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