dpkg-checkbuilddeps.pl 5.1 KB

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