blob: 880a26acf7019a28392b7485dd9715f1ab4343a3 [file] [log] [blame]
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00001//===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000010// This is the entry point to the clang driver; it is a thin wrapper
11// for functionality in the Driver clang library.
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Driver/Compilation.h"
16#include "clang/Driver/Driver.h"
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000017#include "clang/Driver/Option.h"
18#include "clang/Driver/Options.h"
19
Daniel Dunbar510d7322009-03-18 02:11:26 +000020#include "llvm/ADT/SmallString.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000021#include "llvm/ADT/OwningPtr.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000022#include "llvm/Config/config.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000023#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbaraf07f932009-03-31 17:35:15 +000026#include "llvm/System/Host.h"
Daniel Dunbar365c02f2009-03-10 20:52:46 +000027#include "llvm/System/Path.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000028#include "llvm/System/Signals.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000029using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000030using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000031
Daniel Dunbar510d7322009-03-18 02:11:26 +000032class DriverDiagnosticPrinter : public DiagnosticClient {
33 std::string ProgName;
34 llvm::raw_ostream &OS;
35
36public:
37 DriverDiagnosticPrinter(const std::string _ProgName,
38 llvm::raw_ostream &_OS)
39 : ProgName(_ProgName),
40 OS(_OS) {}
41
42 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
43 const DiagnosticInfo &Info);
44};
45
46void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
47 const DiagnosticInfo &Info) {
48 OS << ProgName << ": ";
49
50 switch (Level) {
51 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
52 case Diagnostic::Note: OS << "note: "; break;
53 case Diagnostic::Warning: OS << "warning: "; break;
54 case Diagnostic::Error: OS << "error: "; break;
55 case Diagnostic::Fatal: OS << "fatal error: "; break;
56 }
57
58 llvm::SmallString<100> OutStr;
59 Info.FormatDiagnostic(OutStr);
60 OS.write(OutStr.begin(), OutStr.size());
61 OS << '\n';
62}
63
Daniel Dunbar734932c2009-03-18 20:25:53 +000064llvm::sys::Path GetExecutablePath(const char *Argv0) {
65 // This just needs to be some symbol in the binary; C++ doesn't
66 // allow taking the address of ::main however.
67 void *P = (void*) (intptr_t) GetExecutablePath;
68 return llvm::sys::Path::GetMainExecutable(Argv0, P);
69}
70
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000071int main(int argc, const char **argv) {
72 llvm::sys::PrintStackTraceOnErrorSignal();
Daniel Dunbar8f25c792009-03-18 01:38:48 +000073 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000074
Daniel Dunbar734932c2009-03-18 20:25:53 +000075 llvm::sys::Path Path = GetExecutablePath(argv[0]);
Daniel Dunbar510d7322009-03-18 02:11:26 +000076 llvm::OwningPtr<DiagnosticClient>
77 DiagClient(new DriverDiagnosticPrinter(Path.getBasename(), llvm::errs()));
78
79 Diagnostic Diags(DiagClient.get());
80
Daniel Dunbaraf07f932009-03-31 17:35:15 +000081 llvm::OwningPtr<Driver>
82 TheDriver(new Driver(Path.getBasename().c_str(), Path.getDirname().c_str(),
83 llvm::sys::getHostTriple().c_str(),
84 "a.out", Diags));
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000085
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000086 llvm::OwningPtr<Compilation> C(TheDriver->BuildCompilation(argc, argv));
87
Daniel Dunbaraf96def2009-03-21 00:40:53 +000088 int Res = 0;
89 if (C.get())
90 Res = C->Execute();
Daniel Dunbar8f25c792009-03-18 01:38:48 +000091
92 llvm::llvm_shutdown();
93
Daniel Dunbaraf96def2009-03-21 00:40:53 +000094 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000095}
Daniel Dunbar7ec3daf2009-03-24 03:07:05 +000096