GraphWriter.cpp revision 4ee451de366474b9c228b4e5fa573795a715216d
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/GraphWriter.h"
15#include "llvm/Support/Streams.h"
16#include "llvm/System/Path.h"
17#include "llvm/System/Program.h"
18#include "llvm/Config/config.h"
19using namespace llvm;
20
21void llvm::DisplayGraph(const sys::Path &Filename) {
22  std::string ErrMsg;
23#if HAVE_GRAPHVIZ
24  sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
25
26  std::vector<const char*> args;
27  args.push_back(Graphviz.c_str());
28  args.push_back(Filename.c_str());
29  args.push_back(0);
30
31  cerr << "Running 'Graphviz' program... " << std::flush;
32  if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg)) {
33    cerr << "Error viewing graph: " << ErrMsg << "\n";
34  }
35#elif (HAVE_GV && HAVE_DOT)
36  sys::Path PSFilename = Filename;
37  PSFilename.appendSuffix("ps");
38
39  sys::Path dot(LLVM_PATH_DOT);
40
41  std::vector<const char*> args;
42  args.push_back(dot.c_str());
43  args.push_back("-Tps");
44  args.push_back("-Nfontname=Courier");
45  args.push_back("-Gsize=7.5,10");
46  args.push_back(Filename.c_str());
47  args.push_back("-o");
48  args.push_back(PSFilename.c_str());
49  args.push_back(0);
50
51  cerr << "Running 'dot' program... " << std::flush;
52  if (sys::Program::ExecuteAndWait(dot, &args[0],0,0,0,0,&ErrMsg)) {
53    cerr << "Error viewing graph: '" << ErrMsg << "\n";
54  } else {
55    cerr << " done. \n";
56
57    sys::Path gv(LLVM_PATH_GV);
58    args.clear();
59    args.push_back(gv.c_str());
60    args.push_back(PSFilename.c_str());
61    args.push_back(0);
62
63    ErrMsg.clear();
64    if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg)) {
65      cerr << "Error viewing graph: " << ErrMsg << "\n";
66    }
67  }
68  PSFilename.eraseFromDisk();
69#elif HAVE_DOTTY
70  sys::Path dotty(LLVM_PATH_DOTTY);
71
72  std::vector<const char*> args;
73  args.push_back(dotty.c_str());
74  args.push_back(Filename.c_str());
75  args.push_back(0);
76
77  cerr << "Running 'dotty' program... " << std::flush;
78  if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
79    cerr << "Error viewing graph: " << ErrMsg << "\n";
80  } else {
81#ifdef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
82    return;
83#endif
84  }
85#endif
86
87  Filename.eraseFromDisk();
88}
89