blob: 28b2737f38b893647bd0d808ac47abff12462ba9 [file] [log] [blame]
Mikhail Glushenkov583cf312009-06-30 00:15:24 +00001//===--- 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/BuiltinOptions.h"
15#include "llvm/CompilerDriver/CompilationGraph.h"
16#include "llvm/CompilerDriver/Error.h"
17#include "llvm/CompilerDriver/Plugin.h"
18
Bill Wendling9cdd4f52009-06-30 04:07:12 +000019#include "llvm/Support/raw_ostream.h"
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000020#include "llvm/System/Path.h"
21
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000022#include <sstream>
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000023#include <string>
24
25namespace cl = llvm::cl;
26namespace sys = llvm::sys;
27using namespace llvmc;
28
29namespace {
30
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000031 std::stringstream* GlobalTimeLog;
32
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000033 int getTempDir(sys::Path& tempDir) {
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000034 // The --temp-dir option.
35 if (!TempDirname.empty()) {
Sanjiv Gupta0b8f4182009-07-09 08:23:38 +000036 tempDir = TempDirname;
Sanjiv Gupta0b8f4182009-07-09 08:23:38 +000037 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000038 // GCC 4.5-style -save-temps handling.
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000039 else if (SaveTemps == SaveTempsEnum::Unset) {
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000040 tempDir = sys::Path::GetTemporaryDirectory();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000041 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000042 }
43 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
44 tempDir = OutputFilename;
Mikhail Glushenkoveebf60c2009-07-04 14:23:32 +000045 tempDir = tempDir.getDirname();
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000046 }
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000047 else {
48 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000049 return 0;
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000050 }
51
52 if (!tempDir.exists()) {
53 std::string ErrMsg;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000054 if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
55 PrintError(ErrMsg);
56 return -1;
57 }
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000058 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000059
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000060 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000061 }
62
63 /// BuildTargets - A small wrapper for CompilationGraph::Build.
64 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
65 int ret;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000066 sys::Path tempDir;
Mikhail Glushenkov63bb60f2009-07-11 19:27:07 +000067 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000068
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000069 if (int ret = getTempDir(tempDir))
70 return ret;
71
72 ret = graph.Build(tempDir, langMap);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000073
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000074 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000075 tempDir.eraseFromDisk(true);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000076
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000077 return ret;
78 }
79}
80
81namespace llvmc {
82
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000083// Used to implement -time option. External linkage is intentional.
84void AppendToGlobalTimeLog(const std::string& cmd, double time) {
85 *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
86}
87
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000088// Sometimes plugins want to condition on the value in argv[0].
89const char* ProgramName;
90
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000091int Main(int argc, char** argv) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000092 int ret = 0;
93 LanguageMap langMap;
94 CompilationGraph graph;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000095
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000096 ProgramName = argv[0];
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000097
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000098 cl::ParseCommandLineOptions
99 (argc, argv, "LLVM Compiler Driver (Work In Progress)",
100 /* ReadResponseFiles = */ false);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000101
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000102 PluginLoader Plugins;
103 if (int ret = Plugins.RunInitialization(langMap, graph))
104 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000105
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000106 if (CheckGraph) {
107 ret = graph.Check();
108 if (!ret)
109 llvm::errs() << "check-graph: no errors found.\n";
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000110
111 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000112 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000113
114 if (ViewGraph) {
115 graph.viewGraph();
116 if (!WriteGraph)
117 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000118 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000119
120 if (WriteGraph) {
121 const std::string& Out = (OutputFilename.empty()
122 ? std::string("compilation-graph.dot")
123 : OutputFilename);
124 return graph.writeGraph(Out);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000125 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000126
127 if (Time) {
128 GlobalTimeLog = new std::stringstream;
129 GlobalTimeLog->precision(2);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000130 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000131
132 ret = BuildTargets(graph, langMap);
133
134 if (Time) {
135 llvm::errs() << GlobalTimeLog->str();
136 delete GlobalTimeLog;
137 }
138
139 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000140}
141
142} // end namespace llvmc