dpkg-checkbuilddeps.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/perl
  2. # GPL copyright 2001 by Joey Hess <joeyh@debian.org>
  3. use strict;
  4. use warnings;
  5. use Getopt::Long;
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(error);
  9. push(@INC,$dpkglibdir);
  10. require 'controllib.pl';
  11. our %fi;
  12. textdomain("dpkg-dev");
  13. sub usage {
  14. printf _g(
  15. "Usage: %s [<option> ...] [<control-file>]
  16. Options:
  17. control-file control file to process (default: debian/control).
  18. -B binary-only, ignore -Indep.
  19. --admindir=<directory>
  20. change the administrative directory.
  21. -h show this help message.
  22. "), $progname;
  23. }
  24. my $binary_only=0;
  25. my $want_help=0;
  26. if (! GetOptions('-B' => \$binary_only,
  27. '-h' => \$want_help,
  28. '--admindir=s' => \$admindir)) {
  29. usage();
  30. exit(2);
  31. }
  32. if ($want_help) {
  33. usage();
  34. exit(0);
  35. }
  36. my $controlfile = shift || "debian/control";
  37. parsecontrolfile($controlfile);
  38. my @status = parse_status("$admindir/status");
  39. my (@unmet, @conflicts);
  40. my $dep_regex=qr/[ \t]*(([^\n]+|\n[ \t])*)\s/; # allow multi-line
  41. if (defined($fi{"C Build-Depends"})) {
  42. push @unmet, build_depends('Build-Depends',
  43. parsedep($fi{"C Build-Depends"}, 1, 1),
  44. @status);
  45. }
  46. if (defined($fi{"C Build-Conflicts"})) {
  47. push @conflicts, build_conflicts('Build-Conflicts',
  48. parsedep($fi{"C Build-Conflicts"}, 1, 1),
  49. @status);
  50. }
  51. if (! $binary_only && defined($fi{"C Build-Depends-Indep"})) {
  52. push @unmet, build_depends('Build-Depends-Indep',
  53. parsedep($fi{"C Build-Depends-Indep"}, 1, 1),
  54. @status);
  55. }
  56. if (! $binary_only && defined($fi{"C Build-Conflicts-Indep"})) {
  57. push @conflicts, build_conflicts('Build-Conflicts-Indep',
  58. parsedep($fi{"C Build-Conflicts-Indep"}, 1, 1),
  59. @status);
  60. }
  61. if (@unmet) {
  62. printf STDERR _g("%s: Unmet build dependencies: "), $progname;
  63. print STDERR join(" ", @unmet), "\n";
  64. }
  65. if (@conflicts) {
  66. printf STDERR _g("%s: Build conflicts: "), $progname;
  67. print STDERR join(" ", @conflicts), "\n";
  68. }
  69. exit 1 if @unmet || @conflicts;
  70. # This part could be replaced. Silly little status file parser.
  71. # thanks to Matt Zimmerman. Returns two hash references that
  72. # are exactly what the other functions need...
  73. sub parse_status {
  74. my $status = shift;
  75. my %providers;
  76. my %version;
  77. local $/ = '';
  78. open(STATUS, "<$status") || die "$status: $!\n";
  79. while (<STATUS>) {
  80. next unless /^Status: .*ok installed$/m;
  81. my ($package) = /^Package: (.*)$/m;
  82. push @{$providers{$package}}, $package;
  83. ($version{$package}) = /^Version: (.*)$/m;
  84. if (/^Provides: (.*)$/m) {
  85. foreach (split(/,\s*/, $1)) {
  86. push @{$providers{$_}}, $package;
  87. }
  88. }
  89. }
  90. close STATUS;
  91. return \%version, \%providers;
  92. }
  93. # This function checks the build dependencies passed in as the first
  94. # parameter. If they are satisfied, returns false. If they are unsatisfied,
  95. # an list of the unsatisfied depends is returned.
  96. #
  97. # Additional parameters that must be passed:
  98. # * A reference to a hash of all "ok installed" the packages on the system,
  99. # with the hash key being the package name, and the value being the
  100. # installed version.
  101. # * A reference to a hash, where the keys are package names, and the
  102. # value is a true value iff some package installed on the system provides
  103. # that package (all installed packages provide themselves)
  104. #
  105. # Optionally, the architecture the package is to be built for can be passed
  106. # in as the 4th parameter. If not set, dpkg will be queried for the build
  107. # architecture.
  108. sub build_depends {
  109. return check_line(1, @_);
  110. }
  111. # This function is exactly like unmet_build_depends, except it
  112. # checks for build conflicts, and returns a list of the packages
  113. # that are installed and are conflicted with.
  114. sub build_conflicts {
  115. return check_line(0, @_);
  116. }
  117. # This function does all the work. The first parameter is 1 to check build
  118. # deps, and 0 to check build conflicts.
  119. sub check_line {
  120. my $build_depends=shift;
  121. my $fieldname=shift;
  122. my $dep_list=shift;
  123. my %version=%{shift()};
  124. my %providers=%{shift()};
  125. my $host_arch = shift || get_host_arch();
  126. chomp $host_arch;
  127. my @unmet=();
  128. unless(defined($dep_list)) {
  129. &error(sprintf(_g("error occurred while parsing %s"),
  130. $fieldname));
  131. }
  132. foreach my $dep_and (@$dep_list) {
  133. my $ok=0;
  134. my @possibles=();
  135. ALTERNATE: foreach my $alternate (@$dep_and) {
  136. my ($package, $relation, $version, $arch_list)= @{$alternate};
  137. # This is a possibile way to meet the dependency.
  138. # Remove the arch stuff from $alternate.
  139. push @possibles, $package . ($relation && $version ? " ($relation $version)" : '');
  140. if ($relation && $version) {
  141. if (! exists $version{$package}) {
  142. # Not installed at all, so fail.
  143. next;
  144. }
  145. else {
  146. # Compare installed and needed
  147. # version number.
  148. system("dpkg", "--compare-versions",
  149. $version{$package}, $relation,
  150. $version);
  151. if (($? >> 8) != 0) {
  152. next; # fail
  153. }
  154. }
  155. }
  156. elsif (! defined $providers{$package}) {
  157. # It's not a versioned dependency, and
  158. # nothing provides it, so fail.
  159. next;
  160. }
  161. # If we get to here, the dependency was met.
  162. $ok=1;
  163. }
  164. if (@possibles && (($build_depends && ! $ok) ||
  165. (! $build_depends && $ok))) {
  166. # TODO: this could return a more complex
  167. # data structure instead to save re-parsing.
  168. push @unmet, join (" | ", @possibles);
  169. }
  170. }
  171. return @unmet;
  172. }