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  if (!n || !n->in_edge()) {
133  Remove(i->first.AsString());
134  }
135  }
136  PrintFooter();
137  return status_;
138 }
139 
141  if (Edge* e = target->in_edge()) {
142  // Do not try to remove phony targets
143  if (!e->is_phony()) {
144  Remove(target->path());
145  RemoveEdgeFiles(e);
146  }
147  for (vector<Node*>::iterator n = e->inputs_.begin(); n != e->inputs_.end();
148  ++n) {
149  Node* next = *n;
150  // call DoCleanTarget recursively if this node has not been visited
151  if (cleaned_.count(next) == 0) {
152  DoCleanTarget(next);
153  }
154  }
155  }
156 
157  // mark this target to be cleaned already
158  cleaned_.insert(target);
159 }
160 
162  assert(target);
163 
164  Reset();
165  PrintHeader();
166  LoadDyndeps();
167  DoCleanTarget(target);
168  PrintFooter();
169  return status_;
170 }
171 
172 int Cleaner::CleanTarget(const char* target) {
173  assert(target);
174 
175  Reset();
176  Node* node = state_->LookupNode(target);
177  if (node) {
178  CleanTarget(node);
179  } else {
180  Error("unknown target '%s'", target);
181  status_ = 1;
182  }
183  return status_;
184 }
185 
186 int Cleaner::CleanTargets(int target_count, char* targets[]) {
187  Reset();
188  PrintHeader();
189  LoadDyndeps();
190  for (int i = 0; i < target_count; ++i) {
191  string target_name = targets[i];
192  uint64_t slash_bits;
193  string err;
194  if (!CanonicalizePath(&target_name, &slash_bits, &err)) {
195  Error("failed to canonicalize '%s': %s", target_name.c_str(), err.c_str());
196  status_ = 1;
197  } else {
198  Node* target = state_->LookupNode(target_name);
199  if (target) {
200  if (IsVerbose())
201  printf("Target %s\n", target_name.c_str());
202  DoCleanTarget(target);
203  } else {
204  Error("unknown target '%s'", target_name.c_str());
205  status_ = 1;
206  }
207  }
208  }
209  PrintFooter();
210  return status_;
211 }
212 
213 void Cleaner::DoCleanRule(const Rule* rule) {
214  assert(rule);
215 
216  for (vector<Edge*>::iterator e = state_->edges_.begin();
217  e != state_->edges_.end(); ++e) {
218  if ((*e)->rule().name() == rule->name()) {
219  for (vector<Node*>::iterator out_node = (*e)->outputs_.begin();
220  out_node != (*e)->outputs_.end(); ++out_node) {
221  Remove((*out_node)->path());
222  RemoveEdgeFiles(*e);
223  }
224  }
225  }
226 }
227 
228 int Cleaner::CleanRule(const Rule* rule) {
229  assert(rule);
230 
231  Reset();
232  PrintHeader();
233  LoadDyndeps();
234  DoCleanRule(rule);
235  PrintFooter();
236  return status_;
237 }
238 
239 int Cleaner::CleanRule(const char* rule) {
240  assert(rule);
241 
242  Reset();
243  const Rule* r = state_->bindings_.LookupRule(rule);
244  if (r) {
245  CleanRule(r);
246  } else {
247  Error("unknown rule '%s'", rule);
248  status_ = 1;
249  }
250  return status_;
251 }
252 
253 int Cleaner::CleanRules(int rule_count, char* rules[]) {
254  assert(rules);
255 
256  Reset();
257  PrintHeader();
258  LoadDyndeps();
259  for (int i = 0; i < rule_count; ++i) {
260  const char* rule_name = rules[i];
261  const Rule* rule = state_->bindings_.LookupRule(rule_name);
262  if (rule) {
263  if (IsVerbose())
264  printf("Rule %s\n", rule_name);
265  DoCleanRule(rule);
266  } else {
267  Error("unknown rule '%s'", rule_name);
268  status_ = 1;
269  }
270  }
271  PrintFooter();
272  return status_;
273 }
274 
276  status_ = 0;
278  removed_.clear();
279  cleaned_.clear();
280 }
281 
283  // Load dyndep files that exist, before they are cleaned.
284  for (vector<Edge*>::iterator e = state_->edges_.begin();
285  e != state_->edges_.end(); ++e) {
286  if (Node* dyndep = (*e)->dyndep_) {
287  // Capture and ignore errors loading the dyndep file.
288  // We clean as much of the graph as we know.
289  std::string err;
290  dyndep_loader_.LoadDyndeps(dyndep, &err);
291  }
292  }
293 }
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:169
int CleanTargets(int target_count, char *targets[])
Clean the given target targets.
Definition: clean.cc:186
Edge * in_edge() const
Definition: graph.h:94
bool CanonicalizePath(string *path, uint64_t *slash_bits, string *err)
Definition: util.cc:95
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:37
DyndepLoader dyndep_loader_
Definition: clean.h:103
void Reset()
Definition: clean.cc:275
Interface for accessing the disk.
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:139
Node * LookupNode(StringPiece path) const
Definition: state.cc:113
std::set< Node * > cleaned_
Definition: clean.h:105
DiskInterface * disk_interface_
Definition: clean.h:107
const std::string & path() const
Definition: graph.h:76
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:140
int CleanRule(const Rule *rule)
Clean all the file built with the given rule rule.
Definition: clean.cc:228
An invokable 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:253
void LoadDyndeps()
Load dependencies from dyndep bindings.
Definition: clean.cc:282
bool IsAlreadyRemoved(const std::string &path)
Definition: clean.cc:72
std::string GetUnescapedRspfile() const
Like GetBinding("rspfile"), but without shell escaping.
Definition: graph.cc:430
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:126
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:420
State * state_
Definition: clean.h:101
void DoCleanRule(const Rule *rule)
Definition: clean.cc:213
int cleaned_files_count_
Definition: clean.h:106
void RemoveEdgeFiles(Edge *edge)
Remove the depfile and rspfile for an Edge.
Definition: clean.cc:77
Options (e.g. verbosity, parallelism) passed to a build.
Definition: build.h:160
Global state (file status) for a single run.
Definition: state.h:84
unsigned long long uint64_t
Definition: win32port.h:29
std::vector< Edge * > edges_
All the edges of the graph.
Definition: state.h:124
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:161
bool dry_run
Definition: build.h:170
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:86