dpkg-checkbuilddeps.pl 4.7 KB

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