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