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