add some missing quotes in debug output
[llvm/avr.git] / lib / CompilerDriver / Main.cpp
blob3e1fc9f124e6fe3aaf9fbe08462aacfaec7d2968
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 // llvmc::Main function - driver entry point.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CompilerDriver/BuiltinOptions.h"
15 #include "llvm/CompilerDriver/CompilationGraph.h"
16 #include "llvm/CompilerDriver/Error.h"
17 #include "llvm/CompilerDriver/Plugin.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/System/Path.h"
22 #include <stdexcept>
23 #include <string>
25 namespace cl = llvm::cl;
26 namespace sys = llvm::sys;
27 using namespace llvmc;
29 namespace {
31 sys::Path getTempDir() {
32 sys::Path tempDir;
34 // The --temp-dir option.
35 if (!TempDirname.empty()) {
36 tempDir = TempDirname;
38 // GCC 4.5-style -save-temps handling.
39 else if (SaveTemps == SaveTempsEnum::Unset) {
40 tempDir = sys::Path::GetTemporaryDirectory();
41 return tempDir;
43 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
44 tempDir = OutputFilename;
45 tempDir = tempDir.getDirname();
47 else {
48 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
49 return tempDir;
52 if (!tempDir.exists()) {
53 std::string ErrMsg;
54 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
55 throw std::runtime_error(ErrMsg);
58 return tempDir;
61 /// BuildTargets - A small wrapper for CompilationGraph::Build.
62 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
63 int ret;
64 const sys::Path& tempDir = getTempDir();
65 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
67 try {
68 ret = graph.Build(tempDir, langMap);
70 catch(...) {
71 if (toDelete)
72 tempDir.eraseFromDisk(true);
73 throw;
76 if (toDelete)
77 tempDir.eraseFromDisk(true);
78 return ret;
82 namespace llvmc {
84 // Sometimes plugins want to condition on the value in argv[0].
85 const char* ProgramName;
87 int Main(int argc, char** argv) {
88 try {
89 LanguageMap langMap;
90 CompilationGraph graph;
92 ProgramName = argv[0];
94 cl::ParseCommandLineOptions
95 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
97 PluginLoader Plugins;
98 Plugins.PopulateLanguageMap(langMap);
99 Plugins.PopulateCompilationGraph(graph);
101 if (CheckGraph) {
102 int ret = graph.Check();
103 if (!ret)
104 llvm::errs() << "check-graph: no errors found.\n";
106 return ret;
109 if (ViewGraph) {
110 graph.viewGraph();
111 if (!WriteGraph)
112 return 0;
115 if (WriteGraph) {
116 graph.writeGraph(OutputFilename.empty()
117 ? std::string("compilation-graph.dot")
118 : OutputFilename);
119 return 0;
122 if (InputFilenames.empty()) {
123 throw std::runtime_error("no input files");
126 return BuildTargets(graph, langMap);
128 catch(llvmc::error_code& ec) {
129 return ec.code();
131 catch(const std::exception& ex) {
132 llvm::errs() << argv[0] << ": " << ex.what() << '\n';
134 catch(...) {
135 llvm::errs() << argv[0] << ": unknown error!\n";
137 return 1;
140 } // end namespace llvmc