use of com.facebook.buck.android.NdkCxxPlatforms.TargetCpuType in project buck by facebook.
the class CopyNativeLibraries method copyNativeLibrary.
public static void copyNativeLibrary(final ProjectFilesystem filesystem, Path sourceDir, final Path destinationDir, ImmutableSet<TargetCpuType> cpuFilters, ImmutableList.Builder<Step> steps) {
if (cpuFilters.isEmpty()) {
steps.add(CopyStep.forDirectory(filesystem, sourceDir, destinationDir, CopyStep.DirectoryMode.CONTENTS_ONLY));
} else {
for (TargetCpuType cpuType : cpuFilters) {
Optional<String> abiDirectoryComponent = getAbiDirectoryComponent(cpuType);
Preconditions.checkState(abiDirectoryComponent.isPresent());
final Path libSourceDir = sourceDir.resolve(abiDirectoryComponent.get());
Path libDestinationDir = destinationDir.resolve(abiDirectoryComponent.get());
final MkdirStep mkDirStep = new MkdirStep(filesystem, libDestinationDir);
final CopyStep copyStep = CopyStep.forDirectory(filesystem, libSourceDir, libDestinationDir, CopyStep.DirectoryMode.CONTENTS_ONLY);
steps.add(new Step() {
@Override
public StepExecutionResult execute(ExecutionContext context) {
// different cells --- this check works by coincidence.
if (!filesystem.exists(libSourceDir)) {
return StepExecutionResult.SUCCESS;
}
if (mkDirStep.execute(context).isSuccess() && copyStep.execute(context).isSuccess()) {
return StepExecutionResult.SUCCESS;
}
return StepExecutionResult.ERROR;
}
@Override
public String getShortName() {
return "copy_native_libraries";
}
@Override
public String getDescription(ExecutionContext context) {
ImmutableList.Builder<String> stringBuilder = ImmutableList.builder();
stringBuilder.add(String.format("[ -d %s ]", libSourceDir.toString()));
stringBuilder.add(mkDirStep.getDescription(context));
stringBuilder.add(copyStep.getDescription(context));
return Joiner.on(" && ").join(stringBuilder.build());
}
});
}
}
// Rename native files named like "*-disguised-exe" to "lib*.so" so they will be unpacked
// by the Android package installer. Then they can be executed like normal binaries
// on the device.
steps.add(new AbstractExecutionStep("rename_native_executables") {
@Override
public StepExecutionResult execute(ExecutionContext context) {
final ImmutableSet.Builder<Path> executablesBuilder = ImmutableSet.builder();
try {
filesystem.walkRelativeFileTree(destinationDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith("-disguised-exe")) {
executablesBuilder.add(file);
}
return FileVisitResult.CONTINUE;
}
});
for (Path exePath : executablesBuilder.build()) {
Path fakeSoPath = Paths.get(MorePaths.pathWithUnixSeparators(exePath).replaceAll("/([^/]+)-disguised-exe$", "/lib$1.so"));
filesystem.move(exePath, fakeSoPath);
}
} catch (IOException e) {
context.logError(e, "Renaming native executables failed.");
return StepExecutionResult.ERROR;
}
return StepExecutionResult.SUCCESS;
}
});
}
Aggregations