blob: 898f16d515fc98fcc6eae62ffc3846ed63037cad [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 Glushenkov67d985f2010-07-27 11:19:36 +000033 /// GetTempDir - Get the temporary directory location. Returns non-zero value
34 /// on error.
35 int GetTempDir(sys::Path& tempDir) {
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000036 // The --temp-dir option.
37 if (!TempDirname.empty()) {
Sanjiv Gupta0b8f4182009-07-09 08:23:38 +000038 tempDir = TempDirname;
Sanjiv Gupta0b8f4182009-07-09 08:23:38 +000039 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000040 // GCC 4.5-style -save-temps handling.
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000041 else if (SaveTemps == SaveTempsEnum::Unset) {
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000042 tempDir = sys::Path::GetTemporaryDirectory();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000043 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000044 }
45 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
46 tempDir = OutputFilename;
Mikhail Glushenkoveebf60c2009-07-04 14:23:32 +000047 tempDir = tempDir.getDirname();
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000048 }
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000049 else {
50 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000051 return 0;
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000052 }
53
54 if (!tempDir.exists()) {
55 std::string ErrMsg;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000056 if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
57 PrintError(ErrMsg);
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +000058 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000059 }
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000060 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000061
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000062 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000063 }
64
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +000065 /// BuildTargets - A small wrapper for CompilationGraph::Build. Returns non-zero value
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000066 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
67 int ret;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000068 sys::Path tempDir;
Mikhail Glushenkov63bb60f2009-07-11 19:27:07 +000069 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000070
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +000071 if (int ret = GetTempDir(tempDir))
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000072 return ret;
73
74 ret = graph.Build(tempDir, langMap);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000075
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000076 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000077 tempDir.eraseFromDisk(true);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000078
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000079 return ret;
80 }
81}
82
83namespace llvmc {
84
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000085// Used to implement -time option. External linkage is intentional.
86void AppendToGlobalTimeLog(const std::string& cmd, double time) {
87 *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
88}
89
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000090// Sometimes plugins want to condition on the value in argv[0].
91const char* ProgramName;
92
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000093int Main(int argc, char** argv) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000094 int ret = 0;
95 LanguageMap langMap;
96 CompilationGraph graph;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000097
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000098 ProgramName = argv[0];
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000099
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000100 cl::ParseCommandLineOptions
101 (argc, argv, "LLVM Compiler Driver (Work In Progress)",
102 /* ReadResponseFiles = */ false);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000103
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000104 PluginLoader Plugins;
105 if (int ret = Plugins.RunInitialization(langMap, graph))
106 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000107
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000108 if (CheckGraph) {
109 ret = graph.Check();
110 if (!ret)
111 llvm::errs() << "check-graph: no errors found.\n";
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000112
113 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000114 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000115
116 if (ViewGraph) {
117 graph.viewGraph();
118 if (!WriteGraph)
119 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000120 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000121
122 if (WriteGraph) {
123 const std::string& Out = (OutputFilename.empty()
124 ? std::string("compilation-graph.dot")
125 : OutputFilename);
126 return graph.writeGraph(Out);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000127 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000128
129 if (Time) {
130 GlobalTimeLog = new std::stringstream;
131 GlobalTimeLog->precision(2);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000132 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000133
134 ret = BuildTargets(graph, langMap);
135
136 if (Time) {
137 llvm::errs() << GlobalTimeLog->str();
138 delete GlobalTimeLog;
139 }
140
141 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000142}
143
144} // end namespace llvmc