So I want to use bazel and the googletest framework for running unittests of a simulation. For this, I want to have sample-inputfiles ans sample-outputfiles.
I want to get the absolute path of the source file, the test file or any file in the repository, so I can create a relative path to input files from there.
E.g.:
std::string path = __SOME_PATH_IN_REPO__+"/../inputfiles/input.txt";
std::fstream infile(path);
So after a lot of searching, I found out that you can define constants straight from the BUILD file for bazel like so:
cc_test(
name = "gtest_capabilities",
srcs = [
"testing/pwdTest.cpp",
],
deps = [
"@gtest//:main",
],
copts = [
"-D __ABSOLUTE_PATH__=$(workspace)",
],
)
The important line is in copts, where I am using $(workspace)
.
I read here, that you can specify copts (or alternatively defines
) like this, which does work.
This page, which the first page links to, tells me, I can use environment variables like this: $(FOO)
It also tells me, that I can run bazel info --show_make_env
to get all availabe environment variables.
When I run it, I get a big list of environment variables, which contains workspace, next to alot of others.
Now here comes the problem:
But when I use the $(workspace)
variable, bazel tells me, that $(workspace) is not defined.
I would also work for me if I could use the $PWD variable, but I couldn't get this to work either.
I hope someone can help me.
Greetings,
Romeo
Edit:
I now use the 'data' option in the bazel build file. This works mostly