Search in sources :

Example 66 with TargetNode

use of com.facebook.buck.rules.TargetNode in project buck by facebook.

the class DistBuildTargetGraphCodec method createTargetGraph.

public TargetGraphAndBuildTargets createTargetGraph(BuildJobStateTargetGraph remoteTargetGraph, Function<Integer, Cell> cellLookup) throws IOException {
    ImmutableMap.Builder<BuildTarget, TargetNode<?, ?>> targetNodeIndexBuilder = ImmutableMap.builder();
    ImmutableSet.Builder<BuildTarget> buildTargetsBuilder = ImmutableSet.builder();
    for (BuildJobStateTargetNode remoteNode : remoteTargetGraph.getNodes()) {
        Cell cell = cellLookup.apply(remoteNode.getCellIndex());
        ProjectFilesystem projectFilesystem = cell.getFilesystem();
        BuildTarget target = decodeBuildTarget(remoteNode.getBuildTarget(), cell);
        if (topLevelTargets.contains(target.getFullyQualifiedName())) {
            buildTargetsBuilder.add(target);
        }
        @SuppressWarnings("unchecked") Map<String, Object> rawNode = objectMapper.readValue(remoteNode.getRawNode(), Map.class);
        Path buildFilePath = projectFilesystem.resolve(target.getBasePath()).resolve(cell.getBuildFileName());
        TargetNode<?, ?> targetNode = parserTargetNodeFactory.createTargetNode(cell, buildFilePath, target, rawNode, input -> SimplePerfEvent.scope(Optional.empty(), input));
        targetNodeIndexBuilder.put(targetNode.getBuildTarget(), targetNode);
    }
    ImmutableSet<BuildTarget> buildTargets = buildTargetsBuilder.build();
    Preconditions.checkArgument(topLevelTargets.size() == buildTargets.size());
    ImmutableMap<BuildTarget, TargetNode<?, ?>> targetNodeIndex = targetNodeIndexBuilder.build();
    MutableDirectedGraph<TargetNode<?, ?>> mutableTargetGraph = new MutableDirectedGraph<>();
    for (TargetNode<?, ?> targetNode : targetNodeIndex.values()) {
        mutableTargetGraph.addNode(targetNode);
        for (BuildTarget dep : targetNode.getDeps()) {
            mutableTargetGraph.addEdge(targetNode, Preconditions.checkNotNull(targetNodeIndex.get(dep)));
        }
    }
    // TODO(tophyr): make this work with TargetGroups
    TargetGraph targetGraph = new TargetGraph(mutableTargetGraph, targetNodeIndex, ImmutableSet.of());
    return TargetGraphAndBuildTargets.builder().setTargetGraph(targetGraph).addAllBuildTargets(buildTargets).build();
}
Also used : Path(java.nio.file.Path) BuildJobStateTargetNode(com.facebook.buck.distributed.thrift.BuildJobStateTargetNode) BuildJobStateTargetNode(com.facebook.buck.distributed.thrift.BuildJobStateTargetNode) TargetNode(com.facebook.buck.rules.TargetNode) BuildJobStateTargetGraph(com.facebook.buck.distributed.thrift.BuildJobStateTargetGraph) TargetGraph(com.facebook.buck.rules.TargetGraph) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildJobStateBuildTarget(com.facebook.buck.distributed.thrift.BuildJobStateBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Cell(com.facebook.buck.rules.Cell) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph)

Example 67 with TargetNode

use of com.facebook.buck.rules.TargetNode in project buck by facebook.

the class AndroidResourceTest method testRuleKeyForDifferentInputFilenames.

@Test
public void testRuleKeyForDifferentInputFilenames() throws Exception {
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/src/com/facebook/base:res");
    Function<Path, BuildRuleResolver> createResourceRule = (Path resourcePath) -> {
        FakeProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
        projectFilesystem.createNewFile(resourcePath);
        projectFilesystem.createNewFile(Paths.get("java/src/com/facebook/base/assets/drawable/B.xml"));
        TargetNode<?, ?> resourceNode = AndroidResourceBuilder.createBuilder(buildTarget, projectFilesystem).setRes(new FakeSourcePath(projectFilesystem, "java/src/com/facebook/base/res")).setRDotJavaPackage("com.facebook").setAssets(new FakeSourcePath(projectFilesystem, "java/src/com/facebook/base/assets")).setManifest(new PathSourcePath(projectFilesystem, Paths.get("java/src/com/facebook/base/AndroidManifest.xml"))).build();
        TargetGraph targetGraph = TargetGraphFactory.newInstance(resourceNode);
        return new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    };
    FakeFileHashCache hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.of("java/src/com/facebook/base/AndroidManifest.xml", "bbbbbbbbbb", "java/src/com/facebook/base/assets/drawable/B.xml", "aaaaaaaaaaaa", "java/src/com/facebook/base/res/drawable/A.xml", "dddddddddd", "java/src/com/facebook/base/res/drawable/C.xml", "eeeeeeeeee"));
    BuildRuleResolver resolver1 = createResourceRule.apply(Paths.get("java/src/com/facebook/base/res/drawable/A.xml"));
    BuildRuleResolver resolver2 = createResourceRule.apply(Paths.get("java/src/com/facebook/base/res/drawable/C.xml"));
    BuildRule androidResource1 = resolver1.requireRule(buildTarget);
    SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(resolver1);
    SourcePathResolver pathResolver1 = new SourcePathResolver(ruleFinder1);
    BuildRule androidResource2 = resolver2.requireRule(buildTarget);
    SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(resolver2);
    SourcePathResolver pathResolver2 = new SourcePathResolver(ruleFinder2);
    RuleKey ruleKey1 = new DefaultRuleKeyFactory(0, hashCache, pathResolver1, ruleFinder1).build(androidResource1);
    RuleKey ruleKey2 = new DefaultRuleKeyFactory(0, hashCache, pathResolver2, ruleFinder2).build(androidResource2);
    assertNotEquals("The two android_resource rules should have different rule keys.", ruleKey1, ruleKey2);
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetNode(com.facebook.buck.rules.TargetNode) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 68 with TargetNode

use of com.facebook.buck.rules.TargetNode in project buck by facebook.

the class DuplicateResourcesTest method getAaptStepShellCommand.

private ImmutableList<String> getAaptStepShellCommand(TargetNode<AndroidBinaryDescription.Arg, AndroidBinaryDescription> binary) {
    TargetGraph targetGraph = TargetGraphFactory.newInstance(binary, mainRes, directDepRes, transitiveDepRes, transitiveDepLib, bottomDepRes, library, keystore);
    ActionGraphAndResolver actionGraphAndResolver = ActionGraphCache.getFreshActionGraph(BuckEventBusFactory.newInstance(new IncrementingFakeClock(TimeUnit.SECONDS.toNanos(1))), new DefaultTargetNodeToBuildRuleTransformer(), targetGraph);
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(actionGraphAndResolver.getResolver()));
    ImmutableSet<ImmutableList<Step>> ruleSteps = RichStream.from(actionGraphAndResolver.getActionGraph().getNodes()).filter(AaptPackageResources.class).filter(r -> androidBinaryTarget.getUnflavoredBuildTarget().equals(r.getBuildTarget().getUnflavoredBuildTarget())).map(b -> b.getBuildSteps(FakeBuildContext.withSourcePathResolver(pathResolver), new FakeBuildableContext())).map(steps -> steps.stream().filter(step -> step instanceof AaptStep).collect(MoreCollectors.toImmutableList())).filter(steps -> !steps.isEmpty()).collect(MoreCollectors.toImmutableSet());
    assertEquals(1, ruleSteps.size());
    assertEquals(1, Iterables.getOnlyElement(ruleSteps).size());
    AaptStep step = (AaptStep) Iterables.getOnlyElement(Iterables.getOnlyElement(ruleSteps));
    AndroidDirectoryResolver androidDirectoryResolver = new FakeAndroidDirectoryResolver(Optional.of(filesystem.getPath("/android-sdk")), Optional.of(filesystem.getPath("/android-build-tools")), Optional.empty(), Optional.empty());
    AndroidPlatformTarget androidPlatformTarget = AndroidPlatformTarget.createFromDefaultDirectoryStructure("", androidDirectoryResolver, "", ImmutableSet.of(), Optional.empty());
    ExecutionContext context = TestExecutionContext.newBuilder().setAndroidPlatformTargetSupplier(Suppliers.ofInstance(androidPlatformTarget)).build();
    return step.getShellCommand(context);
}
Also used : Iterables(com.google.common.collect.Iterables) FakeBuildContext(com.facebook.buck.rules.FakeBuildContext) Step(com.facebook.buck.step.Step) Assume.assumeFalse(org.junit.Assume.assumeFalse) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) RichStream(com.facebook.buck.util.RichStream) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) BuckEventBusFactory(com.facebook.buck.event.BuckEventBusFactory) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) KeystoreBuilder(com.facebook.buck.jvm.java.KeystoreBuilder) ExecutionContext(com.facebook.buck.step.ExecutionContext) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) ImmutableList(com.google.common.collect.ImmutableList) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildTargetFactory(com.facebook.buck.model.BuildTargetFactory) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Suppliers(com.google.common.base.Suppliers) ActionGraphCache(com.facebook.buck.rules.ActionGraphCache) MoreCollectors(com.facebook.buck.util.MoreCollectors) Before(org.junit.Before) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) Platform(com.facebook.buck.util.environment.Platform) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) BuildTarget(com.facebook.buck.model.BuildTarget) ActionGraphAndResolver(com.facebook.buck.rules.ActionGraphAndResolver) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) TargetGraphFactory(com.facebook.buck.testutil.TargetGraphFactory) Matcher(org.hamcrest.Matcher) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) KeystoreDescription(com.facebook.buck.jvm.java.KeystoreDescription) Joiner(com.google.common.base.Joiner) FakeBuildableContext(com.facebook.buck.rules.FakeBuildableContext) ImmutableList(com.google.common.collect.ImmutableList) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) TargetGraph(com.facebook.buck.rules.TargetGraph) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ActionGraphAndResolver(com.facebook.buck.rules.ActionGraphAndResolver) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)

Example 69 with TargetNode

use of com.facebook.buck.rules.TargetNode in project buck by facebook.

the class AuditInputCommandTest method testNonExistentInputFileThrows.

@Test
public void testNonExistentInputFileThrows() throws IOException {
    thrown.expect(HumanReadableException.class);
    thrown.expectMessage("Target //:test-java-library refers to non-existent input file: " + MorePaths.pathWithPlatformSeparators("src/com/facebook/NonExistentFile.java"));
    BuildTarget rootTarget = BuildTargetFactory.newInstance("//:test-java-library");
    TargetNode<?, ?> rootNode = JavaLibraryBuilder.createBuilder(rootTarget).addSrc(Paths.get("src/com/facebook/NonExistentFile.java")).build();
    ImmutableSet<TargetNode<?, ?>> nodes = ImmutableSet.of(rootNode);
    TargetGraph targetGraph = TargetGraphFactory.newInstance(nodes);
    auditInputCommand.printJsonInputs(params, targetGraph);
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph) Test(org.junit.Test)

Example 70 with TargetNode

use of com.facebook.buck.rules.TargetNode in project buck by facebook.

the class AuditInputCommandTest method testJsonClassPathOutput.

@Test
public void testJsonClassPathOutput() throws IOException {
    ObjectMapper mapper = ObjectMappers.newDefaultInstance();
    final String expectedJson = Joiner.on("").join("{", "\"//:test-android-library\":", "[", mapper.valueToTree(MorePaths.pathWithPlatformSeparators("src/com/facebook/AndroidLibraryTwo.java")), ",", mapper.valueToTree(MorePaths.pathWithPlatformSeparators("src/com/facebook/TestAndroidLibrary.java")), "],", "\"//:test-java-library\":", "[", mapper.valueToTree(MorePaths.pathWithPlatformSeparators("src/com/facebook/TestJavaLibrary.java")), "]", "}");
    BuildTarget rootTarget = BuildTargetFactory.newInstance("//:test-java-library");
    TargetNode<?, ?> rootNode = JavaLibraryBuilder.createBuilder(rootTarget).addSrc(Paths.get("src/com/facebook/TestJavaLibrary.java")).build();
    BuildTarget libraryTarget = BuildTargetFactory.newInstance("//:test-android-library");
    TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(libraryTarget).addSrc(Paths.get("src/com/facebook/TestAndroidLibrary.java")).addSrc(Paths.get("src/com/facebook/AndroidLibraryTwo.java")).addDep(rootTarget).build();
    ImmutableSet<TargetNode<?, ?>> nodes = ImmutableSet.of(rootNode, libraryNode);
    TargetGraph targetGraph = TargetGraphFactory.newInstance(nodes);
    auditInputCommand.printJsonInputs(params, targetGraph);
    assertEquals(expectedJson, console.getTextWrittenToStdOut());
    assertEquals("", console.getTextWrittenToStdErr());
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

TargetNode (com.facebook.buck.rules.TargetNode)88 BuildTarget (com.facebook.buck.model.BuildTarget)73 TargetGraph (com.facebook.buck.rules.TargetGraph)43 Test (org.junit.Test)40 Path (java.nio.file.Path)36 ImmutableSet (com.google.common.collect.ImmutableSet)33 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)23 PathSourcePath (com.facebook.buck.rules.PathSourcePath)22 SourcePath (com.facebook.buck.rules.SourcePath)22 ImmutableMap (com.google.common.collect.ImmutableMap)22 Optional (java.util.Optional)20 Map (java.util.Map)18 HumanReadableException (com.facebook.buck.util.HumanReadableException)17 ImmutableList (com.google.common.collect.ImmutableList)17 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)13 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)13 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)13 Cell (com.facebook.buck.rules.Cell)13 IOException (java.io.IOException)13 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)12