CMakeToolchain可以在toolchain()方法中使用:
from conans import ConanFile, CMake, CMakeToolchainclass App(ConanFile):settings = "os", "arch", "compiler", "build_type"requires = "hello/0.1"generators = "cmake_find_package_multi"options = {"shared": [True, False], "fPIC": [True, False]}default_options = {"shared": False, "fPIC": True}def toolchain(self):tc = CMakeToolchain(self)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值的定义:
- CMake生成器平台和生成器工具集的定义
2. CMake build_type的定义
3. 基于fPIC选项的CMAKE_POSITION_INDEPENDENT_CODE的定义。
4. 必要时定义C ++标准
5. 用于C++的标准库的定义
6. 在OSX中停用rpath
constructor
def __init__(self, conanfile, generator=None, generator_platform=None, build_type=None,cmake_system_name=True, toolset=None, parallel=True, make_program=None):
大多数参数是可选的,将从当前设置中推导出,而不必定义它们。
definitions
该属性允许为多种配置(调试,发布等)定义CMake变量。
def toolchain(self):tc = CMakeToolchain(self)tc.definitions["MYVAR"] = "MyValue"tc.definitions.debug["MYCONFIGVAR"] = "MyDebugValue"tc.definitions.release["MYCONFIGVAR"] = "MyReleaseValue"return tc
这将被翻译为:conan_toolchain.cmake文件中MYVAR的一个set()定义。
一个set()定义,在conan_project_include.cmake文件中使用cmake生成器表达式,对不同的配置使用不同的值。 重要的是要记住,依赖于构建类型的事物不能在工具链中直接设置。
generators
CMakeToolchain仅与cmake_find_package和cmake_find_package_multi生成器一起使用。 使用其他方法会引起麻烦,因为它们的定义重叠可能会产生冲突。
Using the toolchain in developer flow
使用Conan工具链的优点之一是,与在缓存中创建软件包相比,它们可以帮助使用本地开发流程实现完全相同的构建。
使用CMakeToolchain,可以对像Visual Studio这样的多配置系统进行操作(假设我们使用的是cmake_find_package_multi生成器):
# Lets start in the folder containing the conanfile.py$ mkdir build && cd build# Install both debug and release deps and create the toolchain$ conan install ..$ conan install .. -s build_type=Debug# the conan_toolchain.cmake is common for both configurations# Need to pass the generator WITHOUT the platform, that matches your default settings$ cmake .. -G "Visual Studio 15" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake# Now you can open the IDE, select Debug or Release config and build# or, in the command line$ cmake --build . --config Release$ cmake --build . --config Debug
注意:平台(Win64)已被编码在工具链中。 命令行不应该传递它,因此使用-G "Visual Studio 15"而不是-G "Visual Studio 15 Win64"
对于单配置构建系统:
# Lets start in the folder containing the conanfile.py$ mkdir build_release && cd build_release$ conan install ..# the build type Release is encoded in the toolchain already.# This conan_toolchain.cmake is specific for release$ cmake .. -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake$ cmake --build . # or just "make"# debug build requires its own folder$ cd .. && mkdir build_debug && cd build_debug$ conan install .. -s build_type=Debug# the build type Debug is encoded in the toolchain already.# This conan_toolchain.cmake is specific for debug$ cmake .. -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake$ cmake --build . # or just "make"
CMake build helper
与CMakeToolchain一起使用的CMake()构建帮助器也是实验性的,将来可能会发生重大变化。 它将演变为适应和补充工具链功能。
该帮助程序旨在用于build()方法中,以在柯南直接构建软件包(创建,安装)时自动调用CMake命令。
from conans import CMakedef build(self):cmake = CMake(self)cmake.configure(source_folder="src")cmake.build()
它支持以下方法:
constructor
def __init__(self, conanfile, generator=None, build_folder=None, parallel=True,msbuild_verbosity="minimal"):
conanfile:当前配方对象。 始终使用self。generator:CMake生成器。 仅定义它即可覆盖默认值(如Visual Studio 15)。 注意,由于平台(x64,Win32…)现在已在工具链中定义,因此无需在此处指定。build_folder:包含临时构建文件的文件夹的相对路径parallel:将其设置为False可使用并行构建停用。 如果激活,它将使用cpu_count配置作为要使用的并行作业数。msbuild_verbosity:用于定义MSBuild构建的输出。
configure()
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()
def build(self, build_type=None, target=None):
调用构建系统。 等效于cmake —build。 在构建文件夹中。build_type:仅用于覆盖多配置生成器(例如Visual Studio,XCode)的settings.build_type中定义的值。 对于单配置生成器,此值将被忽略,它们将在安装步骤中使用工具链文件中定义的值。target:要运行的构建目标的名称。
install()
def install(self, build_type=None):
等效于运行 cmake --build . --target=installbuild_type:仅用于覆盖settings.build_type中定义的值。 如果构建是单一配置(例如Unix Makefiles),它将失败,因为在这种情况下,必须在配置时指定构建类型,而不是构建类型。
test()
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_TESTS或test。
