我试图建立与巴泽尔,这需要在运行时的一些文件的C ++应用程序。我用的是构建规则的data
属性包括在构建目标的那些文件:
cc_binary(
name = "myapp",
srcs = [
"main.cpp",
],
data = glob([ "media/**" ], exclude = [ "media/BUILD", "media/.keep" ]),
)
问题是,巴泽尔提出下一个奇怪的路径,构建系统相关的目录(<build target name>.runfiles/__main__/<build target name>/
)的runfiles。
是否有任何理智(或灵活,如果你愿意的话)的方式,是指除其他runfiles硬编码这样这些路径
// "myapp" is the build target name
FILE* f = fopen("myapp/myapp.runfiles/__main__/myapp/media/file.txt", "r");
或从一清单读出路径(这仍然是不能更好的选择,因为每个文件与__main__/<build target name>/
前缀)?
是否有任何理智(或灵活,如果你愿意的话)的方式,是指除其他runfiles硬编码这样这些路径
还没。但我们正在努力就可以了。
除了拉斯洛的回答为设计文档,有一对夫妇的事情,你可以尝试:
你可以在这里看到一些例子(他们是为Java,但应该是CC规则类似):
https://groups.google.com/d/topic/bazel-discuss/UTeGdXjO_lQ/discussion
另一种选择是使用类似pkg_tar
或类似的规则,重新包装的二进制和runfiles到你需要的任何结构:https://docs.bazel.build/versions/master/be/pkg.html(据我虽然知道,pkg_tar不包二进制文件的runfiles。)
看起来像什么叫做added巴泽勒0.15 Rlocation
支持,允许循环起来,加入运行时文件使用这个类中的代码:
cc_binary(
name = "my_binary",
...
deps = ["@bazel_tools//tools/cpp/runfiles"],
)
#include "tools/cpp/runfiles/runfiles.h"
using bazel::tools::cpp::runfiles::Runfiles;
Runfiles
对象,并使用Rlocation
查找运行文件的路径:
int main(int argc, char** argv) {
std::string error;
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error));
// Important:
// If this is a test, use Runfiles::CreateForTest(&error).
// Otherwise, if you don't have the value for argv[0] for whatever
// reason, then use Runfiles::Create(&error).
if (runfiles == nullptr) {
// error handling
}
std::string path = runfiles->Rlocation("my_workspace/path/to/my/data.txt");
// ...
}