Preparing the build

In the previous tutorial section, we added the fmt requirement to our Conan package to provide colour output to our “Hello World” C++ library. In this section, we focus on the generate() method of the recipe. The aim of this method generating all the information that could be needed while running the build step. That means things like:

  • Write files to be used in the build step, like scripts that inject environment variables, files to pass to the build system, etc.

  • Configuring the toolchain to provide extra information based on the settings and options or removing information from the toolchain that Conan generates by default and may not apply for certain cases.

We explain how to use this method for a simple example based on the previous tutorial section. We add a with_fmt option to the recipe, depending on the value we require the fmt library or not. We use the generate() method to modify the toolchain so that it passes a variable to CMake so that we can conditionally add that library and use fmt or not in the source code.

Please, first clone the sources to recreate this project. You can find them in the examples2 repository on GitHub:

  1. $ git clone https://github.com/conan-io/examples2.git
  2. $ cd examples2/tutorial/creating_packages/preparing_the_build

You will notice some changes in the conanfile.py file from the previous recipe. Let’s check the relevant parts:

  1. ...
  2. from conan.tools.build import check_max_cppstd, check_min_cppstd
  3. ...
  4. class helloRecipe(ConanFile):
  5. name = "hello"
  6. version = "1.0"
  7. ...
  8. options = {"shared": [True, False],
  9. "fPIC": [True, False],
  10. "with_fmt": [True, False]}
  11. default_options = {"shared": False,
  12. "fPIC": True,
  13. "with_fmt": True}
  14. ...
  15. def validate(self):
  16. if self.options.with_fmt:
  17. check_min_cppstd(self, "11")
  18. check_max_cppstd(self, "14")
  19. def source(self):
  20. git = Git(self)
  21. git.clone(url="https://github.com/conan-io/libhello.git", target=".")
  22. # Please, be aware that using the head of the branch instead of an immutable tag
  23. # or commit is not a good practice in general
  24. git.checkout("optional_fmt")
  25. def requirements(self):
  26. if self.options.with_fmt:
  27. self.requires("fmt/8.1.1")
  28. def generate(self):
  29. tc = CMakeToolchain(self)
  30. if self.options.with_fmt:
  31. tc.variables["WITH_FMT"] = True
  32. tc.generate()
  33. ...

As you can see:

  • We declare a new with_fmt option with the default value set to True

  • Based on the value of the with_fmt option:

    • We install or not the fmt/8.1.1 Conan package.

    • We require or not a minimum and a maximum C++ standard as the fmt library requires at least C++11 and it will not compile if we try to use a standard above C++14 (just an example, fmt can build with more modern standards)

    • We inject the WITH_FMT variable with the value True to the CMakeToolchain so that we can use it in the CMakeLists.txt of the hello library to add the CMake fmt::fmt target conditionally.

  • We are cloning another branch of the library. The optional_fmt branch contains some changes in the code. Let’s see what changed on the CMake side:

CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.15)
  2. project(hello CXX)
  3. add_library(hello src/hello.cpp)
  4. target_include_directories(hello PUBLIC include)
  5. set_target_properties(hello PROPERTIES PUBLIC_HEADER "include/hello.h")
  6. if (WITH_FMT)
  7. find_package(fmt)
  8. target_link_libraries(hello fmt::fmt)
  9. target_compile_definitions(hello PRIVATE USING_FMT=1)
  10. endif()
  11. install(TARGETS hello)

As you can see, we use the WITH_FMT we injected in the CMakeToolchain. Depending on the value we will try to find the fmt library and link our hello library with it. Also, check that we add the USING_FMT=1 compile definition that we use in the source code depending on whether we choose to add support for fmt or not.

hello.cpp

  1. #include <iostream>
  2. #include "hello.h"
  3. #if USING_FMT == 1
  4. #include <fmt/color.h>
  5. #endif
  6. void hello(){
  7. #if USING_FMT == 1
  8. #ifdef NDEBUG
  9. fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "hello/1.0: Hello World Release! (with color!)\n");
  10. #else
  11. fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "hello/1.0: Hello World Debug! (with color!)\n");
  12. #endif
  13. #else
  14. #ifdef NDEBUG
  15. std::cout << "hello/1.0: Hello World Release! (without color)" << std::endl;
  16. #else
  17. std::cout << "hello/1.0: Hello World Debug! (without color)" << std::endl;
  18. #endif
  19. #endif
  20. }

Let’s build the package from sources first using with_fmt=True and then with_fmt=False. When test_package runs it will show different messages depending on the value of the option.

  1. $ conan create . --build=missing -o with_fmt=True
  2. -------- Exporting the recipe ----------
  3. ...
  4. -------- Testing the package: Running test() ----------
  5. hello/1.0 (test package): Running test()
  6. hello/1.0 (test package): RUN: ./example
  7. hello/1.0: Hello World Release! (with color!)
  8. $ conan create . --build=missing -o with_fmt=False
  9. -------- Exporting the recipe ----------
  10. ...
  11. -------- Testing the package: Running test() ----------
  12. hello/1.0 (test package): Running test()
  13. hello/1.0 (test package): RUN: ./example
  14. hello/1.0: Hello World Release! (without color)

This is just a simple example of how to use the generate() method to customize the toolchain based on the value of one option, but there are lots of other things that you could do in the generate() method like:

  • Create a complete custom toolchain based on your needs to use in your build.

  • Access to certain information about the package dependencies, like:

    • The configuration accessing the defined conf_info.

    • Accessing the dependencies options.

    • Import files from dependencies using the copy tool. You could also import the files create manifests for the package, collecting all dependencies versions and licenses.

  • Use the Environment tools to generate information for the system environment.

  • Adding custom configurations besides Release and Debug, taking into account the settings, like ReleaseShared or DebugShared.

See also