LLVM API Documentation

GraphWriter.cpp

Go to the documentation of this file.
00001 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements misc. GraphWriter support routines.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/GraphWriter.h"
00015 #include "llvm/Support/Streams.h"
00016 #include "llvm/System/Path.h"
00017 #include "llvm/System/Program.h"
00018 #include "llvm/Config/config.h"
00019 using namespace llvm;
00020 
00021 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
00022                         GraphProgram::Name program) {
00023   std::string ErrMsg;
00024 #if HAVE_GRAPHVIZ
00025   sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
00026 
00027   std::vector<const char*> args;
00028   args.push_back(Graphviz.c_str());
00029   args.push_back(Filename.c_str());
00030   args.push_back(0);
00031   
00032   cerr << "Running 'Graphviz' program... ";
00033   if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg)) {
00034      cerr << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
00035   }
00036   else {
00037      Filename.eraseFromDisk();
00038   }
00039 
00040 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
00041                    HAVE_TWOPI || HAVE_CIRCO))
00042   sys::Path PSFilename = Filename;
00043   PSFilename.appendSuffix("ps");
00044 
00045   sys::Path prog;
00046 
00047   // Set default grapher
00048 #if HAVE_CIRCO
00049   prog = sys::Path(LLVM_PATH_CIRCO);
00050 #endif
00051 #if HAVE_TWOPI
00052   prog = sys::Path(LLVM_PATH_TWOPI);
00053 #endif
00054 #if HAVE_NEATO
00055   prog = sys::Path(LLVM_PATH_NEATO);
00056 #endif
00057 #if HAVE_FDP
00058   prog = sys::Path(LLVM_PATH_FDP);
00059 #endif
00060 #if HAVE_DOT
00061   prog = sys::Path(LLVM_PATH_DOT);
00062 #endif
00063 
00064   // Find which program the user wants
00065 #if HAVE_DOT
00066   if (program == GraphProgram::DOT) {
00067     prog = sys::Path(LLVM_PATH_DOT);
00068   }
00069 #endif
00070 #if (HAVE_FDP)
00071   if (program == GraphProgram::FDP) {
00072     prog = sys::Path(LLVM_PATH_FDP);
00073   }
00074 #endif
00075 #if (HAVE_NEATO)
00076   if (program == GraphProgram::NEATO) {
00077     prog = sys::Path(LLVM_PATH_NEATO);
00078     }
00079 #endif
00080 #if (HAVE_TWOPI)
00081   if (program == GraphProgram::TWOPI) {
00082     prog = sys::Path(LLVM_PATH_TWOPI);
00083   }
00084 #endif
00085 #if (HAVE_CIRCO)
00086   if (program == GraphProgram::CIRCO) {
00087     prog = sys::Path(LLVM_PATH_CIRCO);
00088   }
00089 #endif
00090 
00091   std::vector<const char*> args;
00092   args.push_back(prog.c_str());
00093   args.push_back("-Tps");
00094   args.push_back("-Nfontname=Courier");
00095   args.push_back("-Gsize=7.5,10");
00096   args.push_back(Filename.c_str());
00097   args.push_back("-o");
00098   args.push_back(PSFilename.c_str());
00099   args.push_back(0);
00100   
00101   cerr << "Running '" << prog << "' program... ";
00102 
00103   if (sys::Program::ExecuteAndWait(prog, &args[0],0,0,0,0,&ErrMsg)) {
00104      cerr << "Error viewing graph " << Filename << ": '" << ErrMsg << "\n";
00105   } else {
00106     cerr << " done. \n";
00107 
00108     sys::Path gv(LLVM_PATH_GV);
00109     args.clear();
00110     args.push_back(gv.c_str());
00111     args.push_back(PSFilename.c_str());
00112     args.push_back("-spartan");
00113     args.push_back(0);
00114     
00115     ErrMsg.clear();
00116     if (wait) {
00117        if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg)) {
00118           cerr << "Error viewing graph: " << ErrMsg << "\n";
00119        }
00120        Filename.eraseFromDisk();
00121        PSFilename.eraseFromDisk();
00122     }
00123     else {
00124        sys::Program::ExecuteNoWait(gv, &args[0],0,0,0,&ErrMsg);
00125        cerr << "Remember to erase graph files: " << Filename << " " << PSFilename << "\n";
00126     }
00127   }
00128 #elif HAVE_DOTTY
00129   sys::Path dotty(LLVM_PATH_DOTTY);
00130 
00131   std::vector<const char*> args;
00132   args.push_back(dotty.c_str());
00133   args.push_back(Filename.c_str());
00134   args.push_back(0);
00135   
00136   cerr << "Running 'dotty' program... ";
00137   if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
00138      cerr << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
00139   } else {
00140 #ifdef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
00141     return;
00142 #endif
00143     Filename.eraseFromDisk();
00144   }
00145 #endif
00146 }