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 sys::Path getTempDir() {
34 /////////////////////////////////////////////
35 std::string p = "tmp-objs";
36 tempDir = sys::Path(p);
37 if (!tempDir.exists()) {
39 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
40 throw std::runtime_error(ErrMsg);
43 /////////////////////////////////////////////
45 // GCC 4.5-style -save-temps handling.
46 if (SaveTemps == SaveTempsEnum::Unset) {
47 tempDir = sys::Path::GetTemporaryDirectory();
49 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
50 tempDir = OutputFilename;
51 tempDir = tempDir.getDirname();
53 if (!tempDir.exists()) {
55 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
56 throw std::runtime_error(ErrMsg);
59 // else if (SaveTemps == Cwd) -> use current dir (leave tempDir empty)
64 /// BuildTargets - A small wrapper for CompilationGraph::Build.
65 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
67 const sys::Path& tempDir = getTempDir();
70 ret = graph.Build(tempDir, langMap);
73 if (SaveTemps == SaveTempsEnum::Unset)
74 tempDir.eraseFromDisk(true);
78 if (SaveTemps == SaveTempsEnum::Unset)
79 tempDir.eraseFromDisk(true);
86 // Sometimes plugins want to condition on the value in argv[0].
87 const char* ProgramName;
89 int Main(int argc, char** argv) {
92 CompilationGraph graph;
94 ProgramName = argv[0];
96 cl::ParseCommandLineOptions
97 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
100 Plugins.PopulateLanguageMap(langMap);
101 Plugins.PopulateCompilationGraph(graph);
104 int ret = graph.Check();
106 llvm::errs() << "check-graph: no errors found.\n";
118 graph.writeGraph(OutputFilename.empty()
119 ? std::string("compilation-graph.dot")
124 if (InputFilenames.empty()) {
125 throw std::runtime_error("no input files");
128 return BuildTargets(graph, langMap);
130 catch(llvmc::error_code& ec) {
133 catch(const std::exception& ex) {
134 llvm::errs() << argv[0] << ": " << ex.what() << '\n';
137 llvm::errs() << argv[0] << ": unknown error!\n";
142 } // end namespace llvmc