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"
19using namespace llvm;
20
21static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
22  cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
23
24std::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.
57static bool LLVM_ATTRIBUTE_UNUSED
58ExecGraphViewer(const sys::Path &ExecPath, std::vector<const char*> &args,
59                const sys::Path &Filename, bool wait, std::string &ErrMsg) {
60  if (wait) {
61    if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
62      errs() << "Error: " << ErrMsg << "\n";
63      return false;
64    }
65    Filename.eraseFromDisk();
66    errs() << " done. \n";
67  }
68  else {
69    sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
70    errs() << "Remember to erase graph file: " << Filename.str() << "\n";
71  }
72  return true;
73}
74
75void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
76                        GraphProgram::Name program) {
77  wait &= !ViewBackground;
78  std::string ErrMsg;
79#if HAVE_GRAPHVIZ
80  sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
81
82  std::vector<const char*> args;
83  args.push_back(Graphviz.c_str());
84  args.push_back(Filename.c_str());
85  args.push_back(0);
86
87  errs() << "Running 'Graphviz' program... ";
88  if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
89    return;
90
91#elif HAVE_XDOT_PY
92  std::vector<const char*> args;
93  args.push_back(LLVM_PATH_XDOT_PY);
94  args.push_back(Filename.c_str());
95
96  switch (program) {
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";
103  }
104
105  args.push_back(0);
106
107  errs() << "Running 'xdot.py' program... ";
108  if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
109    return;
110
111#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
112                   HAVE_TWOPI || HAVE_CIRCO))
113  sys::Path PSFilename = Filename;
114  PSFilename.appendSuffix("ps");
115
116  sys::Path prog;
117
118  // Set default grapher
119#if HAVE_CIRCO
120  prog = sys::Path(LLVM_PATH_CIRCO);
121#endif
122#if HAVE_TWOPI
123  prog = sys::Path(LLVM_PATH_TWOPI);
124#endif
125#if HAVE_NEATO
126  prog = sys::Path(LLVM_PATH_NEATO);
127#endif
128#if HAVE_FDP
129  prog = sys::Path(LLVM_PATH_FDP);
130#endif
131#if HAVE_DOT
132  prog = sys::Path(LLVM_PATH_DOT);
133#endif
134
135  // Find which program the user wants
136#if HAVE_DOT
137  if (program == GraphProgram::DOT)
138    prog = sys::Path(LLVM_PATH_DOT);
139#endif
140#if (HAVE_FDP)
141  if (program == GraphProgram::FDP)
142    prog = sys::Path(LLVM_PATH_FDP);
143#endif
144#if (HAVE_NEATO)
145  if (program == GraphProgram::NEATO)
146    prog = sys::Path(LLVM_PATH_NEATO);
147#endif
148#if (HAVE_TWOPI)
149  if (program == GraphProgram::TWOPI)
150    prog = sys::Path(LLVM_PATH_TWOPI);
151#endif
152#if (HAVE_CIRCO)
153  if (program == GraphProgram::CIRCO)
154    prog = sys::Path(LLVM_PATH_CIRCO);
155#endif
156
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());
165  args.push_back(0);
166
167  errs() << "Running '" << prog.str() << "' program... ";
168
169  if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
170    return;
171
172  sys::Path gv(LLVM_PATH_GV);
173  args.clear();
174  args.push_back(gv.c_str());
175  args.push_back(PSFilename.c_str());
176  args.push_back("--spartan");
177  args.push_back(0);
178
179  ErrMsg.clear();
180  if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
181    return;
182
183#elif HAVE_DOTTY
184  sys::Path dotty(LLVM_PATH_DOTTY);
185
186  std::vector<const char*> args;
187  args.push_back(dotty.c_str());
188  args.push_back(Filename.c_str());
189  args.push_back(0);
190
191// Dotty spawns another app and doesn't wait until it returns
192#if defined (__MINGW32__) || defined (_WINDOWS)
193  wait = false;
194#endif
195  errs() << "Running 'dotty' program... ";
196  if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))
197    return;
198#endif
199}
200