4

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

4

1 回答 1

1

呃,这是一个可怕的输出。基本上,--show_make_env将输出和 make 变量混合在一起bazel info,没有说明哪个是哪个。要查找 make 变量,您可以区分输出以删除非 make 信息:

$ 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

这些是可用的 make 变量。

您能否在测试中找到 PWD,而不是使用 a-D对其进行硬编码?

于 2016-07-07T15:18:40.437 回答