what to put in WORKSPACE and/or BUILD files to enable the cc_proto_library rule?

96 views
Skip to first unread message

JI Ioannidis

unread,
Jul 18, 2016, 1:25:45 PM7/18/16
to Protocol Buffers
What is the sequence of magic incantations to create a cc_proto_library rule? I have tried copying protobuf.bzl from the protobuf repo to the root of my workspace, as well as some (at least a year old) .bzl files referenced in this group, to no avail.

My end goal is to have a BUILD file such as 
$ cat protoa/BUILD  
cc_proto_library(
 name = "alice",
 srcs = ["alice.proto"],
)

then type 

$ bazel build //protoa:alice

and have it create the right .pb.h and .pb.cc files

FWIW, I have already installed bazel at sha 42d8290 (compiled, bazel binary copied to ~/bin/) and built and installed protobuf at sha b99577c

Thanks!

/ji

Paul Johnston

unread,
Aug 5, 2016, 2:47:26 PM8/5/16
to Protocol Buffers
1. During the loading phase, bring the google/protobuf into your WORKSPACE so you can refer to it as @com_github_google_protobuf later.  The bazel convention is to have fully qualified names, flattening special characters to underscore.

git_repository(
  name = "com_github_google_protobuf",
  tag = "v3.0.0",
)

2. During the analysis phase, load the function in your BUILD file by referring to the skylark bzl filename.  
    As the protobuf repo has a BUILD file in the root (which defines it as a "package"), you need to refer to the 
    bzl relative to the package name.  That's why you need the colon. 

# You can also alias the function to whatever you want with 'foo = "cc_proto_library"'
load("@com_github_google_protobuf//:protobuf.bzl", "cc_proto_library")

# These are called "target pattern operators" IIRC
@ = names an external dependency that is loaded in WORKSPACE.
// = relative to the workspace root
: relative to package

3. Call it


cc_proto_library( 
 name = "alice", 
 srcs = ["alice.proto"], 
)

But then, where did the files go?  If it worked, they should be in $(bazel info bazel-genfiles)/protoa/alice.pb.h

I've been working on a set of rules for protobuf +/- gRPC at https://github.com/pubref/rules_protobuf.  Perhaps it will be of use to you.
Reply all
Reply to author
Forward
0 new messages