Search in sources :

Example 1 with AcyclicDepthFirstPostOrderTraversal

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

the class Parser method buildTargetGraph.

@SuppressWarnings("PMD.PrematureDeclaration")
protected TargetGraph buildTargetGraph(final PerBuildState state, final BuckEventBus eventBus, final Iterable<BuildTarget> toExplore, final boolean ignoreBuckAutodepsFiles) throws IOException, InterruptedException, BuildFileParseException, BuildTargetException {
    if (Iterables.isEmpty(toExplore)) {
        return TargetGraph.EMPTY;
    }
    final Map<BuildTarget, TargetGroup> groups = Maps.newHashMap();
    for (TargetGroup group : state.getAllGroups()) {
        groups.put(group.getBuildTarget(), group);
    }
    final MutableDirectedGraph<TargetNode<?, ?>> graph = new MutableDirectedGraph<>();
    final Map<BuildTarget, TargetNode<?, ?>> index = new HashMap<>();
    ParseEvent.Started parseStart = ParseEvent.started(toExplore);
    eventBus.post(parseStart);
    GraphTraversable<BuildTarget> traversable = target -> {
        TargetNode<?, ?> node;
        try {
            node = state.getTargetNode(target);
        } catch (BuildFileParseException | BuildTargetException e) {
            throw new RuntimeException(e);
        }
        if (ignoreBuckAutodepsFiles) {
            return Collections.emptyIterator();
        }
        for (BuildTarget dep : node.getDeps()) {
            try {
                state.getTargetNode(dep);
            } catch (BuildFileParseException | BuildTargetException | HumanReadableException e) {
                throw new HumanReadableException(e, "Couldn't get dependency '%s' of target '%s':\n%s", dep, target, e.getMessage());
            }
        }
        return node.getDeps().iterator();
    };
    GraphTraversable<BuildTarget> groupExpander = target -> {
        TargetGroup group = Preconditions.checkNotNull(groups.get(target), "SANITY FAILURE: Tried to expand group %s but it doesn't exist.", target);
        return Iterators.filter(group.iterator(), groups::containsKey);
    };
    AcyclicDepthFirstPostOrderTraversal<BuildTarget> targetGroupExpansion = new AcyclicDepthFirstPostOrderTraversal<>(groupExpander);
    AcyclicDepthFirstPostOrderTraversal<BuildTarget> targetNodeTraversal = new AcyclicDepthFirstPostOrderTraversal<>(traversable);
    TargetGraph targetGraph = null;
    try {
        for (BuildTarget target : targetNodeTraversal.traverse(toExplore)) {
            TargetNode<?, ?> targetNode = state.getTargetNode(target);
            Preconditions.checkNotNull(targetNode, "No target node found for %s", target);
            graph.addNode(targetNode);
            MoreMaps.putCheckEquals(index, target, targetNode);
            if (target.isFlavored()) {
                BuildTarget unflavoredTarget = BuildTarget.of(target.getUnflavoredBuildTarget());
                MoreMaps.putCheckEquals(index, unflavoredTarget, state.getTargetNode(unflavoredTarget));
            }
            for (BuildTarget dep : targetNode.getDeps()) {
                graph.addEdge(targetNode, state.getTargetNode(dep));
            }
        }
        for (BuildTarget groupTarget : targetGroupExpansion.traverse(groups.keySet())) {
            ImmutableMap<BuildTarget, Iterable<BuildTarget>> replacements = Maps.toMap(groupExpander.findChildren(groupTarget), target -> {
                TargetGroup group = groups.get(target);
                return Preconditions.checkNotNull(group, "SANITY FAILURE: Tried to expand group %s but it doesn't exist.", target);
            });
            if (!replacements.isEmpty()) {
                // TODO(tophyr): Stop duplicating target lists
                groups.put(groupTarget, Preconditions.checkNotNull(groups.get(groupTarget)).withReplacedTargets(replacements));
            }
        }
        targetGraph = new TargetGraph(graph, ImmutableMap.copyOf(index), ImmutableSet.copyOf(groups.values()));
        state.ensureConcreteFilesExist(eventBus);
        return targetGraph;
    } catch (AcyclicDepthFirstPostOrderTraversal.CycleException e) {
        throw new HumanReadableException(e.getMessage());
    } catch (RuntimeException e) {
        throw propagateRuntimeCause(e);
    } finally {
        eventBus.post(ParseEvent.finished(parseStart, Optional.ofNullable(targetGraph)));
    }
}
Also used : BroadcastEventListener(com.facebook.buck.event.listener.BroadcastEventListener) PerfEventId(com.facebook.buck.event.PerfEventId) BuckEvent(com.facebook.buck.event.BuckEvent) MoreMaps(com.facebook.buck.util.MoreMaps) TypeCoercerFactory(com.facebook.buck.rules.coercer.TypeCoercerFactory) Map(java.util.Map) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) Cell(com.facebook.buck.rules.Cell) Path(java.nio.file.Path) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) Collection(java.util.Collection) BuildTargetException(com.facebook.buck.model.BuildTargetException) WatchEvents(com.facebook.buck.io.WatchEvents) BuildTarget(com.facebook.buck.model.BuildTarget) HasDefaultFlavors(com.facebook.buck.model.HasDefaultFlavors) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) Optional(java.util.Optional) SortedMap(java.util.SortedMap) ImplicitFlavorsInferringDescription(com.facebook.buck.rules.ImplicitFlavorsInferringDescription) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) BuckEventBus(com.facebook.buck.event.BuckEventBus) Iterables(com.google.common.collect.Iterables) TargetGraphAndBuildTargets(com.facebook.buck.rules.TargetGraphAndBuildTargets) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) GraphTraversable(com.facebook.buck.graph.GraphTraversable) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TargetGroup(com.facebook.buck.rules.TargetGroup) Subscribe(com.google.common.eventbus.Subscribe) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Logger(com.facebook.buck.log.Logger) Counter(com.facebook.buck.counters.Counter) TargetNode(com.facebook.buck.rules.TargetNode) WatchEvent(java.nio.file.WatchEvent) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) Maps(com.google.common.collect.Maps) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) TargetNode(com.facebook.buck.rules.TargetNode) HashMap(java.util.HashMap) TargetGraph(com.facebook.buck.rules.TargetGraph) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) TargetGroup(com.facebook.buck.rules.TargetGroup) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph)

Example 2 with AcyclicDepthFirstPostOrderTraversal

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

the class TargetGraphHashing method hashTargetGraph.

/**
   * Given a {@link TargetGraph} and any number of root nodes to traverse,
   * returns a map of {@code (BuildTarget, HashCode)} pairs for all root
   * build targets and their dependencies.
   */
public ImmutableMap<BuildTarget, HashCode> hashTargetGraph() throws CycleException {
    try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("ShowTargetHashes"))) {
        AcyclicDepthFirstPostOrderTraversal<TargetNode<?, ?>> traversal = new AcyclicDepthFirstPostOrderTraversal<>(node -> targetGraph.getAll(node.getDeps()).iterator());
        final Map<BuildTarget, ForkJoinTask<HashCode>> buildTargetHashes = new HashMap<>();
        Queue<ForkJoinTask<HashCode>> tasksToSchedule = new ArrayDeque<>();
        // Start all the node tasks, bottom up
        for (final TargetNode<?, ?> node : traversal.traverse(roots)) {
            HashNodeTask task = new HashNodeTask(node, buildTargetHashes);
            buildTargetHashes.put(node.getBuildTarget(), task);
            tasksToSchedule.add(task);
        }
        // Execute tasks in parallel
        ForkJoinPool pool = new ForkJoinPool(numThreads);
        for (ForkJoinTask<HashCode> task : tasksToSchedule) {
            pool.execute(task);
        }
        // Wait for all scheduled tasks to complete
        return ImmutableMap.copyOf(Maps.transformEntries(buildTargetHashes, (key, value) -> value.join()));
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) PerfEventId(com.facebook.buck.event.PerfEventId) RecursiveTask(java.util.concurrent.RecursiveTask) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) Hashing(com.google.common.hash.Hashing) HashMap(java.util.HashMap) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Map(java.util.Map) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) CycleException(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal.CycleException) Hasher(com.google.common.hash.Hasher) StringHashing(com.facebook.buck.hashing.StringHashing) Path(java.nio.file.Path) FileHashLoader(com.facebook.buck.hashing.FileHashLoader) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Logger(com.facebook.buck.log.Logger) ForkJoinTask(java.util.concurrent.ForkJoinTask) ImmutableMap(com.google.common.collect.ImmutableMap) HashCode(com.google.common.hash.HashCode) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) ForkJoinPool(java.util.concurrent.ForkJoinPool) Preconditions(com.google.common.base.Preconditions) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) HashMap(java.util.HashMap) ForkJoinTask(java.util.concurrent.ForkJoinTask) ArrayDeque(java.util.ArrayDeque) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Aggregations

BuckEventBus (com.facebook.buck.event.BuckEventBus)2 PerfEventId (com.facebook.buck.event.PerfEventId)2 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)2 AcyclicDepthFirstPostOrderTraversal (com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal)2 Logger (com.facebook.buck.log.Logger)2 BuildTarget (com.facebook.buck.model.BuildTarget)2 HumanReadableException (com.facebook.buck.util.HumanReadableException)2 Preconditions (com.google.common.base.Preconditions)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 Maps (com.google.common.collect.Maps)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Counter (com.facebook.buck.counters.Counter)1 BuckEvent (com.facebook.buck.event.BuckEvent)1 BroadcastEventListener (com.facebook.buck.event.listener.BroadcastEventListener)1 CycleException (com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal.CycleException)1 GraphTraversable (com.facebook.buck.graph.GraphTraversable)1