dpkg-checkbuilddeps.pl 5.1 KB

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