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/System/Path.h"
25 namespace cl = llvm::cl;
26 namespace sys = llvm::sys;
27 using namespace llvmc;
31 sys::Path getTempDir() {
34 // GCC 4.5-style -save-temps handling.
35 if (SaveTemps == SaveTempsEnum::Unset) {
36 tempDir = sys::Path::GetTemporaryDirectory();
38 else if (SaveTemps == SaveTempsEnum::Obj && !OutputFilename.empty()) {
39 tempDir = OutputFilename;
41 if (!tempDir.exists()) {
43 if (tempDir.createDirectoryOnDisk(true, &ErrMsg))
44 throw std::runtime_error(ErrMsg);
47 // else if (SaveTemps == Cwd) -> use current dir (leave tempDir empty)
52 /// BuildTargets - A small wrapper for CompilationGraph::Build.
53 int BuildTargets(CompilationGraph& graph, const LanguageMap& langMap) {
55 const sys::Path& tempDir = getTempDir();
58 ret = graph.Build(tempDir, langMap);
61 if (SaveTemps == SaveTempsEnum::Unset)
62 tempDir.eraseFromDisk(true);
66 if (SaveTemps == SaveTempsEnum::Unset)
67 tempDir.eraseFromDisk(true);
74 // Sometimes plugins want to condition on the value in argv[0].
75 const char* ProgramName;
77 int Main(int argc, char** argv) {
80 CompilationGraph graph;
82 ProgramName = argv[0];
84 cl::ParseCommandLineOptions
85 (argc, argv, "LLVM Compiler Driver (Work In Progress)", true);
88 Plugins.PopulateLanguageMap(langMap);
89 Plugins.PopulateCompilationGraph(graph);
92 int ret = graph.Check();
94 std::cerr << "check-graph: no errors found.\n";
106 graph.writeGraph(OutputFilename.empty()
107 ? std::string("compilation-graph.dot")
112 if (InputFilenames.empty()) {
113 throw std::runtime_error("no input files");
116 return BuildTargets(graph, langMap);
118 catch(llvmc::error_code& ec) {
121 catch(const std::exception& ex) {
122 std::cerr << argv[0] << ": " << ex.what() << '\n';
125 std::cerr << argv[0] << ": unknown error!\n";
130 } // end namespace llvmc