OLD | NEW |
(Empty) | |
| 1 // Copyright 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_ANALYZER_H_ |
| 6 #define TOOLS_GN_ANALYZER_H_ |
| 7 |
| 8 #include <set> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "tools/gn/builder.h" |
| 13 #include "tools/gn/label.h" |
| 14 #include "tools/gn/source_file.h" |
| 15 #include "tools/gn/target.h" |
| 16 |
| 17 // An Analyzer can answer questions about a build graph. It is used |
| 18 // to answer queries for the `refs` and `analyze` commands, where we |
| 19 // need to look at the graph in ways that can't easily be determined |
| 20 // from just a single Target. |
| 21 class Analyzer { |
| 22 public: |
| 23 using LabelSet = std::set<Label>; |
| 24 using SourceFileSet = std::set<const SourceFile*>; |
| 25 using TargetSet = std::set<const Target*>; |
| 26 |
| 27 explicit Analyzer(const Builder& builder); |
| 28 ~Analyzer(); |
| 29 |
| 30 // Figures out from a Buider and a JSON-formatted string containing lists |
| 31 // of files and targets, which targets would be affected by modifications |
| 32 // to the files . See the help text for the analyze command (kAnalyze_Help) |
| 33 // for the specification of the input and output string formats and the |
| 34 // expected behavior of the method. |
| 35 std::string Analyze(const std::string& input, Err* err) const; |
| 36 |
| 37 private: |
| 38 // Returns the roots of the build graph: the set of targets that |
| 39 // no other target depends on. |
| 40 TargetSet& roots() { return roots_; }; |
| 41 |
| 42 // Returns the set of all targets that might be affected, directly or |
| 43 // indirectly, by modifications to the given source files. |
| 44 TargetSet AllAffectedTargets(const SourceFileSet& source_files) const; |
| 45 |
| 46 // Returns the set of labels that do not refer to objects in the graph. |
| 47 LabelSet InvalidLabels(const LabelSet& labels) const; |
| 48 |
| 49 // Returns the set of all targets that have a label in the given set. |
| 50 // Invalid (or missing) labels will be ignored. |
| 51 TargetSet TargetsFor(const LabelSet& labels) const; |
| 52 |
| 53 // Returns a filtered set of the given targets, meaning that for each of the |
| 54 // given targets, |
| 55 // - if the target is not a group, add it to the set |
| 56 // - if the target is a group, recursively filter each dependency and add |
| 57 // its filtered results to the set. |
| 58 // |
| 59 // For example, if we had: |
| 60 // |
| 61 // group("foobar") { deps = [ ":foo", ":bar" ] } |
| 62 // group("bar") { deps = [ ":baz", ":quux" ] } |
| 63 // executable("foo") { ... } |
| 64 // executable("baz") { ... } |
| 65 // executable("quux") { ... } |
| 66 // |
| 67 // Then the filtered version of {"foobar"} would be {":foo", ":baz", |
| 68 // ":quux"}. This is used by the analyze command in order to only build |
| 69 // the affected dependencies of a group (and not also build the unaffected |
| 70 // ones). |
| 71 // |
| 72 // This filtering behavior is also known as "pruning" the list of targets. |
| 73 TargetSet Filter(const TargetSet& targets) const; |
| 74 |
| 75 // Filter an individual target and adds the results to filtered |
| 76 // (see Filter(), above). |
| 77 void FilterTarget(const Target*, TargetSet* seen, TargetSet* filtered) const; |
| 78 |
| 79 bool TargetRefersToFile(const Target* target, const SourceFile* file) const; |
| 80 |
| 81 void AddTargetsDirectlyReferringToFileTo(const SourceFile* file, |
| 82 TargetSet* matches) const; |
| 83 |
| 84 void AddAllRefsTo(const Target* target, TargetSet* matches) const; |
| 85 |
| 86 std::vector<const Target*> all_targets_; |
| 87 std::map<const Label, const Target*> labels_to_targets_; |
| 88 Label default_toolchain_; |
| 89 std::set<const Target*> roots_; |
| 90 |
| 91 // Maps targets to the list of targets that depend on them. |
| 92 std::multimap<const Target*, const Target*> dep_map_; |
| 93 }; |
| 94 |
| 95 #endif // TOOLS_GN_ANALYZER_H_ |
OLD | NEW |