简体   繁体   English

如何在bazel中获取目标目录

[英]How do I get the target directory in bazel

I've got a genrule that produces some output files but the tool I'm using needs to know where to put the files. 我有一个生成一些输出文件的genrule ,但是我使用的工具需要知道将文件放置在何处。

So far, I've been able to get working by using dirname $(location outputfile) , but this seems like a very fragile solution 到目前为止,我已经能够通过使用dirname $(location outputfile) ,但这似乎是一个非常脆弱的解决方案

You can read about which make variables are available in a genrule here: https://docs.bazel.build/versions/master/be/make-variables.html 您可以在以下genrule了解哪些make变量可用: https : genrule

In particular: 尤其是:

@D: The output directory. @D:输出目录。 If there is only one filename in outs , this expands to the directory containing that file. 如果outs只有一个文件名,它将扩展到包含该文件的目录。 If there are multiple filenames, this variable instead expands to the package's root directory in the genfiles tree, even if all the generated files belong to the same subdirectory ! 如果存在多个文件名,则即使所有生成的文件都属于同一子目录 ,该变量也会扩展到genfiles树中程序包的根目录! If the genrule needs to generate temporary intermediate files (perhaps as a result of using some other tool like a compiler) then it should attempt to write the temporary files to @D (although /tmp will also be writable), and to remove any such generated temporary files. 如果genrule需要生成临时中间文件(可能是由于使用诸如编译器之类的其他工具的结果),则应尝试将临时文件写入@D (尽管/tmp也可写),并删除任何此类中间文件。生成的临时文件。 Especially, avoid writing to directories containing inputs - they may be on read-only filesystems, and even if they aren't, doing so would trash the source tree. 特别是,避免写入包含输入的目录-它们可能位于只读文件系统上,即使没有,也这样做会破坏源树。

In general, if the tool lets you (or if you're writing your own tool) it's best if you give the tool the individual input and output file names. 通常,如果该工具允许您(或者如果您正在编写自己的工具),则最好为该工具提供单独的输入和输出文件名。 For example, if the tool understands inputs only as directories, and that's usually ok if the directory contains only the things you want, but if it doesn't, then you have to rely on sandboxing to show the tool only the files you want, or you have to manually create temporary directories. 例如,如果该工具仅将输入理解为目录,并且如果该目录仅包含您想要的内容,那么通常就可以,但是如果目录不包含该内容,那么您就必须依靠沙箱来仅向该工具显示所需的文件,或者您必须手动创建临时目录。 Outputs as directories gives you less control over what the outputs are named, and you still have to enumerate the files in the genrule 's outs . 将输出作为目录可以减少对输出命名的控制,并且您仍然必须枚举genrule outs的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM