mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-04-15 21:01:29 +00:00

Summary: This is the first patch implementing the new Flang driver as outlined in [1], [2] & [3]. It creates Flang driver (`flang-new`) and Flang frontend driver (`flang-new -fc1`). These will be renamed as `flang` and `flang -fc1` once the current Flang throwaway driver, `flang`, can be replaced with `flang-new`. Currently only 2 options are supported: `-help` and `--version`. `flang-new` is implemented in terms of libclangDriver, defaulting the driver mode to `FlangMode` (added to libclangDriver in [4]). This ensures that the driver runs in Flang mode regardless of the name of the binary inferred from argv[0]. The design of the new Flang compiler and frontend drivers is inspired by it counterparts in Clang [3]. Currently, the new Flang compiler and frontend drivers re-use Clang libraries: clangBasic, clangDriver and clangFrontend. To identify Flang options, this patch adds FlangOption/FC1Option enums. Driver::printHelp is updated so that `flang-new` prints only Flang options. The new Flang driver is disabled by default. To enable it, set `-DBUILD_FLANG_NEW_DRIVER=ON` when configuring CMake and add clang to `LLVM_ENABLE_PROJECTS` (e.g. -DLLVM_ENABLE_PROJECTS=“clang;flang;mlir”). [1] “RFC: new Flang driver - next steps” http://lists.llvm.org/pipermail/flang-dev/2020-July/000470.html [2] “RFC: Adding a fortran mode to the clang driver for flang” http://lists.llvm.org/pipermail/cfe-dev/2019-June/062669.html [3] “RFC: refactoring libclangDriver/libclangFrontend to share with Flang” http://lists.llvm.org/pipermail/cfe-dev/2020-July/066393.html [4] https://reviews.llvm.org/rG6bf55804924d5a1d902925ad080b1a2b57c5c75c co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com> Reviewed By: richard.barton.arm, sameeranjoshi Differential Revision: https://reviews.llvm.org/D86089
101 lines
3.7 KiB
C++
101 lines
3.7 KiB
C++
//===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Construct a compiler invocation object for command line driver arguments
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Frontend/Utils.h"
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
#include "clang/Driver/Compilation.h"
|
|
#include "clang/Driver/Driver.h"
|
|
#include "clang/Driver/Action.h"
|
|
#include "clang/Driver/Options.h"
|
|
#include "clang/Driver/Tool.h"
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
#include "llvm/Option/ArgList.h"
|
|
#include "llvm/Support/Host.h"
|
|
using namespace clang;
|
|
using namespace llvm::opt;
|
|
|
|
std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
|
|
ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool ShouldRecoverOnErorrs,
|
|
std::vector<std::string> *CC1Args) {
|
|
if (!Diags.get()) {
|
|
// No diagnostics engine was provided, so create our own diagnostics object
|
|
// with the default options.
|
|
Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
|
|
}
|
|
|
|
SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
|
|
|
|
// FIXME: Find a cleaner way to force the driver into restricted modes.
|
|
Args.push_back("-fsyntax-only");
|
|
|
|
// FIXME: We shouldn't have to pass in the path info.
|
|
driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
|
|
"clang LLVM compiler", VFS);
|
|
|
|
// Don't check that inputs exist, they may have been remapped.
|
|
TheDriver.setCheckInputsExist(false);
|
|
|
|
std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
|
|
if (!C)
|
|
return nullptr;
|
|
|
|
// Just print the cc1 options if -### was present.
|
|
if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
|
|
C->getJobs().Print(llvm::errs(), "\n", true);
|
|
return nullptr;
|
|
}
|
|
|
|
// We expect to get back exactly one command job, if we didn't something
|
|
// failed. Offload compilation is an exception as it creates multiple jobs. If
|
|
// that's the case, we proceed with the first job. If caller needs a
|
|
// particular job, it should be controlled via options (e.g.
|
|
// --cuda-{host|device}-only for CUDA) passed to the driver.
|
|
const driver::JobList &Jobs = C->getJobs();
|
|
bool OffloadCompilation = false;
|
|
if (Jobs.size() > 1) {
|
|
for (auto &A : C->getActions()){
|
|
// On MacOSX real actions may end up being wrapped in BindArchAction
|
|
if (isa<driver::BindArchAction>(A))
|
|
A = *A->input_begin();
|
|
if (isa<driver::OffloadAction>(A)) {
|
|
OffloadCompilation = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
|
|
(Jobs.size() > 1 && !OffloadCompilation)) {
|
|
SmallString<256> Msg;
|
|
llvm::raw_svector_ostream OS(Msg);
|
|
Jobs.Print(OS, "; ", true);
|
|
Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
|
|
return nullptr;
|
|
}
|
|
|
|
const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
|
|
if (StringRef(Cmd.getCreator().getName()) != "clang") {
|
|
Diags->Report(diag::err_fe_expected_clang_command);
|
|
return nullptr;
|
|
}
|
|
|
|
const ArgStringList &CCArgs = Cmd.getArguments();
|
|
if (CC1Args)
|
|
*CC1Args = {CCArgs.begin(), CCArgs.end()};
|
|
auto CI = std::make_unique<CompilerInvocation>();
|
|
if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
|
|
!ShouldRecoverOnErorrs)
|
|
return nullptr;
|
|
return CI;
|
|
}
|