dpkg-checkbuilddeps.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/perl
  2. # GPL copyright 2001 by Joey Hess <joeyh@debian.org>
  3. use strict;
  4. use warnings;
  5. use Getopt::Long;
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(error);
  9. use Dpkg::Arch qw(get_host_arch);
  10. use Dpkg::Deps;
  11. use Dpkg::Control;
  12. push(@INC,$dpkglibdir);
  13. require 'controllib.pl';
  14. textdomain("dpkg-dev");
  15. sub usage {
  16. printf _g(
  17. "Usage: %s [<option> ...] [<control-file>]
  18. Options:
  19. control-file control file to process (default: debian/control).
  20. -B binary-only, ignore -Indep.
  21. --admindir=<directory>
  22. change the administrative directory.
  23. -h show this help message.
  24. "), $progname;
  25. }
  26. my $binary_only=0;
  27. my $want_help=0;
  28. if (! GetOptions('-B' => \$binary_only,
  29. '-h' => \$want_help,
  30. '--admindir=s' => \$admindir)) {
  31. usage();
  32. exit(2);
  33. }
  34. if ($want_help) {
  35. usage();
  36. exit(0);
  37. }
  38. my $controlfile = shift || "debian/control";
  39. my $control = Dpkg::Control->new($controlfile);
  40. my $fields = $control->get_source();
  41. my $facts = parse_status("$admindir/status");
  42. my (@unmet, @conflicts);
  43. my $dep_regex=qr/[ \t]*(([^\n]+|\n[ \t])*)\s/; # allow multi-line
  44. if (defined($fields->{"Build-Depends"})) {
  45. push @unmet, build_depends('Build-Depends',
  46. Dpkg::Deps::parse($fields->{"Build-Depends"},
  47. reduce_arch => 1), $facts);
  48. }
  49. if (defined($fields->{"C Build-Conflicts"})) {
  50. push @conflicts, build_conflicts('Build-Conflicts',
  51. Dpkg::Deps::parse($fields->{"Build-Conflicts"},
  52. reduce_arch => 1, union => 1), $facts);
  53. }
  54. if (! $binary_only && defined($fields->{"Build-Depends-Indep"})) {
  55. push @unmet, build_depends('Build-Depends-Indep',
  56. Dpkg::Deps::parse($fields->{"Build-Depends-Indep"},
  57. reduce_arch => 1), $facts);
  58. }
  59. if (! $binary_only && defined($fields->{"Build-Conflicts-Indep"})) {
  60. push @conflicts, build_conflicts('Build-Conflicts-Indep',
  61. Dpkg::Deps::parse($fields->{"Build-Conflicts-Indep"},
  62. reduce_arch => 1, union => 1), $facts);
  63. }
  64. if (@unmet) {
  65. printf STDERR _g("%s: Unmet build dependencies: "), $progname;
  66. print STDERR join(" ", map { $_->dump() } @unmet), "\n";
  67. }
  68. if (@conflicts) {
  69. printf STDERR _g("%s: Build conflicts: "), $progname;
  70. print STDERR join(" ", map { $_->dump() } @conflicts), "\n";
  71. }
  72. exit 1 if @unmet || @conflicts;
  73. # Silly little status file parser that returns a Dpkg::Deps::KnownFacts
  74. sub parse_status {
  75. my $status = shift;
  76. my $facts = Dpkg::Deps::KnownFacts->new();
  77. local $/ = '';
  78. open(STATUS, "<$status") || die "$status: $!\n";
  79. while (<STATUS>) {
  80. next unless /^Status: .*ok installed$/m;
  81. my ($package) = /^Package: (.*)$/m;
  82. my ($version) = /^Version: (.*)$/m;
  83. $facts->add_installed_package($package, $version);
  84. if (/^Provides: (.*)$/m) {
  85. my $provides = Dpkg::Deps::parse($1,
  86. reduce_arch => 1, union => 1);
  87. next if not defined $provides;
  88. foreach (grep { $_->isa('Dpkg::Deps::Simple') }
  89. $provides->get_deps())
  90. {
  91. $facts->add_provided_package($_->{package},
  92. $_->{relation}, $_->{version},
  93. $package);
  94. }
  95. }
  96. }
  97. close STATUS;
  98. return $facts;
  99. }
  100. # This function checks the build dependencies passed in as the first
  101. # parameter. If they are satisfied, returns false. If they are unsatisfied,
  102. # an list of the unsatisfied depends is returned.
  103. #
  104. # Additional parameters that must be passed:
  105. # * A reference to a hash of all "ok installed" the packages on the system,
  106. # with the hash key being the package name, and the value being the
  107. # installed version.
  108. # * A reference to a hash, where the keys are package names, and the
  109. # value is a true value iff some package installed on the system provides
  110. # that package (all installed packages provide themselves)
  111. #
  112. # Optionally, the architecture the package is to be built for can be passed
  113. # in as the 4th parameter. If not set, dpkg will be queried for the build
  114. # architecture.
  115. sub build_depends {
  116. return check_line(1, @_);
  117. }
  118. # This function is exactly like unmet_build_depends, except it
  119. # checks for build conflicts, and returns a list of the packages
  120. # that are installed and are conflicted with.
  121. sub build_conflicts {
  122. return check_line(0, @_);
  123. }
  124. # This function does all the work. The first parameter is 1 to check build
  125. # deps, and 0 to check build conflicts.
  126. sub check_line {
  127. my $build_depends=shift;
  128. my $fieldname=shift;
  129. my $dep_list=shift;
  130. my $facts=shift;
  131. my $host_arch = shift || get_host_arch();
  132. chomp $host_arch;
  133. my @unmet=();
  134. unless(defined($dep_list)) {
  135. error(_g("error occurred while parsing %s"), $fieldname);
  136. }
  137. if ($build_depends) {
  138. $dep_list->simplify_deps($facts);
  139. if ($dep_list->is_empty()) {
  140. return ();
  141. } else {
  142. return $dep_list->get_deps();
  143. }
  144. } else { # Build-Conflicts
  145. my @conflicts = ();
  146. foreach my $dep ($dep_list->get_deps()) {
  147. if ($dep->get_evaluation($facts)) {
  148. push @conflicts, $dep;
  149. }
  150. }
  151. return @conflicts;
  152. }
  153. }