dpkg-checkbuilddeps.pl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-checkbuilddeps
  4. #
  5. # Copyright © 2001 Joey Hess <joeyh@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. use strict;
  20. use warnings;
  21. use Getopt::Long;
  22. use Dpkg;
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Arch qw(get_host_arch);
  26. use Dpkg::Deps;
  27. use Dpkg::Control::Info;
  28. textdomain("dpkg-dev");
  29. sub usage {
  30. printf _g(
  31. "Usage: %s [<option> ...] [<control-file>]
  32. Options:
  33. control-file control file to process (default: debian/control).
  34. -B binary-only, ignore -Indep.
  35. -d build-deps use given string as build dependencies instead of
  36. retrieving them from control file
  37. -c build-conf use given string for build conflicts instead of
  38. retrieving them from control file
  39. --admindir=<directory>
  40. change the administrative directory.
  41. -h show this help message.
  42. "), $progname;
  43. }
  44. my $binary_only=0;
  45. my $want_help=0;
  46. my ($bd_value, $bc_value);
  47. if (! GetOptions('-B' => \$binary_only,
  48. '-h' => \$want_help,
  49. '-d=s' => \$bd_value,
  50. '-c=s' => \$bc_value,
  51. '--admindir=s' => \$admindir)) {
  52. usage();
  53. exit(2);
  54. }
  55. if ($want_help) {
  56. usage();
  57. exit(0);
  58. }
  59. my $controlfile = shift || "debian/control";
  60. my $control = Dpkg::Control::Info->new($controlfile);
  61. my $fields = $control->get_source();
  62. my $facts = parse_status("$admindir/status");
  63. unless (defined($bd_value) or defined($bc_value)) {
  64. $bd_value = 'build-essential';
  65. $bd_value .= ", " . $fields->{"Build-Depends"} if defined $fields->{"Build-Depends"};
  66. if (not $binary_only and defined $fields->{"Build-Depends-Indep"}) {
  67. $bd_value .= ", " . $fields->{"Build-Depends-Indep"};
  68. }
  69. $bc_value = $fields->{"Build-Conflicts"} if defined $fields->{"Build-Conflicts"};
  70. if (not $binary_only and defined $fields->{"Build-Conflicts-Indep"}) {
  71. if ($bc_value) {
  72. $bc_value .= ", " . $fields->{"Build-Conflicts-Indep"};
  73. } else {
  74. $bc_value = $fields->{"Build-Conflicts-Indep"};
  75. }
  76. }
  77. }
  78. my (@unmet, @conflicts);
  79. if ($bd_value) {
  80. push @unmet, build_depends('Build-Depends/Build-Depends-Indep)',
  81. Dpkg::Deps::parse($bd_value, reduce_arch => 1), $facts);
  82. }
  83. if ($bc_value) {
  84. push @conflicts, build_conflicts('Build-Conflicts/Build-Conflicts-Indep',
  85. Dpkg::Deps::parse($bc_value, reduce_arch => 1, union => 1), $facts);
  86. }
  87. if (@unmet) {
  88. printf STDERR _g("%s: Unmet build dependencies: "), $progname;
  89. print STDERR join(" ", map { $_->dump() } @unmet), "\n";
  90. }
  91. if (@conflicts) {
  92. printf STDERR _g("%s: Build conflicts: "), $progname;
  93. print STDERR join(" ", map { $_->dump() } @conflicts), "\n";
  94. }
  95. exit 1 if @unmet || @conflicts;
  96. # Silly little status file parser that returns a Dpkg::Deps::KnownFacts
  97. sub parse_status {
  98. my $status = shift;
  99. my $facts = Dpkg::Deps::KnownFacts->new();
  100. local $/ = '';
  101. open(STATUS, "<$status") || die "$status: $!\n";
  102. while (<STATUS>) {
  103. next unless /^Status: .*ok installed$/m;
  104. my ($package) = /^Package: (.*)$/m;
  105. my ($version) = /^Version: (.*)$/m;
  106. $facts->add_installed_package($package, $version);
  107. if (/^Provides: (.*)$/m) {
  108. my $provides = Dpkg::Deps::parse($1,
  109. reduce_arch => 1, union => 1);
  110. next if not defined $provides;
  111. foreach (grep { $_->isa('Dpkg::Deps::Simple') }
  112. $provides->get_deps())
  113. {
  114. $facts->add_provided_package($_->{package},
  115. $_->{relation}, $_->{version},
  116. $package);
  117. }
  118. }
  119. }
  120. close STATUS;
  121. return $facts;
  122. }
  123. # This function checks the build dependencies passed in as the first
  124. # parameter. If they are satisfied, returns false. If they are unsatisfied,
  125. # an list of the unsatisfied depends is returned.
  126. #
  127. # Additional parameters that must be passed:
  128. # * A reference to a hash of all "ok installed" the packages on the system,
  129. # with the hash key being the package name, and the value being the
  130. # installed version.
  131. # * A reference to a hash, where the keys are package names, and the
  132. # value is a true value iff some package installed on the system provides
  133. # that package (all installed packages provide themselves)
  134. #
  135. # Optionally, the architecture the package is to be built for can be passed
  136. # in as the 4th parameter. If not set, dpkg will be queried for the build
  137. # architecture.
  138. sub build_depends {
  139. return check_line(1, @_);
  140. }
  141. # This function is exactly like unmet_build_depends, except it
  142. # checks for build conflicts, and returns a list of the packages
  143. # that are installed and are conflicted with.
  144. sub build_conflicts {
  145. return check_line(0, @_);
  146. }
  147. # This function does all the work. The first parameter is 1 to check build
  148. # deps, and 0 to check build conflicts.
  149. sub check_line {
  150. my $build_depends=shift;
  151. my $fieldname=shift;
  152. my $dep_list=shift;
  153. my $facts=shift;
  154. my $host_arch = shift || get_host_arch();
  155. chomp $host_arch;
  156. my @unmet=();
  157. unless(defined($dep_list)) {
  158. error(_g("error occurred while parsing %s"), $fieldname);
  159. }
  160. if ($build_depends) {
  161. $dep_list->simplify_deps($facts);
  162. if ($dep_list->is_empty()) {
  163. return ();
  164. } else {
  165. return $dep_list->get_deps();
  166. }
  167. } else { # Build-Conflicts
  168. my @conflicts = ();
  169. foreach my $dep ($dep_list->get_deps()) {
  170. if ($dep->get_evaluation($facts)) {
  171. push @conflicts, $dep;
  172. }
  173. }
  174. return @conflicts;
  175. }
  176. }