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 | af07f93 | 2009-03-31 17:35:15 +0000 | [diff] [blame] | 26 | #include "llvm/System/Host.h" |
Daniel Dunbar | 365c02f | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 27 | #include "llvm/System/Path.h" |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 28 | #include "llvm/System/Signals.h" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 29 | using namespace clang; |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 30 | using namespace clang::driver; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 31 | |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 32 | class DriverDiagnosticPrinter : public DiagnosticClient { |
| 33 | std::string ProgName; |
| 34 | llvm::raw_ostream &OS; |
| 35 | |
| 36 | public: |
| 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 | |
| 46 | void 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 Dunbar | 734932c | 2009-03-18 20:25:53 +0000 | [diff] [blame] | 64 | llvm::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 Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 71 | int main(int argc, const char **argv) { |
| 72 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 73 | llvm::PrettyStackTraceProgram X(argc, argv); |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | 734932c | 2009-03-18 20:25:53 +0000 | [diff] [blame] | 75 | llvm::sys::Path Path = GetExecutablePath(argv[0]); |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 76 | llvm::OwningPtr<DiagnosticClient> |
| 77 | DiagClient(new DriverDiagnosticPrinter(Path.getBasename(), llvm::errs())); |
| 78 | |
| 79 | Diagnostic Diags(DiagClient.get()); |
| 80 | |
Daniel Dunbar | af07f93 | 2009-03-31 17:35:15 +0000 | [diff] [blame] | 81 | 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 Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame^] | 85 | |
| 86 | llvm::OwningPtr<Compilation> C; |
| 87 | |
| 88 | // Handle CCC_ADD_ARGS, a comma separated list of extra arguments. |
| 89 | if (const char *Cur = ::getenv("CCC_ADD_ARGS")) { |
| 90 | std::set<std::string> SavedStrings; |
| 91 | std::vector<const char*> StringPointers; |
| 92 | |
| 93 | // FIXME: Driver shouldn't take extra initial argument. |
| 94 | StringPointers.push_back(argv[0]); |
| 95 | |
| 96 | for (;;) { |
| 97 | const char *Next = strchr(Cur, ','); |
| 98 | |
| 99 | if (Next) { |
| 100 | if (Cur != Next) { |
| 101 | const char *P = |
| 102 | SavedStrings.insert(std::string(Cur, Next)).first->c_str(); |
| 103 | StringPointers.push_back(P); |
| 104 | } |
| 105 | Cur = Next + 1; |
| 106 | } else { |
| 107 | if (*Cur != '\0') { |
| 108 | const char *P = |
| 109 | SavedStrings.insert(std::string(Cur)).first->c_str(); |
| 110 | StringPointers.push_back(P); |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | StringPointers.insert(StringPointers.end(), argv + 1, argv + argc); |
| 117 | |
| 118 | C.reset(TheDriver->BuildCompilation(StringPointers.size(), |
| 119 | &StringPointers[0])); |
| 120 | } else |
| 121 | C.reset(TheDriver->BuildCompilation(argc, argv)); |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 122 | |
Daniel Dunbar | af96def | 2009-03-21 00:40:53 +0000 | [diff] [blame] | 123 | int Res = 0; |
| 124 | if (C.get()) |
| 125 | Res = C->Execute(); |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 126 | |
| 127 | llvm::llvm_shutdown(); |
| 128 | |
Daniel Dunbar | af96def | 2009-03-21 00:40:53 +0000 | [diff] [blame] | 129 | return Res; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 130 | } |
Daniel Dunbar | 7ec3daf | 2009-03-24 03:07:05 +0000 | [diff] [blame] | 131 | |