Build a RPM package using CMake

Some while ago I presented a way to package projects using different build systems as RPM packages. If you are using CMake for your projects you can use CPack to build RPM packages (in addition to tarballs, NSIS installers, deb packages and so on). This is a really nice option for deployment of your own projects because installation and update can be easily done by the users using familiar package management tools like zypper, yum and yast2.

Your first CPack RPM

It is really easy to add RPM using CPack to your existing project. Just set the mandatory CPack variables and include CPack below the variable definitions, usually as one of the last steps:

project (my_project)
cmake_minimum_required (VERSION 2.8)

set(VERSION "1.0.1")
<----snip your usual build instructions snip--->
set(CPACK_PACKAGE_NAME "my_project")
set(CPACK_PACKAGE_CONTACT "John Explainer")

These few lines should be enough to get you going. After that you can execute a make package command should obtain the RPM package.

Spicing up the package

RPM packages can contain much more metadata and especially package dependencies and a version changelog. Most of the stuff can be specified using CPACK variables. We sometimes prefer to use a SPEC file template to be filled and used by CPack because it then contains most of the RPM specific stuff in a familiar manner instead of polluting the CMakeLists.txt itself:

project (my_project)
<----snip your usual CMake stuff snip--->
<----snip your additional CPack variables snip--->

The variables in the RPM SPEC file will be replaced by the values provided in the CMakeLists.txt and then be used for the RPM package. It looks very similar to a standard SPEC file but you can omit the usual build instructions boiling down to something like this:

Summary:        My very cool Project
Name:           @CPACK_PACKAGE_NAME@
License:        GPL
Group:          Development/Tools/Other
Vendor:         @CPACK_PACKAGE_VENDOR@
Requires:       opencv >= 2.4

%define _rpmdir @CMAKE_CURRENT_BINARY_DIR@/_CPack_Packages/Linux/RPM
%define _rpmfilename @CPACK_PACKAGE_FILE_NAME@.rpm
%define _unpackaged_files_terminate_build 0
%define _topdir @CMAKE_CURRENT_BINARY_DIR@/_CPack_Packages/Linux/RPM

Cool project solving the problems of many colleagues.

# This is a shortcutted spec file generated by CMake RPM generator
# we skip _install step because CPack does that for us.
# We do only save CPack installed tree in _prepr
# and then restore it in build.

if [ -e $RPM_BUILD_ROOT ];


* Tue Jan 29 2013 John Explainer <> 1.0.1-3
- use correct maintainer address
* Tue Jan 29 2013 John Explainer <> 1.0.1-2
- fix something about the package
* Thu Jan 24 2013 John Explainer <> 1.0.1-1
- important bugfixes
* Fri Nov 16 2012 John Explainer <> 1.0.0-1
- first release


Integrating RPM (or other package formats) to your CMake-based build is not as hard as it seems and quite flexible. You do not need to rely on the tools provided by your OS vendor and still deliver your software in a way your users are accustomed to. This makes CPack very continuous integration (CI) friendly too!

9 thoughts on “Build a RPM package using CMake”

  1. could you offer more information about how to build a rpm package with rpmbuild after finish a project by Clion(Cmake 3.6.1)

  2. Thanks, very useful. Similar tutorial to generate SRPM (RPM with source files, which can be used to build RPMs for more architectures in COPR or similar) would be welcome.

  3. Thanks for writing this article. Starting with your “first example”, it produces error: CPACK_INSTALLED_DIRECTORIES should contain pairs of and . There is almost no documentation about this error. Any ideas?

  4. Yeah, always wonder about posts starting ‘its simple…’ because invariably they contain nothing really helpful. How about an example of a cmakelists.txt which includes the binary, a library needed and the source code (maybe more than one file an maybe even more than one directory).
    There is a LOT of documentation on the web about cpack, none of which seems to have a single useful example to use as the basis for creating my own. I can if I name each individual file manage to make a package but unlike the actual build which can take *.cpp this isnt going to work going forward when I want to create an rpm containing all the source and someone (even maybe me) decides a new source file is required. I refuse to believe no one has thought of doing this, just seems no one has thought to say how.

    1. I think there are some misconceptions here:
      1) It is true that my blog article assumes a basic knowledge of CMake and how to build your project with it. My article is about RPM packaging using CMake/CPack. Basically, CPack will package everything you “install” in your CMake build.
      2) RPMs typically only contain binaries and needed resources and NOT the source code. That’s what SOURCE RPMs are for but building them with CPack would be kind of pointless because you do not use the classic RPM directory structure, tools (like rpmbuild) and build recipes (aka spec files).

      1. Thanks, to be honest I have used rpms to install applications but havent made them before. The inclusion of source did seem a bit odd to me, I was expecting to be able to produce TWO rpms at the same time, one for the installation of the application and one for the source (though as the source is in git, is proprietary and not for sharing it still seems a bizarre thing to have been asked for). Your comment clears up my worry that perhaps I had missed out on something there, seems I hadnt :0
        It would still be very very very very nice of you to actually share a completed file with comments as you clearly know it far better than myself – and there are going to be others like me – thrown in a bit at the deep end. You want me to architect, design, code, automate tests etc etc etc I have dozens of years of experience, but rpms, no, thats new, even cmakelists.txt is a little new once beyond the basics of getting some source files to build and link

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.