Ninja
clean.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_CLEAN_H_
16 #define NINJA_CLEAN_H_
17 
18 #include <set>
19 #include <string>
20 
21 #include "build.h"
22 #include "dyndep.h"
23 #include "build_log.h"
24 
25 struct State;
26 struct Node;
27 struct Rule;
28 struct DiskInterface;
29 
30 struct Cleaner {
31  /// Build a cleaner object with the given @a disk_interface
32  Cleaner(State* state,
33  const BuildConfig& config,
34  DiskInterface* disk_interface);
35 
36  /// Clean the given @a target and all the file built for it.
37  /// @return non-zero if an error occurs.
38  int CleanTarget(Node* target);
39  /// Clean the given target @a target.
40  /// @return non-zero if an error occurs.
41  int CleanTarget(const char* target);
42  /// Clean the given target @a targets.
43  /// @return non-zero if an error occurs.
44  int CleanTargets(int target_count, char* targets[]);
45 
46  /// Clean all built files, except for files created by generator rules.
47  /// @param generator If set, also clean files created by generator rules.
48  /// @return non-zero if an error occurs.
49  int CleanAll(bool generator = false);
50 
51  /// Clean all the file built with the given rule @a rule.
52  /// @return non-zero if an error occurs.
53  int CleanRule(const Rule* rule);
54  /// Clean the file produced by the given @a rule.
55  /// @return non-zero if an error occurs.
56  int CleanRule(const char* rule);
57  /// Clean the file produced by the given @a rules.
58  /// @return non-zero if an error occurs.
59  int CleanRules(int rule_count, char* rules[]);
60  /// Clean the files produced by previous builds that are no longer in the
61  /// manifest.
62  /// @return non-zero if an error occurs.
63  int CleanDead(const BuildLog::Entries& entries);
64 
65  /// @return the number of file cleaned.
66  int cleaned_files_count() const {
67  return cleaned_files_count_;
68  }
69 
70  /// @return whether the cleaner is in verbose mode.
71  bool IsVerbose() const {
74  }
75 
76  private:
77  /// Remove the file @a path.
78  /// @return whether the file has been removed.
79  int RemoveFile(const std::string& path);
80  /// @returns whether the file @a path exists.
81  bool FileExists(const std::string& path);
82  void Report(const std::string& path);
83 
84  /// Remove the given @a path file only if it has not been already removed.
85  void Remove(const std::string& path);
86  /// @return whether the given @a path has already been removed.
87  bool IsAlreadyRemoved(const std::string& path);
88  /// Remove the depfile and rspfile for an Edge.
89  void RemoveEdgeFiles(Edge* edge);
90 
91  /// Helper recursive method for CleanTarget().
92  void DoCleanTarget(Node* target);
93  void PrintHeader();
94  void PrintFooter();
95  void DoCleanRule(const Rule* rule);
96  void Reset();
97 
98  /// Load dependencies from dyndep bindings.
99  void LoadDyndeps();
100 
104  std::set<std::string> removed_;
105  std::set<Node*> cleaned_;
108  int status_;
109 };
110 
111 #endif // NINJA_CLEAN_H_
Options (e.g. verbosity, parallelism) passed to a build.
Definition: build.h:176
@ VERBOSE
Definition: build.h:183
bool dry_run
Definition: build.h:186
Verbosity verbosity
Definition: build.h:185
ExternalStringHashMap< std::unique_ptr< LogEntry > >::Type Entries
Definition: build_log.h:95
Definition: clean.h:30
const BuildConfig & config_
Definition: clean.h:102
int CleanRule(const Rule *rule)
Clean all the file built with the given rule rule.
Definition: clean.cc:238
int CleanTarget(Node *target)
Clean the given target and all the file built for it.
Definition: clean.cc:171
void PrintFooter()
Definition: clean.cc:98
void RemoveEdgeFiles(Edge *edge)
Remove the depfile and rspfile for an Edge.
Definition: clean.cc:77
int cleaned_files_count_
Definition: clean.h:106
int cleaned_files_count() const
Definition: clean.h:66
void DoCleanTarget(Node *target)
Helper recursive method for CleanTarget().
Definition: clean.cc:150
bool IsVerbose() const
Definition: clean.h:71
bool IsAlreadyRemoved(const std::string &path)
Definition: clean.cc:72
void Remove(const std::string &path)
Remove the given path file only if it has not been already removed.
Definition: clean.cc:56
int CleanRules(int rule_count, char *rules[])
Clean the file produced by the given rules.
Definition: clean.cc:263
void Report(const std::string &path)
Definition: clean.cc:50
void Reset()
Definition: clean.cc:285
DyndepLoader dyndep_loader_
Definition: clean.h:103
void LoadDyndeps()
Load dependencies from dyndep bindings.
Definition: clean.cc:292
bool FileExists(const std::string &path)
Definition: clean.cc:42
Cleaner(State *state, const BuildConfig &config, DiskInterface *disk_interface)
Build a cleaner object with the given disk_interface.
Definition: clean.cc:27
void DoCleanRule(const Rule *rule)
Definition: clean.cc:223
std::set< std::string > removed_
Definition: clean.h:104
std::set< Node * > cleaned_
Definition: clean.h:105
int CleanTargets(int target_count, char *targets[])
Clean the given target targets.
Definition: clean.cc:196
int CleanDead(const BuildLog::Entries &entries)
Clean the files produced by previous builds that are no longer in the manifest.
Definition: clean.cc:127
int status_
Definition: clean.h:108
int CleanAll(bool generator=false)
Clean all built files, except for files created by generator rules.
Definition: clean.cc:104
int RemoveFile(const std::string &path)
Remove the file path.
Definition: clean.cc:38
State * state_
Definition: clean.h:101
DiskInterface * disk_interface_
Definition: clean.h:107
void PrintHeader()
Definition: clean.cc:87
Interface for accessing the disk.
DyndepLoader loads dynamically discovered dependencies, as referenced via the "dyndep" attribute in b...
Definition: dyndep.h:46
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:175
Information about a node in the dependency graph: the file, whether it's dirty, mtime,...
Definition: graph.h:42
An invocable build command and associated metadata (description, etc.).
Definition: eval_env.h:66
Global state (file status) for a single run.
Definition: state.h:95