Skip to content

Commit

Permalink
Use proto toolchains in java_lite_proto_library
Browse files Browse the repository at this point in the history
Retrieve proto_lang_toolchain using toolchains in java_lite_proto_library when proto toolchain resolution is enabled.

Issue: bazelbuild/rules_proto#179
PiperOrigin-RevId: 570056144
Change-Id: Ia952003017d18baca71d4e0ba75fc3134b972817
  • Loading branch information
comius authored and copybara-github committed Oct 2, 2023
1 parent 51970d2 commit 8c38be3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ semantics = struct(
compatible_javac_options = _compatible_javac_options,
LAUNCHER_FLAG_LABEL = Label("@bazel_tools//tools/jdk:launcher_flag_alias"),
JAVA_PROTO_TOOLCHAIN = "@rules_java//java/proto:toolchain_type",
JAVA_LITE_PROTO_TOOLCHAIN = "@rules_java//java/proto:lite_toolchain_type",
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ load(":common/java/java_common.bzl", "java_common")
load(":common/java/java_info.bzl", "JavaInfo", _merge_private_for_builtins = "merge")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/java/proto/java_proto_library.bzl", "JavaProtoAspectInfo", "java_compile_for_protos")
load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo", proto_common = "proto_common_do_not_use")
load(":common/proto/proto_common.bzl", "toolchains", proto_common = "proto_common_do_not_use")
load(":common/proto/proto_info.bzl", "ProtoInfo")

PROTO_TOOLCHAIN_ATTR = "_aspect_proto_toolchain_for_javalite"
Expand All @@ -40,7 +40,11 @@ def _aspect_impl(target, ctx):

deps = [dep[JavaInfo] for dep in ctx.rule.attr.deps]
exports = [exp[JavaInfo] for exp in ctx.rule.attr.exports]
proto_toolchain_info = ctx.attr._aspect_proto_toolchain_for_javalite[ProtoLangToolchainInfo]
proto_toolchain_info = toolchains.find_toolchain(
ctx,
"_aspect_proto_toolchain_for_javalite",
semantics.JAVA_LITE_PROTO_TOOLCHAIN,
)
source_jar = None

if proto_common.experimental_should_generate_code(target[ProtoInfo], proto_toolchain_info, "java_lite_proto_library", target.label):
Expand Down Expand Up @@ -74,15 +78,16 @@ def _aspect_impl(target, ctx):
java_lite_proto_aspect = aspect(
implementation = _aspect_impl,
attr_aspects = ["deps", "exports"],
attrs = {
attrs = toolchains.if_legacy_toolchain({
PROTO_TOOLCHAIN_ATTR: attr.label(
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java_lite"),
),
},
}),
fragments = ["java"],
required_providers = [ProtoInfo],
provides = [JavaInfo, JavaProtoAspectInfo],
toolchains = [semantics.JAVA_TOOLCHAIN],
toolchains = [semantics.JAVA_TOOLCHAIN] +
toolchains.use_toolchain(semantics.JAVA_LITE_PROTO_TOOLCHAIN),
)

def _rule_impl(ctx):
Expand All @@ -98,7 +103,11 @@ def _rule_impl(ctx):
([JavaInfo, DefaultInfo, OutputGroupInfo, ProguardSpecProvider])
"""

proto_toolchain_info = ctx.attr._aspect_proto_toolchain_for_javalite[ProtoLangToolchainInfo]
proto_toolchain_info = toolchains.find_toolchain(
ctx,
"_aspect_proto_toolchain_for_javalite",
semantics.JAVA_LITE_PROTO_TOOLCHAIN,
)
for dep in ctx.attr.deps:
proto_common.check_collocated(ctx.label, dep[ProtoInfo], proto_toolchain_info)

Expand Down Expand Up @@ -131,9 +140,11 @@ java_lite_proto_library = rule(
implementation = _rule_impl,
attrs = {
"deps": attr.label_list(providers = [ProtoInfo], aspects = [java_lite_proto_aspect]),
} | toolchains.if_legacy_toolchain({
PROTO_TOOLCHAIN_ATTR: attr.label(
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java_lite"),
),
},
}),
provides = [JavaInfo],
toolchains = toolchains.use_toolchain(semantics.JAVA_LITE_PROTO_TOOLCHAIN),
)

0 comments on commit 8c38be3

Please sign in to comment.