| 1 |
# Copyright 2022 The Bazel Authors. All rights reserved. |
| 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 |
""" |
| 16 |
Definition of ProtoInfo provider. |
| 17 |
""" |
| 18 |
|
| 19 |
_warning = """ Don't use this field. It's intended for internal use and will be changed or removed |
| 20 |
without warning.""" |
| 21 |
|
| 22 |
def _uniq(iterable): |
| 23 |
unique_elements = {element: None for element in iterable} |
| 24 |
return list(unique_elements.keys()) |
| 25 |
|
| 26 |
def _join(*path): |
| 27 |
return "/".join([p for p in path if p != ""]) |
| 28 |
|
| 29 |
def _empty_to_dot(path): |
| 30 |
return path if path else "." |
| 31 |
|
| 32 |
def _from_root(root, repo, relpath): |
| 33 |
"""Constructs an exec path from root to relpath""" |
| 34 |
if not root: |
| 35 |
# `relpath` is a directory with an input source file, the exec path is one of: |
| 36 |
# - when in main repo: `package/path` |
| 37 |
# - when in a external repository: `external/repo/package/path` |
| 38 |
# - with sibling layout: `../repo/package/path` |
| 39 |
return _join(repo, relpath) |
| 40 |
else: |
| 41 |
# `relpath` is a directory with a generated file or an output directory: |
| 42 |
# - when in main repo: `{root}/package/path` |
| 43 |
# - when in an external repository: `{root}/external/repo/package/path` |
| 44 |
# - with sibling layout: `{root}/package/path` |
| 45 |
return _join(root, "" if repo.startswith("../") else repo, relpath) |
| 46 |
|
| 47 |
def _create_proto_info(*, srcs, deps, descriptor_set, proto_path = "", workspace_root = "", bin_dir = None, allow_exports = None): |
| 48 |
"""Constructs ProtoInfo. |
| 49 |
|
| 50 |
Args: |
| 51 |
srcs: ([File]) List of .proto files (possibly under _virtual path) |
| 52 |
deps: ([ProtoInfo]) List of dependencies |
| 53 |
descriptor_set: (File) Descriptor set for this Proto |
| 54 |
proto_path: (str) Path that should be stripped from files in srcs. When |
| 55 |
stripping is needed, the files should be symlinked into `_virtual_imports/target_name` |
| 56 |
directory. Only such paths are accepted. |
| 57 |
workspace_root: (str) Set to ctx.workspace_root if this is not the main repository. |
| 58 |
bin_dir: (str) Set to ctx.bin_dir if _virtual_imports are used. |
| 59 |
|
| 60 |
Returns: |
| 61 |
(ProtoInfo) |
| 62 |
""" |
| 63 |
|
| 64 |
# Validate parameters |
| 65 |
src_prefix = _join(workspace_root.replace("external/", "../"), proto_path) |
| 66 |
for src in srcs: |
| 67 |
if type(src) != "File": |
| 68 |
fail("srcs parameter expects a list of Files") |
| 69 |
if src.owner.workspace_root != workspace_root: |
| 70 |
fail("srcs parameter expects all files to have the same workspace_root: ", workspace_root) |
| 71 |
if not src.short_path.startswith(src_prefix): |
| 72 |
fail("srcs parameter expects all files start with %s" % src_prefix) |
| 73 |
if type(descriptor_set) != "File": |
| 74 |
fail("descriptor_set parameter expected to be a File") |
| 75 |
if proto_path: |
| 76 |
if "_virtual_imports/" not in proto_path: |
| 77 |
fail("proto_path needs to contain '_virtual_imports' directory") |
| 78 |
if proto_path.split("/")[-2] != "_virtual_imports": |
| 79 |
fail("proto_path needs to be formed like '_virtual_imports/target_name'") |
| 80 |
if not bin_dir: |
| 81 |
fail("bin_dir parameter should be set when _virtual_imports are used") |
| 82 |
|
| 83 |
direct_proto_sources = srcs |
| 84 |
transitive_proto_sources = depset( |
| 85 |
direct = direct_proto_sources, |
| 86 |
transitive = [dep._transitive_proto_sources for dep in deps], |
| 87 |
order = "preorder", |
| 88 |
) |
| 89 |
transitive_sources = depset( |
| 90 |
direct = srcs, |
| 91 |
transitive = [dep.transitive_sources for dep in deps], |
| 92 |
order = "preorder", |
| 93 |
) |
| 94 |
|
| 95 |
# There can be up more than 1 direct proto_paths, for example when there's |
| 96 |
# a generated and non-generated .proto file in srcs |
| 97 |
root_paths = _uniq([src.root.path for src in srcs]) |
| 98 |
transitive_proto_path = depset( |
| 99 |
direct = [_empty_to_dot(_from_root(root, workspace_root, proto_path)) for root in root_paths], |
| 100 |
transitive = [dep.transitive_proto_path for dep in deps], |
| 101 |
) |
| 102 |
|
| 103 |
if srcs: |
| 104 |
check_deps_sources = depset(direct = srcs) |
| 105 |
else: |
| 106 |
check_deps_sources = depset(transitive = [dep.check_deps_sources for dep in deps]) |
| 107 |
|
| 108 |
transitive_descriptor_sets = depset( |
| 109 |
direct = [descriptor_set], |
| 110 |
transitive = [dep.transitive_descriptor_sets for dep in deps], |
| 111 |
) |
| 112 |
|
| 113 |
# Layering checks. |
| 114 |
if srcs: |
| 115 |
exported_sources = depset(direct = direct_proto_sources) |
| 116 |
else: |
| 117 |
exported_sources = depset(transitive = [dep._exported_sources for dep in deps]) |
| 118 |
|
| 119 |
if "_virtual_imports/" in proto_path: |
| 120 |
#TODO(b/281812523): remove bin_dir from proto_source_root (when users assuming it's there are migrated) |
| 121 |
proto_source_root = _empty_to_dot(_from_root(bin_dir, workspace_root, proto_path)) |
| 122 |
elif workspace_root.startswith("../"): |
| 123 |
proto_source_root = proto_path |
| 124 |
else: |
| 125 |
proto_source_root = _empty_to_dot(_join(workspace_root, proto_path)) |
| 126 |
|
| 127 |
proto_info = dict( |
| 128 |
direct_sources = srcs, |
| 129 |
transitive_sources = transitive_sources, |
| 130 |
direct_descriptor_set = descriptor_set, |
| 131 |
transitive_descriptor_sets = transitive_descriptor_sets, |
| 132 |
proto_source_root = proto_source_root, |
| 133 |
transitive_proto_path = transitive_proto_path, |
| 134 |
check_deps_sources = check_deps_sources, |
| 135 |
transitive_imports = transitive_sources, |
| 136 |
_direct_proto_sources = direct_proto_sources, |
| 137 |
_transitive_proto_sources = transitive_proto_sources, |
| 138 |
_exported_sources = exported_sources, |
| 139 |
) |
| 140 |
if allow_exports: |
| 141 |
proto_info["allow_exports"] = allow_exports |
| 142 |
return proto_info |
| 143 |
|
| 144 |
ProtoInfo, _ = provider( |
| 145 |
doc = "Encapsulates information provided by a `proto_library.`", |
| 146 |
fields = { |
| 147 |
"direct_sources": "(list[File]) The `.proto` source files from the `srcs` attribute.", |
| 148 |
"transitive_sources": """(depset[File]) The `.proto` source files from this rule and all |
| 149 |
its dependent protocol buffer rules.""", |
| 150 |
"direct_descriptor_set": """(File) The descriptor set of the direct sources. If no srcs, |
| 151 |
contains an empty file.""", |
| 152 |
"transitive_descriptor_sets": """(depset[File]) A set of descriptor set files of all |
| 153 |
dependent `proto_library` rules, and this one's. This is not the same as passing |
| 154 |
--include_imports to proto-compiler. Will be empty if no dependencies.""", |
| 155 |
"proto_source_root": """(str) The directory relative to which the `.proto` files defined in |
| 156 |
the `proto_library` are defined. For example, if this is `a/b` and the rule has the |
| 157 |
file `a/b/c/d.proto` as a source, that source file would be imported as |
| 158 |
`import c/d.proto` |
| 159 |
|
| 160 |
In principle, the `proto_source_root` directory itself should always |
| 161 |
be relative to the output directory (`ctx.bin_dir`). |
| 162 |
|
| 163 |
This is at the moment not true for `proto_libraries` using (additional and/or strip) |
| 164 |
import prefixes. `proto_source_root` is in this case prefixed with the output |
| 165 |
directory. For example, the value is similar to |
| 166 |
`bazel-out/k8-fastbuild/bin/a/_virtual_includes/b` for an input file in |
| 167 |
`a/_virtual_includes/b/c.proto` that should be imported as `c.proto`. |
| 168 |
|
| 169 |
When using the value please account for both cases in a general way. |
| 170 |
That is assume the value is either prefixed with the output directory or not. |
| 171 |
This will make it possible to fix `proto_library` in the future. |
| 172 |
""", |
| 173 |
"transitive_proto_path": """(depset(str) A set of `proto_source_root`s collected from the |
| 174 |
transitive closure of this rule.""", |
| 175 |
"check_deps_sources": """(depset[File]) The `.proto` sources from the 'srcs' attribute. |
| 176 |
If the library is a proxy library that has no sources, it contains the |
| 177 |
`check_deps_sources` from this library's direct deps.""", |
| 178 |
"allow_exports": """(Target) The packages where this proto_library can be exported.""", |
| 179 |
|
| 180 |
# Deprecated fields: |
| 181 |
"transitive_imports": """(depset[File]) Deprecated: use `transitive_sources` instead.""", |
| 182 |
|
| 183 |
# Internal fields: |
| 184 |
"_direct_proto_sources": """(list[File]) The `ProtoSourceInfo`s from the `srcs` |
| 185 |
attribute.""" + _warning, |
| 186 |
"_transitive_proto_sources": """(depset[File]) The `ProtoSourceInfo`s from this |
| 187 |
rule and all its dependent protocol buffer rules.""" + _warning, |
| 188 |
"_exported_sources": """(depset[File]) A set of `ProtoSourceInfo`s that may be |
| 189 |
imported by another `proto_library` depending on this one.""" + _warning, |
| 190 |
}, |
| 191 |
init = _create_proto_info, |
| 192 |
) |