I want to be able to "bake" build information, such as the git SHA of the repo off which the build was based, time of build, who actually did the build, etc.
I thought I would create a script named "mkbuildinfo.sh", for example:
#!/bin/sh
cat << @EOF
DATE="$(date)"
SHA="$(git rev-parse HEAD)"
@EOF
and have the following rule in my BUILD file (per
https://bazel.build/versions/master/docs/be/general.html#genrule)
genrule(
name = "genbuildinfo",
outs = [
"buildinfo.py",
],
cmd = "./$(location mkbuildinfo.sh) > \"$@\"",
tools = [
"mkbuildinfo.sh",
],
)
"bazel build genbuildinfo" should generate buildinfo.py, right?
PROBLEM #1: when mkbuildinfo.sh runs, its current working directory is not the directory where bazel build was run. Specifying "local = True" does not change that. *How do I access the source directory? I don't see anything in the environment, and there are no Makefile variables that contain that information.*
Now, even if I were to ignore that problem (even the build date is useful), how do I include the generated python file in a subsequent binary? My naïve approach was to use the following rule:
py_library(
name = "buildinfo",
deps = [
":genbuildinfo",
],
)
but that fails with
$ blaze build buildinfo
ERROR: /home/owal/workspace/owal/src/BUILD:8:10: in deps attribute of py_library rule //:buildinfo: '//:genbuildinfo' does not have mandatory provider 'py'.
ERROR: Analysis of target '//:buildinfo' failed; build aborted.
INFO: Elapsed time: 0.176s
What's the right way to accomplish this?
Thanks,
/ji