summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Despres <nicolas.despres@gmail.com>2011-04-14 13:28:43 (GMT)
committerNicolas Despres <nicolas.despres@gmail.com>2011-04-26 11:23:07 (GMT)
commita9c54d0413fd0a320d4df789183332ccad39b1db (patch)
treecf6632a0cd264cbd6313434dedd0c19c647c0fcf
parent7b67e48a1479c6fc7326c55756088bd57d64fc82 (diff)
downloadNinja-a9c54d0413fd0a320d4df789183332ccad39b1db.zip
Ninja-a9c54d0413fd0a320d4df789183332ccad39b1db.tar.gz
Ninja-a9c54d0413fd0a320d4df789183332ccad39b1db.tar.bz2
Add the 'clean' tool.
It removes built files either by rule or target or everything.
-rw-r--r--build.ninja3
-rw-r--r--manual.asciidoc11
-rw-r--r--src/clean.cc190
-rw-r--r--src/clean.h68
-rw-r--r--src/ninja.cc38
5 files changed, 308 insertions, 2 deletions
diff --git a/build.ninja b/build.ninja
index e780603..be21296 100644
--- a/build.ninja
+++ b/build.ninja
@@ -47,11 +47,12 @@ build $builddir/graphviz.o: cxx src/graphviz.cc
build $builddir/parsers.o: cxx src/parsers.cc
build $builddir/subprocess.o: cxx src/subprocess.cc
build $builddir/util.o: cxx src/util.cc
+build $builddir/clean.o: cxx src/clean.cc
build $builddir/ninja_jumble.o: cxx src/ninja_jumble.cc
build $builddir/ninja.a: ar $builddir/browse.o $builddir/build.o \
$builddir/build_log.o $builddir/eval_env.o $builddir/graph.o \
$builddir/graphviz.o $builddir/parsers.o $builddir/subprocess.o \
- $builddir/util.o $builddir/ninja_jumble.o
+ $builddir/util.o $builddir/ninja_jumble.o $builddir/clean.o
build $builddir/ninja.o: cxx src/ninja.cc
build ninja: link $builddir/ninja.o $builddir/ninja.a
diff --git a/manual.asciidoc b/manual.asciidoc
index 53d6416..7ea3da8 100644
--- a/manual.asciidoc
+++ b/manual.asciidoc
@@ -330,6 +330,17 @@ than the _depth_ mode. It returns non-zero if an error occurs.
one. It can be used to know which rule name to pass to
+ninja -t targets rule _name_+.
+`clean`:: remove built files. If used like this +ninja -t clean+ it
+removes all the built files. If used like this
++ninja -t clean _targets..._+ or like this
++ninja -t clean target _targets..._+ it removes the given targets and
+recursively all files built for it. If used like this
++ninja -t clean rule _rules_+ it removes all files built using the given
+rules. The depfiles are not removed. Files created but not referenced in
+the graph are not removed. This tool takes in account the +-v+ and the
++-n+ options (note that +-n+ implies +-v+). It returns non-zero if an
+error occurs.
+
Ninja file reference
--------------------
diff --git a/src/clean.cc b/src/clean.cc
new file mode 100644
index 0000000..2cdb27d
--- /dev/null
+++ b/src/clean.cc
@@ -0,0 +1,190 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "clean.h"
+
+#include "graph.h"
+#include "ninja.h"
+#include "util.h"
+#include "build.h"
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <assert.h>
+
+Cleaner::Cleaner(State* state, const BuildConfig& config)
+ : state_(state)
+ , verbose_(config.verbosity == BuildConfig::VERBOSE || config.dry_run)
+ , dry_run_(config.dry_run)
+ , removed_()
+{
+}
+
+bool Cleaner::RemoveFile(const string& path) {
+ if (remove(path.c_str()) < 0) {
+ switch (errno) {
+ case ENOENT:
+ return false;
+ default:
+ Error("remove(%s): %s", path.c_str(), strerror(errno));
+ return false;
+ }
+ } else {
+ return true;
+ }
+}
+
+bool Cleaner::FileExists(const string& path) {
+ struct stat st;
+ if (stat(path.c_str(), &st) < 0) {
+ switch (errno) {
+ case ENOENT:
+ return false;
+ default:
+ Error("stat(%s): %s", path.c_str(), strerror(errno));
+ return false;
+ }
+ } else {
+ return true;
+ }
+}
+
+void Cleaner::Report(const string& path) {
+ if (verbose_)
+ printf("Remove %s\n", path.c_str());
+}
+
+void Cleaner::Remove(const string& path) {
+ if (!IsAlreadyRemoved(path)) {
+ removed_.insert(path);
+ if (dry_run_) {
+ if (FileExists(path))
+ Report(path);
+ } else {
+ if (RemoveFile(path))
+ Report(path);
+ }
+ }
+}
+
+bool Cleaner::IsAlreadyRemoved(const string& path) {
+ set<string>::iterator i = removed_.find(path);
+ return (i != removed_.end());
+}
+
+void Cleaner::PrintHeader() {
+ printf("Cleaning...");
+ if (verbose_)
+ printf("\n");
+ else
+ printf(" ");
+}
+
+void Cleaner::PrintFooter() {
+ printf("%d files.\n", removed_.size());
+}
+
+void Cleaner::CleanAll() {
+ PrintHeader();
+ for (vector<Edge*>::iterator e = state_->edges_.begin();
+ e != state_->edges_.end();
+ ++e)
+ for (vector<Node*>::iterator out_node = (*e)->outputs_.begin();
+ out_node != (*e)->outputs_.end();
+ ++out_node)
+ Remove((*out_node)->file_->path_);
+ PrintFooter();
+}
+
+void Cleaner::DoCleanTarget(Node* target) {
+ if (target->in_edge_) {
+ Remove(target->file_->path_);
+ for (vector<Node*>::iterator n = target->in_edge_->inputs_.begin();
+ n != target->in_edge_->inputs_.end();
+ ++n) {
+ DoCleanTarget(*n);
+ }
+ }
+}
+
+void Cleaner::CleanTarget(Node* target) {
+ assert(target);
+
+ PrintHeader();
+ DoCleanTarget(target);
+ PrintFooter();
+}
+
+int Cleaner::CleanTargets(int target_count, char* targets[]) {
+ int status = 0;
+ PrintHeader();
+ for (int i = 0; i < target_count; ++i) {
+ const char* target_name = targets[i];
+ Node* target = state_->LookupNode(target_name);
+ if (target) {
+ if (verbose_)
+ printf("Target %s\n", target_name);
+ DoCleanTarget(target);
+ } else {
+ Error("unknown target '%s'", target_name);
+ status = 1;
+ }
+ }
+ PrintFooter();
+ return status;
+}
+
+void Cleaner::DoCleanRule(const Rule* rule) {
+ assert(rule);
+
+ for (vector<Edge*>::iterator e = state_->edges_.begin();
+ e != state_->edges_.end();
+ ++e)
+ if ((*e)->rule_->name_ == rule->name_)
+ for (vector<Node*>::iterator out_node = (*e)->outputs_.begin();
+ out_node != (*e)->outputs_.end();
+ ++out_node)
+ Remove((*out_node)->file_->path_);
+}
+
+void Cleaner::CleanRule(const Rule* rule) {
+ assert(rule);
+
+ PrintHeader();
+ DoCleanRule(rule);
+ PrintFooter();
+}
+
+int Cleaner::CleanRules(int rule_count, char* rules[]) {
+ assert(rules);
+
+ int status = 0;
+ PrintHeader();
+ for (int i = 0; i < rule_count; ++i) {
+ const char* rule_name = rules[i];
+ const Rule* rule = state_->LookupRule(rule_name);
+ if (rule) {
+ if (verbose_)
+ printf("Rule %s\n", rule_name);
+ DoCleanRule(rule);
+ } else {
+ Error("unknown rule '%s'", rule_name);
+ status = 1;
+ }
+ }
+ PrintFooter();
+ return status;
+}
diff --git a/src/clean.h b/src/clean.h
new file mode 100644
index 0000000..4513596
--- /dev/null
+++ b/src/clean.h
@@ -0,0 +1,68 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef NINJA_CLEAN_H_
+#define NINJA_CLEAN_H_
+
+#include <string>
+#include <set>
+using namespace std;
+
+struct State;
+struct BuildConfig;
+struct Node;
+struct Rule;
+
+class Cleaner
+{
+public:
+ /// Constructor.
+ Cleaner(State* state, const BuildConfig& config);
+
+ /// Clean the given @a target and all the file built for it.
+ void CleanTarget(Node* target);
+ int CleanTargets(int target_count, char* targets[]);
+
+ /// Clean all built files.
+ void CleanAll();
+
+ /// Clean all the file built with the given rule @a rule.
+ void CleanRule(const Rule* rule);
+ int CleanRules(int rule_count, char* rules[]);
+
+private:
+ /// Remove the file @a path.
+ /// @return whether the file has been removed.
+ bool RemoveFile(const string& path);
+ /// @returns whether the file @a path exists.
+ bool FileExists(const string& path);
+ void Report(const string& path);
+ /// Remove the given @a path file only if it has not been already removed.
+ void Remove(const string& path);
+ /// @return whether the given @a path has already been removed.
+ bool IsAlreadyRemoved(const string& path);
+ /// Helper recursive method for CleanTarget().
+ void DoCleanTarget(Node* target);
+ void PrintHeader();
+ void PrintFooter();
+ void DoCleanRule(const Rule* rule);
+
+private:
+ State* state_;
+ bool verbose_;
+ bool dry_run_;
+ set<string> removed_;
+};
+
+#endif // NINJA_CLEAN_H_
diff --git a/src/ninja.cc b/src/ninja.cc
index 3912139..5a0c00d 100644
--- a/src/ninja.cc
+++ b/src/ninja.cc
@@ -38,6 +38,7 @@
#include "graphviz.h"
#include "parsers.h"
#include "util.h"
+#include "clean.h"
option options[] = {
{ "help", no_argument, NULL, 'h' },
@@ -60,7 +61,8 @@ void usage(const BuildConfig& config) {
" graph output graphviz dot file for targets\n"
" query show inputs/outputs for a path\n"
" targets list targets by their rule or depth in the DAG\n"
-" rules list all rules\n",
+" rules list all rules\n"
+" clean clean built files\n",
config.parallelism);
}
@@ -276,6 +278,38 @@ int CmdRules(State* state, int argc, char* argv[]) {
return 0;
}
+int CmdClean(State* state,
+ int argc,
+ char* argv[],
+ const BuildConfig& config) {
+ Cleaner cleaner(state, config);
+ if (argc >= 1)
+ {
+ string mode = argv[0];
+ if (mode == "target") {
+ if (argc >= 2) {
+ return cleaner.CleanTargets(argc - 1, &argv[1]);
+ } else {
+ Error("expected a target to clean");
+ return 1;
+ }
+ } else if (mode == "rule") {
+ if (argc >= 2) {
+ return cleaner.CleanRules(argc - 1, &argv[1]);
+ } else {
+ Error("expected a rule to clean");
+ return 1;
+ }
+ } else {
+ return cleaner.CleanTargets(argc, argv);
+ }
+ }
+ else {
+ cleaner.CleanAll();
+ return 0;
+ }
+}
+
int main(int argc, char** argv) {
BuildConfig config;
const char* input_file = "build.ninja";
@@ -345,6 +379,8 @@ int main(int argc, char** argv) {
return CmdTargets(&state, argc, argv);
if (tool == "rules")
return CmdRules(&state, argc, argv);
+ if (tool == "clean")
+ return CmdClean(&state, argc, argv, config);
Error("unknown tool '%s'", tool.c_str());
}