CMakeToolchain: Inject arbitrary CMake variables into dependencies

You can find the sources to recreate this project in the examples2 repository in GitHub:

  1. $ git clone https://github.com/conan-io/examples2.git
  2. $ cd examples2/examples/tools/cmake/cmake_toolchain/user_toolchain_profile

In the general case, Conan package recipes provide the necessary abstractions via settings, confs, and options to control different aspects of the build. Many recipes define options to activate or deactivate features, optional dependencies, or binary characteristics. Configurations like tools.build:cxxflags can be used to inject arbitrary C++ compile flags.

In some exceptional cases, it might be desired to inject CMake variables directly into dependencies doing CMake builds. This is possible when these dependencies use the CMakeToolchain integration. Let’s check it in this simple example.

If we have the following package recipe, with a simple conanfile.py and a CMakeLists.txt printing a variable:

conanfile.py

  1. from conan import ConanFile
  2. from conan.tools.cmake import CMake
  3. class AppConan(ConanFile):
  4. name = "foo"
  5. version = "1.0"
  6. settings = "os", "compiler", "build_type", "arch"
  7. exports_sources = "CMakeLists.txt"
  8. generators = "CMakeToolchain"
  9. def build(self):
  10. cmake = CMake(self)
  11. cmake.configure()
  12. cmake.build()

CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.15)
  2. project(foo LANGUAGES NONE)
  3. message(STATUS "MYVAR1 ${MY_USER_VAR1}!!")

We can define a profile file and a myvars.cmake file (both in the same folder) like the following:

myprofile

  1. include(default)
  2. [conf]
  3. tools.cmake.cmaketoolchain:user_toolchain+={{profile_dir}}/myvars.cmake

Note the {{profile_dir}} is a jinja template expression that evaluates to the current profile folder, allowing to compute the necessary path to myvars.cmake file. The tools.cmake.cmaketoolchain:user_toolchain is a list of files to inject to the generated conan_toolchain.cmake, so the += operator is used to append to it.

The myvars.cmake can define as many variables as we want:

myvars.cmake

  1. set(MY_USER_VAR1 "MYVALUE1")

Applying this profile, we can see that the package CMake build effectively uses the variable provided in the external myvars.cmake file:

  1. $ conan create . -pr=myprofile
  2. ...
  3. -- MY_USER_VAR1 MYVALUE1

Note that using user_toolchain while defining values for confs like tools.cmake.cmaketoolchain:system_name is supported.

The tools.cmake.cmaketoolchain:user_toolchain conf value might also be passed in the command line -c argument, but the location of the myvars.cmake needs to be absolute to be found, as jinja replacement doesn’t happen in the command line.