简体   繁体   English

如何在bazel / googletest中使用环境变量

[英]How to use environment variables in bazel/googletest

So I want to use bazel and the googletest framework for running unittests of a simulation. 所以我想使用bazel和googletest框架来运行模拟的单元测试。 For this, I want to have sample-inputfiles ans sample-outputfiles. 为此,我想要sample-inputfiles和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. 我想获取源文件,测试文件或存储库中的任何文件的绝对路径,因此我可以创建从那里输入文件的相对路径。
Eg: 例如:

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: 经过大量的搜索后,我发现你可以直接从BUILD文件中为bazel定义常量,如下所示:

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) . 重要的是在copts中,我使用$(workspace)
I read here , that you can specify copts (or alternatively defines ) like this, which does work. 在这里读到,你可以像这样指定copts(或者defines ),这确实有效。
This page, which the first page links to, tells me, I can use environment variables like this: $(FOO) 这个页面,第一页链接到,告诉我,我可以使用这样的环境变量: $(FOO)
It also tells me, that I can run bazel info --show_make_env to get all availabe environment variables. 它还告诉我,我可以运行bazel info --show_make_env来获取所有bazel info --show_make_env环境变量。

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. 但是当我使用$(workspace)变量时,bazel告诉我,$(工作空间)没有定义。
I would also work for me if I could use the $PWD variable, but I couldn't get this to work either. 如果我可以使用$ PWD变量,我也会为我工作,但我也无法使用它。

I hope someone can help me. 我希望有一个人可以帮助我。
Greetings, 问候,
Romeo 罗密欧

Edit: 编辑:
I now use the 'data' option in the bazel build file. 我现在使用bazel构建文件中的'data'选项。 This works mostly 这主要起作用

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. 基本上, --show_make_env show_make_env将bazel info和make变量的输出结合起来,没有说明哪个是哪个。 To find the make variables, you can diff the outputs to remove the non-make 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

Those are the make variables that are available. 这些是可用的make变量。

Could you find the PWD in your test, instead of using a -D to hardcode it? 您可以在测试中找到PWD,而不是使用-D进行硬编码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM