py_proto_library.bzl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (c) 2009-2021, Google LLC
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of Google LLC nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. """An implementation of py_proto_library().
  26. We have to implement this ourselves because there is currently no reasonable
  27. py_proto_library() rule available for Bazel.
  28. Our py_proto_library() is similar to how a real py_proto_library() should work.
  29. But it hasn't been deeply tested or reviewed, and upb should not be in the
  30. business of vending py_proto_library(), so we keep it private to upb.
  31. """
  32. load("@bazel_skylib//lib:paths.bzl", "paths")
  33. load("@rules_proto//proto:defs.bzl", "ProtoInfo") # copybara:strip_for_google3
  34. # Generic support code #########################################################
  35. def _get_real_short_path(file):
  36. # For some reason, files from other archives have short paths that look like:
  37. # ../com_google_protobuf/google/protobuf/descriptor.proto
  38. short_path = file.short_path
  39. if short_path.startswith("../"):
  40. second_slash = short_path.index("/", 3)
  41. short_path = short_path[second_slash + 1:]
  42. # Sometimes it has another few prefixes like:
  43. # _virtual_imports/any_proto/google/protobuf/any.proto
  44. # benchmarks/_virtual_imports/100_msgs_proto/benchmarks/100_msgs.proto
  45. # We want just google/protobuf/any.proto.
  46. virtual_imports = "_virtual_imports/"
  47. if virtual_imports in short_path:
  48. short_path = short_path.split(virtual_imports)[1].split("/", 1)[1]
  49. return short_path
  50. def _get_real_root(file):
  51. real_short_path = _get_real_short_path(file)
  52. return file.path[:-len(real_short_path) - 1]
  53. def _generate_output_file(ctx, src, extension):
  54. real_short_path = _get_real_short_path(src)
  55. real_short_path = paths.relativize(real_short_path, ctx.label.package)
  56. output_filename = paths.replace_extension(real_short_path, extension)
  57. ret = ctx.actions.declare_file(output_filename)
  58. return ret
  59. # py_proto_library() ###########################################################
  60. def _py_proto_library_rule_impl(ctx):
  61. # A real py_proto_library() should enforce this constraint.
  62. # We don't bother for now, since it saves us some effort not to.
  63. #
  64. # if len(ctx.attr.deps) != 1:
  65. # fail("only one deps dependency allowed.")
  66. files = []
  67. for dep in ctx.attr.deps:
  68. files += dep[PyInfo].transitive_sources.to_list()
  69. return [
  70. DefaultInfo(files = depset(direct = files)),
  71. ]
  72. def _py_proto_library_aspect_impl(target, ctx):
  73. proto_info = target[ProtoInfo]
  74. proto_sources = proto_info.direct_sources
  75. srcs = [_generate_output_file(ctx, name, "_pb2.py") for name in proto_sources]
  76. transitive_sets = proto_info.transitive_descriptor_sets.to_list()
  77. ctx.actions.run(
  78. inputs = depset(
  79. direct = [proto_info.direct_descriptor_set],
  80. transitive = [proto_info.transitive_descriptor_sets],
  81. ),
  82. outputs = srcs,
  83. executable = ctx.executable._protoc,
  84. arguments = [
  85. "--python_out=" + _get_real_root(srcs[0]),
  86. "--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]),
  87. ] +
  88. [_get_real_short_path(file) for file in proto_sources],
  89. progress_message = "Generating Python protos for :" + ctx.label.name,
  90. )
  91. outs_depset = depset(srcs)
  92. return [
  93. PyInfo(transitive_sources = outs_depset)
  94. ]
  95. _py_proto_library_aspect = aspect(
  96. attrs = {
  97. "_protoc": attr.label(
  98. executable = True,
  99. cfg = "exec",
  100. default = "@com_google_protobuf//:protoc",
  101. ),
  102. },
  103. implementation = _py_proto_library_aspect_impl,
  104. provides = [
  105. PyInfo,
  106. ],
  107. attr_aspects = ["deps"],
  108. )
  109. py_proto_library = rule(
  110. output_to_genfiles = True,
  111. implementation = _py_proto_library_rule_impl,
  112. attrs = {
  113. "deps": attr.label_list(
  114. aspects = [_py_proto_library_aspect],
  115. allow_rules = ["proto_library"],
  116. providers = [ProtoInfo],
  117. ),
  118. },
  119. )