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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2009-03-03 03:59:07 +08:00
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
2012-12-26 05:56:27 +08:00
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
2015-09-26 01:44:31 +08:00
|
|
|
#include "clang/Driver/ToolChain.h"
|
2014-10-24 06:20:11 +08:00
|
|
|
#include "clang/Frontend/ChainedDiagnosticConsumer.h"
|
2015-01-14 19:23:58 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2014-10-24 06:20:11 +08:00
|
|
|
#include "clang/Frontend/SerializedDiagnosticPrinter.h"
|
2010-02-25 11:23:43 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2012-03-14 04:09:56 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2011-03-23 12:04:01 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2009-03-18 10:11:26 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-07-19 23:20:12 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-06-04 11:28:55 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2013-07-19 04:00:53 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-07-19 23:20:12 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-12-22 00:45:57 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2009-03-18 09:38:48 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2013-10-07 15:33:27 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2015-06-13 20:50:07 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2011-08-25 02:09:14 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2016-07-19 03:02:11 +08:00
|
|
|
#include <set>
|
2014-06-13 01:19:42 +08:00
|
|
|
#include <system_error>
|
2009-03-12 16:55:43 +08:00
|
|
|
using namespace clang;
|
2009-03-05 04:49:20 +08:00
|
|
|
using namespace clang::driver;
|
2013-06-15 01:17:23 +08:00
|
|
|
using namespace llvm::opt;
|
2009-03-03 03:59:07 +08:00
|
|
|
|
2013-06-26 13:03:40 +08:00
|
|
|
std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
|
2017-06-17 06:40:18 +08:00
|
|
|
if (!CanonicalPrefixes) {
|
|
|
|
SmallString<128> ExecutablePath(Argv0);
|
|
|
|
// Do a PATH lookup if Argv0 isn't a valid path.
|
|
|
|
if (!llvm::sys::fs::exists(ExecutablePath))
|
|
|
|
if (llvm::ErrorOr<std::string> P =
|
|
|
|
llvm::sys::findProgramByName(ExecutablePath))
|
|
|
|
ExecutablePath = *P;
|
|
|
|
return ExecutablePath.str();
|
|
|
|
}
|
2009-12-05 03:31:58 +08:00
|
|
|
|
2009-03-19 04:25:53 +08:00
|
|
|
// 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;
|
2013-06-26 13:03:40 +08:00
|
|
|
return llvm::sys::fs::getMainExecutable(Argv0, P);
|
2009-03-19 04:25:53 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 03:23:53 +08:00
|
|
|
static const char *GetStableCStr(std::set<std::string> &SavedStrings,
|
|
|
|
StringRef S) {
|
2009-04-17 09:54:00 +08:00
|
|
|
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.
|
|
|
|
///
|
2010-02-18 05:00:34 +08:00
|
|
|
/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
|
|
|
|
/// line.
|
2009-04-17 09:54:00 +08:00
|
|
|
///
|
|
|
|
/// '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.
|
2011-07-23 18:55:15 +08:00
|
|
|
static void ApplyOneQAOverride(raw_ostream &OS,
|
|
|
|
SmallVectorImpl<const char*> &Args,
|
|
|
|
StringRef Edit,
|
2010-03-30 13:39:52 +08:00
|
|
|
std::set<std::string> &SavedStrings) {
|
2009-04-17 09:54:00 +08:00
|
|
|
// This does not need to be efficient.
|
|
|
|
|
2009-07-18 02:10:27 +08:00
|
|
|
if (Edit[0] == '^') {
|
|
|
|
const char *Str =
|
2014-08-16 03:23:53 +08:00
|
|
|
GetStableCStr(SavedStrings, Edit.substr(1));
|
2009-07-18 02:10:27 +08:00
|
|
|
OS << "### Adding argument " << Str << " at beginning\n";
|
|
|
|
Args.insert(Args.begin() + 1, Str);
|
|
|
|
} else if (Edit[0] == '+') {
|
|
|
|
const char *Str =
|
2014-08-16 03:23:53 +08:00
|
|
|
GetStableCStr(SavedStrings, Edit.substr(1));
|
2009-07-18 02:10:27 +08:00
|
|
|
OS << "### Adding argument " << Str << " at end\n";
|
|
|
|
Args.push_back(Str);
|
2010-02-18 05:00:34 +08:00
|
|
|
} else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
|
2011-07-23 18:55:15 +08:00
|
|
|
Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos) {
|
|
|
|
StringRef MatchPattern = Edit.substr(2).split('/').first;
|
|
|
|
StringRef ReplPattern = Edit.substr(2).split('/').second;
|
2010-02-18 05:00:34 +08:00
|
|
|
ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
|
|
|
|
|
|
|
|
for (unsigned i = 1, e = Args.size(); i != e; ++i) {
|
2014-08-23 03:29:30 +08:00
|
|
|
// Ignore end-of-line response file markers
|
|
|
|
if (Args[i] == nullptr)
|
|
|
|
continue;
|
2010-02-18 05:00:34 +08:00
|
|
|
std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
|
|
|
|
|
|
|
|
if (Repl != Args[i]) {
|
|
|
|
OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
|
2014-08-16 03:23:53 +08:00
|
|
|
Args[i] = GetStableCStr(SavedStrings, Repl);
|
2010-02-18 05:00:34 +08:00
|
|
|
}
|
|
|
|
}
|
2009-07-18 02:10:27 +08:00
|
|
|
} else if (Edit[0] == 'x' || Edit[0] == 'X') {
|
2016-02-13 21:42:54 +08:00
|
|
|
auto Option = Edit.substr(1);
|
2009-07-18 02:10:27 +08:00
|
|
|
for (unsigned i = 1; i < Args.size();) {
|
|
|
|
if (Option == Args[i]) {
|
|
|
|
OS << "### Deleting argument " << Args[i] << '\n';
|
|
|
|
Args.erase(Args.begin() + i);
|
|
|
|
if (Edit[0] == 'X') {
|
|
|
|
if (i < Args.size()) {
|
|
|
|
OS << "### Deleting argument " << Args[i] << '\n';
|
|
|
|
Args.erase(Args.begin() + i);
|
|
|
|
} else
|
|
|
|
OS << "### Invalid X edit, end of command line!\n";
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
} else if (Edit[0] == 'O') {
|
|
|
|
for (unsigned i = 1; i < Args.size();) {
|
|
|
|
const char *A = Args[i];
|
2014-08-23 03:29:30 +08:00
|
|
|
// Ignore end-of-line response file markers
|
|
|
|
if (A == nullptr)
|
|
|
|
continue;
|
2009-07-18 02:10:27 +08:00
|
|
|
if (A[0] == '-' && A[1] == 'O' &&
|
|
|
|
(A[2] == '\0' ||
|
|
|
|
(A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
|
|
|
|
('0' <= A[2] && A[2] <= '9'))))) {
|
|
|
|
OS << "### Deleting argument " << Args[i] << '\n';
|
|
|
|
Args.erase(Args.begin() + i);
|
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
OS << "### Adding argument " << Edit << " at end\n";
|
2014-08-16 03:23:53 +08:00
|
|
|
Args.push_back(GetStableCStr(SavedStrings, '-' + Edit.str()));
|
2009-07-18 02:10:27 +08:00
|
|
|
} else {
|
|
|
|
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.
|
2011-07-23 18:55:15 +08:00
|
|
|
static void ApplyQAOverride(SmallVectorImpl<const char*> &Args,
|
2010-03-30 13:39:52 +08:00
|
|
|
const char *OverrideStr,
|
|
|
|
std::set<std::string> &SavedStrings) {
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream *OS = &llvm::errs();
|
2009-07-18 02:10:27 +08:00
|
|
|
|
2009-07-17 05:32:51 +08:00
|
|
|
if (OverrideStr[0] == '#') {
|
|
|
|
++OverrideStr;
|
|
|
|
OS = &llvm::nulls();
|
|
|
|
}
|
|
|
|
|
2014-01-15 09:41:52 +08:00
|
|
|
*OS << "### CCC_OVERRIDE_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:38:36 +08:00
|
|
|
extern int cc1_main(ArrayRef<const char *> Argv, const char *Argv0,
|
|
|
|
void *MainAddr);
|
|
|
|
extern int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0,
|
|
|
|
void *MainAddr);
|
2009-11-19 15:37:51 +08:00
|
|
|
|
2017-08-29 13:22:26 +08:00
|
|
|
static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
|
2015-09-26 01:44:31 +08:00
|
|
|
SmallVectorImpl<const char *> &ArgVector,
|
|
|
|
std::set<std::string> &SavedStrings) {
|
2017-09-20 23:22:27 +08:00
|
|
|
// Put target and mode arguments at the start of argument list so that
|
|
|
|
// arguments specified in command line could override them. Avoid putting
|
|
|
|
// them at index 0, as an option like '-cc1' must remain the first.
|
|
|
|
auto InsertionPoint = ArgVector.begin();
|
|
|
|
if (InsertionPoint != ArgVector.end())
|
|
|
|
++InsertionPoint;
|
|
|
|
|
2017-08-29 13:22:26 +08:00
|
|
|
if (NameParts.DriverMode) {
|
2015-09-26 01:44:31 +08:00
|
|
|
// Add the mode flag to the arguments.
|
2017-09-20 23:22:27 +08:00
|
|
|
ArgVector.insert(InsertionPoint,
|
2017-08-29 13:22:26 +08:00
|
|
|
GetStableCStr(SavedStrings, NameParts.DriverMode));
|
2015-07-16 06:42:37 +08:00
|
|
|
}
|
2014-10-18 01:07:59 +08:00
|
|
|
|
2017-08-29 13:22:26 +08:00
|
|
|
if (NameParts.TargetIsValid) {
|
|
|
|
const char *arr[] = {"-target", GetStableCStr(SavedStrings,
|
|
|
|
NameParts.TargetPrefix)};
|
2017-09-20 23:22:27 +08:00
|
|
|
ArgVector.insert(InsertionPoint, std::begin(arr), std::end(arr));
|
2011-03-17 04:15:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 02:16:32 +08:00
|
|
|
static void getCLEnvVarOptions(std::string &EnvValue, llvm::StringSaver &Saver,
|
|
|
|
SmallVectorImpl<const char *> &Opts) {
|
|
|
|
llvm::cl::TokenizeWindowsCommandLine(EnvValue, Saver, Opts);
|
|
|
|
// The first instance of '#' should be replaced with '=' in each option.
|
|
|
|
for (const char *Opt : Opts)
|
|
|
|
if (char *NumberSignPtr = const_cast<char *>(::strchr(Opt, '#')))
|
|
|
|
*NumberSignPtr = '=';
|
|
|
|
}
|
|
|
|
|
2014-08-16 04:59:03 +08:00
|
|
|
static void SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
|
2014-08-16 02:50:00 +08:00
|
|
|
// Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
|
|
|
|
TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
|
|
|
|
if (TheDriver.CCPrintOptions)
|
|
|
|
TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
|
|
|
|
|
|
|
|
// Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE.
|
|
|
|
TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS");
|
|
|
|
if (TheDriver.CCPrintHeaders)
|
|
|
|
TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE");
|
|
|
|
|
|
|
|
// Handle CC_LOG_DIAGNOSTICS and CC_LOG_DIAGNOSTICS_FILE.
|
|
|
|
TheDriver.CCLogDiagnostics = !!::getenv("CC_LOG_DIAGNOSTICS");
|
|
|
|
if (TheDriver.CCLogDiagnostics)
|
|
|
|
TheDriver.CCLogDiagnosticsFilename = ::getenv("CC_LOG_DIAGNOSTICS_FILE");
|
|
|
|
}
|
|
|
|
|
2014-08-16 02:58:09 +08:00
|
|
|
static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient,
|
|
|
|
const std::string &Path) {
|
|
|
|
// If the clang binary happens to be named cl.exe for compatibility reasons,
|
|
|
|
// use clang-cl.exe as the prefix to avoid confusion between clang and MSVC.
|
|
|
|
StringRef ExeBasename(llvm::sys::path::filename(Path));
|
|
|
|
if (ExeBasename.equals_lower("cl.exe"))
|
|
|
|
ExeBasename = "clang-cl.exe";
|
|
|
|
DiagClient->setPrefix(ExeBasename);
|
|
|
|
}
|
|
|
|
|
2014-08-16 02:58:12 +08:00
|
|
|
// This lets us create the DiagnosticsEngine with a properly-filled-out
|
|
|
|
// DiagnosticOptions instance.
|
|
|
|
static DiagnosticOptions *
|
2015-06-21 14:32:36 +08:00
|
|
|
CreateAndPopulateDiagOpts(ArrayRef<const char *> argv) {
|
2014-08-16 02:58:12 +08:00
|
|
|
auto *DiagOpts = new DiagnosticOptions;
|
|
|
|
std::unique_ptr<OptTable> Opts(createDriverOptTable());
|
|
|
|
unsigned MissingArgIndex, MissingArgCount;
|
2015-06-23 06:07:27 +08:00
|
|
|
InputArgList Args =
|
|
|
|
Opts->ParseArgs(argv.slice(1), MissingArgIndex, MissingArgCount);
|
2014-08-16 02:58:12 +08:00
|
|
|
// We ignore MissingArgCount and the return value of ParseDiagnosticArgs.
|
|
|
|
// Any errors that would be diagnosed here will also be diagnosed later,
|
|
|
|
// when the DiagnosticsEngine actually exists.
|
2015-06-23 06:07:27 +08:00
|
|
|
(void)ParseDiagnosticArgs(*DiagOpts, Args);
|
2014-08-16 02:58:12 +08:00
|
|
|
return DiagOpts;
|
|
|
|
}
|
|
|
|
|
2014-08-16 02:58:15 +08:00
|
|
|
static void SetInstallDir(SmallVectorImpl<const char *> &argv,
|
2015-08-06 01:07:33 +08:00
|
|
|
Driver &TheDriver, bool CanonicalPrefixes) {
|
2014-08-16 02:58:15 +08:00
|
|
|
// Attempt to find the original path used to invoke the driver, to determine
|
|
|
|
// the installed path. We do this manually, because we want to support that
|
|
|
|
// path being a symlink.
|
|
|
|
SmallString<128> InstalledPath(argv[0]);
|
|
|
|
|
|
|
|
// Do a PATH lookup, if there are no directory components.
|
2014-11-08 05:30:32 +08:00
|
|
|
if (llvm::sys::path::filename(InstalledPath) == InstalledPath)
|
|
|
|
if (llvm::ErrorOr<std::string> Tmp = llvm::sys::findProgramByName(
|
|
|
|
llvm::sys::path::filename(InstalledPath.str())))
|
2014-11-04 09:30:55 +08:00
|
|
|
InstalledPath = *Tmp;
|
2015-08-06 01:07:33 +08:00
|
|
|
|
|
|
|
// FIXME: We don't actually canonicalize this, we just make it absolute.
|
|
|
|
if (CanonicalPrefixes)
|
|
|
|
llvm::sys::fs::make_absolute(InstalledPath);
|
|
|
|
|
2016-01-16 06:29:34 +08:00
|
|
|
StringRef InstalledPathParent(llvm::sys::path::parent_path(InstalledPath));
|
|
|
|
if (llvm::sys::fs::exists(InstalledPathParent))
|
|
|
|
TheDriver.setInstalledDir(InstalledPathParent);
|
2014-08-16 02:58:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 05:40:51 +08:00
|
|
|
static int ExecuteCC1Tool(ArrayRef<const char *> argv, StringRef Tool) {
|
2014-08-16 05:38:36 +08:00
|
|
|
void *GetExecutablePathVP = (void *)(intptr_t) GetExecutablePath;
|
2014-08-16 03:23:47 +08:00
|
|
|
if (Tool == "")
|
2014-08-16 05:38:36 +08:00
|
|
|
return cc1_main(argv.slice(2), argv[0], GetExecutablePathVP);
|
2014-08-16 03:23:47 +08:00
|
|
|
if (Tool == "as")
|
2014-08-16 05:38:36 +08:00
|
|
|
return cc1as_main(argv.slice(2), argv[0], GetExecutablePathVP);
|
2014-08-16 03:23:47 +08:00
|
|
|
|
|
|
|
// Reject unknown tools.
|
|
|
|
llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-07-19 23:20:12 +08:00
|
|
|
int main(int argc_, const char **argv_) {
|
2016-06-09 08:53:41 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv_[0]);
|
2010-07-19 23:20:12 +08:00
|
|
|
llvm::PrettyStackTraceProgram X(argc_, argv_);
|
2016-06-09 08:53:41 +08:00
|
|
|
llvm::llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2010-07-19 23:20:12 +08:00
|
|
|
|
2014-10-07 07:52:23 +08:00
|
|
|
if (llvm::sys::Process::FixupStandardFileDescriptors())
|
|
|
|
return 1;
|
|
|
|
|
2013-10-07 15:33:27 +08:00
|
|
|
SmallVector<const char *, 256> argv;
|
|
|
|
llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
|
2014-06-12 22:02:15 +08:00
|
|
|
std::error_code EC = llvm::sys::Process::GetArgumentVector(
|
2014-08-31 00:55:39 +08:00
|
|
|
argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
|
2013-10-07 15:33:27 +08:00
|
|
|
if (EC) {
|
|
|
|
llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-26 01:44:31 +08:00
|
|
|
llvm::InitializeAllTargets();
|
2017-08-29 13:22:26 +08:00
|
|
|
auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(argv[0]);
|
2015-07-16 06:42:37 +08:00
|
|
|
|
2015-06-13 20:50:07 +08:00
|
|
|
llvm::BumpPtrAllocator A;
|
2015-08-13 09:07:06 +08:00
|
|
|
llvm::StringSaver Saver(A);
|
2009-03-03 03:59:07 +08:00
|
|
|
|
2015-07-16 06:42:37 +08:00
|
|
|
// Parse response files using the GNU syntax, unless we're in CL mode. There
|
|
|
|
// are two ways to put clang in CL compatibility mode: argv[0] is either
|
|
|
|
// clang-cl or cl, or --driver-mode=cl is on the command line. The normal
|
|
|
|
// command line parsing can't happen until after response file parsing, so we
|
|
|
|
// have to manually search for a --driver-mode=cl argument the hard way.
|
|
|
|
// Finally, our -cc1 tools don't care which tokenization mode we use because
|
|
|
|
// response files written by clang will tokenize the same way in either mode.
|
2016-04-20 08:33:06 +08:00
|
|
|
bool ClangCLMode = false;
|
2017-08-29 13:22:26 +08:00
|
|
|
if (StringRef(TargetAndMode.DriverMode).equals("--driver-mode=cl") ||
|
2015-07-16 06:42:37 +08:00
|
|
|
std::find_if(argv.begin(), argv.end(), [](const char *F) {
|
|
|
|
return F && strcmp(F, "--driver-mode=cl") == 0;
|
|
|
|
}) != argv.end()) {
|
2016-04-20 08:33:06 +08:00
|
|
|
ClangCLMode = true;
|
2015-07-16 06:42:37 +08:00
|
|
|
}
|
2016-04-26 05:15:49 +08:00
|
|
|
enum { Default, POSIX, Windows } RSPQuoting = Default;
|
|
|
|
for (const char *F : argv) {
|
|
|
|
if (strcmp(F, "--rsp-quoting=posix") == 0)
|
|
|
|
RSPQuoting = POSIX;
|
|
|
|
else if (strcmp(F, "--rsp-quoting=windows") == 0)
|
|
|
|
RSPQuoting = Windows;
|
|
|
|
}
|
2015-07-16 06:42:37 +08:00
|
|
|
|
2014-08-23 03:29:30 +08:00
|
|
|
// Determines whether we want nullptr markers in argv to indicate response
|
2016-04-20 08:33:06 +08:00
|
|
|
// files end-of-lines. We only use this for the /LINK driver argument with
|
|
|
|
// clang-cl.exe on Windows.
|
2016-04-26 05:15:49 +08:00
|
|
|
bool MarkEOLs = ClangCLMode;
|
2016-04-20 08:33:06 +08:00
|
|
|
|
2016-04-26 05:15:49 +08:00
|
|
|
llvm::cl::TokenizerCallback Tokenizer;
|
|
|
|
if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode))
|
2016-04-20 08:33:06 +08:00
|
|
|
Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
|
2016-04-26 05:15:49 +08:00
|
|
|
else
|
|
|
|
Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
|
2016-04-20 08:33:06 +08:00
|
|
|
|
|
|
|
if (MarkEOLs && argv.size() > 1 && StringRef(argv[1]).startswith("-cc1"))
|
2014-08-23 03:29:30 +08:00
|
|
|
MarkEOLs = false;
|
2015-07-16 06:42:37 +08:00
|
|
|
llvm::cl::ExpandResponseFiles(Saver, Tokenizer, argv, MarkEOLs);
|
2014-08-23 03:29:30 +08:00
|
|
|
|
|
|
|
// Handle -cc1 integrated tools, even if -cc1 was expanded from a response
|
|
|
|
// file.
|
|
|
|
auto FirstArg = std::find_if(argv.begin() + 1, argv.end(),
|
|
|
|
[](const char *A) { return A != nullptr; });
|
|
|
|
if (FirstArg != argv.end() && StringRef(*FirstArg).startswith("-cc1")) {
|
|
|
|
// If -cc1 came from a response file, remove the EOL sentinels.
|
|
|
|
if (MarkEOLs) {
|
|
|
|
auto newEnd = std::remove(argv.begin(), argv.end(), nullptr);
|
|
|
|
argv.resize(newEnd - argv.begin());
|
|
|
|
}
|
2014-08-16 05:40:51 +08:00
|
|
|
return ExecuteCC1Tool(argv, argv[1] + 4);
|
2014-08-23 03:29:30 +08:00
|
|
|
}
|
2009-11-30 15:18:13 +08:00
|
|
|
|
2009-12-05 03:31:58 +08:00
|
|
|
bool CanonicalPrefixes = true;
|
2010-07-19 23:20:12 +08:00
|
|
|
for (int i = 1, size = argv.size(); i < size; ++i) {
|
2014-08-23 03:29:30 +08:00
|
|
|
// Skip end-of-line response file markers
|
|
|
|
if (argv[i] == nullptr)
|
|
|
|
continue;
|
2011-07-23 18:55:15 +08:00
|
|
|
if (StringRef(argv[i]) == "-no-canonical-prefixes") {
|
2009-12-05 03:31:58 +08:00
|
|
|
CanonicalPrefixes = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 02:16:32 +08:00
|
|
|
// Handle CL and _CL_ which permits additional command line options to be
|
|
|
|
// prepended or appended.
|
2016-09-06 18:48:27 +08:00
|
|
|
if (ClangCLMode) {
|
2015-08-11 02:16:32 +08:00
|
|
|
// Arguments in "CL" are prepended.
|
|
|
|
llvm::Optional<std::string> OptCL = llvm::sys::Process::GetEnv("CL");
|
|
|
|
if (OptCL.hasValue()) {
|
|
|
|
SmallVector<const char *, 8> PrependedOpts;
|
|
|
|
getCLEnvVarOptions(OptCL.getValue(), Saver, PrependedOpts);
|
|
|
|
|
|
|
|
// Insert right after the program name to prepend to the argument list.
|
|
|
|
argv.insert(argv.begin() + 1, PrependedOpts.begin(), PrependedOpts.end());
|
|
|
|
}
|
|
|
|
// Arguments in "_CL_" are appended.
|
|
|
|
llvm::Optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv("_CL_");
|
|
|
|
if (Opt_CL_.hasValue()) {
|
|
|
|
SmallVector<const char *, 8> AppendedOpts;
|
|
|
|
getCLEnvVarOptions(Opt_CL_.getValue(), Saver, AppendedOpts);
|
|
|
|
|
|
|
|
// Insert at the end of the argument list to append.
|
|
|
|
argv.append(AppendedOpts.begin(), AppendedOpts.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 20:50:07 +08:00
|
|
|
std::set<std::string> SavedStrings;
|
2014-01-15 09:41:52 +08:00
|
|
|
// Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the
|
2014-02-23 08:11:56 +08:00
|
|
|
// scenes.
|
2014-01-15 09:41:52 +08:00
|
|
|
if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
|
2013-02-22 02:56:55 +08:00
|
|
|
// FIXME: Driver shouldn't take extra initial argument.
|
|
|
|
ApplyQAOverride(argv, OverrideStr, SavedStrings);
|
|
|
|
}
|
|
|
|
|
2013-06-26 13:03:40 +08:00
|
|
|
std::string Path = GetExecutablePath(argv[0], CanonicalPrefixes);
|
2009-12-05 03:31:58 +08:00
|
|
|
|
2014-08-16 02:58:12 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
|
|
|
|
CreateAndPopulateDiagOpts(argv);
|
|
|
|
|
2010-08-19 06:29:43 +08:00
|
|
|
TextDiagnosticPrinter *DiagClient
|
2012-10-24 06:26:28 +08:00
|
|
|
= new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
|
2014-08-16 02:58:09 +08:00
|
|
|
FixupDiagPrefixExeName(DiagClient, Path);
|
2013-09-04 09:37:22 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2012-03-14 04:09:56 +08:00
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
|
2014-10-24 06:20:11 +08:00
|
|
|
|
|
|
|
if (!DiagOpts->DiagnosticSerializationFile.empty()) {
|
|
|
|
auto SerializedConsumer =
|
|
|
|
clang::serialized_diags::create(DiagOpts->DiagnosticSerializationFile,
|
|
|
|
&*DiagOpts, /*MergeChildRecords=*/true);
|
|
|
|
Diags.setClient(new ChainedDiagnosticConsumer(
|
2014-11-18 07:46:02 +08:00
|
|
|
Diags.takeClient(), std::move(SerializedConsumer)));
|
2014-10-24 06:20:11 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 09:21:53 +08:00
|
|
|
ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
|
2009-03-18 10:11:26 +08:00
|
|
|
|
2014-05-16 06:26:36 +08:00
|
|
|
Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags);
|
2015-08-06 01:07:33 +08:00
|
|
|
SetInstallDir(argv, TheDriver, CanonicalPrefixes);
|
2017-08-29 13:22:26 +08:00
|
|
|
TheDriver.setTargetAndMode(TargetAndMode);
|
2010-08-02 06:29:51 +08:00
|
|
|
|
2017-08-29 13:22:26 +08:00
|
|
|
insertTargetAndModeArgs(TargetAndMode, argv, SavedStrings);
|
2011-03-07 07:31:01 +08:00
|
|
|
|
2014-08-16 04:59:03 +08:00
|
|
|
SetBackdoorDriverOutputsFromEnvVars(TheDriver);
|
2011-04-08 02:01:20 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(argv));
|
2017-05-24 22:57:17 +08:00
|
|
|
int Res = 1;
|
2017-06-30 21:21:27 +08:00
|
|
|
if (C && !C->containsError()) {
|
2017-05-24 22:57:17 +08:00
|
|
|
SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
|
2013-01-30 04:15:05 +08:00
|
|
|
Res = TheDriver.ExecuteCompilation(*C, FailingCommands);
|
2011-08-03 01:58:04 +08:00
|
|
|
|
2017-05-24 22:57:17 +08:00
|
|
|
// Force a crash to test the diagnostics.
|
|
|
|
if (TheDriver.GenReproducer) {
|
|
|
|
Diags.Report(diag::err_drv_force_crash)
|
2017-04-13 05:46:20 +08:00
|
|
|
<< !::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH");
|
2014-10-21 05:02:05 +08:00
|
|
|
|
2017-05-24 22:57:17 +08:00
|
|
|
// Pretend that every command failed.
|
|
|
|
FailingCommands.clear();
|
|
|
|
for (const auto &J : C->getJobs())
|
|
|
|
if (const Command *C = dyn_cast<Command>(&J))
|
|
|
|
FailingCommands.push_back(std::make_pair(-1, C));
|
|
|
|
}
|
2012-04-21 01:08:59 +08:00
|
|
|
|
2017-05-24 22:57:17 +08:00
|
|
|
for (const auto &P : FailingCommands) {
|
|
|
|
int CommandRes = P.first;
|
|
|
|
const Command *FailingCommand = P.second;
|
|
|
|
if (!Res)
|
|
|
|
Res = CommandRes;
|
|
|
|
|
|
|
|
// If result status is < 0, then the driver command signalled an error.
|
|
|
|
// If result status is 70, then the driver command reported a fatal error.
|
|
|
|
// On Windows, abort will return an exit code of 3. In these cases,
|
|
|
|
// generate additional diagnostic information if possible.
|
|
|
|
bool DiagnoseCrash = CommandRes < 0 || CommandRes == 70;
|
2014-07-08 04:23:27 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
2017-05-24 22:57:17 +08:00
|
|
|
DiagnoseCrash |= CommandRes == 3;
|
2014-07-08 04:23:27 +08:00
|
|
|
#endif
|
2017-05-24 22:57:17 +08:00
|
|
|
if (DiagnoseCrash) {
|
|
|
|
TheDriver.generateCompilationDiagnostics(*C, *FailingCommand);
|
|
|
|
break;
|
|
|
|
}
|
2013-01-30 04:15:05 +08:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 01:58:04 +08:00
|
|
|
|
2014-10-24 06:20:11 +08:00
|
|
|
Diags.getClient()->finish();
|
|
|
|
|
2010-03-30 13:39:52 +08:00
|
|
|
// If any timers were active but haven't been destroyed yet, print their
|
|
|
|
// results now. This happens in -disable-free mode.
|
|
|
|
llvm::TimerGroup::printAll(llvm::errs());
|
2014-10-24 06:20:11 +08:00
|
|
|
|
2014-03-13 00:07:46 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
2012-07-17 13:09:29 +08:00
|
|
|
// Exit status should not be negative on Win32, unless abnormal termination.
|
|
|
|
// Once abnormal termiation was caught, negative status should not be
|
|
|
|
// propagated.
|
|
|
|
if (Res < 0)
|
|
|
|
Res = 1;
|
|
|
|
#endif
|
|
|
|
|
2013-01-30 04:15:05 +08:00
|
|
|
// If we have multiple failing commands, we return the result of the first
|
|
|
|
// failing command.
|
2009-03-21 08:40:53 +08:00
|
|
|
return Res;
|
2009-03-03 03:59:07 +08:00
|
|
|
}
|