Qt Creator: CMake package-manager auto-setup

As you might know, using 3rd party software in CMake code is done via packages, by using the find_package command.

find_package does what the name says: it finds a package. If the package doesn’t exist, it will set the <PackageName>_FOUND variable to FALSE, and if the package was REQUIRED the the CMake configuration will error out.

This is where a package manager comes into play. The setup of the package manager could be done in CMake code, by hard coding specific code, or by documenting how the environment needs to be setup before configuring the CMake project.

Package manager auto-setup

CMake, starting with version 3.15, allows injecting custom code without modifying the project CMake source code via CMAKE_PROJECT_INCLUDE_BEFORE.

Qt Creator 4.15 will insert in the initial cmake parameters the line:

-DCMAKE_PROJECT_INCLUDE_BEFORE:PATH=%{IDE:ResourcePath}/package-manager/auto-setup.cmake

auto-setup.cmake is a cmake-script shipped with Qt Creator that will do the auto-setup of the following package managers: conan, vcpkg, custom CMake package manager e.g. cpp-pm/hunter.

This feature can be turned off in Qt Creator’s Options -> Build & Run -> CMake and then uncheck “Package manager auto-setup”.

Hello World project

In order to test this feature, I’m going to present a Hello World project using the fmtlib/fmt library.

CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.15)

project(hello-world)

add_executable(hello-world main.cpp)

find_package(fmt REQUIRED)
target_link_libraries(hello-world PRIVATE fmt::fmt)

main.cpp looks like this:

#include <fmt/core.h>

int main()
{
    fmt::print("Привет мир\n");
    return 0;
}

And here is how Qt Creator looked after building and running the application:

qtcreator-4.15-hello-world

Qt Creator 4.15 gained the ability to specify the “Text codec for tools” which needs to be set either to UTF-8 or IBM866 in order to get the Привет мир message displayed in the output pane.

Conan

Using conan for getting the fmt library into the project is as easy as creating this conanfile.txt:

[requires]
fmt/7.1.3
[generators]
cmake_find_package

The cmake_find_package conan generator is needed for the fmt::fmt CMake target.

Getting conan on your system is as easy as running:

pip3 install conan

This assumes that you have python3 installed on your system.

Qt Creator’s auto-setup.cmake takes care of running conan install for the project and for making the experience as painless as possible. This has been made possible by the usage of the conan-cmake, which is also shipped with Qt Creator.

If you don’t want the conan auto-setup, you can set the CMake variable QT_CREATOR_SKIP_CONAN_SETUP to TRUE.

vcpkg

Using vcpkg for getting the fmt library into the project is as easy as creating this vcpkg.json file:

{
  "name": "hello-world",
  "version-string": "0.0.1",
  "dependencies": [
    "fmt"
  ]
}

Setting up vcpkg for usage in Qt Creator is as easy as running:

> git clone https://github.com/microsoft/vcpkg
> .\vcpkg\bootstrap-vcpkg.bat

The above commands will setup the directory structure for vcpkg and download a prebuilt vcpkg executable.

Qt Creator’s auto-setup.cmake needs to have vcpkg.exe in the PATH environment variable.

With Qt Creator 4.15, you can edit the PATH environment in the Environment settings like this:

PATH=${PATH};c:\projects\vcpkg

auto-setup.cmake will set the CMAKE_TOOLCHAIN_FILE to the vcpkg toolchain file and before that set the appropriate value for VCPKG_TARGET_TRIPLET.

If you don’t want the vcpkg auto-setup, you can set the CMake variable QT_CREATOR_SKIP_VCPKG_SETUP to TRUE.

Custom CMake package manager

Qt Creator’s auto-setup.cmake will first look in the source directory after a file named QtCreatorPackageManager.cmake, if found then it’s included.

For the CMake only package manager cpp-pm/hunter we can have a QtCreatorPackageManager.cmake which looks like this:

set(HUNTER_PACKAGES fmt)

include(FetchContent)
FetchContent_Declare(cpp-pm-setup GIT_REPOSITORY https://github.com/cpp-pm/gate)
FetchContent_MakeAvailable(cpp-pm-setup)

set(QT_CREATOR_SKIP_PACKAGE_MANAGER_SETUP ON)

QT_CREATOR_SKIP_PACKAGE_MANAGER_SETUP will tell to auto-setup.cmake to exit early, skipping conan and vcpkg autodetection.


Blog Topics:

Comments