10b57cec5SDimitry Andric //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Construct a compiler invocation object for command line driver arguments
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h"
14fe6060f1SDimitry Andric #include "clang/Driver/Action.h"
150b57cec5SDimitry Andric #include "clang/Driver/Compilation.h"
160b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
170b57cec5SDimitry Andric #include "clang/Driver/Options.h"
180b57cec5SDimitry Andric #include "clang/Driver/Tool.h"
190b57cec5SDimitry Andric #include "clang/Frontend/CompilerInstance.h"
200b57cec5SDimitry Andric #include "clang/Frontend/FrontendDiagnostic.h"
21fe6060f1SDimitry Andric #include "clang/Frontend/Utils.h"
22fe6060f1SDimitry Andric #include "llvm/ADT/STLExtras.h"
23fe6060f1SDimitry Andric #include "llvm/ADT/StringRef.h"
240b57cec5SDimitry Andric #include "llvm/Option/ArgList.h"
25*06c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h"
260b57cec5SDimitry Andric using namespace clang;
270b57cec5SDimitry Andric using namespace llvm::opt;
280b57cec5SDimitry Andric
2981ad6265SDimitry Andric std::unique_ptr<CompilerInvocation>
createInvocation(ArrayRef<const char * > ArgList,CreateInvocationOptions Opts)3081ad6265SDimitry Andric clang::createInvocation(ArrayRef<const char *> ArgList,
3181ad6265SDimitry Andric CreateInvocationOptions Opts) {
32349cc55cSDimitry Andric assert(!ArgList.empty());
3381ad6265SDimitry Andric auto Diags = Opts.Diags
3481ad6265SDimitry Andric ? std::move(Opts.Diags)
3581ad6265SDimitry Andric : CompilerInstance::createDiagnostics(new DiagnosticOptions);
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric // FIXME: Find a cleaner way to force the driver into restricted modes.
40fe6060f1SDimitry Andric Args.insert(
41fe6060f1SDimitry Andric llvm::find_if(
42fe6060f1SDimitry Andric Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
43fe6060f1SDimitry Andric "-fsyntax-only");
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric // FIXME: We shouldn't have to pass in the path info.
46e8d8bef9SDimitry Andric driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
4781ad6265SDimitry Andric "clang LLVM compiler", Opts.VFS);
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric // Don't check that inputs exist, they may have been remapped.
500b57cec5SDimitry Andric TheDriver.setCheckInputsExist(false);
5181ad6265SDimitry Andric TheDriver.setProbePrecompiled(Opts.ProbePrecompiled);
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
540b57cec5SDimitry Andric if (!C)
550b57cec5SDimitry Andric return nullptr;
560b57cec5SDimitry Andric
5781ad6265SDimitry Andric if (C->getArgs().hasArg(driver::options::OPT_fdriver_only))
5881ad6265SDimitry Andric return nullptr;
5981ad6265SDimitry Andric
600b57cec5SDimitry Andric // Just print the cc1 options if -### was present.
610b57cec5SDimitry Andric if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
620b57cec5SDimitry Andric C->getJobs().Print(llvm::errs(), "\n", true);
630b57cec5SDimitry Andric return nullptr;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric // We expect to get back exactly one command job, if we didn't something
670b57cec5SDimitry Andric // failed. Offload compilation is an exception as it creates multiple jobs. If
680b57cec5SDimitry Andric // that's the case, we proceed with the first job. If caller needs a
690b57cec5SDimitry Andric // particular job, it should be controlled via options (e.g.
700b57cec5SDimitry Andric // --cuda-{host|device}-only for CUDA) passed to the driver.
710b57cec5SDimitry Andric const driver::JobList &Jobs = C->getJobs();
720b57cec5SDimitry Andric bool OffloadCompilation = false;
730b57cec5SDimitry Andric if (Jobs.size() > 1) {
740b57cec5SDimitry Andric for (auto &A : C->getActions()){
750b57cec5SDimitry Andric // On MacOSX real actions may end up being wrapped in BindArchAction
760b57cec5SDimitry Andric if (isa<driver::BindArchAction>(A))
770b57cec5SDimitry Andric A = *A->input_begin();
780b57cec5SDimitry Andric if (isa<driver::OffloadAction>(A)) {
790b57cec5SDimitry Andric OffloadCompilation = true;
800b57cec5SDimitry Andric break;
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric }
84349cc55cSDimitry Andric
8581ad6265SDimitry Andric bool PickFirstOfMany = OffloadCompilation || Opts.RecoverOnError;
86349cc55cSDimitry Andric if (Jobs.size() == 0 || (Jobs.size() > 1 && !PickFirstOfMany)) {
870b57cec5SDimitry Andric SmallString<256> Msg;
880b57cec5SDimitry Andric llvm::raw_svector_ostream OS(Msg);
890b57cec5SDimitry Andric Jobs.Print(OS, "; ", true);
900b57cec5SDimitry Andric Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
910b57cec5SDimitry Andric return nullptr;
920b57cec5SDimitry Andric }
93349cc55cSDimitry Andric auto Cmd = llvm::find_if(Jobs, [](const driver::Command &Cmd) {
94349cc55cSDimitry Andric return StringRef(Cmd.getCreator().getName()) == "clang";
95349cc55cSDimitry Andric });
96349cc55cSDimitry Andric if (Cmd == Jobs.end()) {
970b57cec5SDimitry Andric Diags->Report(diag::err_fe_expected_clang_command);
980b57cec5SDimitry Andric return nullptr;
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric
101349cc55cSDimitry Andric const ArgStringList &CCArgs = Cmd->getArguments();
10281ad6265SDimitry Andric if (Opts.CC1Args)
10381ad6265SDimitry Andric *Opts.CC1Args = {CCArgs.begin(), CCArgs.end()};
104a7dea167SDimitry Andric auto CI = std::make_unique<CompilerInvocation>();
1055ffd83dbSDimitry Andric if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
10681ad6265SDimitry Andric !Opts.RecoverOnError)
1070b57cec5SDimitry Andric return nullptr;
1080b57cec5SDimitry Andric return CI;
1090b57cec5SDimitry Andric }
110