TExcelTAD
Home › Modern CMake (with CTest & CPack) › Components & Other Generators

Components & Other Generators

Linux distributions split a library into a runtime package (libgreet1: the .so) and a development package (libgreet-dev: headers, the .so symlink, CMake config files). Users who only run programs install the first; developers install both. CPack does the same split with components.

Tag install rules with components

cmake
install(TARGETS greet EXPORT GreetTargets
  LIBRARY  COMPONENT runtime NAMELINK_COMPONENT dev   # libgreet.so.1* -> runtime, libgreet.so -> dev
  ARCHIVE  COMPONENT dev
  FILE_SET HEADERS COMPONENT dev)
install(EXPORT GreetTargets ... COMPONENT dev)

NAMELINK_COMPONENT is the detail people miss: the unversioned libgreet.so symlink is only needed to link against the library, so it belongs to the dev package, exactly as Debian does it.

One .deb per component

bash · try it yourself
mkdir -p greet/include/greet && cd greet
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(greet VERSION 1.4.0 DESCRIPTION "Greeting library" LANGUAGES CXX)
include(GNUInstallDirs)
add_library(greet SHARED greet.cpp)
set_target_properties(greet PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION 1)
target_sources(greet PUBLIC FILE_SET HEADERS BASE_DIRS include FILES include/greet/greet.hpp)
add_executable(greet-cli main.cpp)
target_link_libraries(greet-cli PRIVATE greet)
set_target_properties(greet-cli PROPERTIES INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")

install(TARGETS greet
  LIBRARY COMPONENT runtime NAMELINK_COMPONENT dev
  FILE_SET HEADERS COMPONENT dev)
install(TARGETS greet-cli RUNTIME COMPONENT tools)

set(CPACK_GENERATOR DEB)
set(CPACK_PACKAGE_CONTACT "Ada Lovelace <ada@example.com>")
set(CPACK_DEB_COMPONENT_INSTALL ON)               # one package per component
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_RUNTIME_PACKAGE_NAME "libgreet1")
set(CPACK_DEBIAN_DEV_PACKAGE_NAME "libgreet-dev")
set(CPACK_DEBIAN_TOOLS_PACKAGE_NAME "greet-tools")
set(CPACK_COMPONENT_DEV_DEPENDS runtime)          # becomes Depends: libgreet1 (= 1.4.0)
set(CPACK_COMPONENT_TOOLS_DEPENDS runtime)
set(CPACK_DEBIAN_ENABLE_COMPONENT_DEPENDS ON)
include(CPack)
CM
echo '#pragma once
const char* greet();' > include/greet/greet.hpp
echo '#include <greet/greet.hpp>
const char* greet() { return "hi"; }' > greet.cpp
echo '#include <cstdio>
#include <greet/greet.hpp>
int main() { std::puts(greet()); }' > main.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_BUILD_TYPE=Release > /dev/null
cmake --build build > /dev/null
cd build && cpack | grep package:
for deb in *.deb; do
  echo "=== $deb: $(dpkg-deb --field $deb Package) depends on [$(dpkg-deb --field $deb Depends)]"
  dpkg-deb --contents $deb | awk '$1 !~ /^d/ {print "   ", $6, $7, $8}'
done
CPack: - package: /home/learner/greet/build/libgreet-dev_1.4.0_amd64.deb generated.
CPack: - package: /home/learner/greet/build/libgreet1_1.4.0_amd64.deb generated.
CPack: - package: /home/learner/greet/build/greet-tools_1.4.0_amd64.deb generated.
=== greet-tools_1.4.0_amd64.deb: greet-tools depends on [libgreet1 (= 1.4.0)]
    ./usr/bin/greet-cli
=== libgreet-dev_1.4.0_amd64.deb: libgreet-dev depends on [libgreet1 (= 1.4.0)]
    ./usr/include/greet/greet.hpp
    ./usr/lib/libgreet.so -> libgreet.so.1
=== libgreet1_1.4.0_amd64.deb: libgreet1 depends on []
    ./usr/lib/libgreet.so.1 -> libgreet.so.1.4.0
    ./usr/lib/libgreet.so.1.4.0

Three packages, split the Debian way: the versioned library in libgreet1, the header and the libgreet.so link in libgreet-dev, the program in greet-tools, with the dependencies between them derived from CPACK_COMPONENT_<NAME>_DEPENDS.

The same component tags work for cmake --install build --component dev and for the archive generators (CPACK_ARCHIVE_COMPONENT_INSTALL ON gives one .tar.gz per component).

Other generators

CPack has many more generators. The course image has the Linux ones (dpkg-deb and dpkg-shlibdeps for DEB, rpmbuild for RPM, plus the archives); the Windows and macOS installers need their own platforms and tools, so those are described, not run:

Generator Produces Needs Notes
TGZ, TXZ, TZST, ZIP, 7Z archives nothing (built into CPack) portable; good for "download and unpack" releases and SDKs
DEB .deb nothing (dpkg-shlibdeps optional) Debian, Ubuntu
RPM .rpm rpmbuild Fedora, RHEL, SUSE. Variables CPACK_RPM_PACKAGE_LICENSE, CPACK_RPM_PACKAGE_REQUIRES, CPACK_RPM_PACKAGE_AUTOREQ; components with CPACK_RPM_COMPONENT_INSTALL
NSIS / NSIS64 Windows .exe installer makensis Start-menu entries, PATH changes, uninstaller; CPACK_NSIS_* variables
WIX Windows .msi WiX Toolset (v3, or v4+ with CPACK_WIX_VERSION 4) MSI for corporate deployment; needs a stable CPACK_WIX_UPGRADE_GUID
INNOSETUP Windows .exe Inno Setup added in CMake 3.27
DragNDrop macOS .dmg macOS (hdiutil) drag-the-app-to-Applications image
productbuild macOS .pkg macOS signed installer packages
IFW Qt Installer Framework Qt IFW cross-platform GUI installers
External a JSON description your own script hand the staged files to any packaging tool

An RPM from the same install rules

Switching to RPM is a change of generator plus a few CPACK_RPM_* variables. CPack writes a .spec file and runs rpmbuild on the staged files, so the package can be built on Debian or Ubuntu too (here rpm 4.18). rpm -qp… queries a package file without installing it: -qpi the header, -qpl the file list, -qpR the requirements:

bash · try it yourself
mkdir -p tally && cd tally
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(tally VERSION 0.3.1 DESCRIPTION "Counts words" LANGUAGES CXX)
add_executable(tally main.cpp)
install(TARGETS tally)
set(CPACK_GENERATOR "RPM")
set(CPACK_PACKAGE_CONTACT "Ada Lovelace <ada@example.com>")
set(CPACK_RPM_PACKAGE_LICENSE "MIT")
set(CPACK_RPM_PACKAGE_GROUP "Applications/Text")
set(CPACK_RPM_PACKAGE_RELEASE 1)
set(CPACK_RPM_FILE_NAME RPM-DEFAULT)          # tally-0.3.1-1.x86_64.rpm
include(CPack)
CM
echo 'int main() { return 0; }' > main.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_BUILD_TYPE=Release > /dev/null
cmake --build build > /dev/null
cd build && cpack 2>&1 | grep -E 'Error|Warning|package:'
rpm -qpi tally-0.3.1-1.x86_64.rpm 2>&1 | grep -E '^(Name|Version|Release|Architecture|License|Group|Summary) '
echo "--- files"
rpm -qpl tally-0.3.1-1.x86_64.rpm
echo "--- requires"
rpm -qpR tally-0.3.1-1.x86_64.rpm
CPack: - package: /home/learner/tally/build/tally-0.3.1-1.x86_64.rpm generated.
Name        : tally
Version     : 0.3.1
Release     : 1
Group       : Applications/Text
License     : MIT
Summary     : Counts words
--- files
/usr/bin/tally
/usr/lib/.build-id
/usr/lib/.build-id/cc
/usr/lib/.build-id/cc/23932334f3fe4bc9b00c42142892b9880558e0
--- requires
libc.so.6()(64bit)
libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6(GLIBC_2.34)(64bit)
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rtld(GNU_HASH)

Three differences from the DEB:

  • Requirements are automatic. rpmbuild scans the binaries (CPACK_RPM_PACKAGE_AUTOREQ, on by default), so Requires: lists the sonames and symbol versions the program needs (libc.so.6(GLIBC_2.34)) rather than package names; the package manager on the target finds which package provides them. Add extra ones with CPACK_RPM_PACKAGE_REQUIRES.
  • rpmbuild adds /usr/lib/.build-id/… links named after the binary's GNU build ID, which debuggers use to find matching debug symbols.
  • The file name follows the RPM convention name-version-release.arch.rpm (RPM-DEFAULT); CPACK_RPM_COMPONENT_INSTALL ON splits components into several RPMs, like the DEB example above.

A common pattern is to build each OS's native format on that OS in CI (DEB and RPM in Linux containers, NSIS or WiX on a Windows runner, DragNDrop on macOS) from the same install rules, plus a TGZ/ZIP everywhere.

Test yourself

Which component should the unversioned libgreet.so symlink belong to?

  1. runtime
  2. dev, via NAMELINK_COMPONENT
  3. tools
  4. none

What does CPACK_DEB_COMPONENT_INSTALL ON do?

  1. Installs the .deb
  2. Produces one .deb per install component instead of one for everything
  3. Enables dpkg-shlibdeps
  4. Adds a postinst script

Which generator needs rpmbuild installed?

  1. DEB
  2. TGZ
  3. RPM
  4. ZIP