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"
26 namespace cl = llvm::cl;
27 namespace sys = llvm::sys;
28 using namespace llvmc;
32 std::stringstream* GlobalTimeLog;
34 sys::Path getTempDir() {
37 // The --temp-dir option.
38 if (!TempDirname.empty()) {
39 tempDir = TempDirname;
41 // GCC 4.5-style -save-temps handling.
42 else if (SaveTemps == SaveTempsEnum::Unset) {
43 tempDir = sys::Path::GetTemporaryDirectory();
46 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
47 tempDir = OutputFilename;
48 tempDir = tempDir.getDirname();
51 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
55 if (!tempDir.exists()) {
57 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
58 throw std::runtime_error(ErrMsg);
64 /// BuildTargets - A small wrapper for CompilationGraph::Build.
65 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
67 const sys::Path& tempDir = getTempDir();
68 bool toDelete = (SaveTemps == SaveTempsEnum::Unset);
71 ret = graph.Build(tempDir, langMap);
75 tempDir.eraseFromDisk(true);
80 tempDir.eraseFromDisk(true);
87 // Used to implement -time option. External linkage is intentional.
88 void AppendToGlobalTimeLog(const std::string& cmd, double time) {
89 *GlobalTimeLog << "# " << cmd << ' ' << time << '\n';
92 // Sometimes plugins want to condition on the value in argv[0].
93 const char* ProgramName;
95 int Main(int argc, char** argv) {
98 CompilationGraph graph;
100 ProgramName = argv[0];
102 cl::ParseCommandLineOptions
103 (argc, argv, "LLVM Compiler Driver (Work In Progress)",
104 /* ReadResponseFiles = */ false);
106 PluginLoader Plugins;
107 Plugins.RunInitialization(langMap, graph);
110 int ret = graph.Check();
112 llvm::errs() << "check-graph: no errors found.\n";
124 graph.writeGraph(OutputFilename.empty()
125 ? std::string("compilation-graph.dot")
131 GlobalTimeLog = new std::stringstream;
132 GlobalTimeLog->precision(2);
135 int ret = BuildTargets(graph, langMap);
138 llvm::errs() << GlobalTimeLog->str();
139 delete GlobalTimeLog;
144 catch(llvmc::error_code& ec) {
147 catch(const std::exception& ex) {
148 llvm::errs() << argv[0] << ": " << ex.what() << '\n';
151 llvm::errs() << argv[0] << ": unknown error!\n";
156 } // end namespace llvmc