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