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  Node(const string& path, unsigned int slash_bits)
37  : path_(path),
38  slash_bits_(slash_bits),
39  mtime_(-1),
40  dirty_(false),
41  in_edge_(NULL),
42  id_(-1) {}
43 
44  /// Return false on error.
45  bool Stat(DiskInterface* disk_interface, string* err);
46 
47  /// Return false on error.
48  bool StatIfNecessary(DiskInterface* disk_interface, string* err) {
49  if (status_known())
50  return true;
51  return Stat(disk_interface, err);
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  /// Get |path()| but use slash_bits to convert back to original slash styles.
75  string PathDecanonicalized() const;
76  unsigned int slash_bits() const { return slash_bits_; }
77  TimeStamp mtime() const { return mtime_; }
78 
79  bool dirty() const { return dirty_; }
80  void set_dirty(bool dirty) { dirty_ = dirty; }
81  void MarkDirty() { dirty_ = true; }
82 
83  Edge* in_edge() const { return in_edge_; }
84  void set_in_edge(Edge* edge) { in_edge_ = edge; }
85 
86  int id() const { return id_; }
87  void set_id(int id) { id_ = id; }
88 
89  const vector<Edge*>& out_edges() const { return out_edges_; }
90  void AddOutEdge(Edge* edge) { out_edges_.push_back(edge); }
91 
92  void Dump(const char* prefix="") const;
93 
94 private:
95  string path_;
96 
97  /// Set bits starting from lowest for backslashes that were normalized to
98  /// forward slashes by CanonicalizePath. See |PathDecanonicalized|.
99  unsigned int slash_bits_;
100 
101  /// Possible values of mtime_:
102  /// -1: file hasn't been examined
103  /// 0: we looked, and file doesn't exist
104  /// >0: actual file's mtime
106 
107  /// Dirty is true when the underlying file is out-of-date.
108  /// But note that Edge::outputs_ready_ is also used in judging which
109  /// edges to build.
110  bool dirty_;
111 
112  /// The Edge that produces this Node, or NULL when there is no
113  /// known edge to produce it.
115 
116  /// All Edges that use this Node as an input.
117  vector<Edge*> out_edges_;
118 
119  /// A dense integer id for the node, assigned and used by DepsLog.
120  int id_;
121 };
122 
123 /// An edge in the dependency graph; links between Nodes using Rules.
124 struct Edge {
125  Edge() : rule_(NULL), pool_(NULL), env_(NULL),
126  outputs_ready_(false), deps_missing_(false),
127  implicit_deps_(0), order_only_deps_(0) {}
128 
129  /// Return true if all inputs' in-edges are ready.
130  bool AllInputsReady() const;
131 
132  /// Expand all variables in a command and return it as a string.
133  /// If incl_rsp_file is enabled, the string will also contain the
134  /// full contents of a response file (if applicable)
135  string EvaluateCommand(bool incl_rsp_file = false);
136 
137  /// Returns the shell-escaped value of |key|.
138  string GetBinding(const string& key);
139  bool GetBindingBool(const string& key);
140 
141  /// Like GetBinding("depfile"), but without shell escaping.
142  string GetUnescapedDepfile();
143  /// Like GetBinding("rspfile"), but without shell escaping.
144  string GetUnescapedRspfile();
145 
146  void Dump(const char* prefix="") const;
147 
148  const Rule* rule_;
150  vector<Node*> inputs_;
151  vector<Node*> outputs_;
155 
156  const Rule& rule() const { return *rule_; }
157  Pool* pool() const { return pool_; }
158  int weight() const { return 1; }
159  bool outputs_ready() const { return outputs_ready_; }
160 
161  // There are three types of inputs.
162  // 1) explicit deps, which show up as $in on the command line;
163  // 2) implicit deps, which the target depends on implicitly (e.g. C headers),
164  // and changes in them cause the target to rebuild;
165  // 3) order-only deps, which are needed before the target builds but which
166  // don't cause the target to rebuild.
167  // These are stored in inputs_ in that order, and we keep counts of
168  // #2 and #3 when we need to access the various subsets.
171  bool is_implicit(size_t index) {
172  return index >= inputs_.size() - order_only_deps_ - implicit_deps_ &&
173  !is_order_only(index);
174  }
175  bool is_order_only(size_t index) {
176  return index >= inputs_.size() - order_only_deps_;
177  }
178 
179  bool is_phony() const;
180  bool use_console() const;
181 };
182 
183 
184 /// ImplicitDepLoader loads implicit dependencies, as referenced via the
185 /// "depfile" attribute in build files.
187  ImplicitDepLoader(State* state, DepsLog* deps_log,
188  DiskInterface* disk_interface)
189  : state_(state), disk_interface_(disk_interface), deps_log_(deps_log) {}
190 
191  /// Load implicit dependencies for \a edge.
192  /// @return false on error (without filling \a err if info is just missing
193  // or out of date).
194  bool LoadDeps(Edge* edge, string* err);
195 
196  DepsLog* deps_log() const {
197  return deps_log_;
198  }
199 
200  private:
201  /// Load implicit dependencies for \a edge from a depfile attribute.
202  /// @return false on error (without filling \a err if info is just missing).
203  bool LoadDepFile(Edge* edge, const string& path, string* err);
204 
205  /// Load implicit dependencies for \a edge from the DepsLog.
206  /// @return false on error (without filling \a err if info is just missing).
207  bool LoadDepsFromLog(Edge* edge, string* err);
208 
209  /// Preallocate \a count spaces in the input array on \a edge, returning
210  /// an iterator pointing at the first new space.
211  vector<Node*>::iterator PreallocateSpace(Edge* edge, int count);
212 
213  /// If we don't have a edge that generates this input already,
214  /// create one; this makes us not abort if the input is missing,
215  /// but instead will rebuild in that circumstance.
216  void CreatePhonyInEdge(Node* node);
217 
221 };
222 
223 
224 /// DependencyScan manages the process of scanning the files in a graph
225 /// and updating the dirty/outputs_ready state of all the nodes and edges.
227  DependencyScan(State* state, BuildLog* build_log, DepsLog* deps_log,
228  DiskInterface* disk_interface)
229  : build_log_(build_log),
230  disk_interface_(disk_interface),
231  dep_loader_(state, deps_log, disk_interface) {}
232 
233  /// Examine inputs, outputs, and command lines to judge whether an edge
234  /// needs to be re-run, and update outputs_ready_ and each outputs' |dirty_|
235  /// state accordingly.
236  /// Returns false on failure.
237  bool RecomputeDirty(Edge* edge, string* err);
238 
239  /// Recompute whether any output of the edge is dirty, if so sets |*dirty|.
240  /// Returns false on failure.
241  bool RecomputeOutputsDirty(Edge* edge, Node* most_recent_input,
242  bool* dirty, string* err);
243 
244  BuildLog* build_log() const {
245  return build_log_;
246  }
247  void set_build_log(BuildLog* log) {
248  build_log_ = log;
249  }
250 
251  DepsLog* deps_log() const {
252  return dep_loader_.deps_log();
253  }
254 
255  private:
256  /// Recompute whether a given single output should be marked dirty.
257  /// Returns true if so.
258  bool RecomputeOutputDirty(Edge* edge, Node* most_recent_input,
259  const string& command, Node* output);
260 
264 };
265 
266 #endif // NINJA_GRAPH_H_
int order_only_deps_
Definition: graph.h:170
TimeStamp mtime() const
Definition: graph.h:77
int implicit_deps_
Definition: graph.h:169
void set_dirty(bool dirty)
Definition: graph.h:80
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Definition: graph.h:35
Node(const string &path, unsigned int slash_bits)
Definition: graph.h:36
ImplicitDepLoader dep_loader_
Definition: graph.h:263
Interface for accessing the disk.
Edge * in_edge() const
Definition: graph.h:83
void AddOutEdge(Edge *edge)
Definition: graph.h:90
int TimeStamp
Definition: timestamp.h:22
bool outputs_ready() const
Definition: graph.h:159
Pool * pool() const
Definition: graph.h:157
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:124
Edge * in_edge_
The Edge that produces this Node, or NULL when there is no known edge to produce it.
Definition: graph.h:114
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:175
void MarkDirty()
Definition: graph.h:81
ImplicitDepLoader(State *state, DepsLog *deps_log, DiskInterface *disk_interface)
Definition: graph.h:187
vector< Edge * > out_edges_
All Edges that use this Node as an input.
Definition: graph.h:117
vector< Node * > inputs_
Definition: graph.h:150
As build commands run they can output extra dependency information (e.g.
Definition: deps_log.h:66
An Env which contains a mapping of variables to values as well as a pointer to a parent scope...
Definition: eval_env.h:77
bool outputs_ready_
Definition: graph.h:153
bool is_implicit(size_t index)
Definition: graph.h:171
BuildLog * build_log() const
Definition: graph.h:244
void set_id(int id)
Definition: graph.h:87
DiskInterface * disk_interface_
Definition: graph.h:262
DepsLog * deps_log_
Definition: graph.h:220
BindingEnv * env_
Definition: graph.h:152
An invokable build command and associated metadata (description, etc.).
Definition: eval_env.h:55
bool dirty() const
Definition: graph.h:79
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:261
DepsLog * deps_log() const
Definition: graph.h:196
Edge()
Definition: graph.h:125
A pool for delayed edges.
Definition: state.h:39
void set_build_log(BuildLog *log)
Definition: graph.h:247
TimeStamp mtime_
Possible values of mtime_: -1: file hasn't been examined 0: we looked, and file doesn't exist >0: actu...
Definition: graph.h:105
const string & path() const
Definition: graph.h:73
int id() const
Definition: graph.h:86
DependencyScan(State *state, BuildLog *build_log, DepsLog *deps_log, DiskInterface *disk_interface)
Definition: graph.h:227
bool dirty_
Dirty is true when the underlying file is out-of-date.
Definition: graph.h:110
int id_
A dense integer id for the node, assigned and used by DepsLog.
Definition: graph.h:120
Pool * pool_
Definition: graph.h:149
DependencyScan manages the process of scanning the files in a graph and updating the dirty/outputs_re...
Definition: graph.h:226
const Rule * rule_
Definition: graph.h:148
ImplicitDepLoader loads implicit dependencies, as referenced via the "depfile" attribute in build fil...
Definition: graph.h:186
unsigned int slash_bits_
Set bits starting from lowest for backslashes that were normalized to forward slashes by Canonicalize...
Definition: graph.h:99
State * state_
Definition: graph.h:218
Global state (file status) for a single run.
Definition: state.h:84
void set_in_edge(Edge *edge)
Definition: graph.h:84
DiskInterface * disk_interface_
Definition: graph.h:219
int weight() const
Definition: graph.h:158
string path_
Definition: graph.h:95
unsigned int slash_bits() const
Definition: graph.h:76
const Rule & rule() const
Definition: graph.h:156
const vector< Edge * > & out_edges() const
Definition: graph.h:89
bool deps_missing_
Definition: graph.h:154
DepsLog * deps_log() const
Definition: graph.h:251
bool StatIfNecessary(DiskInterface *disk_interface, string *err)
Return false on error.
Definition: graph.h:48
vector< Node * > outputs_
Definition: graph.h:151