blob: 3a3487a0a9c03224f5654ee77c39580b06a1bc63 [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
103 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
104
105 PluginLoader Plugins;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +0000106 Plugins.RunInitialization(langMap, graph);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000107
108 if (CheckGraph) {
109 int ret = graph.Check();
110 if (!ret)
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000111 llvm::errs() << "check-graph: no errors found.\n";
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000112
113 return ret;
114 }
115
116 if (ViewGraph) {
117 graph.viewGraph();
118 if (!WriteGraph)
119 return 0;
120 }
121
122 if (WriteGraph) {
123 graph.writeGraph(OutputFilename.empty()
124 ? std::string("compilation-graph.dot")
125 : OutputFilename);
126 return 0;
127 }
128
129 if (InputFilenames.empty()) {
130 throw std::runtime_error("no input files");
131 }
132
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000133 if (Time) {
134 GlobalTimeLog = new std::stringstream;
135 GlobalTimeLog->precision(2);
136 }
137
138 int ret = BuildTargets(graph, langMap);
139
140 if (Time) {
141 llvm::errs() << GlobalTimeLog->str();
142 delete GlobalTimeLog;
143 }
144
145 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000146 }
147 catch(llvmc::error_code& ec) {
148 return ec.code();
149 }
150 catch(const std::exception& ex) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000151 llvm::errs() << argv[0] << ": " << ex.what() << '\n';
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000152 }
153 catch(...) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000154 llvm::errs() << argv[0] << ": unknown error!\n";
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000155 }
156 return 1;
157}
158
159} // end namespace llvmc