如何将文件写入当前目录而不是打包 [英] How to write files to current directory instead of bazel-out
问题描述
我具有以下目录结构:
my_dir
|
--> src
| |
| --> foo.cc
| --> BUILD
|
--> WORKSPACE
|
--> bazel-out/ (symlink)
|
| ...
src/BUILD
包含以下代码:
cc_binary(
name = "foo",
srcs = ["foo.cc"]
)
文件foo.cc
使用<fstream>
实用程序以常规方式创建一个名为bar.txt
的文件.
The file foo.cc
creates a file named bar.txt
using the regular way with <fstream>
utilities.
但是,当我使用bazel run //src:foo
调用Bazel时,将创建文件bar.txt
并将其放置在bazel-out/darwin-fastbuild/bin/src/foo.runfiles/foo/bar.txt
中,而不是原始源所在的my_dir/src/bar.txt
中.
However, when I invoke Bazel with bazel run //src:foo
the file bar.txt
is created and placed in bazel-out/darwin-fastbuild/bin/src/foo.runfiles/foo/bar.txt
instead of my_dir/src/bar.txt
, where the original source is.
我尝试在foo
规则中添加outs
字段,但Bazel抱怨说outs
不是cc_binary
的公认属性.
I tried adding an outs
field to the foo
rule, but Bazel complained that outs
is not a recognized attribute for cc_binary
.
我还考虑过创建filegroup
规则,但是没有deps
字段可以在其中声明foo
作为这些文件的依赖项.
I also thought of creating a filegroup
rule, but there is no deps
field where I can declare foo
as a dependency for those files.
如何确保将通过运行cc_binary
规则生成的文件放置在my_dir/src/bar.txt
中而不是bazel-out/...
中?
How can I make sure that the files generated by running the cc_binary
rule are placed in my_dir/src/bar.txt
instead of bazel-out/...
?
推荐答案
Bazel不允许您通过设计修改工作区的状态.
Bazel doesn't allow you to modify the state of your workspace, by design.
简短的答案是,您不希望过去构建的结果来修改工作区的状态,因此可能会修改未来构建的结果.如果在同一工作空间中多次运行Bazel会导致不同的输出,则会违反可重复性.
The short answer is that you don't want the results of the past builds to modify the state of your workspace, hence potentially modifying the results of the future builds. It'll violate reproducibility if running Bazel multiple times on the same workspace results in different outputs.
给出您的示例:想象调用bazel run //src:foo
插入
Given your example: imagine calling bazel run //src:foo
which inserts
#define true false
#define false true
src/foo.cc
顶部的
.如果再次呼叫bazel run //src:foo
,会怎样?
Here's more information on the output directory: https://docs.bazel.build/versions/master/output_directories.html#documentation-of-the-current-bazel-output-directory-layout
这篇关于如何将文件写入当前目录而不是打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!