Conan2: Using build tools as Conan packages

The way of how to build project with a specific CMake version, different from the one already installed system-wide. Conan can also help to install these tools and use them to compile consumer projects or other Conan packages. In this case, one can declare this dependency in Conan using a type of requirement named tool_requires.

  1. Prepare the example project
> git clone https://github.com/conan-io/examples2.git
> cd examples2/tutorial/consuming_packages/tool_requires

E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires>tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 XXXX-XXXX
E:.
|   CMakeLists.txt
|   conanfile.txt
|   run_example.py
|
\---src
        main.c
  1. [tool_requires] section in the conanfile.txt file
    In this section, CMake v3.22.6 is declared to build this application.

conanfile.txt

[requires]
zlib/1.2.11

[tool_requires]
cmake/3.22.6

[generators]
CMakeDeps
CMakeToolchain

Please note that this conanfile.txt will install zlib/1.2.11 and cmake/3.22.6 separately.

However, if Conandoes 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.

  1. A message is added 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)

  1. 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:
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires>conan install . --output-folder=build --build=missing
...
...
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires>tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 XXXX-XXXX
E:.
|   CMakeLists.txt
|   CMakeUserPresets.json
|   conanfile.txt
|   run_example.py
|
+---build
|       cmakedeps_macros.cmake
|       CMakePresets.json
|       conanbuild.bat
|       conanbuildenv-release-x86_64.bat
|       conandeps_legacy.cmake
|       conanrun.bat
|       conanrunenv-release-x86_64.bat
|       conan_toolchain.cmake
|       deactivate_conanbuild.bat
|       deactivate_conanrun.bat
|       FindZLIB.cmake
|       module-ZLIB-release-x86_64-data.cmake
|       module-ZLIB-Target-release.cmake
|       module-ZLIBTargets.cmake
|       ZLIB-release-x86_64-data.cmake
|       ZLIB-Target-release.cmake
|       ZLIBConfig.cmake
|       ZLIBConfigVersion.cmake
|       ZLIBTargets.cmake
|
\---src
        main.c

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 by conanbuild.sh/bat, and run cmake --version to check that you have installed the new CMake version in the path.

> cd build
> conanbuild.bat
...
...
in a new shell:
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires\build>cmake --version
cmake version 3.22.6

CMake suite maintained and supported by Kitware (kitware.com/cmake).

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:

  1. build & run
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires\build>cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
-- Using Conan toolchain: E:/XXXXX/examples2/tutorial/consuming_packages/tool_requires/build/conan_toolchain.cmake
-- Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143
-- Conan toolchain: C++ Standard 20 with extensions OFF
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.36.32532.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Conan: Target declared 'ZLIB::ZLIB'
Building with CMake version: 3.22.6
-- Configuring done
-- Generating done
-- Build files have been written to: E:/XXXXX/examples2/tutorial/consuming_packages/tool_requires/build
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires\build>cmake --build . --config Release
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires\build>cd Release
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires\build\Release>compressor.exe
Uncompressed size is: 233
Compressed size is: 147
ZLIB VERSION: 1.2.11


  1. 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.
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires\build>deactivate_conanbuild.bat
E:\XXXXX\examples2\tutorial\consuming_packages\tool_requires\build>cmake --version
cmake version 3.26.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

At last:
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.

References

Conan Documentation --Release 2.0.17

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值