构建需求可以在配置文件中声明,例如:

    my_profile¶

    1. [build_requires]
    2. tool1/0.1@user/channel
    3. tool2/0.1@user/channel, tool3/0.1@user/channel
    4. *: tool4/0.1@user/channel
    5. my_pkg*: tool5/0.1@user/channel
    6. &: tool6/0.1@user/channel
    7. &!: tool7/0.1@user/channel

    构建要求由模式指定:。如果未指定此类模式,则假定为 ,即适用于所有包。包可以用不同的行或逗号分隔的列表声明。在此示例中,tool1 、tool2 、tool3 和tool4 将用于依赖关系图中的所有包 (在运行conan install或conan create时)。
    如果指定了类似my_pkg
    的模式,则声明的构建要求将仅应用于与该模式匹配的包: 例如,tool5 不会应用于Zlib,但它将应用于my_pkg_zlib。
    消费者conanfile的特殊情况 (没有名称或版本) 无法与模式匹配,因此使用特殊字符处理 &:

    • & 意味着将这些构建要求应用于消费者conanfile
    • &!意味着将构建要求应用到除消费者包以外的所有包。

    请记住,消费者conanfile是test_package文件夹中的文件,或者是conan install命令中引用的文件。
    构建要求也可以在包配方中指定,具有build_requires属性和build_requirements() 方法:

    1. class MyPkg(ConanFile):
    2. build_requires = "tool_a/0.2@user/testing", "tool_b/0.2@user/testing"
    3. def build_requirements(self):
    4. # useful for example for conditional build_requires
    5. # This means, if we are running on a Windows machine, require ToolWin
    6. if platform.system() == "Windows":
    7. self.build_requires("tool_win/0.1@user/stable")

    上述tool_a和tool_b将始终被检索并用于构建此配方,而tool_win仅在Windows中使用。
    如果在build_requirements() 中定义的任何构建需求与在build_requires属性中定义的包名称具有相同的包名称,则以build_requirements() 方法中定义的包名称为准。
    根据经验,下游定义的值始终覆盖上游依赖项值。如果在配置文件中定义了某些构建要求,它将覆盖具有相同包名称的包配方中定义的构建要求。