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::Simple - represents a single dependency statement
30 This class represents a single dependency statement.
31 It has several interesting properties:
37 The package name (can be undef if the dependency has not been initialized
38 or if the simplification of the dependency lead to its removal).
42 The relational operator: "=", "<<", "<=", ">=" or ">>". It can be
43 undefined if the dependency had no version restriction. In that case the
44 following field is also undefined.
52 The list of architectures where this dependency is applicable. It is
53 undefined when there's no restriction, otherwise it is an
54 array ref. It can contain an exclusion list, in that case each
55 architecture is prefixed with an exclamation mark.
59 The arch qualifier of the dependency (can be undef if there is none).
60 In the dependency "python:any (>= 2.6)", the arch qualifier is "any".
64 The restrictions formula for this dependency. It is undefined when there
65 is no restriction formula. Otherwise it is an array ref.
71 package Dpkg
::Deps
::Simple
1.02;
78 use Dpkg
::Arch
qw(debarch_is_concerned debarch_list_parse);
79 use Dpkg
::BuildProfiles
qw(parse_build_profiles evaluate_restriction_formula);
81 use Dpkg
::ErrorHandling
;
84 use parent
qw(Dpkg::Interface::Storable);
90 =item $dep = Dpkg::Deps::Simple->new([$dep[, %opts]]);
100 Sets the host architecture.
104 Sets the build architecture.
108 Specifies whether the parser should consider it a build dependency.
113 Specifies whether the parser should consider it a tests dependency.
116 This option implicitly (and forcibly) enables C<build_dep> because test
117 dependencies are based on build dependencies (since dpkg 1.22.1).
124 my ($this, $arg, %opts) = @_;
125 my $class = ref($this) || $this;
130 $self->{host_arch
} = $opts{host_arch
};
131 $self->{build_arch
} = $opts{build_arch
};
132 $self->{build_dep
} = $opts{build_dep
} // 0;
133 $self->{tests_dep
} = $opts{tests_dep
} // 0;
134 if ($self->{tests_dep
}) {
135 $self->{build_dep
} = 1;
138 $self->parse_string($arg) if defined $arg;
144 Clears any dependency information stored in $dep so that $dep->is_empty()
152 $self->{package} = undef;
153 $self->{relation
} = undef;
154 $self->{version
} = undef;
155 $self->{arches
} = undef;
156 $self->{archqual
} = undef;
157 $self->{restrictions
} = undef;
160 =item $dep->parse_string($dep_string)
162 Parses the dependency string and modifies internal properties to match the
168 my ($self, $dep) = @_;
171 if ($self->{tests_dep
}) {
172 $pkgname_re = qr/[\@a-zA-Z0-9][\@a-zA-Z0-9+.-]*/;
174 $pkgname_re = qr/[a-zA-Z0-9][a-zA-Z0-9+.-]*/;
177 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
178 return if not $dep =~
179 m
{^\s
* # skip leading whitespace
180 ($pkgname_re) # package name
181 (?
: # start of optional part
182 : # colon for architecture
183 ([a
-zA
-Z0
-9][a
-zA
-Z0
-9-]*) # architecture name
184 )?
# end of optional part
185 (?
: # start of optional part
186 \s
* \
( # open parenthesis for version part
187 \s
* (<<|<=|=|>=|>>|[<>]) # relation part
188 \s
* ([^\
)\s
]+) # do not attempt to parse version
189 \s
* \
) # closing parenthesis
190 )?
# end of optional part
191 (?
: # start of optional architecture
192 \s
* \
[ # open bracket for architecture
193 \s
* ([^\
]]+) # don't parse architectures now
194 \s
* \
] # closing bracket
195 )?
# end of optional architecture
197 (?
: # start of optional restriction
198 \s
* < # open bracket for restriction
199 \s
* [^>]+ # do not parse restrictions now
200 \s
* > # closing bracket
202 )?
# end of optional restriction
203 \s
*$ # trailing spaces at end
206 return if $2 eq 'native' and not $self->{build_dep
};
207 $self->{archqual
} = $2;
209 $self->{package} = $1;
210 $self->{relation
} = version_normalize_relation
($3) if defined $3;
212 $self->{version
} = Dpkg
::Version
->new($4);
215 $self->{arches
} = [ debarch_list_parse
($5) ];
218 $self->{restrictions
} = [ parse_build_profiles
($6) ];
222 =item $dep->parse($fh, $desc)
224 Parse a dependency line from a filehandle.
229 my ($self, $fh, $desc) = @_;
233 return $self->parse_string($line);
236 =item $dep->load($filename)
238 Parse a dependency line from $filename.
240 =item $dep->output([$fh])
244 Returns a string representing the dependency. If $fh is set, it prints
245 the string to the filehandle.
250 my ($self, $fh) = @_;
252 my $res = $self->{package};
253 if (defined $self->{archqual
}) {
254 $res .= ':' . $self->{archqual
};
256 if (defined $self->{relation
}) {
257 $res .= ' (' . $self->{relation
} . ' ' . $self->{version
} . ')';
259 if (defined $self->{arches
}) {
260 $res .= ' [' . join(' ', @
{$self->{arches
}}) . ']';
262 if (defined $self->{restrictions
}) {
263 for my $restrlist (@
{$self->{restrictions
}}) {
264 $res .= ' <' . join(' ', @
{$restrlist}) . '>';
273 =item $dep->save($filename)
275 Save the dependency into the given $filename.
279 # _arch_is_superset(\@p, \@q)
281 # Returns true if the arch list @p is a superset of arch list @q.
282 # The arguments can also be undef in case there's no explicit architecture
284 sub _arch_is_superset
{
286 my $p_arch_neg = defined $p and $p->[0] =~ /^!/;
287 my $q_arch_neg = defined $q and $q->[0] =~ /^!/;
289 if (not defined $p) {
290 # If "p" has no arches, it is a superset of q and we should fall through
291 # to the version check.
293 } elsif (not defined $q) {
294 # If q has no arches, it is a superset of p and there are no useful
297 } elsif (not $p_arch_neg and not $q_arch_neg) {
298 # Both have arches. If neither are negated, we know nothing useful
299 # unless q is a subset of p.
301 my %p_arches = map { $_ => 1 } @
{$p};
303 for my $arch (@
{$q}) {
304 $subset = 0 unless $p_arches{$arch};
306 return 0 unless $subset;
307 } elsif ($p_arch_neg and $q_arch_neg) {
308 # If both are negated, we know nothing useful unless p is a subset of
309 # q (and therefore has fewer things excluded, and therefore is more
312 my %q_arches = map { $_ => 1 } @
{$q};
314 for my $arch (@
{$p}) {
315 $subset = 0 unless $q_arches{$arch};
317 return 0 unless $subset;
318 } elsif (not $p_arch_neg and $q_arch_neg) {
319 # If q is negated and p isn't, we'd need to know the full list of
320 # arches to know if there's any relationship, so bail.
322 } elsif ($p_arch_neg and not $q_arch_neg) {
323 # If p is negated and q isn't, q is a subset of p if none of the
324 # negated arches in p are present in q.
326 my %q_arches = map { $_ => 1 } @
{$q};
328 for my $arch (@
{$p}) {
329 $subset = 0 if $q_arches{substr($arch, 1)};
331 return 0 unless $subset;
336 # _arch_qualifier_implies($p, $q)
338 # Returns true if the arch qualifier $p and $q are compatible with the
339 # implication $p -> $q, false otherwise. $p/$q can be undef/"any"/"native"
340 # or an architecture string.
342 # Because we are handling dependencies in isolation, and the full context
343 # of the implications are only known when doing dependency resolution at
344 # run-time, we can only assert that they are implied if they are equal.
346 # For example dependencies with different arch-qualifiers cannot be simplified
347 # as these depend on the state of Multi-Arch field in the package depended on.
348 sub _arch_qualifier_implies
{
351 return $p eq $q if defined $p and defined $q;
352 return 1 if not defined $p and not defined $q;
356 # _restrictions_imply($p, $q)
358 # Returns true if the restrictions $p and $q are compatible with the
359 # implication $p -> $q, false otherwise.
360 # NOTE: We don't try to be very clever here, so we may conservatively
361 # return false when there is an implication.
362 sub _restrictions_imply
{
365 if (not defined $p) {
367 } elsif (not defined $q) {
370 # Check whether set difference is empty.
373 for my $restrlist (@
{$q}) {
374 my $reststr = join ' ', sort @
{$restrlist};
375 $restr{$reststr} = 1;
377 for my $restrlist (@
{$p}) {
378 my $reststr = join ' ', sort @
{$restrlist};
379 delete $restr{$reststr};
382 return keys %restr == 0;
386 =item $dep->implies($other_dep)
388 Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
389 NOT($other_dep). Returns undef when there is no implication. $dep and
390 $other_dep do not need to be of the same type.
397 if ($o->isa('Dpkg::Deps::Simple')) {
398 # An implication is only possible on the same package
399 return if $self->{package} ne $o->{package};
401 # Our architecture set must be a superset of the architectures for
402 # o, otherwise we can't conclude anything.
403 return unless _arch_is_superset
($self->{arches
}, $o->{arches
});
405 # The arch qualifier must not forbid an implication
406 return unless _arch_qualifier_implies
($self->{archqual
},
409 # Our restrictions must imply the restrictions for o
410 return unless _restrictions_imply
($self->{restrictions
},
413 # If o has no version clause, then our dependency is stronger
414 return 1 if not defined $o->{relation
};
415 # If o has a version clause, we must also have one, otherwise there
416 # can't be an implication
417 return if not defined $self->{relation
};
419 return Dpkg
::Deps
::deps_eval_implication
($self->{relation
},
420 $self->{version
}, $o->{relation
}, $o->{version
});
421 } elsif ($o->isa('Dpkg::Deps::AND')) {
422 # TRUE: Need to imply all individual elements
423 # FALSE: Need to NOT imply at least one individual element
425 foreach my $dep ($o->get_deps()) {
426 my $implication = $self->implies($dep);
427 unless (defined $implication and $implication == 1) {
429 last if defined $res;
433 } elsif ($o->isa('Dpkg::Deps::OR')) {
434 # TRUE: Need to imply at least one individual element
435 # FALSE: Need to not apply all individual elements
438 foreach my $dep ($o->get_deps()) {
439 my $implication = $self->implies($dep);
440 if (defined $implication) {
441 if (not defined $res) {
443 } elsif ($implication) {
448 last if defined $res and $res == 1;
453 croak
'Dpkg::Deps::Simple cannot evaluate implication with a ' .
458 =item $dep->get_deps()
460 Returns a list of sub-dependencies, which for this object it means it
473 This method is a no-op for this object.
481 =item $dep->arch_is_concerned($arch)
483 Returns true if the dependency applies to the indicated architecture.
487 sub arch_is_concerned
{
488 my ($self, $host_arch) = @_;
490 return 0 if not defined $self->{package}; # Empty dep
491 return 1 if not defined $self->{arches
}; # Dep without arch spec
493 return debarch_is_concerned
($host_arch, @
{$self->{arches
}});
496 =item $dep->reduce_arch($arch)
498 Simplifies the dependency to contain only information relevant to the given
499 architecture. This object can be left empty after this operation. This trims
500 off the architecture restriction list of these objects.
505 my ($self, $host_arch) = @_;
507 if (not $self->arch_is_concerned($host_arch)) {
510 $self->{arches
} = undef;
514 =item $dep->has_arch_restriction()
516 Returns the package name if the dependency applies only to a subset of
521 sub has_arch_restriction
{
524 if (defined $self->{arches
}) {
525 return $self->{package};
531 =item $dep->profile_is_concerned()
533 Returns true if the dependency applies to the indicated profile.
537 sub profile_is_concerned
{
538 my ($self, $build_profiles) = @_;
540 return 0 if not defined $self->{package}; # Empty dep
541 return 1 if not defined $self->{restrictions
}; # Dep without restrictions
542 return evaluate_restriction_formula
($self->{restrictions
}, $build_profiles);
545 =item $dep->reduce_profiles()
547 Simplifies the dependency to contain only information relevant to the given
548 profile. This object can be left empty after this operation. This trims off
549 the profile restriction list of this object.
553 sub reduce_profiles
{
554 my ($self, $build_profiles) = @_;
556 if (not $self->profile_is_concerned($build_profiles)) {
559 $self->{restrictions
} = undef;
563 =item $dep->get_evaluation($facts)
565 Evaluates the dependency given a list of installed packages and a list of
566 virtual packages provided. These lists are part of the
567 L<Dpkg::Deps::KnownFacts> object given as parameters.
569 Returns 1 when it's true, 0 when it's false, undef when some information
570 is lacking to conclude.
575 my ($self, $facts) = @_;
577 return if not defined $self->{package};
578 return $facts->evaluate_simple_dep($self);
581 =item $dep->simplify_deps($facts, @assumed_deps)
583 Simplifies the dependency as much as possible given the list of facts (see
584 class L<Dpkg::Deps::KnownFacts>) and a list of other dependencies that are
590 my ($self, $facts) = @_;
592 my $eval = $self->get_evaluation($facts);
593 $self->reset() if defined $eval and $eval == 1;
596 =item $dep->is_empty()
598 Returns true if the dependency is empty and doesn't contain any useful
599 information. This is true when the object has not yet been initialized.
606 return not defined $self->{package};
609 =item $dep->merge_union($other_dep)
611 Returns true if $dep could be modified to represent the union of both
612 dependencies. Otherwise returns false.
619 return 0 if not $o->isa('Dpkg::Deps::Simple');
620 return 0 if $self->is_empty() or $o->is_empty();
621 return 0 if $self->{package} ne $o->{package};
622 return 0 if defined $self->{arches
} or defined $o->{arches
};
624 if (not defined $o->{relation
} and defined $self->{relation
}) {
625 # Union is the non-versioned dependency
626 $self->{relation
} = undef;
627 $self->{version
} = undef;
631 my $implication = $self->implies($o);
632 my $rev_implication = $o->implies($self);
633 if (defined $implication) {
635 $self->{relation
} = $o->{relation
};
636 $self->{version
} = $o->{version
};
642 if (defined $rev_implication) {
643 if ($rev_implication) {
657 =head2 Version 1.02 (dpkg 1.17.10)
659 New methods: Add $dep->profile_is_concerned() and $dep->reduce_profiles().
661 =head2 Version 1.01 (dpkg 1.16.1)
663 New method: Add $dep->reset().
665 New property: recognizes the arch qualifier "any" and stores it in the
666 "archqual" property when present.
668 =head2 Version 1.00 (dpkg 1.15.6)
670 Mark the module as public.