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