在应用程序访问运行时文件建有巴泽尔

问题描述 投票:0回答:3

我试图建立与巴泽尔,这需要在运行时的一些文件的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>/前缀)?

c++ build bazel build-system
3个回答
1
投票

是否有任何理智(或灵活,如果你愿意的话)的方式,是指除其他runfiles硬编码这样这些路径

还没。但我们正在努力就可以了。

https://github.com/bazelbuild/bazel/issues/4460的设计文档和进步。


1
投票

除了拉斯洛的回答为设计文档,有一对夫妇的事情,你可以尝试:

  1. 使用ARGS属性,告诉那里的文件是二进制文件。仅当您通过巴泽勒运行运行的二进制虽然工作,并要求二进制理解这些标志。
  2. 创建使用包含所需文件的路径genrule清单。该清单将在一个已知的位置,这样你可以在运行时读取。

你可以在这里看到一些例子(他们是为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。)


1
投票

看起来像什么叫做added巴泽勒0.15 Rlocation支持,允许循环起来,加入运行时文件使用这个类中的代码:

  1. 依靠这种runfiles库从您的生成规则: cc_binary( name = "my_binary", ... deps = ["@bazel_tools//tools/cpp/runfiles"], )
  2. 包括runfiles库。 #include "tools/cpp/runfiles/runfiles.h" using bazel::tools::cpp::runfiles::Runfiles;
  3. 创建一个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"); // ... }
© www.soinside.com 2019 - 2024. All rights reserved.