configure()¶
configure()
方法应该用于在 recipe 中配置 settings 和 options,以便后续在不同的方法中使用,例如 generate()
、build()
或 package()
。此方法在构建依赖关系图和扩展包依赖项时执行,这意味着当此方法执行时,依赖项仍然不存在,它们不存在,并且无法访问 self.dependencies
。
例如,对于 C (非 C++) 库,compiler.libcxx
和 compiler.cppstd
设置甚至不应该在 build()
期间存在。不仅它们不是 package_id
的一部分,而且它们根本不应该在构建过程中使用。它们将在 profile 中定义,因为图中其他包可能是 C++ 包并且需要它们,但此 recipe 有责任删除它们,以便它们不在 recipe 中使用
settings = "os", "compiler", "build_type", "arch"
def configure(self):
# Not all compilers have libcxx subsetting, so we use rm_safe
# to avoid exceptions
self.settings.rm_safe("compiler.libcxx")
self.settings.rm_safe("compiler.cppstd")
def package_id(self):
# No need to delete those settings here, they were already deleted
pass
注意
从 Conan 2.4 开始,如果定义了 languages = "C"
recipe 属性(实验性),则上述 configure()
不是必需的。
对于您想要删除 setting 的每个子设置的包,您可以使用带有通配符的 rm_safe
方法
settings = "os", "compiler", "build_type", "arch"
def configure(self):
self.settings.rm_safe("compiler.*")
这将删除 compiler
setting 的所有子设置,例如 compiler.libcxx
或 compiler.cppstd
,但保留 compiler
setting 本身(self.settings.rm_safe("compiler")
将删除它)。
同样,对于包含库的包,fPIC
option 实际上仅在库编译为静态库时适用,否则,fPIC
option 没有意义,因此应将其删除
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
def configure(self):
if self.options.shared:
# fPIC might have been removed in config_options(), so we use rm_safe
self.options.rm_safe("fPIC")
可用的自动实现¶
警告
此功能是实验性的,可能会有破坏性更改。有关更多信息,请参阅 Conan 稳定性 部分。
当未定义 configure()
方法时,如果在 implements ConanFile 属性中指定,Conan 可以自动管理一些常规选项