Add -x option (like in gcc).
[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 "Tool.h"
19
20 #include "llvm/System/Path.h"
21 #include "llvm/Support/CommandLine.h"
22
23 #include <iostream>
24 #include <stdexcept>
25 #include <string>
26
27 namespace cl = llvm::cl;
28 namespace sys = llvm::sys;
29 using namespace llvmc;
30
31 // Built-in command-line options.
32 // External linkage here is intentional.
33
34 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input file>"),
35                                      cl::ZeroOrMore);
36 cl::opt<std::string> OutputFilename("o", cl::desc("Output file name"),
37                                     cl::value_desc("file"));
38 cl::list<std::string> Languages("x",
39           cl::desc("Specify the language of the following input files"),
40           cl::ZeroOrMore);
41 cl::opt<bool> VerboseMode("v",
42                           cl::desc("Enable verbose mode"));
43 cl::opt<bool> WriteGraph("write-graph",
44                          cl::desc("Write compilation-graph.dot file"),
45                          cl::Hidden);
46 cl::opt<bool> ViewGraph("view-graph",
47                          cl::desc("Show compilation graph in GhostView"),
48                          cl::Hidden);
49
50 namespace {
51   int BuildTargets(CompilationGraph& graph) {
52     int ret;
53     sys::Path tempDir(sys::Path::GetTemporaryDirectory());
54
55     try {
56       ret = graph.Build(tempDir);
57     }
58     catch(...) {
59       tempDir.eraseFromDisk(true);
60       throw;
61     }
62
63     tempDir.eraseFromDisk(true);
64     return ret;
65   }
66 }
67
68 int main(int argc, char** argv) {
69   try {
70     CompilationGraph graph;
71
72     cl::ParseCommandLineOptions(argc, argv,
73                                 "LLVM Compiler Driver (Work In Progress)");
74     PopulateCompilationGraph(graph);
75
76     if (WriteGraph) {
77       graph.writeGraph();
78       if (!ViewGraph)
79         return 0;
80     }
81
82     if (ViewGraph) {
83       graph.viewGraph();
84       return 0;
85     }
86
87     if (InputFilenames.empty()) {
88       std::cerr << "No input files.\n";
89       return 1;
90     }
91
92     return BuildTargets(graph);
93   }
94   catch(const std::exception& ex) {
95     std::cerr << ex.what() << '\n';
96   }
97   catch(...) {
98     std::cerr << "Unknown error!\n";
99   }
100   return 1;
101 }