我需要{strong> 旁边的另一条genrule
的输出。 (即py_binary
)。
假设我们有以下文件:
# root/gen/BUILD:
genrule(
name = "gen",
outs = ["a.txt", "a2.txt"],
cmd = "cd $(RULEDIR) && echo salam > a.txt && echo hello > a2.txt",
)
# root/BUILD:
py_binary(
name = "use",
srcs = ["use.py"],
data = ["gen:a.txt", "gen:a2.txt"],
)
# use.py:
f = open("a.txt", "r")
f2 = open("a2.txt", "r")
print(f.read())
print(f2.read())
一目了然
project
├── root
│ ├── BUILD
│ ├── gen
│ │ └── BUILD << This can generate required 'a.txt' and 'a2.txt'
| | files.
│ └── use.py << This script needs to access a.txt file as './a.txt',
| but with 'bazel run root:use' it should access files
| like 'root/gen/a.txt'.
└── WORKSPACE
当我运行bazel run :use
时,找不到文件:
FileNotFoundError: [Errno 2] No such file or directory: 'a.txt'
它需要附近有a.txt
和a2.txt
个文件,但它们位于bazel-bin/gen
目录中。
答案 0 :(得分:0)
您可以在<navbar:extend>
中设置documentation,以便将输出文件写入<svelte:head>
目录而不是genrule
目录,然后在{{ 1}}使用其在工作空间中的相对路径,例如:
bazel-bin
这应该允许应用程序在通过bazel-genfiles
运行时加载文件。
答案 1 :(得分:0)
如果您无法修改py_binary中的代码,则可以编写一个类,该类创建到另一个文件的链接,例如:
genrule(
name = "link_a.txt",
srcs = ["//gen/a.txt"]
outs = ["a.txt"],
cmd = "ln $< $@"
)
genrule(
name = "link_a2.txt",
srcs = ["//gen/a2.txt"]
outs = ["a2.txt"],
cmd = "ln $< $@"
)
({$<
是“只有一个输入文件路径的路径”的快捷方式,而类似的$@
是“只有一个输入文件路径的路径的快捷方式” “)
如果需要,也可以将类型规则包装在可重用的宏中,或者在Starlark中进行更高级的操作。