Deps.pm 40 KB

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