Debian Codegraph @

pbuilder-satisfydepends-funcs

raw
1 #! /bin/bash
2 # pbuilder -- personal Debian package builder
3 # Copyright © 2001-2007 Junichi Uekawa <[email protected]>
4 # 2007 Loïc Minier <[email protected]>
5 # 2015-2021 Mattia Rizzolo <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ###############################################################################
21
22 # module to satisfy build dependencies; common functions
23
24 . "${BASH_SOURCE%/*}/pbuilder-modules"
25
26 package_versions() {
27 local PACKAGE="$1"
28 LC_ALL=C $CHROOTEXEC apt-cache show "$PACKAGE" | sed -n 's/^Version: //p'
29 }
30
31 candidate_version() {
32 local PACKAGE="$1"
33 LC_ALL=C $CHROOTEXEC apt-cache policy "$PACKAGE" | sed -n 's/ *Candidate: //p'
34 }
35
36 checkbuilddep_versiondeps() {
37 local PACKAGE="$1"
38 local COMPARESTRING="$2"
39 local DEPSVERSION="$3"
40 local PACKAGEVERSIONS=$( package_versions "$PACKAGE" | xargs)
41 # no versioned provides.
42 if [ "${FORCEVERSION}" = "yes" ]; then
43 return 0;
44 fi
45 for PACKAGEVERSION in $PACKAGEVERSIONS ; do
46 if dpkg --compare-versions "$PACKAGEVERSION" "$COMPARESTRING" "$DEPSVERSION"; then
47 # satisfies depends
48 return 0;
49 fi
50 done
51 echo " Tried versions: $PACKAGEVERSIONS"
52 # cannot satisfy depends
53 return 1;
54 }
55
56 get_source_control_field() {
57 get822field "$1" "$DEBIAN_CONTROL"
58 }
59
60 get_build_depends_field() {
61 get_source_control_field "$1" | sed -n '
62 # Note the h; b loop; here so that there is no leading
63 # newline in the hold space
64 h
65 b loop
66 : store
67 H
68 : loop
69 $ b done
70 n
71 b store
72 : done
73 x
74 # output on single line
75 s/\n//g
76 # change series of tabs and spaces into a space
77 s/[\t ]\+/ /g
78 # normalize space before and after commas
79 s/ *, */, /g
80 # normalize space before and after pipes
81 s/ *| */ | /g
82 # normalize space before and after parentheses
83 s/ *( */ (/g
84 s/ *) */)/g
85 # normalize space before and after brackets
86 s/ *\[ */ [/g
87 s/ *\] */]/g
88 # normalize space before and after restriction list angle brackets
89 s/\([^(< ]\) *< */\1 </g
90 s/\([^(> ]\) *> */\1>/g
91 # normalize space between restriction lists
92 s/> *</> </g
93 # normalize space after exclamation mark
94 s/! */!/g
95 # normalize space between operator and version
96 s/(\(>>\|>=\|>\|==\|=\|<=\|<<\|<\|!=\) *\([^)]*\))/(\1 \2)/g
97 # normalize space at beginning and end of line
98 s/^ *//
99 s/ *$//
100 p'
101 }
102
103 get_build_deps() {
104 local output
105
106 output="$(get_build_depends_field "Build-Depends")"
107 output="${output%, }"
108 case "$BINARY_ARCH" in
109 any)
110 output="${output:+$output, }$(get_build_depends_field "Build-Depends-Indep")"
111 output="${output%, }"
112 output="${output:+$output, }$(get_build_depends_field "Build-Depends-Arch")"
113 output="${output%, }"
114 ;;
115 binary)
116 output="${output:+$output, }$(get_build_depends_field "Build-Depends-Arch")"
117 output="${output%, }"
118 ;;
119 all)
120 output="${output:+$output, }$(get_build_depends_field "Build-Depends-Indep")"
121 output="${output%, }"
122 ;;
123 esac
124 echo "$output"
125 }
126
127 get_build_conflicts() {
128 local output
129
130 output="$(get_build_depends_field "Build-Conflicts")"
131 output="${output%, }"
132 case "$BINARY_ARCH" in
133 any)
134 output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Indep")"
135 output="${output%, }"
136 output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Arch")"
137 output="${output%, }"
138 ;;
139 binary)
140 output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Arch")"
141 output="${output%, }"
142 ;;
143 all)
144 output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Indep")"
145 output="${output%, }"
146 ;;
147 esac
148 echo "$output"
149 }
150
151 # filter out dependencies sent on input not for this arch; deps can have
152 # multiple lines; output is on a single line or "" if empty
153 filter_arch_deps() {
154 local arch="$1"
155 local INSTALLPKGMULTI
156 local INSTALLPKG
157
158 # split on ","
159 sed 's/[[:space:]]*,[[:space:]]*/\n/g' |
160 while read INSTALLPKGMULTI; do
161 echo "$INSTALLPKGMULTI" |
162 # split on "|"
163 sed 's/[[:space:]]*|[[:space:]]*/\n/g' |
164 while read INSTALLPKG; do
165 if echo "$INSTALLPKG" | grep -q '\['; then
166 if checkbuilddep_archdeps "$INSTALLPKG" "$arch"; then
167 continue
168 fi
169 fi
170 # output the selected package
171 echo "$INSTALLPKG"
172 done |
173 # remove the arch list and add " | " between entries
174 sed 's/\[.*\]//; $,$! s/$/ |/' |
175 xargs --no-run-if-empty
176 done |
177 # add ", " between entries
178 sed '$,$! s/$/,/' |
179 xargs --no-run-if-empty
180 }
181
182 # filter out dependencies sent on input not for selected build profiles; deps
183 # can have multiple lines; output is on a single line or "" if empty
184 filter_restriction_deps() {
185 local profiles="$1"
186 local INSTALLPKGMULTI
187 local INSTALLPKG
188
189 # split on ","
190 sed 's/[[:space:]]*,[[:space:]]*/\n/g' |
191 while read INSTALLPKGMULTI; do
192 echo "$INSTALLPKGMULTI" |
193 # split on "|"
194 sed 's/[[:space:]]*|[[:space:]]*/\n/g' |
195 while read INSTALLPKG; do
196 if echo "$INSTALLPKG" | grep -q '<.*>'; then
197 if checkbuilddep_restrictiondeps "$INSTALLPKG" "$profiles"; then
198 continue
199 fi
200 fi
201 # output the selected package
202 echo "$INSTALLPKG"
203 done |
204 # remove the restriction list and add " | " between entries
205 sed 's/<[^)(]*>//; $,$! s/$/ |/' |
206 xargs --no-run-if-empty
207 done |
208 # add ", " between entries
209 sed '$,$! s/$/,/' |
210 xargs --no-run-if-empty
211 }
212
213 checkbuilddep_archdeps() {
214 # returns FALSE on INSTALL
215 local INSTALLPKG="$1"
216 local ARCH="$2"
217 # architectures listed between [ and ] for this dep
218 local DEP_ARCHES="$(echo "$INSTALLPKG" | sed -e 's/.*\[\(.*\)\].*/\1/' -e 'y|/| |')"
219 local PKG="$(echo "$INSTALLPKG" | cut -d ' ' -f 1)"
220 local USE_IT
221 local IGNORE_IT
222 local INCLUDE
223 # Use 'dpkg-architecture' to support architecture wildcards.
224 for d in $DEP_ARCHES; do
225 if echo "$d" | grep -q '!'; then
226 d="$(echo "$d" | sed 's/!//')"
227 if dpkg-architecture -a"$ARCH" -i"$d" -f; then
228 IGNORE_IT="yes"
229 fi
230 else
231 if dpkg-architecture -a"$ARCH" -i"$d" -f; then
232 USE_IT="yes"
233 fi
234 INCLUDE="yes"
235 fi
236 done
237 if [ $IGNORE_IT ] && [ $USE_IT ]; then
238 printf "W: inconsistent arch restriction on %s: " "$PKG" >&2
239 printf "%s depedency\n" "$DEP_ARCHES" >&2
240 fi
241 if [ $IGNORE_IT ] || ( [ $INCLUDE ] && [ ! $USE_IT ] ); then
242 return 0
243 fi
244 return 1
245 }
246
247 checkbuilddep_restrictiondeps() {
248 # returns FALSE on INSTALL
249 local INSTALLPKG="$1"
250 local PROFILES="$2"
251 # restrictions listed between < and > for this dep
252 local DEP_RESTRICTIONS
253 DEP_RESTRICTIONS="$(echo "$INSTALLPKG" | sed -e 's/[^<]*<\(.*\)>.*/\1/' -e 's/<[^<]*<//' -e 'y|/| |' -e 's/>\s\+</;/g')"
254 local PKG
255 PKG="$(echo "$INSTALLPKG" | cut -d ' ' -f 1)"
256 local SEEN_PROFILE
257 local PROFILE
258 local NEGATED
259 local FOUND
260 IFS=';' read -ra RESTRLISTS <<< "$DEP_RESTRICTIONS"
261 for restrlist in "${RESTRLISTS[@]}"; do
262 SEEN_PROFILE="yes"
263 for restr in $restrlist; do
264 if [[ "$restr" == '!'* ]]; then
265 NEGATED="yes"
266 PROFILE=${restr#!}
267 else
268 NEGATED="no"
269 PROFILE=${restr}
270 fi
271 FOUND="no"
272 for p in $PROFILES; do
273 if [ "$p" = "$PROFILE" ]; then
274 FOUND="yes"
275 break
276 fi
277 done
278 if [ "$FOUND" = "$NEGATED" ]; then
279 SEEN_PROFILE="no"
280 break
281 fi
282 done
283
284 if [ "$SEEN_PROFILE" = "yes" ]; then
285 return 1
286 fi
287 done
288 return 0
289 }
290
291 checkbuilddep_provides() {
292 local PACKAGENAME="$1"
293 # PROVIDED needs to be used outside of this function.
294 PROVIDED=$($CHROOTEXEC apt-cache showpkg "$PACKAGENAME" \
295 | awk '{p=0}/^Reverse Provides:/,/^$/{p=1}{if(p && ($0 !~ "Reverse Provides:")){PACKAGE=$1}} END{print PACKAGE}')
296 }
297
298 # returns either "package=version", to append to an apt-get install line, or
299 # package
300 versioneddep_to_aptcmd() {
301 local INSTALLPKG="$1"
302
303 local PACKAGE
304 local PACKAGE_WITHVERSION
305 local PACKAGEVERSIONS
306 local CANDIDATE_VERSION
307 local COMPARESTRING
308 local DEPSVERSION
309
310 PACKAGE="$(echo "$INSTALLPKG" | sed -e 's/^[/]*//' -e 's/[[/(].*//')"
311 PACKAGE_WITHVERSION="$PACKAGE"
312
313 # if not versionned, we skip directly to outputting $PACKAGE
314 if echo "$INSTALLPKG" | grep '[(]' > /dev/null; then
315 # package versions returned by APT, in reversed order
316 PACKAGEVERSIONS="$( package_versions "$PACKAGE" | tac | xargs )"
317 CANDIDATE_VERSION="$( candidate_version "$PACKAGE" )"
318
319 COMPARESTRING="$(echo "$INSTALLPKG" | tr "/" " " | sed 's/^.*( *\(<<\|<=\|>=\|=\|<\|>>\|>\) *\(.*\)).*$/\1/')"
320 DEPSVERSION="$(echo "$INSTALLPKG" | tr "/" " " | sed 's/^.*( *\(<<\|<=\|>=\|=\|<\|>>\|>\) *\(.*\)).*$/\2/')"
321 # if strictly versionned, we skip to outputting that version
322 if [ "=" = "$COMPARESTRING" ]; then
323 PACKAGE_WITHVERSION="$PACKAGE=$DEPSVERSION"
324 else
325 # try the candidate version, then all available versions (asc)
326 for VERSION in $CANDIDATE_VERSION $PACKAGEVERSIONS; do
327 if dpkg --compare-versions "$VERSION" "$COMPARESTRING" "$DEPSVERSION"; then
328 if [ "$VERSION" != "$CANDIDATE_VERSION" ]; then
329 PACKAGE_WITHVERSION="$PACKAGE=$VERSION"
330 fi
331 break;
332 fi
333 done
334 fi
335 fi
336
337 echo "$PACKAGE_WITHVERSION"
338 }
339
340 print_help() {
341 # print out help message
342 cat <<EOF
343 pbuilder-satisfydepends -- satisfy dependencies
344 Copyright 2002-2007 Junichi Uekawa <[email protected]>
345
346 --help give help
347 --control specify control file (debian/control, *.dsc)
348 --chroot operate inside chroot
349 --binary-all include binary-all
350 --binary-arch include binary-arch only
351 --binary-indep include binary-indep only
352 --eatmydata wrap the chroots commands with eatmydata
353
354 Debugging options:
355 --force-version skip version check.
356 --continue-fail continue even when failed.
357 --internal-chrootexec specify the command to execute instead of \`chroot\`
358 --echo echo mode, do nothing. (--force-version required for most operation)
359
360 EOF
361 }
362