dpkg-checkbuilddeps.pl 5.1 KB

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