dpkg-checkbuilddeps.pl 7.0 KB

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