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