Deps.pm 40 KB

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