blob: 6807a830063892ed61a03afbc8926194fae5815d [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
19#include "llvm/System/Path.h"
20
21#include <iostream>
22#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
34 // GCC 4.5-style -save-temps handling.
35 if (SaveTemps == SaveTempsEnum::Unset) {
36 tempDir = sys::Path::GetTemporaryDirectory();
37 }
38 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
39 tempDir = OutputFilename;
40
41 if (!tempDir.exists()) {
42 std::string ErrMsg;
43 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
44 throw std::runtime_error(ErrMsg);
45 }
46 }
47 // else if (SaveTemps == Cwd) -> use current dir (leave tempDir empty)
48
49 return tempDir;
50 }
51
52 /// BuildTargets - A small wrapper for CompilationGraph::Build.
53 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
54 int ret;
55 const sys::Path& tempDir = getTempDir();
56
57 try {
58 ret = graph.Build(tempDir, langMap);
59 }
60 catch(...) {
61 if (SaveTemps == SaveTempsEnum::Unset)
62 tempDir.eraseFromDisk(true);
63 throw;
64 }
65
66 if (SaveTemps == SaveTempsEnum::Unset)
67 tempDir.eraseFromDisk(true);
68 return ret;
69 }
70}
71
72namespace llvmc {
73
74int Main(int argc, char** argv) {
75 try {
76 LanguageMap langMap;
77 CompilationGraph graph;
78
79 cl::ParseCommandLineOptions
80 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
81
82 PluginLoader Plugins;
83 Plugins.PopulateLanguageMap(langMap);
84 Plugins.PopulateCompilationGraph(graph);
85
86 if (CheckGraph) {
87 int ret = graph.Check();
88 if (!ret)
89 std::cerr << "check-graph: no errors found.\n";
90
91 return ret;
92 }
93
94 if (ViewGraph) {
95 graph.viewGraph();
96 if (!WriteGraph)
97 return 0;
98 }
99
100 if (WriteGraph) {
101 graph.writeGraph(OutputFilename.empty()
102 ? std::string("compilation-graph.dot")
103 : OutputFilename);
104 return 0;
105 }
106
107 if (InputFilenames.empty()) {
108 throw std::runtime_error("no input files");
109 }
110
111 return BuildTargets(graph, langMap);
112 }
113 catch(llvmc::error_code& ec) {
114 return ec.code();
115 }
116 catch(const std::exception& ex) {
117 std::cerr << argv[0] << ": " << ex.what() << '\n';
118 }
119 catch(...) {
120 std::cerr << argv[0] << ": unknown error!\n";
121 }
122 return 1;
123}
124
125} // end namespace llvmc