conan.tools.CppInfo¶
CppInfo
类表示给定包的基本 C++ 用法信息,例如 includedirs
、libdirs
、库名称等。这是包的消费者为了能够找到头文件并正确链接库所需的信息。
package_info()
中的 self.cpp_info
对象是一个 CppInfo
对象,因此在大多数情况下,无需显式实例化它,只需按照 package_info() 章节的说明使用即可。
本节介绍 CppInfo
的其他高级用例。
在自定义生成器中聚合信息¶
警告
此功能是实验性的,可能会有重大更改。有关更多信息,请参阅 Conan 稳定性 章节。
一些生成器,例如内置的 NMakeDeps
,包含等同于以下代码的内容,它将所有依赖项的所有信息合并到一个单一的 CppInfo
对象中,该对象聚合了所有信息。
from conan.tools import CppInfo
...
def generate(self):
aggregated_cpp_info = CppInfo(self)
deps = self.dependencies.host.topological_sort
deps = [dep for dep in reversed(deps.values())]
for dep in deps:
# We don't want independent components management, so we collapse
# the "dep" components into one CppInfo called "dep_cppinfo"
dep_cppinfo = dep.cpp_info.aggregated_components()
# Then we merge and aggregate this dependency "dep" into the final result
aggregated_cpp_info.merge(dep_cppinfo)
aggregated_cpp_info.includedirs # All include dirs from all deps, all components
aggregated_cpp_info.libs # All library names from all deps, all components
aggregated_cpp_info.system_libs # All system-libs from all deps
....
# Creates a file with this information that the build system will use
这种聚合在构建系统无法轻松使用独立依赖项或组件的情况下可能很有用。例如,NMake
或 Autotools
提供依赖项信息的机制是通过 LIBS
、CXXFLAGS
和类似变量。这些变量是全局的,因此传递所有依赖项的所有信息是唯一的可能性。
除了 package_info() 中定义的接口外,公开的文档化接口是:
CppInfo(conanfile)
:构造函数。接收一个conanfile
作为参数,通常是self
。aggregated_components()
:返回一个由所有组件聚合而成的新CppInfo
对象。get_sorted_components()
:获取包的有序组件,优先处理包内依赖项较少的组件。返回一个OrderedDict
格式的有序组件:{component_name: component}
。merge(other_cppinfo: CppInfo)
:修改当前的CppInfo
对象,用参数other_cppinfo
的信息更新它,从而允许聚合来自多个依赖项的信息。