dpkg-checkbuilddeps.pl 5.2 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. local $/='';
  29. my $cdata=<CONTROL>;
  30. close CONTROL;
  31. my $dep_regex=qr/\s*((.|\n\s+)*)\s/; # allow multi-line
  32. if ($cdata =~ /^Build-Depends:$dep_regex/mi) {
  33. push @unmet, build_depends($1, @status);
  34. }
  35. if ($cdata =~ /^Build-Conflicts:$dep_regex/mi) {
  36. push @conflicts, build_conflicts($1, @status);
  37. }
  38. if (! $binary_only && $cdata =~ /^Build-Depends-Indep:$dep_regex/mi) {
  39. push @unmet, build_depends($1, @status);
  40. }
  41. if (! $binary_only && $cdata =~ /^Build-Conflicts-Indep:$dep_regex/mi) {
  42. push @conflicts, build_conflicts($1, @status);
  43. }
  44. if (@unmet) {
  45. print STDERR "$me: Unmet build dependencies: ";
  46. print STDERR join(" ", @unmet), "\n";
  47. }
  48. if (@conflicts) {
  49. print STDERR "$me: Build conflicts: ";
  50. print STDERR join(" ", @conflicts), "\n";
  51. }
  52. exit 1 if @unmet || @conflicts;
  53. # This part could be replaced. Silly little status file parser.
  54. # thanks to Matt Zimmerman. Returns two hash references that
  55. # are exactly what the other functions need...
  56. sub parse_status {
  57. my $status=shift || "/var/lib/dpkg/status";
  58. my %providers;
  59. my %version;
  60. local $/ = '';
  61. open(STATUS, "<$status") || die "$status: $!\n";
  62. while (<STATUS>) {
  63. next unless /^Status: .*ok installed$/m;
  64. my ($package) = /^Package: (.*)$/m;
  65. push @{$providers{$package}}, $package;
  66. ($version{$package}) = /^Version: (.*)$/m;
  67. if (/^Provides: (.*)$/m) {
  68. foreach (split(/,\s*/, $1)) {
  69. push @{$providers{$_}}, $package;
  70. }
  71. }
  72. }
  73. close STATUS;
  74. return \%version, \%providers;
  75. }
  76. # This function checks the build dependencies passed in as the first
  77. # parameter. If they are satisfied, returns false. If they are unsatisfied,
  78. # an list of the unsatisfied depends is returned.
  79. #
  80. # Additional parameters that must be passed:
  81. # * A reference to a hash of all "ok installed" the packages on the system,
  82. # with the hash key being the package name, and the value being the
  83. # installed version.
  84. # * A reference to a hash, where the keys are package names, and the
  85. # value is a true value iff some package installed on the system provides
  86. # that package (all installed packages provide themselves)
  87. #
  88. # Optionally, the architecture the package is to be built for can be passed
  89. # in as the 4th parameter. If not set, dpkg will be queried for the build
  90. # architecture.
  91. sub build_depends {
  92. return check_line(1, @_);
  93. }
  94. # This function is exactly like unmet_build_depends, except it
  95. # checks for build conflicts, and returns a list of the packages
  96. # that are installed and are conflicted with.
  97. sub build_conflicts {
  98. return check_line(0, @_);
  99. }
  100. # This function does all the work. The first parameter is 1 to check build
  101. # deps, and 0 to check build conflicts.
  102. sub check_line {
  103. my $build_depends=shift;
  104. my $line=shift;
  105. my %version=%{shift()};
  106. my %providers=%{shift()};
  107. my $host_arch=shift || `dpkg-architecture -qDEB_HOST_ARCH`;
  108. chomp $host_arch;
  109. my @unmet=();
  110. foreach my $dep (split(/,\s*/, $line)) {
  111. my $ok=0;
  112. my @possibles=();
  113. ALTERNATE: foreach my $alternate (split(/\s*\|\s*/, $dep)) {
  114. my ($package, $rest)=split(/\s*(?=[[(])/, $alternate, 2);
  115. $package =~ s/\s*$//;
  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 $host_arch) {
  122. $seen_arch=1;
  123. next;
  124. }
  125. elsif ($arch eq "!$host_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 dependency.
  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*([<>=]{1,2})\s*(.*?)\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 dependency, and
  165. # nothing provides it, so fail.
  166. next;
  167. }
  168. # If we get to here, the dependency 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. }