There are situations in which a given package needs to have different dependencies in Debian and in Ubuntu. Despite this difference it’s possible to keep a single source package that will build both variants of the package. Continue reading to discover a step-by-step explanation.
1. When is that needed?
While it is possible to have different dependencies depending on the distribution on which the package is built, it should be usually avoided when possible. This infrastructure should only be used as a last resort when there are no better alternatives.
Here are some examples of when it might be needed:
- Ubuntu has some packages that Debian does not have (or vice-versa), and the resulting package would benefit from having them installed.
- The package names differ between Ubuntu and Debian and it’s on purpose (i.e. the difference is a justified choice and not a mistake because both distributions failed to coordinate).
- The packages are built differently in both distributions, and the run time dependencies are not the same due to this. Maybe some associated patches are only applied in Ubuntu.
2. Using substitution variables in debian/control
The dependency that varies between both distributions can’t be hardcoded in debian/control
, instead you should put a substitution variable (substvar) that will be replaced at build time by dpkg-gencontrol
. You can name it ${dist:Depends}
for example:
[...] Depends: bzip2, ${shlibs:Depends}, ${misc:Depends}, ${dist:Depends} [...]
Note that you typically already have other substitution variables (${shlibs:Depends}
comes from dpkg-shlibdeps
and ${misc:Depends}
from debhelper and its dh_* scripts).
3. dpkg-gencontrol needs a -V option
The value used to replace this new variable needs to be communicated to dpkg-gencontrol
. You can use the -V option for this, the syntax would be something like this:
dpkg-gencontrol [...] -Vdist:Depends="foo (>= 2), bar"
If you use debhelper, you have to pass the option to dh_gencontrol
after two dashes (--
):
dh_gencontrol -- -Vdist:Depends="foo (>= 2), bar"
If you use CDBS, you can set the DEB_DH_GENCONTROL_ARGS_ALL make variable:
include /usr/share/cdbs/1/rules/debhelper.mk DEB_DH_GENCONTROL_ARGS_ALL = -- -Vdist:Depends="foo (>= 2), bar"
The value given to dpkg-gencontrol is static in all those examples, now let’s see how we can use give a different value depending on the distribution that we’re targetting.
4. Using dpkg-vendor in debian/rules
dpkg-vendor
is a small tool (provided by the dpkg-dev package) that parses the /etc/dpkg/origins/default
file (provided by the base-files package) to know the current distribution and its ancestry. It can be used in debian/rules
to adjust the behavior depending on the current distribution. You can check its man-page to learn about the various options supported but we’re only going to use --derives-from <vendor>
in this sample. With this option the script exits with zero if the current distribution is or derives from the indicated distribution, or with 1 otherwise.
Now combining all together, we can use dpkg-vendor to dynamically define the content of the substitution variable in debian/rules. Let’s suppose that you want ${dist:Depends}
to be “foo (>= 2)” on Ubuntu (and its derivatives) and “bar” everywhere else. Using debhelper’s 7 tiny rules file, this example could be:
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes) SUBSTVARS = -Vdist:Depends="foo (>= 2)" else SUBSTVARS = -Vdist:Depends="bar" endif %: dh $@ override_dh_gencontrol: dh_gencontrol -- $(SUBSTVARS)
If you use CDBS, it could be this:
include /usr/share/cdbs/1/rules/debhelper.mk ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes) DEB_DH_GENCONTROL_ARGS_ALL = -- -Vdist:Depends="foo (>= 2)" else DEB_DH_GENCONTROL_ARGS_ALL = -- -Vdist:Depends="bar" endif
Do you want to read more tutorials like this one? Click here to subscribe to my free newsletter, you can opt to receive future articles by email.