diff options
author | tomlu <tomlu@google.com> | 2018-03-20 07:25:33 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-20 07:26:45 -0700 |
commit | 0acc6a7a989949635524424d0c88d7dbf32f38f8 (patch) | |
tree | 37af844ef8ca55c045602aa2e7183e35cce4be18 /src/tools/android/java/com/google/devtools | |
parent | 191add5b0e32c39faca09c25e6da6683a79dd8f7 (diff) |
Have the Android R class generators add the target label to the class jar.
This re-enables support for add_deps.
RELNOTES: None
PiperOrigin-RevId: 189737607
Diffstat (limited to 'src/tools/android/java/com/google/devtools')
6 files changed, 121 insertions, 11 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/AndroidCompiledResourceMergingAction.java b/src/tools/android/java/com/google/devtools/build/android/AndroidCompiledResourceMergingAction.java index 3fe61a32df..cd50c9fc06 100644 --- a/src/tools/android/java/com/google/devtools/build/android/AndroidCompiledResourceMergingAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/AndroidCompiledResourceMergingAction.java @@ -91,7 +91,8 @@ public class AndroidCompiledResourceMergingAction { executorService); logger.fine(String.format("Merging finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); - AndroidResourceOutputs.createClassJar(generatedSources, options.classJarOutput); + AndroidResourceOutputs.createClassJar( + generatedSources, options.classJarOutput, options.targetLabel, options.injectingRuleKind); logger.fine( String.format("Create classJar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); diff --git a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceMergingAction.java b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceMergingAction.java index c67806088f..cbd53a512e 100644 --- a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceMergingAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceMergingAction.java @@ -193,6 +193,26 @@ public class AndroidResourceMergingAction { help = "If passed, resource merge conflicts will be treated as errors instead of warnings" ) public boolean throwOnResourceConflict; + + @Option( + name = "targetLabel", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A label to add to the output jar's manifest as 'Target-Label'" + ) + public String targetLabel; + + @Option( + name = "injectingRuleKind", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A string to add to the output jar's manifest as 'Injecting-Rule-Kind'" + ) + public String injectingRuleKind; } public static void main(String[] args) throws Exception { @@ -267,7 +287,11 @@ public class AndroidResourceMergingAction { } if (options.classJarOutput != null) { - AndroidResourceOutputs.createClassJar(generatedSources, options.classJarOutput); + AndroidResourceOutputs.createClassJar( + generatedSources, + options.classJarOutput, + options.targetLabel, + options.injectingRuleKind); logger.fine( String.format( "Create classJar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); diff --git a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java index b277e010e2..41d942736f 100644 --- a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java +++ b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceOutputs.java @@ -37,13 +37,13 @@ import java.util.GregorianCalendar; import java.util.List; import java.util.Objects; import java.util.jar.Attributes; -import java.util.jar.JarFile; import java.util.jar.Manifest; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.CRC32; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import javax.annotation.Nullable; /** Collects all the functionationality for an action to create the final output artifacts. */ public class AndroidResourceOutputs { @@ -127,7 +127,8 @@ public class AndroidResourceOutputs { super(zip, root, null); } - private byte[] manifestContent() throws IOException { + private byte[] manifestContent(@Nullable String targetLabel, @Nullable String injectingRuleKind) + throws IOException { Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); @@ -135,6 +136,14 @@ public class AndroidResourceOutputs { if (attributes.getValue(createdBy) == null) { attributes.put(createdBy, "bazel"); } + if (targetLabel != null) { + // Enable add_deps support. add_deps expects this attribute in the jar manifest. + attributes.putValue("Target-Label", targetLabel); + } + if (injectingRuleKind != null) { + // add_deps support for aspects. Usually null. + attributes.putValue("Injecting-Rule-Kind", injectingRuleKind); + } ByteArrayOutputStream out = new ByteArrayOutputStream(); manifest.write(out); return out.toByteArray(); @@ -150,8 +159,10 @@ public class AndroidResourceOutputs { } } - void writeManifestContent() throws IOException { - addEntry(root.resolve(JarFile.MANIFEST_NAME), manifestContent()); + void writeManifestContent(@Nullable String targetLabel, @Nullable String injectingRuleKind) + throws IOException { + addEntry("META-INF/", new byte[] {}); + addEntry("META-INF/MANIFEST.MF", manifestContent(targetLabel, injectingRuleKind)); } } @@ -247,6 +258,10 @@ public class AndroidResourceOutputs { zipBuilder.addEntry(directoryPrefix + root.relativize(file), content, storageMethod); } + protected void addEntry(String entry, byte[] content) throws IOException { + zipBuilder.addEntry(entry, content, storageMethod); + } + protected void addDirEntry(Path file) throws IOException { Preconditions.checkArgument(file.startsWith(root), "%s does not start with %s", file, root); String entryName = directoryPrefix + root.relativize(file); @@ -336,14 +351,18 @@ public class AndroidResourceOutputs { } /** Creates a zip archive from all found R.class (and inner class) files. */ - public static void createClassJar(Path generatedClassesRoot, Path classJar) { + public static void createClassJar( + Path generatedClassesRoot, + Path classJar, + @Nullable String targetLabel, + @Nullable String injectingRuleKind) { try { Files.createDirectories(classJar.getParent()); try (final ZipBuilder zip = ZipBuilder.createFor(classJar)) { ClassJarBuildingVisitor visitor = new ClassJarBuildingVisitor(zip, generatedClassesRoot); Files.walkFileTree(generatedClassesRoot, visitor); + visitor.writeManifestContent(targetLabel, injectingRuleKind); visitor.writeEntries(); - visitor.writeManifestContent(); } } catch (IOException e) { throw new RuntimeException(e); diff --git a/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java b/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java index d6010bf0e9..b859904daa 100644 --- a/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java @@ -104,6 +104,26 @@ public class GenerateRobolectricResourceSymbolsAction { help = "Path for the generated java class jar." ) public Path classJarOutput; + + @Option( + name = "targetLabel", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A label to add to the output jar's manifest as 'Target-Label'" + ) + public String targetLabel; + + @Option( + name = "injectingRuleKind", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A string to add to the output jar's manifest as 'Injecting-Rule-Kind'" + ) + public String injectingRuleKind; } public static void main(String[] args) throws Exception { @@ -176,7 +196,8 @@ public class GenerateRobolectricResourceSymbolsAction { logger.fine(String.format("Merging finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); - AndroidResourceOutputs.createClassJar(generatedSources, options.classJarOutput); + AndroidResourceOutputs.createClassJar( + generatedSources, options.classJarOutput, options.targetLabel, options.injectingRuleKind); logger.fine( String.format("Create classJar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); diff --git a/src/tools/android/java/com/google/devtools/build/android/LibraryRClassGeneratorAction.java b/src/tools/android/java/com/google/devtools/build/android/LibraryRClassGeneratorAction.java index 2cce77e3cf..73a6c368e8 100644 --- a/src/tools/android/java/com/google/devtools/build/android/LibraryRClassGeneratorAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/LibraryRClassGeneratorAction.java @@ -96,6 +96,26 @@ public class LibraryRClassGeneratorAction { metadataTags = {OptionMetadataTag.DEPRECATED} ) public List<Path> deprecatedSymbols; + + @Option( + name = "targetLabel", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A label to add to the output jar's manifest as 'Target-Label'" + ) + public String targetLabel; + + @Option( + name = "injectingRuleKind", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A string to add to the output jar's manifest as 'Injecting-Rule-Kind'" + ) + public String injectingRuleKind; } public static void main(String[] args) throws Exception { @@ -131,7 +151,11 @@ public class LibraryRClassGeneratorAction { logger.fine( String.format("R writing finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); - AndroidResourceOutputs.createClassJar(scopedTmp.getPath(), options.classJarOutput); + AndroidResourceOutputs.createClassJar( + scopedTmp.getPath(), + options.classJarOutput, + options.targetLabel, + options.injectingRuleKind); logger.fine( String.format( "Creating class jar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); diff --git a/src/tools/android/java/com/google/devtools/build/android/RClassGeneratorAction.java b/src/tools/android/java/com/google/devtools/build/android/RClassGeneratorAction.java index bcfdb5fdb3..9213149e5b 100644 --- a/src/tools/android/java/com/google/devtools/build/android/RClassGeneratorAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/RClassGeneratorAction.java @@ -137,6 +137,26 @@ public class RClassGeneratorAction { help = "Path for the generated jar of R.class files." ) public Path classJarOutput; + + @Option( + name = "targetLabel", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A label to add to the output jar's manifest as 'Target-Label'" + ) + public String targetLabel; + + @Option( + name = "injectingRuleKind", + defaultValue = "null", + category = "input", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "A string to add to the output jar's manifest as 'Injecting-Rule-Kind'" + ) + public String injectingRuleKind; } public static void main(String[] args) throws Exception { @@ -190,7 +210,8 @@ public class RClassGeneratorAction { } // We write .class files to temp, then jar them up after (we create a dummy jar, even if // there are no class files). - AndroidResourceOutputs.createClassJar(classOutPath, options.classJarOutput); + AndroidResourceOutputs.createClassJar( + classOutPath, options.classJarOutput, options.targetLabel, options.injectingRuleKind); logger.fine( String.format("createClassJar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); } finally { |