#!/usr/bin/perl =head1 NAME dh_ifexists - Add dependencies iff they are satisfiable at build time =cut use strict; use warnings; use Debian::Debhelper::Dh_Lib; use Dpkg::Deps; use Dpkg::Version; use AptPkg::Cache; =head1 SYNOPSIS B [S>] =head1 DESCRIPTION dh_ifexists will examine Depends-If-Exists lines in debian/control, check each listed dependency for satisfiability (according to apt in the build environment), and add it to ${misc:Depends} if so. It also does the same for other -If-Exists lines (such as Recommends-If-Exists), adding them to ${misc:Recommends}, etc. Note that these substvars are much less common than ${misc:Depends}, and so your control file almost certainly doesn't already have them listed. =cut init(); my $cache = AptPkg::Cache->new; my $package = ""; open (CONTROL, 'debian/control') || error("cannot read debian/control: $!\n"); while () { chomp; s/\s+$//; if (/^Package:\s*(.*)/) { $package = $1; } if ($package && /^(.*?)-If-Exists: (.*)/) { addsubstvar($package, "misc:$1", satisfiable($2)); } if (!$_ or eof) { $package = ""; } } sub satisfiable { my $fulldeps = deps_parse(shift, use_arch => 1, reduce_arch => 1, union => 1); my $deps = Dpkg::Deps::Union->new; foreach my $dep ($fulldeps->get_deps()) { my $pkg = $cache->get($dep->{package}); next unless $pkg; if ($dep->{relation}) { foreach my $candidate (@{$pkg->{VersionList}}) { if (version_compare_relation($candidate->{VerStr}, $dep->{relation}, $dep->{version})) { $deps->add($dep); last; } } } else { $deps->add($dep); } } return $deps; } =head1 BUGS We don't handle apt_preferences / policy, although we do handle versioned dependencies. Hopefully you use clean build chroots, and hopefully you have no need for apt preferences in those. (This isn't hard to fix, just annoying.) We should probably have some way to specify that you want to want to add a group of dependencies if it's all satisfiable. The easy example is Depends-If-Exists: foo (>= 1.0), foo (<< 2.0), where it would be useless to keep one but not the other; we'd want both or neither. =head1 SEE ALSO L =head1 AUTHOR Geoffrey Thomas =cut