dpkg-checkbuilddeps.pl 5.2 KB

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