| 1 |
# Copyright 2021 The gRPC Authors |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
"""Generates and compiles C++ grpc stubs from proto_library rules.""" |
| 15 |
|
| 16 |
load("@rules_proto//proto:defs.bzl", "proto_library") |
| 17 |
load("//bazel:generate_cc.bzl", "generate_cc") |
| 18 |
load("//bazel:protobuf.bzl", "well_known_proto_libs") |
| 19 |
|
| 20 |
def cc_grpc_library( |
| 21 |
name, |
| 22 |
srcs, |
| 23 |
deps, |
| 24 |
proto_only = False, |
| 25 |
well_known_protos = False, |
| 26 |
generate_mocks = False, |
| 27 |
use_external = False, |
| 28 |
grpc_only = False, |
| 29 |
**kwargs): |
| 30 |
"""Generates C++ grpc classes for services defined in a proto file. |
| 31 |
|
| 32 |
If grpc_only is True, this rule is compatible with proto_library and |
| 33 |
cc_proto_library native rules such that it expects proto_library target |
| 34 |
as srcs argument and generates only grpc library classes, expecting |
| 35 |
protobuf messages classes library (cc_proto_library target) to be passed in |
| 36 |
deps argument. By default grpc_only is False which makes this rule to behave |
| 37 |
in a backwards-compatible mode (trying to generate both proto and grpc |
| 38 |
classes). |
| 39 |
|
| 40 |
Assumes the generated classes will be used in cc_api_version = 2. |
| 41 |
|
| 42 |
Args: |
| 43 |
name (str): Name of rule. |
| 44 |
srcs (list): A single .proto file which contains services definitions, |
| 45 |
or if grpc_only parameter is True, a single proto_library which |
| 46 |
contains services descriptors. |
| 47 |
deps (list): A list of C++ proto_library (or cc_proto_library) which |
| 48 |
provides the compiled code of any message that the services depend on. |
| 49 |
proto_only (bool): If True, create only C++ proto classes library, |
| 50 |
avoid creating C++ grpc classes library (expect it in deps). |
| 51 |
Deprecated, use native cc_proto_library instead. False by default. |
| 52 |
well_known_protos (bool): Should this library additionally depend on |
| 53 |
well known protos. Deprecated, the well known protos should be |
| 54 |
specified as explicit dependencies of the proto_library target |
| 55 |
(passed in srcs parameter) instead. False by default. |
| 56 |
generate_mocks (bool): when True, Google Mock code for client stub is |
| 57 |
generated. False by default. |
| 58 |
use_external (bool): Not used. |
| 59 |
grpc_only (bool): if True, generate only grpc library, expecting |
| 60 |
protobuf messages library (cc_proto_library target) to be passed as |
| 61 |
deps. False by default (will become True by default eventually). |
| 62 |
**kwargs: rest of arguments, e.g., compatible_with and visibility |
| 63 |
""" |
| 64 |
if len(srcs) > 1: |
| 65 |
fail("Only one srcs value supported", "srcs") |
| 66 |
if grpc_only and proto_only: |
| 67 |
fail("A mutualy exclusive configuration is specified: grpc_only = True and proto_only = True") |
| 68 |
|
| 69 |
extra_deps = [] |
| 70 |
proto_targets = [] |
| 71 |
|
| 72 |
if not grpc_only: |
| 73 |
proto_target = "_" + name + "_only" |
| 74 |
cc_proto_target = name if proto_only else "_" + name + "_cc_proto" |
| 75 |
|
| 76 |
proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(":") == -1] |
| 77 |
proto_deps += [dep.split(":")[0] + ":" + "_" + dep.split(":")[1] + "_only" for dep in deps if dep.find(":") != -1] |
| 78 |
if well_known_protos: |
| 79 |
proto_deps += well_known_proto_libs() |
| 80 |
proto_library( |
| 81 |
name = proto_target, |
| 82 |
srcs = srcs, |
| 83 |
deps = proto_deps, |
| 84 |
**kwargs |
| 85 |
) |
| 86 |
|
| 87 |
native.cc_proto_library( |
| 88 |
name = cc_proto_target, |
| 89 |
deps = [":" + proto_target], |
| 90 |
**kwargs |
| 91 |
) |
| 92 |
extra_deps.append(":" + cc_proto_target) |
| 93 |
proto_targets.append(proto_target) |
| 94 |
else: |
| 95 |
if not srcs: |
| 96 |
fail("srcs cannot be empty", "srcs") |
| 97 |
proto_targets += srcs |
| 98 |
|
| 99 |
if not proto_only: |
| 100 |
codegen_grpc_target = "_" + name + "_grpc_codegen" |
| 101 |
generate_cc( |
| 102 |
name = codegen_grpc_target, |
| 103 |
srcs = proto_targets, |
| 104 |
plugin = "@com_github_grpc_grpc//src/compiler:grpc_cpp_plugin", |
| 105 |
well_known_protos = well_known_protos, |
| 106 |
generate_mocks = generate_mocks, |
| 107 |
**kwargs |
| 108 |
) |
| 109 |
|
| 110 |
native.cc_library( |
| 111 |
name = name, |
| 112 |
srcs = [":" + codegen_grpc_target], |
| 113 |
hdrs = [":" + codegen_grpc_target], |
| 114 |
deps = deps + |
| 115 |
extra_deps + |
| 116 |
["@com_github_grpc_grpc//:grpc++_codegen_proto"], |
| 117 |
**kwargs |
| 118 |
) |