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