| 1 |
# Copyright 2021 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 proto_library rule. |
| 17 |
""" |
| 18 |
|
| 19 |
load(":common/paths.bzl", "paths") |
| 20 |
load(":common/proto/proto_common.bzl", "toolchains", proto_common = "proto_common_do_not_use") |
| 21 |
load(":common/proto/proto_info.bzl", "ProtoInfo") |
| 22 |
load(":common/proto/proto_semantics.bzl", "semantics") |
| 23 |
|
| 24 |
PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo |
| 25 |
|
| 26 |
def _check_srcs_package(target_package, srcs): |
| 27 |
"""Check that .proto files in sources are from the same package. |
| 28 |
|
| 29 |
This is done to avoid clashes with the generated sources.""" |
| 30 |
|
| 31 |
#TODO(bazel-team): this does not work with filegroups that contain files that are not in the package |
| 32 |
for src in srcs: |
| 33 |
if target_package != src.label.package: |
| 34 |
fail("Proto source with label '%s' must be in same package as consuming rule." % src.label) |
| 35 |
|
| 36 |
def _get_import_prefix(ctx): |
| 37 |
"""Gets and verifies import_prefix attribute if it is declared.""" |
| 38 |
|
| 39 |
import_prefix = ctx.attr.import_prefix |
| 40 |
|
| 41 |
if not paths.is_normalized(import_prefix): |
| 42 |
fail("should be normalized (without uplevel references or '.' path segments)", attr = "import_prefix") |
| 43 |
if paths.is_absolute(import_prefix): |
| 44 |
fail("should be a relative path", attr = "import_prefix") |
| 45 |
|
| 46 |
return import_prefix |
| 47 |
|
| 48 |
def _get_strip_import_prefix(ctx): |
| 49 |
"""Gets and verifies strip_import_prefix.""" |
| 50 |
|
| 51 |
strip_import_prefix = ctx.attr.strip_import_prefix |
| 52 |
|
| 53 |
if not paths.is_normalized(strip_import_prefix): |
| 54 |
fail("should be normalized (without uplevel references or '.' path segments)", attr = "strip_import_prefix") |
| 55 |
|
| 56 |
if paths.is_absolute(strip_import_prefix): |
| 57 |
strip_import_prefix = strip_import_prefix[1:] |
| 58 |
else: # Relative to current package |
| 59 |
strip_import_prefix = _join(ctx.label.package, strip_import_prefix) |
| 60 |
|
| 61 |
return strip_import_prefix.removesuffix("/") |
| 62 |
|
| 63 |
def _proto_library_impl(ctx): |
| 64 |
# Verifies attributes. |
| 65 |
_check_srcs_package(ctx.label.package, ctx.attr.srcs) |
| 66 |
srcs = ctx.files.srcs |
| 67 |
deps = [dep[ProtoInfo] for dep in ctx.attr.deps] |
| 68 |
exports = [dep[ProtoInfo] for dep in ctx.attr.exports] |
| 69 |
import_prefix = _get_import_prefix(ctx) |
| 70 |
strip_import_prefix = _get_strip_import_prefix(ctx) |
| 71 |
check_for_reexport = deps + exports if not srcs else exports |
| 72 |
for proto in check_for_reexport: |
| 73 |
if hasattr(proto, "allow_exports") and not proto.allow_exports[PackageSpecificationInfo].contains(ctx.label): |
| 74 |
fail("proto_library '%s' can't be reexported in package '//%s'" % (proto.direct_descriptor_set.owner, ctx.label.package)) |
| 75 |
|
| 76 |
proto_path, virtual_srcs = _process_srcs(ctx, srcs, import_prefix, strip_import_prefix) |
| 77 |
descriptor_set = ctx.actions.declare_file(ctx.label.name + "-descriptor-set.proto.bin") |
| 78 |
proto_info = ProtoInfo( |
| 79 |
srcs = virtual_srcs, |
| 80 |
deps = deps, |
| 81 |
descriptor_set = descriptor_set, |
| 82 |
proto_path = proto_path, |
| 83 |
workspace_root = ctx.label.workspace_root, |
| 84 |
bin_dir = ctx.bin_dir.path, |
| 85 |
allow_exports = ctx.attr.allow_exports, |
| 86 |
) |
| 87 |
|
| 88 |
_write_descriptor_set(ctx, proto_info, deps, exports, descriptor_set) |
| 89 |
|
| 90 |
# We assume that the proto sources will not have conflicting artifacts |
| 91 |
# with the same root relative path |
| 92 |
data_runfiles = ctx.runfiles( |
| 93 |
files = [proto_info.direct_descriptor_set], |
| 94 |
transitive_files = depset(transitive = [proto_info.transitive_sources]), |
| 95 |
) |
| 96 |
return [ |
| 97 |
proto_info, |
| 98 |
DefaultInfo( |
| 99 |
files = depset([proto_info.direct_descriptor_set]), |
| 100 |
default_runfiles = ctx.runfiles(), # empty |
| 101 |
data_runfiles = data_runfiles, |
| 102 |
), |
| 103 |
] |
| 104 |
|
| 105 |
def _process_srcs(ctx, srcs, import_prefix, strip_import_prefix): |
| 106 |
"""Returns proto_path and sources, optionally symlinking them to _virtual_imports. |
| 107 |
|
| 108 |
Returns: |
| 109 |
(str, [File]) A pair of proto_path and virtual_sources. |
| 110 |
""" |
| 111 |
if import_prefix != "" or strip_import_prefix != "": |
| 112 |
# Use virtual source roots |
| 113 |
return _symlink_to_virtual_imports(ctx, srcs, import_prefix, strip_import_prefix) |
| 114 |
else: |
| 115 |
# No virtual source roots |
| 116 |
return "", srcs |
| 117 |
|
| 118 |
def _join(*path): |
| 119 |
return "/".join([p for p in path if p != ""]) |
| 120 |
|
| 121 |
def _symlink_to_virtual_imports(ctx, srcs, import_prefix, strip_import_prefix): |
| 122 |
"""Symlinks srcs to _virtual_imports. |
| 123 |
|
| 124 |
Returns: |
| 125 |
A pair proto_path, directs_sources. |
| 126 |
""" |
| 127 |
virtual_imports = _join("_virtual_imports", ctx.label.name) |
| 128 |
proto_path = _join(ctx.label.package, virtual_imports) |
| 129 |
|
| 130 |
if ctx.label.workspace_name == "": |
| 131 |
full_strip_import_prefix = strip_import_prefix |
| 132 |
else: |
| 133 |
full_strip_import_prefix = _join("..", ctx.label.workspace_name, strip_import_prefix) |
| 134 |
if full_strip_import_prefix: |
| 135 |
full_strip_import_prefix += "/" |
| 136 |
|
| 137 |
virtual_srcs = [] |
| 138 |
for src in srcs: |
| 139 |
# Remove strip_import_prefix |
| 140 |
if not src.short_path.startswith(full_strip_import_prefix): |
| 141 |
fail(".proto file '%s' is not under the specified strip prefix '%s'" % |
| 142 |
(src.short_path, full_strip_import_prefix)) |
| 143 |
import_path = src.short_path[len(full_strip_import_prefix):] |
| 144 |
|
| 145 |
# Add import_prefix |
| 146 |
virtual_src = ctx.actions.declare_file(_join(virtual_imports, import_prefix, import_path)) |
| 147 |
ctx.actions.symlink( |
| 148 |
output = virtual_src, |
| 149 |
target_file = src, |
| 150 |
progress_message = "Symlinking virtual .proto sources for %{label}", |
| 151 |
) |
| 152 |
virtual_srcs.append(virtual_src) |
| 153 |
return proto_path, virtual_srcs |
| 154 |
|
| 155 |
def _write_descriptor_set(ctx, proto_info, deps, exports, descriptor_set): |
| 156 |
"""Writes descriptor set.""" |
| 157 |
if proto_info.direct_sources == []: |
| 158 |
ctx.actions.write(descriptor_set, "") |
| 159 |
return |
| 160 |
|
| 161 |
dependencies_descriptor_sets = depset(transitive = [dep.transitive_descriptor_sets for dep in deps]) |
| 162 |
|
| 163 |
args = ctx.actions.args() |
| 164 |
if ctx.fragments.proto.experimental_proto_descriptorsets_include_source_info(): |
| 165 |
args.add("--include_source_info") |
| 166 |
if hasattr(ctx.attr, "_retain_options") and ctx.attr._retain_options: |
| 167 |
args.add("--retain_options") |
| 168 |
|
| 169 |
strict_deps_mode = ctx.fragments.proto.strict_proto_deps() |
| 170 |
strict_deps = strict_deps_mode != "OFF" and strict_deps_mode != "DEFAULT" |
| 171 |
if strict_deps: |
| 172 |
if proto_info.direct_sources: |
| 173 |
strict_importable_sources = depset( |
| 174 |
direct = proto_info._direct_proto_sources, |
| 175 |
transitive = [dep._exported_sources for dep in deps], |
| 176 |
) |
| 177 |
else: |
| 178 |
strict_importable_sources = None |
| 179 |
if strict_importable_sources: |
| 180 |
args.add_joined( |
| 181 |
"--direct_dependencies", |
| 182 |
strict_importable_sources, |
| 183 |
map_each = proto_common.get_import_path, |
| 184 |
join_with = ":", |
| 185 |
) |
| 186 |
# Example: `--direct_dependencies a.proto:b.proto` |
| 187 |
|
| 188 |
else: |
| 189 |
# The proto compiler requires an empty list to turn on strict deps checking |
| 190 |
args.add("--direct_dependencies=") |
| 191 |
|
| 192 |
# Set `-direct_dependencies_violation_msg=` |
| 193 |
args.add(ctx.label, format = semantics.STRICT_DEPS_FLAG_TEMPLATE) |
| 194 |
|
| 195 |
strict_public_imports_mode = ctx.fragments.proto.strict_public_imports() |
| 196 |
strict_imports = strict_public_imports_mode != "OFF" and strict_public_imports_mode != "DEFAULT" |
| 197 |
if strict_imports: |
| 198 |
public_import_protos = depset(transitive = [export._exported_sources for export in exports]) |
| 199 |
if not public_import_protos: |
| 200 |
# This line is necessary to trigger the check. |
| 201 |
args.add("--allowed_public_imports=") |
| 202 |
else: |
| 203 |
args.add_joined( |
| 204 |
"--allowed_public_imports", |
| 205 |
public_import_protos, |
| 206 |
map_each = proto_common.get_import_path, |
| 207 |
join_with = ":", |
| 208 |
) |
| 209 |
if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION: |
| 210 |
toolchain = ctx.toolchains[semantics.PROTO_TOOLCHAIN] |
| 211 |
if not toolchain: |
| 212 |
fail("Protocol compiler toolchain could not be resolved.") |
| 213 |
proto_lang_toolchain_info = toolchain.proto |
| 214 |
else: |
| 215 |
proto_lang_toolchain_info = proto_common.ProtoLangToolchainInfo( |
| 216 |
out_replacement_format_flag = "--descriptor_set_out=%s", |
| 217 |
output_files = "single", |
| 218 |
mnemonic = "GenProtoDescriptorSet", |
| 219 |
progress_message = "Generating Descriptor Set proto_library %{label}", |
| 220 |
proto_compiler = ctx.executable._proto_compiler, |
| 221 |
protoc_opts = ctx.fragments.proto.experimental_protoc_opts, |
| 222 |
plugin = None, |
| 223 |
) |
| 224 |
|
| 225 |
proto_common.compile( |
| 226 |
ctx.actions, |
| 227 |
proto_info, |
| 228 |
proto_lang_toolchain_info, |
| 229 |
generated_files = [descriptor_set], |
| 230 |
additional_inputs = dependencies_descriptor_sets, |
| 231 |
additional_args = args, |
| 232 |
) |
| 233 |
|
| 234 |
proto_library = rule( |
| 235 |
_proto_library_impl, |
| 236 |
attrs = { |
| 237 |
"srcs": attr.label_list( |
| 238 |
allow_files = [".proto", ".protodevel"], |
| 239 |
flags = ["DIRECT_COMPILE_TIME_INPUT"], |
| 240 |
), |
| 241 |
"deps": attr.label_list( |
| 242 |
providers = [ProtoInfo], |
| 243 |
), |
| 244 |
"exports": attr.label_list( |
| 245 |
providers = [ProtoInfo], |
| 246 |
), |
| 247 |
"strip_import_prefix": attr.string(default = "/"), |
| 248 |
"import_prefix": attr.string(), |
| 249 |
"allow_exports": attr.label( |
| 250 |
cfg = "exec", |
| 251 |
providers = [PackageSpecificationInfo], |
| 252 |
), |
| 253 |
"data": attr.label_list( |
| 254 |
allow_files = True, |
| 255 |
flags = ["SKIP_CONSTRAINTS_OVERRIDE"], |
| 256 |
), |
| 257 |
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(), |
| 258 |
} | toolchains.if_legacy_toolchain({ |
| 259 |
"_proto_compiler": attr.label( |
| 260 |
cfg = "exec", |
| 261 |
executable = True, |
| 262 |
allow_files = True, |
| 263 |
default = configuration_field("proto", "proto_compiler"), |
| 264 |
), |
| 265 |
}), |
| 266 |
fragments = ["proto"], |
| 267 |
provides = [ProtoInfo], |
| 268 |
toolchains = toolchains.use_toolchain(semantics.PROTO_TOOLCHAIN), |
| 269 |
) |