Search in sources :

Example 1 with TargetedNativeDirectory

use of com.android.bundle.Files.TargetedNativeDirectory in project bundletool by google.

the class AbiNativeLibrariesSplitter method split.

/**
 * Generates {@link ModuleSplit} objects dividing the native libraries by ABI.
 */
@Override
public ImmutableCollection<ModuleSplit> split(ModuleSplit moduleSplit) {
    if (!moduleSplit.getNativeConfig().isPresent()) {
        return ImmutableList.of(moduleSplit);
    }
    ImmutableList.Builder<ModuleSplit> splits = new ImmutableList.Builder<>();
    // Flatten all targeted directories.
    List<TargetedNativeDirectory> allTargetedDirectories = moduleSplit.getNativeConfig().get().getDirectoryList();
    // Currently we only support targeting via ABI, so grouping it by Targeting.equals() should be
    // enough.
    ImmutableMultimap<NativeDirectoryTargeting, TargetedNativeDirectory> targetingMap = Multimaps.index(allTargetedDirectories, TargetedNativeDirectory::getTargeting);
    // We need to know the exact set of ABIs that we will generate, to set alternatives correctly.
    ImmutableSet<Abi> abisToGenerate = targetingMap.keySet().stream().map(NativeDirectoryTargeting::getAbi).collect(toImmutableSet());
    // Any entries not claimed by the ABI splits will be returned in a separate split using the
    // original targeting.
    HashSet<ModuleEntry> leftOverEntries = new HashSet<>(moduleSplit.getEntries());
    for (NativeDirectoryTargeting targeting : targetingMap.keySet()) {
        ImmutableList<ModuleEntry> entriesList = targetingMap.get(targeting).stream().flatMap(directory -> moduleSplit.findEntriesUnderPath(directory.getPath())).collect(toImmutableList());
        ModuleSplit.Builder splitBuilder = moduleSplit.toBuilder().setApkTargeting(moduleSplit.getApkTargeting().toBuilder().setAbiTargeting(AbiTargeting.newBuilder().addValue(targeting.getAbi()).addAllAlternatives(Sets.difference(abisToGenerate, ImmutableSet.of(targeting.getAbi())))).build()).setMasterSplit(false).addMasterManifestMutator(withSplitsRequired(true)).setEntries(entriesList);
        splits.add(splitBuilder.build());
        leftOverEntries.removeAll(entriesList);
    }
    if (!leftOverEntries.isEmpty()) {
        splits.add(moduleSplit.toBuilder().setEntries(ImmutableList.copyOf(leftOverEntries)).build());
    }
    return splits.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) AbiTargeting(com.android.bundle.Targeting.AbiTargeting) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableCollection(com.google.common.collect.ImmutableCollection) Sets(com.google.common.collect.Sets) Multimaps(com.google.common.collect.Multimaps) HashSet(java.util.HashSet) ManifestMutator.withSplitsRequired(com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired) List(java.util.List) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ImmutableList(com.google.common.collect.ImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) NativeDirectoryTargeting(com.android.bundle.Targeting.NativeDirectoryTargeting) Abi(com.android.bundle.Targeting.Abi) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Abi(com.android.bundle.Targeting.Abi) NativeDirectoryTargeting(com.android.bundle.Targeting.NativeDirectoryTargeting) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) HashSet(java.util.HashSet)

Example 2 with TargetedNativeDirectory

use of com.android.bundle.Files.TargetedNativeDirectory in project bundletool by google.

the class SanitizerNativeLibrariesSplitter method split.

/**
 * Generates {@link ModuleSplit} objects dividing the native libraries by sanitizer.
 */
@Override
public ImmutableCollection<ModuleSplit> split(ModuleSplit moduleSplit) {
    if (!moduleSplit.getNativeConfig().isPresent()) {
        return ImmutableList.of(moduleSplit);
    }
    Set<ZipPath> hwasanDirs = new HashSet<>();
    for (TargetedNativeDirectory dir : moduleSplit.getNativeConfig().get().getDirectoryList()) {
        if (!dir.getTargeting().hasSanitizer()) {
            continue;
        }
        if (dir.getTargeting().getSanitizer().getAlias().equals(SanitizerAlias.HWADDRESS)) {
            hwasanDirs.add(ZipPath.create(dir.getPath()));
        }
    }
    List<ModuleEntry> hwasanEntries = moduleSplit.getEntries().stream().filter(entry -> hwasanDirs.contains(entry.getPath().subpath(0, 2))).collect(toImmutableList());
    if (hwasanEntries.isEmpty()) {
        return ImmutableList.of(moduleSplit);
    }
    List<ModuleEntry> nonHwasanEntries = moduleSplit.getEntries().stream().filter(entry -> !hwasanDirs.contains(entry.getPath().subpath(0, 2))).collect(toImmutableList());
    ModuleSplit hwasanSplit = moduleSplit.toBuilder().setApkTargeting(moduleSplit.getApkTargeting().toBuilder().setSanitizerTargeting(SanitizerTargeting.newBuilder().addValue(Sanitizer.newBuilder().setAlias(SanitizerAlias.HWADDRESS))).build()).setMasterSplit(false).setEntries(hwasanEntries).build();
    ModuleSplit nonHwasanSplit = moduleSplit.toBuilder().setEntries(nonHwasanEntries).build();
    return ImmutableList.of(hwasanSplit, nonHwasanSplit);
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Sanitizer(com.android.bundle.Targeting.Sanitizer) ImmutableCollection(com.google.common.collect.ImmutableCollection) Set(java.util.Set) SanitizerTargeting(com.android.bundle.Targeting.SanitizerTargeting) HashSet(java.util.HashSet) List(java.util.List) SanitizerAlias(com.android.bundle.Targeting.Sanitizer.SanitizerAlias) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ImmutableList(com.google.common.collect.ImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ZipPath(com.android.tools.build.bundletool.model.ZipPath) HashSet(java.util.HashSet)

Example 3 with TargetedNativeDirectory

use of com.android.bundle.Files.TargetedNativeDirectory in project bundletool by google.

the class NativeTargetingValidator method validateTargeting.

private static void validateTargeting(BundleModule module, NativeLibraries nativeLibraries) {
    for (TargetedNativeDirectory targetedDirectory : nativeLibraries.getDirectoryList()) {
        ZipPath path = ZipPath.create(targetedDirectory.getPath());
        NativeDirectoryTargeting targeting = targetedDirectory.getTargeting();
        if (!targeting.hasAbi()) {
            throw InvalidBundleException.builder().withUserMessage("Targeted native directory '%s' does not have the ABI dimension set.", targetedDirectory.getPath()).build();
        }
        if (!path.startsWith(LIB_DIRECTORY) || path.getNameCount() != 2) {
            throw InvalidBundleException.builder().withUserMessage("Path of targeted native directory must be in format 'lib/<directory>' but " + "found '%s'.", path).build();
        }
        if (BundleValidationUtils.directoryContainsNoFiles(module, path)) {
            throw InvalidBundleException.builder().withUserMessage("Targeted directory '%s' is empty.", path).build();
        }
    }
    SetView<String> libDirsWithoutTargeting = Sets.difference(module.findEntriesUnderPath(LIB_DIRECTORY).map(libFile -> libFile.getPath().subpath(0, 2).toString()).collect(toImmutableSet()), nativeLibraries.getDirectoryList().stream().map(TargetedNativeDirectory::getPath).collect(toImmutableSet()));
    if (!libDirsWithoutTargeting.isEmpty()) {
        throw InvalidBundleException.builder().withUserMessage("Following native directories are not targeted: %s", libDirsWithoutTargeting).build();
    }
}
Also used : NativeDirectoryTargeting(com.android.bundle.Targeting.NativeDirectoryTargeting) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 4 with TargetedNativeDirectory

use of com.android.bundle.Files.TargetedNativeDirectory in project bundletool by google.

the class TargetingGeneratorTest method generateTargetingForNativeLibraries_sanitizer.

@Test
public void generateTargetingForNativeLibraries_sanitizer() throws Exception {
    NativeLibraries nativeTargeting = generator.generateTargetingForNativeLibraries(ImmutableList.of("lib/arm64-v8a-hwasan"));
    List<TargetedNativeDirectory> directories = nativeTargeting.getDirectoryList();
    assertThat(directories).hasSize(1);
    assertThat(directories.get(0)).isEqualTo(TargetedNativeDirectory.newBuilder().setPath("lib/arm64-v8a-hwasan").setTargeting(nativeDirectoryTargeting(AbiAlias.ARM64_V8A, SanitizerAlias.HWADDRESS)).build());
}
Also used : TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) NativeLibraries(com.android.bundle.Files.NativeLibraries) Test(org.junit.Test)

Example 5 with TargetedNativeDirectory

use of com.android.bundle.Files.TargetedNativeDirectory in project bundletool by google.

the class TargetingGeneratorTest method generateTargetingForNativeLibraries_createsSingleDirectoryGroup.

@Test
public void generateTargetingForNativeLibraries_createsSingleDirectoryGroup() throws Exception {
    Collection<String> manyDirectories = Arrays.stream(AbiName.values()).map(AbiName::getPlatformName).map(abi -> "lib/" + abi).collect(toImmutableList());
    // Otherwise this test is useless.
    checkState(manyDirectories.size() > 1);
    NativeLibraries nativeTargeting = generator.generateTargetingForNativeLibraries(manyDirectories);
    List<TargetedNativeDirectory> directories = nativeTargeting.getDirectoryList();
    assertThat(directories).hasSize(manyDirectories.size());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AbiName(com.android.tools.build.bundletool.model.AbiName) Arrays(java.util.Arrays) ZipPath(com.android.tools.build.bundletool.model.ZipPath) TargetingUtils.textureCompressionTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.textureCompressionTargeting) RunWith(org.junit.runner.RunWith) TargetingUtils.alternativeLanguageTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.alternativeLanguageTargeting) TargetingUtils.deviceTierTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.deviceTierTargeting) ProtoTruth.assertThat(com.google.common.truth.extensions.proto.ProtoTruth.assertThat) TargetedApexImage(com.android.bundle.Files.TargetedApexImage) ImmutableList(com.google.common.collect.ImmutableList) AssetsDirectoryTargeting(com.android.bundle.Targeting.AssetsDirectoryTargeting) TargetingUtils.languageTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.languageTargeting) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) TargetingUtils.mergeAssetsTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeAssetsTargeting) ImmutableSet(com.google.common.collect.ImmutableSet) TargetingUtils.nativeDirectoryTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.nativeDirectoryTargeting) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NativeLibraries(com.android.bundle.Files.NativeLibraries) AbiAlias(com.android.bundle.Targeting.Abi.AbiAlias) ApexImages(com.android.bundle.Files.ApexImages) List(java.util.List) SanitizerAlias(com.android.bundle.Targeting.Sanitizer.SanitizerAlias) TargetedAssetsDirectory(com.android.bundle.Files.TargetedAssetsDirectory) Assets(com.android.bundle.Files.Assets) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) TextureCompressionFormatAlias(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias) TargetingUtils.assetsDirectoryTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.assetsDirectoryTargeting) Joiner(com.google.common.base.Joiner) TargetedNativeDirectory(com.android.bundle.Files.TargetedNativeDirectory) NativeLibraries(com.android.bundle.Files.NativeLibraries) AbiName(com.android.tools.build.bundletool.model.AbiName) Test(org.junit.Test)

Aggregations

TargetedNativeDirectory (com.android.bundle.Files.TargetedNativeDirectory)5 ZipPath (com.android.tools.build.bundletool.model.ZipPath)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 List (java.util.List)3 NativeLibraries (com.android.bundle.Files.NativeLibraries)2 NativeDirectoryTargeting (com.android.bundle.Targeting.NativeDirectoryTargeting)2 SanitizerAlias (com.android.bundle.Targeting.Sanitizer.SanitizerAlias)2 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)2 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2 Sets (com.google.common.collect.Sets)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Test (org.junit.Test)2 ApexImages (com.android.bundle.Files.ApexImages)1 Assets (com.android.bundle.Files.Assets)1 TargetedApexImage (com.android.bundle.Files.TargetedApexImage)1