Skip to content

Commit

Permalink
Expose information in JavaPluginInfo to Starlark and implement its co…
Browse files Browse the repository at this point in the history
…nstructor.

PiperOrigin-RevId: 372103861
  • Loading branch information
comius authored and copybara-github committed May 5, 2021
1 parent 35a79c5 commit ebe3a40
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.devtools.build.lib.rules.java.JavaInfo;
import com.google.devtools.build.lib.rules.java.JavaOptions;
import com.google.devtools.build.lib.rules.java.JavaPackageConfigurationRule;
import com.google.devtools.build.lib.rules.java.JavaPluginInfo;
import com.google.devtools.build.lib.rules.java.JavaRuleClasses.IjarBaseRule;
import com.google.devtools.build.lib.rules.java.JavaRuleClasses.JavaRuntimeBaseRule;
import com.google.devtools.build.lib.rules.java.JavaRuleClasses.JavaToolchainBaseRule;
Expand Down Expand Up @@ -92,6 +93,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
new JavaBootstrap(
new JavaStarlarkCommon(BazelJavaSemantics.INSTANCE),
JavaInfo.PROVIDER,
JavaPluginInfo.PROVIDER,
new JavaProtoStarlarkCommon(),
JavaCcLinkParamsProvider.PROVIDER,
ProguardSpecProvider.PROVIDER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@
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.rules.java.JavaPluginInfo.JavaPluginData;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaPluginInfoApi.JavaPluginDataApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaPluginInfoApi;
import java.util.ArrayList;
import java.util.List;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;

/** Provider for users of Java plugins. */
@AutoCodec
@Immutable
@AutoValue
public abstract class JavaPluginInfo extends NativeInfo {
public abstract class JavaPluginInfo extends NativeInfo
implements JavaPluginInfoApi<JavaPluginData> {
public static final String PROVIDER_NAME = "JavaPluginInfo";
public static final Provider PROVIDER = new Provider();

Expand All @@ -43,17 +48,44 @@ public Provider getProvider() {
}

/** Provider class for {@link JavaPluginInfo} objects. */
public static class Provider extends BuiltinProvider<JavaPluginInfo> {
public static class Provider extends BuiltinProvider<JavaPluginInfo>
implements JavaPluginInfoApi.Provider<JavaInfo> {
private Provider() {
super(PROVIDER_NAME, JavaPluginInfo.class);
}

@Override
public JavaPluginInfoApi<JavaPluginData> javaPluginInfo(
Sequence<?> runtimeDeps, Object processorClass, Object processorData, Boolean generatesApi)
throws EvalException {
NestedSet<String> processorClasses =
processorClass == Starlark.NONE
? NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER)
: NestedSetBuilder.create(Order.NAIVE_LINK_ORDER, (String) processorClass);
NestedSet<Artifact> processorClasspath =
JavaInfo.merge(Sequence.cast(runtimeDeps, JavaInfo.class, "runtime_deps"))
.getProvider(JavaCompilationArgsProvider.class)
.getRuntimeJars();

final NestedSet<Artifact> data;
if (processorData instanceof Depset) {
data = Depset.cast(processorData, Artifact.class, "data");
} else {
data =
NestedSetBuilder.wrap(
Order.NAIVE_LINK_ORDER, Sequence.cast(processorData, Artifact.class, "data"));
}

return JavaPluginInfo.create(
JavaPluginData.create(processorClasses, processorClasspath, data), generatesApi);
}
}

/** Information about a Java plugin, except for whether it generates API. */
@AutoCodec
@Immutable
@AutoValue
public abstract static class JavaPluginData implements JavaPluginDataApi {
public abstract static class JavaPluginData implements JavaPluginInfoApi.JavaPluginDataApi {

public static JavaPluginData create(
NestedSet<String> processorClasses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ public class JavaBootstrap implements Bootstrap {

private final JavaCommonApi<?, ?, ?, ?, ?, ?> javaCommonApi;
private final JavaInfoProviderApi javaInfoProviderApi;
private final JavaPluginInfoApi.Provider<?> javaPluginInfoProviderApi;
private final JavaProtoCommonApi<?, ?, ?, ?> javaProtoCommonApi;
private final JavaCcLinkParamsProviderApi.Provider<?, ?> javaCcLinkParamsProviderApiProvider;
private final ProguardSpecProviderApi.Provider<?> proguardSpecProvider;

public JavaBootstrap(
JavaCommonApi<?, ?, ?, ?, ?, ?> javaCommonApi,
JavaInfoProviderApi javaInfoProviderApi,
JavaPluginInfoApi.Provider<?> javaPluginInfoProviderApi,
JavaProtoCommonApi<?, ?, ?, ?> javaProtoCommonApi,
JavaCcLinkParamsProviderApi.Provider<?, ?> javaCcLinkParamsProviderApiProvider,
ProguardSpecProviderApi.Provider<?> proguardSpecProvider) {
this.javaCommonApi = javaCommonApi;
this.javaInfoProviderApi = javaInfoProviderApi;
this.javaPluginInfoProviderApi = javaPluginInfoProviderApi;
this.javaProtoCommonApi = javaProtoCommonApi;
this.javaCcLinkParamsProviderApiProvider = javaCcLinkParamsProviderApiProvider;
this.proguardSpecProvider = proguardSpecProvider;
Expand All @@ -46,6 +49,7 @@ public JavaBootstrap(
public void addBindingsToBuilder(ImmutableMap.Builder<String, Object> builder) {
builder.put("java_common", javaCommonApi);
builder.put("JavaInfo", javaInfoProviderApi);
builder.put("JavaPluginInfo", javaPluginInfoProviderApi);
builder.put("java_proto_common", javaProtoCommonApi);
builder.put("JavaCcLinkParamsInfo", javaCcLinkParamsProviderApiProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@
package com.google.devtools.build.lib.starlarkbuildapi.java;

import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.docgen.annot.StarlarkConstructor;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaPluginInfoApi.JavaPluginDataApi;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.NoneType;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkValue;

/** Info object encapsulating information about Java plugins. */
Expand Down Expand Up @@ -72,4 +80,61 @@ interface JavaPluginDataApi extends StarlarkValue {
structField = true)
Depset /*<FileApi>*/ getProcessorDataForStarlark();
}

/** Provider class for {@link JavaPluginInfoApi} objects. */
@StarlarkBuiltin(name = "Provider", documented = false)
interface Provider<JavaInfoT extends JavaInfoApi<?, ?, ?>> extends ProviderApi {

@StarlarkMethod(
name = "JavaPluginInfo",
doc = "The <code>JavaPluginInfo</code> constructor.",
parameters = {
@Param(
name = "runtime_deps",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = JavaInfoApi.class),
},
named = true,
doc = "The library containing an annotation processor."),
@Param(
name = "processor_class",
named = true,
positional = false,
allowedTypes = {
@ParamType(type = String.class),
@ParamType(type = NoneType.class),
},
doc =
"The fully qualified class name that the Java compiler uses as "
+ "an entry point to the annotation processor."),
@Param(
name = "data",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = FileApi.class),
@ParamType(type = Depset.class, generic1 = FileApi.class),
},
named = true,
positional = false,
defaultValue = "[]",
doc = "The files needed by this annotation processor during execution."),
@Param(
name = "generates_api",
named = true,
positional = false,
defaultValue = "False",
doc =
"Set to true when this annotation processor generates API code. "
+ "<p>Such annotation processor is applied to a Java target before producing "
+ "its header jars (which contains method signatures). When no API plugins "
+ "are present, header jars are generated from the sources, reducing the "
+ "critical path. "
+ "<p><em class=\"harmful\">WARNING: This parameter affects build "
+ "performance, use it only if necessary.</em>"),
},
selfCall = true)
@StarlarkConstructor
JavaPluginInfoApi<?> javaPluginInfo(
Sequence<?> runtimeDeps, Object processorClass, Object processorData, Boolean generatesApi)
throws EvalException;
}
}
Loading

0 comments on commit ebe3a40

Please sign in to comment.