dpkg-checkbuilddeps.pl 7.1 KB

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