dpkg-checkbuilddeps.pl 5.0 KB

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