Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bazel: set runtime environment variable and configuration file location for cc_binary/cc_test

Tags:

c++

bazel

I am trying to use odbc++ in a C++ application on linux. The following build file is used to include the library as an external dependency

licenses(["notice"])
cc_library(
    name = "lib",
    srcs=["lib/libodbc.so","lib64/libodbc++-mt.so"],
    hdrs=glob(["include/**/*.h","include/*.h"]),
    includes = ["include"],
    visibility = ["//visibility:public"],
    linkstatic = 1,
    data = [ "odbc.ini", "odbcinst.ini" ]
)

My bash profile has ODBCINI and OBCDINST set so running the binary directly from command line is fine. However, the environment variable ODBCINI and ODBCINST are not set when running bazel test.

Is there anyway to include the configuration files and set the run time environment variables to them?

like image 237
Fardream Avatar asked Nov 08 '25 23:11

Fardream


2 Answers

You can set environment variables for test execution in bazel with the --test_env flag. The documentation is here.

For example, bazel build --test_env ODBCINI=foo //my:test

like image 172
Adam Avatar answered Nov 10 '25 16:11

Adam


For bazel test

bazel test //:mytest --test_env=MYENV=myvalue

For bazel run

MYRUN=value bazel run //:myproject

For bazel build

bazel build //:myproject --action_env=MYENV=myvalue

Thanks to KevinSimper, reference https://www.kevinsimper.dk/posts/how-to-bazel-pass-environment-variables

like image 24
Mohammed Avatar answered Nov 10 '25 16:11

Mohammed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!