Remove special cases for more opcodes.
[oota-llvm.git] / include / llvm / CompilerDriver / Main.inc
1 //===--- Main.inc - 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 //  This
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_INCLUDE_COMPILER_DRIVER_MAIN_INC
20 #define LLVM_INCLUDE_COMPILER_DRIVER_MAIN_INC
21
22 #include "llvm/CompilerDriver/CompilationGraph.h"
23 #include "llvm/CompilerDriver/Error.h"
24 #include "llvm/CompilerDriver/Plugin.h"
25
26 #include "llvm/System/Path.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/PluginLoader.h"
29
30 #include <iostream>
31 #include <stdexcept>
32 #include <string>
33
34 namespace cl = llvm::cl;
35 namespace sys = llvm::sys;
36 using namespace llvmc;
37
38 // Built-in command-line options.
39 // External linkage here is intentional.
40
41 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
42                                      cl::ZeroOrMore);
43 cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
44                                     cl::value_desc("file"), cl::Prefix);
45 cl::list<std::string> Languages("x",
46           cl::desc("Specify the language of the following input files"),
47           cl::ZeroOrMore);
48 cl::opt<bool> DryRun("dry-run",
49                      cl::desc("Only pretend to run commands"));
50 cl::opt<bool> VerboseMode("v",
51                           cl::desc("Enable verbose mode"));
52
53 cl::opt<bool> CheckGraph("check-graph",
54                          cl::desc("Check the compilation graph for errors"),
55                          cl::Hidden);
56 cl::opt<bool> WriteGraph("write-graph",
57                          cl::desc("Write compilation-graph.dot file"),
58                          cl::Hidden);
59 cl::opt<bool> ViewGraph("view-graph",
60                          cl::desc("Show compilation graph in GhostView"),
61                          cl::Hidden);
62 cl::opt<bool> SaveTemps("save-temps",
63                          cl::desc("Keep temporary files"),
64                          cl::Hidden);
65
66 namespace {
67   /// BuildTargets - A small wrapper for CompilationGraph::Build.
68   int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
69     int ret;
70     const sys::Path& tempDir = SaveTemps
71       ? sys::Path("")
72       : sys::Path(sys::Path::GetTemporaryDirectory());
73
74     try {
75       ret = graph.Build(tempDir, langMap);
76     }
77     catch(...) {
78       tempDir.eraseFromDisk(true);
79       throw;
80     }
81
82     if (!SaveTemps)
83       tempDir.eraseFromDisk(true);
84     return ret;
85   }
86 }
87
88 int main(int argc, char** argv) {
89   try {
90     LanguageMap langMap;
91     CompilationGraph graph;
92
93     cl::ParseCommandLineOptions
94       (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
95
96     PluginLoader Plugins;
97     Plugins.PopulateLanguageMap(langMap);
98     Plugins.PopulateCompilationGraph(graph);
99
100     if (CheckGraph) {
101       int ret = graph.Check();
102       if (!ret)
103         std::cerr << "check-graph: no errors found.\n";
104
105       return ret;
106     }
107
108     if (ViewGraph) {
109       graph.viewGraph();
110       if (!WriteGraph)
111         return 0;
112     }
113
114     if (WriteGraph) {
115       graph.writeGraph(OutputFilename.empty()
116                        ? std::string("compilation-graph.dot")
117                        : OutputFilename);
118       return 0;
119     }
120
121     if (InputFilenames.empty()) {
122       throw std::runtime_error("no input files");
123     }
124
125     return BuildTargets(graph, langMap);
126   }
127   catch(llvmc::error_code& ec) {
128     return ec.code();
129   }
130   catch(const std::exception& ex) {
131     std::cerr << argv[0] << ": " << ex.what() << '\n';
132   }
133   catch(...) {
134     std::cerr << argv[0] << ": unknown error!\n";
135   }
136   return 1;
137 }
138
139 #endif // LLVM_INCLUDE_COMPILER_DRIVER_MAIN_INC