OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef TOOLS_GN_GRAPH_H_ |
| 6 #define TOOLS_GN_GRAPH_H_ |
| 7 |
| 8 #include <set> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "tools/gn/label.h" |
| 13 #include "tools/gn/source_file.h" |
| 14 #include "tools/gn/target.h" |
| 15 |
| 16 using DepMap = std::multimap<const Target*, const Target*>; |
| 17 using LabelSet = std::set<const Label*>; |
| 18 using SourceFileSet = std::set<const SourceFile*>; |
| 19 using TargetSet = std::set<const Target*>; |
| 20 using TargetVector = std::vector<const Target*>; |
| 21 |
| 22 // A Graph represents a fully populated build graph. It is used |
| 23 // to answer queries for the `refs` and `analyze` commands, where we |
| 24 // need to look at the graph in ways that can't easily be determined |
| 25 // from just a single Target. |
| 26 class Graph { |
| 27 public: |
| 28 Graph(const TargetVector& all_targets, const Label& default_toolchain); |
| 29 ~Graph(); |
| 30 |
| 31 const Label& default_toolchain() { return default_toolchain_; }; |
| 32 |
| 33 // Returns the roots of the build graph: the set of targets that |
| 34 // no other target depends on. |
| 35 TargetSet& roots() { return roots_; }; |
| 36 |
| 37 // Returns the set of all targets that might be affected, directly or |
| 38 // indirectly, by modifications to the given source files. |
| 39 TargetSet AllAffectedTargets(const SourceFileSet& source_files); |
| 40 |
| 41 // Returns the set of labels that do not refer to objects in the graph. |
| 42 LabelSet InvalidLabels(const LabelSet& labels); |
| 43 |
| 44 // Returns the set of all targets that have a label in the given set. |
| 45 // Invalid (or missing) labels will be ignored. |
| 46 TargetSet TargetsFor(const LabelSet& labels); |
| 47 |
| 48 // Returns a pruned set of the given targets, meaning that for each of the |
| 49 // given targets, |
| 50 // - if the target is not a group, add it to the set |
| 51 // - if the target is a group, recursively prune each dependency and add |
| 52 // its pruned results to the set. |
| 53 // |
| 54 // For example, if we had: |
| 55 // |
| 56 // group("foobar") { deps = [ ":foo", ":bar" ] } |
| 57 // group("bar") { deps = [ ":baz", ":quux" ] } |
| 58 // executable("foo") { ... } |
| 59 // executable("baz") { ... } |
| 60 // executable("quux") { ... } |
| 61 // |
| 62 // Then the pruned version of {"foobar"} would be {":foo", ":baz", ":quux"}. |
| 63 // This is used by the analyze command in order to only build the affected |
| 64 // dependencies of a group (and not also build the unaffected ones). |
| 65 TargetSet Prune(const TargetSet& targets); |
| 66 |
| 67 private: |
| 68 // Prunes an individual target and adds the results to pruned (see Prune(), |
| 69 // above). |
| 70 void PruneTarget(const Target*, TargetSet& seen, TargetSet& pruned); |
| 71 |
| 72 bool TargetRefersToFile(const Target* target, const SourceFile* file); |
| 73 |
| 74 void AddTargetsDirectlyReferringToFileTo(const SourceFile* file, |
| 75 TargetSet* matches); |
| 76 |
| 77 void AddAllRefsTo(const Target* target, TargetSet* matches); |
| 78 |
| 79 std::vector<const Target*> all_targets_; |
| 80 std::map<const Label, const Target*> labels_to_targets_; |
| 81 Label default_toolchain_; |
| 82 std::set<const Target*> roots_; |
| 83 |
| 84 // Maps targets to the list of targets that depend on them. |
| 85 DepMap dep_map_; |
| 86 }; |
| 87 |
| 88 // Given a graph and a JSON-formatted string containing lists of files |
| 89 // and targets, figures out which targets would be affected by modifications |
| 90 // to the files. See command_analyze.cc for expected formats of the files |
| 91 // and a full description of the behavior. |
| 92 Err Analyze(Graph& graph, const std::string& input, std::string* output); |
| 93 |
| 94 #endif // TOOLS_GN_GRAPH_H_ |
OLD | NEW |