Search in sources :

Example 26 with JavaLibrary

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

the class GwtBinaryDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, A args) {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    final ImmutableSortedSet.Builder<BuildRule> extraDeps = ImmutableSortedSet.naturalOrder();
    // Find all of the reachable JavaLibrary rules and grab their associated GwtModules.
    final ImmutableSortedSet.Builder<SourcePath> gwtModuleJarsBuilder = ImmutableSortedSet.naturalOrder();
    ImmutableSortedSet<BuildRule> moduleDependencies = resolver.getAllRules(args.moduleDeps);
    new AbstractBreadthFirstTraversal<BuildRule>(moduleDependencies) {

        @Override
        public ImmutableSet<BuildRule> visit(BuildRule rule) {
            if (!(rule instanceof JavaLibrary)) {
                return ImmutableSet.of();
            }
            // If the java library doesn't generate any output, it doesn't contribute a GwtModule
            JavaLibrary javaLibrary = (JavaLibrary) rule;
            if (javaLibrary.getSourcePathToOutput() == null) {
                return rule.getDeps();
            }
            BuildTarget gwtModuleTarget = BuildTargets.createFlavoredBuildTarget(javaLibrary.getBuildTarget().checkUnflavored(), JavaLibrary.GWT_MODULE_FLAVOR);
            Optional<BuildRule> gwtModule = resolver.getRuleOptional(gwtModuleTarget);
            if (!gwtModule.isPresent() && javaLibrary.getSourcePathToOutput() != null) {
                ImmutableSortedSet<SourcePath> filesForGwtModule = ImmutableSortedSet.<SourcePath>naturalOrder().addAll(javaLibrary.getSources()).addAll(javaLibrary.getResources()).build();
                ImmutableSortedSet<BuildRule> deps = ImmutableSortedSet.copyOf(ruleFinder.filterBuildRuleInputs(filesForGwtModule));
                BuildRule module = resolver.addToIndex(new GwtModule(params.withBuildTarget(gwtModuleTarget).copyReplacingDeclaredAndExtraDeps(Suppliers.ofInstance(deps), Suppliers.ofInstance(ImmutableSortedSet.of())), ruleFinder, filesForGwtModule));
                gwtModule = Optional.of(module);
            }
            // but a rule that exists only as a collection of deps.
            if (gwtModule.isPresent()) {
                extraDeps.add(gwtModule.get());
                gwtModuleJarsBuilder.add(Preconditions.checkNotNull(gwtModule.get().getSourcePathToOutput()));
            }
            // Traverse all of the deps of this rule.
            return rule.getDeps();
        }
    }.start();
    return new GwtBinary(params.copyReplacingExtraDeps(Suppliers.ofInstance(extraDeps.build())), args.modules, javaOptions.getJavaRuntimeLauncher(), args.vmArgs, args.style.orElse(DEFAULT_STYLE), args.draftCompile.orElse(DEFAULT_DRAFT_COMPILE), args.optimize.orElse(DEFAULT_OPTIMIZE), args.localWorkers.orElse(DEFAULT_NUM_LOCAL_WORKERS), args.strict.orElse(DEFAULT_STRICT), args.experimentalArgs, gwtModuleJarsBuilder.build());
}
Also used : Optional(java.util.Optional) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePath(com.facebook.buck.rules.SourcePath) ImmutableSet(com.google.common.collect.ImmutableSet) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BuildRule(com.facebook.buck.rules.BuildRule)

Example 27 with JavaLibrary

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

the class ParserTest method whenBuildFileContainsSourcesUnderSymLinkNewSourcesNotAddedUntilCacheCleaned.

@Test
public void whenBuildFileContainsSourcesUnderSymLinkNewSourcesNotAddedUntilCacheCleaned() 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 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"))), libRule.getJavaSrcs());
    }
    tempDir.newFile("bar/Baz.java");
    WatchEvent<Path> createEvent = createPathEvent(Paths.get("bar/Baz.java"), StandardWatchEventKinds.ENTRY_CREATE);
    parser.onFileSystemChange(createEvent);
    {
        TargetGraph targetGraph = parser.buildTargetGraph(eventBus, cell, false, executorService, buildTargets);
        BuildRuleResolver resolver = buildActionGraph(eventBus, targetGraph);
        JavaLibrary libRule = (JavaLibrary) resolver.requireRule(libTarget);
        assertEquals(ImmutableSet.of(new PathSourcePath(filesystem, Paths.get("foo/bar/Bar.java")), new PathSourcePath(filesystem, Paths.get("foo/bar/Baz.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 28 with JavaLibrary

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

the class DexProducedFromJavaLibraryThatContainsClassFilesTest method testGetBuildStepsWhenThereAreNoClassesToDex.

@Test
public void testGetBuildStepsWhenThereAreNoClassesToDex() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    DefaultJavaLibrary javaLibrary = JavaLibraryBuilder.createBuilder("//foo:bar").build(resolver);
    javaLibrary.getBuildOutputInitializer().setBuildOutput(new JavaLibrary.Data(ImmutableSortedMap.of()));
    BuildContext context = FakeBuildContext.NOOP_CONTEXT;
    FakeBuildableContext buildableContext = new FakeBuildableContext();
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//foo:bar#dex");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).setProjectFilesystem(projectFilesystem).build();
    DexProducedFromJavaLibrary preDex = new DexProducedFromJavaLibrary(params, javaLibrary);
    List<Step> steps = preDex.getBuildSteps(context, buildableContext);
    Path dexOutput = BuildTargets.getGenPath(projectFilesystem, buildTarget, "%s.dex.jar");
    ExecutionContext executionContext = TestExecutionContext.newBuilder().build();
    MoreAsserts.assertSteps("Do not generate a .dex.jar file.", ImmutableList.of(String.format("rm -f %s", projectFilesystem.resolve(dexOutput)), String.format("mkdir -p %s", projectFilesystem.resolve(dexOutput.getParent())), "record_empty_dx"), steps, executionContext);
    Step recordArtifactAndMetadataStep = steps.get(2);
    assertThat(recordArtifactAndMetadataStep.getShortName(), startsWith("record_"));
    int exitCode = recordArtifactAndMetadataStep.execute(executionContext).getExitCode();
    assertEquals(0, exitCode);
}
Also used : Path(java.nio.file.Path) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) EstimateDexWeightStep(com.facebook.buck.dalvik.EstimateDexWeightStep) Step(com.facebook.buck.step.Step) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) DefaultJavaLibrary(com.facebook.buck.jvm.java.DefaultJavaLibrary) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) BuildContext(com.facebook.buck.rules.BuildContext) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 29 with JavaLibrary

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

the class DexWithClassesTest method testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses.

@Test
public void testIntermediateDexRuleToDexWithClassesWhenIntermediateDexHasNoClasses() {
    SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//java/com/example:lib");
    JavaLibrary javaLibrary = new FakeJavaLibrary(javaLibraryTarget, resolver);
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/com/example:lib#dex");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).build();
    DexProducedFromJavaLibrary dexFromJavaLibrary = new DexProducedFromJavaLibrary(params, javaLibrary);
    dexFromJavaLibrary.getBuildOutputInitializer().setBuildOutput(new DexProducedFromJavaLibrary.BuildOutput(/* weightEstimate */
    1600, /* classNamesToHashes */
    ImmutableSortedMap.of(), Optional.empty()));
    DexWithClasses dexWithClasses = DexWithClasses.TO_DEX_WITH_CLASSES.apply(dexFromJavaLibrary);
    assertNull("If the JavaLibraryRule does not produce any .class files, " + "then DexWithClasses.TO_DEX_WITH_CLASSES should return null.", dexWithClasses);
}
Also used : JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 30 with JavaLibrary

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

the class DexWithClassesTest method testIntermediateDexRuleToDexWithClasses.

@Test
public void testIntermediateDexRuleToDexWithClasses() {
    SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//java/com/example:lib");
    JavaLibrary javaLibrary = new FakeJavaLibrary(javaLibraryTarget, resolver);
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/com/example:lib#dex");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(buildTarget).build();
    DexProducedFromJavaLibrary dexFromJavaLibrary = new DexProducedFromJavaLibrary(params, javaLibrary);
    dexFromJavaLibrary.getBuildOutputInitializer().setBuildOutput(new DexProducedFromJavaLibrary.BuildOutput(/* weightEstimate */
    1600, /* classNamesToHashes */
    ImmutableSortedMap.of("com/example/Main", HashCode.fromString(Strings.repeat("cafebabe", 5))), Optional.empty()));
    DexWithClasses dexWithClasses = DexWithClasses.TO_DEX_WITH_CLASSES.apply(dexFromJavaLibrary);
    assertEquals(BuildTargets.getGenPath(javaLibrary.getProjectFilesystem(), buildTarget, "%s.dex.jar"), dexWithClasses.getPathToDexFile());
    assertEquals(ImmutableSet.of("com/example/Main"), dexWithClasses.getClassNames());
    assertEquals(1600, dexWithClasses.getWeightEstimate());
}
Also used : JavaLibrary(com.facebook.buck.jvm.java.JavaLibrary) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) FakeJavaLibrary(com.facebook.buck.jvm.java.FakeJavaLibrary) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) 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