dpkg-checkbuilddeps.pl 6.0 KB

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