1 //===-- driver.cpp - Flang Driver -----------------------------------------===// 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 driver; it is a thin wrapper 10 // for functionality in the Driver flang library. 11 // 12 //===----------------------------------------------------------------------===// 13 // 14 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "clang/Driver/Driver.h" 19 #include "flang/Frontend/CompilerInvocation.h" 20 #include "flang/Frontend/TextDiagnosticPrinter.h" 21 #include "clang/Basic/Diagnostic.h" 22 #include "clang/Basic/DiagnosticIDs.h" 23 #include "clang/Basic/DiagnosticOptions.h" 24 #include "clang/Driver/Compilation.h" 25 #include "llvm/ADT/ArrayRef.h" 26 #include "llvm/ADT/IntrusiveRefCntPtr.h" 27 #include "llvm/Option/ArgList.h" 28 #include "llvm/Support/Host.h" 29 #include "llvm/Support/InitLLVM.h" 30 #include "llvm/Support/VirtualFileSystem.h" 31 32 using llvm::StringRef; 33 34 // main frontend method. Lives inside fc1_main.cpp 35 extern int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0); 36 37 std::string getExecutablePath(const char *argv0) { 38 // This just needs to be some symbol in the binary 39 void *p = (void *)(intptr_t)getExecutablePath; 40 return llvm::sys::fs::getMainExecutable(argv0, p); 41 } 42 43 // This lets us create the DiagnosticsEngine with a properly-filled-out 44 // DiagnosticOptions instance 45 static clang::DiagnosticOptions * 46 createAndPopulateDiagOpts(llvm::ArrayRef<const char *> argv) { 47 auto *diagOpts = new clang::DiagnosticOptions; 48 49 // Ignore missingArgCount and the return value of ParseDiagnosticArgs. 50 // Any errors that would be diagnosed here will also be diagnosed later, 51 // when the DiagnosticsEngine actually exists. 52 unsigned missingArgIndex, missingArgCount; 53 llvm::opt::InputArgList args = clang::driver::getDriverOptTable().ParseArgs( 54 argv.slice(1), missingArgIndex, missingArgCount, 55 /*FlagsToInclude=*/clang::driver::options::FlangOption); 56 57 (void)Fortran::frontend::parseDiagnosticArgs(*diagOpts, args); 58 59 return diagOpts; 60 } 61 62 static int executeFC1Tool(llvm::SmallVectorImpl<const char *> &argV) { 63 llvm::StringRef tool = argV[1]; 64 if (tool == "-fc1") 65 return fc1_main(makeArrayRef(argV).slice(2), argV[0]); 66 67 // Reject unknown tools. 68 // ATM it only supports fc1. Any fc1[*] is rejected. 69 llvm::errs() << "error: unknown integrated tool '" << tool << "'. " 70 << "Valid tools include '-fc1'.\n"; 71 return 1; 72 } 73 74 int main(int argc, const char **argv) { 75 76 // Initialize variables to call the driver 77 llvm::InitLLVM x(argc, argv); 78 llvm::SmallVector<const char *, 256> args(argv, argv + argc); 79 80 clang::driver::ParsedClangName targetandMode("flang", "--driver-mode=flang"); 81 std::string driverPath = getExecutablePath(args[0]); 82 83 // Check if flang-new is in the frontend mode 84 auto firstArg = std::find_if( 85 args.begin() + 1, args.end(), [](const char *a) { return a != nullptr; }); 86 if (firstArg != args.end()) { 87 if (llvm::StringRef(args[1]).startswith("-cc1")) { 88 llvm::errs() << "error: unknown integrated tool '" << args[1] << "'. " 89 << "Valid tools include '-fc1'.\n"; 90 return 1; 91 } 92 // Call flang-new frontend 93 if (llvm::StringRef(args[1]).startswith("-fc1")) { 94 return executeFC1Tool(args); 95 } 96 } 97 98 // Not in the frontend mode - continue in the compiler driver mode. 99 100 // Create DiagnosticsEngine for the compiler driver 101 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts = 102 createAndPopulateDiagOpts(args); 103 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID( 104 new clang::DiagnosticIDs()); 105 Fortran::frontend::TextDiagnosticPrinter *diagClient = 106 new Fortran::frontend::TextDiagnosticPrinter(llvm::errs(), &*diagOpts); 107 108 diagClient->setPrefix( 109 std::string(llvm::sys::path::stem(getExecutablePath(args[0])))); 110 111 clang::DiagnosticsEngine diags(diagID, &*diagOpts, diagClient); 112 113 // Prepare the driver 114 clang::driver::Driver theDriver(driverPath, 115 llvm::sys::getDefaultTargetTriple(), diags, "flang LLVM compiler"); 116 theDriver.setTargetAndMode(targetandMode); 117 std::unique_ptr<clang::driver::Compilation> c( 118 theDriver.BuildCompilation(args)); 119 llvm::SmallVector<std::pair<int, const clang::driver::Command *>, 4> 120 failingCommands; 121 122 // Run the driver 123 int res = 1; 124 bool isCrash = false; 125 res = theDriver.ExecuteCompilation(*c, failingCommands); 126 127 for (const auto &p : failingCommands) { 128 int commandRes = p.first; 129 const clang::driver::Command *failingCommand = p.second; 130 if (!res) 131 res = commandRes; 132 133 // If result status is < 0 (e.g. when sys::ExecuteAndWait returns -1), 134 // then the driver command signalled an error. On Windows, abort will 135 // return an exit code of 3. In these cases, generate additional diagnostic 136 // information if possible. 137 isCrash = commandRes < 0; 138 #ifdef _WIN32 139 isCrash |= commandRes == 3; 140 #endif 141 if (isCrash) { 142 theDriver.generateCompilationDiagnostics(*c, *failingCommand); 143 break; 144 } 145 } 146 147 diags.getClient()->finish(); 148 149 // If we have multiple failing commands, we return the result of the first 150 // failing command. 151 return res; 152 } 153