LLVM API Documentation
00001 //===--- Main.cpp - The LLVM Compiler Driver --------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open 00006 // Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // llvmc::Main function - driver entry point. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CompilerDriver/BuiltinOptions.h" 00015 #include "llvm/CompilerDriver/CompilationGraph.h" 00016 #include "llvm/CompilerDriver/Error.h" 00017 #include "llvm/CompilerDriver/Plugin.h" 00018 00019 #include "llvm/Support/raw_ostream.h" 00020 #include "llvm/System/Path.h" 00021 00022 #include <stdexcept> 00023 #include <string> 00024 00025 namespace cl = llvm::cl; 00026 namespace sys = llvm::sys; 00027 using namespace llvmc; 00028 00029 namespace { 00030 00031 sys::Path getTempDir() { 00032 sys::Path tempDir; 00033 00034 // The --temp-dir option. 00035 if (!TempDirname.empty()) { 00036 tempDir = TempDirname; 00037 } 00038 // GCC 4.5-style -save-temps handling. 00039 else if (SaveTemps == SaveTempsEnum::Unset) { 00040 tempDir = sys::Path::GetTemporaryDirectory(); 00041 return tempDir; 00042 } 00043 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) { 00044 tempDir = OutputFilename; 00045 tempDir = tempDir.getDirname(); 00046 } 00047 else { 00048 // SaveTemps == Cwd --> use current dir (leave tempDir empty). 00049 return tempDir; 00050 } 00051 00052 if (!tempDir.exists()) { 00053 std::string ErrMsg; 00054 if (tempDir.createDirectoryOnDisk(true, &ErrMsg)) 00055 throw std::runtime_error(ErrMsg); 00056 } 00057 00058 return tempDir; 00059 } 00060 00061 /// BuildTargets - A small wrapper for CompilationGraph::Build. 00062 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) { 00063 int ret; 00064 const sys::Path& tempDir = getTempDir(); 00065 bool toDelete = (SaveTemps == SaveTempsEnum::Unset); 00066 00067 try { 00068 ret = graph.Build(tempDir, langMap); 00069 } 00070 catch(...) { 00071 if (toDelete) 00072 tempDir.eraseFromDisk(true); 00073 throw; 00074 } 00075 00076 if (toDelete) 00077 tempDir.eraseFromDisk(true); 00078 return ret; 00079 } 00080 } 00081 00082 namespace llvmc { 00083 00084 // Sometimes plugins want to condition on the value in argv[0]. 00085 const char* ProgramName; 00086 00087 int Main(int argc, char** argv) { 00088 try { 00089 LanguageMap langMap; 00090 CompilationGraph graph; 00091 00092 ProgramName = argv[0]; 00093 00094 cl::ParseCommandLineOptions 00095 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true); 00096 00097 PluginLoader Plugins; 00098 Plugins.PopulateLanguageMap(langMap); 00099 Plugins.PopulateCompilationGraph(graph); 00100 00101 if (CheckGraph) { 00102 int ret = graph.Check(); 00103 if (!ret) 00104 llvm::errs() << "check-graph: no errors found.\n"; 00105 00106 return ret; 00107 } 00108 00109 if (ViewGraph) { 00110 graph.viewGraph(); 00111 if (!WriteGraph) 00112 return 0; 00113 } 00114 00115 if (WriteGraph) { 00116 graph.writeGraph(OutputFilename.empty() 00117 ? std::string("compilation-graph.dot") 00118 : OutputFilename); 00119 return 0; 00120 } 00121 00122 if (InputFilenames.empty()) { 00123 throw std::runtime_error("no input files"); 00124 } 00125 00126 return BuildTargets(graph, langMap); 00127 } 00128 catch(llvmc::error_code& ec) { 00129 return ec.code(); 00130 } 00131 catch(const std::exception& ex) { 00132 llvm::errs() << argv[0] << ": " << ex.what() << '\n'; 00133 } 00134 catch(...) { 00135 llvm::errs() << argv[0] << ": unknown error!\n"; 00136 } 00137 return 1; 00138 } 00139 00140 } // end namespace llvmc