Ninja
graph.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NINJA_GRAPH_H_
16 #define NINJA_GRAPH_H_
17 
18 #include <string>
19 #include <vector>
20 using namespace std;
21 
22 #include "eval_env.h"
23 #include "timestamp.h"
24 
25 struct BuildLog;
26 struct DiskInterface;
27 struct DepsLog;
28 struct Edge;
29 struct Node;
30 struct Pool;
31 struct State;
32 
33 /// Information about a node in the dependency graph: the file, whether
34 /// it's dirty, mtime, etc.
35 struct Node {
36  explicit Node(const string& path)
37  : path_(path),
38  mtime_(-1),
39  dirty_(false),
40  in_edge_(NULL),
41  id_(-1) {}
42 
43  /// Return true if the file exists (mtime_ got a value).
44  bool Stat(DiskInterface* disk_interface);
45 
46  /// Return true if we needed to stat.
47  bool StatIfNecessary(DiskInterface* disk_interface) {
48  if (status_known())
49  return false;
50  Stat(disk_interface);
51  return true;
52  }
53 
54  /// Mark as not-yet-stat()ed and not dirty.
55  void ResetState() {
56  mtime_ = -1;
57  dirty_ = false;
58  }
59 
60  /// Mark the Node as already-stat()ed and missing.
61  void MarkMissing() {
62  mtime_ = 0;
63  }
64 
65  bool exists() const {
66  return mtime_ != 0;
67  }
68 
69  bool status_known() const {
70  return mtime_ != -1;
71  }
72 
73  const string& path() const { return path_; }
74  TimeStamp mtime() const { return mtime_; }
75 
76  bool dirty() const { return dirty_; }
77  void set_dirty(bool dirty) { dirty_ = dirty; }
78  void MarkDirty() { dirty_ = true; }
79 
80  Edge* in_edge() const { return in_edge_; }
81  void set_in_edge(Edge* edge) { in_edge_ = edge; }
82 
83  int id() const { return id_; }
84  void set_id(int id) { id_ = id; }
85 
86  const vector<Edge*>& out_edges() const { return out_edges_; }
87  void AddOutEdge(Edge* edge) { out_edges_.push_back(edge); }
88 
89  void Dump(const char* prefix="") const;
90 
91 private:
92  string path_;
93  /// Possible values of mtime_:
94  /// -1: file hasn't been examined
95  /// 0: we looked, and file doesn't exist
96  /// >0: actual file's mtime
98 
99  /// Dirty is true when the underlying file is out-of-date.
100  /// But note that Edge::outputs_ready_ is also used in judging which
101  /// edges to build.
102  bool dirty_;
103 
104  /// The Edge that produces this Node, or NULL when there is no
105  /// known edge to produce it.
107 
108  /// All Edges that use this Node as an input.
109  vector<Edge*> out_edges_;
110 
111  /// A dense integer id for the node, assigned and used by DepsLog.
112  int id_;
113 };
114 
115 /// An invokable build command and associated metadata (description, etc.).
116 struct Rule {
117  explicit Rule(const string& name) : name_(name) {}
118 
119  const string& name() const { return name_; }
120 
121  typedef map<string, EvalString> Bindings;
122  void AddBinding(const string& key, const EvalString& val);
123 
124  static bool IsReservedBinding(const string& var);
125 
126  const EvalString* GetBinding(const string& key) const;
127 
128  private:
129  // Allow the parsers to reach into this object and fill out its fields.
130  friend struct ManifestParser;
131 
132  string name_;
133  map<string, EvalString> bindings_;
134 };
135 
136 /// An edge in the dependency graph; links between Nodes using Rules.
137 struct Edge {
138  Edge() : rule_(NULL), env_(NULL), outputs_ready_(false), deps_missing_(false),
139  implicit_deps_(0), order_only_deps_(0) {}
140 
141  /// Return true if all inputs' in-edges are ready.
142  bool AllInputsReady() const;
143 
144  /// Expand all variables in a command and return it as a string.
145  /// If incl_rsp_file is enabled, the string will also contain the
146  /// full contents of a response file (if applicable)
147  string EvaluateCommand(bool incl_rsp_file = false);
148 
149  /// Returns the shell-escaped value of |key|.
150  string GetBinding(const string& key);
151  bool GetBindingBool(const string& key);
152 
153  /// Like GetBinding("depfile"), but without shell escaping.
154  string GetUnescapedDepfile();
155  /// Like GetBinding("rspfile"), but without shell escaping.
156  string GetUnescapedRspfile();
157 
158  void Dump(const char* prefix="") const;
159 
160  const Rule* rule_;
162  vector<Node*> inputs_;
163  vector<Node*> outputs_;
167 
168  const Rule& rule() const { return *rule_; }
169  Pool* pool() const { return pool_; }
170  int weight() const { return 1; }
171  bool outputs_ready() const { return outputs_ready_; }
172 
173  // There are three types of inputs.
174  // 1) explicit deps, which show up as $in on the command line;
175  // 2) implicit deps, which the target depends on implicitly (e.g. C headers),
176  // and changes in them cause the target to rebuild;
177  // 3) order-only deps, which are needed before the target builds but which
178  // don't cause the target to rebuild.
179  // These are stored in inputs_ in that order, and we keep counts of
180  // #2 and #3 when we need to access the various subsets.
183  bool is_implicit(size_t index) {
184  return index >= inputs_.size() - order_only_deps_ - implicit_deps_ &&
185  !is_order_only(index);
186  }
187  bool is_order_only(size_t index) {
188  return index >= inputs_.size() - order_only_deps_;
189  }
190 
191  bool is_phony() const;
192  bool use_console() const;
193 };
194 
195 
196 /// ImplicitDepLoader loads implicit dependencies, as referenced via the
197 /// "depfile" attribute in build files.
199  ImplicitDepLoader(State* state, DepsLog* deps_log,
200  DiskInterface* disk_interface)
201  : state_(state), disk_interface_(disk_interface), deps_log_(deps_log) {}
202 
203  /// Load implicit dependencies for \a edge.
204  /// @return false on error (without filling \a err if info is just missing
205  // or out of date).
206  bool LoadDeps(Edge* edge, string* err);
207 
208  DepsLog* deps_log() const {
209  return deps_log_;
210  }
211 
212  private:
213  /// Load implicit dependencies for \a edge from a depfile attribute.
214  /// @return false on error (without filling \a err if info is just missing).
215  bool LoadDepFile(Edge* edge, const string& path, string* err);
216 
217  /// Load implicit dependencies for \a edge from the DepsLog.
218  /// @return false on error (without filling \a err if info is just missing).
219  bool LoadDepsFromLog(Edge* edge, string* err);
220 
221  /// Preallocate \a count spaces in the input array on \a edge, returning
222  /// an iterator pointing at the first new space.
223  vector<Node*>::iterator PreallocateSpace(Edge* edge, int count);
224 
225  /// If we don't have a edge that generates this input already,
226  /// create one; this makes us not abort if the input is missing,
227  /// but instead will rebuild in that circumstance.
228  void CreatePhonyInEdge(Node* node);
229 
233 };
234 
235 
236 /// DependencyScan manages the process of scanning the files in a graph
237 /// and updating the dirty/outputs_ready state of all the nodes and edges.
239  DependencyScan(State* state, BuildLog* build_log, DepsLog* deps_log,
240  DiskInterface* disk_interface)
241  : build_log_(build_log),
242  disk_interface_(disk_interface),
243  dep_loader_(state, deps_log, disk_interface) {}
244 
245  /// Examine inputs, outputs, and command lines to judge whether an edge
246  /// needs to be re-run, and update outputs_ready_ and each outputs' |dirty_|
247  /// state accordingly.
248  /// Returns false on failure.
249  bool RecomputeDirty(Edge* edge, string* err);
250 
251  /// Recompute whether any output of the edge is dirty.
252  /// Returns true if so.
253  bool RecomputeOutputsDirty(Edge* edge, Node* most_recent_input);
254 
255  BuildLog* build_log() const {
256  return build_log_;
257  }
258  void set_build_log(BuildLog* log) {
259  build_log_ = log;
260  }
261 
262  DepsLog* deps_log() const {
263  return dep_loader_.deps_log();
264  }
265 
266  private:
267  /// Recompute whether a given single output should be marked dirty.
268  /// Returns true if so.
269  bool RecomputeOutputDirty(Edge* edge, Node* most_recent_input,
270  const string& command, Node* output);
271 
275 };
276 
277 #endif // NINJA_GRAPH_H_
int order_only_deps_
Definition: graph.h:182
TimeStamp mtime() const
Definition: graph.h:74
int implicit_deps_
Definition: graph.h:181
void set_dirty(bool dirty)
Definition: graph.h:77
Information about a node in the dependency graph: the file, whether it&#39;s dirty, mtime, etc.
Definition: graph.h:35
bool StatIfNecessary(DiskInterface *disk_interface)
Return true if we needed to stat.
Definition: graph.h:47
Node(const string &path)
Definition: graph.h:36
ImplicitDepLoader dep_loader_
Definition: graph.h:274
Interface for accessing the disk.
Edge * in_edge() const
Definition: graph.h:80
void AddOutEdge(Edge *edge)
Definition: graph.h:87
int TimeStamp
Definition: timestamp.h:22
bool outputs_ready() const
Definition: graph.h:171
Pool * pool() const
Definition: graph.h:169
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:137
Edge * in_edge_
The Edge that produces this Node, or NULL when there is no known edge to produce it.
Definition: graph.h:106
Store a log of every command ran for every build.
Definition: build_log.h:42
bool is_order_only(size_t index)
Definition: graph.h:187
void MarkDirty()
Definition: graph.h:78
ImplicitDepLoader(State *state, DepsLog *deps_log, DiskInterface *disk_interface)
Definition: graph.h:199
vector< Edge * > out_edges_
All Edges that use this Node as an input.
Definition: graph.h:109
vector< Node * > inputs_
Definition: graph.h:162
As build commands run they can output extra dependency information (e.g.
Definition: deps_log.h:66
Parses .ninja files.
An Env which contains a mapping of variables to values as well as a pointer to a parent scope...
Definition: eval_env.h:35
bool outputs_ready_
Definition: graph.h:165
bool is_implicit(size_t index)
Definition: graph.h:183
BuildLog * build_log() const
Definition: graph.h:255
void set_id(int id)
Definition: graph.h:84
DiskInterface * disk_interface_
Definition: graph.h:273
DepsLog * deps_log_
Definition: graph.h:232
BindingEnv * env_
Definition: graph.h:164
An invokable build command and associated metadata (description, etc.).
Definition: graph.h:116
bool dirty() const
Definition: graph.h:76
bool exists() const
Definition: graph.h:65
void ResetState()
Mark as not-yet-stat()ed and not dirty.
Definition: graph.h:55
void MarkMissing()
Mark the Node as already-stat()ed and missing.
Definition: graph.h:61
bool status_known() const
Definition: graph.h:69
BuildLog * build_log_
Definition: graph.h:272
DepsLog * deps_log() const
Definition: graph.h:208
Edge()
Definition: graph.h:138
A pool for delayed edges.
Definition: state.h:39
void set_build_log(BuildLog *log)
Definition: graph.h:258
TimeStamp mtime_
Possible values of mtime_: -1: file hasn&#39;t been examined 0: we looked, and file doesn&#39;t exist >0: act...
Definition: graph.h:97
const string & path() const
Definition: graph.h:73
int id() const
Definition: graph.h:83
DependencyScan(State *state, BuildLog *build_log, DepsLog *deps_log, DiskInterface *disk_interface)
Definition: graph.h:239
map< string, EvalString > Bindings
Definition: graph.h:121
bool dirty_
Dirty is true when the underlying file is out-of-date.
Definition: graph.h:102
int id_
A dense integer id for the node, assigned and used by DepsLog.
Definition: graph.h:112
Pool * pool_
Definition: graph.h:161
string name_
Definition: graph.h:132
DependencyScan manages the process of scanning the files in a graph and updating the dirty/outputs_re...
Definition: graph.h:238
const Rule * rule_
Definition: graph.h:160
ImplicitDepLoader loads implicit dependencies, as referenced via the "depfile" attribute in build fil...
Definition: graph.h:198
State * state_
Definition: graph.h:230
Global state (file status, loaded rules) for a single run.
Definition: state.h:83
map< string, EvalString > bindings_
Definition: graph.h:133
void set_in_edge(Edge *edge)
Definition: graph.h:81
const string & name() const
Definition: graph.h:119
DiskInterface * disk_interface_
Definition: graph.h:231
int weight() const
Definition: graph.h:170
string path_
Definition: graph.h:92
const Rule & rule() const
Definition: graph.h:168
A tokenized string that contains variable references.
Definition: eval_env.h:59
const vector< Edge * > & out_edges() const
Definition: graph.h:86
bool deps_missing_
Definition: graph.h:166
DepsLog * deps_log() const
Definition: graph.h:262
vector< Node * > outputs_
Definition: graph.h:163
Rule(const string &name)
Definition: graph.h:117