aboutsummaryrefslogtreecommitdiff
path: root/protobuf.bzl
diff options
context:
space:
mode:
Diffstat (limited to 'protobuf.bzl')
-rw-r--r--protobuf.bzl84
1 files changed, 34 insertions, 50 deletions
diff --git a/protobuf.bzl b/protobuf.bzl
index f674a6c6..71eaba22 100644
--- a/protobuf.bzl
+++ b/protobuf.bzl
@@ -15,9 +15,13 @@ def _GenDir(ctx):
return _GetPath(ctx, ctx.attr.includes[0])
return _GetPath(ctx, ctx.label.package + '/' + ctx.attr.includes[0])
-def _CcOuts(srcs):
- return [s[:-len(".proto")] + ".pb.h" for s in srcs] + \
- [s[:-len(".proto")] + ".pb.cc" for s in srcs]
+def _CcOuts(srcs, use_grpc_plugin=False):
+ ret = [s[:-len(".proto")] + ".pb.h" for s in srcs] + \
+ [s[:-len(".proto")] + ".pb.cc" for s in srcs]
+ if use_grpc_plugin:
+ ret += [s[:-len(".proto")] + ".grpc.pb.h" for s in srcs] + \
+ [s[:-len(".proto")] + ".grpc.pb.cc" for s in srcs]
+ return ret
def _PyOuts(srcs):
return [s[:-len(".proto")] + "_pb2.py" for s in srcs]
@@ -63,6 +67,10 @@ def _proto_gen_impl(ctx):
if ctx.attr.gen_py:
args += ["--python_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
+ if ctx.executable.grpc_cpp_plugin:
+ args += ["--plugin=protoc-gen-grpc=" + ctx.executable.grpc_cpp_plugin.path]
+ args += ["--grpc_out=" + ctx.var["GENDIR"] + "/" + gen_dir]
+
if args:
ctx.action(
inputs=srcs + deps,
@@ -90,6 +98,11 @@ _proto_gen = rule(
single_file = True,
mandatory = True,
),
+ "grpc_cpp_plugin": attr.label(
+ cfg = HOST_CFG,
+ executable = True,
+ single_file = True,
+ ),
"gen_cc": attr.bool(),
"gen_py": attr.bool(),
"outs": attr.output_list(),
@@ -104,9 +117,10 @@ def cc_proto_library(
deps=[],
cc_libs=[],
include=None,
- protoc="//google/protobuf:protoc",
+ protoc="//:protoc",
internal_bootstrap_hack=False,
- default_runtime="//google/protobuf:protobuf",
+ use_grpc_plugin=False,
+ default_runtime="//:protobuf",
**kargs):
"""Bazel rule to create a C++ protobuf library from proto source files
@@ -126,6 +140,8 @@ def cc_proto_library(
for bootstraping. When it is set to True, no files will be generated.
The rule will simply be a provider for .proto files, so that other
cc_proto_library can depend on it.
+ use_grpc_plugin: a flag to indicate whether to call the grpc C++ plugin
+ when processing the proto files.
default_runtime: the implicitly default runtime which will be depended on by
the generated cc_library target.
**kargs: other keyword arguments that are passed to cc_library.
@@ -153,13 +169,19 @@ def cc_proto_library(
**kargs)
return
- outs = _CcOuts(srcs)
+ grpc_cpp_plugin = None
+ if use_grpc_plugin:
+ grpc_cpp_plugin = "//external:grpc_cpp_plugin"
+
+ outs = _CcOuts(srcs, use_grpc_plugin)
+
_proto_gen(
name=name + "_genproto",
srcs=srcs,
deps=[s + "_genproto" for s in deps],
includes=includes,
protoc=protoc,
+ grpc_cpp_plugin=grpc_cpp_plugin,
gen_cc=1,
outs=outs,
visibility=["//visibility:public"],
@@ -167,6 +189,8 @@ def cc_proto_library(
if default_runtime and not default_runtime in cc_libs:
cc_libs += [default_runtime]
+ if use_grpc_plugin:
+ cc_libs += ["//external:grpc_lib"]
native.cc_library(
name=name,
@@ -175,37 +199,6 @@ def cc_proto_library(
includes=includes,
**kargs)
-
-def internal_copied_filegroup(
- name,
- srcs,
- include,
- **kargs):
- """Bazel rule to fix sources file to workaround with python path issues.
-
- Args:
- name: the name of the internal_copied_filegroup rule, which will be the
- name of the generated filegroup.
- srcs: the source files to be copied.
- include: the expected import root of the source.
- **kargs: extra arguments that will be passed into the filegroup.
- """
- outs = [_RelativeOutputPath(s, include) for s in srcs]
-
- native.genrule(
- name=name+"_genrule",
- srcs=srcs,
- outs=outs,
- cmd=" && ".join(["cp $(location %s) $(location %s)" %
- (s, _RelativeOutputPath(s, include))
- for s in srcs]))
-
- native.filegroup(
- name=name,
- srcs=outs,
- **kargs)
-
-
def py_proto_library(
name,
srcs=[],
@@ -213,8 +206,8 @@ def py_proto_library(
py_libs=[],
py_extra_srcs=[],
include=None,
- default_runtime="//google/protobuf:protobuf_python",
- protoc="//google/protobuf:protoc",
+ default_runtime="//:protobuf_python",
+ protoc="//:protoc",
**kargs):
"""Bazel rule to create a Python protobuf library from proto source files
@@ -254,15 +247,6 @@ def py_proto_library(
visibility=["//visibility:public"],
)
- if include != None:
- # Copy the output files to the desired location to make the import work.
- internal_copied_filegroup_name=name + "_internal_copied_filegroup"
- internal_copied_filegroup(
- name=internal_copied_filegroup_name,
- srcs=outs,
- include=include)
- outs=[internal_copied_filegroup_name]
-
if default_runtime and not default_runtime in py_libs + deps:
py_libs += [default_runtime]
@@ -270,6 +254,7 @@ def py_proto_library(
name=name,
srcs=outs+py_extra_srcs,
deps=py_libs+deps,
+ imports=includes,
**kargs)
def internal_protobuf_py_tests(
@@ -286,8 +271,7 @@ def internal_protobuf_py_tests(
"""
for m in modules:
- s = _RelativeOutputPath(
- "python/google/protobuf/internal/%s.py" % m, "python")
+ s = "python/google/protobuf/internal/%s.py" % m
native.py_test(
name="py_%s" % m,
srcs=[s],