-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starlarkify proto_lang_toolchain and ProtoLangToolchainInfo provider
Added StarlarkProtoLangToolchainTest which uses starlarkified rule for verification. I've deleted blacklisted_protos and forbidden_protos since they are not needed anymore. PiperOrigin-RevId: 444223497
- Loading branch information
1 parent
deb2d16
commit 1e9a5de
Showing
7 changed files
with
341 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright 2021 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""A Starlark implementation of the proto_lang_toolchain rule.""" | ||
|
||
load(":common/proto/providers.bzl", "ProtoLangToolchainInfo") | ||
load(":common/proto/proto_semantics.bzl", "semantics") | ||
|
||
ProtoInfo = _builtins.toplevel.ProtoInfo | ||
proto_common = _builtins.toplevel.proto_common | ||
|
||
def _rule_impl(ctx): | ||
provided_proto_sources = [] | ||
transitive_files = depset(transitive = [bp[ProtoInfo].transitive_sources for bp in ctx.attr.blacklisted_protos]) | ||
for file in transitive_files.to_list(): | ||
source_root = file.root.path | ||
provided_proto_sources.append(proto_common.ProtoSource(file, file, source_root)) | ||
|
||
flag = ctx.attr.command_line | ||
if flag.find("$(PLUGIN_OUT)") > -1: | ||
fail("in attribute 'command_line': Placeholder '$(PLUGIN_OUT)' is not supported.") | ||
flag = flag.replace("$(OUT)", "%s") | ||
|
||
plugin = None | ||
if ctx.attr.plugin != None: | ||
plugin = ctx.attr.plugin[DefaultInfo].files_to_run | ||
|
||
return [ | ||
DefaultInfo( | ||
files = depset(), | ||
runfiles = ctx.runfiles(), | ||
), | ||
ProtoLangToolchainInfo( | ||
out_replacement_format_flag = flag, | ||
plugin_format_flag = ctx.attr.plugin_format_flag, | ||
plugin = plugin, | ||
runtime = ctx.attr.runtime, | ||
provided_proto_sources = provided_proto_sources, | ||
proto_compiler = ctx.attr._proto_compiler.files_to_run, | ||
protoc_opts = ctx.fragments.proto.experimental_protoc_opts, | ||
progress_message = ctx.attr.progress_message, | ||
mnemonic = ctx.attr.mnemonic, | ||
), | ||
] | ||
|
||
proto_lang_toolchain = rule( | ||
implementation = _rule_impl, | ||
attrs = { | ||
"progress_message": attr.string(default = "Generating proto_library %{label}"), | ||
"mnemonic": attr.string(default = "GenProto"), | ||
"command_line": attr.string(mandatory = True), | ||
"plugin_format_flag": attr.string(), | ||
"plugin": attr.label( | ||
executable = True, | ||
cfg = "exec", | ||
allow_files = True, | ||
), | ||
"runtime": attr.label( | ||
allow_files = True, | ||
), | ||
"blacklisted_protos": attr.label_list( | ||
allow_files = True, | ||
providers = [ProtoInfo], | ||
), | ||
"_proto_compiler": attr.label( | ||
cfg = "exec", | ||
executable = True, | ||
allow_files = True, | ||
default = configuration_field("proto", "proto_compiler"), | ||
), | ||
}, | ||
provides = [ProtoLangToolchainInfo], | ||
fragments = ["proto"] + semantics.EXTRA_FRAGMENTS, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2021 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Bazel providers for proto rules.""" | ||
|
||
ProtoLangToolchainInfo = provider( | ||
doc = "Specifies how to generate language-specific code from .proto files. Used by LANG_proto_library rules.", | ||
fields = dict( | ||
out_replacement_format_flag = "(str) Format string used when passing output to the plugin used by proto compiler.", | ||
plugin_format_flag = "(str) Format string used when passing plugin to proto compiler.", | ||
plugin = "(FilesToRunProvider) Proto compiler plugin.", | ||
runtime = "(Target) Runtime.", | ||
provided_proto_sources = "(list[ProtoSource]) Proto sources provided by the toolchain.", | ||
proto_compiler = "(FilesToRunProvider) Proto compiler.", | ||
protoc_opts = "(list[str]) Options to pass to proto compiler.", | ||
progress_message = "(str) Progress message to set on the proto compiler action.", | ||
mnemonic = "(str) Mnemonic to set on the proto compiler action.", | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.