1*67e74705SXin Li //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // Construct a compiler invocation object for command line driver arguments
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li
14*67e74705SXin Li #include "clang/Frontend/Utils.h"
15*67e74705SXin Li #include "clang/Basic/DiagnosticOptions.h"
16*67e74705SXin Li #include "clang/Driver/Compilation.h"
17*67e74705SXin Li #include "clang/Driver/Driver.h"
18*67e74705SXin Li #include "clang/Driver/Action.h"
19*67e74705SXin Li #include "clang/Driver/Options.h"
20*67e74705SXin Li #include "clang/Driver/Tool.h"
21*67e74705SXin Li #include "clang/Frontend/CompilerInstance.h"
22*67e74705SXin Li #include "clang/Frontend/FrontendDiagnostic.h"
23*67e74705SXin Li #include "llvm/Option/ArgList.h"
24*67e74705SXin Li #include "llvm/Support/Host.h"
25*67e74705SXin Li using namespace clang;
26*67e74705SXin Li using namespace llvm::opt;
27*67e74705SXin Li
28*67e74705SXin Li /// createInvocationFromCommandLine - Construct a compiler invocation object for
29*67e74705SXin Li /// a command line argument vector.
30*67e74705SXin Li ///
31*67e74705SXin Li /// \return A CompilerInvocation, or 0 if none was built for the given
32*67e74705SXin Li /// argument vector.
33*67e74705SXin Li CompilerInvocation *
createInvocationFromCommandLine(ArrayRef<const char * > ArgList,IntrusiveRefCntPtr<DiagnosticsEngine> Diags)34*67e74705SXin Li clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
35*67e74705SXin Li IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
36*67e74705SXin Li if (!Diags.get()) {
37*67e74705SXin Li // No diagnostics engine was provided, so create our own diagnostics object
38*67e74705SXin Li // with the default options.
39*67e74705SXin Li Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
40*67e74705SXin Li }
41*67e74705SXin Li
42*67e74705SXin Li SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
43*67e74705SXin Li
44*67e74705SXin Li // FIXME: Find a cleaner way to force the driver into restricted modes.
45*67e74705SXin Li Args.push_back("-fsyntax-only");
46*67e74705SXin Li
47*67e74705SXin Li // FIXME: We shouldn't have to pass in the path info.
48*67e74705SXin Li driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(),
49*67e74705SXin Li *Diags);
50*67e74705SXin Li
51*67e74705SXin Li // Don't check that inputs exist, they may have been remapped.
52*67e74705SXin Li TheDriver.setCheckInputsExist(false);
53*67e74705SXin Li
54*67e74705SXin Li std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
55*67e74705SXin Li
56*67e74705SXin Li // Just print the cc1 options if -### was present.
57*67e74705SXin Li if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
58*67e74705SXin Li C->getJobs().Print(llvm::errs(), "\n", true);
59*67e74705SXin Li return nullptr;
60*67e74705SXin Li }
61*67e74705SXin Li
62*67e74705SXin Li // We expect to get back exactly one command job, if we didn't something
63*67e74705SXin Li // failed. CUDA compilation is an exception as it creates multiple jobs. If
64*67e74705SXin Li // that's the case, we proceed with the first job. If caller needs particular
65*67e74705SXin Li // CUDA job, it should be controlled via --cuda-{host|device}-only option
66*67e74705SXin Li // passed to the driver.
67*67e74705SXin Li const driver::JobList &Jobs = C->getJobs();
68*67e74705SXin Li bool CudaCompilation = false;
69*67e74705SXin Li if (Jobs.size() > 1) {
70*67e74705SXin Li for (auto &A : C->getActions()){
71*67e74705SXin Li // On MacOSX real actions may end up being wrapped in BindArchAction
72*67e74705SXin Li if (isa<driver::BindArchAction>(A))
73*67e74705SXin Li A = *A->input_begin();
74*67e74705SXin Li if (isa<driver::CudaDeviceAction>(A)) {
75*67e74705SXin Li CudaCompilation = true;
76*67e74705SXin Li break;
77*67e74705SXin Li }
78*67e74705SXin Li }
79*67e74705SXin Li }
80*67e74705SXin Li if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
81*67e74705SXin Li (Jobs.size() > 1 && !CudaCompilation)) {
82*67e74705SXin Li SmallString<256> Msg;
83*67e74705SXin Li llvm::raw_svector_ostream OS(Msg);
84*67e74705SXin Li Jobs.Print(OS, "; ", true);
85*67e74705SXin Li Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
86*67e74705SXin Li return nullptr;
87*67e74705SXin Li }
88*67e74705SXin Li
89*67e74705SXin Li const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
90*67e74705SXin Li if (StringRef(Cmd.getCreator().getName()) != "clang") {
91*67e74705SXin Li Diags->Report(diag::err_fe_expected_clang_command);
92*67e74705SXin Li return nullptr;
93*67e74705SXin Li }
94*67e74705SXin Li
95*67e74705SXin Li const ArgStringList &CCArgs = Cmd.getArguments();
96*67e74705SXin Li std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation());
97*67e74705SXin Li if (!CompilerInvocation::CreateFromArgs(*CI,
98*67e74705SXin Li const_cast<const char **>(CCArgs.data()),
99*67e74705SXin Li const_cast<const char **>(CCArgs.data()) +
100*67e74705SXin Li CCArgs.size(),
101*67e74705SXin Li *Diags))
102*67e74705SXin Li return nullptr;
103*67e74705SXin Li return CI.release();
104*67e74705SXin Li }
105