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 16load("@rules_proto//proto:defs.bzl", "proto_library") 17load("//bazel:generate_cc.bzl", "generate_cc") 18load("//bazel:protobuf.bzl", "well_known_proto_libs") 19 20def 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 and dep.find("com_google_googleapis") == -1] 78 proto_deps += [dep for dep in deps if dep.find("com_google_googleapis") != -1] 79 if well_known_protos: 80 proto_deps += well_known_proto_libs() 81 proto_library( 82 name = proto_target, 83 srcs = srcs, 84 deps = proto_deps, 85 **kwargs 86 ) 87 88 native.cc_proto_library( 89 name = cc_proto_target, 90 deps = [":" + proto_target], 91 **kwargs 92 ) 93 extra_deps.append(":" + cc_proto_target) 94 proto_targets.append(proto_target) 95 else: 96 if not srcs: 97 fail("srcs cannot be empty", "srcs") 98 proto_targets += srcs 99 100 if not proto_only: 101 codegen_grpc_target = "_" + name + "_grpc_codegen" 102 generate_cc( 103 name = codegen_grpc_target, 104 srcs = proto_targets, 105 plugin = "@com_github_grpc_grpc//src/compiler:grpc_cpp_plugin", 106 well_known_protos = well_known_protos, 107 generate_mocks = generate_mocks, 108 **kwargs 109 ) 110 111 native.cc_library( 112 name = name, 113 srcs = [":" + codegen_grpc_target], 114 hdrs = [":" + codegen_grpc_target], 115 deps = deps + 116 extra_deps + 117 ["@com_github_grpc_grpc//:grpc++_codegen_proto"], 118 **kwargs 119 ) 120