Using build tools as Conan packages — conan 2.8.0 documentation (2024)

Conan Center will stop receiving updates for Conan 1.x packages soon. Read this blog post

  • Tutorial
  • Consuming packages
  • Using build tools as Conan packages
  • Edit on GitHub

In the previous example, we built our CMake project and used Conan to install and locatethe Zlib library. We used the CMake already installed in our system to build ourcompressor binary. However, what happens if you want to build your project with a specificCMake version, different from the one already installed system-wide? Conan can also helpyou install these tools and use them to compile consumer projects or other Conanpackages. In this case, you can declare this dependency in Conan using a type ofrequirement named tool_requires. Let’s see an example of how to add atool_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 theexamples2 repository in GitHub:

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

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

.├── conanfile.txt├── CMakeLists.txt└── src └── main.c

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

conanfile.txt

[requires]zlib/1.2.11[tool_requires]cmake/3.22.6[generators]CMakeDepsCMakeToolchain

Important

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

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

CMakeLists.txt

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

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

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

Note

Powershell users need to add --conf=tools.env.virtualenv:powershell=True to the previous commandto 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:

-------- Computing dependency graph ----------cmake/3.22.6: Not found in local cache, looking in remotes...cmake/3.22.6: Checking remote: conancentercmake/3.22.6: Trying with 'conancenter'...Downloading conanmanifest.txtDownloading conanfile.pycmake/3.22.6: Downloaded recipe revision 3e3d8f3a848b2a60afafbe7a0955085aGraph root conanfile.txt: /Users/user/Documents/developer/conan/examples2/tutorial/consuming_packages/tool_requires/conanfile.txtRequirements zlib/1.2.11#f1fadf0d3b196dc0332750354ad8ab7b - CacheBuild requirements cmake/3.22.6#3e3d8f3a848b2a60afafbe7a0955085a - Downloaded (conancenter)-------- Computing necessary packages ----------Requirements zlib/1.2.11#f1fadf0d3b196dc0332750354ad8ab7b:2a823fda5c9d8b4f682cb27c30caf4124c5726c8#48bc7191ec1ee467f1e951033d7d41b2 - CacheBuild requirements cmake/3.22.6#3e3d8f3a848b2a60afafbe7a0955085a:f2f48d9745706caf77ea883a5855538256e7f2d4#6c519070f013da19afd56b52c465b596 - Download (conancenter)-------- Installing packages ----------Installing (downloading, building) binaries...cmake/3.22.6: Retrieving package f2f48d9745706caf77ea883a5855538256e7f2d4 from remote 'conancenter'Downloading conanmanifest.txtDownloading conaninfo.txtDownloading conan_package.tgzDecompressing conan_package.tgzcmake/3.22.6: Package installed f2f48d9745706caf77ea883a5855538256e7f2d4cmake/3.22.6: Downloaded package revision 6c519070f013da19afd56b52c465b596zlib/1.2.11: Already installed!-------- Finalizing install (deploy, generators) ----------conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'conanfile.txt: Generator 'CMakeDeps' calling 'generate()'conanfile.txt: Generating aggregated env files

Now, if you check the folder you will see that Conan generated a newfile called conanbuild.sh/bat. This is the result of automatically invoking aVirtualBuildEnv generator when we declared the tool_requires in theconanfile.txt. This file sets some environment variables like a new PATH thatwe 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 youhave installed the new CMake version in the path.

Windows

$ cd build$ conanbuild.bat# conanbuild.ps1 if using Powershell

Linux, macOS

$ cd build$ source conanbuild.shCapturing current environment in deactivate_conanbuildenv-release-x86_64.shConfiguring environment variables

Run cmake and check the version:

$ cmake --versioncmake version 3.22.6...

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

Windows

# assuming Visual Studio 15 2017 is your VS version and that it matches your default profile$ cmake .. -G "Visual Studio 15 2017" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake$ cmake --build . --config Release...Building with CMake version: 3.22.6...[100%] Built target compressor$ Release\compressor.exeUncompressed size is: 233Compressed size is: 147ZLIB VERSION: 1.2.11

Linux, macOS

$ cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release$ cmake --build ....Building with CMake version: 3.22.6...[100%] Built target compressor$ ./compressorUncompressed size is: 233Compressed size is: 147ZLIB VERSION: 1.2.11

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

Windows

$ deactivate_conanbuild.bat

Linux, macOS

$ source deactivate_conanbuild.shRestoring environment

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

$ cmake --versioncmake version 3.22.0...

Note

Best practice

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

See also

  • Using [system_tools] in your profiles.

  • Creating recipes for tool_requires: packaging build tools.

  • Using the same requirement as a requires and as a tool_requires

  • Using a MinGW as tool_requires to build with gcc in Windows

  • Using tool_requires in profiles

  • Using conf to set a toolchain from a tool requires

Using build tools as Conan packages — conan 2.8.0 documentation (2024)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Reed Wilderman

Last Updated:

Views: 6489

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.