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 
21 #include "dyndep.h"
22 #include "eval_env.h"
23 #include "timestamp.h"
24 #include "util.h"
25 
26 struct BuildLog;
28 struct DiskInterface;
29 struct DepsLog;
30 struct Edge;
31 struct Node;
32 struct Pool;
33 struct State;
34 
35 /// Information about a node in the dependency graph: the file, whether
36 /// it's dirty, mtime, etc.
37 struct Node {
38  Node(const std::string& path, uint64_t slash_bits)
39  : path_(path),
41  mtime_(-1),
42  dirty_(false),
43  dyndep_pending_(false),
44  in_edge_(NULL),
45  id_(-1) {}
46 
47  /// Return false on error.
48  bool Stat(DiskInterface* disk_interface, std::string* err);
49 
50  /// Return false on error.
51  bool StatIfNecessary(DiskInterface* disk_interface, std::string* err) {
52  if (status_known())
53  return true;
54  return Stat(disk_interface, err);
55  }
56 
57  /// Mark as not-yet-stat()ed and not dirty.
58  void ResetState() {
59  mtime_ = -1;
60  dirty_ = false;
61  }
62 
63  /// Mark the Node as already-stat()ed and missing.
64  void MarkMissing() {
65  mtime_ = 0;
66  }
67 
68  bool exists() const {
69  return mtime_ != 0;
70  }
71 
72  bool status_known() const {
73  return mtime_ != -1;
74  }
75 
76  const std::string& path() const { return path_; }
77  /// Get |path()| but use slash_bits to convert back to original slash styles.
78  std::string PathDecanonicalized() const {
80  }
81  static std::string PathDecanonicalized(const std::string& path,
83  uint64_t slash_bits() const { return slash_bits_; }
84 
85  TimeStamp mtime() const { return mtime_; }
86 
87  bool dirty() const { return dirty_; }
88  void set_dirty(bool dirty) { dirty_ = dirty; }
89  void MarkDirty() { dirty_ = true; }
90 
91  bool dyndep_pending() const { return dyndep_pending_; }
92  void set_dyndep_pending(bool pending) { dyndep_pending_ = pending; }
93 
94  Edge* in_edge() const { return in_edge_; }
95  void set_in_edge(Edge* edge) { in_edge_ = edge; }
96 
97  int id() const { return id_; }
98  void set_id(int id) { id_ = id; }
99 
100  const std::vector<Edge*>& out_edges() const { return out_edges_; }
101  void AddOutEdge(Edge* edge) { out_edges_.push_back(edge); }
102 
103  void Dump(const char* prefix="") const;
104 
105 private:
106  std::string path_;
107 
108  /// Set bits starting from lowest for backslashes that were normalized to
109  /// forward slashes by CanonicalizePath. See |PathDecanonicalized|.
111 
112  /// Possible values of mtime_:
113  /// -1: file hasn't been examined
114  /// 0: we looked, and file doesn't exist
115  /// >0: actual file's mtime
117 
118  /// Dirty is true when the underlying file is out-of-date.
119  /// But note that Edge::outputs_ready_ is also used in judging which
120  /// edges to build.
121  bool dirty_;
122 
123  /// Store whether dyndep information is expected from this node but
124  /// has not yet been loaded.
126 
127  /// The Edge that produces this Node, or NULL when there is no
128  /// known edge to produce it.
130 
131  /// All Edges that use this Node as an input.
132  std::vector<Edge*> out_edges_;
133 
134  /// A dense integer id for the node, assigned and used by DepsLog.
135  int id_;
136 };
137 
138 /// An edge in the dependency graph; links between Nodes using Rules.
139 struct Edge {
140  enum VisitMark {
144  };
145 
146  Edge() : rule_(NULL), pool_(NULL), dyndep_(NULL), env_(NULL),
147  mark_(VisitNone), outputs_ready_(false), deps_loaded_(false),
149  implicit_outs_(0) {}
150 
151  /// Return true if all inputs' in-edges are ready.
152  bool AllInputsReady() const;
153 
154  /// Expand all variables in a command and return it as a string.
155  /// If incl_rsp_file is enabled, the string will also contain the
156  /// full contents of a response file (if applicable)
157  std::string EvaluateCommand(bool incl_rsp_file = false) const;
158 
159  /// Returns the shell-escaped value of |key|.
160  std::string GetBinding(const std::string& key) const;
161  bool GetBindingBool(const std::string& key) const;
162 
163  /// Like GetBinding("depfile"), but without shell escaping.
164  std::string GetUnescapedDepfile() const;
165  /// Like GetBinding("dyndep"), but without shell escaping.
166  std::string GetUnescapedDyndep() const;
167  /// Like GetBinding("rspfile"), but without shell escaping.
168  std::string GetUnescapedRspfile() const;
169 
170  void Dump(const char* prefix="") const;
171 
172  const Rule* rule_;
174  std::vector<Node*> inputs_;
175  std::vector<Node*> outputs_;
182 
183  const Rule& rule() const { return *rule_; }
184  Pool* pool() const { return pool_; }
185  int weight() const { return 1; }
186  bool outputs_ready() const { return outputs_ready_; }
187 
188  // There are three types of inputs.
189  // 1) explicit deps, which show up as $in on the command line;
190  // 2) implicit deps, which the target depends on implicitly (e.g. C headers),
191  // and changes in them cause the target to rebuild;
192  // 3) order-only deps, which are needed before the target builds but which
193  // don't cause the target to rebuild.
194  // These are stored in inputs_ in that order, and we keep counts of
195  // #2 and #3 when we need to access the various subsets.
198  bool is_implicit(size_t index) {
199  return index >= inputs_.size() - order_only_deps_ - implicit_deps_ &&
200  !is_order_only(index);
201  }
202  bool is_order_only(size_t index) {
203  return index >= inputs_.size() - order_only_deps_;
204  }
205 
206  // There are two types of outputs.
207  // 1) explicit outs, which show up as $out on the command line;
208  // 2) implicit outs, which the target generates but are not part of $out.
209  // These are stored in outputs_ in that order, and we keep a count of
210  // #2 to use when we need to access the various subsets.
212  bool is_implicit_out(size_t index) const {
213  return index >= outputs_.size() - implicit_outs_;
214  }
215 
216  bool is_phony() const;
217  bool use_console() const;
218  bool maybe_phonycycle_diagnostic() const;
219 };
220 
221 
222 /// ImplicitDepLoader loads implicit dependencies, as referenced via the
223 /// "depfile" attribute in build files.
226  DiskInterface* disk_interface,
227  DepfileParserOptions const* depfile_parser_options)
228  : state_(state), disk_interface_(disk_interface), deps_log_(deps_log),
229  depfile_parser_options_(depfile_parser_options) {}
230 
231  /// Load implicit dependencies for \a edge.
232  /// @return false on error (without filling \a err if info is just missing
233  // or out of date).
234  bool LoadDeps(Edge* edge, std::string* err);
235 
236  DepsLog* deps_log() const {
237  return deps_log_;
238  }
239 
240  private:
241  /// Load implicit dependencies for \a edge from a depfile attribute.
242  /// @return false on error (without filling \a err if info is just missing).
243  bool LoadDepFile(Edge* edge, const std::string& path, std::string* err);
244 
245  /// Load implicit dependencies for \a edge from the DepsLog.
246  /// @return false on error (without filling \a err if info is just missing).
247  bool LoadDepsFromLog(Edge* edge, std::string* err);
248 
249  /// Preallocate \a count spaces in the input array on \a edge, returning
250  /// an iterator pointing at the first new space.
251  std::vector<Node*>::iterator PreallocateSpace(Edge* edge, int count);
252 
253  /// If we don't have a edge that generates this input already,
254  /// create one; this makes us not abort if the input is missing,
255  /// but instead will rebuild in that circumstance.
256  void CreatePhonyInEdge(Node* node);
257 
262 };
263 
264 
265 /// DependencyScan manages the process of scanning the files in a graph
266 /// and updating the dirty/outputs_ready state of all the nodes and edges.
269  DiskInterface* disk_interface,
270  DepfileParserOptions const* depfile_parser_options)
272  disk_interface_(disk_interface),
273  dep_loader_(state, deps_log, disk_interface, depfile_parser_options),
274  dyndep_loader_(state, disk_interface) {}
275 
276  /// Update the |dirty_| state of the given node by inspecting its input edge.
277  /// Examine inputs, outputs, and command lines to judge whether an edge
278  /// needs to be re-run, and update outputs_ready_ and each outputs' |dirty_|
279  /// state accordingly.
280  /// Returns false on failure.
281  bool RecomputeDirty(Node* node, std::string* err);
282 
283  /// Recompute whether any output of the edge is dirty, if so sets |*dirty|.
284  /// Returns false on failure.
285  bool RecomputeOutputsDirty(Edge* edge, Node* most_recent_input,
286  bool* dirty, std::string* err);
287 
288  BuildLog* build_log() const {
289  return build_log_;
290  }
291  void set_build_log(BuildLog* log) {
292  build_log_ = log;
293  }
294 
295  DepsLog* deps_log() const {
296  return dep_loader_.deps_log();
297  }
298 
299  /// Load a dyndep file from the given node's path and update the
300  /// build graph with the new information. One overload accepts
301  /// a caller-owned 'DyndepFile' object in which to store the
302  /// information loaded from the dyndep file.
303  bool LoadDyndeps(Node* node, std::string* err) const;
304  bool LoadDyndeps(Node* node, DyndepFile* ddf, std::string* err) const;
305 
306  private:
307  bool RecomputeDirty(Node* node, std::vector<Node*>* stack, std::string* err);
308  bool VerifyDAG(Node* node, std::vector<Node*>* stack, std::string* err);
309 
310  /// Recompute whether a given single output should be marked dirty.
311  /// Returns true if so.
312  bool RecomputeOutputDirty(const Edge* edge, const Node* most_recent_input,
313  const std::string& command, Node* output);
314 
319 };
320 
321 #endif // NINJA_GRAPH_H_
bool dyndep_pending() const
Definition: graph.h:91
std::vector< Edge * > out_edges_
All Edges that use this Node as an input.
Definition: graph.h:132
bool Stat(DiskInterface *disk_interface, std::string *err)
Return false on error.
Definition: graph.cc:33
bool RecomputeOutputDirty(const Edge *edge, const Node *most_recent_input, const std::string &command, Node *output)
Recompute whether a given single output should be marked dirty.
Definition: graph.cc:228
int order_only_deps_
Definition: graph.h:197
void Dump(const char *prefix="") const
Definition: graph.cc:435
int implicit_deps_
Definition: graph.h:196
DepsLog * deps_log() const
Definition: graph.h:236
Store data loaded from one dyndep file.
Definition: dyndep.h:40
VisitMark mark_
Definition: graph.h:178
void Dump(const char *prefix="") const
Definition: graph.cc:487
Edge * in_edge() const
Definition: graph.h:94
DepfileParserOptions const * depfile_parser_options_
Definition: graph.h:261
bool RecomputeOutputsDirty(Edge *edge, Node *most_recent_input, bool *dirty, std::string *err)
Recompute whether any output of the edge is dirty, if so sets |*dirty|.
Definition: graph.cc:215
void set_dirty(bool dirty)
Definition: graph.h:88
Information about a node in the dependency graph: the file, whether it&#39;s dirty, mtime, etc.
Definition: graph.h:37
bool RecomputeDirty(Node *node, std::string *err)
Update the |dirty_| state of the given node by inspecting its input edge.
bool outputs_ready() const
Definition: graph.h:186
DyndepLoader dyndep_loader_
Definition: graph.h:318
std::vector< Node * >::iterator PreallocateSpace(Edge *edge, int count)
Preallocate count spaces in the input array on edge, returning an iterator pointing at the first new ...
Definition: graph.cc:639
ImplicitDepLoader dep_loader_
Definition: graph.h:317
Interface for accessing the disk.
uint64_t slash_bits() const
Definition: graph.h:83
bool StatIfNecessary(DiskInterface *disk_interface, std::string *err)
Return false on error.
Definition: graph.h:51
bool is_implicit_out(size_t index) const
Definition: graph.h:212
bool LoadDyndeps(Node *node, std::string *err) const
Load a dyndep file from the given node&#39;s path and update the build graph with the new information...
std::vector< Node * > outputs_
Definition: graph.h:175
void AddOutEdge(Edge *edge)
Definition: graph.h:101
bool LoadDepsFromLog(Edge *edge, std::string *err)
Load implicit dependencies for edge from the DepsLog.
Definition: graph.cc:612
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:139
Edge * in_edge_
The Edge that produces this Node, or NULL when there is no known edge to produce it.
Definition: graph.h:129
bool LoadDepFile(Edge *edge, const std::string &path, std::string *err)
Load implicit dependencies for edge from a depfile attribute.
Definition: graph.cc:528
Store a log of every command ran for every build.
Definition: build_log.h:43
std::string PathDecanonicalized() const
Get |path()| but use slash_bits to convert back to original slash styles.
Definition: graph.h:78
bool is_order_only(size_t index)
Definition: graph.h:202
const std::string & path() const
Definition: graph.h:76
int id() const
Definition: graph.h:97
void MarkDirty()
Definition: graph.h:89
int weight() const
Definition: graph.h:185
std::string path_
Definition: graph.h:106
As build commands run they can output extra dependency information (e.g.
Definition: deps_log.h:68
An Env which contains a mapping of variables to values as well as a pointer to a parent scope...
Definition: eval_env.h:81
bool outputs_ready_
Definition: graph.h:179
bool is_implicit(size_t index)
Definition: graph.h:198
void set_dyndep_pending(bool pending)
Definition: graph.h:92
void set_id(int id)
Definition: graph.h:98
DiskInterface * disk_interface_
Definition: graph.h:316
DepsLog * deps_log_
Definition: graph.h:260
BindingEnv * env_
Definition: graph.h:177
An invokable build command and associated metadata (description, etc.).
Definition: eval_env.h:59
void ResetState()
Mark as not-yet-stat()ed and not dirty.
Definition: graph.h:58
void MarkMissing()
Mark the Node as already-stat()ed and missing.
Definition: graph.h:64
bool dirty() const
Definition: graph.h:87
uint64_t slash_bits_
Set bits starting from lowest for backslashes that were normalized to forward slashes by Canonicalize...
Definition: graph.h:110
std::string GetUnescapedRspfile() const
Like GetBinding("rspfile"), but without shell escaping.
Definition: graph.cc:430
BuildLog * build_log_
Definition: graph.h:315
bool VerifyDAG(Node *node, std::vector< Node *> *stack, std::string *err)
Definition: graph.cc:176
ImplicitDepLoader(State *state, DepsLog *deps_log, DiskInterface *disk_interface, DepfileParserOptions const *depfile_parser_options)
Definition: graph.h:225
DepsLog * deps_log() const
Definition: graph.h:295
BuildLog * build_log() const
Definition: graph.h:288
Edge()
Definition: graph.h:146
Node(const std::string &path, uint64_t slash_bits)
Definition: graph.h:38
int implicit_outs_
Definition: graph.h:211
void CreatePhonyInEdge(Node *node)
If we don&#39;t have a edge that generates this input already, create one; this makes us not abort if the...
Definition: graph.cc:647
int64_t TimeStamp
Definition: timestamp.h:31
bool LoadDeps(Edge *edge, std::string *err)
Load implicit dependencies for edge.
Definition: graph.cc:504
DyndepLoader loads dynamically discovered dependencies, as referenced via the "dyndep" attribute in b...
Definition: dyndep.h:44
A pool for delayed edges.
Definition: state.h:39
void set_build_log(BuildLog *log)
Definition: graph.h:291
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:116
std::vector< Node * > inputs_
Definition: graph.h:174
std::string GetUnescapedDepfile() const
Like GetBinding("depfile"), but without shell escaping.
Definition: graph.cc:420
bool status_known() const
Definition: graph.h:72
bool dirty_
Dirty is true when the underlying file is out-of-date.
Definition: graph.h:121
int id_
A dense integer id for the node, assigned and used by DepsLog.
Definition: graph.h:135
Pool * pool_
Definition: graph.h:173
std::string GetBinding(const std::string &key) const
Returns the shell-escaped value of |key|.
Definition: graph.cc:411
TimeStamp mtime() const
Definition: graph.h:85
DependencyScan manages the process of scanning the files in a graph and updating the dirty/outputs_re...
Definition: graph.h:267
const Rule * rule_
Definition: graph.h:172
ImplicitDepLoader loads implicit dependencies, as referenced via the "depfile" attribute in build fil...
Definition: graph.h:224
VisitMark
Definition: graph.h:140
const Rule & rule() const
Definition: graph.h:183
Pool * pool() const
Definition: graph.h:184
std::string EvaluateCommand(bool incl_rsp_file=false) const
Expand all variables in a command and return it as a string.
Definition: graph.cc:401
bool AllInputsReady() const
Return true if all inputs&#39; in-edges are ready.
Definition: graph.cc:316
bool maybe_phonycycle_diagnostic() const
Definition: graph.cc:464
const std::vector< Edge * > & out_edges() const
Definition: graph.h:100
State * state_
Definition: graph.h:258
Global state (file status) for a single run.
Definition: state.h:84
void set_in_edge(Edge *edge)
Definition: graph.h:95
bool deps_loaded_
Definition: graph.h:180
unsigned long long uint64_t
Definition: win32port.h:29
DiskInterface * disk_interface_
Definition: graph.h:259
bool is_phony() const
Definition: graph.cc:456
bool use_console() const
Definition: graph.cc:460
bool GetBindingBool(const std::string &key) const
Definition: graph.cc:416
std::string GetUnescapedDyndep() const
Like GetBinding("dyndep"), but without shell escaping.
Definition: graph.cc:425
bool exists() const
Definition: graph.h:68
bool dyndep_pending_
Store whether dyndep information is expected from this node but has not yet been loaded.
Definition: graph.h:125
Node * dyndep_
Definition: graph.h:176
bool deps_missing_
Definition: graph.h:181
DependencyScan(State *state, BuildLog *build_log, DepsLog *deps_log, DiskInterface *disk_interface, DepfileParserOptions const *depfile_parser_options)
Definition: graph.h:268