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()
: 获取软件包的排序组件,优先考虑那些在同一软件包中依赖关系较少的组件。 返回格式为{component_name: component}
的排序组件的OrderedDict
。merge(other_cppinfo: CppInfo)
: 修改当前的CppInfo
对象,使用参数other_cppinfo
的信息更新它,允许聚合来自多个依赖项的信息。