Imported Upstream version 1.7.1
[platform/upstream/ninja.git] / src / clean.h
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
23 using namespace std;
24
25 struct State;
26 struct Node;
27 struct Rule;
28 struct DiskInterface;
29
30 struct Cleaner {
31   /// Build a cleaner object with a real disk interface.
32   Cleaner(State* state, const BuildConfig& config);
33
34   /// Build a cleaner object with the given @a disk_interface
35   /// (Useful for testing).
36   Cleaner(State* state,
37           const BuildConfig& config,
38           DiskInterface* disk_interface);
39
40   /// Clean the given @a target and all the file built for it.
41   /// @return non-zero if an error occurs.
42   int CleanTarget(Node* target);
43   /// Clean the given target @a target.
44   /// @return non-zero if an error occurs.
45   int CleanTarget(const char* target);
46   /// Clean the given target @a targets.
47   /// @return non-zero if an error occurs.
48   int CleanTargets(int target_count, char* targets[]);
49
50   /// Clean all built files, except for files created by generator rules.
51   /// @param generator If set, also clean files created by generator rules.
52   /// @return non-zero if an error occurs.
53   int CleanAll(bool generator = false);
54
55   /// Clean all the file built with the given rule @a rule.
56   /// @return non-zero if an error occurs.
57   int CleanRule(const Rule* rule);
58   /// Clean the file produced by the given @a rule.
59   /// @return non-zero if an error occurs.
60   int CleanRule(const char* rule);
61   /// Clean the file produced by the given @a rules.
62   /// @return non-zero if an error occurs.
63   int CleanRules(int rule_count, char* rules[]);
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 {
72     return (config_.verbosity != BuildConfig::QUIET
73             && (config_.verbosity == BuildConfig::VERBOSE || config_.dry_run));
74   }
75
76  private:
77   /// Remove the file @a path.
78   /// @return whether the file has been removed.
79   int RemoveFile(const string& path);
80   /// @returns whether the file @a path exists.
81   bool FileExists(const string& path);
82   void Report(const string& path);
83
84   /// Remove the given @a path file only if it has not been already removed.
85   void Remove(const string& path);
86   /// @return whether the given @a path has already been removed.
87   bool IsAlreadyRemoved(const 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   State* state_;
99   const BuildConfig& config_;
100   set<string> removed_;
101   set<Node*> cleaned_;
102   int cleaned_files_count_;
103   DiskInterface* disk_interface_;
104   int status_;
105 };
106
107 #endif  // NINJA_CLEAN_H_