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