use of com.facebook.buck.jvm.core.JavaPackageFinder 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.JavaPackageFinder in project buck by facebook.
the class CopyResourcesStepTest method testAddResourceCommandsWithBuildFileInJavaPackage.
@Test
public void testAddResourceCommandsWithBuildFileInJavaPackage() {
// Files:
// android/java/src/com/facebook/BUCK
// android/java/src/com/facebook/base/data.json
// android/java/src/com/facebook/common/util/data.json
BuildTarget buildTarget = BuildTargetFactory.newInstance("//android/java/src/com/facebook:resources");
JavaPackageFinder javaPackageFinder = createJavaPackageFinder();
ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
CopyResourcesStep step = new CopyResourcesStep(filesystem, new SourcePathResolver(ruleFinder), ruleFinder, buildTarget, ImmutableSet.of(new FakeSourcePath(filesystem, "android/java/src/com/facebook/base/data.json"), new FakeSourcePath(filesystem, "android/java/src/com/facebook/common/util/data.json")), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/com/facebook/lib__resources__classes"), javaPackageFinder);
List<? extends Step> expected = ImmutableList.of(new MkdirAndSymlinkFileStep(filesystem, filesystem.resolve("android/java/src/com/facebook/base/data.json"), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/com/facebook/lib__resources__classes/" + "com/facebook/base/data.json")), new MkdirAndSymlinkFileStep(filesystem, filesystem.resolve("android/java/src/com/facebook/common/util/data.json"), filesystem.getBuckPaths().getScratchDir().resolve("android/java/src/com/facebook/lib__resources__classes/" + "com/facebook/common/util/data.json")));
assertEquals(expected, step.buildSteps());
}
use of com.facebook.buck.jvm.core.JavaPackageFinder in project buck by facebook.
the class JavaSourceJarTest method shouldOnlyIncludePathBasedSources.
@Test
public void shouldOnlyIncludePathBasedSources() {
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
SourcePath fileBased = new FakeSourcePath("some/path/File.java");
SourcePath ruleBased = new DefaultBuildTargetSourcePath(BuildTargetFactory.newInstance("//cheese:cake"));
JavaPackageFinder finderStub = createNiceMock(JavaPackageFinder.class);
expect(finderStub.findJavaPackageFolder((Path) anyObject())).andStubReturn(Paths.get("cheese"));
expect(finderStub.findJavaPackage((Path) anyObject())).andStubReturn("cheese");
// No need to verify. It's a stub. I don't care about the interactions.
EasyMock.replay(finderStub);
JavaSourceJar rule = new JavaSourceJar(new FakeBuildRuleParamsBuilder("//example:target").build(), ImmutableSortedSet.of(fileBased, ruleBased), Optional.empty());
BuildContext buildContext = BuildContext.builder().setActionGraph(new ActionGraph(ImmutableList.of())).setSourcePathResolver(pathResolver).setJavaPackageFinder(finderStub).setEventBus(BuckEventBusFactory.newInstance()).build();
ImmutableList<Step> steps = rule.getBuildSteps(buildContext, new FakeBuildableContext());
// There should be a CopyStep per file being copied. Count 'em.
int copyStepsCount = FluentIterable.from(steps).filter(CopyStep.class::isInstance).size();
assertEquals(1, copyStepsCount);
}
use of com.facebook.buck.jvm.core.JavaPackageFinder in project buck by facebook.
the class ProjectTest method testSrcRoots.
/**
* A project_config()'s src_roots argument can be {@code []}, or a non-empty array.
* Each of these should be treated differently.
*/
@Test
public void testSrcRoots() throws Exception {
// Create a project_config() with src_roots=[].
BuildRuleResolver ruleResolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
BuildRule baseBuildRule = AndroidLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:base")).build(ruleResolver2);
ProjectConfig inPackageProjectConfig = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:project_config")).setSrcRule(baseBuildRule.getBuildTarget()).setSrcRoots(ImmutableList.of()).build(ruleResolver2);
// Verify that the correct source folders are created.
JavaPackageFinder javaPackageFinder = EasyMock.createMock(JavaPackageFinder.class);
EasyMock.expect(javaPackageFinder.findJavaPackage(Paths.get("java/com/example/base/module_java_com_example_base.iml"))).andReturn("com.example.base");
EasyMock.replay(javaPackageFinder);
ProjectWithModules projectWithModules2 = getModulesForActionGraph(ruleResolver2, ImmutableSortedSet.of(inPackageProjectConfig), javaPackageFinder, null);
EasyMock.verify(javaPackageFinder);
assertEquals(1, projectWithModules2.modules.size());
SerializableModule moduleWithPackagePrefix = projectWithModules2.modules.get(0);
assertListEquals("The current directory should be a source tmp with a package prefix " + "as well as the gen/ directory.", ImmutableList.of(new SourceFolder("file://$MODULE_DIR$", false, /* isTestSource */
"com.example.base"), SerializableModule.SourceFolder.GEN), moduleWithPackagePrefix.sourceFolders);
// Create a project_config() with src_roots=['src'].
BuildRuleResolver ruleResolver3 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
BuildRule baseBuildRule3 = AndroidLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:base")).build(ruleResolver3);
ProjectConfig hasSrcFolderProjectConfig = ProjectConfigBuilder.createBuilder(BuildTargetFactory.newInstance("//java/com/example/base:project_config")).setSrcRule(baseBuildRule3.getBuildTarget()).setSrcRoots(ImmutableList.of("src")).build(ruleResolver3);
ProjectWithModules projectWithModules3 = getModulesForActionGraph(ruleResolver3, ImmutableSortedSet.of(hasSrcFolderProjectConfig), null, /* javaPackageFinder */
null);
// Verify that the correct source folders are created.
assertEquals(1, projectWithModules3.modules.size());
SerializableModule moduleHasSrcFolder = projectWithModules3.modules.get(0);
assertListEquals("Both src/ and gen/ should be source folders.", ImmutableList.of(new SourceFolder("file://$MODULE_DIR$/src", false), SerializableModule.SourceFolder.GEN), moduleHasSrcFolder.sourceFolders);
}
use of com.facebook.buck.jvm.core.JavaPackageFinder in project buck by facebook.
the class ProjectTest method testProject.
/**
* This is an important test that verifies that the {@code no_dx} argument for an
* {@code android_binary} is handled appropriately when generating an IntelliJ project.
*/
@Test
public void testProject() throws Exception {
JavaPackageFinder javaPackageFinder = EasyMock.createMock(JavaPackageFinder.class);
EasyMock.expect(javaPackageFinder.findJavaPackage(Paths.get("foo/module_foo.iml"))).andReturn("");
EasyMock.expect(javaPackageFinder.findJavaPackage(Paths.get("bar/module_bar.iml"))).andReturn("");
EasyMock.replay(javaPackageFinder);
Pair<ProjectWithModules, BuildRuleResolver> projectWithModules = createActionGraphForTesting(javaPackageFinder);
Project project = projectWithModules.getFirst().project;
BuildRuleResolver resolver = projectWithModules.getSecond();
List<SerializableModule> modules = projectWithModules.getFirst().modules;
assertEquals("Should be one module for the java_library, one for the android_library, " + "one module for the android_resource, and one for each android_binary", 5, modules.size());
// Check the values of the module that corresponds to the android_library.
SerializableModule javaLibraryModule = modules.get(4);
assertSame(getRuleByBuildTarget("//java/src/com/facebook/exportlib:exportlib", resolver), javaLibraryModule.srcRule);
assertEquals("module_java_src_com_facebook_exportlib", javaLibraryModule.name);
assertEquals(Paths.get("java/src/com/facebook/exportlib/module_java_src_com_facebook_exportlib.iml"), javaLibraryModule.pathToImlFile);
assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.SRC), javaLibraryModule.sourceFolders);
// Check the dependencies.
SerializableDependentModule inheritedJdk = SerializableDependentModule.newInheritedJdk();
SerializableDependentModule guavaAsProvidedDep = SerializableDependentModule.newLibrary(guava.getBuildTarget(), "buck_out_gen_third_party_guava___guava___guava_10_0_1_jar");
guavaAsProvidedDep.scope = "PROVIDED";
assertListEquals(ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsProvidedDep, SerializableDependentModule.newStandardJdk(Optional.empty(), Optional.empty())), javaLibraryModule.getDependencies());
// Check the values of the module that corresponds to the android_library.
SerializableModule androidLibraryModule = modules.get(3);
assertSame(getRuleByBuildTarget("//java/src/com/facebook/base:base", resolver), androidLibraryModule.srcRule);
assertEquals("module_java_src_com_facebook_base", androidLibraryModule.name);
assertEquals(Paths.get("java/src/com/facebook/base/module_java_src_com_facebook_base.iml"), androidLibraryModule.pathToImlFile);
assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.SRC, new SourceFolder("file://$MODULE_DIR$/src-gen", false), SerializableModule.SourceFolder.GEN), androidLibraryModule.sourceFolders);
assertTrue(androidLibraryModule.hasAndroidFacet);
assertTrue(androidLibraryModule.isAndroidLibraryProject);
assertEquals(null, androidLibraryModule.proguardConfigPath);
assertEquals(null, androidLibraryModule.resFolder);
// Check the dependencies.
SerializableDependentModule androidResourceAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//android_res/base:res"), "module_android_res_base");
SerializableDependentModule childAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//java/src/com/facebook/child:child"), "module_java_src_com_facebook_child");
SerializableDependentModule exportDepsAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//java/src/com/facebook/exportlib:exportlib"), "module_java_src_com_facebook_exportlib");
assertListEquals(ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsProvidedDep, androidResourceAsProvidedDep, childAsProvidedDep, exportDepsAsProvidedDep, inheritedJdk), androidLibraryModule.getDependencies());
// Check the values of the module that corresponds to the android_binary that uses no_dx.
SerializableModule androidResourceModule = modules.get(0);
assertSame(getRuleByBuildTarget("//android_res/base:res", resolver), androidResourceModule.srcRule);
assertEquals(Paths.get("res"), androidResourceModule.resFolder);
// Check the values of the module that corresponds to the android_binary that uses no_dx.
SerializableModule androidBinaryModuleNoDx = modules.get(2);
assertSame(getRuleByBuildTarget("//foo:app", resolver), androidBinaryModuleNoDx.srcRule);
assertEquals("module_foo", androidBinaryModuleNoDx.name);
assertEquals(Paths.get("foo/module_foo.iml"), androidBinaryModuleNoDx.pathToImlFile);
assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.GEN), androidBinaryModuleNoDx.sourceFolders);
assertTrue(androidBinaryModuleNoDx.hasAndroidFacet);
assertFalse(androidBinaryModuleNoDx.isAndroidLibraryProject);
assertEquals(null, androidBinaryModuleNoDx.proguardConfigPath);
assertEquals(null, androidBinaryModuleNoDx.resFolder);
assertEquals(Paths.get("../keystore/debug.keystore"), androidBinaryModuleNoDx.keystorePath);
// Check the moduleDependencies.
SerializableDependentModule grandchildAsProvidedDep = SerializableDependentModule.newModule(BuildTargetFactory.newInstance("//java/src/com/facebook/grandchild:grandchild"), "module_java_src_com_facebook_grandchild");
SerializableDependentModule androidLibraryDep = SerializableDependentModule.newModule(androidLibraryModule.srcRule.getBuildTarget(), "module_java_src_com_facebook_base");
assertEquals(ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsProvidedDep, androidLibraryDep, androidResourceAsProvidedDep, childAsProvidedDep, exportDepsAsProvidedDep, grandchildAsProvidedDep, inheritedJdk), androidBinaryModuleNoDx.getDependencies());
// Check the values of the module that corresponds to the android_binary with an empty no_dx.
SerializableModule androidBinaryModuleEmptyNoDx = modules.get(1);
assertSame(getRuleByBuildTarget("//bar:app", resolver), androidBinaryModuleEmptyNoDx.srcRule);
assertEquals("module_bar", androidBinaryModuleEmptyNoDx.name);
assertEquals(Paths.get("bar/module_bar.iml"), androidBinaryModuleEmptyNoDx.pathToImlFile);
assertListEquals(ImmutableList.of(SerializableModule.SourceFolder.GEN), androidBinaryModuleEmptyNoDx.sourceFolders);
assertTrue(androidBinaryModuleEmptyNoDx.hasAndroidFacet);
assertFalse(androidBinaryModuleEmptyNoDx.isAndroidLibraryProject);
assertEquals(null, androidBinaryModuleEmptyNoDx.proguardConfigPath);
assertEquals(null, androidBinaryModuleEmptyNoDx.resFolder);
assertEquals(Paths.get("../keystore/debug.keystore"), androidBinaryModuleEmptyNoDx.keystorePath);
// Check the moduleDependencies.
SerializableDependentModule guavaAsCompiledDep = SerializableDependentModule.newLibrary(guava.getBuildTarget(), "buck_out_gen_third_party_guava___guava___guava_10_0_1_jar");
assertEquals("Important that Guava is listed as a 'COMPILED' dependency here because it is " + "only listed as a 'PROVIDED' dependency earlier.", ImmutableList.of(SerializableDependentModule.newSourceFolder(), guavaAsCompiledDep, androidLibraryDep, androidResourceAsProvidedDep, childAsProvidedDep, exportDepsAsProvidedDep, grandchildAsProvidedDep, inheritedJdk), androidBinaryModuleEmptyNoDx.getDependencies());
// Check that the correct data was extracted to populate the .idea/libraries directory.
BuildRule guava = getRuleByBuildTarget("//third_party/guava:guava", resolver);
assertSame(guava, Iterables.getOnlyElement(project.getLibraryJars()));
}
Aggregations