1 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements misc. GraphWriter support routines.
12 //===----------------------------------------------------------------------===//
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"
21 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
22 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
24 std::string llvm::DOT::EscapeString(const std::string &Label) {
25 std::string Str(Label);
26 for (unsigned i = 0; i != Str.length(); ++i)
29 Str.insert(Str.begin()+i, '\\'); // Escape character...
34 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
39 if (i+1 != Str.length())
41 case 'l': continue; // don't disturb \l
42 case '|': case '{': case '}':
43 Str.erase(Str.begin()+i); continue;
49 Str.insert(Str.begin()+i, '\\'); // Escape character...
50 ++i; // don't infinite loop
56 // Execute the graph viewer. Return true if successful.
57 static bool LLVM_ATTRIBUTE_UNUSED
58 ExecGraphViewer(const sys::Path &ExecPath, std::vector<const char*> &args,
59 const sys::Path &Filename, bool wait, std::string &ErrMsg) {
61 if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
62 errs() << "Error: " << ErrMsg << "\n";
65 Filename.eraseFromDisk();
66 errs() << " done. \n";
69 sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
70 errs() << "Remember to erase graph file: " << Filename.str() << "\n";
75 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
76 GraphProgram::Name program) {
77 wait &= !ViewBackground;
80 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
82 std::vector<const char*> args;
83 args.push_back(Graphviz.c_str());
84 args.push_back(Filename.c_str());
87 errs() << "Running 'Graphviz' program... ";
88 if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
92 std::vector<const char*> args;
93 args.push_back(LLVM_PATH_XDOT_PY);
94 args.push_back(Filename.c_str());
97 case GraphProgram::DOT: args.push_back("-f"); args.push_back("dot"); break;
98 case GraphProgram::FDP: args.push_back("-f"); args.push_back("fdp"); break;
99 case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
100 case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
101 case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
102 default: errs() << "Unknown graph layout name; using default.\n";
107 errs() << "Running 'xdot.py' program... ";
108 if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
111 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
112 HAVE_TWOPI || HAVE_CIRCO))
113 sys::Path PSFilename = Filename;
114 PSFilename.appendSuffix("ps");
118 // Set default grapher
120 prog = sys::Path(LLVM_PATH_CIRCO);
123 prog = sys::Path(LLVM_PATH_TWOPI);
126 prog = sys::Path(LLVM_PATH_NEATO);
129 prog = sys::Path(LLVM_PATH_FDP);
132 prog = sys::Path(LLVM_PATH_DOT);
135 // Find which program the user wants
137 if (program == GraphProgram::DOT)
138 prog = sys::Path(LLVM_PATH_DOT);
141 if (program == GraphProgram::FDP)
142 prog = sys::Path(LLVM_PATH_FDP);
145 if (program == GraphProgram::NEATO)
146 prog = sys::Path(LLVM_PATH_NEATO);
149 if (program == GraphProgram::TWOPI)
150 prog = sys::Path(LLVM_PATH_TWOPI);
153 if (program == GraphProgram::CIRCO)
154 prog = sys::Path(LLVM_PATH_CIRCO);
157 std::vector<const char*> args;
158 args.push_back(prog.c_str());
159 args.push_back("-Tps");
160 args.push_back("-Nfontname=Courier");
161 args.push_back("-Gsize=7.5,10");
162 args.push_back(Filename.c_str());
163 args.push_back("-o");
164 args.push_back(PSFilename.c_str());
167 errs() << "Running '" << prog.str() << "' program... ";
169 if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
172 sys::Path gv(LLVM_PATH_GV);
174 args.push_back(gv.c_str());
175 args.push_back(PSFilename.c_str());
176 args.push_back("--spartan");
180 if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
184 sys::Path dotty(LLVM_PATH_DOTTY);
186 std::vector<const char*> args;
187 args.push_back(dotty.c_str());
188 args.push_back(Filename.c_str());
191 // Dotty spawns another app and doesn't wait until it returns
192 #if defined (__MINGW32__) || defined (_WINDOWS)
195 errs() << "Running 'dotty' program... ";
196 if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))