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-04-17 09:54:00 +08:00
|
|
|
static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
|
|
|
|
const std::string &S) {
|
|
|
|
return SavedStrings.insert(S).first->c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ApplyQAOverride - Apply a list of edits to the input argument lists.
|
|
|
|
///
|
|
|
|
/// The input string is a space separate list of edits to perform,
|
|
|
|
/// they are applied in order to the input argument lists. Edits
|
|
|
|
/// should be one of the following forms:
|
|
|
|
///
|
2009-07-17 05:32:51 +08:00
|
|
|
/// '#': Silence information about the changes to the command line arguments.
|
|
|
|
///
|
2009-04-17 09:54:00 +08:00
|
|
|
/// '^': Add FOO as a new argument at the beginning of the command line.
|
|
|
|
///
|
|
|
|
/// '+': Add FOO as a new argument at the end of the command line.
|
|
|
|
///
|
|
|
|
/// 's/XXX/YYY/': Replace the literal argument XXX by YYY in the
|
|
|
|
/// command line.
|
|
|
|
///
|
|
|
|
/// 'xOPTION': Removes all instances of the literal argument OPTION.
|
|
|
|
///
|
|
|
|
/// 'XOPTION': Removes all instances of the literal argument OPTION,
|
|
|
|
/// and the following argument.
|
|
|
|
///
|
|
|
|
/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
|
|
|
|
/// at the end of the command line.
|
2009-07-17 05:32:51 +08:00
|
|
|
///
|
|
|
|
/// \param OS - The stream to write edit information to.
|
|
|
|
/// \param Args - The vector of command line arguments.
|
|
|
|
/// \param Edit - The override command to perform.
|
|
|
|
/// \param SavedStrings - Set to use for storing string representations.
|
|
|
|
void ApplyOneQAOverride(llvm::raw_ostream &OS,
|
|
|
|
std::vector<const char*> &Args,
|
2009-04-17 09:54:00 +08:00
|
|
|
const std::string &Edit,
|
|
|
|
std::set<std::string> &SavedStrings) {
|
|
|
|
// This does not need to be efficient.
|
|
|
|
|
|
|
|
if (Edit[0] == '^') {
|
|
|
|
const char *Str =
|
|
|
|
SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Adding argument " << Str << " at beginning\n";
|
2009-04-17 09:54:00 +08:00
|
|
|
Args.insert(Args.begin() + 1, Str);
|
|
|
|
} else if (Edit[0] == '+') {
|
|
|
|
const char *Str =
|
|
|
|
SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Adding argument " << Str << " at end\n";
|
2009-04-17 09:54:00 +08:00
|
|
|
Args.push_back(Str);
|
|
|
|
} else if (Edit[0] == 'x' || Edit[0] == 'X') {
|
|
|
|
std::string Option = Edit.substr(1, std::string::npos);
|
|
|
|
for (unsigned i = 1; i < Args.size();) {
|
|
|
|
if (Option == Args[i]) {
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Deleting argument " << Args[i] << '\n';
|
2009-04-17 09:54:00 +08:00
|
|
|
Args.erase(Args.begin() + i);
|
|
|
|
if (Edit[0] == 'X') {
|
|
|
|
if (i < Args.size()) {
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Deleting argument " << Args[i] << '\n';
|
2009-04-17 09:54:00 +08:00
|
|
|
Args.erase(Args.begin() + i);
|
|
|
|
} else
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Invalid X edit, end of command line!\n";
|
2009-04-17 09:54:00 +08:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
} else if (Edit[0] == 'O') {
|
|
|
|
for (unsigned i = 1; i < Args.size();) {
|
|
|
|
const char *A = Args[i];
|
|
|
|
if (A[0] == '-' && A[1] == 'O' &&
|
|
|
|
(A[2] == '\0' ||
|
|
|
|
(A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
|
|
|
|
('0' <= A[2] && A[2] <= '9'))))) {
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Deleting argument " << Args[i] << '\n';
|
2009-04-17 09:54:00 +08:00
|
|
|
Args.erase(Args.begin() + i);
|
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Adding argument " << Edit << " at end\n";
|
2009-04-17 09:54:00 +08:00
|
|
|
Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit));
|
|
|
|
} else {
|
2009-07-17 05:32:51 +08:00
|
|
|
OS << "### Unrecognized edit: " << Edit << "\n";
|
2009-04-17 09:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ApplyQAOverride - Apply a comma separate list of edits to the
|
|
|
|
/// input argument lists. See ApplyOneQAOverride.
|
|
|
|
void ApplyQAOverride(std::vector<const char*> &Args, const char *OverrideStr,
|
|
|
|
std::set<std::string> &SavedStrings) {
|
2009-07-17 05:32:51 +08:00
|
|
|
llvm::raw_ostream *OS = &llvm::errs();
|
|
|
|
|
|
|
|
if (OverrideStr[0] == '#') {
|
|
|
|
++OverrideStr;
|
|
|
|
OS = &llvm::nulls();
|
|
|
|
}
|
|
|
|
|
|
|
|
*OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
|
2009-04-17 09:54:00 +08:00
|
|
|
|
|
|
|
// This does not need to be efficient.
|
|
|
|
|
|
|
|
const char *S = OverrideStr;
|
|
|
|
while (*S) {
|
|
|
|
const char *End = ::strchr(S, ' ');
|
|
|
|
if (!End)
|
|
|
|
End = S + strlen(S);
|
|
|
|
if (End != S)
|
2009-07-17 05:32:51 +08:00
|
|
|
ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
|
2009-04-17 09:54:00 +08:00
|
|
|
S = End;
|
|
|
|
if (*S != '\0')
|
|
|
|
++S;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-27 00:15:44 +08:00
|
|
|
DriverDiagnosticPrinter DiagClient(Path.getBasename(), llvm::errs());
|
2009-03-18 10:11:26 +08:00
|
|
|
|
2009-05-27 00:15:44 +08:00
|
|
|
Diagnostic Diags(&DiagClient);
|
2009-03-18 10:11:26 +08:00
|
|
|
|
2009-05-27 00:15:44 +08:00
|
|
|
Driver TheDriver(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;
|
|
|
|
|
2009-04-17 09:54:00 +08:00
|
|
|
// Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
|
|
|
|
// command line behind the scenes.
|
2009-04-11 02:32:59 +08:00
|
|
|
std::set<std::string> SavedStrings;
|
2009-04-17 09:54:00 +08:00
|
|
|
if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
|
|
|
|
// FIXME: Driver shouldn't take extra initial argument.
|
|
|
|
std::vector<const char*> StringPointers(argv, argv + argc);
|
|
|
|
|
|
|
|
ApplyQAOverride(StringPointers, OverrideStr, SavedStrings);
|
|
|
|
|
2009-05-27 00:15:44 +08:00
|
|
|
C.reset(TheDriver.BuildCompilation(StringPointers.size(),
|
2009-05-27 02:00:25 +08:00
|
|
|
&StringPointers[0]));
|
2009-04-17 09:54:00 +08:00
|
|
|
} else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
|
2009-04-02 03:08:46 +08:00
|
|
|
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-17 09:54:00 +08:00
|
|
|
StringPointers.push_back(SaveStringInSet(SavedStrings,
|
|
|
|
std::string(Cur, Next)));
|
2009-04-02 03:08:46 +08:00
|
|
|
Cur = Next + 1;
|
|
|
|
} else {
|
2009-04-17 09:54:00 +08:00
|
|
|
if (*Cur != '\0')
|
|
|
|
StringPointers.push_back(SaveStringInSet(SavedStrings, Cur));
|
2009-04-02 03:08:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringPointers.insert(StringPointers.end(), argv + 1, argv + argc);
|
|
|
|
|
2009-05-27 00:15:44 +08:00
|
|
|
C.reset(TheDriver.BuildCompilation(StringPointers.size(),
|
2009-05-27 02:00:25 +08:00
|
|
|
&StringPointers[0]));
|
2009-04-02 03:08:46 +08:00
|
|
|
} else
|
2009-05-27 00:15:44 +08:00
|
|
|
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())
|
2009-07-02 04:03:04 +08:00
|
|
|
Res = TheDriver.ExecuteCompilation(*C);
|
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
|
|
|
|