llvmc: remove dynamic plugins.
[oota-llvm.git] / lib / CompilerDriver / Main.cpp
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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CompilerDriver/AutoGenerated.h"
15 #include "llvm/CompilerDriver/BuiltinOptions.h"
16 #include "llvm/CompilerDriver/CompilationGraph.h"
17 #include "llvm/CompilerDriver/Error.h"
18
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/System/Path.h"
21
22 #include <sstream>
23 #include <string>
24
25 namespace cl = llvm::cl;
26 namespace sys = llvm::sys;
27 using namespace llvmc;
28
29 namespace {
30
31   std::stringstream* GlobalTimeLog;
32
33   /// GetTempDir - Get the temporary directory location. Returns non-zero value
34   /// on error.
35   int GetTempDir(sys::Path& tempDir) {
36     // The --temp-dir option.
37     if (!TempDirname.empty()) {
38       tempDir = TempDirname;
39     }
40     // GCC 4.5-style -save-temps handling.
41     else if (SaveTemps == SaveTempsEnum::Unset) {
42       tempDir = sys::Path::GetTemporaryDirectory();
43       return 0;
44     }
45     else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
46       tempDir = OutputFilename;
47       tempDir = tempDir.getDirname();
48     }
49     else {
50       // SaveTemps == Cwd --> use current dir (leave tempDir empty).
51       return 0;
52     }
53
54     if (!tempDir.exists()) {
55       std::string ErrMsg;
56       if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
57         PrintError(ErrMsg);
58         return 1;
59       }
60     }
61
62     return 0;
63   }
64
65   /// BuildTargets - A small wrapper for CompilationGraph::Build. Returns
66   /// non-zero value in case of error.
67   int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
68     int ret;
69     sys::Path tempDir;
70     bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
71
72     if (int ret = GetTempDir(tempDir))
73       return ret;
74
75     ret = graph.Build(tempDir, langMap);
76
77     if (toDelete)
78       tempDir.eraseFromDisk(true);
79
80     return ret;
81   }
82 }
83
84 namespace llvmc {
85
86 // Used to implement -time option. External linkage is intentional.
87 void AppendToGlobalTimeLog(const std::string& cmd, double time) {
88   *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
89 }
90
91 // Sometimes user code wants to access the argv[0] value.
92 const char* ProgramName;
93
94 int Main(int argc, char** argv) {
95   int ret = 0;
96   LanguageMap langMap;
97   CompilationGraph graph;
98
99   ProgramName = argv[0];
100
101   cl::ParseCommandLineOptions
102     (argc, argv,
103      /* Overview = */ "LLVM Compiler Driver (Work In Progress)",
104      /* ReadResponseFiles = */ false);
105
106   if (int ret = autogenerated::RunInitialization(langMap, graph))
107     return ret;
108
109   if (CheckGraph) {
110     ret = graph.Check();
111     if (!ret)
112       llvm::errs() << "check-graph: no errors found.\n";
113
114     return ret;
115   }
116
117   if (ViewGraph) {
118     graph.viewGraph();
119     if (!WriteGraph)
120       return 0;
121   }
122
123   if (WriteGraph) {
124     const std::string& Out = (OutputFilename.empty()
125                               ? std::string("compilation-graph.dot")
126                               : OutputFilename);
127     return graph.writeGraph(Out);
128   }
129
130   if (Time) {
131     GlobalTimeLog = new std::stringstream;
132     GlobalTimeLog->precision(2);
133   }
134
135   ret = BuildTargets(graph, langMap);
136
137   if (Time) {
138     llvm::errs() << GlobalTimeLog->str();
139     delete GlobalTimeLog;
140   }
141
142   return ret;
143 }
144
145 } // end namespace llvmc