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