Add redirects for proto rules

Redirected rules: proto_library, proto_toolchain, proto_lang_toolchain
Modules and providers: proto_common, ProtoInfo, ProtoLangToolchainInfo

The structure follows proposal: https://docs.google.com/document/d/1L1JFgjpZ7SrBinb24DC_5nTIELeYDacikcme-YcA7xs/edit?usp=drive_open&ouid=102306600948089647787

PiperOrigin-RevId: 622117921
pull/16323/head
Protobuf Team Bot 2024-04-05 02:39:21 -07:00 committed by Copybara-Service
parent 9a61f11fa5
commit d4d34abd7d
10 changed files with 130 additions and 0 deletions

0
bazel/common/BUILD.bazel Normal file
View File

View File

@ -0,0 +1,5 @@
"""proto_common"""
load("//bazel/private:native.bzl", "native_proto_common")
proto_common = native_proto_common

View File

@ -0,0 +1,5 @@
"""ProtoInfo"""
load("//bazel/private:native.bzl", "NativeProtoInfo")
ProtoInfo = NativeProtoInfo

View File

@ -0,0 +1,5 @@
"""ProtoLangToolchainInfo"""
load("//bazel/common:proto_common.bzl", "proto_common")
ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo

5
bazel/private/native.bzl Normal file
View File

@ -0,0 +1,5 @@
"""Renames toplevel symbols so they can be exported in Starlark under the same name"""
NativeProtoInfo = ProtoInfo
native_proto_common = proto_common_do_not_use

View File

@ -0,0 +1,47 @@
"""A Starlark implementation of the proto_toolchain rule."""
load("//bazel/common:proto_common.bzl", "proto_common")
load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo")
def _impl(ctx):
kwargs = {}
if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False):
kwargs["toolchain_type"] = "//third_party/bazel_rules/rules_proto/proto:toolchain_type"
return [
DefaultInfo(
files = depset(),
runfiles = ctx.runfiles(),
),
platform_common.ToolchainInfo(
proto = ProtoLangToolchainInfo(
out_replacement_format_flag = ctx.attr.command_line,
output_files = ctx.attr.output_files,
plugin = None,
runtime = None,
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,
**kwargs
),
),
]
proto_toolchain = rule(
_impl,
attrs =
{
"progress_message": attr.string(default = "Generating Descriptor Set proto_library %{label}"),
"mnemonic": attr.string(default = "GenProtoDescriptorSet"),
"command_line": attr.string(default = "--descriptor_set_out=%s"),
"output_files": attr.string(values = ["single", "multiple", "legacy"], default = "single"),
"proto_compiler": attr.label(
cfg = "exec",
executable = True,
allow_files = True, # Used by mocks in tests. Consider fixing tests and removing it.
),
},
provides = [platform_common.ToolchainInfo],
fragments = ["proto"],
)

3
bazel/proto_library.bzl Normal file
View File

@ -0,0 +1,3 @@
"""proto_library rule"""
proto_library = native.proto_library

View File

View File

@ -0,0 +1,34 @@
"""proto_lang_toolchain rule"""
load("//bazel/common:proto_common.bzl", "proto_common")
def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs):
"""Creates a proto_lang_toolchain and corresponding toolchain target.
Toolchain target is only created when toolchain_type is set.
https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_lang_toolchain
Args:
name: name of the toolchain
toolchain_type: The toolchain type
exec_compatible_with: ([constraints]) List of constraints the prebuild binaries is compatible with.
target_compatible_with: ([constraints]) List of constraints the target libraries are compatible with.
**attrs: Rule attributes
"""
if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False):
attrs["toolchain_type"] = toolchain_type
# buildifier: disable=native-proto
native.proto_lang_toolchain(name = name, **attrs)
if toolchain_type:
native.toolchain(
name = name + "_toolchain",
toolchain_type = toolchain_type,
exec_compatible_with = exec_compatible_with,
target_compatible_with = target_compatible_with,
toolchain = name,
)

View File

@ -0,0 +1,26 @@
"""Macro wrapping the proto_toolchain implementation.
The macro additionally creates toolchain target when toolchain_type is given.
"""
load("//bazel/private:proto_toolchain_rule.bzl", _proto_toolchain_rule = "proto_toolchain")
def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []):
"""Creates a proto_toolchain and toolchain target for proto_library.
Toolchain target is suffixed with "_toolchain".
Args:
name: name of the toolchain
proto_compiler: (Label) of either proto compiler sources or prebuild binaries
exec_compatible_with: ([constraints]) List of constraints the prebuild binary is compatible with.
"""
_proto_toolchain_rule(name = name, proto_compiler = proto_compiler)
native.toolchain(
name = name + "_toolchain",
toolchain_type = "//third_party/bazel_rules/rules_proto/proto:toolchain_type",
exec_compatible_with = exec_compatible_with,
target_compatible_with = [],
toolchain = name,
)