1 # Copyright © 1998 Richard Braakman
2 # Copyright © 1999 Darren Benham
3 # Copyright © 2000 Sean 'Shaleh' Perry
4 # Copyright © 2004 Frank Lichtenheld
5 # Copyright © 2006 Russ Allbery
6 # Copyright © 2007-2009 Raphaël Hertzog <hertzog@debian.org>
7 # Copyright © 2008-2009,2012-2014 Guillem Jover <guillem@debian.org>
9 # This program is free software; you may redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
26 Dpkg::Deps - parse and manipulate dependencies of Debian packages
30 The Dpkg::Deps module provides classes implementing various types of
33 The most important function is deps_parse(), it turns a dependency line in
34 a set of Dpkg::Deps::{Simple,AND,OR,Union} objects depending on the case.
38 package Dpkg
::Deps
1.07;
42 use feature
qw(current_sub);
53 use Exporter
qw(import);
56 use Dpkg
::Arch
qw(get_host_arch get_build_arch debarch_to_debtuple);
57 use Dpkg
::BuildProfiles
qw(get_build_profiles);
58 use Dpkg
::ErrorHandling
;
60 use Dpkg
::Deps
::Simple
;
61 use Dpkg
::Deps
::Union
;
64 use Dpkg
::Deps
::KnownFacts
;
68 All the deps_* functions are exported by default.
72 =item deps_eval_implication($rel_p, $v_p, $rel_q, $v_q)
74 ($rel_p, $v_p) and ($rel_q, $v_q) express two dependencies as (relation,
75 version). The relation variable can have the following values that are
76 exported by L<Dpkg::Version>: REL_EQ, REL_LT, REL_LE, REL_GT, REL_GT.
78 This functions returns 1 if the "p" dependency implies the "q"
79 dependency. It returns 0 if the "p" dependency implies that "q" is
80 not satisfied. It returns undef when there's no implication.
82 The $v_p and $v_q parameter should be L<Dpkg::Version> objects.
86 sub deps_eval_implication
{
87 my ($rel_p, $v_p, $rel_q, $v_q) = @_;
89 # If versions are not valid, we can't decide of any implication
90 return unless defined($v_p) and $v_p->is_valid();
91 return unless defined($v_q) and $v_q->is_valid();
93 # q wants an exact version, so p must provide that exact version. p
94 # disproves q if q's version is outside the range enforced by p.
95 if ($rel_q eq REL_EQ
) {
96 if ($rel_p eq REL_LT
) {
97 return ($v_p <= $v_q) ?
0 : undef;
98 } elsif ($rel_p eq REL_LE
) {
99 return ($v_p < $v_q) ?
0 : undef;
100 } elsif ($rel_p eq REL_GT
) {
101 return ($v_p >= $v_q) ?
0 : undef;
102 } elsif ($rel_p eq REL_GE
) {
103 return ($v_p > $v_q) ?
0 : undef;
104 } elsif ($rel_p eq REL_EQ
) {
105 return ($v_p == $v_q);
109 # A greater than clause may disprove a less than clause. An equal
110 # cause might as well. Otherwise, if
111 # p's clause is <<, <=, or =, the version must be <= q's to imply q.
112 if ($rel_q eq REL_LE
) {
113 if ($rel_p eq REL_GT
) {
114 return ($v_p >= $v_q) ?
0 : undef;
115 } elsif ($rel_p eq REL_GE
) {
116 return ($v_p > $v_q) ?
0 : undef;
117 } elsif ($rel_p eq REL_EQ
) {
118 return ($v_p <= $v_q) ?
1 : 0;
120 return ($v_p <= $v_q) ?
1 : undef;
124 # Similar, but << is stronger than <= so p's version must be << q's
125 # version if the p relation is <= or =.
126 if ($rel_q eq REL_LT
) {
127 if ($rel_p eq REL_GT
or $rel_p eq REL_GE
) {
128 return ($v_p >= $v_p) ?
0 : undef;
129 } elsif ($rel_p eq REL_LT
) {
130 return ($v_p <= $v_q) ?
1 : undef;
131 } elsif ($rel_p eq REL_EQ
) {
132 return ($v_p < $v_q) ?
1 : 0;
134 return ($v_p < $v_q) ?
1 : undef;
138 # Same logic as above, only inverted.
139 if ($rel_q eq REL_GE
) {
140 if ($rel_p eq REL_LT
) {
141 return ($v_p <= $v_q) ?
0 : undef;
142 } elsif ($rel_p eq REL_LE
) {
143 return ($v_p < $v_q) ?
0 : undef;
144 } elsif ($rel_p eq REL_EQ
) {
145 return ($v_p >= $v_q) ?
1 : 0;
147 return ($v_p >= $v_q) ?
1 : undef;
150 if ($rel_q eq REL_GT
) {
151 if ($rel_p eq REL_LT
or $rel_p eq REL_LE
) {
152 return ($v_p <= $v_q) ?
0 : undef;
153 } elsif ($rel_p eq REL_GT
) {
154 return ($v_p >= $v_q) ?
1 : undef;
155 } elsif ($rel_p eq REL_EQ
) {
156 return ($v_p > $v_q) ?
1 : 0;
158 return ($v_p > $v_q) ?
1 : undef;
165 =item $dep = deps_concat(@dep_list)
167 This function concatenates multiple dependency lines into a single line,
168 joining them with ", " if appropriate, and always returning a valid string.
175 return join ', ', grep { defined } @dep_list;
178 =item $dep = deps_parse($line, %options)
180 This function parses the dependency line and returns an object, either a
181 L<Dpkg::Deps::AND> or a L<Dpkg::Deps::Union>. Various options can alter the
182 behavior of that function.
186 =item use_arch (defaults to 1)
188 Take into account the architecture restriction part of the dependencies.
189 Set to 0 to completely ignore that information.
191 =item host_arch (defaults to the current architecture)
193 Define the host architecture. By default it uses
194 Dpkg::Arch::get_host_arch() to identify the proper architecture.
196 =item build_arch (defaults to the current architecture)
198 Define the build architecture. By default it uses
199 Dpkg::Arch::get_build_arch() to identify the proper architecture.
201 =item reduce_arch (defaults to 0)
203 If set to 1, ignore dependencies that do not concern the current host
204 architecture. This implicitly strips off the architecture restriction
205 list so that the resulting dependencies are directly applicable to the
206 current architecture.
208 =item use_profiles (defaults to 1)
210 Take into account the profile restriction part of the dependencies. Set
211 to 0 to completely ignore that information.
213 =item build_profiles (defaults to no profile)
215 Define the active build profiles. By default no profile is defined.
217 =item reduce_profiles (defaults to 0)
219 If set to 1, ignore dependencies that do not concern the current build
220 profile. This implicitly strips off the profile restriction formula so
221 that the resulting dependencies are directly applicable to the current
224 =item reduce_restrictions (defaults to 0)
226 If set to 1, ignore dependencies that do not concern the current set of
227 restrictions. This implicitly strips off any architecture restriction list
228 or restriction formula so that the resulting dependencies are directly
229 applicable to the current restriction.
230 This currently implies C<reduce_arch> and C<reduce_profiles>, and overrides
233 =item union (defaults to 0)
235 If set to 1, returns a L<Dpkg::Deps::Union> instead of a L<Dpkg::Deps::AND>.
236 Use this when parsing non-dependency fields like Conflicts.
238 =item virtual (defaults to 0)
240 If set to 1, allow only virtual package version relations, that is none,
242 This should be set whenever working with Provides fields.
244 =item build_dep (defaults to 0)
246 If set to 1, allow build-dep only arch qualifiers, that is ":native".
247 This should be set whenever working with build-deps.
249 =item tests_dep (defaults to 0)
251 If set to 1, allow tests-specific package names in dependencies, that is
252 "@" and "@builddeps@" (since dpkg 1.18.7). This should be set whenever
253 working with dependency fields from F<debian/tests/control>.
255 This option implicitly (and forcibly) enables C<build_dep> because test
256 dependencies are based on build dependencies (since dpkg 1.22.1).
263 my ($dep_line, %options) = @_;
265 # Validate arguments.
266 croak
"invalid host_arch $options{host_arch}"
267 if defined $options{host_arch
} and not defined debarch_to_debtuple
($options{host_arch
});
268 croak
"invalid build_arch $options{build_arch}"
269 if defined $options{build_arch
} and not defined debarch_to_debtuple
($options{build_arch
});
271 $options{use_arch
} //= 1;
272 $options{reduce_arch
} //= 0;
273 $options{use_profiles
} //= 1;
274 $options{reduce_profiles
} //= 0;
275 $options{reduce_restrictions
} //= 0;
276 $options{union
} //= 0;
277 $options{virtual
} //= 0;
278 $options{build_dep
} //= 0;
279 $options{tests_dep
} //= 0;
281 if ($options{reduce_restrictions
}) {
282 $options{reduce_arch
} = 1;
283 $options{reduce_profiles
} = 1;
285 if ($options{reduce_arch
}) {
286 $options{host_arch
} //= get_host_arch
();
287 $options{build_arch
} //= get_build_arch
();
289 if ($options{reduce_profiles
}) {
290 $options{build_profiles
} //= [ get_build_profiles
() ];
292 if ($options{tests_dep
}) {
293 $options{build_dep
} = 1;
296 # Options for Dpkg::Deps::Simple.
298 host_arch
=> $options{host_arch
},
299 build_arch
=> $options{build_arch
},
300 build_dep
=> $options{build_dep
},
301 tests_dep
=> $options{tests_dep
},
304 # Merge in a single-line
305 $dep_line =~ s/\s*[\r\n]\s*/ /g;
306 # Strip trailing/leading spaces
307 $dep_line =~ s/^\s+//;
308 $dep_line =~ s/\s+$//;
311 foreach my $dep_and (split(/\s*,\s*/m, $dep_line)) {
313 foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
314 my $dep_simple = Dpkg
::Deps
::Simple
->new($dep_or, %deps_options);
315 if (not defined $dep_simple->{package}) {
316 warning
(g_
("can't parse dependency %s"), $dep_or);
319 if ($options{virtual
} && defined $dep_simple->{relation
} &&
320 $dep_simple->{relation
} ne '=') {
321 warning
(g_
('virtual dependency contains invalid relation: %s'),
322 $dep_simple->output);
325 $dep_simple->{arches
} = undef if not $options{use_arch
};
326 if ($options{reduce_arch
}) {
327 $dep_simple->reduce_arch($options{host_arch
});
328 next if not $dep_simple->arch_is_concerned($options{host_arch
});
330 $dep_simple->{restrictions
} = undef if not $options{use_profiles
};
331 if ($options{reduce_profiles
}) {
332 $dep_simple->reduce_profiles($options{build_profiles
});
333 next if not $dep_simple->profile_is_concerned($options{build_profiles
});
335 push @or_list, $dep_simple;
337 next if not @or_list;
338 if (scalar @or_list == 1) {
339 push @dep_list, $or_list[0];
341 my $dep_or = Dpkg
::Deps
::OR
->new();
342 $dep_or->add($_) foreach (@or_list);
343 push @dep_list, $dep_or;
347 if ($options{union
}) {
348 $dep_and = Dpkg
::Deps
::Union
->new();
350 $dep_and = Dpkg
::Deps
::AND
->new();
352 foreach my $dep (@dep_list) {
353 if ($options{union
} and not $dep->isa('Dpkg::Deps::Simple')) {
354 warning
(g_
('an union dependency can only contain simple dependencies'));
362 =item $bool = deps_iterate($deps, $callback_func)
364 This function visits all elements of the dependency object, calling the
365 callback function for each element.
367 The callback function is expected to return true when everything is fine,
368 or false if something went wrong, in which case the iteration will stop.
370 Return the same value as the callback function.
375 my ($deps, $callback_func) = @_;
377 my $visitor_func = sub {
378 foreach my $dep (@_) {
379 return unless defined $dep;
381 if ($dep->isa('Dpkg::Deps::Simple')) {
382 return unless $callback_func->($dep);
384 return unless __SUB__
->($dep->get_deps());
390 return $visitor_func->($deps);
393 =item deps_compare($a, $b)
395 Implements a comparison operator between two dependency objects.
396 This function is mainly used to implement the sort() method.
402 my %relation_ordering = (
412 my ($aref, $bref) = @_;
415 deps_iterate
($aref, sub { push @as, @_ });
416 deps_iterate
($bref, sub { push @bs, @_ });
419 my ($a, $b) = (shift @as, shift @bs);
420 my $aundef = not defined $a or $a->is_empty();
421 my $bundef = not defined $b or $b->is_empty();
423 return 0 if $aundef and $bundef;
424 return -1 if $aundef;
427 my $ar = $a->{relation
} // 'undef';
428 my $br = $b->{relation
} // 'undef';
429 my $av = $a->{version
} // '';
430 my $bv = $b->{version
} // '';
432 my $res = (($a->{package} cmp $b->{package}) ||
433 ($relation_ordering{$ar} <=> $relation_ordering{$br}) ||
435 return $res if $res != 0;
439 =head1 CLASSES - Dpkg::Deps::*
441 There are several kind of dependencies. A L<Dpkg::Deps::Simple> dependency
442 represents a single dependency statement (it relates to one package only).
443 L<Dpkg::Deps::Multiple> dependencies are built on top of this class
444 and combine several dependencies in different manners. L<Dpkg::Deps::AND>
445 represents the logical "AND" between dependencies while L<Dpkg::Deps::OR>
446 represents the logical "OR". L<Dpkg::Deps::Multiple> objects can contain
447 L<Dpkg::Deps::Simple> object as well as other L<Dpkg::Deps::Multiple> objects.
449 In practice, the code is only meant to handle the realistic cases which,
450 given Debian's dependencies structure, imply those restrictions: AND can
451 contain Simple or OR objects, OR can only contain Simple objects.
453 L<Dpkg::Deps::KnownFacts> is a special class that is used while evaluating
454 dependencies and while trying to simplify them. It represents a set of
455 installed packages along with the virtual packages that they might
460 =head2 Version 1.07 (dpkg 1.20.0)
462 New option: Add virtual option to deps_parse().
464 =head2 Version 1.06 (dpkg 1.18.7; module version bumped on dpkg 1.18.24)
466 New option: Add tests_dep option to deps_parse().
468 =head2 Version 1.05 (dpkg 1.17.14)
470 New function: deps_iterate().
472 =head2 Version 1.04 (dpkg 1.17.10)
474 New options: Add use_profiles, build_profiles, reduce_profiles and
475 reduce_restrictions to deps_parse().
477 =head2 Version 1.03 (dpkg 1.17.0)
479 New option: Add build_arch option to deps_parse().
481 =head2 Version 1.02 (dpkg 1.17.0)
483 New function: deps_concat()
485 =head2 Version 1.01 (dpkg 1.16.1)
487 <Used to document changes to Dpkg::Deps::* modules before they were split.>
489 =head2 Version 1.00 (dpkg 1.15.6)
491 Mark the module as public.