summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2016-11-07 21:42:46 (GMT)
committerGitHub <noreply@github.com>2016-11-07 21:42:46 (GMT)
commit04f4bc5dbbc9bfb96ef75f577e919082c8690ae9 (patch)
tree8dc920f9f98f5f0e7af95553f1ba0ee766f9b1f8 /src
parentb0cce09f563d0942fcf1c1256db679cebcbd6bea (diff)
parent8b7155b0e582954909c0b56acfa2a38e04f1c5ee (diff)
downloadNinja-04f4bc5dbbc9bfb96ef75f577e919082c8690ae9.zip
Ninja-04f4bc5dbbc9bfb96ef75f577e919082c8690ae9.tar.gz
Ninja-04f4bc5dbbc9bfb96ef75f577e919082c8690ae9.tar.bz2
Merge pull request #1201 from nico/singlecommand
teach -t commands to optionally print only the final command
Diffstat (limited to 'src')
-rw-r--r--src/ninja.cc40
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;
}