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/AutoGenerated.h"
15 #include "llvm/CompilerDriver/BuiltinOptions.h"
16 #include "llvm/CompilerDriver/CompilationGraph.h"
17 #include "llvm/CompilerDriver/Error.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/Path.h"
25 namespace cl = llvm::cl;
26 namespace sys = llvm::sys;
27 using namespace llvmc;
31 std::stringstream* GlobalTimeLog;
33 /// GetTempDir - Get the temporary directory location. Returns non-zero value
35 int GetTempDir(sys::Path& tempDir) {
36 // The --temp-dir option.
37 if (!TempDirname.empty()) {
38 tempDir = TempDirname;
40 // GCC 4.5-style -save-temps handling.
41 else if (SaveTemps == SaveTempsEnum::Unset) {
42 tempDir = sys::Path::GetTemporaryDirectory();
45 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
46 tempDir = OutputFilename;
47 tempDir = tempDir.getDirname();
50 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
54 if (!tempDir.exists()) {
56 if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) {
65 /// BuildTargets - A small wrapper for CompilationGraph::Build. Returns
66 /// non-zero value in case of error.
67 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
70 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
72 if (int ret = GetTempDir(tempDir))
75 ret = graph.Build(tempDir, langMap);
78 tempDir.eraseFromDisk(true);
86 // Used to implement -time option. External linkage is intentional.
87 void AppendToGlobalTimeLog(const std::string& cmd, double time) {
88 *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
91 // Sometimes user code wants to access the argv[0] value.
92 const char* ProgramName;
94 int Main(int argc, char** argv) {
97 CompilationGraph graph;
99 ProgramName = argv[0];
101 cl::ParseCommandLineOptions
103 /* Overview = */ "LLVM Compiler Driver (Work In Progress)",
104 /* ReadResponseFiles = */ false);
106 if (int ret = autogenerated::RunInitialization(langMap, graph))
112 llvm::errs() << "check-graph: no errors found.\n";
124 const std::string& Out = (OutputFilename.empty()
125 ? std::string("compilation-graph.dot")
127 return graph.writeGraph(Out);
131 GlobalTimeLog = new std::stringstream;
132 GlobalTimeLog->precision(2);
135 ret = BuildTargets(graph, langMap);
138 llvm::errs() << GlobalTimeLog->str();
139 delete GlobalTimeLog;
145 } // end namespace llvmc