Ninja
graphviz.cc
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "graphviz.h"
16 
17 #include <stdio.h>
18 
19 #include "graph.h"
20 
22  if (visited_nodes_.find(node) != visited_nodes_.end())
23  return;
24 
25  printf("\"%p\" [label=\"%s\"]\n", node, node->path().c_str());
26  visited_nodes_.insert(node);
27 
28  Edge* edge = node->in_edge();
29 
30  if (!edge) {
31  // Leaf node.
32  // Draw as a rect?
33  return;
34  }
35 
36  if (visited_edges_.find(edge) != visited_edges_.end())
37  return;
38  visited_edges_.insert(edge);
39 
40  if (edge->inputs_.size() == 1 && edge->outputs_.size() == 1) {
41  // Can draw simply.
42  // Note extra space before label text -- this is cosmetic and feels
43  // like a graphviz bug.
44  printf("\"%p\" -> \"%p\" [label=\" %s\"]\n",
45  edge->inputs_[0], edge->outputs_[0], edge->rule_->name().c_str());
46  } else {
47  printf("\"%p\" [label=\"%s\", shape=ellipse]\n",
48  edge, edge->rule_->name().c_str());
49  for (vector<Node*>::iterator out = edge->outputs_.begin();
50  out != edge->outputs_.end(); ++out) {
51  printf("\"%p\" -> \"%p\"\n", edge, *out);
52  }
53  for (vector<Node*>::iterator in = edge->inputs_.begin();
54  in != edge->inputs_.end(); ++in) {
55  const char* order_only = "";
56  if (edge->is_order_only(in - edge->inputs_.begin()))
57  order_only = " style=dotted";
58  printf("\"%p\" -> \"%p\" [arrowhead=none%s]\n", (*in), edge, order_only);
59  }
60  }
61 
62  for (vector<Node*>::iterator in = edge->inputs_.begin();
63  in != edge->inputs_.end(); ++in) {
64  AddTarget(*in);
65  }
66 }
67 
69  printf("digraph ninja {\n");
70  printf("rankdir=\"LR\"\n");
71  printf("node [fontsize=10, shape=box, height=0.25]\n");
72  printf("edge [fontsize=10]\n");
73 }
74 
76  printf("}\n");
77 }
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Definition: graph.h:35
set< Node * > visited_nodes_
Definition: graphviz.h:30
Edge * in_edge() const
Definition: graph.h:80
An edge in the dependency graph; links between Nodes using Rules.
Definition: graph.h:137
bool is_order_only(size_t index)
Definition: graph.h:180
vector< Node * > inputs_
Definition: graph.h:156
void Finish()
Definition: graphviz.cc:75
void Start()
Definition: graphviz.cc:68
const string & path() const
Definition: graph.h:73
const Rule * rule_
Definition: graph.h:154
const string & name() const
Definition: graph.h:119
void AddTarget(Node *node)
Definition: graphviz.cc:21
set< Edge * > visited_edges_
Definition: graphviz.h:31
vector< Node * > outputs_
Definition: graph.h:157