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