dpkg-checkbuilddeps.pl 5.1 KB

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