From 1941e2b1b5be8596e984b02fd5dd37e3ed25a81f Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 11 Jul 2022 02:36:18 -0700 Subject: [PATCH] Migrate proto_library_target to proto_info in proto_common.experimental_should_generate_code PiperOrigin-RevId: 460162832 Change-Id: I57a6fa4c6e6c9618cf9edb8518e17b46fc90be9f --- .../build/lib/rules/proto/ProtoCommon.java | 7 ++++--- .../java/proto/java_lite_proto_library.bzl | 2 +- .../common/java/proto/java_proto_library.bzl | 2 +- .../builtins_bzl/common/proto/proto_common.bzl | 18 +++++++++--------- .../lib/rules/proto/BazelProtoCommonTest.java | 5 +++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java index e7ec811e36810f..895913fecd9bab 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java @@ -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()); } @@ -258,7 +259,7 @@ public static Sequence 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), diff --git a/src/main/starlark/builtins_bzl/common/java/proto/java_lite_proto_library.bzl b/src/main/starlark/builtins_bzl/common/java/proto/java_lite_proto_library.bzl index 75693e4982c0dc..8c02c889e55f6a 100644 --- a/src/main/starlark/builtins_bzl/common/java/proto/java_lite_proto_library.bzl +++ b/src/main/starlark/builtins_bzl/common/java/proto/java_lite_proto_library.bzl @@ -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, diff --git a/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl b/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl index e1cb39fb4ca514..2b2e905c149ac9 100644 --- a/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl +++ b/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl @@ -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( diff --git a/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl b/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl index 501ec7588306fc..c5e4451a649ebf 100644 --- a/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl +++ b/src/main/starlark/builtins_bzl/common/proto/proto_common.bzl @@ -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 [], [] @@ -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 @@ -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) diff --git a/src/test/java/com/google/devtools/build/lib/rules/proto/BazelProtoCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/proto/BazelProtoCommonTest.java index 5b7df21a267028..d19a053b2b9e92 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/proto/BazelProtoCommonTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/proto/BazelProtoCommonTest.java @@ -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 = {",