• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using namespace std;
26 
27 struct State;
28 struct Node;
29 struct Rule;
30 struct DiskInterface;
31 
32 struct Cleaner {
33   /// Build a cleaner object with the given @a disk_interface
34   Cleaner(State* state,
35           const BuildConfig& config,
36           DiskInterface* disk_interface);
37 
38   /// Clean the given @a target and all the file built for it.
39   /// @return non-zero if an error occurs.
40   int CleanTarget(Node* target);
41   /// Clean the given target @a target.
42   /// @return non-zero if an error occurs.
43   int CleanTarget(const char* target);
44   /// Clean the given target @a targets.
45   /// @return non-zero if an error occurs.
46   int CleanTargets(int target_count, char* targets[]);
47 
48   /// Clean all built files, except for files created by generator rules.
49   /// @param generator If set, also clean files created by generator rules.
50   /// @return non-zero if an error occurs.
51   int CleanAll(bool generator = false);
52 
53   /// Clean all the file built with the given rule @a rule.
54   /// @return non-zero if an error occurs.
55   int CleanRule(const Rule* rule);
56   /// Clean the file produced by the given @a rule.
57   /// @return non-zero if an error occurs.
58   int CleanRule(const char* rule);
59   /// Clean the file produced by the given @a rules.
60   /// @return non-zero if an error occurs.
61   int CleanRules(int rule_count, char* rules[]);
62   /// Clean the files produced by previous builds that are no longer in the
63   /// manifest.
64   /// @return non-zero if an error occurs.
65   int CleanDead(const BuildLog::Entries& entries);
66 
67   /// @return the number of file cleaned.
cleaned_files_countCleaner68   int cleaned_files_count() const {
69     return cleaned_files_count_;
70   }
71 
72   /// @return whether the cleaner is in verbose mode.
IsVerboseCleaner73   bool IsVerbose() const {
74     return (config_.verbosity != BuildConfig::QUIET
75             && (config_.verbosity == BuildConfig::VERBOSE || config_.dry_run));
76   }
77 
78  private:
79   /// Remove the file @a path.
80   /// @return whether the file has been removed.
81   int RemoveFile(const string& path);
82   /// @returns whether the file @a path exists.
83   bool FileExists(const string& path);
84   void Report(const string& path);
85 
86   /// Remove the given @a path file only if it has not been already removed.
87   void Remove(const string& path);
88   /// @return whether the given @a path has already been removed.
89   bool IsAlreadyRemoved(const string& path);
90   /// Remove the depfile and rspfile for an Edge.
91   void RemoveEdgeFiles(Edge* edge);
92 
93   /// Helper recursive method for CleanTarget().
94   void DoCleanTarget(Node* target);
95   void PrintHeader();
96   void PrintFooter();
97   void DoCleanRule(const Rule* rule);
98   void Reset();
99 
100   /// Load dependencies from dyndep bindings.
101   void LoadDyndeps();
102 
103   State* state_;
104   const BuildConfig& config_;
105   DyndepLoader dyndep_loader_;
106   set<string> removed_;
107   set<Node*> cleaned_;
108   int cleaned_files_count_;
109   DiskInterface* disk_interface_;
110   int status_;
111 };
112 
113 #endif  // NINJA_CLEAN_H_
114