创建您的第一个 Conan 包¶
在前面的章节中,我们消耗了 Conan 包(例如Zlib包),首先使用了conanfile.txt,然后是conanfile.py。但是conanfile.py配方文件不仅仅用于消耗其他包,它还可以用于创建您自己的包。在本节中,我们将解释如何使用conanfile.py配方创建一个简单的 Conan 包,以及如何使用 Conan 命令从源代码构建这些包。
重要
这是一个教程章节。我们鼓励您执行这些命令。对于这个具体的例子,您需要在您的路径中安装CMake。Conan 创建包并不严格要求 CMake,您可以使用其他构建系统(如 VS、Meson、Autotools,甚至您自己的构建系统)来完成,而无需依赖 CMake。
使用 conan new 命令创建一个“Hello World” C++ 库示例项目
$ conan new cmake_lib -d name=hello -d version=1.0
这将创建一个具有以下结构的 Conan 包项目。
.
├── CMakeLists.txt
├── conanfile.py
├── include
│ └── hello.h
├── src
│ └── hello.cpp
└── test_package
├── CMakeLists.txt
├── conanfile.py
└── src
└── example.cpp
生成的文件是
conanfile.py:在根文件夹中,有一个 conanfile.py,它是主要的配方文件,负责定义包的构建和使用方式。
CMakeLists.txt:一个简单的通用CMakeLists.txt,其中没有与 Conan 特别相关的内容。
src 和 include 文件夹:包含简单的 C++“hello”库的文件夹。
test_package 文件夹:包含一个需要并链接到创建的包的示例应用程序。这不是强制性的,但有助于检查我们的包是否已正确创建。
让我们看看包的配方conanfile.py
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
class helloRecipe(ConanFile):
name = "hello"
version = "1.0"
# Optional metadata
license = "<Put the package license here>"
author = "<Put your name here> <And your email here>"
url = "<Package recipe repository url here, for issues about the package>"
description = "<Description of hello package here>"
topics = ("<Put some tag here>", "<here>", "<and here>")
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "CMakeLists.txt", "src/*", "include/*"
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def layout(self):
cmake_layout(self)
def generate(self):
deps = CMakeDeps(self)
deps.generate()
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
def package_info(self):
self.cpp_info.libs = ["hello"]
让我们简要解释一下配方的不同部分
首先,您可以看到定义的 Conan 包的名称和版本
name
:一个字符串,长度最少 2 个字符,最多 100 个小写字符,用于定义包的名称。它应该以字母数字字符或下划线开头,并且可以包含字母数字字符、下划线、+、.、-字符。version
:它是一个字符串,可以接受任何值,与name
属性具有相同的约束。如果版本遵循形式为X.Y.Z-pre1+build2
的语义化版本,则该值可以用于通过版本范围而不是精确版本来要求此包。
然后您可以看到一些定义元数据的属性。这些是可选但推荐的,用于定义包的简短description
、打包库的author
、license
、包存储库的url
以及与包相关的topics
。
在此之后,有一个与二进制配置相关的部分。本节定义了包的有效设置和选项。正如我们在消耗包章节中所解释的
settings
是项目范围的配置,不能在配方中进行默认设置。例如操作系统、编译器或构建配置,这些对于多个 Conan 包来说都是通用的。options
是特定于包的配置,可以在配方中进行默认设置。在这种情况下,我们可以选择将包创建为共享库或静态库,默认情况下是静态库。
在此之后,设置了exports_sources
属性来定义哪些源代码属于 Conan 包。这些是您想要打包的库的源代码。在本例中,是我们“hello”库的源代码。
然后,声明了几个方法
该
config_options()
方法(以及configure()
方法)允许对二进制配置模型进行微调。例如,在 Windows 上,没有fPIC
选项,因此可以将其删除。该
layout()
方法声明了我们期望找到源文件以及构建过程中生成文件的目标位置。例如,目标文件夹是生成二进制文件的位置以及 Conan 生成器在generate()
方法中创建的所有文件。在这种情况下,由于我们的项目使用 CMake 作为构建系统,我们调用了cmake_layout()
。调用此函数将设置 CMake 项目的预期位置。该
generate()
方法准备从源代码构建包。在这种情况下,它可以简化为generators = "CMakeToolchain"
属性,但保留它以展示这个重要的方法。在这种情况下,执行CMakeToolchain
generate()
方法将创建一个conan_toolchain.cmake文件,该文件将 Conan 的settings
和options
转换为 CMake 语法。为了完整起见,添加了CMakeDeps
生成器,但在配方中添加requires
之前,它并不是严格必需的。该
build()
方法使用CMake
包装器来调用 CMake 命令。这是一个薄层,在本例中,它将传递-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
参数。它将配置项目并从源代码构建它。该
package()
方法将构件(头文件、库)从构建文件夹复制到最终的包文件夹。这可以用裸的“copy”命令完成,但在这里,它利用了现有的 CMake 安装功能。如果 CMakeLists.txt 没有实现它,那么在package()
方法中使用copy() 工具编写等效内容将很容易。最后,
package_info()
方法定义了使用者在使用此包时必须链接“hello”库。还可以定义其他信息,例如包含或库路径。这些信息用于生成器(如CMakeDeps
)创建供使用者使用的文件。这是关于当前包的通用信息,并且与使用者使用的构建系统以及我们在build()
方法中使用的构建系统无关,对使用者都是可用的。
test_package 文件夹现在对于理解包是如何创建的来说并不关键。重要的部分是
test_package 文件夹与单元测试或集成测试不同。这些是“包”测试,用于验证包是否已正确创建,并且包的使用者是否能够链接并重用它。
它本身就是一个小型 Conan 项目。它包含自己的
conanfile.py
及其源代码,包括构建脚本,这些脚本依赖于正在创建的包,并且会构建并执行一个需要包中库的小应用程序。它不属于包。它只存在于源代码存储库中,而不是在包中。
让我们使用当前的默认配置从源构建包,然后让 test_package
文件夹测试该包
$ conan create .
======== Exporting recipe to the cache ========
hello/1.0: Exporting package recipe
...
hello/1.0: Exported: hello/1.0#dcbfe21e5250264b26595d151796be70 (2024-03-04 17:52:39 UTC)
======== Installing packages ========
-------- Installing package hello/1.0 (1 of 1) --------
hello/1.0: Building from source
hello/1.0: Calling build()
...
hello/1.0: Package '9bdee485ef71c14ac5f8a657202632bdb8b4482b' built
======== Testing the package: Building ========
...
[ 50%] Building CXX object CMakeFiles/example.dir/src/example.cpp.o
[100%] Linking CXX executable example
[100%] Built target example
======== Testing the package: Executing test ========
hello/1.0 (test package): Running test()
hello/1.0 (test package): RUN: ./example
hello/1.0: Hello World Release!
hello/1.0: __aarch64__ defined
hello/1.0: __cplusplus201703
hello/1.0: __GNUC__4
hello/1.0: __GNUC_MINOR__2
hello/1.0: __clang_major__15
hello/1.0: __apple_build_version__15000309
...
如果显示“Hello world Release!”,则表示成功。发生的情况是
conanfile.py 和 src 文件夹的内容已被复制(在 Conan 的术语中称为导出)到本地 Conan 缓存。
将为
hello/1.0
包启动一个新的从源代码构建,调用generate()
、build()
和package()
方法。这将在 Conan 缓存中创建二进制包。Conan 然后移至 test_package 文件夹并执行 conan install + conan build +
test()
方法,以检查包是否已正确创建。
我们现在可以验证配方和包二进制文件是否在缓存中
$ conan list hello
Local Cache
hello
hello/1.0
conan create 命令接收与 conan install 相同的参数,因此您可以向其传递相同的设置和选项。如果我们执行以下行,我们将为 Debug 配置创建新的包二进制文件,并将 hello 库构建为共享库
$ conan create . -s build_type=Debug
...
hello/1.0: Hello World Debug!
$ conan create . -o hello/1.0:shared=True
...
hello/1.0: Hello World Release!
这些新的包二进制文件也将存储在 Conan 缓存中,供此计算机上的任何项目使用。我们可以使用以下命令查看它们:
# list all the binaries built for the hello/1.0 package in the cache
$ conan list "hello/1.0:*"
Local Cache
hello
hello/1.0
revisions
dcbfe21e5250264b26595d151796be70 (2024-05-10 09:40:15 UTC)
packages
2505f7ebb5a4cca156b2d6b8534f415a4a48b5c9
info
settings
arch: armv8
build_type: Release
compiler: apple-clang
compiler.cppstd: gnu17
compiler.libcxx: libc++
compiler.version: 15
os: Macos
options
shared: True
39f48664f195e0847f59889d8a4cdfc6bca84bf1
info
settings
arch: armv8
build_type: Release
compiler: apple-clang
compiler.cppstd: gnu17
compiler.libcxx: libc++
compiler.version: 15
os: Macos
options
fPIC: True
shared: False
814ddaac84bc84f3595aa076660133b88e49fb11
info
settings
arch: armv8
build_type: Debug
compiler: apple-clang
compiler.cppstd: gnu17
compiler.libcxx: libc++
compiler.version: 15
os: Macos
options
fPIC: True
shared: False
现在我们已经创建了一个简单的 Conan 包,我们将更详细地解释 Conanfile 中的每个方法。您将学习如何修改这些方法来实现诸如从外部存储库检索源代码、向我们的包添加依赖项、自定义我们的工具链等等。
关于 Conan 缓存的说明¶
当您执行 conan create 命令时,您的包的构建并未在您的本地文件夹中进行,而是在Conan 缓存中的另一个文件夹中进行。此缓存位于用户主文件夹下的 .conan2
文件夹中。Conan 将使用 ~/.conan2
文件夹来存储构建的包以及不同的配置文件。您已经使用了 conan list 命令来列出存储在本地缓存中的配方和二进制文件。
一项重要说明:Conan 缓存是 Conan 客户端私有的——修改、添加、删除或更改 Conan 缓存中的文件是未定义行为,很可能导致故障。