validate_build()

validate_build() 方法用于验证包二进制文件是否可以构建于当前配置。它与 validate() 方法不同,后者在包无法使用于当前配置时引发异常。

validate_build() 方法可以检查 self.settingsself.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!