Return -1 only on failure to execute a program.
[oota-llvm.git] / lib / CompilerDriver / Main.cpp
1 //===--- 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/Support/raw_ostream.h"
20 #include "llvm/System/Path.h"
21
22 #include <sstream>
23 #include <string>
24
25 namespace cl = llvm::cl;
26 namespace sys = llvm::sys;
27 using namespace llvmc;
28
29 namespace {
30
31   std::stringstream* GlobalTimeLog;
32
33   /// GetTempDir - Get the temporary directory location. Returns non-zero value
34   /// on error.
35   int GetTempDir(sys::Path& tempDir) {
36     // The --temp-dir option.
37     if (!TempDirname.empty()) {
38       tempDir = TempDirname;
39     }
40     // GCC 4.5-style -save-temps handling.
41     else if (SaveTemps == SaveTempsEnum::Unset) {
42       tempDir = sys::Path::GetTemporaryDirectory();
43       return 0;
44     }
45     else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
46       tempDir = OutputFilename;
47       tempDir = tempDir.getDirname();
48     }
49     else {
50       // SaveTemps == Cwd --> use current dir (leave tempDir empty).
51       return 0;
52     }
53
54     if (!tempDir.exists()) {
55       std::string ErrMsg;
56       if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
57         PrintError(ErrMsg);
58         return 1;
59       }
60     }
61
62     return 0;
63   }
64
65   /// BuildTargets - A small wrapper for CompilationGraph::Build. Returns non-zero value
66   int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
67     int ret;
68     sys::Path tempDir;
69     bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
70
71     if (int ret = GetTempDir(tempDir))
72       return ret;
73
74     ret = graph.Build(tempDir, langMap);
75
76     if (toDelete)
77       tempDir.eraseFromDisk(true);
78
79     return ret;
80   }
81 }
82
83 namespace llvmc {
84
85 // Used to implement -time option. External linkage is intentional.
86 void AppendToGlobalTimeLog(const std::string& cmd, double time) {
87   *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
88 }
89
90 // Sometimes plugins want to condition on the value in argv[0].
91 const char* ProgramName;
92
93 int Main(int argc, char** argv) {
94   int ret = 0;
95   LanguageMap langMap;
96   CompilationGraph graph;
97
98   ProgramName = argv[0];
99
100   cl::ParseCommandLineOptions
101     (argc, argv, "LLVM Compiler Driver (Work In Progress)",
102      /* ReadResponseFiles = */ false);
103
104   PluginLoader Plugins;
105   if (int ret = Plugins.RunInitialization(langMap, graph))
106     return ret;
107
108   if (CheckGraph) {
109     ret = graph.Check();
110     if (!ret)
111       llvm::errs() << "check-graph: no errors found.\n";
112
113     return ret;
114   }
115
116   if (ViewGraph) {
117     graph.viewGraph();
118     if (!WriteGraph)
119       return 0;
120   }
121
122   if (WriteGraph) {
123     const std::string& Out = (OutputFilename.empty()
124                               ? std::string("compilation-graph.dot")
125                               : OutputFilename);
126     return graph.writeGraph(Out);
127   }
128
129   if (Time) {
130     GlobalTimeLog = new std::stringstream;
131     GlobalTimeLog->precision(2);
132   }
133
134   ret = BuildTargets(graph, langMap);
135
136   if (Time) {
137     llvm::errs() << GlobalTimeLog->str();
138     delete GlobalTimeLog;
139   }
140
141   return ret;
142 }
143
144 } // end namespace llvmc