| 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 |
"""A Starlark implementation of the proto_lang_toolchain rule.""" |
| 16 |
|
| 17 |
load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo", "toolchains", proto_common = "proto_common_do_not_use") |
| 18 |
load(":common/proto/proto_info.bzl", "ProtoInfo") |
| 19 |
load(":common/proto/proto_semantics.bzl", "semantics") |
| 20 |
|
| 21 |
PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo |
| 22 |
|
| 23 |
def _rule_impl(ctx): |
| 24 |
provided_proto_sources = depset(transitive = [bp[ProtoInfo]._transitive_proto_sources for bp in ctx.attr.blacklisted_protos]).to_list() |
| 25 |
|
| 26 |
flag = ctx.attr.command_line |
| 27 |
if flag.find("$(PLUGIN_OUT)") > -1: |
| 28 |
fail("in attribute 'command_line': Placeholder '$(PLUGIN_OUT)' is not supported.") |
| 29 |
flag = flag.replace("$(OUT)", "%s") |
| 30 |
|
| 31 |
plugin = None |
| 32 |
if ctx.attr.plugin != None: |
| 33 |
plugin = ctx.attr.plugin[DefaultInfo].files_to_run |
| 34 |
|
| 35 |
if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION: |
| 36 |
proto_compiler = ctx.toolchains[semantics.PROTO_TOOLCHAIN].proto.proto_compiler |
| 37 |
protoc_opts = ctx.toolchains[semantics.PROTO_TOOLCHAIN].proto.protoc_opts |
| 38 |
else: |
| 39 |
proto_compiler = ctx.attr._proto_compiler.files_to_run |
| 40 |
protoc_opts = ctx.fragments.proto.experimental_protoc_opts |
| 41 |
|
| 42 |
proto_lang_toolchain_info = ProtoLangToolchainInfo( |
| 43 |
out_replacement_format_flag = flag, |
| 44 |
output_files = ctx.attr.output_files, |
| 45 |
plugin_format_flag = ctx.attr.plugin_format_flag, |
| 46 |
plugin = plugin, |
| 47 |
runtime = ctx.attr.runtime, |
| 48 |
provided_proto_sources = provided_proto_sources, |
| 49 |
proto_compiler = proto_compiler, |
| 50 |
protoc_opts = protoc_opts, |
| 51 |
progress_message = ctx.attr.progress_message, |
| 52 |
mnemonic = ctx.attr.mnemonic, |
| 53 |
allowlist_different_package = ctx.attr.allowlist_different_package, |
| 54 |
toolchain_type = ctx.attr.toolchain_type.label if ctx.attr.toolchain_type else None, |
| 55 |
) |
| 56 |
return [ |
| 57 |
DefaultInfo(files = depset(), runfiles = ctx.runfiles()), |
| 58 |
_builtins.toplevel.platform_common.ToolchainInfo(proto = proto_lang_toolchain_info), |
| 59 |
# TODO(b/300592942): remove when --incompatible_enable_proto_toolchains is flipped and removed |
| 60 |
proto_lang_toolchain_info, |
| 61 |
] |
| 62 |
|
| 63 |
proto_lang_toolchain = rule( |
| 64 |
_rule_impl, |
| 65 |
attrs = |
| 66 |
{ |
| 67 |
"progress_message": attr.string(default = "Generating proto_library %{label}"), |
| 68 |
"mnemonic": attr.string(default = "GenProto"), |
| 69 |
"command_line": attr.string(mandatory = True), |
| 70 |
"output_files": attr.string(values = ["single", "multiple", "legacy"], default = "legacy"), |
| 71 |
"plugin_format_flag": attr.string(), |
| 72 |
"plugin": attr.label( |
| 73 |
executable = True, |
| 74 |
cfg = "exec", |
| 75 |
), |
| 76 |
"runtime": attr.label(), |
| 77 |
"blacklisted_protos": attr.label_list( |
| 78 |
providers = [ProtoInfo], |
| 79 |
), |
| 80 |
"allowlist_different_package": attr.label( |
| 81 |
default = semantics.allowlist_different_package, |
| 82 |
cfg = "exec", |
| 83 |
providers = [PackageSpecificationInfo], |
| 84 |
), |
| 85 |
"toolchain_type": attr.label(), |
| 86 |
} | ({} if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else { |
| 87 |
"_proto_compiler": attr.label( |
| 88 |
cfg = "exec", |
| 89 |
executable = True, |
| 90 |
allow_files = True, |
| 91 |
default = configuration_field("proto", "proto_compiler"), |
| 92 |
), |
| 93 |
}), |
| 94 |
provides = [ProtoLangToolchainInfo], |
| 95 |
fragments = ["proto"], |
| 96 |
toolchains = toolchains.use_toolchain(semantics.PROTO_TOOLCHAIN), # Used to obtain protoc |
| 97 |
) |