为提供多个库的 Conan 包定义组件¶
在关于 package_info() 方法的教程部分中,我们学习了如何在包中为消费者定义信息,例如库名称或包含和库文件夹。在教程中,我们创建了一个只包含一个库的包,供消费者链接。然而,在某些情况下,库将其功能分离成不同的“组件”。这些组件可以独立使用,在某些情况下,它们可能需要来自同一库或其他库的其他组件。例如,考虑一个像 OpenSSL 这样的库,它提供“libcrypto”和“libssl”,其中“libssl”依赖于“libcrypto”。
Conan 提供了一种使用 CppInfo 对象的 components 属性来抽象此信息的方法,以定义 Conan 包中每个独立组件的信息。消费者还可以选择要链接的特定组件,而不是包的其余部分。
让我们以一个游戏引擎库为例,它提供了几个组件,例如“algorithms”、“ai”、“rendering”和“network”。“ai”和“rendering”都依赖于“algorithms”组件。
游戏引擎包的组件¶
请首先克隆源代码以重新创建此项目。您可以在 GitHub 的 examples2 仓库中找到它们。
$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/examples/conanfile/package_info/components
您可以查看项目内容
.
├── CMakeLists.txt
├── conanfile.py
├── include
│ ├── ai.h
│ ├── algorithms.h
│ ├── network.h
│ └── rendering.h
├── src
│ ├── ai.cpp
│ ├── algorithms.cpp
│ ├── network.cpp
│ └── rendering.cpp
└── test_package
├── CMakeLists.txt
├── CMakeUserPresets.json
├── conanfile.py
└── src
└── example.cpp
如您所见,每个组件都有源代码和一个用于构建它们的 CMakeLists.txt 文件。我们还有一个 test_package,我们将用它来测试单独组件的使用。
首先,让我们看一下 conanfile.py 中的 package_info() 方法以及我们如何为我们想要提供给游戏引擎包的消费者声明每个组件的信息
...
def package_info(self):
self.cpp_info.components["algorithms"].libs = ["algorithms"]
self.cpp_info.components["algorithms"].set_property("cmake_target_name", "algorithms")
self.cpp_info.components["network"].libs = ["network"]
self.cpp_info.components["network"].set_property("cmake_target_name", "network")
self.cpp_info.components["ai"].libs = ["ai"]
self.cpp_info.components["ai"].requires = ["algorithms"]
self.cpp_info.components["ai"].set_property("cmake_target_name", "ai")
self.cpp_info.components["rendering"].libs = ["rendering"]
self.cpp_info.components["rendering"].requires = ["algorithms"]
self.cpp_info.components["rendering"].set_property("cmake_target_name", "rendering")
有几个相关事项
我们通过在
cpp_info.components
属性中设置信息来声明每个组件生成的库。您可以为每个组件设置与self.cpp_info
对象相同的信息。组件的cpp_info
定义了一些默认值,就像 self.cpp_info 一样。例如,cpp_info.components
对象提供了.includedirs
和.libdirs
属性来定义这些位置,但 Conan 默认将其值设置为["lib"]
和["include"]
,因此在这种情况下无需添加它们。我们还使用
.requires
属性声明组件的依赖关系。通过此属性,您可以在组件级别声明要求,不仅适用于同一配方中的组件,也适用于声明为 Conan 包要求的其他包中的组件。我们正在使用属性模型更改组件的默认目标名称。默认情况下,Conan 为组件设置目标名称,例如
<package_name::component_name>
,但对于本教程,我们将仅使用组件名称设置组件目标名称,省略::
。当
cpp_info
包含全局构建信息(例如cpp_info.defines
)时,它不会继承到组件。如果您想与组件共享此信息,则需要为每个组件显式设置它。
您可以通过查看 test_package 文件夹来查看消费者部分。首先是 conanfile.py
...
def generate(self):
deps = CMakeDeps(self)
deps.check_components_exist = True
deps.generate()
您可以看到我们正在为 CMakeDeps 设置 check_components_exist 属性。这不是必需的,只是为了展示如果您希望组件不存在时消费者失败,您可以怎么做。因此,CMakeLists.txt 可能如下所示
cmake_minimum_required(VERSION 3.15)
project(PackageTest CXX)
find_package(game-engine REQUIRED COMPONENTS algorithms network ai rendering)
add_executable(example src/example.cpp)
target_link_libraries(example algorithms
network
ai
rendering)
如果任何组件目标不存在,find_package()
调用将失败。
让我们运行示例
$ conan create .
...
game-engine/1.0: RUN: cmake --build "/Users/barbarian/.conan2/p/t/game-d6e361d329116/b/build/Release" -- -j16
[ 12%] Building CXX object CMakeFiles/algorithms.dir/src/algorithms.cpp.o
[ 25%] Building CXX object CMakeFiles/network.dir/src/network.cpp.o
[ 37%] Linking CXX static library libnetwork.a
[ 50%] Linking CXX static library libalgorithms.a
[ 50%] Built target network
[ 50%] Built target algorithms
[ 62%] Building CXX object CMakeFiles/ai.dir/src/ai.cpp.o
[ 75%] Building CXX object CMakeFiles/rendering.dir/src/rendering.cpp.o
[ 87%] Linking CXX static library libai.a
[100%] Linking CXX static library librendering.a
[100%] Built target ai
[100%] Built target rendering
...
======== Launching test_package ========
...
-- Conan: Component target declared 'algorithms'
-- Conan: Component target declared 'network'
-- Conan: Component target declared 'ai'
-- Conan: Component target declared 'rendering'
...
[ 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 ========
game-engine/1.0 (test package): Running test()
game-engine/1.0 (test package): RUN: ./example
I am the algorithms component!
I am the network component!
I am the ai component!
└───> I am the algorithms component!
I am the rendering component!
└───> I am the algorithms component!
您可以检查要求不存在的组件是否会引发错误。将 nonexistent 组件添加到 find_package() 调用
cmake_minimum_required(VERSION 3.15)
project(PackageTest CXX)
find_package(game-engine REQUIRED COMPONENTS nonexistent algorithms network ai rendering)
add_executable(example src/example.cpp)
target_link_libraries(example algorithms
network
ai
rendering)
并再次测试包
$ conan test test_package game-engine/1.0
...
Conan: Component 'nonexistent' NOT found in package 'game-engine'
Call Stack (most recent call first):
CMakeLists.txt:4 (find_package)
-- Configuring incomplete, errors occurred!
...
ERROR: game-engine/1.0 (test package): Error in build() method, line 22
cmake.configure()
ConanException: Error 1 while executing
另请参阅
如果您想在 editable
模式下使用定义组件的配方,请查看使用组件和可编辑包中的示例。