blob: fac44032518bf93ac2be916985c10b3e752764c0 [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 Dunbar365c02f2009-03-10 20:52:46 +000026#include "llvm/System/Path.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000027#include "llvm/System/Signals.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000028using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000029using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000030
Daniel Dunbar510d7322009-03-18 02:11:26 +000031class DriverDiagnosticPrinter : public DiagnosticClient {
32 std::string ProgName;
33 llvm::raw_ostream &OS;
34
35public:
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
45void 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 Dunbar3ede8d02009-03-02 19:59:07 +000063int main(int argc, const char **argv) {
64 llvm::sys::PrintStackTraceOnErrorSignal();
Daniel Dunbar8f25c792009-03-18 01:38:48 +000065 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000066
Daniel Dunbar365c02f2009-03-10 20:52:46 +000067 // FIXME: We should use GetMainExecutable here, probably, but we may
68 // want to handle symbolic links slightly differently. The problem
69 // is that the path derived from this will influence search paths.
70 llvm::sys::Path Path(argv[0]);
71
Daniel Dunbar510d7322009-03-18 02:11:26 +000072 llvm::OwningPtr<DiagnosticClient>
73 DiagClient(new DriverDiagnosticPrinter(Path.getBasename(), llvm::errs()));
74
75 Diagnostic Diags(DiagClient.get());
76
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000077 // FIXME: Use the triple of the host, not the triple that we were
78 // compiled on.
Daniel Dunbar365c02f2009-03-10 20:52:46 +000079 llvm::OwningPtr<Driver> TheDriver(new Driver(Path.getBasename().c_str(),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000080 Path.getDirname().c_str(),
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000081 LLVM_HOSTTRIPLE,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000082 "a.out",
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000083 Diags));
84
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000085 llvm::OwningPtr<Compilation> C(TheDriver->BuildCompilation(argc, argv));
86
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000087 // If there were errors building the compilation, quit now.
88 if (Diags.getNumErrors())
89 return 1;
Daniel Dunbarab835432009-03-13 17:24:34 +000090 if (!C.get())
91 return 0;
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000092
Daniel Dunbar8f25c792009-03-18 01:38:48 +000093 int res = C->Execute();
94
95 llvm::llvm_shutdown();
96
97 return res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000098}