CMakeToolchain可以在toolchain()方法中使用:

  1. from conans import ConanFile, CMake, CMakeToolchain
  2. class App(ConanFile):
  3. settings = "os", "arch", "compiler", "build_type"
  4. requires = "hello/0.1"
  5. generators = "cmake_find_package_multi"
  6. options = {"shared": [True, False], "fPIC": [True, False]}
  7. default_options = {"shared": False, "fPIC": True}
  8. def toolchain(self):
  9. tc = CMakeToolchain(self)
  10. return tc

conan install 命令之后(或在缓存中构建软件包时调用build()方法之前),CMakeToolchain将生成2个文件:
1. conan_toolchain.cmake主文件,可以在命令行中使用。
2. 一个conan_project_include.cmake文件,该文件将在调用cmake>= 3.15的project()之后立即自动调用,其中包含仅在此类调用之后生效的定义。 对于较旧的cmake版本,应在CMakeLists.txt中显式调用include(... / conan_project_include.cmake)

这些文件将根据当前conan settings自动管理cmake值的定义:

  1. CMake生成器平台和生成器工具集的定义
    2. CMake build_type的定义
    3. 基于fPIC选项的CMAKE_POSITION_INDEPENDENT_CODE的定义。
    4. 必要时定义C ++标准
    5. 用于C++的标准库的定义
    6. 在OSX中停用rpath

constructor

  1. def __init__(self, conanfile, generator=None, generator_platform=None, build_type=None,
  2. cmake_system_name=True, toolset=None, parallel=True, make_program=None):

大多数参数是可选的,将从当前设置中推导出,而不必定义它们。

definitions

该属性允许为多种配置(调试,发布等)定义CMake变量。

  1. def toolchain(self):
  2. tc = CMakeToolchain(self)
  3. tc.definitions["MYVAR"] = "MyValue"
  4. tc.definitions.debug["MYCONFIGVAR"] = "MyDebugValue"
  5. tc.definitions.release["MYCONFIGVAR"] = "MyReleaseValue"
  6. return tc

这将被翻译为:
conan_toolchain.cmake文件中MYVAR的一个set()定义。
一个set()定义,在conan_project_include.cmake文件中使用cmake生成器表达式,对不同的配置使用不同的值。 重要的是要记住,依赖于构建类型的事物不能在工具链中直接设置。

generators

CMakeToolchain仅与cmake_find_packagecmake_find_package_multi生成器一起使用。 使用其他方法会引起麻烦,因为它们的定义重叠可能会产生冲突。

Using the toolchain in developer flow

使用Conan工具链的优点之一是,与在缓存中创建软件包相比,它们可以帮助使用本地开发流程实现完全相同的构建。
使用CMakeToolchain,可以对像Visual Studio这样的多配置系统进行操作(假设我们使用的是cmake_find_package_multi生成器):

  1. # Lets start in the folder containing the conanfile.py
  2. $ mkdir build && cd build
  3. # Install both debug and release deps and create the toolchain
  4. $ conan install ..
  5. $ conan install .. -s build_type=Debug
  6. # the conan_toolchain.cmake is common for both configurations
  7. # Need to pass the generator WITHOUT the platform, that matches your default settings
  8. $ cmake .. -G "Visual Studio 15" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
  9. # Now you can open the IDE, select Debug or Release config and build
  10. # or, in the command line
  11. $ cmake --build . --config Release
  12. $ cmake --build . --config Debug

注意:平台(Win64)已被编码在工具链中。 命令行不应该传递它,因此使用-G "Visual Studio 15"而不是-G "Visual Studio 15 Win64"
对于单配置构建系统:

  1. # Lets start in the folder containing the conanfile.py
  2. $ mkdir build_release && cd build_release
  3. $ conan install ..
  4. # the build type Release is encoded in the toolchain already.
  5. # This conan_toolchain.cmake is specific for release
  6. $ cmake .. -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
  7. $ cmake --build . # or just "make"
  8. # debug build requires its own folder
  9. $ cd .. && mkdir build_debug && cd build_debug
  10. $ conan install .. -s build_type=Debug
  11. # the build type Debug is encoded in the toolchain already.
  12. # This conan_toolchain.cmake is specific for debug
  13. $ cmake .. -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
  14. $ cmake --build . # or just "make"

CMake build helper

CMakeToolchain一起使用的CMake()构建帮助器也是实验性的,将来可能会发生重大变化。 它将演变为适应和补充工具链功能。
该帮助程序旨在用于build()方法中,以在柯南直接构建软件包(创建,安装)时自动调用CMake命令。

  1. from conans import CMake
  2. def build(self):
  3. cmake = CMake(self)
  4. cmake.configure(source_folder="src")
  5. cmake.build()

它支持以下方法:

constructor

  1. def __init__(self, conanfile, generator=None, build_folder=None, parallel=True,
  2. msbuild_verbosity="minimal"):
  • conanfile:当前配方对象。 始终使用self
  • generator:CMake生成器。 仅定义它即可覆盖默认值(如Visual Studio 15)。 注意,由于平台(x64,Win32…)现在已在工具链中定义,因此无需在此处指定。
  • build_folder:包含临时构建文件的文件夹的相对路径
  • parallel:将其设置为False可使用并行构建停用。 如果激活,它将使用cpu_count配置作为要使用的并行作业数。
  • msbuild_verbosity:用于定义MSBuild构建的输出。

configure()

  1. def configure(self, source_folder=None):

使用给定的生成器并调用 -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake 来调用 cmake。 它还将在命令中提供CMake生成器,例如 -G "Visual Studio 15"。 注意,不必指定平台,例如 -G "Visual Studio 15 Win64",因为平台已在工具链文件中定义。
source_folder:包含根CMakeLists.txt的文件夹的相对路径

build()

  1. def build(self, build_type=None, target=None):

调用构建系统。 等效于cmake —build。 在构建文件夹中。
build_type:仅用于覆盖多配置生成器(例如Visual Studio,XCode)的settings.build_type中定义的值。 对于单配置生成器,此值将被忽略,它们将在安装步骤中使用工具链文件中定义的值。
target:要运行的构建目标的名称。

install()

  1. def install(self, build_type=None):

等效于运行 cmake --build . --target=install
build_type:仅用于覆盖settings.build_type中定义的值。 如果构建是单一配置(例如Unix Makefiles),它将失败,因为在这种情况下,必须在配置时指定构建类型,而不是构建类型。

test()

  1. def test(self, build_type=None, target=None, output_on_failure=False):

等同于运行 cmake --build . --target=RUN_TESTS
build_type:仅用于覆盖settings.build_type中定义的值。 如果构建是单一配置(例如Unix Makefiles),它将失败,因为在这种情况下,必须在配置时指定构建类型,而不是构建类型。
target:要运行的构建目标的名称,默认为RUN_TESTStest