"baking" build information into python binary?

259 views
Skip to first unread message

j...@tla.org

unread,
Apr 26, 2017, 1:36:37 AM4/26/17
to bazel-discuss
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

Brian Silverman

unread,
Apr 26, 2017, 2:12:40 AM4/26/17
to j...@tla.org, bazel-discuss
You want stamping. Bazel takes care of rebuilding stuff when you want that way, which isn't going to happen any other way. I see the py_* rules have a stamp attr, but I'm not familiar with how to use it with Python specifically. I would recommend looking at the implementation of those rules to figure out what it does with the information if nobody else responds with more information on that.

For more general information/examples about stamping, see this thread and bazel_workspace_status_test.sh. The stuff in that thread about working with genrule is out of date FYI; if you do genrule(stamp = True) it can read bazel-out/{stable,volatile}-status.txt now.

Your problem with including the generated .py file in a py_library is that it belongs in srcs, not deps. deps is for other py_library rules. When you put a genrule's label someplace like that, it's pretty much treated as listing all of the genrule's outputs, which in this case is a .py file.


--
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-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/a84c4d7f-6e0d-43bb-8b42-4aa1ff194bdd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

JI Ioannidis

unread,
Apr 27, 2017, 1:04:09 AM4/27/17
to bazel-discuss, j...@tla.org
Thanks for the hints. Here is my solution:


It should be pretty obvious how to adapt it to other languages! 

I wonder how version-dependent my relying on the status files being in bazel-out is, but I'll burn that bridge when I cross it :)

/ji
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages