Accessing environment variables in genrules

3,515 views
Skip to first unread message

Indigo Orton

unread,
Apr 21, 2015, 5:47:08 PM4/21/15
to bazel-...@googlegroups.com
Hi,

I'm writing some genrules that access scripts that rely on System environment variables. I can't find anything in the docs on how to allow (or pass) genrules access to the system environment variables.

Is this supported? If so how do I do it?

Thanks
Indigo

Han-Wen Nienhuys

unread,
Apr 22, 2015, 4:10:20 AM4/22/15
to Indigo Orton, bazel-...@googlegroups.com
The environment of rules is standardized to increase reproducibilty.

One way of addressing this is to set environment variables explicitly, eg.

genrules(name = "blah",
cmd= "LD_LIBRARY_PATH=/usr/local/lib ; FOO_CONFIG=/opt/foo/etc ; " +
"/opt/foo/bin/foo")

another strategy is to have a separate script which will write the
relevant variables into a source file, and then run the source file

genrules(name = "blah",
cmd= "source $(location //:common.sh) ; /opt/foo/bin/foo",
data = "//:common.sh")
> --
> You received this message because you are subscribed to the Google Groups
> "bazel-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to bazel-discus...@googlegroups.com.
> To post to this group, send email to bazel-...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bazel-discuss/f8d853ed-cf46-40f4-9204-f9174fc08eb0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Han-Wen Nienhuys
Google Munich
han...@google.com

Indigo Orton

unread,
Apr 22, 2015, 12:11:48 PM4/22/15
to bazel-...@googlegroups.com, indigo...@gmail.com
Thanks Han-Wen, that works for a few of the variables (e.g. sdk-location variables), however it doesn't help for things like CMake variables.

As much as I hate it, I currently use CMake to compile my JNI library (it is the easiest way I've found with generated code, and I was using CMake before so it wasn't hard to bring into Bazel). When I try to configure from the CMakeLists.txt I get errors regarding CMake compiler flags/variables (specifically "CMAKE_C_COMPILER_ENV_VAR" and "CMAKE_CXX_COMPILER_ENV_VAR"). When I run the same commands outside of the genrule it all works fine, so it is the environment.

Is there a better way to do jni compilation (aside from an Android.mk and storing all jni sources in a jni folder in an Android project)? Will imagine this will be supported in the mid year release? What can I do until then?

Ulf Adams

unread,
Apr 22, 2015, 3:48:02 PM4/22/15
to Indigo Orton, bazel-...@googlegroups.com
Well, cc_library and friends are intended to compile C++ code. If you want to build for Android, it shouldn't be too tricky to write a corresponding CROSSTOOL file. Is this 'only' an Android app or do you also have other code in your repo?

Indigo Orton

unread,
Apr 22, 2015, 7:07:50 PM4/22/15
to bazel-...@googlegroups.com, indigo...@gmail.com
Lots of other code. We have backend, web, Android and iOS (Android and iOS share the C++ code). I'll look into building a CROSSTOOL, but it would be nice to have someway of bringing these kind of environment variables.
Reply all
Reply to author
Forward
0 new messages