gpt4 book ai didi

bazel - 如何在 Bazel 规则中运行其他规则的可执行文件?

转载 作者:行者123 更新时间:2023-12-03 14:24:00 26 4
gpt4 key购买 nike

假设我有一个自定义规则,my_object .看起来像:

my_object(
name = "foo",
deps = [
//services/image-A:push,
//services/image-B:push,
]
)
deps中的标签在哪里是 rules_dockercontainer_push规则。

我希望能够 bazel run //:foo并让它在 deps 中推送 Docker 镜像列表。我该怎么做呢?

这似乎是一个特定情况,通常只是希望在自定义规则的可执行文件中运行其他规则的可执行文件。

最佳答案

这里要做的就是拥有my_object输出执行其他可执行文件的可执行文件。

考虑这个例子:

def _impl1(ctx):
ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = "echo %s 123" % ctx.label.name)
return DefaultInfo(executable = ctx.outputs.executable)


exec_rule1 = rule(
implementation = _impl1,
executable = True,
)


def _impl2(ctx):

executable_paths = []
runfiles = ctx.runfiles()
for dep in ctx.attr.deps:
# the "./" is needed if the executable is in the current directory
# (i.e. in the workspace root)
executable_paths.append("./" + dep.files_to_run.executable.short_path)
# collect the runfiles of the other executables so their own runfiles
# will be available when the top-level executable runs
runfiles = runfiles.merge(dep.default_runfiles)

ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = "\n".join(executable_paths))

return DefaultInfo(
executable = ctx.outputs.executable,
runfiles = runfiles)


exec_rule2 = rule(
implementation = _impl2,
executable = True,
attrs = {
"deps": attr.label_list(),
},
)
BUILD.bazel :

load(":defs.bzl", "exec_rule1", "exec_rule2")

exec_rule1(name = "foo")
exec_rule1(name = "bar")
exec_rule2(name = "baz", deps = [":foo", ":bar"])

然后运行它:
$ bazel run //:baz
INFO: Analyzed target //:baz (4 packages loaded, 19 targets configured).
INFO: Found 1 target...
Target //:baz up-to-date:
bazel-bin/baz
INFO: Elapsed time: 0.211s, Critical Path: 0.01s
INFO: 0 processes.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
foo 123
bar 123

关于bazel - 如何在 Bazel 规则中运行其他规则的可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58761400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com