Packaging with Debhelper

[Important]

Requirements: The requirements from the section called “Packaging From Scratch” plus debhelper and dh-make

As a packager, you will rarely create packages from scratch as we have done in the previous section. As you can imagine, many of the tasks and information in the rules file, for instance, are common to packages. To make packaging easier and more efficient, you can use debhelper to help with these tasks. Debhelper is a set of Perl scripts (prefixed with dh_) that automate the process of package-building. With these scripts, building a Debian package becomes quite simple.

In this example, we will again build the GNU Hello package, but this time we will be comparing our work to the Ubuntu hello-debhelper package. Again, create a directory where you will be working:

mkdir ~/hello-debhelper
cd ~/hello-debhelper
wget http://ftp.gnu.org/gnu/hello/hello-2.1.1.tar.gz
mkdir ubuntu
cd ubuntu

Then, get the Ubuntu source package:

apt-get source hello-debhelper
cd ..

Like the previous example, the first thing we need to do is unpack the original (upstream) tarball.

tar -xzvf hello-2.1.1.tar.gz

Instead of copying the upstream tarball to hello_2.1.1.orig.tar.gz as we did in the previous example, we will let dh_make do the work for us. The only thing you have to do is rename the source folder so it is in the form of <packagename>-<version> where packagename is lowercase. In this case, just untarring the tarball produces a correctly named source directory so we can move into it:

cd hello-2.1.1

To create the initial "debianization" of the source we will use dh_make.

dh_make -e your.maintainer@address -f ../hello-2.1.1.tar.gz
		

dh_make will then ask you a series of questions:

Type of package: single binary, multiple binary, library, kernel module or cdbs?
[s/m/l/k/b] s
Maintainer name : Captain Packager
Email-Address   : [email protected]
Date            : Thu,  6 Apr 2006 10:07:19 -0700
Package Name    : hello
Version         : 2.1.1
License         : blank
Type of Package : Single
Hit <enter> to confirm: Enter
[Caution]

Only run dh_make -e once. If you run it again after you do it the first time, it will not work properly. If you want to change it or made a mistake, remove the source directory and untar the upstream tarball afresh. Then you can migrate into the source directory and try again.

Running dh_make -e does two things:

  1. Creates the hello_2.1.1.orig.tar.gz file in the parent directory,

  2. Creates the basic files needed in debian/ and many template files (.ex) that may be needed.

The Hello program is not very complicated, and as we have seen in the section called “Packaging From Scratch”, packaging it does not require much more than the basic files. Therefore, let us remove the .ex files:

cd debian
rm *.ex *.EX

For hello, you will also not need README.Debian (README file for specific Debian issues, not the program's README), dirs (used by dh_installdirs to create needed directories), docs (used by dh_installdocs to install program documentation), or info (used by dh_installinfo to install the info file) files into the debian directory. For more information on these files, see the section called “dh_make example files”.

At this point, you should have only changelog, compat, control, copyright, and rules files in the debian directory. From the section called “Packaging From Scratch”, the only file that is new is compat, which is a file that contains the debhelper version (in this case 4) that is used.

You will need to adjust the changelogslightly in this case to reflect that this package is named hello-debhelper rather than just hello:

hello-debhelper (2.1.1-1) dapper; urgency=low

  * Initial release

  -- Captain Packager <[email protected]>  Thu,  6 Apr 2006 10:07:19 -0700

By using debhelper, the only things we need to change in control are the name (substituting hello for hello-debhelper) and adding debhelper (>= 4.0.0) to the Build-Depends field for the source package. The Ubuntu package for hello-debhelper looks like:

Source: hello-debhelper
Section: devel
Priority: extra
Maintainer: Capitan Packager <[email protected]>
Standards-Version: 3.6.1
Build-Depends: debhelper (>= 4)

Package: hello-debhelper
Architecture: any
Depends: ${shlibs:Depends}
Conflicts: hello
Provides: hello
Replaces: hello
Description: The classic greeting, and a good example
 The GNU hello program produces a familiar, friendly greeting.  It
 allows non-programmers to use a classic computer science tool which
 would otherwise be unavailable to them.
 .
 Seriously, though: this is an example of how to do a Debian package.
 It is the Debian version of the GNU Project's `hello world' program
 (which is itself an example for the GNU Project).
 .
 This is the same as the hello package, except it uses debhelper to
 make the deb.  Please see debhelper as to what it is.
		

We can copy the copyright file and the postinst and prerm scripts from the Ubuntu hello-debhelper package, as they have not changed since the section called “Packaging From Scratch”. We will also copy the rules file so we can inspect it.

cp ../../ubuntu/hello-debhelper-2.1.1/debian/copyright .
cp ../../ubuntu/hello-debhelper-2.1.1/debian/postinst .
cp ../../ubuntu/hello-debhelper-2.1.1/debian/prerm .
cp ../../ubuntu/hello-debhelper-2.1.1/debian/rules.

The last file we need to look at is rules, where the power of debhelper scripts can be seen. The debhelper version of rules is somewhat smaller (54 lines as opposed to 72 lines in the version from the section called “rules”).

The debhelper version looks like:

 #!/usr/bin/make -f

package = hello-debhelper

CC = gcc
CFLAGS = -g -Wall

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O2
endif

#export DH_VERBOSE=1

clean:
        dh_testdir
        dh_clean
        rm -f build
        -$(MAKE) -i distclean

install: build
        dh_clean
        dh_installdirs
        $(MAKE) prefix=$(CURDIR)/debian/$(package)/usr \
                mandir=$(CURDIR)/debian/$(package)/usr/share/man \
                infodir=$(CURDIR)/debian/$(package)/usr/share/info \
                install

build:
        ./configure --prefix=/usr
        $(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)"
        touch build

binary-indep: install
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch: install
        dh_testdir -a
        dh_testroot -a
        dh_installdocs -a NEWS
        dh_installchangelogs -a ChangeLog
        dh_strip -a
        dh_compress -a
        dh_fixperms -a
        dh_installdeb -a
        dh_shlibdeps -a
        dh_gencontrol -a
        dh_md5sums -a
        dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean checkroot
			

Notice that tasks like testing if you are in the right directory (dh_testdir), making sure you are building the package with root privileges (dh_testroot), installing documentation (dh_installdocs and dh_installchangelogs), and cleaning up after the build (dh_clean) are handled automatically. Many packages much more complicated than hello have rules files no bigger because the debhelper scripts handle most of the tasks. For a complete list of debhelper scripts, please see the section called “List of debhelper scripts”. They are also well documented in their respective man pages. It is a useful exercise to read the man page (they are well written and not lengthy) for each helper script used in the above rules file.


Building the Source Package

Now that we have gone through the files in the debian directory for hello-debhelper, we can build the source (and binary) packages. First, let us move back into the source directory:

cd ..

Now we build the source package using debuild, a wrapper script for dpkg-buildpackage:

debuild -S

the binary package, using pbuilder:

sudo pbuilder build ../*.dsc

and finally check the source package for common mistakes using lintian:

cd ..
lintian -i *.dsc