dpkg-checkbuilddeps.pl 7.3 KB

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