How debian/rules Works in Debian Software Compilation
The debian/rules file is the central automation script
responsible for driving the compilation, testing, and packaging of
software within Debian and Debian-based operating systems. Serving as an
executable Makefile, it dictates the exact instructions that
package-building tools follow to transform raw upstream source code into
binary .deb packages. This article explains how the file is
structured, how it executes standard build targets, and how modern
package maintainers utilize helper tools to streamline the build
process.
Executable Makefile Architecture
At its core, debian/rules is an executable script
written using GNU Make syntax. It begins with the standard shebang:
#!/usr/bin/make -fBecause it is marked as executable
(chmod +x debian/rules), build utilities like
dpkg-buildpackage can invoke it directly. The Debian Policy
Manual requires debian/rules to support a set of mandatory
standard targets:
clean: Reverses any compilation and configuration steps, returning the source directory to an unmodified, pristine state.build: Compiles the source code and documentation. This is often split intobuild-arch(architecture-dependent binaries) andbuild-indep(architecture-independent files, such as documentation or data).binary: Gathers compiled files, places them into package staging directories, runs post-processing commands (like stripping debug symbols or compressing man pages), and produces the final.debpackages. This is similarly split intobinary-archandbinary-indep.
Modern Execution with
Debhelper (dh)
In modern Debian packaging, maintainers rarely write raw Make recipes
for standard compilation steps. Instead, they rely on Debhelper's
dh sequencer. A typical modern debian/rules
file often consists of just three lines:
#!/usr/bin/make -f
%:
dh $@The wildcard rule (%) passes every target requested by
the build environment to the dh sequencer. When
dpkg-buildpackage calls debian/rules build,
the sequencer detects the upstream build system (such as CMake, Meson,
Autotools, or Python setuptools) and automatically executes the
appropriate configure, build, and test commands in the correct
order.
Customization via Overrides
When custom compilation steps, compiler flags, or non-standard
configurations are required, the file uses override targets. Maintainers
override specific Debhelper phases by creating rules prefixed with
override_:
#!/usr/bin/make -f
%:
dh $@
override_dh_auto_configure:
dh_auto_configure -- -DENABLE_CUSTOM_FEATURE=ON
override_dh_auto_test:
# Skip test suite during compilationIn this workflow, dh will call the custom
override_dh_auto_configure recipe instead of the default
configuration command, passing the designated build flags directly to
upstream tools like CMake.
Role in the Build Lifecycle
During an end-to-end package build, higher-level build tools interact
with debian/rules in a defined sequence:
- Preparation: The environment sets standard build
variables (like
CFLAGSandLDFLAGSviadpkg-buildflags). - Cleaning: The
cleantarget executes to ensure a consistent starting state. - Compilation: The
buildtarget runs upstream compilation steps inside the source directory. - Installation: Binaries are installed not into the
host root directory, but into a temporary staging tree (typically
debian/tmpordebian/<package-name>). - Packaging: The
binarytarget computes runtime shared-library dependencies, generates control files, and invokesdpkg-debto generate the final binary packages.