build ninja_test: link @ninja_test.o @parsers_test.o @ninja.a
ldflags = -lgtest -lgtest_main -lpthread
+
+# Generate a graph of the dependency tree (including the
+# graph generation itself in the resulting tree).
+graph_targets = ninja ninja_test graph.png
+rule gendot
+ command = ./ninja -g $graph_targets > $out
+rule gengraph
+ command = dot -Tpng $in > $out
+
+build @graph.dot: gendot ninja build.ninja
+build graph.png: gengraph @graph.dot
--- /dev/null
+#include <set>
+
+struct Node;
+
+struct GraphViz {
+ void Start();
+ void AddTarget(Node* node);
+ void Finish();
+
+ set<Node*> visited_;
+};
+
+void GraphViz::AddTarget(Node* node) {
+ if (visited_.find(node) != visited_.end())
+ return;
+ printf("\"%p\" [label=\"%s\"]\n", node, node->file_->path_.c_str());
+ visited_.insert(node);
+
+ if (!node->in_edge_) {
+ // Leaf node.
+ // Draw as a rect?
+ return;
+ }
+
+ Edge* edge = node->in_edge_;
+
+ if (edge->inputs_.size() == 1 && edge->outputs_.size() == 1) {
+ // Can draw simply.
+ printf("\"%p\" -> \"%p\" [label=\"%s\"]\n",
+ edge->inputs_[0], edge->outputs_[0], edge->rule_->name_.c_str());
+ } else {
+ printf("\"%p\" [label=\"%s\", shape=plaintext]\n",
+ edge, edge->rule_->name_.c_str());
+ for (vector<Node*>::iterator out = edge->outputs_.begin();
+ out != edge->outputs_.end(); ++out) {
+ printf("\"%p\" -> \"%p\"\n", edge, *out);
+ }
+ for (vector<Node*>::iterator in = edge->inputs_.begin();
+ in != edge->inputs_.end(); ++in) {
+ printf("\"%p\" -> \"%p\"\n", (*in), edge);
+ }
+ }
+
+ for (vector<Node*>::iterator in = edge->inputs_.begin();
+ in != edge->inputs_.end(); ++in) {
+ AddTarget(*in);
+ }
+}
+
+void GraphViz::Start() {
+ printf("digraph ninja {\n");
+ printf("node [fontsize=10, shape=box, height=0.25]\n");
+ printf("edge [fontsize=10]\n");
+}
+
+void GraphViz::Finish() {
+ printf("}\n");
+}
+
#include <getopt.h>
#include <stdio.h>
+#include "graphviz.h"
#include "parsers.h"
option options[] = {
"usage: ninja [options] target\n"
"\n"
"options:\n"
+" -g output graphviz dot file for targets and exit\n"
" -i FILE specify input build file [default=build.ninja]\n"
);
}
const char* input_file = "build.ninja";
int opt;
- while ((opt = getopt_long(argc, argv, "hi:", options, NULL)) != -1) {
+ bool graph = false;
+ while ((opt = getopt_long(argc, argv, "ghi:", options, NULL)) != -1) {
switch (opt) {
+ case 'g':
+ graph = true;
+ break;
case 'i':
input_file = optarg;
break;
usage();
return 1;
}
+ argv += optind;
+ argc -= optind;
State state;
RealFileReader file_reader;
fprintf(stderr, "error loading '%s': %s\n", input_file, err.c_str());
return 1;
}
+
+ if (graph) {
+ GraphViz graph;
+ graph.Start();
+ for (int i = 0; i < argc; ++i)
+ graph.AddTarget(state.GetNode(argv[i]));
+ graph.Finish();
+ return 0;
+ }
+
Shell shell;
Builder builder(&state);
- for (int i = optind; i < argc; ++i) {
+ for (int i = 0; i < argc; ++i) {
if (!builder.AddTarget(argv[i], &err)) {
if (!err.empty()) {
fprintf(stderr, "%s\n", err.c_str());