Use toolchain helpers in py_proto_library

The change is a no-op. Just reduces code duplication.

PiperOrigin-RevId: 654702701
pull/17479/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent 630dbfc964
commit aa47311a83
  1. 1
      bazel/BUILD.bazel
  2. 12
      bazel/py_proto_library.bzl

@ -40,6 +40,7 @@ bzl_library(
deps = [
"//bazel/common:proto_common_bzl",
"//bazel/common:proto_info_bzl",
"//bazel/private:toolchain_helpers_bzl",
"@rules_python//python:py_info_bzl",
],
)

@ -3,6 +3,7 @@
load("@rules_python//python:py_info.bzl", "PyInfo")
load("//bazel/common:proto_common.bzl", "proto_common")
load("//bazel/common:proto_info.bzl", "ProtoInfo")
load("//bazel/private:toolchain_helpers.bzl", "toolchains")
PY_PROTO_TOOLCHAIN = "@rules_python//python/proto:toolchain_type"
@ -22,9 +23,6 @@ _PyProtoInfo = provider(
def _filter_provider(provider, *attrs):
return [dep[provider] for attr in attrs for dep in attr if provider in dep]
def _incompatible_toolchains_enabled():
return getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)
def _py_proto_aspect_impl(target, ctx):
"""Generates and compiles Python code for a proto_library.
@ -51,7 +49,7 @@ def _py_proto_aspect_impl(target, ctx):
proto.path,
))
if _incompatible_toolchains_enabled():
if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
toolchain = ctx.toolchains[PY_PROTO_TOOLCHAIN]
if not toolchain:
fail("No toolchains registered for '%s'." % PY_PROTO_TOOLCHAIN)
@ -120,15 +118,15 @@ def _py_proto_aspect_impl(target, ctx):
_py_proto_aspect = aspect(
implementation = _py_proto_aspect_impl,
attrs = {} if _incompatible_toolchains_enabled() else {
attrs = toolchains.if_legacy_toolchain({
"_aspect_proto_toolchain": attr.label(
default = "//python:python_toolchain",
),
},
}),
attr_aspects = ["deps"],
required_providers = [ProtoInfo],
provides = [_PyProtoInfo],
toolchains = [PY_PROTO_TOOLCHAIN] if _incompatible_toolchains_enabled() else [],
toolchains = toolchains.use_toolchain(PY_PROTO_TOOLCHAIN),
)
def _py_proto_library_rule(ctx):

Loading…
Cancel
Save