为提供多个库的 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
属性声明组件的依赖项。 使用此属性,您可以在组件级别声明需求,不仅适用于同一 recipe 中的组件,也适用于声明为 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
模式下使用定义组件的 recipes,请查看 使用组件和可编辑包中的示例。