dpkg-checkbuilddeps.pl 6.3 KB

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