aca46d8b754749f8948fbec497112dc36bed5b94
[oota-llvm.git] / lib / Support / GraphWriter.cpp
1 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements misc. GraphWriter support routines.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/GraphWriter.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/Program.h"
18 #include "llvm/Config/config.h"
19 using namespace llvm;
20
21 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
22   cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
23
24 std::string llvm::DOT::EscapeString(const std::string &Label) {
25   std::string Str(Label);
26   for (unsigned i = 0; i != Str.length(); ++i)
27   switch (Str[i]) {
28     case '\n':
29       Str.insert(Str.begin()+i, '\\');  // Escape character...
30       ++i;
31       Str[i] = 'n';
32       break;
33     case '\t':
34       Str.insert(Str.begin()+i, ' ');  // Convert to two spaces
35       ++i;
36       Str[i] = ' ';
37       break;
38     case '\\':
39       if (i+1 != Str.length())
40         switch (Str[i+1]) {
41           case 'l': continue; // don't disturb \l
42           case '|': case '{': case '}':
43             Str.erase(Str.begin()+i); continue;
44           default: break;
45         }
46     case '{': case '}':
47     case '<': case '>':
48     case '|': case '"':
49       Str.insert(Str.begin()+i, '\\');  // Escape character...
50       ++i;  // don't infinite loop
51       break;
52   }
53   return Str;
54 }
55
56 // Execute the graph viewer. Return true if successful.
57 static bool ExecGraphViewer(const sys::Path &ExecPath,
58                             std::vector<const char*> &args,
59                             const sys::Path &Filename,
60                             bool wait,
61                             std::string &ErrMsg) {
62   if (wait) {
63     if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
64       errs() << "Error: " << ErrMsg << "\n";
65       return false;
66     }
67     Filename.eraseFromDisk();
68     errs() << " done. \n";
69   }
70   else {
71     sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
72     errs() << "Remember to erase graph file: " << Filename.str() << "\n";
73   }
74   return true;
75 }
76
77 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
78                         GraphProgram::Name program) {
79   wait &= !ViewBackground;
80   std::string ErrMsg;
81 #if HAVE_GRAPHVIZ
82   sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
83
84   std::vector<const char*> args;
85   args.push_back(Graphviz.c_str());
86   args.push_back(Filename.c_str());
87   args.push_back(0);
88
89   errs() << "Running 'Graphviz' program... ";
90   if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
91     return;
92
93 #elif HAVE_XDOT_PY
94   std::vector<const char*> args;
95   args.push_back(LLVM_PATH_XDOT_PY);
96   args.push_back(Filename.c_str());
97
98   switch (program) {
99   case GraphProgram::DOT:   args.push_back("-f"); args.push_back("dot"); break;
100   case GraphProgram::FDP:   args.push_back("-f"); args.push_back("fdp"); break;
101   case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
102   case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
103   case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
104   default: errs() << "Unknown graph layout name; using default.\n";
105   }
106
107   args.push_back(0);
108
109   errs() << "Running 'xdot.py' program... ";
110   if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
111     return;
112
113 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
114                    HAVE_TWOPI || HAVE_CIRCO))
115   sys::Path PSFilename = Filename;
116   PSFilename.appendSuffix("ps");
117
118   sys::Path prog;
119
120   // Set default grapher
121 #if HAVE_CIRCO
122   prog = sys::Path(LLVM_PATH_CIRCO);
123 #endif
124 #if HAVE_TWOPI
125   prog = sys::Path(LLVM_PATH_TWOPI);
126 #endif
127 #if HAVE_NEATO
128   prog = sys::Path(LLVM_PATH_NEATO);
129 #endif
130 #if HAVE_FDP
131   prog = sys::Path(LLVM_PATH_FDP);
132 #endif
133 #if HAVE_DOT
134   prog = sys::Path(LLVM_PATH_DOT);
135 #endif
136
137   // Find which program the user wants
138 #if HAVE_DOT
139   if (program == GraphProgram::DOT)
140     prog = sys::Path(LLVM_PATH_DOT);
141 #endif
142 #if (HAVE_FDP)
143   if (program == GraphProgram::FDP)
144     prog = sys::Path(LLVM_PATH_FDP);
145 #endif
146 #if (HAVE_NEATO)
147   if (program == GraphProgram::NEATO)
148     prog = sys::Path(LLVM_PATH_NEATO);
149 #endif
150 #if (HAVE_TWOPI)
151   if (program == GraphProgram::TWOPI)
152     prog = sys::Path(LLVM_PATH_TWOPI);
153 #endif
154 #if (HAVE_CIRCO)
155   if (program == GraphProgram::CIRCO)
156     prog = sys::Path(LLVM_PATH_CIRCO);
157 #endif
158
159   std::vector<const char*> args;
160   args.push_back(prog.c_str());
161   args.push_back("-Tps");
162   args.push_back("-Nfontname=Courier");
163   args.push_back("-Gsize=7.5,10");
164   args.push_back(Filename.c_str());
165   args.push_back("-o");
166   args.push_back(PSFilename.c_str());
167   args.push_back(0);
168
169   errs() << "Running '" << prog.str() << "' program... ";
170
171   if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
172     return;
173
174   sys::Path gv(LLVM_PATH_GV);
175   args.clear();
176   args.push_back(gv.c_str());
177   args.push_back(PSFilename.c_str());
178   args.push_back("--spartan");
179   args.push_back(0);
180
181   ErrMsg.clear();
182   if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
183     return;
184
185 #elif HAVE_DOTTY
186   sys::Path dotty(LLVM_PATH_DOTTY);
187
188   std::vector<const char*> args;
189   args.push_back(dotty.c_str());
190   args.push_back(Filename.c_str());
191   args.push_back(0);
192
193 // Dotty spawns another app and doesn't wait until it returns
194 #if defined (__MINGW32__) || defined (_WINDOWS)
195   wait = false;
196 #endif
197   errs() << "Running 'dotty' program... ";
198   if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))
199     return;
200 #endif
201 }