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