Teach InlineFunction how to differentiate between multiple-value
[oota-llvm.git] / tools / llvmc2 / llvmc.cpp
1 //===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This tool provides a single point of access to the LLVM
11 //  compilation tools.  It has many options. To discover the options
12 //  supported please refer to the tools' manual page or run the tool
13 //  with the --help option.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "CompilationGraph.h"
18 #include "Error.h"
19 #include "Tool.h"
20
21 #include "llvm/System/Path.h"
22 #include "llvm/Support/CommandLine.h"
23
24 #include <iostream>
25 #include <stdexcept>
26 #include <string>
27
28 namespace cl = llvm::cl;
29 namespace sys = llvm::sys;
30 using namespace llvmc;
31
32 // Built-in command-line options.
33 // External linkage here is intentional.
34
35 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
36                                      cl::ZeroOrMore);
37 cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
38                                     cl::value_desc("file"));
39 cl::list<std::string> Languages("x",
40           cl::desc("Specify the language of the following input files"),
41           cl::ZeroOrMore);
42 cl::opt<bool> DryRun("dry-run",
43                      cl::desc("Only pretend to run commands"));
44 cl::opt<bool> VerboseMode("v",
45                           cl::desc("Enable verbose mode"));
46 cl::opt<bool> WriteGraph("write-graph",
47                          cl::desc("Write compilation-graph.dot file"),
48                          cl::Hidden);
49 cl::opt<bool> ViewGraph("view-graph",
50                          cl::desc("Show compilation graph in GhostView"),
51                          cl::Hidden);
52 cl::opt<bool> SaveTemps("save-temps",
53                          cl::desc("Keep temporary files"),
54                          cl::Hidden);
55
56 namespace {
57   /// BuildTargets - A small wrapper for CompilationGraph::Build.
58   int BuildTargets(CompilationGraph& graph) {
59     int ret;
60     const sys::Path& tempDir = SaveTemps
61       ? sys::Path("")
62       : sys::Path(sys::Path::GetTemporaryDirectory());
63
64     try {
65       ret = graph.Build(tempDir);
66     }
67     catch(...) {
68       tempDir.eraseFromDisk(true);
69       throw;
70     }
71
72     if (!SaveTemps)
73       tempDir.eraseFromDisk(true);
74     return ret;
75   }
76 }
77
78 int main(int argc, char** argv) {
79   try {
80     CompilationGraph graph;
81
82     cl::ParseCommandLineOptions
83       (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
84     PopulateCompilationGraph(graph);
85
86     if (WriteGraph) {
87       graph.writeGraph();
88       if (!ViewGraph)
89         return 0;
90     }
91
92     if (ViewGraph) {
93       graph.viewGraph();
94       return 0;
95     }
96
97     if (InputFilenames.empty()) {
98       throw std::runtime_error("no input files");
99     }
100
101     return BuildTargets(graph);
102   }
103   catch(llvmc::error_code& ec) {
104     return ec.code();
105   }
106   catch(const std::exception& ex) {
107     std::cerr << argv[0] << ": " << ex.what() << '\n';
108   }
109   catch(...) {
110     std::cerr << argv[0] << ": unknown error!\n";
111   }
112   return 1;
113 }