2009-03-03 03:59:07 +08:00
|
|
|
//===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-03-03 13:55:11 +08:00
|
|
|
// This is the entry point to the clang driver; it is a thin wrapper
|
|
|
|
// for functionality in the Driver clang library.
|
2009-03-03 03:59:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
2009-03-04 16:33:23 +08:00
|
|
|
#include "clang/Driver/Option.h"
|
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
|
2009-03-18 10:11:26 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-03-03 03:59:07 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2009-03-11 07:41:59 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2009-03-18 09:38:48 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2009-03-12 16:55:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-04-01 01:35:15 +08:00
|
|
|
#include "llvm/System/Host.h"
|
2009-03-11 04:52:46 +08:00
|
|
|
#include "llvm/System/Path.h"
|
2009-03-03 03:59:07 +08:00
|
|
|
#include "llvm/System/Signals.h"
|
2009-03-12 16:55:43 +08:00
|
|
|
using namespace clang;
|
2009-03-05 04:49:20 +08:00
|
|
|
using namespace clang::driver;
|
2009-03-03 03:59:07 +08:00
|
|
|
|
2009-03-18 10:11:26 +08:00
|
|
|
class DriverDiagnosticPrinter : public DiagnosticClient {
|
|
|
|
std::string ProgName;
|
|
|
|
llvm::raw_ostream &OS;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DriverDiagnosticPrinter(const std::string _ProgName,
|
|
|
|
llvm::raw_ostream &_OS)
|
|
|
|
: ProgName(_ProgName),
|
|
|
|
OS(_OS) {}
|
|
|
|
|
|
|
|
virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
|
|
|
|
const DiagnosticInfo &Info);
|
|
|
|
};
|
|
|
|
|
|
|
|
void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
|
|
|
|
const DiagnosticInfo &Info) {
|
|
|
|
OS << ProgName << ": ";
|
|
|
|
|
|
|
|
switch (Level) {
|
|
|
|
case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
|
|
|
|
case Diagnostic::Note: OS << "note: "; break;
|
|
|
|
case Diagnostic::Warning: OS << "warning: "; break;
|
|
|
|
case Diagnostic::Error: OS << "error: "; break;
|
|
|
|
case Diagnostic::Fatal: OS << "fatal error: "; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallString<100> OutStr;
|
|
|
|
Info.FormatDiagnostic(OutStr);
|
|
|
|
OS.write(OutStr.begin(), OutStr.size());
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2009-03-19 04:25:53 +08:00
|
|
|
llvm::sys::Path GetExecutablePath(const char *Argv0) {
|
|
|
|
// This just needs to be some symbol in the binary; C++ doesn't
|
|
|
|
// allow taking the address of ::main however.
|
|
|
|
void *P = (void*) (intptr_t) GetExecutablePath;
|
|
|
|
return llvm::sys::Path::GetMainExecutable(Argv0, P);
|
|
|
|
}
|
|
|
|
|
2009-03-03 03:59:07 +08:00
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
2009-03-18 09:38:48 +08:00
|
|
|
llvm::PrettyStackTraceProgram X(argc, argv);
|
2009-03-03 03:59:07 +08:00
|
|
|
|
2009-03-19 04:25:53 +08:00
|
|
|
llvm::sys::Path Path = GetExecutablePath(argv[0]);
|
2009-03-18 10:11:26 +08:00
|
|
|
llvm::OwningPtr<DiagnosticClient>
|
|
|
|
DiagClient(new DriverDiagnosticPrinter(Path.getBasename(), llvm::errs()));
|
|
|
|
|
|
|
|
Diagnostic Diags(DiagClient.get());
|
|
|
|
|
2009-04-01 01:35:15 +08:00
|
|
|
llvm::OwningPtr<Driver>
|
|
|
|
TheDriver(new Driver(Path.getBasename().c_str(), Path.getDirname().c_str(),
|
|
|
|
llvm::sys::getHostTriple().c_str(),
|
|
|
|
"a.out", Diags));
|
2009-04-02 03:08:46 +08:00
|
|
|
|
|
|
|
llvm::OwningPtr<Compilation> C;
|
|
|
|
|
|
|
|
// Handle CCC_ADD_ARGS, a comma separated list of extra arguments.
|
2009-04-11 02:32:59 +08:00
|
|
|
std::set<std::string> SavedStrings;
|
2009-04-02 03:08:46 +08:00
|
|
|
if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
|
|
|
|
std::vector<const char*> StringPointers;
|
|
|
|
|
|
|
|
// FIXME: Driver shouldn't take extra initial argument.
|
|
|
|
StringPointers.push_back(argv[0]);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char *Next = strchr(Cur, ',');
|
|
|
|
|
|
|
|
if (Next) {
|
2009-04-02 03:38:07 +08:00
|
|
|
const char *P =
|
|
|
|
SavedStrings.insert(std::string(Cur, Next)).first->c_str();
|
|
|
|
StringPointers.push_back(P);
|
2009-04-02 03:08:46 +08:00
|
|
|
Cur = Next + 1;
|
|
|
|
} else {
|
|
|
|
if (*Cur != '\0') {
|
|
|
|
const char *P =
|
|
|
|
SavedStrings.insert(std::string(Cur)).first->c_str();
|
|
|
|
StringPointers.push_back(P);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringPointers.insert(StringPointers.end(), argv + 1, argv + argc);
|
|
|
|
|
|
|
|
C.reset(TheDriver->BuildCompilation(StringPointers.size(),
|
|
|
|
&StringPointers[0]));
|
|
|
|
} else
|
|
|
|
C.reset(TheDriver->BuildCompilation(argc, argv));
|
2009-03-03 03:59:07 +08:00
|
|
|
|
2009-03-21 08:40:53 +08:00
|
|
|
int Res = 0;
|
|
|
|
if (C.get())
|
|
|
|
Res = C->Execute();
|
2009-03-18 09:38:48 +08:00
|
|
|
|
|
|
|
llvm::llvm_shutdown();
|
|
|
|
|
2009-03-21 08:40:53 +08:00
|
|
|
return Res;
|
2009-03-03 03:59:07 +08:00
|
|
|
}
|
2009-03-24 11:07:05 +08:00
|
|
|
|