dpkg-checkbuilddeps.pl 6.9 KB

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