Deps.pm 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. # Copyright © 2007-2009 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2008-2009,2012-2014 Guillem Jover <guillem@debian.org>
  3. #
  4. # This program is free software; you may redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. #########################################################################
  17. # Several parts are inspired by lib/Dep.pm from lintian (same license)
  18. #
  19. # Copyright © 1998 Richard Braakman
  20. # Portions Copyright © 1999 Darren Benham
  21. # Portions Copyright © 2000 Sean 'Shaleh' Perry
  22. # Portions Copyright © 2004 Frank Lichtenheld
  23. # Portions Copyright © 2006 Russ Allbery
  24. package Dpkg::Deps;
  25. =encoding utf8
  26. =head1 NAME
  27. Dpkg::Deps - parse and manipulate dependencies of Debian packages
  28. =head1 DESCRIPTION
  29. The Dpkg::Deps module provides objects implementing various types of
  30. dependencies.
  31. The most important function is deps_parse(), it turns a dependency line in
  32. a set of Dpkg::Deps::{Simple,AND,OR,Union} objects depending on the case.
  33. =head1 FUNCTIONS
  34. All the deps_* functions are exported by default.
  35. =over 4
  36. =cut
  37. use strict;
  38. use warnings;
  39. our $VERSION = '1.04';
  40. use Dpkg::Version;
  41. use Dpkg::Arch qw(get_host_arch get_build_arch);
  42. use Dpkg::BuildProfiles qw(get_build_profiles);
  43. use Dpkg::ErrorHandling;
  44. use Dpkg::Gettext;
  45. use Exporter qw(import);
  46. our @EXPORT = qw(deps_concat deps_parse deps_eval_implication deps_compare);
  47. =item deps_eval_implication($rel_p, $v_p, $rel_q, $v_q)
  48. ($rel_p, $v_p) and ($rel_q, $v_q) express two dependencies as (relation,
  49. version). The relation variable can have the following values that are
  50. exported by Dpkg::Version: REL_EQ, REL_LT, REL_LE, REL_GT, REL_GT.
  51. This functions returns 1 if the "p" dependency implies the "q"
  52. dependency. It returns 0 if the "p" dependency implies that "q" is
  53. not satisfied. It returns undef when there's no implication.
  54. The $v_p and $v_q parameter should be Dpkg::Version objects.
  55. =cut
  56. sub deps_eval_implication {
  57. my ($rel_p, $v_p, $rel_q, $v_q) = @_;
  58. # If versions are not valid, we can't decide of any implication
  59. return unless defined($v_p) and $v_p->is_valid();
  60. return unless defined($v_q) and $v_q->is_valid();
  61. # q wants an exact version, so p must provide that exact version. p
  62. # disproves q if q's version is outside the range enforced by p.
  63. if ($rel_q eq REL_EQ) {
  64. if ($rel_p eq REL_LT) {
  65. return ($v_p <= $v_q) ? 0 : undef;
  66. } elsif ($rel_p eq REL_LE) {
  67. return ($v_p < $v_q) ? 0 : undef;
  68. } elsif ($rel_p eq REL_GT) {
  69. return ($v_p >= $v_q) ? 0 : undef;
  70. } elsif ($rel_p eq REL_GE) {
  71. return ($v_p > $v_q) ? 0 : undef;
  72. } elsif ($rel_p eq REL_EQ) {
  73. return ($v_p == $v_q);
  74. }
  75. }
  76. # A greater than clause may disprove a less than clause. An equal
  77. # cause might as well. Otherwise, if
  78. # p's clause is <<, <=, or =, the version must be <= q's to imply q.
  79. if ($rel_q eq REL_LE) {
  80. if ($rel_p eq REL_GT) {
  81. return ($v_p >= $v_q) ? 0 : undef;
  82. } elsif ($rel_p eq REL_GE) {
  83. return ($v_p > $v_q) ? 0 : undef;
  84. } elsif ($rel_p eq REL_EQ) {
  85. return ($v_p <= $v_q) ? 1 : 0;
  86. } else { # <<, <=
  87. return ($v_p <= $v_q) ? 1 : undef;
  88. }
  89. }
  90. # Similar, but << is stronger than <= so p's version must be << q's
  91. # version if the p relation is <= or =.
  92. if ($rel_q eq REL_LT) {
  93. if ($rel_p eq REL_GT or $rel_p eq REL_GE) {
  94. return ($v_p >= $v_p) ? 0 : undef;
  95. } elsif ($rel_p eq REL_LT) {
  96. return ($v_p <= $v_q) ? 1 : undef;
  97. } elsif ($rel_p eq REL_EQ) {
  98. return ($v_p < $v_q) ? 1 : 0;
  99. } else { # <<, <=
  100. return ($v_p < $v_q) ? 1 : undef;
  101. }
  102. }
  103. # Same logic as above, only inverted.
  104. if ($rel_q eq REL_GE) {
  105. if ($rel_p eq REL_LT) {
  106. return ($v_p <= $v_q) ? 0 : undef;
  107. } elsif ($rel_p eq REL_LE) {
  108. return ($v_p < $v_q) ? 0 : undef;
  109. } elsif ($rel_p eq REL_EQ) {
  110. return ($v_p >= $v_q) ? 1 : 0;
  111. } else { # >>, >=
  112. return ($v_p >= $v_q) ? 1 : undef;
  113. }
  114. }
  115. if ($rel_q eq REL_GT) {
  116. if ($rel_p eq REL_LT or $rel_p eq REL_LE) {
  117. return ($v_p <= $v_q) ? 0 : undef;
  118. } elsif ($rel_p eq REL_GT) {
  119. return ($v_p >= $v_q) ? 1 : undef;
  120. } elsif ($rel_p eq REL_EQ) {
  121. return ($v_p > $v_q) ? 1 : 0;
  122. } else {
  123. return ($v_p > $v_q) ? 1 : undef;
  124. }
  125. }
  126. return;
  127. }
  128. =item my $dep = deps_concat(@dep_list)
  129. This function concatenates multiple dependency lines into a single line,
  130. joining them with ", " if appropriate, and always returning a valid string.
  131. =cut
  132. sub deps_concat {
  133. my (@dep_list) = @_;
  134. return join(', ', grep { defined $_ } @dep_list);
  135. }
  136. =item my $dep = deps_parse($line, %options)
  137. This function parses the dependency line and returns an object, either a
  138. Dpkg::Deps::AND or a Dpkg::Deps::Union. Various options can alter the
  139. behaviour of that function.
  140. =over 4
  141. =item use_arch (defaults to 1)
  142. Take into account the architecture restriction part of the dependencies.
  143. Set to 0 to completely ignore that information.
  144. =item host_arch (defaults to the current architecture)
  145. Define the host architecture. By default it uses
  146. Dpkg::Arch::get_host_arch() to identify the proper architecture.
  147. =item build_arch (defaults to the current architecture)
  148. Define the build architecture. By default it uses
  149. Dpkg::Arch::get_build_arch() to identify the proper architecture.
  150. =item reduce_arch (defaults to 0)
  151. If set to 1, ignore dependencies that do not concern the current host
  152. architecture. This implicitely strips off the architecture restriction
  153. list so that the resulting dependencies are directly applicable to the
  154. current architecture.
  155. =item use_profiles (defaults to 1)
  156. Take into account the profile restriction part of the dependencies. Set
  157. to 0 to completely ignore that information.
  158. =item build_profiles (defaults to no profile)
  159. Define the active build profiles. By default no profile is defined.
  160. =item reduce_profiles (defaults to 0)
  161. If set to 1, ignore dependencies that do not concern the current build
  162. profile. This implicitly strips off the profile restriction list so
  163. that the resulting dependencies are directly applicable to the current
  164. profiles.
  165. =item reduce_restrictions (defaults to 0)
  166. If set to 1, ignore dependencies that do not concern the current set of
  167. restrictions. This implicitly strips off any restriction list so that the
  168. resulting dependencies are directly applicable to the current restriction.
  169. This currently implies C<reduce_arch> and C<reduce_profiles>, and overrides
  170. them if set.
  171. =item union (defaults to 0)
  172. If set to 1, returns a Dpkg::Deps::Union instead of a Dpkg::Deps::AND. Use
  173. this when parsing non-dependency fields like Conflicts.
  174. =item build_dep (defaults to 0)
  175. If set to 1, allow build-dep only arch qualifiers, that is “:native”.
  176. This should be set whenever working with build-deps.
  177. =back
  178. =cut
  179. sub deps_parse {
  180. my $dep_line = shift;
  181. my %options = (@_);
  182. $options{use_arch} = 1 if not exists $options{use_arch};
  183. $options{reduce_arch} = 0 if not exists $options{reduce_arch};
  184. $options{host_arch} = get_host_arch() if not exists $options{host_arch};
  185. $options{build_arch} = get_build_arch() if not exists $options{build_arch};
  186. $options{use_profiles} = 1 if not exists $options{use_profiles};
  187. $options{reduce_profiles} = 0 if not exists $options{reduce_profiles};
  188. $options{build_profiles} = [ get_build_profiles() ]
  189. if not exists $options{build_profiles};
  190. $options{reduce_restrictions} = 0 if not exists $options{reduce_restrictions};
  191. $options{union} = 0 if not exists $options{union};
  192. $options{build_dep} = 0 if not exists $options{build_dep};
  193. if ($options{reduce_restrictions}) {
  194. $options{reduce_arch} = 1;
  195. $options{reduce_profiles} = 1;
  196. }
  197. # Strip trailing/leading spaces
  198. $dep_line =~ s/^\s+//;
  199. $dep_line =~ s/\s+$//;
  200. my @dep_list;
  201. foreach my $dep_and (split(/\s*,\s*/m, $dep_line)) {
  202. my @or_list = ();
  203. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  204. my $dep_simple = Dpkg::Deps::Simple->new($dep_or, host_arch =>
  205. $options{host_arch},
  206. build_arch =>
  207. $options{build_arch},
  208. build_dep =>
  209. $options{build_dep});
  210. if (not defined $dep_simple->{package}) {
  211. warning(_g("can't parse dependency %s"), $dep_or);
  212. return;
  213. }
  214. $dep_simple->{arches} = undef if not $options{use_arch};
  215. if ($options{reduce_arch}) {
  216. $dep_simple->reduce_arch($options{host_arch});
  217. next if not $dep_simple->arch_is_concerned($options{host_arch});
  218. }
  219. $dep_simple->{restrictions} = undef if not $options{use_profiles};
  220. if ($options{reduce_profiles}) {
  221. $dep_simple->reduce_profiles($options{build_profiles});
  222. next if not $dep_simple->profile_is_concerned($options{build_profiles});
  223. }
  224. push @or_list, $dep_simple;
  225. }
  226. next if not @or_list;
  227. if (scalar @or_list == 1) {
  228. push @dep_list, $or_list[0];
  229. } else {
  230. my $dep_or = Dpkg::Deps::OR->new();
  231. $dep_or->add($_) foreach (@or_list);
  232. push @dep_list, $dep_or;
  233. }
  234. }
  235. my $dep_and;
  236. if ($options{union}) {
  237. $dep_and = Dpkg::Deps::Union->new();
  238. } else {
  239. $dep_and = Dpkg::Deps::AND->new();
  240. }
  241. foreach my $dep (@dep_list) {
  242. if ($options{union} and not $dep->isa('Dpkg::Deps::Simple')) {
  243. warning(_g('an union dependency can only contain simple dependencies'));
  244. return;
  245. }
  246. $dep_and->add($dep);
  247. }
  248. return $dep_and;
  249. }
  250. =item deps_compare($a, $b)
  251. Implements a comparison operator between two dependency objects.
  252. This function is mainly used to implement the sort() method.
  253. =back
  254. =cut
  255. my %relation_ordering = (
  256. undef => 0,
  257. REL_GE() => 1,
  258. REL_GT() => 2,
  259. REL_EQ() => 3,
  260. REL_LT() => 4,
  261. REL_LE() => 5,
  262. );
  263. sub deps_compare {
  264. my ($a, $b) = @_;
  265. return -1 if $a->is_empty();
  266. return 1 if $b->is_empty();
  267. while ($a->isa('Dpkg::Deps::Multiple')) {
  268. return -1 if $a->is_empty();
  269. my @deps = $a->get_deps();
  270. $a = $deps[0];
  271. }
  272. while ($b->isa('Dpkg::Deps::Multiple')) {
  273. return 1 if $b->is_empty();
  274. my @deps = $b->get_deps();
  275. $b = $deps[0];
  276. }
  277. my $ar = $a->{relation} // 'undef';
  278. my $br = $b->{relation} // 'undef';
  279. my $av = $a->{version} // '';
  280. my $bv = $a->{version} // '';
  281. return (($a->{package} cmp $b->{package}) ||
  282. ($relation_ordering{$ar} <=> $relation_ordering{$br}) ||
  283. ($av cmp $bv));
  284. }
  285. package Dpkg::Deps::Simple;
  286. =head1 OBJECTS - Dpkg::Deps::*
  287. There are several kind of dependencies. A Dpkg::Deps::Simple dependency
  288. represents a single dependency statement (it relates to one package only).
  289. Dpkg::Deps::Multiple dependencies are built on top of this object
  290. and combine several dependencies in a different manners. Dpkg::Deps::AND
  291. represents the logical "AND" between dependencies while Dpkg::Deps::OR
  292. represents the logical "OR". Dpkg::Deps::Multiple objects can contain
  293. Dpkg::Deps::Simple object as well as other Dpkg::Deps::Multiple objects.
  294. In practice, the code is only meant to handle the realistic cases which,
  295. given Debian's dependencies structure, imply those restrictions: AND can
  296. contain Simple or OR objects, OR can only contain Simple objects.
  297. Dpkg::Deps::KnownFacts is a special object that is used while evaluating
  298. dependencies and while trying to simplify them. It represents a set of
  299. installed packages along with the virtual packages that they might
  300. provide.
  301. =head2 COMMON FUNCTIONS
  302. =over 4
  303. =item $dep->is_empty()
  304. Returns true if the dependency is empty and doesn't contain any useful
  305. information. This is true when a Dpkg::Deps::Simple object has not yet
  306. been initialized or when a (descendant of) Dpkg::Deps::Multiple contains
  307. an empty list of dependencies.
  308. =item $dep->get_deps()
  309. Returns a list of sub-dependencies. For Dpkg::Deps::Simple it returns
  310. itself.
  311. =item $dep->output([$fh])
  312. =item "$dep"
  313. Returns a string representing the dependency. If $fh is set, it prints
  314. the string to the filehandle.
  315. =item $dep->implies($other_dep)
  316. Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
  317. NOT($other_dep). Returns undef when there's no implication. $dep and
  318. $other_dep do not need to be of the same type.
  319. =item $dep->sort()
  320. Sorts alphabetically the internal list of dependencies. It's a no-op for
  321. Dpkg::Deps::Simple objects.
  322. =item $dep->arch_is_concerned($arch)
  323. Returns true if the dependency applies to the indicated architecture. For
  324. multiple dependencies, it returns true if at least one of the
  325. sub-dependencies apply to this architecture.
  326. =item $dep->reduce_arch($arch)
  327. Simplifies the dependency to contain only information relevant to the given
  328. architecture. A Dpkg::Deps::Simple object can be left empty after this
  329. operation. For Dpkg::Deps::Multiple objects, the non-relevant
  330. sub-dependencies are simply removed.
  331. This trims off the architecture restriction list of Dpkg::Deps::Simple
  332. objects.
  333. =item $dep->get_evaluation($facts)
  334. Evaluates the dependency given a list of installed packages and a list of
  335. virtual packages provided. Those lists are part of the
  336. Dpkg::Deps::KnownFacts object given as parameters.
  337. Returns 1 when it's true, 0 when it's false, undef when some information
  338. is lacking to conclude.
  339. =item $dep->simplify_deps($facts, @assumed_deps)
  340. Simplifies the dependency as much as possible given the list of facts (see
  341. object Dpkg::Deps::KnownFacts) and a list of other dependencies that are
  342. known to be true.
  343. =item $dep->has_arch_restriction()
  344. For a simple dependency, returns the package name if the dependency
  345. applies only to a subset of architectures. For multiple dependencies, it
  346. returns the list of package names that have such a restriction.
  347. =item $dep->reset()
  348. Clears any dependency information stored in $dep so that $dep->is_empty()
  349. returns true.
  350. =back
  351. =head2 Dpkg::Deps::Simple
  352. Such an object has four interesting properties:
  353. =over 4
  354. =item package
  355. The package name (can be undef if the dependency has not been initialized
  356. or if the simplification of the dependency lead to its removal).
  357. =item relation
  358. The relational operator: "=", "<<", "<=", ">=" or ">>". It can be
  359. undefined if the dependency had no version restriction. In that case the
  360. following field is also undefined.
  361. =item version
  362. The version.
  363. =item arches
  364. The list of architectures where this dependency is applicable. It's
  365. undefined when there's no restriction, otherwise it's an
  366. array ref. It can contain an exclusion list, in that case each
  367. architecture is prefixed with an exclamation mark.
  368. =item archqual
  369. The arch qualifier of the dependency (can be undef if there's none).
  370. In the dependency "python:any (>= 2.6)", the arch qualifier is "any".
  371. =back
  372. =head3 METHODS
  373. =over 4
  374. =item $simple_dep->parse_string('dpkg-dev (>= 1.14.8) [!hurd-i386]')
  375. Parses the dependency and modifies internal properties to match the parsed
  376. dependency.
  377. =item $simple_dep->merge_union($other_dep)
  378. Returns true if $simple_dep could be modified to represent the union of
  379. both dependencies. Otherwise returns false.
  380. =back
  381. =cut
  382. use strict;
  383. use warnings;
  384. use Carp;
  385. use Dpkg::Arch qw(debarch_is);
  386. use Dpkg::Version;
  387. use Dpkg::ErrorHandling;
  388. use Dpkg::Gettext;
  389. use Dpkg::Util qw(:list);
  390. use parent qw(Dpkg::Interface::Storable);
  391. sub new {
  392. my ($this, $arg, %opts) = @_;
  393. my $class = ref($this) || $this;
  394. my $self = {};
  395. bless $self, $class;
  396. $self->reset();
  397. $self->{host_arch} = $opts{host_arch} || Dpkg::Arch::get_host_arch();
  398. $self->{build_arch} = $opts{build_arch} || Dpkg::Arch::get_build_arch();
  399. $self->{build_dep} = $opts{build_dep} || 0;
  400. $self->parse_string($arg) if defined($arg);
  401. return $self;
  402. }
  403. sub reset {
  404. my ($self) = @_;
  405. $self->{package} = undef;
  406. $self->{relation} = undef;
  407. $self->{version} = undef;
  408. $self->{arches} = undef;
  409. $self->{archqual} = undef;
  410. $self->{restrictions} = undef;
  411. }
  412. sub parse {
  413. my ($self, $fh, $desc) = @_;
  414. my $line = <$fh>;
  415. chomp($line);
  416. return $self->parse_string($line);
  417. }
  418. sub parse_string {
  419. my ($self, $dep) = @_;
  420. return if not $dep =~
  421. m{^\s* # skip leading whitespace
  422. ([a-zA-Z0-9][a-zA-Z0-9+.-]*) # package name
  423. (?: # start of optional part
  424. : # colon for architecture
  425. ([a-zA-Z0-9][a-zA-Z0-9-]*) # architecture name
  426. )? # end of optional part
  427. (?: # start of optional part
  428. \s* \( # open parenthesis for version part
  429. \s* (<<|<=|=|>=|>>|[<>]) # relation part
  430. \s* (.*?) # do not attempt to parse version
  431. \s* \) # closing parenthesis
  432. )? # end of optional part
  433. (?: # start of optional architecture
  434. \s* \[ # open bracket for architecture
  435. \s* (.*?) # don't parse architectures now
  436. \s* \] # closing bracket
  437. )? # end of optional architecture
  438. (?: # start of optional restriction
  439. \s* < # open bracket for restriction
  440. \s* (.*?) # don't parse restrictions now
  441. \s* > # closing bracket
  442. )? # end of optional restriction
  443. \s*$ # trailing spaces at end
  444. }x;
  445. if (defined($2)) {
  446. return if $2 eq 'native' and not $self->{build_dep};
  447. $self->{archqual} = $2;
  448. }
  449. $self->{package} = $1;
  450. $self->{relation} = version_normalize_relation($3) if defined($3);
  451. if (defined($4)) {
  452. $self->{version} = Dpkg::Version->new($4);
  453. }
  454. if (defined($5)) {
  455. $self->{arches} = [ split(/\s+/, $5) ];
  456. }
  457. if (defined($6)) {
  458. $self->{restrictions} = [ map { lc } split /\s+/, $6 ];
  459. }
  460. }
  461. sub output {
  462. my ($self, $fh) = @_;
  463. my $res = $self->{package};
  464. if (defined($self->{archqual})) {
  465. $res .= ':' . $self->{archqual};
  466. }
  467. if (defined($self->{relation})) {
  468. $res .= ' (' . $self->{relation} . ' ' . $self->{version} . ')';
  469. }
  470. if (defined($self->{arches})) {
  471. $res .= ' [' . join(' ', @{$self->{arches}}) . ']';
  472. }
  473. if (defined($self->{restrictions})) {
  474. $res .= ' <' . join(' ', @{$self->{restrictions}}) . '>';
  475. }
  476. if (defined($fh)) {
  477. print { $fh } $res;
  478. }
  479. return $res;
  480. }
  481. # _arch_is_superset(\@p, \@q)
  482. #
  483. # Returns true if the arch list @p is a superset of arch list @q.
  484. # The arguments can also be undef in case there's no explicit architecture
  485. # restriction.
  486. sub _arch_is_superset {
  487. my ($p, $q) = @_;
  488. my $p_arch_neg = defined($p) && $p->[0] =~ /^!/;
  489. my $q_arch_neg = defined($q) && $q->[0] =~ /^!/;
  490. # If "p" has no arches, it is a superset of q and we should fall through
  491. # to the version check.
  492. if (not defined $p) {
  493. return 1;
  494. }
  495. # If q has no arches, it is a superset of p and there are no useful
  496. # implications.
  497. elsif (not defined $q) {
  498. return 0;
  499. }
  500. # Both have arches. If neither are negated, we know nothing useful
  501. # unless q is a subset of p.
  502. elsif (not $p_arch_neg and not $q_arch_neg) {
  503. my %p_arches = map { $_ => 1 } @{$p};
  504. my $subset = 1;
  505. for my $arch (@{$q}) {
  506. $subset = 0 unless $p_arches{$arch};
  507. }
  508. return 0 unless $subset;
  509. }
  510. # If both are negated, we know nothing useful unless p is a subset of
  511. # q (and therefore has fewer things excluded, and therefore is more
  512. # general).
  513. elsif ($p_arch_neg and $q_arch_neg) {
  514. my %q_arches = map { $_ => 1 } @{$q};
  515. my $subset = 1;
  516. for my $arch (@{$p}) {
  517. $subset = 0 unless $q_arches{$arch};
  518. }
  519. return 0 unless $subset;
  520. }
  521. # If q is negated and p isn't, we'd need to know the full list of
  522. # arches to know if there's any relationship, so bail.
  523. elsif (not $p_arch_neg and $q_arch_neg) {
  524. return 0;
  525. }
  526. # If p is negated and q isn't, q is a subset of p if none of the
  527. # negated arches in p are present in q.
  528. elsif ($p_arch_neg and not $q_arch_neg) {
  529. my %q_arches = map { $_ => 1 } @{$q};
  530. my $subset = 1;
  531. for my $arch (@{$p}) {
  532. $subset = 0 if $q_arches{substr($arch, 1)};
  533. }
  534. return 0 unless $subset;
  535. }
  536. return 1;
  537. }
  538. # _arch_qualifier_allows_implication($p, $q)
  539. #
  540. # Returns true if the arch qualifier $p and $q are compatible with the
  541. # implication $p -> $q, false otherwise. $p/$q can be
  542. # undef/"any"/"native" or an architecture string.
  543. sub _arch_qualifier_allows_implication {
  544. my ($p, $q) = @_;
  545. if (defined $p and $p eq 'any') {
  546. return 1 if defined $q and $q eq 'any';
  547. return 0;
  548. } elsif (defined $p and $p eq 'native') {
  549. return 1 if defined $q and ($q eq 'any' or $q eq 'native');
  550. return 0;
  551. } elsif (defined $p) {
  552. return 1 if defined $q and ($p eq $q or $q eq 'any');
  553. return 0;
  554. } else {
  555. return 0 if defined $q and $q ne 'any' and $q ne 'native';
  556. return 1;
  557. }
  558. }
  559. # Returns true if the dependency in parameter can deduced from the current
  560. # dependency. Returns false if it can be negated. Returns undef if nothing
  561. # can be concluded.
  562. sub implies {
  563. my ($self, $o) = @_;
  564. if ($o->isa('Dpkg::Deps::Simple')) {
  565. # An implication is only possible on the same package
  566. return if $self->{package} ne $o->{package};
  567. # Our architecture set must be a superset of the architectures for
  568. # o, otherwise we can't conclude anything.
  569. return unless _arch_is_superset($self->{arches}, $o->{arches});
  570. # The arch qualifier must not forbid an implication
  571. return unless _arch_qualifier_allows_implication($self->{archqual},
  572. $o->{archqual});
  573. # If o has no version clause, then our dependency is stronger
  574. return 1 if not defined $o->{relation};
  575. # If o has a version clause, we must also have one, otherwise there
  576. # can't be an implication
  577. return if not defined $self->{relation};
  578. return Dpkg::Deps::deps_eval_implication($self->{relation},
  579. $self->{version}, $o->{relation}, $o->{version});
  580. } elsif ($o->isa('Dpkg::Deps::AND')) {
  581. # TRUE: Need to imply all individual elements
  582. # FALSE: Need to NOT imply at least one individual element
  583. my $res = 1;
  584. foreach my $dep ($o->get_deps()) {
  585. my $implication = $self->implies($dep);
  586. unless (defined($implication) && $implication == 1) {
  587. $res = $implication;
  588. last if defined $res;
  589. }
  590. }
  591. return $res;
  592. } elsif ($o->isa('Dpkg::Deps::OR')) {
  593. # TRUE: Need to imply at least one individual element
  594. # FALSE: Need to not apply all individual elements
  595. # UNDEF: The rest
  596. my $res = undef;
  597. foreach my $dep ($o->get_deps()) {
  598. my $implication = $self->implies($dep);
  599. if (defined($implication)) {
  600. if (not defined $res) {
  601. $res = $implication;
  602. } else {
  603. if ($implication) {
  604. $res = 1;
  605. } else {
  606. $res = 0;
  607. }
  608. }
  609. last if defined($res) && $res == 1;
  610. }
  611. }
  612. return $res;
  613. } else {
  614. croak 'Dpkg::Deps::Simple cannot evaluate implication with a ' .
  615. ref($o);
  616. }
  617. }
  618. sub get_deps {
  619. my $self = shift;
  620. return $self;
  621. }
  622. sub sort {
  623. # Nothing to sort
  624. }
  625. sub arch_is_concerned {
  626. my ($self, $host_arch) = @_;
  627. return 0 if not defined $self->{package}; # Empty dep
  628. return 1 if not defined $self->{arches}; # Dep without arch spec
  629. my $seen_arch = 0;
  630. foreach my $arch (@{$self->{arches}}) {
  631. $arch=lc($arch);
  632. if ($arch =~ /^!/) {
  633. my $not_arch = $arch;
  634. $not_arch =~ s/^!//;
  635. if (debarch_is($host_arch, $not_arch)) {
  636. $seen_arch = 0;
  637. last;
  638. } else {
  639. # !arch includes by default all other arches
  640. # unless they also appear in a !otherarch
  641. $seen_arch = 1;
  642. }
  643. } elsif (debarch_is($host_arch, $arch)) {
  644. $seen_arch = 1;
  645. last;
  646. }
  647. }
  648. return $seen_arch;
  649. }
  650. sub reduce_arch {
  651. my ($self, $host_arch) = @_;
  652. if (not $self->arch_is_concerned($host_arch)) {
  653. $self->reset();
  654. } else {
  655. $self->{arches} = undef;
  656. }
  657. }
  658. sub has_arch_restriction {
  659. my ($self) = @_;
  660. if (defined $self->{arches}) {
  661. return $self->{package};
  662. } else {
  663. return ();
  664. }
  665. }
  666. sub profile_is_concerned {
  667. my ($self, $build_profiles) = @_;
  668. return 0 if not defined $self->{package}; # Empty dep
  669. return 1 if not defined $self->{restrictions}; # Dep without restrictions
  670. my $seen_profile = 0;
  671. foreach my $restriction (@{$self->{restrictions}}) {
  672. # Determine if this restriction is negated, and within the "profile"
  673. # namespace, otherwise it does not concern this check.
  674. next if $restriction !~ m/^(!)?profile\.(.*)/;
  675. my $negated = defined $1 && $1 eq '!';
  676. my $profile = $2;
  677. # Determine if the restriction matches any of the specified profiles.
  678. my $found = any { $_ eq $profile } @{$build_profiles};
  679. if ($negated) {
  680. if ($found) {
  681. $seen_profile = 0;
  682. last;
  683. } else {
  684. # "!profile.this" includes by default all other profiles
  685. # unless they also appear in a "!profile.other".
  686. $seen_profile = 1;
  687. }
  688. } elsif ($found) {
  689. $seen_profile = 1;
  690. last;
  691. }
  692. }
  693. return $seen_profile;
  694. }
  695. sub reduce_profiles {
  696. my ($self, $build_profiles) = @_;
  697. if (not $self->profile_is_concerned($build_profiles)) {
  698. $self->reset();
  699. } else {
  700. $self->{restrictions} = undef;
  701. }
  702. }
  703. sub get_evaluation {
  704. my ($self, $facts) = @_;
  705. return if not defined $self->{package};
  706. return $facts->_evaluate_simple_dep($self);
  707. }
  708. sub simplify_deps {
  709. my ($self, $facts) = @_;
  710. my $eval = $self->get_evaluation($facts);
  711. $self->reset() if defined $eval and $eval == 1;
  712. }
  713. sub is_empty {
  714. my $self = shift;
  715. return not defined $self->{package};
  716. }
  717. sub merge_union {
  718. my ($self, $o) = @_;
  719. return 0 if not $o->isa('Dpkg::Deps::Simple');
  720. return 0 if $self->is_empty() or $o->is_empty();
  721. return 0 if $self->{package} ne $o->{package};
  722. return 0 if defined $self->{arches} or defined $o->{arches};
  723. if (not defined $o->{relation} and defined $self->{relation}) {
  724. # Union is the non-versioned dependency
  725. $self->{relation} = undef;
  726. $self->{version} = undef;
  727. return 1;
  728. }
  729. my $implication = $self->implies($o);
  730. my $rev_implication = $o->implies($self);
  731. if (defined($implication)) {
  732. if ($implication) {
  733. $self->{relation} = $o->{relation};
  734. $self->{version} = $o->{version};
  735. return 1;
  736. } else {
  737. return 0;
  738. }
  739. }
  740. if (defined($rev_implication)) {
  741. if ($rev_implication) {
  742. # Already merged...
  743. return 1;
  744. } else {
  745. return 0;
  746. }
  747. }
  748. return 0;
  749. }
  750. package Dpkg::Deps::Multiple;
  751. =head2 Dpkg::Deps::Multiple
  752. This is the base class for Dpkg::Deps::{AND,OR,Union}. It implements
  753. the following methods:
  754. =over 4
  755. =item $mul->add($dep)
  756. Adds a new dependency object at the end of the list.
  757. =back
  758. =cut
  759. use strict;
  760. use warnings;
  761. use Carp;
  762. use Dpkg::ErrorHandling;
  763. use parent qw(Dpkg::Interface::Storable);
  764. sub new {
  765. my $this = shift;
  766. my $class = ref($this) || $this;
  767. my $self = { list => [ @_ ] };
  768. bless $self, $class;
  769. return $self;
  770. }
  771. sub reset {
  772. my ($self) = @_;
  773. $self->{list} = [];
  774. }
  775. sub add {
  776. my $self = shift;
  777. push @{$self->{list}}, @_;
  778. }
  779. sub get_deps {
  780. my $self = shift;
  781. return grep { not $_->is_empty() } @{$self->{list}};
  782. }
  783. sub sort {
  784. my $self = shift;
  785. my @res = ();
  786. @res = sort { Dpkg::Deps::deps_compare($a, $b) } @{$self->{list}};
  787. $self->{list} = [ @res ];
  788. }
  789. sub arch_is_concerned {
  790. my ($self, $host_arch) = @_;
  791. my $res = 0;
  792. foreach my $dep (@{$self->{list}}) {
  793. $res = 1 if $dep->arch_is_concerned($host_arch);
  794. }
  795. return $res;
  796. }
  797. sub reduce_arch {
  798. my ($self, $host_arch) = @_;
  799. my @new;
  800. foreach my $dep (@{$self->{list}}) {
  801. $dep->reduce_arch($host_arch);
  802. push @new, $dep if $dep->arch_is_concerned($host_arch);
  803. }
  804. $self->{list} = [ @new ];
  805. }
  806. sub has_arch_restriction {
  807. my ($self) = @_;
  808. my @res;
  809. foreach my $dep (@{$self->{list}}) {
  810. push @res, $dep->has_arch_restriction();
  811. }
  812. return @res;
  813. }
  814. sub profile_is_concerned {
  815. my ($self, $build_profiles) = @_;
  816. my $res = 0;
  817. foreach my $dep (@{$self->{list}}) {
  818. $res = 1 if $dep->profile_is_concerned($build_profiles);
  819. }
  820. return $res;
  821. }
  822. sub reduce_profiles {
  823. my ($self, $build_profiles) = @_;
  824. my @new;
  825. foreach my $dep (@{$self->{list}}) {
  826. $dep->reduce_profiles($build_profiles);
  827. push @new, $dep if $dep->profile_is_concerned($build_profiles);
  828. }
  829. $self->{list} = [ @new ];
  830. }
  831. sub is_empty {
  832. my $self = shift;
  833. return scalar @{$self->{list}} == 0;
  834. }
  835. sub merge_union {
  836. croak 'method merge_union() is only valid for Dpkg::Deps::Simple';
  837. }
  838. package Dpkg::Deps::AND;
  839. =head2 Dpkg::Deps::AND
  840. This object represents a list of dependencies who must be met at the same
  841. time.
  842. =over 4
  843. =item $and->output([$fh])
  844. The output method uses ", " to join the list of sub-dependencies.
  845. =back
  846. =cut
  847. use strict;
  848. use warnings;
  849. use parent -norequire, qw(Dpkg::Deps::Multiple);
  850. sub output {
  851. my ($self, $fh) = @_;
  852. my $res = join(', ', map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  853. if (defined($fh)) {
  854. print { $fh } $res;
  855. }
  856. return $res;
  857. }
  858. sub implies {
  859. my ($self, $o) = @_;
  860. # If any individual member can imply $o or NOT $o, we're fine
  861. foreach my $dep ($self->get_deps()) {
  862. my $implication = $dep->implies($o);
  863. return 1 if defined($implication) && $implication == 1;
  864. return 0 if defined($implication) && $implication == 0;
  865. }
  866. # If o is an AND, we might have an implication, if we find an
  867. # implication within us for each predicate in o
  868. if ($o->isa('Dpkg::Deps::AND')) {
  869. my $subset = 1;
  870. foreach my $odep ($o->get_deps()) {
  871. my $found = 0;
  872. foreach my $dep ($self->get_deps()) {
  873. $found = 1 if $dep->implies($odep);
  874. }
  875. $subset = 0 if not $found;
  876. }
  877. return 1 if $subset;
  878. }
  879. return;
  880. }
  881. sub get_evaluation {
  882. my ($self, $facts) = @_;
  883. # Return 1 only if all members evaluates to true
  884. # Return 0 if at least one member evaluates to false
  885. # Return undef otherwise
  886. my $result = 1;
  887. foreach my $dep ($self->get_deps()) {
  888. my $eval = $dep->get_evaluation($facts);
  889. if (not defined $eval) {
  890. $result = undef;
  891. } elsif ($eval == 0) {
  892. $result = 0;
  893. last;
  894. } elsif ($eval == 1) {
  895. # Still possible
  896. }
  897. }
  898. return $result;
  899. }
  900. sub simplify_deps {
  901. my ($self, $facts, @knowndeps) = @_;
  902. my @new;
  903. WHILELOOP:
  904. while (@{$self->{list}}) {
  905. my $dep = shift @{$self->{list}};
  906. my $eval = $dep->get_evaluation($facts);
  907. next if defined($eval) and $eval == 1;
  908. foreach my $odep (@knowndeps, @new) {
  909. next WHILELOOP if $odep->implies($dep);
  910. }
  911. # When a dependency is implied by another dependency that
  912. # follows, then invert them
  913. # "a | b, c, a" becomes "a, c" and not "c, a"
  914. my $i = 0;
  915. foreach my $odep (@{$self->{list}}) {
  916. if (defined $odep and $odep->implies($dep)) {
  917. splice @{$self->{list}}, $i, 1;
  918. unshift @{$self->{list}}, $odep;
  919. next WHILELOOP;
  920. }
  921. $i++;
  922. }
  923. push @new, $dep;
  924. }
  925. $self->{list} = [ @new ];
  926. }
  927. package Dpkg::Deps::OR;
  928. =head2 Dpkg::Deps::OR
  929. This object represents a list of dependencies of which only one must be met
  930. for the dependency to be true.
  931. =over 4
  932. =item $or->output([$fh])
  933. The output method uses " | " to join the list of sub-dependencies.
  934. =back
  935. =cut
  936. use strict;
  937. use warnings;
  938. use parent -norequire, qw(Dpkg::Deps::Multiple);
  939. sub output {
  940. my ($self, $fh) = @_;
  941. my $res = join(' | ', map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  942. if (defined($fh)) {
  943. print { $fh } $res;
  944. }
  945. return $res;
  946. }
  947. sub implies {
  948. my ($self, $o) = @_;
  949. # Special case for AND with a single member, replace it by its member
  950. if ($o->isa('Dpkg::Deps::AND')) {
  951. my @subdeps = $o->get_deps();
  952. if (scalar(@subdeps) == 1) {
  953. $o = $subdeps[0];
  954. }
  955. }
  956. # In general, an OR dependency can't imply anything except if each
  957. # of its member implies a member in the other OR dependency
  958. if ($o->isa('Dpkg::Deps::OR')) {
  959. my $subset = 1;
  960. foreach my $dep ($self->get_deps()) {
  961. my $found = 0;
  962. foreach my $odep ($o->get_deps()) {
  963. $found = 1 if $dep->implies($odep);
  964. }
  965. $subset = 0 if not $found;
  966. }
  967. return 1 if $subset;
  968. }
  969. return;
  970. }
  971. sub get_evaluation {
  972. my ($self, $facts) = @_;
  973. # Returns false if all members evaluates to 0
  974. # Returns true if at least one member evaluates to true
  975. # Returns undef otherwise
  976. my $result = 0;
  977. foreach my $dep ($self->get_deps()) {
  978. my $eval = $dep->get_evaluation($facts);
  979. if (not defined $eval) {
  980. $result = undef;
  981. } elsif ($eval == 1) {
  982. $result = 1;
  983. last;
  984. } elsif ($eval == 0) {
  985. # Still possible to have a false evaluation
  986. }
  987. }
  988. return $result;
  989. }
  990. sub simplify_deps {
  991. my ($self, $facts) = @_;
  992. my @new;
  993. WHILELOOP:
  994. while (@{$self->{list}}) {
  995. my $dep = shift @{$self->{list}};
  996. my $eval = $dep->get_evaluation($facts);
  997. if (defined($eval) and $eval == 1) {
  998. $self->{list} = [];
  999. return;
  1000. }
  1001. foreach my $odep (@new, @{$self->{list}}) {
  1002. next WHILELOOP if $odep->implies($dep);
  1003. }
  1004. push @new, $dep;
  1005. }
  1006. $self->{list} = [ @new ];
  1007. }
  1008. package Dpkg::Deps::Union;
  1009. =head2 Dpkg::Deps::Union
  1010. This object represents a list of relationships.
  1011. =over 4
  1012. =item $union->output([$fh])
  1013. The output method uses ", " to join the list of relationships.
  1014. =item $union->implies($other_dep)
  1015. =item $union->get_evaluation($other_dep)
  1016. Those methods are not meaningful for this object and always return undef.
  1017. =item $union->simplify_deps($facts)
  1018. The simplication is done to generate an union of all the relationships.
  1019. It uses $simple_dep->merge_union($other_dep) to get its job done.
  1020. =back
  1021. =cut
  1022. use strict;
  1023. use warnings;
  1024. use parent -norequire, qw(Dpkg::Deps::Multiple);
  1025. sub output {
  1026. my ($self, $fh) = @_;
  1027. my $res = join(', ', map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  1028. if (defined($fh)) {
  1029. print { $fh } $res;
  1030. }
  1031. return $res;
  1032. }
  1033. sub implies {
  1034. # Implication test are not useful on Union
  1035. return;
  1036. }
  1037. sub get_evaluation {
  1038. # Evaluation are not useful on Union
  1039. return;
  1040. }
  1041. sub simplify_deps {
  1042. my ($self, $facts) = @_;
  1043. my @new;
  1044. WHILELOOP:
  1045. while (@{$self->{list}}) {
  1046. my $odep = shift @{$self->{list}};
  1047. foreach my $dep (@new) {
  1048. next WHILELOOP if $dep->merge_union($odep);
  1049. }
  1050. push @new, $odep;
  1051. }
  1052. $self->{list} = [ @new ];
  1053. }
  1054. package Dpkg::Deps::KnownFacts;
  1055. =head2 Dpkg::Deps::KnownFacts
  1056. This object represents a list of installed packages and a list of virtual
  1057. packages provided (by the set of installed packages).
  1058. =over 4
  1059. =item my $facts = Dpkg::Deps::KnownFacts->new();
  1060. Creates a new object.
  1061. =cut
  1062. use strict;
  1063. use warnings;
  1064. use Dpkg::Version;
  1065. sub new {
  1066. my $this = shift;
  1067. my $class = ref($this) || $this;
  1068. my $self = {
  1069. pkg => {},
  1070. virtualpkg => {},
  1071. };
  1072. bless $self, $class;
  1073. return $self;
  1074. }
  1075. =item $facts->add_installed_package($package, $version, $arch, $multiarch)
  1076. Records that the given version of the package is installed. If
  1077. $version/$arch is undefined we know that the package is installed but we
  1078. don't know which version/architecture it is. $multiarch is the Multi-Arch
  1079. field of the package. If $multiarch is undef, it will be equivalent to
  1080. "Multi-Arch: no".
  1081. Note that $multiarch is only used if $arch is provided.
  1082. =cut
  1083. sub add_installed_package {
  1084. my ($self, $pkg, $ver, $arch, $multiarch) = @_;
  1085. my $p = {
  1086. package => $pkg,
  1087. version => $ver,
  1088. architecture => $arch,
  1089. multiarch => $multiarch || 'no',
  1090. };
  1091. $self->{pkg}{"$pkg:$arch"} = $p if defined $arch;
  1092. push @{$self->{pkg}{$pkg}}, $p;
  1093. }
  1094. =item $facts->add_provided_package($virtual, $relation, $version, $by)
  1095. Records that the "$by" package provides the $virtual package. $relation
  1096. and $version correspond to the associated relation given in the Provides
  1097. field. This might be used in the future for versioned provides.
  1098. =cut
  1099. sub add_provided_package {
  1100. my ($self, $pkg, $rel, $ver, $by) = @_;
  1101. if (not exists $self->{virtualpkg}{$pkg}) {
  1102. $self->{virtualpkg}{$pkg} = [];
  1103. }
  1104. push @{$self->{virtualpkg}{$pkg}}, [ $by, $rel, $ver ];
  1105. }
  1106. =item my ($check, $param) = $facts->check_package($package)
  1107. $check is one when the package is found. For a real package, $param
  1108. contains the version. For a virtual package, $param contains an array
  1109. reference containing the list of packages that provide it (each package is
  1110. listed as [ $provider, $relation, $version ]).
  1111. This function is obsolete and should not be used. Dpkg::Deps::KnownFacts
  1112. is only meant to be filled with data and then passed to Dpkg::Deps
  1113. methods where appropriate, but it should not be directly queried.
  1114. =back
  1115. =cut
  1116. sub check_package {
  1117. my ($self, $pkg) = @_;
  1118. if (exists $self->{pkg}{$pkg}) {
  1119. return (1, $self->{pkg}{$pkg}[0]{version});
  1120. }
  1121. if (exists $self->{virtualpkg}{$pkg}) {
  1122. return (1, $self->{virtualpkg}{$pkg});
  1123. }
  1124. return (0, undef);
  1125. }
  1126. ## The functions below are private to Dpkg::Deps
  1127. sub _find_package {
  1128. my ($self, $dep, $lackinfos) = @_;
  1129. my ($pkg, $archqual) = ($dep->{package}, $dep->{archqual});
  1130. return if not exists $self->{pkg}{$pkg};
  1131. my $host_arch = $dep->{host_arch};
  1132. my $build_arch = $dep->{build_arch};
  1133. foreach my $p (@{$self->{pkg}{$pkg}}) {
  1134. my $a = $p->{architecture};
  1135. my $ma = $p->{multiarch};
  1136. if (not defined $a) {
  1137. $$lackinfos = 1;
  1138. next;
  1139. }
  1140. if (not defined $archqual) {
  1141. return $p if $ma eq 'foreign';
  1142. return $p if $a eq $host_arch or $a eq 'all';
  1143. } elsif ($archqual eq 'any') {
  1144. return $p if $ma eq 'allowed';
  1145. } elsif ($archqual eq 'native') {
  1146. return $p if $a eq $build_arch and $ma ne 'foreign';
  1147. } else {
  1148. return $p if $a eq $archqual;
  1149. }
  1150. }
  1151. return;
  1152. }
  1153. sub _find_virtual_packages {
  1154. my ($self, $pkg) = @_;
  1155. return () if not exists $self->{virtualpkg}{$pkg};
  1156. return @{$self->{virtualpkg}{$pkg}};
  1157. }
  1158. sub _evaluate_simple_dep {
  1159. my ($self, $dep) = @_;
  1160. my ($lackinfos, $pkg) = (0, $dep->{package});
  1161. my $p = $self->_find_package($dep, \$lackinfos);
  1162. if ($p) {
  1163. if (defined $dep->{relation}) {
  1164. if (defined $p->{version}) {
  1165. return 1 if version_compare_relation($p->{version},
  1166. $dep->{relation}, $dep->{version});
  1167. } else {
  1168. $lackinfos = 1;
  1169. }
  1170. } else {
  1171. return 1;
  1172. }
  1173. }
  1174. foreach my $virtpkg ($self->_find_virtual_packages($pkg)) {
  1175. # XXX: Adapt when versioned provides are allowed
  1176. next if defined $virtpkg->[1];
  1177. next if defined $dep->{relation}; # Provides don't satisfy versioned deps
  1178. return 1;
  1179. }
  1180. return if $lackinfos;
  1181. return 0;
  1182. }
  1183. =head1 CHANGES
  1184. =head2 Version 1.04
  1185. New options: Add use_profiles, build_profiles, reduce_profiles and
  1186. reduce_restrictions to Dpkg::Deps::deps_parse().
  1187. New methods: Add $dep->profile_is_concerned() and $dep->reduce_profiles()
  1188. for all dependency objects.
  1189. =head2 Version 1.03
  1190. New option: Add build_arch option to Dpkg::Deps::deps_parse().
  1191. =head2 Version 1.02
  1192. New function: Dpkg::Deps::deps_concat()
  1193. =head2 Version 1.01
  1194. New method: Add $dep->reset() for all dependency objects.
  1195. New property: Dpkg::Deps::Simple now recognizes the arch qualifier "any"
  1196. and stores it in the "archqual" property when present.
  1197. New option: Dpkg::Deps::KnownFacts->add_installed_package() now accepts 2
  1198. supplementary parameters ($arch and $multiarch).
  1199. Deprecated method: Dpkg::Deps::KnownFacts->check_package() is obsolete,
  1200. it should not have been part of the public API.
  1201. =cut
  1202. 1;