validate_build()¶
validate_build() 方法用于验证使用当前配置是否可以构建一个包的二进制文件。这与 `validate()` 方法不同,后者在包不能与当前配置使用时会抛出异常。
validate_build() 方法可以检查 `self.settings` 和 `self.options` 的值,并在必要时抛出 `ConanInvalidConfiguration`。
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
class Pkg(ConanFile):
name = "pkg"
version = "1.0"
settings = "os", "arch", "compiler", "build_type"
def package_id(self):
# For this package, it doesn't matter the compiler used for the binary package
del self.info.settings.compiler
def validate_build(self):
# But we know this cannot be build with "gcc"
if self.settings.compiler == "gcc":
raise ConanInvalidConfiguration("This doesn't build in GCC")
此包无法使用 `gcc` 编译器创建,但可以使用其他编译器创建。
$ conan create . -s compiler=gcc
...
ERROR: There are invalid packages:
pkg/1.0: Cannot build for this configuration: This doesn't build in GCC
$ conan create . -s compiler=clang # WORKS!
一旦包被构建,就可以使用该编译器来消费。
$ conan install --requires=pkg/1.0 -s compiler=gcc # WORKS!