Imported Upstream version 1.7.1
[platform/upstream/ninja.git] / src / graphviz.cc
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 #include <algorithm>
19
20 #include "graph.h"
21
22 void GraphViz::AddTarget(Node* node) {
23   if (visited_nodes_.find(node) != visited_nodes_.end())
24     return;
25
26   string pathstr = node->path();
27   replace(pathstr.begin(), pathstr.end(), '\\', '/');
28   printf("\"%p\" [label=\"%s\"]\n", node, pathstr.c_str());
29   visited_nodes_.insert(node);
30
31   Edge* edge = node->in_edge();
32
33   if (!edge) {
34     // Leaf node.
35     // Draw as a rect?
36     return;
37   }
38
39   if (visited_edges_.find(edge) != visited_edges_.end())
40     return;
41   visited_edges_.insert(edge);
42
43   if (edge->inputs_.size() == 1 && edge->outputs_.size() == 1) {
44     // Can draw simply.
45     // Note extra space before label text -- this is cosmetic and feels
46     // like a graphviz bug.
47     printf("\"%p\" -> \"%p\" [label=\" %s\"]\n",
48            edge->inputs_[0], edge->outputs_[0], edge->rule_->name().c_str());
49   } else {
50     printf("\"%p\" [label=\"%s\", shape=ellipse]\n",
51            edge, edge->rule_->name().c_str());
52     for (vector<Node*>::iterator out = edge->outputs_.begin();
53          out != edge->outputs_.end(); ++out) {
54       printf("\"%p\" -> \"%p\"\n", edge, *out);
55     }
56     for (vector<Node*>::iterator in = edge->inputs_.begin();
57          in != edge->inputs_.end(); ++in) {
58       const char* order_only = "";
59       if (edge->is_order_only(in - edge->inputs_.begin()))
60         order_only = " style=dotted";
61       printf("\"%p\" -> \"%p\" [arrowhead=none%s]\n", (*in), edge, order_only);
62     }
63   }
64
65   for (vector<Node*>::iterator in = edge->inputs_.begin();
66        in != edge->inputs_.end(); ++in) {
67     AddTarget(*in);
68   }
69 }
70
71 void GraphViz::Start() {
72   printf("digraph ninja {\n");
73   printf("rankdir=\"LR\"\n");
74   printf("node [fontsize=10, shape=box, height=0.25]\n");
75   printf("edge [fontsize=10]\n");
76 }
77
78 void GraphViz::Finish() {
79   printf("}\n");
80 }