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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Dunbar | 544ecd1 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Daniel Dunbar | 544ecd1 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 9 | #include "clang/Driver/Driver.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 10 | #include "InputInfo.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 11 | #include "ToolChains/AMDGPU.h" |
| 12 | #include "ToolChains/AVR.h" |
Ed Schouten | 4dabea2 | 2017-06-25 08:29:09 +0000 | [diff] [blame] | 13 | #include "ToolChains/Ananas.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 14 | #include "ToolChains/BareMetal.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 15 | #include "ToolChains/Clang.h" |
| 16 | #include "ToolChains/CloudABI.h" |
| 17 | #include "ToolChains/Contiki.h" |
| 18 | #include "ToolChains/CrossWindows.h" |
| 19 | #include "ToolChains/Cuda.h" |
| 20 | #include "ToolChains/Darwin.h" |
| 21 | #include "ToolChains/DragonFly.h" |
| 22 | #include "ToolChains/FreeBSD.h" |
| 23 | #include "ToolChains/Fuchsia.h" |
| 24 | #include "ToolChains/Gnu.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 25 | #include "ToolChains/HIP.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 26 | #include "ToolChains/Haiku.h" |
| 27 | #include "ToolChains/Hexagon.h" |
Kristina Brooks | 77a4adc | 2018-11-29 03:49:14 +0000 | [diff] [blame] | 28 | #include "ToolChains/Hurd.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 29 | #include "ToolChains/Lanai.h" |
| 30 | #include "ToolChains/Linux.h" |
Anton Korobeynikov | 93165d6 | 2019-01-15 19:44:05 +0000 | [diff] [blame] | 31 | #include "ToolChains/MSP430.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 32 | #include "ToolChains/MSVC.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 33 | #include "ToolChains/MinGW.h" |
| 34 | #include "ToolChains/Minix.h" |
Konstantin Zhuravlyov | e37b32c | 2017-03-08 22:36:04 +0000 | [diff] [blame] | 35 | #include "ToolChains/MipsLinux.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 36 | #include "ToolChains/Myriad.h" |
| 37 | #include "ToolChains/NaCl.h" |
| 38 | #include "ToolChains/NetBSD.h" |
| 39 | #include "ToolChains/OpenBSD.h" |
| 40 | #include "ToolChains/PS4CPU.h" |
Erik Pilkington | 46420b6 | 2018-09-27 20:36:28 +0000 | [diff] [blame] | 41 | #include "ToolChains/RISCVToolchain.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 42 | #include "ToolChains/Solaris.h" |
| 43 | #include "ToolChains/TCE.h" |
| 44 | #include "ToolChains/WebAssembly.h" |
| 45 | #include "ToolChains/XCore.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 46 | #include "clang/Basic/Version.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 47 | #include "clang/Config/config.h" |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 48 | #include "clang/Driver/Action.h" |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 49 | #include "clang/Driver/Compilation.h" |
Daniel Dunbar | c0b3e95 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 50 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 51 | #include "clang/Driver/Job.h" |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 52 | #include "clang/Driver/Options.h" |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 53 | #include "clang/Driver/SanitizerArgs.h" |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 54 | #include "clang/Driver/Tool.h" |
| 55 | #include "clang/Driver/ToolChain.h" |
Chris Lattner | ce6c42f | 2011-03-23 04:04:01 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/ArrayRef.h" |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/STLExtras.h" |
Justin Lebar | 6290761 | 2016-07-06 21:21:39 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/SmallSet.h" |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/StringSet.h" |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/StringSwitch.h" |
Nico Weber | d637c05 | 2018-04-30 13:52:15 +0000 | [diff] [blame] | 62 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 63 | #include "llvm/Option/Arg.h" |
| 64 | #include "llvm/Option/ArgList.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 65 | #include "llvm/Option/OptSpecifier.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 66 | #include "llvm/Option/OptTable.h" |
| 67 | #include "llvm/Option/Option.h" |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 68 | #include "llvm/Support/CommandLine.h" |
David Blaikie | 7900020 | 2011-09-23 05:57:42 +0000 | [diff] [blame] | 69 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | f28df4c | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 70 | #include "llvm/Support/FileSystem.h" |
Fangrui Song | d002823 | 2018-10-10 00:15:33 +0000 | [diff] [blame] | 71 | #include "llvm/Support/FormatVariadic.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 72 | #include "llvm/Support/Path.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 73 | #include "llvm/Support/PrettyStackTrace.h" |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 74 | #include "llvm/Support/Process.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 75 | #include "llvm/Support/Program.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 76 | #include "llvm/Support/StringSaver.h" |
Dimitry Andric | 81c4042 | 2017-06-06 21:54:21 +0000 | [diff] [blame] | 77 | #include "llvm/Support/TargetRegistry.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 78 | #include "llvm/Support/VirtualFileSystem.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 79 | #include "llvm/Support/raw_ostream.h" |
Dylan Noblesmith | 4f4e745 | 2012-02-02 00:40:14 +0000 | [diff] [blame] | 80 | #include <map> |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 81 | #include <memory> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 82 | #include <utility> |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 83 | #if LLVM_ON_UNIX |
| 84 | #include <unistd.h> // getpid |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 85 | #include <sysexits.h> // EX_IOERR |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 86 | #endif |
Dylan Noblesmith | 86780e9 | 2012-02-01 14:25:28 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 88 | using namespace clang::driver; |
Chris Lattner | ada1c91 | 2009-03-26 05:56:24 +0000 | [diff] [blame] | 89 | using namespace clang; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 90 | using namespace llvm::opt; |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 91 | |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 92 | Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple, |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 93 | DiagnosticsEngine &Diags, |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 94 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 95 | : Opts(createDriverOptTable()), Diags(Diags), VFS(std::move(VFS)), |
| 96 | Mode(GCCMode), SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone), |
| 97 | LTOMode(LTOK_None), ClangExecutable(ClangExecutable), |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 98 | SysRoot(DEFAULT_SYSROOT), DriverTitle("clang LLVM compiler"), |
| 99 | CCPrintOptionsFilename(nullptr), CCPrintHeadersFilename(nullptr), |
| 100 | CCLogDiagnosticsFilename(nullptr), CCCPrintBindings(false), |
| 101 | CCPrintOptions(false), CCPrintHeaders(false), CCLogDiagnostics(false), |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 102 | CCGenDiagnostics(false), TargetTriple(TargetTriple), |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 103 | CCCGenericGCCName(""), Saver(Alloc), CheckInputsExist(true), |
Erich Keane | 0a6b5b6 | 2018-12-04 14:34:09 +0000 | [diff] [blame] | 104 | GenReproducer(false), SuppressMissingInputWarning(false) { |
Daniel Dunbar | 3f3e2cd | 2010-01-20 02:35:16 +0000 | [diff] [blame] | 105 | |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 106 | // Provide a sane fallback if no VFS is specified. |
| 107 | if (!this->VFS) |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 108 | this->VFS = llvm::vfs::getRealFileSystem(); |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 109 | |
Sumanth Gundapaneni | 3a159294 | 2015-03-03 20:43:12 +0000 | [diff] [blame] | 110 | Name = llvm::sys::path::filename(ClangExecutable); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 111 | Dir = llvm::sys::path::parent_path(ClangExecutable); |
Benjamin Kramer | f420dda | 2015-10-13 15:19:32 +0000 | [diff] [blame] | 112 | InstalledDir = Dir; // Provide a sensible default installed dir. |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 113 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 114 | #if defined(CLANG_CONFIG_FILE_SYSTEM_DIR) |
| 115 | SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR; |
| 116 | #endif |
| 117 | #if defined(CLANG_CONFIG_FILE_USER_DIR) |
| 118 | UserConfigDir = CLANG_CONFIG_FILE_USER_DIR; |
| 119 | #endif |
| 120 | |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 121 | // Compute the path to the resource directory. |
| 122 | StringRef ClangResourceDir(CLANG_RESOURCE_DIR); |
| 123 | SmallString<128> P(Dir); |
Chandler Carruth | fd3cc70 | 2014-12-29 12:09:08 +0000 | [diff] [blame] | 124 | if (ClangResourceDir != "") { |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 125 | llvm::sys::path::append(P, ClangResourceDir); |
Chandler Carruth | fd3cc70 | 2014-12-29 12:09:08 +0000 | [diff] [blame] | 126 | } else { |
| 127 | StringRef ClangLibdirSuffix(CLANG_LIBDIR_SUFFIX); |
Argyrios Kyrtzidis | bcae047 | 2017-02-25 18:14:35 +0000 | [diff] [blame] | 128 | P = llvm::sys::path::parent_path(Dir); |
| 129 | llvm::sys::path::append(P, Twine("lib") + ClangLibdirSuffix, "clang", |
Chandler Carruth | fd3cc70 | 2014-12-29 12:09:08 +0000 | [diff] [blame] | 130 | CLANG_VERSION_STRING); |
| 131 | } |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 132 | ResourceDir = P.str(); |
Daniel Dunbar | 544ecd1 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 135 | void Driver::ParseDriverMode(StringRef ProgramName, |
| 136 | ArrayRef<const char *> Args) { |
Martin Storsjo | 1fab23d | 2018-04-25 21:23:59 +0000 | [diff] [blame] | 137 | if (ClangNameParts.isEmpty()) |
| 138 | ClangNameParts = ToolChain::getTargetAndModeFromProgramName(ProgramName); |
Serge Pavlov | 4e76984 | 2017-08-29 05:22:26 +0000 | [diff] [blame] | 139 | setDriverModeFromOption(ClangNameParts.DriverMode); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 140 | |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 141 | for (const char *ArgPtr : Args) { |
Erich Keane | 258f059 | 2018-02-07 00:19:58 +0000 | [diff] [blame] | 142 | // Ignore nullptrs, they are the response file's EOL markers. |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 143 | if (ArgPtr == nullptr) |
Reid Kleckner | af5fd6a | 2014-08-22 19:29:30 +0000 | [diff] [blame] | 144 | continue; |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 145 | const StringRef Arg = ArgPtr; |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 146 | setDriverModeFromOption(Arg); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 150 | void Driver::setDriverModeFromOption(StringRef Opt) { |
| 151 | const std::string OptName = |
| 152 | getOpts().getOption(options::OPT_driver_mode).getPrefixedName(); |
| 153 | if (!Opt.startswith(OptName)) |
| 154 | return; |
| 155 | StringRef Value = Opt.drop_front(OptName.size()); |
| 156 | |
Erich Keane | 2908a04 | 2018-02-12 17:47:01 +0000 | [diff] [blame] | 157 | if (auto M = llvm::StringSwitch<llvm::Optional<DriverMode>>(Value) |
| 158 | .Case("gcc", GCCMode) |
| 159 | .Case("g++", GXXMode) |
| 160 | .Case("cpp", CPPMode) |
| 161 | .Case("cl", CLMode) |
| 162 | .Default(None)) |
| 163 | Mode = *M; |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 164 | else |
| 165 | Diag(diag::err_drv_unsupported_option_argument) << OptName << Value; |
| 166 | } |
| 167 | |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 168 | InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 169 | bool IsClCompatMode, |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 170 | bool &ContainsError) { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 171 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 172 | ContainsError = false; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 173 | |
| 174 | unsigned IncludedFlagsBitmask; |
| 175 | unsigned ExcludedFlagsBitmask; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 176 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 177 | getIncludeExcludeOptionFlagMasks(IsClCompatMode); |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 178 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 179 | unsigned MissingArgIndex, MissingArgCount; |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 180 | InputArgList Args = |
David Blaikie | 6d492ad | 2015-06-21 06:32:36 +0000 | [diff] [blame] | 181 | getOpts().ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount, |
| 182 | IncludedFlagsBitmask, ExcludedFlagsBitmask); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 183 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 184 | // Check for missing argument error. |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 185 | if (MissingArgCount) { |
| 186 | Diag(diag::err_drv_missing_argument) |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 187 | << Args.getArgString(MissingArgIndex) << MissingArgCount; |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 188 | ContainsError |= |
| 189 | Diags.getDiagnosticLevel(diag::err_drv_missing_argument, |
| 190 | SourceLocation()) > DiagnosticsEngine::Warning; |
| 191 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 192 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 193 | // Check for unsupported options. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 194 | for (const Arg *A : Args) { |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 195 | if (A->getOption().hasFlag(options::Unsupported)) { |
Brian Gesiak | 2491076 | 2018-01-06 00:25:40 +0000 | [diff] [blame] | 196 | unsigned DiagID; |
| 197 | auto ArgString = A->getAsString(Args); |
| 198 | std::string Nearest; |
| 199 | if (getOpts().findNearest( |
| 200 | ArgString, Nearest, IncludedFlagsBitmask, |
| 201 | ExcludedFlagsBitmask | options::Unsupported) > 1) { |
| 202 | DiagID = diag::err_drv_unsupported_opt; |
| 203 | Diag(DiagID) << ArgString; |
| 204 | } else { |
| 205 | DiagID = diag::err_drv_unsupported_opt_with_suggestion; |
| 206 | Diag(DiagID) << ArgString << Nearest; |
| 207 | } |
| 208 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 209 | DiagnosticsEngine::Warning; |
Daniel Dunbar | d8500f3 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 210 | continue; |
| 211 | } |
Chad Rosier | ce975d9 | 2012-02-22 17:55:22 +0000 | [diff] [blame] | 212 | |
| 213 | // Warn about -mcpu= without an argument. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 214 | if (A->getOption().matches(options::OPT_mcpu_EQ) && A->containsValue("")) { |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 215 | Diag(diag::warn_drv_empty_joined_argument) << A->getAsString(Args); |
| 216 | ContainsError |= Diags.getDiagnosticLevel( |
| 217 | diag::warn_drv_empty_joined_argument, |
| 218 | SourceLocation()) > DiagnosticsEngine::Warning; |
Chad Rosier | ce975d9 | 2012-02-22 17:55:22 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 222 | for (const Arg *A : Args.filtered(options::OPT_UNKNOWN)) { |
Brian Gesiak | 2491076 | 2018-01-06 00:25:40 +0000 | [diff] [blame] | 223 | unsigned DiagID; |
| 224 | auto ArgString = A->getAsString(Args); |
| 225 | std::string Nearest; |
| 226 | if (getOpts().findNearest( |
| 227 | ArgString, Nearest, IncludedFlagsBitmask, ExcludedFlagsBitmask) > 1) { |
| 228 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl |
| 229 | : diag::err_drv_unknown_argument; |
| 230 | Diags.Report(DiagID) << ArgString; |
| 231 | } else { |
| 232 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion |
| 233 | : diag::err_drv_unknown_argument_with_suggestion; |
| 234 | Diags.Report(DiagID) << ArgString << Nearest; |
| 235 | } |
| 236 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 237 | DiagnosticsEngine::Warning; |
| 238 | } |
Rafael Espindola | 8a2d496 | 2013-09-23 23:55:25 +0000 | [diff] [blame] | 239 | |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 240 | return Args; |
| 241 | } |
| 242 | |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 243 | // Determine which compilation mode we are in. We look for options which |
| 244 | // affect the phase, starting with the earliest phases, and record which |
| 245 | // option we used to determine the final phase. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 246 | phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, |
| 247 | Arg **FinalPhaseArg) const { |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 248 | Arg *PhaseArg = nullptr; |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 249 | phases::ID FinalPhase; |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 250 | |
Hans Wennborg | e50cec3 | 2014-06-13 20:59:54 +0000 | [diff] [blame] | 251 | // -{E,EP,P,M,MM} only run the preprocessor. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 252 | if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) || |
Hans Wennborg | e50cec3 | 2014-06-13 20:59:54 +0000 | [diff] [blame] | 253 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) || |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 254 | (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) || |
| 255 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 256 | FinalPhase = phases::Preprocess; |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 257 | |
Richard Smith | dd4ad3d | 2016-08-30 19:06:26 +0000 | [diff] [blame] | 258 | // --precompile only runs up to precompilation. |
| 259 | } else if ((PhaseArg = DAL.getLastArg(options::OPT__precompile))) { |
| 260 | FinalPhase = phases::Precompile; |
| 261 | |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 262 | // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler. |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 263 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) || |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 264 | (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) || |
Ben Langmuir | 2cb4a78 | 2014-02-05 22:21:15 +0000 | [diff] [blame] | 265 | (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) || |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 266 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) || |
Fariborz Jahanian | 73223bb | 2012-04-02 15:59:19 +0000 | [diff] [blame] | 267 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) || |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 268 | (PhaseArg = DAL.getLastArg(options::OPT__migrate)) || |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 269 | (PhaseArg = DAL.getLastArg(options::OPT__analyze, |
Chad Rosier | 1aeb15a | 2012-03-06 23:14:35 +0000 | [diff] [blame] | 270 | options::OPT__analyze_auto)) || |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 271 | (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 272 | FinalPhase = phases::Compile; |
| 273 | |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 274 | // -S only runs up to the backend. |
| 275 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) { |
| 276 | FinalPhase = phases::Backend; |
| 277 | |
Artem Belevich | 4242f41 | 2015-07-28 21:01:21 +0000 | [diff] [blame] | 278 | // -c compilation only runs up to the assembler. |
| 279 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 280 | FinalPhase = phases::Assemble; |
| 281 | |
| 282 | // Otherwise do everything. |
| 283 | } else |
| 284 | FinalPhase = phases::Link; |
| 285 | |
| 286 | if (FinalPhaseArg) |
| 287 | *FinalPhaseArg = PhaseArg; |
| 288 | |
| 289 | return FinalPhase; |
| 290 | } |
| 291 | |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 292 | static Arg *MakeInputArg(DerivedArgList &Args, OptTable &Opts, |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 293 | StringRef Value, bool Claim = true) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 294 | Arg *A = new Arg(Opts.getOption(options::OPT_INPUT), Value, |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 295 | Args.getBaseArgs().MakeIndex(Value), Value.data()); |
Hans Wennborg | 5536285 | 2014-05-02 22:55:30 +0000 | [diff] [blame] | 296 | Args.AddSynthesizedArg(A); |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 297 | if (Claim) |
| 298 | A->claim(); |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 299 | return A; |
| 300 | } |
| 301 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 302 | DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const { |
| 303 | DerivedArgList *DAL = new DerivedArgList(Args); |
| 304 | |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 305 | bool HasNostdlib = Args.hasArg(options::OPT_nostdlib); |
Dan Albert | f5ffa1a | 2018-12-18 23:29:35 +0000 | [diff] [blame] | 306 | bool HasNostdlibxx = Args.hasArg(options::OPT_nostdlibxx); |
Nirav Dave | b0bb614 | 2015-11-24 16:07:21 +0000 | [diff] [blame] | 307 | bool HasNodefaultlib = Args.hasArg(options::OPT_nodefaultlibs); |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 308 | for (Arg *A : Args) { |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 309 | // Unfortunately, we have to parse some forwarding options (-Xassembler, |
| 310 | // -Xlinker, -Xpreprocessor) because we either integrate their functionality |
| 311 | // (assembler and preprocessor), or bypass a previous driver ('collect2'). |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 312 | |
| 313 | // Rewrite linker options, to replace --no-demangle with a custom internal |
| 314 | // option. |
| 315 | if ((A->getOption().matches(options::OPT_Wl_COMMA) || |
| 316 | A->getOption().matches(options::OPT_Xlinker)) && |
| 317 | A->containsValue("--no-demangle")) { |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 318 | // Add the rewritten no-demangle argument. |
| 319 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle)); |
| 320 | |
| 321 | // Add the remaining values as Xlinker arguments. |
Benjamin Kramer | 72e6431 | 2015-09-24 14:48:49 +0000 | [diff] [blame] | 322 | for (StringRef Val : A->getValues()) |
Douglas Katzman | a34b7bf | 2015-06-30 19:32:57 +0000 | [diff] [blame] | 323 | if (Val != "--no-demangle") |
| 324 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_Xlinker), Val); |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 325 | |
| 326 | continue; |
| 327 | } |
| 328 | |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 329 | // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by |
| 330 | // some build systems. We don't try to be complete here because we don't |
| 331 | // care to encourage this usage model. |
| 332 | if (A->getOption().matches(options::OPT_Wp_COMMA) && |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 333 | (A->getValue(0) == StringRef("-MD") || |
| 334 | A->getValue(0) == StringRef("-MMD"))) { |
Daniel Dunbar | 3648ba7 | 2010-06-15 20:30:18 +0000 | [diff] [blame] | 335 | // Rewrite to -MD/-MMD along with -MF. |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 336 | if (A->getValue(0) == StringRef("-MD")) |
Daniel Dunbar | 3648ba7 | 2010-06-15 20:30:18 +0000 | [diff] [blame] | 337 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD)); |
| 338 | else |
| 339 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MMD)); |
Michael J. Spencer | 70d85be | 2012-11-07 23:37:14 +0000 | [diff] [blame] | 340 | if (A->getNumValues() == 2) |
| 341 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF), |
| 342 | A->getValue(1)); |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 343 | continue; |
| 344 | } |
| 345 | |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 346 | // Rewrite reserved library names. |
| 347 | if (A->getOption().matches(options::OPT_l)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 348 | StringRef Value = A->getValue(); |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 349 | |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 350 | // Rewrite unless -nostdlib is present. |
Dan Albert | f5ffa1a | 2018-12-18 23:29:35 +0000 | [diff] [blame] | 351 | if (!HasNostdlib && !HasNodefaultlib && !HasNostdlibxx && |
| 352 | Value == "stdc++") { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 353 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_stdcxx)); |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 354 | continue; |
| 355 | } |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 356 | |
| 357 | // Rewrite unconditionally. |
| 358 | if (Value == "cc_kext") { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 359 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_cckext)); |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 360 | continue; |
| 361 | } |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 364 | // Pick up inputs via the -- option. |
| 365 | if (A->getOption().matches(options::OPT__DASH_DASH)) { |
| 366 | A->claim(); |
Benjamin Kramer | 72e6431 | 2015-09-24 14:48:49 +0000 | [diff] [blame] | 367 | for (StringRef Val : A->getValues()) |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 368 | DAL->append(MakeInputArg(*DAL, *Opts, Val, false)); |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 369 | continue; |
| 370 | } |
| 371 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 372 | DAL->append(A); |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 373 | } |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 374 | |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 375 | // Enforce -static if -miamcu is present. |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 376 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) |
| 377 | DAL->AddFlagArg(0, Opts->getOption(options::OPT_static)); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 378 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 379 | // Add a default value of -mlinker-version=, if one was given and the user |
| 380 | // didn't specify one. |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 381 | #if defined(HOST_LINK_VERSION) |
Tim Northover | 018578c | 2015-06-12 19:21:35 +0000 | [diff] [blame] | 382 | if (!Args.hasArg(options::OPT_mlinker_version_EQ) && |
| 383 | strlen(HOST_LINK_VERSION) > 0) { |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 384 | DAL->AddJoinedArg(0, Opts->getOption(options::OPT_mlinker_version_EQ), |
| 385 | HOST_LINK_VERSION); |
Daniel Dunbar | b613ffc | 2010-08-17 22:32:45 +0000 | [diff] [blame] | 386 | DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim(); |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 387 | } |
| 388 | #endif |
| 389 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 390 | return DAL; |
| 391 | } |
| 392 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 393 | /// Compute target triple from args. |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 394 | /// |
| 395 | /// This routine provides the logic to compute a target triple from various |
| 396 | /// args passed to the driver and the default triple string. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 397 | static llvm::Triple computeTargetTriple(const Driver &D, |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 398 | StringRef TargetTriple, |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 399 | const ArgList &Args, |
| 400 | StringRef DarwinArchName = "") { |
| 401 | // FIXME: Already done in Compilation *Driver::BuildCompilation |
| 402 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 403 | TargetTriple = A->getValue(); |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 404 | |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 405 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 406 | |
Kristina Brooks | 77a4adc | 2018-11-29 03:49:14 +0000 | [diff] [blame] | 407 | // GNU/Hurd's triples should have been -hurd-gnu*, but were historically made |
| 408 | // -gnu* only, and we can not change this, so we have to detect that case as |
| 409 | // being the Hurd OS. |
| 410 | if (TargetTriple.find("-unknown-gnu") != StringRef::npos || |
| 411 | TargetTriple.find("-pc-gnu") != StringRef::npos) |
| 412 | Target.setOSName("hurd"); |
| 413 | |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 414 | // Handle Apple-specific options available here. |
| 415 | if (Target.isOSBinFormatMachO()) { |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 416 | // If an explicit Darwin arch name is given, that trumps all. |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 417 | if (!DarwinArchName.empty()) { |
| 418 | tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName); |
| 419 | return Target; |
| 420 | } |
| 421 | |
| 422 | // Handle the Darwin '-arch' flag. |
| 423 | if (Arg *A = Args.getLastArg(options::OPT_arch)) { |
| 424 | StringRef ArchName = A->getValue(); |
| 425 | tools::darwin::setTripleTypeForMachOArchName(Target, ArchName); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // Handle pseudo-target flags '-mlittle-endian'/'-EL' and |
| 430 | // '-mbig-endian'/'-EB'. |
| 431 | if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, |
| 432 | options::OPT_mbig_endian)) { |
| 433 | if (A->getOption().matches(options::OPT_mlittle_endian)) { |
| 434 | llvm::Triple LE = Target.getLittleEndianArchVariant(); |
| 435 | if (LE.getArch() != llvm::Triple::UnknownArch) |
| 436 | Target = std::move(LE); |
| 437 | } else { |
| 438 | llvm::Triple BE = Target.getBigEndianArchVariant(); |
| 439 | if (BE.getArch() != llvm::Triple::UnknownArch) |
| 440 | Target = std::move(BE); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // 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] | 445 | if (Target.getArch() == llvm::Triple::tce || |
| 446 | Target.getOS() == llvm::Triple::Minix) |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 447 | return Target; |
| 448 | |
| 449 | // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 450 | Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32, |
| 451 | options::OPT_m32, options::OPT_m16); |
| 452 | if (A) { |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 453 | llvm::Triple::ArchType AT = llvm::Triple::UnknownArch; |
| 454 | |
| 455 | if (A->getOption().matches(options::OPT_m64)) { |
| 456 | AT = Target.get64BitArchVariant().getArch(); |
| 457 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 458 | Target.setEnvironment(llvm::Triple::GNU); |
| 459 | } else if (A->getOption().matches(options::OPT_mx32) && |
| 460 | Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) { |
| 461 | AT = llvm::Triple::x86_64; |
| 462 | Target.setEnvironment(llvm::Triple::GNUX32); |
| 463 | } else if (A->getOption().matches(options::OPT_m32)) { |
| 464 | AT = Target.get32BitArchVariant().getArch(); |
| 465 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 466 | Target.setEnvironment(llvm::Triple::GNU); |
| 467 | } else if (A->getOption().matches(options::OPT_m16) && |
| 468 | Target.get32BitArchVariant().getArch() == llvm::Triple::x86) { |
| 469 | AT = llvm::Triple::x86; |
| 470 | Target.setEnvironment(llvm::Triple::CODE16); |
| 471 | } |
| 472 | |
| 473 | if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) |
| 474 | Target.setArch(AT); |
| 475 | } |
| 476 | |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 477 | // Handle -miamcu flag. |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 478 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { |
| 479 | if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86) |
| 480 | D.Diag(diag::err_drv_unsupported_opt_for_target) << "-miamcu" |
| 481 | << Target.str(); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 482 | |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 483 | if (A && !A->getOption().matches(options::OPT_m32)) |
| 484 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 485 | << "-miamcu" << A->getBaseArg().getAsString(Args); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 486 | |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 487 | Target.setArch(llvm::Triple::x86); |
| 488 | Target.setArchName("i586"); |
| 489 | Target.setEnvironment(llvm::Triple::UnknownEnvironment); |
| 490 | Target.setEnvironmentName(""); |
| 491 | Target.setOS(llvm::Triple::ELFIAMCU); |
| 492 | Target.setVendor(llvm::Triple::UnknownVendor); |
| 493 | Target.setVendorName("intel"); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Simon Atanasyan | 0c0fb4b | 2018-09-27 05:04:50 +0000 | [diff] [blame] | 496 | // If target is MIPS adjust the target triple |
| 497 | // accordingly to provided ABI name. |
| 498 | A = Args.getLastArg(options::OPT_mabi_EQ); |
Simon Atanasyan | e60eae4 | 2018-10-16 10:19:06 +0000 | [diff] [blame] | 499 | if (A && Target.isMIPS()) { |
| 500 | StringRef ABIName = A->getValue(); |
| 501 | if (ABIName == "32") { |
| 502 | Target = Target.get32BitArchVariant(); |
| 503 | if (Target.getEnvironment() == llvm::Triple::GNUABI64 || |
| 504 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
| 505 | Target.setEnvironment(llvm::Triple::GNU); |
| 506 | } else if (ABIName == "n32") { |
| 507 | Target = Target.get64BitArchVariant(); |
| 508 | if (Target.getEnvironment() == llvm::Triple::GNU || |
| 509 | Target.getEnvironment() == llvm::Triple::GNUABI64) |
| 510 | Target.setEnvironment(llvm::Triple::GNUABIN32); |
| 511 | } else if (ABIName == "64") { |
| 512 | Target = Target.get64BitArchVariant(); |
| 513 | if (Target.getEnvironment() == llvm::Triple::GNU || |
| 514 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
| 515 | Target.setEnvironment(llvm::Triple::GNUABI64); |
| 516 | } |
| 517 | } |
Simon Atanasyan | 0c0fb4b | 2018-09-27 05:04:50 +0000 | [diff] [blame] | 518 | |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 519 | return Target; |
| 520 | } |
| 521 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 522 | // Parse the LTO options and record the type of LTO compilation |
Teresa Johnson | 945bc50 | 2015-10-15 20:35:53 +0000 | [diff] [blame] | 523 | // based on which -f(no-)?lto(=.*)? option occurs last. |
| 524 | void Driver::setLTOMode(const llvm::opt::ArgList &Args) { |
| 525 | LTOMode = LTOK_None; |
| 526 | if (!Args.hasFlag(options::OPT_flto, options::OPT_flto_EQ, |
| 527 | options::OPT_fno_lto, false)) |
| 528 | return; |
| 529 | |
| 530 | StringRef LTOName("full"); |
| 531 | |
| 532 | const Arg *A = Args.getLastArg(options::OPT_flto_EQ); |
Teresa Johnson | 6ef80dc | 2015-11-02 18:03:12 +0000 | [diff] [blame] | 533 | if (A) |
| 534 | LTOName = A->getValue(); |
Teresa Johnson | 945bc50 | 2015-10-15 20:35:53 +0000 | [diff] [blame] | 535 | |
| 536 | LTOMode = llvm::StringSwitch<LTOKind>(LTOName) |
| 537 | .Case("full", LTOK_Full) |
| 538 | .Case("thin", LTOK_Thin) |
| 539 | .Default(LTOK_Unknown); |
| 540 | |
| 541 | if (LTOMode == LTOK_Unknown) { |
| 542 | assert(A); |
| 543 | Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() |
| 544 | << A->getValue(); |
| 545 | } |
| 546 | } |
| 547 | |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 548 | /// Compute the desired OpenMP runtime from the flags provided. |
| 549 | Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const { |
| 550 | StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME); |
| 551 | |
| 552 | const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ); |
| 553 | if (A) |
| 554 | RuntimeName = A->getValue(); |
| 555 | |
| 556 | auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName) |
| 557 | .Case("libomp", OMPRT_OMP) |
| 558 | .Case("libgomp", OMPRT_GOMP) |
| 559 | .Case("libiomp5", OMPRT_IOMP5) |
| 560 | .Default(OMPRT_Unknown); |
| 561 | |
| 562 | if (RT == OMPRT_Unknown) { |
| 563 | if (A) |
| 564 | Diag(diag::err_drv_unsupported_option_argument) |
| 565 | << A->getOption().getName() << A->getValue(); |
| 566 | else |
| 567 | // FIXME: We could use a nicer diagnostic here. |
| 568 | Diag(diag::err_drv_unsupported_opt) << "-fopenmp"; |
| 569 | } |
| 570 | |
| 571 | return RT; |
| 572 | } |
| 573 | |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 574 | void Driver::CreateOffloadingDeviceToolChains(Compilation &C, |
| 575 | InputList &Inputs) { |
| 576 | |
| 577 | // |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 578 | // CUDA/HIP |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 579 | // |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 580 | // We need to generate a CUDA/HIP toolchain if any of the inputs has a CUDA |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 581 | // or HIP type. However, mixed CUDA/HIP compilation is not supported. |
| 582 | bool IsCuda = |
| 583 | llvm::any_of(Inputs, [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 584 | return types::isCuda(I.first); |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 585 | }); |
| 586 | bool IsHIP = |
| 587 | llvm::any_of(Inputs, |
| 588 | [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
| 589 | return types::isHIP(I.first); |
| 590 | }) || |
| 591 | C.getInputArgs().hasArg(options::OPT_hip_link); |
| 592 | if (IsCuda && IsHIP) { |
| 593 | Diag(clang::diag::err_drv_mix_cuda_hip); |
| 594 | return; |
| 595 | } |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 596 | if (IsCuda) { |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 597 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 598 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 599 | StringRef DeviceTripleStr; |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 600 | auto OFK = Action::OFK_Cuda; |
| 601 | DeviceTripleStr = |
| 602 | HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda" : "nvptx-nvidia-cuda"; |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 603 | llvm::Triple CudaTriple(DeviceTripleStr); |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 604 | // Use the CUDA and host triples as the key into the ToolChains map, |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 605 | // because the device toolchain we create depends on both. |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 606 | auto &CudaTC = ToolChains[CudaTriple.str() + "/" + HostTriple.str()]; |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 607 | if (!CudaTC) { |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 608 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 609 | *this, CudaTriple, *HostTC, C.getInputArgs(), OFK); |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 610 | } |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 611 | C.addOffloadDeviceToolChain(CudaTC.get(), OFK); |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 612 | } else if (IsHIP) { |
| 613 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 614 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
| 615 | StringRef DeviceTripleStr; |
| 616 | auto OFK = Action::OFK_HIP; |
| 617 | DeviceTripleStr = "amdgcn-amd-amdhsa"; |
| 618 | llvm::Triple HIPTriple(DeviceTripleStr); |
| 619 | // Use the HIP and host triples as the key into the ToolChains map, |
| 620 | // because the device toolchain we create depends on both. |
| 621 | auto &HIPTC = ToolChains[HIPTriple.str() + "/" + HostTriple.str()]; |
| 622 | if (!HIPTC) { |
| 623 | HIPTC = llvm::make_unique<toolchains::HIPToolChain>( |
| 624 | *this, HIPTriple, *HostTC, C.getInputArgs()); |
| 625 | } |
| 626 | C.addOffloadDeviceToolChain(HIPTC.get(), OFK); |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 630 | // OpenMP |
| 631 | // |
| 632 | // We need to generate an OpenMP toolchain if the user specified targets with |
| 633 | // the -fopenmp-targets option. |
| 634 | if (Arg *OpenMPTargets = |
| 635 | C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) { |
| 636 | if (OpenMPTargets->getNumValues()) { |
| 637 | // We expect that -fopenmp-targets is always used in conjunction with the |
| 638 | // option -fopenmp specifying a valid runtime with offloading support, |
| 639 | // i.e. libomp or libiomp. |
| 640 | bool HasValidOpenMPRuntime = C.getInputArgs().hasFlag( |
| 641 | options::OPT_fopenmp, options::OPT_fopenmp_EQ, |
| 642 | options::OPT_fno_openmp, false); |
| 643 | if (HasValidOpenMPRuntime) { |
| 644 | OpenMPRuntimeKind OpenMPKind = getOpenMPRuntime(C.getInputArgs()); |
| 645 | HasValidOpenMPRuntime = |
| 646 | OpenMPKind == OMPRT_OMP || OpenMPKind == OMPRT_IOMP5; |
| 647 | } |
| 648 | |
| 649 | if (HasValidOpenMPRuntime) { |
| 650 | llvm::StringMap<const char *> FoundNormalizedTriples; |
| 651 | for (const char *Val : OpenMPTargets->getValues()) { |
| 652 | llvm::Triple TT(Val); |
| 653 | std::string NormalizedName = TT.normalize(); |
| 654 | |
| 655 | // Make sure we don't have a duplicate triple. |
| 656 | auto Duplicate = FoundNormalizedTriples.find(NormalizedName); |
| 657 | if (Duplicate != FoundNormalizedTriples.end()) { |
| 658 | Diag(clang::diag::warn_drv_omp_offload_target_duplicate) |
| 659 | << Val << Duplicate->second; |
| 660 | continue; |
| 661 | } |
| 662 | |
| 663 | // Store the current triple so that we can check for duplicates in the |
| 664 | // following iterations. |
| 665 | FoundNormalizedTriples[NormalizedName] = Val; |
| 666 | |
| 667 | // If the specified target is invalid, emit a diagnostic. |
| 668 | if (TT.getArch() == llvm::Triple::UnknownArch) |
| 669 | Diag(clang::diag::err_drv_invalid_omp_target) << Val; |
| 670 | else { |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 671 | const ToolChain *TC; |
| 672 | // CUDA toolchains have to be selected differently. They pair host |
| 673 | // and device in their implementation. |
| 674 | if (TT.isNVPTX()) { |
| 675 | const ToolChain *HostTC = |
| 676 | C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 677 | assert(HostTC && "Host toolchain should be always defined."); |
| 678 | auto &CudaTC = |
Gheorghe-Teodor Bercea | 5289843 | 2017-08-09 01:02:19 +0000 | [diff] [blame] | 679 | ToolChains[TT.str() + "/" + HostTC->getTriple().normalize()]; |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 680 | if (!CudaTC) |
| 681 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
Gheorghe-Teodor Bercea | 2c92693 | 2017-08-08 14:33:05 +0000 | [diff] [blame] | 682 | *this, TT, *HostTC, C.getInputArgs(), Action::OFK_OpenMP); |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 683 | TC = CudaTC.get(); |
| 684 | } else |
| 685 | TC = &getToolChain(C.getInputArgs(), TT); |
| 686 | C.addOffloadDeviceToolChain(TC, Action::OFK_OpenMP); |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | } else |
| 690 | Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets); |
| 691 | } else |
| 692 | Diag(clang::diag::warn_drv_empty_joined_argument) |
| 693 | << OpenMPTargets->getAsString(C.getInputArgs()); |
| 694 | } |
| 695 | |
| 696 | // |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 697 | // TODO: Add support for other offloading programming models here. |
| 698 | // |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 701 | /// Looks the given directories for the specified file. |
| 702 | /// |
| 703 | /// \param[out] FilePath File path, if the file was found. |
| 704 | /// \param[in] Dirs Directories used for the search. |
| 705 | /// \param[in] FileName Name of the file to search for. |
| 706 | /// \return True if file was found. |
| 707 | /// |
| 708 | /// Looks for file specified by FileName sequentially in directories specified |
| 709 | /// by Dirs. |
| 710 | /// |
| 711 | static bool searchForFile(SmallVectorImpl<char> &FilePath, |
| 712 | ArrayRef<std::string> Dirs, |
| 713 | StringRef FileName) { |
| 714 | SmallString<128> WPath; |
| 715 | for (const StringRef &Dir : Dirs) { |
| 716 | if (Dir.empty()) |
| 717 | continue; |
| 718 | WPath.clear(); |
| 719 | llvm::sys::path::append(WPath, Dir, FileName); |
| 720 | llvm::sys::path::native(WPath); |
| 721 | if (llvm::sys::fs::is_regular_file(WPath)) { |
| 722 | FilePath = std::move(WPath); |
| 723 | return true; |
| 724 | } |
| 725 | } |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | bool Driver::readConfigFile(StringRef FileName) { |
| 730 | // Try reading the given file. |
| 731 | SmallVector<const char *, 32> NewCfgArgs; |
| 732 | if (!llvm::cl::readConfigFile(FileName, Saver, NewCfgArgs)) { |
| 733 | Diag(diag::err_drv_cannot_read_config_file) << FileName; |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | // Read options from config file. |
| 738 | llvm::SmallString<128> CfgFileName(FileName); |
| 739 | llvm::sys::path::native(CfgFileName); |
| 740 | ConfigFile = CfgFileName.str(); |
| 741 | bool ContainErrors; |
| 742 | CfgOptions = llvm::make_unique<InputArgList>( |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 743 | ParseArgStrings(NewCfgArgs, IsCLMode(), ContainErrors)); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 744 | if (ContainErrors) { |
| 745 | CfgOptions.reset(); |
| 746 | return true; |
| 747 | } |
| 748 | |
| 749 | if (CfgOptions->hasArg(options::OPT_config)) { |
| 750 | CfgOptions.reset(); |
| 751 | Diag(diag::err_drv_nested_config_file); |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | // Claim all arguments that come from a configuration file so that the driver |
| 756 | // does not warn on any that is unused. |
| 757 | for (Arg *A : *CfgOptions) |
| 758 | A->claim(); |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | bool Driver::loadConfigFile() { |
| 763 | std::string CfgFileName; |
| 764 | bool FileSpecifiedExplicitly = false; |
| 765 | |
| 766 | // Process options that change search path for config files. |
| 767 | if (CLOptions) { |
| 768 | if (CLOptions->hasArg(options::OPT_config_system_dir_EQ)) { |
| 769 | SmallString<128> CfgDir; |
| 770 | CfgDir.append( |
| 771 | CLOptions->getLastArgValue(options::OPT_config_system_dir_EQ)); |
| 772 | if (!CfgDir.empty()) { |
Sam McCall | 296d832 | 2018-01-02 09:35:10 +0000 | [diff] [blame] | 773 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 774 | SystemConfigDir.clear(); |
| 775 | else |
| 776 | SystemConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 777 | } |
| 778 | } |
| 779 | if (CLOptions->hasArg(options::OPT_config_user_dir_EQ)) { |
| 780 | SmallString<128> CfgDir; |
| 781 | CfgDir.append( |
| 782 | CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ)); |
| 783 | if (!CfgDir.empty()) { |
Sam McCall | 296d832 | 2018-01-02 09:35:10 +0000 | [diff] [blame] | 784 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 785 | UserConfigDir.clear(); |
| 786 | else |
| 787 | UserConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | // First try to find config file specified in command line. |
| 793 | if (CLOptions) { |
| 794 | std::vector<std::string> ConfigFiles = |
| 795 | CLOptions->getAllArgValues(options::OPT_config); |
| 796 | if (ConfigFiles.size() > 1) { |
| 797 | Diag(diag::err_drv_duplicate_config); |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | if (!ConfigFiles.empty()) { |
| 802 | CfgFileName = ConfigFiles.front(); |
| 803 | assert(!CfgFileName.empty()); |
| 804 | |
| 805 | // If argument contains directory separator, treat it as a path to |
| 806 | // configuration file. |
| 807 | if (llvm::sys::path::has_parent_path(CfgFileName)) { |
| 808 | SmallString<128> CfgFilePath; |
| 809 | if (llvm::sys::path::is_relative(CfgFileName)) |
| 810 | llvm::sys::fs::current_path(CfgFilePath); |
| 811 | llvm::sys::path::append(CfgFilePath, CfgFileName); |
| 812 | if (!llvm::sys::fs::is_regular_file(CfgFilePath)) { |
| 813 | Diag(diag::err_drv_config_file_not_exist) << CfgFilePath; |
| 814 | return true; |
| 815 | } |
| 816 | return readConfigFile(CfgFilePath); |
| 817 | } |
| 818 | |
| 819 | FileSpecifiedExplicitly = true; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | // If config file is not specified explicitly, try to deduce configuration |
| 824 | // from executable name. For instance, an executable 'armv7l-clang' will |
| 825 | // search for config file 'armv7l-clang.cfg'. |
| 826 | if (CfgFileName.empty() && !ClangNameParts.TargetPrefix.empty()) |
| 827 | CfgFileName = ClangNameParts.TargetPrefix + '-' + ClangNameParts.ModeSuffix; |
| 828 | |
| 829 | if (CfgFileName.empty()) |
| 830 | return false; |
| 831 | |
| 832 | // Determine architecture part of the file name, if it is present. |
| 833 | StringRef CfgFileArch = CfgFileName; |
| 834 | size_t ArchPrefixLen = CfgFileArch.find('-'); |
| 835 | if (ArchPrefixLen == StringRef::npos) |
| 836 | ArchPrefixLen = CfgFileArch.size(); |
| 837 | llvm::Triple CfgTriple; |
| 838 | CfgFileArch = CfgFileArch.take_front(ArchPrefixLen); |
| 839 | CfgTriple = llvm::Triple(llvm::Triple::normalize(CfgFileArch)); |
| 840 | if (CfgTriple.getArch() == llvm::Triple::ArchType::UnknownArch) |
| 841 | ArchPrefixLen = 0; |
| 842 | |
| 843 | if (!StringRef(CfgFileName).endswith(".cfg")) |
| 844 | CfgFileName += ".cfg"; |
| 845 | |
| 846 | // If config file starts with architecture name and command line options |
| 847 | // redefine architecture (with options like -m32 -LE etc), try finding new |
| 848 | // config file with that architecture. |
| 849 | SmallString<128> FixedConfigFile; |
| 850 | size_t FixedArchPrefixLen = 0; |
| 851 | if (ArchPrefixLen) { |
| 852 | // Get architecture name from config file name like 'i386.cfg' or |
| 853 | // 'armv7l-clang.cfg'. |
| 854 | // Check if command line options changes effective triple. |
| 855 | llvm::Triple EffectiveTriple = computeTargetTriple(*this, |
| 856 | CfgTriple.getTriple(), *CLOptions); |
| 857 | if (CfgTriple.getArch() != EffectiveTriple.getArch()) { |
| 858 | FixedConfigFile = EffectiveTriple.getArchName(); |
| 859 | FixedArchPrefixLen = FixedConfigFile.size(); |
| 860 | // Append the rest of original file name so that file name transforms |
| 861 | // like: i386-clang.cfg -> x86_64-clang.cfg. |
| 862 | if (ArchPrefixLen < CfgFileName.size()) |
| 863 | FixedConfigFile += CfgFileName.substr(ArchPrefixLen); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // Prepare list of directories where config file is searched for. |
| 868 | SmallVector<std::string, 3> CfgFileSearchDirs; |
| 869 | CfgFileSearchDirs.push_back(UserConfigDir); |
| 870 | CfgFileSearchDirs.push_back(SystemConfigDir); |
| 871 | CfgFileSearchDirs.push_back(Dir); |
| 872 | |
| 873 | // Try to find config file. First try file with corrected architecture. |
| 874 | llvm::SmallString<128> CfgFilePath; |
| 875 | if (!FixedConfigFile.empty()) { |
| 876 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 877 | return readConfigFile(CfgFilePath); |
| 878 | // If 'x86_64-clang.cfg' was not found, try 'x86_64.cfg'. |
| 879 | FixedConfigFile.resize(FixedArchPrefixLen); |
| 880 | FixedConfigFile.append(".cfg"); |
| 881 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 882 | return readConfigFile(CfgFilePath); |
| 883 | } |
| 884 | |
| 885 | // Then try original file name. |
| 886 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 887 | return readConfigFile(CfgFilePath); |
| 888 | |
| 889 | // Finally try removing driver mode part: 'x86_64-clang.cfg' -> 'x86_64.cfg'. |
| 890 | if (!ClangNameParts.ModeSuffix.empty() && |
| 891 | !ClangNameParts.TargetPrefix.empty()) { |
| 892 | CfgFileName.assign(ClangNameParts.TargetPrefix); |
| 893 | CfgFileName.append(".cfg"); |
| 894 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 895 | return readConfigFile(CfgFilePath); |
| 896 | } |
| 897 | |
| 898 | // Report error but only if config file was specified explicitly, by option |
| 899 | // --config. If it was deduced from executable name, it is not an error. |
| 900 | if (FileSpecifiedExplicitly) { |
| 901 | Diag(diag::err_drv_config_file_not_found) << CfgFileName; |
| 902 | for (const std::string &SearchDir : CfgFileSearchDirs) |
| 903 | if (!SearchDir.empty()) |
| 904 | Diag(diag::note_drv_config_file_searched_in) << SearchDir; |
| 905 | return true; |
| 906 | } |
| 907 | |
| 908 | return false; |
| 909 | } |
| 910 | |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 911 | Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 912 | llvm::PrettyStackTraceString CrashInfo("Compilation construction"); |
| 913 | |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 914 | // FIXME: Handle environment options which affect driver behavior, somewhere |
Bill Wendling | adbeb9f | 2012-03-12 21:24:57 +0000 | [diff] [blame] | 915 | // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS. |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 916 | |
David Majnemer | 85c25b4 | 2016-07-24 17:44:03 +0000 | [diff] [blame] | 917 | if (Optional<std::string> CompilerPathValue = |
| 918 | llvm::sys::Process::GetEnv("COMPILER_PATH")) { |
| 919 | StringRef CompilerPath = *CompilerPathValue; |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 920 | while (!CompilerPath.empty()) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 921 | std::pair<StringRef, StringRef> Split = |
| 922 | CompilerPath.split(llvm::sys::EnvPathSeparator); |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 923 | PrefixDirs.push_back(Split.first); |
| 924 | CompilerPath = Split.second; |
| 925 | } |
| 926 | } |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 927 | |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 928 | // We look for the driver mode option early, because the mode can affect |
| 929 | // how other options are parsed. |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 930 | ParseDriverMode(ClangExecutable, ArgList.slice(1)); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 931 | |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 932 | // FIXME: What are we going to do with -V and -b? |
| 933 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 934 | // Arguments specified in command line. |
| 935 | bool ContainsError; |
| 936 | CLOptions = llvm::make_unique<InputArgList>( |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 937 | ParseArgStrings(ArgList.slice(1), IsCLMode(), ContainsError)); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 938 | |
| 939 | // Try parsing configuration file. |
| 940 | if (!ContainsError) |
| 941 | ContainsError = loadConfigFile(); |
| 942 | bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr); |
| 943 | |
| 944 | // All arguments, from both config file and command line. |
| 945 | InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions) |
| 946 | : std::move(*CLOptions)); |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 947 | |
| 948 | auto appendOneArg = [&Args](const Arg *Opt, const Arg *BaseArg) { |
Serge Pavlov | f015a56 | 2018-04-26 06:28:47 +0000 | [diff] [blame] | 949 | unsigned Index = Args.MakeIndex(Opt->getSpelling()); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 950 | Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Opt->getSpelling(), |
Serge Pavlov | f015a56 | 2018-04-26 06:28:47 +0000 | [diff] [blame] | 951 | Index, BaseArg); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 952 | Copy->getValues() = Opt->getValues(); |
| 953 | if (Opt->isClaimed()) |
| 954 | Copy->claim(); |
| 955 | Args.append(Copy); |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 956 | }; |
| 957 | |
| 958 | if (HasConfigFile) |
| 959 | for (auto *Opt : *CLOptions) { |
| 960 | if (Opt->getOption().matches(options::OPT_config)) |
| 961 | continue; |
| 962 | const Arg *BaseArg = &Opt->getBaseArg(); |
| 963 | if (BaseArg == Opt) |
| 964 | BaseArg = nullptr; |
| 965 | appendOneArg(Opt, BaseArg); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 968 | // In CL mode, look for any pass-through arguments |
| 969 | if (IsCLMode() && !ContainsError) { |
| 970 | SmallVector<const char *, 16> CLModePassThroughArgList; |
| 971 | for (const auto *A : Args.filtered(options::OPT__SLASH_clang)) { |
| 972 | A->claim(); |
| 973 | CLModePassThroughArgList.push_back(A->getValue()); |
| 974 | } |
| 975 | |
| 976 | if (!CLModePassThroughArgList.empty()) { |
| 977 | // Parse any pass through args using default clang processing rather |
| 978 | // than clang-cl processing. |
| 979 | auto CLModePassThroughOptions = llvm::make_unique<InputArgList>( |
| 980 | ParseArgStrings(CLModePassThroughArgList, false, ContainsError)); |
| 981 | |
| 982 | if (!ContainsError) |
| 983 | for (auto *Opt : *CLModePassThroughOptions) { |
| 984 | appendOneArg(Opt, nullptr); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 989 | // FIXME: This stuff needs to go into the Compilation, not the driver. |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 990 | bool CCCPrintPhases; |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 991 | |
Filipe Cabecinhas | 136f35e | 2015-07-18 06:35:24 +0000 | [diff] [blame] | 992 | // Silence driver warnings if requested |
| 993 | Diags.setIgnoreAllWarnings(Args.hasArg(options::OPT_w)); |
| 994 | |
Rafael Espindola | 59ae799 | 2009-12-07 18:28:29 +0000 | [diff] [blame] | 995 | // -no-canonical-prefixes is used very early in main. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 996 | Args.ClaimAllArgs(options::OPT_no_canonical_prefixes); |
Rafael Espindola | 59ae799 | 2009-12-07 18:28:29 +0000 | [diff] [blame] | 997 | |
Daniel Dunbar | 926f81f | 2010-08-02 02:38:03 +0000 | [diff] [blame] | 998 | // Ignore -pipe. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 999 | Args.ClaimAllArgs(options::OPT_pipe); |
Daniel Dunbar | 926f81f | 2010-08-02 02:38:03 +0000 | [diff] [blame] | 1000 | |
Daniel Dunbar | acd6957 | 2009-12-04 21:55:23 +0000 | [diff] [blame] | 1001 | // Extract -ccc args. |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 1002 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1003 | // FIXME: We need to figure out where this behavior should live. Most of it |
| 1004 | // should be outside in the client; the parts that aren't should have proper |
| 1005 | // options, either by introducing new ones or by overloading gcc ones like -V |
| 1006 | // or -b. |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1007 | CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1008 | CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings); |
| 1009 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1010 | CCCGenericGCCName = A->getValue(); |
Bruno Cardoso Lopes | 52dfe71 | 2017-04-12 21:46:20 +0000 | [diff] [blame] | 1011 | GenReproducer = Args.hasFlag(options::OPT_gen_reproducer, |
| 1012 | options::OPT_fno_crash_diagnostics, |
| 1013 | !!::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")); |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1014 | // FIXME: TargetTriple is used by the target-prefixed calls to as/ld |
Joerg Sonnenberger | 17d7551 | 2012-02-22 19:15:16 +0000 | [diff] [blame] | 1015 | // and getToolChain is const. |
Hans Wennborg | 2e27459 | 2013-08-13 23:38:57 +0000 | [diff] [blame] | 1016 | if (IsCLMode()) { |
Hans Wennborg | 73609a0 | 2014-03-28 01:19:04 +0000 | [diff] [blame] | 1017 | // clang-cl targets MSVC-style Win32. |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1018 | llvm::Triple T(TargetTriple); |
Hans Wennborg | 9d9ce7a | 2014-03-28 20:49:28 +0000 | [diff] [blame] | 1019 | T.setOS(llvm::Triple::Win32); |
Hans Wennborg | 0f0e8d6 | 2015-09-18 17:11:50 +0000 | [diff] [blame] | 1020 | T.setVendor(llvm::Triple::PC); |
Hans Wennborg | 9d9ce7a | 2014-03-28 20:49:28 +0000 | [diff] [blame] | 1021 | T.setEnvironment(llvm::Triple::MSVC); |
Oleg Ranevskyy | 8052318 | 2017-08-31 20:31:30 +0000 | [diff] [blame] | 1022 | T.setObjectFormat(llvm::Triple::COFF); |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1023 | TargetTriple = T.str(); |
Hans Wennborg | 2e27459 | 2013-08-13 23:38:57 +0000 | [diff] [blame] | 1024 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1025 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1026 | TargetTriple = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1027 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1028 | Dir = InstalledDir = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1029 | for (const Arg *A : Args.filtered(options::OPT_B)) { |
Benjamin Kramer | 1a648d1 | 2011-02-08 20:31:42 +0000 | [diff] [blame] | 1030 | A->claim(); |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1031 | PrefixDirs.push_back(A->getValue(0)); |
Benjamin Kramer | 1a648d1 | 2011-02-08 20:31:42 +0000 | [diff] [blame] | 1032 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1033 | if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1034 | SysRoot = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1035 | if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ)) |
Peter Collingbourne | 9d9e1fc | 2013-05-27 21:40:20 +0000 | [diff] [blame] | 1036 | DyldPrefix = A->getValue(); |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 1037 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1038 | if (const Arg *A = Args.getLastArg(options::OPT_resource_dir)) |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 1039 | ResourceDir = A->getValue(); |
Jim Grosbach | 061dabf | 2013-03-12 20:17:58 +0000 | [diff] [blame] | 1040 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1041 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) { |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 1042 | SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue()) |
| 1043 | .Case("cwd", SaveTempsCwd) |
| 1044 | .Case("obj", SaveTempsObj) |
| 1045 | .Default(SaveTempsCwd); |
| 1046 | } |
| 1047 | |
Steven Wu | 1257cd8 | 2016-05-18 17:04:52 +0000 | [diff] [blame] | 1048 | setLTOMode(Args); |
| 1049 | |
Steven Wu | 844ab6a | 2016-11-16 06:06:44 +0000 | [diff] [blame] | 1050 | // Process -fembed-bitcode= flags. |
| 1051 | if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) { |
| 1052 | StringRef Name = A->getValue(); |
| 1053 | unsigned Model = llvm::StringSwitch<unsigned>(Name) |
| 1054 | .Case("off", EmbedNone) |
| 1055 | .Case("all", EmbedBitcode) |
| 1056 | .Case("bitcode", EmbedBitcode) |
| 1057 | .Case("marker", EmbedMarker) |
| 1058 | .Default(~0U); |
| 1059 | if (Model == ~0U) { |
| 1060 | Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) |
| 1061 | << Name; |
| 1062 | } else |
| 1063 | BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model); |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1066 | std::unique_ptr<llvm::opt::InputArgList> UArgs = |
| 1067 | llvm::make_unique<InputArgList>(std::move(Args)); |
| 1068 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 1069 | // Perform the default argument translations. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1070 | DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs); |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 1071 | |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1072 | // Owned by the host. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 1073 | const ToolChain &TC = getToolChain( |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1074 | *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs)); |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1075 | |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 1076 | // The compilation takes ownership of Args. |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 1077 | Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs, |
| 1078 | ContainsError); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1079 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1080 | if (!HandleImmediateArgs(*C)) |
| 1081 | return C; |
| 1082 | |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1083 | // Construct the list of inputs. |
| 1084 | InputList Inputs; |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 1085 | BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs); |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1086 | |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 1087 | // Populate the tool chains for the offloading devices, if any. |
| 1088 | CreateOffloadingDeviceToolChains(*C, Inputs); |
Justin Lebar | 0e450a5 | 2016-03-30 23:30:25 +0000 | [diff] [blame] | 1089 | |
Chandler Carruth | 7f1417f | 2012-01-24 10:43:44 +0000 | [diff] [blame] | 1090 | // Construct the list of abstract actions to perform for this compilation. On |
Tim Northover | 157d911 | 2014-01-16 08:48:16 +0000 | [diff] [blame] | 1091 | // MachO targets this uses the driver-driver and universal actions. |
| 1092 | if (TC.getTriple().isOSBinFormatMachO()) |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1093 | BuildUniversalActions(*C, C->getDefaultToolChain(), Inputs); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1094 | else |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1095 | BuildActions(*C, C->getArgs(), Inputs, C->getActions()); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1096 | |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1097 | if (CCCPrintPhases) { |
Daniel Dunbar | eb843be | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 1098 | PrintActions(*C); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1099 | return C; |
| 1100 | } |
| 1101 | |
| 1102 | BuildJobs(*C); |
Daniel Dunbar | adc91e6 | 2009-03-15 01:38:15 +0000 | [diff] [blame] | 1103 | |
| 1104 | return C; |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Justin Bogner | ed9cbe0 | 2015-07-09 06:58:31 +0000 | [diff] [blame] | 1107 | static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) { |
| 1108 | llvm::opt::ArgStringList ASL; |
| 1109 | for (const auto *A : Args) |
| 1110 | A->render(Args, ASL); |
| 1111 | |
| 1112 | for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) { |
| 1113 | if (I != ASL.begin()) |
| 1114 | OS << ' '; |
| 1115 | Command::printArg(OS, *I, true); |
| 1116 | } |
| 1117 | OS << '\n'; |
| 1118 | } |
| 1119 | |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1120 | bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename, |
| 1121 | SmallString<128> &CrashDiagDir) { |
| 1122 | using namespace llvm::sys; |
| 1123 | assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() && |
| 1124 | "Only knows about .crash files on Darwin"); |
| 1125 | |
| 1126 | // The .crash file can be found on at ~/Library/Logs/DiagnosticReports/ |
| 1127 | // (or /Library/Logs/DiagnosticReports for root) and has the filename pattern |
| 1128 | // clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash. |
| 1129 | path::home_directory(CrashDiagDir); |
| 1130 | if (CrashDiagDir.startswith("/var/root")) |
| 1131 | CrashDiagDir = "/"; |
| 1132 | path::append(CrashDiagDir, "Library/Logs/DiagnosticReports"); |
| 1133 | int PID = |
| 1134 | #if LLVM_ON_UNIX |
| 1135 | getpid(); |
| 1136 | #else |
| 1137 | 0; |
| 1138 | #endif |
| 1139 | std::error_code EC; |
| 1140 | fs::file_status FileStatus; |
| 1141 | TimePoint<> LastAccessTime; |
| 1142 | SmallString<128> CrashFilePath; |
| 1143 | // Lookup the .crash files and get the one generated by a subprocess spawned |
| 1144 | // by this driver invocation. |
| 1145 | for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd; |
| 1146 | File != FileEnd && !EC; File.increment(EC)) { |
| 1147 | StringRef FileName = path::filename(File->path()); |
| 1148 | if (!FileName.startswith(Name)) |
| 1149 | continue; |
| 1150 | if (fs::status(File->path(), FileStatus)) |
| 1151 | continue; |
| 1152 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile = |
| 1153 | llvm::MemoryBuffer::getFile(File->path()); |
| 1154 | if (!CrashFile) |
| 1155 | continue; |
| 1156 | // The first line should start with "Process:", otherwise this isn't a real |
| 1157 | // .crash file. |
| 1158 | StringRef Data = CrashFile.get()->getBuffer(); |
| 1159 | if (!Data.startswith("Process:")) |
| 1160 | continue; |
| 1161 | // Parse parent process pid line, e.g: "Parent Process: clang-4.0 [79141]" |
| 1162 | size_t ParentProcPos = Data.find("Parent Process:"); |
| 1163 | if (ParentProcPos == StringRef::npos) |
| 1164 | continue; |
| 1165 | size_t LineEnd = Data.find_first_of("\n", ParentProcPos); |
| 1166 | if (LineEnd == StringRef::npos) |
| 1167 | continue; |
| 1168 | StringRef ParentProcess = Data.slice(ParentProcPos+15, LineEnd).trim(); |
| 1169 | int OpenBracket = -1, CloseBracket = -1; |
| 1170 | for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) { |
| 1171 | if (ParentProcess[i] == '[') |
| 1172 | OpenBracket = i; |
| 1173 | if (ParentProcess[i] == ']') |
| 1174 | CloseBracket = i; |
| 1175 | } |
| 1176 | // Extract the parent process PID from the .crash file and check whether |
| 1177 | // it matches this driver invocation pid. |
| 1178 | int CrashPID; |
| 1179 | if (OpenBracket < 0 || CloseBracket < 0 || |
| 1180 | ParentProcess.slice(OpenBracket + 1, CloseBracket) |
| 1181 | .getAsInteger(10, CrashPID) || CrashPID != PID) { |
| 1182 | continue; |
| 1183 | } |
| 1184 | |
| 1185 | // Found a .crash file matching the driver pid. To avoid getting an older |
| 1186 | // and misleading crash file, continue looking for the most recent. |
| 1187 | // FIXME: the driver can dispatch multiple cc1 invocations, leading to |
| 1188 | // multiple crashes poiting to the same parent process. Since the driver |
| 1189 | // does not collect pid information for the dispatched invocation there's |
| 1190 | // currently no way to distinguish among them. |
| 1191 | const auto FileAccessTime = FileStatus.getLastModificationTime(); |
| 1192 | if (FileAccessTime > LastAccessTime) { |
| 1193 | CrashFilePath.assign(File->path()); |
| 1194 | LastAccessTime = FileAccessTime; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | // If found, copy it over to the location of other reproducer files. |
| 1199 | if (!CrashFilePath.empty()) { |
| 1200 | EC = fs::copy_file(CrashFilePath, ReproCrashFilename); |
| 1201 | if (EC) |
| 1202 | return false; |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 1209 | // When clang crashes, produce diagnostic information including the fully |
| 1210 | // preprocessed source file(s). Request that the developer attach the |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1211 | // diagnostic information to a bug report. |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1212 | void Driver::generateCompilationDiagnostics( |
| 1213 | Compilation &C, const Command &FailingCommand, |
| 1214 | StringRef AdditionalInformation, CompilationDiagnosticReport *Report) { |
Chad Rosier | 877c0a2 | 2012-02-22 00:30:39 +0000 | [diff] [blame] | 1215 | if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics)) |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 1216 | return; |
Chad Rosier | bee5a1d | 2012-03-07 00:30:40 +0000 | [diff] [blame] | 1217 | |
Chad Rosier | dbf46a1 | 2013-02-01 18:30:26 +0000 | [diff] [blame] | 1218 | // Don't try to generate diagnostics for link or dsymutil jobs. |
Justin Bogner | e1a33d1 | 2014-10-20 21:02:05 +0000 | [diff] [blame] | 1219 | if (FailingCommand.getCreator().isLinkJob() || |
| 1220 | FailingCommand.getCreator().isDsymutilJob()) |
Chad Rosier | 877c0a2 | 2012-02-22 00:30:39 +0000 | [diff] [blame] | 1221 | return; |
| 1222 | |
Chad Rosier | 8179f11 | 2012-06-19 17:51:34 +0000 | [diff] [blame] | 1223 | // Print the version of the compiler. |
| 1224 | PrintVersion(C, llvm::errs()); |
| 1225 | |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1226 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1227 | << "PLEASE submit a bug report to " BUG_REPORT_URL " and include the " |
| 1228 | "crash backtrace, preprocessed source, and associated run script."; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1229 | |
| 1230 | // Suppress driver output and emit preprocessor output to temp file. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 1231 | Mode = CPPMode; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1232 | CCGenDiagnostics = true; |
| 1233 | |
Chad Rosier | cdb008d | 2011-11-02 21:29:05 +0000 | [diff] [blame] | 1234 | // Save the original job command(s). |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 1235 | Command Cmd = FailingCommand; |
Chad Rosier | cdb008d | 2011-11-02 21:29:05 +0000 | [diff] [blame] | 1236 | |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 1237 | // Keep track of whether we produce any errors while trying to produce |
| 1238 | // preprocessed sources. |
| 1239 | DiagnosticErrorTrap Trap(Diags); |
| 1240 | |
| 1241 | // Suppress tool output. |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1242 | C.initCompilationForDiagnostics(); |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Construct the list of inputs. |
| 1245 | InputList Inputs; |
| 1246 | BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1247 | |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1248 | for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) { |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1249 | bool IgnoreInput = false; |
| 1250 | |
| 1251 | // Ignore input from stdin or any inputs that cannot be preprocessed. |
Paul Robinson | f44157d | 2014-04-28 22:24:44 +0000 | [diff] [blame] | 1252 | // Check type first as not all linker inputs have a value. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1253 | if (types::getPreprocessedType(it->first) == types::TY_INVALID) { |
Paul Robinson | f44157d | 2014-04-28 22:24:44 +0000 | [diff] [blame] | 1254 | IgnoreInput = true; |
| 1255 | } else if (!strcmp(it->second->getValue(), "-")) { |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1256 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1257 | << "Error generating preprocessed source(s) - " |
| 1258 | "ignoring input from stdin."; |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1259 | IgnoreInput = true; |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | if (IgnoreInput) { |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1263 | it = Inputs.erase(it); |
| 1264 | ie = Inputs.end(); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1265 | } else { |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1266 | ++it; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1267 | } |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1268 | } |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1269 | |
Chad Rosier | c5103c3 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 1270 | if (Inputs.empty()) { |
| 1271 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1272 | << "Error generating preprocessed source(s) - " |
| 1273 | "no preprocessable inputs."; |
Chad Rosier | c5103c3 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 1274 | return; |
| 1275 | } |
| 1276 | |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1277 | // Don't attempt to generate preprocessed files if multiple -arch options are |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1278 | // used, unless they're all duplicates. |
| 1279 | llvm::StringSet<> ArchNames; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 1280 | for (const Arg *A : C.getArgs()) { |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1281 | if (A->getOption().matches(options::OPT_arch)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1282 | StringRef ArchName = A->getValue(); |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1283 | ArchNames.insert(ArchName); |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1284 | } |
| 1285 | } |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1286 | if (ArchNames.size() > 1) { |
| 1287 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1288 | << "Error generating preprocessed source(s) - cannot generate " |
| 1289 | "preprocessed source with multiple -arch options."; |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1290 | return; |
| 1291 | } |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1292 | |
Chandler Carruth | 7f1417f | 2012-01-24 10:43:44 +0000 | [diff] [blame] | 1293 | // Construct the list of abstract actions to perform for this compilation. On |
| 1294 | // Darwin OSes this uses the driver-driver and builds universal actions. |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1295 | const ToolChain &TC = C.getDefaultToolChain(); |
Tim Northover | 157d911 | 2014-01-16 08:48:16 +0000 | [diff] [blame] | 1296 | if (TC.getTriple().isOSBinFormatMachO()) |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1297 | BuildUniversalActions(C, TC, Inputs); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1298 | else |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1299 | BuildActions(C, C.getArgs(), Inputs, C.getActions()); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1300 | |
| 1301 | BuildJobs(C); |
| 1302 | |
| 1303 | // If there were errors building the compilation, quit now. |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 1304 | if (Trap.hasErrorOccurred()) { |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1305 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1306 | << "Error generating preprocessed source(s)."; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | // Generate preprocessed output. |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1311 | SmallVector<std::pair<int, const Command *>, 4> FailingCommands; |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1312 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1313 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1314 | // If any of the preprocessing commands failed, clean up and exit. |
| 1315 | if (!FailingCommands.empty()) { |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1316 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1317 | << "Error generating preprocessed source(s)."; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1318 | return; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1319 | } |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1320 | |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1321 | const ArgStringList &TempFiles = C.getTempFiles(); |
| 1322 | if (TempFiles.empty()) { |
| 1323 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1324 | << "Error generating preprocessed source(s)."; |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | } |
| 1327 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1328 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1329 | << "\n********************\n\n" |
| 1330 | "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n" |
| 1331 | "Preprocessed source(s) and associated run script(s) are located at:"; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1332 | |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1333 | SmallString<128> VFS; |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1334 | SmallString<128> ReproCrashFilename; |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1335 | for (const char *TempFile : TempFiles) { |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1336 | Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile; |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1337 | if (Report) |
| 1338 | Report->TemporaryFiles.push_back(TempFile); |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1339 | if (ReproCrashFilename.empty()) { |
| 1340 | ReproCrashFilename = TempFile; |
| 1341 | llvm::sys::path::replace_extension(ReproCrashFilename, ".crash"); |
| 1342 | } |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1343 | if (StringRef(TempFile).endswith(".cache")) { |
| 1344 | // In some cases (modules) we'll dump extra data to help with reproducing |
| 1345 | // the crash into a directory next to the output. |
| 1346 | VFS = llvm::sys::path::filename(TempFile); |
| 1347 | llvm::sys::path::append(VFS, "vfs", "vfs.yaml"); |
| 1348 | } |
| 1349 | } |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1350 | |
| 1351 | // Assume associated files are based off of the first temporary file. |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 1352 | CrashReportInfo CrashInfo(TempFiles[0], VFS); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1353 | |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1354 | llvm::SmallString<128> Script(CrashInfo.Filename); |
| 1355 | llvm::sys::path::replace_extension(Script, "sh"); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1356 | std::error_code EC; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1357 | llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1358 | if (EC) { |
| 1359 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1360 | << "Error generating run script: " << Script << " " << EC.message(); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1361 | } else { |
Justin Bogner | 42220ed | 2015-03-12 00:14:35 +0000 | [diff] [blame] | 1362 | ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n" |
Justin Bogner | ed9cbe0 | 2015-07-09 06:58:31 +0000 | [diff] [blame] | 1363 | << "# Driver args: "; |
| 1364 | printArgList(ScriptOS, C.getInputArgs()); |
| 1365 | ScriptOS << "# Original command: "; |
Justin Bogner | 42220ed | 2015-03-12 00:14:35 +0000 | [diff] [blame] | 1366 | Cmd.Print(ScriptOS, "\n", /*Quote=*/true); |
Justin Bogner | 33bdbc6 | 2014-10-21 18:03:08 +0000 | [diff] [blame] | 1367 | Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo); |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1368 | if (!AdditionalInformation.empty()) |
| 1369 | ScriptOS << "\n# Additional information: " << AdditionalInformation |
| 1370 | << "\n"; |
| 1371 | if (Report) |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1372 | Report->TemporaryFiles.push_back(Script.str()); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1373 | Diag(clang::diag::note_drv_command_failed_diag_msg) << Script; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1374 | } |
Saleem Abdulrasool | 3661a82 | 2015-01-12 02:33:09 +0000 | [diff] [blame] | 1375 | |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1376 | // On darwin, provide information about the .crash diagnostic report. |
| 1377 | if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) { |
| 1378 | SmallString<128> CrashDiagDir; |
| 1379 | if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) { |
| 1380 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1381 | << ReproCrashFilename.str(); |
| 1382 | } else { // Suggest a directory for the user to look for .crash files. |
| 1383 | llvm::sys::path::append(CrashDiagDir, Name); |
| 1384 | CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash"; |
| 1385 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1386 | << "Crash backtrace is located in"; |
| 1387 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1388 | << CrashDiagDir.str(); |
| 1389 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1390 | << "(choose the .crash file that corresponds to your crash)"; |
| 1391 | } |
| 1392 | } |
| 1393 | |
Saleem Abdulrasool | 3661a82 | 2015-01-12 02:33:09 +0000 | [diff] [blame] | 1394 | for (const auto &A : C.getArgs().filtered(options::OPT_frewrite_map_file, |
| 1395 | options::OPT_frewrite_map_file_EQ)) |
| 1396 | Diag(clang::diag::note_drv_command_failed_diag_msg) << A->getValue(); |
| 1397 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1398 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1399 | << "\n\n********************"; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1402 | void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) { |
Oleg Ranevskyy | cd51637 | 2016-01-05 19:54:39 +0000 | [diff] [blame] | 1403 | // Since commandLineFitsWithinSystemLimits() may underestimate system's capacity |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1404 | // if the tool does not support response files, there is a chance/ that things |
| 1405 | // 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] | 1406 | if (Cmd.getCreator().getResponseFilesSupport() == Tool::RF_None || |
Oleg Ranevskyy | cd51637 | 2016-01-05 19:54:39 +0000 | [diff] [blame] | 1407 | llvm::sys::commandLineFitsWithinSystemLimits(Cmd.getExecutable(), Cmd.getArguments())) |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1408 | return; |
| 1409 | |
| 1410 | std::string TmpName = GetTemporaryPath("response", "txt"); |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 1411 | Cmd.setResponseFile(C.addTempFile(C.getArgs().MakeArgString(TmpName))); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1414 | int Driver::ExecuteCompilation( |
| 1415 | Compilation &C, |
| 1416 | SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) { |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1417 | // Just print if -### was present. |
| 1418 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 1419 | C.getJobs().Print(llvm::errs(), "\n", true); |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | // If there were errors building the compilation, quit now. |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1424 | if (Diags.hasErrorOccurred()) |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1425 | return 1; |
| 1426 | |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1427 | // Set up response file names for each command, if necessary |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1428 | for (auto &Job : C.getJobs()) |
| 1429 | setUpResponseFiles(C, Job); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1430 | |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1431 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1432 | |
Daniel Dunbar | 0749479 | 2010-05-22 00:37:20 +0000 | [diff] [blame] | 1433 | // If the command succeeded, we are done. |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1434 | if (FailingCommands.empty()) |
| 1435 | return 0; |
Daniel Dunbar | 0749479 | 2010-05-22 00:37:20 +0000 | [diff] [blame] | 1436 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1437 | // Otherwise, remove result files and print extra information about abnormal |
| 1438 | // failures. |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1439 | int Res = 0; |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 1440 | for (const auto &CmdPair : FailingCommands) { |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1441 | int CommandRes = CmdPair.first; |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 1442 | const Command *FailingCommand = CmdPair.second; |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1443 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1444 | // Remove result files if we're not saving temps. |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 1445 | if (!isSaveTempsEnabled()) { |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1446 | const JobAction *JA = cast<JobAction>(&FailingCommand->getSource()); |
| 1447 | C.CleanupFileMap(C.getResultFiles(), JA, true); |
| 1448 | |
| 1449 | // Failure result files are valid unless we crashed. |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1450 | if (CommandRes < 0) |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1451 | C.CleanupFileMap(C.getFailureResultFiles(), JA, true); |
| 1452 | } |
| 1453 | |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1454 | #if LLVM_ON_UNIX |
| 1455 | // llvm/lib/Support/Unix/Signals.inc will exit with a special return code |
| 1456 | // for SIGPIPE. Do not print diagnostics for this case. |
| 1457 | if (CommandRes == EX_IOERR) { |
| 1458 | Res = CommandRes; |
| 1459 | continue; |
| 1460 | } |
| 1461 | #endif |
| 1462 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1463 | // Print extra information about abnormal failures, if possible. |
| 1464 | // |
| 1465 | // 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] | 1466 | // status was 1, assume the command failed normally. In particular, if it |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1467 | // was the compiler then assume it gave a reasonable error code. Failures |
| 1468 | // in other tools are less common, and they generally have worse |
| 1469 | // diagnostics, so always print the diagnostic there. |
| 1470 | const Tool &FailingTool = FailingCommand->getCreator(); |
| 1471 | |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1472 | if (!FailingCommand->getCreator().hasGoodDiagnostics() || CommandRes != 1) { |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1473 | // FIXME: See FIXME above regarding result code interpretation. |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1474 | if (CommandRes < 0) |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1475 | Diag(clang::diag::err_drv_command_signalled) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1476 | << FailingTool.getShortName(); |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1477 | else |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1478 | Diag(clang::diag::err_drv_command_failed) |
| 1479 | << FailingTool.getShortName() << CommandRes; |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1480 | } |
Peter Collingbourne | 119cfaa | 2011-11-21 00:01:05 +0000 | [diff] [blame] | 1481 | } |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1482 | return Res; |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Daniel Dunbar | a7b5e21 | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 1485 | void Driver::PrintHelp(bool ShowHidden) const { |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 1486 | unsigned IncludedFlagsBitmask; |
| 1487 | unsigned ExcludedFlagsBitmask; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1488 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 1489 | getIncludeExcludeOptionFlagMasks(IsCLMode()); |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 1490 | |
| 1491 | ExcludedFlagsBitmask |= options::NoDriverOption; |
| 1492 | if (!ShowHidden) |
| 1493 | ExcludedFlagsBitmask |= HelpHidden; |
| 1494 | |
Fangrui Song | d002823 | 2018-10-10 00:15:33 +0000 | [diff] [blame] | 1495 | std::string Usage = llvm::formatv("{0} [options] file...", Name).str(); |
| 1496 | getOpts().PrintHelp(llvm::outs(), Usage.c_str(), DriverTitle.c_str(), |
George Rimar | 5dbfa4e | 2017-07-26 09:10:17 +0000 | [diff] [blame] | 1497 | IncludedFlagsBitmask, ExcludedFlagsBitmask, |
| 1498 | /*ShowAllAliases=*/false); |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1501 | void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1502 | // FIXME: The following handlers should use a callback mechanism, we don't |
| 1503 | // know what the client would like to do. |
Ted Kremenek | 4c0df3d | 2010-01-23 02:11:34 +0000 | [diff] [blame] | 1504 | OS << getClangFullVersion() << '\n'; |
Daniel Dunbar | b1024880 | 2009-03-26 16:09:13 +0000 | [diff] [blame] | 1505 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1506 | OS << "Target: " << TC.getTripleString() << '\n'; |
Daniel Dunbar | 10978e4 | 2009-06-16 23:32:58 +0000 | [diff] [blame] | 1507 | |
| 1508 | // Print the threading model. |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 1509 | if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) { |
| 1510 | // Don't print if the ToolChain would have barfed on it already |
| 1511 | if (TC.isThreadModelSupported(A->getValue())) |
| 1512 | OS << "Thread model: " << A->getValue(); |
| 1513 | } else |
| 1514 | OS << "Thread model: " << TC.getThreadModel(); |
| 1515 | OS << '\n'; |
Chandler Carruth | 9ade6a9 | 2015-08-05 17:07:33 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Print out the install directory. |
| 1518 | OS << "InstalledDir: " << InstalledDir << '\n'; |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 1519 | |
| 1520 | // If configuration file was used, print its path. |
| 1521 | if (!ConfigFile.empty()) |
| 1522 | OS << "Configuration file: " << ConfigFile << '\n'; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1525 | /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories |
| 1526 | /// option. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1527 | static void PrintDiagnosticCategories(raw_ostream &OS) { |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1528 | // Skip the empty category. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1529 | for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max; |
| 1530 | ++i) |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1531 | OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n'; |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1534 | void Driver::HandleAutocompletions(StringRef PassedFlags) const { |
Yuka Takahashi | 503da8f | 2018-03-05 09:01:31 +0000 | [diff] [blame] | 1535 | if (PassedFlags == "") |
| 1536 | return; |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1537 | // Print out all options that start with a given argument. This is used for |
| 1538 | // shell autocompletion. |
| 1539 | std::vector<std::string> SuggestedCompletions; |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1540 | std::vector<std::string> Flags; |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1541 | |
| 1542 | unsigned short DisableFlags = |
| 1543 | options::NoDriverOption | options::Unsupported | options::Ignored; |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1544 | |
Yuka Takahashi | 46106f5 | 2018-10-24 12:43:25 +0000 | [diff] [blame] | 1545 | // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag," |
| 1546 | // because the latter indicates that the user put space before pushing tab |
| 1547 | // which should end up in a file completion. |
| 1548 | const bool HasSpace = PassedFlags.endswith(","); |
| 1549 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1550 | // Parse PassedFlags by "," as all the command-line flags are passed to this |
| 1551 | // function separated by "," |
| 1552 | StringRef TargetFlags = PassedFlags; |
| 1553 | while (TargetFlags != "") { |
| 1554 | StringRef CurFlag; |
| 1555 | std::tie(CurFlag, TargetFlags) = TargetFlags.split(","); |
| 1556 | Flags.push_back(std::string(CurFlag)); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1559 | // We want to show cc1-only options only when clang is invoked with -cc1 or |
| 1560 | // -Xclang. |
Yuka Takahashi | 503da8f | 2018-03-05 09:01:31 +0000 | [diff] [blame] | 1561 | if (std::find(Flags.begin(), Flags.end(), "-Xclang") != Flags.end() || |
| 1562 | std::find(Flags.begin(), Flags.end(), "-cc1") != Flags.end()) |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1563 | DisableFlags &= ~options::NoDriverOption; |
| 1564 | |
| 1565 | StringRef Cur; |
| 1566 | Cur = Flags.at(Flags.size() - 1); |
| 1567 | StringRef Prev; |
| 1568 | if (Flags.size() >= 2) { |
| 1569 | Prev = Flags.at(Flags.size() - 2); |
| 1570 | SuggestedCompletions = Opts->suggestValueCompletions(Prev, Cur); |
| 1571 | } |
| 1572 | |
| 1573 | if (SuggestedCompletions.empty()) |
| 1574 | SuggestedCompletions = Opts->suggestValueCompletions(Cur, ""); |
| 1575 | |
Yuka Takahashi | 46106f5 | 2018-10-24 12:43:25 +0000 | [diff] [blame] | 1576 | // If Flags were empty, it means the user typed `clang [tab]` where we should |
| 1577 | // list all possible flags. If there was no value completion and the user |
| 1578 | // pressed tab after a space, we should fall back to a file completion. |
| 1579 | // We're printing a newline to be consistent with what we print at the end of |
| 1580 | // this function. |
| 1581 | if (SuggestedCompletions.empty() && HasSpace && !Flags.empty()) { |
| 1582 | llvm::outs() << '\n'; |
| 1583 | return; |
| 1584 | } |
| 1585 | |
Yuka Takahashi | df9c7e3 | 2018-10-24 08:24:16 +0000 | [diff] [blame] | 1586 | // When flag ends with '=' and there was no value completion, return empty |
| 1587 | // string and fall back to the file autocompletion. |
| 1588 | if (SuggestedCompletions.empty() && !Cur.endswith("=")) { |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1589 | // If the flag is in the form of "--autocomplete=-foo", |
| 1590 | // we were requested to print out all option names that start with "-foo". |
| 1591 | // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only". |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1592 | SuggestedCompletions = Opts->findByPrefix(Cur, DisableFlags); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1593 | |
| 1594 | // We have to query the -W flags manually as they're not in the OptTable. |
| 1595 | // TODO: Find a good way to add them to OptTable instead and them remove |
| 1596 | // this code. |
| 1597 | for (StringRef S : DiagnosticIDs::getDiagnosticFlags()) |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1598 | if (S.startswith(Cur)) |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1599 | SuggestedCompletions.push_back(S); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | // Sort the autocomplete candidates so that shells print them out in a |
| 1603 | // deterministic order. We could sort in any way, but we chose |
| 1604 | // case-insensitive sorting for consistency with the -help option |
| 1605 | // which prints out options in the case-insensitive alphabetical order. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 1606 | llvm::sort(SuggestedCompletions, [](StringRef A, StringRef B) { |
| 1607 | if (int X = A.compare_lower(B)) |
| 1608 | return X < 0; |
| 1609 | return A.compare(B) > 0; |
| 1610 | }); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1611 | |
| 1612 | llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n'; |
| 1613 | } |
| 1614 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1615 | bool Driver::HandleImmediateArgs(const Compilation &C) { |
Daniel Dunbar | 18974bd | 2010-06-11 22:00:19 +0000 | [diff] [blame] | 1616 | // 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] | 1617 | // don't expect inconsistencies w.r.t. that to matter in practice. |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1618 | |
Daniel Dunbar | 1b09e04 | 2010-09-17 02:47:28 +0000 | [diff] [blame] | 1619 | if (C.getArgs().hasArg(options::OPT_dumpmachine)) { |
| 1620 | llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n'; |
| 1621 | return false; |
| 1622 | } |
| 1623 | |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1624 | if (C.getArgs().hasArg(options::OPT_dumpversion)) { |
Daniel Dunbar | e26e500 | 2011-01-12 00:43:47 +0000 | [diff] [blame] | 1625 | // Since -dumpversion is only implemented for pedantic GCC compatibility, we |
| 1626 | // return an answer which matches our definition of __VERSION__. |
| 1627 | // |
| 1628 | // If we want to return a more correct answer some day, then we should |
| 1629 | // introduce a non-pedantically GCC compatible mode to Clang in which we |
| 1630 | // provide sensible definitions for -dumpversion, __VERSION__, etc. |
| 1631 | llvm::outs() << "4.2.1\n"; |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1632 | return false; |
| 1633 | } |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1635 | if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) { |
| 1636 | PrintDiagnosticCategories(llvm::outs()); |
| 1637 | return false; |
| 1638 | } |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1639 | |
James Molloy | a3c85b8 | 2012-05-01 14:57:16 +0000 | [diff] [blame] | 1640 | if (C.getArgs().hasArg(options::OPT_help) || |
Daniel Dunbar | a7b5e21 | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 1641 | C.getArgs().hasArg(options::OPT__help_hidden)) { |
| 1642 | PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1643 | return false; |
| 1644 | } |
| 1645 | |
Daniel Dunbar | b0006ae | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 1646 | if (C.getArgs().hasArg(options::OPT__version)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1647 | // Follow gcc behavior and use stdout for --version and stderr for -v. |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1648 | PrintVersion(C, llvm::outs()); |
Daniel Dunbar | b0006ae | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 1649 | return false; |
| 1650 | } |
| 1651 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1652 | if (C.getArgs().hasArg(options::OPT_v) || |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1653 | C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1654 | PrintVersion(C, llvm::errs()); |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1655 | SuppressMissingInputWarning = true; |
| 1656 | } |
| 1657 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 1658 | if (C.getArgs().hasArg(options::OPT_v)) { |
| 1659 | if (!SystemConfigDir.empty()) |
| 1660 | llvm::errs() << "System configuration file directory: " |
| 1661 | << SystemConfigDir << "\n"; |
| 1662 | if (!UserConfigDir.empty()) |
| 1663 | llvm::errs() << "User configuration file directory: " |
| 1664 | << UserConfigDir << "\n"; |
| 1665 | } |
| 1666 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1667 | const ToolChain &TC = C.getDefaultToolChain(); |
Chandler Carruth | 0ae39aa | 2013-07-30 17:57:09 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (C.getArgs().hasArg(options::OPT_v)) |
| 1670 | TC.printVerboseInfo(llvm::errs()); |
| 1671 | |
Meador Inge | 51208a3 | 2017-04-04 21:46:50 +0000 | [diff] [blame] | 1672 | if (C.getArgs().hasArg(options::OPT_print_resource_dir)) { |
Meador Inge | a911395 | 2017-04-05 22:27:20 +0000 | [diff] [blame] | 1673 | llvm::outs() << ResourceDir << '\n'; |
Meador Inge | 51208a3 | 2017-04-04 21:46:50 +0000 | [diff] [blame] | 1674 | return false; |
| 1675 | } |
| 1676 | |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1677 | if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { |
| 1678 | llvm::outs() << "programs: ="; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1679 | bool separator = false; |
| 1680 | for (const std::string &Path : TC.getProgramPaths()) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1681 | if (separator) |
| 1682 | llvm::outs() << ':'; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1683 | llvm::outs() << Path; |
| 1684 | separator = true; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1685 | } |
| 1686 | llvm::outs() << "\n"; |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 1687 | llvm::outs() << "libraries: =" << ResourceDir; |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1688 | |
Sebastian Pop | 980920a | 2012-04-16 04:16:43 +0000 | [diff] [blame] | 1689 | StringRef sysroot = C.getSysRoot(); |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1690 | |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1691 | for (const std::string &Path : TC.getFilePaths()) { |
| 1692 | // Always print a separator. ResourceDir was the first item shown. |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 1693 | llvm::outs() << ':'; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1694 | // Interpretation of leading '=' is needed only for NetBSD. |
| 1695 | if (Path[0] == '=') |
Douglas Katzman | 26eabf6 | 2015-06-24 15:10:30 +0000 | [diff] [blame] | 1696 | llvm::outs() << sysroot << Path.substr(1); |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1697 | else |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1698 | llvm::outs() << Path; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1699 | } |
| 1700 | llvm::outs() << "\n"; |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1701 | return false; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1704 | // FIXME: The following handlers should use a callback mechanism, we don't |
| 1705 | // know what the client would like to do. |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1706 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1707 | llvm::outs() << GetFilePath(A->getValue(), TC) << "\n"; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1708 | return false; |
| 1709 | } |
| 1710 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1711 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { |
Saleem Abdulrasool | e5f3cf8 | 2018-05-01 18:40:42 +0000 | [diff] [blame] | 1712 | StringRef ProgName = A->getValue(); |
| 1713 | |
| 1714 | // Null program name cannot have a path. |
| 1715 | if (! ProgName.empty()) |
| 1716 | llvm::outs() << GetProgramPath(ProgName, TC); |
| 1717 | |
| 1718 | llvm::outs() << "\n"; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1719 | return false; |
| 1720 | } |
| 1721 | |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 1722 | if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) { |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 1723 | StringRef PassedFlags = A->getValue(); |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1724 | HandleAutocompletions(PassedFlags); |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 1725 | return false; |
| 1726 | } |
| 1727 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1728 | if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { |
Michal Gorny | 7cfe480 | 2016-10-10 12:23:40 +0000 | [diff] [blame] | 1729 | ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs()); |
Leo Li | 23bb21c | 2017-08-24 01:51:51 +0000 | [diff] [blame] | 1730 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1731 | RegisterEffectiveTriple TripleRAII(TC, Triple); |
Michal Gorny | 7cfe480 | 2016-10-10 12:23:40 +0000 | [diff] [blame] | 1732 | switch (RLT) { |
| 1733 | case ToolChain::RLT_CompilerRT: |
| 1734 | llvm::outs() << TC.getCompilerRT(C.getArgs(), "builtins") << "\n"; |
| 1735 | break; |
| 1736 | case ToolChain::RLT_Libgcc: |
| 1737 | llvm::outs() << GetFilePath("libgcc.a", TC) << "\n"; |
| 1738 | break; |
| 1739 | } |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1740 | return false; |
| 1741 | } |
| 1742 | |
Daniel Dunbar | 1b3ec3a | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 1743 | if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { |
Douglas Katzman | a34b7bf | 2015-06-30 19:32:57 +0000 | [diff] [blame] | 1744 | for (const Multilib &Multilib : TC.getMultilibs()) |
| 1745 | llvm::outs() << Multilib << "\n"; |
Daniel Dunbar | 1b3ec3a | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 1746 | return false; |
| 1747 | } |
| 1748 | |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 1749 | if (C.getArgs().hasArg(options::OPT_print_multi_directory)) { |
Christian Bruel | 6ccc4a7 | 2018-09-06 14:03:44 +0000 | [diff] [blame] | 1750 | const Multilib &Multilib = TC.getMultilib(); |
| 1751 | if (Multilib.gccSuffix().empty()) |
| 1752 | llvm::outs() << ".\n"; |
| 1753 | else { |
| 1754 | StringRef Suffix(Multilib.gccSuffix()); |
| 1755 | assert(Suffix.front() == '/'); |
| 1756 | llvm::outs() << Suffix.substr(1) << "\n"; |
Jonathan Roelofs | 3fa96d8 | 2014-02-12 01:36:51 +0000 | [diff] [blame] | 1757 | } |
Jonathan Roelofs | 0e7ec60 | 2014-02-12 01:29:25 +0000 | [diff] [blame] | 1758 | return false; |
| 1759 | } |
Petr Hosek | bc86a99 | 2018-08-16 00:22:03 +0000 | [diff] [blame] | 1760 | |
| 1761 | if (C.getArgs().hasArg(options::OPT_print_target_triple)) { |
| 1762 | llvm::outs() << TC.getTripleString() << "\n"; |
| 1763 | return false; |
| 1764 | } |
| 1765 | |
| 1766 | if (C.getArgs().hasArg(options::OPT_print_effective_triple)) { |
| 1767 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1768 | llvm::outs() << Triple.getTriple() << "\n"; |
| 1769 | return false; |
| 1770 | } |
| 1771 | |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1772 | return true; |
| 1773 | } |
| 1774 | |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1775 | // Display an action graph human-readably. Action A is the "sink" node |
| 1776 | // and latest-occuring action. Traversal is in pre-order, visiting the |
| 1777 | // inputs to each action before printing the action itself. |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1778 | static unsigned PrintActions1(const Compilation &C, Action *A, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1779 | std::map<Action *, unsigned> &Ids) { |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1780 | if (Ids.count(A)) // A was already visited. |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1781 | return Ids[A]; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1782 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1783 | std::string str; |
| 1784 | llvm::raw_string_ostream os(str); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1785 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1786 | os << Action::getClassName(A->getKind()) << ", "; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1787 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1788 | os << "\"" << IA->getInputArg().getValue() << "\""; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1789 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1790 | os << '"' << BIA->getArchName() << '"' << ", {" |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 1791 | << PrintActions1(C, *BIA->input_begin(), Ids) << "}"; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1792 | } else if (OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
| 1793 | bool IsFirst = true; |
| 1794 | OA->doOnEachDependence( |
| 1795 | [&](Action *A, const ToolChain *TC, const char *BoundArch) { |
| 1796 | // E.g. for two CUDA device dependences whose bound arch is sm_20 and |
| 1797 | // sm_35 this will generate: |
| 1798 | // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device" |
| 1799 | // (nvptx64-nvidia-cuda:sm_35) {#ID} |
| 1800 | if (!IsFirst) |
| 1801 | os << ", "; |
| 1802 | os << '"'; |
| 1803 | if (TC) |
| 1804 | os << A->getOffloadingKindPrefix(); |
| 1805 | else |
| 1806 | os << "host"; |
| 1807 | os << " ("; |
| 1808 | os << TC->getTriple().normalize(); |
| 1809 | |
| 1810 | if (BoundArch) |
| 1811 | os << ":" << BoundArch; |
| 1812 | os << ")"; |
| 1813 | os << '"'; |
| 1814 | os << " {" << PrintActions1(C, A, Ids) << "}"; |
| 1815 | IsFirst = false; |
| 1816 | }); |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1817 | } else { |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1818 | const ActionList *AL = &A->getInputs(); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 1819 | |
Artem Belevich | 2325675 | 2015-09-22 17:23:09 +0000 | [diff] [blame] | 1820 | if (AL->size()) { |
| 1821 | const char *Prefix = "{"; |
| 1822 | for (Action *PreRequisite : *AL) { |
| 1823 | os << Prefix << PrintActions1(C, PreRequisite, Ids); |
| 1824 | Prefix = ", "; |
| 1825 | } |
| 1826 | os << "}"; |
| 1827 | } else |
| 1828 | os << "{}"; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1831 | // Append offload info for all options other than the offloading action |
| 1832 | // itself (e.g. (cuda-device, sm_20) or (cuda-host)). |
| 1833 | std::string offload_str; |
| 1834 | llvm::raw_string_ostream offload_os(offload_str); |
| 1835 | if (!isa<OffloadAction>(A)) { |
| 1836 | auto S = A->getOffloadingKindPrefix(); |
| 1837 | if (!S.empty()) { |
| 1838 | offload_os << ", (" << S; |
| 1839 | if (A->getOffloadingArch()) |
| 1840 | offload_os << ", " << A->getOffloadingArch(); |
| 1841 | offload_os << ")"; |
| 1842 | } |
| 1843 | } |
| 1844 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1845 | unsigned Id = Ids.size(); |
| 1846 | Ids[A] = Id; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1847 | llvm::errs() << Id << ": " << os.str() << ", " |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1848 | << types::getTypeName(A->getType()) << offload_os.str() << "\n"; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1849 | |
| 1850 | return Id; |
| 1851 | } |
| 1852 | |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1853 | // Print the action graphs in a compilation C. |
| 1854 | // 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] | 1855 | void Driver::PrintActions(const Compilation &C) const { |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1856 | std::map<Action *, unsigned> Ids; |
| 1857 | for (Action *A : C.getActions()) |
| 1858 | PrintActions1(C, A, Ids); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1861 | /// Check whether the given input tree contains any compilation or |
Joerg Sonnenberger | 5fe4a7d | 2011-05-06 14:05:11 +0000 | [diff] [blame] | 1862 | /// assembly actions. |
| 1863 | static bool ContainsCompileOrAssembleAction(const Action *A) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1864 | if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A) || |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 1865 | isa<AssembleJobAction>(A)) |
Daniel Dunbar | 00d3d8e | 2010-06-29 16:38:33 +0000 | [diff] [blame] | 1866 | return true; |
| 1867 | |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 1868 | for (const Action *Input : A->inputs()) |
Nico Weber | fb80f96 | 2015-09-19 21:36:51 +0000 | [diff] [blame] | 1869 | if (ContainsCompileOrAssembleAction(Input)) |
Daniel Dunbar | 00d3d8e | 2010-06-29 16:38:33 +0000 | [diff] [blame] | 1870 | return true; |
| 1871 | |
| 1872 | return false; |
| 1873 | } |
| 1874 | |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1875 | void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC, |
| 1876 | const InputList &BAInputs) const { |
| 1877 | DerivedArgList &Args = C.getArgs(); |
| 1878 | ActionList &Actions = C.getActions(); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1879 | llvm::PrettyStackTraceString CrashInfo("Building universal build actions"); |
| 1880 | // Collect the list of architectures. Duplicates are allowed, but should only |
| 1881 | // be handled once (in the order seen). |
Daniel Dunbar | e5dc482 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 1882 | llvm::StringSet<> ArchNames; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1883 | SmallVector<const char *, 4> Archs; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 1884 | for (Arg *A : Args) { |
Daniel Dunbar | 0bfb21e | 2009-11-19 03:26:40 +0000 | [diff] [blame] | 1885 | if (A->getOption().matches(options::OPT_arch)) { |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1886 | // Validate the option here; we don't save the type here because its |
| 1887 | // particular spelling may participate in other driver choices. |
| 1888 | llvm::Triple::ArchType Arch = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1889 | tools::darwin::getArchTypeForMachOArchName(A->getValue()); |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1890 | if (Arch == llvm::Triple::UnknownArch) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1891 | Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args); |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1892 | continue; |
| 1893 | } |
| 1894 | |
Daniel Dunbar | 2da0272 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 1895 | A->claim(); |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 1896 | if (ArchNames.insert(A->getValue()).second) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1897 | Archs.push_back(A->getValue()); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1901 | // When there is no explicit arch for this platform, make sure we still bind |
| 1902 | // the architecture (to the default) so that -Xarch_ is handled correctly. |
Daniel Dunbar | eb843be | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 1903 | if (!Archs.size()) |
Daniel Dunbar | c3bd9f5 | 2012-11-08 03:38:26 +0000 | [diff] [blame] | 1904 | Archs.push_back(Args.MakeArgString(TC.getDefaultUniversalArchName())); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1905 | |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1906 | ActionList SingleActions; |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1907 | BuildActions(C, Args, BAInputs, SingleActions); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1908 | |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1909 | // Add in arch bindings for every top level action, as well as lipo and |
| 1910 | // dsymutil steps if needed. |
Nico Weber | fb80f96 | 2015-09-19 21:36:51 +0000 | [diff] [blame] | 1911 | for (Action* Act : SingleActions) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1912 | // Make sure we can lipo this kind of output. If not (and it is an actual |
| 1913 | // output) then we disallow, since we can't create an output file with the |
| 1914 | // right name without overwriting it. We could remove this oddity by just |
| 1915 | // changing the output names to include the arch, which would also fix |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1916 | // -save-temps. Compatibility wins for now. |
| 1917 | |
Daniel Dunbar | e2ca3bd | 2009-03-13 17:46:02 +0000 | [diff] [blame] | 1918 | if (Archs.size() > 1 && !types::canLipoType(Act->getType())) |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1919 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1920 | << types::getTypeName(Act->getType()); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1921 | |
| 1922 | ActionList Inputs; |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1923 | for (unsigned i = 0, e = Archs.size(); i != e; ++i) |
| 1924 | Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i])); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1925 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1926 | // Lipo if necessary, we do it this way because we need to set the arch flag |
| 1927 | // so that -Xarch_ gets overwritten. |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1928 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 1929 | Actions.append(Inputs.begin(), Inputs.end()); |
| 1930 | else |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1931 | Actions.push_back(C.MakeAction<LipoJobAction>(Inputs, Act->getType())); |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1932 | |
Eric Christopher | 65c05fa | 2012-02-06 19:43:51 +0000 | [diff] [blame] | 1933 | // Handle debug info queries. |
| 1934 | Arg *A = Args.getLastArg(options::OPT_g_Group); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1935 | if (A && !A->getOption().matches(options::OPT_g0) && |
| 1936 | !A->getOption().matches(options::OPT_gstabs) && |
| 1937 | ContainsCompileOrAssembleAction(Actions.back())) { |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 1938 | |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1939 | // Add a 'dsymutil' step if necessary, when debug info is enabled and we |
| 1940 | // 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] | 1941 | // because the debug info will refer to a temporary object file which |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1942 | // will be removed at the end of the compilation process. |
| 1943 | if (Act->getType() == types::TY_Image) { |
| 1944 | ActionList Inputs; |
| 1945 | Inputs.push_back(Actions.back()); |
| 1946 | Actions.pop_back(); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1947 | Actions.push_back( |
| 1948 | C.MakeAction<DsymutilJobAction>(Inputs, types::TY_dSYM)); |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1949 | } |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1950 | |
Ben Langmuir | 9b9a8d3 | 2014-02-06 18:53:25 +0000 | [diff] [blame] | 1951 | // Verify the debug info output. |
Alp Toker | e9d2bfc | 2014-01-17 02:06:23 +0000 | [diff] [blame] | 1952 | if (Args.hasArg(options::OPT_verify_debug_info)) { |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1953 | Action* LastAction = Actions.back(); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1954 | Actions.pop_back(); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1955 | Actions.push_back(C.MakeAction<VerifyDebugInfoJobAction>( |
| 1956 | LastAction, types::TY_Nothing)); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1957 | } |
| 1958 | } |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1959 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1962 | /// Check that the file referenced by Value exists. If it doesn't, |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1963 | /// issue a diagnostic and return false. |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 1964 | static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args, |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 1965 | StringRef Value, types::ID Ty) { |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1966 | if (!D.getCheckInputsExist()) |
| 1967 | return true; |
| 1968 | |
| 1969 | // stdin always exists. |
| 1970 | if (Value == "-") |
| 1971 | return true; |
| 1972 | |
| 1973 | SmallString<64> Path(Value); |
| 1974 | if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) { |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1975 | if (!llvm::sys::path::is_absolute(Path)) { |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1976 | SmallString<64> Directory(WorkDir->getValue()); |
| 1977 | llvm::sys::path::append(Directory, Value); |
| 1978 | Path.assign(Directory); |
| 1979 | } |
| 1980 | } |
| 1981 | |
Sam McCall | 2c78247 | 2018-11-08 16:57:01 +0000 | [diff] [blame] | 1982 | if (D.getVFS().exists(Path)) |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1983 | return true; |
| 1984 | |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 1985 | if (D.IsCLMode()) { |
| 1986 | if (!llvm::sys::path::is_absolute(Twine(Path)) && |
| 1987 | llvm::sys::Process::FindInEnvPath("LIB", Value)) |
| 1988 | return true; |
| 1989 | |
| 1990 | if (Args.hasArg(options::OPT__SLASH_link) && Ty == types::TY_Object) { |
| 1991 | // Arguments to the /link flag might cause the linker to search for object |
| 1992 | // and library files in paths we don't know about. Don't error in such |
| 1993 | // cases. |
| 1994 | return true; |
| 1995 | } |
| 1996 | } |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 1997 | |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1998 | D.Diag(clang::diag::err_drv_no_such_file) << Path; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1999 | return false; |
| 2000 | } |
| 2001 | |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2002 | // Construct a the list of inputs and their types. |
Hans Wennborg | 5536285 | 2014-05-02 22:55:30 +0000 | [diff] [blame] | 2003 | void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args, |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2004 | InputList &Inputs) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2005 | // Track the current user specified (-x) input. We also explicitly track the |
| 2006 | // argument used to set the type; we only want to claim the type when we |
| 2007 | // actually use it, so we warn about unused -x arguments. |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 2008 | types::ID InputType = types::TY_Nothing; |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 2009 | Arg *InputTypeArg = nullptr; |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 2010 | |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2011 | // 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] | 2012 | if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC, |
| 2013 | options::OPT__SLASH_TP)) { |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2014 | InputTypeArg = TCTP; |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2015 | InputType = TCTP->getOption().matches(options::OPT__SLASH_TC) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 2016 | ? types::TY_C |
| 2017 | : types::TY_CXX; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2018 | |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 2019 | Arg *Previous = nullptr; |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2020 | bool ShowNote = false; |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 2021 | for (Arg *A : Args.filtered(options::OPT__SLASH_TC, options::OPT__SLASH_TP)) { |
| 2022 | if (Previous) { |
| 2023 | Diag(clang::diag::warn_drv_overriding_flag_option) |
| 2024 | << Previous->getSpelling() << A->getSpelling(); |
| 2025 | ShowNote = true; |
| 2026 | } |
| 2027 | Previous = A; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2028 | } |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2029 | if (ShowNote) |
| 2030 | Diag(clang::diag::note_drv_t_option_is_global); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2031 | |
| 2032 | // No driver mode exposes -x and /TC or /TP; we don't support mixing them. |
| 2033 | assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed"); |
| 2034 | } |
| 2035 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 2036 | for (Arg *A : Args) { |
Michael J. Spencer | ad3ccc3 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 2037 | if (A->getOption().getKind() == Option::InputClass) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2038 | const char *Value = A->getValue(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2039 | types::ID Ty = types::TY_INVALID; |
| 2040 | |
| 2041 | // Infer the input type if necessary. |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 2042 | if (InputType == types::TY_Nothing) { |
| 2043 | // If there was an explicit arg for this, claim it. |
| 2044 | if (InputTypeArg) |
| 2045 | InputTypeArg->claim(); |
| 2046 | |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2047 | // stdin must be handled specially. |
| 2048 | if (memcmp(Value, "-", 2) == 0) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2049 | // If running with -E, treat as a C input (this changes the builtin |
| 2050 | // macros, for example). This may be overridden by -ObjC below. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2051 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2052 | // Otherwise emit an error but still use a valid type to avoid |
| 2053 | // spurious errors (e.g., no inputs). |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2054 | if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP()) |
Hans Wennborg | cfdd8b5 | 2014-01-29 01:04:40 +0000 | [diff] [blame] | 2055 | Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl |
| 2056 | : clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2057 | Ty = types::TY_C; |
| 2058 | } else { |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2059 | // Otherwise lookup by extension. |
Reid Kleckner | a643e64 | 2018-12-26 21:04:08 +0000 | [diff] [blame] | 2060 | // Fallback is C if invoked as C preprocessor, C++ if invoked with |
| 2061 | // clang-cl /E, or Object otherwise. |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2062 | // 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] | 2063 | // idea of what .s is. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2064 | if (const char *Ext = strrchr(Value, '.')) |
Daniel Dunbar | cc7df6c | 2010-08-02 05:43:56 +0000 | [diff] [blame] | 2065 | Ty = TC.LookupTypeForExtension(Ext + 1); |
Daniel Dunbar | ea9f032 | 2009-03-20 23:39:23 +0000 | [diff] [blame] | 2066 | |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2067 | if (Ty == types::TY_INVALID) { |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2068 | if (CCCIsCPP()) |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2069 | Ty = types::TY_C; |
Reid Kleckner | a643e64 | 2018-12-26 21:04:08 +0000 | [diff] [blame] | 2070 | else if (IsCLMode() && Args.hasArgNoClaim(options::OPT_E)) |
| 2071 | Ty = types::TY_CXX; |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2072 | else |
| 2073 | Ty = types::TY_Object; |
| 2074 | } |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 2075 | |
| 2076 | // If the driver is invoked as C++ compiler (like clang++ or c++) it |
| 2077 | // should autodetect some input files as C++ for g++ compatibility. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2078 | if (CCCIsCXX()) { |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 2079 | types::ID OldTy = Ty; |
| 2080 | Ty = types::lookupCXXTypeForCType(Ty); |
| 2081 | |
| 2082 | if (Ty != OldTy) |
| 2083 | Diag(clang::diag::warn_drv_treating_input_as_cxx) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 2084 | << getTypeName(OldTy) << getTypeName(Ty); |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 2085 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Daniel Dunbar | 82b2210 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 2088 | // -ObjC and -ObjC++ override the default language, but only for "source |
| 2089 | // files". We just treat everything that isn't a linker input as a |
| 2090 | // source file. |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2091 | // |
Daniel Dunbar | 82b2210 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 2092 | // 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] | 2093 | if (Ty != types::TY_Object) { |
| 2094 | if (Args.hasArg(options::OPT_ObjC)) |
| 2095 | Ty = types::TY_ObjC; |
| 2096 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 2097 | Ty = types::TY_ObjCXX; |
| 2098 | } |
| 2099 | } else { |
| 2100 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
Ehsan Akhgari | 7e954ea | 2014-09-12 18:15:10 +0000 | [diff] [blame] | 2101 | if (!InputTypeArg->getOption().matches(options::OPT_x)) { |
| 2102 | // If emulating cl.exe, make sure that /TC and /TP don't affect input |
| 2103 | // object files. |
| 2104 | const char *Ext = strrchr(Value, '.'); |
| 2105 | if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object) |
| 2106 | Ty = types::TY_Object; |
| 2107 | } |
| 2108 | if (Ty == types::TY_INVALID) { |
| 2109 | Ty = InputType; |
| 2110 | InputTypeArg->claim(); |
| 2111 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 2114 | if (DiagnoseInputExistence(*this, Args, Value, Ty)) |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2115 | Inputs.push_back(std::make_pair(Ty, A)); |
| 2116 | |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2117 | } else if (A->getOption().matches(options::OPT__SLASH_Tc)) { |
| 2118 | StringRef Value = A->getValue(); |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 2119 | if (DiagnoseInputExistence(*this, Args, Value, types::TY_C)) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2120 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2121 | Inputs.push_back(std::make_pair(types::TY_C, InputArg)); |
| 2122 | } |
| 2123 | A->claim(); |
| 2124 | } else if (A->getOption().matches(options::OPT__SLASH_Tp)) { |
| 2125 | StringRef Value = A->getValue(); |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 2126 | if (DiagnoseInputExistence(*this, Args, Value, types::TY_CXX)) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2127 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2128 | Inputs.push_back(std::make_pair(types::TY_CXX, InputArg)); |
| 2129 | } |
| 2130 | A->claim(); |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 2131 | } else if (A->getOption().hasFlag(options::LinkerInput)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2132 | // Just treat as object type, we could make a special type for this if |
| 2133 | // necessary. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2134 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 2135 | |
Daniel Dunbar | 0bfb21e | 2009-11-19 03:26:40 +0000 | [diff] [blame] | 2136 | } else if (A->getOption().matches(options::OPT_x)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2137 | InputTypeArg = A; |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2138 | InputType = types::lookupTypeForTypeSpecifier(A->getValue()); |
Chad Rosier | 706c235 | 2012-04-07 00:01:31 +0000 | [diff] [blame] | 2139 | A->claim(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2140 | |
| 2141 | // Follow gcc behavior and treat as linker input for invalid -x |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2142 | // 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] | 2143 | // this isn't very important, we might as well be bug compatible. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2144 | if (!InputType) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2145 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2146 | InputType = types::TY_Object; |
| 2147 | } |
Hans Wennborg | 091f1b6 | 2017-01-27 17:09:41 +0000 | [diff] [blame] | 2148 | } else if (A->getOption().getID() == options::OPT__SLASH_U) { |
| 2149 | assert(A->getNumValues() == 1 && "The /U option has one value."); |
| 2150 | StringRef Val = A->getValue(0); |
| 2151 | if (Val.find_first_of("/\\") != StringRef::npos) { |
| 2152 | // Warn about e.g. "/Users/me/myfile.c". |
| 2153 | Diag(diag::warn_slash_u_filename) << Val; |
| 2154 | Diag(diag::note_use_dashdash); |
| 2155 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2156 | } |
| 2157 | } |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2158 | if (CCCIsCPP() && Inputs.empty()) { |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 2159 | // If called as standalone preprocessor, stdin is processed |
| 2160 | // if no other input is present. |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2161 | Arg *A = MakeInputArg(Args, *Opts, "-"); |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 2162 | Inputs.push_back(std::make_pair(types::TY_C, A)); |
| 2163 | } |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2166 | namespace { |
| 2167 | /// Provides a convenient interface for different programming models to generate |
| 2168 | /// the required device actions. |
| 2169 | class OffloadingActionBuilder final { |
| 2170 | /// Flag used to trace errors in the builder. |
| 2171 | bool IsValid = false; |
| 2172 | |
| 2173 | /// The compilation that is using this builder. |
| 2174 | Compilation &C; |
| 2175 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2176 | /// Map between an input argument and the offload kinds used to process it. |
| 2177 | std::map<const Arg *, unsigned> InputArgToOffloadKindMap; |
| 2178 | |
| 2179 | /// Builder interface. It doesn't build anything or keep any state. |
| 2180 | class DeviceActionBuilder { |
| 2181 | public: |
| 2182 | typedef llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PhasesTy; |
| 2183 | |
| 2184 | enum ActionBuilderReturnCode { |
| 2185 | // The builder acted successfully on the current action. |
| 2186 | ABRT_Success, |
| 2187 | // The builder didn't have to act on the current action. |
| 2188 | ABRT_Inactive, |
| 2189 | // The builder was successful and requested the host action to not be |
| 2190 | // generated. |
| 2191 | ABRT_Ignore_Host, |
| 2192 | }; |
| 2193 | |
| 2194 | protected: |
| 2195 | /// Compilation associated with this builder. |
| 2196 | Compilation &C; |
| 2197 | |
| 2198 | /// Tool chains associated with this builder. The same programming |
| 2199 | /// model may have associated one or more tool chains. |
| 2200 | SmallVector<const ToolChain *, 2> ToolChains; |
| 2201 | |
| 2202 | /// The derived arguments associated with this builder. |
| 2203 | DerivedArgList &Args; |
| 2204 | |
| 2205 | /// The inputs associated with this builder. |
| 2206 | const Driver::InputList &Inputs; |
| 2207 | |
| 2208 | /// The associated offload kind. |
| 2209 | Action::OffloadKind AssociatedOffloadKind = Action::OFK_None; |
| 2210 | |
| 2211 | public: |
| 2212 | DeviceActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2213 | const Driver::InputList &Inputs, |
| 2214 | Action::OffloadKind AssociatedOffloadKind) |
| 2215 | : C(C), Args(Args), Inputs(Inputs), |
| 2216 | AssociatedOffloadKind(AssociatedOffloadKind) {} |
| 2217 | virtual ~DeviceActionBuilder() {} |
| 2218 | |
| 2219 | /// Fill up the array \a DA with all the device dependences that should be |
| 2220 | /// added to the provided host action \a HostAction. By default it is |
| 2221 | /// inactive. |
| 2222 | virtual ActionBuilderReturnCode |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2223 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2224 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2225 | PhasesTy &Phases) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2226 | return ABRT_Inactive; |
| 2227 | } |
| 2228 | |
| 2229 | /// Update the state to include the provided host action \a HostAction as a |
| 2230 | /// dependency of the current device action. By default it is inactive. |
| 2231 | virtual ActionBuilderReturnCode addDeviceDepences(Action *HostAction) { |
| 2232 | return ABRT_Inactive; |
| 2233 | } |
| 2234 | |
| 2235 | /// Append top level actions generated by the builder. Return true if errors |
| 2236 | /// were found. |
| 2237 | virtual void appendTopLevelActions(ActionList &AL) {} |
| 2238 | |
| 2239 | /// Append linker actions generated by the builder. Return true if errors |
| 2240 | /// were found. |
| 2241 | virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {} |
| 2242 | |
| 2243 | /// Initialize the builder. Return true if any initialization errors are |
| 2244 | /// found. |
| 2245 | virtual bool initialize() { return false; } |
| 2246 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2247 | /// Return true if the builder can use bundling/unbundling. |
| 2248 | virtual bool canUseBundlerUnbundler() const { return false; } |
| 2249 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2250 | /// Return true if this builder is valid. We have a valid builder if we have |
| 2251 | /// associated device tool chains. |
| 2252 | bool isValid() { return !ToolChains.empty(); } |
| 2253 | |
| 2254 | /// Return the associated offload kind. |
| 2255 | Action::OffloadKind getAssociatedOffloadKind() { |
| 2256 | return AssociatedOffloadKind; |
| 2257 | } |
| 2258 | }; |
| 2259 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2260 | /// Base class for CUDA/HIP action builder. It injects device code in |
| 2261 | /// the host backend action. |
| 2262 | class CudaActionBuilderBase : public DeviceActionBuilder { |
| 2263 | protected: |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2264 | /// Flags to signal if the user requested host-only or device-only |
| 2265 | /// compilation. |
| 2266 | bool CompileHostOnly = false; |
| 2267 | bool CompileDeviceOnly = false; |
| 2268 | |
| 2269 | /// List of GPU architectures to use in this compilation. |
| 2270 | SmallVector<CudaArch, 4> GpuArchList; |
| 2271 | |
| 2272 | /// The CUDA actions for the current input. |
| 2273 | ActionList CudaDeviceActions; |
| 2274 | |
| 2275 | /// The CUDA fat binary if it was generated for the current input. |
| 2276 | Action *CudaFatBinary = nullptr; |
| 2277 | |
| 2278 | /// Flag that is set to true if this builder acted on the current input. |
| 2279 | bool IsActive = false; |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2280 | public: |
| 2281 | CudaActionBuilderBase(Compilation &C, DerivedArgList &Args, |
| 2282 | const Driver::InputList &Inputs, |
| 2283 | Action::OffloadKind OFKind) |
| 2284 | : DeviceActionBuilder(C, Args, Inputs, OFKind) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2285 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2286 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2287 | // While generating code for CUDA, we only depend on the host input action |
| 2288 | // to trigger the creation of all the CUDA device actions. |
| 2289 | |
| 2290 | // If we are dealing with an input action, replicate it for each GPU |
| 2291 | // architecture. If we are in host-only mode we return 'success' so that |
| 2292 | // the host uses the CUDA offload kind. |
| 2293 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2294 | assert(!GpuArchList.empty() && |
| 2295 | "We should have at least one GPU architecture."); |
| 2296 | |
| 2297 | // If the host input is not CUDA or HIP, we don't need to bother about |
| 2298 | // this input. |
| 2299 | if (IA->getType() != types::TY_CUDA && |
| 2300 | IA->getType() != types::TY_HIP) { |
| 2301 | // The builder will ignore this input. |
| 2302 | IsActive = false; |
| 2303 | return ABRT_Inactive; |
| 2304 | } |
| 2305 | |
| 2306 | // Set the flag to true, so that the builder acts on the current input. |
| 2307 | IsActive = true; |
| 2308 | |
| 2309 | if (CompileHostOnly) |
| 2310 | return ABRT_Success; |
| 2311 | |
| 2312 | // Replicate inputs for each GPU architecture. |
| 2313 | auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE |
| 2314 | : types::TY_CUDA_DEVICE; |
| 2315 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2316 | CudaDeviceActions.push_back( |
| 2317 | C.MakeAction<InputAction>(IA->getInputArg(), Ty)); |
| 2318 | } |
| 2319 | |
| 2320 | return ABRT_Success; |
| 2321 | } |
| 2322 | |
| 2323 | // If this is an unbundling action use it as is for each CUDA toolchain. |
| 2324 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
| 2325 | CudaDeviceActions.clear(); |
Aaron Enye Shi | bea57bb | 2019-01-04 19:05:41 +0000 | [diff] [blame] | 2326 | auto *IA = cast<InputAction>(UA->getInputs().back()); |
| 2327 | std::string FileName = IA->getInputArg().getAsString(Args); |
| 2328 | // Check if the type of the file is the same as the action. Do not |
| 2329 | // unbundle it if it is not. Do not unbundle .so files, for example, |
| 2330 | // which are not object files. |
| 2331 | if (IA->getType() == types::TY_Object && |
| 2332 | (!llvm::sys::path::has_extension(FileName) || |
| 2333 | types::lookupTypeForExtension( |
| 2334 | llvm::sys::path::extension(FileName).drop_front()) != |
| 2335 | types::TY_Object)) |
| 2336 | return ABRT_Inactive; |
| 2337 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2338 | for (auto Arch : GpuArchList) { |
| 2339 | CudaDeviceActions.push_back(UA); |
| 2340 | UA->registerDependentActionInfo(ToolChains[0], CudaArchToString(Arch), |
| 2341 | AssociatedOffloadKind); |
| 2342 | } |
| 2343 | return ABRT_Success; |
| 2344 | } |
| 2345 | |
| 2346 | return IsActive ? ABRT_Success : ABRT_Inactive; |
| 2347 | } |
| 2348 | |
| 2349 | void appendTopLevelActions(ActionList &AL) override { |
| 2350 | // Utility to append actions to the top level list. |
| 2351 | auto AddTopLevel = [&](Action *A, CudaArch BoundArch) { |
| 2352 | OffloadAction::DeviceDependences Dep; |
| 2353 | Dep.add(*A, *ToolChains.front(), CudaArchToString(BoundArch), |
| 2354 | AssociatedOffloadKind); |
| 2355 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2356 | }; |
| 2357 | |
| 2358 | // If we have a fat binary, add it to the list. |
| 2359 | if (CudaFatBinary) { |
| 2360 | AddTopLevel(CudaFatBinary, CudaArch::UNKNOWN); |
| 2361 | CudaDeviceActions.clear(); |
| 2362 | CudaFatBinary = nullptr; |
| 2363 | return; |
| 2364 | } |
| 2365 | |
| 2366 | if (CudaDeviceActions.empty()) |
| 2367 | return; |
| 2368 | |
| 2369 | // If we have CUDA actions at this point, that's because we have a have |
| 2370 | // partial compilation, so we should have an action for each GPU |
| 2371 | // architecture. |
| 2372 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2373 | "Expecting one action per GPU architecture."); |
| 2374 | assert(ToolChains.size() == 1 && |
| 2375 | "Expecting to have a sing CUDA toolchain."); |
| 2376 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) |
| 2377 | AddTopLevel(CudaDeviceActions[I], GpuArchList[I]); |
| 2378 | |
| 2379 | CudaDeviceActions.clear(); |
| 2380 | } |
| 2381 | |
| 2382 | bool initialize() override { |
| 2383 | assert(AssociatedOffloadKind == Action::OFK_Cuda || |
| 2384 | AssociatedOffloadKind == Action::OFK_HIP); |
| 2385 | |
| 2386 | // We don't need to support CUDA. |
| 2387 | if (AssociatedOffloadKind == Action::OFK_Cuda && |
| 2388 | !C.hasOffloadToolChain<Action::OFK_Cuda>()) |
| 2389 | return false; |
| 2390 | |
| 2391 | // We don't need to support HIP. |
| 2392 | if (AssociatedOffloadKind == Action::OFK_HIP && |
| 2393 | !C.hasOffloadToolChain<Action::OFK_HIP>()) |
| 2394 | return false; |
| 2395 | |
| 2396 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 2397 | assert(HostTC && "No toolchain for host compilation."); |
| 2398 | if (HostTC->getTriple().isNVPTX() || |
| 2399 | HostTC->getTriple().getArch() == llvm::Triple::amdgcn) { |
| 2400 | // We do not support targeting NVPTX/AMDGCN for host compilation. Throw |
| 2401 | // an error and abort pipeline construction early so we don't trip |
| 2402 | // asserts that assume device-side compilation. |
| 2403 | C.getDriver().Diag(diag::err_drv_cuda_host_arch) |
| 2404 | << HostTC->getTriple().getArchName(); |
| 2405 | return true; |
| 2406 | } |
| 2407 | |
| 2408 | ToolChains.push_back( |
| 2409 | AssociatedOffloadKind == Action::OFK_Cuda |
| 2410 | ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() |
| 2411 | : C.getSingleOffloadToolChain<Action::OFK_HIP>()); |
| 2412 | |
| 2413 | Arg *PartialCompilationArg = Args.getLastArg( |
| 2414 | options::OPT_cuda_host_only, options::OPT_cuda_device_only, |
| 2415 | options::OPT_cuda_compile_host_device); |
| 2416 | CompileHostOnly = PartialCompilationArg && |
| 2417 | PartialCompilationArg->getOption().matches( |
| 2418 | options::OPT_cuda_host_only); |
| 2419 | CompileDeviceOnly = PartialCompilationArg && |
| 2420 | PartialCompilationArg->getOption().matches( |
| 2421 | options::OPT_cuda_device_only); |
| 2422 | |
| 2423 | // Collect all cuda_gpu_arch parameters, removing duplicates. |
| 2424 | std::set<CudaArch> GpuArchs; |
| 2425 | bool Error = false; |
| 2426 | for (Arg *A : Args) { |
| 2427 | if (!(A->getOption().matches(options::OPT_cuda_gpu_arch_EQ) || |
| 2428 | A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ))) |
| 2429 | continue; |
| 2430 | A->claim(); |
| 2431 | |
| 2432 | const StringRef ArchStr = A->getValue(); |
| 2433 | if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ) && |
| 2434 | ArchStr == "all") { |
| 2435 | GpuArchs.clear(); |
| 2436 | continue; |
| 2437 | } |
| 2438 | CudaArch Arch = StringToCudaArch(ArchStr); |
| 2439 | if (Arch == CudaArch::UNKNOWN) { |
| 2440 | C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr; |
| 2441 | Error = true; |
| 2442 | } else if (A->getOption().matches(options::OPT_cuda_gpu_arch_EQ)) |
| 2443 | GpuArchs.insert(Arch); |
| 2444 | else if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ)) |
| 2445 | GpuArchs.erase(Arch); |
| 2446 | else |
| 2447 | llvm_unreachable("Unexpected option."); |
| 2448 | } |
| 2449 | |
| 2450 | // Collect list of GPUs remaining in the set. |
| 2451 | for (CudaArch Arch : GpuArchs) |
| 2452 | GpuArchList.push_back(Arch); |
| 2453 | |
| 2454 | // Default to sm_20 which is the lowest common denominator for |
| 2455 | // supported GPUs. sm_20 code should work correctly, if |
| 2456 | // suboptimally, on all newer GPUs. |
| 2457 | if (GpuArchList.empty()) |
| 2458 | GpuArchList.push_back(CudaArch::SM_20); |
| 2459 | |
| 2460 | return Error; |
| 2461 | } |
| 2462 | }; |
| 2463 | |
| 2464 | /// \brief CUDA action builder. It injects device code in the host backend |
| 2465 | /// action. |
| 2466 | class CudaActionBuilder final : public CudaActionBuilderBase { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2467 | public: |
| 2468 | CudaActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2469 | const Driver::InputList &Inputs) |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2470 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2471 | |
| 2472 | ActionBuilderReturnCode |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2473 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2474 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2475 | PhasesTy &Phases) override { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2476 | if (!IsActive) |
| 2477 | return ABRT_Inactive; |
| 2478 | |
| 2479 | // If we don't have more CUDA actions, we don't have any dependences to |
| 2480 | // create for the host. |
| 2481 | if (CudaDeviceActions.empty()) |
| 2482 | return ABRT_Success; |
| 2483 | |
| 2484 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2485 | "Expecting one action per GPU architecture."); |
| 2486 | assert(!CompileHostOnly && |
| 2487 | "Not expecting CUDA actions in host-only compilation."); |
| 2488 | |
| 2489 | // If we are generating code for the device or we are in a backend phase, |
| 2490 | // we attempt to generate the fat binary. We compile each arch to ptx and |
| 2491 | // assemble to cubin, then feed the cubin *and* the ptx into a device |
| 2492 | // "link" action, which uses fatbinary to combine these cubins into one |
| 2493 | // fatbin. The fatbin is then an input to the host action if not in |
| 2494 | // device-only mode. |
| 2495 | if (CompileDeviceOnly || CurPhase == phases::Backend) { |
| 2496 | ActionList DeviceActions; |
| 2497 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2498 | // Produce the device action from the current phase up to the assemble |
| 2499 | // phase. |
| 2500 | for (auto Ph : Phases) { |
| 2501 | // Skip the phases that were already dealt with. |
| 2502 | if (Ph < CurPhase) |
| 2503 | continue; |
| 2504 | // We have to be consistent with the host final phase. |
| 2505 | if (Ph > FinalPhase) |
| 2506 | break; |
| 2507 | |
| 2508 | CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction( |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 2509 | C, Args, Ph, CudaDeviceActions[I], Action::OFK_Cuda); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2510 | |
| 2511 | if (Ph == phases::Assemble) |
| 2512 | break; |
| 2513 | } |
| 2514 | |
| 2515 | // If we didn't reach the assemble phase, we can't generate the fat |
| 2516 | // binary. We don't need to generate the fat binary if we are not in |
| 2517 | // device-only mode. |
| 2518 | if (!isa<AssembleJobAction>(CudaDeviceActions[I]) || |
| 2519 | CompileDeviceOnly) |
| 2520 | continue; |
| 2521 | |
| 2522 | Action *AssembleAction = CudaDeviceActions[I]; |
| 2523 | assert(AssembleAction->getType() == types::TY_Object); |
| 2524 | assert(AssembleAction->getInputs().size() == 1); |
| 2525 | |
| 2526 | Action *BackendAction = AssembleAction->getInputs()[0]; |
| 2527 | assert(BackendAction->getType() == types::TY_PP_Asm); |
| 2528 | |
| 2529 | for (auto &A : {AssembleAction, BackendAction}) { |
| 2530 | OffloadAction::DeviceDependences DDep; |
| 2531 | DDep.add(*A, *ToolChains.front(), CudaArchToString(GpuArchList[I]), |
| 2532 | Action::OFK_Cuda); |
| 2533 | DeviceActions.push_back( |
| 2534 | C.MakeAction<OffloadAction>(DDep, A->getType())); |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | // We generate the fat binary if we have device input actions. |
| 2539 | if (!DeviceActions.empty()) { |
| 2540 | CudaFatBinary = |
| 2541 | C.MakeAction<LinkJobAction>(DeviceActions, types::TY_CUDA_FATBIN); |
| 2542 | |
| 2543 | if (!CompileDeviceOnly) { |
| 2544 | DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr, |
| 2545 | Action::OFK_Cuda); |
| 2546 | // Clear the fat binary, it is already a dependence to an host |
| 2547 | // action. |
| 2548 | CudaFatBinary = nullptr; |
| 2549 | } |
| 2550 | |
| 2551 | // Remove the CUDA actions as they are already connected to an host |
| 2552 | // action or fat binary. |
| 2553 | CudaDeviceActions.clear(); |
| 2554 | } |
| 2555 | |
| 2556 | // We avoid creating host action in device-only mode. |
| 2557 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
Samuel Antao | 5b1a89a | 2016-10-27 00:53:34 +0000 | [diff] [blame] | 2558 | } else if (CurPhase > phases::Backend) { |
| 2559 | // If we are past the backend phase and still have a device action, we |
| 2560 | // don't have to do anything as this action is already a device |
| 2561 | // top-level action. |
| 2562 | return ABRT_Success; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | assert(CurPhase < phases::Backend && "Generating single CUDA " |
| 2566 | "instructions should only occur " |
| 2567 | "before the backend phase!"); |
| 2568 | |
| 2569 | // By default, we produce an action for each device arch. |
| 2570 | for (Action *&A : CudaDeviceActions) |
| 2571 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2572 | |
| 2573 | return ABRT_Success; |
| 2574 | } |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2575 | }; |
| 2576 | /// \brief HIP action builder. It injects device code in the host backend |
| 2577 | /// action. |
| 2578 | class HIPActionBuilder final : public CudaActionBuilderBase { |
| 2579 | /// The linker inputs obtained for each device arch. |
| 2580 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2581 | bool Relocatable; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2582 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2583 | public: |
| 2584 | HIPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2585 | const Driver::InputList &Inputs) |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2586 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP), |
| 2587 | Relocatable(false) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2588 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2589 | bool canUseBundlerUnbundler() const override { return true; } |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2590 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2591 | ActionBuilderReturnCode |
| 2592 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2593 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2594 | PhasesTy &Phases) override { |
| 2595 | // amdgcn does not support linking of object files, therefore we skip |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2596 | // backend and assemble phases to output LLVM IR. Except for generating |
| 2597 | // non-relocatable device coee, where we generate fat binary for device |
| 2598 | // code and pass to host in Backend phase. |
| 2599 | if (CudaDeviceActions.empty() || |
| 2600 | (CurPhase == phases::Backend && Relocatable) || |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2601 | CurPhase == phases::Assemble) |
| 2602 | return ABRT_Success; |
| 2603 | |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2604 | assert(((CurPhase == phases::Link && Relocatable) || |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2605 | CudaDeviceActions.size() == GpuArchList.size()) && |
| 2606 | "Expecting one action per GPU architecture."); |
| 2607 | assert(!CompileHostOnly && |
| 2608 | "Not expecting CUDA actions in host-only compilation."); |
| 2609 | |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2610 | if (!Relocatable && CurPhase == phases::Backend) { |
| 2611 | // If we are in backend phase, we attempt to generate the fat binary. |
| 2612 | // We compile each arch to IR and use a link action to generate code |
| 2613 | // object containing ISA. Then we use a special "link" action to create |
| 2614 | // a fat binary containing all the code objects for different GPU's. |
| 2615 | // The fat binary is then an input to the host action. |
| 2616 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2617 | // Create a link action to link device IR with device library |
| 2618 | // and generate ISA. |
| 2619 | ActionList AL; |
| 2620 | AL.push_back(CudaDeviceActions[I]); |
| 2621 | CudaDeviceActions[I] = |
| 2622 | C.MakeAction<LinkJobAction>(AL, types::TY_Image); |
| 2623 | |
| 2624 | // OffloadingActionBuilder propagates device arch until an offload |
| 2625 | // action. Since the next action for creating fatbin does |
| 2626 | // not have device arch, whereas the above link action and its input |
| 2627 | // have device arch, an offload action is needed to stop the null |
| 2628 | // device arch of the next action being propagated to the above link |
| 2629 | // action. |
| 2630 | OffloadAction::DeviceDependences DDep; |
| 2631 | DDep.add(*CudaDeviceActions[I], *ToolChains.front(), |
| 2632 | CudaArchToString(GpuArchList[I]), AssociatedOffloadKind); |
| 2633 | CudaDeviceActions[I] = C.MakeAction<OffloadAction>( |
| 2634 | DDep, CudaDeviceActions[I]->getType()); |
| 2635 | } |
| 2636 | // Create HIP fat binary with a special "link" action. |
| 2637 | CudaFatBinary = |
| 2638 | C.MakeAction<LinkJobAction>(CudaDeviceActions, |
| 2639 | types::TY_HIP_FATBIN); |
| 2640 | |
Yaxun Liu | 2bcc951 | 2018-11-14 04:47:31 +0000 | [diff] [blame] | 2641 | if (!CompileDeviceOnly) { |
| 2642 | DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr, |
| 2643 | AssociatedOffloadKind); |
| 2644 | // Clear the fat binary, it is already a dependence to an host |
| 2645 | // action. |
| 2646 | CudaFatBinary = nullptr; |
| 2647 | } |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2648 | |
| 2649 | // Remove the CUDA actions as they are already connected to an host |
| 2650 | // action or fat binary. |
| 2651 | CudaDeviceActions.clear(); |
| 2652 | |
Yaxun Liu | 2bcc951 | 2018-11-14 04:47:31 +0000 | [diff] [blame] | 2653 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2654 | } else if (CurPhase == phases::Link) { |
| 2655 | // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch. |
| 2656 | // This happens to each device action originated from each input file. |
| 2657 | // Later on, device actions in DeviceLinkerInputs are used to create |
| 2658 | // device link actions in appendLinkDependences and the created device |
| 2659 | // link actions are passed to the offload action as device dependence. |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2660 | DeviceLinkerInputs.resize(CudaDeviceActions.size()); |
| 2661 | auto LI = DeviceLinkerInputs.begin(); |
| 2662 | for (auto *A : CudaDeviceActions) { |
| 2663 | LI->push_back(A); |
| 2664 | ++LI; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2667 | // We will pass the device action as a host dependence, so we don't |
| 2668 | // need to do anything else with them. |
| 2669 | CudaDeviceActions.clear(); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2670 | return ABRT_Success; |
| 2671 | } |
| 2672 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2673 | // By default, we produce an action for each device arch. |
| 2674 | for (Action *&A : CudaDeviceActions) |
| 2675 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A, |
| 2676 | AssociatedOffloadKind); |
| 2677 | |
| 2678 | return ABRT_Success; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2681 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2682 | // Append a new link action for each device. |
| 2683 | unsigned I = 0; |
| 2684 | for (auto &LI : DeviceLinkerInputs) { |
| 2685 | auto *DeviceLinkAction = |
| 2686 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2687 | DA.add(*DeviceLinkAction, *ToolChains[0], |
| 2688 | CudaArchToString(GpuArchList[I]), AssociatedOffloadKind); |
| 2689 | ++I; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2690 | } |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2691 | } |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2692 | |
| 2693 | bool initialize() override { |
| 2694 | Relocatable = Args.hasFlag(options::OPT_fgpu_rdc, |
| 2695 | options::OPT_fno_gpu_rdc, /*Default=*/false); |
| 2696 | |
| 2697 | return CudaActionBuilderBase::initialize(); |
| 2698 | } |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2699 | }; |
| 2700 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2701 | /// OpenMP action builder. The host bitcode is passed to the device frontend |
| 2702 | /// and all the device linked images are passed to the host link phase. |
| 2703 | class OpenMPActionBuilder final : public DeviceActionBuilder { |
| 2704 | /// The OpenMP actions for the current input. |
| 2705 | ActionList OpenMPDeviceActions; |
| 2706 | |
| 2707 | /// The linker inputs obtained for each toolchain. |
| 2708 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
| 2709 | |
| 2710 | public: |
| 2711 | OpenMPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2712 | const Driver::InputList &Inputs) |
| 2713 | : DeviceActionBuilder(C, Args, Inputs, Action::OFK_OpenMP) {} |
| 2714 | |
| 2715 | ActionBuilderReturnCode |
| 2716 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2717 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2718 | PhasesTy &Phases) override { |
Alexey Bataev | a5178f5 | 2018-09-28 16:17:59 +0000 | [diff] [blame] | 2719 | if (OpenMPDeviceActions.empty()) |
| 2720 | return ABRT_Inactive; |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2721 | |
| 2722 | // We should always have an action for each input. |
| 2723 | assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2724 | "Number of OpenMP actions and toolchains do not match."); |
| 2725 | |
| 2726 | // The host only depends on device action in the linking phase, when all |
| 2727 | // the device images have to be embedded in the host image. |
| 2728 | if (CurPhase == phases::Link) { |
| 2729 | assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2730 | "Toolchains and linker inputs sizes do not match."); |
| 2731 | auto LI = DeviceLinkerInputs.begin(); |
| 2732 | for (auto *A : OpenMPDeviceActions) { |
| 2733 | LI->push_back(A); |
| 2734 | ++LI; |
| 2735 | } |
| 2736 | |
| 2737 | // We passed the device action as a host dependence, so we don't need to |
| 2738 | // do anything else with them. |
| 2739 | OpenMPDeviceActions.clear(); |
| 2740 | return ABRT_Success; |
| 2741 | } |
| 2742 | |
| 2743 | // By default, we produce an action for each device arch. |
| 2744 | for (Action *&A : OpenMPDeviceActions) |
| 2745 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2746 | |
| 2747 | return ABRT_Success; |
| 2748 | } |
| 2749 | |
| 2750 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2751 | |
| 2752 | // If this is an input action replicate it for each OpenMP toolchain. |
| 2753 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2754 | OpenMPDeviceActions.clear(); |
| 2755 | for (unsigned I = 0; I < ToolChains.size(); ++I) |
| 2756 | OpenMPDeviceActions.push_back( |
| 2757 | C.MakeAction<InputAction>(IA->getInputArg(), IA->getType())); |
| 2758 | return ABRT_Success; |
| 2759 | } |
| 2760 | |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2761 | // If this is an unbundling action use it as is for each OpenMP toolchain. |
| 2762 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
| 2763 | OpenMPDeviceActions.clear(); |
Alexey Bataev | a5178f5 | 2018-09-28 16:17:59 +0000 | [diff] [blame] | 2764 | auto *IA = cast<InputAction>(UA->getInputs().back()); |
| 2765 | std::string FileName = IA->getInputArg().getAsString(Args); |
| 2766 | // Check if the type of the file is the same as the action. Do not |
| 2767 | // unbundle it if it is not. Do not unbundle .so files, for example, |
| 2768 | // which are not object files. |
| 2769 | if (IA->getType() == types::TY_Object && |
| 2770 | (!llvm::sys::path::has_extension(FileName) || |
| 2771 | types::lookupTypeForExtension( |
| 2772 | llvm::sys::path::extension(FileName).drop_front()) != |
| 2773 | types::TY_Object)) |
| 2774 | return ABRT_Inactive; |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2775 | for (unsigned I = 0; I < ToolChains.size(); ++I) { |
| 2776 | OpenMPDeviceActions.push_back(UA); |
| 2777 | UA->registerDependentActionInfo( |
| 2778 | ToolChains[I], /*BoundArch=*/StringRef(), Action::OFK_OpenMP); |
| 2779 | } |
| 2780 | return ABRT_Success; |
| 2781 | } |
| 2782 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2783 | // When generating code for OpenMP we use the host compile phase result as |
| 2784 | // a dependence to the device compile phase so that it can learn what |
| 2785 | // declarations should be emitted. However, this is not the only use for |
| 2786 | // the host action, so we prevent it from being collapsed. |
| 2787 | if (isa<CompileJobAction>(HostAction)) { |
| 2788 | HostAction->setCannotBeCollapsedWithNextDependentAction(); |
| 2789 | assert(ToolChains.size() == OpenMPDeviceActions.size() && |
| 2790 | "Toolchains and device action sizes do not match."); |
| 2791 | OffloadAction::HostDependence HDep( |
| 2792 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2793 | /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2794 | auto TC = ToolChains.begin(); |
| 2795 | for (Action *&A : OpenMPDeviceActions) { |
| 2796 | assert(isa<CompileJobAction>(A)); |
| 2797 | OffloadAction::DeviceDependences DDep; |
| 2798 | DDep.add(*A, **TC, /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2799 | A = C.MakeAction<OffloadAction>(HDep, DDep); |
| 2800 | ++TC; |
| 2801 | } |
| 2802 | } |
| 2803 | return ABRT_Success; |
| 2804 | } |
| 2805 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2806 | void appendTopLevelActions(ActionList &AL) override { |
| 2807 | if (OpenMPDeviceActions.empty()) |
| 2808 | return; |
| 2809 | |
| 2810 | // We should always have an action for each input. |
| 2811 | assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2812 | "Number of OpenMP actions and toolchains do not match."); |
| 2813 | |
| 2814 | // Append all device actions followed by the proper offload action. |
| 2815 | auto TI = ToolChains.begin(); |
| 2816 | for (auto *A : OpenMPDeviceActions) { |
| 2817 | OffloadAction::DeviceDependences Dep; |
| 2818 | Dep.add(*A, **TI, /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2819 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2820 | ++TI; |
| 2821 | } |
| 2822 | // We no longer need the action stored in this builder. |
| 2823 | OpenMPDeviceActions.clear(); |
| 2824 | } |
| 2825 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2826 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2827 | assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2828 | "Toolchains and linker inputs sizes do not match."); |
| 2829 | |
| 2830 | // Append a new link action for each device. |
| 2831 | auto TC = ToolChains.begin(); |
| 2832 | for (auto &LI : DeviceLinkerInputs) { |
| 2833 | auto *DeviceLinkAction = |
| 2834 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2835 | DA.add(*DeviceLinkAction, **TC, /*BoundArch=*/nullptr, |
| 2836 | Action::OFK_OpenMP); |
| 2837 | ++TC; |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | bool initialize() override { |
| 2842 | // Get the OpenMP toolchains. If we don't get any, the action builder will |
| 2843 | // know there is nothing to do related to OpenMP offloading. |
| 2844 | auto OpenMPTCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); |
| 2845 | for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE; |
| 2846 | ++TI) |
| 2847 | ToolChains.push_back(TI->second); |
| 2848 | |
| 2849 | DeviceLinkerInputs.resize(ToolChains.size()); |
| 2850 | return false; |
| 2851 | } |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2852 | |
| 2853 | bool canUseBundlerUnbundler() const override { |
| 2854 | // OpenMP should use bundled files whenever possible. |
| 2855 | return true; |
| 2856 | } |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2857 | }; |
| 2858 | |
| 2859 | /// |
| 2860 | /// TODO: Add the implementation for other specialized builders here. |
| 2861 | /// |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2862 | |
| 2863 | /// Specialized builders being used by this offloading action builder. |
| 2864 | SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders; |
| 2865 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2866 | /// Flag set to true if all valid builders allow file bundling/unbundling. |
| 2867 | bool CanUseBundler; |
| 2868 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2869 | public: |
| 2870 | OffloadingActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2871 | const Driver::InputList &Inputs) |
Samuel Antao | 10e905c | 2016-10-27 01:08:58 +0000 | [diff] [blame] | 2872 | : C(C) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2873 | // Create a specialized builder for each device toolchain. |
| 2874 | |
| 2875 | IsValid = true; |
| 2876 | |
| 2877 | // Create a specialized builder for CUDA. |
| 2878 | SpecializedBuilders.push_back(new CudaActionBuilder(C, Args, Inputs)); |
| 2879 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2880 | // Create a specialized builder for HIP. |
| 2881 | SpecializedBuilders.push_back(new HIPActionBuilder(C, Args, Inputs)); |
| 2882 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2883 | // Create a specialized builder for OpenMP. |
| 2884 | SpecializedBuilders.push_back(new OpenMPActionBuilder(C, Args, Inputs)); |
| 2885 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2886 | // |
| 2887 | // TODO: Build other specialized builders here. |
| 2888 | // |
| 2889 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2890 | // Initialize all the builders, keeping track of errors. If all valid |
| 2891 | // builders agree that we can use bundling, set the flag to true. |
| 2892 | unsigned ValidBuilders = 0u; |
| 2893 | unsigned ValidBuildersSupportingBundling = 0u; |
| 2894 | for (auto *SB : SpecializedBuilders) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2895 | IsValid = IsValid && !SB->initialize(); |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2896 | |
| 2897 | // Update the counters if the builder is valid. |
| 2898 | if (SB->isValid()) { |
| 2899 | ++ValidBuilders; |
| 2900 | if (SB->canUseBundlerUnbundler()) |
| 2901 | ++ValidBuildersSupportingBundling; |
| 2902 | } |
| 2903 | } |
| 2904 | CanUseBundler = |
| 2905 | ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling; |
Artem Belevich | f981e30b | 2016-08-02 22:37:47 +0000 | [diff] [blame] | 2906 | } |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 2907 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2908 | ~OffloadingActionBuilder() { |
| 2909 | for (auto *SB : SpecializedBuilders) |
| 2910 | delete SB; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 2911 | } |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2912 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2913 | /// Generate an action that adds device dependences (if any) to a host action. |
| 2914 | /// If no device dependence actions exist, just return the host action \a |
| 2915 | /// HostAction. If an error is found or if no builder requires the host action |
| 2916 | /// to be generated, return nullptr. |
| 2917 | Action * |
| 2918 | addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg, |
| 2919 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2920 | DeviceActionBuilder::PhasesTy &Phases) { |
| 2921 | if (!IsValid) |
| 2922 | return nullptr; |
Justin Lebar | 7bf7798 | 2016-01-11 23:27:13 +0000 | [diff] [blame] | 2923 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2924 | if (SpecializedBuilders.empty()) |
| 2925 | return HostAction; |
| 2926 | |
| 2927 | assert(HostAction && "Invalid host action!"); |
| 2928 | |
| 2929 | OffloadAction::DeviceDependences DDeps; |
| 2930 | // Check if all the programming models agree we should not emit the host |
| 2931 | // action. Also, keep track of the offloading kinds employed. |
| 2932 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 2933 | unsigned InactiveBuilders = 0u; |
| 2934 | unsigned IgnoringBuilders = 0u; |
| 2935 | for (auto *SB : SpecializedBuilders) { |
| 2936 | if (!SB->isValid()) { |
| 2937 | ++InactiveBuilders; |
| 2938 | continue; |
| 2939 | } |
| 2940 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2941 | auto RetCode = |
| 2942 | SB->getDeviceDependences(DDeps, CurPhase, FinalPhase, Phases); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2943 | |
| 2944 | // If the builder explicitly says the host action should be ignored, |
| 2945 | // we need to increment the variable that tracks the builders that request |
| 2946 | // the host object to be ignored. |
| 2947 | if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host) |
| 2948 | ++IgnoringBuilders; |
| 2949 | |
| 2950 | // Unless the builder was inactive for this action, we have to record the |
| 2951 | // offload kind because the host will have to use it. |
| 2952 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 2953 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 2954 | } |
| 2955 | |
| 2956 | // If all builders agree that the host object should be ignored, just return |
| 2957 | // nullptr. |
| 2958 | if (IgnoringBuilders && |
| 2959 | SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders)) |
| 2960 | return nullptr; |
| 2961 | |
| 2962 | if (DDeps.getActions().empty()) |
| 2963 | return HostAction; |
| 2964 | |
| 2965 | // We have dependences we need to bundle together. We use an offload action |
| 2966 | // for that. |
| 2967 | OffloadAction::HostDependence HDep( |
| 2968 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2969 | /*BoundArch=*/nullptr, DDeps); |
| 2970 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2973 | /// Generate an action that adds a host dependence to a device action. The |
| 2974 | /// results will be kept in this action builder. Return true if an error was |
| 2975 | /// found. |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2976 | bool addHostDependenceToDeviceActions(Action *&HostAction, |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2977 | const Arg *InputArg) { |
| 2978 | if (!IsValid) |
| 2979 | return true; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2980 | |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2981 | // If we are supporting bundling/unbundling and the current action is an |
| 2982 | // input action of non-source file, we replace the host action by the |
| 2983 | // unbundling action. The bundler tool has the logic to detect if an input |
| 2984 | // is a bundle or not and if the input is not a bundle it assumes it is a |
| 2985 | // host file. Therefore it is safe to create an unbundling action even if |
| 2986 | // the input is not a bundle. |
| 2987 | if (CanUseBundler && isa<InputAction>(HostAction) && |
| 2988 | InputArg->getOption().getKind() == llvm::opt::Option::InputClass && |
| 2989 | !types::isSrcFile(HostAction->getType())) { |
| 2990 | auto UnbundlingHostAction = |
| 2991 | C.MakeAction<OffloadUnbundlingJobAction>(HostAction); |
| 2992 | UnbundlingHostAction->registerDependentActionInfo( |
| 2993 | C.getSingleOffloadToolChain<Action::OFK_Host>(), |
Yaxun Liu | f37b50a | 2018-07-24 01:03:44 +0000 | [diff] [blame] | 2994 | /*BoundArch=*/StringRef(), Action::OFK_Host); |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2995 | HostAction = UnbundlingHostAction; |
| 2996 | } |
| 2997 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2998 | assert(HostAction && "Invalid host action!"); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2999 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3000 | // Register the offload kinds that are used. |
| 3001 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 3002 | for (auto *SB : SpecializedBuilders) { |
| 3003 | if (!SB->isValid()) |
| 3004 | continue; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3005 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3006 | auto RetCode = SB->addDeviceDepences(HostAction); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3007 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3008 | // Host dependences for device actions are not compatible with that same |
| 3009 | // action being ignored. |
| 3010 | assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host && |
| 3011 | "Host dependence not expected to be ignored.!"); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3012 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3013 | // Unless the builder was inactive for this action, we have to record the |
| 3014 | // offload kind because the host will have to use it. |
| 3015 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 3016 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 3017 | } |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3018 | |
Alexey Bataev | a5178f5 | 2018-09-28 16:17:59 +0000 | [diff] [blame] | 3019 | // Do not use unbundler if the Host does not depend on device action. |
| 3020 | if (OffloadKind == Action::OFK_None && CanUseBundler) |
| 3021 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) |
| 3022 | HostAction = UA->getInputs().back(); |
| 3023 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3024 | return false; |
| 3025 | } |
| 3026 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3027 | /// Add the offloading top level actions to the provided action list. This |
| 3028 | /// function can replace the host action by a bundling action if the |
| 3029 | /// programming models allow it. |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3030 | bool appendTopLevelActions(ActionList &AL, Action *HostAction, |
| 3031 | const Arg *InputArg) { |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3032 | // Get the device actions to be appended. |
| 3033 | ActionList OffloadAL; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3034 | for (auto *SB : SpecializedBuilders) { |
| 3035 | if (!SB->isValid()) |
| 3036 | continue; |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3037 | SB->appendTopLevelActions(OffloadAL); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3040 | // If we can use the bundler, replace the host action by the bundling one in |
Yaxun Liu | 2bcc951 | 2018-11-14 04:47:31 +0000 | [diff] [blame] | 3041 | // the resulting list. Otherwise, just append the device actions. For |
| 3042 | // device only compilation, HostAction is a null pointer, therefore only do |
| 3043 | // this when HostAction is not a null pointer. |
| 3044 | if (CanUseBundler && HostAction && !OffloadAL.empty()) { |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3045 | // Add the host action to the list in order to create the bundling action. |
| 3046 | OffloadAL.push_back(HostAction); |
| 3047 | |
| 3048 | // We expect that the host action was just appended to the action list |
| 3049 | // before this method was called. |
| 3050 | assert(HostAction == AL.back() && "Host action not in the list??"); |
| 3051 | HostAction = C.MakeAction<OffloadBundlingJobAction>(OffloadAL); |
| 3052 | AL.back() = HostAction; |
| 3053 | } else |
| 3054 | AL.append(OffloadAL.begin(), OffloadAL.end()); |
| 3055 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3056 | // Propagate to the current host action (if any) the offload information |
| 3057 | // associated with the current input. |
| 3058 | if (HostAction) |
| 3059 | HostAction->propagateHostOffloadInfo(InputArgToOffloadKindMap[InputArg], |
| 3060 | /*BoundArch=*/nullptr); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3061 | return false; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3062 | } |
Artem Belevich | 5bde4e0 | 2015-07-20 20:02:54 +0000 | [diff] [blame] | 3063 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3064 | /// Processes the host linker action. This currently consists of replacing it |
| 3065 | /// with an offload action if there are device link objects and propagate to |
| 3066 | /// the host action all the offload kinds used in the current compilation. The |
| 3067 | /// resulting action is returned. |
| 3068 | Action *processHostLinkAction(Action *HostAction) { |
| 3069 | // Add all the dependences from the device linking actions. |
| 3070 | OffloadAction::DeviceDependences DDeps; |
| 3071 | for (auto *SB : SpecializedBuilders) { |
| 3072 | if (!SB->isValid()) |
| 3073 | continue; |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 3074 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3075 | SB->appendLinkDependences(DDeps); |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 3076 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3077 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3078 | // Calculate all the offload kinds used in the current compilation. |
| 3079 | unsigned ActiveOffloadKinds = 0u; |
| 3080 | for (auto &I : InputArgToOffloadKindMap) |
| 3081 | ActiveOffloadKinds |= I.second; |
| 3082 | |
| 3083 | // If we don't have device dependencies, we don't have to create an offload |
| 3084 | // action. |
| 3085 | if (DDeps.getActions().empty()) { |
| 3086 | // Propagate all the active kinds to host action. Given that it is a link |
| 3087 | // action it is assumed to depend on all actions generated so far. |
| 3088 | HostAction->propagateHostOffloadInfo(ActiveOffloadKinds, |
| 3089 | /*BoundArch=*/nullptr); |
| 3090 | return HostAction; |
| 3091 | } |
| 3092 | |
| 3093 | // Create the offload action with all dependences. When an offload action |
| 3094 | // 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] | 3095 | // to do that explicitly here. |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3096 | OffloadAction::HostDependence HDep( |
| 3097 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 3098 | /*BoundArch*/ nullptr, ActiveOffloadKinds); |
| 3099 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
| 3100 | } |
| 3101 | }; |
| 3102 | } // anonymous namespace. |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3103 | |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 3104 | void Driver::BuildActions(Compilation &C, DerivedArgList &Args, |
| 3105 | const InputList &Inputs, ActionList &Actions) const { |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 3106 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 3107 | |
Daniel Dunbar | 34c4187 | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 3108 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 3109 | Diag(clang::diag::err_drv_no_input_files); |
| 3110 | return; |
| 3111 | } |
| 3112 | |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 3113 | Arg *FinalPhaseArg; |
| 3114 | phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg); |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 3115 | |
Bob Haarman | aaf5191 | 2017-02-27 19:40:19 +0000 | [diff] [blame] | 3116 | if (FinalPhase == phases::Link) { |
| 3117 | if (Args.hasArg(options::OPT_emit_llvm)) |
| 3118 | Diag(clang::diag::err_drv_emit_llvm_link); |
| 3119 | if (IsCLMode() && LTOMode != LTOK_None && |
| 3120 | !Args.getLastArgValue(options::OPT_fuse_ld_EQ).equals_lower("lld")) |
| 3121 | Diag(clang::diag::err_drv_lto_without_lld); |
Rafael Espindola | e802564 | 2013-08-25 14:27:09 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3124 | // Reject -Z* at the top level, these options should never have been exposed |
| 3125 | // by gcc. |
Daniel Dunbar | dd76524 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 3126 | if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) |
Daniel Dunbar | 0e75994 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 3127 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 3128 | |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3129 | // Diagnose misuse of /Fo. |
| 3130 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) { |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3131 | StringRef V = A->getValue(); |
Hans Wennborg | 6164753 | 2014-11-17 19:16:36 +0000 | [diff] [blame] | 3132 | if (Inputs.size() > 1 && !V.empty() && |
| 3133 | !llvm::sys::path::is_separator(V.back())) { |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3134 | // Check whether /Fo tries to name an output file for multiple inputs. |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3135 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3136 | << A->getSpelling() << V; |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3137 | Args.eraseArg(options::OPT__SLASH_Fo); |
| 3138 | } |
| 3139 | } |
| 3140 | |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3141 | // Diagnose misuse of /Fa. |
| 3142 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) { |
| 3143 | StringRef V = A->getValue(); |
Hans Wennborg | 6164753 | 2014-11-17 19:16:36 +0000 | [diff] [blame] | 3144 | if (Inputs.size() > 1 && !V.empty() && |
| 3145 | !llvm::sys::path::is_separator(V.back())) { |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3146 | // Check whether /Fa tries to name an asm file for multiple inputs. |
| 3147 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3148 | << A->getSpelling() << V; |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3149 | Args.eraseArg(options::OPT__SLASH_Fa); |
| 3150 | } |
| 3151 | } |
| 3152 | |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 3153 | // Diagnose misuse of /o. |
| 3154 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) { |
| 3155 | if (A->getValue()[0] == '\0') { |
| 3156 | // It has to have a value. |
| 3157 | Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1; |
| 3158 | Args.eraseArg(options::OPT__SLASH_o); |
| 3159 | } |
| 3160 | } |
| 3161 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 3162 | // Ignore /Yc/Yu if both /Yc and /Yu passed but with different filenames. |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3163 | Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); |
| 3164 | Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3165 | if (YcArg && YuArg && strcmp(YcArg->getValue(), YuArg->getValue()) != 0) { |
| 3166 | Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl); |
| 3167 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3168 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 3169 | YcArg = YuArg = nullptr; |
| 3170 | } |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3171 | if (YcArg && Inputs.size() > 1) { |
| 3172 | Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl); |
| 3173 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3174 | YcArg = nullptr; |
| 3175 | } |
Erich Keane | eaca388 | 2018-08-17 13:43:39 +0000 | [diff] [blame] | 3176 | if (FinalPhase == phases::Preprocess || Args.hasArg(options::OPT__SLASH_Y_)) { |
| 3177 | // If only preprocessing or /Y- is used, all pch handling is disabled. |
| 3178 | // Rather than check for it everywhere, just remove clang-cl pch-related |
| 3179 | // flags here. |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3180 | Args.eraseArg(options::OPT__SLASH_Fp); |
| 3181 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3182 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 3183 | YcArg = YuArg = nullptr; |
| 3184 | } |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3185 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3186 | // Builder to be used to build offloading actions. |
| 3187 | OffloadingActionBuilder OffloadBuilder(C, Args, Inputs); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3188 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3189 | // Construct the actions to perform. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3190 | HeaderModulePrecompileJobAction *HeaderModuleAction = nullptr; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3191 | ActionList LinkerInputs; |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 3192 | |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3193 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL; |
Artem Belevich | 5bde4e0 | 2015-07-20 20:02:54 +0000 | [diff] [blame] | 3194 | for (auto &I : Inputs) { |
| 3195 | types::ID InputType = I.first; |
| 3196 | const Arg *InputArg = I.second; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3197 | |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3198 | PL.clear(); |
| 3199 | types::getCompilationPhases(InputType, PL); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3200 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3201 | // If the first step comes after the final phase we are doing as part of |
| 3202 | // this compilation, warn the user about it. |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3203 | phases::ID InitialPhase = PL[0]; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3204 | if (InitialPhase > FinalPhase) { |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 3205 | if (InputArg->isClaimed()) |
| 3206 | continue; |
| 3207 | |
Daniel Dunbar | adc5c7c | 2009-03-19 07:57:08 +0000 | [diff] [blame] | 3208 | // Claim here to avoid the more general unused warning. |
| 3209 | InputArg->claim(); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3210 | |
Daniel Dunbar | 2db3e73 | 2011-04-20 15:44:48 +0000 | [diff] [blame] | 3211 | // Suppress all unused style warnings with -Qunused-arguments |
| 3212 | if (Args.hasArg(options::OPT_Qunused_arguments)) |
| 3213 | continue; |
| 3214 | |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3215 | // Special case when final phase determined by binary name, rather than |
| 3216 | // by a command-line argument with a corresponding Arg. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 3217 | if (CCCIsCPP()) |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3218 | Diag(clang::diag::warn_drv_input_file_unused_by_cpp) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3219 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3220 | // Special case '-E' warning on a previously preprocessed file to make |
| 3221 | // more sense. |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3222 | else if (InitialPhase == phases::Compile && |
| 3223 | FinalPhase == phases::Preprocess && |
| 3224 | getPreprocessedType(InputType) == types::TY_INVALID) |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3225 | Diag(clang::diag::warn_drv_preprocessed_input_file_unused) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3226 | << InputArg->getAsString(Args) << !!FinalPhaseArg |
| 3227 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3228 | else |
| 3229 | Diag(clang::diag::warn_drv_input_file_unused) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3230 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase) |
| 3231 | << !!FinalPhaseArg |
| 3232 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3233 | continue; |
| 3234 | } |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3235 | |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3236 | if (YcArg) { |
| 3237 | // Add a separate precompile phase for the compile phase. |
| 3238 | if (FinalPhase >= phases::Compile) { |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3239 | const types::ID HeaderType = lookupHeaderTypeForSourceType(InputType); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3240 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PCHPL; |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3241 | types::getCompilationPhases(HeaderType, PCHPL); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3242 | // Build the pipeline for the pch file. |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3243 | Action *ClangClPch = |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 3244 | C.MakeAction<InputAction>(*InputArg, HeaderType); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3245 | for (phases::ID Phase : PCHPL) |
| 3246 | ClangClPch = ConstructPhaseAction(C, Args, Phase, ClangClPch); |
| 3247 | assert(ClangClPch); |
| 3248 | Actions.push_back(ClangClPch); |
| 3249 | // The driver currently exits after the first failed command. This |
| 3250 | // relies on that behavior, to make sure if the pch generation fails, |
| 3251 | // the main compilation won't run. |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 3252 | // FIXME: If the main compilation fails, the PCH generation should |
| 3253 | // probably not be considered successful either. |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3254 | } |
| 3255 | } |
| 3256 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3257 | // Build the pipeline for this file. |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3258 | Action *Current = C.MakeAction<InputAction>(*InputArg, InputType); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3259 | |
| 3260 | // Use the current host action in any of the offloading actions, if |
| 3261 | // required. |
| 3262 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3263 | break; |
| 3264 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3265 | for (SmallVectorImpl<phases::ID>::iterator i = PL.begin(), e = PL.end(); |
| 3266 | i != e; ++i) { |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3267 | phases::ID Phase = *i; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3268 | |
| 3269 | // We are done if this step is past what the user requested. |
| 3270 | if (Phase > FinalPhase) |
| 3271 | break; |
| 3272 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3273 | // Add any offload action the host action depends on. |
| 3274 | Current = OffloadBuilder.addDeviceDependencesToHostAction( |
| 3275 | Current, InputArg, Phase, FinalPhase, PL); |
| 3276 | if (!Current) |
| 3277 | break; |
| 3278 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3279 | // Queue linker inputs. |
| 3280 | if (Phase == phases::Link) { |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3281 | assert((i + 1) == e && "linking must be final compilation step."); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3282 | LinkerInputs.push_back(Current); |
| 3283 | Current = nullptr; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3284 | break; |
| 3285 | } |
| 3286 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3287 | // Each precompiled header file after a module file action is a module |
| 3288 | // header of that same module file, rather than being compiled to a |
| 3289 | // separate PCH. |
| 3290 | if (Phase == phases::Precompile && HeaderModuleAction && |
| 3291 | getPrecompiledType(InputType) == types::TY_PCH) { |
| 3292 | HeaderModuleAction->addModuleHeaderInput(Current); |
| 3293 | Current = nullptr; |
| 3294 | break; |
| 3295 | } |
| 3296 | |
| 3297 | // FIXME: Should we include any prior module file outputs as inputs of |
| 3298 | // later actions in the same command line? |
| 3299 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3300 | // Otherwise construct the appropriate action. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3301 | Action *NewCurrent = ConstructPhaseAction(C, Args, Phase, Current); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3302 | |
| 3303 | // We didn't create a new action, so we will just move to the next phase. |
| 3304 | if (NewCurrent == Current) |
Daniel Dunbar | 1386495 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 3305 | continue; |
| 3306 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3307 | if (auto *HMA = dyn_cast<HeaderModulePrecompileJobAction>(NewCurrent)) |
| 3308 | HeaderModuleAction = HMA; |
| 3309 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3310 | Current = NewCurrent; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3311 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3312 | // Use the current host action in any of the offloading actions, if |
| 3313 | // required. |
| 3314 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3315 | break; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3316 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3317 | if (Current->getType() == types::TY_Nothing) |
| 3318 | break; |
| 3319 | } |
| 3320 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3321 | // If we ended with something, add to the output list. |
| 3322 | if (Current) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3323 | Actions.push_back(Current); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3324 | |
| 3325 | // Add any top level actions generated for offloading. |
| 3326 | OffloadBuilder.appendTopLevelActions(Actions, Current, InputArg); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 3327 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3328 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3329 | // Add a link action if necessary. |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3330 | if (!LinkerInputs.empty()) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3331 | Action *LA = C.MakeAction<LinkJobAction>(LinkerInputs, types::TY_Image); |
| 3332 | LA = OffloadBuilder.processHostLinkAction(LA); |
| 3333 | Actions.push_back(LA); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3334 | } |
Daniel Dunbar | c7a67b7 | 2009-12-22 23:19:32 +0000 | [diff] [blame] | 3335 | |
| 3336 | // If we are linking, claim any options which are obviously only used for |
| 3337 | // compilation. |
Hans Wennborg | a8ef14f | 2013-09-17 00:03:41 +0000 | [diff] [blame] | 3338 | if (FinalPhase == phases::Link && PL.size() == 1) { |
Daniel Dunbar | c7a67b7 | 2009-12-22 23:19:32 +0000 | [diff] [blame] | 3339 | Args.ClaimAllArgs(options::OPT_CompileOnly_Group); |
Hans Wennborg | a8ef14f | 2013-09-17 00:03:41 +0000 | [diff] [blame] | 3340 | Args.ClaimAllArgs(options::OPT_cl_compile_Group); |
| 3341 | } |
| 3342 | |
| 3343 | // Claim ignored clang-cl options. |
| 3344 | Args.ClaimAllArgs(options::OPT_cl_ignored_Group); |
Artem Belevich | baae093 | 2015-07-28 21:01:30 +0000 | [diff] [blame] | 3345 | |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 3346 | // Claim --cuda-host-only and --cuda-compile-host-device, which may be passed |
| 3347 | // to non-CUDA compilations and should not trigger warnings there. |
Artem Belevich | baae093 | 2015-07-28 21:01:30 +0000 | [diff] [blame] | 3348 | Args.ClaimAllArgs(options::OPT_cuda_host_only); |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 3349 | Args.ClaimAllArgs(options::OPT_cuda_compile_host_device); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 3352 | Action *Driver::ConstructPhaseAction( |
| 3353 | Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input, |
| 3354 | Action::OffloadKind TargetDeviceOffloadKind) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 3355 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3356 | |
| 3357 | // Some types skip the assembler phase (e.g., llvm-bc), but we can't |
| 3358 | // encode this in the steps because the intermediate type depends on |
| 3359 | // arguments. Just special case here. |
| 3360 | if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm) |
| 3361 | return Input; |
| 3362 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3363 | // Build the appropriate action. |
| 3364 | switch (Phase) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3365 | case phases::Link: |
| 3366 | llvm_unreachable("link action invalid here."); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3367 | case phases::Preprocess: { |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3368 | types::ID OutputTy; |
| 3369 | // -{M, MM} alter the output type. |
Daniel Dunbar | 86aed7d | 2010-12-08 21:33:40 +0000 | [diff] [blame] | 3370 | if (Args.hasArg(options::OPT_M, options::OPT_MM)) { |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3371 | OutputTy = types::TY_Dependencies; |
| 3372 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 3373 | OutputTy = Input->getType(); |
| 3374 | if (!Args.hasFlag(options::OPT_frewrite_includes, |
Justin Bogner | ce8245b | 2014-06-24 08:01:01 +0000 | [diff] [blame] | 3375 | options::OPT_fno_rewrite_includes, false) && |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 3376 | !Args.hasFlag(options::OPT_frewrite_imports, |
| 3377 | options::OPT_fno_rewrite_imports, false) && |
Justin Bogner | ce8245b | 2014-06-24 08:01:01 +0000 | [diff] [blame] | 3378 | !CCGenDiagnostics) |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 3379 | OutputTy = types::getPreprocessedType(OutputTy); |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3380 | assert(OutputTy != types::TY_INVALID && |
| 3381 | "Cannot preprocess this input type!"); |
| 3382 | } |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3383 | return C.MakeAction<PreprocessJobAction>(Input, OutputTy); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3384 | } |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3385 | case phases::Precompile: { |
Richard Smith | dd4ad3d | 2016-08-30 19:06:26 +0000 | [diff] [blame] | 3386 | types::ID OutputTy = getPrecompiledType(Input->getType()); |
| 3387 | assert(OutputTy != types::TY_INVALID && |
| 3388 | "Cannot precompile this input type!"); |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3389 | |
| 3390 | // If we're given a module name, precompile header file inputs as a |
| 3391 | // module, not as a precompiled header. |
| 3392 | const char *ModName = nullptr; |
| 3393 | if (OutputTy == types::TY_PCH) { |
| 3394 | if (Arg *A = Args.getLastArg(options::OPT_fmodule_name_EQ)) |
| 3395 | ModName = A->getValue(); |
| 3396 | if (ModName) |
| 3397 | OutputTy = types::TY_ModuleFile; |
| 3398 | } |
| 3399 | |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3400 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 3401 | // Syntax checks should not emit a PCH file |
| 3402 | OutputTy = types::TY_Nothing; |
| 3403 | } |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3404 | |
| 3405 | if (ModName) |
| 3406 | return C.MakeAction<HeaderModulePrecompileJobAction>(Input, OutputTy, |
| 3407 | ModName); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3408 | return C.MakeAction<PrecompileJobAction>(Input, OutputTy); |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3409 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3410 | case phases::Compile: { |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3411 | if (Args.hasArg(options::OPT_fsyntax_only)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3412 | return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3413 | if (Args.hasArg(options::OPT_rewrite_objc)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3414 | return C.MakeAction<CompileJobAction>(Input, types::TY_RewrittenObjC); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3415 | if (Args.hasArg(options::OPT_rewrite_legacy_objc)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3416 | return C.MakeAction<CompileJobAction>(Input, |
| 3417 | types::TY_RewrittenLegacyObjC); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3418 | if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3419 | return C.MakeAction<AnalyzeJobAction>(Input, types::TY_Plist); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3420 | if (Args.hasArg(options::OPT__migrate)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3421 | return C.MakeAction<MigrateJobAction>(Input, types::TY_Remap); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3422 | if (Args.hasArg(options::OPT_emit_ast)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3423 | return C.MakeAction<CompileJobAction>(Input, types::TY_AST); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3424 | if (Args.hasArg(options::OPT_module_file_info)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3425 | return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3426 | if (Args.hasArg(options::OPT_verify_pch)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3427 | return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing); |
| 3428 | return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC); |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3429 | } |
| 3430 | case phases::Backend: { |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 3431 | if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3432 | types::ID Output = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3433 | 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] | 3434 | return C.MakeAction<BackendJobAction>(Input, Output); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3435 | } |
| 3436 | if (Args.hasArg(options::OPT_emit_llvm)) { |
Shuxin Yang | 4b3c7ff | 2013-08-23 21:34:57 +0000 | [diff] [blame] | 3437 | types::ID Output = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3438 | 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] | 3439 | return C.MakeAction<BackendJobAction>(Input, Output); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3440 | } |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3441 | return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3442 | } |
| 3443 | case phases::Assemble: |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 3444 | return C.MakeAction<AssembleJobAction>(std::move(Input), types::TY_Object); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3447 | llvm_unreachable("invalid phase in ConstructPhaseAction"); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 3450 | void Driver::BuildJobs(Compilation &C) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 3451 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3452 | |
| 3453 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 3454 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3455 | // It is an error to provide a -o option if we are making multiple output |
| 3456 | // files. |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3457 | if (FinalOutput) { |
| 3458 | unsigned NumOutputs = 0; |
Saleem Abdulrasool | da3f4e5 | 2014-12-29 21:02:47 +0000 | [diff] [blame] | 3459 | for (const Action *A : C.getActions()) |
| 3460 | if (A->getType() != types::TY_Nothing) |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3461 | ++NumOutputs; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3462 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3463 | if (NumOutputs > 1) { |
| 3464 | Diag(clang::diag::err_drv_output_argument_with_multiple_files); |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3465 | FinalOutput = nullptr; |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3466 | } |
| 3467 | } |
| 3468 | |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3469 | // Collect the list of architectures. |
| 3470 | llvm::StringSet<> ArchNames; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3471 | if (C.getDefaultToolChain().getTriple().isOSBinFormatMachO()) |
| 3472 | for (const Arg *A : C.getArgs()) |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3473 | if (A->getOption().matches(options::OPT_arch)) |
| 3474 | ArchNames.insert(A->getValue()); |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3475 | |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3476 | // Set of (Action, canonical ToolChain triple) pairs we've built jobs for. |
| 3477 | std::map<std::pair<const Action *, std::string>, InputInfo> CachedResults; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3478 | for (Action *A : C.getActions()) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3479 | // If we are linking an image for multiple archs then the linker wants |
| 3480 | // -arch_multiple and -final_output <final image name>. Unfortunately, this |
| 3481 | // 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] | 3482 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3483 | // FIXME: This is a hack; find a cleaner way to integrate this into the |
| 3484 | // process. |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3485 | const char *LinkingOutput = nullptr; |
Daniel Dunbar | dd76524 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 3486 | if (isa<LipoJobAction>(A)) { |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3487 | if (FinalOutput) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 3488 | LinkingOutput = FinalOutput->getValue(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3489 | else |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 3490 | LinkingOutput = getDefaultImageName(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3493 | BuildJobsForAction(C, A, &C.getDefaultToolChain(), |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3494 | /*BoundArch*/ StringRef(), |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3495 | /*AtTopLevel*/ true, |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3496 | /*MultipleArchs*/ ArchNames.size() > 1, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3497 | /*LinkingOutput*/ LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3498 | /*TargetDeviceOffloadKind*/ Action::OFK_None); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3499 | } |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3500 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3501 | // If the user passed -Qunused-arguments or there were errors, don't warn |
| 3502 | // about any unused arguments. |
Argyrios Kyrtzidis | 31448a4 | 2010-11-18 21:47:07 +0000 | [diff] [blame] | 3503 | if (Diags.hasErrorOccurred() || |
Daniel Dunbar | a3cfbe3 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 3504 | C.getArgs().hasArg(options::OPT_Qunused_arguments)) |
Daniel Dunbar | d175d97 | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 3505 | return; |
| 3506 | |
Daniel Dunbar | 58399ae | 2009-03-29 22:24:54 +0000 | [diff] [blame] | 3507 | // Claim -### here. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3508 | (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3509 | |
Nico Weber | 47bf505 | 2016-04-25 21:15:49 +0000 | [diff] [blame] | 3510 | // Claim --driver-mode, --rsp-quoting, it was handled earlier. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3511 | (void)C.getArgs().hasArg(options::OPT_driver_mode); |
Nico Weber | 47bf505 | 2016-04-25 21:15:49 +0000 | [diff] [blame] | 3512 | (void)C.getArgs().hasArg(options::OPT_rsp_quoting); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 3513 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3514 | for (Arg *A : C.getArgs()) { |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3515 | // 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] | 3516 | // DiagnosticsEngine, so that extra values, position, and so on could be |
| 3517 | // printed. |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3518 | if (!A->isClaimed()) { |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 3519 | if (A->getOption().hasFlag(options::NoArgumentUnused)) |
Daniel Dunbar | a3cfbe3 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 3520 | continue; |
| 3521 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3522 | // Suppress the warning automatically if this is just a flag, and it is an |
| 3523 | // instance of an argument we already claimed. |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3524 | const Option &Opt = A->getOption(); |
Michael J. Spencer | ad3ccc3 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 3525 | if (Opt.getKind() == Option::FlagClass) { |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3526 | bool DuplicateClaimed = false; |
| 3527 | |
Sean Silva | 14facf3 | 2015-06-09 01:57:17 +0000 | [diff] [blame] | 3528 | for (const Arg *AA : C.getArgs().filtered(&Opt)) { |
| 3529 | if (AA->isClaimed()) { |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3530 | DuplicateClaimed = true; |
| 3531 | break; |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | if (DuplicateClaimed) |
| 3536 | continue; |
| 3537 | } |
| 3538 | |
Ehsan Akhgari | d851833 | 2016-01-25 21:14:52 +0000 | [diff] [blame] | 3539 | // In clang-cl, don't mention unknown arguments here since they have |
| 3540 | // already been warned about. |
| 3541 | if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) |
| 3542 | Diag(clang::diag::warn_drv_unused_argument) |
| 3543 | << A->getAsString(C.getArgs()); |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3544 | } |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3545 | } |
Daniel Dunbar | 95e6b19 | 2009-03-13 22:12:33 +0000 | [diff] [blame] | 3546 | } |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3547 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3548 | namespace { |
| 3549 | /// Utility class to control the collapse of dependent actions and select the |
| 3550 | /// tools accordingly. |
| 3551 | class ToolSelector final { |
| 3552 | /// The tool chain this selector refers to. |
| 3553 | const ToolChain &TC; |
Daniel Dunbar | f9ff350 | 2010-05-14 02:03:00 +0000 | [diff] [blame] | 3554 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3555 | /// The compilation this selector refers to. |
| 3556 | const Compilation &C; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3557 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3558 | /// The base action this selector refers to. |
| 3559 | const JobAction *BaseAction; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3560 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3561 | /// Set to true if the current toolchain refers to host actions. |
| 3562 | bool IsHostSelector; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3563 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3564 | /// Set to true if save-temps and embed-bitcode functionalities are active. |
| 3565 | bool SaveTemps; |
| 3566 | bool EmbedBitcode; |
| 3567 | |
| 3568 | /// Get previous dependent action or null if that does not exist. If |
| 3569 | /// \a CanBeCollapsed is false, that action must be legal to collapse or |
| 3570 | /// null will be returned. |
| 3571 | const JobAction *getPrevDependentAction(const ActionList &Inputs, |
| 3572 | ActionList &SavedOffloadAction, |
| 3573 | bool CanBeCollapsed = true) { |
| 3574 | // An option can be collapsed only if it has a single input. |
| 3575 | if (Inputs.size() != 1) |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3576 | return nullptr; |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3577 | |
| 3578 | Action *CurAction = *Inputs.begin(); |
| 3579 | if (CanBeCollapsed && |
| 3580 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3581 | return nullptr; |
| 3582 | |
| 3583 | // If the input action is an offload action. Look through it and save any |
| 3584 | // offload action that can be dropped in the event of a collapse. |
| 3585 | if (auto *OA = dyn_cast<OffloadAction>(CurAction)) { |
| 3586 | // If the dependent action is a device action, we will attempt to collapse |
| 3587 | // only with other device actions. Otherwise, we would do the same but |
| 3588 | // with host actions only. |
| 3589 | if (!IsHostSelector) { |
| 3590 | if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) { |
| 3591 | CurAction = |
| 3592 | OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true); |
| 3593 | if (CanBeCollapsed && |
| 3594 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3595 | return nullptr; |
| 3596 | SavedOffloadAction.push_back(OA); |
| 3597 | return dyn_cast<JobAction>(CurAction); |
| 3598 | } |
| 3599 | } else if (OA->hasHostDependence()) { |
| 3600 | CurAction = OA->getHostDependence(); |
| 3601 | if (CanBeCollapsed && |
| 3602 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3603 | return nullptr; |
| 3604 | SavedOffloadAction.push_back(OA); |
| 3605 | return dyn_cast<JobAction>(CurAction); |
| 3606 | } |
| 3607 | return nullptr; |
| 3608 | } |
| 3609 | |
| 3610 | return dyn_cast<JobAction>(CurAction); |
| 3611 | } |
| 3612 | |
| 3613 | /// Return true if an assemble action can be collapsed. |
| 3614 | bool canCollapseAssembleAction() const { |
| 3615 | return TC.useIntegratedAs() && !SaveTemps && |
| 3616 | !C.getArgs().hasArg(options::OPT_via_file_asm) && |
| 3617 | !C.getArgs().hasArg(options::OPT__SLASH_FA) && |
| 3618 | !C.getArgs().hasArg(options::OPT__SLASH_Fa); |
| 3619 | } |
| 3620 | |
| 3621 | /// Return true if a preprocessor action can be collapsed. |
| 3622 | bool canCollapsePreprocessorAction() const { |
| 3623 | return !C.getArgs().hasArg(options::OPT_no_integrated_cpp) && |
| 3624 | !C.getArgs().hasArg(options::OPT_traditional_cpp) && !SaveTemps && |
| 3625 | !C.getArgs().hasArg(options::OPT_rewrite_objc); |
| 3626 | } |
| 3627 | |
| 3628 | /// Struct that relates an action with the offload actions that would be |
| 3629 | /// collapsed with it. |
| 3630 | struct JobActionInfo final { |
| 3631 | /// The action this info refers to. |
| 3632 | const JobAction *JA = nullptr; |
| 3633 | /// The offload actions we need to take care off if this action is |
| 3634 | /// collapsed. |
| 3635 | ActionList SavedOffloadAction; |
| 3636 | }; |
| 3637 | |
| 3638 | /// Append collapsed offload actions from the give nnumber of elements in the |
| 3639 | /// action info array. |
| 3640 | static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction, |
| 3641 | ArrayRef<JobActionInfo> &ActionInfo, |
| 3642 | unsigned ElementNum) { |
| 3643 | assert(ElementNum <= ActionInfo.size() && "Invalid number of elements."); |
| 3644 | for (unsigned I = 0; I < ElementNum; ++I) |
| 3645 | CollapsedOffloadAction.append(ActionInfo[I].SavedOffloadAction.begin(), |
| 3646 | ActionInfo[I].SavedOffloadAction.end()); |
| 3647 | } |
| 3648 | |
| 3649 | /// Functions that attempt to perform the combining. They detect if that is |
| 3650 | /// legal, and if so they update the inputs \a Inputs and the offload action |
| 3651 | /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with |
| 3652 | /// the combined action is returned. If the combining is not legal or if the |
| 3653 | /// tool does not exist, null is returned. |
| 3654 | /// Currently three kinds of collapsing are supported: |
| 3655 | /// - Assemble + Backend + Compile; |
| 3656 | /// - Assemble + Backend ; |
| 3657 | /// - Backend + Compile. |
| 3658 | const Tool * |
| 3659 | combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3660 | ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3661 | ActionList &CollapsedOffloadAction) { |
| 3662 | if (ActionInfo.size() < 3 || !canCollapseAssembleAction()) |
| 3663 | return nullptr; |
| 3664 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3665 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3666 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[2].JA); |
| 3667 | if (!AJ || !BJ || !CJ) |
| 3668 | return nullptr; |
| 3669 | |
| 3670 | // Get compiler tool. |
| 3671 | const Tool *T = TC.SelectTool(*CJ); |
| 3672 | if (!T) |
| 3673 | return nullptr; |
| 3674 | |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 3675 | // When using -fembed-bitcode, it is required to have the same tool (clang) |
| 3676 | // for both CompilerJA and BackendJA. Otherwise, combine two stages. |
| 3677 | if (EmbedBitcode) { |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3678 | const Tool *BT = TC.SelectTool(*BJ); |
| 3679 | if (BT == T) |
| 3680 | return nullptr; |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 3681 | } |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3682 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3683 | if (!T->hasIntegratedAssembler()) |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3684 | return nullptr; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3685 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3686 | Inputs = CJ->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3687 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3688 | /*NumElements=*/3); |
| 3689 | return T; |
| 3690 | } |
| 3691 | const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo, |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3692 | ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3693 | ActionList &CollapsedOffloadAction) { |
| 3694 | if (ActionInfo.size() < 2 || !canCollapseAssembleAction()) |
| 3695 | return nullptr; |
| 3696 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3697 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3698 | if (!AJ || !BJ) |
| 3699 | return nullptr; |
| 3700 | |
| 3701 | // Retrieve the compile job, backend action must always be preceded by one. |
| 3702 | ActionList CompileJobOffloadActions; |
| 3703 | auto *CJ = getPrevDependentAction(BJ->getInputs(), CompileJobOffloadActions, |
| 3704 | /*CanBeCollapsed=*/false); |
| 3705 | if (!AJ || !BJ || !CJ) |
| 3706 | return nullptr; |
| 3707 | |
| 3708 | assert(isa<CompileJobAction>(CJ) && |
| 3709 | "Expecting compile job preceding backend job."); |
| 3710 | |
| 3711 | // Get compiler tool. |
| 3712 | const Tool *T = TC.SelectTool(*CJ); |
| 3713 | if (!T) |
| 3714 | return nullptr; |
| 3715 | |
| 3716 | if (!T->hasIntegratedAssembler()) |
| 3717 | return nullptr; |
| 3718 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3719 | Inputs = BJ->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3720 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3721 | /*NumElements=*/2); |
| 3722 | return T; |
| 3723 | } |
| 3724 | const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3725 | ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3726 | ActionList &CollapsedOffloadAction) { |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3727 | if (ActionInfo.size() < 2) |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3728 | return nullptr; |
| 3729 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[0].JA); |
| 3730 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[1].JA); |
| 3731 | if (!BJ || !CJ) |
| 3732 | return nullptr; |
| 3733 | |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3734 | // Check if the initial input (to the compile job or its predessor if one |
| 3735 | // exists) is LLVM bitcode. In that case, no preprocessor step is required |
| 3736 | // and we can still collapse the compile and backend jobs when we have |
| 3737 | // -save-temps. I.e. there is no need for a separate compile job just to |
| 3738 | // emit unoptimized bitcode. |
| 3739 | bool InputIsBitcode = true; |
| 3740 | for (size_t i = 1; i < ActionInfo.size(); i++) |
| 3741 | if (ActionInfo[i].JA->getType() != types::TY_LLVM_BC && |
| 3742 | ActionInfo[i].JA->getType() != types::TY_LTO_BC) { |
| 3743 | InputIsBitcode = false; |
| 3744 | break; |
| 3745 | } |
| 3746 | if (!InputIsBitcode && !canCollapsePreprocessorAction()) |
| 3747 | return nullptr; |
| 3748 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3749 | // Get compiler tool. |
| 3750 | const Tool *T = TC.SelectTool(*CJ); |
| 3751 | if (!T) |
| 3752 | return nullptr; |
| 3753 | |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3754 | if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode)) |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3755 | return nullptr; |
| 3756 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3757 | Inputs = CJ->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3758 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3759 | /*NumElements=*/2); |
| 3760 | return T; |
| 3761 | } |
| 3762 | |
| 3763 | /// Updates the inputs if the obtained tool supports combining with |
| 3764 | /// preprocessor action, and the current input is indeed a preprocessor |
| 3765 | /// action. If combining results in the collapse of offloading actions, those |
| 3766 | /// are appended to \a CollapsedOffloadAction. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3767 | void combineWithPreprocessor(const Tool *T, ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3768 | ActionList &CollapsedOffloadAction) { |
| 3769 | if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP()) |
| 3770 | return; |
| 3771 | |
| 3772 | // Attempt to get a preprocessor action dependence. |
| 3773 | ActionList PreprocessJobOffloadActions; |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3774 | ActionList NewInputs; |
| 3775 | for (Action *A : Inputs) { |
| 3776 | auto *PJ = getPrevDependentAction({A}, PreprocessJobOffloadActions); |
| 3777 | if (!PJ || !isa<PreprocessJobAction>(PJ)) { |
| 3778 | NewInputs.push_back(A); |
| 3779 | continue; |
| 3780 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3781 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3782 | // This is legal to combine. Append any offload action we found and add the |
| 3783 | // current input to preprocessor inputs. |
| 3784 | CollapsedOffloadAction.append(PreprocessJobOffloadActions.begin(), |
| 3785 | PreprocessJobOffloadActions.end()); |
| 3786 | NewInputs.append(PJ->input_begin(), PJ->input_end()); |
| 3787 | } |
| 3788 | Inputs = NewInputs; |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | public: |
| 3792 | ToolSelector(const JobAction *BaseAction, const ToolChain &TC, |
| 3793 | const Compilation &C, bool SaveTemps, bool EmbedBitcode) |
| 3794 | : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps), |
| 3795 | EmbedBitcode(EmbedBitcode) { |
| 3796 | assert(BaseAction && "Invalid base action."); |
| 3797 | IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None; |
| 3798 | } |
| 3799 | |
| 3800 | /// Check if a chain of actions can be combined and return the tool that can |
| 3801 | /// handle the combination of actions. The pointer to the current inputs \a |
| 3802 | /// Inputs and the list of offload actions \a CollapsedOffloadActions |
| 3803 | /// connected to collapsed actions are updated accordingly. The latter enables |
| 3804 | /// the caller of the selector to process them afterwards instead of just |
| 3805 | /// dropping them. If no suitable tool is found, null will be returned. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3806 | const Tool *getTool(ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3807 | ActionList &CollapsedOffloadAction) { |
| 3808 | // |
| 3809 | // Get the largest chain of actions that we could combine. |
| 3810 | // |
| 3811 | |
| 3812 | SmallVector<JobActionInfo, 5> ActionChain(1); |
| 3813 | ActionChain.back().JA = BaseAction; |
| 3814 | while (ActionChain.back().JA) { |
| 3815 | const Action *CurAction = ActionChain.back().JA; |
| 3816 | |
| 3817 | // Grow the chain by one element. |
| 3818 | ActionChain.resize(ActionChain.size() + 1); |
| 3819 | JobActionInfo &AI = ActionChain.back(); |
| 3820 | |
| 3821 | // Attempt to fill it with the |
| 3822 | AI.JA = |
| 3823 | getPrevDependentAction(CurAction->getInputs(), AI.SavedOffloadAction); |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3824 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3825 | |
| 3826 | // Pop the last action info as it could not be filled. |
| 3827 | ActionChain.pop_back(); |
| 3828 | |
| 3829 | // |
| 3830 | // Attempt to combine actions. If all combining attempts failed, just return |
| 3831 | // the tool of the provided action. At the end we attempt to combine the |
| 3832 | // action with any preprocessor action it may depend on. |
| 3833 | // |
| 3834 | |
| 3835 | const Tool *T = combineAssembleBackendCompile(ActionChain, Inputs, |
| 3836 | CollapsedOffloadAction); |
| 3837 | if (!T) |
| 3838 | T = combineAssembleBackend(ActionChain, Inputs, CollapsedOffloadAction); |
| 3839 | if (!T) |
| 3840 | T = combineBackendCompile(ActionChain, Inputs, CollapsedOffloadAction); |
| 3841 | if (!T) { |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3842 | Inputs = BaseAction->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3843 | T = TC.SelectTool(*BaseAction); |
| 3844 | } |
| 3845 | |
| 3846 | combineWithPreprocessor(T, Inputs, CollapsedOffloadAction); |
| 3847 | return T; |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3848 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3849 | }; |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3852 | /// Return a string that uniquely identifies the result of a job. The bound arch |
| 3853 | /// is not necessarily represented in the toolchain's triple -- for example, |
| 3854 | /// armv7 and armv7s both map to the same triple -- so we need both in our map. |
| 3855 | /// Also, we need to add the offloading device kind, as the same tool chain can |
| 3856 | /// be used for host and device for some programming models, e.g. OpenMP. |
| 3857 | static std::string GetTriplePlusArchString(const ToolChain *TC, |
| 3858 | StringRef BoundArch, |
| 3859 | Action::OffloadKind OffloadKind) { |
Justin Lebar | 55c8332 | 2016-01-16 03:30:08 +0000 | [diff] [blame] | 3860 | std::string TriplePlusArch = TC->getTriple().normalize(); |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3861 | if (!BoundArch.empty()) { |
Justin Lebar | 55c8332 | 2016-01-16 03:30:08 +0000 | [diff] [blame] | 3862 | TriplePlusArch += "-"; |
| 3863 | TriplePlusArch += BoundArch; |
| 3864 | } |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 3865 | TriplePlusArch += "-"; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3866 | TriplePlusArch += Action::GetOffloadKindName(OffloadKind); |
| 3867 | return TriplePlusArch; |
| 3868 | } |
| 3869 | |
| 3870 | InputInfo Driver::BuildJobsForAction( |
| 3871 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
| 3872 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
| 3873 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
| 3874 | Action::OffloadKind TargetDeviceOffloadKind) const { |
| 3875 | std::pair<const Action *, std::string> ActionTC = { |
| 3876 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3877 | auto CachedResult = CachedResults.find(ActionTC); |
| 3878 | if (CachedResult != CachedResults.end()) { |
| 3879 | return CachedResult->second; |
| 3880 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3881 | InputInfo Result = BuildJobsForActionNoCache( |
| 3882 | C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3883 | CachedResults, TargetDeviceOffloadKind); |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3884 | CachedResults[ActionTC] = Result; |
| 3885 | return Result; |
| 3886 | } |
| 3887 | |
| 3888 | InputInfo Driver::BuildJobsForActionNoCache( |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3889 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3890 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3891 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3892 | Action::OffloadKind TargetDeviceOffloadKind) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3893 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | c4acf9d | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 3894 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3895 | InputInfoList OffloadDependencesInputInfo; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3896 | bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3897 | if (const OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
Alex Lorenz | 4003a98 | 2017-08-08 11:22:21 +0000 | [diff] [blame] | 3898 | // The 'Darwin' toolchain is initialized only when its arguments are |
| 3899 | // computed. Get the default arguments for OFK_None to ensure that |
| 3900 | // initialization is performed before processing the offload action. |
| 3901 | // FIXME: Remove when darwin's toolchain is initialized during construction. |
| 3902 | C.getArgsForToolChain(TC, BoundArch, Action::OFK_None); |
| 3903 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3904 | // The offload action is expected to be used in four different situations. |
| 3905 | // |
| 3906 | // a) Set a toolchain/architecture/kind for a host action: |
| 3907 | // Host Action 1 -> OffloadAction -> Host Action 2 |
| 3908 | // |
| 3909 | // b) Set a toolchain/architecture/kind for a device action; |
| 3910 | // Device Action 1 -> OffloadAction -> Device Action 2 |
| 3911 | // |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3912 | // c) Specify a device dependence to a host action; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3913 | // Device Action 1 _ |
| 3914 | // \ |
| 3915 | // Host Action 1 ---> OffloadAction -> Host Action 2 |
| 3916 | // |
| 3917 | // d) Specify a host dependence to a device action. |
| 3918 | // Host Action 1 _ |
| 3919 | // \ |
| 3920 | // Device Action 1 ---> OffloadAction -> Device Action 2 |
| 3921 | // |
| 3922 | // For a) and b), we just return the job generated for the dependence. For |
| 3923 | // c) and d) we override the current action with the host/device dependence |
| 3924 | // if the current toolchain is host/device and set the offload dependences |
| 3925 | // info with the jobs obtained from the device/host dependence(s). |
| 3926 | |
| 3927 | // If there is a single device option, just generate the job for it. |
| 3928 | if (OA->hasSingleDeviceDependence()) { |
| 3929 | InputInfo DevA; |
| 3930 | OA->doOnEachDeviceDependence([&](Action *DepA, const ToolChain *DepTC, |
| 3931 | const char *DepBoundArch) { |
| 3932 | DevA = |
| 3933 | BuildJobsForAction(C, DepA, DepTC, DepBoundArch, AtTopLevel, |
| 3934 | /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3935 | CachedResults, DepA->getOffloadingDeviceKind()); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3936 | }); |
| 3937 | return DevA; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3938 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3939 | |
| 3940 | // If 'Action 2' is host, we generate jobs for the device dependences and |
| 3941 | // override the current action with the host dependence. Otherwise, we |
| 3942 | // generate the host dependences and override the action with the device |
| 3943 | // dependence. The dependences can't therefore be a top-level action. |
| 3944 | OA->doOnEachDependence( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3945 | /*IsHostDependence=*/BuildingForOffloadDevice, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3946 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 3947 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
| 3948 | C, DepA, DepTC, DepBoundArch, /*AtTopLevel=*/false, |
| 3949 | /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3950 | DepA->getOffloadingDeviceKind())); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3951 | }); |
| 3952 | |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3953 | A = BuildingForOffloadDevice |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3954 | ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true) |
| 3955 | : OA->getHostDependence(); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3958 | if (const InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3959 | // FIXME: It would be nice to not claim this here; maybe the old scheme of |
| 3960 | // just using Args was better? |
Daniel Dunbar | 5cdf3e0 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 3961 | const Arg &Input = IA->getInputArg(); |
| 3962 | Input.claim(); |
Daniel Dunbar | 35cbfeb | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 3963 | if (Input.getOption().matches(options::OPT_INPUT)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 3964 | const char *Name = Input.getValue(); |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 3965 | return InputInfo(A, Name, /* BaseInput = */ Name); |
Douglas Katzman | 678d0cb | 2015-06-16 18:01:24 +0000 | [diff] [blame] | 3966 | } |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 3967 | return InputInfo(A, &Input, /* BaseInput = */ ""); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { |
Chad Rosier | a78a6eb | 2012-04-27 16:50:38 +0000 | [diff] [blame] | 3971 | const ToolChain *TC; |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3972 | StringRef ArchName = BAA->getArchName(); |
Daniel Dunbar | 1ef3f2a | 2009-09-08 23:37:19 +0000 | [diff] [blame] | 3973 | |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3974 | if (!ArchName.empty()) |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 3975 | TC = &getToolChain(C.getArgs(), |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 3976 | computeTargetTriple(*this, TargetTriple, |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 3977 | C.getArgs(), ArchName)); |
Chad Rosier | a78a6eb | 2012-04-27 16:50:38 +0000 | [diff] [blame] | 3978 | else |
| 3979 | TC = &C.getDefaultToolChain(); |
Daniel Dunbar | 1ef3f2a | 2009-09-08 23:37:19 +0000 | [diff] [blame] | 3980 | |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 3981 | return BuildJobsForAction(C, *BAA->input_begin(), TC, ArchName, AtTopLevel, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3982 | MultipleArchs, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3983 | TargetDeviceOffloadKind); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3986 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3987 | ActionList Inputs = A->getInputs(); |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3988 | |
| 3989 | const JobAction *JA = cast<JobAction>(A); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3990 | ActionList CollapsedOffloadActions; |
| 3991 | |
Mehdi Amini | 6683e22 | 2017-01-24 18:12:25 +0000 | [diff] [blame] | 3992 | ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), |
| 3993 | embedBitcodeInObject() && !isUsingLTO()); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3994 | const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions); |
| 3995 | |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 3996 | if (!T) |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 3997 | return InputInfo(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3998 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3999 | // If we've collapsed action list that contained OffloadAction we |
| 4000 | // need to build jobs for host/device-side inputs it may have held. |
| 4001 | for (const auto *OA : CollapsedOffloadActions) |
| 4002 | cast<OffloadAction>(OA)->doOnEachDependence( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4003 | /*IsHostDependence=*/BuildingForOffloadDevice, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4004 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 4005 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
Artem Belevich | bee2f41 | 2016-08-22 18:50:34 +0000 | [diff] [blame] | 4006 | C, DepA, DepTC, DepBoundArch, /* AtTopLevel */ false, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4007 | /*MultipleArchs=*/!!DepBoundArch, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4008 | DepA->getOffloadingDeviceKind())); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4009 | }); |
Artem Belevich | f8144ab | 2015-08-27 18:10:41 +0000 | [diff] [blame] | 4010 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4011 | // Only use pipes when there is exactly one input. |
Daniel Dunbar | 1a093d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 4012 | InputInfoList InputInfos; |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 4013 | for (const Action *Input : Inputs) { |
Eric Christopher | 14668dd | 2013-02-18 00:38:25 +0000 | [diff] [blame] | 4014 | // Treat dsymutil and verify sub-jobs as being at the top-level too, they |
| 4015 | // shouldn't get temporary output names. |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 4016 | // FIXME: Clean this up. |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 4017 | bool SubJobAtTopLevel = |
| 4018 | AtTopLevel && (isa<DsymutilJobAction>(A) || isa<VerifyJobAction>(A)); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4019 | InputInfos.push_back(BuildJobsForAction( |
| 4020 | C, Input, TC, BoundArch, SubJobAtTopLevel, MultipleArchs, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4021 | CachedResults, A->getOffloadingDeviceKind())); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4024 | // Always use the first input as the base input. |
| 4025 | const char *BaseInput = InputInfos[0].getBaseInput(); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4026 | |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 4027 | // ... except dsymutil actions, which use their actual input as the base |
| 4028 | // input. |
| 4029 | if (JA->getType() == types::TY_dSYM) |
| 4030 | BaseInput = InputInfos[0].getFilename(); |
| 4031 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 4032 | // ... and in header module compilations, which use the module name. |
| 4033 | if (auto *ModuleJA = dyn_cast<HeaderModulePrecompileJobAction>(JA)) |
| 4034 | BaseInput = ModuleJA->getModuleName(); |
| 4035 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4036 | // Append outputs of offload device jobs to the input list |
| 4037 | if (!OffloadDependencesInputInfo.empty()) |
| 4038 | InputInfos.append(OffloadDependencesInputInfo.begin(), |
| 4039 | OffloadDependencesInputInfo.end()); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 4040 | |
Vedant Kumar | 18286cf | 2016-07-27 23:02:20 +0000 | [diff] [blame] | 4041 | // Set the effective triple of the toolchain for the duration of this job. |
| 4042 | llvm::Triple EffectiveTriple; |
| 4043 | const ToolChain &ToolTC = T->getToolChain(); |
Samuel Antao | 31fef98 | 2016-10-27 17:39:44 +0000 | [diff] [blame] | 4044 | const ArgList &Args = |
| 4045 | C.getArgsForToolChain(TC, BoundArch, A->getOffloadingDeviceKind()); |
Vedant Kumar | 18286cf | 2016-07-27 23:02:20 +0000 | [diff] [blame] | 4046 | if (InputInfos.size() != 1) { |
| 4047 | EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args)); |
| 4048 | } else { |
| 4049 | // Pass along the input type if it can be unambiguously determined. |
| 4050 | EffectiveTriple = llvm::Triple( |
| 4051 | ToolTC.ComputeEffectiveClangTriple(Args, InputInfos[0].getType())); |
| 4052 | } |
| 4053 | RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple); |
| 4054 | |
Daniel Dunbar | d00272f | 2010-08-02 02:38:15 +0000 | [diff] [blame] | 4055 | // Determine the place to write output to, if any. |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 4056 | InputInfo Result; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4057 | InputInfoList UnbundlingResults; |
| 4058 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(JA)) { |
| 4059 | // If we have an unbundling job, we need to create results for all the |
| 4060 | // outputs. We also update the results cache so that other actions using |
| 4061 | // this unbundling action can get the right results. |
| 4062 | for (auto &UI : UA->getDependentActionsInfo()) { |
| 4063 | assert(UI.DependentOffloadKind != Action::OFK_None && |
| 4064 | "Unbundling with no offloading??"); |
| 4065 | |
| 4066 | // Unbundling actions are never at the top level. When we generate the |
| 4067 | // offloading prefix, we also do that for the host file because the |
| 4068 | // unbundling action does not change the type of the output which can |
| 4069 | // cause a overwrite. |
| 4070 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 4071 | UI.DependentOffloadKind, |
| 4072 | UI.DependentToolChain->getTriple().normalize(), |
| 4073 | /*CreatePrefixForHost=*/true); |
| 4074 | auto CurI = InputInfo( |
Yaxun Liu | 609f752 | 2018-05-11 19:02:18 +0000 | [diff] [blame] | 4075 | UA, |
| 4076 | GetNamedOutputPath(C, *UA, BaseInput, UI.DependentBoundArch, |
| 4077 | /*AtTopLevel=*/false, |
| 4078 | MultipleArchs || |
| 4079 | UI.DependentOffloadKind == Action::OFK_HIP, |
| 4080 | OffloadingPrefix), |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4081 | BaseInput); |
| 4082 | // Save the unbundling result. |
| 4083 | UnbundlingResults.push_back(CurI); |
| 4084 | |
| 4085 | // Get the unique string identifier for this dependence and cache the |
| 4086 | // result. |
Yaxun Liu | 470b833 | 2018-06-06 19:44:10 +0000 | [diff] [blame] | 4087 | StringRef Arch; |
| 4088 | if (TargetDeviceOffloadKind == Action::OFK_HIP) { |
| 4089 | if (UI.DependentOffloadKind == Action::OFK_Host) |
Yaxun Liu | f37b50a | 2018-07-24 01:03:44 +0000 | [diff] [blame] | 4090 | Arch = StringRef(); |
Yaxun Liu | 470b833 | 2018-06-06 19:44:10 +0000 | [diff] [blame] | 4091 | else |
| 4092 | Arch = UI.DependentBoundArch; |
| 4093 | } else |
| 4094 | Arch = BoundArch; |
| 4095 | |
| 4096 | CachedResults[{A, GetTriplePlusArchString(UI.DependentToolChain, Arch, |
| 4097 | UI.DependentOffloadKind)}] = |
| 4098 | CurI; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | // Now that we have all the results generated, select the one that should be |
| 4102 | // returned for the current depending action. |
| 4103 | std::pair<const Action *, std::string> ActionTC = { |
| 4104 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
| 4105 | assert(CachedResults.find(ActionTC) != CachedResults.end() && |
| 4106 | "Result does not exist??"); |
| 4107 | Result = CachedResults[ActionTC]; |
| 4108 | } else if (JA->getType() == types::TY_Nothing) |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4109 | Result = InputInfo(A, BaseInput); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4110 | else { |
| 4111 | // We only have to generate a prefix for the host if this is not a top-level |
| 4112 | // action. |
| 4113 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 4114 | A->getOffloadingDeviceKind(), TC->getTriple().normalize(), |
| 4115 | /*CreatePrefixForHost=*/!!A->getOffloadingHostActiveKinds() && |
| 4116 | !AtTopLevel); |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4117 | Result = InputInfo(A, GetNamedOutputPath(C, *JA, BaseInput, BoundArch, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4118 | AtTopLevel, MultipleArchs, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4119 | OffloadingPrefix), |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4120 | BaseInput); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4121 | } |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4122 | |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 4123 | if (CCCPrintBindings && !CCGenDiagnostics) { |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 4124 | llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"' |
| 4125 | << " - \"" << T->getName() << "\", inputs: ["; |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 4126 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
| 4127 | llvm::errs() << InputInfos[i].getAsString(); |
| 4128 | if (i + 1 != e) |
| 4129 | llvm::errs() << ", "; |
| 4130 | } |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4131 | if (UnbundlingResults.empty()) |
| 4132 | llvm::errs() << "], output: " << Result.getAsString() << "\n"; |
| 4133 | else { |
| 4134 | llvm::errs() << "], outputs: ["; |
| 4135 | for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) { |
| 4136 | llvm::errs() << UnbundlingResults[i].getAsString(); |
| 4137 | if (i + 1 != e) |
| 4138 | llvm::errs() << ", "; |
| 4139 | } |
| 4140 | llvm::errs() << "] \n"; |
| 4141 | } |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 4142 | } else { |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4143 | if (UnbundlingResults.empty()) |
| 4144 | T->ConstructJob( |
| 4145 | C, *JA, Result, InputInfos, |
| 4146 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 4147 | LinkingOutput); |
| 4148 | else |
Samuel Antao | 7108bf3 | 2016-11-03 15:41:50 +0000 | [diff] [blame] | 4149 | T->ConstructJobMultipleOutputs( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4150 | C, *JA, UnbundlingResults, InputInfos, |
| 4151 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 4152 | LinkingOutput); |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 4153 | } |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 4154 | return Result; |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 4157 | const char *Driver::getDefaultImageName() const { |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4158 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 4159 | return Target.isOSWindows() ? "a.exe" : "a.out"; |
| 4160 | } |
| 4161 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4162 | /// Create output filename based on ArgValue, which could either be a |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4163 | /// full filename, filename without extension, or a directory. If ArgValue |
| 4164 | /// does not provide a filename, then use BaseName, and use the extension |
| 4165 | /// suitable for FileType. |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4166 | static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4167 | StringRef BaseName, |
| 4168 | types::ID FileType) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4169 | SmallString<128> Filename = ArgValue; |
Justin Bogner | 5aaf2e7 | 2014-06-26 20:59:36 +0000 | [diff] [blame] | 4170 | |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 4171 | if (ArgValue.empty()) { |
| 4172 | // If the argument is empty, output to BaseName in the current dir. |
| 4173 | Filename = BaseName; |
| 4174 | } else if (llvm::sys::path::is_separator(Filename.back())) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4175 | // If the argument is a directory, output to BaseName in that dir. |
| 4176 | llvm::sys::path::append(Filename, BaseName); |
| 4177 | } |
| 4178 | |
| 4179 | if (!llvm::sys::path::has_extension(ArgValue)) { |
| 4180 | // If the argument didn't provide an extension, then set it. |
| 4181 | const char *Extension = types::getTypeTempSuffix(FileType, true); |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 4182 | |
| 4183 | if (FileType == types::TY_Image && |
| 4184 | Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) { |
| 4185 | // The output file is a dll. |
| 4186 | Extension = "dll"; |
| 4187 | } |
| 4188 | |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4189 | llvm::sys::path::replace_extension(Filename, Extension); |
| 4190 | } |
| 4191 | |
| 4192 | return Args.MakeArgString(Filename.c_str()); |
| 4193 | } |
| 4194 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4195 | const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4196 | const char *BaseInput, |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 4197 | StringRef BoundArch, bool AtTopLevel, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4198 | bool MultipleArchs, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4199 | StringRef OffloadingPrefix) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 4200 | llvm::PrettyStackTraceString CrashInfo("Computing output path"); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4201 | // Output to a user requested destination? |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4202 | if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) { |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4203 | if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4204 | return C.addResultFile(FinalOutput->getValue(), &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 4207 | // For /P, preprocess to file named after BaseInput. |
| 4208 | if (C.getArgs().hasArg(options::OPT__SLASH_P)) { |
| 4209 | assert(AtTopLevel && isa<PreprocessJobAction>(JA)); |
| 4210 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
Hans Wennborg | 04c764f | 2014-06-17 00:19:12 +0000 | [diff] [blame] | 4211 | StringRef NameArg; |
Greg Bedwell | 065f70a | 2015-06-09 10:24:06 +0000 | [diff] [blame] | 4212 | if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi)) |
Hans Wennborg | 04c764f | 2014-06-17 00:19:12 +0000 | [diff] [blame] | 4213 | NameArg = A->getValue(); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4214 | return C.addResultFile( |
| 4215 | MakeCLOutputFilename(C.getArgs(), NameArg, BaseName, types::TY_PP_C), |
| 4216 | &JA); |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
Nick Lewycky | 6e1ce29 | 2010-09-24 00:46:53 +0000 | [diff] [blame] | 4219 | // Default to writing to stdout? |
David Blaikie | bc023c9 | 2018-06-14 23:09:06 +0000 | [diff] [blame] | 4220 | if (AtTopLevel && !CCGenDiagnostics && isa<PreprocessJobAction>(JA)) |
Nick Lewycky | 6e1ce29 | 2010-09-24 00:46:53 +0000 | [diff] [blame] | 4221 | return "-"; |
| 4222 | |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4223 | // Is this the assembly listing for /FA? |
| 4224 | if (JA.getType() == types::TY_PP_Asm && |
| 4225 | (C.getArgs().hasArg(options::OPT__SLASH_FA) || |
| 4226 | C.getArgs().hasArg(options::OPT__SLASH_Fa))) { |
| 4227 | // Use /Fa and the input filename to determine the asm file name. |
| 4228 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
| 4229 | StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4230 | return C.addResultFile( |
| 4231 | MakeCLOutputFilename(C.getArgs(), FaValue, BaseName, JA.getType()), |
| 4232 | &JA); |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4235 | // Output to a temporary file? |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4236 | if ((!AtTopLevel && !isSaveTempsEnabled() && |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4237 | !C.getArgs().hasArg(options::OPT__SLASH_Fo)) || |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 4238 | CCGenDiagnostics) { |
Chad Rosier | 97c3737 | 2011-08-26 22:27:02 +0000 | [diff] [blame] | 4239 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4240 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 4241 | SmallString<128> TmpName; |
| 4242 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
| 4243 | Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir); |
| 4244 | if (CCGenDiagnostics && A) { |
| 4245 | SmallString<128> CrashDirectory(A->getValue()); |
| 4246 | llvm::sys::path::append(CrashDirectory, Split.first); |
| 4247 | const char *Middle = Suffix ? "-%%%%%%." : "-%%%%%%"; |
| 4248 | std::error_code EC = |
| 4249 | llvm::sys::fs::createUniqueFile(CrashDirectory + Middle + Suffix, TmpName); |
| 4250 | if (EC) { |
| 4251 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4252 | return ""; |
| 4253 | } |
| 4254 | } else { |
| 4255 | TmpName = GetTemporaryPath(Split.first, Suffix); |
| 4256 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4257 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4260 | SmallString<128> BasePath(BaseInput); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4261 | StringRef BaseName; |
Daniel Dunbar | 67fea71 | 2011-03-25 18:16:51 +0000 | [diff] [blame] | 4262 | |
| 4263 | // Dsymutil actions should use the full path. |
Eric Christopher | 551ef45 | 2011-08-23 17:56:55 +0000 | [diff] [blame] | 4264 | if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA)) |
Daniel Dunbar | 67fea71 | 2011-03-25 18:16:51 +0000 | [diff] [blame] | 4265 | BaseName = BasePath; |
| 4266 | else |
| 4267 | BaseName = llvm::sys::path::filename(BasePath); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4268 | |
| 4269 | // Determine what the derived output name should be. |
| 4270 | const char *NamedOutput; |
Hans Wennborg | 2b89a26 | 2013-08-06 22:11:28 +0000 | [diff] [blame] | 4271 | |
Hans Wennborg | 625fba8 | 2016-10-04 21:01:04 +0000 | [diff] [blame] | 4272 | if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) && |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 4273 | C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) { |
| 4274 | // The /Fo or /o flag decides the object filename. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4275 | StringRef Val = |
| 4276 | C.getArgs() |
| 4277 | .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o) |
| 4278 | ->getValue(); |
| 4279 | NamedOutput = |
| 4280 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object); |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4281 | } else if (JA.getType() == types::TY_Image && |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4282 | C.getArgs().hasArg(options::OPT__SLASH_Fe, |
| 4283 | options::OPT__SLASH_o)) { |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 4284 | // The /Fe or /o flag names the linked file. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4285 | StringRef Val = |
| 4286 | C.getArgs() |
| 4287 | .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o) |
| 4288 | ->getValue(); |
| 4289 | NamedOutput = |
| 4290 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Image); |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 4291 | } else if (JA.getType() == types::TY_Image) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4292 | if (IsCLMode()) { |
| 4293 | // clang-cl uses BaseName for the executable name. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4294 | NamedOutput = |
| 4295 | MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image); |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 4296 | } else { |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 4297 | SmallString<128> Output(getDefaultImageName()); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4298 | Output += OffloadingPrefix; |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 4299 | if (MultipleArchs && !BoundArch.empty()) { |
| 4300 | Output += "-"; |
| 4301 | Output.append(BoundArch); |
| 4302 | } |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4303 | NamedOutput = C.getArgs().MakeArgString(Output.c_str()); |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4304 | } |
| 4305 | } else if (JA.getType() == types::TY_PCH && IsCLMode()) { |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4306 | NamedOutput = C.getArgs().MakeArgString(GetClPchPath(C, BaseName)); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4307 | } else { |
Hans Wennborg | 0a096a0 | 2013-09-05 17:05:56 +0000 | [diff] [blame] | 4308 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4309 | assert(Suffix && "All types used for output should have a suffix."); |
| 4310 | |
| 4311 | std::string::size_type End = std::string::npos; |
| 4312 | if (!types::appendSuffixForType(JA.getType())) |
| 4313 | End = BaseName.rfind('.'); |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4314 | SmallString<128> Suffixed(BaseName.substr(0, End)); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4315 | Suffixed += OffloadingPrefix; |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 4316 | if (MultipleArchs && !BoundArch.empty()) { |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4317 | Suffixed += "-"; |
| 4318 | Suffixed.append(BoundArch); |
| 4319 | } |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 4320 | // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for |
| 4321 | // the unoptimized bitcode so that it does not get overwritten by the ".bc" |
| 4322 | // optimized bitcode output. |
| 4323 | if (!AtTopLevel && C.getArgs().hasArg(options::OPT_emit_llvm) && |
| 4324 | JA.getType() == types::TY_LLVM_BC) |
| 4325 | Suffixed += ".tmp"; |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4326 | Suffixed += '.'; |
| 4327 | Suffixed += Suffix; |
| 4328 | NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); |
| 4329 | } |
| 4330 | |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4331 | // Prepend object file path if -save-temps=obj |
| 4332 | if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) && |
| 4333 | JA.getType() != types::TY_PCH) { |
| 4334 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 4335 | SmallString<128> TempPath(FinalOutput->getValue()); |
| 4336 | llvm::sys::path::remove_filename(TempPath); |
| 4337 | StringRef OutputFileName = llvm::sys::path::filename(NamedOutput); |
| 4338 | llvm::sys::path::append(TempPath, OutputFileName); |
| 4339 | NamedOutput = C.getArgs().MakeArgString(TempPath.c_str()); |
| 4340 | } |
| 4341 | |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 4342 | // 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] | 4343 | // then avoid overwriting input file. |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4344 | if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) { |
Chad Rosier | 5b58af0 | 2012-04-20 20:05:08 +0000 | [diff] [blame] | 4345 | bool SameFile = false; |
| 4346 | SmallString<256> Result; |
| 4347 | llvm::sys::fs::current_path(Result); |
| 4348 | llvm::sys::path::append(Result, BaseName); |
| 4349 | llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile); |
| 4350 | // Must share the same path to conflict. |
| 4351 | if (SameFile) { |
| 4352 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4353 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4354 | std::string TmpName = GetTemporaryPath( |
| 4355 | Split.first, types::getTypeTempSuffix(JA.getType(), IsCLMode())); |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4356 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
Chad Rosier | 5b58af0 | 2012-04-20 20:05:08 +0000 | [diff] [blame] | 4357 | } |
Chad Rosier | f8412cd | 2011-07-15 21:54:29 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4360 | // As an annoying special case, PCH generation doesn't strip the pathname. |
Nico Weber | 8ab9219 | 2016-03-02 23:29:29 +0000 | [diff] [blame] | 4361 | if (JA.getType() == types::TY_PCH && !IsCLMode()) { |
Michael J. Spencer | e169675 | 2010-12-18 00:19:12 +0000 | [diff] [blame] | 4362 | llvm::sys::path::remove_filename(BasePath); |
| 4363 | if (BasePath.empty()) |
Daniel Dunbar | e6c8319 | 2009-03-18 09:58:30 +0000 | [diff] [blame] | 4364 | BasePath = NamedOutput; |
| 4365 | else |
Michael J. Spencer | e169675 | 2010-12-18 00:19:12 +0000 | [diff] [blame] | 4366 | llvm::sys::path::append(BasePath, NamedOutput); |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4367 | return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4368 | } else { |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4369 | return C.addResultFile(NamedOutput, &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4373 | std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const { |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 4374 | // Search for Name in a list of paths. |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame] | 4375 | auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P) |
| 4376 | -> llvm::Optional<std::string> { |
| 4377 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
| 4378 | // attempting to use this prefix when looking for file paths. |
| 4379 | for (const auto &Dir : P) { |
| 4380 | if (Dir.empty()) |
| 4381 | continue; |
| 4382 | SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); |
| 4383 | llvm::sys::path::append(P, Name); |
| 4384 | if (llvm::sys::fs::exists(Twine(P))) |
Simon Pilgrim | 4588fc8 | 2018-09-13 10:10:18 +0000 | [diff] [blame] | 4385 | return P.str().str(); |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame] | 4386 | } |
| 4387 | return None; |
| 4388 | }; |
| 4389 | |
| 4390 | if (auto P = SearchPaths(PrefixDirs)) |
| 4391 | return *P; |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4392 | |
Petr Hosek | 916a467 | 2017-08-10 04:16:38 +0000 | [diff] [blame] | 4393 | SmallString<128> R(ResourceDir); |
| 4394 | llvm::sys::path::append(R, Name); |
| 4395 | if (llvm::sys::fs::exists(Twine(R))) |
| 4396 | return R.str(); |
| 4397 | |
| 4398 | SmallString<128> P(TC.getCompilerRTPath()); |
Rafael Espindola | 609a664 | 2013-06-24 18:33:43 +0000 | [diff] [blame] | 4399 | llvm::sys::path::append(P, Name); |
| 4400 | if (llvm::sys::fs::exists(Twine(P))) |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 4401 | return P.str(); |
| 4402 | |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame] | 4403 | if (auto P = SearchPaths(TC.getLibraryPaths())) |
| 4404 | return *P; |
| 4405 | |
| 4406 | if (auto P = SearchPaths(TC.getFilePaths())) |
| 4407 | return *P; |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4408 | |
Daniel Dunbar | 1ce8153 | 2009-09-09 22:33:00 +0000 | [diff] [blame] | 4409 | return Name; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4412 | void Driver::generatePrefixedToolNames( |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4413 | StringRef Tool, const ToolChain &TC, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4414 | SmallVectorImpl<std::string> &Names) const { |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4415 | // FIXME: Needs a better variable than TargetTriple |
| 4416 | Names.emplace_back((TargetTriple + "-" + Tool).str()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 4417 | Names.emplace_back(Tool); |
Vasileios Kalintiris | c744e12 | 2015-11-12 15:26:54 +0000 | [diff] [blame] | 4418 | |
| 4419 | // Allow the discovery of tools prefixed with LLVM's default target triple. |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4420 | std::string DefaultTargetTriple = llvm::sys::getDefaultTargetTriple(); |
| 4421 | if (DefaultTargetTriple != TargetTriple) |
| 4422 | Names.emplace_back((DefaultTargetTriple + "-" + Tool).str()); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Benjamin Kramer | 7111b91 | 2014-11-04 20:26:01 +0000 | [diff] [blame] | 4425 | static bool ScanDirForExecutable(SmallString<128> &Dir, |
| 4426 | ArrayRef<std::string> Names) { |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4427 | for (const auto &Name : Names) { |
| 4428 | llvm::sys::path::append(Dir, Name); |
| 4429 | if (llvm::sys::fs::can_execute(Twine(Dir))) |
| 4430 | return true; |
| 4431 | llvm::sys::path::remove_filename(Dir); |
| 4432 | } |
| 4433 | return false; |
| 4434 | } |
| 4435 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4436 | std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const { |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4437 | SmallVector<std::string, 2> TargetSpecificExecutables; |
| 4438 | generatePrefixedToolNames(Name, TC, TargetSpecificExecutables); |
| 4439 | |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4440 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
Logan Chien | cd679fd | 2012-10-04 08:08:56 +0000 | [diff] [blame] | 4441 | // attempting to use this prefix when looking for program paths. |
Saleem Abdulrasool | 23d99b1 | 2014-09-16 03:48:32 +0000 | [diff] [blame] | 4442 | for (const auto &PrefixDir : PrefixDirs) { |
| 4443 | if (llvm::sys::fs::is_directory(PrefixDir)) { |
| 4444 | SmallString<128> P(PrefixDir); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4445 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4446 | return P.str(); |
Simon Atanasyan | a47ba29 | 2012-10-31 14:39:28 +0000 | [diff] [blame] | 4447 | } else { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4448 | SmallString<128> P((PrefixDir + Name).str()); |
Rafael Espindola | 609a664 | 2013-06-24 18:33:43 +0000 | [diff] [blame] | 4449 | if (llvm::sys::fs::can_execute(Twine(P))) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4450 | return P.str(); |
Simon Atanasyan | 86bdab7 | 2012-10-31 12:01:53 +0000 | [diff] [blame] | 4451 | } |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4454 | const ToolChain::path_list &List = TC.getProgramPaths(); |
Saleem Abdulrasool | 23d99b1 | 2014-09-16 03:48:32 +0000 | [diff] [blame] | 4455 | for (const auto &Path : List) { |
| 4456 | SmallString<128> P(Path); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4457 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4458 | return P.str(); |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Daniel Dunbar | 76ce741 | 2009-03-23 16:15:50 +0000 | [diff] [blame] | 4461 | // If all else failed, search the path. |
Michael J. Spencer | b011d48 | 2014-11-07 21:30:32 +0000 | [diff] [blame] | 4462 | for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) |
| 4463 | if (llvm::ErrorOr<std::string> P = |
| 4464 | llvm::sys::findProgramByName(TargetSpecificExecutable)) |
Michael J. Spencer | 04162ea | 2014-11-04 01:30:55 +0000 | [diff] [blame] | 4465 | return *P; |
Daniel Dunbar | 6f66877 | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 4466 | |
Daniel Dunbar | 1ce8153 | 2009-09-09 22:33:00 +0000 | [diff] [blame] | 4467 | return Name; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4470 | std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const { |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4471 | SmallString<128> Path; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 4472 | std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path); |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4473 | if (EC) { |
| 4474 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
Daniel Dunbar | e627c1c | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 4475 | return ""; |
| 4476 | } |
| 4477 | |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4478 | return Path.str(); |
Daniel Dunbar | e627c1c | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Steven Wu | c3399db6 | 2019-01-11 21:16:04 +0000 | [diff] [blame] | 4481 | std::string Driver::GetTemporaryDirectory(StringRef Prefix) const { |
| 4482 | SmallString<128> Path; |
| 4483 | std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, Path); |
| 4484 | if (EC) { |
| 4485 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4486 | return ""; |
| 4487 | } |
| 4488 | |
| 4489 | return Path.str(); |
| 4490 | } |
| 4491 | |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4492 | std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const { |
| 4493 | SmallString<128> Output; |
| 4494 | if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) { |
| 4495 | // FIXME: If anybody needs it, implement this obscure rule: |
| 4496 | // "If you specify a directory without a file name, the default file name |
| 4497 | // is VCx0.pch., where x is the major version of Visual C++ in use." |
| 4498 | Output = FpArg->getValue(); |
| 4499 | |
| 4500 | // "If you do not specify an extension as part of the path name, an |
| 4501 | // extension of .pch is assumed. " |
| 4502 | if (!llvm::sys::path::has_extension(Output)) |
| 4503 | Output += ".pch"; |
| 4504 | } else { |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 4505 | if (Arg *YcArg = C.getArgs().getLastArg(options::OPT__SLASH_Yc)) |
| 4506 | Output = YcArg->getValue(); |
| 4507 | if (Output.empty()) |
| 4508 | Output = BaseName; |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4509 | llvm::sys::path::replace_extension(Output, ".pch"); |
| 4510 | } |
| 4511 | return Output.str(); |
| 4512 | } |
| 4513 | |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4514 | const ToolChain &Driver::getToolChain(const ArgList &Args, |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 4515 | const llvm::Triple &Target) const { |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4516 | |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4517 | auto &TC = ToolChains[Target.str()]; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4518 | if (!TC) { |
| 4519 | switch (Target.getOS()) { |
Reid Kleckner | 330fb17 | 2016-05-11 16:19:05 +0000 | [diff] [blame] | 4520 | case llvm::Triple::Haiku: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4521 | TC = llvm::make_unique<toolchains::Haiku>(*this, Target, Args); |
Reid Kleckner | 330fb17 | 2016-05-11 16:19:05 +0000 | [diff] [blame] | 4522 | break; |
Ed Schouten | 4dabea2 | 2017-06-25 08:29:09 +0000 | [diff] [blame] | 4523 | case llvm::Triple::Ananas: |
| 4524 | TC = llvm::make_unique<toolchains::Ananas>(*this, Target, Args); |
| 4525 | break; |
Ed Schouten | 3c3e58c | 2015-03-26 11:13:44 +0000 | [diff] [blame] | 4526 | case llvm::Triple::CloudABI: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4527 | TC = llvm::make_unique<toolchains::CloudABI>(*this, Target, Args); |
Ed Schouten | 3c3e58c | 2015-03-26 11:13:44 +0000 | [diff] [blame] | 4528 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4529 | case llvm::Triple::Darwin: |
| 4530 | case llvm::Triple::MacOSX: |
| 4531 | case llvm::Triple::IOS: |
Tim Northover | 6f3ff22 | 2015-10-30 16:30:27 +0000 | [diff] [blame] | 4532 | case llvm::Triple::TvOS: |
| 4533 | case llvm::Triple::WatchOS: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4534 | TC = llvm::make_unique<toolchains::DarwinClang>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4535 | break; |
| 4536 | case llvm::Triple::DragonFly: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4537 | TC = llvm::make_unique<toolchains::DragonFly>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4538 | break; |
| 4539 | case llvm::Triple::OpenBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4540 | TC = llvm::make_unique<toolchains::OpenBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4541 | break; |
| 4542 | case llvm::Triple::NetBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4543 | TC = llvm::make_unique<toolchains::NetBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4544 | break; |
| 4545 | case llvm::Triple::FreeBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4546 | TC = llvm::make_unique<toolchains::FreeBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4547 | break; |
| 4548 | case llvm::Triple::Minix: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4549 | TC = llvm::make_unique<toolchains::Minix>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4550 | break; |
| 4551 | case llvm::Triple::Linux: |
Andrey Turetskiy | 4798eb6 | 2016-06-16 10:36:09 +0000 | [diff] [blame] | 4552 | case llvm::Triple::ELFIAMCU: |
Chandler Carruth | cf705b2 | 2012-01-25 21:03:58 +0000 | [diff] [blame] | 4553 | if (Target.getArch() == llvm::Triple::hexagon) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4554 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4555 | Args); |
Vasileios Kalintiris | c744e12 | 2015-11-12 15:26:54 +0000 | [diff] [blame] | 4556 | else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) && |
| 4557 | !Target.hasEnvironment()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4558 | TC = llvm::make_unique<toolchains::MipsLLVMToolChain>(*this, Target, |
| 4559 | Args); |
Chandler Carruth | cf705b2 | 2012-01-25 21:03:58 +0000 | [diff] [blame] | 4560 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4561 | TC = llvm::make_unique<toolchains::Linux>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4562 | break; |
Derek Schuff | 6ab52fa | 2015-03-30 20:31:33 +0000 | [diff] [blame] | 4563 | case llvm::Triple::NaCl: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4564 | TC = llvm::make_unique<toolchains::NaClToolChain>(*this, Target, Args); |
Derek Schuff | 6ab52fa | 2015-03-30 20:31:33 +0000 | [diff] [blame] | 4565 | break; |
Petr Hosek | 62e1d23 | 2016-10-06 06:08:09 +0000 | [diff] [blame] | 4566 | case llvm::Triple::Fuchsia: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4567 | TC = llvm::make_unique<toolchains::Fuchsia>(*this, Target, Args); |
Petr Hosek | 62e1d23 | 2016-10-06 06:08:09 +0000 | [diff] [blame] | 4568 | break; |
David Chisnall | f571cde | 2012-02-15 13:39:01 +0000 | [diff] [blame] | 4569 | case llvm::Triple::Solaris: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4570 | TC = llvm::make_unique<toolchains::Solaris>(*this, Target, Args); |
David Chisnall | f571cde | 2012-02-15 13:39:01 +0000 | [diff] [blame] | 4571 | break; |
Tom Stellard | 8fa3309 | 2015-07-18 01:49:05 +0000 | [diff] [blame] | 4572 | case llvm::Triple::AMDHSA: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4573 | TC = llvm::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args); |
Tom Stellard | 8fa3309 | 2015-07-18 01:49:05 +0000 | [diff] [blame] | 4574 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4575 | case llvm::Triple::Win32: |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4576 | switch (Target.getEnvironment()) { |
| 4577 | default: |
| 4578 | if (Target.isOSBinFormatELF()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4579 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4580 | else if (Target.isOSBinFormatMachO()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4581 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4582 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4583 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4584 | break; |
| 4585 | case llvm::Triple::GNU: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4586 | TC = llvm::make_unique<toolchains::MinGW>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4587 | break; |
Saleem Abdulrasool | 543a78b | 2014-10-24 03:13:37 +0000 | [diff] [blame] | 4588 | case llvm::Triple::Itanium: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4589 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>(*this, Target, |
| 4590 | Args); |
Saleem Abdulrasool | 543a78b | 2014-10-24 03:13:37 +0000 | [diff] [blame] | 4591 | break; |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4592 | case llvm::Triple::MSVC: |
| 4593 | case llvm::Triple::UnknownEnvironment: |
Dave Lee | f96bedf | 2017-11-06 21:18:05 +0000 | [diff] [blame] | 4594 | if (Args.getLastArgValue(options::OPT_fuse_ld_EQ) |
| 4595 | .startswith_lower("bfd")) |
| 4596 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>( |
| 4597 | *this, Target, Args); |
| 4598 | else |
| 4599 | TC = |
| 4600 | llvm::make_unique<toolchains::MSVCToolChain>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4601 | break; |
| 4602 | } |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4603 | break; |
Filipe Cabecinhas | c888e19 | 2015-10-14 12:25:43 +0000 | [diff] [blame] | 4604 | case llvm::Triple::PS4: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4605 | TC = llvm::make_unique<toolchains::PS4CPU>(*this, Target, Args); |
Filipe Cabecinhas | c888e19 | 2015-10-14 12:25:43 +0000 | [diff] [blame] | 4606 | break; |
David L Kreitzer | d397ea4 | 2016-10-14 20:44:33 +0000 | [diff] [blame] | 4607 | case llvm::Triple::Contiki: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4608 | TC = llvm::make_unique<toolchains::Contiki>(*this, Target, Args); |
David L Kreitzer | d397ea4 | 2016-10-14 20:44:33 +0000 | [diff] [blame] | 4609 | break; |
Kristina Brooks | 77a4adc | 2018-11-29 03:49:14 +0000 | [diff] [blame] | 4610 | case llvm::Triple::Hurd: |
| 4611 | TC = llvm::make_unique<toolchains::Hurd>(*this, Target, Args); |
| 4612 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4613 | default: |
Douglas Katzman | 9f5e70e | 2015-05-26 18:01:33 +0000 | [diff] [blame] | 4614 | // Of these targets, Hexagon is the only one that might have |
| 4615 | // an OS of Linux, in which case it got handled above already. |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4616 | switch (Target.getArch()) { |
| 4617 | case llvm::Triple::tce: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4618 | TC = llvm::make_unique<toolchains::TCEToolChain>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4619 | break; |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 4620 | case llvm::Triple::tcele: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4621 | TC = llvm::make_unique<toolchains::TCELEToolChain>(*this, Target, Args); |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 4622 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4623 | case llvm::Triple::hexagon: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4624 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4625 | Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4626 | break; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 4627 | case llvm::Triple::lanai: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4628 | TC = llvm::make_unique<toolchains::LanaiToolChain>(*this, Target, Args); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 4629 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4630 | case llvm::Triple::xcore: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4631 | TC = llvm::make_unique<toolchains::XCoreToolChain>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4632 | break; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 4633 | case llvm::Triple::wasm32: |
| 4634 | case llvm::Triple::wasm64: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4635 | TC = llvm::make_unique<toolchains::WebAssembly>(*this, Target, Args); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 4636 | break; |
Dylan McKay | 924fa3a | 2017-01-05 05:20:27 +0000 | [diff] [blame] | 4637 | case llvm::Triple::avr: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4638 | TC = llvm::make_unique<toolchains::AVRToolChain>(*this, Target, Args); |
Dylan McKay | 924fa3a | 2017-01-05 05:20:27 +0000 | [diff] [blame] | 4639 | break; |
Anton Korobeynikov | 93165d6 | 2019-01-15 19:44:05 +0000 | [diff] [blame] | 4640 | case llvm::Triple::msp430: |
| 4641 | TC = |
| 4642 | llvm::make_unique<toolchains::MSP430ToolChain>(*this, Target, Args); |
| 4643 | break; |
David Bolvansky | f4be253 | 2018-07-31 14:21:46 +0000 | [diff] [blame] | 4644 | case llvm::Triple::riscv32: |
| 4645 | case llvm::Triple::riscv64: |
| 4646 | TC = llvm::make_unique<toolchains::RISCVToolChain>(*this, Target, Args); |
| 4647 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4648 | default: |
Douglas Katzman | d6e597c | 2015-09-17 19:56:40 +0000 | [diff] [blame] | 4649 | if (Target.getVendor() == llvm::Triple::Myriad) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4650 | TC = llvm::make_unique<toolchains::MyriadToolChain>(*this, Target, |
| 4651 | Args); |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 4652 | else if (toolchains::BareMetal::handlesTarget(Target)) |
| 4653 | TC = llvm::make_unique<toolchains::BareMetal>(*this, Target, Args); |
Douglas Katzman | d6e597c | 2015-09-17 19:56:40 +0000 | [diff] [blame] | 4654 | else if (Target.isOSBinFormatELF()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4655 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4656 | else if (Target.isOSBinFormatMachO()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4657 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4658 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4659 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4660 | } |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4661 | } |
| 4662 | } |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 4663 | |
| 4664 | // Intentionally omitted from the switch above: llvm::Triple::CUDA. CUDA |
| 4665 | // compiles always need two toolchains, the CUDA toolchain and the host |
| 4666 | // toolchain. So the only valid way to create a CUDA toolchain is via |
| 4667 | // CreateOffloadingDeviceToolChains. |
| 4668 | |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4669 | return *TC; |
Daniel Dunbar | 4dff6a4 | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 4670 | } |
Daniel Dunbar | 8fa879d | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 4671 | |
Rafael Espindola | 2f69d40 | 2013-03-18 15:33:26 +0000 | [diff] [blame] | 4672 | bool Driver::ShouldUseClangCompiler(const JobAction &JA) const { |
Douglas Katzman | 0024909 | 2015-06-12 15:45:21 +0000 | [diff] [blame] | 4673 | // 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] | 4674 | if (JA.size() != 1 || |
| 4675 | !types::isAcceptedByClang((*JA.input_begin())->getType())) |
Nick Lewycky | 5cc9ebb | 2012-11-15 05:36:36 +0000 | [diff] [blame] | 4676 | return false; |
| 4677 | |
Douglas Katzman | 0024909 | 2015-06-12 15:45:21 +0000 | [diff] [blame] | 4678 | // 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] | 4679 | if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) && |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 4680 | !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA)) |
Nick Lewycky | 5cc9ebb | 2012-11-15 05:36:36 +0000 | [diff] [blame] | 4681 | return false; |
| 4682 | |
| 4683 | return true; |
| 4684 | } |
| 4685 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4686 | /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the |
| 4687 | /// 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] | 4688 | /// |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4689 | /// \return True if the entire string was parsed (9.2), or all groups were |
| 4690 | /// parsed (10.3.5extrastuff). |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4691 | bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor, |
| 4692 | unsigned &Micro, bool &HadExtra) { |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4693 | HadExtra = false; |
| 4694 | |
| 4695 | Major = Minor = Micro = 0; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4696 | if (Str.empty()) |
Bob Wilson | 433cb31 | 2015-04-07 01:03:35 +0000 | [diff] [blame] | 4697 | return false; |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4698 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4699 | if (Str.consumeInteger(10, Major)) |
| 4700 | return false; |
| 4701 | if (Str.empty()) |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4702 | return true; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4703 | if (Str[0] != '.') |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4704 | return false; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4705 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4706 | Str = Str.drop_front(1); |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4707 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4708 | if (Str.consumeInteger(10, Minor)) |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4709 | return false; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4710 | if (Str.empty()) |
| 4711 | return true; |
| 4712 | if (Str[0] != '.') |
| 4713 | return false; |
| 4714 | Str = Str.drop_front(1); |
| 4715 | |
| 4716 | if (Str.consumeInteger(10, Micro)) |
| 4717 | return false; |
| 4718 | if (!Str.empty()) |
| 4719 | HadExtra = true; |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4720 | return true; |
| 4721 | } |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4722 | |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4723 | /// Parse digits from a string \p Str and fulfill \p Digits with |
| 4724 | /// the parsed numbers. This method assumes that the max number of |
| 4725 | /// digits to look for is equal to Digits.size(). |
| 4726 | /// |
| 4727 | /// \return True if the entire string was parsed and there are |
| 4728 | /// no extra characters remaining at the end. |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4729 | bool Driver::GetReleaseVersion(StringRef Str, |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4730 | MutableArrayRef<unsigned> Digits) { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4731 | if (Str.empty()) |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4732 | return false; |
| 4733 | |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4734 | unsigned CurDigit = 0; |
| 4735 | while (CurDigit < Digits.size()) { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4736 | unsigned Digit; |
| 4737 | if (Str.consumeInteger(10, Digit)) |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4738 | return false; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4739 | Digits[CurDigit] = Digit; |
| 4740 | if (Str.empty()) |
| 4741 | return true; |
| 4742 | if (Str[0] != '.') |
| 4743 | return false; |
| 4744 | Str = Str.drop_front(1); |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4745 | CurDigit++; |
| 4746 | } |
| 4747 | |
| 4748 | // More digits than requested, bail out... |
| 4749 | return false; |
| 4750 | } |
| 4751 | |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 4752 | std::pair<unsigned, unsigned> Driver::getIncludeExcludeOptionFlagMasks(bool IsClCompatMode) const { |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4753 | unsigned IncludedFlagsBitmask = 0; |
Rafael Espindola | cc707bc | 2013-09-25 15:54:41 +0000 | [diff] [blame] | 4754 | unsigned ExcludedFlagsBitmask = options::NoDriverOption; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4755 | |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 4756 | if (IsClCompatMode) { |
Hans Wennborg | 1907610 | 2013-07-31 20:51:53 +0000 | [diff] [blame] | 4757 | // Include CL and Core options. |
| 4758 | IncludedFlagsBitmask |= options::CLOption; |
| 4759 | IncludedFlagsBitmask |= options::CoreOption; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4760 | } else { |
| 4761 | ExcludedFlagsBitmask |= options::CLOption; |
| 4762 | } |
| 4763 | |
| 4764 | return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask); |
| 4765 | } |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 4766 | |
Douglas Katzman | f08fadf | 2015-06-04 14:40:44 +0000 | [diff] [blame] | 4767 | bool clang::driver::isOptimizationLevelFast(const ArgList &Args) { |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 4768 | return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false); |
| 4769 | } |