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