blob: 0a6613aa77a3b4e4d1f172e88b4c4a9da01cad37 [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
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +000014#include "llvm/CompilerDriver/AutoGenerated.h"
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000015#include "llvm/CompilerDriver/BuiltinOptions.h"
16#include "llvm/CompilerDriver/CompilationGraph.h"
17#include "llvm/CompilerDriver/Error.h"
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000018
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 Glushenkovb3d36292010-08-15 07:07:12 +000065 /// BuildTargets - A small wrapper for CompilationGraph::Build. Returns
66 /// non-zero value in case of error.
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000067 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
68 int ret;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000069 sys::Path tempDir;
Mikhail Glushenkov63bb60f2009-07-11 19:27:07 +000070 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000071
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +000072 if (int ret = GetTempDir(tempDir))
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000073 return ret;
74
75 ret = graph.Build(tempDir, langMap);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000076
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000077 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000078 tempDir.eraseFromDisk(true);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000079
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000080 return ret;
81 }
82}
83
84namespace llvmc {
85
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000086// Used to implement -time option. External linkage is intentional.
87void AppendToGlobalTimeLog(const std::string& cmd, double time) {
88 *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
89}
90
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +000091// Sometimes user code wants to access the argv[0] value.
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000092const char* ProgramName;
93
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000094int Main(int argc, char** argv) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000095 int ret = 0;
96 LanguageMap langMap;
97 CompilationGraph graph;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000098
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000099 ProgramName = argv[0];
Mikhail Glushenkov875ace52009-06-30 00:16:00 +0000100
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000101 cl::ParseCommandLineOptions
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000102 (argc, argv,
103 /* Overview = */ "LLVM Compiler Driver (Work In Progress)",
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000104 /* ReadResponseFiles = */ false);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000105
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000106 if (int ret = autogenerated::RunInitialization(langMap, graph))
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000107 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000108
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000109 if (CheckGraph) {
110 ret = graph.Check();
111 if (!ret)
112 llvm::errs() << "check-graph: no errors found.\n";
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000113
114 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000115 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000116
117 if (ViewGraph) {
118 graph.viewGraph();
119 if (!WriteGraph)
120 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000121 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000122
123 if (WriteGraph) {
124 const std::string& Out = (OutputFilename.empty()
125 ? std::string("compilation-graph.dot")
126 : OutputFilename);
127 return graph.writeGraph(Out);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000128 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000129
130 if (Time) {
131 GlobalTimeLog = new std::stringstream;
132 GlobalTimeLog->precision(2);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000133 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000134
135 ret = BuildTargets(graph, langMap);
136
137 if (Time) {
138 llvm::errs() << GlobalTimeLog->str();
139 delete GlobalTimeLog;
140 }
141
142 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000143}
144
145} // end namespace llvmc