blob: b5e507dfc3a37e70d7d8da8bf86a853cd21caa36 [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 <stdexcept>
24#include <string>
25
26namespace cl = llvm::cl;
27namespace sys = llvm::sys;
28using namespace llvmc;
29
30namespace {
31
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000032 std::stringstream* GlobalTimeLog;
33
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000034 sys::Path getTempDir() {
35 sys::Path tempDir;
36
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000037 // The --temp-dir option.
38 if (!TempDirname.empty()) {
Sanjiv Gupta0b8f4182009-07-09 08:23:38 +000039 tempDir = TempDirname;
Sanjiv Gupta0b8f4182009-07-09 08:23:38 +000040 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000041 // GCC 4.5-style -save-temps handling.
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000042 else if (SaveTemps == SaveTempsEnum::Unset) {
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000043 tempDir = sys::Path::GetTemporaryDirectory();
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000044 return tempDir;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000045 }
46 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
47 tempDir = OutputFilename;
Mikhail Glushenkoveebf60c2009-07-04 14:23:32 +000048 tempDir = tempDir.getDirname();
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000049 }
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000050 else {
51 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
52 return tempDir;
53 }
54
55 if (!tempDir.exists()) {
56 std::string ErrMsg;
57 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
58 throw std::runtime_error(ErrMsg);
59 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000060
61 return tempDir;
62 }
63
64 /// BuildTargets - A small wrapper for CompilationGraph::Build.
65 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
66 int ret;
67 const sys::Path& tempDir = getTempDir();
Mikhail Glushenkov63bb60f2009-07-11 19:27:07 +000068 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000069
70 try {
71 ret = graph.Build(tempDir, langMap);
72 }
73 catch(...) {
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000074 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000075 tempDir.eraseFromDisk(true);
76 throw;
77 }
78
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000079 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000080 tempDir.eraseFromDisk(true);
81 return ret;
82 }
83}
84
85namespace llvmc {
86
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000087// Used to implement -time option. External linkage is intentional.
88void AppendToGlobalTimeLog(const std::string& cmd, double time) {
89 *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
90}
91
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000092// Sometimes plugins want to condition on the value in argv[0].
93const char* ProgramName;
94
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000095int Main(int argc, char** argv) {
96 try {
97 LanguageMap langMap;
98 CompilationGraph graph;
99
Mikhail Glushenkov875ace52009-06-30 00:16:00 +0000100 ProgramName = argv[0];
101
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000102 cl::ParseCommandLineOptions
Mikhail Glushenkov94f84b92010-02-23 09:05:10 +0000103 (argc, argv, "LLVM Compiler Driver (Work In Progress)",
104 /* ReadResponseFiles = */ false);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000105
106 PluginLoader Plugins;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000107 Plugins.RunInitialization(langMap, graph);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000108
109 if (CheckGraph) {
110 int ret = graph.Check();
111 if (!ret)
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000112 llvm::errs() << "check-graph: no errors found.\n";
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000113
114 return ret;
115 }
116
117 if (ViewGraph) {
118 graph.viewGraph();
119 if (!WriteGraph)
120 return 0;
121 }
122
123 if (WriteGraph) {
124 graph.writeGraph(OutputFilename.empty()
125 ? std::string("compilation-graph.dot")
126 : OutputFilename);
127 return 0;
128 }
129
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000130 if (Time) {
131 GlobalTimeLog = new std::stringstream;
132 GlobalTimeLog->precision(2);
133 }
134
135 int 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 catch(llvmc::error_code& ec) {
145 return ec.code();
146 }
147 catch(const std::exception& ex) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000148 llvm::errs() << argv[0] << ": " << ex.what() << '\n';
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000149 }
150 catch(...) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000151 llvm::errs() << argv[0] << ": unknown error!\n";
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000152 }
153 return 1;
154}
155
156} // end namespace llvmc