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/FileSystem.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Path.h"
26 namespace cl
= llvm::cl
;
27 namespace sys
= llvm::sys
;
28 using namespace llvmc
;
32 std::stringstream
* GlobalTimeLog
;
34 /// GetTempDir - Get the temporary directory location. Returns non-zero value
36 int GetTempDir(sys::Path
& tempDir
) {
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
= sys::path::parent_path(OutputFilename
);
50 // SaveTemps == Cwd --> use current dir (leave tempDir empty).
55 if (llvm::sys::fs::exists(tempDir
.str(), Exists
) || !Exists
) {
57 if (tempDir
.createDirectoryOnDisk(true, &ErrMsg
)) {
66 /// BuildTargets - A small wrapper for CompilationGraph::Build. Returns
67 /// non-zero value in case of error.
68 int BuildTargets(CompilationGraph
& graph
, const LanguageMap
& langMap
) {
71 bool toDelete
= (SaveTemps
== SaveTempsEnum::Unset
);
73 if (int ret
= GetTempDir(tempDir
))
76 ret
= graph
.Build(tempDir
, langMap
);
79 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 user code wants to access the argv[0] value.
93 const char* ProgramName
;
95 int Main(int argc
, char** argv
) {
98 CompilationGraph graph
;
100 ProgramName
= argv
[0];
102 cl::ParseCommandLineOptions
104 /* Overview = */ "LLVM Compiler Driver (Work In Progress)",
105 /* ReadResponseFiles = */ false);
107 if (int ret
= autogenerated::RunInitialization(langMap
, graph
))
113 llvm::errs() << "check-graph: no errors found.\n";
125 const std::string
& Out
= (OutputFilename
.empty()
126 ? std::string("compilation-graph.dot")
128 return graph
.writeGraph(Out
);
132 GlobalTimeLog
= new std::stringstream
;
133 GlobalTimeLog
->precision(2);
136 ret
= BuildTargets(graph
, langMap
);
139 llvm::errs() << GlobalTimeLog
->str();
140 delete GlobalTimeLog
;
146 } // end namespace llvmc