diff options
author | Nico Weber <nicolasweber@gmx.de> | 2016-11-06 20:20:24 (GMT) |
---|---|---|
committer | Nico Weber <nicolasweber@gmx.de> | 2016-11-06 20:20:24 (GMT) |
commit | 8b7155b0e582954909c0b56acfa2a38e04f1c5ee (patch) | |
tree | 9524237dd1edd60f7edb8c37a367fcb1e0f1a96d | |
parent | ddaf63fe1b41dfd58c646b05fd16dbe374a81ef3 (diff) | |
download | Ninja-8b7155b0e582954909c0b56acfa2a38e04f1c5ee.zip Ninja-8b7155b0e582954909c0b56acfa2a38e04f1c5ee.tar.gz Ninja-8b7155b0e582954909c0b56acfa2a38e04f1c5ee.tar.bz2 |
teach -t commands to optionally print only the final command
-rw-r--r-- | src/ninja.cc | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/ninja.cc b/src/ninja.cc index 25eafe8..5ab73e9 100644 --- a/src/ninja.cc +++ b/src/ninja.cc @@ -533,21 +533,51 @@ int NinjaMain::ToolTargets(const Options* options, int argc, char* argv[]) { } } -void PrintCommands(Edge* edge, set<Edge*>* seen) { +enum PrintCommandMode { PCM_Single, PCM_All }; +void PrintCommands(Edge* edge, set<Edge*>* seen, PrintCommandMode mode) { if (!edge) return; if (!seen->insert(edge).second) return; - for (vector<Node*>::iterator in = edge->inputs_.begin(); - in != edge->inputs_.end(); ++in) - PrintCommands((*in)->in_edge(), seen); + if (mode == PCM_All) { + for (vector<Node*>::iterator in = edge->inputs_.begin(); + in != edge->inputs_.end(); ++in) + PrintCommands((*in)->in_edge(), seen, mode); + } if (!edge->is_phony()) puts(edge->EvaluateCommand().c_str()); } int NinjaMain::ToolCommands(const Options* options, int argc, char* argv[]) { + // The clean tool uses getopt, and expects argv[0] to contain the name of + // the tool, i.e. "commands". + ++argc; + --argv; + + PrintCommandMode mode = PCM_All; + + optind = 1; + int opt; + while ((opt = getopt(argc, argv, const_cast<char*>("hs"))) != -1) { + switch (opt) { + case 's': + mode = PCM_Single; + break; + case 'h': + default: + printf("usage: ninja -t commands [options] [targets]\n" +"\n" +"options:\n" +" -s only print the final command to build [target], not the whole chain\n" + ); + return 1; + } + } + argv += optind; + argc -= optind; + vector<Node*> nodes; string err; if (!CollectTargetsFromArgs(argc, argv, &nodes, &err)) { @@ -557,7 +587,7 @@ int NinjaMain::ToolCommands(const Options* options, int argc, char* argv[]) { set<Edge*> seen; for (vector<Node*>::iterator in = nodes.begin(); in != nodes.end(); ++in) - PrintCommands((*in)->in_edge(), &seen); + PrintCommands((*in)->in_edge(), &seen, mode); return 0; } |