The code for this recipe is available at https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-11/recipe-01. The recipe is valid with CMake version 3.6 (and higher) and has been tested on GNU/Linux, macOS, and Windows.
If your code is open source, users will expect to be able to download the sources for your project and build by themselves using your perfectly tailored CMake scripts. Of course, the packaging operation could be done with a script, but CPack offers a more compact and portable alternative. This recipe will guide you through the creation of a number of packaging alternatives:
- Source code archives: You can use these formats to ship the source code directly as a compressed archive in your favorite format. Your users will not have to worry about your specific version control system.
- Binary archives: Use these to package the freshly built targets into compressed archives in your favorite format. These can be extremely useful, but might not be robust enough to distribute libraries and executables.
- Platform-native binary installers: CPack is able to generate binary installers in many different formats, so you can target the distribution of your software to many different platforms. In particular, we will show how to generate installers:
- In .deb format for Debian-based GNU/Linux distributions: https://manpages.debian.org/unstable/dpkg-dev/deb.5.en.html
- In .rpm format for Red Hat-based GNU/Linux distributions: http://rpm.org/
- In .dmg format for macOS Bundles: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html
- In NSIS format for Windows: http://nsis.sourceforge.net/Main_Page