| 1 | //===-- fc1_main.cpp - Flang FC1 Compiler Frontend ------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This is the entry point to the flang -fc1 functionality, which implements the |
| 10 | // core compiler functionality along with a number of additional tools for |
| 11 | // demonstration and testing purposes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | // |
| 15 | // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "flang/Frontend/CompilerInstance.h" |
| 20 | #include "flang/Frontend/CompilerInvocation.h" |
| 21 | #include "flang/Frontend/TextDiagnosticBuffer.h" |
| 22 | #include "flang/FrontendTool/Utils.h" |
| 23 | #include "clang/Driver/DriverDiagnostic.h" |
| 24 | #include "llvm/MC/TargetRegistry.h" |
| 25 | #include "llvm/Option/Arg.h" |
| 26 | #include "llvm/Option/ArgList.h" |
| 27 | #include "llvm/Option/OptTable.h" |
| 28 | #include "llvm/Support/TargetSelect.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | |
| 31 | #include <cstdio> |
| 32 | |
| 33 | using namespace Fortran::frontend; |
| 34 | |
| 35 | /// Print supported cpus of the given target. |
| 36 | static int printSupportedCPUs(llvm::StringRef triple) { |
| 37 | std::string error; |
| 38 | const llvm::Target *target = |
| 39 | llvm::TargetRegistry::lookupTarget(TripleStr: triple, Error&: error); |
| 40 | if (!target) { |
| 41 | llvm::errs() << error; |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | // the target machine will handle the mcpu printing |
| 46 | llvm::TargetOptions targetOpts; |
| 47 | std::unique_ptr<llvm::TargetMachine> targetMachine( |
| 48 | target->createTargetMachine(llvm::Triple(triple), "" , "+cpuhelp" , |
| 49 | targetOpts, std::nullopt)); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) { |
| 54 | // Create CompilerInstance |
| 55 | std::unique_ptr<CompilerInstance> flang(new CompilerInstance()); |
| 56 | |
| 57 | // Create DiagnosticsEngine for the frontend driver |
| 58 | flang->createDiagnostics(); |
| 59 | if (!flang->hasDiagnostics()) |
| 60 | return 1; |
| 61 | |
| 62 | // We will buffer diagnostics from argument parsing so that we can output |
| 63 | // them using a well formed diagnostic object. |
| 64 | TextDiagnosticBuffer *diagsBuffer = new TextDiagnosticBuffer; |
| 65 | |
| 66 | // Create CompilerInvocation - use a dedicated instance of DiagnosticsEngine |
| 67 | // for parsing the arguments |
| 68 | llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID( |
| 69 | new clang::DiagnosticIDs()); |
| 70 | clang::DiagnosticOptions diagOpts; |
| 71 | clang::DiagnosticsEngine diags(diagID, diagOpts, diagsBuffer); |
| 72 | bool success = CompilerInvocation::createFromArgs(flang->getInvocation(), |
| 73 | argv, diags, argv0); |
| 74 | |
| 75 | // Initialize targets first, so that --version shows registered targets. |
| 76 | llvm::InitializeAllTargets(); |
| 77 | llvm::InitializeAllTargetMCs(); |
| 78 | llvm::InitializeAllAsmPrinters(); |
| 79 | |
| 80 | // --print-supported-cpus takes priority over the actual compilation. |
| 81 | if (flang->getFrontendOpts().printSupportedCPUs) |
| 82 | return printSupportedCPUs(flang->getInvocation().getTargetOpts().triple); |
| 83 | |
| 84 | diagsBuffer->flushDiagnostics(flang->getDiagnostics()); |
| 85 | |
| 86 | if (!success) |
| 87 | return 1; |
| 88 | |
| 89 | // Execute the frontend actions. |
| 90 | success = executeCompilerInvocation(flang.get()); |
| 91 | |
| 92 | // Delete output files to free Compiler Instance |
| 93 | flang->clearOutputFiles(/*EraseFiles=*/false); |
| 94 | |
| 95 | return !success; |
| 96 | } |
| 97 | |