dpkg-checkbuilddeps.pl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-checkbuilddeps
  4. #
  5. # Copyright © 2001 Joey Hess <joeyh@debian.org>
  6. # Copyright © 2006-2009,2011-2012 Guillem Jover <guillem@debian.org>
  7. # Copyright © 2007-2011 Raphael Hertzog <hertzog@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. use strict;
  22. use warnings;
  23. use Getopt::Long qw(:config posix_default bundling no_ignorecase);
  24. use Dpkg;
  25. use Dpkg::Gettext;
  26. use Dpkg::ErrorHandling;
  27. use Dpkg::Arch qw(get_host_arch);
  28. use Dpkg::Deps;
  29. use Dpkg::Control::Info;
  30. textdomain('dpkg-dev');
  31. sub version()
  32. {
  33. printf(_g("Debian %s version %s.\n"), $progname, $version);
  34. exit(0);
  35. }
  36. sub usage {
  37. printf _g(
  38. 'Usage: %s [<option>...] [<control-file>]')
  39. . "\n\n" . _g(
  40. 'Options:
  41. -A ignore Build-Depends-Arch and Build-Conflicts-Arch.
  42. -B ignore Build-Depends-Indep and Build-Conflicts-Indep.
  43. -d build-deps use given string as build dependencies instead of
  44. retrieving them from control file
  45. -c build-conf use given string for build conflicts instead of
  46. retrieving them from control file
  47. -a arch assume given host architecture
  48. --admindir=<directory>
  49. change the administrative directory.
  50. -?, --help show this help message.
  51. --version show the version.')
  52. . "\n\n" . _g(
  53. '<control-file> is the control file to process (default: debian/control).')
  54. . "\n", $progname;
  55. }
  56. my $ignore_bd_arch = 0;
  57. my $ignore_bd_indep = 0;
  58. my ($bd_value, $bc_value);
  59. my $host_arch = get_host_arch();
  60. if (!GetOptions('A' => \$ignore_bd_arch,
  61. 'B' => \$ignore_bd_indep,
  62. 'help|?' => sub { usage(); exit(0); },
  63. 'version' => \&version,
  64. 'd=s' => \$bd_value,
  65. 'c=s' => \$bc_value,
  66. 'a=s' => \$host_arch,
  67. 'admindir=s' => \$admindir)) {
  68. usage();
  69. exit(2);
  70. }
  71. my $controlfile = shift || 'debian/control';
  72. my $control = Dpkg::Control::Info->new($controlfile);
  73. my $fields = $control->get_source();
  74. my $facts = parse_status("$admindir/status");
  75. unless (defined($bd_value) or defined($bc_value)) {
  76. my @bd_list = ('build-essential:native', $fields->{'Build-Depends'});
  77. push @bd_list, $fields->{'Build-Depends-Arch'} if not $ignore_bd_arch;
  78. push @bd_list, $fields->{'Build-Depends-Indep'} if not $ignore_bd_indep;
  79. $bd_value = deps_concat(@bd_list);
  80. my @bc_list = ($fields->{'Build-Conflicts'});
  81. push @bc_list, $fields->{'Build-Conflicts-Arch'} if not $ignore_bd_arch;
  82. push @bc_list, $fields->{'Build-Conflicts-Indep'} if not $ignore_bd_indep;
  83. $bc_value = deps_concat(@bc_list);
  84. }
  85. my (@unmet, @conflicts);
  86. if ($bd_value) {
  87. push @unmet, build_depends('Build-Depends/Build-Depends-Arch/Build-Depends-Indep',
  88. deps_parse($bd_value, build_dep => 1, host_arch => $host_arch,
  89. reduce_arch => 1), $facts);
  90. }
  91. if ($bc_value) {
  92. push @conflicts, build_conflicts('Build-Conflicts/Build-Conflicts-Arch/Build-Conflicts-Indep',
  93. deps_parse($bc_value, build_dep => 1, host_arch => $host_arch,
  94. reduce_arch => 1, union => 1), $facts);
  95. }
  96. if (@unmet) {
  97. printf STDERR _g('%s: Unmet build dependencies: '), $progname;
  98. print STDERR join(' ', map { $_->output() } @unmet), "\n";
  99. }
  100. if (@conflicts) {
  101. printf STDERR _g('%s: Build conflicts: '), $progname;
  102. print STDERR join(' ', map { $_->output() } @conflicts), "\n";
  103. }
  104. exit 1 if @unmet || @conflicts;
  105. # Silly little status file parser that returns a Dpkg::Deps::KnownFacts
  106. sub parse_status {
  107. my $status = shift;
  108. my $facts = Dpkg::Deps::KnownFacts->new();
  109. local $/ = '';
  110. open(my $status_fh, '<', $status) ||
  111. syserr(_g('cannot open %s'), $status);
  112. while (<$status_fh>) {
  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_fh;
  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. }