Ninja
clean.cc
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 #include "clean.h"
16 
17 #include <assert.h>
18 #include <stdio.h>
19 
20 #include "disk_interface.h"
21 #include "graph.h"
22 #include "state.h"
23 #include "util.h"
24 
25 using namespace std;
26 
28  const BuildConfig& config,
29  DiskInterface* disk_interface)
30  : state_(state),
31  config_(config),
32  dyndep_loader_(state, disk_interface),
33  cleaned_files_count_(0),
34  disk_interface_(disk_interface),
35  status_(0) {
36 }
37 
38 int Cleaner::RemoveFile(const string& path) {
39  return disk_interface_->RemoveFile(path);
40 }
41 
42 bool Cleaner::FileExists(const string& path) {
43  string err;
44  TimeStamp mtime = disk_interface_->Stat(path, &err);
45  if (mtime == -1)
46  Error("%s", err.c_str());
47  return mtime > 0; // Treat Stat() errors as "file does not exist".
48 }
49 
50 void Cleaner::Report(const string& path) {
52  if (IsVerbose())
53  printf("Remove %s\n", path.c_str());
54 }
55 
56 void Cleaner::Remove(const string& path) {
57  if (!IsAlreadyRemoved(path)) {
58  removed_.insert(path);
59  if (config_.dry_run) {
60  if (FileExists(path))
61  Report(path);
62  } else {
63  int ret = RemoveFile(path);
64  if (ret == 0)
65  Report(path);
66  else if (ret == -1)
67  status_ = 1;
68  }
69  }
70 }
71 
72 bool Cleaner::IsAlreadyRemoved(const string& path) {
73  set<string>::iterator i = removed_.find(path);
74  return (i != removed_.end());
75 }
76 
78  string depfile = edge->GetUnescapedDepfile();
79  if (!depfile.empty())
80  Remove(depfile);
81 
82  string rspfile = edge->GetUnescapedRspfile();
83  if (!rspfile.empty())
84  Remove(rspfile);
85 }
86 
89  return;
90  printf("Cleaning...");
91  if (IsVerbose())
92  printf("\n");
93  else
94  printf(" ");
95  fflush(stdout);
96 }
97 
100  return;
101  printf("%d files.\n", cleaned_files_count_);
102 }
103 
104 int Cleaner::CleanAll(bool generator) {
105  Reset();
106  PrintHeader();
107  LoadDyndeps();
108  for (vector<Edge*>::iterator e = state_->edges_.begin();
109  e != state_->edges_.end(); ++e) {
110  // Do not try to remove phony targets
111  if ((*e)->is_phony())
112  continue;
113  // Do not remove generator's files unless generator specified.
114  if (!generator && (*e)->GetBindingBool("generator"))
115  continue;
116  for (vector<Node*>::iterator out_node = (*e)->outputs_.begin();
117  out_node != (*e)->outputs_.end(); ++out_node) {
118  Remove((*out_node)->path());
119  }
120 
121  RemoveEdgeFiles(*e);
122  }
123  PrintFooter();
124  return status_;
125 }
126 
128  Reset();
129  PrintHeader();
130  for (BuildLog::Entries::const_iterator i = entries.begin(); i != entries.end(); ++i) {
131  Node* n = state_->LookupNode(i->first);
132  // Detecting stale outputs works as follows:
133  //
134  // - If it has no Node, it is not in the build graph, or the deps log
135  // anymore, hence is stale.
136  //
137  // - If it isn't an output or input for any edge, it comes from a stale
138  // entry in the deps log, but no longer referenced from the build
139  // graph.
140  //
141  if (!n || (!n->in_edge() && n->out_edges().empty())) {
142  Remove(i->first.AsString());
143  }
144  }
145  PrintFooter();
146  return status_;
147 }
148 
150  if (Edge* e = target->in_edge()) {
151  // Do not try to remove phony targets
152  if (!e->is_phony()) {
153  Remove(target->path());
154  RemoveEdgeFiles(e);
155  }
156  for (vector<Node*>::iterator n = e->inputs_.begin(); n != e->inputs_.end();
157  ++n) {
158  Node* next = *n;
159  // call DoCleanTarget recursively if this node has not been visited
160  if (cleaned_.count(next) == 0) {
161  DoCleanTarget(next);
162  }
163  }
164  }
165 
166  // mark this target to be cleaned already
167  cleaned_.insert(target);
168 }
169 
171  assert(target);
172 
173  Reset();
174  PrintHeader();
175  LoadDyndeps();
176  DoCleanTarget(target);
177  PrintFooter();
178  return status_;
179 }
180 
181 int Cleaner::CleanTarget(const char* target) {
182  assert(target);
183 
184  Reset();
185  Node* node = state_->LookupNode(target);
186  if (node) {
187  CleanTarget(node);
188  } else {
189  Error("unknown target '%s'", target);
190  status_ = 1;
191  }
192  return status_;
193 }
194 
195 int Cleaner::CleanTargets(int target_count, char* targets[]) {
196  Reset();
197  PrintHeader();
198  LoadDyndeps();
199  for (int i = 0; i < target_count; ++i) {
200  string target_name = targets[i];
201  if (target_name.empty()) {
202  Error("failed to canonicalize '': empty path");
203  status_ = 1;
204  continue;
205  }
206  uint64_t slash_bits;
207  CanonicalizePath(&target_name, &slash_bits);
208  Node* target = state_->LookupNode(target_name);
209  if (target) {
210  if (IsVerbose())
211  printf("Target %s\n", target_name.c_str());
212  DoCleanTarget(target);
213  } else {
214  Error("unknown target '%s'", target_name.c_str());
215  status_ = 1;
216  }
217  }
218  PrintFooter();
219  return status_;
220 }
221 
222 void Cleaner::DoCleanRule(const Rule* rule) {
223  assert(rule);
224 
225  for (vector<Edge*>::iterator e = state_->edges_.begin();
226  e != state_->edges_.end(); ++e) {
227  if ((*e)->rule().name() == rule->name()) {
228  for (vector<Node*>::iterator out_node = (*e)->outputs_.begin();
229  out_node != (*e)->outputs_.end(); ++out_node) {
230  Remove((*out_node)->path());
231  RemoveEdgeFiles(*e);
232  }
233  }
234  }
235 }
236 
237 int Cleaner::CleanRule(const Rule* rule) {
238  assert(rule);
239 
240  Reset();
241  PrintHeader();
242  LoadDyndeps();
243  DoCleanRule(rule);
244  PrintFooter();
245  return status_;
246 }
247 
248 int Cleaner::CleanRule(const char* rule) {
249  assert(rule);
250 
251  Reset();
252  const Rule* r = state_->bindings_.LookupRule(rule);
253  if (r) {
254  CleanRule(r);
255  } else {
256  Error("unknown rule '%s'", rule);
257  status_ = 1;
258  }
259  return status_;
260 }
261 
262 int Cleaner::CleanRules(int rule_count, char* rules[]) {
263  assert(rules);
264 
265  Reset();
266  PrintHeader();
267  LoadDyndeps();
268  for (int i = 0; i < rule_count; ++i) {
269  const char* rule_name = rules[i];
270  const Rule* rule = state_->bindings_.LookupRule(rule_name);
271  if (rule) {
272  if (IsVerbose())
273  printf("Rule %s\n", rule_name);
274  DoCleanRule(rule);
275  } else {
276  Error("unknown rule '%s'", rule_name);
277  status_ = 1;
278  }
279  }
280  PrintFooter();
281  return status_;
282 }
283 
285  status_ = 0;
287  removed_.clear();
288  cleaned_.clear();
289 }
290 
292  // Load dyndep files that exist, before they are cleaned.
293  for (vector<Edge*>::iterator e = state_->edges_.begin();
294  e != state_->edges_.end(); ++e) {
295  if (Node* dyndep = (*e)->dyndep_) {
296  // Capture and ignore errors loading the dyndep file.
297  // We clean as much of the graph as we know.
298  std::string err;
299  dyndep_loader_.LoadDyndeps(dyndep, &err);
300  }
301  }
302 }
int status_
Definition: clean.h:108
const BuildConfig & config_
Definition: clean.h:102
ExternalStringHashMap< LogEntry * >::Type Entries
Definition: build_log.h:93
Verbosity verbosity
Definition: build.h:167
int CleanTargets(int target_count, char *targets[])
Clean the given target targets.
Definition: clean.cc:195
Edge * in_edge() const
Definition: graph.h:104
std::set< std::string > removed_
Definition: clean.h:104
const std::string & name() const
Definition: eval_env.h:62
virtual int RemoveFile(const std::string &path)=0
Remove the file named path.
Information about a node in the dependency graph: the file, whether it&#39;s dirty, mtime, etc.
Definition: graph.h:39
DyndepLoader dyndep_loader_
Definition: clean.h:103
void Reset()
Definition: clean.cc:284
Interface for accessing the disk.
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:164
Node * LookupNode(StringPiece path) const
Definition: state.cc:105
std::set< Node * > cleaned_
Definition: clean.h:105
DiskInterface * disk_interface_
Definition: clean.h:107
const std::string & path() const
Definition: graph.h:86
bool IsVerbose() const
Definition: clean.h:71
Cleaner(State *state, const BuildConfig &config, DiskInterface *disk_interface)
Build a cleaner object with the given disk_interface.
Definition: clean.cc:27
int RemoveFile(const std::string &path)
Remove the file path.
Definition: clean.cc:38
int CleanAll(bool generator=false)
Clean all built files, except for files created by generator rules.
Definition: clean.cc:104
void DoCleanTarget(Node *target)
Helper recursive method for CleanTarget().
Definition: clean.cc:149
int CleanRule(const Rule *rule)
Clean all the file built with the given rule rule.
Definition: clean.cc:237
An invocable build command and associated metadata (description, etc.).
Definition: eval_env.h:59
int CleanRules(int rule_count, char *rules[])
Clean the file produced by the given rules.
Definition: clean.cc:262
void LoadDyndeps()
Load dependencies from dyndep bindings.
Definition: clean.cc:291
bool IsAlreadyRemoved(const std::string &path)
Definition: clean.cc:72
std::string GetUnescapedRspfile() const
Like GetBinding("rspfile"), but without shell escaping.
Definition: graph.cc:510
int64_t TimeStamp
Definition: timestamp.h:31
int CleanDead(const BuildLog::Entries &entries)
Clean the files produced by previous builds that are no longer in the manifest.
Definition: clean.cc:127
BindingEnv bindings_
Definition: state.h:135
const Rule * LookupRule(const std::string &rule_name)
Definition: eval_env.cc:46
void Remove(const std::string &path)
Remove the given path file only if it has not been already removed.
Definition: clean.cc:56
void PrintHeader()
Definition: clean.cc:87
std::string GetUnescapedDepfile() const
Like GetBinding("depfile"), but without shell escaping.
Definition: graph.cc:500
State * state_
Definition: clean.h:101
void DoCleanRule(const Rule *rule)
Definition: clean.cc:222
int cleaned_files_count_
Definition: clean.h:106
void RemoveEdgeFiles(Edge *edge)
Remove the depfile and rspfile for an Edge.
Definition: clean.cc:77
const std::vector< Edge * > & out_edges() const
Definition: graph.h:110
void CanonicalizePath(string *path, uint64_t *slash_bits)
Definition: util.cc:122
Options (e.g. verbosity, parallelism) passed to a build.
Definition: build.h:157
Global state (file status) for a single run.
Definition: state.h:92
void Error(const char *msg, va_list ap)
Definition: util.cc:96
unsigned long long uint64_t
Definition: win32port.h:29
std::vector< Edge * > edges_
All the edges of the graph.
Definition: state.h:133
void PrintFooter()
Definition: clean.cc:98
bool FileExists(const std::string &path)
Definition: clean.cc:42
void Report(const std::string &path)
Definition: clean.cc:50
virtual TimeStamp Stat(const std::string &path, std::string *err) const =0
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
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...
Definition: dyndep.cc:29
int CleanTarget(Node *target)
Clean the given target and all the file built for it.
Definition: clean.cc:170
bool dry_run
Definition: build.h:168