Skip to content

Commit

Permalink
Add redirects for proto rules
Browse files Browse the repository at this point in the history
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
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Apr 5, 2024
1 parent 9a61f11 commit d4d34ab
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 0 deletions.
Empty file added bazel/common/BUILD.bazel
Empty file.
5 changes: 5 additions & 0 deletions bazel/common/proto_common.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""proto_common"""

load("//bazel/private:native.bzl", "native_proto_common")

proto_common = native_proto_common
5 changes: 5 additions & 0 deletions bazel/common/proto_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""ProtoInfo"""

load("//bazel/private:native.bzl", "NativeProtoInfo")

ProtoInfo = NativeProtoInfo
5 changes: 5 additions & 0 deletions bazel/common/proto_lang_toolchain_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""ProtoLangToolchainInfo"""

load("//bazel/common:proto_common.bzl", "proto_common")

ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo
5 changes: 5 additions & 0 deletions bazel/private/native.bzl
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions bazel/private/proto_toolchain_rule.bzl
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions bazel/proto_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""proto_library rule"""

proto_library = native.proto_library
Empty file added bazel/toolchains/BUILD.bazel
Empty file.
34 changes: 34 additions & 0 deletions bazel/toolchains/proto_lang_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
26 changes: 26 additions & 0 deletions bazel/toolchains/proto_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit d4d34ab

Please sign in to comment.