I am new to bazel and facing the below issue while building my CPP code with bazel.
I want to pass the current date as one of the local defines in the below format.
date = < rule to get current date >
cc_lib( ... , local_define = [ "xyz = 28-11-2022"], )
how can I get the current date every time I run the bazel build? local_define = [ "xyz = date"],
I tried loading the python date but starlark doesn't have support for that.
The usual way that bazel handles non-deterministic information in the build is using "build stamping", described here:
https://bazel.build/docs/user-manual#workspace-status
Bazel goes to great lengths to avoid non-determinism because it's bad for caching, so adding something like the current time to a build isn't straight forward. Using build stamping sometimes requires changing your program or generating files based on the
status.txt
files and incorporating those into your build. If defines are really needed, you can do that with something like this:BUILD
:main.c
:(All the escaping is necessary to allow for spaces in the date, but you might not need all that.)
Note that the
--define DATE="\\\"$(date)\\\""
must be provided on the command line, and can't be put into a.bazelrc
because.bazelrc
isn't evaluated like the shell.