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