Debian Codegraph @

pbuilder-buildpackage-funcs

raw
1 #! /bin/bash
2 # pbuilder -- personal Debian package builder
3 # Copyright © 2001-2007 Junichi Uekawa <[email protected]>
4 # 2015-2016 Mattia Rizzolo <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 ###############################################################################
20
21 # functions local to pbuilder-buildpackage
22
23 . "${BASH_SOURCE%/*}/pbuilder-modules"
24
25 get822files () {
26 # get list of files listed in the Files field of a .changes or .dsc (to
27 # be specified in the first parameter) and output them one file per line
28 local type="$1"
29 local input="$2"
30 case $type in
31 changes) local field=5 ;;
32 dsc) local field=3 ;;
33 *) log.e "unexpected error in get822files()" ; exit 1 ;;
34 esac
35 echo "$input"
36 cat "$input" | awk -v dir="$(dirname "$input")" '
37 BEGIN {p=0}
38 ! /^[ \t]/ {p=0}
39 /^[ \t]*$/ {p=0}
40 /^Files:/ {p=1}
41 /^ / && p {print dir "/" $'${field}'}
42 '
43 }
44
45 function copydsc () {
46 local DSCFILE=$(get822files "dsc" "$1")
47 local TARGET="$2"
48 local FILE
49 while read -r FILE ; do
50 log.i "copying [$FILE]"
51 cp -p "$FILE" "$TARGET"
52 chmod u+rw "$TARGET/$(basename "$FILE")"
53 done <<< "$DSCFILE"
54 }
55
56 function dsc_get_basename() {
57 local dscfile="$1"
58 local with_revision="$2"
59
60 local src=$(get822field "Source" "$dscfile")
61 local source_version=$(get822field "Version" "$dscfile")
62
63 # Parse version string
64 local epoch
65 local version
66 local revision
67 case "$source_version" in
68 *:*)
69 epoch=${source_version%%:*}
70 source_version=${source_version#*:}
71 ;;
72 *)
73 ;;
74 esac
75 case "$source_version" in
76 *-*)
77 version=${source_version%-*}
78 revision=${source_version##*-}
79 ;;
80 *)
81 version=$source_version
82 ;;
83 esac
84
85 local vs
86 if [ "$with_revision" = "yes" -a -z "${revision:-}" ]; then
87 with_revision=no
88 fi
89 # Epoch always omitted
90 case "$with_revision" in
91 yes) vs="${version}-${revision}" ;;
92 no) vs="${version}" ;;
93 *) log.error "unexpected with_revision: $with_revision" ; exit 1 ;;
94 esac
95
96 echo "${src}_${vs}"
97 }
98
99 function checkbuilddep () {
100 # call satisfydepends
101 local BUILDOPT="--binary-all"
102 case "${BINARY_ARCH}" in
103 binary) BUILDOPT="--binary-arch";;
104 all) BUILDOPT="--binary-indep";;
105 *) ;;
106 esac
107 case "$(readlink -e "$PBUILDERSATISFYDEPENDSCMD")" in
108 *-apt) local control="$BUILDDIR/$(basename "$1")" ;;
109 *) local control="$1" ;;
110 esac
111 if \
112 ("$PBUILDERSATISFYDEPENDSCMD" \
113 --build-arch "$ARCHITECTURE" \
114 --host-arch "$HOST_ARCH" \
115 --control "$control" \
116 --chroot "${BUILDPLACE}" \
117 --internal-chrootexec "${CHROOTEXEC}" \
118 "${BUILDOPT}" \
119 "${PBUILDERSATISFYDEPENDSOPT[@]}"); \
120 then
121 :
122 else
123 # If asked to preserve the build place, and pbuilder-satisfydepends
124 # didn't taint it, then don't clean it when exiting.
125 if [ $? -eq 2 -a "${PRESERVE_BUILDPLACE}" = "yes" ]; then
126 trap umountproc_trap exit sighup
127 fi
128 log.e "pbuilder-satisfydepends failed."
129 exit 2
130 fi
131 # install extra packages to the chroot
132 if [ -n "$EXTRAPACKAGES" ]; then
133 $CHROOTEXEC apt-get -q -y "${APTGETOPT[@]}" install ${EXTRAPACKAGES}
134 fi
135 }
136
137 function showbuildbanner () {
138 # show some banners for building
139 log.i "pbuilder-buildpackage/"$(dpkg --print-architecture)
140 log.i "$1"
141 }
142
143 function cowprotect () {
144 # a hack for cowdancer, used when a file is edited in-place;
145 # Copy the file to create a new i-node so that hardlinked original is intact
146 for A in "$@"; do
147 if readlink -f "$A" > /dev/null; then
148 A=$(readlink -f "$A")
149 mv "$A" "$A"~
150 cp -a "$A"~ "$A"
151 rm -f "$A"~
152 fi
153 done
154 }
155
156 function addgrsecgroup () {
157 # if grsecurity with TPE (Trusted Path Execution) is active, add user to allowed group
158 [ "$(sysctl -ne kernel.grsecurity.tpe)" = "1" ] || return 0
159 [ "$(sysctl -ne kernel.grsecurity.tpe_invert)" = "1" ] || return 0
160
161 local TPEGID=$(sysctl -n kernel.grsecurity.tpe_gid)
162 log.i "Adding build user to grsec-tpe group"
163 sed -i "/\(^grsec-tpe:\|:$TPEGID:\)/d" "$BUILDPLACE/etc/group"
164 echo "grsec-tpe:x:$TPEGID:$BUILDUSERNAME" >> "$BUILDPLACE/etc/group"
165 }
166
167 function createbuilduser () {
168 # create the build user, if it is necessary and specified.
169 if [ -n "$BUILDUSERNAME" -a -n "$BUILDUSERID" ]; then
170 if [ -e $BUILDPLACE/etc/shadow ]; then p='x'; else p='*'; fi
171 if [ -e $BUILDPLACE/etc/gshadow ]; then g='x'; else g='*'; fi
172 if ! grep -q ^$BUILDUSERNAME: $BUILDPLACE/etc/passwd; then
173 cowprotect $BUILDPLACE/etc/passwd
174 echo "$BUILDUSERNAME:$p:$BUILDUSERID:$BUILDUSERID:,,,:$BUILD_HOME:/bin/sh" >> $BUILDPLACE/etc/passwd
175 fi
176 if ! grep -q ^$BUILDUSERNAME: $BUILDPLACE/etc/group; then
177 cowprotect $BUILDPLACE/etc/group
178 echo "$BUILDUSERNAME:$g:$BUILDUSERID:" >> $BUILDPLACE/etc/group
179 addgrsecgroup
180 fi
181 if [ -e $BUILDPLACE/etc/shadow ] && ! grep -q ^$BUILDUSERNAME: $BUILDPLACE/etc/shadow; then
182 cowprotect $BUILDPLACE/etc/shadow
183 echo "$BUILDUSERNAME:!:::::::" >> $BUILDPLACE/etc/shadow
184 fi
185 if [ -e $BUILDPLACE/etc/gshadow ] && ! grep -q ^$BUILDUSERNAME: $BUILDPLACE/etc/gshadow; then
186 cowprotect $BUILDPLACE/etc/gshadow
187 echo "$BUILDUSERNAME:!::" >> $BUILDPLACE/etc/gshadow
188 fi
189 unset LOGNAME || true
190 else
191 unset LOGNAME || true
192 fi
193 }
194
195 function setup_ccache() {
196 if [ -n "$CCACHEDIR" ]; then
197 log.i "Setting up ccache"
198 if ! [ -d "$BUILDPLACE/$CCACHEDIR" ]; then
199 mkdir -p "$BUILDPLACE/$CCACHEDIR"
200 fi
201 chown -R $BUILDUSERID:$BUILDUSERID "$BUILDPLACE/$CCACHEDIR"
202 CCACHE_ENV="CCACHE_DIR=$CCACHEDIR"
203 unset CCACHE_DIR
204 fi
205 }
206
207 function binNMU() {
208 if [ "$BIN_NMU" == "no" ]; then
209 return
210 fi
211 if [ -z "$BINNMU_MESSAGE" ]; then
212 log.e "No changelog message provided for binNMU entry."
213 exit 1
214 fi
215 if [ -z "$BINNMU_VERSION" ]; then
216 log.w "No version provided for binNMU entry, fall back to 1."
217 BINNMU_VERSION=1
218 fi
219 local arch=$($CHROOTEXEC dpkg-architecture -qDEB_HOST_ARCH)
220 local date=$(date -R ${BINNMU_TIMESTAMP:+-d "${BINNMU_TIMESTAMP}"})
221 log.i "Doing a binNMU, version $BINNMU_VERSION for $DISTRIBUTION/$arch to '$BINNMU_MESSAGE'"
222 local cl=$(ls "$BUILDPLACE/$BUILDDIR/$BUILDSUBDIR"/debian/changelog)
223 local tmpcl=$(mktemp pbuilder.tmpchangelog.XXXXXXXXXXX)
224 if [ ! -f "$cl" ]; then
225 log.e "Cannot open debian/changelog for binNMU version handling."
226 exit 1
227 fi
228 mv "$cl" "$tmpcl"
229 local package=$(dpkg-parsechangelog -l "$tmpcl" -c 1 --show-field Source)
230 local version=$(dpkg-parsechangelog -l "$tmpcl" -c 1 --show-field Version)
231 if [ -z "$BINNMU_MAINTAINER" ]; then
232 log.w "No maintainer provided for binNMU entry, fall back to last uploader."
233 BINNMU_MAINTAINER=$(dpkg-parsechangelog -l $tmpcl -c 1 --show-field Maintainer)
234 fi
235 if [ -z "$DISTRIBUTION" ]; then
236 log.w "No distribution provided, using the field from the last upload"
237 DISTRIBUTION=$(dpkg-parsechangelog -l "$tmpcl" -c 1 --show-field Distribution)
238 fi
239 CHANGES_BASENAME="$CHANGES_BASENAME+b$BINNMU_VERSION"
240 DEBBUILDOPTS="${DEBBUILDOPTS} -e\"$BINNMU_MAINTAINER\" -m\"$BINNMU_MAINTAINER\""
241 cat > "$cl" << EOF
242 $package ($version+b$BINNMU_VERSION) $DISTRIBUTION; urgency=low, binary-only=yes
243
244 * Binary-only non-maintainer upload for $arch; no source changes.
245 * $BINNMU_MESSAGE
246
247 -- $BINNMU_MAINTAINER $date
248
249 EOF
250 cat "$tmpcl" >> "$cl"
251 rm "$tmpcl"
252 }
253
254 cross_build_setup () {
255 if [[ "$ARCHITECTURE" = "$HOST_ARCH" ]]; then
256 # native build, nothing interesting to do here
257 return
258 fi
259 log.i "Doing a cross-architecture build"
260 log.i "Build architecture: $ARCHITECTURE"
261 log.i "Host architecture: $HOST_ARCH"
262
263 if [ "${NO_AUTO_CROSS:-}" != "really-dont-mess-with-me" ]; then
264 log.i "Setting up the environment for a cross build..."
265 if [[ "$(readlink -e "$PBUILDERSATISFYDEPENDSCMD")" != *-apt ]]; then
266 log.e "Cross building is possible only with the APT dependency resolver"
267 exit 1
268 fi
269 $CHROOTEXEC dpkg --add-architecture "$HOST_ARCH"
270 $CHROOTEXEC apt-get -q "${APTGETOPT[@]}" update
271 # preinstall libc-dev and libstdc++-dev 'cause https://bugs.debian.org/815172
272 # also, we end need to figure a versioned libstdc++-dev because the unversioned
273 # variant is a virtual package, and often there are more than one, and apt can't
274 # pick one for us
275 local gcc_ver
276 gcc_ver="$($CHROOTEXEC dpkg-query -W --showformat='${Depends}\n' gcc | sed -n 's/^.*gcc-\([0-9.]\+\)\([ ,].*\|$\)/\1/p')"
277 if [ -z "$gcc_ver" ]; then
278 log.e "Failed to determine default GCC version for installing cross build dependencies"
279 exit 1
280 fi
281 EXTRAPACKAGES="${EXTRAPACKAGES:+"$EXTRAPACKAGES" }crossbuild-essential-$HOST_ARCH libc-dev:$HOST_ARCH libstdc++${gcc_ver:+-"$gcc_ver"}-dev:$HOST_ARCH"
282 DEBBUILDOPTS="${DEBBUILDOPTS:+"$DEBBUILDOPTS" }--host-arch $HOST_ARCH"
283 if [ "${NO_AUTO_CROSS:-}" != "yes" ]; then
284 export DEB_BUILD_OPTIONS="${DEB_BUILD_OPTIONS:+"$DEB_BUILD_OPTIONS" }nocheck"
285 export DEB_BUILD_PROFILES="${DEB_BUILD_PROFILES:+"$DEB_BUILD_PROFILES" }nocheck cross"
286 fi
287 else
288 log.w "Doing a cross build, but not setting up the environment as instructed"
289 return
290 fi
291 }
292
293 function _find_additional_buildresults() {
294 local file f
295 local root="${BUILDPLACE}${BUILDDIR}/${BUILDSUBDIR}"
296 for file in "${ADDITIONAL_BUILDRESULTS[@]}"; do
297 log.d "checking [$file]..."
298 echo "$root/$file" | perl -ne 'print "$_\n" foreach glob($_)' | \
299 while read f ; do
300 if [ -e "$f" ]; then
301 echo "$f"
302 else
303 log.w "file [$file] not found"
304 fi
305 done
306 done
307 }
308
309 function export_additional_buildresults() {
310 local file
311 for file in $(_find_additional_buildresults); do
312 log.i "Trying to save additional result '${file}'"
313 chown -R "${BUILDRESULTUID}:${BUILDRESULTGID}" "${file}"
314 cp -a "${file}" "${BUILDRESULT}" || true
315 done
316 }