blob: 9020eacb740453cbb4f96250aa13d8e2c184e586 [file] [log] [blame]
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +02001"""Generates and compiles C++ grpc stubs from proto_library rules."""
2
3load("//:bazel/generate_cc.bzl", "generate_cc")
4
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +02005def cc_grpc_library(name, srcs, deps, proto_only, **kwargs):
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +02006 """Generates C++ grpc classes from a .proto file.
7
8 Assumes the generated classes will be used in cc_api_version = 2.
9
10 Arguments:
11 name: name of rule.
12 srcs: a single proto_library, which wraps the .proto files with services.
13 deps: a list of C++ proto_library (or cc_proto_library) which provides
14 the compiled code of any message that the services depend on.
15 **kwargs: rest of arguments, e.g., compatible_with and visibility.
16 """
17 if len(srcs) > 1:
18 fail("Only one srcs value supported", "srcs")
19
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020020 proto_target = "_" + name + "_only"
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020021 codegen_target = "_" + name + "_codegen"
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020022 codegen_grpc_target = "_" + name + "_grpc_codegen"
23 proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(':') == -1]
24 proto_deps += [dep.split(':')[0] + ':' + "_" + dep.split(':')[1] + "_only" for dep in deps if dep.find(':') != -1]
25
26 native.proto_library(
27 name = proto_target,
28 srcs = srcs,
29 deps = proto_deps,
30 **kwargs
31 )
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020032
33 generate_cc(
34 name = codegen_target,
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020035 srcs = [proto_target],
Nicolas "Pixel" Noble4dc64312016-10-20 23:07:37 +020036 **kwargs
37 )
38
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020039 if not proto_only:
40 generate_cc(
41 name = codegen_grpc_target,
42 srcs = [proto_target],
43 plugin = "//:grpc_cpp_plugin",
44 **kwargs
45 )
46
Nicolas "Pixel" Noble799bd5e2016-10-21 01:54:32 +020047 native.cc_library(
48 name = name,
49 srcs = [":" + codegen_grpc_target, ":" + codegen_target],
50 hdrs = [":" + codegen_grpc_target, ":" + codegen_target],
51 deps = deps + ["//:grpc++", "//:grpc++_codegen_proto", "//external:protobuf"],
52 **kwargs
53 )
54 else:
55 native.cc_library(
56 name = name,
57 srcs = [":" + codegen_target],
58 hdrs = [":" + codegen_target],
59 deps = deps + ["//external:protobuf"],
60 **kwargs
61 )