1 //===--- Main.cpp - The LLVM Compiler Driver --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // llvmc::Main function - driver entry point.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CompilerDriver/BuiltinOptions.h"
15 #include "llvm/CompilerDriver/CompilationGraph.h"
16 #include "llvm/CompilerDriver/Error.h"
17 #include "llvm/CompilerDriver/Plugin.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/System/Path.h"
25 namespace cl = llvm::cl;
26 namespace sys = llvm::sys;
27 using namespace llvmc;
31 std::stringstream* GlobalTimeLog;
33 int getTempDir(sys::Path& tempDir) {
34 // The --temp-dir option.
35 if (!TempDirname.empty()) {
36 tempDir = TempDirname;
38 // GCC 4.5-style -save-temps handling.
39 else if (SaveTemps == SaveTempsEnum::Unset) {
40 tempDir = sys::Path::GetTemporaryDirectory();
43 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
44 tempDir = OutputFilename;
45 tempDir = tempDir.getDirname();
48 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
52 if (!tempDir.exists()) {
54 if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
63 /// BuildTargets - A small wrapper for CompilationGraph::Build.
64 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
67 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
69 if (int ret = getTempDir(tempDir))
72 ret = graph.Build(tempDir, langMap);
75 tempDir.eraseFromDisk(true);
83 // Used to implement -time option. External linkage is intentional.
84 void AppendToGlobalTimeLog(const std::string& cmd, double time) {
85 *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
88 // Sometimes plugins want to condition on the value in argv[0].
89 const char* ProgramName;
91 int Main(int argc, char** argv) {
94 CompilationGraph graph;
96 ProgramName = argv[0];
98 cl::ParseCommandLineOptions
99 (argc, argv, "LLVM Compiler Driver (Work In Progress)",
100 /* ReadResponseFiles = */ false);
102 PluginLoader Plugins;
103 if (int ret = Plugins.RunInitialization(langMap, graph))
109 llvm::errs() << "check-graph: no errors found.\n";
121 const std::string& Out = (OutputFilename.empty()
122 ? std::string("compilation-graph.dot")
124 return graph.writeGraph(Out);
128 GlobalTimeLog = new std::stringstream;
129 GlobalTimeLog->precision(2);
132 ret = BuildTargets(graph, langMap);
135 llvm::errs() << GlobalTimeLog->str();
136 delete GlobalTimeLog;
142 } // end namespace llvmc