chiark / gitweb /
bin/disorder-notify: Rewrite and take over the functionality of `media-keys'.
[profile] / bin / mdw-sbuild-server
CommitLineData
b94830d9
MW
1#! /bin/sh -e
2###
3### Build a Debian package on supported architectures
4###
5### (c) 2016 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### Configuration.
26
27unset buildroot default_targets parallel
28for i in \
29 "/etc/mdw-sbuild.conf" \
30 "${XDG_CONFIG_HOME-$HOME/.config}/mdw-sbuild.conf"
31do
32 if [ -f "$i" ]; then . "$i"; fi
33done
34: ${buildroot=$HOME/build}
35: ${default_targets="wheezy-amd64 wheezy-i386"}
b94830d9
MW
36: ${DEB_BUILD_OPTIONS=parallel=4}; export DEB_BUILD_OPTIONS
37
38###--------------------------------------------------------------------------
39### Some utilities.
40
41prog=${0##*/}
42
43fail () { echo >&2 "$prog: $*"; exit 1; }
44usage () { echo "usage: $prog [-ain] [-t TARGET] COMMAND [ARGUMENTS ...]"; }
45fail_usage () { usage >&2; exit 1; }
46
47want_1 () {
48 what=$1 pat=$2 type=$3; shift 3
49 for i in "$@"; do
50 [ $type "$i" ] || fail "$what not found: \`$i'"
51 done
52 case $# in
53 1) ;;
54 *) fail "expected exactly one $what matching \`$pat', but found $#" ;;
55 esac
56 echo "$1"
57}
58
ba9719eb
MW
59run () {
60 case $notreally in
61 t) echo "+ $*" ;;
5b950572 62 nil) nice "$@" ;;
ba9719eb
MW
63 esac
64}
65
66decor () {
67 tag=$1 marker=$2
68 while IFS= read -r line; do
69 printf "%-21s %c %s\n" "$tag" "$marker" "$line"
70 done
71}
72
b94830d9
MW
73###--------------------------------------------------------------------------
74### Parse options.
75
ba9719eb 76bogusp=nil archp=nil indepp=nil keepon=nil notreally=nil
5e9e7146 77unset targets dbpargs
b94830d9 78
5e9e7146 79while getopts "haint:A:" opt; do
b94830d9
MW
80 case $opt in
81 h)
82 usage
83 cat <<EOF
84
85Options:
86 -h Show this help text.
87 -a Build only architecture-dependent packages.
88 -i Build only architecture-neutral packages.
ba9719eb 89 -k Keep going even if one fails.
b94830d9
MW
90 -n Don't actually do the build.
91 -t TARGET Build in TARGET build environment.
5e9e7146 92 -A ARGS Pass ARGS to \`dpkg-buildpackage'.
b94830d9
MW
93
94Commands available:
95
96 dir PROJECT/VERSION
97 Return a freshly-made directory for the source code to
98 go in.
99
100 build BUILDDIR
101 Build the package placed in BUILDDIR, which should contain
102 exactly one \`.dsc' file, and whatever source archive files
103 are necessary.
104EOF
105 exit
106 ;;
107 a) archp=t ;;
108 i) indepp=t ;;
ba9719eb
MW
109 k) keepon=t ;;
110 n) notreally=t ;;
b94830d9 111 t) targets="${targets+$targets }$OPTARG" ;;
5e9e7146 112 A) dbpargs="${dbpargs+$dbpargs }$OPTARG" ;;
b94830d9
MW
113 *) bogusp=nil ;;
114 esac
115done
116shift $(( $OPTIND - 1 ))
117
118case $bogusp in t) fail_usage ;; esac
119case $archp,$indepp in nil,nil) archp=t indepp=t ;; esac
120case ${targets+t} in t) ;; *) targets=$default_targets ;; esac
121
122###--------------------------------------------------------------------------
123### Main work.
124
125case "$#,$1" in
126 0,*) fail_usage ;;
127 *,*,*) fail "bad command name \`$1'" ;;
128
129 2,dir)
130 ## dirname PROJECT/VERSION
131
132 ## Try to create a fresh build directory.
133 dist=$2
134 case "$dist" in */*/*) fail "bad distribution name \`$dist'" ;; esac
135 proj=${dist%/*} ver=${dist#*/}
136 cd "$buildroot"
137 mkdir -p "$proj"
138 cd "$proj"
139 i=0
140 winp=nil
141 while [ $i -lt 50 ]; do
142 i=$(( $i + 1 ))
143
144 ## Find a sequence number different from all of the existing builds of
145 ## this version.
146 nn=1
147 for j in "$ver#"*; do
148 case "$j" in "$ver#*") break ;; esac
149 n=${j##*\#}
150 if [ $nn -le $n ]; then nn=$(( $n + 1 )); fi
151 done
152
153 ## Try to make the build directory. This might not work if we're
154 ## racing with another process, but that's why we're trying in a loop.
155 if mkdir "$ver#$nn" >/dev/null 2>&1; then
156 winp=t
6cf97414 157 cd "$ver#$nn"
b94830d9
MW
158 break
159 fi
160
161 ## Make sure it actually failed because a directory appeared, rather
162 ## than for some other reason.
163 [ -e "$ver#$nn" ] || \
164 fail "unexpectedly couldn't create \`$buildroot/$dist#$nn'"
165 done
166
167 ## Make sure we actually succeeded.
168 case $winp in t) ;; *) fail "failed to create build directory" ;; esac
169
6cf97414
MW
170 ## Make an empty directory for dependency packages.
171 mkdir -p pkgs/
172
b94830d9
MW
173 ## Done.
174 echo "$buildroot/$dist#$nn"
175 ;;
176
177 *,dir)
178 echo >&2 "usage: $prog dir PROJECT/VERSION"; exit 1 ;;
179
180 2,build)
181 ## build BUILDDIR
182
183 ## Track down the build directory.
184 builddir=$2
185 cd "$builddir"
186 dsc=$(want_1 "file" "*.dsc" -f *.dsc)
187
188 ## Figure out which targets need building. If the `.dsc' file isn't
189 ## telling, assume it needs building everywhere and let sbuild(1) sort
190 ## out the mess.
191 os=$(dpkg-architecture -qDEB_HOST_ARCH_OS)
192 unset first rest; anyp=nil depp=nil allp=nil
193 wantarchs=$(sed -n '/^[Aa]rchitecture:/ s/^[^:]*: *//p' "$dsc")
194 : ${wantarchs:=any}
be0b4ef6 195 unset buildarchs buildarchs_seen=:
b94830d9
MW
196
197 ## Work through the available targets assigning builds to them. This is
198 ## actually a little tricky.
199 for t in $targets; do
200
92b05c85
MW
201 ## Dissect the target name.
202 suite=${t%%-*} archs=${t#*-}
203 case $archs in
204 */*) target=${archs%/*} host=${archs#*/} ;;
205 *) target=$archs host=$archs; t=$suite-$target/$host ;;
206 esac
be0b4ef6
MW
207 case $buildarchs_seen in
208 *:$target:*)
209 ;;
210 *)
211 buildarchs=${buildarchs+$buildarchs }$target
212 buildarchs_seen=$buildarchs_seen$target:
213 ;;
214 esac
92b05c85 215
b94830d9
MW
216 ## Work through the architectures which we can build.
217 for arch in $wantarchs; do
218 case $arch in
219 all)
220 ## Package suitable for all architectures.
221
222 ## If we don't want to build architecture-neutral packages then
223 ## there's nothing to do.
224 case $indepp in nil) continue ;; esac
225
226 ## Pick this up if nobody has volunteered. However, we should be
227 ## ready to let some other architecture build this if it's going
228 ## to build some architecture-dependent package too.
229 case $anyp in nil) first=$t anyp=t allp=t ;; esac
230 ;;
231 *)
232 ## There's at least one architecture-specific package.
233
28f48d70 234 ## If we don't want to build architecture-specific packages then
b94830d9
MW
235 ## there's nothing to do.
236 case $archp in nil) continue ;; esac
237
238 ## If we can't build it then we shouldn't try.
92b05c85 239 if ! dpkg-architecture -a"$os-$target" -i"$arch"; then
b94830d9
MW
240 continue
241 fi
242
243 ## Decide whether we should take responsibility for the
244 ## architecture-neutral packages. If nobody's claimed them yet,
245 ## or the previous claimant wasn't building architecture-specific
246 ## packages, we should take over.
247 case $depp in
248 nil) first=$t depp=t anyp=t ;;
249 t) rest="${rest+$rest }$t" ;;
250 esac
251 ;;
252 esac
253 done
254 done
255
256 ## If we never found a match then we can't do anything.
257 case $anyp in nil) echo "$prog: no packages to build"; exit 0 ;; esac
258
259 ## Figure out the right options to use.
260 case $indepp in
261 t) firstopt="--arch-all" ;;
262 nil) firstopt="--no-arch-all" ;;
263 esac
264 case $archp in
265 t) ;;
ea91eab4 266 nil) firstopt="$firstopt --no-arch-any" ;;
b94830d9
MW
267 esac
268
6cf97414
MW
269 ## Sort out the additional packages. This is rather annoying, because
270 ## sbuild(1) does this in a really stupid way.
271 rm -rf pkgs.*
272 for a in $buildarchs; do
273 mkdir pkgs.$a
274 for f in $(dpkg-scanpackages -a$a pkgs/ |
275 sed -n '/^Filename: /s///p')
276 do
277 ln $f pkgs.$a/
278 done
279 done
280
ba9719eb
MW
281 ## Build the builds sequentially. Tests can conflict with each other,
282 ## e.g., over port numbers.
283 rc=0 buildopt=$firstopt
284 for t in $first $rest; do
285 host=${t##*/} full=${t%/*}
286 suite=${full%%-*} target=${full#*-}
287
288 ## And we're ready to go.
289 exec 3>&1
290 thisrc=$(
291 { { { { set +e
292 run sbuild --extra-package=$pkgs.$target \
293 --dist=$suite --build=$host --host=$target \
294 --chroot=$suite-$host --verbose $buildopt $dsc \
5e9e7146 295 ${dbpargs+--debbuildopts="$dbpargs"} \
ba9719eb
MW
296 3>&- 4>&- 5>&-
297 echo $? >&5
298 } |
299 decor "$full" "|" >&4; } 2>&1 |
300 decor "$full" "*" >&4; } 4>&1 |
301 cat -u >&3; } 5>&1 </dev/null)
302 exec 3>&-
303 case $thisrc in 0) ;;
304 *)
305 echo failed rc=$thisrc >$stat; rc=$thisrc
306 case $keepon in nil) break ;; esac
307 ;;
308 esac
309 buildopt=--no-arch-all
310 done
b94830d9
MW
311 exit $rc
312 ;;
313 build,*)
314 echo >&2 "usage: $prog build BUILDDIR"; exit 1 ;;
315
316 *)
317 fail "unknown command \`$1'"
318 ;;
319esac
320
321###----- That's all, folks --------------------------------------------------