Skip to content

Commit

Permalink
Migrate proto_library_target to proto_info in proto_common.experiment…
Browse files Browse the repository at this point in the history
…al_should_generate_code

PiperOrigin-RevId: 460162832
Change-Id: I57a6fa4c6e6c9618cf9edb8518e17b46fc90be9f
  • Loading branch information
comius authored and copybara-github committed Jul 11, 2022
1 parent 9144c66 commit 1941e2b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ public static boolean shouldGenerateCode(
ruleContext.callStarlarkOrThrowRuleError(
shouldGenerateCode,
ImmutableList.of(
/* proto_library_target */ protoTarget,
/* proto_info */ protoTarget.get(ProtoInfo.PROVIDER),
/* proto_lang_toolchain_info */ protoLangToolchainInfo,
/* rule_name */ ruleName),
/* rule_name */ ruleName,
/* target_label */ protoTarget.getLabel()),
ImmutableMap.of());
}

Expand All @@ -258,7 +259,7 @@ public static Sequence<Artifact> filterSources(
ruleContext.callStarlarkOrThrowRuleError(
filterSources,
ImmutableList.of(
/* proto_library_target */ protoTarget,
/* proto_info */ protoTarget.get(ProtoInfo.PROVIDER),
/* proto_lang_toolchain_info */ protoLangToolchainInfo),
ImmutableMap.of()))
.get(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _aspect_impl(target, ctx):
proto_toolchain_info = ctx.attr._aspect_proto_toolchain_for_javalite[ProtoLangToolchainInfo]
source_jar = None

if proto_common.experimental_should_generate_code(target, proto_toolchain_info, "java_lite_proto_library"):
if proto_common.experimental_should_generate_code(target[ProtoInfo], proto_toolchain_info, "java_lite_proto_library", target.label):
source_jar = ctx.actions.declare_file(ctx.label.name + "-lite-src.jar")
proto_common.compile(
ctx.actions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _bazel_java_proto_aspect_impl(target, ctx):

proto_toolchain_info = ctx.attr._aspect_java_proto_toolchain[ProtoLangToolchainInfo]
source_jar = None
if proto_common.experimental_should_generate_code(target, proto_toolchain_info, "java_proto_library"):
if proto_common.experimental_should_generate_code(target[ProtoInfo], proto_toolchain_info, "java_proto_library", target.label):
# Generate source jar using proto compiler.
source_jar = ctx.actions.declare_file(ctx.label.name + "-speed-src.jar")
proto_common.compile(
Expand Down
18 changes: 9 additions & 9 deletions src/main/starlark/builtins_bzl/common/proto/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ def _compile(

_BAZEL_TOOLS_PREFIX = "external/bazel_tools/"

def _experimental_filter_sources(proto_library_target, proto_lang_toolchain_info):
proto_info = proto_library_target[_builtins.toplevel.ProtoInfo]
def _experimental_filter_sources(proto_info, proto_lang_toolchain_info):
if not proto_info.direct_sources:
return [], []

Expand Down Expand Up @@ -151,9 +150,10 @@ def _experimental_filter_sources(proto_library_target, proto_lang_toolchain_info
return included, excluded

def _experimental_should_generate_code(
proto_library_target,
proto_info,
proto_lang_toolchain_info,
rule_name):
rule_name,
target_label):
"""Checks if the code should be generated for the given proto_library.
The code shouldn't be generated only when the toolchain already provides it
Expand All @@ -163,26 +163,26 @@ def _experimental_should_generate_code(
shouldn't generate code.
Args:
proto_library_target: (Target) The proto_library to generate the sources for.
Obtained as the `target` parameter from an aspect's implementation.
proto_info: (ProtoInfo) The ProtoInfo from proto_library to check the generation for.
proto_lang_toolchain_info: (ProtoLangToolchainInfo) The proto lang toolchain info.
Obtained from a `proto_lang_toolchain` target or constructed ad-hoc.
rule_name: (str) Name of the rule used in the failure message.
target_label: (Label) The label of the target used in the failure message.
Returns:
(bool) True when the code should be generated.
"""
included, excluded = _experimental_filter_sources(proto_library_target, proto_lang_toolchain_info)
included, excluded = _experimental_filter_sources(proto_info, proto_lang_toolchain_info)

if included and excluded:
fail(("The 'srcs' attribute of '%s' contains protos for which '%s' " +
"shouldn't generate code (%s), in addition to protos for which it should (%s).\n" +
"Separate '%s' into 2 proto_library rules.") % (
proto_library_target.label,
target_label,
rule_name,
", ".join([f.short_path for f in excluded]),
", ".join([f.short_path for f in included]),
proto_library_target.label,
target_label,
))

return bool(included)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ public final void setup() throws Exception {
"BoolProvider = provider()",
"def _impl(ctx):",
" result = proto_common_do_not_use.experimental_should_generate_code(",
" ctx.attr.proto_dep,",
" ctx.attr.proto_dep[ProtoInfo],",
" ctx.attr.toolchain[proto_common_do_not_use.ProtoLangToolchainInfo],",
" 'MyRule')",
" 'MyRule',",
" ctx.attr.proto_dep.label)",
" return [BoolProvider(value = result)]",
"should_generate_rule = rule(_impl,",
" attrs = {",
Expand Down

0 comments on commit 1941e2b

Please sign in to comment.