Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 1 | //===-- 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 Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 10 | // This is the entry point to the clang driver; it is a thin wrapper |
| 11 | // for functionality in the Driver clang library. |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Driver/Compilation.h" |
| 16 | #include "clang/Driver/Driver.h" |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Option.h" |
| 18 | #include "clang/Driver/Options.h" |
| 19 | |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 22 | #include "llvm/Config/config.h" |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
| 24 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 26 | #include "llvm/System/Path.h" |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 27 | #include "llvm/System/Signals.h" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 28 | using namespace clang; |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 29 | using namespace clang::driver; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 31 | class DriverDiagnosticPrinter : public DiagnosticClient { |
| 32 | std::string ProgName; |
| 33 | llvm::raw_ostream &OS; |
| 34 | |
| 35 | public: |
| 36 | DriverDiagnosticPrinter(const std::string _ProgName, |
| 37 | llvm::raw_ostream &_OS) |
| 38 | : ProgName(_ProgName), |
| 39 | OS(_OS) {} |
| 40 | |
| 41 | virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 42 | const DiagnosticInfo &Info); |
| 43 | }; |
| 44 | |
| 45 | void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, |
| 46 | const DiagnosticInfo &Info) { |
| 47 | OS << ProgName << ": "; |
| 48 | |
| 49 | switch (Level) { |
| 50 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
| 51 | case Diagnostic::Note: OS << "note: "; break; |
| 52 | case Diagnostic::Warning: OS << "warning: "; break; |
| 53 | case Diagnostic::Error: OS << "error: "; break; |
| 54 | case Diagnostic::Fatal: OS << "fatal error: "; break; |
| 55 | } |
| 56 | |
| 57 | llvm::SmallString<100> OutStr; |
| 58 | Info.FormatDiagnostic(OutStr); |
| 59 | OS.write(OutStr.begin(), OutStr.size()); |
| 60 | OS << '\n'; |
| 61 | } |
| 62 | |
Daniel Dunbar | 734932c | 2009-03-18 20:25:53 +0000 | [diff] [blame] | 63 | llvm::sys::Path GetExecutablePath(const char *Argv0) { |
| 64 | // This just needs to be some symbol in the binary; C++ doesn't |
| 65 | // allow taking the address of ::main however. |
| 66 | void *P = (void*) (intptr_t) GetExecutablePath; |
| 67 | return llvm::sys::Path::GetMainExecutable(Argv0, P); |
| 68 | } |
| 69 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 70 | int main(int argc, const char **argv) { |
| 71 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 72 | llvm::PrettyStackTraceProgram X(argc, argv); |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 73 | |
Daniel Dunbar | 734932c | 2009-03-18 20:25:53 +0000 | [diff] [blame] | 74 | llvm::sys::Path Path = GetExecutablePath(argv[0]); |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 75 | llvm::OwningPtr<DiagnosticClient> |
| 76 | DiagClient(new DriverDiagnosticPrinter(Path.getBasename(), llvm::errs())); |
| 77 | |
| 78 | Diagnostic Diags(DiagClient.get()); |
| 79 | |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 80 | // FIXME: Use the triple of the host, not the triple that we were |
| 81 | // compiled on. |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 82 | llvm::OwningPtr<Driver> TheDriver(new Driver(Path.getBasename().c_str(), |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 83 | Path.getDirname().c_str(), |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 84 | LLVM_HOSTTRIPLE, |
Daniel Dunbar | f353c8c | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 85 | "a.out", |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 86 | Diags)); |
| 87 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 88 | llvm::OwningPtr<Compilation> C(TheDriver->BuildCompilation(argc, argv)); |
| 89 | |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 90 | // If there were errors building the compilation, quit now. |
| 91 | if (Diags.getNumErrors()) |
| 92 | return 1; |
Daniel Dunbar | ab83543 | 2009-03-13 17:24:34 +0000 | [diff] [blame] | 93 | if (!C.get()) |
| 94 | return 0; |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 95 | |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 96 | int res = C->Execute(); |
| 97 | |
| 98 | llvm::llvm_shutdown(); |
| 99 | |
| 100 | return res; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 101 | } |