ba2d70a62031d3e79f33c9ba0e63bc9a566c39ce
[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 // TOFIX: Add a --keep-temps option.
36 // TOFIX: Write a 'driver driver' (easier to do as a separate
37 // executable that drives llvmc2 proper).
38 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
39                                      cl::ZeroOrMore);
40 cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
41                                     cl::value_desc("file"));
42 cl::list<std::string> Languages("x",
43           cl::desc("Specify the language of the following input files"),
44           cl::ZeroOrMore);
45 cl::opt<bool> VerboseMode("v",
46                           cl::desc("Enable verbose mode"));
47 cl::opt<bool> WriteGraph("write-graph",
48                          cl::desc("Write compilation-graph.dot file"),
49                          cl::Hidden);
50 cl::opt<bool> ViewGraph("view-graph",
51                          cl::desc("Show compilation graph in GhostView"),
52                          cl::Hidden);
53
54 namespace {
55   /// BuildTargets - A small wrapper for CompilationGraph::Build.
56   int BuildTargets(CompilationGraph& graph) {
57     int ret;
58     sys::Path tempDir(sys::Path::GetTemporaryDirectory());
59
60     try {
61       ret = graph.Build(tempDir);
62     }
63     catch(...) {
64       tempDir.eraseFromDisk(true);
65       throw;
66     }
67
68     tempDir.eraseFromDisk(true);
69     return ret;
70   }
71 }
72
73 int main(int argc, char** argv) {
74   try {
75     CompilationGraph graph;
76
77     cl::ParseCommandLineOptions
78       (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
79     PopulateCompilationGraph(graph);
80
81     if (WriteGraph) {
82       graph.writeGraph();
83       if (!ViewGraph)
84         return 0;
85     }
86
87     if (ViewGraph) {
88       graph.viewGraph();
89       return 0;
90     }
91
92     if (InputFilenames.empty()) {
93       throw std::runtime_error("no input files");
94     }
95
96     return BuildTargets(graph);
97   }
98   catch(llvmc::error_code& ec) {
99     return ec.code();
100   }
101   catch(const std::exception& ex) {
102     std::cerr << argv[0] << ": " << ex.what() << '\n';
103   }
104   catch(...) {
105     std::cerr << argv[0] << ": unknown error!\n";
106   }
107   return 1;
108 }