Search in sources :

Example 6 with AbstractExecutionStep

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

the class PreDexMerge method addMetadataWriteStep.

private void addMetadataWriteStep(final PreDexedFilesSorter.Result result, final ImmutableList.Builder<Step> steps, final Path metadataFilePath) {
    StringBuilder nameBuilder = new StringBuilder(30);
    final boolean isRootModule = result.apkModule.equals(apkModuleGraph.getRootAPKModule());
    final String storeId = result.apkModule.getName();
    nameBuilder.append("write_");
    if (!isRootModule) {
        nameBuilder.append(storeId);
        nameBuilder.append("_");
    }
    nameBuilder.append("metadata_txt");
    steps.add(new AbstractExecutionStep(nameBuilder.toString()) {

        @Override
        public StepExecutionResult execute(ExecutionContext executionContext) {
            Map<Path, DexWithClasses> metadataTxtEntries = result.metadataTxtDexEntries;
            List<String> lines = Lists.newArrayListWithCapacity(metadataTxtEntries.size());
            lines.add(".id " + storeId);
            if (isRootModule) {
                if (dexSplitMode.getDexStore() == DexStore.RAW) {
                    lines.add(".root_relative");
                }
            } else {
                for (APKModule dependency : apkModuleGraph.getGraph().getOutgoingNodesFor(result.apkModule)) {
                    lines.add(".requires " + dependency.getName());
                }
            }
            try {
                for (Map.Entry<Path, DexWithClasses> entry : metadataTxtEntries.entrySet()) {
                    Path pathToSecondaryDex = entry.getKey();
                    String containedClass = Iterables.get(entry.getValue().getClassNames(), 0);
                    containedClass = containedClass.replace('/', '.');
                    Sha1HashCode hash = getProjectFilesystem().computeSha1(pathToSecondaryDex);
                    lines.add(String.format("%s %s %s", pathToSecondaryDex.getFileName(), hash, containedClass));
                }
                getProjectFilesystem().writeLinesToPath(lines, metadataFilePath);
            } catch (IOException e) {
                executionContext.logError(e, "Failed when writing metadata.txt multi-dex.");
                return StepExecutionResult.ERROR;
            }
            return StepExecutionResult.SUCCESS;
        }
    });
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) IOException(java.io.IOException) ExecutionContext(com.facebook.buck.step.ExecutionContext) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractMap(java.util.AbstractMap)

Example 7 with AbstractExecutionStep

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

the class UnzipAar method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), unpackDirectory));
    steps.add(new UnzipStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(aarFile), unpackDirectory));
    steps.add(new TouchStep(getProjectFilesystem(), getProguardConfig()));
    steps.add(new MkdirStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(getAssetsDirectory())));
    steps.add(new MkdirStep(getProjectFilesystem(), getNativeLibsDirectory()));
    steps.add(new TouchStep(getProjectFilesystem(), getTextSymbolsFile()));
    // We take the classes.jar file that is required to exist in an .aar and merge it with any
    // .jar files under libs/ into an "uber" jar. We do this for simplicity because we do not know
    // how many entries there are in libs/ at graph enhancement time, but we need to make sure
    // that all of the .class files in the .aar get packaged. As it is implemented today, an
    // android_library that depends on an android_prebuilt_aar can compile against anything in the
    // .aar's classes.jar or libs/.
    steps.add(new MkdirStep(getProjectFilesystem(), uberClassesJar.getParent()));
    steps.add(new AbstractExecutionStep("create_uber_classes_jar") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            Path libsDirectory = unpackDirectory.resolve("libs");
            boolean dirDoesNotExistOrIsEmpty;
            if (!getProjectFilesystem().exists(libsDirectory)) {
                dirDoesNotExistOrIsEmpty = true;
            } else {
                try {
                    dirDoesNotExistOrIsEmpty = getProjectFilesystem().getDirectoryContents(libsDirectory).isEmpty();
                } catch (IOException e) {
                    context.logError(e, "Failed to get directory contents of %s", libsDirectory);
                    return StepExecutionResult.ERROR;
                }
            }
            Path classesJar = unpackDirectory.resolve("classes.jar");
            JavacEventSinkToBuckEventBusBridge eventSink = new JavacEventSinkToBuckEventBusBridge(context.getBuckEventBus());
            if (!getProjectFilesystem().exists(classesJar)) {
                try {
                    JarDirectoryStepHelper.createEmptyJarFile(getProjectFilesystem(), classesJar, eventSink, context.getStdErr());
                } catch (IOException e) {
                    context.logError(e, "Failed to create empty jar %s", classesJar);
                    return StepExecutionResult.ERROR;
                }
            }
            if (dirDoesNotExistOrIsEmpty) {
                try {
                    getProjectFilesystem().copy(classesJar, uberClassesJar, ProjectFilesystem.CopySourceMode.FILE);
                } catch (IOException e) {
                    context.logError(e, "Failed to copy from %s to %s", classesJar, uberClassesJar);
                    return StepExecutionResult.ERROR;
                }
            } else {
                // Glob all of the contents from classes.jar and the entries in libs/ into a single JAR.
                ImmutableSortedSet.Builder<Path> entriesToJarBuilder = ImmutableSortedSet.naturalOrder();
                entriesToJarBuilder.add(classesJar);
                try {
                    entriesToJarBuilder.addAll(getProjectFilesystem().getDirectoryContents(libsDirectory));
                } catch (IOException e) {
                    context.logError(e, "Failed to get directory contents of %s", libsDirectory);
                    return StepExecutionResult.ERROR;
                }
                ImmutableSortedSet<Path> entriesToJar = entriesToJarBuilder.build();
                try {
                    JarDirectoryStepHelper.createJarFile(getProjectFilesystem(), uberClassesJar, entriesToJar, /* mainClass */
                    Optional.empty(), /* manifestFile */
                    Optional.empty(), /* mergeManifests */
                    true, /* blacklist */
                    ImmutableSet.of(), eventSink, context.getStdErr());
                } catch (IOException e) {
                    context.logError(e, "Failed to jar %s into %s", entriesToJar, uberClassesJar);
                    return StepExecutionResult.ERROR;
                }
            }
            return StepExecutionResult.SUCCESS;
        }
    });
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToTextSymbolsDir));
    steps.add(new ExtractFromAndroidManifestStep(getAndroidManifest(), getProjectFilesystem(), buildableContext, METADATA_KEY_FOR_R_DOT_JAVA_PACKAGE, pathToRDotJavaPackageFile));
    steps.add(CopyStep.forFile(getProjectFilesystem(), getTextSymbolsFile(), pathToTextSymbolsFile));
    buildableContext.recordArtifact(unpackDirectory);
    buildableContext.recordArtifact(uberClassesJar);
    buildableContext.recordArtifact(pathToTextSymbolsFile);
    buildableContext.recordArtifact(pathToRDotJavaPackageFile);
    return steps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) UnzipStep(com.facebook.buck.zip.UnzipStep) 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) CopyStep(com.facebook.buck.step.fs.CopyStep) UnzipStep(com.facebook.buck.zip.UnzipStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) TouchStep(com.facebook.buck.step.fs.TouchStep) TouchStep(com.facebook.buck.step.fs.TouchStep) IOException(java.io.IOException) JavacEventSinkToBuckEventBusBridge(com.facebook.buck.jvm.java.JavacEventSinkToBuckEventBusBridge) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep)

Example 8 with AbstractExecutionStep

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

the class AbstractExecutionStepTest method testDescriptionGetters.

@Test
public void testDescriptionGetters() {
    String description = "How I describe myself.";
    Step step = new AbstractExecutionStep(description) {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            return StepExecutionResult.SUCCESS;
        }
    };
    ExecutionContext context = TestExecutionContext.newInstance();
    assertEquals(description, step.getShortName());
    assertEquals(description, step.getDescription(context));
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) Step(com.facebook.buck.step.Step) Test(org.junit.Test)

Example 9 with AbstractExecutionStep

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

the class NdkLibrary method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, final BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    // .so files are written to the libs/ subdirectory of the output directory.
    // All of them should be recorded via the BuildableContext.
    Path binDirectory = buildArtifactsDirectory.resolve("libs");
    steps.add(new RmStep(getProjectFilesystem(), makefile));
    steps.add(new MkdirStep(getProjectFilesystem(), makefile.getParent()));
    steps.add(new WriteFileStep(getProjectFilesystem(), makefileContents, makefile, false));
    steps.add(new NdkBuildStep(getProjectFilesystem(), root, makefile, buildArtifactsDirectory, binDirectory, flags, macroExpander));
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), genDirectory));
    steps.add(CopyStep.forDirectory(getProjectFilesystem(), binDirectory, genDirectory, CopyStep.DirectoryMode.CONTENTS_ONLY));
    buildableContext.recordArtifact(genDirectory);
    // Some tools need to inspect .so files whose symbols haven't been stripped, so cache these too.
    // However, the intermediate object files are huge and we have no interest in them, so filter
    // them out.
    steps.add(new AbstractExecutionStep("cache_unstripped_so") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            try {
                Set<Path> unstrippedSharedObjs = getProjectFilesystem().getFilesUnderPath(buildArtifactsDirectory, input -> input.toString().endsWith(".so"));
                for (Path path : unstrippedSharedObjs) {
                    buildableContext.recordArtifact(path);
                }
            } catch (IOException e) {
                context.logError(e, "Failed to cache intermediate artifacts of %s.", getBuildTarget());
                return StepExecutionResult.ERROR;
            }
            return StepExecutionResult.SUCCESS;
        }
    });
    return steps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) RmStep(com.facebook.buck.step.fs.RmStep) LIBRARY(com.facebook.buck.rules.BuildableProperties.Kind.LIBRARY) Step(com.facebook.buck.step.Step) CopyStep(com.facebook.buck.step.fs.CopyStep) SourcePath(com.facebook.buck.rules.SourcePath) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Function(com.google.common.base.Function) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) BuildableProperties(com.facebook.buck.rules.BuildableProperties) BuildableContext(com.facebook.buck.rules.BuildableContext) Set(java.util.Set) IOException(java.io.IOException) BuildTarget(com.facebook.buck.model.BuildTarget) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) ANDROID(com.facebook.buck.rules.BuildableProperties.Kind.ANDROID) List(java.util.List) BuildContext(com.facebook.buck.rules.BuildContext) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) BuildTargets(com.facebook.buck.model.BuildTargets) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Set(java.util.Set) 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) RmStep(com.facebook.buck.step.fs.RmStep) 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) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep) IOException(java.io.IOException) RmStep(com.facebook.buck.step.fs.RmStep) ExecutionContext(com.facebook.buck.step.ExecutionContext) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep)

Example 10 with AbstractExecutionStep

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

the class JavaSymbolsRule method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    Step mkdirStep = new MkdirStep(getProjectFilesystem(), outputPath.getParent());
    Step extractSymbolsStep = new AbstractExecutionStep("java-symbols") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) throws IOException {
            Symbols symbols = symbolsFinder.extractSymbols();
            Symbols symbolsToSerialize;
            if (generatedSymbols.isEmpty()) {
                symbolsToSerialize = symbols;
            } else {
                symbolsToSerialize = new Symbols(Iterables.concat(symbols.provided, generatedSymbols), symbols.required, symbols.exported);
            }
            try (OutputStream output = getProjectFilesystem().newFileOutputStream(outputPath)) {
                context.getObjectMapper().writeValue(output, symbolsToSerialize);
            }
            return StepExecutionResult.SUCCESS;
        }
    };
    return ImmutableList.of(mkdirStep, extractSymbolsStep);
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) OutputStream(java.io.OutputStream) Step(com.facebook.buck.step.Step) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep)

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