dpkg-checkbuilddeps.pl 7.0 KB

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