• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Generates and compiles C++ grpc stubs from proto_library rules."""
2
3load("//:bazel/generate_cc.bzl", "generate_cc")
4
5def cc_grpc_library(name, srcs, deps, proto_only, well_known_protos, generate_mocks = False, use_external = False, **kwargs):
6  """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      well_known_protos: Should this library additionally depend on well known
16        protos
17      use_external: When True the grpc deps are prefixed with //external. This
18        allows grpc to be used as a dependency in other bazel projects.
19      generate_mocks: When True, Google Mock code for client stub is generated.
20      **kwargs: rest of arguments, e.g., compatible_with and visibility.
21  """
22  if len(srcs) > 1:
23    fail("Only one srcs value supported", "srcs")
24
25  proto_target = "_" + name + "_only"
26  codegen_target = "_" + name + "_codegen"
27  codegen_grpc_target = "_" + name + "_grpc_codegen"
28  proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(':') == -1]
29  proto_deps += [dep.split(':')[0] + ':' + "_" + dep.split(':')[1] + "_only" for dep in deps if dep.find(':') != -1]
30
31  native.proto_library(
32      name = proto_target,
33      srcs = srcs,
34      deps = proto_deps,
35      **kwargs
36  )
37
38  generate_cc(
39      name = codegen_target,
40      srcs = [proto_target],
41      well_known_protos = well_known_protos,
42      **kwargs
43  )
44
45  if not proto_only:
46    plugin = "@com_github_grpc_grpc//:grpc_cpp_plugin"
47    generate_cc(
48        name = codegen_grpc_target,
49        srcs = [proto_target],
50        plugin = plugin,
51        well_known_protos = well_known_protos,
52        generate_mocks = generate_mocks,
53        **kwargs
54    )
55    grpc_deps  = ["@com_github_grpc_grpc//:grpc++_codegen_proto",
56                  "//external:protobuf"]
57    native.cc_library(
58        name = name,
59        srcs = [":" + codegen_grpc_target, ":" + codegen_target],
60        hdrs = [":" + codegen_grpc_target, ":" + codegen_target],
61        deps = deps + grpc_deps,
62        **kwargs
63    )
64  else:
65    native.cc_library(
66        name = name,
67        srcs = [":" + codegen_target],
68        hdrs = [":" + codegen_target],
69        deps = deps + ["//external:protobuf"],
70        **kwargs
71    )
72