3 ### Build a Debian package on supported architectures
5 ### (c) 2016 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
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.
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.
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.
24 ###--------------------------------------------------------------------------
27 unset buildroot default_targets parallel
29 "/etc/mdw-sbuild.conf" \
30 "${XDG_CONFIG_HOME-$HOME/.config}/mdw-sbuild.conf"
32 if [ -f "$i" ]; then . "$i"; fi
34 : ${buildroot=$HOME/build}
35 : ${default_targets="stretch-amd64 stretch-i386"}
36 : ${DEB_BUILD_OPTIONS=parallel=4}; export DEB_BUILD_OPTIONS
38 ###--------------------------------------------------------------------------
43 fail () { echo >&2 "$prog: $*"; exit 1; }
44 usage () { echo "usage: $prog [-aiknT] [-t TARGET] [-A DBPARGS] COMMAND [ARGUMENTS ...]"; }
45 fail_usage () { usage >&2; exit 1; }
48 what=$1 pat=$2 type=$3; shift 3
50 [ $type "$i" ] || fail "$what not found: \`$i'"
54 *) fail "expected exactly one $what matching \`$pat', but found $#" ;;
68 while IFS= read -r line; do
69 printf "%-21s %c %s\n" "$tag" "$marker" "$line"
73 ###--------------------------------------------------------------------------
76 bogusp=nil archp=nil indepp=nil keepon=nil notreally=nil
79 while getopts "haint:A:T" opt; do
86 -h Show this help text.
87 -a Build only architecture-dependent packages.
88 -i Build only architecture-neutral packages.
89 -k Keep going even if one fails.
90 -n Don't actually do the build.
91 -t TARGET Build in TARGET build environment.
92 -A ARGS Pass ARGS to \`dpkg-buildpackage'.
93 -T Don't run the tests.
98 Return a freshly-made directory for the source code to
102 Build the package placed in BUILDDIR, which should contain
103 exactly one \`.dsc' file, and whatever source archive files
112 t) targets="${targets+$targets }$OPTARG" ;;
113 A) dbpargs="${dbpargs+$dbpargs }$OPTARG" ;;
115 case " $DEB_BUILD_OPTIONS " in
117 *) DEB_BUILD_OPTIONS=${DEB_BUILD_OPTIONS+"$DEB_BUILD_OPTIONS "} nocheck ;;
123 shift $(( $OPTIND - 1 ))
125 case $bogusp in t) fail_usage ;; esac
126 case $archp,$indepp in nil,nil) archp=t indepp=t ;; esac
127 case ${targets+t} in t) ;; *) targets=$default_targets ;; esac
129 ###--------------------------------------------------------------------------
134 *,*,*) fail "bad command name \`$1'" ;;
137 ## dirname PROJECT/VERSION
139 ## Try to create a fresh build directory.
141 case "$dist" in */*/*) fail "bad distribution name \`$dist'" ;; esac
142 proj=${dist%/*} ver=${dist#*/}
148 while [ $i -lt 50 ]; do
151 ## Find a sequence number different from all of the existing builds of
154 for j in "$ver#"*; do
155 case "$j" in "$ver#*") break ;; esac
157 if [ $nn -le $n ]; then nn=$(( $n + 1 )); fi
160 ## Try to make the build directory. This might not work if we're
161 ## racing with another process, but that's why we're trying in a loop.
162 if mkdir "$ver#$nn" >/dev/null 2>&1; then
168 ## Make sure it actually failed because a directory appeared, rather
169 ## than for some other reason.
170 [ -e "$ver#$nn" ] || \
171 fail "unexpectedly couldn't create \`$buildroot/$dist#$nn'"
174 ## Make sure we actually succeeded.
175 case $winp in t) ;; *) fail "failed to create build directory" ;; esac
177 ## Make an empty directory for dependency packages.
181 echo "$buildroot/$dist#$nn"
185 echo >&2 "usage: $prog dir PROJECT/VERSION"; exit 1 ;;
190 ## Track down the build directory.
193 dsc=$(want_1 "file" "*.dsc" -f *.dsc)
195 ## Figure out which targets need building. If the `.dsc' file isn't
196 ## telling, assume it needs building everywhere and let sbuild(1) sort
198 os=$(dpkg-architecture -qDEB_HOST_ARCH_OS)
199 unset first rest; anyp=nil depp=nil allp=nil
200 wantarchs=$(sed -n '/^[Aa]rchitecture:/ s/^[^:]*: *//p' "$dsc")
202 unset buildarchs buildarchs_seen=:
204 ## Work through the available targets assigning builds to them. This is
205 ## actually a little tricky.
206 for t in $targets; do
208 ## Dissect the target name.
209 suite=${t%%-*} archs=${t#*-}
211 */*) target=${archs%/*} host=${archs#*/} ;;
212 *) target=$archs host=$archs; t=$suite-$target/$host ;;
214 case $buildarchs_seen in
218 buildarchs=${buildarchs+$buildarchs }$target
219 buildarchs_seen=$buildarchs_seen$target:
223 ## Work through the architectures which we can build.
224 for arch in $wantarchs; do
227 ## Package suitable for all architectures.
229 ## If we don't want to build architecture-neutral packages then
230 ## there's nothing to do.
231 case $indepp in nil) continue ;; esac
233 ## Pick this up if nobody has volunteered. However, we should be
234 ## ready to let some other architecture build this if it's going
235 ## to build some architecture-dependent package too.
236 case $anyp in nil) first=$t anyp=t allp=t ;; esac
239 ## There's at least one architecture-specific package.
241 ## If we don't want to build architecture-specific packages then
242 ## there's nothing to do.
243 case $archp in nil) continue ;; esac
245 ## If we can't build it then we shouldn't try.
246 if ! dpkg-architecture -a"$os-$target" -i"$arch"; then
250 ## Decide whether we should take responsibility for the
251 ## architecture-neutral packages. If nobody's claimed them yet,
252 ## or the previous claimant wasn't building architecture-specific
253 ## packages, we should take over.
255 nil) first=$t depp=t anyp=t ;;
256 t) rest="${rest+$rest }$t" ;;
263 ## If we never found a match then we can't do anything.
264 case $anyp in nil) echo "$prog: no packages to build"; exit 0 ;; esac
266 ## Figure out the right options to use.
268 t) firstopt="--arch-all" ;;
269 nil) firstopt="--no-arch-all" ;;
273 nil) firstopt="$firstopt --no-arch-any" ;;
276 ## Sort out the additional packages. This is rather annoying, because
277 ## sbuild(1) does this in a really stupid way.
279 for a in $buildarchs; do
281 for f in $(dpkg-scanpackages -a$a pkgs/ |
282 sed -n '/^Filename: /s///p')
288 ## Build the builds sequentially. Tests can conflict with each other,
289 ## e.g., over port numbers.
290 rc=0 buildopt=$firstopt
291 for t in $first $rest; do
292 host=${t##*/} full=${t%/*}
293 suite=${full%%-*} target=${full#*-}
295 ## And we're ready to go.
299 run sbuild --extra-package=pkgs.$target \
300 --dist=$suite --build=$host --host=$target \
301 --chroot=$suite-$host --verbose $buildopt $dsc \
302 ${dbpargs+--debbuildopts="$dbpargs"} \
306 decor "$full" "|" >&4; } 2>&1 |
307 decor "$full" "*" >&4; } 4>&1 |
308 cat -u >&3; } 5>&1 </dev/null)
310 case $thisrc in 0) ;;
312 echo failed rc=$thisrc >$stat; rc=$thisrc
313 case $keepon in nil) break ;; esac
316 buildopt=--no-arch-all
321 echo >&2 "usage: $prog build BUILDDIR"; exit 1 ;;
324 fail "unknown command \`$1'"
328 ###----- That's all, folks --------------------------------------------------