Using build tools as Conan packages

In the previous example, we built our CMake project and used Conan to install and locate the Zlib library. We used the CMake already installed in our system to build our compressor binary. However, what happens if you want to build your project with a specific CMake version, different from the one already installed system-wide? Conan can also help you install these tools and use them to compile consumer projects or other Conan packages. In this case, you can declare this dependency in Conan using a type of requirement named tool_requires. Let’s see an example of how to add a tool_requires to our project and use a different CMake version to build it.

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

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

The structure of the project is the same as the one of the previous example:

  1. .
  2. ├── conanfile.txt
  3. ├── CMakeLists.txt
  4. └── src
  5. └── main.c

The main difference is the addition of the [tool_requires] section in the conanfile.txt file. In this section, we declare that we want to build our application using CMake v3.22.6.

conanfile.txt

  1. [requires]
  2. zlib/1.2.11
  3. [tool_requires]
  4. cmake/3.22.6
  5. [generators]
  6. CMakeDeps
  7. CMakeToolchain

Important

Please note that this conanfile.txt will install zlib/1.2.11 and cmake/3.22.6 separately. However, if Conan does not find a binary for Zlib in Conan Center and it needs to be built from sources, a CMake installation must already be present in your system, because the cmake/3.22.6 declared in your conanfile.txt only applies to your current project, not all dependencies. If you want to use that cmake/3.22.6 to also build Zlib, when installing if necessary, you may add the [tool_requires] section to the profile you are using. Please check the profile doc for more information.

We also added a message to the CMakeLists.txt to output the CMake version:

CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.15)
  2. project(compressor C)
  3. find_package(ZLIB REQUIRED)
  4. message("Building with CMake version: ${CMAKE_VERSION}")
  5. add_executable(${PROJECT_NAME} src/main.c)
  6. target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)

Now, as in the previous example, we will use Conan to install Zlib and CMake 3.22.6 and generate the files to find both of them. We will generate those files the folder build. To do that, just run:

  1. $ conan install . --output-folder=build --build=missing

Note

Powershell users need to add --conf=tools.env.virtualenv:powershell=True to the previous command to generate .ps1 files instead of .bat files. To avoid the need to add this line every time, we recommend configuring it in the [conf] section of your profile. For detailed information, please refer to the profiles section.

You can check the output:

  1. -------- Computing dependency graph ----------
  2. cmake/3.22.6: Not found in local cache, looking in remotes...
  3. cmake/3.22.6: Checking remote: conancenter
  4. cmake/3.22.6: Trying with 'conancenter'...
  5. Downloading conanmanifest.txt
  6. Downloading conanfile.py
  7. cmake/3.22.6: Downloaded recipe revision 3e3d8f3a848b2a60afafbe7a0955085a
  8. Graph root
  9. conanfile.txt: /Users/user/Documents/developer/conan/examples2/tutorial/consuming_packages/tool_requires/conanfile.txt
  10. Requirements
  11. zlib/1.2.11#f1fadf0d3b196dc0332750354ad8ab7b - Cache
  12. Build requirements
  13. cmake/3.22.6#3e3d8f3a848b2a60afafbe7a0955085a - Downloaded (conancenter)
  14. -------- Computing necessary packages ----------
  15. Requirements
  16. zlib/1.2.11#f1fadf0d3b196dc0332750354ad8ab7b:2a823fda5c9d8b4f682cb27c30caf4124c5726c8#48bc7191ec1ee467f1e951033d7d41b2 - Cache
  17. Build requirements
  18. cmake/3.22.6#3e3d8f3a848b2a60afafbe7a0955085a:f2f48d9745706caf77ea883a5855538256e7f2d4#6c519070f013da19afd56b52c465b596 - Download (conancenter)
  19. -------- Installing packages ----------
  20. Installing (downloading, building) binaries...
  21. cmake/3.22.6: Retrieving package f2f48d9745706caf77ea883a5855538256e7f2d4 from remote 'conancenter'
  22. Downloading conanmanifest.txt
  23. Downloading conaninfo.txt
  24. Downloading conan_package.tgz
  25. Decompressing conan_package.tgz
  26. cmake/3.22.6: Package installed f2f48d9745706caf77ea883a5855538256e7f2d4
  27. cmake/3.22.6: Downloaded package revision 6c519070f013da19afd56b52c465b596
  28. zlib/1.2.11: Already installed!
  29. -------- Finalizing install (deploy, generators) ----------
  30. conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
  31. conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
  32. conanfile.txt: Generating aggregated env files

Now, if you check the folder you will see that Conan generated a new file called conanbuild.sh/bat. This is the result of automatically invoking a VirtualBuildEnv generator when we declared the tool_requires in the conanfile.txt. This file sets some environment variables like a new PATH that we can use to inject to our environment the location of CMake v3.22.6.

Activate the virtual environment, and run cmake --version to check that you have installed the new CMake version in the path.

Windows

  1. $ cd build
  2. $ conanbuild.bat
  3. # conanbuild.ps1 if using Powershell

Linux, macOS

  1. $ cd build
  2. $ source conanbuild.sh
  3. Capturing current environment in deactivate_conanbuildenv-release-x86_64.sh
  4. Configuring environment variables

Run cmake and check the version:

  1. $ cmake --version
  2. cmake version 3.22.6
  3. ...

As you can see, after activating the environment, the CMake v3.22.6 binary folder was added to the path and is the currently active version now. Now you can build your project as you previously did, but this time Conan will use CMake 3.22.6 to build it:

Windows

  1. # assuming Visual Studio 15 2017 is your VS version and that it matches your default profile
  2. $ cmake .. -G "Visual Studio 15 2017" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
  3. $ cmake --build . --config Release
  4. ...
  5. Building with CMake version: 3.22.6
  6. ...
  7. [100%] Built target compressor
  8. $ Release\compressor.exe
  9. Uncompressed size is: 233
  10. Compressed size is: 147
  11. ZLIB VERSION: 1.2.11

Linux, macOS

  1. $ cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
  2. $ cmake --build .
  3. ...
  4. Building with CMake version: 3.22.6
  5. ...
  6. [100%] Built target compressor
  7. $ ./compressor
  8. Uncompressed size is: 233
  9. Compressed size is: 147
  10. ZLIB VERSION: 1.2.11

Note that when we activated the environment, a new file named deactivate_conanbuild.sh/bat was created in the same folder. If you source this file you can restore the environment as it was before.

Windows

  1. $ deactivate_conanbuild.bat

Linux, macOS

  1. $ source deactivate_conanbuild.sh
  2. Restoring environment

Run cmake and check the version, it will be the version that was installed previous to the environment activation:

  1. $ cmake --version
  2. cmake version 3.22.0
  3. ...

Note

Best practice

tool_requires and tool packages are intended for executable applications, like cmake or ninja. Do not use tool_requires to depend on library or library-like dependencies.

See also