diff options
author | Evan Martin <martine@danga.com> | 2011-04-29 17:55:26 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2011-04-29 17:55:26 (GMT) |
commit | c3c9b66a4261c7b329e07aef6f79ec1364900c2c (patch) | |
tree | f05a7e457a615ac149ba76cb4c994eca4265fe08 | |
parent | fcd3e881dfa8068c7db1dcf5d7660c319593baa8 (diff) | |
parent | a9c54d0413fd0a320d4df789183332ccad39b1db (diff) | |
download | Ninja-c3c9b66a4261c7b329e07aef6f79ec1364900c2c.zip Ninja-c3c9b66a4261c7b329e07aef6f79ec1364900c2c.tar.gz Ninja-c3c9b66a4261c7b329e07aef6f79ec1364900c2c.tar.bz2 |
Merged pull request #30 from polrop/more-tools.
adds the -C option and the following tools: targets, rules and clean
-rw-r--r-- | build.ninja | 3 | ||||
-rw-r--r-- | manual.asciidoc | 27 | ||||
-rw-r--r-- | src/clean.cc | 190 | ||||
-rw-r--r-- | src/clean.h | 68 | ||||
-rw-r--r-- | src/ninja.cc | 170 | ||||
-rw-r--r-- | src/ninja.h | 3 | ||||
-rw-r--r-- | src/ninja_jumble.cc | 18 |
7 files changed, 476 insertions, 3 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 80dfc50..7ea3da8 100644 --- a/manual.asciidoc +++ b/manual.asciidoc @@ -313,6 +313,33 @@ graph layout tool. Use it like: +ninja -t graph _target_ | dot -Tpng -ograph.png /dev/stdin+ . In the Ninja source tree, `ninja graph` generates an image for Ninja itself. +`targets`:: output a list of targets either by rule or by depth. If used +like this +ninja -t targets rule _name_+ it prints the list of targets +using the given rule to be built. If no rule is given, it prints the source +files (the leaf of the graph). If used like this ++ninja -t targets depth _digit_+ it +prints the list of targets in a depth-first manner starting by the root +targets (the ones with no outputs). Indentation is used to mark dependencies. +If the depth is zero it prints all targets. If no arguments are provided ++ninja -t targets depth 1+ is assumed. In this mode targets may be listed +several times. If used like this +ninja -t targets all+ it +prints all the targets available without indentation and it is way faster +than the _depth_ mode. It returns non-zero if an error occurs. + +`rules`:: output the list of all rules with their description if they have +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 6359aaf..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' }, @@ -53,11 +54,15 @@ void usage(const BuildConfig& config) { " -j N run N jobs in parallel [default=%d]\n" " -n dry run (don't run commands but pretend they succeeded)\n" " -v show all command lines\n" +" -C DIR change to DIR before doing anything else\n" "\n" " -t TOOL run a subtool. tools are:\n" " browse browse dependency graph in a web browser\n" " graph output graphviz dot file for targets\n" -" query show inputs/outputs for a path\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" +" clean clean built files\n", config.parallelism); } @@ -160,15 +165,161 @@ int CmdBrowse(State* state, int argc, char* argv[]) { return 1; } +int CmdTargetsList(const vector<Node*>& nodes, int depth, int indent) { + for (vector<Node*>::const_iterator n = nodes.begin(); + n != nodes.end(); + ++n) { + for (int i = 0; i < indent; ++i) + printf(" "); + const char* target = (*n)->file_->path_.c_str(); + if ((*n)->in_edge_) { + printf("%s: %s\n", target, (*n)->in_edge_->rule_->name_.c_str()); + if (depth > 1 || depth <= 0) + CmdTargetsList((*n)->in_edge_->inputs_, depth - 1, indent + 1); + } else { + printf("%s\n", target); + } + } + return 0; +} + +int CmdTargetsList(const vector<Node*>& nodes, int depth) { + return CmdTargetsList(nodes, depth, 0); +} + +int CmdTargetsSourceList(State* state) { + for (vector<Edge*>::iterator e = state->edges_.begin(); + e != state->edges_.end(); + ++e) + for (vector<Node*>::iterator inps = (*e)->inputs_.begin(); + inps != (*e)->inputs_.end(); + ++inps) + if (!(*inps)->in_edge_) + printf("%s\n", (*inps)->file_->path_.c_str()); + return 0; +} + +int CmdTargetsList(State* state, const string& rule_name) { + set<string> rules; + // Gather the outputs. + 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) + rules.insert((*out_node)->file_->path_); + // Print them. + for (set<string>::const_iterator i = rules.begin(); + i != rules.end(); + ++i) + printf("%s\n", (*i).c_str()); + return 0; +} + +int CmdTargetsList(State* state) { + 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) + printf("%s: %s\n", + (*out_node)->file_->path_.c_str(), + (*e)->rule_->name_.c_str()); + return 0; +} + +int CmdTargets(State* state, int argc, char* argv[]) { + int depth = 1; + if (argc >= 1) { + string mode = argv[0]; + if (mode == "rule") { + string rule; + if (argc > 1) + rule = argv[1]; + if (rule.empty()) + return CmdTargetsSourceList(state); + else + return CmdTargetsList(state, rule); + } else if (mode == "depth") { + if (argc > 1) + depth = atoi(argv[1]); + } else if (mode == "all") { + return CmdTargetsList(state); + } else { + Error("unknown target tool mode '%s'", mode.c_str()); + return 1; + } + } + + string err; + vector<Node*> root_nodes = state->RootNodes(&err); + if (err.empty()) { + return CmdTargetsList(root_nodes, depth); + } else { + Error("%s", err.c_str()); + return 1; + } +} + +int CmdRules(State* state, int argc, char* argv[]) { + for (map<string, const Rule*>::iterator i = state->rules_.begin(); + i != state->rules_.end(); + ++i) { + if (i->second->description_.unparsed_.empty()) + printf("%s\n", i->first.c_str()); + else + printf("%s: %s\n", + i->first.c_str(), + i->second->description_.unparsed_.c_str()); + } + 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"; + const char* working_dir = 0; string tool; config.parallelism = GuessParallelism(); int opt; - while ((opt = getopt_long(argc, argv, "f:hj:nt:v", options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "f:hj:nt:vC:", options, NULL)) != -1) { switch (opt) { case 'f': input_file = optarg; @@ -185,6 +336,9 @@ int main(int argc, char** argv) { case 't': tool = optarg; break; + case 'C': + working_dir = optarg; + break; case 'h': default: usage(config); @@ -199,6 +353,12 @@ int main(int argc, char** argv) { argv += optind; argc -= optind; + if (working_dir) { + if (chdir(working_dir) < 0) { + Fatal("chdir to '%s' - %s", working_dir, strerror(errno)); + } + } + State state; RealFileReader file_reader; ManifestParser parser(&state, &file_reader); @@ -215,6 +375,12 @@ int main(int argc, char** argv) { return CmdQuery(&state, argc, argv); if (tool == "browse") return CmdBrowse(&state, argc, argv); + if (tool == "targets") + 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()); } diff --git a/src/ninja.h b/src/ninja.h index 2590e63..f387ffc 100644 --- a/src/ninja.h +++ b/src/ninja.h @@ -73,6 +73,9 @@ struct State { Node* LookupNode(const string& path); void AddIn(Edge* edge, const string& path); void AddOut(Edge* edge, const string& path); + /// @return the root node(s) of the graph. (Root nodes have no input edges). + /// @param error where to write the error message if somethings went wrong. + vector<Node*> RootNodes(string* error); StatCache stat_cache_; /// All the rules used in the graph. diff --git a/src/ninja_jumble.cc b/src/ninja_jumble.cc index fd2ee18..e6a0de6 100644 --- a/src/ninja_jumble.cc +++ b/src/ninja_jumble.cc @@ -182,3 +182,21 @@ void State::AddOut(Edge* edge, const string& path) { } node->in_edge_ = edge; } + +vector<Node*> State::RootNodes(string* error) +{ + assert(error); + vector<Node*> root_nodes; + // Search for nodes with no output. + for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e) + for (vector<Node*>::iterator outs = (*e)->outputs_.begin(); + outs != (*e)->outputs_.end(); + ++outs) + if ((*outs)->out_edges_.size() == 0) + root_nodes.push_back(*outs); + if (!edges_.empty() && root_nodes.empty()) { + *error = "could not determine root nodes of build graph"; + } + assert(edges_.empty() || !root_nodes.empty()); + return root_nodes; +} |