summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/ninja.cc52
1 files changed, 31 insertions, 21 deletions
diff --git a/src/ninja.cc b/src/ninja.cc
index 2ed897a..950c610 100644
--- a/src/ninja.cc
+++ b/src/ninja.cc
@@ -40,6 +40,8 @@
#include "util.h"
#include "clean.h"
+namespace {
+
option options[] = {
{ "help", no_argument, NULL, 'h' },
{ }
@@ -104,35 +106,41 @@ struct RealFileReader : public ManifestParser::FileReader {
}
};
-int CmdGraph(State* state, int argc, char* argv[]) {
- int status = 0;
- GraphViz graph;
- graph.Start();
+bool CollectTargetsFromArgs(State* state, int argc, char* argv[],
+ vector<Node*>* targets, string* err) {
if (argc == 0) {
- string err;
- vector<Node*> root_nodes = state->RootNodes(&err);
- if (err.empty()) {
- for (vector<Node*>::const_iterator n = root_nodes.begin();
- n != root_nodes.end();
- ++n)
- graph.AddTarget(*n);
- } else {
- Error("%s", err.c_str());
- status = 1;
- }
+ *targets = state->RootNodes(err);
+ if (!err->empty())
+ return false;
} else {
for (int i = 0; i < argc; ++i) {
Node* node = state->LookupNode(argv[i]);
- if (node)
- graph.AddTarget(node);
- else {
- Error("unknown target '%s'", argv[i]);
- status = 1;
+ if (node) {
+ targets->push_back(node);
+ } else {
+ *err = string("unknown target '") + argv[i] + "'";
+ return false;
}
}
}
+ return true;
+}
+
+int CmdGraph(State* state, int argc, char* argv[]) {
+ vector<Node*> nodes;
+ string err;
+ if (!CollectTargetsFromArgs(state, argc, argv, &nodes, &err)) {
+ Error("%s", err.c_str());
+ return 1;
+ }
+
+ GraphViz graph;
+ graph.Start();
+ for (vector<Node*>::const_iterator n = nodes.begin(); n != nodes.end(); ++n)
+ graph.AddTarget(*n);
graph.Finish();
- return status;
+
+ return 0;
}
int CmdQuery(State* state, int argc, char* argv[]) {
@@ -326,6 +334,8 @@ int CmdClean(State* state,
}
}
+} // anonymous namespace
+
int main(int argc, char** argv) {
BuildConfig config;
const char* input_file = "build.ninja";