diff options
author | 2017-01-03 16:15:06 +0000 | |
---|---|---|
committer | 2017-01-03 16:44:37 +0000 | |
commit | 59d2a6a8cdfcae1935a9e91ec329a6ff68fa7d5b (patch) | |
tree | 2201200fc8549204b8444013948d31cb48c63d16 /src/main/java | |
parent | c2d0c66c7d555b13e45182bea2fb3feef2ea6052 (diff) |
Refactor JavaSkylarkApiProvider to source its info from passed-in providers instead of getting them from the base.
This is a preparatory change to allow aspects to bind JavaSkylarkApiProvider with a different name.
java_proto_library needs to be able to advertise its jars from each aspect. The elegant way to do this would be to bind the "java" skylark provider. Unfortunately, proto_library also advertises this provider, even though it is incorrect to do so in the context of java_proto_library, because the jars advertised by the base configured target are certainly not going to be produced.
Unless we can override providers when the merged configured target is produced, we then need to be able to bind a java provider with a different name. Unfortunately the implementation of this accesses providers from the configured target, which again belong to the proto_library, not the aspect. This CL allows the java skylark api provider to serve data defined at construction time rather than from hard-coded providers from the configured target.
--
PiperOrigin-RevId: 143445977
MOS_MIGRATED_REVID=143445977
Diffstat (limited to 'src/main/java')
8 files changed, 251 insertions, 105 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java index 159ca75366..a3fda94283 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java @@ -62,6 +62,7 @@ import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider; import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar; import com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider; import com.google.devtools.build.lib.rules.java.JavaSemantics; +import com.google.devtools.build.lib.rules.java.JavaSkylarkApiProvider; import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider; import com.google.devtools.build.lib.rules.java.JavaTargetAttributes; import com.google.devtools.build.lib.rules.java.JavaUtil; @@ -738,8 +739,7 @@ public class AndroidCommon { ResourceApk resourceApk, Artifact zipAlignedApk, Iterable<Artifact> apksUnderTest) { - javaCommon.addTransitiveInfoProviders(builder, filesToBuild, classJar, ANDROID_COLLECTION_SPEC); - javaCommon.addGenJarsProvider(builder, genClassJar, genSourceJar); + idlHelper.addTransitiveInfoProviders(builder, classJar, manifestProtoOutput); if (generatedExtensionRegistryProvider != null) { @@ -751,17 +751,32 @@ public class AndroidCommon { javaRuleOutputJarsProviderBuilder.addOutputJar(resourceJar); } - JavaSourceJarsProvider javaSourceJarsProvider = javaSourceJarsProviderBuilder.build(); + JavaRuleOutputJarsProvider ruleOutputJarsProvider = + javaRuleOutputJarsProviderBuilder + .addOutputJar(classJar, iJar, srcJar) + .setJdeps(outputDepsProto) + .build(); + JavaSourceJarsProvider sourceJarsProvider = javaSourceJarsProviderBuilder.build(); + JavaCompilationArgsProvider compilationArgsProvider = + JavaCompilationArgsProvider.create( + javaCompilationArgs, + recursiveJavaCompilationArgs, + compileTimeDependencyArtifacts, + NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)); + JavaSkylarkApiProvider.Builder skylarkApiProvider = + JavaSkylarkApiProvider.builder() + .setRuleOutputJarsProvider(ruleOutputJarsProvider) + .setSourceJarsProvider(sourceJarsProvider) + .setCompilationArgsProvider(compilationArgsProvider); + javaCommon.addTransitiveInfoProviders( + builder, skylarkApiProvider, filesToBuild, classJar, ANDROID_COLLECTION_SPEC); + javaCommon.addGenJarsProvider(builder, skylarkApiProvider, genClassJar, genSourceJar); return builder .setFilesToBuild(filesToBuild) - .add( - JavaRuleOutputJarsProvider.class, - javaRuleOutputJarsProviderBuilder - .addOutputJar(classJar, iJar, srcJar) - .setJdeps(outputDepsProto) - .build()) - .add(JavaSourceJarsProvider.class, javaSourceJarsProvider) + .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()) + .add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider) + .add(JavaSourceJarsProvider.class, sourceJarsProvider) .add( JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaCommon.getJavaCompilationArtifacts().getRuntimeJars())) @@ -778,13 +793,7 @@ public class AndroidCommon { resourceApk, zipAlignedApk, apksUnderTest)) - .add( - JavaCompilationArgsProvider.class, - JavaCompilationArgsProvider.create( - javaCompilationArgs, - recursiveJavaCompilationArgs, - compileTimeDependencyArtifacts, - NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER))) + .add(JavaCompilationArgsProvider.class, compilationArgsProvider) .add( JackLibraryProvider.class, asNeverLink @@ -794,8 +803,7 @@ public class AndroidCommon { .addOutputGroup( OutputGroupProvider.HIDDEN_TOP_LEVEL, collectHiddenTopLevelArtifacts(ruleContext)) .addOutputGroup( - JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, - javaSourceJarsProvider.getTransitiveSourceJars()); + JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, sourceJarsProvider.getTransitiveSourceJars()); } private Runfiles getRunfiles() { diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java index 3b38f779a5..9175a25e2b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java @@ -122,9 +122,8 @@ public class JavaBinary implements RuleConfiguredTargetFactory { .addSourceJar(srcJar) .addAllTransitiveSourceJars(common.collectTransitiveSourceJars(srcJar)); Artifact classJar = ruleContext.getImplicitOutputArtifact(JavaSemantics.JAVA_BINARY_CLASS_JAR); - JavaRuleOutputJarsProvider.Builder javaRuleOutputJarsProviderBuilder = - JavaRuleOutputJarsProvider.builder() - .addOutputJar(classJar, null /* iJar */, srcJar); + JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder = + JavaRuleOutputJarsProvider.builder().addOutputJar(classJar, null /* iJar */, srcJar); CppConfiguration cppConfiguration = ruleContext.getConfiguration().getFragment( CppConfiguration.class); @@ -193,10 +192,10 @@ public class JavaBinary implements RuleConfiguredTargetFactory { common, filesBuilder, javaArtifactsBuilder, - javaRuleOutputJarsProviderBuilder, + ruleOutputJarsProviderBuilder, javaSourceJarsProviderBuilder); Artifact outputDepsProto = helper.createOutputDepsProtoArtifact(classJar, javaArtifactsBuilder); - javaRuleOutputJarsProviderBuilder.setJdeps(outputDepsProto); + ruleOutputJarsProviderBuilder.setJdeps(outputDepsProto); JavaCompilationArtifacts javaArtifacts = javaArtifactsBuilder.build(); common.setJavaCompilationArtifacts(javaArtifacts); @@ -250,8 +249,8 @@ public class JavaBinary implements RuleConfiguredTargetFactory { } } - JavaSourceJarsProvider javaSourceJarsProvider = javaSourceJarsProviderBuilder.build(); - NestedSet<Artifact> transitiveSourceJars = javaSourceJarsProvider.getTransitiveSourceJars(); + JavaSourceJarsProvider sourceJarsProvider = javaSourceJarsProviderBuilder.build(); + NestedSet<Artifact> transitiveSourceJars = sourceJarsProvider.getTransitiveSourceJars(); // TODO(bazel-team): if (getOptions().sourceJars) then make this a dummy prerequisite for the // DeployArchiveAction ? Needs a few changes there as we can't pass inputs @@ -370,12 +369,19 @@ public class JavaBinary implements RuleConfiguredTargetFactory { FileWriteAction.create(ruleContext, unstrippedDeployJar, "", false)); } - common.addTransitiveInfoProviders(builder, filesToBuild, classJar); - common.addGenJarsProvider(builder, genClassJar, genSourceJar); + JavaRuleOutputJarsProvider ruleOutputJarsProvider = ruleOutputJarsProviderBuilder.build(); + JavaSkylarkApiProvider.Builder skylarkApiProvider = + JavaSkylarkApiProvider.builder() + .setRuleOutputJarsProvider(ruleOutputJarsProvider) + .setSourceJarsProvider(sourceJarsProvider); + + common.addTransitiveInfoProviders(builder, skylarkApiProvider, filesToBuild, classJar); + common.addGenJarsProvider(builder, skylarkApiProvider, genClassJar, genSourceJar); return builder .setFilesToBuild(filesToBuild) - .add(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProviderBuilder.build()) + .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()) + .add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider) .add(RunfilesProvider.class, runfilesProvider) // The executable to run (below) may be different from the executable for runfiles (the one // we create the runfiles support object with). On Linux they are the same (it's the same @@ -388,7 +394,7 @@ public class JavaBinary implements RuleConfiguredTargetFactory { .add( JavaSourceInfoProvider.class, JavaSourceInfoProvider.fromJavaTargetAttributes(attributes, semantics)) - .add(JavaSourceJarsProvider.class, javaSourceJarsProviderBuilder.build()) + .add(JavaSourceJarsProvider.class, sourceJarsProvider) .addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, transitiveSourceJars) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java index 9f528c44de..e2d9111ec8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java @@ -638,21 +638,36 @@ public class JavaCommon { return builder.build(); } - public void addTransitiveInfoProviders(RuleConfiguredTargetBuilder builder, - NestedSet<Artifact> filesToBuild, @Nullable Artifact classJar) { - addTransitiveInfoProviders(builder, filesToBuild, classJar, JAVA_COLLECTION_SPEC); - } - - public void addTransitiveInfoProviders(RuleConfiguredTargetBuilder builder, - NestedSet<Artifact> filesToBuild, @Nullable Artifact classJar, + public void addTransitiveInfoProviders( + RuleConfiguredTargetBuilder builder, + JavaSkylarkApiProvider.Builder skylarkApiProvider, + NestedSet<Artifact> filesToBuild, + @Nullable Artifact classJar) { + addTransitiveInfoProviders( + builder, skylarkApiProvider, filesToBuild, classJar, JAVA_COLLECTION_SPEC); + } + + public void addTransitiveInfoProviders( + RuleConfiguredTargetBuilder builder, + JavaSkylarkApiProvider.Builder skylarkApiProvider, + NestedSet<Artifact> filesToBuild, + @Nullable Artifact classJar, InstrumentationSpec instrumentationSpec) { + + JavaCompilationInfoProvider compilationInfoProvider = createCompilationInfoProvider(); + JavaExportsProvider exportsProvider = collectTransitiveExports(); + + skylarkApiProvider + .setCompilationInfoProvider(compilationInfoProvider) + .setExportsProvider(exportsProvider); + builder - .add(InstrumentedFilesProvider.class, + .add( + InstrumentedFilesProvider.class, getInstrumentationFilesProvider(ruleContext, filesToBuild, instrumentationSpec)) - .add(JavaExportsProvider.class, collectTransitiveExports()) - .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, new JavaSkylarkApiProvider()) + .add(JavaExportsProvider.class, exportsProvider) .addOutputGroup(OutputGroupProvider.FILES_TO_COMPILE, getFilesToCompile(classJar)) - .add(JavaCompilationInfoProvider.class, createCompilationInfoProvider()); + .add(JavaCompilationInfoProvider.class, compilationInfoProvider); } private static InstrumentedFilesProvider getInstrumentationFilesProvider(RuleContext ruleContext, @@ -667,8 +682,11 @@ public class JavaCommon { /*withBaselineCoverage*/!TargetUtils.isTestRule(ruleContext.getTarget())); } - public void addGenJarsProvider(RuleConfiguredTargetBuilder builder, - @Nullable Artifact genClassJar, @Nullable Artifact genSourceJar) { + public void addGenJarsProvider( + RuleConfiguredTargetBuilder builder, + JavaSkylarkApiProvider.Builder javaSkylarkApiProvider, + @Nullable Artifact genClassJar, + @Nullable Artifact genSourceJar) { JavaGenJarsProvider genJarsProvider = collectTransitiveGenJars( javaCompilationHelper.usesAnnotationProcessing(), genClassJar, genSourceJar); @@ -677,6 +695,7 @@ public class JavaCommon { genJarsBuilder.addTransitive(genJarsProvider.getTransitiveGenClassJars()); genJarsBuilder.addTransitive(genJarsProvider.getTransitiveGenSourceJars()); + javaSkylarkApiProvider.setGenJarsProvider1(genJarsProvider); builder .add(JavaGenJarsProvider.class, genJarsProvider) .addOutputGroup(JavaSemantics.GENERATED_JARS_OUTPUT_GROUP, genJarsBuilder.build()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java index d1f9aabacb..2d31f531f0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java @@ -141,38 +141,43 @@ public class JavaImport implements RuleConfiguredTargetFactory { .setSourceJarsForJarFiles(srcJars) .build(); - JavaRuleOutputJarsProvider.Builder ruleOutputJarsProvider = + JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder = JavaRuleOutputJarsProvider.builder(); for (Artifact jar : jars) { - ruleOutputJarsProvider.addOutputJar( - jar, - compilationToRuntimeJarMap.inverse().get(jar), - srcJar); + ruleOutputJarsProviderBuilder.addOutputJar( + jar, compilationToRuntimeJarMap.inverse().get(jar), srcJar); } NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs(); - common.addTransitiveInfoProviders(ruleBuilder, filesToBuild, null); + JavaRuleOutputJarsProvider ruleOutputJarsProvider = ruleOutputJarsProviderBuilder.build(); + JavaSourceJarsProvider sourceJarsProvider = + JavaSourceJarsProvider.create(transitiveJavaSourceJars, srcJars); + JavaCompilationArgsProvider compilationArgsProvider = + JavaCompilationArgsProvider.create(javaCompilationArgs, recursiveJavaCompilationArgs); + JavaSkylarkApiProvider.Builder skylarkApiProvider = + JavaSkylarkApiProvider.builder() + .setRuleOutputJarsProvider(ruleOutputJarsProvider) + .setSourceJarsProvider(sourceJarsProvider) + .setCompilationArgsProvider(compilationArgsProvider); + common.addTransitiveInfoProviders(ruleBuilder, skylarkApiProvider, filesToBuild, null); return ruleBuilder .setFilesToBuild(filesToBuild) - .add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider.build()) + .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()) + .add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider) .add( JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaArtifacts.getRuntimeJars())) .add(JavaNeverlinkInfoProvider.class, new JavaNeverlinkInfoProvider(neverLink)) .add(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) .add(CcLinkParamsProvider.class, new CcLinkParamsProvider(ccLinkParamsStore)) - .add( - JavaCompilationArgsProvider.class, - JavaCompilationArgsProvider.create(javaCompilationArgs, recursiveJavaCompilationArgs)) + .add(JavaCompilationArgsProvider.class, compilationArgsProvider) .add( JavaNativeLibraryProvider.class, new JavaNativeLibraryProvider(transitiveJavaNativeLibraries)) .add(CppCompilationContext.class, transitiveCppDeps) .add(JavaSourceInfoProvider.class, javaSourceInfoProvider) - .add( - JavaSourceJarsProvider.class, - JavaSourceJarsProvider.create(transitiveJavaSourceJars, srcJars)) + .add(JavaSourceJarsProvider.class, sourceJarsProvider) .add(ProguardSpecProvider.class, new ProguardSpecProvider(proguardSpecs)) .addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, transitiveJavaSourceJars) .addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, proguardSpecs) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java index a8de642c9f..888d4456c1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibrary.java @@ -161,25 +161,34 @@ public class JavaLibrary implements RuleConfiguredTargetFactory { genClassJar, genSourceJar, ImmutableMap.<Artifact, Artifact>of(), filesBuilder, builder); + JavaRuleOutputJarsProvider ruleOutputJarsProvider = + JavaRuleOutputJarsProvider.builder() + .addOutputJar(classJar, iJar, srcJar) + .setJdeps(outputDepsProto) + .build(); + JavaCompilationArgsProvider compilationArgsProvider = + JavaCompilationArgsProvider.create( + javaCompilationArgs, recursiveJavaCompilationArgs, + compileTimeJavaDepArtifacts, runTimeJavaDepArtifacts); + JavaSourceJarsProvider sourceJarsProvider = + JavaSourceJarsProvider.create(transitiveSourceJars, ImmutableList.of(srcJar)); + JavaSkylarkApiProvider.Builder skylarkApiProvider = + JavaSkylarkApiProvider.builder() + .setRuleOutputJarsProvider(ruleOutputJarsProvider) + .setSourceJarsProvider(sourceJarsProvider) + .setCompilationArgsProvider(compilationArgsProvider); + NestedSet<Artifact> filesToBuild = filesBuilder.build(); - common.addTransitiveInfoProviders(builder, filesToBuild, classJar); - common.addGenJarsProvider(builder, genClassJar, genSourceJar); + common.addTransitiveInfoProviders(builder, skylarkApiProvider, filesToBuild, classJar); + common.addGenJarsProvider(builder, skylarkApiProvider, genClassJar, genSourceJar); NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs(); CcLinkParamsProvider ccLinkParamsProvider = new CcLinkParamsProvider(ccLinkParamsStore); - JavaCompilationArgsProvider compilationArgsProvider = - JavaCompilationArgsProvider.create( - javaCompilationArgs, recursiveJavaCompilationArgs, - compileTimeJavaDepArtifacts, runTimeJavaDepArtifacts); JavaProvider javaProvider = new JavaProvider(compilationArgsProvider); builder - .add( - JavaRuleOutputJarsProvider.class, - JavaRuleOutputJarsProvider.builder() - .addOutputJar(classJar, iJar, srcJar) - .setJdeps(outputDepsProto) - .build()) + .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()) + .add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider) .add( JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaArtifacts.getRuntimeJars())) @@ -201,9 +210,7 @@ public class JavaLibrary implements RuleConfiguredTargetFactory { .add( JavaSourceInfoProvider.class, JavaSourceInfoProvider.fromJavaTargetAttributes(attributes, semantics)) - .add( - JavaSourceJarsProvider.class, - JavaSourceJarsProvider.create(transitiveSourceJars, ImmutableList.of(srcJar))) + .add(JavaSourceJarsProvider.class, sourceJarsProvider) // TODO(bazel-team): this should only happen for java_plugin .add(JavaPluginInfoProvider.class, JavaCommon.getTransitivePlugins(ruleContext)) .add(ProguardSpecProvider.class, new ProguardSpecProvider(proguardSpecs)) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java index 4e6678b772..39574dbfb5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java @@ -14,6 +14,8 @@ package com.google.devtools.build.lib.rules.java; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; @@ -23,6 +25,7 @@ import com.google.devtools.build.lib.rules.SkylarkApiProvider; import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import javax.annotation.Nullable; /** * A class that exposes the Java providers to Skylark. It is intended to provide a simple and stable @@ -40,48 +43,76 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { /** The name of the field in Skylark used to access this class. */ public static final String NAME = "java"; + private final JavaRuleOutputJarsProvider ruleOutputJarsProvider; + @Nullable private final JavaSourceJarsProvider sourceJarsProvider; + @Nullable private final JavaGenJarsProvider genJarsProvider; + @Nullable private final JavaCompilationInfoProvider compilationInfoProvider; + @Nullable private final JavaCompilationArgsProvider compilationArgsProvider; + @Nullable private final JavaExportsProvider exportsProvider; + + private JavaSkylarkApiProvider( + JavaRuleOutputJarsProvider ruleOutputJarsProvider, + @Nullable JavaSourceJarsProvider sourceJarsProvider, + @Nullable JavaGenJarsProvider genJarsProvider, + @Nullable JavaCompilationInfoProvider compilationInfoProvider, + @Nullable JavaCompilationArgsProvider compilationArgsProvider, + @Nullable JavaExportsProvider exportsProvider) { + this.compilationInfoProvider = compilationInfoProvider; + this.ruleOutputJarsProvider = ruleOutputJarsProvider; + this.sourceJarsProvider = sourceJarsProvider; + this.genJarsProvider = genJarsProvider; + this.compilationArgsProvider = compilationArgsProvider; + this.exportsProvider = exportsProvider; + } + @SkylarkCallable( - name = "source_jars", - doc = "Returns the Jars containing Java source files for the target", - structField = true) + name = "source_jars", + doc = "Returns the Jars containing Java source files for the target", + structField = true + ) public NestedSet<Artifact> getSourceJars() { - JavaSourceJarsProvider sourceJars = getInfo().getProvider(JavaSourceJarsProvider.class); - return NestedSetBuilder.wrap(Order.STABLE_ORDER, sourceJars.getSourceJars()); + if (sourceJarsProvider == null) { + return NestedSetBuilder.emptySet(Order.STABLE_ORDER); + } + return NestedSetBuilder.wrap(Order.STABLE_ORDER, sourceJarsProvider.getSourceJars()); } @SkylarkCallable( - name = "transitive_deps", - doc = "Returns the transitive set of Jars required to build the target", - structField = true) + name = "transitive_deps", + doc = "Returns the transitive set of Jars required to build the target", + structField = true + ) public NestedSet<Artifact> getTransitiveDeps() { - JavaCompilationArgsProvider args = getInfo().getProvider(JavaCompilationArgsProvider.class); - if (args == null) { + if (compilationArgsProvider == null) { return NestedSetBuilder.emptySet(Order.STABLE_ORDER); } - return args.getRecursiveJavaCompilationArgs().getCompileTimeJars(); + return compilationArgsProvider.getRecursiveJavaCompilationArgs().getCompileTimeJars(); } @SkylarkCallable( - name = "transitive_runtime_deps", - doc = "Returns the transitive set of Jars required on the target's runtime classpath", - structField = true) + name = "transitive_runtime_deps", + doc = "Returns the transitive set of Jars required on the target's runtime classpath", + structField = true + ) public NestedSet<Artifact> getTransitiveRuntimeDeps() { - JavaCompilationArgsProvider args = getInfo().getProvider(JavaCompilationArgsProvider.class); - if (args == null) { + if (compilationArgsProvider == null) { return NestedSetBuilder.emptySet(Order.STABLE_ORDER); } - return args.getRecursiveJavaCompilationArgs().getRuntimeJars(); + return compilationArgsProvider.getRecursiveJavaCompilationArgs().getRuntimeJars(); } @SkylarkCallable( - name = "transitive_source_jars", - doc = - "Returns the Jars containing Java source files for the target and all of its transitive " - + "dependencies", - structField = true) + name = "transitive_source_jars", + doc = + "Returns the Jars containing Java source files for the target and all of its transitive " + + "dependencies", + structField = true + ) public NestedSet<Artifact> getTransitiveSourceJars() { - JavaSourceJarsProvider sourceJars = getInfo().getProvider(JavaSourceJarsProvider.class); - return sourceJars.getTransitiveSourceJars(); + if (sourceJarsProvider == null) { + return NestedSetBuilder.emptySet(Order.STABLE_ORDER); + } + return sourceJarsProvider.getTransitiveSourceJars(); } @SkylarkCallable( @@ -90,7 +121,7 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { structField = true ) public JavaRuleOutputJarsProvider getOutputJars() { - return getInfo().getProvider(JavaRuleOutputJarsProvider.class); + return ruleOutputJarsProvider; } @SkylarkCallable( @@ -99,9 +130,8 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { doc = "Returns transitive set of labels that are being exported from this rule." ) public NestedSet<Label> getTransitiveExports() { - JavaExportsProvider provider = getInfo().getProvider(JavaExportsProvider.class); - if (provider != null) { - return provider.getTransitiveExports(); + if (exportsProvider != null) { + return exportsProvider.getTransitiveExports(); } else { return NestedSetBuilder.emptySet(Order.STABLE_ORDER); } @@ -114,7 +144,7 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { doc = "Returns information about annotation processing for this Java target" ) public JavaGenJarsProvider getGenJarsProvider() { - return getInfo().getProvider(JavaGenJarsProvider.class); + return genJarsProvider; } @SkylarkCallable( @@ -124,7 +154,61 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { doc = "Returns compilation information for this Java target" ) public JavaCompilationInfoProvider getCompilationInfoProvider() { - return getInfo().getProvider(JavaCompilationInfoProvider.class); + return compilationInfoProvider; } + public static Builder builder() { + return new Builder(); + } + + /** Builder for {@link JavaSkylarkApiProvider} */ + public static class Builder { + private JavaRuleOutputJarsProvider ruleOutputJarsProvider; + private JavaSourceJarsProvider sourceJarsProvider; + private JavaGenJarsProvider genJarsProvider; + private JavaCompilationInfoProvider compilationInfoProvider; + private JavaCompilationArgsProvider compilationArgsProvider; + private JavaExportsProvider exportsProvider; + + public Builder setRuleOutputJarsProvider(JavaRuleOutputJarsProvider ruleOutputJarsProvider) { + this.ruleOutputJarsProvider = ruleOutputJarsProvider; + return this; + } + + public Builder setSourceJarsProvider(JavaSourceJarsProvider sourceJarsProvider) { + this.sourceJarsProvider = sourceJarsProvider; + return this; + } + + public Builder setGenJarsProvider1(JavaGenJarsProvider genJarsProvider) { + this.genJarsProvider = genJarsProvider; + return this; + } + + public Builder setCompilationInfoProvider(JavaCompilationInfoProvider compilationInfoProvider) { + this.compilationInfoProvider = compilationInfoProvider; + return this; + } + + public Builder setCompilationArgsProvider(JavaCompilationArgsProvider compilationArgsProvider) { + this.compilationArgsProvider = compilationArgsProvider; + return this; + } + + public Builder setExportsProvider(JavaExportsProvider exportsProvider) { + this.exportsProvider = exportsProvider; + return this; + } + + public JavaSkylarkApiProvider build() { + checkNotNull(ruleOutputJarsProvider, "Must provide JavaRuleOutputJarsProvider"); + return new JavaSkylarkApiProvider( + ruleOutputJarsProvider, + sourceJarsProvider, + genJarsProvider, + compilationInfoProvider, + compilationArgsProvider, + exportsProvider); + } + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java index 5e32d8cc2b..f38c10d0db 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoLibrary.java @@ -78,8 +78,17 @@ public class JavaLiteProtoLibrary implements RuleConfiguredTargetFactory { filesToBuild.addTransitive(provider.getJars()); } + JavaRuleOutputJarsProvider javaRuleOutputJarsProvider = + JavaRuleOutputJarsProvider.builder().build(); + JavaSkylarkApiProvider.Builder skylarkApiProvider = + JavaSkylarkApiProvider.builder() + .setRuleOutputJarsProvider(javaRuleOutputJarsProvider) + .setSourceJarsProvider(sourceJarsProvider) + .setCompilationArgsProvider(dependencyArgsProviders); + return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(filesToBuild.build()) + .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()) .addProvider(RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, runfiles)) .addOutputGroup( OutputGroupProvider.DEFAULT, NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER)) @@ -87,8 +96,7 @@ public class JavaLiteProtoLibrary implements RuleConfiguredTargetFactory { .add(JavaSourceJarsProvider.class, sourceJarsProvider) .add(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles)) .add(ProguardSpecProvider.class, getJavaLiteRuntimeSpec(ruleContext)) - .add(JavaRuleOutputJarsProvider.class, JavaRuleOutputJarsProvider.builder().build()) - .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, new JavaSkylarkApiProvider()) + .add(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProvider) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoLibrary.java index d0e3849273..9cf4c735d0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoLibrary.java @@ -76,16 +76,25 @@ public class JavaProtoLibrary implements RuleConfiguredTargetFactory { filesToBuild.addTransitive(provider.getJars()); } + + JavaRuleOutputJarsProvider ruleOutputJarsProvider = + JavaRuleOutputJarsProvider.builder().build(); + JavaSkylarkApiProvider.Builder skylarkApiProvider = + JavaSkylarkApiProvider.builder() + .setRuleOutputJarsProvider(ruleOutputJarsProvider) + .setSourceJarsProvider(sourceJarsProvider) + .setCompilationArgsProvider(dependencyArgsProviders); + return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(filesToBuild.build()) + .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()) .addProvider(RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, runfiles)) .addOutputGroup( OutputGroupProvider.DEFAULT, NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER)) .add(JavaCompilationArgsProvider.class, dependencyArgsProviders) .add(JavaSourceJarsProvider.class, sourceJarsProvider) .add(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles)) - .add(JavaRuleOutputJarsProvider.class, JavaRuleOutputJarsProvider.builder().build()) - .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, new JavaSkylarkApiProvider()) + .add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider) .build(); } |