Skip to content

Commit

Permalink
Use @Autovalue for Java providers
Browse files Browse the repository at this point in the history
This allows comparing different Starlark-constructed `JavaInfo` instances in native. This is primarily required for our unit tests that verify provider propagation.

PiperOrigin-RevId: 548146901
Change-Id: I7474c0dd7d3e711dcf082f20a29e934d60367764
  • Loading branch information
hvadehra authored and copybara-github committed Jul 14, 2023
1 parent c9c9fb7 commit 5a167b3
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

import static com.google.common.collect.Iterables.getOnlyElement;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.packages.Info;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
Expand All @@ -37,12 +38,12 @@
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;
import net.starlark.java.syntax.Location;

/** Information about the system APIs for a Java compilation. */
@Immutable
public final class BootClassPathInfo extends NativeInfo implements StarlarkValue {
@AutoValue
public abstract class BootClassPathInfo implements Info {

/** Provider singleton constant. */
public static final Provider PROVIDER = new Provider();
Expand Down Expand Up @@ -106,7 +107,7 @@ public BootClassPathInfo bootClassPathInfo(
throws EvalException {
NestedSet<Artifact> systemInputs = getSystemInputs(systemOrNone);
Optional<PathFragment> systemPath = getSystemPath(systemInputs);
return new BootClassPathInfo(
return new AutoValue_BootClassPathInfo(
getBootClassPath(bootClassPathList),
getAuxiliary(auxiliaryList),
systemInputs,
Expand Down Expand Up @@ -167,71 +168,52 @@ private static Optional<PathFragment> getSystemPath(NestedSet<Artifact> systemIn
}
}

private final NestedSet<Artifact> bootclasspath;
private final NestedSet<Artifact> auxiliary;
private final NestedSet<Artifact> systemInputs;
private final Optional<PathFragment> systemPath;

private BootClassPathInfo(
NestedSet<Artifact> bootclasspath,
NestedSet<Artifact> auxiliary,
NestedSet<Artifact> systemInputs,
Optional<PathFragment> systemPath,
Location creationLocation) {
super(creationLocation);
this.bootclasspath = bootclasspath;
this.auxiliary = auxiliary;
this.systemInputs = systemInputs;
this.systemPath = systemPath;
}

@Override
public Provider getProvider() {
return PROVIDER;
}

public static BootClassPathInfo create(NestedSet<Artifact> bootclasspath) {
return new BootClassPathInfo(
return new AutoValue_BootClassPathInfo(
bootclasspath,
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
Optional.empty(),
null);
Location.BUILTIN);
}

public static BootClassPathInfo empty() {
return new BootClassPathInfo(
return new AutoValue_BootClassPathInfo(
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
Optional.empty(),
null);
Location.BUILTIN);
}

/** The jar files containing classes for system APIs, i.e. a Java <= 8 bootclasspath. */
public NestedSet<Artifact> bootclasspath() {
return bootclasspath;
}
public abstract NestedSet<Artifact> bootclasspath();

/**
* The jar files containing extra classes for system APIs that should not be put in the system
* image to support split-package compilation scenarios.
*/
public NestedSet<Artifact> auxiliary() {
return auxiliary;
}
public abstract NestedSet<Artifact> auxiliary();

/** Contents of the directory that is passed to the javac >= 9 {@code --system} flag. */
public abstract NestedSet<Artifact> systemInputs();

/** An argument to the javac >= 9 {@code --system} flag. */
public Optional<PathFragment> systemPath() {
return systemPath;
}
public abstract Optional<PathFragment> systemPath();

/** Contents of the directory that is passed to the javac >= 9 {@code --system} flag. */
public NestedSet<Artifact> systemInputs() {
return systemInputs;
public abstract Location creationLocation();

@Override
public Location getCreationLocation() {
return creationLocation();
}

public boolean isEmpty() {
return bootclasspath.isEmpty();
return bootclasspath().isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
Expand All @@ -31,28 +32,25 @@

/** Provides information about C++ libraries to be linked into Java targets. */
@Immutable
public final class JavaCcInfoProvider implements JavaInfoInternalProvider {
@AutoValue
public abstract class JavaCcInfoProvider implements JavaInfoInternalProvider {

// TODO(b/183579145): Replace CcInfo with only linking information.
private final CcInfo ccInfo;
public abstract CcInfo getCcInfo();

public CcInfo getCcInfo() {
return ccInfo;
}

public JavaCcInfoProvider(CcInfo ccInfo) {
this.ccInfo =
public static JavaCcInfoProvider create(CcInfo ccInfo) {
return new AutoValue_JavaCcInfoProvider(
CcInfo.builder()
.setCcLinkingContext(ccInfo.getCcLinkingContext())
.setCcNativeLibraryInfo(ccInfo.getCcNativeLibraryInfo())
.build();
.build());
}

/** Merges several JavaCcInfoProvider providers into one. */
public static JavaCcInfoProvider merge(Collection<JavaCcInfoProvider> providers) {
ImmutableList<CcInfo> ccInfos =
providers.stream().map(JavaCcInfoProvider::getCcInfo).collect(toImmutableList());
return new JavaCcInfoProvider(CcInfo.merge(ccInfos));
return create(CcInfo.merge(ccInfos));
}

@Nullable
Expand All @@ -72,6 +70,6 @@ static JavaCcInfoProvider fromStarlarkJavaInfo(StructImpl javaInfo) throws EvalE
.setCcNativeLibraryInfo(new CcNativeLibraryInfo(transitiveCcNativeLibraries))
.build();
}
return new JavaCcInfoProvider(ccInfo);
return create(ccInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ private void addCcRelatedProviders(JavaInfo.Builder javaInfoBuilder) throws Rule

CcInfo mergedCcInfo = CcInfo.merge(ccInfos);

javaInfoBuilder.javaCcInfo(new JavaCcInfoProvider(mergedCcInfo));
javaInfoBuilder.javaCcInfo(JavaCcInfoProvider.create(mergedCcInfo));
}

private InstrumentedFilesInfo getInstrumentationFilesProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.lib.rules.java;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
Expand All @@ -36,12 +37,9 @@
* A class that provides compilation information in Java rules, for perusal of aspects and tools.
*/
@Immutable
public final class JavaCompilationInfoProvider
@AutoValue
public abstract class JavaCompilationInfoProvider
implements JavaInfoInternalProvider, JavaCompilationInfoProviderApi<Artifact> {
private final ImmutableList<String> javacOpts;
@Nullable private final NestedSet<Artifact> runtimeClasspath;
@Nullable private final NestedSet<Artifact> compilationClasspath;
private final BootClassPathInfo bootClasspath;

/**
* Transforms the {@code compilation_info} field from a {@link JavaInfo} into a native instance.
Expand Down Expand Up @@ -125,45 +123,39 @@ public Builder setBootClasspath(BootClassPathInfo bootClasspath) {
}

public JavaCompilationInfoProvider build() {
return new JavaCompilationInfoProvider(
javacOpts, runtimeClasspath, compilationClasspath, bootClasspath);
return new AutoValue_JavaCompilationInfoProvider(
javacOpts, runtimeClasspath, compilationClasspath, bootClasspath.bootclasspath());
}
}

@Override
public ImmutableList<String> getJavacOpts() {
return javacOpts;
}
@Nullable
public abstract ImmutableList<String> getJavacOpts();

@Nullable
public abstract NestedSet<Artifact> runtimeClasspath();

@Override
@Nullable
public Depset /*<Artifact>*/ getRuntimeClasspath() {
return runtimeClasspath == null ? null : Depset.of(Artifact.class, runtimeClasspath);
return runtimeClasspath() == null ? null : Depset.of(Artifact.class, runtimeClasspath());
}

@Nullable
public abstract NestedSet<Artifact> compilationClasspath();

@Override
@Nullable
public Depset /*<Artifact>*/ getCompilationClasspath() {
return compilationClasspath == null ? null : Depset.of(Artifact.class, compilationClasspath);
return compilationClasspath() == null
? null
: Depset.of(Artifact.class, compilationClasspath());
}

@Override
public ImmutableList<Artifact> getBootClasspath() {
return bootClasspath.bootclasspath().toList();
}

public NestedSet<Artifact> getBootClasspathAsNestedSet() {
return bootClasspath.bootclasspath();
public ImmutableList<Artifact> getBootClasspathList() {
return bootClasspath().toList();
}

private JavaCompilationInfoProvider(
ImmutableList<String> javacOpts,
@Nullable NestedSet<Artifact> runtimeClasspath,
@Nullable NestedSet<Artifact> compilationClasspath,
BootClassPathInfo bootClasspath) {
this.javacOpts = javacOpts;
this.runtimeClasspath = runtimeClasspath;
this.compilationClasspath = compilationClasspath;
this.bootClasspath = Preconditions.checkNotNull(bootClasspath);
}
public abstract NestedSet<Artifact> bootClasspath();
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static JavaGenJarsProvider from(Object obj) throws EvalException {
} else if (obj instanceof JavaGenJarsProvider) {
return (JavaGenJarsProvider) obj;
} else if (obj instanceof StructImpl) {
return new StarlarkJavaGenJarsProvider((StructImpl) obj);
return new AutoValue_JavaGenJarsProvider_StarlarkJavaGenJarsProvider((StructImpl) obj);
}
throw Starlark.errorf("wanted JavaGenJarsProvider, got %s", Starlark.type(obj));
}
Expand Down Expand Up @@ -160,19 +160,16 @@ public ImmutableList<String> getProcessorClassNamesList() {
}

/** Wrapper for Starlark constructed JavaGenJarsProvider */
class StarlarkJavaGenJarsProvider implements JavaGenJarsProvider {

private final StructImpl struct;
@AutoValue
abstract class StarlarkJavaGenJarsProvider implements JavaGenJarsProvider {

private StarlarkJavaGenJarsProvider(StructImpl struct) {
this.struct = struct;
}
abstract StructImpl struct();

@Override
public NestedSet<Artifact> getTransitiveGenClassJars() throws RuleErrorException {
try {
return Depset.cast(
struct.getValue("transitive_class_jars"), Artifact.class, "transitive_class_jars");
struct().getValue("transitive_class_jars"), Artifact.class, "transitive_class_jars");
} catch (EvalException e) {
throw new RuleErrorException(e);
}
Expand All @@ -182,7 +179,7 @@ public NestedSet<Artifact> getTransitiveGenClassJars() throws RuleErrorException
public NestedSet<Artifact> getTransitiveGenSourceJars() throws RuleErrorException {
try {
return Depset.cast(
struct.getValue("transitive_source_jars"), Artifact.class, "transitive_source_jars");
struct().getValue("transitive_source_jars"), Artifact.class, "transitive_source_jars");
} catch (EvalException e) {
throw new RuleErrorException(e);
}
Expand All @@ -191,45 +188,45 @@ public NestedSet<Artifact> getTransitiveGenSourceJars() throws RuleErrorExceptio
@Override
public NestedSet<Artifact> getProcessorClasspath() throws EvalException {
return Depset.cast(
struct.getValue("processor_classpath"), Artifact.class, "processor_classpath");
struct().getValue("processor_classpath"), Artifact.class, "processor_classpath");
}

@Override
public boolean usesAnnotationProcessing() throws EvalException {
return struct.getValue("enabled", Boolean.class);
return struct().getValue("enabled", Boolean.class);
}

@Nullable
@Override
public Artifact getGenClassJar() throws EvalException {
return nullIfNone(struct.getValue("class_jar"), Artifact.class);
return nullIfNone(struct().getValue("class_jar"), Artifact.class);
}

@Nullable
@Override
public Artifact getGenSourceJar() throws EvalException {
return nullIfNone(struct.getValue("source_jar"), Artifact.class);
return nullIfNone(struct().getValue("source_jar"), Artifact.class);
}

@Override
public Depset getTransitiveGenClassJarsForStarlark() throws EvalException {
return struct.getValue("transitive_class_jars", Depset.class);
return struct().getValue("transitive_class_jars", Depset.class);
}

@Override
public Depset getTransitiveGenSourceJarsForStarlark() throws EvalException {
return struct.getValue("transitive_source_jars", Depset.class);
return struct().getValue("transitive_source_jars", Depset.class);
}

@Override
public Depset getProcessorClasspathForStarlark() throws EvalException {
return struct.getValue("processor_classpath", Depset.class);
return struct().getValue("processor_classpath", Depset.class);
}

@Override
public ImmutableList<String> getProcessorClassNamesList() throws EvalException {
return Sequence.cast(
struct.getValue("processor_classnames"), String.class, "processor_classname")
struct().getValue("processor_classnames"), String.class, "processor_classname")
.getImmutableList();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static NestedSet<Artifact> bootClasspath(TransitiveInfoCollection target)
throws RuleErrorException {
JavaInfo javaInfo = JavaInfo.getJavaInfo(target);
if (javaInfo != null && javaInfo.providerJavaCompilationInfo != null) {
return javaInfo.providerJavaCompilationInfo.getBootClasspathAsNestedSet();
return javaInfo.providerJavaCompilationInfo.bootClasspath();
}
return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public JavaInfo createJavaCompileAction(
streamProviders(runtimeDeps, JavaCcInfoProvider.class),
streamProviders(exports, JavaCcInfoProvider.class),
streamProviders(deps, JavaCcInfoProvider.class),
Stream.of(new JavaCcInfoProvider(CcInfo.merge(nativeLibraries))))
Stream.of(JavaCcInfoProvider.create(CcInfo.merge(nativeLibraries))))
.collect(toImmutableList());

return javaInfoBuilder
Expand Down
Loading

0 comments on commit 5a167b3

Please sign in to comment.