Search in sources :

Example 1 with TargetCpuType

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;
        }
    });
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) TargetCpuType(com.facebook.buck.android.NdkCxxPlatforms.TargetCpuType) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) CopyStep(com.facebook.buck.step.fs.CopyStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) IOException(java.io.IOException) CopyStep(com.facebook.buck.step.fs.CopyStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

TargetCpuType (com.facebook.buck.android.NdkCxxPlatforms.TargetCpuType)1 SourcePath (com.facebook.buck.rules.SourcePath)1 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)1 ExecutionContext (com.facebook.buck.step.ExecutionContext)1 Step (com.facebook.buck.step.Step)1 StepExecutionResult (com.facebook.buck.step.StepExecutionResult)1 CopyStep (com.facebook.buck.step.fs.CopyStep)1 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)1 MkdirStep (com.facebook.buck.step.fs.MkdirStep)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1