]> git.proxmox.com Git - debcargo-conf.git/blame - build.sh
vars.sh.frag: Replace use of which with type -p
[debcargo-conf.git] / build.sh
CommitLineData
987288b3 1#!/bin/bash
5e18ef4f
XL
2# Build a packaged crate.
3#
4# This script is not run directly, but linked into ./build/ when running one of
5# the other scripts, so you should do that instead of running this directly.
6#
7# Usage: [REALVER=<EXACTVER>] ./build.sh <CRATE> [<SEMVER>] [<EXTRA DEPENDENCY DEB> ...]
8#
9# Envvars:
10# IGNORE_MISSING_BUILD_DEPS=1
11# Don't abort on missing build deps. You'll need
12# this when giving extra dependency debs.
13# DISTRIBUTION=$suite
14# Release to something other than unstable, e.g. experimental
15# CHROOT=$chroot
16# Build using another schroot than debcargo-unstable-amd64-sbuild
1057693c
XL
17set -e
18
9cc53ba7
XL
19SCRIPTDIR="$(dirname $(readlink -f "$0"))"
20
1057693c 21abort() { local x=$1; shift; for i in "$@"; do echo >&2 "$0: abort: $i"; done; exit "$x"; }
9cc53ba7 22report() { for i in "$@"; do echo >&2 "debcargo-conf builder: $i"; done; }
1057693c
XL
23
24if [ "$(basename "$PWD")" != "build" ]; then
25 abort 1 "This script is only meant to be run from the build/ directory."
26fi
27
28if [ -n "$DEBCARGO" ]; then
29 true
30elif which debcargo >/dev/null; then
31 DEBCARGO=$(which debcargo)
32elif [ -f "$HOME/.cargo/bin/debcargo" ]; then
33 DEBCARGO="$HOME/.cargo/bin/debcargo"
34else
35 abort 1 "debcargo not found, run \`cargo install debcargo\` or set DEBCARGO to point to it"
36fi
37
38CRATE="$1"
39VER="$2"
987288b3
XL
40if test -z "$VER" -o -f "$VER"; then
41 VER=""
42 shift
43else
44 shift 2
45fi
1057693c
XL
46DISTRIBUTION="${DISTRIBUTION:-unstable}"
47
48PKGNAME=$($DEBCARGO deb-src-name "$CRATE" $VER || abort 1 "couldn't find crate $CRATE")
49DEBVER=$(dpkg-parsechangelog -l $PKGNAME/debian/changelog -SVersion)
50DEBSRC=$(dpkg-parsechangelog -l $PKGNAME/debian/changelog -SSource)
51DEBDIST=$(dpkg-parsechangelog -l $PKGNAME/debian/changelog -SDistribution)
52DEB_HOST_ARCH=$(dpkg-architecture -q DEB_HOST_ARCH)
9cc53ba7
XL
53SRCNAME="${DEBSRC}_${DEBVER}"
54BUILDNAME="${DEBSRC}_${DEBVER}_${DEB_HOST_ARCH}"
43b8c908 55if [ -z "$CHROOT" ]; then
18699184 56 if schroot -i -c "debcargo-unstable-${DEB_HOST_ARCH}-sbuild" >/dev/null 2>&1; then
43b8c908 57 CHROOT="debcargo-unstable-${DEB_HOST_ARCH}-sbuild"
18699184 58 elif schroot -i -c "unstable-${DEB_HOST_ARCH}-sbuild" >/dev/null 2>&1; then
43b8c908
XL
59 CHROOT="unstable-${DEB_HOST_ARCH}-sbuild"
60 echo >&2 "Automatically using sbuild chroot unstable-${DEB_HOST_ARCH}-sbuild; however it's"
61 echo >&2 "strongly recommended to create a separate chroot debcargo-unstable-${DEB_HOST_ARCH}-sbuild"
62 echo >&2 "so your builds won't have to re-download & re-install cargo, rustc, and llvm every time."
63 echo >&2 "See README.rst section \"Build environment\" for details."
64 sleep 1
18699184 65 elif [ "$SOURCEONLY" != 1 ]; then
43b8c908
XL
66 abort 1 "could not automatically find a suitable chroot; set CHROOT"
67 fi
1057693c
XL
68fi
69
70shouldbuild() {
71 local dst="$1"
72 local src="$2"
73 test ! -e "$dst" -o "$src" -nt "$dst"
74}
75
9cc53ba7 76if shouldbuild "$SRCNAME.dsc" "$PKGNAME/debian/changelog" ]; then
2f1d8dc5
XL
77 if [ "$REUSE_EXISTING_ORIG_TARBALL" = 1 ]; then
78 UPSVER="${DEBVER%-*}"
79 mv "${DEBSRC}_${UPSVER}.orig.tar.gz" "${DEBSRC}_${UPSVER}.orig.tar.gz.new"
551c3a83 80 apt-get -t unstable source --download-only "${DEBSRC}" # "=${DEBVER}"
2f1d8dc5 81 # check that old tarball contains same contents as new tarball
a0c73b1a
XL
82 if ! diff -ru \
83 --label "${DEBSRC}_${UPSVER}.orig.tar.gz.new" \
84 <(zcat "${DEBSRC}_${UPSVER}.orig.tar.gz.new" | tar -tvvf-) \
85 --label "${DEBSRC}_${UPSVER}.orig.tar.gz" \
86 <(zcat "${DEBSRC}_${UPSVER}.orig.tar.gz" | tar -tvvf-); then
87 read -p "contents differ, continue with old tarball or abort? [y/N] " x
88 if [ "$x" != "y" ]; then exit 1; fi
89 fi
551c3a83
XL
90 # extract old tarball into new directory, to avoid "modified files" problems with dpkg-source later
91 ( cd "$PKGNAME" && dpkg-source --after-build . && tar --strip-components=1 -xf "../${DEBSRC}_${UPSVER}.orig.tar.gz" )
2f1d8dc5 92 fi
551c3a83 93 ( cd "$PKGNAME" && dpkg-buildpackage -d -S --no-sign )
1057693c
XL
94 # sign if not UNRELEASED
95 if echo "$DEBDIST" | grep -qv UNRELEASED-FIXME-AUTOGENERATED-DEBCARGO; then
9cc53ba7 96 debsign "${SRCNAME}_source.changes"
1057693c
XL
97 fi
98fi
99
d0306da4 100EXTRA_DEBS=( "$@" )
101
1057693c
XL
102check_build_deps() {
103 mkdir -p dpkg-dummy
104 if shouldbuild dpkg-dummy/status /var/cache/apt/pkgcache.bin; then
105 # pretend dpkg status file that marks all packages as installed
106 # this is because dpkg-checkbuilddeps only works on installed pkgs
d0306da4 107 ( apt-cache dumpavail -o APT::Default-Release=unstable && \
108 for i in ${EXTRA_DEBS[*]}; do apt-cache show $(echo $i | cut -d_ -f1); done ) | \
1057693c 109 sed -e 's/Package: .*/\0\nStatus: install ok installed/g' > dpkg-dummy/status
89617f5c 110 if ! test -s dpkg-dummy/status; then
9cc53ba7 111 abort 1 "couldn't generate dpkg-dummy/status, is Debian unstable in your APT sources?"
89617f5c 112 fi
1057693c 113 fi
c7386c93 114 ( cd "$PKGNAME" && dpkg-checkbuilddeps --admindir=../dpkg-dummy )
1057693c
XL
115}
116
117if ! check_build_deps; then
581335d2
XL
118 if [ "$IGNORE_MISSING_BUILD_DEPS" != 1 ]; then
119 abort 1 "Missing build-dependencies, but maybe try '{apt,cargo} update'"
120 fi
1057693c
XL
121fi
122
123if [ "$SOURCEONLY" = 1 ]; then
124 exit
125fi
126
95bfde43
FG
127if [ -n "${EXTRA_DEBS[*]}" ]; then
128 EXTRA_DEBS_SBUILD=("${EXTRA_DEBS[@]/#/--extra-package=}")
129 EXTRA_DEBS_REPO_TMP=$(mktemp -d "${SRCNAME}_REPO_XXXXXXXX")
5e18ef4f
XL
130 # trap cleans up even if user does Ctrl-C
131 # https://stackoverflow.com/a/14812383 inside "trap" avoids running handler twice
132 trap 'excode=$?; rm -rf "'"$EXTRA_DEBS_REPO_TMP"'"; trap - EXIT' EXIT HUP INT QUIT PIPE TERM
95bfde43
FG
133 # symlinks don't work here
134 ln -f "${EXTRA_DEBS[@]}" "$EXTRA_DEBS_REPO_TMP/"
135 ( cd "$EXTRA_DEBS_REPO_TMP"; apt-ftparchive packages . > Packages )
136 EXTRA_DEBS_AUTOPKGTEST_OPTS=([0]=--autopkgtest-opt=--copy="$PWD/$EXTRA_DEBS_REPO_TMP/:/tmp/$EXTRA_DEBS_REPO_TMP/" [1]=--autopkgtest-opt=--add-apt-source="deb [trusted=yes] file:///tmp/$EXTRA_DEBS_REPO_TMP ./")
137fi
138
139AUTOPKGTEST_OPTS=("--run-autopkgtest" "--autopkgtest-root-arg=" "--autopkgtest-opts=-- schroot ${CHROOT}")
9cc53ba7
XL
140if [ "$SKIP_AUTOPKGTEST" = 1 ]; then
141 AUTOPKGTEST_OPTS=()
95bfde43 142 EXTRA_DEBS_AUTOPKGTEST_OPTS=()
e3985deb
RK
143fi
144
4a5c5885
RK
145LINTIAN_OPTS=()
146if echo "$DEBDIST" | grep -q UNRELEASED-FIXME-AUTOGENERATED-DEBCARGO; then
95bfde43 147 LINTIAN_OPTS=([0]="--lintian-opt=--suppress-tags" [1]="--lintian-opt=bad-distribution-in-changes-file")
4a5c5885
RK
148fi
149
f890c341 150SBUILD_CONFIG="$SCRIPTDIR/dev/sbuildrc" sbuild --no-source --arch-any --arch-all \
987288b3
XL
151 ${CHROOT:+-c $CHROOT} \
152 ${DISTRIBUTION:+-d $DISTRIBUTION} \
95bfde43
FG
153 "${EXTRA_DEBS_SBUILD[@]}" \
154 "${EXTRA_DEBS_AUTOPKGTEST_OPTS[@]}" \
e3985deb 155 "${AUTOPKGTEST_OPTS[@]}" \
4a5c5885 156 "${LINTIAN_OPTS[@]}" \
9cc53ba7
XL
157 "$SRCNAME.dsc"
158if [ "$SKIP_AUTOPKGTEST" != 1 ]; then
159 report "analyzing autopkgtest log: $BUILDNAME.test.log"
160 # this is a bit of a hack but works OK for now, until sbuild supports %SBUILD_BUILD_DIR in --autopkgtest-opt
161 sed -ne '/autopkgtest .*: testing package .* version .*/,$p' "$BUILDNAME.build" > "$BUILDNAME.test.log"
162 "$SCRIPTDIR/dev/rust-regressions.sh" "$BUILDNAME.test.log"
163fi
95bfde43 164
9cc53ba7
XL
165changestool "$BUILDNAME.changes" adddsc "$SRCNAME.dsc"
166report "build complete: $BUILDNAME.changes"
1057693c
XL
167
168# sign if not UNRELEASED
169if echo "$DEBDIST" | grep -qv UNRELEASED-FIXME-AUTOGENERATED-DEBCARGO; then
9cc53ba7 170 debsign ${DEBSIGN_KEYID:+-k $DEBSIGN_KEYID }--no-re-sign "$BUILDNAME.changes"
1057693c 171fi