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