创建conan包-工具链
创建conan包-工具链
- 1 Toolchains
本文是基于对conan官方文档Toolchains翻译而来, 更详细的信息可以去查阅conan官方文档。
1 Toolchains
Toolchains are the new way to integrate with build systems in Conan. Recipes can define a generate()
method that will return an object which can generate files from the current configuration that can be used by the build systems. Conan generators provide information about dependencies, while toolchains provide a “translation” from the Conan settings and options, and the recipe defined configuration to something that the build system can understand. A recipe that does not have dependencies does not need a generator, but can still use a toolchain.
工具链是conan与构建系统集成的新方法。配方可以定义一个 "generate() "方法,该方法将返回一个对象,该对象可以根据当前配置生成可供构建系统使用的文件。conan生成器提供有关依赖关系的信息,而工具链则将conan设置和选项以及配方定义的配置 "翻译 "为构建系统可以理解的内容。没有依赖关系的配方不需要生成器,但仍可使用工具链。
A toolchain can be defined, among the built-ins toolchains, with an attribute with the name of the toolchain class to use.
在内置工具链中,可以定义一个工具链,其属性包含要使用的工具链类的名称。
generators = "<ToolChainClassName>"
For example, for using the CMake toolchain this should be declared in the recipe:
例如,使用 CMake 工具链时,应在配方中声明这一点:
generators = "CMakeToolchain"
Tip
You can explore available toolchains in the new tools section
您可以在新工具部分查看可用的工具链
But in the more general case, and if it needs any specific configuration beyond the default one:
但在更一般的情况下,如果它需要默认配置之外的任何特定配置:
from conan.tools.cmake import CMakeToolchain
def generate(self):
tc = CMakeToolchain(self)
# customize toolchain "tc"
tc.generate()
It is possible to use the generate() method to create your own files, which will typically be deduced from the current configuration of self.settings
and self.options
.
你可以使用 generate()
方法创建自己的文件,这些文件通常是根据 self.settings
和 self.options
的当前配置推导出来的。
from conan.tools.files import save
def generate(self):
# Based on the self.settings, self.options, the user
# can generate their own files:
save("mytoolchain.tool", "my own toolchain contents, deduced from the settings and options")
# The "mytoolchain.tool" file can be used by the build system to
# define the build
And as usual, you can create your own toolchain helpers, put them in a python_requires
package and reuse them in all your recipes.
像往常一样,你可以创建自己的工具链辅助工具,将其放入 python_requires
包中,并在所有配方中重复使用。
Toolchains have some important advantages:
工具链有一些重要优势:
- They execute at conan install time. They generate files, not command line arguments, providing better reproducibility and debugging of builds.
- 它们在conan安装时执行。它们生成的是文件,而不是命令行参数,从而提供更好的可重复性和构建调试。
- They provide a better developer experience. The command line used by developers locally, like
cmake ...
will achieve the same build, with the same flags, as theconan build
or the build that is done in the cache with aconan create
. - 它们为开发人员提供了更好的体验。开发人员在本地使用的命令行,如 “cmake …”,将与 "conan build "或在缓存中使用 "conan create "完成的构建一样,具有相同的标记。
- They are more extensible and configurable.
- 它们更具扩展性和可配置性。
The toolchains implement most of the build system logic, leaving the build helpers, like CMake()
, doing less work, and acting basically as a high level wrapper of the build system. Many of the existing arguments, attributes or methods of those build helpers will not be available. Check the documentation of each toolchain to check the associated build helper available functionality.
工具链实现了大部分的编译系统逻辑,而像 CMake()
这样的编译助手所做的工作较少,它们基本上充当了编译系统的高级封装器。这些联编助手的许多现有参数、属性或方法将不可用。请查阅每个工具链的文档,查看相关联编助手的可用功能。
from conan.tools.cmake import CMakeToolchain, CMake
def generate(self):
tc = CMakeToolchain(self)
# customize toolchain "tc"
tc.generate()
def build(self):
# NOTE: This is a simplified helper
# Not all arguments attributes and methods might be available
cmake = CMake(self)