dpkg-checkbuilddeps.pl 5.3 KB

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