带有数据存储库的 C++ Bazel 项目
C++ Bazel project with a Data repository
我有一个(基本的)C++ 项目:
├── bin
│ ├── BUILD
│ ├── example.cpp
├── data
│ └── someData.txt
└── WORKSPACE
其中可执行文件 example.cpp 使用来自 data/
目录的一些数据文件:
#include <fstream>
#include <iostream>
int main()
{
std::ifstream in("data/someData.txt");
if (!in)
{
std::cerr << "Can not open file!";
return EXIT_FAILURE;
}
std::string message;
if (!(in >> message))
{
std::cerr << "Can not read file content!";
return EXIT_FAILURE;
}
std::cout << message << std::endl;
return EXIT_SUCCESS;
}
我的 Bazel 设置是最小的:
- WORKSPACE: 空文件
- bin/BUILD:
cc_binary(name = "example",srcs = ["example.cpp"])
- data/someData.txt:包含
Hello_world!
行
问题是 Bazel 将所有这些文件移动到特殊位置:
.
├── bazel-Bazel_with_Data -> ...
├── bazel-bin -> ...
├── bazel-genfiles -> ...
├── bazel-out -> ...
├── bazel-testlogs -> ...
特别是 example 可执行文件将找不到 data/someData.txt 文件:
bazel run bin:example
将打印:
INFO: Analysed target //bin:example (0 packages loaded).
INFO: Found 1 target...
Target //bin:example up-to-date:
bazel-bin/bin/example
INFO: Elapsed time: 0.101s, Critical Path: 0.00s
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/bin/example
Can not open file!ERROR: Non-zero return code '1' from command: Process exited with status 1
问题是如何管理这个?
我希望 example 可执行文件能够找到 Data/someData.txt 文件。
警告: 似乎此解决方案在 Windows 下不起作用(参见评论)。
必须在 data 目录中创建一个额外的 BUILD 文件,用于定义必须导出的数据文件。现在的项目结构是:
├── bin
│ ├── BUILD
│ ├── example.cpp
├── data
│ ├── BUILD
│ └── someData.txt
└── WORKSPACE
这个新的 data/BUILD 文件是:
exports_files(["someData.txt"])
并且修改bin/BUILD文件添加someData.txt依赖:
cc_binary(
name = "example",
srcs = ["example.cpp"],
data = ["//data:someData.txt"],
)
现在如果你 运行:
bazel run bin:example
你应该得到:
INFO: Analysed target //bin:example (2 packages loaded).
INFO: Found 1 target...
Target //bin:example up-to-date:
bazel-bin/bin/example
INFO: Elapsed time: 0.144s, Critical Path: 0.01s
INFO: Build completed successfully, 3 total actions
INFO: Running command line: bazel-bin/bin/example
Hello_world!
表示 example 可执行文件已找到 data/someData.txt 文件并打印其内容。
另请注意,您可以对 单元测试 和
使用相同的方案
cc_test(...,data =["//data:someData.txt"], )
您可以从 GitHub repo.
中复制此注释
在 Windows 上尝试:
bazel run --enable_runfiles
可以找到更多详细信息 here。
您也可以将其添加到 .bazelrc
文件中:
build:myconfig --enable_runfiles
以 Windows 为基础:
bazel build --config=myconfig //...
运行文件库也是一个选项。
我将问题发布到 Bazel issues。建议使用 Runfiles 从相对路径中提取绝对路径。然后你应该可以插入到 ifstream
的路径。需要注意的是你的相对路径需要在前面加上\__main__
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error));
std::string path = runfiles->Rlocation("__main__/relative_path");
std::ifstream in(path);
请参阅 Documentation 运行文件用法。
GitHub 问题 here
我有一个(基本的)C++ 项目:
├── bin
│ ├── BUILD
│ ├── example.cpp
├── data
│ └── someData.txt
└── WORKSPACE
其中可执行文件 example.cpp 使用来自 data/
目录的一些数据文件:
#include <fstream>
#include <iostream>
int main()
{
std::ifstream in("data/someData.txt");
if (!in)
{
std::cerr << "Can not open file!";
return EXIT_FAILURE;
}
std::string message;
if (!(in >> message))
{
std::cerr << "Can not read file content!";
return EXIT_FAILURE;
}
std::cout << message << std::endl;
return EXIT_SUCCESS;
}
我的 Bazel 设置是最小的:
- WORKSPACE: 空文件
- bin/BUILD:
cc_binary(name = "example",srcs = ["example.cpp"])
- data/someData.txt:包含
Hello_world!
行
问题是 Bazel 将所有这些文件移动到特殊位置:
.
├── bazel-Bazel_with_Data -> ...
├── bazel-bin -> ...
├── bazel-genfiles -> ...
├── bazel-out -> ...
├── bazel-testlogs -> ...
特别是 example 可执行文件将找不到 data/someData.txt 文件:
bazel run bin:example
将打印:
INFO: Analysed target //bin:example (0 packages loaded).
INFO: Found 1 target...
Target //bin:example up-to-date:
bazel-bin/bin/example
INFO: Elapsed time: 0.101s, Critical Path: 0.00s
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/bin/example
Can not open file!ERROR: Non-zero return code '1' from command: Process exited with status 1
问题是如何管理这个?
我希望 example 可执行文件能够找到 Data/someData.txt 文件。
警告: 似乎此解决方案在 Windows 下不起作用(参见评论)。
必须在 data 目录中创建一个额外的 BUILD 文件,用于定义必须导出的数据文件。现在的项目结构是:
├── bin
│ ├── BUILD
│ ├── example.cpp
├── data
│ ├── BUILD
│ └── someData.txt
└── WORKSPACE
这个新的 data/BUILD 文件是:
exports_files(["someData.txt"])
并且修改bin/BUILD文件添加someData.txt依赖:
cc_binary(
name = "example",
srcs = ["example.cpp"],
data = ["//data:someData.txt"],
)
现在如果你 运行:
bazel run bin:example
你应该得到:
INFO: Analysed target //bin:example (2 packages loaded).
INFO: Found 1 target...
Target //bin:example up-to-date:
bazel-bin/bin/example
INFO: Elapsed time: 0.144s, Critical Path: 0.01s
INFO: Build completed successfully, 3 total actions
INFO: Running command line: bazel-bin/bin/example
Hello_world!
表示 example 可执行文件已找到 data/someData.txt 文件并打印其内容。
另请注意,您可以对 单元测试 和
使用相同的方案 cc_test(...,data =["//data:someData.txt"], )
您可以从 GitHub repo.
中复制此注释在 Windows 上尝试:
bazel run --enable_runfiles
可以找到更多详细信息 here。
您也可以将其添加到 .bazelrc
文件中:
build:myconfig --enable_runfiles
以 Windows 为基础:
bazel build --config=myconfig //...
运行文件库也是一个选项。
我将问题发布到 Bazel issues。建议使用 Runfiles 从相对路径中提取绝对路径。然后你应该可以插入到 ifstream
的路径。需要注意的是你的相对路径需要在前面加上\__main__
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error));
std::string path = runfiles->Rlocation("__main__/relative_path");
std::ifstream in(path);
请参阅 Documentation 运行文件用法。
GitHub 问题 here