I'm using Bazel and Google's protocol buffers. I want to add a Bazel rule so I can generate the C++ API from the .proto
files. In GNU make, I would do (simplified example):
%.h: %.cc
%.cc: %.proto
protoc --cpp_out=. $<
How can I accomplish the same (i.e. generate the API whenever mymessage.proto
changes) using Bazel?
Native support for cc_proto_library
has recently landed in Bazel: http://bazel.build/blog/2017/02/27/protocol-buffers.html.
tl;dr, after setting 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
There's an example at https://github.com/cgrushko/proto_library.
The gist is that you define a proto_library
to "import" you .proto file into Bazel, and cc_proto_library
to compile it to C++. The protocol buffer compiler and runtimes are taken by default from @com_google_protobuf//:protoc
and @com_google_protobuf_cc//:cc_toolchain
, respectively.
The reason for this separation is to enable large proto graphs that need to be compiled to multiple languages.