From e8243465731f5c407448e7b9d5686663f149b9da Mon Sep 17 00:00:00 2001 From: asteinb Date: Wed, 11 Apr 2018 14:26:07 -0700 Subject: Add skipped manifest processing methods Add some required manifest support I didn't previously implement: - Support for exports_manifest field - Get properly processed manifest from AndroidSemantics - Correctly represent the current relationship between manifest and resource processing - resource processing uses the stamped manifest. RELNOTES: none PiperOrigin-RevId: 192508962 --- .../build/lib/rules/android/AndroidManifest.java | 62 ++++++++++++++++++++-- .../AndroidResourceParsingActionBuilder.java | 11 +++- .../lib/rules/android/ParsedAndroidResources.java | 34 ++++++------ .../lib/rules/android/StampedAndroidManifest.java | 15 +++++- 4 files changed, 98 insertions(+), 24 deletions(-) (limited to 'src/main/java/com/google/devtools/build/lib/rules') diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidManifest.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidManifest.java index 1e11c75def..ad05739c90 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidManifest.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidManifest.java @@ -13,35 +13,56 @@ // limitations under the License. package com.google.devtools.build.lib.rules.android; +import com.google.common.annotations.VisibleForTesting; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; import com.google.devtools.build.lib.rules.java.JavaUtil; +import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.vfs.PathFragment; +import java.util.Objects; import javax.annotation.Nullable; /** Wraps an Android Manifest and provides utilities for working with it */ @Immutable public class AndroidManifest { + private static final String CUSTOM_PACKAGE_ATTR = "custom_package"; private final Artifact manifest; /** The Android package. Will be null if and only if this is an aar_import target. */ @Nullable private final String pkg; + private final boolean exported; public static AndroidManifest forAarImport(RuleContext ruleContext, Artifact manifest) { - return new AndroidManifest(manifest, null); + return new AndroidManifest(manifest, /* pkg = */ null, /* exported = */ true); } - AndroidManifest(Artifact manifest, @Nullable String pkg) { + public static AndroidManifest from(RuleContext ruleContext, AndroidSemantics androidSemantics) + throws RuleErrorException, InterruptedException { + return new AndroidManifest( + androidSemantics.getManifestForRule(ruleContext).getManifest(), + getAndroidPackage(ruleContext), + AndroidCommon.getExportsManifest(ruleContext)); + } + + AndroidManifest(AndroidManifest other, Artifact manifest) { + this(manifest, other.pkg, other.exported); + } + + @VisibleForTesting + AndroidManifest(Artifact manifest, @Nullable String pkg, boolean exported) { this.manifest = manifest; this.pkg = pkg; + this.exported = exported; } /** If needed, stamps the manifest with the correct Java package */ public StampedAndroidManifest stamp(RuleContext ruleContext) { return new StampedAndroidManifest( ApplicationManifest.maybeSetManifestPackage(ruleContext, manifest, pkg).orElse(manifest), - pkg); + pkg, + exported); } Artifact getManifest() { @@ -53,8 +74,21 @@ public class AndroidManifest { return pkg; } + boolean isExported() { + return exported; + } + + /** Gets the Android package for this target, from either rule configuration or Java package */ + private static String getAndroidPackage(RuleContext ruleContext) { + if (ruleContext.attributes().isAttributeValueExplicitlySpecified(CUSTOM_PACKAGE_ATTR)) { + return ruleContext.attributes().get(CUSTOM_PACKAGE_ATTR, Type.STRING); + } + + return getDefaultPackage(ruleContext); + } + /** Gets the default Java package */ - static String getDefaultPackage(RuleContext ruleContext) { + private static String getDefaultPackage(RuleContext ruleContext) { PathFragment dummyJar = ruleContext.getPackageDirectory().getChild("Dummy.jar"); return getJavaPackageFromPath(ruleContext, dummyJar); } @@ -86,4 +120,24 @@ public class AndroidManifest { } return JavaUtil.getJavaPackageName(jarPathFragment); } + + @Override + public boolean equals(Object object) { + if (object == null || getClass() != object.getClass()) { + return false; + } + + AndroidManifest other = (AndroidManifest) object; + + return manifest.equals(other.manifest) + && Objects.equals(pkg, other.pkg) + && exported == other.exported; + } + + @Override + public int hashCode() { + // Hash the current class with the other fields to distinguish between this AndroidManifest and + // classes that extend it. + return Objects.hash(manifest, pkg, exported, getClass()); + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceParsingActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceParsingActionBuilder.java index c890627b71..842e20cc7e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceParsingActionBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceParsingActionBuilder.java @@ -190,12 +190,19 @@ public class AndroidResourceParsingActionBuilder { * Builds and registers the action, and returns a copy of the passed resources with artifacts for * parsed and compiled information. */ - public ParsedAndroidResources build(AndroidResources androidResources) { + public ParsedAndroidResources build( + AndroidResources androidResources, StampedAndroidManifest manifest) { + if (dataBindingInfoZip != null) { + // Manifest information is needed for data binding + setManifest(manifest.getManifest()); + setJavaPackage(manifest.getPackage()); + } + setResources(androidResources); build(ruleContext); return ParsedAndroidResources.of( - androidResources, output, compiledSymbols, ruleContext.getLabel()); + androidResources, output, compiledSymbols, ruleContext.getLabel(), manifest); } /** diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ParsedAndroidResources.java b/src/main/java/com/google/devtools/build/lib/rules/android/ParsedAndroidResources.java index fc0af30386..21a4a4ba25 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/ParsedAndroidResources.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/ParsedAndroidResources.java @@ -26,6 +26,7 @@ public class ParsedAndroidResources extends AndroidResources { private final Artifact symbols; @Nullable private final Artifact compiledSymbols; private final Label label; + private final StampedAndroidManifest manifest; public static ParsedAndroidResources parseFrom( RuleContext ruleContext, AndroidResources resources, StampedAndroidManifest manifest) @@ -41,10 +42,7 @@ public class ParsedAndroidResources extends AndroidResources { // TODO(corysmith): Centralize the data binding processing and zipping into a single // action. Data binding processing needs to be triggered here as well as the merger to // avoid aapt2 from throwing an error during compilation. - builder - .setDataBindingInfoZip(DataBinding.getSuffixedInfoFile(ruleContext, "_unused")) - .setManifest(manifest.getManifest()) - .setJavaPackage(manifest.getPackage()); + builder.setDataBindingInfoZip(DataBinding.getSuffixedInfoFile(ruleContext, "_unused")); } return builder @@ -53,34 +51,33 @@ public class ParsedAndroidResources extends AndroidResources { isAapt2 ? ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_COMPILED_SYMBOLS) : null) - .build(resources); + .build(resources, manifest); } public static ParsedAndroidResources of( AndroidResources resources, Artifact symbols, @Nullable Artifact compiledSymbols, - Label label) { - return new ParsedAndroidResources( - resources, - symbols, - compiledSymbols, - label); + Label label, + StampedAndroidManifest manifest) { + return new ParsedAndroidResources(resources, symbols, compiledSymbols, label, manifest); } - ParsedAndroidResources(ParsedAndroidResources other) { - this(other, other.symbols, other.compiledSymbols, other.label); + ParsedAndroidResources(ParsedAndroidResources other, StampedAndroidManifest manifest) { + this(other, other.symbols, other.compiledSymbols, other.label, manifest); } private ParsedAndroidResources( AndroidResources resources, Artifact symbols, @Nullable Artifact compiledSymbols, - Label label) { + Label label, + StampedAndroidManifest manifest) { super(resources); this.symbols = symbols; this.compiledSymbols = compiledSymbols; this.label = label; + this.manifest = manifest; } public Artifact getSymbols() { @@ -92,6 +89,10 @@ public class ParsedAndroidResources extends AndroidResources { return compiledSymbols; } + public Artifact getManifest() { + return manifest.getManifest(); + } + public Label getLabel() { return label; } @@ -105,11 +106,12 @@ public class ParsedAndroidResources extends AndroidResources { ParsedAndroidResources other = (ParsedAndroidResources) object; return symbols.equals(other.symbols) && Objects.equals(compiledSymbols, other.compiledSymbols) - && label.equals(other.label); + && label.equals(other.label) + && manifest.equals(other.manifest); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), symbols, compiledSymbols, label); + return Objects.hash(super.hashCode(), symbols, compiledSymbols, label, manifest); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/StampedAndroidManifest.java b/src/main/java/com/google/devtools/build/lib/rules/android/StampedAndroidManifest.java index 2314406390..49250db50b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/StampedAndroidManifest.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/StampedAndroidManifest.java @@ -16,13 +16,14 @@ package com.google.devtools.build.lib.rules.android; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import javax.annotation.Nullable; /** An {@link AndroidManifest} stamped with the correct package. */ @Immutable public class StampedAndroidManifest extends AndroidManifest { - StampedAndroidManifest(Artifact manifest, String pkg) { - super(manifest, pkg); + StampedAndroidManifest(Artifact manifest, @Nullable String pkg, boolean exported) { + super(manifest, pkg, exported); } @Override @@ -30,4 +31,14 @@ public class StampedAndroidManifest extends AndroidManifest { // This manifest is already stamped return this; } + + /** + * Gets the manifest artifact wrapped by this object. + * + *

The manifest is guaranteed to be stamped with the correct Android package. + */ + @Override + Artifact getManifest() { + return super.getManifest(); + } } -- cgit v1.2.3