grpc.bzl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright 2015 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. """
  15. Bazel macros to declare gRPC libraries automatically generated from proto files.
  16. This file declares two macros:
  17. - objc_proto_library
  18. - objc_grpc_library
  19. """
  20. def _lower_underscore_to_upper_camel(str):
  21. humps = []
  22. for hump in str.split("_"):
  23. humps.append(hump[0].upper() + hump[1:])
  24. return "".join(humps)
  25. def _file_to_upper_camel(src):
  26. elements = src.rpartition("/")
  27. upper_camel = _lower_underscore_to_upper_camel(elements[-1])
  28. return "".join(elements[:-1] + [upper_camel])
  29. def _file_with_extension(src, ext):
  30. elements = src.rpartition("/")
  31. basename = elements[-1].partition(".")[0]
  32. return "".join(elements[:-1] + [basename, ext])
  33. def _protoc_invocation(srcs, flags):
  34. """Returns a CLI command to invoke protoc.
  35. Uses the given sources and flags. Suitable for use in a genrule.
  36. """
  37. protoc_command = "$(location //external:protoc) -I . "
  38. srcs_params = ""
  39. for src in srcs:
  40. srcs_params += " $(location %s)" % (src)
  41. return protoc_command + flags + srcs_params
  42. def objc_proto_library(name, srcs, visibility = None):
  43. """Declares an objc_library for the code generated by protoc.
  44. Uses the given proto sources. This generated code doesn't include proto
  45. services.
  46. Args:
  47. name: The name of the library.
  48. srcs: A list of .proto file sources.
  49. visibility: The visibility label to apply to the target.
  50. """
  51. h_files = []
  52. m_files = []
  53. for src in srcs:
  54. src = _file_to_upper_camel(src)
  55. h_files.append(_file_with_extension(src, ".pbobjc.h"))
  56. m_files.append(_file_with_extension(src, ".pbobjc.m"))
  57. protoc_flags = "--objc_out=$(GENDIR)"
  58. native.genrule(
  59. name = name + "_codegen",
  60. srcs = srcs + ["//external:protoc"],
  61. outs = h_files + m_files,
  62. cmd = _protoc_invocation(srcs, protoc_flags),
  63. )
  64. native.objc_library(
  65. name = name,
  66. hdrs = h_files,
  67. includes = ["."],
  68. non_arc_srcs = m_files,
  69. deps = ["//external:protobuf_objc"],
  70. visibility = visibility,
  71. )
  72. def objc_grpc_library(name, services, other_messages, visibility = None):
  73. """Declares an objc_library for the code generated by the gRPC ObjC plugin.
  74. The generated code does not include the services of the files in other_messages.
  75. Args:
  76. name: The name of the library.
  77. services: The .proto files from which to generate the library.
  78. other_messages: A list of .proto files containing messages needed for the library.
  79. visibility: The visibility label to apply to the library.
  80. """
  81. objc_proto_library(name + "_messages", services + other_messages)
  82. h_files = []
  83. m_files = []
  84. for src in services:
  85. src = _file_to_upper_camel(src)
  86. h_files.append(_file_with_extension(src, ".pbrpc.h"))
  87. m_files.append(_file_with_extension(src, ".pbrpc.m"))
  88. protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" +
  89. "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)")
  90. native.genrule(
  91. name = name + "_codegen",
  92. srcs = services + [
  93. "//external:grpc_protoc_plugin_objc",
  94. "//external:protoc",
  95. ],
  96. outs = h_files + m_files,
  97. cmd = _protoc_invocation(services, protoc_flags),
  98. )
  99. native.objc_library(
  100. name = name,
  101. hdrs = h_files,
  102. includes = ["."],
  103. srcs = m_files,
  104. deps = [
  105. ":" + name + "_messages",
  106. "//external:proto_objc_rpc",
  107. ],
  108. visibility = visibility,
  109. )