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!