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