Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: tools/gn/analyzer.h

Issue 2265833002: Implement `gn analyze`. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: do not pretty print the written json; this gets around crlf issues Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/analyzer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_
OLDNEW
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/analyzer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698