Simplify some getNode calls.
[oota-llvm.git] / tools / llvmc2 / llvmc.cpp
index 138c30691ac4f93a555502dff8b6acf3e8fd386d..e073845d09e07978666672e3880cca0241012946 100644 (file)
@@ -1,4 +1,4 @@
-//===--- llvmc.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
+//===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CompilationGraph.h"
+#include "Error.h"
 #include "Tool.h"
 
 #include "llvm/System/Path.h"
@@ -38,6 +39,8 @@ cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
 cl::list<std::string> Languages("x",
           cl::desc("Specify the language of the following input files"),
           cl::ZeroOrMore);
+cl::opt<bool> DryRun("dry-run",
+                     cl::desc("Only pretend to run commands"));
 cl::opt<bool> VerboseMode("v",
                           cl::desc("Enable verbose mode"));
 cl::opt<bool> WriteGraph("write-graph",
@@ -46,11 +49,17 @@ cl::opt<bool> WriteGraph("write-graph",
 cl::opt<bool> ViewGraph("view-graph",
                          cl::desc("Show compilation graph in GhostView"),
                          cl::Hidden);
+cl::opt<bool> SaveTemps("save-temps",
+                         cl::desc("Keep temporary files"),
+                         cl::Hidden);
 
 namespace {
+  /// BuildTargets - A small wrapper for CompilationGraph::Build.
   int BuildTargets(CompilationGraph& graph) {
     int ret;
-    sys::Path tempDir(sys::Path::GetTemporaryDirectory());
+    const sys::Path& tempDir = SaveTemps
+      ? sys::Path("")
+      : sys::Path(sys::Path::GetTemporaryDirectory());
 
     try {
       ret = graph.Build(tempDir);
@@ -60,7 +69,8 @@ namespace {
       throw;
     }
 
-    tempDir.eraseFromDisk(true);
+    if (!SaveTemps)
+      tempDir.eraseFromDisk(true);
     return ret;
   }
 }
@@ -69,8 +79,8 @@ int main(int argc, char** argv) {
   try {
     CompilationGraph graph;
 
-    cl::ParseCommandLineOptions(argc, argv,
-                                "LLVM Compiler Driver (Work In Progress)");
+    cl::ParseCommandLineOptions
+      (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
     PopulateCompilationGraph(graph);
 
     if (WriteGraph) {
@@ -85,17 +95,19 @@ int main(int argc, char** argv) {
     }
 
     if (InputFilenames.empty()) {
-      std::cerr << "No input files.\n";
-      return 1;
+      throw std::runtime_error("no input files");
     }
 
     return BuildTargets(graph);
   }
+  catch(llvmc::error_code& ec) {
+    return ec.code();
+  }
   catch(const std::exception& ex) {
-    std::cerr << ex.what() << '\n';
+    std::cerr << argv[0] << ": " << ex.what() << '\n';
   }
   catch(...) {
-    std::cerr << "Unknown error!\n";
+    std::cerr << argv[0] << ": unknown error!\n";
   }
   return 1;
 }