| 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 |
|
| 26 |
"""An implementation of py_proto_library(). |
| 27 |
|
| 28 |
We have to implement this ourselves because there is currently no reasonable |
| 29 |
py_proto_library() rule available for Bazel. |
| 30 |
|
| 31 |
Our py_proto_library() is similar to how a real py_proto_library() should work. |
| 32 |
But it hasn't been deeply tested or reviewed, and upb should not be in the |
| 33 |
business of vending py_proto_library(), so we keep it private to upb. |
| 34 |
""" |
| 35 |
|
| 36 |
load("@bazel_skylib//lib:paths.bzl", "paths") |
| 37 |
|
| 38 |
# begin:github_only |
| 39 |
load("@rules_proto//proto:defs.bzl", "ProtoInfo") |
| 40 |
# end:github_only |
| 41 |
|
| 42 |
# Generic support code ######################################################### |
| 43 |
|
| 44 |
def _get_real_short_path(file): |
| 45 |
# For some reason, files from other archives have short paths that look like: |
| 46 |
# ../com_google_protobuf/google/protobuf/descriptor.proto |
| 47 |
short_path = file.short_path |
| 48 |
if short_path.startswith("../"): |
| 49 |
second_slash = short_path.index("/", 3) |
| 50 |
short_path = short_path[second_slash + 1:] |
| 51 |
|
| 52 |
# Sometimes it has another few prefixes like: |
| 53 |
# _virtual_imports/any_proto/google/protobuf/any.proto |
| 54 |
# benchmarks/_virtual_imports/100_msgs_proto/benchmarks/100_msgs.proto |
| 55 |
# We want just google/protobuf/any.proto. |
| 56 |
virtual_imports = "_virtual_imports/" |
| 57 |
if virtual_imports in short_path: |
| 58 |
short_path = short_path.split(virtual_imports)[1].split("/", 1)[1] |
| 59 |
return short_path |
| 60 |
|
| 61 |
def _get_real_root(file): |
| 62 |
real_short_path = _get_real_short_path(file) |
| 63 |
return file.path[:-len(real_short_path) - 1] |
| 64 |
|
| 65 |
def _generate_output_file(ctx, src, extension): |
| 66 |
real_short_path = _get_real_short_path(src) |
| 67 |
real_short_path = paths.relativize(real_short_path, ctx.label.package) |
| 68 |
output_filename = paths.replace_extension(real_short_path, extension) |
| 69 |
ret = ctx.actions.declare_file(output_filename) |
| 70 |
return ret |
| 71 |
|
| 72 |
# py_proto_library() ########################################################### |
| 73 |
|
| 74 |
def _py_proto_library_rule_impl(ctx): |
| 75 |
# A real py_proto_library() should enforce this constraint. |
| 76 |
# We don't bother for now, since it saves us some effort not to. |
| 77 |
# |
| 78 |
# if len(ctx.attr.deps) != 1: |
| 79 |
# fail("only one deps dependency allowed.") |
| 80 |
|
| 81 |
files = [] |
| 82 |
for dep in ctx.attr.deps: |
| 83 |
files += dep[PyInfo].transitive_sources.to_list() |
| 84 |
return [ |
| 85 |
DefaultInfo(files = depset(direct = files)), |
| 86 |
] |
| 87 |
|
| 88 |
def _py_proto_library_aspect_impl(target, ctx): |
| 89 |
proto_info = target[ProtoInfo] |
| 90 |
proto_sources = proto_info.direct_sources |
| 91 |
srcs = [_generate_output_file(ctx, name, "_pb2.py") for name in proto_sources] |
| 92 |
transitive_sets = proto_info.transitive_descriptor_sets.to_list() |
| 93 |
ctx.actions.run( |
| 94 |
inputs = depset( |
| 95 |
direct = [proto_info.direct_descriptor_set], |
| 96 |
transitive = [proto_info.transitive_descriptor_sets], |
| 97 |
), |
| 98 |
outputs = srcs, |
| 99 |
executable = ctx.executable._protoc, |
| 100 |
arguments = [ |
| 101 |
"--python_out=" + _get_real_root(srcs[0]), |
| 102 |
"--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]), |
| 103 |
] + |
| 104 |
[_get_real_short_path(file) for file in proto_sources], |
| 105 |
progress_message = "Generating Python protos for :" + ctx.label.name, |
| 106 |
) |
| 107 |
outs_depset = depset(srcs) |
| 108 |
return [ |
| 109 |
PyInfo(transitive_sources = outs_depset), |
| 110 |
] |
| 111 |
|
| 112 |
_py_proto_library_aspect = aspect( |
| 113 |
attrs = { |
| 114 |
"_protoc": attr.label( |
| 115 |
executable = True, |
| 116 |
cfg = "exec", |
| 117 |
default = "@com_google_protobuf//:protoc", |
| 118 |
), |
| 119 |
}, |
| 120 |
implementation = _py_proto_library_aspect_impl, |
| 121 |
provides = [ |
| 122 |
PyInfo, |
| 123 |
], |
| 124 |
attr_aspects = ["deps"], |
| 125 |
) |
| 126 |
|
| 127 |
py_proto_library = rule( |
| 128 |
output_to_genfiles = True, |
| 129 |
implementation = _py_proto_library_rule_impl, |
| 130 |
attrs = { |
| 131 |
"deps": attr.label_list( |
| 132 |
aspects = [_py_proto_library_aspect], |
| 133 |
allow_rules = ["proto_library"], |
| 134 |
providers = [ProtoInfo], |
| 135 |
), |
| 136 |
}, |
| 137 |
) |