17330f729Sjoerg //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // Construct a compiler invocation object for command line driver arguments
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/Frontend/Utils.h"
147330f729Sjoerg #include "clang/Basic/DiagnosticOptions.h"
157330f729Sjoerg #include "clang/Driver/Compilation.h"
167330f729Sjoerg #include "clang/Driver/Driver.h"
177330f729Sjoerg #include "clang/Driver/Action.h"
187330f729Sjoerg #include "clang/Driver/Options.h"
197330f729Sjoerg #include "clang/Driver/Tool.h"
207330f729Sjoerg #include "clang/Frontend/CompilerInstance.h"
217330f729Sjoerg #include "clang/Frontend/FrontendDiagnostic.h"
227330f729Sjoerg #include "llvm/Option/ArgList.h"
237330f729Sjoerg #include "llvm/Support/Host.h"
247330f729Sjoerg using namespace clang;
257330f729Sjoerg using namespace llvm::opt;
267330f729Sjoerg
createInvocationFromCommandLine(ArrayRef<const char * > ArgList,IntrusiveRefCntPtr<DiagnosticsEngine> Diags,IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,bool ShouldRecoverOnErorrs,std::vector<std::string> * CC1Args)277330f729Sjoerg std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
287330f729Sjoerg ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
29*e038c9c4Sjoerg IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool ShouldRecoverOnErorrs,
30*e038c9c4Sjoerg std::vector<std::string> *CC1Args) {
317330f729Sjoerg if (!Diags.get()) {
327330f729Sjoerg // No diagnostics engine was provided, so create our own diagnostics object
337330f729Sjoerg // with the default options.
347330f729Sjoerg Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
357330f729Sjoerg }
367330f729Sjoerg
377330f729Sjoerg SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
387330f729Sjoerg
397330f729Sjoerg // FIXME: Find a cleaner way to force the driver into restricted modes.
407330f729Sjoerg Args.push_back("-fsyntax-only");
417330f729Sjoerg
427330f729Sjoerg // FIXME: We shouldn't have to pass in the path info.
43*e038c9c4Sjoerg driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
44*e038c9c4Sjoerg "clang LLVM compiler", VFS);
457330f729Sjoerg
467330f729Sjoerg // Don't check that inputs exist, they may have been remapped.
477330f729Sjoerg TheDriver.setCheckInputsExist(false);
487330f729Sjoerg
497330f729Sjoerg std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
507330f729Sjoerg if (!C)
517330f729Sjoerg return nullptr;
527330f729Sjoerg
537330f729Sjoerg // Just print the cc1 options if -### was present.
547330f729Sjoerg if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
557330f729Sjoerg C->getJobs().Print(llvm::errs(), "\n", true);
567330f729Sjoerg return nullptr;
577330f729Sjoerg }
587330f729Sjoerg
597330f729Sjoerg // We expect to get back exactly one command job, if we didn't something
607330f729Sjoerg // failed. Offload compilation is an exception as it creates multiple jobs. If
617330f729Sjoerg // that's the case, we proceed with the first job. If caller needs a
627330f729Sjoerg // particular job, it should be controlled via options (e.g.
637330f729Sjoerg // --cuda-{host|device}-only for CUDA) passed to the driver.
647330f729Sjoerg const driver::JobList &Jobs = C->getJobs();
657330f729Sjoerg bool OffloadCompilation = false;
667330f729Sjoerg if (Jobs.size() > 1) {
677330f729Sjoerg for (auto &A : C->getActions()){
687330f729Sjoerg // On MacOSX real actions may end up being wrapped in BindArchAction
697330f729Sjoerg if (isa<driver::BindArchAction>(A))
707330f729Sjoerg A = *A->input_begin();
717330f729Sjoerg if (isa<driver::OffloadAction>(A)) {
727330f729Sjoerg OffloadCompilation = true;
737330f729Sjoerg break;
747330f729Sjoerg }
757330f729Sjoerg }
767330f729Sjoerg }
777330f729Sjoerg if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
787330f729Sjoerg (Jobs.size() > 1 && !OffloadCompilation)) {
797330f729Sjoerg SmallString<256> Msg;
807330f729Sjoerg llvm::raw_svector_ostream OS(Msg);
817330f729Sjoerg Jobs.Print(OS, "; ", true);
827330f729Sjoerg Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
837330f729Sjoerg return nullptr;
847330f729Sjoerg }
857330f729Sjoerg
867330f729Sjoerg const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
877330f729Sjoerg if (StringRef(Cmd.getCreator().getName()) != "clang") {
887330f729Sjoerg Diags->Report(diag::err_fe_expected_clang_command);
897330f729Sjoerg return nullptr;
907330f729Sjoerg }
917330f729Sjoerg
927330f729Sjoerg const ArgStringList &CCArgs = Cmd.getArguments();
93*e038c9c4Sjoerg if (CC1Args)
94*e038c9c4Sjoerg *CC1Args = {CCArgs.begin(), CCArgs.end()};
957330f729Sjoerg auto CI = std::make_unique<CompilerInvocation>();
96*e038c9c4Sjoerg if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
977330f729Sjoerg !ShouldRecoverOnErorrs)
987330f729Sjoerg return nullptr;
997330f729Sjoerg return CI;
1007330f729Sjoerg }
101