dpkg-checkbuilddeps.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. push @unmet, build_depends("build-essential", @status);
  32. my $dep_regex=qr/\s*((.|\n\s+)*)\s/; # allow multi-line
  33. if ($cdata =~ /^Build-Depends:$dep_regex/mi) {
  34. push @unmet, build_depends($1, @status);
  35. }
  36. if ($cdata =~ /^Build-Conflicts:$dep_regex/mi) {
  37. push @conflicts, build_conflicts($1, @status);
  38. }
  39. if (! $binary_only && $cdata =~ /^Build-Depends-Indep:$dep_regex/mi) {
  40. push @unmet, build_depends($1, @status);
  41. }
  42. if (! $binary_only && $cdata =~ /^Build-Conflicts-Indep:$dep_regex/mi) {
  43. push @conflicts, build_conflicts($1, @status);
  44. }
  45. if (@unmet) {
  46. print STDERR "$me: Unmet build dependencies: ";
  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 dependencies 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 $host_arch=shift || `dpkg-architecture -qDEB_HOST_ARCH`;
  109. chomp $host_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. $package =~ s/\s*$//;
  117. # Check arch specifications.
  118. if (defined $rest && $rest=~m/\[(.*?)\]/) {
  119. my $arches=lc($1);
  120. my $seen_arch='';
  121. foreach my $arch (split(' ', $arches)) {
  122. if ($arch eq $host_arch) {
  123. $seen_arch=1;
  124. next;
  125. }
  126. elsif ($arch eq "!$host_arch") {
  127. next ALTERNATE;
  128. }
  129. elsif ($arch =~ /!/) {
  130. # This is equivilant to
  131. # having seen the current arch,
  132. # unless the current arch
  133. # is also listed..
  134. $seen_arch=1;
  135. }
  136. }
  137. if (! $seen_arch) {
  138. next;
  139. }
  140. }
  141. # This is a possibile way to meet the dependency.
  142. # Remove the arch stuff from $alternate.
  143. $alternate=~s/\s+\[.*?\]//;
  144. push @possibles, $alternate;
  145. # Check version.
  146. if (defined $rest && $rest=~m/\(\s*([<>=]{1,2})\s*(.*?)\s*\)/) {
  147. my $relation=$1;
  148. my $version=$2;
  149. if (! exists $version{$package}) {
  150. # Not installed at all, so fail.
  151. next;
  152. }
  153. else {
  154. # Compare installed and needed
  155. # version number.
  156. system("dpkg", "--compare-versions",
  157. $version{$package}, $relation,
  158. $version);
  159. if (($? >> 8) != 0) {
  160. next; # fail
  161. }
  162. }
  163. }
  164. elsif (! defined $providers{$package}) {
  165. # It's not a versioned dependency, and
  166. # nothing provides it, so fail.
  167. next;
  168. }
  169. # If we get to here, the dependency was met.
  170. $ok=1;
  171. }
  172. if (@possibles && (($build_depends && ! $ok) ||
  173. (! $build_depends && $ok))) {
  174. # TODO: this could return a more complex
  175. # data structure instead to save re-parsing.
  176. push @unmet, join (" | ", @possibles);
  177. }
  178. }
  179. return @unmet;
  180. }