Add a --dry-run option to llvmc2. Patch by Holger Schurig.
[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: Write a 'driver driver' (easier to do as a separate
36 // executable that drives llvmc2 proper).
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) {
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);
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     CompilationGraph graph;
83
84     cl::ParseCommandLineOptions
85       (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
86     PopulateCompilationGraph(graph);
87
88     if (WriteGraph) {
89       graph.writeGraph();
90       if (!ViewGraph)
91         return 0;
92     }
93
94     if (ViewGraph) {
95       graph.viewGraph();
96       return 0;
97     }
98
99     if (InputFilenames.empty()) {
100       throw std::runtime_error("no input files");
101     }
102
103     return BuildTargets(graph);
104   }
105   catch(llvmc::error_code& ec) {
106     return ec.code();
107   }
108   catch(const std::exception& ex) {
109     std::cerr << argv[0] << ": " << ex.what() << '\n';
110   }
111   catch(...) {
112     std::cerr << argv[0] << ": unknown error!\n";
113   }
114   return 1;
115 }