Search in sources :

Example 1 with AbstractExecutionStep

use of com.facebook.buck.step.AbstractExecutionStep in project buck by facebook.

the class AndroidBinary method addAccumulateClassNamesStep.

public Supplier<ImmutableMap<String, HashCode>> addAccumulateClassNamesStep(final ImmutableSet<Path> classPathEntriesToDex, ImmutableList.Builder<Step> steps) {
    final ImmutableMap.Builder<String, HashCode> builder = ImmutableMap.builder();
    steps.add(new AbstractExecutionStep("collect_all_class_names") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            for (Path path : classPathEntriesToDex) {
                Optional<ImmutableSortedMap<String, HashCode>> hashes = AccumulateClassNamesStep.calculateClassHashes(context, getProjectFilesystem(), path);
                if (!hashes.isPresent()) {
                    return StepExecutionResult.ERROR;
                }
                builder.putAll(hashes.get());
            }
            return StepExecutionResult.SUCCESS;
        }
    });
    return Suppliers.memoize(builder::build);
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) HashCode(com.google.common.hash.HashCode) ExecutionContext(com.facebook.buck.step.ExecutionContext) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) Optional(java.util.Optional) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with AbstractExecutionStep

use of com.facebook.buck.step.AbstractExecutionStep 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)

Example 3 with AbstractExecutionStep

use of com.facebook.buck.step.AbstractExecutionStep in project buck by facebook.

the class AndroidBinary method getStepsForNativeAssets.

private void getStepsForNativeAssets(SourcePathResolver resolver, ImmutableList.Builder<Step> steps, Optional<ImmutableCollection<SourcePath>> nativeLibDirs, final Path libSubdirectory, final String metadataFilename, final APKModule module) {
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), libSubdirectory));
    // Filter, rename and copy the ndk libraries marked as assets.
    if (nativeLibDirs.isPresent()) {
        for (SourcePath nativeLibDir : nativeLibDirs.get()) {
            CopyNativeLibraries.copyNativeLibrary(getProjectFilesystem(), resolver.getAbsolutePath(nativeLibDir), libSubdirectory, cpuFilters, steps);
        }
    }
    // Input asset libraries are sorted in descending filesize order.
    final ImmutableSortedSet.Builder<Path> inputAssetLibrariesBuilder = ImmutableSortedSet.orderedBy((libPath1, libPath2) -> {
        try {
            ProjectFilesystem filesystem = getProjectFilesystem();
            int filesizeResult = -Long.compare(filesystem.getFileSize(libPath1), filesystem.getFileSize(libPath2));
            int pathnameResult = libPath1.compareTo(libPath2);
            return filesizeResult != 0 ? filesizeResult : pathnameResult;
        } catch (IOException e) {
            return 0;
        }
    });
    if (packageAssetLibraries || !module.isRootModule()) {
        if (enhancementResult.getCopyNativeLibraries().isPresent() && enhancementResult.getCopyNativeLibraries().get().containsKey(module)) {
            // Copy in cxx libraries marked as assets. Filtering and renaming was already done
            // in CopyNativeLibraries.getBuildSteps().
            Path cxxNativeLibsSrc = enhancementResult.getCopyNativeLibraries().get().get(module).getPathToNativeLibsAssetsDir();
            steps.add(CopyStep.forDirectory(getProjectFilesystem(), cxxNativeLibsSrc, libSubdirectory, CopyStep.DirectoryMode.CONTENTS_ONLY));
        }
        steps.add(// Step that populates a list of libraries and writes a metadata.txt to decompress.
        new AbstractExecutionStep("write_metadata_for_asset_libraries_" + module.getName()) {

            @Override
            public StepExecutionResult execute(ExecutionContext context) {
                ProjectFilesystem filesystem = getProjectFilesystem();
                try {
                    // Walk file tree to find libraries
                    filesystem.walkRelativeFileTree(libSubdirectory, new SimpleFileVisitor<Path>() {

                        @Override
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            if (!file.toString().endsWith(".so")) {
                                throw new IOException("unexpected file in lib directory");
                            }
                            inputAssetLibrariesBuilder.add(file);
                            return FileVisitResult.CONTINUE;
                        }
                    });
                    // Write a metadata
                    ImmutableList.Builder<String> metadataLines = ImmutableList.builder();
                    Path metadataOutput = libSubdirectory.resolve(metadataFilename);
                    for (Path libPath : inputAssetLibrariesBuilder.build()) {
                        // Should return something like x86/libfoo.so
                        Path relativeLibPath = libSubdirectory.relativize(libPath);
                        long filesize = filesystem.getFileSize(libPath);
                        String desiredOutput = relativeLibPath.toString();
                        String checksum = filesystem.computeSha256(libPath);
                        metadataLines.add(desiredOutput + ' ' + filesize + ' ' + checksum);
                    }
                    ImmutableList<String> metadata = metadataLines.build();
                    if (!metadata.isEmpty()) {
                        filesystem.writeLinesToPath(metadata, metadataOutput);
                    }
                } catch (IOException e) {
                    context.logError(e, "Writing metadata for asset libraries failed.");
                    return StepExecutionResult.ERROR;
                }
                return StepExecutionResult.SUCCESS;
            }
        });
    }
    if (compressAssetLibraries || !module.isRootModule()) {
        final ImmutableList.Builder<Path> outputAssetLibrariesBuilder = ImmutableList.builder();
        steps.add(new AbstractExecutionStep("rename_asset_libraries_as_temp_files_" + module.getName()) {

            @Override
            public StepExecutionResult execute(ExecutionContext context) {
                try {
                    ProjectFilesystem filesystem = getProjectFilesystem();
                    for (Path libPath : inputAssetLibrariesBuilder.build()) {
                        Path tempPath = libPath.resolveSibling(libPath.getFileName() + "~");
                        filesystem.move(libPath, tempPath);
                        outputAssetLibrariesBuilder.add(tempPath);
                    }
                    return StepExecutionResult.SUCCESS;
                } catch (IOException e) {
                    context.logError(e, "Renaming asset libraries failed");
                    return StepExecutionResult.ERROR;
                }
            }
        });
        // Concat and xz compress.
        Path libOutputBlob = libSubdirectory.resolve("libraries.blob");
        steps.add(new ConcatStep(getProjectFilesystem(), outputAssetLibrariesBuilder, libOutputBlob));
        int compressionLevel = xzCompressionLevel.orElse(XzStep.DEFAULT_COMPRESSION_LEVEL).intValue();
        steps.add(new XzStep(getProjectFilesystem(), libOutputBlob, libSubdirectory.resolve(SOLID_COMPRESSED_ASSET_LIBRARY_FILENAME), compressionLevel));
    }
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) XzStep(com.facebook.buck.step.fs.XzStep) IOException(java.io.IOException) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 4 with AbstractExecutionStep

use of com.facebook.buck.step.AbstractExecutionStep in project buck by facebook.

the class CxxInferAnalyze method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    buildableContext.recordArtifact(specsDir);
    buildableContext.recordArtifact(context.getSourcePathResolver().getRelativePath(getSourcePathToOutput()));
    return ImmutableList.<Step>builder().add(new MkdirStep(getProjectFilesystem(), specsDir)).add(new SymCopyStep(getProjectFilesystem(), captureAndAnalyzeRules.captureRules.stream().map(CxxInferCapture::getSourcePathToOutput).map(context.getSourcePathResolver()::getRelativePath).collect(MoreCollectors.toImmutableList()), resultsDir)).add(new AbstractExecutionStep("write_specs_path_list") {

        @Override
        public StepExecutionResult execute(ExecutionContext executionContext) throws IOException {
            try {
                ImmutableList<String> specsDirsWithAbsolutePath = getSpecsOfAllDeps().stream().map(input -> context.getSourcePathResolver().getAbsolutePath(input).toString()).collect(MoreCollectors.toImmutableList());
                getProjectFilesystem().writeLinesToPath(specsDirsWithAbsolutePath, specsPathList);
            } catch (IOException e) {
                executionContext.logError(e, "Error while writing specs path list file for the analyzer");
                return StepExecutionResult.ERROR;
            }
            return StepExecutionResult.SUCCESS;
        }
    }).add(new DefaultShellStep(getProjectFilesystem().getRootPath(), getAnalyzeCommand(), ImmutableMap.of())).build();
}
Also used : DefaultShellStep(com.facebook.buck.shell.DefaultShellStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) MkdirStep(com.facebook.buck.step.fs.MkdirStep) ImmutableList(com.google.common.collect.ImmutableList) SymCopyStep(com.facebook.buck.step.fs.SymCopyStep) IOException(java.io.IOException)

Example 5 with AbstractExecutionStep

use of com.facebook.buck.step.AbstractExecutionStep in project buck by facebook.

the class WriteStringTemplateRule method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    buildableContext.recordArtifact(output);
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    steps.add(new MkdirStep(getProjectFilesystem(), output.getParent()));
    steps.add(new StringTemplateStep(context.getSourcePathResolver().getAbsolutePath(template), getProjectFilesystem(), output, st -> {
        for (Map.Entry<String, String> ent : values.entrySet()) {
            st = st.add(ent.getKey(), ent.getValue());
        }
        return st;
    }));
    if (executable) {
        steps.add(new AbstractExecutionStep("chmod +x") {

            @Override
            public StepExecutionResult execute(ExecutionContext context) throws IOException {
                MoreFiles.makeExecutable(getProjectFilesystem().resolve(output));
                return StepExecutionResult.of(0, Optional.empty());
            }
        });
    }
    return steps.build();
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Step(com.facebook.buck.step.Step) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) BuildTarget(com.facebook.buck.model.BuildTarget) MkdirStep(com.facebook.buck.step.fs.MkdirStep) StringTemplateStep(com.facebook.buck.step.fs.StringTemplateStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Suppliers(com.google.common.base.Suppliers) Optional(java.util.Optional) MoreFiles(com.facebook.buck.io.MoreFiles) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) StringTemplateStep(com.facebook.buck.step.fs.StringTemplateStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) IOException(java.io.IOException) StringTemplateStep(com.facebook.buck.step.fs.StringTemplateStep)

Aggregations

AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)15 ExecutionContext (com.facebook.buck.step.ExecutionContext)15 StepExecutionResult (com.facebook.buck.step.StepExecutionResult)11 Path (java.nio.file.Path)11 SourcePath (com.facebook.buck.rules.SourcePath)10 Step (com.facebook.buck.step.Step)10 ImmutableList (com.google.common.collect.ImmutableList)10 MkdirStep (com.facebook.buck.step.fs.MkdirStep)9 IOException (java.io.IOException)9 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)6 CopyStep (com.facebook.buck.step.fs.CopyStep)4 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)4 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)3 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 BuildTarget (com.facebook.buck.model.BuildTarget)2 RmStep (com.facebook.buck.step.fs.RmStep)2 HashCode (com.google.common.hash.HashCode)2 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)2