2011-04-05 07:16:36 +08:00
|
|
|
//===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Construct a compiler invocation object for command line driver arguments
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/Utils.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2011-04-05 07:16:36 +08:00
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
2015-07-14 07:27:56 +08:00
|
|
|
#include "clang/Driver/Action.h"
|
2011-04-05 07:16:36 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "clang/Driver/Tool.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
2011-04-05 07:16:36 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
using namespace clang;
|
2013-06-15 01:17:23 +08:00
|
|
|
using namespace llvm::opt;
|
2011-04-05 07:16:36 +08:00
|
|
|
|
|
|
|
/// createInvocationFromCommandLine - Construct a compiler invocation object for
|
|
|
|
/// a command line argument vector.
|
|
|
|
///
|
|
|
|
/// \return A CompilerInvocation, or 0 if none was built for the given
|
|
|
|
/// argument vector.
|
|
|
|
CompilerInvocation *
|
2011-07-24 01:14:25 +08:00
|
|
|
clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
|
2014-07-05 11:08:06 +08:00
|
|
|
if (!Diags.get()) {
|
2011-04-05 07:16:36 +08:00
|
|
|
// No diagnostics engine was provided, so create our own diagnostics object
|
|
|
|
// with the default options.
|
2013-01-20 09:58:28 +08:00
|
|
|
Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
|
2011-04-05 07:16:36 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 00:14:27 +08:00
|
|
|
SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
|
2011-04-05 07:16:36 +08:00
|
|
|
|
2012-05-22 04:11:54 +08:00
|
|
|
// FIXME: Find a cleaner way to force the driver into restricted modes.
|
2011-04-05 07:16:36 +08:00
|
|
|
Args.push_back("-fsyntax-only");
|
|
|
|
|
|
|
|
// FIXME: We shouldn't have to pass in the path info.
|
2015-11-19 00:14:27 +08:00
|
|
|
driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(),
|
2014-05-16 06:26:36 +08:00
|
|
|
*Diags);
|
2011-04-05 07:16:36 +08:00
|
|
|
|
|
|
|
// Don't check that inputs exist, they may have been remapped.
|
|
|
|
TheDriver.setCheckInputsExist(false);
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
|
2011-04-05 07:16:36 +08:00
|
|
|
|
|
|
|
// Just print the cc1 options if -### was present.
|
|
|
|
if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
|
2013-09-13 02:23:34 +08:00
|
|
|
C->getJobs().Print(llvm::errs(), "\n", true);
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2011-04-05 07:16:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We expect to get back exactly one command job, if we didn't something
|
2015-07-14 07:27:56 +08:00
|
|
|
// failed. CUDA compilation is an exception as it creates multiple jobs. If
|
|
|
|
// that's the case, we proceed with the first job. If caller needs particular
|
|
|
|
// CUDA job, it should be controlled via --cuda-{host|device}-only option
|
|
|
|
// passed to the driver.
|
2011-04-05 07:16:36 +08:00
|
|
|
const driver::JobList &Jobs = C->getJobs();
|
2015-07-14 07:27:56 +08:00
|
|
|
bool CudaCompilation = false;
|
|
|
|
if (Jobs.size() > 1) {
|
|
|
|
for (auto &A : C->getActions()){
|
|
|
|
// On MacOSX real actions may end up being wrapped in BindArchAction
|
|
|
|
if (isa<driver::BindArchAction>(A))
|
2016-02-24 03:30:43 +08:00
|
|
|
A = *A->input_begin();
|
2015-07-14 07:27:56 +08:00
|
|
|
if (isa<driver::CudaDeviceAction>(A)) {
|
|
|
|
CudaCompilation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
|
|
|
|
(Jobs.size() > 1 && !CudaCompilation)) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Msg;
|
2011-04-05 07:16:36 +08:00
|
|
|
llvm::raw_svector_ostream OS(Msg);
|
2013-09-13 02:23:34 +08:00
|
|
|
Jobs.Print(OS, "; ", true);
|
2011-04-05 07:16:36 +08:00
|
|
|
Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2011-04-05 07:16:36 +08:00
|
|
|
}
|
|
|
|
|
2014-10-03 09:04:53 +08:00
|
|
|
const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
|
2014-09-05 00:04:28 +08:00
|
|
|
if (StringRef(Cmd.getCreator().getName()) != "clang") {
|
2011-04-05 07:16:36 +08:00
|
|
|
Diags->Report(diag::err_fe_expected_clang_command);
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2011-04-05 07:16:36 +08:00
|
|
|
}
|
|
|
|
|
2014-09-05 00:04:28 +08:00
|
|
|
const ArgStringList &CCArgs = Cmd.getArguments();
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
|
2011-12-23 11:05:38 +08:00
|
|
|
if (!CompilerInvocation::CreateFromArgs(*CI,
|
2011-04-05 07:16:36 +08:00
|
|
|
const_cast<const char **>(CCArgs.data()),
|
|
|
|
const_cast<const char **>(CCArgs.data()) +
|
|
|
|
CCArgs.size(),
|
2011-12-23 11:05:38 +08:00
|
|
|
*Diags))
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2014-03-08 03:33:25 +08:00
|
|
|
return CI.release();
|
2011-04-05 07:16:36 +08:00
|
|
|
}
|