dpkg-checkbuilddeps.pl 5.1 KB

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