blob: c581809d04b96d785714751a70206249815b2bcc [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 Glushenkov583cf312009-06-30 00:15:24 +000022#include <stdexcept>
23#include <string>
24
25namespace cl = llvm::cl;
26namespace sys = llvm::sys;
27using namespace llvmc;
28
29namespace {
30
31 sys::Path getTempDir() {
32 sys::Path tempDir;
33
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 Glushenkovf5f9a4d2009-07-09 19:37:17 +000041 return tempDir;
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).
49 return tempDir;
50 }
51
52 if (!tempDir.exists()) {
53 std::string ErrMsg;
54 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
55 throw std::runtime_error(ErrMsg);
56 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000057
58 return tempDir;
59 }
60
61 /// BuildTargets - A small wrapper for CompilationGraph::Build.
62 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
63 int ret;
64 const sys::Path& tempDir = getTempDir();
Mikhail Glushenkov63bb60f2009-07-11 19:27:07 +000065 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000066
67 try {
68 ret = graph.Build(tempDir, langMap);
69 }
70 catch(...) {
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000071 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000072 tempDir.eraseFromDisk(true);
73 throw;
74 }
75
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000076 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000077 tempDir.eraseFromDisk(true);
78 return ret;
79 }
80}
81
82namespace llvmc {
83
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000084// Sometimes plugins want to condition on the value in argv[0].
85const char* ProgramName;
86
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000087int Main(int argc, char** argv) {
88 try {
89 LanguageMap langMap;
90 CompilationGraph graph;
91
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000092 ProgramName = argv[0];
93
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000094 cl::ParseCommandLineOptions
95 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
96
97 PluginLoader Plugins;
Mikhail Glushenkov0a22fb62009-10-17 20:09:29 +000098 Plugins.RunInitialization(langMap, graph);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000099
100 if (CheckGraph) {
101 int ret = graph.Check();
102 if (!ret)
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000103 llvm::errs() << "check-graph: no errors found.\n";
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000104
105 return ret;
106 }
107
108 if (ViewGraph) {
109 graph.viewGraph();
110 if (!WriteGraph)
111 return 0;
112 }
113
114 if (WriteGraph) {
115 graph.writeGraph(OutputFilename.empty()
116 ? std::string("compilation-graph.dot")
117 : OutputFilename);
118 return 0;
119 }
120
121 if (InputFilenames.empty()) {
122 throw std::runtime_error("no input files");
123 }
124
125 return BuildTargets(graph, langMap);
126 }
127 catch(llvmc::error_code& ec) {
128 return ec.code();
129 }
130 catch(const std::exception& ex) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000131 llvm::errs() << argv[0] << ": " << ex.what() << '\n';
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000132 }
133 catch(...) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000134 llvm::errs() << argv[0] << ": unknown error!\n";
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000135 }
136 return 1;
137}
138
139} // end namespace llvmc