Bazel:输出目录的一般规则 [英] Bazel: genrule that outputs a directory
本文介绍了Bazel:输出目录的一般规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我才刚刚开始和Bazel合作。因此,我提前道歉,我没有弄清楚这一点。
我正在尝试运行一个命令,该命令将一组文件输出到一个目录,并使该目录可供后续目标使用。我有两种不同的尝试:
- 使用一般规则
- 写我自己的规则
我天真地希望用genrule
来做这件事。但是,您似乎不能说"我不知道这个命令将输出什么"并在outs
中放置一个目录。现在,我正在尝试编写一条可以使用ctx.actions.declare_directory
的规则,但我并没有完全理解正确。我似乎无法将tools
从我的工作区转移到我的规则中。
我的一般规则尝试如下所示:
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
规则,我的构建文件如下所示:
doit(
name = 'doit',
tools = ['path/to/doit'],
)
在我的一般规则中,该命令会运行,但它似乎不像我尝试使用outs
中的目录。在我的自定义规则中,我似乎无法告诉Bazel我希望将./path/to/doit
用作工作区中的工具,例如expected type 'File' for 'tools' element but got type 'list' instead
...
似乎我肯定遗漏了一些基本的东西,因为运行命令并将一堆未知内容输出到目录肯定是常见的情况?
推荐答案
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:输出目录的一般规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文