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
25struct State;
26struct Node;
27struct Rule;
28struct DiskInterface;
29
30struct 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 {
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 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
101 State* state_;
102 const BuildConfig& config_;
103 DyndepLoader dyndep_loader_;
104 std::set<std::string> removed_;
105 std::set<Node*> cleaned_;
106 int cleaned_files_count_;
107 DiskInterface* disk_interface_;
108 int status_;
109};
110
111#endif // NINJA_CLEAN_H_
112