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