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

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

Issue 2265833002: Implement `gn analyze`. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sample tests 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
OLDNEW
(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*>;
brettw 2016/08/22 21:08:27 These shouldn't be added to the global namespace.
Dirk Pranke 2016/08/23 19:27:31 Good catch, agreed. Some of the names are used i
17 using LabelSet = std::set<const Label*>;
brettw 2016/08/22 21:08:27 This should be std::set<Label> since labels have n
Dirk Pranke 2016/08/23 19:27:31 Ack. My thinking was simply that "someone else" ne
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 {
brettw 2016/08/22 21:08:27 Class name should match file name suggestion from
Dirk Pranke 2016/08/23 19:27:31 Acknowledged.
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);
brettw 2016/08/22 21:08:27 Don't pass non-const refs. The style guide says th
Dirk Pranke 2016/08/23 19:27:31 Good catch. (They're in-out parameters, but, yeah,
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);
brettw 2016/08/22 21:08:27 Having "graph" be a non-const ref is against the s
Dirk Pranke 2016/08/23 19:27:31 No particular reason apart from keeping the class
93
94 #endif // TOOLS_GN_GRAPH_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698