首页 > 解决方案 > 如何从 repository_ctx 运行 bazel 二进制可执行文件

问题描述

在 Bazel 中,是否有一种等效的方法来运行如下所述的二进制可执行文件,但在repository_rule实现函数中使用 repository_ctx?

# def _impl
ctx.actions.run(
    ...
    executable = ctx.executable.foo_binary,
)

# Is doing the ff also possible for a repository_rule?
bar = rule(
    implementation = _impl,
    attrs = {
        "foo_binary": attr.label(
            default = Label("//actions_run:foo"),
            executable = True,
            cfg = "exec",
            allow_files = True,
        ),
    },
)

repository_ctx的文档表明有一个execute()函数,但我不确定如何使用它运行另一个 bazel 构建的二进制文件。任何示例都会有所帮助。

PS:我是Bazel的新手。如果这不是目的,请重定向repository_ctx.execute

标签: bazelbazel-rules

解决方案


repository_ctx.execute()相似但不一样...根本区别在于它们何时发生。

repository_ctx可用于存储库规则的实施:

外部存储库是只能在 WORKSPACE 文件中使用的规则,并在 Bazel 的加载阶段启用非封闭操作

而用于在分析阶段评估ctx的规则实现。

换句话说。repository_ctx.execute()确实是为了在您的主机上运行(非密封)工具(当然可以位于存储库中),但这仍然是在从树构建任何东西并且可以访问执行之前。如果可执行文件在 repo 树中,您可以通过标签引用它(它将被解析),但它必须已经可用。


推荐阅读