There are several ways. As a one-off, you can just create a genrule to execute the command over certain 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'd assume you want to do this multiple times. In that case, you can define a macro in a .bzl file. Macros are basically functions that call build rules:
# In, say, foo/bar.bzl.
def cpp_proto(name, src):
native.genrule(
name = "%s-gen" % name,
outs = ["%s.cc" % name, "%s.h" % name],
cmd = "$(location //path/to:protoc) --cpp_out=$@ $<",
srcs = [src],
tools = ["//path/to:protoc"],
)
native.cc_library(
name = name,
srcs = ["%s.cc" % name],
hdrs = ["%s.h" % name],
)
Then in, say, foo/BUILD, you can import & use your macro to concisely call the rules:
load('//foo:bar.bzl', 'cpp_proto')
cpp_proto('my-proto', 'my_proto.proto')
Then you can depend on //foo:my-proto
from 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 mzhaom's macro).