From 66ea9a037f2d727f5e1c8d70d187dfe680a48c06 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 12 Nov 2024 00:12:06 -0800 Subject: [PATCH] Add a third variant of native provider wrappers for `@rules_java` itself Needed for when the main repository is `rules_java` PiperOrigin-RevId: 695621150 Change-Id: I66ff003dcf063ecf7905accfc83a4babedd2d34c --- .../lib/rules/java/BootClassPathInfo.java | 10 ++++ .../build/lib/rules/java/JavaInfo.java | 32 ++++++++++ .../build/lib/rules/java/JavaPluginInfo.java | 60 +++++++++++-------- .../rules/java/JavaPluginsFlagAliasRule.java | 25 +++++--- .../build/lib/rules/java/JavaRuntimeInfo.java | 11 +++- .../lib/rules/java/JavaTargetAttributes.java | 2 +- .../lib/rules/java/JavaToolchainProvider.java | 10 ++++ .../rules/java/JavaInfoStarlarkApiTest.java | 5 +- .../rules/java/JavaPluginsFlagAliasTest.java | 4 +- 9 files changed, 122 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/BootClassPathInfo.java b/src/main/java/com/google/devtools/build/lib/rules/java/BootClassPathInfo.java index 0a47b42016b430..47fcd79fa989d2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/BootClassPathInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/BootClassPathInfo.java @@ -42,6 +42,8 @@ public class BootClassPathInfo extends StarlarkInfoWrapper { new BuiltinsProvider(); public static final StarlarkProviderWrapper PROVIDER = new Provider(); + public static final StarlarkProviderWrapper RULES_JAVA_PROVIDER = + new RulesJavaProvider(); private static final BootClassPathInfo EMPTY = new BootClassPathInfo(null) { @@ -85,6 +87,8 @@ public static BootClassPathInfo wrap(Info info) throws RuleErrorException { return PROVIDER.wrap(info); } else if (key.equals(LEGACY_BUILTINS_PROVIDER.getKey())) { return LEGACY_BUILTINS_PROVIDER.wrap(info); + } else if (key.equals(RULES_JAVA_PROVIDER.getKey())) { + return RULES_JAVA_PROVIDER.wrap(info); } else { throw new RuleErrorException("expected BootClassPathInfo, got: " + key); } @@ -129,6 +133,12 @@ private BuiltinsProvider() { } } + private static class RulesJavaProvider extends Provider { + private RulesJavaProvider() { + super(keyForBuild(Label.parseCanonicalUnchecked("//java/private:boot_class_path_info.bzl"))); + } + } + private static class Provider extends StarlarkProviderWrapper { private Provider() { this( diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfo.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfo.java index 80a7230fe13abc..493a47781cae2a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfo.java @@ -72,6 +72,7 @@ public class JavaInfo extends NativeInfo public static final String STARLARK_NAME = "JavaInfo"; public static final JavaInfoProvider LEGACY_BUILTINS_PROVIDER = new BuiltinsJavaInfoProvider(); + public static final JavaInfoProvider RULES_JAVA_PROVIDER = new RulesJavaJavaInfoProvider(); public static final JavaInfoProvider PROVIDER = new JavaInfoProvider(); // Ideally we would check if the target has a JavaInfo, but this check predates the Starlark @@ -210,6 +211,9 @@ public static JavaInfo getJavaInfo(TransitiveInfoCollection target) throws RuleE if (info == null) { info = target.get(LEGACY_BUILTINS_PROVIDER); } + if (info == null) { + info = target.get(RULES_JAVA_PROVIDER); + } return info; } @@ -217,6 +221,8 @@ public static JavaInfo wrap(Info info) throws RuleErrorException { Provider.Key key = info.getProvider().getKey(); if (key.equals(LEGACY_BUILTINS_PROVIDER.getKey())) { return LEGACY_BUILTINS_PROVIDER.wrap(info); + } else if (key.equals(RULES_JAVA_PROVIDER.getKey())) { + return RULES_JAVA_PROVIDER.wrap(info); } else { return JavaInfo.PROVIDER.wrap(info); } @@ -539,6 +545,19 @@ public JavaInfoProvider getProvider() { } } + private static class RulesJavaJavaInfo extends JavaInfo { + + private RulesJavaJavaInfo(StructImpl javaInfo) + throws EvalException, TypeException, RuleErrorException { + super(javaInfo); + } + + @Override + public JavaInfoProvider getProvider() { + return RULES_JAVA_PROVIDER; + } + } + /** Legacy Provider class for {@link JavaInfo} objects. */ public static class BuiltinsJavaInfoProvider extends JavaInfoProvider { private BuiltinsJavaInfoProvider() { @@ -553,6 +572,19 @@ protected JavaInfo makeNewInstance(StructImpl info) } } + /** Legacy Provider class for {@link JavaInfo} objects. */ + public static class RulesJavaJavaInfoProvider extends JavaInfoProvider { + private RulesJavaJavaInfoProvider() { + super(keyForBuild(Label.parseCanonicalUnchecked("//java/private:java_info.bzl"))); + } + + @Override + protected JavaInfo makeNewInstance(StructImpl info) + throws RuleErrorException, TypeException, EvalException { + return new RulesJavaJavaInfo(info); + } + } + /** Provider class for {@link JavaInfo} objects. */ public static class JavaInfoProvider extends StarlarkProviderWrapper implements com.google.devtools.build.lib.packages.Provider { diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java index 7b32df553a17c8..d160613c27e5b1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginInfo.java @@ -52,14 +52,22 @@ public abstract class JavaPluginInfo extends NativeInfo public static final String PROVIDER_NAME = "JavaPluginInfo"; public static final Provider LEGACY_BUILTINS_PROVIDER = new BuiltinsProvider(); public static final Provider PROVIDER = new Provider(); + public static final Provider RULES_JAVA_PROVIDER = new RulesJavaProvider(); private static final JavaPluginInfo EMPTY_BUILTIN = new AutoValue_JavaPluginInfo( - ImmutableList.of(), JavaPluginData.empty(), JavaPluginData.empty(), true); + ImmutableList.of(), + JavaPluginData.empty(), + JavaPluginData.empty(), + LEGACY_BUILTINS_PROVIDER); private static final JavaPluginInfo EMPTY = new AutoValue_JavaPluginInfo( - ImmutableList.of(), JavaPluginData.empty(), JavaPluginData.empty(), false); + ImmutableList.of(), JavaPluginData.empty(), JavaPluginData.empty(), PROVIDER); + + private static final JavaPluginInfo EMPTY_RULES_JAVA = + new AutoValue_JavaPluginInfo( + ImmutableList.of(), JavaPluginData.empty(), JavaPluginData.empty(), RULES_JAVA_PROVIDER); public static JavaPluginInfo wrap(Info info) throws RuleErrorException { com.google.devtools.build.lib.packages.Provider.Key key = info.getProvider().getKey(); @@ -83,8 +91,8 @@ public static JavaPluginInfo get(ConfiguredTarget target) throws RuleErrorExcept } @Override - public Provider getProvider() { - return builtin() ? LEGACY_BUILTINS_PROVIDER : PROVIDER; + public com.google.devtools.build.lib.packages.Provider getProvider() { + return providerType(); } /** Legacy Provider class for {@link JavaPluginInfo} objects. */ @@ -93,10 +101,12 @@ private BuiltinsProvider() { super( keyForBuiltins(Label.parseCanonicalUnchecked("@_builtins//:common/java/java_info.bzl"))); } + } - @Override - protected boolean isBuiltin() { - return true; + /** Provider class for {@link JavaPluginInfo} objects in rules_java itself. */ + public static class RulesJavaProvider extends Provider { + private RulesJavaProvider() { + super(keyForBuild(Label.parseCanonicalUnchecked("//java/private:java_info.bzl"))); } } @@ -144,7 +154,7 @@ public JavaPluginInfo wrap(Info value) throws RuleErrorException { Sequence.cast(info.getValue("java_outputs"), Object.class, "java_outputs")), JavaPluginData.wrap(info.getValue("plugins")), JavaPluginData.wrap(info.getValue("api_generating_plugins")), - isBuiltin()); + value.getProvider()); } catch (EvalException e) { throw new RuleErrorException(e); } @@ -153,10 +163,6 @@ public JavaPluginInfo wrap(Info value) throws RuleErrorException { "got element of type " + Starlark.type(value) + ", want JavaPluginInfo"); } } - - protected boolean isBuiltin() { - return false; - } } /** Information about a Java plugin, except for whether it generates API. */ @@ -242,13 +248,12 @@ public boolean isEmpty() { public static JavaPluginInfo mergeWithoutJavaOutputs(JavaPluginInfo a, JavaPluginInfo b) { return a.isEmpty() ? b - : b.isEmpty() - ? a - : mergeWithoutJavaOutputs(ImmutableList.of(a, b), a.builtin() && b.builtin()); + : b.isEmpty() ? a : mergeWithoutJavaOutputs(ImmutableList.of(a, b), a.providerType()); } public static JavaPluginInfo mergeWithoutJavaOutputs( - Iterable providers, boolean builtin) { + Iterable providers, + com.google.devtools.build.lib.packages.Provider providerType) { List plugins = new ArrayList<>(); List apiGeneratingPlugins = new ArrayList<>(); for (JavaPluginInfo provider : providers) { @@ -258,20 +263,24 @@ public static JavaPluginInfo mergeWithoutJavaOutputs( if (!provider.apiGeneratingPlugins().isEmpty()) { apiGeneratingPlugins.add(provider.apiGeneratingPlugins()); } - builtin = builtin && provider.builtin(); } if (plugins.isEmpty() && apiGeneratingPlugins.isEmpty()) { - return JavaPluginInfo.empty(builtin); + return JavaPluginInfo.empty(providerType); } return new AutoValue_JavaPluginInfo( ImmutableList.of(), JavaPluginData.merge(plugins), JavaPluginData.merge(apiGeneratingPlugins), - builtin); + providerType); } - public static JavaPluginInfo empty(boolean builtin) { - return builtin ? EMPTY_BUILTIN : EMPTY; + public static JavaPluginInfo empty(com.google.devtools.build.lib.packages.Provider providerType) { + if (providerType.equals(LEGACY_BUILTINS_PROVIDER)) { + return EMPTY_BUILTIN; + } else if (providerType.equals(RULES_JAVA_PROVIDER)) { + return EMPTY_RULES_JAVA; + } + return EMPTY; } @Override @@ -280,7 +289,7 @@ public static JavaPluginInfo empty(boolean builtin) { @Override public abstract JavaPluginData apiGeneratingPlugins(); - protected abstract boolean builtin(); + protected abstract com.google.devtools.build.lib.packages.Provider providerType(); /** Returns true if the provider has no associated data. */ public boolean isEmpty() { @@ -308,15 +317,14 @@ public boolean hasProcessors() { */ static JavaPluginInfo fromStarlarkJavaInfo(StructImpl javaInfo) throws EvalException, RuleErrorException { - boolean builtin = - javaInfo.getProvider().getKey().equals(JavaInfo.LEGACY_BUILTINS_PROVIDER.getKey()); + com.google.devtools.build.lib.packages.Provider providerType = javaInfo.getProvider(); JavaPluginData plugins = JavaPluginData.wrap(javaInfo.getValue("plugins")); JavaPluginData apiGeneratingPlugins = JavaPluginData.wrap(javaInfo.getValue("api_generating_plugins")); if (plugins.isEmpty() && apiGeneratingPlugins.isEmpty()) { - return JavaPluginInfo.empty(builtin); + return JavaPluginInfo.empty(providerType); } return new AutoValue_JavaPluginInfo( - ImmutableList.of(), plugins, apiGeneratingPlugins, /* builtin= */ builtin); + ImmutableList.of(), plugins, apiGeneratingPlugins, providerType); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasRule.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasRule.java index b4a31cb0a4fa08..571f66e683ad3f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasRule.java @@ -86,18 +86,29 @@ public ConfiguredTarget create(RuleContext ruleContext) ruleContext .getRulePrerequisitesCollection() .getPrerequisites(":java_plugins", JavaPluginInfo.PROVIDER); - // try builtin provider as well - ImmutableList builtinsProviderPlugins = - ruleContext - .getRulePrerequisitesCollection() - .getPrerequisites(":java_plugins", JavaPluginInfo.LEGACY_BUILTINS_PROVIDER); - JavaPluginInfo javaPluginInfo = JavaPluginInfo.mergeWithoutJavaOutputs(plugins, false); + if (plugins.isEmpty()) { + plugins = + ruleContext + .getRulePrerequisitesCollection() + .getPrerequisites(":java_plugins", JavaPluginInfo.LEGACY_BUILTINS_PROVIDER); + } + if (plugins.isEmpty()) { + plugins = + ruleContext + .getRulePrerequisitesCollection() + .getPrerequisites(":java_plugins", JavaPluginInfo.RULES_JAVA_PROVIDER); + } + JavaPluginInfo javaPluginInfo = + JavaPluginInfo.mergeWithoutJavaOutputs(plugins, JavaPluginInfo.PROVIDER); JavaPluginInfo builtinsProviderInfo = - JavaPluginInfo.mergeWithoutJavaOutputs(builtinsProviderPlugins, true); + JavaPluginInfo.mergeWithoutJavaOutputs(plugins, JavaPluginInfo.LEGACY_BUILTINS_PROVIDER); + JavaPluginInfo rulesJavaProviderInfo = + JavaPluginInfo.mergeWithoutJavaOutputs(plugins, JavaPluginInfo.RULES_JAVA_PROVIDER); return new RuleConfiguredTargetBuilder(ruleContext) .addStarlarkDeclaredProvider(javaPluginInfo) .addStarlarkDeclaredProvider(builtinsProviderInfo) + .addStarlarkDeclaredProvider(rulesJavaProviderInfo) .addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java index 6aa7d24af25116..7fb91641f781ce 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java @@ -48,6 +48,8 @@ public final class JavaRuntimeInfo extends StarlarkInfoWrapper { public static final StarlarkProviderWrapper LEGACY_BUILTINS_PROVIDER = new BuiltinsProvider(); + public static final StarlarkProviderWrapper RULES_JAVA_PROVIDER = + new RulesJavaProvider(); public static final StarlarkProviderWrapper PROVIDER = new Provider(); // Helper methods to access an instance of JavaRuntimeInfo. @@ -102,6 +104,8 @@ public static JavaRuntimeInfo wrap(Info info) throws RuleErrorException { return PROVIDER.wrap(info); } else if (key.equals(LEGACY_BUILTINS_PROVIDER.getKey())) { return LEGACY_BUILTINS_PROVIDER.wrap(info); + } else if (key.equals(RULES_JAVA_PROVIDER.getKey())) { + return RULES_JAVA_PROVIDER.wrap(info); } else { throw new RuleErrorException("expected JavaRuntimeInfo, got: " + key); } @@ -143,7 +147,6 @@ public int version() throws RuleErrorException { } private static class BuiltinsProvider extends Provider { - private BuiltinsProvider() { super( keyForBuiltins( @@ -151,6 +154,12 @@ private BuiltinsProvider() { } } + private static class RulesJavaProvider extends Provider { + private RulesJavaProvider() { + super(keyForBuild(Label.parseCanonicalUnchecked("//java/common/rules:java_runtime.bzl"))); + } + } + private static class Provider extends StarlarkProviderWrapper { private Provider() { diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java index 38270c6cafbb97..a6f7e6e618e6a8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java @@ -63,7 +63,7 @@ public static class Builder { private ImmutableList sourcePath = ImmutableList.of(); private final ImmutableList.Builder nativeLibraries = ImmutableList.builder(); - private JavaPluginInfo plugins = JavaPluginInfo.empty(false); + private JavaPluginInfo plugins = JavaPluginInfo.empty(JavaPluginInfo.PROVIDER); private final Map resources = new LinkedHashMap<>(); private final NestedSetBuilder resourceJars = NestedSetBuilder.stableOrder(); diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainProvider.java index cdc77080fb9ace..bf4c85557aa8ca 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainProvider.java @@ -53,6 +53,8 @@ public final class JavaToolchainProvider extends StarlarkInfoWrapper { public static final StarlarkProviderWrapper LEGACY_BUILTINS_PROVIDER = new BuiltinsProvider(); + public static final StarlarkProviderWrapper RULES_JAVA_PROVIDER = + new RulesJavaProvider(); public static final StarlarkProviderWrapper PROVIDER = new Provider(); private JavaToolchainProvider(StarlarkInfo underlying) { @@ -65,6 +67,8 @@ public static JavaToolchainProvider wrap(Info info) throws RuleErrorException { return PROVIDER.wrap(info); } else if (key.equals(LEGACY_BUILTINS_PROVIDER.getKey())) { return LEGACY_BUILTINS_PROVIDER.wrap(info); + } else if (key.equals(RULES_JAVA_PROVIDER.getKey())) { + return RULES_JAVA_PROVIDER.wrap(info); } else { throw new RuleErrorException("expected JavaToolchainInfo, got: " + key); } @@ -321,6 +325,12 @@ private BuiltinsProvider() { } } + private static class RulesJavaProvider extends Provider { + private RulesJavaProvider() { + super(keyForBuild(Label.parseCanonicalUnchecked("//java/common/rules:java_toolchain.bzl"))); + } + } + private static class Provider extends StarlarkProviderWrapper { private Provider() { this( diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java index f4b0b8205dfcca..59fbae4d287559 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java @@ -1344,7 +1344,10 @@ public void translateStarlarkJavaInfo_minimal() throws Exception { assertThat(javaInfo.getProvider(JavaCompilationArgsProvider.class)).isNotNull(); assertThat(javaInfo.getCompilationInfoProvider()).isNull(); assertThat(javaInfo.getJavaModuleFlagsInfo()).isEqualTo(JavaModuleFlagsProvider.EMPTY); - assertThat(javaInfo.getJavaPluginInfo()).isEqualTo(JavaPluginInfo.empty(false)); + assertThat(javaInfo.getJavaPluginInfo()) + .isAnyOf( + JavaPluginInfo.empty(JavaPluginInfo.LEGACY_BUILTINS_PROVIDER), + JavaPluginInfo.empty(JavaPluginInfo.PROVIDER)); } @Test diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java index ae058b65ee4a03..375327a12488d2 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java @@ -46,7 +46,9 @@ public void javaPluginFlagAlias_noFlagSet() throws Exception { getConfiguredTarget(TestConstants.TOOLS_REPOSITORY + "//tools/jdk:java_plugins_flag_alias"); assertThat(JavaPluginInfo.get(target)) - .isAnyOf(JavaPluginInfo.empty(true), JavaPluginInfo.empty(false)); + .isAnyOf( + JavaPluginInfo.empty(JavaPluginInfo.PROVIDER), + JavaPluginInfo.empty(JavaPluginInfo.LEGACY_BUILTINS_PROVIDER)); } /** Tests that a single plugin passed by a flag is returned by java_plugins_flag_alias. */