Nick Lewycky | e47c245 | 2010-09-23 23:48:20 +0000 | [diff] [blame] | 1 | //===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===// |
Daniel Dunbar | 544ecd1 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 544ecd1 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 10 | #include "clang/Driver/Driver.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 11 | #include "InputInfo.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 12 | #include "ToolChains/AMDGPU.h" |
| 13 | #include "ToolChains/AVR.h" |
Ed Schouten | 4dabea2 | 2017-06-25 08:29:09 +0000 | [diff] [blame] | 14 | #include "ToolChains/Ananas.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 15 | #include "ToolChains/BareMetal.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 16 | #include "ToolChains/Clang.h" |
| 17 | #include "ToolChains/CloudABI.h" |
| 18 | #include "ToolChains/Contiki.h" |
| 19 | #include "ToolChains/CrossWindows.h" |
| 20 | #include "ToolChains/Cuda.h" |
| 21 | #include "ToolChains/Darwin.h" |
| 22 | #include "ToolChains/DragonFly.h" |
| 23 | #include "ToolChains/FreeBSD.h" |
| 24 | #include "ToolChains/Fuchsia.h" |
| 25 | #include "ToolChains/Gnu.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 26 | #include "ToolChains/HIP.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 27 | #include "ToolChains/Haiku.h" |
| 28 | #include "ToolChains/Hexagon.h" |
| 29 | #include "ToolChains/Lanai.h" |
| 30 | #include "ToolChains/Linux.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 31 | #include "ToolChains/MSVC.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 32 | #include "ToolChains/MinGW.h" |
| 33 | #include "ToolChains/Minix.h" |
Konstantin Zhuravlyov | e37b32c | 2017-03-08 22:36:04 +0000 | [diff] [blame] | 34 | #include "ToolChains/MipsLinux.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 35 | #include "ToolChains/Myriad.h" |
| 36 | #include "ToolChains/NaCl.h" |
| 37 | #include "ToolChains/NetBSD.h" |
| 38 | #include "ToolChains/OpenBSD.h" |
| 39 | #include "ToolChains/PS4CPU.h" |
David Bolvansky | f4be253 | 2018-07-31 14:21:46 +0000 | [diff] [blame] | 40 | #include "ToolChains/RISCV.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 41 | #include "ToolChains/Solaris.h" |
| 42 | #include "ToolChains/TCE.h" |
| 43 | #include "ToolChains/WebAssembly.h" |
| 44 | #include "ToolChains/XCore.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 45 | #include "clang/Basic/Version.h" |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 46 | #include "clang/Basic/VirtualFileSystem.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 47 | #include "clang/Config/config.h" |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 48 | #include "clang/Driver/Action.h" |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 49 | #include "clang/Driver/Compilation.h" |
Daniel Dunbar | c0b3e95 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 50 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 51 | #include "clang/Driver/Job.h" |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 52 | #include "clang/Driver/Options.h" |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 53 | #include "clang/Driver/SanitizerArgs.h" |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 54 | #include "clang/Driver/Tool.h" |
| 55 | #include "clang/Driver/ToolChain.h" |
Chris Lattner | ce6c42f | 2011-03-23 04:04:01 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/ArrayRef.h" |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/STLExtras.h" |
Justin Lebar | 6290761 | 2016-07-06 21:21:39 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/SmallSet.h" |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/StringSet.h" |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/StringSwitch.h" |
Nico Weber | d637c05 | 2018-04-30 13:52:15 +0000 | [diff] [blame] | 62 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 63 | #include "llvm/Option/Arg.h" |
| 64 | #include "llvm/Option/ArgList.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 65 | #include "llvm/Option/OptSpecifier.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 66 | #include "llvm/Option/OptTable.h" |
| 67 | #include "llvm/Option/Option.h" |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 68 | #include "llvm/Support/CommandLine.h" |
David Blaikie | 7900020 | 2011-09-23 05:57:42 +0000 | [diff] [blame] | 69 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | f28df4c | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 70 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 71 | #include "llvm/Support/Path.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 72 | #include "llvm/Support/PrettyStackTrace.h" |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 73 | #include "llvm/Support/Process.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 74 | #include "llvm/Support/Program.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 75 | #include "llvm/Support/StringSaver.h" |
Dimitry Andric | 81c4042 | 2017-06-06 21:54:21 +0000 | [diff] [blame] | 76 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 77 | #include "llvm/Support/raw_ostream.h" |
Dylan Noblesmith | 4f4e745 | 2012-02-02 00:40:14 +0000 | [diff] [blame] | 78 | #include <map> |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 79 | #include <memory> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 80 | #include <utility> |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 81 | #if LLVM_ON_UNIX |
| 82 | #include <unistd.h> // getpid |
| 83 | #endif |
Dylan Noblesmith | 86780e9 | 2012-02-01 14:25:28 +0000 | [diff] [blame] | 84 | |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 85 | using namespace clang::driver; |
Chris Lattner | ada1c91 | 2009-03-26 05:56:24 +0000 | [diff] [blame] | 86 | using namespace clang; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 87 | using namespace llvm::opt; |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 88 | |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 89 | Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple, |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 90 | DiagnosticsEngine &Diags, |
| 91 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 92 | : Opts(createDriverOptTable()), Diags(Diags), VFS(std::move(VFS)), |
| 93 | Mode(GCCMode), SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone), |
| 94 | LTOMode(LTOK_None), ClangExecutable(ClangExecutable), |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 95 | SysRoot(DEFAULT_SYSROOT), DriverTitle("clang LLVM compiler"), |
| 96 | CCPrintOptionsFilename(nullptr), CCPrintHeadersFilename(nullptr), |
| 97 | CCLogDiagnosticsFilename(nullptr), CCCPrintBindings(false), |
| 98 | CCPrintOptions(false), CCPrintHeaders(false), CCLogDiagnostics(false), |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 99 | CCGenDiagnostics(false), TargetTriple(TargetTriple), |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 100 | CCCGenericGCCName(""), Saver(Alloc), CheckInputsExist(true), |
| 101 | CCCUsePCH(true), GenReproducer(false), |
| 102 | SuppressMissingInputWarning(false) { |
Daniel Dunbar | 3f3e2cd | 2010-01-20 02:35:16 +0000 | [diff] [blame] | 103 | |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 104 | // Provide a sane fallback if no VFS is specified. |
| 105 | if (!this->VFS) |
| 106 | this->VFS = vfs::getRealFileSystem(); |
| 107 | |
Sumanth Gundapaneni | 3a159294 | 2015-03-03 20:43:12 +0000 | [diff] [blame] | 108 | Name = llvm::sys::path::filename(ClangExecutable); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 109 | Dir = llvm::sys::path::parent_path(ClangExecutable); |
Benjamin Kramer | f420dda | 2015-10-13 15:19:32 +0000 | [diff] [blame] | 110 | InstalledDir = Dir; // Provide a sensible default installed dir. |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 111 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 112 | #if defined(CLANG_CONFIG_FILE_SYSTEM_DIR) |
| 113 | SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR; |
| 114 | #endif |
| 115 | #if defined(CLANG_CONFIG_FILE_USER_DIR) |
| 116 | UserConfigDir = CLANG_CONFIG_FILE_USER_DIR; |
| 117 | #endif |
| 118 | |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 119 | // Compute the path to the resource directory. |
| 120 | StringRef ClangResourceDir(CLANG_RESOURCE_DIR); |
| 121 | SmallString<128> P(Dir); |
Chandler Carruth | fd3cc70 | 2014-12-29 12:09:08 +0000 | [diff] [blame] | 122 | if (ClangResourceDir != "") { |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 123 | llvm::sys::path::append(P, ClangResourceDir); |
Chandler Carruth | fd3cc70 | 2014-12-29 12:09:08 +0000 | [diff] [blame] | 124 | } else { |
| 125 | StringRef ClangLibdirSuffix(CLANG_LIBDIR_SUFFIX); |
Argyrios Kyrtzidis | bcae047 | 2017-02-25 18:14:35 +0000 | [diff] [blame] | 126 | P = llvm::sys::path::parent_path(Dir); |
| 127 | llvm::sys::path::append(P, Twine("lib") + ClangLibdirSuffix, "clang", |
Chandler Carruth | fd3cc70 | 2014-12-29 12:09:08 +0000 | [diff] [blame] | 128 | CLANG_VERSION_STRING); |
| 129 | } |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 130 | ResourceDir = P.str(); |
Daniel Dunbar | 544ecd1 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 133 | void Driver::ParseDriverMode(StringRef ProgramName, |
| 134 | ArrayRef<const char *> Args) { |
Martin Storsjo | 1fab23d | 2018-04-25 21:23:59 +0000 | [diff] [blame] | 135 | if (ClangNameParts.isEmpty()) |
| 136 | ClangNameParts = ToolChain::getTargetAndModeFromProgramName(ProgramName); |
Serge Pavlov | 4e76984 | 2017-08-29 05:22:26 +0000 | [diff] [blame] | 137 | setDriverModeFromOption(ClangNameParts.DriverMode); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 138 | |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 139 | for (const char *ArgPtr : Args) { |
Erich Keane | 258f059 | 2018-02-07 00:19:58 +0000 | [diff] [blame] | 140 | // Ignore nullptrs, they are the response file's EOL markers. |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 141 | if (ArgPtr == nullptr) |
Reid Kleckner | af5fd6a | 2014-08-22 19:29:30 +0000 | [diff] [blame] | 142 | continue; |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 143 | const StringRef Arg = ArgPtr; |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 144 | setDriverModeFromOption(Arg); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 148 | void Driver::setDriverModeFromOption(StringRef Opt) { |
| 149 | const std::string OptName = |
| 150 | getOpts().getOption(options::OPT_driver_mode).getPrefixedName(); |
| 151 | if (!Opt.startswith(OptName)) |
| 152 | return; |
| 153 | StringRef Value = Opt.drop_front(OptName.size()); |
| 154 | |
Erich Keane | 2908a04 | 2018-02-12 17:47:01 +0000 | [diff] [blame] | 155 | if (auto M = llvm::StringSwitch<llvm::Optional<DriverMode>>(Value) |
| 156 | .Case("gcc", GCCMode) |
| 157 | .Case("g++", GXXMode) |
| 158 | .Case("cpp", CPPMode) |
| 159 | .Case("cl", CLMode) |
| 160 | .Default(None)) |
| 161 | Mode = *M; |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 162 | else |
| 163 | Diag(diag::err_drv_unsupported_option_argument) << OptName << Value; |
| 164 | } |
| 165 | |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 166 | InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, |
| 167 | bool &ContainsError) { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 168 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 169 | ContainsError = false; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 170 | |
| 171 | unsigned IncludedFlagsBitmask; |
| 172 | unsigned ExcludedFlagsBitmask; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 173 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 174 | getIncludeExcludeOptionFlagMasks(); |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 176 | unsigned MissingArgIndex, MissingArgCount; |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 177 | InputArgList Args = |
David Blaikie | 6d492ad | 2015-06-21 06:32:36 +0000 | [diff] [blame] | 178 | getOpts().ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount, |
| 179 | IncludedFlagsBitmask, ExcludedFlagsBitmask); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 180 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 181 | // Check for missing argument error. |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 182 | if (MissingArgCount) { |
| 183 | Diag(diag::err_drv_missing_argument) |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 184 | << Args.getArgString(MissingArgIndex) << MissingArgCount; |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 185 | ContainsError |= |
| 186 | Diags.getDiagnosticLevel(diag::err_drv_missing_argument, |
| 187 | SourceLocation()) > DiagnosticsEngine::Warning; |
| 188 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 190 | // Check for unsupported options. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 191 | for (const Arg *A : Args) { |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 192 | if (A->getOption().hasFlag(options::Unsupported)) { |
Brian Gesiak | 2491076 | 2018-01-06 00:25:40 +0000 | [diff] [blame] | 193 | unsigned DiagID; |
| 194 | auto ArgString = A->getAsString(Args); |
| 195 | std::string Nearest; |
| 196 | if (getOpts().findNearest( |
| 197 | ArgString, Nearest, IncludedFlagsBitmask, |
| 198 | ExcludedFlagsBitmask | options::Unsupported) > 1) { |
| 199 | DiagID = diag::err_drv_unsupported_opt; |
| 200 | Diag(DiagID) << ArgString; |
| 201 | } else { |
| 202 | DiagID = diag::err_drv_unsupported_opt_with_suggestion; |
| 203 | Diag(DiagID) << ArgString << Nearest; |
| 204 | } |
| 205 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 206 | DiagnosticsEngine::Warning; |
Daniel Dunbar | d8500f3 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 207 | continue; |
| 208 | } |
Chad Rosier | ce975d9 | 2012-02-22 17:55:22 +0000 | [diff] [blame] | 209 | |
| 210 | // Warn about -mcpu= without an argument. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 211 | if (A->getOption().matches(options::OPT_mcpu_EQ) && A->containsValue("")) { |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 212 | Diag(diag::warn_drv_empty_joined_argument) << A->getAsString(Args); |
| 213 | ContainsError |= Diags.getDiagnosticLevel( |
| 214 | diag::warn_drv_empty_joined_argument, |
| 215 | SourceLocation()) > DiagnosticsEngine::Warning; |
Chad Rosier | ce975d9 | 2012-02-22 17:55:22 +0000 | [diff] [blame] | 216 | } |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 219 | for (const Arg *A : Args.filtered(options::OPT_UNKNOWN)) { |
Brian Gesiak | 2491076 | 2018-01-06 00:25:40 +0000 | [diff] [blame] | 220 | unsigned DiagID; |
| 221 | auto ArgString = A->getAsString(Args); |
| 222 | std::string Nearest; |
| 223 | if (getOpts().findNearest( |
| 224 | ArgString, Nearest, IncludedFlagsBitmask, ExcludedFlagsBitmask) > 1) { |
| 225 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl |
| 226 | : diag::err_drv_unknown_argument; |
| 227 | Diags.Report(DiagID) << ArgString; |
| 228 | } else { |
| 229 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion |
| 230 | : diag::err_drv_unknown_argument_with_suggestion; |
| 231 | Diags.Report(DiagID) << ArgString << Nearest; |
| 232 | } |
| 233 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 234 | DiagnosticsEngine::Warning; |
| 235 | } |
Rafael Espindola | 8a2d496 | 2013-09-23 23:55:25 +0000 | [diff] [blame] | 236 | |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 237 | return Args; |
| 238 | } |
| 239 | |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 240 | // Determine which compilation mode we are in. We look for options which |
| 241 | // affect the phase, starting with the earliest phases, and record which |
| 242 | // option we used to determine the final phase. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 243 | phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, |
| 244 | Arg **FinalPhaseArg) const { |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 245 | Arg *PhaseArg = nullptr; |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 246 | phases::ID FinalPhase; |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 247 | |
Hans Wennborg | e50cec3 | 2014-06-13 20:59:54 +0000 | [diff] [blame] | 248 | // -{E,EP,P,M,MM} only run the preprocessor. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 249 | if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) || |
Hans Wennborg | e50cec3 | 2014-06-13 20:59:54 +0000 | [diff] [blame] | 250 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) || |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 251 | (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) || |
| 252 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 253 | FinalPhase = phases::Preprocess; |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 254 | |
Richard Smith | dd4ad3d | 2016-08-30 19:06:26 +0000 | [diff] [blame] | 255 | // --precompile only runs up to precompilation. |
| 256 | } else if ((PhaseArg = DAL.getLastArg(options::OPT__precompile))) { |
| 257 | FinalPhase = phases::Precompile; |
| 258 | |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 259 | // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler. |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 260 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) || |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 261 | (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) || |
Ben Langmuir | 2cb4a78 | 2014-02-05 22:21:15 +0000 | [diff] [blame] | 262 | (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) || |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 263 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) || |
Fariborz Jahanian | 73223bb | 2012-04-02 15:59:19 +0000 | [diff] [blame] | 264 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) || |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 265 | (PhaseArg = DAL.getLastArg(options::OPT__migrate)) || |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 266 | (PhaseArg = DAL.getLastArg(options::OPT__analyze, |
Chad Rosier | 1aeb15a | 2012-03-06 23:14:35 +0000 | [diff] [blame] | 267 | options::OPT__analyze_auto)) || |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 268 | (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 269 | FinalPhase = phases::Compile; |
| 270 | |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 271 | // -S only runs up to the backend. |
| 272 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) { |
| 273 | FinalPhase = phases::Backend; |
| 274 | |
Artem Belevich | 4242f41 | 2015-07-28 21:01:21 +0000 | [diff] [blame] | 275 | // -c compilation only runs up to the assembler. |
| 276 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 277 | FinalPhase = phases::Assemble; |
| 278 | |
| 279 | // Otherwise do everything. |
| 280 | } else |
| 281 | FinalPhase = phases::Link; |
| 282 | |
| 283 | if (FinalPhaseArg) |
| 284 | *FinalPhaseArg = PhaseArg; |
| 285 | |
| 286 | return FinalPhase; |
| 287 | } |
| 288 | |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 289 | static Arg *MakeInputArg(DerivedArgList &Args, OptTable &Opts, |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 290 | StringRef Value, bool Claim = true) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 291 | Arg *A = new Arg(Opts.getOption(options::OPT_INPUT), Value, |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 292 | Args.getBaseArgs().MakeIndex(Value), Value.data()); |
Hans Wennborg | 5536285 | 2014-05-02 22:55:30 +0000 | [diff] [blame] | 293 | Args.AddSynthesizedArg(A); |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 294 | if (Claim) |
| 295 | A->claim(); |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 296 | return A; |
| 297 | } |
| 298 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 299 | DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const { |
| 300 | DerivedArgList *DAL = new DerivedArgList(Args); |
| 301 | |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 302 | bool HasNostdlib = Args.hasArg(options::OPT_nostdlib); |
Nirav Dave | b0bb614 | 2015-11-24 16:07:21 +0000 | [diff] [blame] | 303 | bool HasNodefaultlib = Args.hasArg(options::OPT_nodefaultlibs); |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 304 | for (Arg *A : Args) { |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 305 | // Unfortunately, we have to parse some forwarding options (-Xassembler, |
| 306 | // -Xlinker, -Xpreprocessor) because we either integrate their functionality |
| 307 | // (assembler and preprocessor), or bypass a previous driver ('collect2'). |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 308 | |
| 309 | // Rewrite linker options, to replace --no-demangle with a custom internal |
| 310 | // option. |
| 311 | if ((A->getOption().matches(options::OPT_Wl_COMMA) || |
| 312 | A->getOption().matches(options::OPT_Xlinker)) && |
| 313 | A->containsValue("--no-demangle")) { |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 314 | // Add the rewritten no-demangle argument. |
| 315 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle)); |
| 316 | |
| 317 | // Add the remaining values as Xlinker arguments. |
Benjamin Kramer | 72e6431 | 2015-09-24 14:48:49 +0000 | [diff] [blame] | 318 | for (StringRef Val : A->getValues()) |
Douglas Katzman | a34b7bf | 2015-06-30 19:32:57 +0000 | [diff] [blame] | 319 | if (Val != "--no-demangle") |
| 320 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_Xlinker), Val); |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 321 | |
| 322 | continue; |
| 323 | } |
| 324 | |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 325 | // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by |
| 326 | // some build systems. We don't try to be complete here because we don't |
| 327 | // care to encourage this usage model. |
| 328 | if (A->getOption().matches(options::OPT_Wp_COMMA) && |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 329 | (A->getValue(0) == StringRef("-MD") || |
| 330 | A->getValue(0) == StringRef("-MMD"))) { |
Daniel Dunbar | 3648ba7 | 2010-06-15 20:30:18 +0000 | [diff] [blame] | 331 | // Rewrite to -MD/-MMD along with -MF. |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 332 | if (A->getValue(0) == StringRef("-MD")) |
Daniel Dunbar | 3648ba7 | 2010-06-15 20:30:18 +0000 | [diff] [blame] | 333 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD)); |
| 334 | else |
| 335 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MMD)); |
Michael J. Spencer | 70d85be | 2012-11-07 23:37:14 +0000 | [diff] [blame] | 336 | if (A->getNumValues() == 2) |
| 337 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF), |
| 338 | A->getValue(1)); |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 339 | continue; |
| 340 | } |
| 341 | |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 342 | // Rewrite reserved library names. |
| 343 | if (A->getOption().matches(options::OPT_l)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 344 | StringRef Value = A->getValue(); |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 345 | |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 346 | // Rewrite unless -nostdlib is present. |
Nirav Dave | b0bb614 | 2015-11-24 16:07:21 +0000 | [diff] [blame] | 347 | if (!HasNostdlib && !HasNodefaultlib && Value == "stdc++") { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 348 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_stdcxx)); |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 349 | continue; |
| 350 | } |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 351 | |
| 352 | // Rewrite unconditionally. |
| 353 | if (Value == "cc_kext") { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 354 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_cckext)); |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 355 | continue; |
| 356 | } |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 359 | // Pick up inputs via the -- option. |
| 360 | if (A->getOption().matches(options::OPT__DASH_DASH)) { |
| 361 | A->claim(); |
Benjamin Kramer | 72e6431 | 2015-09-24 14:48:49 +0000 | [diff] [blame] | 362 | for (StringRef Val : A->getValues()) |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 363 | DAL->append(MakeInputArg(*DAL, *Opts, Val, false)); |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 364 | continue; |
| 365 | } |
| 366 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 367 | DAL->append(A); |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 368 | } |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 369 | |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 370 | // Enforce -static if -miamcu is present. |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 371 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) |
| 372 | DAL->AddFlagArg(0, Opts->getOption(options::OPT_static)); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 373 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 374 | // Add a default value of -mlinker-version=, if one was given and the user |
| 375 | // didn't specify one. |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 376 | #if defined(HOST_LINK_VERSION) |
Tim Northover | 018578c | 2015-06-12 19:21:35 +0000 | [diff] [blame] | 377 | if (!Args.hasArg(options::OPT_mlinker_version_EQ) && |
| 378 | strlen(HOST_LINK_VERSION) > 0) { |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 379 | DAL->AddJoinedArg(0, Opts->getOption(options::OPT_mlinker_version_EQ), |
| 380 | HOST_LINK_VERSION); |
Daniel Dunbar | b613ffc | 2010-08-17 22:32:45 +0000 | [diff] [blame] | 381 | DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim(); |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 382 | } |
| 383 | #endif |
| 384 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 385 | return DAL; |
| 386 | } |
| 387 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 388 | /// Compute target triple from args. |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 389 | /// |
| 390 | /// This routine provides the logic to compute a target triple from various |
| 391 | /// args passed to the driver and the default triple string. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 392 | static llvm::Triple computeTargetTriple(const Driver &D, |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 393 | StringRef TargetTriple, |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 394 | const ArgList &Args, |
| 395 | StringRef DarwinArchName = "") { |
| 396 | // FIXME: Already done in Compilation *Driver::BuildCompilation |
| 397 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 398 | TargetTriple = A->getValue(); |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 399 | |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 400 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 401 | |
| 402 | // Handle Apple-specific options available here. |
| 403 | if (Target.isOSBinFormatMachO()) { |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 404 | // If an explicit Darwin arch name is given, that trumps all. |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 405 | if (!DarwinArchName.empty()) { |
| 406 | tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName); |
| 407 | return Target; |
| 408 | } |
| 409 | |
| 410 | // Handle the Darwin '-arch' flag. |
| 411 | if (Arg *A = Args.getLastArg(options::OPT_arch)) { |
| 412 | StringRef ArchName = A->getValue(); |
| 413 | tools::darwin::setTripleTypeForMachOArchName(Target, ArchName); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // Handle pseudo-target flags '-mlittle-endian'/'-EL' and |
| 418 | // '-mbig-endian'/'-EB'. |
| 419 | if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, |
| 420 | options::OPT_mbig_endian)) { |
| 421 | if (A->getOption().matches(options::OPT_mlittle_endian)) { |
| 422 | llvm::Triple LE = Target.getLittleEndianArchVariant(); |
| 423 | if (LE.getArch() != llvm::Triple::UnknownArch) |
| 424 | Target = std::move(LE); |
| 425 | } else { |
| 426 | llvm::Triple BE = Target.getBigEndianArchVariant(); |
| 427 | if (BE.getArch() != llvm::Triple::UnknownArch) |
| 428 | Target = std::move(BE); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // Skip further flag support on OSes which don't support '-m32' or '-m64'. |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 433 | if (Target.getArch() == llvm::Triple::tce || |
| 434 | Target.getOS() == llvm::Triple::Minix) |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 435 | return Target; |
| 436 | |
| 437 | // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 438 | Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32, |
| 439 | options::OPT_m32, options::OPT_m16); |
| 440 | if (A) { |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 441 | llvm::Triple::ArchType AT = llvm::Triple::UnknownArch; |
| 442 | |
| 443 | if (A->getOption().matches(options::OPT_m64)) { |
| 444 | AT = Target.get64BitArchVariant().getArch(); |
| 445 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 446 | Target.setEnvironment(llvm::Triple::GNU); |
| 447 | } else if (A->getOption().matches(options::OPT_mx32) && |
| 448 | Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) { |
| 449 | AT = llvm::Triple::x86_64; |
| 450 | Target.setEnvironment(llvm::Triple::GNUX32); |
| 451 | } else if (A->getOption().matches(options::OPT_m32)) { |
| 452 | AT = Target.get32BitArchVariant().getArch(); |
| 453 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 454 | Target.setEnvironment(llvm::Triple::GNU); |
| 455 | } else if (A->getOption().matches(options::OPT_m16) && |
| 456 | Target.get32BitArchVariant().getArch() == llvm::Triple::x86) { |
| 457 | AT = llvm::Triple::x86; |
| 458 | Target.setEnvironment(llvm::Triple::CODE16); |
| 459 | } |
| 460 | |
| 461 | if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) |
| 462 | Target.setArch(AT); |
| 463 | } |
| 464 | |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 465 | // Handle -miamcu flag. |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 466 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { |
| 467 | if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86) |
| 468 | D.Diag(diag::err_drv_unsupported_opt_for_target) << "-miamcu" |
| 469 | << Target.str(); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 470 | |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 471 | if (A && !A->getOption().matches(options::OPT_m32)) |
| 472 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 473 | << "-miamcu" << A->getBaseArg().getAsString(Args); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 474 | |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 475 | Target.setArch(llvm::Triple::x86); |
| 476 | Target.setArchName("i586"); |
| 477 | Target.setEnvironment(llvm::Triple::UnknownEnvironment); |
| 478 | Target.setEnvironmentName(""); |
| 479 | Target.setOS(llvm::Triple::ELFIAMCU); |
| 480 | Target.setVendor(llvm::Triple::UnknownVendor); |
| 481 | Target.setVendorName("intel"); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 484 | return Target; |
| 485 | } |
| 486 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 487 | // Parse the LTO options and record the type of LTO compilation |
Teresa Johnson | 945bc50 | 2015-10-15 20:35:53 +0000 | [diff] [blame] | 488 | // based on which -f(no-)?lto(=.*)? option occurs last. |
| 489 | void Driver::setLTOMode(const llvm::opt::ArgList &Args) { |
| 490 | LTOMode = LTOK_None; |
| 491 | if (!Args.hasFlag(options::OPT_flto, options::OPT_flto_EQ, |
| 492 | options::OPT_fno_lto, false)) |
| 493 | return; |
| 494 | |
| 495 | StringRef LTOName("full"); |
| 496 | |
| 497 | const Arg *A = Args.getLastArg(options::OPT_flto_EQ); |
Teresa Johnson | 6ef80dc | 2015-11-02 18:03:12 +0000 | [diff] [blame] | 498 | if (A) |
| 499 | LTOName = A->getValue(); |
Teresa Johnson | 945bc50 | 2015-10-15 20:35:53 +0000 | [diff] [blame] | 500 | |
| 501 | LTOMode = llvm::StringSwitch<LTOKind>(LTOName) |
| 502 | .Case("full", LTOK_Full) |
| 503 | .Case("thin", LTOK_Thin) |
| 504 | .Default(LTOK_Unknown); |
| 505 | |
| 506 | if (LTOMode == LTOK_Unknown) { |
| 507 | assert(A); |
| 508 | Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() |
| 509 | << A->getValue(); |
| 510 | } |
| 511 | } |
| 512 | |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 513 | /// Compute the desired OpenMP runtime from the flags provided. |
| 514 | Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const { |
| 515 | StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME); |
| 516 | |
| 517 | const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ); |
| 518 | if (A) |
| 519 | RuntimeName = A->getValue(); |
| 520 | |
| 521 | auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName) |
| 522 | .Case("libomp", OMPRT_OMP) |
| 523 | .Case("libgomp", OMPRT_GOMP) |
| 524 | .Case("libiomp5", OMPRT_IOMP5) |
| 525 | .Default(OMPRT_Unknown); |
| 526 | |
| 527 | if (RT == OMPRT_Unknown) { |
| 528 | if (A) |
| 529 | Diag(diag::err_drv_unsupported_option_argument) |
| 530 | << A->getOption().getName() << A->getValue(); |
| 531 | else |
| 532 | // FIXME: We could use a nicer diagnostic here. |
| 533 | Diag(diag::err_drv_unsupported_opt) << "-fopenmp"; |
| 534 | } |
| 535 | |
| 536 | return RT; |
| 537 | } |
| 538 | |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 539 | void Driver::CreateOffloadingDeviceToolChains(Compilation &C, |
| 540 | InputList &Inputs) { |
| 541 | |
| 542 | // |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 543 | // CUDA/HIP |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 544 | // |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 545 | // We need to generate a CUDA/HIP toolchain if any of the inputs has a CUDA |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 546 | // or HIP type. However, mixed CUDA/HIP compilation is not supported. |
| 547 | bool IsCuda = |
| 548 | llvm::any_of(Inputs, [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 549 | return types::isCuda(I.first); |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 550 | }); |
| 551 | bool IsHIP = |
| 552 | llvm::any_of(Inputs, |
| 553 | [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
| 554 | return types::isHIP(I.first); |
| 555 | }) || |
| 556 | C.getInputArgs().hasArg(options::OPT_hip_link); |
| 557 | if (IsCuda && IsHIP) { |
| 558 | Diag(clang::diag::err_drv_mix_cuda_hip); |
| 559 | return; |
| 560 | } |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 561 | if (IsCuda) { |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 562 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 563 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 564 | StringRef DeviceTripleStr; |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 565 | auto OFK = Action::OFK_Cuda; |
| 566 | DeviceTripleStr = |
| 567 | HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda" : "nvptx-nvidia-cuda"; |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 568 | llvm::Triple CudaTriple(DeviceTripleStr); |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 569 | // Use the CUDA and host triples as the key into the ToolChains map, |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 570 | // because the device toolchain we create depends on both. |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 571 | auto &CudaTC = ToolChains[CudaTriple.str() + "/" + HostTriple.str()]; |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 572 | if (!CudaTC) { |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 573 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 574 | *this, CudaTriple, *HostTC, C.getInputArgs(), OFK); |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 575 | } |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 576 | C.addOffloadDeviceToolChain(CudaTC.get(), OFK); |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 577 | } else if (IsHIP) { |
| 578 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 579 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
| 580 | StringRef DeviceTripleStr; |
| 581 | auto OFK = Action::OFK_HIP; |
| 582 | DeviceTripleStr = "amdgcn-amd-amdhsa"; |
| 583 | llvm::Triple HIPTriple(DeviceTripleStr); |
| 584 | // Use the HIP and host triples as the key into the ToolChains map, |
| 585 | // because the device toolchain we create depends on both. |
| 586 | auto &HIPTC = ToolChains[HIPTriple.str() + "/" + HostTriple.str()]; |
| 587 | if (!HIPTC) { |
| 588 | HIPTC = llvm::make_unique<toolchains::HIPToolChain>( |
| 589 | *this, HIPTriple, *HostTC, C.getInputArgs()); |
| 590 | } |
| 591 | C.addOffloadDeviceToolChain(HIPTC.get(), OFK); |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 595 | // OpenMP |
| 596 | // |
| 597 | // We need to generate an OpenMP toolchain if the user specified targets with |
| 598 | // the -fopenmp-targets option. |
| 599 | if (Arg *OpenMPTargets = |
| 600 | C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) { |
| 601 | if (OpenMPTargets->getNumValues()) { |
| 602 | // We expect that -fopenmp-targets is always used in conjunction with the |
| 603 | // option -fopenmp specifying a valid runtime with offloading support, |
| 604 | // i.e. libomp or libiomp. |
| 605 | bool HasValidOpenMPRuntime = C.getInputArgs().hasFlag( |
| 606 | options::OPT_fopenmp, options::OPT_fopenmp_EQ, |
| 607 | options::OPT_fno_openmp, false); |
| 608 | if (HasValidOpenMPRuntime) { |
| 609 | OpenMPRuntimeKind OpenMPKind = getOpenMPRuntime(C.getInputArgs()); |
| 610 | HasValidOpenMPRuntime = |
| 611 | OpenMPKind == OMPRT_OMP || OpenMPKind == OMPRT_IOMP5; |
| 612 | } |
| 613 | |
| 614 | if (HasValidOpenMPRuntime) { |
| 615 | llvm::StringMap<const char *> FoundNormalizedTriples; |
| 616 | for (const char *Val : OpenMPTargets->getValues()) { |
| 617 | llvm::Triple TT(Val); |
| 618 | std::string NormalizedName = TT.normalize(); |
| 619 | |
| 620 | // Make sure we don't have a duplicate triple. |
| 621 | auto Duplicate = FoundNormalizedTriples.find(NormalizedName); |
| 622 | if (Duplicate != FoundNormalizedTriples.end()) { |
| 623 | Diag(clang::diag::warn_drv_omp_offload_target_duplicate) |
| 624 | << Val << Duplicate->second; |
| 625 | continue; |
| 626 | } |
| 627 | |
| 628 | // Store the current triple so that we can check for duplicates in the |
| 629 | // following iterations. |
| 630 | FoundNormalizedTriples[NormalizedName] = Val; |
| 631 | |
| 632 | // If the specified target is invalid, emit a diagnostic. |
| 633 | if (TT.getArch() == llvm::Triple::UnknownArch) |
| 634 | Diag(clang::diag::err_drv_invalid_omp_target) << Val; |
| 635 | else { |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 636 | const ToolChain *TC; |
| 637 | // CUDA toolchains have to be selected differently. They pair host |
| 638 | // and device in their implementation. |
| 639 | if (TT.isNVPTX()) { |
| 640 | const ToolChain *HostTC = |
| 641 | C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 642 | assert(HostTC && "Host toolchain should be always defined."); |
| 643 | auto &CudaTC = |
Gheorghe-Teodor Bercea | 5289843 | 2017-08-09 01:02:19 +0000 | [diff] [blame] | 644 | ToolChains[TT.str() + "/" + HostTC->getTriple().normalize()]; |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 645 | if (!CudaTC) |
| 646 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
Gheorghe-Teodor Bercea | 2c92693 | 2017-08-08 14:33:05 +0000 | [diff] [blame] | 647 | *this, TT, *HostTC, C.getInputArgs(), Action::OFK_OpenMP); |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 648 | TC = CudaTC.get(); |
| 649 | } else |
| 650 | TC = &getToolChain(C.getInputArgs(), TT); |
| 651 | C.addOffloadDeviceToolChain(TC, Action::OFK_OpenMP); |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } else |
| 655 | Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets); |
| 656 | } else |
| 657 | Diag(clang::diag::warn_drv_empty_joined_argument) |
| 658 | << OpenMPTargets->getAsString(C.getInputArgs()); |
| 659 | } |
| 660 | |
| 661 | // |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 662 | // TODO: Add support for other offloading programming models here. |
| 663 | // |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 666 | /// Looks the given directories for the specified file. |
| 667 | /// |
| 668 | /// \param[out] FilePath File path, if the file was found. |
| 669 | /// \param[in] Dirs Directories used for the search. |
| 670 | /// \param[in] FileName Name of the file to search for. |
| 671 | /// \return True if file was found. |
| 672 | /// |
| 673 | /// Looks for file specified by FileName sequentially in directories specified |
| 674 | /// by Dirs. |
| 675 | /// |
| 676 | static bool searchForFile(SmallVectorImpl<char> &FilePath, |
| 677 | ArrayRef<std::string> Dirs, |
| 678 | StringRef FileName) { |
| 679 | SmallString<128> WPath; |
| 680 | for (const StringRef &Dir : Dirs) { |
| 681 | if (Dir.empty()) |
| 682 | continue; |
| 683 | WPath.clear(); |
| 684 | llvm::sys::path::append(WPath, Dir, FileName); |
| 685 | llvm::sys::path::native(WPath); |
| 686 | if (llvm::sys::fs::is_regular_file(WPath)) { |
| 687 | FilePath = std::move(WPath); |
| 688 | return true; |
| 689 | } |
| 690 | } |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | bool Driver::readConfigFile(StringRef FileName) { |
| 695 | // Try reading the given file. |
| 696 | SmallVector<const char *, 32> NewCfgArgs; |
| 697 | if (!llvm::cl::readConfigFile(FileName, Saver, NewCfgArgs)) { |
| 698 | Diag(diag::err_drv_cannot_read_config_file) << FileName; |
| 699 | return true; |
| 700 | } |
| 701 | |
| 702 | // Read options from config file. |
| 703 | llvm::SmallString<128> CfgFileName(FileName); |
| 704 | llvm::sys::path::native(CfgFileName); |
| 705 | ConfigFile = CfgFileName.str(); |
| 706 | bool ContainErrors; |
| 707 | CfgOptions = llvm::make_unique<InputArgList>( |
| 708 | ParseArgStrings(NewCfgArgs, ContainErrors)); |
| 709 | if (ContainErrors) { |
| 710 | CfgOptions.reset(); |
| 711 | return true; |
| 712 | } |
| 713 | |
| 714 | if (CfgOptions->hasArg(options::OPT_config)) { |
| 715 | CfgOptions.reset(); |
| 716 | Diag(diag::err_drv_nested_config_file); |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | // Claim all arguments that come from a configuration file so that the driver |
| 721 | // does not warn on any that is unused. |
| 722 | for (Arg *A : *CfgOptions) |
| 723 | A->claim(); |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | bool Driver::loadConfigFile() { |
| 728 | std::string CfgFileName; |
| 729 | bool FileSpecifiedExplicitly = false; |
| 730 | |
| 731 | // Process options that change search path for config files. |
| 732 | if (CLOptions) { |
| 733 | if (CLOptions->hasArg(options::OPT_config_system_dir_EQ)) { |
| 734 | SmallString<128> CfgDir; |
| 735 | CfgDir.append( |
| 736 | CLOptions->getLastArgValue(options::OPT_config_system_dir_EQ)); |
| 737 | if (!CfgDir.empty()) { |
Sam McCall | 296d832 | 2018-01-02 09:35:10 +0000 | [diff] [blame] | 738 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 739 | SystemConfigDir.clear(); |
| 740 | else |
| 741 | SystemConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 742 | } |
| 743 | } |
| 744 | if (CLOptions->hasArg(options::OPT_config_user_dir_EQ)) { |
| 745 | SmallString<128> CfgDir; |
| 746 | CfgDir.append( |
| 747 | CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ)); |
| 748 | if (!CfgDir.empty()) { |
Sam McCall | 296d832 | 2018-01-02 09:35:10 +0000 | [diff] [blame] | 749 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 750 | UserConfigDir.clear(); |
| 751 | else |
| 752 | UserConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | // First try to find config file specified in command line. |
| 758 | if (CLOptions) { |
| 759 | std::vector<std::string> ConfigFiles = |
| 760 | CLOptions->getAllArgValues(options::OPT_config); |
| 761 | if (ConfigFiles.size() > 1) { |
| 762 | Diag(diag::err_drv_duplicate_config); |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | if (!ConfigFiles.empty()) { |
| 767 | CfgFileName = ConfigFiles.front(); |
| 768 | assert(!CfgFileName.empty()); |
| 769 | |
| 770 | // If argument contains directory separator, treat it as a path to |
| 771 | // configuration file. |
| 772 | if (llvm::sys::path::has_parent_path(CfgFileName)) { |
| 773 | SmallString<128> CfgFilePath; |
| 774 | if (llvm::sys::path::is_relative(CfgFileName)) |
| 775 | llvm::sys::fs::current_path(CfgFilePath); |
| 776 | llvm::sys::path::append(CfgFilePath, CfgFileName); |
| 777 | if (!llvm::sys::fs::is_regular_file(CfgFilePath)) { |
| 778 | Diag(diag::err_drv_config_file_not_exist) << CfgFilePath; |
| 779 | return true; |
| 780 | } |
| 781 | return readConfigFile(CfgFilePath); |
| 782 | } |
| 783 | |
| 784 | FileSpecifiedExplicitly = true; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // If config file is not specified explicitly, try to deduce configuration |
| 789 | // from executable name. For instance, an executable 'armv7l-clang' will |
| 790 | // search for config file 'armv7l-clang.cfg'. |
| 791 | if (CfgFileName.empty() && !ClangNameParts.TargetPrefix.empty()) |
| 792 | CfgFileName = ClangNameParts.TargetPrefix + '-' + ClangNameParts.ModeSuffix; |
| 793 | |
| 794 | if (CfgFileName.empty()) |
| 795 | return false; |
| 796 | |
| 797 | // Determine architecture part of the file name, if it is present. |
| 798 | StringRef CfgFileArch = CfgFileName; |
| 799 | size_t ArchPrefixLen = CfgFileArch.find('-'); |
| 800 | if (ArchPrefixLen == StringRef::npos) |
| 801 | ArchPrefixLen = CfgFileArch.size(); |
| 802 | llvm::Triple CfgTriple; |
| 803 | CfgFileArch = CfgFileArch.take_front(ArchPrefixLen); |
| 804 | CfgTriple = llvm::Triple(llvm::Triple::normalize(CfgFileArch)); |
| 805 | if (CfgTriple.getArch() == llvm::Triple::ArchType::UnknownArch) |
| 806 | ArchPrefixLen = 0; |
| 807 | |
| 808 | if (!StringRef(CfgFileName).endswith(".cfg")) |
| 809 | CfgFileName += ".cfg"; |
| 810 | |
| 811 | // If config file starts with architecture name and command line options |
| 812 | // redefine architecture (with options like -m32 -LE etc), try finding new |
| 813 | // config file with that architecture. |
| 814 | SmallString<128> FixedConfigFile; |
| 815 | size_t FixedArchPrefixLen = 0; |
| 816 | if (ArchPrefixLen) { |
| 817 | // Get architecture name from config file name like 'i386.cfg' or |
| 818 | // 'armv7l-clang.cfg'. |
| 819 | // Check if command line options changes effective triple. |
| 820 | llvm::Triple EffectiveTriple = computeTargetTriple(*this, |
| 821 | CfgTriple.getTriple(), *CLOptions); |
| 822 | if (CfgTriple.getArch() != EffectiveTriple.getArch()) { |
| 823 | FixedConfigFile = EffectiveTriple.getArchName(); |
| 824 | FixedArchPrefixLen = FixedConfigFile.size(); |
| 825 | // Append the rest of original file name so that file name transforms |
| 826 | // like: i386-clang.cfg -> x86_64-clang.cfg. |
| 827 | if (ArchPrefixLen < CfgFileName.size()) |
| 828 | FixedConfigFile += CfgFileName.substr(ArchPrefixLen); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // Prepare list of directories where config file is searched for. |
| 833 | SmallVector<std::string, 3> CfgFileSearchDirs; |
| 834 | CfgFileSearchDirs.push_back(UserConfigDir); |
| 835 | CfgFileSearchDirs.push_back(SystemConfigDir); |
| 836 | CfgFileSearchDirs.push_back(Dir); |
| 837 | |
| 838 | // Try to find config file. First try file with corrected architecture. |
| 839 | llvm::SmallString<128> CfgFilePath; |
| 840 | if (!FixedConfigFile.empty()) { |
| 841 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 842 | return readConfigFile(CfgFilePath); |
| 843 | // If 'x86_64-clang.cfg' was not found, try 'x86_64.cfg'. |
| 844 | FixedConfigFile.resize(FixedArchPrefixLen); |
| 845 | FixedConfigFile.append(".cfg"); |
| 846 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 847 | return readConfigFile(CfgFilePath); |
| 848 | } |
| 849 | |
| 850 | // Then try original file name. |
| 851 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 852 | return readConfigFile(CfgFilePath); |
| 853 | |
| 854 | // Finally try removing driver mode part: 'x86_64-clang.cfg' -> 'x86_64.cfg'. |
| 855 | if (!ClangNameParts.ModeSuffix.empty() && |
| 856 | !ClangNameParts.TargetPrefix.empty()) { |
| 857 | CfgFileName.assign(ClangNameParts.TargetPrefix); |
| 858 | CfgFileName.append(".cfg"); |
| 859 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 860 | return readConfigFile(CfgFilePath); |
| 861 | } |
| 862 | |
| 863 | // Report error but only if config file was specified explicitly, by option |
| 864 | // --config. If it was deduced from executable name, it is not an error. |
| 865 | if (FileSpecifiedExplicitly) { |
| 866 | Diag(diag::err_drv_config_file_not_found) << CfgFileName; |
| 867 | for (const std::string &SearchDir : CfgFileSearchDirs) |
| 868 | if (!SearchDir.empty()) |
| 869 | Diag(diag::note_drv_config_file_searched_in) << SearchDir; |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | return false; |
| 874 | } |
| 875 | |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 876 | Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 877 | llvm::PrettyStackTraceString CrashInfo("Compilation construction"); |
| 878 | |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 879 | // FIXME: Handle environment options which affect driver behavior, somewhere |
Bill Wendling | adbeb9f | 2012-03-12 21:24:57 +0000 | [diff] [blame] | 880 | // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS. |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 881 | |
David Majnemer | 85c25b4 | 2016-07-24 17:44:03 +0000 | [diff] [blame] | 882 | if (Optional<std::string> CompilerPathValue = |
| 883 | llvm::sys::Process::GetEnv("COMPILER_PATH")) { |
| 884 | StringRef CompilerPath = *CompilerPathValue; |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 885 | while (!CompilerPath.empty()) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 886 | std::pair<StringRef, StringRef> Split = |
| 887 | CompilerPath.split(llvm::sys::EnvPathSeparator); |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 888 | PrefixDirs.push_back(Split.first); |
| 889 | CompilerPath = Split.second; |
| 890 | } |
| 891 | } |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 892 | |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 893 | // We look for the driver mode option early, because the mode can affect |
| 894 | // how other options are parsed. |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 895 | ParseDriverMode(ClangExecutable, ArgList.slice(1)); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 896 | |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 897 | // FIXME: What are we going to do with -V and -b? |
| 898 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 899 | // Arguments specified in command line. |
| 900 | bool ContainsError; |
| 901 | CLOptions = llvm::make_unique<InputArgList>( |
| 902 | ParseArgStrings(ArgList.slice(1), ContainsError)); |
| 903 | |
| 904 | // Try parsing configuration file. |
| 905 | if (!ContainsError) |
| 906 | ContainsError = loadConfigFile(); |
| 907 | bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr); |
| 908 | |
| 909 | // All arguments, from both config file and command line. |
| 910 | InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions) |
| 911 | : std::move(*CLOptions)); |
| 912 | if (HasConfigFile) |
| 913 | for (auto *Opt : *CLOptions) { |
Serge Pavlov | f015a56 | 2018-04-26 06:28:47 +0000 | [diff] [blame] | 914 | if (Opt->getOption().matches(options::OPT_config)) |
| 915 | continue; |
| 916 | unsigned Index = Args.MakeIndex(Opt->getSpelling()); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 917 | const Arg *BaseArg = &Opt->getBaseArg(); |
| 918 | if (BaseArg == Opt) |
| 919 | BaseArg = nullptr; |
| 920 | Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Opt->getSpelling(), |
Serge Pavlov | f015a56 | 2018-04-26 06:28:47 +0000 | [diff] [blame] | 921 | Index, BaseArg); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 922 | Copy->getValues() = Opt->getValues(); |
| 923 | if (Opt->isClaimed()) |
| 924 | Copy->claim(); |
| 925 | Args.append(Copy); |
| 926 | } |
| 927 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 928 | // FIXME: This stuff needs to go into the Compilation, not the driver. |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 929 | bool CCCPrintPhases; |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 930 | |
Filipe Cabecinhas | 136f35e | 2015-07-18 06:35:24 +0000 | [diff] [blame] | 931 | // Silence driver warnings if requested |
| 932 | Diags.setIgnoreAllWarnings(Args.hasArg(options::OPT_w)); |
| 933 | |
Rafael Espindola | 59ae799 | 2009-12-07 18:28:29 +0000 | [diff] [blame] | 934 | // -no-canonical-prefixes is used very early in main. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 935 | Args.ClaimAllArgs(options::OPT_no_canonical_prefixes); |
Rafael Espindola | 59ae799 | 2009-12-07 18:28:29 +0000 | [diff] [blame] | 936 | |
Daniel Dunbar | 926f81f | 2010-08-02 02:38:03 +0000 | [diff] [blame] | 937 | // Ignore -pipe. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 938 | Args.ClaimAllArgs(options::OPT_pipe); |
Daniel Dunbar | 926f81f | 2010-08-02 02:38:03 +0000 | [diff] [blame] | 939 | |
Daniel Dunbar | acd6957 | 2009-12-04 21:55:23 +0000 | [diff] [blame] | 940 | // Extract -ccc args. |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 941 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 942 | // FIXME: We need to figure out where this behavior should live. Most of it |
| 943 | // should be outside in the client; the parts that aren't should have proper |
| 944 | // options, either by introducing new ones or by overloading gcc ones like -V |
| 945 | // or -b. |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 946 | CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 947 | CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings); |
| 948 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 949 | CCCGenericGCCName = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 950 | CCCUsePCH = |
| 951 | Args.hasFlag(options::OPT_ccc_pch_is_pch, options::OPT_ccc_pch_is_pth); |
Bruno Cardoso Lopes | 52dfe71 | 2017-04-12 21:46:20 +0000 | [diff] [blame] | 952 | GenReproducer = Args.hasFlag(options::OPT_gen_reproducer, |
| 953 | options::OPT_fno_crash_diagnostics, |
| 954 | !!::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")); |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 955 | // FIXME: TargetTriple is used by the target-prefixed calls to as/ld |
Joerg Sonnenberger | 17d7551 | 2012-02-22 19:15:16 +0000 | [diff] [blame] | 956 | // and getToolChain is const. |
Hans Wennborg | 2e27459 | 2013-08-13 23:38:57 +0000 | [diff] [blame] | 957 | if (IsCLMode()) { |
Hans Wennborg | 73609a0 | 2014-03-28 01:19:04 +0000 | [diff] [blame] | 958 | // clang-cl targets MSVC-style Win32. |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 959 | llvm::Triple T(TargetTriple); |
Hans Wennborg | 9d9ce7a | 2014-03-28 20:49:28 +0000 | [diff] [blame] | 960 | T.setOS(llvm::Triple::Win32); |
Hans Wennborg | 0f0e8d6 | 2015-09-18 17:11:50 +0000 | [diff] [blame] | 961 | T.setVendor(llvm::Triple::PC); |
Hans Wennborg | 9d9ce7a | 2014-03-28 20:49:28 +0000 | [diff] [blame] | 962 | T.setEnvironment(llvm::Triple::MSVC); |
Oleg Ranevskyy | 8052318 | 2017-08-31 20:31:30 +0000 | [diff] [blame] | 963 | T.setObjectFormat(llvm::Triple::COFF); |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 964 | TargetTriple = T.str(); |
Hans Wennborg | 2e27459 | 2013-08-13 23:38:57 +0000 | [diff] [blame] | 965 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 966 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 967 | TargetTriple = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 968 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 969 | Dir = InstalledDir = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 970 | for (const Arg *A : Args.filtered(options::OPT_B)) { |
Benjamin Kramer | 1a648d1 | 2011-02-08 20:31:42 +0000 | [diff] [blame] | 971 | A->claim(); |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 972 | PrefixDirs.push_back(A->getValue(0)); |
Benjamin Kramer | 1a648d1 | 2011-02-08 20:31:42 +0000 | [diff] [blame] | 973 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 974 | if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 975 | SysRoot = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 976 | if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ)) |
Peter Collingbourne | 9d9e1fc | 2013-05-27 21:40:20 +0000 | [diff] [blame] | 977 | DyldPrefix = A->getValue(); |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 978 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 979 | if (const Arg *A = Args.getLastArg(options::OPT_resource_dir)) |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 980 | ResourceDir = A->getValue(); |
Jim Grosbach | 061dabf | 2013-03-12 20:17:58 +0000 | [diff] [blame] | 981 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 982 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) { |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 983 | SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue()) |
| 984 | .Case("cwd", SaveTempsCwd) |
| 985 | .Case("obj", SaveTempsObj) |
| 986 | .Default(SaveTempsCwd); |
| 987 | } |
| 988 | |
Steven Wu | 1257cd8 | 2016-05-18 17:04:52 +0000 | [diff] [blame] | 989 | setLTOMode(Args); |
| 990 | |
Steven Wu | 844ab6a | 2016-11-16 06:06:44 +0000 | [diff] [blame] | 991 | // Process -fembed-bitcode= flags. |
| 992 | if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) { |
| 993 | StringRef Name = A->getValue(); |
| 994 | unsigned Model = llvm::StringSwitch<unsigned>(Name) |
| 995 | .Case("off", EmbedNone) |
| 996 | .Case("all", EmbedBitcode) |
| 997 | .Case("bitcode", EmbedBitcode) |
| 998 | .Case("marker", EmbedMarker) |
| 999 | .Default(~0U); |
| 1000 | if (Model == ~0U) { |
| 1001 | Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) |
| 1002 | << Name; |
| 1003 | } else |
| 1004 | BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model); |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1007 | std::unique_ptr<llvm::opt::InputArgList> UArgs = |
| 1008 | llvm::make_unique<InputArgList>(std::move(Args)); |
| 1009 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 1010 | // Perform the default argument translations. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1011 | DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs); |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 1012 | |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1013 | // Owned by the host. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 1014 | const ToolChain &TC = getToolChain( |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1015 | *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs)); |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1016 | |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 1017 | // The compilation takes ownership of Args. |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 1018 | Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs, |
| 1019 | ContainsError); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1020 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1021 | if (!HandleImmediateArgs(*C)) |
| 1022 | return C; |
| 1023 | |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1024 | // Construct the list of inputs. |
| 1025 | InputList Inputs; |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 1026 | BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs); |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1027 | |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 1028 | // Populate the tool chains for the offloading devices, if any. |
| 1029 | CreateOffloadingDeviceToolChains(*C, Inputs); |
Justin Lebar | 0e450a5 | 2016-03-30 23:30:25 +0000 | [diff] [blame] | 1030 | |
Chandler Carruth | 7f1417f | 2012-01-24 10:43:44 +0000 | [diff] [blame] | 1031 | // Construct the list of abstract actions to perform for this compilation. On |
Tim Northover | 157d911 | 2014-01-16 08:48:16 +0000 | [diff] [blame] | 1032 | // MachO targets this uses the driver-driver and universal actions. |
| 1033 | if (TC.getTriple().isOSBinFormatMachO()) |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1034 | BuildUniversalActions(*C, C->getDefaultToolChain(), Inputs); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1035 | else |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1036 | BuildActions(*C, C->getArgs(), Inputs, C->getActions()); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1037 | |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1038 | if (CCCPrintPhases) { |
Daniel Dunbar | eb843be | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 1039 | PrintActions(*C); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1040 | return C; |
| 1041 | } |
| 1042 | |
| 1043 | BuildJobs(*C); |
Daniel Dunbar | adc91e6 | 2009-03-15 01:38:15 +0000 | [diff] [blame] | 1044 | |
| 1045 | return C; |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Justin Bogner | ed9cbe0 | 2015-07-09 06:58:31 +0000 | [diff] [blame] | 1048 | static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) { |
| 1049 | llvm::opt::ArgStringList ASL; |
| 1050 | for (const auto *A : Args) |
| 1051 | A->render(Args, ASL); |
| 1052 | |
| 1053 | for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) { |
| 1054 | if (I != ASL.begin()) |
| 1055 | OS << ' '; |
| 1056 | Command::printArg(OS, *I, true); |
| 1057 | } |
| 1058 | OS << '\n'; |
| 1059 | } |
| 1060 | |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1061 | bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename, |
| 1062 | SmallString<128> &CrashDiagDir) { |
| 1063 | using namespace llvm::sys; |
| 1064 | assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() && |
| 1065 | "Only knows about .crash files on Darwin"); |
| 1066 | |
| 1067 | // The .crash file can be found on at ~/Library/Logs/DiagnosticReports/ |
| 1068 | // (or /Library/Logs/DiagnosticReports for root) and has the filename pattern |
| 1069 | // clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash. |
| 1070 | path::home_directory(CrashDiagDir); |
| 1071 | if (CrashDiagDir.startswith("/var/root")) |
| 1072 | CrashDiagDir = "/"; |
| 1073 | path::append(CrashDiagDir, "Library/Logs/DiagnosticReports"); |
| 1074 | int PID = |
| 1075 | #if LLVM_ON_UNIX |
| 1076 | getpid(); |
| 1077 | #else |
| 1078 | 0; |
| 1079 | #endif |
| 1080 | std::error_code EC; |
| 1081 | fs::file_status FileStatus; |
| 1082 | TimePoint<> LastAccessTime; |
| 1083 | SmallString<128> CrashFilePath; |
| 1084 | // Lookup the .crash files and get the one generated by a subprocess spawned |
| 1085 | // by this driver invocation. |
| 1086 | for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd; |
| 1087 | File != FileEnd && !EC; File.increment(EC)) { |
| 1088 | StringRef FileName = path::filename(File->path()); |
| 1089 | if (!FileName.startswith(Name)) |
| 1090 | continue; |
| 1091 | if (fs::status(File->path(), FileStatus)) |
| 1092 | continue; |
| 1093 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile = |
| 1094 | llvm::MemoryBuffer::getFile(File->path()); |
| 1095 | if (!CrashFile) |
| 1096 | continue; |
| 1097 | // The first line should start with "Process:", otherwise this isn't a real |
| 1098 | // .crash file. |
| 1099 | StringRef Data = CrashFile.get()->getBuffer(); |
| 1100 | if (!Data.startswith("Process:")) |
| 1101 | continue; |
| 1102 | // Parse parent process pid line, e.g: "Parent Process: clang-4.0 [79141]" |
| 1103 | size_t ParentProcPos = Data.find("Parent Process:"); |
| 1104 | if (ParentProcPos == StringRef::npos) |
| 1105 | continue; |
| 1106 | size_t LineEnd = Data.find_first_of("\n", ParentProcPos); |
| 1107 | if (LineEnd == StringRef::npos) |
| 1108 | continue; |
| 1109 | StringRef ParentProcess = Data.slice(ParentProcPos+15, LineEnd).trim(); |
| 1110 | int OpenBracket = -1, CloseBracket = -1; |
| 1111 | for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) { |
| 1112 | if (ParentProcess[i] == '[') |
| 1113 | OpenBracket = i; |
| 1114 | if (ParentProcess[i] == ']') |
| 1115 | CloseBracket = i; |
| 1116 | } |
| 1117 | // Extract the parent process PID from the .crash file and check whether |
| 1118 | // it matches this driver invocation pid. |
| 1119 | int CrashPID; |
| 1120 | if (OpenBracket < 0 || CloseBracket < 0 || |
| 1121 | ParentProcess.slice(OpenBracket + 1, CloseBracket) |
| 1122 | .getAsInteger(10, CrashPID) || CrashPID != PID) { |
| 1123 | continue; |
| 1124 | } |
| 1125 | |
| 1126 | // Found a .crash file matching the driver pid. To avoid getting an older |
| 1127 | // and misleading crash file, continue looking for the most recent. |
| 1128 | // FIXME: the driver can dispatch multiple cc1 invocations, leading to |
| 1129 | // multiple crashes poiting to the same parent process. Since the driver |
| 1130 | // does not collect pid information for the dispatched invocation there's |
| 1131 | // currently no way to distinguish among them. |
| 1132 | const auto FileAccessTime = FileStatus.getLastModificationTime(); |
| 1133 | if (FileAccessTime > LastAccessTime) { |
| 1134 | CrashFilePath.assign(File->path()); |
| 1135 | LastAccessTime = FileAccessTime; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | // If found, copy it over to the location of other reproducer files. |
| 1140 | if (!CrashFilePath.empty()) { |
| 1141 | EC = fs::copy_file(CrashFilePath, ReproCrashFilename); |
| 1142 | if (EC) |
| 1143 | return false; |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 1150 | // When clang crashes, produce diagnostic information including the fully |
| 1151 | // preprocessed source file(s). Request that the developer attach the |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1152 | // diagnostic information to a bug report. |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1153 | void Driver::generateCompilationDiagnostics( |
| 1154 | Compilation &C, const Command &FailingCommand, |
| 1155 | StringRef AdditionalInformation, CompilationDiagnosticReport *Report) { |
Chad Rosier | 877c0a2 | 2012-02-22 00:30:39 +0000 | [diff] [blame] | 1156 | if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics)) |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 1157 | return; |
Chad Rosier | bee5a1d | 2012-03-07 00:30:40 +0000 | [diff] [blame] | 1158 | |
Chad Rosier | dbf46a1 | 2013-02-01 18:30:26 +0000 | [diff] [blame] | 1159 | // Don't try to generate diagnostics for link or dsymutil jobs. |
Justin Bogner | e1a33d1 | 2014-10-20 21:02:05 +0000 | [diff] [blame] | 1160 | if (FailingCommand.getCreator().isLinkJob() || |
| 1161 | FailingCommand.getCreator().isDsymutilJob()) |
Chad Rosier | 877c0a2 | 2012-02-22 00:30:39 +0000 | [diff] [blame] | 1162 | return; |
| 1163 | |
Chad Rosier | 8179f11 | 2012-06-19 17:51:34 +0000 | [diff] [blame] | 1164 | // Print the version of the compiler. |
| 1165 | PrintVersion(C, llvm::errs()); |
| 1166 | |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1167 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1168 | << "PLEASE submit a bug report to " BUG_REPORT_URL " and include the " |
| 1169 | "crash backtrace, preprocessed source, and associated run script."; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1170 | |
| 1171 | // Suppress driver output and emit preprocessor output to temp file. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 1172 | Mode = CPPMode; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1173 | CCGenDiagnostics = true; |
| 1174 | |
Chad Rosier | cdb008d | 2011-11-02 21:29:05 +0000 | [diff] [blame] | 1175 | // Save the original job command(s). |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 1176 | Command Cmd = FailingCommand; |
Chad Rosier | cdb008d | 2011-11-02 21:29:05 +0000 | [diff] [blame] | 1177 | |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 1178 | // Keep track of whether we produce any errors while trying to produce |
| 1179 | // preprocessed sources. |
| 1180 | DiagnosticErrorTrap Trap(Diags); |
| 1181 | |
| 1182 | // Suppress tool output. |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1183 | C.initCompilationForDiagnostics(); |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1184 | |
| 1185 | // Construct the list of inputs. |
| 1186 | InputList Inputs; |
| 1187 | BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1188 | |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1189 | for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) { |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1190 | bool IgnoreInput = false; |
| 1191 | |
| 1192 | // Ignore input from stdin or any inputs that cannot be preprocessed. |
Paul Robinson | f44157d | 2014-04-28 22:24:44 +0000 | [diff] [blame] | 1193 | // Check type first as not all linker inputs have a value. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1194 | if (types::getPreprocessedType(it->first) == types::TY_INVALID) { |
Paul Robinson | f44157d | 2014-04-28 22:24:44 +0000 | [diff] [blame] | 1195 | IgnoreInput = true; |
| 1196 | } else if (!strcmp(it->second->getValue(), "-")) { |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1197 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1198 | << "Error generating preprocessed source(s) - " |
| 1199 | "ignoring input from stdin."; |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1200 | IgnoreInput = true; |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | if (IgnoreInput) { |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1204 | it = Inputs.erase(it); |
| 1205 | ie = Inputs.end(); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1206 | } else { |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1207 | ++it; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1208 | } |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1209 | } |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1210 | |
Chad Rosier | c5103c3 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 1211 | if (Inputs.empty()) { |
| 1212 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1213 | << "Error generating preprocessed source(s) - " |
| 1214 | "no preprocessable inputs."; |
Chad Rosier | c5103c3 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 1215 | return; |
| 1216 | } |
| 1217 | |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1218 | // Don't attempt to generate preprocessed files if multiple -arch options are |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1219 | // used, unless they're all duplicates. |
| 1220 | llvm::StringSet<> ArchNames; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 1221 | for (const Arg *A : C.getArgs()) { |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1222 | if (A->getOption().matches(options::OPT_arch)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1223 | StringRef ArchName = A->getValue(); |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1224 | ArchNames.insert(ArchName); |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1225 | } |
| 1226 | } |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1227 | if (ArchNames.size() > 1) { |
| 1228 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1229 | << "Error generating preprocessed source(s) - cannot generate " |
| 1230 | "preprocessed source with multiple -arch options."; |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1231 | return; |
| 1232 | } |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1233 | |
Chandler Carruth | 7f1417f | 2012-01-24 10:43:44 +0000 | [diff] [blame] | 1234 | // Construct the list of abstract actions to perform for this compilation. On |
| 1235 | // Darwin OSes this uses the driver-driver and builds universal actions. |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1236 | const ToolChain &TC = C.getDefaultToolChain(); |
Tim Northover | 157d911 | 2014-01-16 08:48:16 +0000 | [diff] [blame] | 1237 | if (TC.getTriple().isOSBinFormatMachO()) |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1238 | BuildUniversalActions(C, TC, Inputs); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1239 | else |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1240 | BuildActions(C, C.getArgs(), Inputs, C.getActions()); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1241 | |
| 1242 | BuildJobs(C); |
| 1243 | |
| 1244 | // If there were errors building the compilation, quit now. |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 1245 | if (Trap.hasErrorOccurred()) { |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1246 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1247 | << "Error generating preprocessed source(s)."; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | // Generate preprocessed output. |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1252 | SmallVector<std::pair<int, const Command *>, 4> FailingCommands; |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1253 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1254 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1255 | // If any of the preprocessing commands failed, clean up and exit. |
| 1256 | if (!FailingCommands.empty()) { |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1257 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1258 | << "Error generating preprocessed source(s)."; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1259 | return; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1260 | } |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1261 | |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1262 | const ArgStringList &TempFiles = C.getTempFiles(); |
| 1263 | if (TempFiles.empty()) { |
| 1264 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1265 | << "Error generating preprocessed source(s)."; |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1266 | return; |
| 1267 | } |
| 1268 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1269 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1270 | << "\n********************\n\n" |
| 1271 | "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n" |
| 1272 | "Preprocessed source(s) and associated run script(s) are located at:"; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1273 | |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1274 | SmallString<128> VFS; |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1275 | SmallString<128> ReproCrashFilename; |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1276 | for (const char *TempFile : TempFiles) { |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1277 | Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile; |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1278 | if (Report) |
| 1279 | Report->TemporaryFiles.push_back(TempFile); |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1280 | if (ReproCrashFilename.empty()) { |
| 1281 | ReproCrashFilename = TempFile; |
| 1282 | llvm::sys::path::replace_extension(ReproCrashFilename, ".crash"); |
| 1283 | } |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1284 | if (StringRef(TempFile).endswith(".cache")) { |
| 1285 | // In some cases (modules) we'll dump extra data to help with reproducing |
| 1286 | // the crash into a directory next to the output. |
| 1287 | VFS = llvm::sys::path::filename(TempFile); |
| 1288 | llvm::sys::path::append(VFS, "vfs", "vfs.yaml"); |
| 1289 | } |
| 1290 | } |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Assume associated files are based off of the first temporary file. |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 1293 | CrashReportInfo CrashInfo(TempFiles[0], VFS); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1294 | |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1295 | llvm::SmallString<128> Script(CrashInfo.Filename); |
| 1296 | llvm::sys::path::replace_extension(Script, "sh"); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1297 | std::error_code EC; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1298 | llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1299 | if (EC) { |
| 1300 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1301 | << "Error generating run script: " << Script << " " << EC.message(); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1302 | } else { |
Justin Bogner | 42220ed | 2015-03-12 00:14:35 +0000 | [diff] [blame] | 1303 | ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n" |
Justin Bogner | ed9cbe0 | 2015-07-09 06:58:31 +0000 | [diff] [blame] | 1304 | << "# Driver args: "; |
| 1305 | printArgList(ScriptOS, C.getInputArgs()); |
| 1306 | ScriptOS << "# Original command: "; |
Justin Bogner | 42220ed | 2015-03-12 00:14:35 +0000 | [diff] [blame] | 1307 | Cmd.Print(ScriptOS, "\n", /*Quote=*/true); |
Justin Bogner | 33bdbc6 | 2014-10-21 18:03:08 +0000 | [diff] [blame] | 1308 | Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo); |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1309 | if (!AdditionalInformation.empty()) |
| 1310 | ScriptOS << "\n# Additional information: " << AdditionalInformation |
| 1311 | << "\n"; |
| 1312 | if (Report) |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1313 | Report->TemporaryFiles.push_back(Script.str()); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1314 | Diag(clang::diag::note_drv_command_failed_diag_msg) << Script; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1315 | } |
Saleem Abdulrasool | 3661a82 | 2015-01-12 02:33:09 +0000 | [diff] [blame] | 1316 | |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1317 | // On darwin, provide information about the .crash diagnostic report. |
| 1318 | if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) { |
| 1319 | SmallString<128> CrashDiagDir; |
| 1320 | if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) { |
| 1321 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1322 | << ReproCrashFilename.str(); |
| 1323 | } else { // Suggest a directory for the user to look for .crash files. |
| 1324 | llvm::sys::path::append(CrashDiagDir, Name); |
| 1325 | CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash"; |
| 1326 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1327 | << "Crash backtrace is located in"; |
| 1328 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1329 | << CrashDiagDir.str(); |
| 1330 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1331 | << "(choose the .crash file that corresponds to your crash)"; |
| 1332 | } |
| 1333 | } |
| 1334 | |
Saleem Abdulrasool | 3661a82 | 2015-01-12 02:33:09 +0000 | [diff] [blame] | 1335 | for (const auto &A : C.getArgs().filtered(options::OPT_frewrite_map_file, |
| 1336 | options::OPT_frewrite_map_file_EQ)) |
| 1337 | Diag(clang::diag::note_drv_command_failed_diag_msg) << A->getValue(); |
| 1338 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1339 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1340 | << "\n\n********************"; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1343 | void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) { |
Oleg Ranevskyy | cd51637 | 2016-01-05 19:54:39 +0000 | [diff] [blame] | 1344 | // Since commandLineFitsWithinSystemLimits() may underestimate system's capacity |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1345 | // if the tool does not support response files, there is a chance/ that things |
| 1346 | // will just work without a response file, so we silently just skip it. |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1347 | if (Cmd.getCreator().getResponseFilesSupport() == Tool::RF_None || |
Oleg Ranevskyy | cd51637 | 2016-01-05 19:54:39 +0000 | [diff] [blame] | 1348 | llvm::sys::commandLineFitsWithinSystemLimits(Cmd.getExecutable(), Cmd.getArguments())) |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1349 | return; |
| 1350 | |
| 1351 | std::string TmpName = GetTemporaryPath("response", "txt"); |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 1352 | Cmd.setResponseFile(C.addTempFile(C.getArgs().MakeArgString(TmpName))); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1355 | int Driver::ExecuteCompilation( |
| 1356 | Compilation &C, |
| 1357 | SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) { |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1358 | // Just print if -### was present. |
| 1359 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 1360 | C.getJobs().Print(llvm::errs(), "\n", true); |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | // If there were errors building the compilation, quit now. |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1365 | if (Diags.hasErrorOccurred()) |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1366 | return 1; |
| 1367 | |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1368 | // Set up response file names for each command, if necessary |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1369 | for (auto &Job : C.getJobs()) |
| 1370 | setUpResponseFiles(C, Job); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1371 | |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1372 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1373 | |
Daniel Dunbar | 0749479 | 2010-05-22 00:37:20 +0000 | [diff] [blame] | 1374 | // If the command succeeded, we are done. |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1375 | if (FailingCommands.empty()) |
| 1376 | return 0; |
Daniel Dunbar | 0749479 | 2010-05-22 00:37:20 +0000 | [diff] [blame] | 1377 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1378 | // Otherwise, remove result files and print extra information about abnormal |
| 1379 | // failures. |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 1380 | for (const auto &CmdPair : FailingCommands) { |
| 1381 | int Res = CmdPair.first; |
| 1382 | const Command *FailingCommand = CmdPair.second; |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1383 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1384 | // Remove result files if we're not saving temps. |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 1385 | if (!isSaveTempsEnabled()) { |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1386 | const JobAction *JA = cast<JobAction>(&FailingCommand->getSource()); |
| 1387 | C.CleanupFileMap(C.getResultFiles(), JA, true); |
| 1388 | |
| 1389 | // Failure result files are valid unless we crashed. |
| 1390 | if (Res < 0) |
| 1391 | C.CleanupFileMap(C.getFailureResultFiles(), JA, true); |
| 1392 | } |
| 1393 | |
| 1394 | // Print extra information about abnormal failures, if possible. |
| 1395 | // |
| 1396 | // This is ad-hoc, but we don't want to be excessively noisy. If the result |
Justin Bogner | 5aaf2e7 | 2014-06-26 20:59:36 +0000 | [diff] [blame] | 1397 | // status was 1, assume the command failed normally. In particular, if it |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1398 | // was the compiler then assume it gave a reasonable error code. Failures |
| 1399 | // in other tools are less common, and they generally have worse |
| 1400 | // diagnostics, so always print the diagnostic there. |
| 1401 | const Tool &FailingTool = FailingCommand->getCreator(); |
| 1402 | |
| 1403 | if (!FailingCommand->getCreator().hasGoodDiagnostics() || Res != 1) { |
| 1404 | // FIXME: See FIXME above regarding result code interpretation. |
| 1405 | if (Res < 0) |
| 1406 | Diag(clang::diag::err_drv_command_signalled) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1407 | << FailingTool.getShortName(); |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1408 | else |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1409 | Diag(clang::diag::err_drv_command_failed) << FailingTool.getShortName() |
| 1410 | << Res; |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1411 | } |
Peter Collingbourne | 119cfaa | 2011-11-21 00:01:05 +0000 | [diff] [blame] | 1412 | } |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1413 | return 0; |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Daniel Dunbar | a7b5e21 | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 1416 | void Driver::PrintHelp(bool ShowHidden) const { |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 1417 | unsigned IncludedFlagsBitmask; |
| 1418 | unsigned ExcludedFlagsBitmask; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1419 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1420 | getIncludeExcludeOptionFlagMasks(); |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 1421 | |
| 1422 | ExcludedFlagsBitmask |= options::NoDriverOption; |
| 1423 | if (!ShowHidden) |
| 1424 | ExcludedFlagsBitmask |= HelpHidden; |
| 1425 | |
| 1426 | getOpts().PrintHelp(llvm::outs(), Name.c_str(), DriverTitle.c_str(), |
George Rimar | 5dbfa4e | 2017-07-26 09:10:17 +0000 | [diff] [blame] | 1427 | IncludedFlagsBitmask, ExcludedFlagsBitmask, |
| 1428 | /*ShowAllAliases=*/false); |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1431 | void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1432 | // FIXME: The following handlers should use a callback mechanism, we don't |
| 1433 | // know what the client would like to do. |
Ted Kremenek | 4c0df3d | 2010-01-23 02:11:34 +0000 | [diff] [blame] | 1434 | OS << getClangFullVersion() << '\n'; |
Daniel Dunbar | b1024880 | 2009-03-26 16:09:13 +0000 | [diff] [blame] | 1435 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1436 | OS << "Target: " << TC.getTripleString() << '\n'; |
Daniel Dunbar | 10978e4 | 2009-06-16 23:32:58 +0000 | [diff] [blame] | 1437 | |
| 1438 | // Print the threading model. |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 1439 | if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) { |
| 1440 | // Don't print if the ToolChain would have barfed on it already |
| 1441 | if (TC.isThreadModelSupported(A->getValue())) |
| 1442 | OS << "Thread model: " << A->getValue(); |
| 1443 | } else |
| 1444 | OS << "Thread model: " << TC.getThreadModel(); |
| 1445 | OS << '\n'; |
Chandler Carruth | 9ade6a9 | 2015-08-05 17:07:33 +0000 | [diff] [blame] | 1446 | |
| 1447 | // Print out the install directory. |
| 1448 | OS << "InstalledDir: " << InstalledDir << '\n'; |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 1449 | |
| 1450 | // If configuration file was used, print its path. |
| 1451 | if (!ConfigFile.empty()) |
| 1452 | OS << "Configuration file: " << ConfigFile << '\n'; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1455 | /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories |
| 1456 | /// option. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1457 | static void PrintDiagnosticCategories(raw_ostream &OS) { |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1458 | // Skip the empty category. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1459 | for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max; |
| 1460 | ++i) |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1461 | OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n'; |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1464 | void Driver::HandleAutocompletions(StringRef PassedFlags) const { |
Yuka Takahashi | 503da8f | 2018-03-05 09:01:31 +0000 | [diff] [blame] | 1465 | if (PassedFlags == "") |
| 1466 | return; |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1467 | // Print out all options that start with a given argument. This is used for |
| 1468 | // shell autocompletion. |
| 1469 | std::vector<std::string> SuggestedCompletions; |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1470 | std::vector<std::string> Flags; |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1471 | |
| 1472 | unsigned short DisableFlags = |
| 1473 | options::NoDriverOption | options::Unsupported | options::Ignored; |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1474 | |
| 1475 | // Parse PassedFlags by "," as all the command-line flags are passed to this |
| 1476 | // function separated by "," |
| 1477 | StringRef TargetFlags = PassedFlags; |
| 1478 | while (TargetFlags != "") { |
| 1479 | StringRef CurFlag; |
| 1480 | std::tie(CurFlag, TargetFlags) = TargetFlags.split(","); |
| 1481 | Flags.push_back(std::string(CurFlag)); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1484 | // We want to show cc1-only options only when clang is invoked with -cc1 or |
| 1485 | // -Xclang. |
Yuka Takahashi | 503da8f | 2018-03-05 09:01:31 +0000 | [diff] [blame] | 1486 | if (std::find(Flags.begin(), Flags.end(), "-Xclang") != Flags.end() || |
| 1487 | std::find(Flags.begin(), Flags.end(), "-cc1") != Flags.end()) |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1488 | DisableFlags &= ~options::NoDriverOption; |
| 1489 | |
| 1490 | StringRef Cur; |
| 1491 | Cur = Flags.at(Flags.size() - 1); |
| 1492 | StringRef Prev; |
| 1493 | if (Flags.size() >= 2) { |
| 1494 | Prev = Flags.at(Flags.size() - 2); |
| 1495 | SuggestedCompletions = Opts->suggestValueCompletions(Prev, Cur); |
| 1496 | } |
| 1497 | |
| 1498 | if (SuggestedCompletions.empty()) |
| 1499 | SuggestedCompletions = Opts->suggestValueCompletions(Cur, ""); |
| 1500 | |
| 1501 | if (SuggestedCompletions.empty()) { |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1502 | // If the flag is in the form of "--autocomplete=-foo", |
| 1503 | // we were requested to print out all option names that start with "-foo". |
| 1504 | // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only". |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1505 | SuggestedCompletions = Opts->findByPrefix(Cur, DisableFlags); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1506 | |
| 1507 | // We have to query the -W flags manually as they're not in the OptTable. |
| 1508 | // TODO: Find a good way to add them to OptTable instead and them remove |
| 1509 | // this code. |
| 1510 | for (StringRef S : DiagnosticIDs::getDiagnosticFlags()) |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1511 | if (S.startswith(Cur)) |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1512 | SuggestedCompletions.push_back(S); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | // Sort the autocomplete candidates so that shells print them out in a |
| 1516 | // deterministic order. We could sort in any way, but we chose |
| 1517 | // case-insensitive sorting for consistency with the -help option |
| 1518 | // which prints out options in the case-insensitive alphabetical order. |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 1519 | llvm::sort(SuggestedCompletions.begin(), SuggestedCompletions.end(), |
| 1520 | [](StringRef A, StringRef B) { |
| 1521 | if (int X = A.compare_lower(B)) |
| 1522 | return X < 0; |
| 1523 | return A.compare(B) > 0; |
Mandeep Singh Grang | 8f54ae1 | 2017-11-20 18:49:14 +0000 | [diff] [blame] | 1524 | }); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1525 | |
| 1526 | llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n'; |
| 1527 | } |
| 1528 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1529 | bool Driver::HandleImmediateArgs(const Compilation &C) { |
Daniel Dunbar | 18974bd | 2010-06-11 22:00:19 +0000 | [diff] [blame] | 1530 | // The order these options are handled in gcc is all over the place, but we |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1531 | // don't expect inconsistencies w.r.t. that to matter in practice. |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1532 | |
Daniel Dunbar | 1b09e04 | 2010-09-17 02:47:28 +0000 | [diff] [blame] | 1533 | if (C.getArgs().hasArg(options::OPT_dumpmachine)) { |
| 1534 | llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n'; |
| 1535 | return false; |
| 1536 | } |
| 1537 | |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1538 | if (C.getArgs().hasArg(options::OPT_dumpversion)) { |
Daniel Dunbar | e26e500 | 2011-01-12 00:43:47 +0000 | [diff] [blame] | 1539 | // Since -dumpversion is only implemented for pedantic GCC compatibility, we |
| 1540 | // return an answer which matches our definition of __VERSION__. |
| 1541 | // |
| 1542 | // If we want to return a more correct answer some day, then we should |
| 1543 | // introduce a non-pedantically GCC compatible mode to Clang in which we |
| 1544 | // provide sensible definitions for -dumpversion, __VERSION__, etc. |
| 1545 | llvm::outs() << "4.2.1\n"; |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1546 | return false; |
| 1547 | } |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1549 | if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) { |
| 1550 | PrintDiagnosticCategories(llvm::outs()); |
| 1551 | return false; |
| 1552 | } |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1553 | |
James Molloy | a3c85b8 | 2012-05-01 14:57:16 +0000 | [diff] [blame] | 1554 | if (C.getArgs().hasArg(options::OPT_help) || |
Daniel Dunbar | a7b5e21 | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 1555 | C.getArgs().hasArg(options::OPT__help_hidden)) { |
| 1556 | PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1557 | return false; |
| 1558 | } |
| 1559 | |
Daniel Dunbar | b0006ae | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 1560 | if (C.getArgs().hasArg(options::OPT__version)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1561 | // Follow gcc behavior and use stdout for --version and stderr for -v. |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1562 | PrintVersion(C, llvm::outs()); |
Daniel Dunbar | b0006ae | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 1563 | return false; |
| 1564 | } |
| 1565 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1566 | if (C.getArgs().hasArg(options::OPT_v) || |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1567 | C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1568 | PrintVersion(C, llvm::errs()); |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1569 | SuppressMissingInputWarning = true; |
| 1570 | } |
| 1571 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 1572 | if (C.getArgs().hasArg(options::OPT_v)) { |
| 1573 | if (!SystemConfigDir.empty()) |
| 1574 | llvm::errs() << "System configuration file directory: " |
| 1575 | << SystemConfigDir << "\n"; |
| 1576 | if (!UserConfigDir.empty()) |
| 1577 | llvm::errs() << "User configuration file directory: " |
| 1578 | << UserConfigDir << "\n"; |
| 1579 | } |
| 1580 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1581 | const ToolChain &TC = C.getDefaultToolChain(); |
Chandler Carruth | 0ae39aa | 2013-07-30 17:57:09 +0000 | [diff] [blame] | 1582 | |
| 1583 | if (C.getArgs().hasArg(options::OPT_v)) |
| 1584 | TC.printVerboseInfo(llvm::errs()); |
| 1585 | |
Meador Inge | 51208a3 | 2017-04-04 21:46:50 +0000 | [diff] [blame] | 1586 | if (C.getArgs().hasArg(options::OPT_print_resource_dir)) { |
Meador Inge | a911395 | 2017-04-05 22:27:20 +0000 | [diff] [blame] | 1587 | llvm::outs() << ResourceDir << '\n'; |
Meador Inge | 51208a3 | 2017-04-04 21:46:50 +0000 | [diff] [blame] | 1588 | return false; |
| 1589 | } |
| 1590 | |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1591 | if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { |
| 1592 | llvm::outs() << "programs: ="; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1593 | bool separator = false; |
| 1594 | for (const std::string &Path : TC.getProgramPaths()) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1595 | if (separator) |
| 1596 | llvm::outs() << ':'; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1597 | llvm::outs() << Path; |
| 1598 | separator = true; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1599 | } |
| 1600 | llvm::outs() << "\n"; |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 1601 | llvm::outs() << "libraries: =" << ResourceDir; |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1602 | |
Sebastian Pop | 980920a | 2012-04-16 04:16:43 +0000 | [diff] [blame] | 1603 | StringRef sysroot = C.getSysRoot(); |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1604 | |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1605 | for (const std::string &Path : TC.getFilePaths()) { |
| 1606 | // Always print a separator. ResourceDir was the first item shown. |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 1607 | llvm::outs() << ':'; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1608 | // Interpretation of leading '=' is needed only for NetBSD. |
| 1609 | if (Path[0] == '=') |
Douglas Katzman | 26eabf6 | 2015-06-24 15:10:30 +0000 | [diff] [blame] | 1610 | llvm::outs() << sysroot << Path.substr(1); |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1611 | else |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1612 | llvm::outs() << Path; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1613 | } |
| 1614 | llvm::outs() << "\n"; |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1615 | return false; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1618 | // FIXME: The following handlers should use a callback mechanism, we don't |
| 1619 | // know what the client would like to do. |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1620 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1621 | llvm::outs() << GetFilePath(A->getValue(), TC) << "\n"; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1622 | return false; |
| 1623 | } |
| 1624 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1625 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { |
Saleem Abdulrasool | e5f3cf8 | 2018-05-01 18:40:42 +0000 | [diff] [blame] | 1626 | StringRef ProgName = A->getValue(); |
| 1627 | |
| 1628 | // Null program name cannot have a path. |
| 1629 | if (! ProgName.empty()) |
| 1630 | llvm::outs() << GetProgramPath(ProgName, TC); |
| 1631 | |
| 1632 | llvm::outs() << "\n"; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1633 | return false; |
| 1634 | } |
| 1635 | |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 1636 | if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) { |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 1637 | StringRef PassedFlags = A->getValue(); |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1638 | HandleAutocompletions(PassedFlags); |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1642 | if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { |
Michal Gorny | 7cfe480 | 2016-10-10 12:23:40 +0000 | [diff] [blame] | 1643 | ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs()); |
Leo Li | 23bb21c | 2017-08-24 01:51:51 +0000 | [diff] [blame] | 1644 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1645 | RegisterEffectiveTriple TripleRAII(TC, Triple); |
Michal Gorny | 7cfe480 | 2016-10-10 12:23:40 +0000 | [diff] [blame] | 1646 | switch (RLT) { |
| 1647 | case ToolChain::RLT_CompilerRT: |
| 1648 | llvm::outs() << TC.getCompilerRT(C.getArgs(), "builtins") << "\n"; |
| 1649 | break; |
| 1650 | case ToolChain::RLT_Libgcc: |
| 1651 | llvm::outs() << GetFilePath("libgcc.a", TC) << "\n"; |
| 1652 | break; |
| 1653 | } |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1654 | return false; |
| 1655 | } |
| 1656 | |
Daniel Dunbar | 1b3ec3a | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 1657 | if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { |
Douglas Katzman | a34b7bf | 2015-06-30 19:32:57 +0000 | [diff] [blame] | 1658 | for (const Multilib &Multilib : TC.getMultilibs()) |
| 1659 | llvm::outs() << Multilib << "\n"; |
Daniel Dunbar | 1b3ec3a | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 1660 | return false; |
| 1661 | } |
| 1662 | |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 1663 | if (C.getArgs().hasArg(options::OPT_print_multi_directory)) { |
Christian Bruel | 6ccc4a7 | 2018-09-06 14:03:44 +0000 | [diff] [blame] | 1664 | const Multilib &Multilib = TC.getMultilib(); |
| 1665 | if (Multilib.gccSuffix().empty()) |
| 1666 | llvm::outs() << ".\n"; |
| 1667 | else { |
| 1668 | StringRef Suffix(Multilib.gccSuffix()); |
| 1669 | assert(Suffix.front() == '/'); |
| 1670 | llvm::outs() << Suffix.substr(1) << "\n"; |
Jonathan Roelofs | 3fa96d8 | 2014-02-12 01:36:51 +0000 | [diff] [blame] | 1671 | } |
Jonathan Roelofs | 0e7ec60 | 2014-02-12 01:29:25 +0000 | [diff] [blame] | 1672 | return false; |
| 1673 | } |
Petr Hosek | bc86a99 | 2018-08-16 00:22:03 +0000 | [diff] [blame] | 1674 | |
| 1675 | if (C.getArgs().hasArg(options::OPT_print_target_triple)) { |
| 1676 | llvm::outs() << TC.getTripleString() << "\n"; |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
| 1680 | if (C.getArgs().hasArg(options::OPT_print_effective_triple)) { |
| 1681 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1682 | llvm::outs() << Triple.getTriple() << "\n"; |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1686 | return true; |
| 1687 | } |
| 1688 | |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1689 | // Display an action graph human-readably. Action A is the "sink" node |
| 1690 | // and latest-occuring action. Traversal is in pre-order, visiting the |
| 1691 | // inputs to each action before printing the action itself. |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1692 | static unsigned PrintActions1(const Compilation &C, Action *A, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1693 | std::map<Action *, unsigned> &Ids) { |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1694 | if (Ids.count(A)) // A was already visited. |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1695 | return Ids[A]; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1696 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1697 | std::string str; |
| 1698 | llvm::raw_string_ostream os(str); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1699 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1700 | os << Action::getClassName(A->getKind()) << ", "; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1701 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1702 | os << "\"" << IA->getInputArg().getValue() << "\""; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1703 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1704 | os << '"' << BIA->getArchName() << '"' << ", {" |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 1705 | << PrintActions1(C, *BIA->input_begin(), Ids) << "}"; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1706 | } else if (OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
| 1707 | bool IsFirst = true; |
| 1708 | OA->doOnEachDependence( |
| 1709 | [&](Action *A, const ToolChain *TC, const char *BoundArch) { |
| 1710 | // E.g. for two CUDA device dependences whose bound arch is sm_20 and |
| 1711 | // sm_35 this will generate: |
| 1712 | // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device" |
| 1713 | // (nvptx64-nvidia-cuda:sm_35) {#ID} |
| 1714 | if (!IsFirst) |
| 1715 | os << ", "; |
| 1716 | os << '"'; |
| 1717 | if (TC) |
| 1718 | os << A->getOffloadingKindPrefix(); |
| 1719 | else |
| 1720 | os << "host"; |
| 1721 | os << " ("; |
| 1722 | os << TC->getTriple().normalize(); |
| 1723 | |
| 1724 | if (BoundArch) |
| 1725 | os << ":" << BoundArch; |
| 1726 | os << ")"; |
| 1727 | os << '"'; |
| 1728 | os << " {" << PrintActions1(C, A, Ids) << "}"; |
| 1729 | IsFirst = false; |
| 1730 | }); |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1731 | } else { |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1732 | const ActionList *AL = &A->getInputs(); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 1733 | |
Artem Belevich | 2325675 | 2015-09-22 17:23:09 +0000 | [diff] [blame] | 1734 | if (AL->size()) { |
| 1735 | const char *Prefix = "{"; |
| 1736 | for (Action *PreRequisite : *AL) { |
| 1737 | os << Prefix << PrintActions1(C, PreRequisite, Ids); |
| 1738 | Prefix = ", "; |
| 1739 | } |
| 1740 | os << "}"; |
| 1741 | } else |
| 1742 | os << "{}"; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1745 | // Append offload info for all options other than the offloading action |
| 1746 | // itself (e.g. (cuda-device, sm_20) or (cuda-host)). |
| 1747 | std::string offload_str; |
| 1748 | llvm::raw_string_ostream offload_os(offload_str); |
| 1749 | if (!isa<OffloadAction>(A)) { |
| 1750 | auto S = A->getOffloadingKindPrefix(); |
| 1751 | if (!S.empty()) { |
| 1752 | offload_os << ", (" << S; |
| 1753 | if (A->getOffloadingArch()) |
| 1754 | offload_os << ", " << A->getOffloadingArch(); |
| 1755 | offload_os << ")"; |
| 1756 | } |
| 1757 | } |
| 1758 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1759 | unsigned Id = Ids.size(); |
| 1760 | Ids[A] = Id; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1761 | llvm::errs() << Id << ": " << os.str() << ", " |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1762 | << types::getTypeName(A->getType()) << offload_os.str() << "\n"; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1763 | |
| 1764 | return Id; |
| 1765 | } |
| 1766 | |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1767 | // Print the action graphs in a compilation C. |
| 1768 | // For example "clang -c file1.c file2.c" is composed of two subgraphs. |
Daniel Dunbar | eb843be | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 1769 | void Driver::PrintActions(const Compilation &C) const { |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1770 | std::map<Action *, unsigned> Ids; |
| 1771 | for (Action *A : C.getActions()) |
| 1772 | PrintActions1(C, A, Ids); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1775 | /// Check whether the given input tree contains any compilation or |
Joerg Sonnenberger | 5fe4a7d | 2011-05-06 14:05:11 +0000 | [diff] [blame] | 1776 | /// assembly actions. |
| 1777 | static bool ContainsCompileOrAssembleAction(const Action *A) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1778 | if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A) || |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 1779 | isa<AssembleJobAction>(A)) |
Daniel Dunbar | 00d3d8e | 2010-06-29 16:38:33 +0000 | [diff] [blame] | 1780 | return true; |
| 1781 | |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 1782 | for (const Action *Input : A->inputs()) |
Nico Weber | fb80f96 | 2015-09-19 21:36:51 +0000 | [diff] [blame] | 1783 | if (ContainsCompileOrAssembleAction(Input)) |
Daniel Dunbar | 00d3d8e | 2010-06-29 16:38:33 +0000 | [diff] [blame] | 1784 | return true; |
| 1785 | |
| 1786 | return false; |
| 1787 | } |
| 1788 | |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1789 | void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC, |
| 1790 | const InputList &BAInputs) const { |
| 1791 | DerivedArgList &Args = C.getArgs(); |
| 1792 | ActionList &Actions = C.getActions(); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1793 | llvm::PrettyStackTraceString CrashInfo("Building universal build actions"); |
| 1794 | // Collect the list of architectures. Duplicates are allowed, but should only |
| 1795 | // be handled once (in the order seen). |
Daniel Dunbar | e5dc482 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 1796 | llvm::StringSet<> ArchNames; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1797 | SmallVector<const char *, 4> Archs; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 1798 | for (Arg *A : Args) { |
Daniel Dunbar | 0bfb21e | 2009-11-19 03:26:40 +0000 | [diff] [blame] | 1799 | if (A->getOption().matches(options::OPT_arch)) { |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1800 | // Validate the option here; we don't save the type here because its |
| 1801 | // particular spelling may participate in other driver choices. |
| 1802 | llvm::Triple::ArchType Arch = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1803 | tools::darwin::getArchTypeForMachOArchName(A->getValue()); |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1804 | if (Arch == llvm::Triple::UnknownArch) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1805 | Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args); |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1806 | continue; |
| 1807 | } |
| 1808 | |
Daniel Dunbar | 2da0272 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 1809 | A->claim(); |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 1810 | if (ArchNames.insert(A->getValue()).second) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1811 | Archs.push_back(A->getValue()); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1815 | // When there is no explicit arch for this platform, make sure we still bind |
| 1816 | // the architecture (to the default) so that -Xarch_ is handled correctly. |
Daniel Dunbar | eb843be | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 1817 | if (!Archs.size()) |
Daniel Dunbar | c3bd9f5 | 2012-11-08 03:38:26 +0000 | [diff] [blame] | 1818 | Archs.push_back(Args.MakeArgString(TC.getDefaultUniversalArchName())); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1819 | |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1820 | ActionList SingleActions; |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1821 | BuildActions(C, Args, BAInputs, SingleActions); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1822 | |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1823 | // Add in arch bindings for every top level action, as well as lipo and |
| 1824 | // dsymutil steps if needed. |
Nico Weber | fb80f96 | 2015-09-19 21:36:51 +0000 | [diff] [blame] | 1825 | for (Action* Act : SingleActions) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1826 | // Make sure we can lipo this kind of output. If not (and it is an actual |
| 1827 | // output) then we disallow, since we can't create an output file with the |
| 1828 | // right name without overwriting it. We could remove this oddity by just |
| 1829 | // changing the output names to include the arch, which would also fix |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1830 | // -save-temps. Compatibility wins for now. |
| 1831 | |
Daniel Dunbar | e2ca3bd | 2009-03-13 17:46:02 +0000 | [diff] [blame] | 1832 | if (Archs.size() > 1 && !types::canLipoType(Act->getType())) |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1833 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1834 | << types::getTypeName(Act->getType()); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1835 | |
| 1836 | ActionList Inputs; |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1837 | for (unsigned i = 0, e = Archs.size(); i != e; ++i) |
| 1838 | Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i])); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1839 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1840 | // Lipo if necessary, we do it this way because we need to set the arch flag |
| 1841 | // so that -Xarch_ gets overwritten. |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1842 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 1843 | Actions.append(Inputs.begin(), Inputs.end()); |
| 1844 | else |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1845 | Actions.push_back(C.MakeAction<LipoJobAction>(Inputs, Act->getType())); |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1846 | |
Eric Christopher | 65c05fa | 2012-02-06 19:43:51 +0000 | [diff] [blame] | 1847 | // Handle debug info queries. |
| 1848 | Arg *A = Args.getLastArg(options::OPT_g_Group); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1849 | if (A && !A->getOption().matches(options::OPT_g0) && |
| 1850 | !A->getOption().matches(options::OPT_gstabs) && |
| 1851 | ContainsCompileOrAssembleAction(Actions.back())) { |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 1852 | |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1853 | // Add a 'dsymutil' step if necessary, when debug info is enabled and we |
| 1854 | // have a compile input. We need to run 'dsymutil' ourselves in such cases |
Eric Christopher | 776c26f | 2013-01-28 17:39:03 +0000 | [diff] [blame] | 1855 | // because the debug info will refer to a temporary object file which |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1856 | // will be removed at the end of the compilation process. |
| 1857 | if (Act->getType() == types::TY_Image) { |
| 1858 | ActionList Inputs; |
| 1859 | Inputs.push_back(Actions.back()); |
| 1860 | Actions.pop_back(); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1861 | Actions.push_back( |
| 1862 | C.MakeAction<DsymutilJobAction>(Inputs, types::TY_dSYM)); |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1863 | } |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1864 | |
Ben Langmuir | 9b9a8d3 | 2014-02-06 18:53:25 +0000 | [diff] [blame] | 1865 | // Verify the debug info output. |
Alp Toker | e9d2bfc | 2014-01-17 02:06:23 +0000 | [diff] [blame] | 1866 | if (Args.hasArg(options::OPT_verify_debug_info)) { |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1867 | Action* LastAction = Actions.back(); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1868 | Actions.pop_back(); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1869 | Actions.push_back(C.MakeAction<VerifyDebugInfoJobAction>( |
| 1870 | LastAction, types::TY_Nothing)); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1873 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1876 | /// Check that the file referenced by Value exists. If it doesn't, |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1877 | /// issue a diagnostic and return false. |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 1878 | static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args, |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 1879 | StringRef Value, types::ID Ty) { |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1880 | if (!D.getCheckInputsExist()) |
| 1881 | return true; |
| 1882 | |
| 1883 | // stdin always exists. |
| 1884 | if (Value == "-") |
| 1885 | return true; |
| 1886 | |
| 1887 | SmallString<64> Path(Value); |
| 1888 | if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) { |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1889 | if (!llvm::sys::path::is_absolute(Path)) { |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1890 | SmallString<64> Directory(WorkDir->getValue()); |
| 1891 | llvm::sys::path::append(Directory, Value); |
| 1892 | Path.assign(Directory); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | if (llvm::sys::fs::exists(Twine(Path))) |
| 1897 | return true; |
| 1898 | |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 1899 | if (D.IsCLMode()) { |
| 1900 | if (!llvm::sys::path::is_absolute(Twine(Path)) && |
| 1901 | llvm::sys::Process::FindInEnvPath("LIB", Value)) |
| 1902 | return true; |
| 1903 | |
| 1904 | if (Args.hasArg(options::OPT__SLASH_link) && Ty == types::TY_Object) { |
| 1905 | // Arguments to the /link flag might cause the linker to search for object |
| 1906 | // and library files in paths we don't know about. Don't error in such |
| 1907 | // cases. |
| 1908 | return true; |
| 1909 | } |
| 1910 | } |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 1911 | |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1912 | D.Diag(clang::diag::err_drv_no_such_file) << Path; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1913 | return false; |
| 1914 | } |
| 1915 | |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1916 | // Construct a the list of inputs and their types. |
Hans Wennborg | 5536285 | 2014-05-02 22:55:30 +0000 | [diff] [blame] | 1917 | void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args, |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1918 | InputList &Inputs) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1919 | // Track the current user specified (-x) input. We also explicitly track the |
| 1920 | // argument used to set the type; we only want to claim the type when we |
| 1921 | // actually use it, so we warn about unused -x arguments. |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 1922 | types::ID InputType = types::TY_Nothing; |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 1923 | Arg *InputTypeArg = nullptr; |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 1924 | |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 1925 | // The last /TC or /TP option sets the input type to C or C++ globally. |
Ehsan Akhgari | c249abb | 2014-09-12 21:44:24 +0000 | [diff] [blame] | 1926 | if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC, |
| 1927 | options::OPT__SLASH_TP)) { |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1928 | InputTypeArg = TCTP; |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 1929 | InputType = TCTP->getOption().matches(options::OPT__SLASH_TC) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1930 | ? types::TY_C |
| 1931 | : types::TY_CXX; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1932 | |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 1933 | Arg *Previous = nullptr; |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 1934 | bool ShowNote = false; |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 1935 | for (Arg *A : Args.filtered(options::OPT__SLASH_TC, options::OPT__SLASH_TP)) { |
| 1936 | if (Previous) { |
| 1937 | Diag(clang::diag::warn_drv_overriding_flag_option) |
| 1938 | << Previous->getSpelling() << A->getSpelling(); |
| 1939 | ShowNote = true; |
| 1940 | } |
| 1941 | Previous = A; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1942 | } |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 1943 | if (ShowNote) |
| 1944 | Diag(clang::diag::note_drv_t_option_is_global); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1945 | |
| 1946 | // No driver mode exposes -x and /TC or /TP; we don't support mixing them. |
| 1947 | assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed"); |
| 1948 | } |
| 1949 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 1950 | for (Arg *A : Args) { |
Michael J. Spencer | ad3ccc3 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 1951 | if (A->getOption().getKind() == Option::InputClass) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1952 | const char *Value = A->getValue(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1953 | types::ID Ty = types::TY_INVALID; |
| 1954 | |
| 1955 | // Infer the input type if necessary. |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 1956 | if (InputType == types::TY_Nothing) { |
| 1957 | // If there was an explicit arg for this, claim it. |
| 1958 | if (InputTypeArg) |
| 1959 | InputTypeArg->claim(); |
| 1960 | |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1961 | // stdin must be handled specially. |
| 1962 | if (memcmp(Value, "-", 2) == 0) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1963 | // If running with -E, treat as a C input (this changes the builtin |
| 1964 | // macros, for example). This may be overridden by -ObjC below. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1965 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1966 | // Otherwise emit an error but still use a valid type to avoid |
| 1967 | // spurious errors (e.g., no inputs). |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 1968 | if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP()) |
Hans Wennborg | cfdd8b5 | 2014-01-29 01:04:40 +0000 | [diff] [blame] | 1969 | Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl |
| 1970 | : clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1971 | Ty = types::TY_C; |
| 1972 | } else { |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 1973 | // Otherwise lookup by extension. |
| 1974 | // Fallback is C if invoked as C preprocessor or Object otherwise. |
| 1975 | // We use a host hook here because Darwin at least has its own |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1976 | // idea of what .s is. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1977 | if (const char *Ext = strrchr(Value, '.')) |
Daniel Dunbar | cc7df6c | 2010-08-02 05:43:56 +0000 | [diff] [blame] | 1978 | Ty = TC.LookupTypeForExtension(Ext + 1); |
Daniel Dunbar | ea9f032 | 2009-03-20 23:39:23 +0000 | [diff] [blame] | 1979 | |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 1980 | if (Ty == types::TY_INVALID) { |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 1981 | if (CCCIsCPP()) |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 1982 | Ty = types::TY_C; |
| 1983 | else |
| 1984 | Ty = types::TY_Object; |
| 1985 | } |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 1986 | |
| 1987 | // If the driver is invoked as C++ compiler (like clang++ or c++) it |
| 1988 | // should autodetect some input files as C++ for g++ compatibility. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 1989 | if (CCCIsCXX()) { |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 1990 | types::ID OldTy = Ty; |
| 1991 | Ty = types::lookupCXXTypeForCType(Ty); |
| 1992 | |
| 1993 | if (Ty != OldTy) |
| 1994 | Diag(clang::diag::warn_drv_treating_input_as_cxx) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1995 | << getTypeName(OldTy) << getTypeName(Ty); |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 1996 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Daniel Dunbar | 82b2210 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 1999 | // -ObjC and -ObjC++ override the default language, but only for "source |
| 2000 | // files". We just treat everything that isn't a linker input as a |
| 2001 | // source file. |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2002 | // |
Daniel Dunbar | 82b2210 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 2003 | // FIXME: Clean this up if we move the phase sequence into the type. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2004 | if (Ty != types::TY_Object) { |
| 2005 | if (Args.hasArg(options::OPT_ObjC)) |
| 2006 | Ty = types::TY_ObjC; |
| 2007 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 2008 | Ty = types::TY_ObjCXX; |
| 2009 | } |
| 2010 | } else { |
| 2011 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
Ehsan Akhgari | 7e954ea | 2014-09-12 18:15:10 +0000 | [diff] [blame] | 2012 | if (!InputTypeArg->getOption().matches(options::OPT_x)) { |
| 2013 | // If emulating cl.exe, make sure that /TC and /TP don't affect input |
| 2014 | // object files. |
| 2015 | const char *Ext = strrchr(Value, '.'); |
| 2016 | if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object) |
| 2017 | Ty = types::TY_Object; |
| 2018 | } |
| 2019 | if (Ty == types::TY_INVALID) { |
| 2020 | Ty = InputType; |
| 2021 | InputTypeArg->claim(); |
| 2022 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 2025 | if (DiagnoseInputExistence(*this, Args, Value, Ty)) |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2026 | Inputs.push_back(std::make_pair(Ty, A)); |
| 2027 | |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2028 | } else if (A->getOption().matches(options::OPT__SLASH_Tc)) { |
| 2029 | StringRef Value = A->getValue(); |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 2030 | if (DiagnoseInputExistence(*this, Args, Value, types::TY_C)) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2031 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2032 | Inputs.push_back(std::make_pair(types::TY_C, InputArg)); |
| 2033 | } |
| 2034 | A->claim(); |
| 2035 | } else if (A->getOption().matches(options::OPT__SLASH_Tp)) { |
| 2036 | StringRef Value = A->getValue(); |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 2037 | if (DiagnoseInputExistence(*this, Args, Value, types::TY_CXX)) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2038 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2039 | Inputs.push_back(std::make_pair(types::TY_CXX, InputArg)); |
| 2040 | } |
| 2041 | A->claim(); |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 2042 | } else if (A->getOption().hasFlag(options::LinkerInput)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2043 | // Just treat as object type, we could make a special type for this if |
| 2044 | // necessary. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2045 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 2046 | |
Daniel Dunbar | 0bfb21e | 2009-11-19 03:26:40 +0000 | [diff] [blame] | 2047 | } else if (A->getOption().matches(options::OPT_x)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2048 | InputTypeArg = A; |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2049 | InputType = types::lookupTypeForTypeSpecifier(A->getValue()); |
Chad Rosier | 706c235 | 2012-04-07 00:01:31 +0000 | [diff] [blame] | 2050 | A->claim(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Follow gcc behavior and treat as linker input for invalid -x |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2053 | // options. Its not clear why we shouldn't just revert to unknown; but |
Michael J. Spencer | 1a4fe8c | 2010-12-17 21:22:33 +0000 | [diff] [blame] | 2054 | // this isn't very important, we might as well be bug compatible. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2055 | if (!InputType) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2056 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2057 | InputType = types::TY_Object; |
| 2058 | } |
Hans Wennborg | 091f1b6 | 2017-01-27 17:09:41 +0000 | [diff] [blame] | 2059 | } else if (A->getOption().getID() == options::OPT__SLASH_U) { |
| 2060 | assert(A->getNumValues() == 1 && "The /U option has one value."); |
| 2061 | StringRef Val = A->getValue(0); |
| 2062 | if (Val.find_first_of("/\\") != StringRef::npos) { |
| 2063 | // Warn about e.g. "/Users/me/myfile.c". |
| 2064 | Diag(diag::warn_slash_u_filename) << Val; |
| 2065 | Diag(diag::note_use_dashdash); |
| 2066 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2067 | } |
| 2068 | } |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2069 | if (CCCIsCPP() && Inputs.empty()) { |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 2070 | // If called as standalone preprocessor, stdin is processed |
| 2071 | // if no other input is present. |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2072 | Arg *A = MakeInputArg(Args, *Opts, "-"); |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 2073 | Inputs.push_back(std::make_pair(types::TY_C, A)); |
| 2074 | } |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2077 | namespace { |
| 2078 | /// Provides a convenient interface for different programming models to generate |
| 2079 | /// the required device actions. |
| 2080 | class OffloadingActionBuilder final { |
| 2081 | /// Flag used to trace errors in the builder. |
| 2082 | bool IsValid = false; |
| 2083 | |
| 2084 | /// The compilation that is using this builder. |
| 2085 | Compilation &C; |
| 2086 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2087 | /// Map between an input argument and the offload kinds used to process it. |
| 2088 | std::map<const Arg *, unsigned> InputArgToOffloadKindMap; |
| 2089 | |
| 2090 | /// Builder interface. It doesn't build anything or keep any state. |
| 2091 | class DeviceActionBuilder { |
| 2092 | public: |
| 2093 | typedef llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PhasesTy; |
| 2094 | |
| 2095 | enum ActionBuilderReturnCode { |
| 2096 | // The builder acted successfully on the current action. |
| 2097 | ABRT_Success, |
| 2098 | // The builder didn't have to act on the current action. |
| 2099 | ABRT_Inactive, |
| 2100 | // The builder was successful and requested the host action to not be |
| 2101 | // generated. |
| 2102 | ABRT_Ignore_Host, |
| 2103 | }; |
| 2104 | |
| 2105 | protected: |
| 2106 | /// Compilation associated with this builder. |
| 2107 | Compilation &C; |
| 2108 | |
| 2109 | /// Tool chains associated with this builder. The same programming |
| 2110 | /// model may have associated one or more tool chains. |
| 2111 | SmallVector<const ToolChain *, 2> ToolChains; |
| 2112 | |
| 2113 | /// The derived arguments associated with this builder. |
| 2114 | DerivedArgList &Args; |
| 2115 | |
| 2116 | /// The inputs associated with this builder. |
| 2117 | const Driver::InputList &Inputs; |
| 2118 | |
| 2119 | /// The associated offload kind. |
| 2120 | Action::OffloadKind AssociatedOffloadKind = Action::OFK_None; |
| 2121 | |
| 2122 | public: |
| 2123 | DeviceActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2124 | const Driver::InputList &Inputs, |
| 2125 | Action::OffloadKind AssociatedOffloadKind) |
| 2126 | : C(C), Args(Args), Inputs(Inputs), |
| 2127 | AssociatedOffloadKind(AssociatedOffloadKind) {} |
| 2128 | virtual ~DeviceActionBuilder() {} |
| 2129 | |
| 2130 | /// Fill up the array \a DA with all the device dependences that should be |
| 2131 | /// added to the provided host action \a HostAction. By default it is |
| 2132 | /// inactive. |
| 2133 | virtual ActionBuilderReturnCode |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2134 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2135 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2136 | PhasesTy &Phases) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2137 | return ABRT_Inactive; |
| 2138 | } |
| 2139 | |
| 2140 | /// Update the state to include the provided host action \a HostAction as a |
| 2141 | /// dependency of the current device action. By default it is inactive. |
| 2142 | virtual ActionBuilderReturnCode addDeviceDepences(Action *HostAction) { |
| 2143 | return ABRT_Inactive; |
| 2144 | } |
| 2145 | |
| 2146 | /// Append top level actions generated by the builder. Return true if errors |
| 2147 | /// were found. |
| 2148 | virtual void appendTopLevelActions(ActionList &AL) {} |
| 2149 | |
| 2150 | /// Append linker actions generated by the builder. Return true if errors |
| 2151 | /// were found. |
| 2152 | virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {} |
| 2153 | |
| 2154 | /// Initialize the builder. Return true if any initialization errors are |
| 2155 | /// found. |
| 2156 | virtual bool initialize() { return false; } |
| 2157 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2158 | /// Return true if the builder can use bundling/unbundling. |
| 2159 | virtual bool canUseBundlerUnbundler() const { return false; } |
| 2160 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2161 | /// Return true if this builder is valid. We have a valid builder if we have |
| 2162 | /// associated device tool chains. |
| 2163 | bool isValid() { return !ToolChains.empty(); } |
| 2164 | |
| 2165 | /// Return the associated offload kind. |
| 2166 | Action::OffloadKind getAssociatedOffloadKind() { |
| 2167 | return AssociatedOffloadKind; |
| 2168 | } |
| 2169 | }; |
| 2170 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2171 | /// Base class for CUDA/HIP action builder. It injects device code in |
| 2172 | /// the host backend action. |
| 2173 | class CudaActionBuilderBase : public DeviceActionBuilder { |
| 2174 | protected: |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2175 | /// Flags to signal if the user requested host-only or device-only |
| 2176 | /// compilation. |
| 2177 | bool CompileHostOnly = false; |
| 2178 | bool CompileDeviceOnly = false; |
| 2179 | |
| 2180 | /// List of GPU architectures to use in this compilation. |
| 2181 | SmallVector<CudaArch, 4> GpuArchList; |
| 2182 | |
| 2183 | /// The CUDA actions for the current input. |
| 2184 | ActionList CudaDeviceActions; |
| 2185 | |
| 2186 | /// The CUDA fat binary if it was generated for the current input. |
| 2187 | Action *CudaFatBinary = nullptr; |
| 2188 | |
| 2189 | /// Flag that is set to true if this builder acted on the current input. |
| 2190 | bool IsActive = false; |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2191 | public: |
| 2192 | CudaActionBuilderBase(Compilation &C, DerivedArgList &Args, |
| 2193 | const Driver::InputList &Inputs, |
| 2194 | Action::OffloadKind OFKind) |
| 2195 | : DeviceActionBuilder(C, Args, Inputs, OFKind) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2196 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2197 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2198 | // While generating code for CUDA, we only depend on the host input action |
| 2199 | // to trigger the creation of all the CUDA device actions. |
| 2200 | |
| 2201 | // If we are dealing with an input action, replicate it for each GPU |
| 2202 | // architecture. If we are in host-only mode we return 'success' so that |
| 2203 | // the host uses the CUDA offload kind. |
| 2204 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2205 | assert(!GpuArchList.empty() && |
| 2206 | "We should have at least one GPU architecture."); |
| 2207 | |
| 2208 | // If the host input is not CUDA or HIP, we don't need to bother about |
| 2209 | // this input. |
| 2210 | if (IA->getType() != types::TY_CUDA && |
| 2211 | IA->getType() != types::TY_HIP) { |
| 2212 | // The builder will ignore this input. |
| 2213 | IsActive = false; |
| 2214 | return ABRT_Inactive; |
| 2215 | } |
| 2216 | |
| 2217 | // Set the flag to true, so that the builder acts on the current input. |
| 2218 | IsActive = true; |
| 2219 | |
| 2220 | if (CompileHostOnly) |
| 2221 | return ABRT_Success; |
| 2222 | |
| 2223 | // Replicate inputs for each GPU architecture. |
| 2224 | auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE |
| 2225 | : types::TY_CUDA_DEVICE; |
| 2226 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2227 | CudaDeviceActions.push_back( |
| 2228 | C.MakeAction<InputAction>(IA->getInputArg(), Ty)); |
| 2229 | } |
| 2230 | |
| 2231 | return ABRT_Success; |
| 2232 | } |
| 2233 | |
| 2234 | // If this is an unbundling action use it as is for each CUDA toolchain. |
| 2235 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
| 2236 | CudaDeviceActions.clear(); |
| 2237 | for (auto Arch : GpuArchList) { |
| 2238 | CudaDeviceActions.push_back(UA); |
| 2239 | UA->registerDependentActionInfo(ToolChains[0], CudaArchToString(Arch), |
| 2240 | AssociatedOffloadKind); |
| 2241 | } |
| 2242 | return ABRT_Success; |
| 2243 | } |
| 2244 | |
| 2245 | return IsActive ? ABRT_Success : ABRT_Inactive; |
| 2246 | } |
| 2247 | |
| 2248 | void appendTopLevelActions(ActionList &AL) override { |
| 2249 | // Utility to append actions to the top level list. |
| 2250 | auto AddTopLevel = [&](Action *A, CudaArch BoundArch) { |
| 2251 | OffloadAction::DeviceDependences Dep; |
| 2252 | Dep.add(*A, *ToolChains.front(), CudaArchToString(BoundArch), |
| 2253 | AssociatedOffloadKind); |
| 2254 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2255 | }; |
| 2256 | |
| 2257 | // If we have a fat binary, add it to the list. |
| 2258 | if (CudaFatBinary) { |
| 2259 | AddTopLevel(CudaFatBinary, CudaArch::UNKNOWN); |
| 2260 | CudaDeviceActions.clear(); |
| 2261 | CudaFatBinary = nullptr; |
| 2262 | return; |
| 2263 | } |
| 2264 | |
| 2265 | if (CudaDeviceActions.empty()) |
| 2266 | return; |
| 2267 | |
| 2268 | // If we have CUDA actions at this point, that's because we have a have |
| 2269 | // partial compilation, so we should have an action for each GPU |
| 2270 | // architecture. |
| 2271 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2272 | "Expecting one action per GPU architecture."); |
| 2273 | assert(ToolChains.size() == 1 && |
| 2274 | "Expecting to have a sing CUDA toolchain."); |
| 2275 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) |
| 2276 | AddTopLevel(CudaDeviceActions[I], GpuArchList[I]); |
| 2277 | |
| 2278 | CudaDeviceActions.clear(); |
| 2279 | } |
| 2280 | |
| 2281 | bool initialize() override { |
| 2282 | assert(AssociatedOffloadKind == Action::OFK_Cuda || |
| 2283 | AssociatedOffloadKind == Action::OFK_HIP); |
| 2284 | |
| 2285 | // We don't need to support CUDA. |
| 2286 | if (AssociatedOffloadKind == Action::OFK_Cuda && |
| 2287 | !C.hasOffloadToolChain<Action::OFK_Cuda>()) |
| 2288 | return false; |
| 2289 | |
| 2290 | // We don't need to support HIP. |
| 2291 | if (AssociatedOffloadKind == Action::OFK_HIP && |
| 2292 | !C.hasOffloadToolChain<Action::OFK_HIP>()) |
| 2293 | return false; |
| 2294 | |
| 2295 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 2296 | assert(HostTC && "No toolchain for host compilation."); |
| 2297 | if (HostTC->getTriple().isNVPTX() || |
| 2298 | HostTC->getTriple().getArch() == llvm::Triple::amdgcn) { |
| 2299 | // We do not support targeting NVPTX/AMDGCN for host compilation. Throw |
| 2300 | // an error and abort pipeline construction early so we don't trip |
| 2301 | // asserts that assume device-side compilation. |
| 2302 | C.getDriver().Diag(diag::err_drv_cuda_host_arch) |
| 2303 | << HostTC->getTriple().getArchName(); |
| 2304 | return true; |
| 2305 | } |
| 2306 | |
| 2307 | ToolChains.push_back( |
| 2308 | AssociatedOffloadKind == Action::OFK_Cuda |
| 2309 | ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() |
| 2310 | : C.getSingleOffloadToolChain<Action::OFK_HIP>()); |
| 2311 | |
| 2312 | Arg *PartialCompilationArg = Args.getLastArg( |
| 2313 | options::OPT_cuda_host_only, options::OPT_cuda_device_only, |
| 2314 | options::OPT_cuda_compile_host_device); |
| 2315 | CompileHostOnly = PartialCompilationArg && |
| 2316 | PartialCompilationArg->getOption().matches( |
| 2317 | options::OPT_cuda_host_only); |
| 2318 | CompileDeviceOnly = PartialCompilationArg && |
| 2319 | PartialCompilationArg->getOption().matches( |
| 2320 | options::OPT_cuda_device_only); |
| 2321 | |
| 2322 | // Collect all cuda_gpu_arch parameters, removing duplicates. |
| 2323 | std::set<CudaArch> GpuArchs; |
| 2324 | bool Error = false; |
| 2325 | for (Arg *A : Args) { |
| 2326 | if (!(A->getOption().matches(options::OPT_cuda_gpu_arch_EQ) || |
| 2327 | A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ))) |
| 2328 | continue; |
| 2329 | A->claim(); |
| 2330 | |
| 2331 | const StringRef ArchStr = A->getValue(); |
| 2332 | if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ) && |
| 2333 | ArchStr == "all") { |
| 2334 | GpuArchs.clear(); |
| 2335 | continue; |
| 2336 | } |
| 2337 | CudaArch Arch = StringToCudaArch(ArchStr); |
| 2338 | if (Arch == CudaArch::UNKNOWN) { |
| 2339 | C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr; |
| 2340 | Error = true; |
| 2341 | } else if (A->getOption().matches(options::OPT_cuda_gpu_arch_EQ)) |
| 2342 | GpuArchs.insert(Arch); |
| 2343 | else if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ)) |
| 2344 | GpuArchs.erase(Arch); |
| 2345 | else |
| 2346 | llvm_unreachable("Unexpected option."); |
| 2347 | } |
| 2348 | |
| 2349 | // Collect list of GPUs remaining in the set. |
| 2350 | for (CudaArch Arch : GpuArchs) |
| 2351 | GpuArchList.push_back(Arch); |
| 2352 | |
| 2353 | // Default to sm_20 which is the lowest common denominator for |
| 2354 | // supported GPUs. sm_20 code should work correctly, if |
| 2355 | // suboptimally, on all newer GPUs. |
| 2356 | if (GpuArchList.empty()) |
| 2357 | GpuArchList.push_back(CudaArch::SM_20); |
| 2358 | |
| 2359 | return Error; |
| 2360 | } |
| 2361 | }; |
| 2362 | |
| 2363 | /// \brief CUDA action builder. It injects device code in the host backend |
| 2364 | /// action. |
| 2365 | class CudaActionBuilder final : public CudaActionBuilderBase { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2366 | public: |
| 2367 | CudaActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2368 | const Driver::InputList &Inputs) |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2369 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2370 | |
| 2371 | ActionBuilderReturnCode |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2372 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2373 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2374 | PhasesTy &Phases) override { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2375 | if (!IsActive) |
| 2376 | return ABRT_Inactive; |
| 2377 | |
| 2378 | // If we don't have more CUDA actions, we don't have any dependences to |
| 2379 | // create for the host. |
| 2380 | if (CudaDeviceActions.empty()) |
| 2381 | return ABRT_Success; |
| 2382 | |
| 2383 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2384 | "Expecting one action per GPU architecture."); |
| 2385 | assert(!CompileHostOnly && |
| 2386 | "Not expecting CUDA actions in host-only compilation."); |
| 2387 | |
| 2388 | // If we are generating code for the device or we are in a backend phase, |
| 2389 | // we attempt to generate the fat binary. We compile each arch to ptx and |
| 2390 | // assemble to cubin, then feed the cubin *and* the ptx into a device |
| 2391 | // "link" action, which uses fatbinary to combine these cubins into one |
| 2392 | // fatbin. The fatbin is then an input to the host action if not in |
| 2393 | // device-only mode. |
| 2394 | if (CompileDeviceOnly || CurPhase == phases::Backend) { |
| 2395 | ActionList DeviceActions; |
| 2396 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2397 | // Produce the device action from the current phase up to the assemble |
| 2398 | // phase. |
| 2399 | for (auto Ph : Phases) { |
| 2400 | // Skip the phases that were already dealt with. |
| 2401 | if (Ph < CurPhase) |
| 2402 | continue; |
| 2403 | // We have to be consistent with the host final phase. |
| 2404 | if (Ph > FinalPhase) |
| 2405 | break; |
| 2406 | |
| 2407 | CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction( |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 2408 | C, Args, Ph, CudaDeviceActions[I], Action::OFK_Cuda); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2409 | |
| 2410 | if (Ph == phases::Assemble) |
| 2411 | break; |
| 2412 | } |
| 2413 | |
| 2414 | // If we didn't reach the assemble phase, we can't generate the fat |
| 2415 | // binary. We don't need to generate the fat binary if we are not in |
| 2416 | // device-only mode. |
| 2417 | if (!isa<AssembleJobAction>(CudaDeviceActions[I]) || |
| 2418 | CompileDeviceOnly) |
| 2419 | continue; |
| 2420 | |
| 2421 | Action *AssembleAction = CudaDeviceActions[I]; |
| 2422 | assert(AssembleAction->getType() == types::TY_Object); |
| 2423 | assert(AssembleAction->getInputs().size() == 1); |
| 2424 | |
| 2425 | Action *BackendAction = AssembleAction->getInputs()[0]; |
| 2426 | assert(BackendAction->getType() == types::TY_PP_Asm); |
| 2427 | |
| 2428 | for (auto &A : {AssembleAction, BackendAction}) { |
| 2429 | OffloadAction::DeviceDependences DDep; |
| 2430 | DDep.add(*A, *ToolChains.front(), CudaArchToString(GpuArchList[I]), |
| 2431 | Action::OFK_Cuda); |
| 2432 | DeviceActions.push_back( |
| 2433 | C.MakeAction<OffloadAction>(DDep, A->getType())); |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | // We generate the fat binary if we have device input actions. |
| 2438 | if (!DeviceActions.empty()) { |
| 2439 | CudaFatBinary = |
| 2440 | C.MakeAction<LinkJobAction>(DeviceActions, types::TY_CUDA_FATBIN); |
| 2441 | |
| 2442 | if (!CompileDeviceOnly) { |
| 2443 | DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr, |
| 2444 | Action::OFK_Cuda); |
| 2445 | // Clear the fat binary, it is already a dependence to an host |
| 2446 | // action. |
| 2447 | CudaFatBinary = nullptr; |
| 2448 | } |
| 2449 | |
| 2450 | // Remove the CUDA actions as they are already connected to an host |
| 2451 | // action or fat binary. |
| 2452 | CudaDeviceActions.clear(); |
| 2453 | } |
| 2454 | |
| 2455 | // We avoid creating host action in device-only mode. |
| 2456 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
Samuel Antao | 5b1a89a | 2016-10-27 00:53:34 +0000 | [diff] [blame] | 2457 | } else if (CurPhase > phases::Backend) { |
| 2458 | // If we are past the backend phase and still have a device action, we |
| 2459 | // don't have to do anything as this action is already a device |
| 2460 | // top-level action. |
| 2461 | return ABRT_Success; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | assert(CurPhase < phases::Backend && "Generating single CUDA " |
| 2465 | "instructions should only occur " |
| 2466 | "before the backend phase!"); |
| 2467 | |
| 2468 | // By default, we produce an action for each device arch. |
| 2469 | for (Action *&A : CudaDeviceActions) |
| 2470 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2471 | |
| 2472 | return ABRT_Success; |
| 2473 | } |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2474 | }; |
| 2475 | /// \brief HIP action builder. It injects device code in the host backend |
| 2476 | /// action. |
| 2477 | class HIPActionBuilder final : public CudaActionBuilderBase { |
| 2478 | /// The linker inputs obtained for each device arch. |
| 2479 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2480 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2481 | public: |
| 2482 | HIPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2483 | const Driver::InputList &Inputs) |
| 2484 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2485 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2486 | bool canUseBundlerUnbundler() const override { return true; } |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2487 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2488 | ActionBuilderReturnCode |
| 2489 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2490 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2491 | PhasesTy &Phases) override { |
| 2492 | // amdgcn does not support linking of object files, therefore we skip |
| 2493 | // backend and assemble phases to output LLVM IR. |
| 2494 | if (CudaDeviceActions.empty() || CurPhase == phases::Backend || |
| 2495 | CurPhase == phases::Assemble) |
| 2496 | return ABRT_Success; |
| 2497 | |
| 2498 | assert((CurPhase == phases::Link || |
| 2499 | CudaDeviceActions.size() == GpuArchList.size()) && |
| 2500 | "Expecting one action per GPU architecture."); |
| 2501 | assert(!CompileHostOnly && |
| 2502 | "Not expecting CUDA actions in host-only compilation."); |
| 2503 | |
| 2504 | // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch. |
| 2505 | // This happens to each device action originated from each input file. |
| 2506 | // Later on, device actions in DeviceLinkerInputs are used to create |
| 2507 | // device link actions in appendLinkDependences and the created device |
| 2508 | // link actions are passed to the offload action as device dependence. |
| 2509 | if (CurPhase == phases::Link) { |
| 2510 | DeviceLinkerInputs.resize(CudaDeviceActions.size()); |
| 2511 | auto LI = DeviceLinkerInputs.begin(); |
| 2512 | for (auto *A : CudaDeviceActions) { |
| 2513 | LI->push_back(A); |
| 2514 | ++LI; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2517 | // We will pass the device action as a host dependence, so we don't |
| 2518 | // need to do anything else with them. |
| 2519 | CudaDeviceActions.clear(); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2520 | return ABRT_Success; |
| 2521 | } |
| 2522 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2523 | // By default, we produce an action for each device arch. |
| 2524 | for (Action *&A : CudaDeviceActions) |
| 2525 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A, |
| 2526 | AssociatedOffloadKind); |
| 2527 | |
| 2528 | return ABRT_Success; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2531 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2532 | // Append a new link action for each device. |
| 2533 | unsigned I = 0; |
| 2534 | for (auto &LI : DeviceLinkerInputs) { |
| 2535 | auto *DeviceLinkAction = |
| 2536 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2537 | DA.add(*DeviceLinkAction, *ToolChains[0], |
| 2538 | CudaArchToString(GpuArchList[I]), AssociatedOffloadKind); |
| 2539 | ++I; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2540 | } |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2541 | } |
| 2542 | }; |
| 2543 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2544 | /// OpenMP action builder. The host bitcode is passed to the device frontend |
| 2545 | /// and all the device linked images are passed to the host link phase. |
| 2546 | class OpenMPActionBuilder final : public DeviceActionBuilder { |
| 2547 | /// The OpenMP actions for the current input. |
| 2548 | ActionList OpenMPDeviceActions; |
| 2549 | |
| 2550 | /// The linker inputs obtained for each toolchain. |
| 2551 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
| 2552 | |
| 2553 | public: |
| 2554 | OpenMPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2555 | const Driver::InputList &Inputs) |
| 2556 | : DeviceActionBuilder(C, Args, Inputs, Action::OFK_OpenMP) {} |
| 2557 | |
| 2558 | ActionBuilderReturnCode |
| 2559 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2560 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2561 | PhasesTy &Phases) override { |
| 2562 | |
| 2563 | // We should always have an action for each input. |
| 2564 | assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2565 | "Number of OpenMP actions and toolchains do not match."); |
| 2566 | |
| 2567 | // The host only depends on device action in the linking phase, when all |
| 2568 | // the device images have to be embedded in the host image. |
| 2569 | if (CurPhase == phases::Link) { |
| 2570 | assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2571 | "Toolchains and linker inputs sizes do not match."); |
| 2572 | auto LI = DeviceLinkerInputs.begin(); |
| 2573 | for (auto *A : OpenMPDeviceActions) { |
| 2574 | LI->push_back(A); |
| 2575 | ++LI; |
| 2576 | } |
| 2577 | |
| 2578 | // We passed the device action as a host dependence, so we don't need to |
| 2579 | // do anything else with them. |
| 2580 | OpenMPDeviceActions.clear(); |
| 2581 | return ABRT_Success; |
| 2582 | } |
| 2583 | |
| 2584 | // By default, we produce an action for each device arch. |
| 2585 | for (Action *&A : OpenMPDeviceActions) |
| 2586 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2587 | |
| 2588 | return ABRT_Success; |
| 2589 | } |
| 2590 | |
| 2591 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2592 | |
| 2593 | // If this is an input action replicate it for each OpenMP toolchain. |
| 2594 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2595 | OpenMPDeviceActions.clear(); |
| 2596 | for (unsigned I = 0; I < ToolChains.size(); ++I) |
| 2597 | OpenMPDeviceActions.push_back( |
| 2598 | C.MakeAction<InputAction>(IA->getInputArg(), IA->getType())); |
| 2599 | return ABRT_Success; |
| 2600 | } |
| 2601 | |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2602 | // If this is an unbundling action use it as is for each OpenMP toolchain. |
| 2603 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
| 2604 | OpenMPDeviceActions.clear(); |
| 2605 | for (unsigned I = 0; I < ToolChains.size(); ++I) { |
| 2606 | OpenMPDeviceActions.push_back(UA); |
| 2607 | UA->registerDependentActionInfo( |
| 2608 | ToolChains[I], /*BoundArch=*/StringRef(), Action::OFK_OpenMP); |
| 2609 | } |
| 2610 | return ABRT_Success; |
| 2611 | } |
| 2612 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2613 | // When generating code for OpenMP we use the host compile phase result as |
| 2614 | // a dependence to the device compile phase so that it can learn what |
| 2615 | // declarations should be emitted. However, this is not the only use for |
| 2616 | // the host action, so we prevent it from being collapsed. |
| 2617 | if (isa<CompileJobAction>(HostAction)) { |
| 2618 | HostAction->setCannotBeCollapsedWithNextDependentAction(); |
| 2619 | assert(ToolChains.size() == OpenMPDeviceActions.size() && |
| 2620 | "Toolchains and device action sizes do not match."); |
| 2621 | OffloadAction::HostDependence HDep( |
| 2622 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2623 | /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2624 | auto TC = ToolChains.begin(); |
| 2625 | for (Action *&A : OpenMPDeviceActions) { |
| 2626 | assert(isa<CompileJobAction>(A)); |
| 2627 | OffloadAction::DeviceDependences DDep; |
| 2628 | DDep.add(*A, **TC, /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2629 | A = C.MakeAction<OffloadAction>(HDep, DDep); |
| 2630 | ++TC; |
| 2631 | } |
| 2632 | } |
| 2633 | return ABRT_Success; |
| 2634 | } |
| 2635 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2636 | void appendTopLevelActions(ActionList &AL) override { |
| 2637 | if (OpenMPDeviceActions.empty()) |
| 2638 | return; |
| 2639 | |
| 2640 | // We should always have an action for each input. |
| 2641 | assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2642 | "Number of OpenMP actions and toolchains do not match."); |
| 2643 | |
| 2644 | // Append all device actions followed by the proper offload action. |
| 2645 | auto TI = ToolChains.begin(); |
| 2646 | for (auto *A : OpenMPDeviceActions) { |
| 2647 | OffloadAction::DeviceDependences Dep; |
| 2648 | Dep.add(*A, **TI, /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2649 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2650 | ++TI; |
| 2651 | } |
| 2652 | // We no longer need the action stored in this builder. |
| 2653 | OpenMPDeviceActions.clear(); |
| 2654 | } |
| 2655 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2656 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2657 | assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2658 | "Toolchains and linker inputs sizes do not match."); |
| 2659 | |
| 2660 | // Append a new link action for each device. |
| 2661 | auto TC = ToolChains.begin(); |
| 2662 | for (auto &LI : DeviceLinkerInputs) { |
| 2663 | auto *DeviceLinkAction = |
| 2664 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2665 | DA.add(*DeviceLinkAction, **TC, /*BoundArch=*/nullptr, |
| 2666 | Action::OFK_OpenMP); |
| 2667 | ++TC; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | bool initialize() override { |
| 2672 | // Get the OpenMP toolchains. If we don't get any, the action builder will |
| 2673 | // know there is nothing to do related to OpenMP offloading. |
| 2674 | auto OpenMPTCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); |
| 2675 | for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE; |
| 2676 | ++TI) |
| 2677 | ToolChains.push_back(TI->second); |
| 2678 | |
| 2679 | DeviceLinkerInputs.resize(ToolChains.size()); |
| 2680 | return false; |
| 2681 | } |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2682 | |
| 2683 | bool canUseBundlerUnbundler() const override { |
| 2684 | // OpenMP should use bundled files whenever possible. |
| 2685 | return true; |
| 2686 | } |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2687 | }; |
| 2688 | |
| 2689 | /// |
| 2690 | /// TODO: Add the implementation for other specialized builders here. |
| 2691 | /// |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2692 | |
| 2693 | /// Specialized builders being used by this offloading action builder. |
| 2694 | SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders; |
| 2695 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2696 | /// Flag set to true if all valid builders allow file bundling/unbundling. |
| 2697 | bool CanUseBundler; |
| 2698 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2699 | public: |
| 2700 | OffloadingActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2701 | const Driver::InputList &Inputs) |
Samuel Antao | 10e905c | 2016-10-27 01:08:58 +0000 | [diff] [blame] | 2702 | : C(C) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2703 | // Create a specialized builder for each device toolchain. |
| 2704 | |
| 2705 | IsValid = true; |
| 2706 | |
| 2707 | // Create a specialized builder for CUDA. |
| 2708 | SpecializedBuilders.push_back(new CudaActionBuilder(C, Args, Inputs)); |
| 2709 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2710 | // Create a specialized builder for HIP. |
| 2711 | SpecializedBuilders.push_back(new HIPActionBuilder(C, Args, Inputs)); |
| 2712 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2713 | // Create a specialized builder for OpenMP. |
| 2714 | SpecializedBuilders.push_back(new OpenMPActionBuilder(C, Args, Inputs)); |
| 2715 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2716 | // |
| 2717 | // TODO: Build other specialized builders here. |
| 2718 | // |
| 2719 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2720 | // Initialize all the builders, keeping track of errors. If all valid |
| 2721 | // builders agree that we can use bundling, set the flag to true. |
| 2722 | unsigned ValidBuilders = 0u; |
| 2723 | unsigned ValidBuildersSupportingBundling = 0u; |
| 2724 | for (auto *SB : SpecializedBuilders) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2725 | IsValid = IsValid && !SB->initialize(); |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2726 | |
| 2727 | // Update the counters if the builder is valid. |
| 2728 | if (SB->isValid()) { |
| 2729 | ++ValidBuilders; |
| 2730 | if (SB->canUseBundlerUnbundler()) |
| 2731 | ++ValidBuildersSupportingBundling; |
| 2732 | } |
| 2733 | } |
| 2734 | CanUseBundler = |
| 2735 | ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling; |
Artem Belevich | f981e30b | 2016-08-02 22:37:47 +0000 | [diff] [blame] | 2736 | } |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 2737 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2738 | ~OffloadingActionBuilder() { |
| 2739 | for (auto *SB : SpecializedBuilders) |
| 2740 | delete SB; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 2741 | } |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2742 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2743 | /// Generate an action that adds device dependences (if any) to a host action. |
| 2744 | /// If no device dependence actions exist, just return the host action \a |
| 2745 | /// HostAction. If an error is found or if no builder requires the host action |
| 2746 | /// to be generated, return nullptr. |
| 2747 | Action * |
| 2748 | addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg, |
| 2749 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2750 | DeviceActionBuilder::PhasesTy &Phases) { |
| 2751 | if (!IsValid) |
| 2752 | return nullptr; |
Justin Lebar | 7bf7798 | 2016-01-11 23:27:13 +0000 | [diff] [blame] | 2753 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2754 | if (SpecializedBuilders.empty()) |
| 2755 | return HostAction; |
| 2756 | |
| 2757 | assert(HostAction && "Invalid host action!"); |
| 2758 | |
| 2759 | OffloadAction::DeviceDependences DDeps; |
| 2760 | // Check if all the programming models agree we should not emit the host |
| 2761 | // action. Also, keep track of the offloading kinds employed. |
| 2762 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 2763 | unsigned InactiveBuilders = 0u; |
| 2764 | unsigned IgnoringBuilders = 0u; |
| 2765 | for (auto *SB : SpecializedBuilders) { |
| 2766 | if (!SB->isValid()) { |
| 2767 | ++InactiveBuilders; |
| 2768 | continue; |
| 2769 | } |
| 2770 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2771 | auto RetCode = |
| 2772 | SB->getDeviceDependences(DDeps, CurPhase, FinalPhase, Phases); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2773 | |
| 2774 | // If the builder explicitly says the host action should be ignored, |
| 2775 | // we need to increment the variable that tracks the builders that request |
| 2776 | // the host object to be ignored. |
| 2777 | if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host) |
| 2778 | ++IgnoringBuilders; |
| 2779 | |
| 2780 | // Unless the builder was inactive for this action, we have to record the |
| 2781 | // offload kind because the host will have to use it. |
| 2782 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 2783 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 2784 | } |
| 2785 | |
| 2786 | // If all builders agree that the host object should be ignored, just return |
| 2787 | // nullptr. |
| 2788 | if (IgnoringBuilders && |
| 2789 | SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders)) |
| 2790 | return nullptr; |
| 2791 | |
| 2792 | if (DDeps.getActions().empty()) |
| 2793 | return HostAction; |
| 2794 | |
| 2795 | // We have dependences we need to bundle together. We use an offload action |
| 2796 | // for that. |
| 2797 | OffloadAction::HostDependence HDep( |
| 2798 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2799 | /*BoundArch=*/nullptr, DDeps); |
| 2800 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2803 | /// Generate an action that adds a host dependence to a device action. The |
| 2804 | /// results will be kept in this action builder. Return true if an error was |
| 2805 | /// found. |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2806 | bool addHostDependenceToDeviceActions(Action *&HostAction, |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2807 | const Arg *InputArg) { |
| 2808 | if (!IsValid) |
| 2809 | return true; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2810 | |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2811 | // If we are supporting bundling/unbundling and the current action is an |
| 2812 | // input action of non-source file, we replace the host action by the |
| 2813 | // unbundling action. The bundler tool has the logic to detect if an input |
| 2814 | // is a bundle or not and if the input is not a bundle it assumes it is a |
| 2815 | // host file. Therefore it is safe to create an unbundling action even if |
| 2816 | // the input is not a bundle. |
| 2817 | if (CanUseBundler && isa<InputAction>(HostAction) && |
| 2818 | InputArg->getOption().getKind() == llvm::opt::Option::InputClass && |
| 2819 | !types::isSrcFile(HostAction->getType())) { |
| 2820 | auto UnbundlingHostAction = |
| 2821 | C.MakeAction<OffloadUnbundlingJobAction>(HostAction); |
| 2822 | UnbundlingHostAction->registerDependentActionInfo( |
| 2823 | C.getSingleOffloadToolChain<Action::OFK_Host>(), |
Yaxun Liu | f37b50a | 2018-07-24 01:03:44 +0000 | [diff] [blame] | 2824 | /*BoundArch=*/StringRef(), Action::OFK_Host); |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2825 | HostAction = UnbundlingHostAction; |
| 2826 | } |
| 2827 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2828 | assert(HostAction && "Invalid host action!"); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2829 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2830 | // Register the offload kinds that are used. |
| 2831 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 2832 | for (auto *SB : SpecializedBuilders) { |
| 2833 | if (!SB->isValid()) |
| 2834 | continue; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2835 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2836 | auto RetCode = SB->addDeviceDepences(HostAction); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2837 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2838 | // Host dependences for device actions are not compatible with that same |
| 2839 | // action being ignored. |
| 2840 | assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host && |
| 2841 | "Host dependence not expected to be ignored.!"); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 2842 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2843 | // Unless the builder was inactive for this action, we have to record the |
| 2844 | // offload kind because the host will have to use it. |
| 2845 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 2846 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 2847 | } |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2848 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2849 | return false; |
| 2850 | } |
| 2851 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2852 | /// Add the offloading top level actions to the provided action list. This |
| 2853 | /// function can replace the host action by a bundling action if the |
| 2854 | /// programming models allow it. |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2855 | bool appendTopLevelActions(ActionList &AL, Action *HostAction, |
| 2856 | const Arg *InputArg) { |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2857 | // Get the device actions to be appended. |
| 2858 | ActionList OffloadAL; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2859 | for (auto *SB : SpecializedBuilders) { |
| 2860 | if (!SB->isValid()) |
| 2861 | continue; |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2862 | SB->appendTopLevelActions(OffloadAL); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2865 | // If we can use the bundler, replace the host action by the bundling one in |
| 2866 | // the resulting list. Otherwise, just append the device actions. |
| 2867 | if (CanUseBundler && !OffloadAL.empty()) { |
| 2868 | // Add the host action to the list in order to create the bundling action. |
| 2869 | OffloadAL.push_back(HostAction); |
| 2870 | |
| 2871 | // We expect that the host action was just appended to the action list |
| 2872 | // before this method was called. |
| 2873 | assert(HostAction == AL.back() && "Host action not in the list??"); |
| 2874 | HostAction = C.MakeAction<OffloadBundlingJobAction>(OffloadAL); |
| 2875 | AL.back() = HostAction; |
| 2876 | } else |
| 2877 | AL.append(OffloadAL.begin(), OffloadAL.end()); |
| 2878 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2879 | // Propagate to the current host action (if any) the offload information |
| 2880 | // associated with the current input. |
| 2881 | if (HostAction) |
| 2882 | HostAction->propagateHostOffloadInfo(InputArgToOffloadKindMap[InputArg], |
| 2883 | /*BoundArch=*/nullptr); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2884 | return false; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2885 | } |
Artem Belevich | 5bde4e0 | 2015-07-20 20:02:54 +0000 | [diff] [blame] | 2886 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2887 | /// Processes the host linker action. This currently consists of replacing it |
| 2888 | /// with an offload action if there are device link objects and propagate to |
| 2889 | /// the host action all the offload kinds used in the current compilation. The |
| 2890 | /// resulting action is returned. |
| 2891 | Action *processHostLinkAction(Action *HostAction) { |
| 2892 | // Add all the dependences from the device linking actions. |
| 2893 | OffloadAction::DeviceDependences DDeps; |
| 2894 | for (auto *SB : SpecializedBuilders) { |
| 2895 | if (!SB->isValid()) |
| 2896 | continue; |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 2897 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2898 | SB->appendLinkDependences(DDeps); |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 2899 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 2900 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2901 | // Calculate all the offload kinds used in the current compilation. |
| 2902 | unsigned ActiveOffloadKinds = 0u; |
| 2903 | for (auto &I : InputArgToOffloadKindMap) |
| 2904 | ActiveOffloadKinds |= I.second; |
| 2905 | |
| 2906 | // If we don't have device dependencies, we don't have to create an offload |
| 2907 | // action. |
| 2908 | if (DDeps.getActions().empty()) { |
| 2909 | // Propagate all the active kinds to host action. Given that it is a link |
| 2910 | // action it is assumed to depend on all actions generated so far. |
| 2911 | HostAction->propagateHostOffloadInfo(ActiveOffloadKinds, |
| 2912 | /*BoundArch=*/nullptr); |
| 2913 | return HostAction; |
| 2914 | } |
| 2915 | |
| 2916 | // Create the offload action with all dependences. When an offload action |
| 2917 | // is created the kinds are propagated to the host action, so we don't have |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 2918 | // to do that explicitly here. |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2919 | OffloadAction::HostDependence HDep( |
| 2920 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2921 | /*BoundArch*/ nullptr, ActiveOffloadKinds); |
| 2922 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
| 2923 | } |
| 2924 | }; |
| 2925 | } // anonymous namespace. |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2926 | |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 2927 | void Driver::BuildActions(Compilation &C, DerivedArgList &Args, |
| 2928 | const InputList &Inputs, ActionList &Actions) const { |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2929 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 2930 | |
Daniel Dunbar | 34c4187 | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 2931 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 2932 | Diag(clang::diag::err_drv_no_input_files); |
| 2933 | return; |
| 2934 | } |
| 2935 | |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 2936 | Arg *FinalPhaseArg; |
| 2937 | phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg); |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 2938 | |
Bob Haarman | aaf5191 | 2017-02-27 19:40:19 +0000 | [diff] [blame] | 2939 | if (FinalPhase == phases::Link) { |
| 2940 | if (Args.hasArg(options::OPT_emit_llvm)) |
| 2941 | Diag(clang::diag::err_drv_emit_llvm_link); |
| 2942 | if (IsCLMode() && LTOMode != LTOK_None && |
| 2943 | !Args.getLastArgValue(options::OPT_fuse_ld_EQ).equals_lower("lld")) |
| 2944 | Diag(clang::diag::err_drv_lto_without_lld); |
Rafael Espindola | e802564 | 2013-08-25 14:27:09 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2947 | // Reject -Z* at the top level, these options should never have been exposed |
| 2948 | // by gcc. |
Daniel Dunbar | dd76524 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 2949 | if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) |
Daniel Dunbar | 0e75994 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 2950 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 2951 | |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 2952 | // Diagnose misuse of /Fo. |
| 2953 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) { |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 2954 | StringRef V = A->getValue(); |
Hans Wennborg | 6164753 | 2014-11-17 19:16:36 +0000 | [diff] [blame] | 2955 | if (Inputs.size() > 1 && !V.empty() && |
| 2956 | !llvm::sys::path::is_separator(V.back())) { |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 2957 | // Check whether /Fo tries to name an output file for multiple inputs. |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 2958 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 2959 | << A->getSpelling() << V; |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 2960 | Args.eraseArg(options::OPT__SLASH_Fo); |
| 2961 | } |
| 2962 | } |
| 2963 | |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 2964 | // Diagnose misuse of /Fa. |
| 2965 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) { |
| 2966 | StringRef V = A->getValue(); |
Hans Wennborg | 6164753 | 2014-11-17 19:16:36 +0000 | [diff] [blame] | 2967 | if (Inputs.size() > 1 && !V.empty() && |
| 2968 | !llvm::sys::path::is_separator(V.back())) { |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 2969 | // Check whether /Fa tries to name an asm file for multiple inputs. |
| 2970 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 2971 | << A->getSpelling() << V; |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 2972 | Args.eraseArg(options::OPT__SLASH_Fa); |
| 2973 | } |
| 2974 | } |
| 2975 | |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 2976 | // Diagnose misuse of /o. |
| 2977 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) { |
| 2978 | if (A->getValue()[0] == '\0') { |
| 2979 | // It has to have a value. |
| 2980 | Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1; |
| 2981 | Args.eraseArg(options::OPT__SLASH_o); |
| 2982 | } |
| 2983 | } |
| 2984 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 2985 | // Ignore /Yc/Yu if both /Yc and /Yu passed but with different filenames. |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 2986 | Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); |
| 2987 | Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 2988 | if (YcArg && YuArg && strcmp(YcArg->getValue(), YuArg->getValue()) != 0) { |
| 2989 | Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl); |
| 2990 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 2991 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 2992 | YcArg = YuArg = nullptr; |
| 2993 | } |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 2994 | if (YcArg && Inputs.size() > 1) { |
| 2995 | Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl); |
| 2996 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 2997 | YcArg = nullptr; |
| 2998 | } |
Erich Keane | eaca388 | 2018-08-17 13:43:39 +0000 | [diff] [blame] | 2999 | if (FinalPhase == phases::Preprocess || Args.hasArg(options::OPT__SLASH_Y_)) { |
| 3000 | // If only preprocessing or /Y- is used, all pch handling is disabled. |
| 3001 | // Rather than check for it everywhere, just remove clang-cl pch-related |
| 3002 | // flags here. |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3003 | Args.eraseArg(options::OPT__SLASH_Fp); |
| 3004 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3005 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 3006 | YcArg = YuArg = nullptr; |
| 3007 | } |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3008 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3009 | // Builder to be used to build offloading actions. |
| 3010 | OffloadingActionBuilder OffloadBuilder(C, Args, Inputs); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3011 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3012 | // Construct the actions to perform. |
| 3013 | ActionList LinkerInputs; |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 3014 | |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3015 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL; |
Artem Belevich | 5bde4e0 | 2015-07-20 20:02:54 +0000 | [diff] [blame] | 3016 | for (auto &I : Inputs) { |
| 3017 | types::ID InputType = I.first; |
| 3018 | const Arg *InputArg = I.second; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3019 | |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3020 | PL.clear(); |
| 3021 | types::getCompilationPhases(InputType, PL); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3022 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3023 | // If the first step comes after the final phase we are doing as part of |
| 3024 | // this compilation, warn the user about it. |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3025 | phases::ID InitialPhase = PL[0]; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3026 | if (InitialPhase > FinalPhase) { |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 3027 | if (InputArg->isClaimed()) |
| 3028 | continue; |
| 3029 | |
Daniel Dunbar | adc5c7c | 2009-03-19 07:57:08 +0000 | [diff] [blame] | 3030 | // Claim here to avoid the more general unused warning. |
| 3031 | InputArg->claim(); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3032 | |
Daniel Dunbar | 2db3e73 | 2011-04-20 15:44:48 +0000 | [diff] [blame] | 3033 | // Suppress all unused style warnings with -Qunused-arguments |
| 3034 | if (Args.hasArg(options::OPT_Qunused_arguments)) |
| 3035 | continue; |
| 3036 | |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3037 | // Special case when final phase determined by binary name, rather than |
| 3038 | // by a command-line argument with a corresponding Arg. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 3039 | if (CCCIsCPP()) |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3040 | Diag(clang::diag::warn_drv_input_file_unused_by_cpp) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3041 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3042 | // Special case '-E' warning on a previously preprocessed file to make |
| 3043 | // more sense. |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3044 | else if (InitialPhase == phases::Compile && |
| 3045 | FinalPhase == phases::Preprocess && |
| 3046 | getPreprocessedType(InputType) == types::TY_INVALID) |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3047 | Diag(clang::diag::warn_drv_preprocessed_input_file_unused) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3048 | << InputArg->getAsString(Args) << !!FinalPhaseArg |
| 3049 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3050 | else |
| 3051 | Diag(clang::diag::warn_drv_input_file_unused) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3052 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase) |
| 3053 | << !!FinalPhaseArg |
| 3054 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3055 | continue; |
| 3056 | } |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3057 | |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3058 | if (YcArg) { |
| 3059 | // Add a separate precompile phase for the compile phase. |
| 3060 | if (FinalPhase >= phases::Compile) { |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3061 | const types::ID HeaderType = lookupHeaderTypeForSourceType(InputType); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3062 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PCHPL; |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3063 | types::getCompilationPhases(HeaderType, PCHPL); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3064 | // Build the pipeline for the pch file. |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3065 | Action *ClangClPch = |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 3066 | C.MakeAction<InputAction>(*InputArg, HeaderType); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3067 | for (phases::ID Phase : PCHPL) |
| 3068 | ClangClPch = ConstructPhaseAction(C, Args, Phase, ClangClPch); |
| 3069 | assert(ClangClPch); |
| 3070 | Actions.push_back(ClangClPch); |
| 3071 | // The driver currently exits after the first failed command. This |
| 3072 | // relies on that behavior, to make sure if the pch generation fails, |
| 3073 | // the main compilation won't run. |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 3074 | // FIXME: If the main compilation fails, the PCH generation should |
| 3075 | // probably not be considered successful either. |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3079 | // Build the pipeline for this file. |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3080 | Action *Current = C.MakeAction<InputAction>(*InputArg, InputType); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3081 | |
| 3082 | // Use the current host action in any of the offloading actions, if |
| 3083 | // required. |
| 3084 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3085 | break; |
| 3086 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3087 | for (SmallVectorImpl<phases::ID>::iterator i = PL.begin(), e = PL.end(); |
| 3088 | i != e; ++i) { |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3089 | phases::ID Phase = *i; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3090 | |
| 3091 | // We are done if this step is past what the user requested. |
| 3092 | if (Phase > FinalPhase) |
| 3093 | break; |
| 3094 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3095 | // Add any offload action the host action depends on. |
| 3096 | Current = OffloadBuilder.addDeviceDependencesToHostAction( |
| 3097 | Current, InputArg, Phase, FinalPhase, PL); |
| 3098 | if (!Current) |
| 3099 | break; |
| 3100 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3101 | // Queue linker inputs. |
| 3102 | if (Phase == phases::Link) { |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3103 | assert((i + 1) == e && "linking must be final compilation step."); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3104 | LinkerInputs.push_back(Current); |
| 3105 | Current = nullptr; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3106 | break; |
| 3107 | } |
| 3108 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3109 | // Otherwise construct the appropriate action. |
| 3110 | auto *NewCurrent = ConstructPhaseAction(C, Args, Phase, Current); |
| 3111 | |
| 3112 | // We didn't create a new action, so we will just move to the next phase. |
| 3113 | if (NewCurrent == Current) |
Daniel Dunbar | 1386495 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 3114 | continue; |
| 3115 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3116 | Current = NewCurrent; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3117 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3118 | // Use the current host action in any of the offloading actions, if |
| 3119 | // required. |
| 3120 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3121 | break; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3122 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3123 | if (Current->getType() == types::TY_Nothing) |
| 3124 | break; |
| 3125 | } |
| 3126 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3127 | // If we ended with something, add to the output list. |
| 3128 | if (Current) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3129 | Actions.push_back(Current); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3130 | |
| 3131 | // Add any top level actions generated for offloading. |
| 3132 | OffloadBuilder.appendTopLevelActions(Actions, Current, InputArg); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 3133 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3134 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3135 | // Add a link action if necessary. |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3136 | if (!LinkerInputs.empty()) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3137 | Action *LA = C.MakeAction<LinkJobAction>(LinkerInputs, types::TY_Image); |
| 3138 | LA = OffloadBuilder.processHostLinkAction(LA); |
| 3139 | Actions.push_back(LA); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3140 | } |
Daniel Dunbar | c7a67b7 | 2009-12-22 23:19:32 +0000 | [diff] [blame] | 3141 | |
| 3142 | // If we are linking, claim any options which are obviously only used for |
| 3143 | // compilation. |
Hans Wennborg | a8ef14f | 2013-09-17 00:03:41 +0000 | [diff] [blame] | 3144 | if (FinalPhase == phases::Link && PL.size() == 1) { |
Daniel Dunbar | c7a67b7 | 2009-12-22 23:19:32 +0000 | [diff] [blame] | 3145 | Args.ClaimAllArgs(options::OPT_CompileOnly_Group); |
Hans Wennborg | a8ef14f | 2013-09-17 00:03:41 +0000 | [diff] [blame] | 3146 | Args.ClaimAllArgs(options::OPT_cl_compile_Group); |
| 3147 | } |
| 3148 | |
| 3149 | // Claim ignored clang-cl options. |
| 3150 | Args.ClaimAllArgs(options::OPT_cl_ignored_Group); |
Artem Belevich | baae093 | 2015-07-28 21:01:30 +0000 | [diff] [blame] | 3151 | |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 3152 | // Claim --cuda-host-only and --cuda-compile-host-device, which may be passed |
| 3153 | // to non-CUDA compilations and should not trigger warnings there. |
Artem Belevich | baae093 | 2015-07-28 21:01:30 +0000 | [diff] [blame] | 3154 | Args.ClaimAllArgs(options::OPT_cuda_host_only); |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 3155 | Args.ClaimAllArgs(options::OPT_cuda_compile_host_device); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 3158 | Action *Driver::ConstructPhaseAction( |
| 3159 | Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input, |
| 3160 | Action::OffloadKind TargetDeviceOffloadKind) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 3161 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Some types skip the assembler phase (e.g., llvm-bc), but we can't |
| 3164 | // encode this in the steps because the intermediate type depends on |
| 3165 | // arguments. Just special case here. |
| 3166 | if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm) |
| 3167 | return Input; |
| 3168 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3169 | // Build the appropriate action. |
| 3170 | switch (Phase) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3171 | case phases::Link: |
| 3172 | llvm_unreachable("link action invalid here."); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3173 | case phases::Preprocess: { |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3174 | types::ID OutputTy; |
| 3175 | // -{M, MM} alter the output type. |
Daniel Dunbar | 86aed7d | 2010-12-08 21:33:40 +0000 | [diff] [blame] | 3176 | if (Args.hasArg(options::OPT_M, options::OPT_MM)) { |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3177 | OutputTy = types::TY_Dependencies; |
| 3178 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 3179 | OutputTy = Input->getType(); |
| 3180 | if (!Args.hasFlag(options::OPT_frewrite_includes, |
Justin Bogner | ce8245b | 2014-06-24 08:01:01 +0000 | [diff] [blame] | 3181 | options::OPT_fno_rewrite_includes, false) && |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 3182 | !Args.hasFlag(options::OPT_frewrite_imports, |
| 3183 | options::OPT_fno_rewrite_imports, false) && |
Justin Bogner | ce8245b | 2014-06-24 08:01:01 +0000 | [diff] [blame] | 3184 | !CCGenDiagnostics) |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 3185 | OutputTy = types::getPreprocessedType(OutputTy); |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3186 | assert(OutputTy != types::TY_INVALID && |
| 3187 | "Cannot preprocess this input type!"); |
| 3188 | } |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3189 | return C.MakeAction<PreprocessJobAction>(Input, OutputTy); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3190 | } |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3191 | case phases::Precompile: { |
Richard Smith | dd4ad3d | 2016-08-30 19:06:26 +0000 | [diff] [blame] | 3192 | types::ID OutputTy = getPrecompiledType(Input->getType()); |
| 3193 | assert(OutputTy != types::TY_INVALID && |
| 3194 | "Cannot precompile this input type!"); |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3195 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 3196 | // Syntax checks should not emit a PCH file |
| 3197 | OutputTy = types::TY_Nothing; |
| 3198 | } |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3199 | return C.MakeAction<PrecompileJobAction>(Input, OutputTy); |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3200 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3201 | case phases::Compile: { |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3202 | if (Args.hasArg(options::OPT_fsyntax_only)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3203 | return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3204 | if (Args.hasArg(options::OPT_rewrite_objc)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3205 | return C.MakeAction<CompileJobAction>(Input, types::TY_RewrittenObjC); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3206 | if (Args.hasArg(options::OPT_rewrite_legacy_objc)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3207 | return C.MakeAction<CompileJobAction>(Input, |
| 3208 | types::TY_RewrittenLegacyObjC); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3209 | if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3210 | return C.MakeAction<AnalyzeJobAction>(Input, types::TY_Plist); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3211 | if (Args.hasArg(options::OPT__migrate)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3212 | return C.MakeAction<MigrateJobAction>(Input, types::TY_Remap); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3213 | if (Args.hasArg(options::OPT_emit_ast)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3214 | return C.MakeAction<CompileJobAction>(Input, types::TY_AST); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3215 | if (Args.hasArg(options::OPT_module_file_info)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3216 | return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3217 | if (Args.hasArg(options::OPT_verify_pch)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3218 | return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing); |
| 3219 | return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC); |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3220 | } |
| 3221 | case phases::Backend: { |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 3222 | if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3223 | types::ID Output = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3224 | Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC; |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3225 | return C.MakeAction<BackendJobAction>(Input, Output); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3226 | } |
| 3227 | if (Args.hasArg(options::OPT_emit_llvm)) { |
Shuxin Yang | 4b3c7ff | 2013-08-23 21:34:57 +0000 | [diff] [blame] | 3228 | types::ID Output = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3229 | Args.hasArg(options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC; |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3230 | return C.MakeAction<BackendJobAction>(Input, Output); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3231 | } |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3232 | return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3233 | } |
| 3234 | case phases::Assemble: |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 3235 | return C.MakeAction<AssembleJobAction>(std::move(Input), types::TY_Object); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3238 | llvm_unreachable("invalid phase in ConstructPhaseAction"); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 3241 | void Driver::BuildJobs(Compilation &C) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 3242 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3243 | |
| 3244 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 3245 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3246 | // It is an error to provide a -o option if we are making multiple output |
| 3247 | // files. |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3248 | if (FinalOutput) { |
| 3249 | unsigned NumOutputs = 0; |
Saleem Abdulrasool | da3f4e5 | 2014-12-29 21:02:47 +0000 | [diff] [blame] | 3250 | for (const Action *A : C.getActions()) |
| 3251 | if (A->getType() != types::TY_Nothing) |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3252 | ++NumOutputs; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3253 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3254 | if (NumOutputs > 1) { |
| 3255 | Diag(clang::diag::err_drv_output_argument_with_multiple_files); |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3256 | FinalOutput = nullptr; |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3257 | } |
| 3258 | } |
| 3259 | |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3260 | // Collect the list of architectures. |
| 3261 | llvm::StringSet<> ArchNames; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3262 | if (C.getDefaultToolChain().getTriple().isOSBinFormatMachO()) |
| 3263 | for (const Arg *A : C.getArgs()) |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3264 | if (A->getOption().matches(options::OPT_arch)) |
| 3265 | ArchNames.insert(A->getValue()); |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3266 | |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3267 | // Set of (Action, canonical ToolChain triple) pairs we've built jobs for. |
| 3268 | std::map<std::pair<const Action *, std::string>, InputInfo> CachedResults; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3269 | for (Action *A : C.getActions()) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3270 | // If we are linking an image for multiple archs then the linker wants |
| 3271 | // -arch_multiple and -final_output <final image name>. Unfortunately, this |
| 3272 | // doesn't fit in cleanly because we have to pass this information down. |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3273 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3274 | // FIXME: This is a hack; find a cleaner way to integrate this into the |
| 3275 | // process. |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3276 | const char *LinkingOutput = nullptr; |
Daniel Dunbar | dd76524 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 3277 | if (isa<LipoJobAction>(A)) { |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3278 | if (FinalOutput) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 3279 | LinkingOutput = FinalOutput->getValue(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3280 | else |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 3281 | LinkingOutput = getDefaultImageName(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3284 | BuildJobsForAction(C, A, &C.getDefaultToolChain(), |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3285 | /*BoundArch*/ StringRef(), |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3286 | /*AtTopLevel*/ true, |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3287 | /*MultipleArchs*/ ArchNames.size() > 1, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3288 | /*LinkingOutput*/ LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3289 | /*TargetDeviceOffloadKind*/ Action::OFK_None); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3290 | } |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3291 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3292 | // If the user passed -Qunused-arguments or there were errors, don't warn |
| 3293 | // about any unused arguments. |
Argyrios Kyrtzidis | 31448a4 | 2010-11-18 21:47:07 +0000 | [diff] [blame] | 3294 | if (Diags.hasErrorOccurred() || |
Daniel Dunbar | a3cfbe3 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 3295 | C.getArgs().hasArg(options::OPT_Qunused_arguments)) |
Daniel Dunbar | d175d97 | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 3296 | return; |
| 3297 | |
Daniel Dunbar | 58399ae | 2009-03-29 22:24:54 +0000 | [diff] [blame] | 3298 | // Claim -### here. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3299 | (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3300 | |
Nico Weber | 47bf505 | 2016-04-25 21:15:49 +0000 | [diff] [blame] | 3301 | // Claim --driver-mode, --rsp-quoting, it was handled earlier. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3302 | (void)C.getArgs().hasArg(options::OPT_driver_mode); |
Nico Weber | 47bf505 | 2016-04-25 21:15:49 +0000 | [diff] [blame] | 3303 | (void)C.getArgs().hasArg(options::OPT_rsp_quoting); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 3304 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3305 | for (Arg *A : C.getArgs()) { |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3306 | // FIXME: It would be nice to be able to send the argument to the |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3307 | // DiagnosticsEngine, so that extra values, position, and so on could be |
| 3308 | // printed. |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3309 | if (!A->isClaimed()) { |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 3310 | if (A->getOption().hasFlag(options::NoArgumentUnused)) |
Daniel Dunbar | a3cfbe3 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 3311 | continue; |
| 3312 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3313 | // Suppress the warning automatically if this is just a flag, and it is an |
| 3314 | // instance of an argument we already claimed. |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3315 | const Option &Opt = A->getOption(); |
Michael J. Spencer | ad3ccc3 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 3316 | if (Opt.getKind() == Option::FlagClass) { |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3317 | bool DuplicateClaimed = false; |
| 3318 | |
Sean Silva | 14facf3 | 2015-06-09 01:57:17 +0000 | [diff] [blame] | 3319 | for (const Arg *AA : C.getArgs().filtered(&Opt)) { |
| 3320 | if (AA->isClaimed()) { |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3321 | DuplicateClaimed = true; |
| 3322 | break; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | if (DuplicateClaimed) |
| 3327 | continue; |
| 3328 | } |
| 3329 | |
Ehsan Akhgari | d851833 | 2016-01-25 21:14:52 +0000 | [diff] [blame] | 3330 | // In clang-cl, don't mention unknown arguments here since they have |
| 3331 | // already been warned about. |
| 3332 | if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) |
| 3333 | Diag(clang::diag::warn_drv_unused_argument) |
| 3334 | << A->getAsString(C.getArgs()); |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3335 | } |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3336 | } |
Daniel Dunbar | 95e6b19 | 2009-03-13 22:12:33 +0000 | [diff] [blame] | 3337 | } |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3338 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3339 | namespace { |
| 3340 | /// Utility class to control the collapse of dependent actions and select the |
| 3341 | /// tools accordingly. |
| 3342 | class ToolSelector final { |
| 3343 | /// The tool chain this selector refers to. |
| 3344 | const ToolChain &TC; |
Daniel Dunbar | f9ff350 | 2010-05-14 02:03:00 +0000 | [diff] [blame] | 3345 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3346 | /// The compilation this selector refers to. |
| 3347 | const Compilation &C; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3348 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3349 | /// The base action this selector refers to. |
| 3350 | const JobAction *BaseAction; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3351 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3352 | /// Set to true if the current toolchain refers to host actions. |
| 3353 | bool IsHostSelector; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3354 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3355 | /// Set to true if save-temps and embed-bitcode functionalities are active. |
| 3356 | bool SaveTemps; |
| 3357 | bool EmbedBitcode; |
| 3358 | |
| 3359 | /// Get previous dependent action or null if that does not exist. If |
| 3360 | /// \a CanBeCollapsed is false, that action must be legal to collapse or |
| 3361 | /// null will be returned. |
| 3362 | const JobAction *getPrevDependentAction(const ActionList &Inputs, |
| 3363 | ActionList &SavedOffloadAction, |
| 3364 | bool CanBeCollapsed = true) { |
| 3365 | // An option can be collapsed only if it has a single input. |
| 3366 | if (Inputs.size() != 1) |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3367 | return nullptr; |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3368 | |
| 3369 | Action *CurAction = *Inputs.begin(); |
| 3370 | if (CanBeCollapsed && |
| 3371 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3372 | return nullptr; |
| 3373 | |
| 3374 | // If the input action is an offload action. Look through it and save any |
| 3375 | // offload action that can be dropped in the event of a collapse. |
| 3376 | if (auto *OA = dyn_cast<OffloadAction>(CurAction)) { |
| 3377 | // If the dependent action is a device action, we will attempt to collapse |
| 3378 | // only with other device actions. Otherwise, we would do the same but |
| 3379 | // with host actions only. |
| 3380 | if (!IsHostSelector) { |
| 3381 | if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) { |
| 3382 | CurAction = |
| 3383 | OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true); |
| 3384 | if (CanBeCollapsed && |
| 3385 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3386 | return nullptr; |
| 3387 | SavedOffloadAction.push_back(OA); |
| 3388 | return dyn_cast<JobAction>(CurAction); |
| 3389 | } |
| 3390 | } else if (OA->hasHostDependence()) { |
| 3391 | CurAction = OA->getHostDependence(); |
| 3392 | if (CanBeCollapsed && |
| 3393 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3394 | return nullptr; |
| 3395 | SavedOffloadAction.push_back(OA); |
| 3396 | return dyn_cast<JobAction>(CurAction); |
| 3397 | } |
| 3398 | return nullptr; |
| 3399 | } |
| 3400 | |
| 3401 | return dyn_cast<JobAction>(CurAction); |
| 3402 | } |
| 3403 | |
| 3404 | /// Return true if an assemble action can be collapsed. |
| 3405 | bool canCollapseAssembleAction() const { |
| 3406 | return TC.useIntegratedAs() && !SaveTemps && |
| 3407 | !C.getArgs().hasArg(options::OPT_via_file_asm) && |
| 3408 | !C.getArgs().hasArg(options::OPT__SLASH_FA) && |
| 3409 | !C.getArgs().hasArg(options::OPT__SLASH_Fa); |
| 3410 | } |
| 3411 | |
| 3412 | /// Return true if a preprocessor action can be collapsed. |
| 3413 | bool canCollapsePreprocessorAction() const { |
| 3414 | return !C.getArgs().hasArg(options::OPT_no_integrated_cpp) && |
| 3415 | !C.getArgs().hasArg(options::OPT_traditional_cpp) && !SaveTemps && |
| 3416 | !C.getArgs().hasArg(options::OPT_rewrite_objc); |
| 3417 | } |
| 3418 | |
| 3419 | /// Struct that relates an action with the offload actions that would be |
| 3420 | /// collapsed with it. |
| 3421 | struct JobActionInfo final { |
| 3422 | /// The action this info refers to. |
| 3423 | const JobAction *JA = nullptr; |
| 3424 | /// The offload actions we need to take care off if this action is |
| 3425 | /// collapsed. |
| 3426 | ActionList SavedOffloadAction; |
| 3427 | }; |
| 3428 | |
| 3429 | /// Append collapsed offload actions from the give nnumber of elements in the |
| 3430 | /// action info array. |
| 3431 | static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction, |
| 3432 | ArrayRef<JobActionInfo> &ActionInfo, |
| 3433 | unsigned ElementNum) { |
| 3434 | assert(ElementNum <= ActionInfo.size() && "Invalid number of elements."); |
| 3435 | for (unsigned I = 0; I < ElementNum; ++I) |
| 3436 | CollapsedOffloadAction.append(ActionInfo[I].SavedOffloadAction.begin(), |
| 3437 | ActionInfo[I].SavedOffloadAction.end()); |
| 3438 | } |
| 3439 | |
| 3440 | /// Functions that attempt to perform the combining. They detect if that is |
| 3441 | /// legal, and if so they update the inputs \a Inputs and the offload action |
| 3442 | /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with |
| 3443 | /// the combined action is returned. If the combining is not legal or if the |
| 3444 | /// tool does not exist, null is returned. |
| 3445 | /// Currently three kinds of collapsing are supported: |
| 3446 | /// - Assemble + Backend + Compile; |
| 3447 | /// - Assemble + Backend ; |
| 3448 | /// - Backend + Compile. |
| 3449 | const Tool * |
| 3450 | combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
| 3451 | const ActionList *&Inputs, |
| 3452 | ActionList &CollapsedOffloadAction) { |
| 3453 | if (ActionInfo.size() < 3 || !canCollapseAssembleAction()) |
| 3454 | return nullptr; |
| 3455 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3456 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3457 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[2].JA); |
| 3458 | if (!AJ || !BJ || !CJ) |
| 3459 | return nullptr; |
| 3460 | |
| 3461 | // Get compiler tool. |
| 3462 | const Tool *T = TC.SelectTool(*CJ); |
| 3463 | if (!T) |
| 3464 | return nullptr; |
| 3465 | |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 3466 | // When using -fembed-bitcode, it is required to have the same tool (clang) |
| 3467 | // for both CompilerJA and BackendJA. Otherwise, combine two stages. |
| 3468 | if (EmbedBitcode) { |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3469 | const Tool *BT = TC.SelectTool(*BJ); |
| 3470 | if (BT == T) |
| 3471 | return nullptr; |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 3472 | } |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3473 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3474 | if (!T->hasIntegratedAssembler()) |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3475 | return nullptr; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3476 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3477 | Inputs = &CJ->getInputs(); |
| 3478 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3479 | /*NumElements=*/3); |
| 3480 | return T; |
| 3481 | } |
| 3482 | const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo, |
| 3483 | const ActionList *&Inputs, |
| 3484 | ActionList &CollapsedOffloadAction) { |
| 3485 | if (ActionInfo.size() < 2 || !canCollapseAssembleAction()) |
| 3486 | return nullptr; |
| 3487 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3488 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3489 | if (!AJ || !BJ) |
| 3490 | return nullptr; |
| 3491 | |
| 3492 | // Retrieve the compile job, backend action must always be preceded by one. |
| 3493 | ActionList CompileJobOffloadActions; |
| 3494 | auto *CJ = getPrevDependentAction(BJ->getInputs(), CompileJobOffloadActions, |
| 3495 | /*CanBeCollapsed=*/false); |
| 3496 | if (!AJ || !BJ || !CJ) |
| 3497 | return nullptr; |
| 3498 | |
| 3499 | assert(isa<CompileJobAction>(CJ) && |
| 3500 | "Expecting compile job preceding backend job."); |
| 3501 | |
| 3502 | // Get compiler tool. |
| 3503 | const Tool *T = TC.SelectTool(*CJ); |
| 3504 | if (!T) |
| 3505 | return nullptr; |
| 3506 | |
| 3507 | if (!T->hasIntegratedAssembler()) |
| 3508 | return nullptr; |
| 3509 | |
| 3510 | Inputs = &BJ->getInputs(); |
| 3511 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3512 | /*NumElements=*/2); |
| 3513 | return T; |
| 3514 | } |
| 3515 | const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
| 3516 | const ActionList *&Inputs, |
| 3517 | ActionList &CollapsedOffloadAction) { |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3518 | if (ActionInfo.size() < 2) |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3519 | return nullptr; |
| 3520 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[0].JA); |
| 3521 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[1].JA); |
| 3522 | if (!BJ || !CJ) |
| 3523 | return nullptr; |
| 3524 | |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3525 | // Check if the initial input (to the compile job or its predessor if one |
| 3526 | // exists) is LLVM bitcode. In that case, no preprocessor step is required |
| 3527 | // and we can still collapse the compile and backend jobs when we have |
| 3528 | // -save-temps. I.e. there is no need for a separate compile job just to |
| 3529 | // emit unoptimized bitcode. |
| 3530 | bool InputIsBitcode = true; |
| 3531 | for (size_t i = 1; i < ActionInfo.size(); i++) |
| 3532 | if (ActionInfo[i].JA->getType() != types::TY_LLVM_BC && |
| 3533 | ActionInfo[i].JA->getType() != types::TY_LTO_BC) { |
| 3534 | InputIsBitcode = false; |
| 3535 | break; |
| 3536 | } |
| 3537 | if (!InputIsBitcode && !canCollapsePreprocessorAction()) |
| 3538 | return nullptr; |
| 3539 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3540 | // Get compiler tool. |
| 3541 | const Tool *T = TC.SelectTool(*CJ); |
| 3542 | if (!T) |
| 3543 | return nullptr; |
| 3544 | |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3545 | if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode)) |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3546 | return nullptr; |
| 3547 | |
| 3548 | Inputs = &CJ->getInputs(); |
| 3549 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3550 | /*NumElements=*/2); |
| 3551 | return T; |
| 3552 | } |
| 3553 | |
| 3554 | /// Updates the inputs if the obtained tool supports combining with |
| 3555 | /// preprocessor action, and the current input is indeed a preprocessor |
| 3556 | /// action. If combining results in the collapse of offloading actions, those |
| 3557 | /// are appended to \a CollapsedOffloadAction. |
| 3558 | void combineWithPreprocessor(const Tool *T, const ActionList *&Inputs, |
| 3559 | ActionList &CollapsedOffloadAction) { |
| 3560 | if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP()) |
| 3561 | return; |
| 3562 | |
| 3563 | // Attempt to get a preprocessor action dependence. |
| 3564 | ActionList PreprocessJobOffloadActions; |
| 3565 | auto *PJ = getPrevDependentAction(*Inputs, PreprocessJobOffloadActions); |
| 3566 | if (!PJ || !isa<PreprocessJobAction>(PJ)) |
| 3567 | return; |
| 3568 | |
| 3569 | // This is legal to combine. Append any offload action we found and set the |
| 3570 | // current inputs to preprocessor inputs. |
| 3571 | CollapsedOffloadAction.append(PreprocessJobOffloadActions.begin(), |
| 3572 | PreprocessJobOffloadActions.end()); |
| 3573 | Inputs = &PJ->getInputs(); |
| 3574 | } |
| 3575 | |
| 3576 | public: |
| 3577 | ToolSelector(const JobAction *BaseAction, const ToolChain &TC, |
| 3578 | const Compilation &C, bool SaveTemps, bool EmbedBitcode) |
| 3579 | : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps), |
| 3580 | EmbedBitcode(EmbedBitcode) { |
| 3581 | assert(BaseAction && "Invalid base action."); |
| 3582 | IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None; |
| 3583 | } |
| 3584 | |
| 3585 | /// Check if a chain of actions can be combined and return the tool that can |
| 3586 | /// handle the combination of actions. The pointer to the current inputs \a |
| 3587 | /// Inputs and the list of offload actions \a CollapsedOffloadActions |
| 3588 | /// connected to collapsed actions are updated accordingly. The latter enables |
| 3589 | /// the caller of the selector to process them afterwards instead of just |
| 3590 | /// dropping them. If no suitable tool is found, null will be returned. |
| 3591 | const Tool *getTool(const ActionList *&Inputs, |
| 3592 | ActionList &CollapsedOffloadAction) { |
| 3593 | // |
| 3594 | // Get the largest chain of actions that we could combine. |
| 3595 | // |
| 3596 | |
| 3597 | SmallVector<JobActionInfo, 5> ActionChain(1); |
| 3598 | ActionChain.back().JA = BaseAction; |
| 3599 | while (ActionChain.back().JA) { |
| 3600 | const Action *CurAction = ActionChain.back().JA; |
| 3601 | |
| 3602 | // Grow the chain by one element. |
| 3603 | ActionChain.resize(ActionChain.size() + 1); |
| 3604 | JobActionInfo &AI = ActionChain.back(); |
| 3605 | |
| 3606 | // Attempt to fill it with the |
| 3607 | AI.JA = |
| 3608 | getPrevDependentAction(CurAction->getInputs(), AI.SavedOffloadAction); |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3609 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3610 | |
| 3611 | // Pop the last action info as it could not be filled. |
| 3612 | ActionChain.pop_back(); |
| 3613 | |
| 3614 | // |
| 3615 | // Attempt to combine actions. If all combining attempts failed, just return |
| 3616 | // the tool of the provided action. At the end we attempt to combine the |
| 3617 | // action with any preprocessor action it may depend on. |
| 3618 | // |
| 3619 | |
| 3620 | const Tool *T = combineAssembleBackendCompile(ActionChain, Inputs, |
| 3621 | CollapsedOffloadAction); |
| 3622 | if (!T) |
| 3623 | T = combineAssembleBackend(ActionChain, Inputs, CollapsedOffloadAction); |
| 3624 | if (!T) |
| 3625 | T = combineBackendCompile(ActionChain, Inputs, CollapsedOffloadAction); |
| 3626 | if (!T) { |
| 3627 | Inputs = &BaseAction->getInputs(); |
| 3628 | T = TC.SelectTool(*BaseAction); |
| 3629 | } |
| 3630 | |
| 3631 | combineWithPreprocessor(T, Inputs, CollapsedOffloadAction); |
| 3632 | return T; |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3633 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3634 | }; |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3637 | /// Return a string that uniquely identifies the result of a job. The bound arch |
| 3638 | /// is not necessarily represented in the toolchain's triple -- for example, |
| 3639 | /// armv7 and armv7s both map to the same triple -- so we need both in our map. |
| 3640 | /// Also, we need to add the offloading device kind, as the same tool chain can |
| 3641 | /// be used for host and device for some programming models, e.g. OpenMP. |
| 3642 | static std::string GetTriplePlusArchString(const ToolChain *TC, |
| 3643 | StringRef BoundArch, |
| 3644 | Action::OffloadKind OffloadKind) { |
Justin Lebar | 55c8332 | 2016-01-16 03:30:08 +0000 | [diff] [blame] | 3645 | std::string TriplePlusArch = TC->getTriple().normalize(); |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3646 | if (!BoundArch.empty()) { |
Justin Lebar | 55c8332 | 2016-01-16 03:30:08 +0000 | [diff] [blame] | 3647 | TriplePlusArch += "-"; |
| 3648 | TriplePlusArch += BoundArch; |
| 3649 | } |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 3650 | TriplePlusArch += "-"; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3651 | TriplePlusArch += Action::GetOffloadKindName(OffloadKind); |
| 3652 | return TriplePlusArch; |
| 3653 | } |
| 3654 | |
| 3655 | InputInfo Driver::BuildJobsForAction( |
| 3656 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
| 3657 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
| 3658 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
| 3659 | Action::OffloadKind TargetDeviceOffloadKind) const { |
| 3660 | std::pair<const Action *, std::string> ActionTC = { |
| 3661 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3662 | auto CachedResult = CachedResults.find(ActionTC); |
| 3663 | if (CachedResult != CachedResults.end()) { |
| 3664 | return CachedResult->second; |
| 3665 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3666 | InputInfo Result = BuildJobsForActionNoCache( |
| 3667 | C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3668 | CachedResults, TargetDeviceOffloadKind); |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3669 | CachedResults[ActionTC] = Result; |
| 3670 | return Result; |
| 3671 | } |
| 3672 | |
| 3673 | InputInfo Driver::BuildJobsForActionNoCache( |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3674 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3675 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3676 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3677 | Action::OffloadKind TargetDeviceOffloadKind) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3678 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | c4acf9d | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 3679 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3680 | InputInfoList OffloadDependencesInputInfo; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3681 | bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3682 | if (const OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
Alex Lorenz | 4003a98 | 2017-08-08 11:22:21 +0000 | [diff] [blame] | 3683 | // The 'Darwin' toolchain is initialized only when its arguments are |
| 3684 | // computed. Get the default arguments for OFK_None to ensure that |
| 3685 | // initialization is performed before processing the offload action. |
| 3686 | // FIXME: Remove when darwin's toolchain is initialized during construction. |
| 3687 | C.getArgsForToolChain(TC, BoundArch, Action::OFK_None); |
| 3688 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3689 | // The offload action is expected to be used in four different situations. |
| 3690 | // |
| 3691 | // a) Set a toolchain/architecture/kind for a host action: |
| 3692 | // Host Action 1 -> OffloadAction -> Host Action 2 |
| 3693 | // |
| 3694 | // b) Set a toolchain/architecture/kind for a device action; |
| 3695 | // Device Action 1 -> OffloadAction -> Device Action 2 |
| 3696 | // |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3697 | // c) Specify a device dependence to a host action; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3698 | // Device Action 1 _ |
| 3699 | // \ |
| 3700 | // Host Action 1 ---> OffloadAction -> Host Action 2 |
| 3701 | // |
| 3702 | // d) Specify a host dependence to a device action. |
| 3703 | // Host Action 1 _ |
| 3704 | // \ |
| 3705 | // Device Action 1 ---> OffloadAction -> Device Action 2 |
| 3706 | // |
| 3707 | // For a) and b), we just return the job generated for the dependence. For |
| 3708 | // c) and d) we override the current action with the host/device dependence |
| 3709 | // if the current toolchain is host/device and set the offload dependences |
| 3710 | // info with the jobs obtained from the device/host dependence(s). |
| 3711 | |
| 3712 | // If there is a single device option, just generate the job for it. |
| 3713 | if (OA->hasSingleDeviceDependence()) { |
| 3714 | InputInfo DevA; |
| 3715 | OA->doOnEachDeviceDependence([&](Action *DepA, const ToolChain *DepTC, |
| 3716 | const char *DepBoundArch) { |
| 3717 | DevA = |
| 3718 | BuildJobsForAction(C, DepA, DepTC, DepBoundArch, AtTopLevel, |
| 3719 | /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3720 | CachedResults, DepA->getOffloadingDeviceKind()); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3721 | }); |
| 3722 | return DevA; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3723 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3724 | |
| 3725 | // If 'Action 2' is host, we generate jobs for the device dependences and |
| 3726 | // override the current action with the host dependence. Otherwise, we |
| 3727 | // generate the host dependences and override the action with the device |
| 3728 | // dependence. The dependences can't therefore be a top-level action. |
| 3729 | OA->doOnEachDependence( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3730 | /*IsHostDependence=*/BuildingForOffloadDevice, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3731 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 3732 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
| 3733 | C, DepA, DepTC, DepBoundArch, /*AtTopLevel=*/false, |
| 3734 | /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3735 | DepA->getOffloadingDeviceKind())); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3736 | }); |
| 3737 | |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3738 | A = BuildingForOffloadDevice |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3739 | ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true) |
| 3740 | : OA->getHostDependence(); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3743 | if (const InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3744 | // FIXME: It would be nice to not claim this here; maybe the old scheme of |
| 3745 | // just using Args was better? |
Daniel Dunbar | 5cdf3e0 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 3746 | const Arg &Input = IA->getInputArg(); |
| 3747 | Input.claim(); |
Daniel Dunbar | 35cbfeb | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 3748 | if (Input.getOption().matches(options::OPT_INPUT)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 3749 | const char *Name = Input.getValue(); |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 3750 | return InputInfo(A, Name, /* BaseInput = */ Name); |
Douglas Katzman | 678d0cb | 2015-06-16 18:01:24 +0000 | [diff] [blame] | 3751 | } |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 3752 | return InputInfo(A, &Input, /* BaseInput = */ ""); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { |
Chad Rosier | a78a6eb | 2012-04-27 16:50:38 +0000 | [diff] [blame] | 3756 | const ToolChain *TC; |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3757 | StringRef ArchName = BAA->getArchName(); |
Daniel Dunbar | 1ef3f2a | 2009-09-08 23:37:19 +0000 | [diff] [blame] | 3758 | |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3759 | if (!ArchName.empty()) |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 3760 | TC = &getToolChain(C.getArgs(), |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 3761 | computeTargetTriple(*this, TargetTriple, |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 3762 | C.getArgs(), ArchName)); |
Chad Rosier | a78a6eb | 2012-04-27 16:50:38 +0000 | [diff] [blame] | 3763 | else |
| 3764 | TC = &C.getDefaultToolChain(); |
Daniel Dunbar | 1ef3f2a | 2009-09-08 23:37:19 +0000 | [diff] [blame] | 3765 | |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 3766 | return BuildJobsForAction(C, *BAA->input_begin(), TC, ArchName, AtTopLevel, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3767 | MultipleArchs, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3768 | TargetDeviceOffloadKind); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3771 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3772 | const ActionList *Inputs = &A->getInputs(); |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3773 | |
| 3774 | const JobAction *JA = cast<JobAction>(A); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3775 | ActionList CollapsedOffloadActions; |
| 3776 | |
Mehdi Amini | 6683e22 | 2017-01-24 18:12:25 +0000 | [diff] [blame] | 3777 | ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), |
| 3778 | embedBitcodeInObject() && !isUsingLTO()); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3779 | const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions); |
| 3780 | |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 3781 | if (!T) |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 3782 | return InputInfo(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3783 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3784 | // If we've collapsed action list that contained OffloadAction we |
| 3785 | // need to build jobs for host/device-side inputs it may have held. |
| 3786 | for (const auto *OA : CollapsedOffloadActions) |
| 3787 | cast<OffloadAction>(OA)->doOnEachDependence( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3788 | /*IsHostDependence=*/BuildingForOffloadDevice, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3789 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 3790 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
Artem Belevich | bee2f41 | 2016-08-22 18:50:34 +0000 | [diff] [blame] | 3791 | C, DepA, DepTC, DepBoundArch, /* AtTopLevel */ false, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3792 | /*MultipleArchs=*/!!DepBoundArch, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3793 | DepA->getOffloadingDeviceKind())); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3794 | }); |
Artem Belevich | f8144ab | 2015-08-27 18:10:41 +0000 | [diff] [blame] | 3795 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3796 | // Only use pipes when there is exactly one input. |
Daniel Dunbar | 1a093d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 3797 | InputInfoList InputInfos; |
Saleem Abdulrasool | da3f4e5 | 2014-12-29 21:02:47 +0000 | [diff] [blame] | 3798 | for (const Action *Input : *Inputs) { |
Eric Christopher | 14668dd | 2013-02-18 00:38:25 +0000 | [diff] [blame] | 3799 | // Treat dsymutil and verify sub-jobs as being at the top-level too, they |
| 3800 | // shouldn't get temporary output names. |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 3801 | // FIXME: Clean this up. |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 3802 | bool SubJobAtTopLevel = |
| 3803 | AtTopLevel && (isa<DsymutilJobAction>(A) || isa<VerifyJobAction>(A)); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3804 | InputInfos.push_back(BuildJobsForAction( |
| 3805 | C, Input, TC, BoundArch, SubJobAtTopLevel, MultipleArchs, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3806 | CachedResults, A->getOffloadingDeviceKind())); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3809 | // Always use the first input as the base input. |
| 3810 | const char *BaseInput = InputInfos[0].getBaseInput(); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 3811 | |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 3812 | // ... except dsymutil actions, which use their actual input as the base |
| 3813 | // input. |
| 3814 | if (JA->getType() == types::TY_dSYM) |
| 3815 | BaseInput = InputInfos[0].getFilename(); |
| 3816 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3817 | // Append outputs of offload device jobs to the input list |
| 3818 | if (!OffloadDependencesInputInfo.empty()) |
| 3819 | InputInfos.append(OffloadDependencesInputInfo.begin(), |
| 3820 | OffloadDependencesInputInfo.end()); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3821 | |
Vedant Kumar | 18286cf | 2016-07-27 23:02:20 +0000 | [diff] [blame] | 3822 | // Set the effective triple of the toolchain for the duration of this job. |
| 3823 | llvm::Triple EffectiveTriple; |
| 3824 | const ToolChain &ToolTC = T->getToolChain(); |
Samuel Antao | 31fef98 | 2016-10-27 17:39:44 +0000 | [diff] [blame] | 3825 | const ArgList &Args = |
| 3826 | C.getArgsForToolChain(TC, BoundArch, A->getOffloadingDeviceKind()); |
Vedant Kumar | 18286cf | 2016-07-27 23:02:20 +0000 | [diff] [blame] | 3827 | if (InputInfos.size() != 1) { |
| 3828 | EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args)); |
| 3829 | } else { |
| 3830 | // Pass along the input type if it can be unambiguously determined. |
| 3831 | EffectiveTriple = llvm::Triple( |
| 3832 | ToolTC.ComputeEffectiveClangTriple(Args, InputInfos[0].getType())); |
| 3833 | } |
| 3834 | RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple); |
| 3835 | |
Daniel Dunbar | d00272f | 2010-08-02 02:38:15 +0000 | [diff] [blame] | 3836 | // Determine the place to write output to, if any. |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 3837 | InputInfo Result; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3838 | InputInfoList UnbundlingResults; |
| 3839 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(JA)) { |
| 3840 | // If we have an unbundling job, we need to create results for all the |
| 3841 | // outputs. We also update the results cache so that other actions using |
| 3842 | // this unbundling action can get the right results. |
| 3843 | for (auto &UI : UA->getDependentActionsInfo()) { |
| 3844 | assert(UI.DependentOffloadKind != Action::OFK_None && |
| 3845 | "Unbundling with no offloading??"); |
| 3846 | |
| 3847 | // Unbundling actions are never at the top level. When we generate the |
| 3848 | // offloading prefix, we also do that for the host file because the |
| 3849 | // unbundling action does not change the type of the output which can |
| 3850 | // cause a overwrite. |
| 3851 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 3852 | UI.DependentOffloadKind, |
| 3853 | UI.DependentToolChain->getTriple().normalize(), |
| 3854 | /*CreatePrefixForHost=*/true); |
| 3855 | auto CurI = InputInfo( |
Yaxun Liu | 609f752 | 2018-05-11 19:02:18 +0000 | [diff] [blame] | 3856 | UA, |
| 3857 | GetNamedOutputPath(C, *UA, BaseInput, UI.DependentBoundArch, |
| 3858 | /*AtTopLevel=*/false, |
| 3859 | MultipleArchs || |
| 3860 | UI.DependentOffloadKind == Action::OFK_HIP, |
| 3861 | OffloadingPrefix), |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3862 | BaseInput); |
| 3863 | // Save the unbundling result. |
| 3864 | UnbundlingResults.push_back(CurI); |
| 3865 | |
| 3866 | // Get the unique string identifier for this dependence and cache the |
| 3867 | // result. |
Yaxun Liu | 470b833 | 2018-06-06 19:44:10 +0000 | [diff] [blame] | 3868 | StringRef Arch; |
| 3869 | if (TargetDeviceOffloadKind == Action::OFK_HIP) { |
| 3870 | if (UI.DependentOffloadKind == Action::OFK_Host) |
Yaxun Liu | f37b50a | 2018-07-24 01:03:44 +0000 | [diff] [blame] | 3871 | Arch = StringRef(); |
Yaxun Liu | 470b833 | 2018-06-06 19:44:10 +0000 | [diff] [blame] | 3872 | else |
| 3873 | Arch = UI.DependentBoundArch; |
| 3874 | } else |
| 3875 | Arch = BoundArch; |
| 3876 | |
| 3877 | CachedResults[{A, GetTriplePlusArchString(UI.DependentToolChain, Arch, |
| 3878 | UI.DependentOffloadKind)}] = |
| 3879 | CurI; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | // Now that we have all the results generated, select the one that should be |
| 3883 | // returned for the current depending action. |
| 3884 | std::pair<const Action *, std::string> ActionTC = { |
| 3885 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
| 3886 | assert(CachedResults.find(ActionTC) != CachedResults.end() && |
| 3887 | "Result does not exist??"); |
| 3888 | Result = CachedResults[ActionTC]; |
| 3889 | } else if (JA->getType() == types::TY_Nothing) |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 3890 | Result = InputInfo(A, BaseInput); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3891 | else { |
| 3892 | // We only have to generate a prefix for the host if this is not a top-level |
| 3893 | // action. |
| 3894 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 3895 | A->getOffloadingDeviceKind(), TC->getTriple().normalize(), |
| 3896 | /*CreatePrefixForHost=*/!!A->getOffloadingHostActiveKinds() && |
| 3897 | !AtTopLevel); |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 3898 | Result = InputInfo(A, GetNamedOutputPath(C, *JA, BaseInput, BoundArch, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3899 | AtTopLevel, MultipleArchs, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3900 | OffloadingPrefix), |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 3901 | BaseInput); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3902 | } |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 3903 | |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 3904 | if (CCCPrintBindings && !CCGenDiagnostics) { |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 3905 | llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"' |
| 3906 | << " - \"" << T->getName() << "\", inputs: ["; |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 3907 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
| 3908 | llvm::errs() << InputInfos[i].getAsString(); |
| 3909 | if (i + 1 != e) |
| 3910 | llvm::errs() << ", "; |
| 3911 | } |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3912 | if (UnbundlingResults.empty()) |
| 3913 | llvm::errs() << "], output: " << Result.getAsString() << "\n"; |
| 3914 | else { |
| 3915 | llvm::errs() << "], outputs: ["; |
| 3916 | for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) { |
| 3917 | llvm::errs() << UnbundlingResults[i].getAsString(); |
| 3918 | if (i + 1 != e) |
| 3919 | llvm::errs() << ", "; |
| 3920 | } |
| 3921 | llvm::errs() << "] \n"; |
| 3922 | } |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 3923 | } else { |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3924 | if (UnbundlingResults.empty()) |
| 3925 | T->ConstructJob( |
| 3926 | C, *JA, Result, InputInfos, |
| 3927 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 3928 | LinkingOutput); |
| 3929 | else |
Samuel Antao | 7108bf3 | 2016-11-03 15:41:50 +0000 | [diff] [blame] | 3930 | T->ConstructJobMultipleOutputs( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3931 | C, *JA, UnbundlingResults, InputInfos, |
| 3932 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 3933 | LinkingOutput); |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 3934 | } |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 3935 | return Result; |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 3938 | const char *Driver::getDefaultImageName() const { |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 3939 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 3940 | return Target.isOSWindows() ? "a.exe" : "a.out"; |
| 3941 | } |
| 3942 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3943 | /// Create output filename based on ArgValue, which could either be a |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 3944 | /// full filename, filename without extension, or a directory. If ArgValue |
| 3945 | /// does not provide a filename, then use BaseName, and use the extension |
| 3946 | /// suitable for FileType. |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 3947 | static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3948 | StringRef BaseName, |
| 3949 | types::ID FileType) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 3950 | SmallString<128> Filename = ArgValue; |
Justin Bogner | 5aaf2e7 | 2014-06-26 20:59:36 +0000 | [diff] [blame] | 3951 | |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 3952 | if (ArgValue.empty()) { |
| 3953 | // If the argument is empty, output to BaseName in the current dir. |
| 3954 | Filename = BaseName; |
| 3955 | } else if (llvm::sys::path::is_separator(Filename.back())) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 3956 | // If the argument is a directory, output to BaseName in that dir. |
| 3957 | llvm::sys::path::append(Filename, BaseName); |
| 3958 | } |
| 3959 | |
| 3960 | if (!llvm::sys::path::has_extension(ArgValue)) { |
| 3961 | // If the argument didn't provide an extension, then set it. |
| 3962 | const char *Extension = types::getTypeTempSuffix(FileType, true); |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 3963 | |
| 3964 | if (FileType == types::TY_Image && |
| 3965 | Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) { |
| 3966 | // The output file is a dll. |
| 3967 | Extension = "dll"; |
| 3968 | } |
| 3969 | |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 3970 | llvm::sys::path::replace_extension(Filename, Extension); |
| 3971 | } |
| 3972 | |
| 3973 | return Args.MakeArgString(Filename.c_str()); |
| 3974 | } |
| 3975 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3976 | const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 3977 | const char *BaseInput, |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3978 | StringRef BoundArch, bool AtTopLevel, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3979 | bool MultipleArchs, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3980 | StringRef OffloadingPrefix) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 3981 | llvm::PrettyStackTraceString CrashInfo("Computing output path"); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 3982 | // Output to a user requested destination? |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3983 | if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) { |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 3984 | if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 3985 | return C.addResultFile(FinalOutput->getValue(), &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 3988 | // For /P, preprocess to file named after BaseInput. |
| 3989 | if (C.getArgs().hasArg(options::OPT__SLASH_P)) { |
| 3990 | assert(AtTopLevel && isa<PreprocessJobAction>(JA)); |
| 3991 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
Hans Wennborg | 04c764f | 2014-06-17 00:19:12 +0000 | [diff] [blame] | 3992 | StringRef NameArg; |
Greg Bedwell | 065f70a | 2015-06-09 10:24:06 +0000 | [diff] [blame] | 3993 | if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi)) |
Hans Wennborg | 04c764f | 2014-06-17 00:19:12 +0000 | [diff] [blame] | 3994 | NameArg = A->getValue(); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3995 | return C.addResultFile( |
| 3996 | MakeCLOutputFilename(C.getArgs(), NameArg, BaseName, types::TY_PP_C), |
| 3997 | &JA); |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Nick Lewycky | 6e1ce29 | 2010-09-24 00:46:53 +0000 | [diff] [blame] | 4000 | // Default to writing to stdout? |
David Blaikie | bc023c9 | 2018-06-14 23:09:06 +0000 | [diff] [blame] | 4001 | if (AtTopLevel && !CCGenDiagnostics && isa<PreprocessJobAction>(JA)) |
Nick Lewycky | 6e1ce29 | 2010-09-24 00:46:53 +0000 | [diff] [blame] | 4002 | return "-"; |
| 4003 | |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4004 | // Is this the assembly listing for /FA? |
| 4005 | if (JA.getType() == types::TY_PP_Asm && |
| 4006 | (C.getArgs().hasArg(options::OPT__SLASH_FA) || |
| 4007 | C.getArgs().hasArg(options::OPT__SLASH_Fa))) { |
| 4008 | // Use /Fa and the input filename to determine the asm file name. |
| 4009 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
| 4010 | StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4011 | return C.addResultFile( |
| 4012 | MakeCLOutputFilename(C.getArgs(), FaValue, BaseName, JA.getType()), |
| 4013 | &JA); |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4016 | // Output to a temporary file? |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4017 | if ((!AtTopLevel && !isSaveTempsEnabled() && |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4018 | !C.getArgs().hasArg(options::OPT__SLASH_Fo)) || |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 4019 | CCGenDiagnostics) { |
Chad Rosier | 97c3737 | 2011-08-26 22:27:02 +0000 | [diff] [blame] | 4020 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4021 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 4022 | SmallString<128> TmpName; |
| 4023 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
| 4024 | Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir); |
| 4025 | if (CCGenDiagnostics && A) { |
| 4026 | SmallString<128> CrashDirectory(A->getValue()); |
| 4027 | llvm::sys::path::append(CrashDirectory, Split.first); |
| 4028 | const char *Middle = Suffix ? "-%%%%%%." : "-%%%%%%"; |
| 4029 | std::error_code EC = |
| 4030 | llvm::sys::fs::createUniqueFile(CrashDirectory + Middle + Suffix, TmpName); |
| 4031 | if (EC) { |
| 4032 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4033 | return ""; |
| 4034 | } |
| 4035 | } else { |
| 4036 | TmpName = GetTemporaryPath(Split.first, Suffix); |
| 4037 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4038 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4041 | SmallString<128> BasePath(BaseInput); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4042 | StringRef BaseName; |
Daniel Dunbar | 67fea71 | 2011-03-25 18:16:51 +0000 | [diff] [blame] | 4043 | |
| 4044 | // Dsymutil actions should use the full path. |
Eric Christopher | 551ef45 | 2011-08-23 17:56:55 +0000 | [diff] [blame] | 4045 | if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA)) |
Daniel Dunbar | 67fea71 | 2011-03-25 18:16:51 +0000 | [diff] [blame] | 4046 | BaseName = BasePath; |
| 4047 | else |
| 4048 | BaseName = llvm::sys::path::filename(BasePath); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4049 | |
| 4050 | // Determine what the derived output name should be. |
| 4051 | const char *NamedOutput; |
Hans Wennborg | 2b89a26 | 2013-08-06 22:11:28 +0000 | [diff] [blame] | 4052 | |
Hans Wennborg | 625fba8 | 2016-10-04 21:01:04 +0000 | [diff] [blame] | 4053 | if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) && |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 4054 | C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) { |
| 4055 | // The /Fo or /o flag decides the object filename. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4056 | StringRef Val = |
| 4057 | C.getArgs() |
| 4058 | .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o) |
| 4059 | ->getValue(); |
| 4060 | NamedOutput = |
| 4061 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object); |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4062 | } else if (JA.getType() == types::TY_Image && |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4063 | C.getArgs().hasArg(options::OPT__SLASH_Fe, |
| 4064 | options::OPT__SLASH_o)) { |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 4065 | // The /Fe or /o flag names the linked file. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4066 | StringRef Val = |
| 4067 | C.getArgs() |
| 4068 | .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o) |
| 4069 | ->getValue(); |
| 4070 | NamedOutput = |
| 4071 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Image); |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 4072 | } else if (JA.getType() == types::TY_Image) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4073 | if (IsCLMode()) { |
| 4074 | // clang-cl uses BaseName for the executable name. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4075 | NamedOutput = |
| 4076 | MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image); |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 4077 | } else { |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 4078 | SmallString<128> Output(getDefaultImageName()); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4079 | Output += OffloadingPrefix; |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 4080 | if (MultipleArchs && !BoundArch.empty()) { |
| 4081 | Output += "-"; |
| 4082 | Output.append(BoundArch); |
| 4083 | } |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4084 | NamedOutput = C.getArgs().MakeArgString(Output.c_str()); |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4085 | } |
| 4086 | } else if (JA.getType() == types::TY_PCH && IsCLMode()) { |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4087 | NamedOutput = C.getArgs().MakeArgString(GetClPchPath(C, BaseName)); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4088 | } else { |
Hans Wennborg | 0a096a0 | 2013-09-05 17:05:56 +0000 | [diff] [blame] | 4089 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4090 | assert(Suffix && "All types used for output should have a suffix."); |
| 4091 | |
| 4092 | std::string::size_type End = std::string::npos; |
| 4093 | if (!types::appendSuffixForType(JA.getType())) |
| 4094 | End = BaseName.rfind('.'); |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4095 | SmallString<128> Suffixed(BaseName.substr(0, End)); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4096 | Suffixed += OffloadingPrefix; |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 4097 | if (MultipleArchs && !BoundArch.empty()) { |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4098 | Suffixed += "-"; |
| 4099 | Suffixed.append(BoundArch); |
| 4100 | } |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 4101 | // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for |
| 4102 | // the unoptimized bitcode so that it does not get overwritten by the ".bc" |
| 4103 | // optimized bitcode output. |
| 4104 | if (!AtTopLevel && C.getArgs().hasArg(options::OPT_emit_llvm) && |
| 4105 | JA.getType() == types::TY_LLVM_BC) |
| 4106 | Suffixed += ".tmp"; |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4107 | Suffixed += '.'; |
| 4108 | Suffixed += Suffix; |
| 4109 | NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); |
| 4110 | } |
| 4111 | |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4112 | // Prepend object file path if -save-temps=obj |
| 4113 | if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) && |
| 4114 | JA.getType() != types::TY_PCH) { |
| 4115 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 4116 | SmallString<128> TempPath(FinalOutput->getValue()); |
| 4117 | llvm::sys::path::remove_filename(TempPath); |
| 4118 | StringRef OutputFileName = llvm::sys::path::filename(NamedOutput); |
| 4119 | llvm::sys::path::append(TempPath, OutputFileName); |
| 4120 | NamedOutput = C.getArgs().MakeArgString(TempPath.c_str()); |
| 4121 | } |
| 4122 | |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 4123 | // If we're saving temps and the temp file conflicts with the input file, |
Chad Rosier | 5b58af0 | 2012-04-20 20:05:08 +0000 | [diff] [blame] | 4124 | // then avoid overwriting input file. |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4125 | if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) { |
Chad Rosier | 5b58af0 | 2012-04-20 20:05:08 +0000 | [diff] [blame] | 4126 | bool SameFile = false; |
| 4127 | SmallString<256> Result; |
| 4128 | llvm::sys::fs::current_path(Result); |
| 4129 | llvm::sys::path::append(Result, BaseName); |
| 4130 | llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile); |
| 4131 | // Must share the same path to conflict. |
| 4132 | if (SameFile) { |
| 4133 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4134 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4135 | std::string TmpName = GetTemporaryPath( |
| 4136 | Split.first, types::getTypeTempSuffix(JA.getType(), IsCLMode())); |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4137 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
Chad Rosier | 5b58af0 | 2012-04-20 20:05:08 +0000 | [diff] [blame] | 4138 | } |
Chad Rosier | f8412cd | 2011-07-15 21:54:29 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4141 | // As an annoying special case, PCH generation doesn't strip the pathname. |
Nico Weber | 8ab9219 | 2016-03-02 23:29:29 +0000 | [diff] [blame] | 4142 | if (JA.getType() == types::TY_PCH && !IsCLMode()) { |
Michael J. Spencer | e169675 | 2010-12-18 00:19:12 +0000 | [diff] [blame] | 4143 | llvm::sys::path::remove_filename(BasePath); |
| 4144 | if (BasePath.empty()) |
Daniel Dunbar | e6c8319 | 2009-03-18 09:58:30 +0000 | [diff] [blame] | 4145 | BasePath = NamedOutput; |
| 4146 | else |
Michael J. Spencer | e169675 | 2010-12-18 00:19:12 +0000 | [diff] [blame] | 4147 | llvm::sys::path::append(BasePath, NamedOutput); |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4148 | return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4149 | } else { |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4150 | return C.addResultFile(NamedOutput, &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4151 | } |
| 4152 | } |
| 4153 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4154 | std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const { |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame^] | 4155 | // Seach for Name in a list of paths. |
| 4156 | auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P) |
| 4157 | -> llvm::Optional<std::string> { |
| 4158 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
| 4159 | // attempting to use this prefix when looking for file paths. |
| 4160 | for (const auto &Dir : P) { |
| 4161 | if (Dir.empty()) |
| 4162 | continue; |
| 4163 | SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); |
| 4164 | llvm::sys::path::append(P, Name); |
| 4165 | if (llvm::sys::fs::exists(Twine(P))) |
| 4166 | return {P.str()}; |
| 4167 | } |
| 4168 | return None; |
| 4169 | }; |
| 4170 | |
| 4171 | if (auto P = SearchPaths(PrefixDirs)) |
| 4172 | return *P; |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4173 | |
Petr Hosek | 916a467 | 2017-08-10 04:16:38 +0000 | [diff] [blame] | 4174 | SmallString<128> R(ResourceDir); |
| 4175 | llvm::sys::path::append(R, Name); |
| 4176 | if (llvm::sys::fs::exists(Twine(R))) |
| 4177 | return R.str(); |
| 4178 | |
| 4179 | SmallString<128> P(TC.getCompilerRTPath()); |
Rafael Espindola | 609a664 | 2013-06-24 18:33:43 +0000 | [diff] [blame] | 4180 | llvm::sys::path::append(P, Name); |
| 4181 | if (llvm::sys::fs::exists(Twine(P))) |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 4182 | return P.str(); |
| 4183 | |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame^] | 4184 | if (auto P = SearchPaths(TC.getLibraryPaths())) |
| 4185 | return *P; |
| 4186 | |
| 4187 | if (auto P = SearchPaths(TC.getFilePaths())) |
| 4188 | return *P; |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4189 | |
Daniel Dunbar | 1ce8153 | 2009-09-09 22:33:00 +0000 | [diff] [blame] | 4190 | return Name; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4193 | void Driver::generatePrefixedToolNames( |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4194 | StringRef Tool, const ToolChain &TC, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4195 | SmallVectorImpl<std::string> &Names) const { |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4196 | // FIXME: Needs a better variable than TargetTriple |
| 4197 | Names.emplace_back((TargetTriple + "-" + Tool).str()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 4198 | Names.emplace_back(Tool); |
Vasileios Kalintiris | c744e12 | 2015-11-12 15:26:54 +0000 | [diff] [blame] | 4199 | |
| 4200 | // Allow the discovery of tools prefixed with LLVM's default target triple. |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4201 | std::string DefaultTargetTriple = llvm::sys::getDefaultTargetTriple(); |
| 4202 | if (DefaultTargetTriple != TargetTriple) |
| 4203 | Names.emplace_back((DefaultTargetTriple + "-" + Tool).str()); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
Benjamin Kramer | 7111b91 | 2014-11-04 20:26:01 +0000 | [diff] [blame] | 4206 | static bool ScanDirForExecutable(SmallString<128> &Dir, |
| 4207 | ArrayRef<std::string> Names) { |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4208 | for (const auto &Name : Names) { |
| 4209 | llvm::sys::path::append(Dir, Name); |
| 4210 | if (llvm::sys::fs::can_execute(Twine(Dir))) |
| 4211 | return true; |
| 4212 | llvm::sys::path::remove_filename(Dir); |
| 4213 | } |
| 4214 | return false; |
| 4215 | } |
| 4216 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4217 | std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const { |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4218 | SmallVector<std::string, 2> TargetSpecificExecutables; |
| 4219 | generatePrefixedToolNames(Name, TC, TargetSpecificExecutables); |
| 4220 | |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4221 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
Logan Chien | cd679fd | 2012-10-04 08:08:56 +0000 | [diff] [blame] | 4222 | // attempting to use this prefix when looking for program paths. |
Saleem Abdulrasool | 23d99b1 | 2014-09-16 03:48:32 +0000 | [diff] [blame] | 4223 | for (const auto &PrefixDir : PrefixDirs) { |
| 4224 | if (llvm::sys::fs::is_directory(PrefixDir)) { |
| 4225 | SmallString<128> P(PrefixDir); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4226 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4227 | return P.str(); |
Simon Atanasyan | a47ba29 | 2012-10-31 14:39:28 +0000 | [diff] [blame] | 4228 | } else { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4229 | SmallString<128> P((PrefixDir + Name).str()); |
Rafael Espindola | 609a664 | 2013-06-24 18:33:43 +0000 | [diff] [blame] | 4230 | if (llvm::sys::fs::can_execute(Twine(P))) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4231 | return P.str(); |
Simon Atanasyan | 86bdab7 | 2012-10-31 12:01:53 +0000 | [diff] [blame] | 4232 | } |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4235 | const ToolChain::path_list &List = TC.getProgramPaths(); |
Saleem Abdulrasool | 23d99b1 | 2014-09-16 03:48:32 +0000 | [diff] [blame] | 4236 | for (const auto &Path : List) { |
| 4237 | SmallString<128> P(Path); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4238 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4239 | return P.str(); |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Daniel Dunbar | 76ce741 | 2009-03-23 16:15:50 +0000 | [diff] [blame] | 4242 | // If all else failed, search the path. |
Michael J. Spencer | b011d48 | 2014-11-07 21:30:32 +0000 | [diff] [blame] | 4243 | for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) |
| 4244 | if (llvm::ErrorOr<std::string> P = |
| 4245 | llvm::sys::findProgramByName(TargetSpecificExecutable)) |
Michael J. Spencer | 04162ea | 2014-11-04 01:30:55 +0000 | [diff] [blame] | 4246 | return *P; |
Daniel Dunbar | 6f66877 | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 4247 | |
Daniel Dunbar | 1ce8153 | 2009-09-09 22:33:00 +0000 | [diff] [blame] | 4248 | return Name; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4251 | std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const { |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4252 | SmallString<128> Path; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 4253 | std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path); |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4254 | if (EC) { |
| 4255 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
Daniel Dunbar | e627c1c | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 4256 | return ""; |
| 4257 | } |
| 4258 | |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4259 | return Path.str(); |
Daniel Dunbar | e627c1c | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4262 | std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const { |
| 4263 | SmallString<128> Output; |
| 4264 | if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) { |
| 4265 | // FIXME: If anybody needs it, implement this obscure rule: |
| 4266 | // "If you specify a directory without a file name, the default file name |
| 4267 | // is VCx0.pch., where x is the major version of Visual C++ in use." |
| 4268 | Output = FpArg->getValue(); |
| 4269 | |
| 4270 | // "If you do not specify an extension as part of the path name, an |
| 4271 | // extension of .pch is assumed. " |
| 4272 | if (!llvm::sys::path::has_extension(Output)) |
| 4273 | Output += ".pch"; |
| 4274 | } else { |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 4275 | if (Arg *YcArg = C.getArgs().getLastArg(options::OPT__SLASH_Yc)) |
| 4276 | Output = YcArg->getValue(); |
| 4277 | if (Output.empty()) |
| 4278 | Output = BaseName; |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4279 | llvm::sys::path::replace_extension(Output, ".pch"); |
| 4280 | } |
| 4281 | return Output.str(); |
| 4282 | } |
| 4283 | |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4284 | const ToolChain &Driver::getToolChain(const ArgList &Args, |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 4285 | const llvm::Triple &Target) const { |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4286 | |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4287 | auto &TC = ToolChains[Target.str()]; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4288 | if (!TC) { |
| 4289 | switch (Target.getOS()) { |
Reid Kleckner | 330fb17 | 2016-05-11 16:19:05 +0000 | [diff] [blame] | 4290 | case llvm::Triple::Haiku: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4291 | TC = llvm::make_unique<toolchains::Haiku>(*this, Target, Args); |
Reid Kleckner | 330fb17 | 2016-05-11 16:19:05 +0000 | [diff] [blame] | 4292 | break; |
Ed Schouten | 4dabea2 | 2017-06-25 08:29:09 +0000 | [diff] [blame] | 4293 | case llvm::Triple::Ananas: |
| 4294 | TC = llvm::make_unique<toolchains::Ananas>(*this, Target, Args); |
| 4295 | break; |
Ed Schouten | 3c3e58c | 2015-03-26 11:13:44 +0000 | [diff] [blame] | 4296 | case llvm::Triple::CloudABI: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4297 | TC = llvm::make_unique<toolchains::CloudABI>(*this, Target, Args); |
Ed Schouten | 3c3e58c | 2015-03-26 11:13:44 +0000 | [diff] [blame] | 4298 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4299 | case llvm::Triple::Darwin: |
| 4300 | case llvm::Triple::MacOSX: |
| 4301 | case llvm::Triple::IOS: |
Tim Northover | 6f3ff22 | 2015-10-30 16:30:27 +0000 | [diff] [blame] | 4302 | case llvm::Triple::TvOS: |
| 4303 | case llvm::Triple::WatchOS: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4304 | TC = llvm::make_unique<toolchains::DarwinClang>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4305 | break; |
| 4306 | case llvm::Triple::DragonFly: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4307 | TC = llvm::make_unique<toolchains::DragonFly>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4308 | break; |
| 4309 | case llvm::Triple::OpenBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4310 | TC = llvm::make_unique<toolchains::OpenBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4311 | break; |
| 4312 | case llvm::Triple::NetBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4313 | TC = llvm::make_unique<toolchains::NetBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4314 | break; |
| 4315 | case llvm::Triple::FreeBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4316 | TC = llvm::make_unique<toolchains::FreeBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4317 | break; |
| 4318 | case llvm::Triple::Minix: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4319 | TC = llvm::make_unique<toolchains::Minix>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4320 | break; |
| 4321 | case llvm::Triple::Linux: |
Andrey Turetskiy | 4798eb6 | 2016-06-16 10:36:09 +0000 | [diff] [blame] | 4322 | case llvm::Triple::ELFIAMCU: |
Chandler Carruth | cf705b2 | 2012-01-25 21:03:58 +0000 | [diff] [blame] | 4323 | if (Target.getArch() == llvm::Triple::hexagon) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4324 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4325 | Args); |
Vasileios Kalintiris | c744e12 | 2015-11-12 15:26:54 +0000 | [diff] [blame] | 4326 | else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) && |
| 4327 | !Target.hasEnvironment()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4328 | TC = llvm::make_unique<toolchains::MipsLLVMToolChain>(*this, Target, |
| 4329 | Args); |
Chandler Carruth | cf705b2 | 2012-01-25 21:03:58 +0000 | [diff] [blame] | 4330 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4331 | TC = llvm::make_unique<toolchains::Linux>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4332 | break; |
Derek Schuff | 6ab52fa | 2015-03-30 20:31:33 +0000 | [diff] [blame] | 4333 | case llvm::Triple::NaCl: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4334 | TC = llvm::make_unique<toolchains::NaClToolChain>(*this, Target, Args); |
Derek Schuff | 6ab52fa | 2015-03-30 20:31:33 +0000 | [diff] [blame] | 4335 | break; |
Petr Hosek | 62e1d23 | 2016-10-06 06:08:09 +0000 | [diff] [blame] | 4336 | case llvm::Triple::Fuchsia: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4337 | TC = llvm::make_unique<toolchains::Fuchsia>(*this, Target, Args); |
Petr Hosek | 62e1d23 | 2016-10-06 06:08:09 +0000 | [diff] [blame] | 4338 | break; |
David Chisnall | f571cde | 2012-02-15 13:39:01 +0000 | [diff] [blame] | 4339 | case llvm::Triple::Solaris: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4340 | TC = llvm::make_unique<toolchains::Solaris>(*this, Target, Args); |
David Chisnall | f571cde | 2012-02-15 13:39:01 +0000 | [diff] [blame] | 4341 | break; |
Tom Stellard | 8fa3309 | 2015-07-18 01:49:05 +0000 | [diff] [blame] | 4342 | case llvm::Triple::AMDHSA: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4343 | TC = llvm::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args); |
Tom Stellard | 8fa3309 | 2015-07-18 01:49:05 +0000 | [diff] [blame] | 4344 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4345 | case llvm::Triple::Win32: |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4346 | switch (Target.getEnvironment()) { |
| 4347 | default: |
| 4348 | if (Target.isOSBinFormatELF()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4349 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4350 | else if (Target.isOSBinFormatMachO()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4351 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4352 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4353 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4354 | break; |
| 4355 | case llvm::Triple::GNU: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4356 | TC = llvm::make_unique<toolchains::MinGW>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4357 | break; |
Saleem Abdulrasool | 543a78b | 2014-10-24 03:13:37 +0000 | [diff] [blame] | 4358 | case llvm::Triple::Itanium: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4359 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>(*this, Target, |
| 4360 | Args); |
Saleem Abdulrasool | 543a78b | 2014-10-24 03:13:37 +0000 | [diff] [blame] | 4361 | break; |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4362 | case llvm::Triple::MSVC: |
| 4363 | case llvm::Triple::UnknownEnvironment: |
Dave Lee | f96bedf | 2017-11-06 21:18:05 +0000 | [diff] [blame] | 4364 | if (Args.getLastArgValue(options::OPT_fuse_ld_EQ) |
| 4365 | .startswith_lower("bfd")) |
| 4366 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>( |
| 4367 | *this, Target, Args); |
| 4368 | else |
| 4369 | TC = |
| 4370 | llvm::make_unique<toolchains::MSVCToolChain>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4371 | break; |
| 4372 | } |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4373 | break; |
Filipe Cabecinhas | c888e19 | 2015-10-14 12:25:43 +0000 | [diff] [blame] | 4374 | case llvm::Triple::PS4: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4375 | TC = llvm::make_unique<toolchains::PS4CPU>(*this, Target, Args); |
Filipe Cabecinhas | c888e19 | 2015-10-14 12:25:43 +0000 | [diff] [blame] | 4376 | break; |
David L Kreitzer | d397ea4 | 2016-10-14 20:44:33 +0000 | [diff] [blame] | 4377 | case llvm::Triple::Contiki: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4378 | TC = llvm::make_unique<toolchains::Contiki>(*this, Target, Args); |
David L Kreitzer | d397ea4 | 2016-10-14 20:44:33 +0000 | [diff] [blame] | 4379 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4380 | default: |
Douglas Katzman | 9f5e70e | 2015-05-26 18:01:33 +0000 | [diff] [blame] | 4381 | // Of these targets, Hexagon is the only one that might have |
| 4382 | // an OS of Linux, in which case it got handled above already. |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4383 | switch (Target.getArch()) { |
| 4384 | case llvm::Triple::tce: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4385 | TC = llvm::make_unique<toolchains::TCEToolChain>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4386 | break; |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 4387 | case llvm::Triple::tcele: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4388 | TC = llvm::make_unique<toolchains::TCELEToolChain>(*this, Target, Args); |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 4389 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4390 | case llvm::Triple::hexagon: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4391 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4392 | Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4393 | break; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 4394 | case llvm::Triple::lanai: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4395 | TC = llvm::make_unique<toolchains::LanaiToolChain>(*this, Target, Args); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 4396 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4397 | case llvm::Triple::xcore: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4398 | TC = llvm::make_unique<toolchains::XCoreToolChain>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4399 | break; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 4400 | case llvm::Triple::wasm32: |
| 4401 | case llvm::Triple::wasm64: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4402 | TC = llvm::make_unique<toolchains::WebAssembly>(*this, Target, Args); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 4403 | break; |
Dylan McKay | 924fa3a | 2017-01-05 05:20:27 +0000 | [diff] [blame] | 4404 | case llvm::Triple::avr: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4405 | TC = llvm::make_unique<toolchains::AVRToolChain>(*this, Target, Args); |
Dylan McKay | 924fa3a | 2017-01-05 05:20:27 +0000 | [diff] [blame] | 4406 | break; |
David Bolvansky | f4be253 | 2018-07-31 14:21:46 +0000 | [diff] [blame] | 4407 | case llvm::Triple::riscv32: |
| 4408 | case llvm::Triple::riscv64: |
| 4409 | TC = llvm::make_unique<toolchains::RISCVToolChain>(*this, Target, Args); |
| 4410 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4411 | default: |
Douglas Katzman | d6e597c | 2015-09-17 19:56:40 +0000 | [diff] [blame] | 4412 | if (Target.getVendor() == llvm::Triple::Myriad) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4413 | TC = llvm::make_unique<toolchains::MyriadToolChain>(*this, Target, |
| 4414 | Args); |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 4415 | else if (toolchains::BareMetal::handlesTarget(Target)) |
| 4416 | TC = llvm::make_unique<toolchains::BareMetal>(*this, Target, Args); |
Douglas Katzman | d6e597c | 2015-09-17 19:56:40 +0000 | [diff] [blame] | 4417 | else if (Target.isOSBinFormatELF()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4418 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4419 | else if (Target.isOSBinFormatMachO()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4420 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4421 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4422 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4423 | } |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4424 | } |
| 4425 | } |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 4426 | |
| 4427 | // Intentionally omitted from the switch above: llvm::Triple::CUDA. CUDA |
| 4428 | // compiles always need two toolchains, the CUDA toolchain and the host |
| 4429 | // toolchain. So the only valid way to create a CUDA toolchain is via |
| 4430 | // CreateOffloadingDeviceToolChains. |
| 4431 | |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4432 | return *TC; |
Daniel Dunbar | 4dff6a4 | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 4433 | } |
Daniel Dunbar | 8fa879d | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 4434 | |
Rafael Espindola | 2f69d40 | 2013-03-18 15:33:26 +0000 | [diff] [blame] | 4435 | bool Driver::ShouldUseClangCompiler(const JobAction &JA) const { |
Douglas Katzman | 0024909 | 2015-06-12 15:45:21 +0000 | [diff] [blame] | 4436 | // Say "no" if there is not exactly one input of a type clang understands. |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 4437 | if (JA.size() != 1 || |
| 4438 | !types::isAcceptedByClang((*JA.input_begin())->getType())) |
Nick Lewycky | 5cc9ebb | 2012-11-15 05:36:36 +0000 | [diff] [blame] | 4439 | return false; |
| 4440 | |
Douglas Katzman | 0024909 | 2015-06-12 15:45:21 +0000 | [diff] [blame] | 4441 | // And say "no" if this is not a kind of action clang understands. |
Nick Lewycky | 5cc9ebb | 2012-11-15 05:36:36 +0000 | [diff] [blame] | 4442 | if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) && |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 4443 | !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA)) |
Nick Lewycky | 5cc9ebb | 2012-11-15 05:36:36 +0000 | [diff] [blame] | 4444 | return false; |
| 4445 | |
| 4446 | return true; |
| 4447 | } |
| 4448 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4449 | /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the |
| 4450 | /// grouped values as integers. Numbers which are not provided are set to 0. |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4451 | /// |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4452 | /// \return True if the entire string was parsed (9.2), or all groups were |
| 4453 | /// parsed (10.3.5extrastuff). |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4454 | bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor, |
| 4455 | unsigned &Micro, bool &HadExtra) { |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4456 | HadExtra = false; |
| 4457 | |
| 4458 | Major = Minor = Micro = 0; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4459 | if (Str.empty()) |
Bob Wilson | 433cb31 | 2015-04-07 01:03:35 +0000 | [diff] [blame] | 4460 | return false; |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4461 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4462 | if (Str.consumeInteger(10, Major)) |
| 4463 | return false; |
| 4464 | if (Str.empty()) |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4465 | return true; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4466 | if (Str[0] != '.') |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4467 | return false; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4468 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4469 | Str = Str.drop_front(1); |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4470 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4471 | if (Str.consumeInteger(10, Minor)) |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4472 | return false; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4473 | if (Str.empty()) |
| 4474 | return true; |
| 4475 | if (Str[0] != '.') |
| 4476 | return false; |
| 4477 | Str = Str.drop_front(1); |
| 4478 | |
| 4479 | if (Str.consumeInteger(10, Micro)) |
| 4480 | return false; |
| 4481 | if (!Str.empty()) |
| 4482 | HadExtra = true; |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4483 | return true; |
| 4484 | } |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4485 | |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4486 | /// Parse digits from a string \p Str and fulfill \p Digits with |
| 4487 | /// the parsed numbers. This method assumes that the max number of |
| 4488 | /// digits to look for is equal to Digits.size(). |
| 4489 | /// |
| 4490 | /// \return True if the entire string was parsed and there are |
| 4491 | /// no extra characters remaining at the end. |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4492 | bool Driver::GetReleaseVersion(StringRef Str, |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4493 | MutableArrayRef<unsigned> Digits) { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4494 | if (Str.empty()) |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4495 | return false; |
| 4496 | |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4497 | unsigned CurDigit = 0; |
| 4498 | while (CurDigit < Digits.size()) { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4499 | unsigned Digit; |
| 4500 | if (Str.consumeInteger(10, Digit)) |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4501 | return false; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4502 | Digits[CurDigit] = Digit; |
| 4503 | if (Str.empty()) |
| 4504 | return true; |
| 4505 | if (Str[0] != '.') |
| 4506 | return false; |
| 4507 | Str = Str.drop_front(1); |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4508 | CurDigit++; |
| 4509 | } |
| 4510 | |
| 4511 | // More digits than requested, bail out... |
| 4512 | return false; |
| 4513 | } |
| 4514 | |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4515 | std::pair<unsigned, unsigned> Driver::getIncludeExcludeOptionFlagMasks() const { |
| 4516 | unsigned IncludedFlagsBitmask = 0; |
Rafael Espindola | cc707bc | 2013-09-25 15:54:41 +0000 | [diff] [blame] | 4517 | unsigned ExcludedFlagsBitmask = options::NoDriverOption; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4518 | |
| 4519 | if (Mode == CLMode) { |
Hans Wennborg | 1907610 | 2013-07-31 20:51:53 +0000 | [diff] [blame] | 4520 | // Include CL and Core options. |
| 4521 | IncludedFlagsBitmask |= options::CLOption; |
| 4522 | IncludedFlagsBitmask |= options::CoreOption; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4523 | } else { |
| 4524 | ExcludedFlagsBitmask |= options::CLOption; |
| 4525 | } |
| 4526 | |
| 4527 | return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask); |
| 4528 | } |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 4529 | |
Douglas Katzman | f08fadf | 2015-06-04 14:40:44 +0000 | [diff] [blame] | 4530 | bool clang::driver::isOptimizationLevelFast(const ArgList &Args) { |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 4531 | return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false); |
| 4532 | } |