Bazel:输出目录的风格 [英] Bazel: genrule that outputs an directory
问题描述
我刚刚开始使用Bazel.因此,我预先向您致歉,我无法弄清这一点.
I'm just getting started working with Bazel. So, I apologize in advance that I haven't been able to figure this out.
我正在尝试运行一个命令,该命令将一堆文件输出到目录,并使该目录可用于后续目标.我有两种不同的尝试:
I'm trying to run a command that outputs a bunch of files to a directory and make this directory available for subsequent targets. I have two different attempts:
- 使用genrule
- 写下我自己的规则
我天真地希望用genrule
做到这一点.但是,似乎您不能说我不知道此命令将要输出什么",而是将目录放在outs
中.现在,我正在尝试编写可以使用ctx.actions.declare_directory
的规则,但是我还没有完全正确.我似乎无法将tools
从我的工作区转移到我的规则中.
I was naively hoping to just do this with a genrule
. But, it doesn't seem you can say "I don't know exactly what this command is going to output" and put a directory in outs
. Now I'm trying to write a rule that can use ctx.actions.declare_directory
but I haven't gotten it quite right. I can't seem to get the tools
over from my workspace and into my rule.
我的绅士尝试看起来像这样:
My genrule attempt looks something like this:
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'],
)
我的自定义规则尝试如下所示:
My custom rule attempt looks something like this:
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文件如下所示:
Then, to run my doit
rule, my BUILD file looks like this:
doit(
name = 'doit',
tools = ['path/to/doit'],
)
在我看来,该命令可以运行,但似乎不像我尝试在outs
中使用目录.在我的自定义规则中,我似乎无法告诉Bazel我想使用./path/to/doit
作为工作区中的工具,例如expected type 'File' for 'tools' element but got type 'list' instead
...
In my genrule, the command runs but it doesn't like my trying to use a directory in outs
, it seems. In my custom rule, I can't seem to tell Bazel that I want to use ./path/to/doit
as a tool from my workspace, eg expected type 'File' for 'tools' element but got type 'list' instead
...
似乎我必须缺少一些基本知识,因为肯定是运行命令并将一堆未知内容输出到目录中的常见情况吗?
Seems like I must be missing something basic because surely this is a common situation to run a command and output a bunch of unknown stuff to a directory?
推荐答案
The output of a genrule
must be a fixed list of files. As a work-around, you can create a zip from the output directory.
我使用这种方法来操纵yarn install
的输出,而通常的方法不可行:
I used this approach to manipulate the output of yarn install
where the usual method was not viable:
genrule(
name = "node_modules",
srcs = [
"package.json",
"yarn.lock",
],
cmd = " && ".join([
"yarn install --pure-lockfile",
"zip -r $@ node_modules",
]),
outs = [
"node_modules.zip",
],
)
然后是一个使用zip的规则:
Then a rule that consumes the 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屋!