我刚刚开始使用 Bazel。因此,我提前表示歉意,因为我无法弄清楚这一点。
我正在尝试运行一个命令,将一堆文件输出到一个目录,并使该目录可用于后续目标。我有两种不同的尝试:
- 使用 genrule
- 编写我自己的规则
我天真地希望用 genrule
来做到这一点。但是,您似乎不能说“我不知道这个命令到底要输出什么”并将目录放入 outs
中。现在我正在尝试编写一个可以使用 ctx.actions.declare_directory 的规则,但我还没有完全正确。我似乎无法将工具
从我的工作区转移到我的规则中。
我的 genrule 尝试看起来像这样:
genrule(
name = "doit",
srcs = [
"doitConfigA",
"doitConfigB",
],
cmd = 'HOME=. ./$(location path/to/doit) install',
# Neither of the below outs work - seems like bazel wants to know
# exactly this list of files. I don't know the files that
# will be output ahead of time.
# This one looks at the `out_dir` that I already have and
# expects the files to be the same which they might not be
outs = glob(["out_dir/**/*.*"]),
# this fails with:
# "declared output 'out_dir' was not
# created by genrule. This is probably because the genrule actually
# didn't create this output, or because the output was a directory
# and the genrule was run remotely (note that only the contents of
# declared file outputs are copied from genrules run remotely)"
outs = ['out_dir'],
tools = ['path/to/doit'],
)
我的自定义规则尝试如下所示:
def _impl(ctx):
dir = ctx.actions.declare_directory("out_dir")
ctx.actions.run_shell(
outputs=[dir],
progress_message="Running doit install ...",
command="HOME=. ./path/to/doit install",
tools=[ctx.attr.tools],
)
doit = rule(
implementation=_impl,
attrs={
"tools": attr.label_list(allow_files=True),
},
outputs={"out": "out_dir"},
)
然后,要运行我的 doit
规则,我的 BUILD 文件如下所示:
doit(
name = 'doit',
tools = ['path/to/doit'],
)
在我的 genrule 中,该命令运行,但它看起来不像我尝试在 outs
中使用目录。在我的自定义规则中,我似乎无法告诉 Bazel 我想使用 ./path/to/doit
作为工作区中的工具,例如 expected type 'File' for ' tools' 元素,但改为类型 'list'
...
看来我一定错过了一些基本的东西,因为这肯定是运行命令并向目录输出一堆未知内容的常见情况?
最佳答案
genrule
的输出 must be a fixed list of files 。作为解决方法,您可以从输出目录创建 zip。
我使用这种方法来操作 yarn install
的输出,而通常的方法不可行:
genrule(
name = "node_modules",
srcs = [
"package.json",
"yarn.lock",
],
cmd = " && ".join([
"yarn install --pure-lockfile",
"zip -r $@ node_modules",
]),
outs = [
"node_modules.zip",
],
)
然后是使用 zip 的规则:
# Rule that generates a list of the folders in node_modules
genrule(
name = "node_modules_ls",
srcs = [
":node_modules",
],
cmd = " && ".join([
"unzip $(location :node_modules) -d . ",
"ls > $@",
]),
outs = [
"out.txt",
],
)
关于Bazel:输出目录的genrule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073133/