Skip to content

Commit d4d34ab

Browse files
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
1 parent 9a61f11 commit d4d34ab

10 files changed

Lines changed: 130 additions & 0 deletions

bazel/common/BUILD.bazel

Whitespace-only changes.

bazel/common/proto_common.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""proto_common"""
2+
3+
load("//bazel/private:native.bzl", "native_proto_common")
4+
5+
proto_common = native_proto_common

bazel/common/proto_info.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""ProtoInfo"""
2+
3+
load("//bazel/private:native.bzl", "NativeProtoInfo")
4+
5+
ProtoInfo = NativeProtoInfo
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""ProtoLangToolchainInfo"""
2+
3+
load("//bazel/common:proto_common.bzl", "proto_common")
4+
5+
ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo

bazel/private/native.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Renames toplevel symbols so they can be exported in Starlark under the same name"""
2+
3+
NativeProtoInfo = ProtoInfo
4+
5+
native_proto_common = proto_common_do_not_use
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""A Starlark implementation of the proto_toolchain rule."""
2+
3+
load("//bazel/common:proto_common.bzl", "proto_common")
4+
load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo")
5+
6+
def _impl(ctx):
7+
kwargs = {}
8+
if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False):
9+
kwargs["toolchain_type"] = "//third_party/bazel_rules/rules_proto/proto:toolchain_type"
10+
11+
return [
12+
DefaultInfo(
13+
files = depset(),
14+
runfiles = ctx.runfiles(),
15+
),
16+
platform_common.ToolchainInfo(
17+
proto = ProtoLangToolchainInfo(
18+
out_replacement_format_flag = ctx.attr.command_line,
19+
output_files = ctx.attr.output_files,
20+
plugin = None,
21+
runtime = None,
22+
proto_compiler = ctx.attr.proto_compiler.files_to_run,
23+
protoc_opts = ctx.fragments.proto.experimental_protoc_opts,
24+
progress_message = ctx.attr.progress_message,
25+
mnemonic = ctx.attr.mnemonic,
26+
**kwargs
27+
),
28+
),
29+
]
30+
31+
proto_toolchain = rule(
32+
_impl,
33+
attrs =
34+
{
35+
"progress_message": attr.string(default = "Generating Descriptor Set proto_library %{label}"),
36+
"mnemonic": attr.string(default = "GenProtoDescriptorSet"),
37+
"command_line": attr.string(default = "--descriptor_set_out=%s"),
38+
"output_files": attr.string(values = ["single", "multiple", "legacy"], default = "single"),
39+
"proto_compiler": attr.label(
40+
cfg = "exec",
41+
executable = True,
42+
allow_files = True, # Used by mocks in tests. Consider fixing tests and removing it.
43+
),
44+
},
45+
provides = [platform_common.ToolchainInfo],
46+
fragments = ["proto"],
47+
)

bazel/proto_library.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""proto_library rule"""
2+
3+
proto_library = native.proto_library

bazel/toolchains/BUILD.bazel

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""proto_lang_toolchain rule"""
2+
3+
load("//bazel/common:proto_common.bzl", "proto_common")
4+
5+
def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs):
6+
"""Creates a proto_lang_toolchain and corresponding toolchain target.
7+
8+
Toolchain target is only created when toolchain_type is set.
9+
10+
https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_lang_toolchain
11+
12+
Args:
13+
14+
name: name of the toolchain
15+
toolchain_type: The toolchain type
16+
exec_compatible_with: ([constraints]) List of constraints the prebuild binaries is compatible with.
17+
target_compatible_with: ([constraints]) List of constraints the target libraries are compatible with.
18+
**attrs: Rule attributes
19+
"""
20+
21+
if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False):
22+
attrs["toolchain_type"] = toolchain_type
23+
24+
# buildifier: disable=native-proto
25+
native.proto_lang_toolchain(name = name, **attrs)
26+
27+
if toolchain_type:
28+
native.toolchain(
29+
name = name + "_toolchain",
30+
toolchain_type = toolchain_type,
31+
exec_compatible_with = exec_compatible_with,
32+
target_compatible_with = target_compatible_with,
33+
toolchain = name,
34+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Macro wrapping the proto_toolchain implementation.
2+
3+
The macro additionally creates toolchain target when toolchain_type is given.
4+
"""
5+
6+
load("//bazel/private:proto_toolchain_rule.bzl", _proto_toolchain_rule = "proto_toolchain")
7+
8+
def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []):
9+
"""Creates a proto_toolchain and toolchain target for proto_library.
10+
11+
Toolchain target is suffixed with "_toolchain".
12+
13+
Args:
14+
name: name of the toolchain
15+
proto_compiler: (Label) of either proto compiler sources or prebuild binaries
16+
exec_compatible_with: ([constraints]) List of constraints the prebuild binary is compatible with.
17+
"""
18+
_proto_toolchain_rule(name = name, proto_compiler = proto_compiler)
19+
20+
native.toolchain(
21+
name = name + "_toolchain",
22+
toolchain_type = "//third_party/bazel_rules/rules_proto/proto:toolchain_type",
23+
exec_compatible_with = exec_compatible_with,
24+
target_compatible_with = [],
25+
toolchain = name,
26+
)

0 commit comments

Comments
 (0)