Romeo Valentin Romeo Valentin - 3 months ago 25
C++ Question

How to use environment variables in bazel/googletest

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

Answer

Ugh, this is a terrible output. Basically, --show_make_env mushes together the output of bazel info and the make variables, giving no indication of which is which. To find the make variables, you can diff the outputs to remove the non-make info:

$ bazel info --show_make_env > make-vars
$ bazel info > no-make-vars
$ diff make-vars no-make-vars
1,22d0
< ABI: local
< ABI_GLIBC_VERSION: local
< ANDROID_CPU: armeabi
< AR: /usr/bin/ar
< BINDIR: bazel-out/local-fastbuild/bin
< BINMODE: -dbg
< CC: /usr/bin/gcc
< CC_FLAGS: 
< COMPILATION_MODE: fastbuild
< CROSSTOOLTOP: external/local_config_cc
< C_COMPILER: compiler
< GENDIR: bazel-out/local-fastbuild/genfiles
< GLIBC_VERSION: local
< JAVA: external/local_jdk/bin/java
< JAVABASE: external/local_jdk
< JAVA_CPU: k8
< JAVA_TRANSLATIONS: 0
< NM: /usr/bin/nm
< OBJCOPY: /usr/bin/objcopy
< STACK_FRAME_UNLIMITED: 
< STRIP: /usr/bin/strip
< TARGET_CPU: k8

Those are the make variables that are available.

Could you find the PWD in your test, instead of using a -D to hardcode it?

Comments