Search in sources :

Example 1 with SuggestBuildRules

use of com.facebook.buck.jvm.core.SuggestBuildRules in project buck by facebook.

the class DefaultJavaLibrary method getBuildSteps.

/**
   * Building a java_library() rule entails compiling the .java files specified in the srcs
   * attribute. They are compiled into a directory under {@link BuckPaths#getScratchDir()}.
   */
@Override
public final ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    FluentIterable<JavaLibrary> declaredClasspathDeps = JavaLibraryClasspathProvider.getJavaLibraryDeps(getDepsForTransitiveClasspathEntries());
    // Always create the output directory, even if there are no .java files to compile because there
    // might be resources that need to be copied there.
    BuildTarget target = getBuildTarget();
    Path outputDirectory = getClassesDir(target, getProjectFilesystem());
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), outputDirectory));
    SuggestBuildRules suggestBuildRule = DefaultSuggestBuildRules.createSuggestBuildFunction(JAR_RESOLVER, context.getSourcePathResolver(), declaredClasspathDeps.toSet(), ImmutableSet.<JavaLibrary>builder().addAll(getTransitiveClasspathDeps()).add(this).build(), context.getActionGraph().getNodes());
    // We don't want to add these to the declared or transitive deps, since they're only used at
    // compile time.
    Collection<Path> provided = JavaLibraryClasspathProvider.getJavaLibraryDeps(providedDeps).transformAndConcat(JavaLibrary::getOutputClasspaths).filter(Objects::nonNull).transform(context.getSourcePathResolver()::getAbsolutePath).toSet();
    Iterable<Path> declaredClasspaths = declaredClasspathDeps.transformAndConcat(JavaLibrary::getOutputClasspaths).transform(context.getSourcePathResolver()::getAbsolutePath);
    // Only override the bootclasspath if this rule is supposed to compile Android code.
    ImmutableSortedSet<Path> declared = ImmutableSortedSet.<Path>naturalOrder().addAll(declaredClasspaths).addAll(additionalClasspathEntries.stream().map(e -> e.isLeft() ? context.getSourcePathResolver().getAbsolutePath(e.getLeft()) : checkIsAbsolute(e.getRight())).collect(MoreCollectors.toImmutableSet())).addAll(provided).build();
    // Make sure that this directory exists because ABI information will be written here.
    Step mkdir = new MakeCleanDirectoryStep(getProjectFilesystem(), getPathToAbiOutputDir());
    steps.add(mkdir);
    // If there are resources, then link them to the appropriate place in the classes directory.
    JavaPackageFinder finder = context.getJavaPackageFinder();
    if (resourcesRoot.isPresent()) {
        finder = new ResourcesRootPackageFinder(resourcesRoot.get(), finder);
    }
    steps.add(new CopyResourcesStep(getProjectFilesystem(), context.getSourcePathResolver(), ruleFinder, target, resources, outputDirectory, finder));
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), getOutputJarDirPath(target, getProjectFilesystem())));
    // into the built jar.
    if (!getJavaSrcs().isEmpty()) {
        ClassUsageFileWriter usedClassesFileWriter;
        if (trackClassUsage) {
            final Path usedClassesFilePath = getUsedClassesFilePath(getBuildTarget(), getProjectFilesystem());
            depFileOutputPath = getProjectFilesystem().getPathForRelativePath(usedClassesFilePath);
            usedClassesFileWriter = new DefaultClassUsageFileWriter(usedClassesFilePath);
            buildableContext.recordArtifact(usedClassesFilePath);
        } else {
            usedClassesFileWriter = NoOpClassUsageFileWriter.instance();
        }
        // This adds the javac command, along with any supporting commands.
        Path pathToSrcsList = BuildTargets.getGenPath(getProjectFilesystem(), getBuildTarget(), "__%s__srcs");
        steps.add(new MkdirStep(getProjectFilesystem(), pathToSrcsList.getParent()));
        Path scratchDir = BuildTargets.getGenPath(getProjectFilesystem(), target, "lib__%s____working_directory");
        steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), scratchDir));
        Optional<Path> workingDirectory = Optional.of(scratchDir);
        ImmutableSortedSet<Path> javaSrcs = getJavaSrcs().stream().map(context.getSourcePathResolver()::getRelativePath).collect(MoreCollectors.toImmutableSortedSet());
        compileStepFactory.createCompileToJarStep(context, javaSrcs, target, context.getSourcePathResolver(), ruleFinder, getProjectFilesystem(), declared, outputDirectory, workingDirectory, pathToSrcsList, Optional.of(suggestBuildRule), postprocessClassesCommands, ImmutableSortedSet.of(outputDirectory), /* mainClass */
        Optional.empty(), manifestFile.map(context.getSourcePathResolver()::getAbsolutePath), outputJar.get(), usedClassesFileWriter, /* output params */
        steps, buildableContext, classesToRemoveFromJar);
    }
    if (outputJar.isPresent()) {
        Path output = outputJar.get();
        // No source files, only resources
        if (getJavaSrcs().isEmpty()) {
            steps.add(new JarDirectoryStep(getProjectFilesystem(), output, ImmutableSortedSet.of(outputDirectory), /* mainClass */
            null, manifestFile.map(context.getSourcePathResolver()::getAbsolutePath).orElse(null), true, classesToRemoveFromJar));
        }
        buildableContext.recordArtifact(output);
    }
    JavaLibraryRules.addAccumulateClassNamesStep(this, buildableContext, context.getSourcePathResolver(), steps);
    return steps.build();
}
Also used : ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) ClassPath(com.google.common.reflect.ClassPath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) JavaPackageFinder(com.facebook.buck.jvm.core.JavaPackageFinder) BuildTarget(com.facebook.buck.model.BuildTarget) Objects(java.util.Objects) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) SuggestBuildRules(com.facebook.buck.jvm.core.SuggestBuildRules)

Example 2 with SuggestBuildRules

use of com.facebook.buck.jvm.core.SuggestBuildRules in project buck by facebook.

the class DefaultSuggestBuildRulesTest method suggestTopologicallyDistantDependency.

@Test
public void suggestTopologicallyDistantDependency() throws NoSuchBuildTargetException {
    // TODO(grumpyjames): stop duplicating source/symbol names if possible
    TargetNode<?, ?> libraryTwoNode = javaLibrary("//:libtwo", "com/facebook/Bar.java");
    TargetNode<?, ?> parentNode = javaLibrary("//:parent", "com/facebook/Foo.java", libraryTwoNode);
    TargetNode<?, ?> grandparentNode = javaLibrary("//:grandparent", "com/parent/OldManRiver.java", parentNode);
    TargetGraph targetGraph = TargetGraphFactory.newInstance(libraryTwoNode, parentNode, grandparentNode);
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    BuildRule libraryTwo = resolver.requireRule(libraryTwoNode.getBuildTarget());
    BuildRule parent = resolver.requireRule(parentNode.getBuildTarget());
    BuildRule grandparent = resolver.requireRule(grandparentNode.getBuildTarget());
    ImmutableMap<Path, String> jarPathToSymbols = ImmutableMap.of(pathResolver.getAbsolutePath(parent.getSourcePathToOutput()), "com.facebook.Foo", pathResolver.getAbsolutePath(libraryTwo.getSourcePathToOutput()), "com.facebook.Bar");
    ImmutableSetMultimap<JavaLibrary, Path> transitiveClasspathEntries = fromLibraries(pathResolver, libraryTwo, parent, grandparent);
    SuggestBuildRules.JarResolver jarResolver = createJarResolver(jarPathToSymbols);
    SuggestBuildRules suggestFn = DefaultSuggestBuildRules.createSuggestBuildFunction(jarResolver, pathResolver, ImmutableSet.of(), transitiveClasspathEntries.keySet(), ImmutableList.of(libraryTwo, parent, grandparent));
    final ImmutableSet<String> suggestions = suggestFn.suggest(ImmutableSet.of("com.facebook.Bar"));
    assertEquals(ImmutableSet.of("//:libtwo"), suggestions);
}
Also used : Path(java.nio.file.Path) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SuggestBuildRules(com.facebook.buck.jvm.core.SuggestBuildRules) Test(org.junit.Test)

Example 3 with SuggestBuildRules

use of com.facebook.buck.jvm.core.SuggestBuildRules in project buck by facebook.

the class DefaultSuggestBuildRulesTest method suggestTheTopologicallyNearestDependency.

@Test
public void suggestTheTopologicallyNearestDependency() throws NoSuchBuildTargetException {
    // TODO(grumpyjames): stop duplicating source/symbol names if possible
    TargetNode<?, ?> libraryTwoNode = javaLibrary("//:libtwo", "com/facebook/Foo.java");
    TargetNode<?, ?> parentNode = javaLibrary("//:parent", "com/facebook/Foo.java", libraryTwoNode);
    TargetNode<?, ?> grandparentNode = javaLibrary("//:grandparent", "com/parent/OldManRiver.java", parentNode);
    TargetGraph targetGraph = TargetGraphFactory.newInstance(libraryTwoNode, parentNode, grandparentNode);
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
    BuildRule libraryTwo = resolver.requireRule(libraryTwoNode.getBuildTarget());
    BuildRule parent = resolver.requireRule(parentNode.getBuildTarget());
    BuildRule grandparent = resolver.requireRule(grandparentNode.getBuildTarget());
    ImmutableMap<Path, String> jarPathToSymbols = ImmutableMap.of(pathResolver.getAbsolutePath(parent.getSourcePathToOutput()), "com.facebook.Foo", pathResolver.getAbsolutePath(libraryTwo.getSourcePathToOutput()), "com.facebook.Foo");
    ImmutableSetMultimap<JavaLibrary, Path> transitiveClasspathEntries = fromLibraries(pathResolver, libraryTwo, parent, grandparent);
    SuggestBuildRules.JarResolver jarResolver = createJarResolver(jarPathToSymbols);
    SuggestBuildRules suggestFn = DefaultSuggestBuildRules.createSuggestBuildFunction(jarResolver, pathResolver, ImmutableSet.of(), transitiveClasspathEntries.keySet(), ImmutableList.of(libraryTwo, parent, grandparent));
    final ImmutableSet<String> suggestions = suggestFn.suggest(ImmutableSet.of("com.facebook.Foo"));
    assertEquals(ImmutableSet.of("//:parent"), suggestions);
}
Also used : Path(java.nio.file.Path) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SuggestBuildRules(com.facebook.buck.jvm.core.SuggestBuildRules) Test(org.junit.Test)

Aggregations

SuggestBuildRules (com.facebook.buck.jvm.core.SuggestBuildRules)3 Path (java.nio.file.Path)3 BuildRule (com.facebook.buck.rules.BuildRule)2 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)2 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)2 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)2 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)2 TargetGraph (com.facebook.buck.rules.TargetGraph)2 Test (org.junit.Test)2 JavaPackageFinder (com.facebook.buck.jvm.core.JavaPackageFinder)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 ArchiveMemberSourcePath (com.facebook.buck.rules.ArchiveMemberSourcePath)1 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 Step (com.facebook.buck.step.Step)1 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)1 MkdirStep (com.facebook.buck.step.fs.MkdirStep)1 ImmutableList (com.google.common.collect.ImmutableList)1 ClassPath (com.google.common.reflect.ClassPath)1 Objects (java.util.Objects)1