cc_grpc_library.bzl 4.8 KB

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