How can i insert binary version info into cc_binary

492 views
Skip to first unread message

sp...@mobvoi.com

unread,
Apr 4, 2016, 1:37:21 PM4/4/16
to bazel-discuss
Hi all ,

as we use blade before , which offer some utilities to setup binary version for all cc_binary targets. So for all binaries , wo can use "--version" to see binary version info, (build time, build host name, build user, svn or git version info, debug or release etc... )
will it be supported in bazel in the near future? Or is it possible to implement this feature in an extension? If Yes, what should i do then?

thanks

Shunping Ye

Alex Humesky

unread,
Apr 4, 2016, 1:56:15 PM4/4/16
to sp...@mobvoi.com, bazel-discuss
One way to do this is to use make variable substitution + genrule + --define:

genrule(
  name = "gen_my_binary_version",
  outs = ["my_binary_version.txt"],
  cmd = "echo $(my_binary_version) > $@")

*_binary(
  name = "my_binary",
  ...
  data = [":my_binary_version.txt"])

Then:
bazel build :my_binary --define my_binary_version=1.2.3.4

Note that the build will fail if "--define my_binary_version=..." is not specified on the command line.

You could also generate source and include that in your srcs:

genrule(
  name = "gen_my_binary_version",
  outs = ["BuildInfo.java"],
  cmd = """
cat > $@ <<EOF
package my.binary.package;
public class BuildInfo {
  public static final String VERSION = "$(my_binary_version)";
}EOF
""")

java_binary(
  name = "my_binary",
  srcs = [..., ":BuildInfo.java"],
   ...)

There's also --stamp:
http://bazel.io/docs/bazel-user-manual.html (search for --stamp)
http://bazel.io/docs/be/java.html#java_binary.stamp

But I'm not sure if there is an easy mechanism to customize that.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/78e489e5-9323-435f-92eb-9dd650e1d573%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kristina Chodorow

unread,
Apr 4, 2016, 1:58:53 PM4/4/16
to Alex Humesky, sp...@mobvoi.com, bazel-discuss
--stamp is probably your best bet, check out the --workspace_state_command test (https://github.com/bazelbuild/bazel/blob/master/src/test/shell/bazel/bazel_workspace_status_test.sh#L73-L115) which I think does what you want.

sp...@mobvoi.com

unread,
Apr 11, 2016, 2:07:24 AM4/11/16
to bazel-discuss, sp...@mobvoi.com
thank you very much , I will implement it using your suggestion.
Reply all
Reply to author
Forward
0 new messages