dpkg-checkbuilddeps.pl 6.7 KB

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