Rename getObjectFile to getObject for consistency.
[oota-llvm.git] / lib / Support / GraphWriter.cpp
index b707a2daa95d2af826bcfb4887c38cbc6dfbfe83..97aedc88473a2db08d6fa7ff3ef82d36041c5007 100644 (file)
@@ -15,7 +15,6 @@
 #include "llvm/Config/config.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
-#include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"
 using namespace llvm;
 
@@ -68,7 +67,7 @@ StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
 std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
   FD = -1;
   SmallString<128> Filename;
-  error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
+  std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
   if (EC) {
     errs() << "Error: " << EC.message() << "\n";
     return "";
@@ -93,11 +92,12 @@ static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
     errs() << " done. \n";
   } else {
     sys::ExecuteNoWait(ExecPath, args.data(), nullptr, nullptr, 0, &ErrMsg);
-    errs() << "Remember to erase graph file: " << Filename.str() << "\n";
+    errs() << "Remember to erase graph file: " << Filename << "\n";
   }
   return false;
 }
 
+namespace {
 struct GraphSession {
   std::string LogBuffer;
   bool TryFindProgram(StringRef Names, std::string &ProgramPath) {
@@ -105,14 +105,16 @@ struct GraphSession {
     SmallVector<StringRef, 8> parts;
     Names.split(parts, "|");
     for (auto Name : parts) {
-      ProgramPath = sys::FindProgramByName(Name);
-      if (!ProgramPath.empty())
+      if (ErrorOr<std::string> P = sys::findProgramByName(Name)) {
+        ProgramPath = *P;
         return true;
+      }
       Log << "  Tried '" << Name << "'\n";
     }
     return false;
   }
 };
+} // namespace
 
 static const char *getProgramName(GraphProgram::Name program) {
   switch (program) {
@@ -127,6 +129,7 @@ static const char *getProgramName(GraphProgram::Name program) {
   case GraphProgram::CIRCO:
     return "circo";
   }
+  llvm_unreachable("bad kind");
 }
 
 bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
@@ -137,6 +140,29 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
   std::string ViewerPath;
   GraphSession S;
 
+#ifdef __APPLE__
+  if (S.TryFindProgram("open", ViewerPath)) {
+    std::vector<const char *> args;
+    args.push_back(ViewerPath.c_str());
+    if (wait)
+      args.push_back("-W");
+    args.push_back(Filename.c_str());
+    args.push_back(nullptr);
+    errs() << "Trying 'open' program... ";
+    if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
+      return false;
+  }
+#endif
+  if (S.TryFindProgram("xdg-open", ViewerPath)) {
+    std::vector<const char *> args;
+    args.push_back(ViewerPath.c_str());
+    args.push_back(Filename.c_str());
+    args.push_back(nullptr);
+    errs() << "Trying 'xdg-open' program... ";
+    if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
+      return false;
+  }
+
   // Graphviz
   if (S.TryFindProgram("Graphviz", ViewerPath)) {
     std::vector<const char *> args;
@@ -163,20 +189,22 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
     return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
   }
 
-  enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_Ghostview };
+  enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_XDGOpen, PSV_Ghostview };
   PSViewerKind PSViewer = PSV_None;
 #ifdef __APPLE__
-  if (S.TryFindProgram("open", ViewerPath))
+  if (!PSViewer && S.TryFindProgram("open", ViewerPath))
     PSViewer = PSV_OSXOpen;
 #endif
   if (!PSViewer && S.TryFindProgram("gv", ViewerPath))
     PSViewer = PSV_Ghostview;
+  if (!PSViewer && S.TryFindProgram("xdg-open", ViewerPath))
+    PSViewer = PSV_XDGOpen;
 
   // PostScript graph generator + PostScript viewer
   std::string GeneratorPath;
   if (PSViewer &&
       (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
-       S.TryFindProgram("circo|twopi|neato|fdp|dot", GeneratorPath))) {
+       S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
     std::string PSFilename = Filename + ".ps";
 
     std::vector<const char *> args;
@@ -201,6 +229,10 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
       args.push_back("-W");
       args.push_back(PSFilename.c_str());
       break;
+    case PSV_XDGOpen:
+      wait = false;
+      args.push_back(PSFilename.c_str());
+      break;
     case PSV_Ghostview:
       args.push_back("--spartan");
       args.push_back(PSFilename.c_str());