Skip to content

Commit

Permalink
changes for picking up well known protos
Browse files Browse the repository at this point in the history
Add dependency and protoc argument for picking up well known protos
from the //third_party/protobuf repo
  • Loading branch information
makdharma committed Mar 8, 2017
1 parent 2280306 commit e3e3b2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 6 additions & 1 deletion bazel/cc_grpc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

load("//:bazel/generate_cc.bzl", "generate_cc")

def cc_grpc_library(name, srcs, deps, proto_only, use_external = False, **kwargs):
def cc_grpc_library(name, srcs, deps, proto_only, well_known_protos, use_external = False, **kwargs):
"""Generates C++ grpc classes from a .proto file.
Assumes the generated classes will be used in cc_api_version = 2.
Expand All @@ -12,6 +12,9 @@ def cc_grpc_library(name, srcs, deps, proto_only, use_external = False, **kwargs
srcs: a single proto_library, which wraps the .proto files with services.
deps: a list of C++ proto_library (or cc_proto_library) which provides
the compiled code of any message that the services depend on.
well_known_protos: The target from protobuf library that exports well
known protos. Currently it will only work if the value is
"@submodule_protobuf//:well_known_protos"
use_external: When True the grpc deps are prefixed with //external. This
allows grpc to be used as a dependency in other bazel projects.
**kwargs: rest of arguments, e.g., compatible_with and visibility.
Expand All @@ -35,6 +38,7 @@ def cc_grpc_library(name, srcs, deps, proto_only, use_external = False, **kwargs
generate_cc(
name = codegen_target,
srcs = [proto_target],
well_known_protos = well_known_protos,
**kwargs
)

Expand All @@ -49,6 +53,7 @@ def cc_grpc_library(name, srcs, deps, proto_only, use_external = False, **kwargs
name = codegen_grpc_target,
srcs = [proto_target],
plugin = plugin,
well_known_protos = well_known_protos,
**kwargs
)

Expand Down
17 changes: 16 additions & 1 deletion bazel/generate_cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ def generate_cc_impl(ctx):
arguments += ["-I{0}={0}".format(include.path) for include in includes]
arguments += [proto.path for proto in protos]

# create a list of well known proto files if the argument is non-None
well_known_proto_files = []
if ctx.attr.well_known_protos:
f = ctx.attr.well_known_protos.files.to_list()[0].dirname
if f != "external/submodule_protobuf/src/google/protobuf":
print("Error: Only @submodule_protobuf//:well_known_protos is supported")
else:
# f points to "external/submodule_protobuf/src/google/protobuf"
# add -I argument to protoc so it knows where to look for the proto files.
arguments += ["-I{0}".format(f + "/../..")]
well_known_proto_files = [f for f in ctx.attr.well_known_protos.files]

ctx.action(
inputs = protos + includes + additional_input,
inputs = protos + includes + additional_input + well_known_proto_files,
outputs = out_files,
executable = ctx.executable._protoc,
arguments = arguments,
Expand All @@ -56,6 +68,9 @@ generate_cc = rule(
mandatory = False,
allow_empty = True,
),
"well_known_protos" : attr.label(
mandatory = False,
),
"_protoc": attr.label(
default = Label("//external:protocol_compiler"),
executable = True,
Expand Down
4 changes: 3 additions & 1 deletion bazel/grpc_build_system.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def grpc_proto_plugin(name, srcs = [], deps = []):

load("//:bazel/cc_grpc_library.bzl", "cc_grpc_library")

def grpc_proto_library(name, srcs = [], deps = [], well_known_deps = [], has_services = True, use_external = False):
def grpc_proto_library(name, srcs = [], deps = [], well_known_protos = None,
has_services = True, use_external = False):
cc_grpc_library(
name = name,
srcs = srcs,
deps = deps,
well_known_protos = well_known_protos,
proto_only = not has_services,
use_external = use_external,
)
Expand Down

0 comments on commit e3e3b2a

Please sign in to comment.