dpkg-checkbuilddeps.pl 5.3 KB

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