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