dpkg-checkbuilddeps.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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") || syserr(_g("cannot open %s"), $status);
  106. while (<STATUS>) {
  107. next unless /^Status: .*ok installed$/m;
  108. my ($package) = /^Package: (.*)$/m;
  109. my ($version) = /^Version: (.*)$/m;
  110. my ($arch) = /^Architecture: (.*)$/m;
  111. my ($multiarch) = /^Multi-Arch: (.*)$/m;
  112. $facts->add_installed_package($package, $version, $arch,
  113. $multiarch);
  114. if (/^Provides: (.*)$/m) {
  115. my $provides = deps_parse($1, reduce_arch => 1, union => 1);
  116. next if not defined $provides;
  117. foreach (grep { $_->isa('Dpkg::Deps::Simple') }
  118. $provides->get_deps())
  119. {
  120. $facts->add_provided_package($_->{package},
  121. $_->{relation}, $_->{version},
  122. $package);
  123. }
  124. }
  125. }
  126. close STATUS;
  127. return $facts;
  128. }
  129. # This function checks the build dependencies passed in as the first
  130. # parameter. If they are satisfied, returns false. If they are unsatisfied,
  131. # an list of the unsatisfied depends is returned.
  132. #
  133. # Additional parameters that must be passed:
  134. # * A reference to a hash of all "ok installed" the packages on the system,
  135. # with the hash key being the package name, and the value being the
  136. # installed version.
  137. # * A reference to a hash, where the keys are package names, and the
  138. # value is a true value iff some package installed on the system provides
  139. # that package (all installed packages provide themselves)
  140. #
  141. # Optionally, the architecture the package is to be built for can be passed
  142. # in as the 4th parameter. If not set, dpkg will be queried for the build
  143. # architecture.
  144. sub build_depends {
  145. return check_line(1, @_);
  146. }
  147. # This function is exactly like unmet_build_depends, except it
  148. # checks for build conflicts, and returns a list of the packages
  149. # that are installed and are conflicted with.
  150. sub build_conflicts {
  151. return check_line(0, @_);
  152. }
  153. # This function does all the work. The first parameter is 1 to check build
  154. # deps, and 0 to check build conflicts.
  155. sub check_line {
  156. my $build_depends=shift;
  157. my $fieldname=shift;
  158. my $dep_list=shift;
  159. my $facts=shift;
  160. my $host_arch = shift || get_host_arch();
  161. chomp $host_arch;
  162. my @unmet=();
  163. unless(defined($dep_list)) {
  164. error(_g("error occurred while parsing %s"), $fieldname);
  165. }
  166. if ($build_depends) {
  167. $dep_list->simplify_deps($facts);
  168. if ($dep_list->is_empty()) {
  169. return ();
  170. } else {
  171. return $dep_list->get_deps();
  172. }
  173. } else { # Build-Conflicts
  174. my @conflicts = ();
  175. foreach my $dep ($dep_list->get_deps()) {
  176. if ($dep->get_evaluation($facts)) {
  177. push @conflicts, $dep;
  178. }
  179. }
  180. return @conflicts;
  181. }
  182. }