blob: 7120027f7ce0e84668115eda8862835cdf2ed0d0 [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
Michael J. Spencer54453f22011-01-10 02:34:23 +000019#include "llvm/Support/FileSystem.h"
Bill Wendling9cdd4f52009-06-30 04:07:12 +000020#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000021#include "llvm/Support/Path.h"
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000022
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +000023#include <sstream>
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000024#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 Glushenkov67d985f2010-07-27 11:19:36 +000034 /// GetTempDir - Get the temporary directory location. Returns non-zero value
35 /// on error.
36 int GetTempDir(sys::Path& tempDir) {
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 Glushenkovb374d4f2010-07-23 03:42:55 +000044 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000045 }
46 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
Michael J. Spencer73855092010-12-18 22:23:07 +000047 tempDir = sys::path::parent_path(OutputFilename);
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
Michael J. Spencer54453f22011-01-10 02:34:23 +000054 bool Exists;
55 if (llvm::sys::fs::exists(tempDir.str(), Exists) || !Exists) {
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000056 std::string ErrMsg;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000057 if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
58 PrintError(ErrMsg);
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +000059 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000060 }
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000061 }
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000062
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000063 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000064 }
65
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +000066 /// BuildTargets - A small wrapper for CompilationGraph::Build. Returns
67 /// non-zero value in case of error.
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000068 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
69 int ret;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000070 sys::Path tempDir;
Mikhail Glushenkov63bb60f2009-07-11 19:27:07 +000071 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000072
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +000073 if (int ret = GetTempDir(tempDir))
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000074 return ret;
75
76 ret = graph.Build(tempDir, langMap);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000077
Mikhail Glushenkovf5f9a4d2009-07-09 19:37:17 +000078 if (toDelete)
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000079 tempDir.eraseFromDisk(true);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000080
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000081 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 Glushenkovb3d36292010-08-15 07:07:12 +000092// Sometimes user code wants to access the argv[0] value.
Mikhail Glushenkov875ace52009-06-30 00:16:00 +000093const char* ProgramName;
94
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000095int Main(int argc, char** argv) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000096 int ret = 0;
97 LanguageMap langMap;
98 CompilationGraph graph;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +000099
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000100 ProgramName = argv[0];
Mikhail Glushenkov875ace52009-06-30 00:16:00 +0000101
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000102 cl::ParseCommandLineOptions
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000103 (argc, argv,
104 /* Overview = */ "LLVM Compiler Driver (Work In Progress)",
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000105 /* ReadResponseFiles = */ false);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000106
Mikhail Glushenkovb3d36292010-08-15 07:07:12 +0000107 if (int ret = autogenerated::RunInitialization(langMap, graph))
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000108 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000109
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000110 if (CheckGraph) {
111 ret = graph.Check();
112 if (!ret)
113 llvm::errs() << "check-graph: no errors found.\n";
Mikhail Glushenkov6533afe2009-11-07 06:33:58 +0000114
115 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000116 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000117
118 if (ViewGraph) {
119 graph.viewGraph();
120 if (!WriteGraph)
121 return 0;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000122 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000123
124 if (WriteGraph) {
125 const std::string& Out = (OutputFilename.empty()
126 ? std::string("compilation-graph.dot")
127 : OutputFilename);
128 return graph.writeGraph(Out);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000129 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000130
131 if (Time) {
132 GlobalTimeLog = new std::stringstream;
133 GlobalTimeLog->precision(2);
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000134 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000135
136 ret = BuildTargets(graph, langMap);
137
138 if (Time) {
139 llvm::errs() << GlobalTimeLog->str();
140 delete GlobalTimeLog;
141 }
142
143 return ret;
Mikhail Glushenkov583cf312009-06-30 00:15:24 +0000144}
145
146} // end namespace llvmc