Heterogeneous Package Search and Retrieval in CMake

Finding and Retrieving third party sources, tools and binaries is currently a hot subject for C++. There are quite a few solutions (biicode, cpm, hunter, …) Also there is github, bitbucket, source balls etc. Further there are many package managers for many different platforms which also can contain packages (apt-get, chocolatey, nuget, npm, pip, ….). So everything is very decentralized and heterogeneous.

On the other hand there is CMake the standard when it comes to creating platform independent projects for various build and developer environments. (of course it is not the only build tool but it is the standard for platform independent C/C++ projects) Most notably it generates MSBuild for Visual Studio and MAKE output which are the most commonly used build systems. CMake is as platform independent as it comes.

For me the need arose to get multiple packages for a project I am working on. My list of dependencies were partially local and partially remote (local project a, local project b, Eigen3, tinyxml2, boost, etc…) I wanted to write my project configuration files in a way that I do not have to copy all the third party tools and sources into my repository but also I did not want the client to be responsible for retrieving them either.

The first part of solving this problem is to be able to find and retrieve packages with platform independent CMake code from all kinds of package sources and that is what this blog post is about. The second part of the solution is to be able not only to retrieve packages but also their dependencies (package management). The third part is the only part specific to C++ – Combining packages in a way that everything builds correctly without tons of testing and configuration.

So my Idea is to abstract over all package sources (current existing ones and ones which do not exist yet) and allow the CMake user to easily query for them as well as to download the data and meta data necessary to configure them.

In the rise of this task I created a cmake library (formerly known as oo-cmake and now audaciously called cmakepp) It has now reached a state which will make it a very useful tool for all users of CMake it is written in pure cmake >=2.8.12 and extends CMake’s capabilities quite considerably (see my other blog posts or the README file).

cmakepp now contains package sources for github bitbucket web archives local archives local directorys git subversion mercurial and more to come (apt get, nuget, npm, are planned)

These can be used to simply search and download packages.

After all of this text I’m sure you want to see how to use it. So I’ll show you a simple Example:

cmake_minimum_required(VERSION 2.8.12)
## CMakeLists.txt for a simple project 
set(current_dir "${CMAKE_CURRENT_SOURCE_DIR}")
## get cmakepp
if(NOT EXISTS "${current_dir}/cmakepp.cmake")
  file(DOWNLOAD "https://github.com/toeb/cmakepp/releases/download/v0.0.3/cmakepp.cmake" "${current_dir}/cmakepp.cmake")


if(NOT EXISTS ${current_dir}/dependencies/eigen3)
 message("installing Eigen3 from bitbucket")
 pull_package(eigen/eigen?tag=3.1.0 dependencies/eigen3)
 if(NOT package_handle)
  message(FATAL_ERROR "could not pull Eigen3")
 ## print the package information returned

## from here on everything can be a normal cmakelists file


fwrite("main.cpp" "
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;
add_executable(myexe "main.cpp")

After you write and configure this CMakeLists.txt you are able to work with you project as before. Of course the sample is overly simple – because I do not show you how this works with non-header-only libraries and installing and multiple targets,… But it is just to give you an idea of how things work.

The only prerequisites are cmake >= 2.8.12 and a build system e.g. MSVC or GCC

To get this to work copy paste the code into a new directory in a file called CMakeLists.txt then type:

> mkdir build
> cd build
build> cmake ..
build> cmake --build .
build> <code to run executable e.g. ./Debug/myexe.exe under windows>

This might seem a bit of overkill – however when you install cmakepp you are not limited to using it in your CMakeLists.txt you can also invoke it through the commandline. Consider the following console log which work on your console of choice like bash powershell cmd.

The following lists all packages available which match the uri toeb

PS> cmakepp query_package toeb

The following returns all available package information for a specific package uri.

PS> cmakepp resolve_package toeb/cmakepp
  "description":"a cmake enhancement suite",
  "author":"Tobias P. Becker"
  "description":"An Enhancement Suite for the CMake Build System",

This concludes this simple blog post – It just scrapes at the edges of what you can do and if I have piqued your interest please visit the the project’s github package https://github.com/toeb/cmakepp were I try to document everything vigorously and also try to explain my reasoning for the choices I make in the README file. Please let me know if you have any suggestions or bugs or feature requests or if you want to help 🙂 This software is free and under the MIT license.

2 comments for “Heterogeneous Package Search and Retrieval in CMake

  1. Andre Bogus
    2015/02/05 at 13:55

    Small nitpick: It should read “piqued your interest”, not peeked. 😉

    Regards, Andre

    • 2015/02/05 at 13:58

      Thanks – Maybe you could become my editor 😉

Leave a Reply

Your email address will not be published. Required fields are marked *