Skip to content

Commit

Permalink
Add a third variant of native provider wrappers for @rules_java itself
Browse files Browse the repository at this point in the history
Needed for when the main repository is `rules_java`

PiperOrigin-RevId: 695621150
Change-Id: I66ff003dcf063ecf7905accfc83a4babedd2d34c
  • Loading branch information
hvadehra authored and copybara-github committed Nov 12, 2024
1 parent 3edda22 commit 66ea9a0
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class BootClassPathInfo extends StarlarkInfoWrapper {
new BuiltinsProvider();

public static final StarlarkProviderWrapper<BootClassPathInfo> PROVIDER = new Provider();
public static final StarlarkProviderWrapper<BootClassPathInfo> RULES_JAVA_PROVIDER =
new RulesJavaProvider();

private static final BootClassPathInfo EMPTY =
new BootClassPathInfo(null) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<BootClassPathInfo> {
private Provider() {
this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -210,13 +211,18 @@ 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;
}

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);
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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<JavaInfo>
implements com.google.devtools.build.lib.packages.Provider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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. */
Expand All @@ -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")));
}
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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. */
Expand Down Expand Up @@ -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<JavaPluginInfo> providers, boolean builtin) {
Iterable<JavaPluginInfo> providers,
com.google.devtools.build.lib.packages.Provider providerType) {
List<JavaPluginData> plugins = new ArrayList<>();
List<JavaPluginData> apiGeneratingPlugins = new ArrayList<>();
for (JavaPluginInfo provider : providers) {
Expand All @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,29 @@ public ConfiguredTarget create(RuleContext ruleContext)
ruleContext
.getRulePrerequisitesCollection()
.getPrerequisites(":java_plugins", JavaPluginInfo.PROVIDER);
// try builtin provider as well
ImmutableList<JavaPluginInfo> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public final class JavaRuntimeInfo extends StarlarkInfoWrapper {

public static final StarlarkProviderWrapper<JavaRuntimeInfo> LEGACY_BUILTINS_PROVIDER =
new BuiltinsProvider();
public static final StarlarkProviderWrapper<JavaRuntimeInfo> RULES_JAVA_PROVIDER =
new RulesJavaProvider();
public static final StarlarkProviderWrapper<JavaRuntimeInfo> PROVIDER = new Provider();

// Helper methods to access an instance of JavaRuntimeInfo.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -143,14 +147,19 @@ public int version() throws RuleErrorException {
}

private static class BuiltinsProvider extends Provider {

private BuiltinsProvider() {
super(
keyForBuiltins(
Label.parseCanonicalUnchecked("@_builtins//:common/java/java_runtime.bzl")));
}
}

private static class RulesJavaProvider extends Provider {
private RulesJavaProvider() {
super(keyForBuild(Label.parseCanonicalUnchecked("//java/common/rules:java_runtime.bzl")));
}
}

private static class Provider extends StarlarkProviderWrapper<JavaRuntimeInfo> {

private Provider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static class Builder {
private ImmutableList<Artifact> sourcePath = ImmutableList.of();
private final ImmutableList.Builder<Artifact> nativeLibraries = ImmutableList.builder();

private JavaPluginInfo plugins = JavaPluginInfo.empty(false);
private JavaPluginInfo plugins = JavaPluginInfo.empty(JavaPluginInfo.PROVIDER);

private final Map<PathFragment, Artifact> resources = new LinkedHashMap<>();
private final NestedSetBuilder<Artifact> resourceJars = NestedSetBuilder.stableOrder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public final class JavaToolchainProvider extends StarlarkInfoWrapper {

public static final StarlarkProviderWrapper<JavaToolchainProvider> LEGACY_BUILTINS_PROVIDER =
new BuiltinsProvider();
public static final StarlarkProviderWrapper<JavaToolchainProvider> RULES_JAVA_PROVIDER =
new RulesJavaProvider();
public static final StarlarkProviderWrapper<JavaToolchainProvider> PROVIDER = new Provider();

private JavaToolchainProvider(StarlarkInfo underlying) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<JavaToolchainProvider> {
private Provider() {
this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 66ea9a0

Please sign in to comment.