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