如何将文件写入当前目录而不是打包 [英] How to write files to current directory instead of bazel-out

查看:137
本文介绍了如何将文件写入当前目录而不是打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下目录结构:

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,会怎样?

长答案: https://docs.bazel.build/versions/master/rule-challenges.html#assumption-aim-for-correctness-throughput-ease-of-use-latency

有关输出目录的更多信息: https://docs.bazel.build/versions/master/output_directories.html#documentation-of-the-current-bazel-output-directory-layout

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆