Skip to content

Commit

Permalink
Get databinding artifacts from Starlark prerequisite if the prerequis…
Browse files Browse the repository at this point in the history
…ite does the Java compilation.

PiperOrigin-RevId: 370784802
  • Loading branch information
Googler authored and copybara-github committed Apr 27, 2021
1 parent f0f7b2f commit a68d889
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ private static RuleConfiguredTargetBuilder init(
ruleContext.getConfiguration().isCodeCoverageEnabled(),
/* collectJavaCompilationArgs= */ true,
/* isBinary= */ true,
shouldCompileJavaSrcs,
excludedRuntimeArtifacts,
/* generateExtensionRegistry= */ true);
ruleContext.assertNoErrors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.rules.android.ZipFilterBuilder.CheckHashMismatchMode;
import com.google.devtools.build.lib.rules.android.databinding.DataBindingContext;
import com.google.devtools.build.lib.rules.android.databinding.DataBindingV2Provider;
import com.google.devtools.build.lib.rules.cpp.CcInfo;
import com.google.devtools.build.lib.rules.cpp.CcLinkingContext;
import com.google.devtools.build.lib.rules.cpp.CcLinkingContext.LinkOptions;
Expand Down Expand Up @@ -438,6 +439,7 @@ public JavaTargetAttributes init(
boolean addCoverageSupport,
boolean collectJavaCompilationArgs,
boolean isBinary,
boolean shouldCompileJavaSrcs,
NestedSet<Artifact> excludedRuntimeArtifacts,
boolean generateExtensionRegistry)
throws InterruptedException, RuleErrorException {
Expand All @@ -462,22 +464,26 @@ public JavaTargetAttributes init(
ImmutableList.Builder<String> javacopts = ImmutableList.builder();
javacopts.addAll(androidSemantics.getCompatibleJavacOptions(ruleContext));

resourceApk
.asDataBindingContext()
.supplyJavaCoptsUsing(ruleContext, isBinary, javacopts::addAll);
if (shouldCompileJavaSrcs) {
resourceApk
.asDataBindingContext()
.supplyJavaCoptsUsing(ruleContext, isBinary, javacopts::addAll);
}
JavaTargetAttributes.Builder attributesBuilder =
javaCommon
.initCommon(idlHelper.getIdlGeneratedJavaSources(), javacopts.build())
.setBootClassPath(bootClassPathInfo);

resourceApk
.asDataBindingContext()
.supplyAnnotationProcessor(
ruleContext,
(plugin, additionalOutputs) -> {
attributesBuilder.addPlugin(plugin);
attributesBuilder.addAdditionalOutputs(additionalOutputs);
});
if (shouldCompileJavaSrcs) {
resourceApk
.asDataBindingContext()
.supplyAnnotationProcessor(
ruleContext,
(plugin, additionalOutputs) -> {
attributesBuilder.addPlugin(plugin);
attributesBuilder.addAdditionalOutputs(additionalOutputs);
});
}

if (excludedRuntimeArtifacts != null) {
attributesBuilder.addExcludedArtifacts(excludedRuntimeArtifacts);
Expand Down Expand Up @@ -505,9 +511,19 @@ public JavaTargetAttributes init(
jarsProducedForRuntime.add(resourceApk.getResourceJavaClassJar());
}

// Databinding metadata that the databinding annotation processor reads.
ImmutableList<Artifact> additionalJavaInputsFromDatabinding =
resourceApk.asDataBindingContext().processDeps(ruleContext, isBinary);
ImmutableList<Artifact> additionalJavaInputsFromDatabinding = null;
if (shouldCompileJavaSrcs) {
// Databinding metadata that the databinding annotation processor reads.
additionalJavaInputsFromDatabinding =
resourceApk.asDataBindingContext().processDeps(ruleContext, isBinary);
} else {
ImmutableList.Builder<Artifact> outputs = ImmutableList.<Artifact>builder();
DataBindingV2Provider p =
ruleContext.getPrerequisite("application_resources", DataBindingV2Provider.PROVIDER);
outputs.addAll(p.getSetterStores());
outputs.addAll(p.getTransitiveBRFiles().toList());
additionalJavaInputsFromDatabinding = outputs.build();
}

JavaCompilationHelper helper =
initAttributes(attributesBuilder, javaSemantics, additionalJavaInputsFromDatabinding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
/* addCoverageSupport= */ false,
/* collectJavaCompilationArgs= */ true,
/* isBinary= */ false,
/* shouldCompileJavaSrcs= */ true,
/* excludedRuntimeArtifacts= */ null,
/* generateExtensionRegistry= */ false);
if (javaTargetAttributes == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.rules.android.AndroidApplicationResourceInfo;
import com.google.devtools.build.lib.rules.android.AndroidCommon;
import com.google.devtools.build.lib.rules.android.AndroidDataBindingProcessorBuilder;
import com.google.devtools.build.lib.rules.android.AndroidDataContext;
Expand Down Expand Up @@ -334,7 +335,14 @@ private static List<Artifact> getDirectClassInfo(RuleContext context) {

@Override
public void addProvider(RuleConfiguredTargetBuilder builder, RuleContext ruleContext) {

if (shouldGetDatabindingArtifactsFromApplicationResources(ruleContext)) {
DataBindingV2Provider p =
ruleContext.getPrerequisite("application_resources", DataBindingV2Provider.PROVIDER);
if (p != null) {
builder.addNativeDeclaredProvider(p);
return;
}
}
Artifact setterStoreFile =
DataBinding.getMetadataOutput(ruleContext, useUpdatedArgs, setterStoreName);

Expand Down Expand Up @@ -395,7 +403,7 @@ public boolean usesAndroidX() {
return useAndroidX;
}

private static Artifact getClassInfoFile(ActionConstructionContext context) {
private static Artifact getClassInfoFile(RuleContext context) {
return context.getUniqueDirectoryArtifact("databinding", "class-info.zip");
}

Expand All @@ -405,4 +413,14 @@ private Artifact getLayoutInfoFile() {
}
return injectedLayoutInfoZip;
}

private static boolean shouldGetDatabindingArtifactsFromApplicationResources(
RuleContext context) {
if (!context.attributes().isAttributeValueExplicitlySpecified("application_resources")) {
return false;
}
AndroidApplicationResourceInfo androidApplicationResourceInfo =
context.getPrerequisite("application_resources", AndroidApplicationResourceInfo.PROVIDER);
return !androidApplicationResourceInfo.shouldCompileJavaSrcs();
}
}

0 comments on commit a68d889

Please sign in to comment.