Skip to content

Commit

Permalink
Prepare for moving the java providers out of builtins
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 695293041
Change-Id: I06c293ee1c8ef119d37c3c2188763914041d55fd
  • Loading branch information
hvadehra authored and copybara-github committed Nov 11, 2024
1 parent 9438840 commit da1aba9
Show file tree
Hide file tree
Showing 20 changed files with 395 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ public JavaInfo enableImplicitSourcelessDepsExportsCompatibility(Info javaInfo,
? JavaCompilationArgsProvider.ClasspathType.COMPILE_ONLY
: JavaCompilationArgsProvider.ClasspathType.BOTH;
JavaInfo.Builder builder = JavaInfo.Builder.create();
JavaInfo.PROVIDER
.wrap(javaInfo)
JavaInfo.wrap(javaInfo)
.compilationArgsProvider()
.ifPresent(
args ->
builder.javaCompilationArgs(
JavaCompilationArgsProvider.builder().addExports(args, type).build()));
return builder.setNeverlink(neverlink).build();
return builder.setProvider(javaInfo.getProvider()).setNeverlink(neverlink).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.java;

import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild;
import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuiltins;

import com.google.devtools.build.lib.actions.Artifact;
Expand All @@ -27,6 +28,7 @@
import com.google.devtools.build.lib.packages.StarlarkInfoWithSchema;
import com.google.devtools.build.lib.packages.StarlarkProviderWrapper;
import com.google.devtools.build.lib.packages.StructImpl;
import com.google.devtools.build.lib.skyframe.BzlLoadValue;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Optional;
import net.starlark.java.eval.Starlark;
Expand All @@ -36,6 +38,9 @@
public class BootClassPathInfo extends StarlarkInfoWrapper {

/** Provider singleton constant. */
public static final StarlarkProviderWrapper<BootClassPathInfo> LEGACY_BUILTINS_PROVIDER =
new BuiltinsProvider();

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

private static final BootClassPathInfo EMPTY =
Expand Down Expand Up @@ -74,6 +79,17 @@ private BootClassPathInfo(StructImpl underlying) {
super(underlying);
}

public static BootClassPathInfo wrap(Info info) throws RuleErrorException {
com.google.devtools.build.lib.packages.Provider.Key key = info.getProvider().getKey();
if (key.equals(PROVIDER.getKey())) {
return PROVIDER.wrap(info);
} else if (key.equals(LEGACY_BUILTINS_PROVIDER.getKey())) {
return LEGACY_BUILTINS_PROVIDER.wrap(info);
} else {
throw new RuleErrorException("expected BootClassPathInfo, got: " + key);
}
}

/** The jar files containing classes for system APIs, i.e. a Java <= 8 bootclasspath. */
public NestedSet<Artifact> bootclasspath() throws RuleErrorException {
return getUnderlyingNestedSet("bootclasspath", Artifact.class);
Expand Down Expand Up @@ -105,12 +121,24 @@ && systemInputs().isEmpty()
&& systemPath().isEmpty();
}

private static class Provider extends StarlarkProviderWrapper<BootClassPathInfo> {
private Provider() {
private static class BuiltinsProvider extends Provider {
private BuiltinsProvider() {
super(
keyForBuiltins(
Label.parseCanonicalUnchecked("@_builtins//:common/java/boot_class_path_info.bzl")),
"BootClassPathInfo");
Label.parseCanonicalUnchecked("@_builtins//:common/java/boot_class_path_info.bzl")));
}
}

private static class Provider extends StarlarkProviderWrapper<BootClassPathInfo> {
private Provider() {
this(
keyForBuild(
Label.parseCanonicalUnchecked(
"@rules_java//java/private:boot_class_path_info.bzl")));
}

private Provider(BzlLoadValue.Key key) {
super(key, "BootClassPathInfo");
}

@Override
Expand Down
143 changes: 124 additions & 19 deletions src/main/java/com/google/devtools/build/lib/rules/java/JavaInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.java;

import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild;
import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuiltins;
import static com.google.devtools.build.lib.unsafe.UnsafeProvider.unsafe;

Expand All @@ -29,13 +30,15 @@
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.Info;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.packages.Provider;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkProviderWrapper;
import com.google.devtools.build.lib.packages.StructImpl;
import com.google.devtools.build.lib.rules.cpp.CcInfo;
import com.google.devtools.build.lib.rules.cpp.LibraryToLink;
import com.google.devtools.build.lib.rules.java.JavaPluginInfo.JavaPluginData;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.JavaOutput;
import com.google.devtools.build.lib.skyframe.BzlLoadValue;
import com.google.devtools.build.lib.skyframe.serialization.AsyncDeserializationContext;
import com.google.devtools.build.lib.skyframe.serialization.DeferredObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.DynamicCodec;
Expand Down Expand Up @@ -63,11 +66,12 @@

/** A Starlark declared provider that encapsulates all providers that are needed by Java rules. */
@Immutable
public final class JavaInfo extends NativeInfo
public class JavaInfo extends NativeInfo
implements JavaInfoApi<Artifact, JavaOutput, JavaPluginData> {

public static final String STARLARK_NAME = "JavaInfo";

public static final JavaInfoProvider LEGACY_BUILTINS_PROVIDER = new BuiltinsJavaInfoProvider();
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 @@ -202,7 +206,20 @@ public static <T extends JavaInfoInternalProvider> T getProvider(
}

public static JavaInfo getJavaInfo(TransitiveInfoCollection target) throws RuleErrorException {
return target.get(JavaInfo.PROVIDER);
JavaInfo info = target.get(PROVIDER);
if (info == null) {
info = target.get(LEGACY_BUILTINS_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 {
return JavaInfo.PROVIDER.wrap(info);
}
}

private JavaInfo(
Expand Down Expand Up @@ -481,13 +498,73 @@ public int hashCode() {
providerJavaPlugin);
}

private static class BuiltinsJavaInfo extends JavaInfo {

private BuiltinsJavaInfo(StructImpl javaInfo)
throws EvalException, TypeException, RuleErrorException {
super(javaInfo);
}

private BuiltinsJavaInfo(
JavaCcInfoProvider javaCcInfoProvider,
JavaCompilationArgsProvider javaCompilationArgsProvider,
JavaCompilationInfoProvider javaCompilationInfoProvider,
JavaGenJarsProvider javaGenJarsProvider,
JavaModuleFlagsProvider javaModuleFlagsProvider,
JavaPluginInfo javaPluginInfo,
JavaRuleOutputJarsProvider javaRuleOutputJarsProvider,
JavaSourceJarsProvider javaSourceJarsProvider,
ImmutableList<Artifact> directRuntimeJars,
boolean neverlink,
ImmutableList<String> javaConstraints,
Location creationLocation) {
super(
javaCcInfoProvider,
javaCompilationArgsProvider,
javaCompilationInfoProvider,
javaGenJarsProvider,
javaModuleFlagsProvider,
javaPluginInfo,
javaRuleOutputJarsProvider,
javaSourceJarsProvider,
directRuntimeJars,
neverlink,
javaConstraints,
creationLocation);
}

@Override
public JavaInfoProvider getProvider() {
return LEGACY_BUILTINS_PROVIDER;
}
}

/** Legacy Provider class for {@link JavaInfo} objects. */
public static class BuiltinsJavaInfoProvider extends JavaInfoProvider {
private BuiltinsJavaInfoProvider() {
super(
keyForBuiltins(Label.parseCanonicalUnchecked("@_builtins//:common/java/java_info.bzl")));
}

@Override
protected JavaInfo makeNewInstance(StructImpl info)
throws RuleErrorException, TypeException, EvalException {
return new BuiltinsJavaInfo(info);
}
}

/** Provider class for {@link JavaInfo} objects. */
public static class JavaInfoProvider extends StarlarkProviderWrapper<JavaInfo>
implements com.google.devtools.build.lib.packages.Provider {
private JavaInfoProvider() {
super(
keyForBuiltins(Label.parseCanonicalUnchecked("@_builtins//:common/java/java_info.bzl")),
STARLARK_NAME);
this(
keyForBuild(
Label.parseCanonicalUnchecked(
"@rules_java//java/private:java_info.bzl")));
}

public JavaInfoProvider(BzlLoadValue.Key key) {
super(key, STARLARK_NAME);
}

@Override
Expand All @@ -496,14 +573,19 @@ public JavaInfo wrap(Info info) throws RuleErrorException {
return javaInfo;
} else if (info instanceof StructImpl) {
try {
return new JavaInfo((StructImpl) info);
return makeNewInstance((StructImpl) info);
} catch (EvalException | TypeException e) {
throw new RuleErrorException(e);
}
}
throw new RuleErrorException("got " + Starlark.type(info) + ", wanted JavaInfo");
}

protected JavaInfo makeNewInstance(StructImpl info)
throws RuleErrorException, TypeException, EvalException {
return new JavaInfo(info);
}

@Override
public boolean isExported() {
return true;
Expand Down Expand Up @@ -531,6 +613,7 @@ public static class Builder {
private ImmutableList<String> javaConstraints;
private boolean neverlink;
private Location creationLocation = Location.BUILTIN;
private Provider provider = PROVIDER;

private Builder() {}

Expand Down Expand Up @@ -588,20 +671,42 @@ public Builder javaSourceJars(JavaSourceJarsProvider provider) {
return this;
}

@CanIgnoreReturnValue
public Builder setProvider(Provider provider) {
this.provider = provider;
return this;
}

public JavaInfo build() {
return new JavaInfo(
/* javaCcInfoProvider= */ null,
providerJavaCompilationArgs,
providerJavaCompilationInfo,
/* javaGenJarsProvider= */ null,
/* javaModuleFlagsProvider= */ null,
/* javaPluginInfo= */ null,
providerJavaRuleOutputJars,
providerJavaSourceJars,
runtimeJars,
neverlink,
javaConstraints,
creationLocation);
if (provider.getKey().equals(LEGACY_BUILTINS_PROVIDER.getKey())) {
return new BuiltinsJavaInfo(
/* javaCcInfoProvider= */ null,
providerJavaCompilationArgs,
providerJavaCompilationInfo,
/* javaGenJarsProvider= */ null,
/* javaModuleFlagsProvider= */ null,
/* javaPluginInfo= */ null,
providerJavaRuleOutputJars,
providerJavaSourceJars,
runtimeJars,
neverlink,
javaConstraints,
creationLocation);
} else {
return new JavaInfo(
/* javaCcInfoProvider= */ null,
providerJavaCompilationArgs,
providerJavaCompilationInfo,
/* javaGenJarsProvider= */ null,
/* javaModuleFlagsProvider= */ null,
/* javaPluginInfo= */ null,
providerJavaRuleOutputJars,
providerJavaSourceJars,
runtimeJars,
neverlink,
javaConstraints,
creationLocation);
}
}
}

Expand Down
Loading

0 comments on commit da1aba9

Please sign in to comment.