dpkg-checkbuilddeps.pl 5.1 KB

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