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