Search in sources :

Example 6 with MutableDirectedGraph

use of com.facebook.buck.graph.MutableDirectedGraph 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 7 with MutableDirectedGraph

use of com.facebook.buck.graph.MutableDirectedGraph in project buck by facebook.

the class NativeLinkables method getTransitiveNativeLinkables.

public static ImmutableMap<BuildTarget, NativeLinkable> getTransitiveNativeLinkables(final CxxPlatform cxxPlatform, Iterable<? extends NativeLinkable> inputs) {
    final Map<BuildTarget, NativeLinkable> nativeLinkables = Maps.newHashMap();
    for (NativeLinkable nativeLinkable : inputs) {
        nativeLinkables.put(nativeLinkable.getBuildTarget(), nativeLinkable);
    }
    final MutableDirectedGraph<BuildTarget> graph = new MutableDirectedGraph<>();
    AbstractBreadthFirstTraversal<BuildTarget> visitor = new AbstractBreadthFirstTraversal<BuildTarget>(nativeLinkables.keySet()) {

        @Override
        public ImmutableSet<BuildTarget> visit(BuildTarget target) {
            NativeLinkable nativeLinkable = Preconditions.checkNotNull(nativeLinkables.get(target));
            graph.addNode(target);
            ImmutableSet.Builder<BuildTarget> deps = ImmutableSet.builder();
            for (NativeLinkable dep : Iterables.concat(nativeLinkable.getNativeLinkableDepsForPlatform(cxxPlatform), nativeLinkable.getNativeLinkableExportedDepsForPlatform(cxxPlatform))) {
                BuildTarget depTarget = dep.getBuildTarget();
                graph.addEdge(target, depTarget);
                deps.add(depTarget);
                nativeLinkables.put(depTarget, dep);
            }
            return deps.build();
        }
    };
    visitor.start();
    return ImmutableMap.copyOf(nativeLinkables);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) AbstractBreadthFirstTraversal(com.facebook.buck.graph.AbstractBreadthFirstTraversal) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph)

Aggregations

MutableDirectedGraph (com.facebook.buck.graph.MutableDirectedGraph)7 BuildTarget (com.facebook.buck.model.BuildTarget)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 TargetGraph (com.facebook.buck.rules.TargetGraph)3 TargetNode (com.facebook.buck.rules.TargetNode)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 HashMap (java.util.HashMap)3 AbstractBreadthFirstTraversal (com.facebook.buck.graph.AbstractBreadthFirstTraversal)2 Cell (com.facebook.buck.rules.Cell)2 Path (java.nio.file.Path)2 Counter (com.facebook.buck.counters.Counter)1 NativeLinkable (com.facebook.buck.cxx.NativeLinkable)1 BuildJobStateBuildTarget (com.facebook.buck.distributed.thrift.BuildJobStateBuildTarget)1 BuildJobStateTargetGraph (com.facebook.buck.distributed.thrift.BuildJobStateTargetGraph)1 BuildJobStateTargetNode (com.facebook.buck.distributed.thrift.BuildJobStateTargetNode)1 BuckEvent (com.facebook.buck.event.BuckEvent)1 BuckEventBus (com.facebook.buck.event.BuckEventBus)1 PerfEventId (com.facebook.buck.event.PerfEventId)1 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)1 BroadcastEventListener (com.facebook.buck.event.listener.BroadcastEventListener)1