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 ExecGraphViewer(const sys::Path &ExecPath,
58 std::vector<const char*> &args,
59 const sys::Path &Filename,
61 std::string &ErrMsg) {
63 if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
64 errs() << "Error: " << ErrMsg << "\n";
67 Filename.eraseFromDisk();
68 errs() << " done. \n";
71 sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
72 errs() << "Remember to erase graph file: " << Filename.str() << "\n";
77 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
78 GraphProgram::Name program) {
79 wait &= !ViewBackground;
82 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
84 std::vector<const char*> args;
85 args.push_back(Graphviz.c_str());
86 args.push_back(Filename.c_str());
89 errs() << "Running 'Graphviz' program... ";
90 if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
94 std::vector<const char*> args;
95 args.push_back(LLVM_PATH_XDOT_PY);
96 args.push_back(Filename.c_str());
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";
109 errs() << "Running 'xdot.py' program... ";
110 if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
113 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
114 HAVE_TWOPI || HAVE_CIRCO))
115 sys::Path PSFilename = Filename;
116 PSFilename.appendSuffix("ps");
120 // Set default grapher
122 prog = sys::Path(LLVM_PATH_CIRCO);
125 prog = sys::Path(LLVM_PATH_TWOPI);
128 prog = sys::Path(LLVM_PATH_NEATO);
131 prog = sys::Path(LLVM_PATH_FDP);
134 prog = sys::Path(LLVM_PATH_DOT);
137 // Find which program the user wants
139 if (program == GraphProgram::DOT)
140 prog = sys::Path(LLVM_PATH_DOT);
143 if (program == GraphProgram::FDP)
144 prog = sys::Path(LLVM_PATH_FDP);
147 if (program == GraphProgram::NEATO)
148 prog = sys::Path(LLVM_PATH_NEATO);
151 if (program == GraphProgram::TWOPI)
152 prog = sys::Path(LLVM_PATH_TWOPI);
155 if (program == GraphProgram::CIRCO)
156 prog = sys::Path(LLVM_PATH_CIRCO);
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());
169 errs() << "Running '" << prog.str() << "' program... ";
171 if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
174 sys::Path gv(LLVM_PATH_GV);
176 args.push_back(gv.c_str());
177 args.push_back(PSFilename.c_str());
178 args.push_back("--spartan");
182 if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
186 sys::Path dotty(LLVM_PATH_DOTTY);
188 std::vector<const char*> args;
189 args.push_back(dotty.c_str());
190 args.push_back(Filename.c_str());
193 // Dotty spawns another app and doesn't wait until it returns
194 #if defined (__MINGW32__) || defined (_WINDOWS)
197 errs() << "Running 'dotty' program... ";
198 if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))