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