diff options
6 files changed, 1 insertions, 426 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/BUILD b/src/tools/android/java/com/google/devtools/build/android/BUILD index d6a5d0d574..9a4d1369fd 100644 --- a/src/tools/android/java/com/google/devtools/build/android/BUILD +++ b/src/tools/android/java/com/google/devtools/build/android/BUILD @@ -69,7 +69,6 @@ java_library( "//src/tools/android/java/com/google/devtools/build/android/proto:serialize_format_proto", "//src/tools/android/java/com/google/devtools/build/android/resources", "//third_party:android_common", - "//third_party:apache_commons_compress", "//third_party:asm", "//third_party:guava", "//third_party:jsr305", diff --git a/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java b/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java index a892ec817d..bd77054d36 100644 --- a/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java +++ b/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java @@ -13,21 +13,16 @@ // limitations under the License. package com.google.devtools.build.android; +import com.android.builder.dependency.SymbolFileProvider; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; - -import com.android.builder.dependency.SymbolFileProvider; -import com.android.ide.common.res2.AssetSet; -import com.android.ide.common.res2.ResourceSet; - import java.io.File; import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; -import java.util.List; import java.util.Objects; import java.util.regex.Pattern; @@ -124,48 +119,6 @@ class DependencyAndroidData { return manifest; } - public AssetSet addToAssets(AssetSet assets) { - for (Path assetDir : assetDirs) { - assets.addSource(assetDir.toFile()); - } - return assets; - } - - public ResourceSet addToResourceSet(ResourceSet resources) { - for (Path resourceDir : resourceDirs) { - resources.addSource(resourceDir.toFile()); - } - return resources; - } - - /** - * Adds all the resource directories as ResourceSets. This acts a loose merge strategy as it does - * not test for overrides. - * - * @param resourceSets A list of resource sets to append to. - */ - void addAsResourceSets(List<ResourceSet> resourceSets) { - for (Path resourceDir : resourceDirs) { - ResourceSet set = new ResourceSet("dependency:" + resourceDir.toString()); - set.addSource(resourceDir.toFile()); - resourceSets.add(set); - } - } - - /** - * Adds all the asset directories as AssetSets. This acts a loose merge strategy as it does not - * test for overrides. - * - * @param assetSets A list of asset sets to append to. - */ - void addAsAssetSets(List<AssetSet> assetSets) { - for (Path assetDir : assetDirs) { - AssetSet set = new AssetSet("dependency:" + assetDir.toString()); - set.addSource(assetDir.toFile()); - assetSets.add(set); - } - } - @Override public String toString() { return String.format( @@ -196,16 +149,6 @@ class DependencyAndroidData { && Objects.equals(other.manifest, manifest); } - public DependencyAndroidData modify(ImmutableList<DirectoryModifier> modifiers) { - ImmutableList<Path> modifiedResources = resourceDirs; - ImmutableList<Path> modifiedAssets = assetDirs; - for (DirectoryModifier modifier : modifiers) { - modifiedAssets = modifier.modify(modifiedAssets); - modifiedResources = modifier.modify(modifiedResources); - } - return new DependencyAndroidData(modifiedResources, modifiedAssets, manifest, rTxt, null); - } - public void walk(final AndroidDataPathWalker pathWalker) throws IOException { for (Path path : resourceDirs) { pathWalker.walkResources(path); diff --git a/src/tools/android/java/com/google/devtools/build/android/DirectoryModifier.java b/src/tools/android/java/com/google/devtools/build/android/DirectoryModifier.java deleted file mode 100644 index 4952b139f8..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/DirectoryModifier.java +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package com.google.devtools.build.android; - -import com.google.common.collect.ImmutableList; - -import java.nio.file.Path; - - -/** - * And interface for apply modifiers to lists of resource directories. - * - * <p> - * This is a common entry point for resource hacks such as the files deduplication and - * the resource unpacking. - */ -interface DirectoryModifier { - public abstract ImmutableList<Path> modify(ImmutableList<Path> directories); -} diff --git a/src/tools/android/java/com/google/devtools/build/android/FileDeDuplicator.java b/src/tools/android/java/com/google/devtools/build/android/FileDeDuplicator.java deleted file mode 100644 index fa6d9bbf7c..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/FileDeDuplicator.java +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package com.google.devtools.build.android; - -import com.google.common.base.Preconditions; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableList.Builder; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Multimap; -import com.google.common.hash.HashCode; -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hasher; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.FileVisitOption; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.logging.Logger; - -/** - * Deduplicates identical files in the provided directories. - * <p> - * This is necessary for the andorid_resources deprecation -- the old style of inheritance - * required all relevant resources to be copied from each dependency. This means each resource is - * duplicated for each resource set. This modifier creates a sym link forest for each unique file - * on a first come, first serve basis. Which makes aapt and the merging code loads happier. - */ -public class FileDeDuplicator implements DirectoryModifier { - private static final Logger LOGGER = Logger.getLogger(FileDeDuplicator.class.getName()); - - private static final class ConditionalCopyVisitor extends SimpleFileVisitor<Path> { - private final Path newRoot; - private final Path workingDir; - private Multimap<Path, HashCode> seen; - private HashFunction hashFunction; - - private ConditionalCopyVisitor(Path newRoot, Path workingDir, - Multimap<Path, HashCode> seen, HashFunction hashFunction) { - this.newRoot = newRoot; - this.workingDir = workingDir; - this.seen = seen; - this.hashFunction = hashFunction; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) - throws IOException { - Files.createDirectories(newRoot.resolve(workingDir.relativize(dir))); - return super.preVisitDirectory(dir, attrs); - } - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Path relativePath = workingDir.relativize(file); - final HashCode fileHash = hashPath(file, hashFunction.newHasher()); - if (!seen.get(relativePath).contains(fileHash)) { - seen.get(relativePath).add(fileHash); - // TODO(bazel-team): Change to a symlink when the AOSP merge code supports symlinks. - Files.copy(file, newRoot.resolve(relativePath)); - // Files.createSymbolicLink(newRoot.resolve(workingDir.relativize(file)), file); - } else { - LOGGER.warning(String.format("Duplicated file %s [%s]", relativePath, file)); - } - return super.visitFile(file, attrs); - } - } - - private static HashCode hashPath(Path file, final Hasher hasher) throws IOException { - byte[] tmpBuffer = new byte[512]; - final InputStream in = Files.newInputStream(file); - for (int read = in.read(tmpBuffer); read > 0; read = in.read(tmpBuffer)) { - hasher.putBytes(tmpBuffer, 0, read); - } - final HashCode fileHash = hasher.hash(); - in.close(); - return fileHash; - } - - private final Multimap<Path, HashCode> seen; - private final HashFunction hashFunction; - private final Path out; - private final Path workingDirectory; - - public FileDeDuplicator(HashFunction hashFunction, Path out, Path workingDirectory) { - this.hashFunction = hashFunction; - this.workingDirectory = workingDirectory; - this.seen = HashMultimap.create(); - this.out = out; - } - - private ImmutableList<Path> conditionallyCopy(ImmutableList<Path> roots) - throws IOException { - final Builder<Path> builder = ImmutableList.builder(); - for (Path root : roots) { - Preconditions.checkArgument(root.startsWith(workingDirectory), - root + " must start with root " + workingDirectory + " from " + roots); - Preconditions.checkArgument(!root.equals(workingDirectory), - "Cannot deduplicate root directory: " + root + " from " + roots); - if (!seen.containsKey(root)) { - seen.put(root, null); - final Path newRoot = out.resolve(workingDirectory.relativize(root)); - Files.walkFileTree(root, ImmutableSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, - new ConditionalCopyVisitor(newRoot, root, seen, hashFunction)); - builder.add(newRoot); - } else { - // Duplicated directories are ok -- multiple files from different libraries - // can reside in the same directory, but duplicate files should not be seen mulitple times. - } - } - return builder.build(); - } - - @Override - public ImmutableList<Path> modify(ImmutableList<Path> directories) { - try { - return conditionallyCopy(directories); - } catch (IOException e) { - throw new RuntimeException(e); - } - } -} diff --git a/src/tools/android/java/com/google/devtools/build/android/PackedResourceTarExpander.java b/src/tools/android/java/com/google/devtools/build/android/PackedResourceTarExpander.java deleted file mode 100644 index ea31a862c0..0000000000 --- a/src/tools/android/java/com/google/devtools/build/android/PackedResourceTarExpander.java +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -package com.google.devtools.build.android; - -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableList.Builder; -import com.google.common.collect.ImmutableSet; - -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.FileVisitOption; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.logging.Logger; - -/** - * Unpacks specially named tar files in a resource file tree. - * - * <p>Scans a list of Resource directories looking for "raw/blaze_internal_packed_resources.tar". - * When found, it is unpacked into a new resource directory.</p> - */ -// TODO(bazel-team): Remove when Android support library version is handled by configurable -// attribute. -class PackedResourceTarExpander implements DirectoryModifier { - private static final Logger LOGGER = Logger.getLogger(PackedResourceTarExpander.class.getName()); - - private static final class ConditionallyLinkingVisitor extends SimpleFileVisitor<Path> { - - private final Path fileToexclude; - private Path out; - private Path workingDirectory; - - private ConditionallyLinkingVisitor(Path fileToExclude, Path out, Path workingDirectory) { - this.fileToexclude = fileToExclude; - this.out = out; - this.workingDirectory = workingDirectory; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) - throws IOException { - Files.createDirectories(out.resolve(workingDirectory.relativize(dir))); - return super.preVisitDirectory(dir, attrs); - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - throws IOException { - if (!fileToexclude.equals(file)) { - // TODO(bazel-team): Change to a symlink when the merge code supports symlinks. - Files.copy(file, out.resolve(workingDirectory.relativize(file))); - //Files.createSymbolicLink(out.resolve(workingDirectory.relativize(file)), file); - } - return super.visitFile(file, attrs); - } - } - - private final Path out; - private Path workingDirectory; - - public PackedResourceTarExpander(Path out, Path workingDirectory) { - this.out = out; - this.workingDirectory = workingDirectory; - } - - @Override - public ImmutableList<Path> modify(ImmutableList<Path> resourceRoots) { - final Builder<Path> outDirs = ImmutableList.builder(); - for (final Path unresolvedRoot : resourceRoots) { - Path root = unresolvedRoot.toAbsolutePath(); - try { - final Path packedResources = - root.resolve("raw/blaze_internal_packed_resources.tar"); - if (Files.exists(packedResources)) { - Preconditions.checkArgument(root.startsWith(workingDirectory), - "%s is not under %s", root, workingDirectory); - final Path resourcePrefix = workingDirectory.relativize(root); - final Path targetDirectory = out.resolve(resourcePrefix); - outDirs.add(targetDirectory); - copyRemainingResources(root, packedResources); - // Group the unpacked resource by the path they came from. - final Path tarOut = - out.resolve("blaze_internal_packed_resources").resolve(resourcePrefix); - unTarPackedResources(tarOut, packedResources); - outDirs.add(tarOut); - } else { - outDirs.add(root); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - return outDirs.build(); - } - - private void unTarPackedResources(final Path tarOut, final Path packedResources) - throws IOException { - LOGGER.fine(String.format("Found packed resources: %s", packedResources)); - try (InputStream inputStream = Files.newInputStream(packedResources); - TarArchiveInputStream tarStream = new TarArchiveInputStream(inputStream)) { - byte[] temp = new byte[4 * 1024]; - for (TarArchiveEntry entry = tarStream.getNextTarEntry(); entry != null; - entry = tarStream.getNextTarEntry()) { - if (!entry.isFile()) { - continue; - } - int read = tarStream.read(temp); - // packed tars can start with a ./. This can cause issues, so remove it. - final Path entryPath = tarOut.resolve(entry.getName().replace("^\\./", "")); - Files.createDirectories(entryPath.getParent()); - final OutputStream entryOutStream = Files.newOutputStream(entryPath); - while (read > -1) { - entryOutStream.write(temp, 0, read); - read = tarStream.read(temp); - } - entryOutStream.flush(); - entryOutStream.close(); - } - } - } - - private void copyRemainingResources(final Path resourcePath, final Path packedResources) - throws IOException { - Files.walkFileTree(resourcePath, ImmutableSet.of(FileVisitOption.FOLLOW_LINKS), - Integer.MAX_VALUE, new ConditionallyLinkingVisitor(packedResources, out, workingDirectory)); - } -} diff --git a/src/tools/android/java/com/google/devtools/build/android/UnvalidatedAndroidData.java b/src/tools/android/java/com/google/devtools/build/android/UnvalidatedAndroidData.java index 5d5cf9804f..9f43441692 100644 --- a/src/tools/android/java/com/google/devtools/build/android/UnvalidatedAndroidData.java +++ b/src/tools/android/java/com/google/devtools/build/android/UnvalidatedAndroidData.java @@ -15,16 +15,11 @@ package com.google.devtools.build.android; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; - -import com.android.ide.common.res2.AssetSet; -import com.android.ide.common.res2.ResourceSet; - import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; -import java.util.List; import java.util.Objects; import java.util.regex.Pattern; @@ -88,30 +83,6 @@ class UnvalidatedAndroidData { return manifest; } - public AssetSet addToAssets(AssetSet assets) { - for (Path assetDir : assetDirs) { - assets.addSource(assetDir.toFile()); - } - return assets; - } - - public ResourceSet addToResourceSet(ResourceSet resources) { - for (Path resourceDir : resourceDirs) { - resources.addSource(resourceDir.toFile()); - } - return resources; - } - - public UnvalidatedAndroidData modify(ImmutableList<DirectoryModifier> modifiers) { - ImmutableList<Path> modifiedResources = resourceDirs; - ImmutableList<Path> modifiedAssets = assetDirs; - for (DirectoryModifier modifier : modifiers) { - modifiedAssets = modifier.modify(modifiedAssets); - modifiedResources = modifier.modify(modifiedResources); - } - return new UnvalidatedAndroidData(modifiedResources, modifiedAssets, manifest); - } - @Override public String toString() { return String.format("UnvalidatedAndroidData(%s, %s, %s)", resourceDirs, assetDirs, manifest); @@ -136,32 +107,6 @@ class UnvalidatedAndroidData { && Objects.equals(other.manifest, manifest); } - /** - * Adds all the resource directories as ResourceSets. This acts a loose merge - * strategy as it does not test for overrides. - * @param resourceSets A list of resource sets to append to. - */ - void addAsResourceSets(List<ResourceSet> resourceSets) { - for (Path resourceDir : resourceDirs) { - ResourceSet set = new ResourceSet("primary:" + resourceDir.toString()); - set.addSource(resourceDir.toFile()); - resourceSets.add(set); - } - } - - /** - * Adds all the asset directories as AssetSets. This acts a loose merge - * strategy as it does not test for overrides. - * @param assetSets A list of asset sets to append to. - */ - void addAsAssetSets(List<AssetSet> assetSets) { - for (Path assetDir : assetDirs) { - AssetSet set = new AssetSet("primary:" + assetDir.toString()); - set.addSource(assetDir.toFile()); - assetSets.add(set); - } - } - public void walk(final AndroidDataPathWalker pathWalker) throws IOException { for (Path path : resourceDirs) { pathWalker.walkResources(path); |