Debian Codegraph @

pbuilder-buildpackage

raw
1 #! /bin/bash
2 # pbuilder -- personal Debian package builder
3 # Copyright © 2001-2007 Junichi Uekawa <[email protected]>
4 # 2015 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
22 export LANG=C
23 export LC_ALL=C
24 set -e
25
26 . "${BASH_SOURCE%/*}/pbuilder-checkparams"
27 . "${BASH_SOURCE%/*}/pbuilder-buildpackage-funcs"
28
29 PACKAGENAME="$1"
30 if [ ! -f "$PACKAGENAME" ]; then
31 log.e "Source package [$PACKAGENAME] does not exist"
32 exit 1;
33 fi;
34 if [ "${PACKAGENAME:(-4)}" != ".dsc" ]; then
35 log.e "The given filename does not seems to be the name of a Debian source package."
36 log.e "You need to pass a file ending with .dsc (given '$PACKAGENAME')."
37 exit 1
38 fi
39 shift
40 if [ -n "$*" ]; then
41 log.w "The following options specified after the .dsc will be ignored: [$@]"
42 fi
43
44 if [ -n "$BUILDUSERNAME" -a -n "$BUILDUSERID" ]; then
45 # Command to SU to user.
46 # LD_PRELOAD: Normal users don't have write access to build
47 # environment, so cowdancer shouldn't have to run, and fakeroot
48 # should be running later, so it shouldn't matter.
49 # LOGNAME and USER: set this to shut up some tools.
50 SUTOUSER="LD_PRELOAD= LOGNAME=$BUILDUSERNAME USER=$BUILDUSERNAME /sbin/start-stop-daemon --start --pidfile /dev/null --chuid $BUILDUSERNAME --startas /bin/sh"
51 else
52 # run the build in root
53 BUILDUSERID=0
54 BUILDUSERNAME=root
55 SUTOUSER="LOGNAME=$BUILDUSERNAME /bin/sh"
56 fi
57
58 UNSHARE=
59 case $USENETWORK in
60 yes)
61 if [ "$DEB_BUILD_ARCH_OS" = "linux" ]; then
62 # omit the warning when running on kFreeBSD or Hurd
63 log.w "pbuilder: network will not be disabled during build!"
64 fi
65 ;;
66 *)
67 if unshare -n -- /usr/lib/pbuilder/pbuilder-unshare-wrapper true; then
68 USENETWORK=no
69 UNSHARE="unshare -n -- /usr/lib/pbuilder/pbuilder-unshare-wrapper"
70 log.i "pbuilder: network access will be disabled during build"
71 else
72 USENETWORK=yes
73 if [ "$DEB_BUILD_ARCH_OS" = "linux" ]; then
74 log.w "pbuilder: unshare CLONE_NEWNET not available"
75 fi
76 log.i "pbuilder: network access is available during build!"
77 fi
78 ;;
79 esac
80
81
82 # created files should have these UID/GIDs outside of chroot.
83 BUILDRESULTUID="${BUILDRESULTUID:-${SUDO_UID:-0}}"
84 BUILDRESULTGID="${BUILDRESULTGID:-${SUDO_GID:-0}}"
85
86 echobacktime
87 extractbuildplace
88 trap umountproc_cleanbuildplace_trap exit sighup sigpipe
89
90 if [ ! -d "${BUILDRESULT}" ]; then
91 if [ -n "${BUILDRESULT}" ] ; then
92 mkdir -p "${BUILDRESULT}"
93 fi
94 if [ -d "${BUILDRESULT}" ]; then
95 log.i "created buildresult dir: ${BUILDRESULT}"
96 else
97 log.e "failed creating buildresult dir: ${BUILDRESULT}"
98 exit 1
99 fi
100 fi
101
102 if [ -z "${PBUILDER_BUILD_LOGFILE}" ]; then
103 if [ "${PKGNAME_LOGFILE}" = "yes" ]; then
104 PBUILDER_BUILD_LOGFILE="${BUILDRESULT}/$(basename "${PACKAGENAME}" .dsc)${PKGNAME_LOGFILE_EXTENSION}"
105 exec > >(tee "${PBUILDER_BUILD_LOGFILE}") 2>&1
106 PBUILDER_BUILD_LOGFILE=$(readlink -f "${PBUILDER_BUILD_LOGFILE}")
107 log.i "Using pkgname logfile"
108 echobacktime
109 fi
110 fi
111
112 # make logfile have the permissions, logfiles should already be created in all cases
113 if [ -f "${PBUILDER_BUILD_LOGFILE}" ]; then
114 chown "${BUILDRESULTUID}:${BUILDRESULTGID}" "${PBUILDER_BUILD_LOGFILE}"
115 fi
116
117 cross_build_setup
118 recover_aptcache
119 createbuilduser
120 CCACHE_ENV=
121 setup_ccache
122 SUTOUSER="$CCACHE_ENV $SUTOUSER"
123 log.i "Copying source file"
124 copydsc "$PACKAGENAME" "${BUILDPLACE}$BUILDDIR"
125 copyinputfile "${BUILDPLACE}$BUILDDIR"
126
127 # don't allow empty values of BUILDSUBDIR
128 if package_version_is_older_than dpkg-dev 1.13.19; then
129 # dpkg-source back then (2005-06-12) didn't allow to specify the
130 # extraction directory.
131 if [ -n "${BUILDSUBDIR:-}" ]; then
132 log.w "dpkg is too old in the chroot, can't use the specified BUILDSUBDIR"
133 fi
134 cannot_change_subdir=yes
135 BUILDSUBDIR=
136 fi
137 # this really needs to be retested here, as BUILDSUBDIR *needs* to be set properly
138 if [ -z "${BUILDSUBDIR:-}" ]; then
139 BUILDSUBDIR="$(dsc_get_basename "$PACKAGENAME" "no")"
140 BUILDSUBDIR="${BUILDSUBDIR//_/-}"
141 fi
142
143 log.i "Extracting source"
144 if echo "chown $BUILDUSERNAME:$BUILDUSERNAME $BUILDDIR $BUILDDIR/*" | $CHROOTEXEC /bin/bash; then
145 : # success
146 else
147 log.e "pbuilder: Failed chowning to $BUILDUSERNAME:$BUILDUSERNAME"
148 exit 1;
149 fi
150 if echo "( cd $BUILDDIR; env PATH=\"$PATH\" dpkg-source -x $(basename "$PACKAGENAME") $( [ -z "$cannot_change_subdir" ] && echo "$BUILDSUBDIR") )" | $UNSHARE $CHROOTEXEC env $SUTOUSER ; then
151 : # success
152 else
153 log.e "pbuilder: Failed extracting the source"
154 exit 1;
155 fi
156
157 RRR="$(get822field Rules-Requires-Root "$BUILDPLACE/$BUILDDIR/$BUILDSUBDIR/debian/control")"
158 if [[ "${RRR,,}" != no ]] || package_version_is_older_than dpkg-dev 1.19.0; then
159 if package_version_is_older_than dpkg-dev 1.14.7 ; then
160 DEBBUILDOPTS="${DEBBUILDOPTS:+$DEBBUILDOPTS }-rfakeroot"
161 fi
162 EXTRAPACKAGES="${EXTRAPACKAGES} fakeroot"
163 log.i "using fakeroot in build."
164 else
165 log.i "Not using root during the build."
166 fi
167
168 log.i "Installing the build-deps"
169 executehooks "D"
170 trap saveaptcache_umountproc_cleanbuildplace_trap exit sighup sigpipe
171 checkbuilddep "$PACKAGENAME"
172 save_aptcache
173 trap umountproc_cleanbuildplace_trap exit sighup sigpipe
174
175 # This potentially gets modified by binNMU so must come first
176 CHANGES_BASENAME=$(dsc_get_basename "$PACKAGENAME" "yes")
177
178 binNMU
179
180 log.i "Building the package"
181
182 executehooks "A"
183
184 ENV_PREFIX="env PATH=\"$PATH\" HOME=\"$BUILD_HOME\""
185 DPKG_COMMANDLINE="$ENV_PREFIX dpkg-buildpackage -us -uc $DEBBUILDOPTS"
186
187 if [ "${SOURCE_ONLY_CHANGES}" = "yes" ]; then
188 DPKG_COMMANDLINE="$DPKG_COMMANDLINE && $ENV_PREFIX dpkg-genchanges -S $(get_changes_options) > ../${CHANGES_BASENAME}_source.changes"
189 fi
190
191 # https://launchpad.net/bugs/816556
192 unset DISPLAY
193
194 if [ "$USENETWORK" = "no" ]; then
195 # empty /etc/resolv.conf, so software trying to resolv addresses even when
196 # no network is possible doesn't choke.
197 rm -f "$BUILDPLACE/etc/resolv.conf" # break hardlinks
198 : > "$BUILDPLACE/etc/resolv.conf"
199 # loopback access only
200 rm -f "$BUILDPLACE/etc/hosts" # break hardlinks
201
202 etchostslines=(
203 "127.0.0.1 localhost localhost.localdomain"
204 "127.0.1.1 $(hostname -f) $(hostname)"
205 "::1 ip6-localhost ip6-loopback localhost6 localhost6.localdomain6"
206 "fe00::0 ip6-localnet"
207 "ff00::0 ip6-mcastprefix"
208 "ff02::1 ip6-allnodes"
209 "ff02::2 ip6-allrouters"
210 "ff02::3 ip6-allhosts"
211 )
212 printf '%s\n' "${etchostslines[@]}" >> "$BUILDPLACE/etc/hosts"
213 fi
214
215 (
216 : Build process
217 if [ -n "$TWICE" ]; then
218 DPKG_COMMANDLINE="$DPKG_COMMANDLINE && $DPKG_COMMANDLINE"
219 fi
220 DPKG_COMMANDLINE="cd ${BUILDDIR}/$BUILDSUBDIR/ && $DPKG_COMMANDLINE"
221 log.i "Running $DPKG_COMMANDLINE"
222 echo "$DPKG_COMMANDLINE" | $UNSHARE $CHROOTEXEC env $SUTOUSER
223 ) &
224 BUILD_PID=$!
225 if [ -n "${TIMEOUT_TIME}" ]; then
226 (
227 : Timeout process
228 sleep "${TIMEOUT_TIME}"
229 log.i "Terminating build process due to timeout "
230 kill "${BUILD_PID}" || true
231 ) & # timeout process
232 KILL_WAIT_PID="kill $! || true; echo \"I: Terminate timeout process\"; "
233 else
234 KILL_WAIT_PID=""
235 fi
236
237 if ! wait "${BUILD_PID}"; then
238 trap umountproc_cleanbuildplace_trap exit sighup sigpipe
239 eval "${KILL_WAIT_PID}"
240 if [ "$USENETWORK" = "no" ]; then
241 copy_local_configuration resolv.conf
242 fi
243 log.e "Failed autobuilding of package"
244 if [ -d "${BUILDRESULT}" ]; then
245 export_additional_buildresults
246 else
247 log.e "BUILDRESULT=[$BUILDRESULT] is not a directory."
248 fi
249 executehooks "C"
250 exit 1;
251 else
252 eval ${KILL_WAIT_PID}
253 # build was successful
254 fi
255
256 if [ "$USENETWORK" = "no" ]; then
257 copy_local_configuration resolv.conf
258 fi
259
260 CHANGES_ARCHITECTURE=${HOST_ARCH:-$($CHROOTEXEC dpkg-architecture -qDEB_HOST_ARCH)}
261
262 # dpkg pre-1.18.8 generated _$arch.changes even for indep-only builds,
263 # so search for both.
264 INDEP_CHANGES_FILE="${BUILDPLACE}$BUILDDIR/${CHANGES_BASENAME}_all.changes"
265 if [ "${BINARY_ARCH}" = "all" -a -f "${INDEP_CHANGES_FILE}" ]; then
266 ARCH_CHANGES_FILE="$INDEP_CHANGES_FILE"
267 else
268 ARCH_CHANGES_FILE="${BUILDPLACE}$BUILDDIR/${CHANGES_BASENAME}_${CHANGES_ARCHITECTURE}.changes"
269
270 if [ ! -f "${ARCH_CHANGES_FILE}" ]; then
271 log.e "Missing changes file: ${ARCH_CHANGES_FILE}"
272 exit 1
273 fi
274 fi
275
276 SOURCE_CHANGES_FILE="${BUILDPLACE}$BUILDDIR/${CHANGES_BASENAME}_source.changes"
277
278 if [ "${SOURCE_ONLY_CHANGES}" = "yes" -a ! -f "${SOURCE_CHANGES_FILE}" ]; then
279 log.e "Missing source-only changes file: ${SOURCE_CHANGES_FILE}"
280 exit 1
281 fi
282
283 executehooks "B"
284
285 if [ -d "${BUILDRESULT}" ]; then
286 chown "${BUILDRESULTUID}:${BUILDRESULTGID}" "${BUILDPLACE}$BUILDDIR/"*
287 if [ "${SOURCE_ONLY_CHANGES}" = "yes" ]; then
288 FILES=$( ( get822files "changes" "$ARCH_CHANGES_FILE" && get822files "changes" "$SOURCE_CHANGES_FILE" ) | sort -u)
289 else
290 FILES=$(get822files "changes" "$ARCH_CHANGES_FILE")
291 fi
292 while read -r FILE; do
293 if [ -f "${FILE}" ]; then
294 cp -p "${FILE}" "${BUILDRESULT}" || true
295 else
296 log.w "Expected file $FILE not found, skipping."
297 fi
298 done <<< "${FILES}"
299 export_additional_buildresults
300 executehooks "I"
301 else
302 log.e "BUILDRESULT=[$BUILDRESULT] is not a directory."
303 fi
304
305 # final cleanup
306 trap umountproc_cleanbuildplace_trap exit sighup sigpipe
307 save_aptcache
308 trap cleanbuildplace_trap exit sighup sigpipe
309 umountproc
310 :
311 cleanbuildplace
312 trap - exit sighup sigpipe
313 echobacktime
314 exit 0