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();
}
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);
}
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();
}
}
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());
}
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());
}
Aggregations