Code

CMakeDebHelper: Using debhelper with CMake/CPack

Posted on

Making CMake/CPack and debhelper play well together.

CMake/CPack is great for quickly packaging your stuff for Debian.

However for instance, you may have programmed a daemon and want to let debhelper help you with update-rc.d-calls in your postinst/postrm-scripts. Also, you may want to easily package cron.d-rules. There’s a debhelper for everything.

CPack does not let you use them without further ado, as it’s built to not rely on anything else.

Here’s my way of making CPack and debhelper really good friends: CMakeDebHelper.

Arguably, it would be more like the “Debian-way” to actually use Debian’s tools for packaging. However, this basically assumes that you’re in the role of a package-maintainer, which may not always be the case.

In my case, I’ve written software that is deployed to clients as a Debian-package and I prefer my build-system (CMake) to be in control of building and packaging.

Since you’ve found this post, you probably already know about the benefits of debhelpers. For those who don’t, here’s an example:

When packaging for Debian, you usually type up scripts to execute after installation (“postinst”) or after removal (“postrm”), among others. A package containing a daemon usually includes stuff for managing the “rc.d”-links through calling “update-rc.d”.

No need to make up that code yourself, the debhelper “dh_installinit” can add that code for you! Look what it generates for example:

# Automatically added by dh_installinit
if [ -x "/etc/init.d/cmakedh" ]; then
        update-rc.d cmakedh defaults >/dev/null
        invoke-rc.d cmakedh start || exit $?
fi
# End automatically added section

It also generates the script to run before removal (“prerm”) itself:

#!/bin/sh
set -e
# Automatically added by dh_installinit
if [ -x "/etc/init.d/cmakedh" ]; then
        invoke-rc.d cmakedh stop || exit $?
fi
# End automatically added section

If you’re anything like me, you’ll really want to leverage functionality like that!

There’s much more stuff, have a look at the Debian New Maintainers’ Guide for more information.

So, with my CMakeDebHelper you’ll easily go from such an input:

.
├── CMakeLists.txt
├── CPackConfig.cmake
├── cmake
│   ├── CMakeDebHelper.cmake
│   └── CMakeDebHelperInstall.cmake
├── debian
│   ├── CMakeLists.txt
│   ├── cmakedh.cron.d.in
│   ├── cmakedh.init.in
│   ├── cmakedh.postinst.in
│   └── cmakedh.postrm.in
└── src
    ├── CMakeLists.txt
    └── main.c

to a Debian-package with these contents:

new debian package, version 2.0.
 size 3690 bytes: control archive=850 bytes.
      40 bytes,     2 lines      conffiles            
     201 bytes,    10 lines      control              
     154 bytes,     3 lines      md5sums              
     458 bytes,    30 lines   *  postinst             #!/bin/sh
     425 bytes,    29 lines   *  postrm               #!/bin/sh
     169 bytes,     7 lines   *  prerm                #!/bin/sh
 Package: cmakedh
 Version: 1.0.0
 Section: devel
 Priority: optional
 Architecture: i386
 Depends: libc6 (>= 2.0), make, rsync, adduser, liblog4cpp5, zlib1g, libboost-system1.49.0, libboost-regex1.49.0, libboost-filesystem1.49.0
 Installed-Size: 6
 Maintainer: seb <seb@asdf>
 Description: Package summary

drwxr-xr-x root/root         0 2013-11-30 16:01 ./usr/
drwxr-xr-x root/root         0 2013-11-30 16:01 ./usr/bin/
-rwxr-xr-x root/root      5360 2013-11-30 16:01 ./usr/bin/runme
drwxr-xr-x root/root         0 2013-11-30 16:01 ./etc/
drwxr-xr-x root/root         0 2013-11-30 16:01 ./etc/init.d/
-rwxr-xr-x root/root        35 2013-11-30 16:01 ./etc/init.d/cmakedh
drwxr-xr-x root/root         0 2013-11-30 16:01 ./etc/cron.d/
-rw-r--r-- root/root        24 2013-11-30 16:01 ./etc/cron.d/cmakedh

The ham and a working example can be found on GitHub, improvements welcome!