Bazel for protobuf c ++ compiler

I use the Bazel and Google protocol buffers. I want to add a Bazel rule so that I can generate the C ++ API from .proto files. In GNU make, I would do (a simplified example):

 %.h: %.cc %.cc: %.proto protoc --cpp_out=. $< 

How can I accomplish the same thing (i.e. generate an API when mymessage.proto changes) using Bazel?

+6
source share
4 answers

Bazel recently introduced cc_proto_library subscription support: http://bazel.build/blog/2017/02/27/protocol-buffers.html .

tl; dr, after installing your WORKSPACE file once,

 cc_proto_library( name = "person_cc_proto", deps = [":person_proto"], ) proto_library( name = "person_proto", srcs = ["person.proto"], deps = [":address_proto"], ) ... 

Then

 $ bazel build :person_cc_proto 

Here is an example at https://github.com/cgrushko/proto_library .

The bottom line is that you define proto_library to "import" your .proto file into Bazel and cc_proto_library to compile it in C ++. The protocol buffer compiler and runtime are taken by default from @com_google_protobuf//:protoc and @com_google_protobuf_cc//:cc_toolchain respectively.

The reason for this separation is the inclusion of large photographers, which must be compiled into several languages.

+3
source

I tried this and it did not seem to work, I got a protoc error protoc to create two directories, and then that the my-proto.h does not exist. Instead i did

 genrule( name = "my-proto-gen", outs = ["my-proto.pb.h my-proto.pb.cc"], cmd = "$(location //third_party/protobuf:protoc) --cpp_out=$(GENDIR) $<", srcs = ["my-proto.proto"], tools = ["//third_party/protobuf:protoc"], ) cc_library( name = "my-proto", srcs = ["my-proto.pb.cc"], hdrs = ["my-proto.pb.h"] ) 

It actually just checks that the header file is created and creates it bazel-genfiles .

You can then enable the proto assembly in cc_library as :my-proto .

UPDATE : To make it all work, do the following:

  • Add the following to your WORKSPACE file. This loads the protobuf library:

     http_archive( name = "protobuf", url = "https://github.com/google/protobuf/releases/download/v3.0.0/protobuf-cpp-3.0.0.zip", strip_prefix = "protobuf-3.0.0", ) 
  • Create a .bzl file (say protobuf.bzl) and add the following:

     def cpp_proto(name, src): native.genrule( name = "%s-gen" % name, outs = ["%s.pb.cc" % name, "%s.pb.h" % name], cmd = "$(location @protobuf//:protoc) --cpp_out=$(GENDIR) $<", srcs = [src], tools = ["@protobuf//:protoc"], ) native.cc_library( name = name, srcs = ["%s.pb.cc" % name], hdrs = ["%s.pb.h" % name], ) 
  • In your build files add load(':protobuf.bzl', 'cpp_proto')

  • Now you can use the macro as such:

     cpp_proto( name = "my-proto", src = "my-proto.proto" ) cc_library( name = "my-program", srcs = ["my-program.cc"], deps = [ ":my-proto", ], ) 
+3
source

There are several ways. As a one-off, you can simply create a genrule to execute a command on specific inputs:

 genrule( name = "my-proto-gen", outs = ["my-proto.cc", "my-proto.h"], cmd = "$(location //path/to:protoc) --cpp_out=$@ $<", srcs = ["my-proto.proto"], tools = ["//path/to:protoc"], ) cc_library( name = "my-proto", srcs = ["my-proto.cc"], hdrs = ["my-proto.h"], ) 

Based on your make rule, I would suggest that you want to do this several times. In this case, you can define the macro in the .bzl file. Macros are basically functions that invoke assembly rules:

 # In, say, foo/bar.bzl. def cpp_proto(name, src): native.genrule( name = "%s-gen" % name, outs = ["%s.cc" % name, "%sh" % name], cmd = "$(location //path/to:protoc) --cpp_out=$@ $<", srcs = [src], tools = ["//path/to:protoc"], ) native.cc_library( name = name, srcs = ["%s.cc" % name], hdrs = ["%sh" % name], ) 

Then, say foo / BUILD, you can import and use your macro to briefly name the rules:

 load('//foo:bar.bzl', 'cpp_proto') cpp_proto('my-proto', 'my_proto.proto') 

Then you can depend on //foo:my-proto on cc_library s, cc_binary s and cc_test s.

Finally, you might want to follow https://github.com/bazelbuild/bazel/issues/52 (and just use the mzhaom macro).

+2
source

I have compiled a set of protobuf generation rules at https://github.com/pubref/rules_protobuf . It supports C ++ in addition to a number of other languages. I hope you find this helpful.

+2
source

All Articles