Search in sources :

Example 11 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class RobolectricTestDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    JavacOptions javacOptions = JavacOptionsFactory.create(templateOptions, params, resolver, ruleFinder, args);
    AndroidLibraryGraphEnhancer graphEnhancer = new AndroidLibraryGraphEnhancer(params.getBuildTarget(), params.copyReplacingExtraDeps(Suppliers.ofInstance(resolver.getAllRules(args.exportedDeps))), javacOptions, DependencyMode.TRANSITIVE, /* forceFinalResourceIds */
    true, /* resourceUnionPackage */
    Optional.empty(), /* rName */
    Optional.empty(), args.useOldStyleableFormat);
    if (CalculateAbi.isAbiTarget(params.getBuildTarget())) {
        if (params.getBuildTarget().getFlavors().contains(AndroidLibraryGraphEnhancer.DUMMY_R_DOT_JAVA_FLAVOR)) {
            return graphEnhancer.getBuildableForAndroidResourcesAbi(resolver, ruleFinder);
        }
        BuildTarget testTarget = CalculateAbi.getLibraryTarget(params.getBuildTarget());
        BuildRule testRule = resolver.requireRule(testTarget);
        return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params, Preconditions.checkNotNull(testRule.getSourcePathToOutput()));
    }
    ImmutableList<String> vmArgs = args.vmArgs;
    Optional<DummyRDotJava> dummyRDotJava = graphEnhancer.getBuildableForAndroidResources(resolver, /* createBuildableIfEmpty */
    true);
    ImmutableSet<Either<SourcePath, Path>> additionalClasspathEntries = ImmutableSet.of();
    if (dummyRDotJava.isPresent()) {
        additionalClasspathEntries = ImmutableSet.of(Either.ofLeft(dummyRDotJava.get().getSourcePathToOutput()));
        ImmutableSortedSet<BuildRule> newExtraDeps = ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getExtraDeps().get()).add(dummyRDotJava.get()).build();
        params = params.copyReplacingExtraDeps(Suppliers.ofInstance(newExtraDeps));
    }
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    JavaTestDescription.CxxLibraryEnhancement cxxLibraryEnhancement = new JavaTestDescription.CxxLibraryEnhancement(params, args.useCxxLibraries, args.cxxLibraryWhitelist, resolver, ruleFinder, cxxPlatform);
    params = cxxLibraryEnhancement.updatedParams;
    // Rewrite dependencies on tests to actually depend on the code which backs the test.
    BuildRuleParams testsLibraryParams = params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getDeclaredDeps().get()).addAll(BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), resolver.getAllRules(args.providedDeps)))).build()), Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>naturalOrder().addAll(params.getExtraDeps().get()).addAll(ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))).build())).withAppendedFlavor(JavaTest.COMPILED_TESTS_LIBRARY_FLAVOR);
    JavaLibrary testsLibrary = resolver.addToIndex(new DefaultJavaLibrary(testsLibraryParams, pathResolver, ruleFinder, args.srcs, validateResources(pathResolver, params.getProjectFilesystem(), args.resources), javacOptions.getGeneratedSourceFolderName(), args.proguardConfig, /* postprocessClassesCommands */
    ImmutableList.of(), /* exportDeps */
    ImmutableSortedSet.of(), /* providedDeps */
    resolver.getAllRules(args.providedDeps), JavaLibraryRules.getAbiInputs(resolver, testsLibraryParams.getDeps()), javacOptions.trackClassUsage(), additionalClasspathEntries, new JavacToJarStepFactory(javacOptions, new BootClasspathAppender()), args.resourcesRoot, args.manifestFile, args.mavenCoords, /* tests */
    ImmutableSortedSet.of(), /* classesToRemoveFromJar */
    ImmutableSet.of()));
    return new RobolectricTest(params.copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(ImmutableSortedSet.of(testsLibrary)), Suppliers.ofInstance(ImmutableSortedSet.of())), ruleFinder, testsLibrary, additionalClasspathEntries, args.labels, args.contacts, TestType.JUNIT, javaOptions, vmArgs, cxxLibraryEnhancement.nativeLibsEnvironment, dummyRDotJava, args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), args.testCaseTimeoutMs, args.env, args.getRunTestSeparately(), args.getForkMode(), args.stdOutLogLevel, args.stdErrLogLevel, args.robolectricRuntimeDependency, args.robolectricManifest);
}
Also used : JavacOptions(com.facebook.buck.jvm.java.JavacOptions) JavacToJarStepFactory(com.facebook.buck.jvm.java.JavacToJarStepFactory) Optional(java.util.Optional) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) Either(com.facebook.buck.model.Either) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription) BuildRule(com.facebook.buck.rules.BuildRule)

Example 12 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class TestRunningTest method testMixedSourceFile.

/**
   * If the source paths specified contains one source path to a non-generated file then
   * we should return the correct source tmp corresponding to that non-generated source path.
   * Especially when the generated file comes first in the ordered set.
   */
@Test
public void testMixedSourceFile() throws Exception {
    BuildTarget genSrcTarget = BuildTargetFactory.newInstance("//:gensrc");
    TargetNode<GenruleDescription.Arg, GenruleDescription> sourceGenerator = GenruleBuilder.newGenruleBuilder(genSrcTarget).setOut("com/facebook/GeneratedFile.java").build();
    Path pathToNonGenFile1 = Paths.get("package/src/SourceFile1.java");
    Path pathToNonGenFile2 = Paths.get("package/src-gen/SourceFile2.java");
    BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//foo:bar");
    TargetNode<JavaLibraryDescription.Arg, JavaLibraryDescription> javaLibraryNode = JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(pathToNonGenFile1).addSrc(pathToNonGenFile2).addSrcTarget(genSrcTarget).build();
    TargetGraph targetGraph = TargetGraphFactory.newInstance(sourceGenerator, javaLibraryNode);
    BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    JavaLibrary javaLibrary = (JavaLibrary) ruleResolver.requireRule(javaLibraryTarget);
    DefaultJavaPackageFinder defaultJavaPackageFinder = createMock(DefaultJavaPackageFinder.class);
    expect(defaultJavaPackageFinder.getPathsFromRoot()).andReturn(pathsFromRoot).times(2);
    expect(defaultJavaPackageFinder.getPathElements()).andReturn(pathElements).times(2);
    replay(defaultJavaPackageFinder);
    ImmutableSet<String> result = TestRunning.getPathToSourceFolders(javaLibrary, resolver, ruleFinder, defaultJavaPackageFinder);
    Path rootPath = javaLibrary.getProjectFilesystem().getRootPath();
    ImmutableSet<String> expected = ImmutableSet.of(rootPath.resolve("package/src-gen").toString() + "/", rootPath.resolve("package/src").toString() + "/");
    assertEquals("The non-generated source files are under two different source folders.", expected, result);
    verify(defaultJavaPackageFinder);
}
Also used : Path(java.nio.file.Path) GenruleDescription(com.facebook.buck.shell.GenruleDescription) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) DefaultJavaPackageFinder(com.facebook.buck.jvm.java.DefaultJavaPackageFinder) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 13 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class TestRunningTest method testUnifiedSourceFile.

/**
   * If the source paths specified are from the new unified source tmp then we should return
   * the correct source tmp corresponding to the unified source path.
   */
@Test
public void testUnifiedSourceFile() throws Exception {
    Path pathToNonGenFile = Paths.get("java/package/src/SourceFile1.java");
    BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//foo:bar");
    TargetNode<JavaLibraryDescription.Arg, JavaLibraryDescription> javaLibraryNode = JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(pathToNonGenFile).build();
    TargetGraph targetGraph = TargetGraphFactory.newInstance(javaLibraryNode);
    BuildRuleResolver ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    JavaLibrary javaLibrary = (JavaLibrary) ruleResolver.requireRule(javaLibraryTarget);
    DefaultJavaPackageFinder defaultJavaPackageFinder = createMock(DefaultJavaPackageFinder.class);
    expect(defaultJavaPackageFinder.getPathsFromRoot()).andReturn(pathsFromRoot);
    Object[] mocks = new Object[] { defaultJavaPackageFinder };
    replay(mocks);
    ImmutableSet<String> result = TestRunning.getPathToSourceFolders(javaLibrary, resolver, ruleFinder, defaultJavaPackageFinder);
    assertEquals("All non-generated source files are under one source tmp.", ImmutableSet.of("java/"), result);
    verify(mocks);
}
Also used : Path(java.nio.file.Path) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) DefaultJavaPackageFinder(com.facebook.buck.jvm.java.DefaultJavaPackageFinder) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 14 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class ParserTest method whenBuildFileContainsSourcesUnderSymLinkDeletedSourcesNotRemovedUntilCacheCleaned.

@Test
public void whenBuildFileContainsSourcesUnderSymLinkDeletedSourcesNotRemovedUntilCacheCleaned() throws Exception {
    // This test depends on creating symbolic links which we cannot do on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    tempDir.newFolder("bar");
    tempDir.newFile("bar/Bar.java");
    tempDir.newFolder("foo");
    Path bazSourceFile = tempDir.newFile("bar/Baz.java");
    Path rootPath = tempDir.getRoot().toRealPath();
    Files.createSymbolicLink(rootPath.resolve("foo/bar"), rootPath.resolve("bar"));
    Path testBuckFile = rootPath.resolve("foo").resolve("BUCK");
    Files.write(testBuckFile, "java_library(name = 'lib', srcs=glob(['bar/*.java']))\n".getBytes(UTF_8));
    // Fetch //:lib to put it in cache.
    BuildTarget libTarget = BuildTarget.builder(cellRoot, "//foo", "lib").build();
    Iterable<BuildTarget> buildTargets = ImmutableList.of(libTarget);
    {
        TargetGraph targetGraph = parser.buildTargetGraph(eventBus, cell, false, executorService, buildTargets);
        BuildRuleResolver resolver = buildActionGraph(eventBus, targetGraph);
        JavaLibrary libRule = (JavaLibrary) resolver.requireRule(libTarget);
        assertEquals(ImmutableSortedSet.of(new PathSourcePath(filesystem, Paths.get("foo/bar/Bar.java")), new PathSourcePath(filesystem, Paths.get("foo/bar/Baz.java"))), libRule.getJavaSrcs());
    }
    Files.delete(bazSourceFile);
    WatchEvent<Path> deleteEvent = createPathEvent(Paths.get("bar/Baz.java"), StandardWatchEventKinds.ENTRY_DELETE);
    parser.onFileSystemChange(deleteEvent);
    {
        TargetGraph targetGraph = parser.buildTargetGraph(eventBus, cell, false, executorService, buildTargets);
        BuildRuleResolver resolver = buildActionGraph(eventBus, targetGraph);
        JavaLibrary libRule = (JavaLibrary) resolver.requireRule(libTarget);
        assertEquals(ImmutableSortedSet.of(new PathSourcePath(filesystem, Paths.get("foo/bar/Bar.java"))), libRule.getJavaSrcs());
    }
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) PathSourcePath(com.facebook.buck.rules.PathSourcePath) TargetGraph(com.facebook.buck.rules.TargetGraph) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 15 with JavaLibrary

use of com.facebook.buck.jvm.java.JavaLibrary in project buck by facebook.

the class GenruleTest method inputBasedRuleKeyClasspathMacro.

@Test
public void inputBasedRuleKeyClasspathMacro() throws Exception {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    GenruleBuilder ruleBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:rule")).setCmd("run $(classpath //:dep)").setOut("output");
    // Create an initial input-based rule key
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    JavaLibrary dep = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep")).addSrc(Paths.get("source.java")).build(resolver, filesystem);
    filesystem.writeContentsToPath("something", Paths.get("source.java"));
    filesystem.writeContentsToPath("something", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
    BuildRule rule = ruleBuilder.build(resolver);
    DefaultRuleKeyFactory defaultRuleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    InputBasedRuleKeyFactory inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey originalRuleKey = defaultRuleKeyFactory.build(rule);
    RuleKey originalInputRuleKey = inputBasedRuleKeyFactory.build(rule);
    // Change the dep's resource root, which will change its normal rule key, but since we're
    // keeping its output JAR the same, the input-based rule key for the consuming rule will stay
    // the same.  This is because the input-based rule key for the consuming rule only cares about
    // the contents of the output this rule produces.
    resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep")).addSrc(Paths.get("source.java")).setResourcesRoot(Paths.get("resource_root")).build(resolver, filesystem);
    rule = ruleBuilder.build(resolver);
    ruleFinder = new SourcePathRuleFinder(resolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    defaultRuleKeyFactory = new DefaultRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey unchangedRuleKey = defaultRuleKeyFactory.build(rule);
    RuleKey unchangedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
    assertThat(unchangedRuleKey, Matchers.not(Matchers.equalTo(originalRuleKey)));
    assertThat(unchangedInputBasedRuleKey, Matchers.equalTo(originalInputRuleKey));
    // Make a change to the dep's output, which *should* affect the input-based rule key.
    resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    dep = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep")).addSrc(Paths.get("source.java")).build(resolver, filesystem);
    filesystem.writeContentsToPath("something else", pathResolver.getRelativePath(dep.getSourcePathToOutput()));
    rule = ruleBuilder.build(resolver);
    ruleFinder = new SourcePathRuleFinder(resolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    inputBasedRuleKeyFactory = new InputBasedRuleKeyFactory(0, new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder);
    RuleKey changedInputBasedRuleKey = inputBasedRuleKeyFactory.build(rule);
    assertThat(changedInputBasedRuleKey, Matchers.not(Matchers.equalTo(originalInputRuleKey)));
}
Also used : DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) InputBasedRuleKeyFactory(com.facebook.buck.rules.keys.InputBasedRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

JavaLibrary (com.facebook.buck.jvm.java.JavaLibrary)33 BuildTarget (com.facebook.buck.model.BuildTarget)24 BuildRule (com.facebook.buck.rules.BuildRule)18 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)18 Path (java.nio.file.Path)17 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)15 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)14 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)13 Test (org.junit.Test)13 SourcePath (com.facebook.buck.rules.SourcePath)12 DefaultJavaLibrary (com.facebook.buck.jvm.java.DefaultJavaLibrary)11 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)11 TargetGraph (com.facebook.buck.rules.TargetGraph)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 Optional (java.util.Optional)8 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)7 DefaultJavaPackageFinder (com.facebook.buck.jvm.java.DefaultJavaPackageFinder)6 PathSourcePath (com.facebook.buck.rules.PathSourcePath)6 JavaLibraryDescription (com.facebook.buck.jvm.java.JavaLibraryDescription)5 JavaTest (com.facebook.buck.jvm.java.JavaTest)5