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" |
Qiu Chaofan | 19828e3 | 2019-04-23 05:50:24 +0000 | [diff] [blame] | 41 | #include "ToolChains/PPCLinux.h" |
Erik Pilkington | 46420b6 | 2018-09-27 20:36:28 +0000 | [diff] [blame] | 42 | #include "ToolChains/RISCVToolchain.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 43 | #include "ToolChains/Solaris.h" |
| 44 | #include "ToolChains/TCE.h" |
| 45 | #include "ToolChains/WebAssembly.h" |
| 46 | #include "ToolChains/XCore.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 47 | #include "clang/Basic/Version.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 48 | #include "clang/Config/config.h" |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 49 | #include "clang/Driver/Action.h" |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 50 | #include "clang/Driver/Compilation.h" |
Daniel Dunbar | c0b3e95 | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 51 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 52 | #include "clang/Driver/Job.h" |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 53 | #include "clang/Driver/Options.h" |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 54 | #include "clang/Driver/SanitizerArgs.h" |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 55 | #include "clang/Driver/Tool.h" |
| 56 | #include "clang/Driver/ToolChain.h" |
Chris Lattner | ce6c42f | 2011-03-23 04:04:01 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/ArrayRef.h" |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/STLExtras.h" |
Justin Lebar | 6290761 | 2016-07-06 21:21:39 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/SmallSet.h" |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/StringSet.h" |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/StringSwitch.h" |
Nico Weber | d637c05 | 2018-04-30 13:52:15 +0000 | [diff] [blame] | 63 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 64 | #include "llvm/Option/Arg.h" |
| 65 | #include "llvm/Option/ArgList.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 66 | #include "llvm/Option/OptSpecifier.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 67 | #include "llvm/Option/OptTable.h" |
| 68 | #include "llvm/Option/Option.h" |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 69 | #include "llvm/Support/CommandLine.h" |
David Blaikie | 7900020 | 2011-09-23 05:57:42 +0000 | [diff] [blame] | 70 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | f28df4c | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 71 | #include "llvm/Support/FileSystem.h" |
Fangrui Song | d002823 | 2018-10-10 00:15:33 +0000 | [diff] [blame] | 72 | #include "llvm/Support/FormatVariadic.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 73 | #include "llvm/Support/Path.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 74 | #include "llvm/Support/PrettyStackTrace.h" |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 75 | #include "llvm/Support/Process.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 76 | #include "llvm/Support/Program.h" |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 77 | #include "llvm/Support/StringSaver.h" |
Dimitry Andric | 81c4042 | 2017-06-06 21:54:21 +0000 | [diff] [blame] | 78 | #include "llvm/Support/TargetRegistry.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 79 | #include "llvm/Support/VirtualFileSystem.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 80 | #include "llvm/Support/raw_ostream.h" |
Dylan Noblesmith | 4f4e745 | 2012-02-02 00:40:14 +0000 | [diff] [blame] | 81 | #include <map> |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 82 | #include <memory> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 83 | #include <utility> |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 84 | #if LLVM_ON_UNIX |
| 85 | #include <unistd.h> // getpid |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 86 | #include <sysexits.h> // EX_IOERR |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 87 | #endif |
Dylan Noblesmith | 86780e9 | 2012-02-01 14:25:28 +0000 | [diff] [blame] | 88 | |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 89 | using namespace clang::driver; |
Chris Lattner | ada1c91 | 2009-03-26 05:56:24 +0000 | [diff] [blame] | 90 | using namespace clang; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 91 | using namespace llvm::opt; |
Daniel Dunbar | b2cd66b | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 92 | |
Nico Weber | 0abcafd | 2019-01-31 22:15:32 +0000 | [diff] [blame] | 93 | // static |
| 94 | std::string Driver::GetResourcesPath(StringRef BinaryPath, |
| 95 | StringRef CustomResourceDir) { |
| 96 | // Since the resource directory is embedded in the module hash, it's important |
| 97 | // that all places that need it call this function, so that they get the |
| 98 | // exact same string ("a/../b/" and "b/" get different hashes, for example). |
| 99 | |
| 100 | // Dir is bin/ or lib/, depending on where BinaryPath is. |
| 101 | std::string Dir = llvm::sys::path::parent_path(BinaryPath); |
| 102 | |
| 103 | SmallString<128> P(Dir); |
| 104 | if (CustomResourceDir != "") { |
| 105 | llvm::sys::path::append(P, CustomResourceDir); |
| 106 | } else { |
| 107 | // On Windows, libclang.dll is in bin/. |
| 108 | // On non-Windows, libclang.so/.dylib is in lib/. |
| 109 | // With a static-library build of libclang, LibClangPath will contain the |
| 110 | // path of the embedding binary, which for LLVM binaries will be in bin/. |
| 111 | // ../lib gets us to lib/ in both cases. |
| 112 | P = llvm::sys::path::parent_path(Dir); |
| 113 | llvm::sys::path::append(P, Twine("lib") + CLANG_LIBDIR_SUFFIX, "clang", |
| 114 | CLANG_VERSION_STRING); |
| 115 | } |
| 116 | |
| 117 | return P.str(); |
| 118 | } |
| 119 | |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 120 | Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple, |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 121 | DiagnosticsEngine &Diags, |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 122 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 123 | : Opts(createDriverOptTable()), Diags(Diags), VFS(std::move(VFS)), |
| 124 | Mode(GCCMode), SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone), |
| 125 | LTOMode(LTOK_None), ClangExecutable(ClangExecutable), |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 126 | SysRoot(DEFAULT_SYSROOT), DriverTitle("clang LLVM compiler"), |
| 127 | CCPrintOptionsFilename(nullptr), CCPrintHeadersFilename(nullptr), |
| 128 | CCLogDiagnosticsFilename(nullptr), CCCPrintBindings(false), |
| 129 | CCPrintOptions(false), CCPrintHeaders(false), CCLogDiagnostics(false), |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 130 | CCGenDiagnostics(false), TargetTriple(TargetTriple), |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 131 | CCCGenericGCCName(""), Saver(Alloc), CheckInputsExist(true), |
Erich Keane | 0a6b5b6 | 2018-12-04 14:34:09 +0000 | [diff] [blame] | 132 | GenReproducer(false), SuppressMissingInputWarning(false) { |
Daniel Dunbar | 3f3e2cd | 2010-01-20 02:35:16 +0000 | [diff] [blame] | 133 | |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 134 | // Provide a sane fallback if no VFS is specified. |
| 135 | if (!this->VFS) |
Michael J. Spencer | 7e48b40 | 2019-05-28 22:21:47 +0000 | [diff] [blame] | 136 | this->VFS = llvm::vfs::createPhysicalFileSystem().release(); |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 137 | |
Sumanth Gundapaneni | 3a159294 | 2015-03-03 20:43:12 +0000 | [diff] [blame] | 138 | Name = llvm::sys::path::filename(ClangExecutable); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 139 | Dir = llvm::sys::path::parent_path(ClangExecutable); |
Benjamin Kramer | f420dda | 2015-10-13 15:19:32 +0000 | [diff] [blame] | 140 | InstalledDir = Dir; // Provide a sensible default installed dir. |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 141 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 142 | #if defined(CLANG_CONFIG_FILE_SYSTEM_DIR) |
| 143 | SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR; |
| 144 | #endif |
| 145 | #if defined(CLANG_CONFIG_FILE_USER_DIR) |
| 146 | UserConfigDir = CLANG_CONFIG_FILE_USER_DIR; |
| 147 | #endif |
| 148 | |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 149 | // Compute the path to the resource directory. |
Nico Weber | 0abcafd | 2019-01-31 22:15:32 +0000 | [diff] [blame] | 150 | ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR); |
Daniel Dunbar | 544ecd1 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 153 | void Driver::ParseDriverMode(StringRef ProgramName, |
| 154 | ArrayRef<const char *> Args) { |
Martin Storsjo | 1fab23d | 2018-04-25 21:23:59 +0000 | [diff] [blame] | 155 | if (ClangNameParts.isEmpty()) |
| 156 | ClangNameParts = ToolChain::getTargetAndModeFromProgramName(ProgramName); |
Serge Pavlov | 4e76984 | 2017-08-29 05:22:26 +0000 | [diff] [blame] | 157 | setDriverModeFromOption(ClangNameParts.DriverMode); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 158 | |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 159 | for (const char *ArgPtr : Args) { |
Erich Keane | 258f059 | 2018-02-07 00:19:58 +0000 | [diff] [blame] | 160 | // Ignore nullptrs, they are the response file's EOL markers. |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 161 | if (ArgPtr == nullptr) |
Reid Kleckner | af5fd6a | 2014-08-22 19:29:30 +0000 | [diff] [blame] | 162 | continue; |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 163 | const StringRef Arg = ArgPtr; |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 164 | setDriverModeFromOption(Arg); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 168 | void Driver::setDriverModeFromOption(StringRef Opt) { |
| 169 | const std::string OptName = |
| 170 | getOpts().getOption(options::OPT_driver_mode).getPrefixedName(); |
| 171 | if (!Opt.startswith(OptName)) |
| 172 | return; |
| 173 | StringRef Value = Opt.drop_front(OptName.size()); |
| 174 | |
Erich Keane | 2908a04 | 2018-02-12 17:47:01 +0000 | [diff] [blame] | 175 | if (auto M = llvm::StringSwitch<llvm::Optional<DriverMode>>(Value) |
| 176 | .Case("gcc", GCCMode) |
| 177 | .Case("g++", GXXMode) |
| 178 | .Case("cpp", CPPMode) |
| 179 | .Case("cl", CLMode) |
| 180 | .Default(None)) |
| 181 | Mode = *M; |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 182 | else |
| 183 | Diag(diag::err_drv_unsupported_option_argument) << OptName << Value; |
| 184 | } |
| 185 | |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 186 | InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 187 | bool IsClCompatMode, |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 188 | bool &ContainsError) { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 189 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 190 | ContainsError = false; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 191 | |
| 192 | unsigned IncludedFlagsBitmask; |
| 193 | unsigned ExcludedFlagsBitmask; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 194 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 195 | getIncludeExcludeOptionFlagMasks(IsClCompatMode); |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 197 | unsigned MissingArgIndex, MissingArgCount; |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 198 | InputArgList Args = |
David Blaikie | 6d492ad | 2015-06-21 06:32:36 +0000 | [diff] [blame] | 199 | getOpts().ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount, |
| 200 | IncludedFlagsBitmask, ExcludedFlagsBitmask); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 202 | // Check for missing argument error. |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 203 | if (MissingArgCount) { |
| 204 | Diag(diag::err_drv_missing_argument) |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 205 | << Args.getArgString(MissingArgIndex) << MissingArgCount; |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 206 | ContainsError |= |
| 207 | Diags.getDiagnosticLevel(diag::err_drv_missing_argument, |
| 208 | SourceLocation()) > DiagnosticsEngine::Warning; |
| 209 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | 52ed5fe | 2009-11-19 06:35:06 +0000 | [diff] [blame] | 211 | // Check for unsupported options. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 212 | for (const Arg *A : Args) { |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 213 | if (A->getOption().hasFlag(options::Unsupported)) { |
Brian Gesiak | 2491076 | 2018-01-06 00:25:40 +0000 | [diff] [blame] | 214 | unsigned DiagID; |
| 215 | auto ArgString = A->getAsString(Args); |
| 216 | std::string Nearest; |
| 217 | if (getOpts().findNearest( |
| 218 | ArgString, Nearest, IncludedFlagsBitmask, |
| 219 | ExcludedFlagsBitmask | options::Unsupported) > 1) { |
| 220 | DiagID = diag::err_drv_unsupported_opt; |
| 221 | Diag(DiagID) << ArgString; |
| 222 | } else { |
| 223 | DiagID = diag::err_drv_unsupported_opt_with_suggestion; |
| 224 | Diag(DiagID) << ArgString << Nearest; |
| 225 | } |
| 226 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 227 | DiagnosticsEngine::Warning; |
Daniel Dunbar | d8500f3 | 2009-03-22 23:26:43 +0000 | [diff] [blame] | 228 | continue; |
| 229 | } |
Chad Rosier | ce975d9 | 2012-02-22 17:55:22 +0000 | [diff] [blame] | 230 | |
| 231 | // Warn about -mcpu= without an argument. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 232 | if (A->getOption().matches(options::OPT_mcpu_EQ) && A->containsValue("")) { |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 233 | Diag(diag::warn_drv_empty_joined_argument) << A->getAsString(Args); |
| 234 | ContainsError |= Diags.getDiagnosticLevel( |
| 235 | diag::warn_drv_empty_joined_argument, |
| 236 | SourceLocation()) > DiagnosticsEngine::Warning; |
Chad Rosier | ce975d9 | 2012-02-22 17:55:22 +0000 | [diff] [blame] | 237 | } |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 240 | for (const Arg *A : Args.filtered(options::OPT_UNKNOWN)) { |
Brian Gesiak | 2491076 | 2018-01-06 00:25:40 +0000 | [diff] [blame] | 241 | unsigned DiagID; |
| 242 | auto ArgString = A->getAsString(Args); |
| 243 | std::string Nearest; |
| 244 | if (getOpts().findNearest( |
| 245 | ArgString, Nearest, IncludedFlagsBitmask, ExcludedFlagsBitmask) > 1) { |
| 246 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl |
| 247 | : diag::err_drv_unknown_argument; |
| 248 | Diags.Report(DiagID) << ArgString; |
| 249 | } else { |
Simon Atanasyan | 27c0f20 | 2019-04-02 18:03:15 +0000 | [diff] [blame] | 250 | DiagID = IsCLMode() |
| 251 | ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion |
| 252 | : diag::err_drv_unknown_argument_with_suggestion; |
Brian Gesiak | 2491076 | 2018-01-06 00:25:40 +0000 | [diff] [blame] | 253 | Diags.Report(DiagID) << ArgString << Nearest; |
| 254 | } |
| 255 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 256 | DiagnosticsEngine::Warning; |
| 257 | } |
Rafael Espindola | 8a2d496 | 2013-09-23 23:55:25 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 259 | return Args; |
| 260 | } |
| 261 | |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 262 | // Determine which compilation mode we are in. We look for options which |
| 263 | // affect the phase, starting with the earliest phases, and record which |
| 264 | // option we used to determine the final phase. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 265 | phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, |
| 266 | Arg **FinalPhaseArg) const { |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 267 | Arg *PhaseArg = nullptr; |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 268 | phases::ID FinalPhase; |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 269 | |
Hans Wennborg | e50cec3 | 2014-06-13 20:59:54 +0000 | [diff] [blame] | 270 | // -{E,EP,P,M,MM} only run the preprocessor. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 271 | if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) || |
Hans Wennborg | e50cec3 | 2014-06-13 20:59:54 +0000 | [diff] [blame] | 272 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) || |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 273 | (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) || |
| 274 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 275 | FinalPhase = phases::Preprocess; |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 276 | |
Richard Smith | dd4ad3d | 2016-08-30 19:06:26 +0000 | [diff] [blame] | 277 | // --precompile only runs up to precompilation. |
| 278 | } else if ((PhaseArg = DAL.getLastArg(options::OPT__precompile))) { |
| 279 | FinalPhase = phases::Precompile; |
| 280 | |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 281 | // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler. |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 282 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) || |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 283 | (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) || |
Ben Langmuir | 2cb4a78 | 2014-02-05 22:21:15 +0000 | [diff] [blame] | 284 | (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) || |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 285 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) || |
Fariborz Jahanian | 73223bb | 2012-04-02 15:59:19 +0000 | [diff] [blame] | 286 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) || |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 287 | (PhaseArg = DAL.getLastArg(options::OPT__migrate)) || |
Puyan Lotfi | 68f29da | 2019-06-20 16:59:48 +0000 | [diff] [blame] | 288 | (PhaseArg = DAL.getLastArg(options::OPT_emit_iterface_stubs)) || |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 289 | (PhaseArg = DAL.getLastArg(options::OPT__analyze, |
Chad Rosier | 1aeb15a | 2012-03-06 23:14:35 +0000 | [diff] [blame] | 290 | options::OPT__analyze_auto)) || |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 291 | (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 292 | FinalPhase = phases::Compile; |
| 293 | |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 294 | // -S only runs up to the backend. |
| 295 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) { |
| 296 | FinalPhase = phases::Backend; |
| 297 | |
Artem Belevich | 4242f41 | 2015-07-28 21:01:21 +0000 | [diff] [blame] | 298 | // -c compilation only runs up to the assembler. |
| 299 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) { |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 300 | FinalPhase = phases::Assemble; |
| 301 | |
| 302 | // Otherwise do everything. |
| 303 | } else |
| 304 | FinalPhase = phases::Link; |
| 305 | |
| 306 | if (FinalPhaseArg) |
| 307 | *FinalPhaseArg = PhaseArg; |
| 308 | |
| 309 | return FinalPhase; |
| 310 | } |
| 311 | |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 312 | static Arg *MakeInputArg(DerivedArgList &Args, OptTable &Opts, |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 313 | StringRef Value, bool Claim = true) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 314 | Arg *A = new Arg(Opts.getOption(options::OPT_INPUT), Value, |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 315 | Args.getBaseArgs().MakeIndex(Value), Value.data()); |
Hans Wennborg | 5536285 | 2014-05-02 22:55:30 +0000 | [diff] [blame] | 316 | Args.AddSynthesizedArg(A); |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 317 | if (Claim) |
| 318 | A->claim(); |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 319 | return A; |
| 320 | } |
| 321 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 322 | DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const { |
| 323 | DerivedArgList *DAL = new DerivedArgList(Args); |
| 324 | |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 325 | bool HasNostdlib = Args.hasArg(options::OPT_nostdlib); |
Dan Albert | f5ffa1a | 2018-12-18 23:29:35 +0000 | [diff] [blame] | 326 | bool HasNostdlibxx = Args.hasArg(options::OPT_nostdlibxx); |
Nirav Dave | b0bb614 | 2015-11-24 16:07:21 +0000 | [diff] [blame] | 327 | bool HasNodefaultlib = Args.hasArg(options::OPT_nodefaultlibs); |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 328 | for (Arg *A : Args) { |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 329 | // Unfortunately, we have to parse some forwarding options (-Xassembler, |
| 330 | // -Xlinker, -Xpreprocessor) because we either integrate their functionality |
| 331 | // (assembler and preprocessor), or bypass a previous driver ('collect2'). |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 332 | |
| 333 | // Rewrite linker options, to replace --no-demangle with a custom internal |
| 334 | // option. |
| 335 | if ((A->getOption().matches(options::OPT_Wl_COMMA) || |
| 336 | A->getOption().matches(options::OPT_Xlinker)) && |
| 337 | A->containsValue("--no-demangle")) { |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 338 | // Add the rewritten no-demangle argument. |
| 339 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle)); |
| 340 | |
| 341 | // Add the remaining values as Xlinker arguments. |
Benjamin Kramer | 72e6431 | 2015-09-24 14:48:49 +0000 | [diff] [blame] | 342 | for (StringRef Val : A->getValues()) |
Douglas Katzman | a34b7bf | 2015-06-30 19:32:57 +0000 | [diff] [blame] | 343 | if (Val != "--no-demangle") |
| 344 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_Xlinker), Val); |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 345 | |
| 346 | continue; |
| 347 | } |
| 348 | |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 349 | // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by |
| 350 | // some build systems. We don't try to be complete here because we don't |
| 351 | // care to encourage this usage model. |
| 352 | if (A->getOption().matches(options::OPT_Wp_COMMA) && |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 353 | (A->getValue(0) == StringRef("-MD") || |
| 354 | A->getValue(0) == StringRef("-MMD"))) { |
Daniel Dunbar | 3648ba7 | 2010-06-15 20:30:18 +0000 | [diff] [blame] | 355 | // Rewrite to -MD/-MMD along with -MF. |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 356 | if (A->getValue(0) == StringRef("-MD")) |
Daniel Dunbar | 3648ba7 | 2010-06-15 20:30:18 +0000 | [diff] [blame] | 357 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD)); |
| 358 | else |
| 359 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MMD)); |
Michael J. Spencer | 70d85be | 2012-11-07 23:37:14 +0000 | [diff] [blame] | 360 | if (A->getNumValues() == 2) |
| 361 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF), |
| 362 | A->getValue(1)); |
Daniel Dunbar | 5a9d183 | 2010-06-14 21:37:09 +0000 | [diff] [blame] | 363 | continue; |
| 364 | } |
| 365 | |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 366 | // Rewrite reserved library names. |
| 367 | if (A->getOption().matches(options::OPT_l)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 368 | StringRef Value = A->getValue(); |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 369 | |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 370 | // Rewrite unless -nostdlib is present. |
Dan Albert | f5ffa1a | 2018-12-18 23:29:35 +0000 | [diff] [blame] | 371 | if (!HasNostdlib && !HasNodefaultlib && !HasNostdlibxx && |
| 372 | Value == "stdc++") { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 373 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_stdcxx)); |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 374 | continue; |
| 375 | } |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 376 | |
| 377 | // Rewrite unconditionally. |
| 378 | if (Value == "cc_kext") { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 379 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_cckext)); |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 380 | continue; |
| 381 | } |
Daniel Dunbar | 2cc3f17 | 2010-09-17 00:45:02 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 384 | // Pick up inputs via the -- option. |
| 385 | if (A->getOption().matches(options::OPT__DASH_DASH)) { |
| 386 | A->claim(); |
Benjamin Kramer | 72e6431 | 2015-09-24 14:48:49 +0000 | [diff] [blame] | 387 | for (StringRef Val : A->getValues()) |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 388 | DAL->append(MakeInputArg(*DAL, *Opts, Val, false)); |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 389 | continue; |
| 390 | } |
| 391 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 392 | DAL->append(A); |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 393 | } |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 394 | |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 395 | // Enforce -static if -miamcu is present. |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 396 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) |
| 397 | DAL->AddFlagArg(0, Opts->getOption(options::OPT_static)); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 398 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 399 | // Add a default value of -mlinker-version=, if one was given and the user |
| 400 | // didn't specify one. |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 401 | #if defined(HOST_LINK_VERSION) |
Tim Northover | 018578c | 2015-06-12 19:21:35 +0000 | [diff] [blame] | 402 | if (!Args.hasArg(options::OPT_mlinker_version_EQ) && |
| 403 | strlen(HOST_LINK_VERSION) > 0) { |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 404 | DAL->AddJoinedArg(0, Opts->getOption(options::OPT_mlinker_version_EQ), |
| 405 | HOST_LINK_VERSION); |
Daniel Dunbar | b613ffc | 2010-08-17 22:32:45 +0000 | [diff] [blame] | 406 | DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim(); |
Daniel Dunbar | 628fcf4 | 2010-08-12 00:05:12 +0000 | [diff] [blame] | 407 | } |
| 408 | #endif |
| 409 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 410 | return DAL; |
| 411 | } |
| 412 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 413 | /// Compute target triple from args. |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 414 | /// |
| 415 | /// This routine provides the logic to compute a target triple from various |
| 416 | /// args passed to the driver and the default triple string. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 417 | static llvm::Triple computeTargetTriple(const Driver &D, |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 418 | StringRef TargetTriple, |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 419 | const ArgList &Args, |
| 420 | StringRef DarwinArchName = "") { |
| 421 | // FIXME: Already done in Compilation *Driver::BuildCompilation |
| 422 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 423 | TargetTriple = A->getValue(); |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 424 | |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 425 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 426 | |
Kristina Brooks | 77a4adc | 2018-11-29 03:49:14 +0000 | [diff] [blame] | 427 | // GNU/Hurd's triples should have been -hurd-gnu*, but were historically made |
| 428 | // -gnu* only, and we can not change this, so we have to detect that case as |
| 429 | // being the Hurd OS. |
| 430 | if (TargetTriple.find("-unknown-gnu") != StringRef::npos || |
| 431 | TargetTriple.find("-pc-gnu") != StringRef::npos) |
| 432 | Target.setOSName("hurd"); |
| 433 | |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 434 | // Handle Apple-specific options available here. |
| 435 | if (Target.isOSBinFormatMachO()) { |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 436 | // If an explicit Darwin arch name is given, that trumps all. |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 437 | if (!DarwinArchName.empty()) { |
| 438 | tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName); |
| 439 | return Target; |
| 440 | } |
| 441 | |
| 442 | // Handle the Darwin '-arch' flag. |
| 443 | if (Arg *A = Args.getLastArg(options::OPT_arch)) { |
| 444 | StringRef ArchName = A->getValue(); |
| 445 | tools::darwin::setTripleTypeForMachOArchName(Target, ArchName); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // Handle pseudo-target flags '-mlittle-endian'/'-EL' and |
| 450 | // '-mbig-endian'/'-EB'. |
| 451 | if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, |
| 452 | options::OPT_mbig_endian)) { |
| 453 | if (A->getOption().matches(options::OPT_mlittle_endian)) { |
| 454 | llvm::Triple LE = Target.getLittleEndianArchVariant(); |
| 455 | if (LE.getArch() != llvm::Triple::UnknownArch) |
| 456 | Target = std::move(LE); |
| 457 | } else { |
| 458 | llvm::Triple BE = Target.getBigEndianArchVariant(); |
| 459 | if (BE.getArch() != llvm::Triple::UnknownArch) |
| 460 | Target = std::move(BE); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // 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] | 465 | if (Target.getArch() == llvm::Triple::tce || |
| 466 | Target.getOS() == llvm::Triple::Minix) |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 467 | return Target; |
| 468 | |
| 469 | // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 470 | Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32, |
| 471 | options::OPT_m32, options::OPT_m16); |
| 472 | if (A) { |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 473 | llvm::Triple::ArchType AT = llvm::Triple::UnknownArch; |
| 474 | |
| 475 | if (A->getOption().matches(options::OPT_m64)) { |
| 476 | AT = Target.get64BitArchVariant().getArch(); |
| 477 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 478 | Target.setEnvironment(llvm::Triple::GNU); |
| 479 | } else if (A->getOption().matches(options::OPT_mx32) && |
| 480 | Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) { |
| 481 | AT = llvm::Triple::x86_64; |
| 482 | Target.setEnvironment(llvm::Triple::GNUX32); |
| 483 | } else if (A->getOption().matches(options::OPT_m32)) { |
| 484 | AT = Target.get32BitArchVariant().getArch(); |
| 485 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 486 | Target.setEnvironment(llvm::Triple::GNU); |
| 487 | } else if (A->getOption().matches(options::OPT_m16) && |
| 488 | Target.get32BitArchVariant().getArch() == llvm::Triple::x86) { |
| 489 | AT = llvm::Triple::x86; |
| 490 | Target.setEnvironment(llvm::Triple::CODE16); |
| 491 | } |
| 492 | |
| 493 | if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) |
| 494 | Target.setArch(AT); |
| 495 | } |
| 496 | |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 497 | // Handle -miamcu flag. |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 498 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { |
| 499 | if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86) |
| 500 | D.Diag(diag::err_drv_unsupported_opt_for_target) << "-miamcu" |
| 501 | << Target.str(); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 502 | |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 503 | if (A && !A->getOption().matches(options::OPT_m32)) |
| 504 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 505 | << "-miamcu" << A->getBaseArg().getAsString(Args); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 506 | |
Andrey Turetskiy | 5fea71c | 2016-06-29 10:57:17 +0000 | [diff] [blame] | 507 | Target.setArch(llvm::Triple::x86); |
| 508 | Target.setArchName("i586"); |
| 509 | Target.setEnvironment(llvm::Triple::UnknownEnvironment); |
| 510 | Target.setEnvironmentName(""); |
| 511 | Target.setOS(llvm::Triple::ELFIAMCU); |
| 512 | Target.setVendor(llvm::Triple::UnknownVendor); |
| 513 | Target.setVendorName("intel"); |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Simon Atanasyan | 0c0fb4b | 2018-09-27 05:04:50 +0000 | [diff] [blame] | 516 | // If target is MIPS adjust the target triple |
| 517 | // accordingly to provided ABI name. |
| 518 | A = Args.getLastArg(options::OPT_mabi_EQ); |
Simon Atanasyan | e60eae4 | 2018-10-16 10:19:06 +0000 | [diff] [blame] | 519 | if (A && Target.isMIPS()) { |
| 520 | StringRef ABIName = A->getValue(); |
| 521 | if (ABIName == "32") { |
| 522 | Target = Target.get32BitArchVariant(); |
| 523 | if (Target.getEnvironment() == llvm::Triple::GNUABI64 || |
| 524 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
| 525 | Target.setEnvironment(llvm::Triple::GNU); |
| 526 | } else if (ABIName == "n32") { |
| 527 | Target = Target.get64BitArchVariant(); |
| 528 | if (Target.getEnvironment() == llvm::Triple::GNU || |
| 529 | Target.getEnvironment() == llvm::Triple::GNUABI64) |
| 530 | Target.setEnvironment(llvm::Triple::GNUABIN32); |
| 531 | } else if (ABIName == "64") { |
| 532 | Target = Target.get64BitArchVariant(); |
| 533 | if (Target.getEnvironment() == llvm::Triple::GNU || |
| 534 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
| 535 | Target.setEnvironment(llvm::Triple::GNUABI64); |
| 536 | } |
| 537 | } |
Simon Atanasyan | 0c0fb4b | 2018-09-27 05:04:50 +0000 | [diff] [blame] | 538 | |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 539 | return Target; |
| 540 | } |
| 541 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 542 | // Parse the LTO options and record the type of LTO compilation |
Teresa Johnson | 945bc50 | 2015-10-15 20:35:53 +0000 | [diff] [blame] | 543 | // based on which -f(no-)?lto(=.*)? option occurs last. |
| 544 | void Driver::setLTOMode(const llvm::opt::ArgList &Args) { |
| 545 | LTOMode = LTOK_None; |
| 546 | if (!Args.hasFlag(options::OPT_flto, options::OPT_flto_EQ, |
| 547 | options::OPT_fno_lto, false)) |
| 548 | return; |
| 549 | |
| 550 | StringRef LTOName("full"); |
| 551 | |
| 552 | const Arg *A = Args.getLastArg(options::OPT_flto_EQ); |
Teresa Johnson | 6ef80dc | 2015-11-02 18:03:12 +0000 | [diff] [blame] | 553 | if (A) |
| 554 | LTOName = A->getValue(); |
Teresa Johnson | 945bc50 | 2015-10-15 20:35:53 +0000 | [diff] [blame] | 555 | |
| 556 | LTOMode = llvm::StringSwitch<LTOKind>(LTOName) |
| 557 | .Case("full", LTOK_Full) |
| 558 | .Case("thin", LTOK_Thin) |
| 559 | .Default(LTOK_Unknown); |
| 560 | |
| 561 | if (LTOMode == LTOK_Unknown) { |
| 562 | assert(A); |
| 563 | Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() |
| 564 | << A->getValue(); |
| 565 | } |
| 566 | } |
| 567 | |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 568 | /// Compute the desired OpenMP runtime from the flags provided. |
| 569 | Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const { |
| 570 | StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME); |
| 571 | |
| 572 | const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ); |
| 573 | if (A) |
| 574 | RuntimeName = A->getValue(); |
| 575 | |
| 576 | auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName) |
| 577 | .Case("libomp", OMPRT_OMP) |
| 578 | .Case("libgomp", OMPRT_GOMP) |
| 579 | .Case("libiomp5", OMPRT_IOMP5) |
| 580 | .Default(OMPRT_Unknown); |
| 581 | |
| 582 | if (RT == OMPRT_Unknown) { |
| 583 | if (A) |
| 584 | Diag(diag::err_drv_unsupported_option_argument) |
| 585 | << A->getOption().getName() << A->getValue(); |
| 586 | else |
| 587 | // FIXME: We could use a nicer diagnostic here. |
| 588 | Diag(diag::err_drv_unsupported_opt) << "-fopenmp"; |
| 589 | } |
| 590 | |
| 591 | return RT; |
| 592 | } |
| 593 | |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 594 | void Driver::CreateOffloadingDeviceToolChains(Compilation &C, |
| 595 | InputList &Inputs) { |
| 596 | |
| 597 | // |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 598 | // CUDA/HIP |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 599 | // |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 600 | // 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] | 601 | // or HIP type. However, mixed CUDA/HIP compilation is not supported. |
| 602 | bool IsCuda = |
| 603 | 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] | 604 | return types::isCuda(I.first); |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 605 | }); |
| 606 | bool IsHIP = |
| 607 | llvm::any_of(Inputs, |
| 608 | [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
| 609 | return types::isHIP(I.first); |
| 610 | }) || |
| 611 | C.getInputArgs().hasArg(options::OPT_hip_link); |
| 612 | if (IsCuda && IsHIP) { |
| 613 | Diag(clang::diag::err_drv_mix_cuda_hip); |
| 614 | return; |
| 615 | } |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 616 | if (IsCuda) { |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 617 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 618 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 619 | StringRef DeviceTripleStr; |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 620 | auto OFK = Action::OFK_Cuda; |
| 621 | DeviceTripleStr = |
| 622 | HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda" : "nvptx-nvidia-cuda"; |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 623 | llvm::Triple CudaTriple(DeviceTripleStr); |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 624 | // 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] | 625 | // because the device toolchain we create depends on both. |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 626 | auto &CudaTC = ToolChains[CudaTriple.str() + "/" + HostTriple.str()]; |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 627 | if (!CudaTC) { |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 628 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 629 | *this, CudaTriple, *HostTC, C.getInputArgs(), OFK); |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 630 | } |
Yaxun Liu | f5a6b96 | 2018-05-11 19:21:39 +0000 | [diff] [blame] | 631 | C.addOffloadDeviceToolChain(CudaTC.get(), OFK); |
Yaxun Liu | f614422 | 2018-05-30 00:53:50 +0000 | [diff] [blame] | 632 | } else if (IsHIP) { |
| 633 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 634 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
| 635 | StringRef DeviceTripleStr; |
| 636 | auto OFK = Action::OFK_HIP; |
| 637 | DeviceTripleStr = "amdgcn-amd-amdhsa"; |
| 638 | llvm::Triple HIPTriple(DeviceTripleStr); |
| 639 | // Use the HIP and host triples as the key into the ToolChains map, |
| 640 | // because the device toolchain we create depends on both. |
| 641 | auto &HIPTC = ToolChains[HIPTriple.str() + "/" + HostTriple.str()]; |
| 642 | if (!HIPTC) { |
| 643 | HIPTC = llvm::make_unique<toolchains::HIPToolChain>( |
| 644 | *this, HIPTriple, *HostTC, C.getInputArgs()); |
| 645 | } |
| 646 | C.addOffloadDeviceToolChain(HIPTC.get(), OFK); |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 650 | // OpenMP |
| 651 | // |
| 652 | // We need to generate an OpenMP toolchain if the user specified targets with |
| 653 | // the -fopenmp-targets option. |
| 654 | if (Arg *OpenMPTargets = |
| 655 | C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) { |
| 656 | if (OpenMPTargets->getNumValues()) { |
| 657 | // We expect that -fopenmp-targets is always used in conjunction with the |
| 658 | // option -fopenmp specifying a valid runtime with offloading support, |
| 659 | // i.e. libomp or libiomp. |
| 660 | bool HasValidOpenMPRuntime = C.getInputArgs().hasFlag( |
| 661 | options::OPT_fopenmp, options::OPT_fopenmp_EQ, |
| 662 | options::OPT_fno_openmp, false); |
| 663 | if (HasValidOpenMPRuntime) { |
| 664 | OpenMPRuntimeKind OpenMPKind = getOpenMPRuntime(C.getInputArgs()); |
| 665 | HasValidOpenMPRuntime = |
| 666 | OpenMPKind == OMPRT_OMP || OpenMPKind == OMPRT_IOMP5; |
| 667 | } |
| 668 | |
| 669 | if (HasValidOpenMPRuntime) { |
| 670 | llvm::StringMap<const char *> FoundNormalizedTriples; |
| 671 | for (const char *Val : OpenMPTargets->getValues()) { |
| 672 | llvm::Triple TT(Val); |
| 673 | std::string NormalizedName = TT.normalize(); |
| 674 | |
| 675 | // Make sure we don't have a duplicate triple. |
| 676 | auto Duplicate = FoundNormalizedTriples.find(NormalizedName); |
| 677 | if (Duplicate != FoundNormalizedTriples.end()) { |
| 678 | Diag(clang::diag::warn_drv_omp_offload_target_duplicate) |
| 679 | << Val << Duplicate->second; |
| 680 | continue; |
| 681 | } |
| 682 | |
| 683 | // Store the current triple so that we can check for duplicates in the |
| 684 | // following iterations. |
| 685 | FoundNormalizedTriples[NormalizedName] = Val; |
| 686 | |
| 687 | // If the specified target is invalid, emit a diagnostic. |
| 688 | if (TT.getArch() == llvm::Triple::UnknownArch) |
| 689 | Diag(clang::diag::err_drv_invalid_omp_target) << Val; |
| 690 | else { |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 691 | const ToolChain *TC; |
| 692 | // CUDA toolchains have to be selected differently. They pair host |
| 693 | // and device in their implementation. |
| 694 | if (TT.isNVPTX()) { |
| 695 | const ToolChain *HostTC = |
| 696 | C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 697 | assert(HostTC && "Host toolchain should be always defined."); |
| 698 | auto &CudaTC = |
Gheorghe-Teodor Bercea | 5289843 | 2017-08-09 01:02:19 +0000 | [diff] [blame] | 699 | ToolChains[TT.str() + "/" + HostTC->getTriple().normalize()]; |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 700 | if (!CudaTC) |
| 701 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
Gheorghe-Teodor Bercea | 2c92693 | 2017-08-08 14:33:05 +0000 | [diff] [blame] | 702 | *this, TT, *HostTC, C.getInputArgs(), Action::OFK_OpenMP); |
Gheorghe-Teodor Bercea | 6a5df72 | 2017-07-06 16:08:15 +0000 | [diff] [blame] | 703 | TC = CudaTC.get(); |
| 704 | } else |
| 705 | TC = &getToolChain(C.getInputArgs(), TT); |
| 706 | C.addOffloadDeviceToolChain(TC, Action::OFK_OpenMP); |
Samuel Antao | 39f9da2 | 2016-10-27 16:38:05 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | } else |
| 710 | Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets); |
| 711 | } else |
| 712 | Diag(clang::diag::warn_drv_empty_joined_argument) |
| 713 | << OpenMPTargets->getAsString(C.getInputArgs()); |
| 714 | } |
| 715 | |
| 716 | // |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 717 | // TODO: Add support for other offloading programming models here. |
| 718 | // |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 721 | /// Looks the given directories for the specified file. |
| 722 | /// |
| 723 | /// \param[out] FilePath File path, if the file was found. |
| 724 | /// \param[in] Dirs Directories used for the search. |
| 725 | /// \param[in] FileName Name of the file to search for. |
| 726 | /// \return True if file was found. |
| 727 | /// |
| 728 | /// Looks for file specified by FileName sequentially in directories specified |
| 729 | /// by Dirs. |
| 730 | /// |
| 731 | static bool searchForFile(SmallVectorImpl<char> &FilePath, |
| 732 | ArrayRef<std::string> Dirs, |
| 733 | StringRef FileName) { |
| 734 | SmallString<128> WPath; |
| 735 | for (const StringRef &Dir : Dirs) { |
| 736 | if (Dir.empty()) |
| 737 | continue; |
| 738 | WPath.clear(); |
| 739 | llvm::sys::path::append(WPath, Dir, FileName); |
| 740 | llvm::sys::path::native(WPath); |
| 741 | if (llvm::sys::fs::is_regular_file(WPath)) { |
| 742 | FilePath = std::move(WPath); |
| 743 | return true; |
| 744 | } |
| 745 | } |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | bool Driver::readConfigFile(StringRef FileName) { |
| 750 | // Try reading the given file. |
| 751 | SmallVector<const char *, 32> NewCfgArgs; |
| 752 | if (!llvm::cl::readConfigFile(FileName, Saver, NewCfgArgs)) { |
| 753 | Diag(diag::err_drv_cannot_read_config_file) << FileName; |
| 754 | return true; |
| 755 | } |
| 756 | |
| 757 | // Read options from config file. |
| 758 | llvm::SmallString<128> CfgFileName(FileName); |
| 759 | llvm::sys::path::native(CfgFileName); |
| 760 | ConfigFile = CfgFileName.str(); |
| 761 | bool ContainErrors; |
| 762 | CfgOptions = llvm::make_unique<InputArgList>( |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 763 | ParseArgStrings(NewCfgArgs, IsCLMode(), ContainErrors)); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 764 | if (ContainErrors) { |
| 765 | CfgOptions.reset(); |
| 766 | return true; |
| 767 | } |
| 768 | |
| 769 | if (CfgOptions->hasArg(options::OPT_config)) { |
| 770 | CfgOptions.reset(); |
| 771 | Diag(diag::err_drv_nested_config_file); |
| 772 | return true; |
| 773 | } |
| 774 | |
| 775 | // Claim all arguments that come from a configuration file so that the driver |
| 776 | // does not warn on any that is unused. |
| 777 | for (Arg *A : *CfgOptions) |
| 778 | A->claim(); |
| 779 | return false; |
| 780 | } |
| 781 | |
| 782 | bool Driver::loadConfigFile() { |
| 783 | std::string CfgFileName; |
| 784 | bool FileSpecifiedExplicitly = false; |
| 785 | |
| 786 | // Process options that change search path for config files. |
| 787 | if (CLOptions) { |
| 788 | if (CLOptions->hasArg(options::OPT_config_system_dir_EQ)) { |
| 789 | SmallString<128> CfgDir; |
| 790 | CfgDir.append( |
| 791 | CLOptions->getLastArgValue(options::OPT_config_system_dir_EQ)); |
| 792 | if (!CfgDir.empty()) { |
Sam McCall | 296d832 | 2018-01-02 09:35:10 +0000 | [diff] [blame] | 793 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 794 | SystemConfigDir.clear(); |
| 795 | else |
| 796 | SystemConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 797 | } |
| 798 | } |
| 799 | if (CLOptions->hasArg(options::OPT_config_user_dir_EQ)) { |
| 800 | SmallString<128> CfgDir; |
| 801 | CfgDir.append( |
| 802 | CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ)); |
| 803 | if (!CfgDir.empty()) { |
Sam McCall | 296d832 | 2018-01-02 09:35:10 +0000 | [diff] [blame] | 804 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 805 | UserConfigDir.clear(); |
| 806 | else |
| 807 | UserConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | // First try to find config file specified in command line. |
| 813 | if (CLOptions) { |
| 814 | std::vector<std::string> ConfigFiles = |
| 815 | CLOptions->getAllArgValues(options::OPT_config); |
| 816 | if (ConfigFiles.size() > 1) { |
| 817 | Diag(diag::err_drv_duplicate_config); |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | if (!ConfigFiles.empty()) { |
| 822 | CfgFileName = ConfigFiles.front(); |
| 823 | assert(!CfgFileName.empty()); |
| 824 | |
| 825 | // If argument contains directory separator, treat it as a path to |
| 826 | // configuration file. |
| 827 | if (llvm::sys::path::has_parent_path(CfgFileName)) { |
| 828 | SmallString<128> CfgFilePath; |
| 829 | if (llvm::sys::path::is_relative(CfgFileName)) |
| 830 | llvm::sys::fs::current_path(CfgFilePath); |
| 831 | llvm::sys::path::append(CfgFilePath, CfgFileName); |
| 832 | if (!llvm::sys::fs::is_regular_file(CfgFilePath)) { |
| 833 | Diag(diag::err_drv_config_file_not_exist) << CfgFilePath; |
| 834 | return true; |
| 835 | } |
| 836 | return readConfigFile(CfgFilePath); |
| 837 | } |
| 838 | |
| 839 | FileSpecifiedExplicitly = true; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | // If config file is not specified explicitly, try to deduce configuration |
| 844 | // from executable name. For instance, an executable 'armv7l-clang' will |
| 845 | // search for config file 'armv7l-clang.cfg'. |
| 846 | if (CfgFileName.empty() && !ClangNameParts.TargetPrefix.empty()) |
| 847 | CfgFileName = ClangNameParts.TargetPrefix + '-' + ClangNameParts.ModeSuffix; |
| 848 | |
| 849 | if (CfgFileName.empty()) |
| 850 | return false; |
| 851 | |
| 852 | // Determine architecture part of the file name, if it is present. |
| 853 | StringRef CfgFileArch = CfgFileName; |
| 854 | size_t ArchPrefixLen = CfgFileArch.find('-'); |
| 855 | if (ArchPrefixLen == StringRef::npos) |
| 856 | ArchPrefixLen = CfgFileArch.size(); |
| 857 | llvm::Triple CfgTriple; |
| 858 | CfgFileArch = CfgFileArch.take_front(ArchPrefixLen); |
| 859 | CfgTriple = llvm::Triple(llvm::Triple::normalize(CfgFileArch)); |
| 860 | if (CfgTriple.getArch() == llvm::Triple::ArchType::UnknownArch) |
| 861 | ArchPrefixLen = 0; |
| 862 | |
| 863 | if (!StringRef(CfgFileName).endswith(".cfg")) |
| 864 | CfgFileName += ".cfg"; |
| 865 | |
| 866 | // If config file starts with architecture name and command line options |
| 867 | // redefine architecture (with options like -m32 -LE etc), try finding new |
| 868 | // config file with that architecture. |
| 869 | SmallString<128> FixedConfigFile; |
| 870 | size_t FixedArchPrefixLen = 0; |
| 871 | if (ArchPrefixLen) { |
| 872 | // Get architecture name from config file name like 'i386.cfg' or |
| 873 | // 'armv7l-clang.cfg'. |
| 874 | // Check if command line options changes effective triple. |
| 875 | llvm::Triple EffectiveTriple = computeTargetTriple(*this, |
| 876 | CfgTriple.getTriple(), *CLOptions); |
| 877 | if (CfgTriple.getArch() != EffectiveTriple.getArch()) { |
| 878 | FixedConfigFile = EffectiveTriple.getArchName(); |
| 879 | FixedArchPrefixLen = FixedConfigFile.size(); |
| 880 | // Append the rest of original file name so that file name transforms |
| 881 | // like: i386-clang.cfg -> x86_64-clang.cfg. |
| 882 | if (ArchPrefixLen < CfgFileName.size()) |
| 883 | FixedConfigFile += CfgFileName.substr(ArchPrefixLen); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // Prepare list of directories where config file is searched for. |
| 888 | SmallVector<std::string, 3> CfgFileSearchDirs; |
| 889 | CfgFileSearchDirs.push_back(UserConfigDir); |
| 890 | CfgFileSearchDirs.push_back(SystemConfigDir); |
| 891 | CfgFileSearchDirs.push_back(Dir); |
| 892 | |
| 893 | // Try to find config file. First try file with corrected architecture. |
| 894 | llvm::SmallString<128> CfgFilePath; |
| 895 | if (!FixedConfigFile.empty()) { |
| 896 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 897 | return readConfigFile(CfgFilePath); |
| 898 | // If 'x86_64-clang.cfg' was not found, try 'x86_64.cfg'. |
| 899 | FixedConfigFile.resize(FixedArchPrefixLen); |
| 900 | FixedConfigFile.append(".cfg"); |
| 901 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 902 | return readConfigFile(CfgFilePath); |
| 903 | } |
| 904 | |
| 905 | // Then try original file name. |
| 906 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 907 | return readConfigFile(CfgFilePath); |
| 908 | |
| 909 | // Finally try removing driver mode part: 'x86_64-clang.cfg' -> 'x86_64.cfg'. |
| 910 | if (!ClangNameParts.ModeSuffix.empty() && |
| 911 | !ClangNameParts.TargetPrefix.empty()) { |
| 912 | CfgFileName.assign(ClangNameParts.TargetPrefix); |
| 913 | CfgFileName.append(".cfg"); |
| 914 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 915 | return readConfigFile(CfgFilePath); |
| 916 | } |
| 917 | |
| 918 | // Report error but only if config file was specified explicitly, by option |
| 919 | // --config. If it was deduced from executable name, it is not an error. |
| 920 | if (FileSpecifiedExplicitly) { |
| 921 | Diag(diag::err_drv_config_file_not_found) << CfgFileName; |
| 922 | for (const std::string &SearchDir : CfgFileSearchDirs) |
| 923 | if (!SearchDir.empty()) |
| 924 | Diag(diag::note_drv_config_file_searched_in) << SearchDir; |
| 925 | return true; |
| 926 | } |
| 927 | |
| 928 | return false; |
| 929 | } |
| 930 | |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 931 | Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 932 | llvm::PrettyStackTraceString CrashInfo("Compilation construction"); |
| 933 | |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 934 | // FIXME: Handle environment options which affect driver behavior, somewhere |
Bill Wendling | adbeb9f | 2012-03-12 21:24:57 +0000 | [diff] [blame] | 935 | // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS. |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 936 | |
David Majnemer | 85c25b4 | 2016-07-24 17:44:03 +0000 | [diff] [blame] | 937 | if (Optional<std::string> CompilerPathValue = |
| 938 | llvm::sys::Process::GetEnv("COMPILER_PATH")) { |
| 939 | StringRef CompilerPath = *CompilerPathValue; |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 940 | while (!CompilerPath.empty()) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 941 | std::pair<StringRef, StringRef> Split = |
| 942 | CompilerPath.split(llvm::sys::EnvPathSeparator); |
Chad Rosier | 8230116 | 2011-09-14 00:47:55 +0000 | [diff] [blame] | 943 | PrefixDirs.push_back(Split.first); |
| 944 | CompilerPath = Split.second; |
| 945 | } |
| 946 | } |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 947 | |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 948 | // We look for the driver mode option early, because the mode can affect |
| 949 | // how other options are parsed. |
Zachary Turner | aff19c3 | 2016-08-12 17:47:52 +0000 | [diff] [blame] | 950 | ParseDriverMode(ClangExecutable, ArgList.slice(1)); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 951 | |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 952 | // FIXME: What are we going to do with -V and -b? |
| 953 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 954 | // Arguments specified in command line. |
| 955 | bool ContainsError; |
| 956 | CLOptions = llvm::make_unique<InputArgList>( |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 957 | ParseArgStrings(ArgList.slice(1), IsCLMode(), ContainsError)); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 958 | |
| 959 | // Try parsing configuration file. |
| 960 | if (!ContainsError) |
| 961 | ContainsError = loadConfigFile(); |
| 962 | bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr); |
| 963 | |
| 964 | // All arguments, from both config file and command line. |
| 965 | InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions) |
| 966 | : std::move(*CLOptions)); |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 967 | |
| 968 | auto appendOneArg = [&Args](const Arg *Opt, const Arg *BaseArg) { |
Serge Pavlov | f015a56 | 2018-04-26 06:28:47 +0000 | [diff] [blame] | 969 | unsigned Index = Args.MakeIndex(Opt->getSpelling()); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 970 | Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Opt->getSpelling(), |
Serge Pavlov | f015a56 | 2018-04-26 06:28:47 +0000 | [diff] [blame] | 971 | Index, BaseArg); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 972 | Copy->getValues() = Opt->getValues(); |
| 973 | if (Opt->isClaimed()) |
| 974 | Copy->claim(); |
| 975 | Args.append(Copy); |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 976 | }; |
| 977 | |
| 978 | if (HasConfigFile) |
| 979 | for (auto *Opt : *CLOptions) { |
| 980 | if (Opt->getOption().matches(options::OPT_config)) |
| 981 | continue; |
| 982 | const Arg *BaseArg = &Opt->getBaseArg(); |
| 983 | if (BaseArg == Opt) |
| 984 | BaseArg = nullptr; |
| 985 | appendOneArg(Opt, BaseArg); |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 988 | // In CL mode, look for any pass-through arguments |
| 989 | if (IsCLMode() && !ContainsError) { |
| 990 | SmallVector<const char *, 16> CLModePassThroughArgList; |
| 991 | for (const auto *A : Args.filtered(options::OPT__SLASH_clang)) { |
| 992 | A->claim(); |
| 993 | CLModePassThroughArgList.push_back(A->getValue()); |
| 994 | } |
| 995 | |
| 996 | if (!CLModePassThroughArgList.empty()) { |
| 997 | // Parse any pass through args using default clang processing rather |
| 998 | // than clang-cl processing. |
| 999 | auto CLModePassThroughOptions = llvm::make_unique<InputArgList>( |
| 1000 | ParseArgStrings(CLModePassThroughArgList, false, ContainsError)); |
| 1001 | |
| 1002 | if (!ContainsError) |
| 1003 | for (auto *Opt : *CLModePassThroughOptions) { |
| 1004 | appendOneArg(Opt, nullptr); |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | |
Michael J. Spencer | 7e48b40 | 2019-05-28 22:21:47 +0000 | [diff] [blame] | 1009 | // Check for working directory option before accessing any files |
| 1010 | if (Arg *WD = Args.getLastArg(options::OPT_working_directory)) |
Mikael Holmen | d8d3e17 | 2019-06-03 10:50:41 +0000 | [diff] [blame] | 1011 | if (VFS->setCurrentWorkingDirectory(WD->getValue())) |
Michael J. Spencer | 7e48b40 | 2019-05-28 22:21:47 +0000 | [diff] [blame] | 1012 | Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue(); |
| 1013 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1014 | // FIXME: This stuff needs to go into the Compilation, not the driver. |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1015 | bool CCCPrintPhases; |
Daniel Dunbar | d02cb1d | 2009-03-05 06:38:47 +0000 | [diff] [blame] | 1016 | |
Filipe Cabecinhas | 136f35e | 2015-07-18 06:35:24 +0000 | [diff] [blame] | 1017 | // Silence driver warnings if requested |
| 1018 | Diags.setIgnoreAllWarnings(Args.hasArg(options::OPT_w)); |
| 1019 | |
Rafael Espindola | 59ae799 | 2009-12-07 18:28:29 +0000 | [diff] [blame] | 1020 | // -no-canonical-prefixes is used very early in main. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1021 | Args.ClaimAllArgs(options::OPT_no_canonical_prefixes); |
Rafael Espindola | 59ae799 | 2009-12-07 18:28:29 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 926f81f | 2010-08-02 02:38:03 +0000 | [diff] [blame] | 1023 | // Ignore -pipe. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1024 | Args.ClaimAllArgs(options::OPT_pipe); |
Daniel Dunbar | 926f81f | 2010-08-02 02:38:03 +0000 | [diff] [blame] | 1025 | |
Daniel Dunbar | acd6957 | 2009-12-04 21:55:23 +0000 | [diff] [blame] | 1026 | // Extract -ccc args. |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 1027 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1028 | // FIXME: We need to figure out where this behavior should live. Most of it |
| 1029 | // should be outside in the client; the parts that aren't should have proper |
| 1030 | // options, either by introducing new ones or by overloading gcc ones like -V |
| 1031 | // or -b. |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1032 | CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1033 | CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings); |
| 1034 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1035 | CCCGenericGCCName = A->getValue(); |
Bruno Cardoso Lopes | 52dfe71 | 2017-04-12 21:46:20 +0000 | [diff] [blame] | 1036 | GenReproducer = Args.hasFlag(options::OPT_gen_reproducer, |
| 1037 | options::OPT_fno_crash_diagnostics, |
| 1038 | !!::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")); |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1039 | // FIXME: TargetTriple is used by the target-prefixed calls to as/ld |
Joerg Sonnenberger | 17d7551 | 2012-02-22 19:15:16 +0000 | [diff] [blame] | 1040 | // and getToolChain is const. |
Hans Wennborg | 2e27459 | 2013-08-13 23:38:57 +0000 | [diff] [blame] | 1041 | if (IsCLMode()) { |
Hans Wennborg | 73609a0 | 2014-03-28 01:19:04 +0000 | [diff] [blame] | 1042 | // clang-cl targets MSVC-style Win32. |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1043 | llvm::Triple T(TargetTriple); |
Hans Wennborg | 9d9ce7a | 2014-03-28 20:49:28 +0000 | [diff] [blame] | 1044 | T.setOS(llvm::Triple::Win32); |
Hans Wennborg | 0f0e8d6 | 2015-09-18 17:11:50 +0000 | [diff] [blame] | 1045 | T.setVendor(llvm::Triple::PC); |
Hans Wennborg | 9d9ce7a | 2014-03-28 20:49:28 +0000 | [diff] [blame] | 1046 | T.setEnvironment(llvm::Triple::MSVC); |
Oleg Ranevskyy | 8052318 | 2017-08-31 20:31:30 +0000 | [diff] [blame] | 1047 | T.setObjectFormat(llvm::Triple::COFF); |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1048 | TargetTriple = T.str(); |
Hans Wennborg | 2e27459 | 2013-08-13 23:38:57 +0000 | [diff] [blame] | 1049 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1050 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1051 | TargetTriple = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1052 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1053 | Dir = InstalledDir = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1054 | for (const Arg *A : Args.filtered(options::OPT_B)) { |
Benjamin Kramer | 1a648d1 | 2011-02-08 20:31:42 +0000 | [diff] [blame] | 1055 | A->claim(); |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1056 | PrefixDirs.push_back(A->getValue(0)); |
Benjamin Kramer | 1a648d1 | 2011-02-08 20:31:42 +0000 | [diff] [blame] | 1057 | } |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1058 | if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1059 | SysRoot = A->getValue(); |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1060 | if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ)) |
Peter Collingbourne | 9d9e1fc | 2013-05-27 21:40:20 +0000 | [diff] [blame] | 1061 | DyldPrefix = A->getValue(); |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 1062 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1063 | if (const Arg *A = Args.getLastArg(options::OPT_resource_dir)) |
Bob Wilson | a20a1da | 2013-03-23 05:17:59 +0000 | [diff] [blame] | 1064 | ResourceDir = A->getValue(); |
Jim Grosbach | 061dabf | 2013-03-12 20:17:58 +0000 | [diff] [blame] | 1065 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1066 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) { |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 1067 | SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue()) |
| 1068 | .Case("cwd", SaveTempsCwd) |
| 1069 | .Case("obj", SaveTempsObj) |
| 1070 | .Default(SaveTempsCwd); |
| 1071 | } |
| 1072 | |
Steven Wu | 1257cd8 | 2016-05-18 17:04:52 +0000 | [diff] [blame] | 1073 | setLTOMode(Args); |
| 1074 | |
Steven Wu | 844ab6a | 2016-11-16 06:06:44 +0000 | [diff] [blame] | 1075 | // Process -fembed-bitcode= flags. |
| 1076 | if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) { |
| 1077 | StringRef Name = A->getValue(); |
| 1078 | unsigned Model = llvm::StringSwitch<unsigned>(Name) |
| 1079 | .Case("off", EmbedNone) |
| 1080 | .Case("all", EmbedBitcode) |
| 1081 | .Case("bitcode", EmbedBitcode) |
| 1082 | .Case("marker", EmbedMarker) |
| 1083 | .Default(~0U); |
| 1084 | if (Model == ~0U) { |
| 1085 | Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) |
| 1086 | << Name; |
| 1087 | } else |
| 1088 | BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model); |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1091 | std::unique_ptr<llvm::opt::InputArgList> UArgs = |
| 1092 | llvm::make_unique<InputArgList>(std::move(Args)); |
| 1093 | |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 1094 | // Perform the default argument translations. |
David Blaikie | 69a1d8c | 2015-06-22 22:07:27 +0000 | [diff] [blame] | 1095 | DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs); |
Daniel Dunbar | 775d406 | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 1096 | |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1097 | // Owned by the host. |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 1098 | const ToolChain &TC = getToolChain( |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 1099 | *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs)); |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1100 | |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 1101 | // The compilation takes ownership of Args. |
Benjamin Kramer | 43c0f48 | 2017-06-30 13:21:27 +0000 | [diff] [blame] | 1102 | Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs, |
| 1103 | ContainsError); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1104 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1105 | if (!HandleImmediateArgs(*C)) |
| 1106 | return C; |
| 1107 | |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1108 | // Construct the list of inputs. |
| 1109 | InputList Inputs; |
Hans Wennborg | ed1d072 | 2013-08-13 21:32:29 +0000 | [diff] [blame] | 1110 | BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs); |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1111 | |
Samuel Antao | c1ffba5 | 2016-06-13 18:10:57 +0000 | [diff] [blame] | 1112 | // Populate the tool chains for the offloading devices, if any. |
| 1113 | CreateOffloadingDeviceToolChains(*C, Inputs); |
Justin Lebar | 0e450a5 | 2016-03-30 23:30:25 +0000 | [diff] [blame] | 1114 | |
Chandler Carruth | 7f1417f | 2012-01-24 10:43:44 +0000 | [diff] [blame] | 1115 | // Construct the list of abstract actions to perform for this compilation. On |
Tim Northover | 157d911 | 2014-01-16 08:48:16 +0000 | [diff] [blame] | 1116 | // MachO targets this uses the driver-driver and universal actions. |
| 1117 | if (TC.getTriple().isOSBinFormatMachO()) |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1118 | BuildUniversalActions(*C, C->getDefaultToolChain(), Inputs); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1119 | else |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1120 | BuildActions(*C, C->getArgs(), Inputs, C->getActions()); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1121 | |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1122 | if (CCCPrintPhases) { |
Daniel Dunbar | eb843be | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 1123 | PrintActions(*C); |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1124 | return C; |
| 1125 | } |
| 1126 | |
| 1127 | BuildJobs(*C); |
Daniel Dunbar | adc91e6 | 2009-03-15 01:38:15 +0000 | [diff] [blame] | 1128 | |
| 1129 | return C; |
Daniel Dunbar | ee66cf2 | 2009-03-10 20:52:46 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Justin Bogner | ed9cbe0 | 2015-07-09 06:58:31 +0000 | [diff] [blame] | 1132 | static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) { |
| 1133 | llvm::opt::ArgStringList ASL; |
| 1134 | for (const auto *A : Args) |
| 1135 | A->render(Args, ASL); |
| 1136 | |
| 1137 | for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) { |
| 1138 | if (I != ASL.begin()) |
| 1139 | OS << ' '; |
| 1140 | Command::printArg(OS, *I, true); |
| 1141 | } |
| 1142 | OS << '\n'; |
| 1143 | } |
| 1144 | |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1145 | bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename, |
| 1146 | SmallString<128> &CrashDiagDir) { |
| 1147 | using namespace llvm::sys; |
| 1148 | assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() && |
| 1149 | "Only knows about .crash files on Darwin"); |
| 1150 | |
| 1151 | // The .crash file can be found on at ~/Library/Logs/DiagnosticReports/ |
| 1152 | // (or /Library/Logs/DiagnosticReports for root) and has the filename pattern |
| 1153 | // clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash. |
| 1154 | path::home_directory(CrashDiagDir); |
| 1155 | if (CrashDiagDir.startswith("/var/root")) |
| 1156 | CrashDiagDir = "/"; |
| 1157 | path::append(CrashDiagDir, "Library/Logs/DiagnosticReports"); |
| 1158 | int PID = |
| 1159 | #if LLVM_ON_UNIX |
| 1160 | getpid(); |
| 1161 | #else |
| 1162 | 0; |
| 1163 | #endif |
| 1164 | std::error_code EC; |
| 1165 | fs::file_status FileStatus; |
| 1166 | TimePoint<> LastAccessTime; |
| 1167 | SmallString<128> CrashFilePath; |
| 1168 | // Lookup the .crash files and get the one generated by a subprocess spawned |
| 1169 | // by this driver invocation. |
| 1170 | for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd; |
| 1171 | File != FileEnd && !EC; File.increment(EC)) { |
| 1172 | StringRef FileName = path::filename(File->path()); |
| 1173 | if (!FileName.startswith(Name)) |
| 1174 | continue; |
| 1175 | if (fs::status(File->path(), FileStatus)) |
| 1176 | continue; |
| 1177 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile = |
| 1178 | llvm::MemoryBuffer::getFile(File->path()); |
| 1179 | if (!CrashFile) |
| 1180 | continue; |
| 1181 | // The first line should start with "Process:", otherwise this isn't a real |
| 1182 | // .crash file. |
| 1183 | StringRef Data = CrashFile.get()->getBuffer(); |
| 1184 | if (!Data.startswith("Process:")) |
| 1185 | continue; |
| 1186 | // Parse parent process pid line, e.g: "Parent Process: clang-4.0 [79141]" |
| 1187 | size_t ParentProcPos = Data.find("Parent Process:"); |
| 1188 | if (ParentProcPos == StringRef::npos) |
| 1189 | continue; |
| 1190 | size_t LineEnd = Data.find_first_of("\n", ParentProcPos); |
| 1191 | if (LineEnd == StringRef::npos) |
| 1192 | continue; |
| 1193 | StringRef ParentProcess = Data.slice(ParentProcPos+15, LineEnd).trim(); |
| 1194 | int OpenBracket = -1, CloseBracket = -1; |
| 1195 | for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) { |
| 1196 | if (ParentProcess[i] == '[') |
| 1197 | OpenBracket = i; |
| 1198 | if (ParentProcess[i] == ']') |
| 1199 | CloseBracket = i; |
| 1200 | } |
| 1201 | // Extract the parent process PID from the .crash file and check whether |
| 1202 | // it matches this driver invocation pid. |
| 1203 | int CrashPID; |
| 1204 | if (OpenBracket < 0 || CloseBracket < 0 || |
| 1205 | ParentProcess.slice(OpenBracket + 1, CloseBracket) |
| 1206 | .getAsInteger(10, CrashPID) || CrashPID != PID) { |
| 1207 | continue; |
| 1208 | } |
| 1209 | |
| 1210 | // Found a .crash file matching the driver pid. To avoid getting an older |
| 1211 | // and misleading crash file, continue looking for the most recent. |
| 1212 | // FIXME: the driver can dispatch multiple cc1 invocations, leading to |
| 1213 | // multiple crashes poiting to the same parent process. Since the driver |
| 1214 | // does not collect pid information for the dispatched invocation there's |
| 1215 | // currently no way to distinguish among them. |
| 1216 | const auto FileAccessTime = FileStatus.getLastModificationTime(); |
| 1217 | if (FileAccessTime > LastAccessTime) { |
| 1218 | CrashFilePath.assign(File->path()); |
| 1219 | LastAccessTime = FileAccessTime; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | // If found, copy it over to the location of other reproducer files. |
| 1224 | if (!CrashFilePath.empty()) { |
| 1225 | EC = fs::copy_file(CrashFilePath, ReproCrashFilename); |
| 1226 | if (EC) |
| 1227 | return false; |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | return false; |
| 1232 | } |
| 1233 | |
Eric Christopher | f901e85 | 2011-08-17 22:59:59 +0000 | [diff] [blame] | 1234 | // When clang crashes, produce diagnostic information including the fully |
| 1235 | // preprocessed source file(s). Request that the developer attach the |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1236 | // diagnostic information to a bug report. |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1237 | void Driver::generateCompilationDiagnostics( |
| 1238 | Compilation &C, const Command &FailingCommand, |
| 1239 | StringRef AdditionalInformation, CompilationDiagnosticReport *Report) { |
Chad Rosier | 877c0a2 | 2012-02-22 00:30:39 +0000 | [diff] [blame] | 1240 | if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics)) |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 1241 | return; |
Chad Rosier | bee5a1d | 2012-03-07 00:30:40 +0000 | [diff] [blame] | 1242 | |
Chad Rosier | dbf46a1 | 2013-02-01 18:30:26 +0000 | [diff] [blame] | 1243 | // Don't try to generate diagnostics for link or dsymutil jobs. |
Justin Bogner | e1a33d1 | 2014-10-20 21:02:05 +0000 | [diff] [blame] | 1244 | if (FailingCommand.getCreator().isLinkJob() || |
| 1245 | FailingCommand.getCreator().isDsymutilJob()) |
Chad Rosier | 877c0a2 | 2012-02-22 00:30:39 +0000 | [diff] [blame] | 1246 | return; |
| 1247 | |
Chad Rosier | 8179f11 | 2012-06-19 17:51:34 +0000 | [diff] [blame] | 1248 | // Print the version of the compiler. |
| 1249 | PrintVersion(C, llvm::errs()); |
| 1250 | |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1251 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1252 | << "PLEASE submit a bug report to " BUG_REPORT_URL " and include the " |
| 1253 | "crash backtrace, preprocessed source, and associated run script."; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Suppress driver output and emit preprocessor output to temp file. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 1256 | Mode = CPPMode; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1257 | CCGenDiagnostics = true; |
| 1258 | |
Chad Rosier | cdb008d | 2011-11-02 21:29:05 +0000 | [diff] [blame] | 1259 | // Save the original job command(s). |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 1260 | Command Cmd = FailingCommand; |
Chad Rosier | cdb008d | 2011-11-02 21:29:05 +0000 | [diff] [blame] | 1261 | |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 1262 | // Keep track of whether we produce any errors while trying to produce |
| 1263 | // preprocessed sources. |
| 1264 | DiagnosticErrorTrap Trap(Diags); |
| 1265 | |
| 1266 | // Suppress tool output. |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1267 | C.initCompilationForDiagnostics(); |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 1268 | |
| 1269 | // Construct the list of inputs. |
| 1270 | InputList Inputs; |
| 1271 | BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1272 | |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1273 | for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) { |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1274 | bool IgnoreInput = false; |
| 1275 | |
| 1276 | // Ignore input from stdin or any inputs that cannot be preprocessed. |
Paul Robinson | f44157d | 2014-04-28 22:24:44 +0000 | [diff] [blame] | 1277 | // Check type first as not all linker inputs have a value. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1278 | if (types::getPreprocessedType(it->first) == types::TY_INVALID) { |
Paul Robinson | f44157d | 2014-04-28 22:24:44 +0000 | [diff] [blame] | 1279 | IgnoreInput = true; |
| 1280 | } else if (!strcmp(it->second->getValue(), "-")) { |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1281 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1282 | << "Error generating preprocessed source(s) - " |
| 1283 | "ignoring input from stdin."; |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1284 | IgnoreInput = true; |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | if (IgnoreInput) { |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1288 | it = Inputs.erase(it); |
| 1289 | ie = Inputs.end(); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1290 | } else { |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1291 | ++it; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1292 | } |
Chad Rosier | 4f81fc2 | 2011-08-12 23:30:05 +0000 | [diff] [blame] | 1293 | } |
Chad Rosier | d57133d | 2011-08-18 00:22:25 +0000 | [diff] [blame] | 1294 | |
Chad Rosier | c5103c3 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 1295 | if (Inputs.empty()) { |
| 1296 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1297 | << "Error generating preprocessed source(s) - " |
| 1298 | "no preprocessable inputs."; |
Chad Rosier | c5103c3 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 1299 | return; |
| 1300 | } |
| 1301 | |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1302 | // Don't attempt to generate preprocessed files if multiple -arch options are |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1303 | // used, unless they're all duplicates. |
| 1304 | llvm::StringSet<> ArchNames; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 1305 | for (const Arg *A : C.getArgs()) { |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1306 | if (A->getOption().matches(options::OPT_arch)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1307 | StringRef ArchName = A->getValue(); |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1308 | ArchNames.insert(ArchName); |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1311 | if (ArchNames.size() > 1) { |
| 1312 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1313 | << "Error generating preprocessed source(s) - cannot generate " |
| 1314 | "preprocessed source with multiple -arch options."; |
Chad Rosier | 636d283 | 2012-02-13 18:16:28 +0000 | [diff] [blame] | 1315 | return; |
| 1316 | } |
Chad Rosier | e75ef40 | 2011-09-06 23:52:36 +0000 | [diff] [blame] | 1317 | |
Chandler Carruth | 7f1417f | 2012-01-24 10:43:44 +0000 | [diff] [blame] | 1318 | // Construct the list of abstract actions to perform for this compilation. On |
| 1319 | // Darwin OSes this uses the driver-driver and builds universal actions. |
Chandler Carruth | cb91619 | 2012-01-25 08:49:21 +0000 | [diff] [blame] | 1320 | const ToolChain &TC = C.getDefaultToolChain(); |
Tim Northover | 157d911 | 2014-01-16 08:48:16 +0000 | [diff] [blame] | 1321 | if (TC.getTriple().isOSBinFormatMachO()) |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1322 | BuildUniversalActions(C, TC, Inputs); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1323 | else |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1324 | BuildActions(C, C.getArgs(), Inputs, C.getActions()); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1325 | |
| 1326 | BuildJobs(C); |
| 1327 | |
| 1328 | // If there were errors building the compilation, quit now. |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 1329 | if (Trap.hasErrorOccurred()) { |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1330 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1331 | << "Error generating preprocessed source(s)."; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
| 1334 | |
| 1335 | // Generate preprocessed output. |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1336 | SmallVector<std::pair<int, const Command *>, 4> FailingCommands; |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1337 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1338 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1339 | // If any of the preprocessing commands failed, clean up and exit. |
| 1340 | if (!FailingCommands.empty()) { |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1341 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1342 | << "Error generating preprocessed source(s)."; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1343 | return; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1344 | } |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1345 | |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1346 | const ArgStringList &TempFiles = C.getTempFiles(); |
| 1347 | if (TempFiles.empty()) { |
| 1348 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1349 | << "Error generating preprocessed source(s)."; |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1350 | return; |
| 1351 | } |
| 1352 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1353 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1354 | << "\n********************\n\n" |
| 1355 | "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n" |
| 1356 | "Preprocessed source(s) and associated run script(s) are located at:"; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1357 | |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1358 | SmallString<128> VFS; |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1359 | SmallString<128> ReproCrashFilename; |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1360 | for (const char *TempFile : TempFiles) { |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1361 | Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile; |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1362 | if (Report) |
| 1363 | Report->TemporaryFiles.push_back(TempFile); |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1364 | if (ReproCrashFilename.empty()) { |
| 1365 | ReproCrashFilename = TempFile; |
| 1366 | llvm::sys::path::replace_extension(ReproCrashFilename, ".crash"); |
| 1367 | } |
Justin Bogner | 659ecc3 | 2014-10-20 22:47:23 +0000 | [diff] [blame] | 1368 | if (StringRef(TempFile).endswith(".cache")) { |
| 1369 | // In some cases (modules) we'll dump extra data to help with reproducing |
| 1370 | // the crash into a directory next to the output. |
| 1371 | VFS = llvm::sys::path::filename(TempFile); |
| 1372 | llvm::sys::path::append(VFS, "vfs", "vfs.yaml"); |
| 1373 | } |
| 1374 | } |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Assume associated files are based off of the first temporary file. |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 1377 | CrashReportInfo CrashInfo(TempFiles[0], VFS); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1378 | |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1379 | llvm::SmallString<128> Script(CrashInfo.Filename); |
| 1380 | llvm::sys::path::replace_extension(Script, "sh"); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1381 | std::error_code EC; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 1382 | llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1383 | if (EC) { |
| 1384 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1385 | << "Error generating run script: " << Script << " " << EC.message(); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1386 | } else { |
Justin Bogner | 42220ed | 2015-03-12 00:14:35 +0000 | [diff] [blame] | 1387 | ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n" |
Justin Bogner | ed9cbe0 | 2015-07-09 06:58:31 +0000 | [diff] [blame] | 1388 | << "# Driver args: "; |
| 1389 | printArgList(ScriptOS, C.getInputArgs()); |
| 1390 | ScriptOS << "# Original command: "; |
Justin Bogner | 42220ed | 2015-03-12 00:14:35 +0000 | [diff] [blame] | 1391 | Cmd.Print(ScriptOS, "\n", /*Quote=*/true); |
Justin Bogner | 33bdbc6 | 2014-10-21 18:03:08 +0000 | [diff] [blame] | 1392 | Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo); |
Alex Lorenz | 045c514 | 2018-04-07 00:03:27 +0000 | [diff] [blame] | 1393 | if (!AdditionalInformation.empty()) |
| 1394 | ScriptOS << "\n# Additional information: " << AdditionalInformation |
| 1395 | << "\n"; |
| 1396 | if (Report) |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 1397 | Report->TemporaryFiles.push_back(Script.str()); |
Justin Bogner | c1fdf7f | 2014-10-20 21:47:56 +0000 | [diff] [blame] | 1398 | Diag(clang::diag::note_drv_command_failed_diag_msg) << Script; |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1399 | } |
Saleem Abdulrasool | 3661a82 | 2015-01-12 02:33:09 +0000 | [diff] [blame] | 1400 | |
Bruno Cardoso Lopes | 02681c4 | 2016-11-17 21:41:22 +0000 | [diff] [blame] | 1401 | // On darwin, provide information about the .crash diagnostic report. |
| 1402 | if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) { |
| 1403 | SmallString<128> CrashDiagDir; |
| 1404 | if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) { |
| 1405 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1406 | << ReproCrashFilename.str(); |
| 1407 | } else { // Suggest a directory for the user to look for .crash files. |
| 1408 | llvm::sys::path::append(CrashDiagDir, Name); |
| 1409 | CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash"; |
| 1410 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1411 | << "Crash backtrace is located in"; |
| 1412 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1413 | << CrashDiagDir.str(); |
| 1414 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1415 | << "(choose the .crash file that corresponds to your crash)"; |
| 1416 | } |
| 1417 | } |
| 1418 | |
Saleem Abdulrasool | 3661a82 | 2015-01-12 02:33:09 +0000 | [diff] [blame] | 1419 | for (const auto &A : C.getArgs().filtered(options::OPT_frewrite_map_file, |
| 1420 | options::OPT_frewrite_map_file_EQ)) |
| 1421 | Diag(clang::diag::note_drv_command_failed_diag_msg) << A->getValue(); |
| 1422 | |
Justin Bogner | bc89b18 | 2014-10-20 21:20:27 +0000 | [diff] [blame] | 1423 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1424 | << "\n\n********************"; |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1427 | void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) { |
Simon Atanasyan | 27c0f20 | 2019-04-02 18:03:15 +0000 | [diff] [blame] | 1428 | // Since commandLineFitsWithinSystemLimits() may underestimate system's |
| 1429 | // capacity if the tool does not support response files, there is a chance/ |
| 1430 | // that things will just work without a response file, so we silently just |
| 1431 | // skip it. |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1432 | if (Cmd.getCreator().getResponseFilesSupport() == Tool::RF_None || |
Simon Atanasyan | 27c0f20 | 2019-04-02 18:03:15 +0000 | [diff] [blame] | 1433 | llvm::sys::commandLineFitsWithinSystemLimits(Cmd.getExecutable(), |
| 1434 | Cmd.getArguments())) |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1435 | return; |
| 1436 | |
| 1437 | std::string TmpName = GetTemporaryPath("response", "txt"); |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 1438 | Cmd.setResponseFile(C.addTempFile(C.getArgs().MakeArgString(TmpName))); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1441 | int Driver::ExecuteCompilation( |
| 1442 | Compilation &C, |
| 1443 | SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) { |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1444 | // Just print if -### was present. |
| 1445 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 1446 | C.getJobs().Print(llvm::errs(), "\n", true); |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | // If there were errors building the compilation, quit now. |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 1451 | if (Diags.hasErrorOccurred()) |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1452 | return 1; |
| 1453 | |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1454 | // Set up response file names for each command, if necessary |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1455 | for (auto &Job : C.getJobs()) |
| 1456 | setUpResponseFiles(C, Job); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 1457 | |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame] | 1458 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1459 | |
Daniel Dunbar | 0749479 | 2010-05-22 00:37:20 +0000 | [diff] [blame] | 1460 | // If the command succeeded, we are done. |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1461 | if (FailingCommands.empty()) |
| 1462 | return 0; |
Daniel Dunbar | 0749479 | 2010-05-22 00:37:20 +0000 | [diff] [blame] | 1463 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1464 | // Otherwise, remove result files and print extra information about abnormal |
| 1465 | // failures. |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1466 | int Res = 0; |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 1467 | for (const auto &CmdPair : FailingCommands) { |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1468 | int CommandRes = CmdPair.first; |
Douglas Katzman | 6bbffc4 | 2015-06-25 18:51:37 +0000 | [diff] [blame] | 1469 | const Command *FailingCommand = CmdPair.second; |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1470 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1471 | // Remove result files if we're not saving temps. |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 1472 | if (!isSaveTempsEnabled()) { |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1473 | const JobAction *JA = cast<JobAction>(&FailingCommand->getSource()); |
| 1474 | C.CleanupFileMap(C.getResultFiles(), JA, true); |
| 1475 | |
| 1476 | // Failure result files are valid unless we crashed. |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1477 | if (CommandRes < 0) |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1478 | C.CleanupFileMap(C.getFailureResultFiles(), JA, true); |
| 1479 | } |
| 1480 | |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1481 | #if LLVM_ON_UNIX |
| 1482 | // llvm/lib/Support/Unix/Signals.inc will exit with a special return code |
| 1483 | // for SIGPIPE. Do not print diagnostics for this case. |
| 1484 | if (CommandRes == EX_IOERR) { |
| 1485 | Res = CommandRes; |
| 1486 | continue; |
| 1487 | } |
| 1488 | #endif |
| 1489 | |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1490 | // Print extra information about abnormal failures, if possible. |
| 1491 | // |
| 1492 | // 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] | 1493 | // status was 1, assume the command failed normally. In particular, if it |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1494 | // was the compiler then assume it gave a reasonable error code. Failures |
| 1495 | // in other tools are less common, and they generally have worse |
| 1496 | // diagnostics, so always print the diagnostic there. |
| 1497 | const Tool &FailingTool = FailingCommand->getCreator(); |
| 1498 | |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1499 | if (!FailingCommand->getCreator().hasGoodDiagnostics() || CommandRes != 1) { |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1500 | // FIXME: See FIXME above regarding result code interpretation. |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1501 | if (CommandRes < 0) |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1502 | Diag(clang::diag::err_drv_command_signalled) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1503 | << FailingTool.getShortName(); |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1504 | else |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1505 | Diag(clang::diag::err_drv_command_failed) |
| 1506 | << FailingTool.getShortName() << CommandRes; |
Chad Rosier | dd60e09 | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 1507 | } |
Peter Collingbourne | 119cfaa | 2011-11-21 00:01:05 +0000 | [diff] [blame] | 1508 | } |
Nick Desaulniers | 9d0df31 | 2018-10-15 17:39:00 +0000 | [diff] [blame] | 1509 | return Res; |
Daniel Dunbar | 38bfda6 | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Daniel Dunbar | a7b5e21 | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 1512 | void Driver::PrintHelp(bool ShowHidden) const { |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 1513 | unsigned IncludedFlagsBitmask; |
| 1514 | unsigned ExcludedFlagsBitmask; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1515 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 1516 | getIncludeExcludeOptionFlagMasks(IsCLMode()); |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 1517 | |
| 1518 | ExcludedFlagsBitmask |= options::NoDriverOption; |
| 1519 | if (!ShowHidden) |
| 1520 | ExcludedFlagsBitmask |= HelpHidden; |
| 1521 | |
Fangrui Song | d002823 | 2018-10-10 00:15:33 +0000 | [diff] [blame] | 1522 | std::string Usage = llvm::formatv("{0} [options] file...", Name).str(); |
| 1523 | getOpts().PrintHelp(llvm::outs(), Usage.c_str(), DriverTitle.c_str(), |
George Rimar | 5dbfa4e | 2017-07-26 09:10:17 +0000 | [diff] [blame] | 1524 | IncludedFlagsBitmask, ExcludedFlagsBitmask, |
| 1525 | /*ShowAllAliases=*/false); |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1528 | void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1529 | // FIXME: The following handlers should use a callback mechanism, we don't |
| 1530 | // know what the client would like to do. |
Ted Kremenek | 4c0df3d | 2010-01-23 02:11:34 +0000 | [diff] [blame] | 1531 | OS << getClangFullVersion() << '\n'; |
Daniel Dunbar | b1024880 | 2009-03-26 16:09:13 +0000 | [diff] [blame] | 1532 | const ToolChain &TC = C.getDefaultToolChain(); |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1533 | OS << "Target: " << TC.getTripleString() << '\n'; |
Daniel Dunbar | 10978e4 | 2009-06-16 23:32:58 +0000 | [diff] [blame] | 1534 | |
| 1535 | // Print the threading model. |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 1536 | if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) { |
| 1537 | // Don't print if the ToolChain would have barfed on it already |
| 1538 | if (TC.isThreadModelSupported(A->getValue())) |
| 1539 | OS << "Thread model: " << A->getValue(); |
| 1540 | } else |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 1541 | OS << "Thread model: " << TC.getThreadModel(); |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 1542 | OS << '\n'; |
Chandler Carruth | 9ade6a9 | 2015-08-05 17:07:33 +0000 | [diff] [blame] | 1543 | |
| 1544 | // Print out the install directory. |
| 1545 | OS << "InstalledDir: " << InstalledDir << '\n'; |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 1546 | |
| 1547 | // If configuration file was used, print its path. |
| 1548 | if (!ConfigFile.empty()) |
| 1549 | OS << "Configuration file: " << ConfigFile << '\n'; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1552 | /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories |
| 1553 | /// option. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1554 | static void PrintDiagnosticCategories(raw_ostream &OS) { |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1555 | // Skip the empty category. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1556 | for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max; |
| 1557 | ++i) |
Argyrios Kyrtzidis | 0e37afa | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1558 | OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n'; |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1561 | void Driver::HandleAutocompletions(StringRef PassedFlags) const { |
Yuka Takahashi | 503da8f | 2018-03-05 09:01:31 +0000 | [diff] [blame] | 1562 | if (PassedFlags == "") |
| 1563 | return; |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1564 | // Print out all options that start with a given argument. This is used for |
| 1565 | // shell autocompletion. |
| 1566 | std::vector<std::string> SuggestedCompletions; |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1567 | std::vector<std::string> Flags; |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1568 | |
| 1569 | unsigned short DisableFlags = |
| 1570 | options::NoDriverOption | options::Unsupported | options::Ignored; |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1571 | |
Yuka Takahashi | 46106f5 | 2018-10-24 12:43:25 +0000 | [diff] [blame] | 1572 | // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag," |
| 1573 | // because the latter indicates that the user put space before pushing tab |
| 1574 | // which should end up in a file completion. |
| 1575 | const bool HasSpace = PassedFlags.endswith(","); |
| 1576 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1577 | // Parse PassedFlags by "," as all the command-line flags are passed to this |
| 1578 | // function separated by "," |
| 1579 | StringRef TargetFlags = PassedFlags; |
| 1580 | while (TargetFlags != "") { |
| 1581 | StringRef CurFlag; |
| 1582 | std::tie(CurFlag, TargetFlags) = TargetFlags.split(","); |
| 1583 | Flags.push_back(std::string(CurFlag)); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1586 | // We want to show cc1-only options only when clang is invoked with -cc1 or |
| 1587 | // -Xclang. |
Fangrui Song | 9ac13a1 | 2019-02-10 05:54:57 +0000 | [diff] [blame] | 1588 | if (llvm::is_contained(Flags, "-Xclang") || llvm::is_contained(Flags, "-cc1")) |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1589 | DisableFlags &= ~options::NoDriverOption; |
| 1590 | |
| 1591 | StringRef Cur; |
| 1592 | Cur = Flags.at(Flags.size() - 1); |
| 1593 | StringRef Prev; |
| 1594 | if (Flags.size() >= 2) { |
| 1595 | Prev = Flags.at(Flags.size() - 2); |
| 1596 | SuggestedCompletions = Opts->suggestValueCompletions(Prev, Cur); |
| 1597 | } |
| 1598 | |
| 1599 | if (SuggestedCompletions.empty()) |
| 1600 | SuggestedCompletions = Opts->suggestValueCompletions(Cur, ""); |
| 1601 | |
Yuka Takahashi | 46106f5 | 2018-10-24 12:43:25 +0000 | [diff] [blame] | 1602 | // If Flags were empty, it means the user typed `clang [tab]` where we should |
| 1603 | // list all possible flags. If there was no value completion and the user |
| 1604 | // pressed tab after a space, we should fall back to a file completion. |
| 1605 | // We're printing a newline to be consistent with what we print at the end of |
| 1606 | // this function. |
| 1607 | if (SuggestedCompletions.empty() && HasSpace && !Flags.empty()) { |
| 1608 | llvm::outs() << '\n'; |
| 1609 | return; |
| 1610 | } |
| 1611 | |
Yuka Takahashi | df9c7e3 | 2018-10-24 08:24:16 +0000 | [diff] [blame] | 1612 | // When flag ends with '=' and there was no value completion, return empty |
| 1613 | // string and fall back to the file autocompletion. |
| 1614 | if (SuggestedCompletions.empty() && !Cur.endswith("=")) { |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1615 | // If the flag is in the form of "--autocomplete=-foo", |
| 1616 | // we were requested to print out all option names that start with "-foo". |
| 1617 | // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only". |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1618 | SuggestedCompletions = Opts->findByPrefix(Cur, DisableFlags); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1619 | |
| 1620 | // We have to query the -W flags manually as they're not in the OptTable. |
| 1621 | // TODO: Find a good way to add them to OptTable instead and them remove |
| 1622 | // this code. |
| 1623 | for (StringRef S : DiagnosticIDs::getDiagnosticFlags()) |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1624 | if (S.startswith(Cur)) |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1625 | SuggestedCompletions.push_back(S); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | // Sort the autocomplete candidates so that shells print them out in a |
| 1629 | // deterministic order. We could sort in any way, but we chose |
| 1630 | // case-insensitive sorting for consistency with the -help option |
| 1631 | // which prints out options in the case-insensitive alphabetical order. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 1632 | llvm::sort(SuggestedCompletions, [](StringRef A, StringRef B) { |
| 1633 | if (int X = A.compare_lower(B)) |
| 1634 | return X < 0; |
| 1635 | return A.compare(B) > 0; |
| 1636 | }); |
Yuka Takahashi | 1a89520 | 2017-08-29 17:46:46 +0000 | [diff] [blame] | 1637 | |
| 1638 | llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n'; |
| 1639 | } |
| 1640 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1641 | bool Driver::HandleImmediateArgs(const Compilation &C) { |
Daniel Dunbar | 18974bd | 2010-06-11 22:00:19 +0000 | [diff] [blame] | 1642 | // 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] | 1643 | // don't expect inconsistencies w.r.t. that to matter in practice. |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1644 | |
Daniel Dunbar | 1b09e04 | 2010-09-17 02:47:28 +0000 | [diff] [blame] | 1645 | if (C.getArgs().hasArg(options::OPT_dumpmachine)) { |
| 1646 | llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n'; |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1650 | if (C.getArgs().hasArg(options::OPT_dumpversion)) { |
Daniel Dunbar | e26e500 | 2011-01-12 00:43:47 +0000 | [diff] [blame] | 1651 | // Since -dumpversion is only implemented for pedantic GCC compatibility, we |
| 1652 | // return an answer which matches our definition of __VERSION__. |
Sylvestre Ledru | a199a9b | 2019-03-25 18:06:20 +0000 | [diff] [blame] | 1653 | llvm::outs() << CLANG_VERSION_STRING << "\n"; |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1654 | return false; |
| 1655 | } |
Daniel Dunbar | fb3d747 | 2010-06-14 21:23:12 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | 86ed5b0 | 2010-05-05 05:53:24 +0000 | [diff] [blame] | 1657 | if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) { |
| 1658 | PrintDiagnosticCategories(llvm::outs()); |
| 1659 | return false; |
| 1660 | } |
Daniel Dunbar | a9bbcfa | 2009-04-04 05:17:38 +0000 | [diff] [blame] | 1661 | |
James Molloy | a3c85b8 | 2012-05-01 14:57:16 +0000 | [diff] [blame] | 1662 | if (C.getArgs().hasArg(options::OPT_help) || |
Daniel Dunbar | a7b5e21 | 2009-04-15 16:34:29 +0000 | [diff] [blame] | 1663 | C.getArgs().hasArg(options::OPT__help_hidden)) { |
| 1664 | PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1665 | return false; |
| 1666 | } |
| 1667 | |
Daniel Dunbar | b0006ae | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 1668 | if (C.getArgs().hasArg(options::OPT__version)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1669 | // Follow gcc behavior and use stdout for --version and stderr for -v. |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1670 | PrintVersion(C, llvm::outs()); |
Daniel Dunbar | b0006ae | 2009-04-02 15:05:41 +0000 | [diff] [blame] | 1671 | return false; |
| 1672 | } |
| 1673 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1674 | if (C.getArgs().hasArg(options::OPT_v) || |
Ziang Wan | af857b9 | 2019-06-14 21:42:21 +0000 | [diff] [blame] | 1675 | C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) || |
| 1676 | C.getArgs().hasArg(options::OPT__print_supported_cpus)) { |
Daniel Dunbar | 08e41d6 | 2009-07-21 20:06:58 +0000 | [diff] [blame] | 1677 | PrintVersion(C, llvm::errs()); |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1678 | SuppressMissingInputWarning = true; |
| 1679 | } |
| 1680 | |
Serge Pavlov | 208ac65 | 2018-01-01 13:27:01 +0000 | [diff] [blame] | 1681 | if (C.getArgs().hasArg(options::OPT_v)) { |
| 1682 | if (!SystemConfigDir.empty()) |
| 1683 | llvm::errs() << "System configuration file directory: " |
| 1684 | << SystemConfigDir << "\n"; |
| 1685 | if (!UserConfigDir.empty()) |
| 1686 | llvm::errs() << "User configuration file directory: " |
| 1687 | << UserConfigDir << "\n"; |
| 1688 | } |
| 1689 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1690 | const ToolChain &TC = C.getDefaultToolChain(); |
Chandler Carruth | 0ae39aa | 2013-07-30 17:57:09 +0000 | [diff] [blame] | 1691 | |
| 1692 | if (C.getArgs().hasArg(options::OPT_v)) |
| 1693 | TC.printVerboseInfo(llvm::errs()); |
| 1694 | |
Meador Inge | 51208a3 | 2017-04-04 21:46:50 +0000 | [diff] [blame] | 1695 | if (C.getArgs().hasArg(options::OPT_print_resource_dir)) { |
Meador Inge | a911395 | 2017-04-05 22:27:20 +0000 | [diff] [blame] | 1696 | llvm::outs() << ResourceDir << '\n'; |
Meador Inge | 51208a3 | 2017-04-04 21:46:50 +0000 | [diff] [blame] | 1697 | return false; |
| 1698 | } |
| 1699 | |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1700 | if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { |
| 1701 | llvm::outs() << "programs: ="; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1702 | bool separator = false; |
| 1703 | for (const std::string &Path : TC.getProgramPaths()) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1704 | if (separator) |
Martin Storsjo | ceb71e8 | 2019-04-25 20:03:20 +0000 | [diff] [blame] | 1705 | llvm::outs() << llvm::sys::EnvPathSeparator; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1706 | llvm::outs() << Path; |
| 1707 | separator = true; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1708 | } |
| 1709 | llvm::outs() << "\n"; |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 1710 | llvm::outs() << "libraries: =" << ResourceDir; |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1711 | |
Sebastian Pop | 980920a | 2012-04-16 04:16:43 +0000 | [diff] [blame] | 1712 | StringRef sysroot = C.getSysRoot(); |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1713 | |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1714 | for (const std::string &Path : TC.getFilePaths()) { |
| 1715 | // Always print a separator. ResourceDir was the first item shown. |
Martin Storsjo | ceb71e8 | 2019-04-25 20:03:20 +0000 | [diff] [blame] | 1716 | llvm::outs() << llvm::sys::EnvPathSeparator; |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1717 | // Interpretation of leading '=' is needed only for NetBSD. |
| 1718 | if (Path[0] == '=') |
Douglas Katzman | 26eabf6 | 2015-06-24 15:10:30 +0000 | [diff] [blame] | 1719 | llvm::outs() << sysroot << Path.substr(1); |
Joerg Sonnenberger | 9c3e69b | 2011-07-16 10:50:05 +0000 | [diff] [blame] | 1720 | else |
Douglas Katzman | 51fe7bf | 2015-06-23 22:43:50 +0000 | [diff] [blame] | 1721 | llvm::outs() << Path; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1722 | } |
| 1723 | llvm::outs() << "\n"; |
Daniel Dunbar | 7c92528 | 2009-03-31 21:38:17 +0000 | [diff] [blame] | 1724 | return false; |
Daniel Dunbar | d972e22 | 2009-03-20 04:37:21 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1727 | // FIXME: The following handlers should use a callback mechanism, we don't |
| 1728 | // know what the client would like to do. |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1729 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1730 | llvm::outs() << GetFilePath(A->getValue(), TC) << "\n"; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1731 | return false; |
| 1732 | } |
| 1733 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1734 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { |
Saleem Abdulrasool | e5f3cf8 | 2018-05-01 18:40:42 +0000 | [diff] [blame] | 1735 | StringRef ProgName = A->getValue(); |
| 1736 | |
| 1737 | // Null program name cannot have a path. |
| 1738 | if (! ProgName.empty()) |
| 1739 | llvm::outs() << GetProgramPath(ProgName, TC); |
| 1740 | |
| 1741 | llvm::outs() << "\n"; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1742 | return false; |
| 1743 | } |
| 1744 | |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 1745 | if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) { |
Yuka Takahashi | ba5d4af | 2017-06-20 16:31:31 +0000 | [diff] [blame] | 1746 | StringRef PassedFlags = A->getValue(); |
Yuka Takahashi | 41789e4 | 2018-03-05 08:54:20 +0000 | [diff] [blame] | 1747 | HandleAutocompletions(PassedFlags); |
Yuka Takahashi | c8068db | 2017-05-23 18:39:08 +0000 | [diff] [blame] | 1748 | return false; |
| 1749 | } |
| 1750 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 1751 | if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { |
Michal Gorny | 7cfe480 | 2016-10-10 12:23:40 +0000 | [diff] [blame] | 1752 | ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs()); |
Leo Li | 23bb21c | 2017-08-24 01:51:51 +0000 | [diff] [blame] | 1753 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1754 | RegisterEffectiveTriple TripleRAII(TC, Triple); |
Michal Gorny | 7cfe480 | 2016-10-10 12:23:40 +0000 | [diff] [blame] | 1755 | switch (RLT) { |
| 1756 | case ToolChain::RLT_CompilerRT: |
| 1757 | llvm::outs() << TC.getCompilerRT(C.getArgs(), "builtins") << "\n"; |
| 1758 | break; |
| 1759 | case ToolChain::RLT_Libgcc: |
| 1760 | llvm::outs() << GetFilePath("libgcc.a", TC) << "\n"; |
| 1761 | break; |
| 1762 | } |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1763 | return false; |
| 1764 | } |
| 1765 | |
Daniel Dunbar | 1b3ec3a | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 1766 | if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { |
Douglas Katzman | a34b7bf | 2015-06-30 19:32:57 +0000 | [diff] [blame] | 1767 | for (const Multilib &Multilib : TC.getMultilibs()) |
| 1768 | llvm::outs() << Multilib << "\n"; |
Daniel Dunbar | 1b3ec3a | 2009-06-16 23:25:22 +0000 | [diff] [blame] | 1769 | return false; |
| 1770 | } |
| 1771 | |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 1772 | if (C.getArgs().hasArg(options::OPT_print_multi_directory)) { |
Christian Bruel | 6ccc4a7 | 2018-09-06 14:03:44 +0000 | [diff] [blame] | 1773 | const Multilib &Multilib = TC.getMultilib(); |
| 1774 | if (Multilib.gccSuffix().empty()) |
| 1775 | llvm::outs() << ".\n"; |
| 1776 | else { |
| 1777 | StringRef Suffix(Multilib.gccSuffix()); |
| 1778 | assert(Suffix.front() == '/'); |
| 1779 | llvm::outs() << Suffix.substr(1) << "\n"; |
Jonathan Roelofs | 3fa96d8 | 2014-02-12 01:36:51 +0000 | [diff] [blame] | 1780 | } |
Jonathan Roelofs | 0e7ec60 | 2014-02-12 01:29:25 +0000 | [diff] [blame] | 1781 | return false; |
| 1782 | } |
Petr Hosek | bc86a99 | 2018-08-16 00:22:03 +0000 | [diff] [blame] | 1783 | |
| 1784 | if (C.getArgs().hasArg(options::OPT_print_target_triple)) { |
| 1785 | llvm::outs() << TC.getTripleString() << "\n"; |
| 1786 | return false; |
| 1787 | } |
| 1788 | |
| 1789 | if (C.getArgs().hasArg(options::OPT_print_effective_triple)) { |
| 1790 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1791 | llvm::outs() << Triple.getTriple() << "\n"; |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 1795 | return true; |
| 1796 | } |
| 1797 | |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1798 | // Display an action graph human-readably. Action A is the "sink" node |
| 1799 | // and latest-occuring action. Traversal is in pre-order, visiting the |
| 1800 | // inputs to each action before printing the action itself. |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1801 | static unsigned PrintActions1(const Compilation &C, Action *A, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1802 | std::map<Action *, unsigned> &Ids) { |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1803 | if (Ids.count(A)) // A was already visited. |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1804 | return Ids[A]; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1805 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1806 | std::string str; |
| 1807 | llvm::raw_string_ostream os(str); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1808 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1809 | os << Action::getClassName(A->getKind()) << ", "; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1810 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1811 | os << "\"" << IA->getInputArg().getValue() << "\""; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1812 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1813 | os << '"' << BIA->getArchName() << '"' << ", {" |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 1814 | << PrintActions1(C, *BIA->input_begin(), Ids) << "}"; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1815 | } else if (OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
| 1816 | bool IsFirst = true; |
| 1817 | OA->doOnEachDependence( |
| 1818 | [&](Action *A, const ToolChain *TC, const char *BoundArch) { |
| 1819 | // E.g. for two CUDA device dependences whose bound arch is sm_20 and |
| 1820 | // sm_35 this will generate: |
| 1821 | // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device" |
| 1822 | // (nvptx64-nvidia-cuda:sm_35) {#ID} |
| 1823 | if (!IsFirst) |
| 1824 | os << ", "; |
| 1825 | os << '"'; |
| 1826 | if (TC) |
| 1827 | os << A->getOffloadingKindPrefix(); |
| 1828 | else |
| 1829 | os << "host"; |
| 1830 | os << " ("; |
| 1831 | os << TC->getTriple().normalize(); |
| 1832 | |
| 1833 | if (BoundArch) |
| 1834 | os << ":" << BoundArch; |
| 1835 | os << ")"; |
| 1836 | os << '"'; |
| 1837 | os << " {" << PrintActions1(C, A, Ids) << "}"; |
| 1838 | IsFirst = false; |
| 1839 | }); |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1840 | } else { |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1841 | const ActionList *AL = &A->getInputs(); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 1842 | |
Artem Belevich | 2325675 | 2015-09-22 17:23:09 +0000 | [diff] [blame] | 1843 | if (AL->size()) { |
| 1844 | const char *Prefix = "{"; |
| 1845 | for (Action *PreRequisite : *AL) { |
| 1846 | os << Prefix << PrintActions1(C, PreRequisite, Ids); |
| 1847 | Prefix = ", "; |
| 1848 | } |
| 1849 | os << "}"; |
| 1850 | } else |
| 1851 | os << "{}"; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1854 | // Append offload info for all options other than the offloading action |
| 1855 | // itself (e.g. (cuda-device, sm_20) or (cuda-host)). |
| 1856 | std::string offload_str; |
| 1857 | llvm::raw_string_ostream offload_os(offload_str); |
| 1858 | if (!isa<OffloadAction>(A)) { |
| 1859 | auto S = A->getOffloadingKindPrefix(); |
| 1860 | if (!S.empty()) { |
| 1861 | offload_os << ", (" << S; |
| 1862 | if (A->getOffloadingArch()) |
| 1863 | offload_os << ", " << A->getOffloadingArch(); |
| 1864 | offload_os << ")"; |
| 1865 | } |
| 1866 | } |
| 1867 | |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1868 | unsigned Id = Ids.size(); |
| 1869 | Ids[A] = Id; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1870 | llvm::errs() << Id << ": " << os.str() << ", " |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 1871 | << types::getTypeName(A->getType()) << offload_os.str() << "\n"; |
Daniel Dunbar | aaf1ea6 | 2009-03-13 12:19:02 +0000 | [diff] [blame] | 1872 | |
| 1873 | return Id; |
| 1874 | } |
| 1875 | |
Douglas Katzman | fd52867 | 2015-06-11 15:05:22 +0000 | [diff] [blame] | 1876 | // Print the action graphs in a compilation C. |
| 1877 | // 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] | 1878 | void Driver::PrintActions(const Compilation &C) const { |
Douglas Katzman | b7e8ef0 | 2015-06-25 19:37:41 +0000 | [diff] [blame] | 1879 | std::map<Action *, unsigned> Ids; |
| 1880 | for (Action *A : C.getActions()) |
| 1881 | PrintActions1(C, A, Ids); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1884 | /// Check whether the given input tree contains any compilation or |
Joerg Sonnenberger | 5fe4a7d | 2011-05-06 14:05:11 +0000 | [diff] [blame] | 1885 | /// assembly actions. |
| 1886 | static bool ContainsCompileOrAssembleAction(const Action *A) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1887 | if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A) || |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 1888 | isa<AssembleJobAction>(A)) |
Daniel Dunbar | 00d3d8e | 2010-06-29 16:38:33 +0000 | [diff] [blame] | 1889 | return true; |
| 1890 | |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 1891 | for (const Action *Input : A->inputs()) |
Nico Weber | fb80f96 | 2015-09-19 21:36:51 +0000 | [diff] [blame] | 1892 | if (ContainsCompileOrAssembleAction(Input)) |
Daniel Dunbar | 00d3d8e | 2010-06-29 16:38:33 +0000 | [diff] [blame] | 1893 | return true; |
| 1894 | |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
Artem Belevich | 5e2a3ec | 2015-11-17 22:28:40 +0000 | [diff] [blame] | 1898 | void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC, |
| 1899 | const InputList &BAInputs) const { |
| 1900 | DerivedArgList &Args = C.getArgs(); |
| 1901 | ActionList &Actions = C.getActions(); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1902 | llvm::PrettyStackTraceString CrashInfo("Building universal build actions"); |
| 1903 | // Collect the list of architectures. Duplicates are allowed, but should only |
| 1904 | // be handled once (in the order seen). |
Daniel Dunbar | e5dc482 | 2009-03-13 20:33:35 +0000 | [diff] [blame] | 1905 | llvm::StringSet<> ArchNames; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1906 | SmallVector<const char *, 4> Archs; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 1907 | for (Arg *A : Args) { |
Daniel Dunbar | 0bfb21e | 2009-11-19 03:26:40 +0000 | [diff] [blame] | 1908 | if (A->getOption().matches(options::OPT_arch)) { |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1909 | // Validate the option here; we don't save the type here because its |
| 1910 | // particular spelling may participate in other driver choices. |
| 1911 | llvm::Triple::ArchType Arch = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1912 | tools::darwin::getArchTypeForMachOArchName(A->getValue()); |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1913 | if (Arch == llvm::Triple::UnknownArch) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1914 | Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args); |
Daniel Dunbar | 9c3f7c4 | 2009-09-08 23:37:30 +0000 | [diff] [blame] | 1915 | continue; |
| 1916 | } |
| 1917 | |
Daniel Dunbar | 2da0272 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 1918 | A->claim(); |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 1919 | if (ArchNames.insert(A->getValue()).second) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 1920 | Archs.push_back(A->getValue()); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1924 | // When there is no explicit arch for this platform, make sure we still bind |
| 1925 | // the architecture (to the default) so that -Xarch_ is handled correctly. |
Daniel Dunbar | eb843be | 2009-03-18 03:13:20 +0000 | [diff] [blame] | 1926 | if (!Archs.size()) |
Daniel Dunbar | c3bd9f5 | 2012-11-08 03:38:26 +0000 | [diff] [blame] | 1927 | Archs.push_back(Args.MakeArgString(TC.getDefaultUniversalArchName())); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1928 | |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1929 | ActionList SingleActions; |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 1930 | BuildActions(C, Args, BAInputs, SingleActions); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1931 | |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1932 | // Add in arch bindings for every top level action, as well as lipo and |
| 1933 | // dsymutil steps if needed. |
Nico Weber | fb80f96 | 2015-09-19 21:36:51 +0000 | [diff] [blame] | 1934 | for (Action* Act : SingleActions) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1935 | // Make sure we can lipo this kind of output. If not (and it is an actual |
| 1936 | // output) then we disallow, since we can't create an output file with the |
| 1937 | // right name without overwriting it. We could remove this oddity by just |
| 1938 | // changing the output names to include the arch, which would also fix |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1939 | // -save-temps. Compatibility wins for now. |
| 1940 | |
Daniel Dunbar | e2ca3bd | 2009-03-13 17:46:02 +0000 | [diff] [blame] | 1941 | if (Archs.size() > 1 && !types::canLipoType(Act->getType())) |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1942 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 1943 | << types::getTypeName(Act->getType()); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1944 | |
| 1945 | ActionList Inputs; |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1946 | for (unsigned i = 0, e = Archs.size(); i != e; ++i) |
| 1947 | Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i])); |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1948 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 1949 | // Lipo if necessary, we do it this way because we need to set the arch flag |
| 1950 | // so that -Xarch_ gets overwritten. |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1951 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 1952 | Actions.append(Inputs.begin(), Inputs.end()); |
| 1953 | else |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1954 | Actions.push_back(C.MakeAction<LipoJobAction>(Inputs, Act->getType())); |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1955 | |
Eric Christopher | 65c05fa | 2012-02-06 19:43:51 +0000 | [diff] [blame] | 1956 | // Handle debug info queries. |
| 1957 | Arg *A = Args.getLastArg(options::OPT_g_Group); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1958 | if (A && !A->getOption().matches(options::OPT_g0) && |
| 1959 | !A->getOption().matches(options::OPT_gstabs) && |
| 1960 | ContainsCompileOrAssembleAction(Actions.back())) { |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 1961 | |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1962 | // Add a 'dsymutil' step if necessary, when debug info is enabled and we |
| 1963 | // 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] | 1964 | // because the debug info will refer to a temporary object file which |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1965 | // will be removed at the end of the compilation process. |
| 1966 | if (Act->getType() == types::TY_Image) { |
| 1967 | ActionList Inputs; |
| 1968 | Inputs.push_back(Actions.back()); |
| 1969 | Actions.pop_back(); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1970 | Actions.push_back( |
| 1971 | C.MakeAction<DsymutilJobAction>(Inputs, types::TY_dSYM)); |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 1972 | } |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1973 | |
Ben Langmuir | 9b9a8d3 | 2014-02-06 18:53:25 +0000 | [diff] [blame] | 1974 | // Verify the debug info output. |
Alp Toker | e9d2bfc | 2014-01-17 02:06:23 +0000 | [diff] [blame] | 1975 | if (Args.hasArg(options::OPT_verify_debug_info)) { |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1976 | Action* LastAction = Actions.back(); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1977 | Actions.pop_back(); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 1978 | Actions.push_back(C.MakeAction<VerifyDebugInfoJobAction>( |
| 1979 | LastAction, types::TY_Nothing)); |
David Blaikie | 6f9f4eb | 2012-04-15 21:22:10 +0000 | [diff] [blame] | 1980 | } |
| 1981 | } |
Daniel Dunbar | f479c12 | 2009-03-12 18:40:18 +0000 | [diff] [blame] | 1982 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Nico Weber | 9b2830b | 2019-05-23 17:58:33 +0000 | [diff] [blame] | 1985 | bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value, |
| 1986 | types::ID Ty, bool TypoCorrect) const { |
| 1987 | if (!getCheckInputsExist()) |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1988 | return true; |
| 1989 | |
| 1990 | // stdin always exists. |
| 1991 | if (Value == "-") |
| 1992 | return true; |
| 1993 | |
Michael J. Spencer | 7e48b40 | 2019-05-28 22:21:47 +0000 | [diff] [blame] | 1994 | if (getVFS().exists(Value)) |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 1995 | return true; |
| 1996 | |
Nico Weber | 9b2830b | 2019-05-23 17:58:33 +0000 | [diff] [blame] | 1997 | if (IsCLMode()) { |
Michael J. Spencer | 7e48b40 | 2019-05-28 22:21:47 +0000 | [diff] [blame] | 1998 | if (!llvm::sys::path::is_absolute(Twine(Value)) && |
Hans Wennborg | 12f4f8b | 2016-04-15 01:12:32 +0000 | [diff] [blame] | 1999 | llvm::sys::Process::FindInEnvPath("LIB", Value)) |
| 2000 | return true; |
| 2001 | |
| 2002 | if (Args.hasArg(options::OPT__SLASH_link) && Ty == types::TY_Object) { |
| 2003 | // Arguments to the /link flag might cause the linker to search for object |
| 2004 | // and library files in paths we don't know about. Don't error in such |
| 2005 | // cases. |
| 2006 | return true; |
| 2007 | } |
| 2008 | } |
Hans Wennborg | 23d26a3 | 2014-06-18 17:21:50 +0000 | [diff] [blame] | 2009 | |
Nico Weber | 9b2830b | 2019-05-23 17:58:33 +0000 | [diff] [blame] | 2010 | if (TypoCorrect) { |
| 2011 | // Check if the filename is a typo for an option flag. OptTable thinks |
| 2012 | // that all args that are not known options and that start with / are |
| 2013 | // filenames, but e.g. `/diagnostic:caret` is more likely a typo for |
| 2014 | // the option `/diagnostics:caret` than a reference to a file in the root |
| 2015 | // directory. |
| 2016 | unsigned IncludedFlagsBitmask; |
| 2017 | unsigned ExcludedFlagsBitmask; |
| 2018 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
| 2019 | getIncludeExcludeOptionFlagMasks(IsCLMode()); |
| 2020 | std::string Nearest; |
| 2021 | if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask, |
| 2022 | ExcludedFlagsBitmask) <= 1) { |
| 2023 | Diag(clang::diag::err_drv_no_such_file_with_suggestion) |
Michael J. Spencer | 7e48b40 | 2019-05-28 22:21:47 +0000 | [diff] [blame] | 2024 | << Value << Nearest; |
Nico Weber | 9b2830b | 2019-05-23 17:58:33 +0000 | [diff] [blame] | 2025 | return false; |
| 2026 | } |
| 2027 | } |
| 2028 | |
Michael J. Spencer | 7e48b40 | 2019-05-28 22:21:47 +0000 | [diff] [blame] | 2029 | Diag(clang::diag::err_drv_no_such_file) << Value; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2030 | return false; |
| 2031 | } |
| 2032 | |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2033 | // Construct a the list of inputs and their types. |
Hans Wennborg | 5536285 | 2014-05-02 22:55:30 +0000 | [diff] [blame] | 2034 | void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args, |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2035 | InputList &Inputs) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2036 | // Track the current user specified (-x) input. We also explicitly track the |
| 2037 | // argument used to set the type; we only want to claim the type when we |
| 2038 | // actually use it, so we warn about unused -x arguments. |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 2039 | types::ID InputType = types::TY_Nothing; |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 2040 | Arg *InputTypeArg = nullptr; |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 2041 | |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2042 | // 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] | 2043 | if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC, |
| 2044 | options::OPT__SLASH_TP)) { |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2045 | InputTypeArg = TCTP; |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2046 | InputType = TCTP->getOption().matches(options::OPT__SLASH_TC) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 2047 | ? types::TY_C |
| 2048 | : types::TY_CXX; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2049 | |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 2050 | Arg *Previous = nullptr; |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2051 | bool ShowNote = false; |
Simon Atanasyan | 27c0f20 | 2019-04-02 18:03:15 +0000 | [diff] [blame] | 2052 | for (Arg *A : |
| 2053 | Args.filtered(options::OPT__SLASH_TC, options::OPT__SLASH_TP)) { |
Richard Smith | 0aef305 | 2017-02-18 01:14:43 +0000 | [diff] [blame] | 2054 | if (Previous) { |
| 2055 | Diag(clang::diag::warn_drv_overriding_flag_option) |
| 2056 | << Previous->getSpelling() << A->getSpelling(); |
| 2057 | ShowNote = true; |
| 2058 | } |
| 2059 | Previous = A; |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2060 | } |
Hans Wennborg | 0d0b19c | 2013-08-12 18:34:17 +0000 | [diff] [blame] | 2061 | if (ShowNote) |
| 2062 | Diag(clang::diag::note_drv_t_option_is_global); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2063 | |
| 2064 | // No driver mode exposes -x and /TC or /TP; we don't support mixing them. |
| 2065 | assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed"); |
| 2066 | } |
| 2067 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 2068 | for (Arg *A : Args) { |
Michael J. Spencer | ad3ccc3 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 2069 | if (A->getOption().getKind() == Option::InputClass) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2070 | const char *Value = A->getValue(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2071 | types::ID Ty = types::TY_INVALID; |
| 2072 | |
| 2073 | // Infer the input type if necessary. |
Daniel Dunbar | c5a5ac5 | 2009-03-13 17:57:10 +0000 | [diff] [blame] | 2074 | if (InputType == types::TY_Nothing) { |
| 2075 | // If there was an explicit arg for this, claim it. |
| 2076 | if (InputTypeArg) |
| 2077 | InputTypeArg->claim(); |
| 2078 | |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2079 | // stdin must be handled specially. |
| 2080 | if (memcmp(Value, "-", 2) == 0) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2081 | // If running with -E, treat as a C input (this changes the builtin |
| 2082 | // macros, for example). This may be overridden by -ObjC below. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2083 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2084 | // Otherwise emit an error but still use a valid type to avoid |
| 2085 | // spurious errors (e.g., no inputs). |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2086 | if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP()) |
Hans Wennborg | cfdd8b5 | 2014-01-29 01:04:40 +0000 | [diff] [blame] | 2087 | Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl |
| 2088 | : clang::diag::err_drv_unknown_stdin_type); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2089 | Ty = types::TY_C; |
| 2090 | } else { |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2091 | // Otherwise lookup by extension. |
Reid Kleckner | a643e64 | 2018-12-26 21:04:08 +0000 | [diff] [blame] | 2092 | // Fallback is C if invoked as C preprocessor, C++ if invoked with |
| 2093 | // clang-cl /E, or Object otherwise. |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2094 | // 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] | 2095 | // idea of what .s is. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2096 | if (const char *Ext = strrchr(Value, '.')) |
Daniel Dunbar | cc7df6c | 2010-08-02 05:43:56 +0000 | [diff] [blame] | 2097 | Ty = TC.LookupTypeForExtension(Ext + 1); |
Daniel Dunbar | ea9f032 | 2009-03-20 23:39:23 +0000 | [diff] [blame] | 2098 | |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2099 | if (Ty == types::TY_INVALID) { |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2100 | if (CCCIsCPP()) |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2101 | Ty = types::TY_C; |
Reid Kleckner | a643e64 | 2018-12-26 21:04:08 +0000 | [diff] [blame] | 2102 | else if (IsCLMode() && Args.hasArgNoClaim(options::OPT_E)) |
| 2103 | Ty = types::TY_CXX; |
Joerg Sonnenberger | bdbdf70 | 2011-03-16 22:45:02 +0000 | [diff] [blame] | 2104 | else |
| 2105 | Ty = types::TY_Object; |
| 2106 | } |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 2107 | |
| 2108 | // If the driver is invoked as C++ compiler (like clang++ or c++) it |
| 2109 | // should autodetect some input files as C++ for g++ compatibility. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2110 | if (CCCIsCXX()) { |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 2111 | types::ID OldTy = Ty; |
| 2112 | Ty = types::lookupCXXTypeForCType(Ty); |
| 2113 | |
| 2114 | if (Ty != OldTy) |
| 2115 | Diag(clang::diag::warn_drv_treating_input_as_cxx) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 2116 | << getTypeName(OldTy) << getTypeName(Ty); |
Daniel Dunbar | 0ac9445 | 2010-02-17 20:32:58 +0000 | [diff] [blame] | 2117 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Daniel Dunbar | 82b2210 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 2120 | // -ObjC and -ObjC++ override the default language, but only for "source |
| 2121 | // files". We just treat everything that isn't a linker input as a |
| 2122 | // source file. |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2123 | // |
Daniel Dunbar | 82b2210 | 2009-05-18 21:47:54 +0000 | [diff] [blame] | 2124 | // 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] | 2125 | if (Ty != types::TY_Object) { |
| 2126 | if (Args.hasArg(options::OPT_ObjC)) |
| 2127 | Ty = types::TY_ObjC; |
| 2128 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 2129 | Ty = types::TY_ObjCXX; |
| 2130 | } |
| 2131 | } else { |
| 2132 | assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
Ehsan Akhgari | 7e954ea | 2014-09-12 18:15:10 +0000 | [diff] [blame] | 2133 | if (!InputTypeArg->getOption().matches(options::OPT_x)) { |
| 2134 | // If emulating cl.exe, make sure that /TC and /TP don't affect input |
| 2135 | // object files. |
| 2136 | const char *Ext = strrchr(Value, '.'); |
| 2137 | if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object) |
| 2138 | Ty = types::TY_Object; |
| 2139 | } |
| 2140 | if (Ty == types::TY_INVALID) { |
| 2141 | Ty = InputType; |
| 2142 | InputTypeArg->claim(); |
| 2143 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Nico Weber | 9b2830b | 2019-05-23 17:58:33 +0000 | [diff] [blame] | 2146 | if (DiagnoseInputExistence(Args, Value, Ty, /*TypoCorrect=*/true)) |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2147 | Inputs.push_back(std::make_pair(Ty, A)); |
| 2148 | |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2149 | } else if (A->getOption().matches(options::OPT__SLASH_Tc)) { |
| 2150 | StringRef Value = A->getValue(); |
Nico Weber | 9b2830b | 2019-05-23 17:58:33 +0000 | [diff] [blame] | 2151 | if (DiagnoseInputExistence(Args, Value, types::TY_C, |
| 2152 | /*TypoCorrect=*/false)) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2153 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2154 | Inputs.push_back(std::make_pair(types::TY_C, InputArg)); |
| 2155 | } |
| 2156 | A->claim(); |
| 2157 | } else if (A->getOption().matches(options::OPT__SLASH_Tp)) { |
| 2158 | StringRef Value = A->getValue(); |
Nico Weber | 9b2830b | 2019-05-23 17:58:33 +0000 | [diff] [blame] | 2159 | if (DiagnoseInputExistence(Args, Value, types::TY_CXX, |
| 2160 | /*TypoCorrect=*/false)) { |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2161 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
Hans Wennborg | 6ee64d5 | 2013-08-06 00:20:31 +0000 | [diff] [blame] | 2162 | Inputs.push_back(std::make_pair(types::TY_CXX, InputArg)); |
| 2163 | } |
| 2164 | A->claim(); |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 2165 | } else if (A->getOption().hasFlag(options::LinkerInput)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2166 | // Just treat as object type, we could make a special type for this if |
| 2167 | // necessary. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2168 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 2169 | |
Daniel Dunbar | 0bfb21e | 2009-11-19 03:26:40 +0000 | [diff] [blame] | 2170 | } else if (A->getOption().matches(options::OPT_x)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2171 | InputTypeArg = A; |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2172 | InputType = types::lookupTypeForTypeSpecifier(A->getValue()); |
Chad Rosier | 706c235 | 2012-04-07 00:01:31 +0000 | [diff] [blame] | 2173 | A->claim(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2174 | |
| 2175 | // Follow gcc behavior and treat as linker input for invalid -x |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 2176 | // 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] | 2177 | // this isn't very important, we might as well be bug compatible. |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2178 | if (!InputType) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 2179 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2180 | InputType = types::TY_Object; |
| 2181 | } |
Hans Wennborg | 091f1b6 | 2017-01-27 17:09:41 +0000 | [diff] [blame] | 2182 | } else if (A->getOption().getID() == options::OPT__SLASH_U) { |
| 2183 | assert(A->getNumValues() == 1 && "The /U option has one value."); |
| 2184 | StringRef Val = A->getValue(0); |
| 2185 | if (Val.find_first_of("/\\") != StringRef::npos) { |
| 2186 | // Warn about e.g. "/Users/me/myfile.c". |
| 2187 | Diag(diag::warn_slash_u_filename) << Val; |
| 2188 | Diag(diag::note_use_dashdash); |
| 2189 | } |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 2190 | } |
| 2191 | } |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 2192 | if (CCCIsCPP() && Inputs.empty()) { |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 2193 | // If called as standalone preprocessor, stdin is processed |
| 2194 | // if no other input is present. |
David Blaikie | 0aaa762 | 2017-01-13 17:34:15 +0000 | [diff] [blame] | 2195 | Arg *A = MakeInputArg(Args, *Opts, "-"); |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 2196 | Inputs.push_back(std::make_pair(types::TY_C, A)); |
| 2197 | } |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2200 | namespace { |
| 2201 | /// Provides a convenient interface for different programming models to generate |
| 2202 | /// the required device actions. |
| 2203 | class OffloadingActionBuilder final { |
| 2204 | /// Flag used to trace errors in the builder. |
| 2205 | bool IsValid = false; |
| 2206 | |
| 2207 | /// The compilation that is using this builder. |
| 2208 | Compilation &C; |
| 2209 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2210 | /// Map between an input argument and the offload kinds used to process it. |
| 2211 | std::map<const Arg *, unsigned> InputArgToOffloadKindMap; |
| 2212 | |
| 2213 | /// Builder interface. It doesn't build anything or keep any state. |
| 2214 | class DeviceActionBuilder { |
| 2215 | public: |
| 2216 | typedef llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PhasesTy; |
| 2217 | |
| 2218 | enum ActionBuilderReturnCode { |
| 2219 | // The builder acted successfully on the current action. |
| 2220 | ABRT_Success, |
| 2221 | // The builder didn't have to act on the current action. |
| 2222 | ABRT_Inactive, |
| 2223 | // The builder was successful and requested the host action to not be |
| 2224 | // generated. |
| 2225 | ABRT_Ignore_Host, |
| 2226 | }; |
| 2227 | |
| 2228 | protected: |
| 2229 | /// Compilation associated with this builder. |
| 2230 | Compilation &C; |
| 2231 | |
| 2232 | /// Tool chains associated with this builder. The same programming |
| 2233 | /// model may have associated one or more tool chains. |
| 2234 | SmallVector<const ToolChain *, 2> ToolChains; |
| 2235 | |
| 2236 | /// The derived arguments associated with this builder. |
| 2237 | DerivedArgList &Args; |
| 2238 | |
| 2239 | /// The inputs associated with this builder. |
| 2240 | const Driver::InputList &Inputs; |
| 2241 | |
| 2242 | /// The associated offload kind. |
| 2243 | Action::OffloadKind AssociatedOffloadKind = Action::OFK_None; |
| 2244 | |
| 2245 | public: |
| 2246 | DeviceActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2247 | const Driver::InputList &Inputs, |
| 2248 | Action::OffloadKind AssociatedOffloadKind) |
| 2249 | : C(C), Args(Args), Inputs(Inputs), |
| 2250 | AssociatedOffloadKind(AssociatedOffloadKind) {} |
| 2251 | virtual ~DeviceActionBuilder() {} |
| 2252 | |
| 2253 | /// Fill up the array \a DA with all the device dependences that should be |
| 2254 | /// added to the provided host action \a HostAction. By default it is |
| 2255 | /// inactive. |
| 2256 | virtual ActionBuilderReturnCode |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2257 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2258 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2259 | PhasesTy &Phases) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2260 | return ABRT_Inactive; |
| 2261 | } |
| 2262 | |
| 2263 | /// Update the state to include the provided host action \a HostAction as a |
| 2264 | /// dependency of the current device action. By default it is inactive. |
| 2265 | virtual ActionBuilderReturnCode addDeviceDepences(Action *HostAction) { |
| 2266 | return ABRT_Inactive; |
| 2267 | } |
| 2268 | |
| 2269 | /// Append top level actions generated by the builder. Return true if errors |
| 2270 | /// were found. |
| 2271 | virtual void appendTopLevelActions(ActionList &AL) {} |
| 2272 | |
| 2273 | /// Append linker actions generated by the builder. Return true if errors |
| 2274 | /// were found. |
| 2275 | virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {} |
| 2276 | |
| 2277 | /// Initialize the builder. Return true if any initialization errors are |
| 2278 | /// found. |
| 2279 | virtual bool initialize() { return false; } |
| 2280 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2281 | /// Return true if the builder can use bundling/unbundling. |
| 2282 | virtual bool canUseBundlerUnbundler() const { return false; } |
| 2283 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2284 | /// Return true if this builder is valid. We have a valid builder if we have |
| 2285 | /// associated device tool chains. |
| 2286 | bool isValid() { return !ToolChains.empty(); } |
| 2287 | |
| 2288 | /// Return the associated offload kind. |
| 2289 | Action::OffloadKind getAssociatedOffloadKind() { |
| 2290 | return AssociatedOffloadKind; |
| 2291 | } |
| 2292 | }; |
| 2293 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2294 | /// Base class for CUDA/HIP action builder. It injects device code in |
| 2295 | /// the host backend action. |
| 2296 | class CudaActionBuilderBase : public DeviceActionBuilder { |
| 2297 | protected: |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2298 | /// Flags to signal if the user requested host-only or device-only |
| 2299 | /// compilation. |
| 2300 | bool CompileHostOnly = false; |
| 2301 | bool CompileDeviceOnly = false; |
| 2302 | |
| 2303 | /// List of GPU architectures to use in this compilation. |
| 2304 | SmallVector<CudaArch, 4> GpuArchList; |
| 2305 | |
| 2306 | /// The CUDA actions for the current input. |
| 2307 | ActionList CudaDeviceActions; |
| 2308 | |
| 2309 | /// The CUDA fat binary if it was generated for the current input. |
| 2310 | Action *CudaFatBinary = nullptr; |
| 2311 | |
| 2312 | /// Flag that is set to true if this builder acted on the current input. |
| 2313 | bool IsActive = false; |
Yaxun Liu | ab85193 | 2019-03-05 16:07:56 +0000 | [diff] [blame] | 2314 | |
| 2315 | /// Flag for -fgpu-rdc. |
| 2316 | bool Relocatable = false; |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2317 | public: |
| 2318 | CudaActionBuilderBase(Compilation &C, DerivedArgList &Args, |
| 2319 | const Driver::InputList &Inputs, |
| 2320 | Action::OffloadKind OFKind) |
| 2321 | : DeviceActionBuilder(C, Args, Inputs, OFKind) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2322 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2323 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2324 | // While generating code for CUDA, we only depend on the host input action |
| 2325 | // to trigger the creation of all the CUDA device actions. |
| 2326 | |
| 2327 | // If we are dealing with an input action, replicate it for each GPU |
| 2328 | // architecture. If we are in host-only mode we return 'success' so that |
| 2329 | // the host uses the CUDA offload kind. |
| 2330 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2331 | assert(!GpuArchList.empty() && |
| 2332 | "We should have at least one GPU architecture."); |
| 2333 | |
| 2334 | // If the host input is not CUDA or HIP, we don't need to bother about |
| 2335 | // this input. |
| 2336 | if (IA->getType() != types::TY_CUDA && |
| 2337 | IA->getType() != types::TY_HIP) { |
| 2338 | // The builder will ignore this input. |
| 2339 | IsActive = false; |
| 2340 | return ABRT_Inactive; |
| 2341 | } |
| 2342 | |
| 2343 | // Set the flag to true, so that the builder acts on the current input. |
| 2344 | IsActive = true; |
| 2345 | |
| 2346 | if (CompileHostOnly) |
| 2347 | return ABRT_Success; |
| 2348 | |
| 2349 | // Replicate inputs for each GPU architecture. |
| 2350 | auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE |
| 2351 | : types::TY_CUDA_DEVICE; |
| 2352 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2353 | CudaDeviceActions.push_back( |
| 2354 | C.MakeAction<InputAction>(IA->getInputArg(), Ty)); |
| 2355 | } |
| 2356 | |
| 2357 | return ABRT_Success; |
| 2358 | } |
| 2359 | |
| 2360 | // If this is an unbundling action use it as is for each CUDA toolchain. |
| 2361 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
Yaxun Liu | ab85193 | 2019-03-05 16:07:56 +0000 | [diff] [blame] | 2362 | |
| 2363 | // If -fgpu-rdc is disabled, should not unbundle since there is no |
| 2364 | // device code to link. |
| 2365 | if (!Relocatable) |
| 2366 | return ABRT_Inactive; |
| 2367 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2368 | CudaDeviceActions.clear(); |
Aaron Enye Shi | bea57bb | 2019-01-04 19:05:41 +0000 | [diff] [blame] | 2369 | auto *IA = cast<InputAction>(UA->getInputs().back()); |
| 2370 | std::string FileName = IA->getInputArg().getAsString(Args); |
| 2371 | // Check if the type of the file is the same as the action. Do not |
| 2372 | // unbundle it if it is not. Do not unbundle .so files, for example, |
| 2373 | // which are not object files. |
| 2374 | if (IA->getType() == types::TY_Object && |
| 2375 | (!llvm::sys::path::has_extension(FileName) || |
| 2376 | types::lookupTypeForExtension( |
| 2377 | llvm::sys::path::extension(FileName).drop_front()) != |
| 2378 | types::TY_Object)) |
| 2379 | return ABRT_Inactive; |
| 2380 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2381 | for (auto Arch : GpuArchList) { |
| 2382 | CudaDeviceActions.push_back(UA); |
| 2383 | UA->registerDependentActionInfo(ToolChains[0], CudaArchToString(Arch), |
| 2384 | AssociatedOffloadKind); |
| 2385 | } |
| 2386 | return ABRT_Success; |
| 2387 | } |
| 2388 | |
| 2389 | return IsActive ? ABRT_Success : ABRT_Inactive; |
| 2390 | } |
| 2391 | |
| 2392 | void appendTopLevelActions(ActionList &AL) override { |
| 2393 | // Utility to append actions to the top level list. |
| 2394 | auto AddTopLevel = [&](Action *A, CudaArch BoundArch) { |
| 2395 | OffloadAction::DeviceDependences Dep; |
| 2396 | Dep.add(*A, *ToolChains.front(), CudaArchToString(BoundArch), |
| 2397 | AssociatedOffloadKind); |
| 2398 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2399 | }; |
| 2400 | |
| 2401 | // If we have a fat binary, add it to the list. |
| 2402 | if (CudaFatBinary) { |
| 2403 | AddTopLevel(CudaFatBinary, CudaArch::UNKNOWN); |
| 2404 | CudaDeviceActions.clear(); |
| 2405 | CudaFatBinary = nullptr; |
| 2406 | return; |
| 2407 | } |
| 2408 | |
| 2409 | if (CudaDeviceActions.empty()) |
| 2410 | return; |
| 2411 | |
| 2412 | // If we have CUDA actions at this point, that's because we have a have |
| 2413 | // partial compilation, so we should have an action for each GPU |
| 2414 | // architecture. |
| 2415 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2416 | "Expecting one action per GPU architecture."); |
| 2417 | assert(ToolChains.size() == 1 && |
| 2418 | "Expecting to have a sing CUDA toolchain."); |
| 2419 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) |
| 2420 | AddTopLevel(CudaDeviceActions[I], GpuArchList[I]); |
| 2421 | |
| 2422 | CudaDeviceActions.clear(); |
| 2423 | } |
| 2424 | |
| 2425 | bool initialize() override { |
| 2426 | assert(AssociatedOffloadKind == Action::OFK_Cuda || |
| 2427 | AssociatedOffloadKind == Action::OFK_HIP); |
| 2428 | |
| 2429 | // We don't need to support CUDA. |
| 2430 | if (AssociatedOffloadKind == Action::OFK_Cuda && |
| 2431 | !C.hasOffloadToolChain<Action::OFK_Cuda>()) |
| 2432 | return false; |
| 2433 | |
| 2434 | // We don't need to support HIP. |
| 2435 | if (AssociatedOffloadKind == Action::OFK_HIP && |
| 2436 | !C.hasOffloadToolChain<Action::OFK_HIP>()) |
| 2437 | return false; |
| 2438 | |
Yaxun Liu | ab85193 | 2019-03-05 16:07:56 +0000 | [diff] [blame] | 2439 | Relocatable = Args.hasFlag(options::OPT_fgpu_rdc, |
| 2440 | options::OPT_fno_gpu_rdc, /*Default=*/false); |
| 2441 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2442 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 2443 | assert(HostTC && "No toolchain for host compilation."); |
| 2444 | if (HostTC->getTriple().isNVPTX() || |
| 2445 | HostTC->getTriple().getArch() == llvm::Triple::amdgcn) { |
| 2446 | // We do not support targeting NVPTX/AMDGCN for host compilation. Throw |
| 2447 | // an error and abort pipeline construction early so we don't trip |
| 2448 | // asserts that assume device-side compilation. |
| 2449 | C.getDriver().Diag(diag::err_drv_cuda_host_arch) |
| 2450 | << HostTC->getTriple().getArchName(); |
| 2451 | return true; |
| 2452 | } |
| 2453 | |
| 2454 | ToolChains.push_back( |
| 2455 | AssociatedOffloadKind == Action::OFK_Cuda |
| 2456 | ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() |
| 2457 | : C.getSingleOffloadToolChain<Action::OFK_HIP>()); |
| 2458 | |
| 2459 | Arg *PartialCompilationArg = Args.getLastArg( |
| 2460 | options::OPT_cuda_host_only, options::OPT_cuda_device_only, |
| 2461 | options::OPT_cuda_compile_host_device); |
| 2462 | CompileHostOnly = PartialCompilationArg && |
| 2463 | PartialCompilationArg->getOption().matches( |
| 2464 | options::OPT_cuda_host_only); |
| 2465 | CompileDeviceOnly = PartialCompilationArg && |
| 2466 | PartialCompilationArg->getOption().matches( |
| 2467 | options::OPT_cuda_device_only); |
| 2468 | |
| 2469 | // Collect all cuda_gpu_arch parameters, removing duplicates. |
| 2470 | std::set<CudaArch> GpuArchs; |
| 2471 | bool Error = false; |
| 2472 | for (Arg *A : Args) { |
| 2473 | if (!(A->getOption().matches(options::OPT_cuda_gpu_arch_EQ) || |
| 2474 | A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ))) |
| 2475 | continue; |
| 2476 | A->claim(); |
| 2477 | |
| 2478 | const StringRef ArchStr = A->getValue(); |
| 2479 | if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ) && |
| 2480 | ArchStr == "all") { |
| 2481 | GpuArchs.clear(); |
| 2482 | continue; |
| 2483 | } |
| 2484 | CudaArch Arch = StringToCudaArch(ArchStr); |
| 2485 | if (Arch == CudaArch::UNKNOWN) { |
| 2486 | C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr; |
| 2487 | Error = true; |
| 2488 | } else if (A->getOption().matches(options::OPT_cuda_gpu_arch_EQ)) |
| 2489 | GpuArchs.insert(Arch); |
| 2490 | else if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ)) |
| 2491 | GpuArchs.erase(Arch); |
| 2492 | else |
| 2493 | llvm_unreachable("Unexpected option."); |
| 2494 | } |
| 2495 | |
| 2496 | // Collect list of GPUs remaining in the set. |
| 2497 | for (CudaArch Arch : GpuArchs) |
| 2498 | GpuArchList.push_back(Arch); |
| 2499 | |
| 2500 | // Default to sm_20 which is the lowest common denominator for |
| 2501 | // supported GPUs. sm_20 code should work correctly, if |
| 2502 | // suboptimally, on all newer GPUs. |
| 2503 | if (GpuArchList.empty()) |
| 2504 | GpuArchList.push_back(CudaArch::SM_20); |
| 2505 | |
| 2506 | return Error; |
| 2507 | } |
| 2508 | }; |
| 2509 | |
| 2510 | /// \brief CUDA action builder. It injects device code in the host backend |
| 2511 | /// action. |
| 2512 | class CudaActionBuilder final : public CudaActionBuilderBase { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2513 | public: |
| 2514 | CudaActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2515 | const Driver::InputList &Inputs) |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2516 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2517 | |
| 2518 | ActionBuilderReturnCode |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2519 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2520 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2521 | PhasesTy &Phases) override { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2522 | if (!IsActive) |
| 2523 | return ABRT_Inactive; |
| 2524 | |
| 2525 | // If we don't have more CUDA actions, we don't have any dependences to |
| 2526 | // create for the host. |
| 2527 | if (CudaDeviceActions.empty()) |
| 2528 | return ABRT_Success; |
| 2529 | |
| 2530 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2531 | "Expecting one action per GPU architecture."); |
| 2532 | assert(!CompileHostOnly && |
| 2533 | "Not expecting CUDA actions in host-only compilation."); |
| 2534 | |
| 2535 | // If we are generating code for the device or we are in a backend phase, |
| 2536 | // we attempt to generate the fat binary. We compile each arch to ptx and |
| 2537 | // assemble to cubin, then feed the cubin *and* the ptx into a device |
| 2538 | // "link" action, which uses fatbinary to combine these cubins into one |
| 2539 | // fatbin. The fatbin is then an input to the host action if not in |
| 2540 | // device-only mode. |
| 2541 | if (CompileDeviceOnly || CurPhase == phases::Backend) { |
| 2542 | ActionList DeviceActions; |
| 2543 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2544 | // Produce the device action from the current phase up to the assemble |
| 2545 | // phase. |
| 2546 | for (auto Ph : Phases) { |
| 2547 | // Skip the phases that were already dealt with. |
| 2548 | if (Ph < CurPhase) |
| 2549 | continue; |
| 2550 | // We have to be consistent with the host final phase. |
| 2551 | if (Ph > FinalPhase) |
| 2552 | break; |
| 2553 | |
| 2554 | CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction( |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 2555 | C, Args, Ph, CudaDeviceActions[I], Action::OFK_Cuda); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2556 | |
| 2557 | if (Ph == phases::Assemble) |
| 2558 | break; |
| 2559 | } |
| 2560 | |
| 2561 | // If we didn't reach the assemble phase, we can't generate the fat |
| 2562 | // binary. We don't need to generate the fat binary if we are not in |
| 2563 | // device-only mode. |
| 2564 | if (!isa<AssembleJobAction>(CudaDeviceActions[I]) || |
| 2565 | CompileDeviceOnly) |
| 2566 | continue; |
| 2567 | |
| 2568 | Action *AssembleAction = CudaDeviceActions[I]; |
| 2569 | assert(AssembleAction->getType() == types::TY_Object); |
| 2570 | assert(AssembleAction->getInputs().size() == 1); |
| 2571 | |
| 2572 | Action *BackendAction = AssembleAction->getInputs()[0]; |
| 2573 | assert(BackendAction->getType() == types::TY_PP_Asm); |
| 2574 | |
| 2575 | for (auto &A : {AssembleAction, BackendAction}) { |
| 2576 | OffloadAction::DeviceDependences DDep; |
| 2577 | DDep.add(*A, *ToolChains.front(), CudaArchToString(GpuArchList[I]), |
| 2578 | Action::OFK_Cuda); |
| 2579 | DeviceActions.push_back( |
| 2580 | C.MakeAction<OffloadAction>(DDep, A->getType())); |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | // We generate the fat binary if we have device input actions. |
| 2585 | if (!DeviceActions.empty()) { |
| 2586 | CudaFatBinary = |
| 2587 | C.MakeAction<LinkJobAction>(DeviceActions, types::TY_CUDA_FATBIN); |
| 2588 | |
| 2589 | if (!CompileDeviceOnly) { |
| 2590 | DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr, |
| 2591 | Action::OFK_Cuda); |
| 2592 | // Clear the fat binary, it is already a dependence to an host |
| 2593 | // action. |
| 2594 | CudaFatBinary = nullptr; |
| 2595 | } |
| 2596 | |
| 2597 | // Remove the CUDA actions as they are already connected to an host |
| 2598 | // action or fat binary. |
| 2599 | CudaDeviceActions.clear(); |
| 2600 | } |
| 2601 | |
| 2602 | // We avoid creating host action in device-only mode. |
| 2603 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
Samuel Antao | 5b1a89a | 2016-10-27 00:53:34 +0000 | [diff] [blame] | 2604 | } else if (CurPhase > phases::Backend) { |
| 2605 | // If we are past the backend phase and still have a device action, we |
| 2606 | // don't have to do anything as this action is already a device |
| 2607 | // top-level action. |
| 2608 | return ABRT_Success; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | assert(CurPhase < phases::Backend && "Generating single CUDA " |
| 2612 | "instructions should only occur " |
| 2613 | "before the backend phase!"); |
| 2614 | |
| 2615 | // By default, we produce an action for each device arch. |
| 2616 | for (Action *&A : CudaDeviceActions) |
| 2617 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2618 | |
| 2619 | return ABRT_Success; |
| 2620 | } |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2621 | }; |
| 2622 | /// \brief HIP action builder. It injects device code in the host backend |
| 2623 | /// action. |
| 2624 | class HIPActionBuilder final : public CudaActionBuilderBase { |
| 2625 | /// The linker inputs obtained for each device arch. |
| 2626 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2627 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2628 | public: |
| 2629 | HIPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2630 | const Driver::InputList &Inputs) |
Yaxun Liu | ab85193 | 2019-03-05 16:07:56 +0000 | [diff] [blame] | 2631 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) {} |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2632 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2633 | bool canUseBundlerUnbundler() const override { return true; } |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2634 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2635 | ActionBuilderReturnCode |
| 2636 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2637 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2638 | PhasesTy &Phases) override { |
| 2639 | // amdgcn does not support linking of object files, therefore we skip |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2640 | // backend and assemble phases to output LLVM IR. Except for generating |
| 2641 | // non-relocatable device coee, where we generate fat binary for device |
| 2642 | // code and pass to host in Backend phase. |
| 2643 | if (CudaDeviceActions.empty() || |
| 2644 | (CurPhase == phases::Backend && Relocatable) || |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2645 | CurPhase == phases::Assemble) |
| 2646 | return ABRT_Success; |
| 2647 | |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2648 | assert(((CurPhase == phases::Link && Relocatable) || |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2649 | CudaDeviceActions.size() == GpuArchList.size()) && |
| 2650 | "Expecting one action per GPU architecture."); |
| 2651 | assert(!CompileHostOnly && |
| 2652 | "Not expecting CUDA actions in host-only compilation."); |
| 2653 | |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2654 | if (!Relocatable && CurPhase == phases::Backend) { |
| 2655 | // If we are in backend phase, we attempt to generate the fat binary. |
| 2656 | // We compile each arch to IR and use a link action to generate code |
| 2657 | // object containing ISA. Then we use a special "link" action to create |
| 2658 | // a fat binary containing all the code objects for different GPU's. |
| 2659 | // The fat binary is then an input to the host action. |
| 2660 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2661 | // Create a link action to link device IR with device library |
| 2662 | // and generate ISA. |
| 2663 | ActionList AL; |
| 2664 | AL.push_back(CudaDeviceActions[I]); |
| 2665 | CudaDeviceActions[I] = |
| 2666 | C.MakeAction<LinkJobAction>(AL, types::TY_Image); |
| 2667 | |
| 2668 | // OffloadingActionBuilder propagates device arch until an offload |
| 2669 | // action. Since the next action for creating fatbin does |
| 2670 | // not have device arch, whereas the above link action and its input |
| 2671 | // have device arch, an offload action is needed to stop the null |
| 2672 | // device arch of the next action being propagated to the above link |
| 2673 | // action. |
| 2674 | OffloadAction::DeviceDependences DDep; |
| 2675 | DDep.add(*CudaDeviceActions[I], *ToolChains.front(), |
| 2676 | CudaArchToString(GpuArchList[I]), AssociatedOffloadKind); |
| 2677 | CudaDeviceActions[I] = C.MakeAction<OffloadAction>( |
| 2678 | DDep, CudaDeviceActions[I]->getType()); |
| 2679 | } |
| 2680 | // Create HIP fat binary with a special "link" action. |
| 2681 | CudaFatBinary = |
| 2682 | C.MakeAction<LinkJobAction>(CudaDeviceActions, |
| 2683 | types::TY_HIP_FATBIN); |
| 2684 | |
Yaxun Liu | 2bcc951 | 2018-11-14 04:47:31 +0000 | [diff] [blame] | 2685 | if (!CompileDeviceOnly) { |
| 2686 | DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr, |
| 2687 | AssociatedOffloadKind); |
| 2688 | // Clear the fat binary, it is already a dependence to an host |
| 2689 | // action. |
| 2690 | CudaFatBinary = nullptr; |
| 2691 | } |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2692 | |
| 2693 | // Remove the CUDA actions as they are already connected to an host |
| 2694 | // action or fat binary. |
| 2695 | CudaDeviceActions.clear(); |
| 2696 | |
Yaxun Liu | 2bcc951 | 2018-11-14 04:47:31 +0000 | [diff] [blame] | 2697 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2698 | } else if (CurPhase == phases::Link) { |
| 2699 | // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch. |
| 2700 | // This happens to each device action originated from each input file. |
| 2701 | // Later on, device actions in DeviceLinkerInputs are used to create |
| 2702 | // device link actions in appendLinkDependences and the created device |
| 2703 | // link actions are passed to the offload action as device dependence. |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2704 | DeviceLinkerInputs.resize(CudaDeviceActions.size()); |
| 2705 | auto LI = DeviceLinkerInputs.begin(); |
| 2706 | for (auto *A : CudaDeviceActions) { |
| 2707 | LI->push_back(A); |
| 2708 | ++LI; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2711 | // We will pass the device action as a host dependence, so we don't |
| 2712 | // need to do anything else with them. |
| 2713 | CudaDeviceActions.clear(); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2714 | return ABRT_Success; |
| 2715 | } |
| 2716 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2717 | // By default, we produce an action for each device arch. |
| 2718 | for (Action *&A : CudaDeviceActions) |
| 2719 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A, |
| 2720 | AssociatedOffloadKind); |
| 2721 | |
| 2722 | return ABRT_Success; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2725 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2726 | // Append a new link action for each device. |
| 2727 | unsigned I = 0; |
| 2728 | for (auto &LI : DeviceLinkerInputs) { |
| 2729 | auto *DeviceLinkAction = |
| 2730 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2731 | DA.add(*DeviceLinkAction, *ToolChains[0], |
| 2732 | CudaArchToString(GpuArchList[I]), AssociatedOffloadKind); |
| 2733 | ++I; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2734 | } |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2735 | } |
| 2736 | }; |
| 2737 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2738 | /// OpenMP action builder. The host bitcode is passed to the device frontend |
| 2739 | /// and all the device linked images are passed to the host link phase. |
| 2740 | class OpenMPActionBuilder final : public DeviceActionBuilder { |
| 2741 | /// The OpenMP actions for the current input. |
| 2742 | ActionList OpenMPDeviceActions; |
| 2743 | |
| 2744 | /// The linker inputs obtained for each toolchain. |
| 2745 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
| 2746 | |
| 2747 | public: |
| 2748 | OpenMPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2749 | const Driver::InputList &Inputs) |
| 2750 | : DeviceActionBuilder(C, Args, Inputs, Action::OFK_OpenMP) {} |
| 2751 | |
| 2752 | ActionBuilderReturnCode |
| 2753 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2754 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2755 | PhasesTy &Phases) override { |
Alexey Bataev | a5178f5 | 2018-09-28 16:17:59 +0000 | [diff] [blame] | 2756 | if (OpenMPDeviceActions.empty()) |
| 2757 | return ABRT_Inactive; |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2758 | |
| 2759 | // We should always have an action for each input. |
| 2760 | assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2761 | "Number of OpenMP actions and toolchains do not match."); |
| 2762 | |
| 2763 | // The host only depends on device action in the linking phase, when all |
| 2764 | // the device images have to be embedded in the host image. |
| 2765 | if (CurPhase == phases::Link) { |
| 2766 | assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2767 | "Toolchains and linker inputs sizes do not match."); |
| 2768 | auto LI = DeviceLinkerInputs.begin(); |
| 2769 | for (auto *A : OpenMPDeviceActions) { |
| 2770 | LI->push_back(A); |
| 2771 | ++LI; |
| 2772 | } |
| 2773 | |
| 2774 | // We passed the device action as a host dependence, so we don't need to |
| 2775 | // do anything else with them. |
| 2776 | OpenMPDeviceActions.clear(); |
| 2777 | return ABRT_Success; |
| 2778 | } |
| 2779 | |
| 2780 | // By default, we produce an action for each device arch. |
| 2781 | for (Action *&A : OpenMPDeviceActions) |
| 2782 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2783 | |
| 2784 | return ABRT_Success; |
| 2785 | } |
| 2786 | |
| 2787 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2788 | |
| 2789 | // If this is an input action replicate it for each OpenMP toolchain. |
| 2790 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2791 | OpenMPDeviceActions.clear(); |
| 2792 | for (unsigned I = 0; I < ToolChains.size(); ++I) |
| 2793 | OpenMPDeviceActions.push_back( |
| 2794 | C.MakeAction<InputAction>(IA->getInputArg(), IA->getType())); |
| 2795 | return ABRT_Success; |
| 2796 | } |
| 2797 | |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2798 | // If this is an unbundling action use it as is for each OpenMP toolchain. |
| 2799 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
| 2800 | OpenMPDeviceActions.clear(); |
Alexey Bataev | a5178f5 | 2018-09-28 16:17:59 +0000 | [diff] [blame] | 2801 | auto *IA = cast<InputAction>(UA->getInputs().back()); |
| 2802 | std::string FileName = IA->getInputArg().getAsString(Args); |
| 2803 | // Check if the type of the file is the same as the action. Do not |
| 2804 | // unbundle it if it is not. Do not unbundle .so files, for example, |
| 2805 | // which are not object files. |
| 2806 | if (IA->getType() == types::TY_Object && |
| 2807 | (!llvm::sys::path::has_extension(FileName) || |
| 2808 | types::lookupTypeForExtension( |
| 2809 | llvm::sys::path::extension(FileName).drop_front()) != |
| 2810 | types::TY_Object)) |
| 2811 | return ABRT_Inactive; |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 2812 | for (unsigned I = 0; I < ToolChains.size(); ++I) { |
| 2813 | OpenMPDeviceActions.push_back(UA); |
| 2814 | UA->registerDependentActionInfo( |
| 2815 | ToolChains[I], /*BoundArch=*/StringRef(), Action::OFK_OpenMP); |
| 2816 | } |
| 2817 | return ABRT_Success; |
| 2818 | } |
| 2819 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2820 | // When generating code for OpenMP we use the host compile phase result as |
| 2821 | // a dependence to the device compile phase so that it can learn what |
| 2822 | // declarations should be emitted. However, this is not the only use for |
| 2823 | // the host action, so we prevent it from being collapsed. |
| 2824 | if (isa<CompileJobAction>(HostAction)) { |
| 2825 | HostAction->setCannotBeCollapsedWithNextDependentAction(); |
| 2826 | assert(ToolChains.size() == OpenMPDeviceActions.size() && |
| 2827 | "Toolchains and device action sizes do not match."); |
| 2828 | OffloadAction::HostDependence HDep( |
| 2829 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2830 | /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2831 | auto TC = ToolChains.begin(); |
| 2832 | for (Action *&A : OpenMPDeviceActions) { |
| 2833 | assert(isa<CompileJobAction>(A)); |
| 2834 | OffloadAction::DeviceDependences DDep; |
| 2835 | DDep.add(*A, **TC, /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2836 | A = C.MakeAction<OffloadAction>(HDep, DDep); |
| 2837 | ++TC; |
| 2838 | } |
| 2839 | } |
| 2840 | return ABRT_Success; |
| 2841 | } |
| 2842 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2843 | void appendTopLevelActions(ActionList &AL) override { |
| 2844 | if (OpenMPDeviceActions.empty()) |
| 2845 | return; |
| 2846 | |
| 2847 | // We should always have an action for each input. |
| 2848 | assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2849 | "Number of OpenMP actions and toolchains do not match."); |
| 2850 | |
| 2851 | // Append all device actions followed by the proper offload action. |
| 2852 | auto TI = ToolChains.begin(); |
| 2853 | for (auto *A : OpenMPDeviceActions) { |
| 2854 | OffloadAction::DeviceDependences Dep; |
| 2855 | Dep.add(*A, **TI, /*BoundArch=*/nullptr, Action::OFK_OpenMP); |
| 2856 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2857 | ++TI; |
| 2858 | } |
| 2859 | // We no longer need the action stored in this builder. |
| 2860 | OpenMPDeviceActions.clear(); |
| 2861 | } |
| 2862 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2863 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2864 | assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2865 | "Toolchains and linker inputs sizes do not match."); |
| 2866 | |
| 2867 | // Append a new link action for each device. |
| 2868 | auto TC = ToolChains.begin(); |
| 2869 | for (auto &LI : DeviceLinkerInputs) { |
| 2870 | auto *DeviceLinkAction = |
| 2871 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2872 | DA.add(*DeviceLinkAction, **TC, /*BoundArch=*/nullptr, |
| 2873 | Action::OFK_OpenMP); |
| 2874 | ++TC; |
| 2875 | } |
| 2876 | } |
| 2877 | |
| 2878 | bool initialize() override { |
| 2879 | // Get the OpenMP toolchains. If we don't get any, the action builder will |
| 2880 | // know there is nothing to do related to OpenMP offloading. |
| 2881 | auto OpenMPTCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); |
| 2882 | for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE; |
| 2883 | ++TI) |
| 2884 | ToolChains.push_back(TI->second); |
| 2885 | |
| 2886 | DeviceLinkerInputs.resize(ToolChains.size()); |
| 2887 | return false; |
| 2888 | } |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2889 | |
| 2890 | bool canUseBundlerUnbundler() const override { |
| 2891 | // OpenMP should use bundled files whenever possible. |
| 2892 | return true; |
| 2893 | } |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2894 | }; |
| 2895 | |
| 2896 | /// |
| 2897 | /// TODO: Add the implementation for other specialized builders here. |
| 2898 | /// |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2899 | |
| 2900 | /// Specialized builders being used by this offloading action builder. |
| 2901 | SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders; |
| 2902 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2903 | /// Flag set to true if all valid builders allow file bundling/unbundling. |
| 2904 | bool CanUseBundler; |
| 2905 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2906 | public: |
| 2907 | OffloadingActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2908 | const Driver::InputList &Inputs) |
Samuel Antao | 10e905c | 2016-10-27 01:08:58 +0000 | [diff] [blame] | 2909 | : C(C) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2910 | // Create a specialized builder for each device toolchain. |
| 2911 | |
| 2912 | IsValid = true; |
| 2913 | |
| 2914 | // Create a specialized builder for CUDA. |
| 2915 | SpecializedBuilders.push_back(new CudaActionBuilder(C, Args, Inputs)); |
| 2916 | |
Yaxun Liu | 3af038b | 2018-05-30 00:49:10 +0000 | [diff] [blame] | 2917 | // Create a specialized builder for HIP. |
| 2918 | SpecializedBuilders.push_back(new HIPActionBuilder(C, Args, Inputs)); |
| 2919 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2920 | // Create a specialized builder for OpenMP. |
| 2921 | SpecializedBuilders.push_back(new OpenMPActionBuilder(C, Args, Inputs)); |
| 2922 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2923 | // |
| 2924 | // TODO: Build other specialized builders here. |
| 2925 | // |
| 2926 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2927 | // Initialize all the builders, keeping track of errors. If all valid |
| 2928 | // builders agree that we can use bundling, set the flag to true. |
| 2929 | unsigned ValidBuilders = 0u; |
| 2930 | unsigned ValidBuildersSupportingBundling = 0u; |
| 2931 | for (auto *SB : SpecializedBuilders) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2932 | IsValid = IsValid && !SB->initialize(); |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 2933 | |
| 2934 | // Update the counters if the builder is valid. |
| 2935 | if (SB->isValid()) { |
| 2936 | ++ValidBuilders; |
| 2937 | if (SB->canUseBundlerUnbundler()) |
| 2938 | ++ValidBuildersSupportingBundling; |
| 2939 | } |
| 2940 | } |
| 2941 | CanUseBundler = |
| 2942 | ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling; |
Artem Belevich | f981e30b | 2016-08-02 22:37:47 +0000 | [diff] [blame] | 2943 | } |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 2944 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2945 | ~OffloadingActionBuilder() { |
| 2946 | for (auto *SB : SpecializedBuilders) |
| 2947 | delete SB; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 2948 | } |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 2949 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2950 | /// Generate an action that adds device dependences (if any) to a host action. |
| 2951 | /// If no device dependence actions exist, just return the host action \a |
| 2952 | /// HostAction. If an error is found or if no builder requires the host action |
| 2953 | /// to be generated, return nullptr. |
| 2954 | Action * |
| 2955 | addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg, |
| 2956 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2957 | DeviceActionBuilder::PhasesTy &Phases) { |
| 2958 | if (!IsValid) |
| 2959 | return nullptr; |
Justin Lebar | 7bf7798 | 2016-01-11 23:27:13 +0000 | [diff] [blame] | 2960 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2961 | if (SpecializedBuilders.empty()) |
| 2962 | return HostAction; |
| 2963 | |
| 2964 | assert(HostAction && "Invalid host action!"); |
| 2965 | |
| 2966 | OffloadAction::DeviceDependences DDeps; |
| 2967 | // Check if all the programming models agree we should not emit the host |
| 2968 | // action. Also, keep track of the offloading kinds employed. |
| 2969 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 2970 | unsigned InactiveBuilders = 0u; |
| 2971 | unsigned IgnoringBuilders = 0u; |
| 2972 | for (auto *SB : SpecializedBuilders) { |
| 2973 | if (!SB->isValid()) { |
| 2974 | ++InactiveBuilders; |
| 2975 | continue; |
| 2976 | } |
| 2977 | |
Samuel Antao | 28c4f18 | 2016-10-27 17:08:03 +0000 | [diff] [blame] | 2978 | auto RetCode = |
| 2979 | SB->getDeviceDependences(DDeps, CurPhase, FinalPhase, Phases); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 2980 | |
| 2981 | // If the builder explicitly says the host action should be ignored, |
| 2982 | // we need to increment the variable that tracks the builders that request |
| 2983 | // the host object to be ignored. |
| 2984 | if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host) |
| 2985 | ++IgnoringBuilders; |
| 2986 | |
| 2987 | // Unless the builder was inactive for this action, we have to record the |
| 2988 | // offload kind because the host will have to use it. |
| 2989 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 2990 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 2991 | } |
| 2992 | |
| 2993 | // If all builders agree that the host object should be ignored, just return |
| 2994 | // nullptr. |
| 2995 | if (IgnoringBuilders && |
| 2996 | SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders)) |
| 2997 | return nullptr; |
| 2998 | |
| 2999 | if (DDeps.getActions().empty()) |
| 3000 | return HostAction; |
| 3001 | |
| 3002 | // We have dependences we need to bundle together. We use an offload action |
| 3003 | // for that. |
| 3004 | OffloadAction::HostDependence HDep( |
| 3005 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 3006 | /*BoundArch=*/nullptr, DDeps); |
| 3007 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3010 | /// Generate an action that adds a host dependence to a device action. The |
| 3011 | /// results will be kept in this action builder. Return true if an error was |
| 3012 | /// found. |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 3013 | bool addHostDependenceToDeviceActions(Action *&HostAction, |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3014 | const Arg *InputArg) { |
| 3015 | if (!IsValid) |
| 3016 | return true; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3017 | |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 3018 | // If we are supporting bundling/unbundling and the current action is an |
| 3019 | // input action of non-source file, we replace the host action by the |
| 3020 | // unbundling action. The bundler tool has the logic to detect if an input |
| 3021 | // is a bundle or not and if the input is not a bundle it assumes it is a |
| 3022 | // host file. Therefore it is safe to create an unbundling action even if |
| 3023 | // the input is not a bundle. |
| 3024 | if (CanUseBundler && isa<InputAction>(HostAction) && |
| 3025 | InputArg->getOption().getKind() == llvm::opt::Option::InputClass && |
| 3026 | !types::isSrcFile(HostAction->getType())) { |
| 3027 | auto UnbundlingHostAction = |
| 3028 | C.MakeAction<OffloadUnbundlingJobAction>(HostAction); |
| 3029 | UnbundlingHostAction->registerDependentActionInfo( |
| 3030 | C.getSingleOffloadToolChain<Action::OFK_Host>(), |
Yaxun Liu | f37b50a | 2018-07-24 01:03:44 +0000 | [diff] [blame] | 3031 | /*BoundArch=*/StringRef(), Action::OFK_Host); |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 3032 | HostAction = UnbundlingHostAction; |
| 3033 | } |
| 3034 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3035 | assert(HostAction && "Invalid host action!"); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3036 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3037 | // Register the offload kinds that are used. |
| 3038 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 3039 | for (auto *SB : SpecializedBuilders) { |
| 3040 | if (!SB->isValid()) |
| 3041 | continue; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3042 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3043 | auto RetCode = SB->addDeviceDepences(HostAction); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3044 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3045 | // Host dependences for device actions are not compatible with that same |
| 3046 | // action being ignored. |
| 3047 | assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host && |
| 3048 | "Host dependence not expected to be ignored.!"); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3049 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3050 | // Unless the builder was inactive for this action, we have to record the |
| 3051 | // offload kind because the host will have to use it. |
| 3052 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 3053 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 3054 | } |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3055 | |
Alexey Bataev | a5178f5 | 2018-09-28 16:17:59 +0000 | [diff] [blame] | 3056 | // Do not use unbundler if the Host does not depend on device action. |
| 3057 | if (OffloadKind == Action::OFK_None && CanUseBundler) |
| 3058 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) |
| 3059 | HostAction = UA->getInputs().back(); |
| 3060 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3061 | return false; |
| 3062 | } |
| 3063 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3064 | /// Add the offloading top level actions to the provided action list. This |
| 3065 | /// function can replace the host action by a bundling action if the |
| 3066 | /// programming models allow it. |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3067 | bool appendTopLevelActions(ActionList &AL, Action *HostAction, |
| 3068 | const Arg *InputArg) { |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3069 | // Get the device actions to be appended. |
| 3070 | ActionList OffloadAL; |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3071 | for (auto *SB : SpecializedBuilders) { |
| 3072 | if (!SB->isValid()) |
| 3073 | continue; |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3074 | SB->appendTopLevelActions(OffloadAL); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3077 | // 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] | 3078 | // the resulting list. Otherwise, just append the device actions. For |
| 3079 | // device only compilation, HostAction is a null pointer, therefore only do |
| 3080 | // this when HostAction is not a null pointer. |
| 3081 | if (CanUseBundler && HostAction && !OffloadAL.empty()) { |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 3082 | // Add the host action to the list in order to create the bundling action. |
| 3083 | OffloadAL.push_back(HostAction); |
| 3084 | |
| 3085 | // We expect that the host action was just appended to the action list |
| 3086 | // before this method was called. |
| 3087 | assert(HostAction == AL.back() && "Host action not in the list??"); |
| 3088 | HostAction = C.MakeAction<OffloadBundlingJobAction>(OffloadAL); |
| 3089 | AL.back() = HostAction; |
| 3090 | } else |
| 3091 | AL.append(OffloadAL.begin(), OffloadAL.end()); |
| 3092 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3093 | // Propagate to the current host action (if any) the offload information |
| 3094 | // associated with the current input. |
| 3095 | if (HostAction) |
| 3096 | HostAction->propagateHostOffloadInfo(InputArgToOffloadKindMap[InputArg], |
| 3097 | /*BoundArch=*/nullptr); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3098 | return false; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3099 | } |
Artem Belevich | 5bde4e0 | 2015-07-20 20:02:54 +0000 | [diff] [blame] | 3100 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3101 | /// Processes the host linker action. This currently consists of replacing it |
| 3102 | /// with an offload action if there are device link objects and propagate to |
| 3103 | /// the host action all the offload kinds used in the current compilation. The |
| 3104 | /// resulting action is returned. |
| 3105 | Action *processHostLinkAction(Action *HostAction) { |
| 3106 | // Add all the dependences from the device linking actions. |
| 3107 | OffloadAction::DeviceDependences DDeps; |
| 3108 | for (auto *SB : SpecializedBuilders) { |
| 3109 | if (!SB->isValid()) |
| 3110 | continue; |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 3111 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3112 | SB->appendLinkDependences(DDeps); |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 3113 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3114 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3115 | // Calculate all the offload kinds used in the current compilation. |
| 3116 | unsigned ActiveOffloadKinds = 0u; |
| 3117 | for (auto &I : InputArgToOffloadKindMap) |
| 3118 | ActiveOffloadKinds |= I.second; |
| 3119 | |
| 3120 | // If we don't have device dependencies, we don't have to create an offload |
| 3121 | // action. |
| 3122 | if (DDeps.getActions().empty()) { |
| 3123 | // Propagate all the active kinds to host action. Given that it is a link |
| 3124 | // action it is assumed to depend on all actions generated so far. |
| 3125 | HostAction->propagateHostOffloadInfo(ActiveOffloadKinds, |
| 3126 | /*BoundArch=*/nullptr); |
| 3127 | return HostAction; |
| 3128 | } |
| 3129 | |
| 3130 | // Create the offload action with all dependences. When an offload action |
| 3131 | // 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] | 3132 | // to do that explicitly here. |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3133 | OffloadAction::HostDependence HDep( |
| 3134 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 3135 | /*BoundArch*/ nullptr, ActiveOffloadKinds); |
| 3136 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
| 3137 | } |
| 3138 | }; |
| 3139 | } // anonymous namespace. |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3140 | |
Justin Lebar | 0f3474c | 2016-02-11 02:00:50 +0000 | [diff] [blame] | 3141 | void Driver::BuildActions(Compilation &C, DerivedArgList &Args, |
| 3142 | const InputList &Inputs, ActionList &Actions) const { |
Chad Rosier | ecdede8 | 2011-08-12 22:08:57 +0000 | [diff] [blame] | 3143 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); |
Joerg Sonnenberger | b86f5f4 | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 3144 | |
Daniel Dunbar | 34c4187 | 2009-03-13 00:17:48 +0000 | [diff] [blame] | 3145 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 3146 | Diag(clang::diag::err_drv_no_input_files); |
| 3147 | return; |
| 3148 | } |
| 3149 | |
Chad Rosier | 7742b5d | 2011-07-27 23:36:45 +0000 | [diff] [blame] | 3150 | Arg *FinalPhaseArg; |
| 3151 | phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg); |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 3152 | |
Bob Haarman | aaf5191 | 2017-02-27 19:40:19 +0000 | [diff] [blame] | 3153 | if (FinalPhase == phases::Link) { |
| 3154 | if (Args.hasArg(options::OPT_emit_llvm)) |
| 3155 | Diag(clang::diag::err_drv_emit_llvm_link); |
| 3156 | if (IsCLMode() && LTOMode != LTOK_None && |
| 3157 | !Args.getLastArgValue(options::OPT_fuse_ld_EQ).equals_lower("lld")) |
| 3158 | Diag(clang::diag::err_drv_lto_without_lld); |
Rafael Espindola | e802564 | 2013-08-25 14:27:09 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3161 | // Reject -Z* at the top level, these options should never have been exposed |
| 3162 | // by gcc. |
Daniel Dunbar | dd76524 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 3163 | if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) |
Daniel Dunbar | 0e75994 | 2009-03-20 06:14:23 +0000 | [diff] [blame] | 3164 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); |
Daniel Dunbar | bfeec74 | 2009-03-12 23:55:14 +0000 | [diff] [blame] | 3165 | |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3166 | // Diagnose misuse of /Fo. |
| 3167 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) { |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3168 | StringRef V = A->getValue(); |
Hans Wennborg | 6164753 | 2014-11-17 19:16:36 +0000 | [diff] [blame] | 3169 | if (Inputs.size() > 1 && !V.empty() && |
| 3170 | !llvm::sys::path::is_separator(V.back())) { |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3171 | // Check whether /Fo tries to name an output file for multiple inputs. |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3172 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3173 | << A->getSpelling() << V; |
Hans Wennborg | 13b7fe7 | 2013-08-12 23:26:25 +0000 | [diff] [blame] | 3174 | Args.eraseArg(options::OPT__SLASH_Fo); |
| 3175 | } |
| 3176 | } |
| 3177 | |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3178 | // Diagnose misuse of /Fa. |
| 3179 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) { |
| 3180 | StringRef V = A->getValue(); |
Hans Wennborg | 6164753 | 2014-11-17 19:16:36 +0000 | [diff] [blame] | 3181 | if (Inputs.size() > 1 && !V.empty() && |
| 3182 | !llvm::sys::path::is_separator(V.back())) { |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3183 | // Check whether /Fa tries to name an asm file for multiple inputs. |
| 3184 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3185 | << A->getSpelling() << V; |
Hans Wennborg | 9c1659b | 2013-10-18 22:49:04 +0000 | [diff] [blame] | 3186 | Args.eraseArg(options::OPT__SLASH_Fa); |
| 3187 | } |
| 3188 | } |
| 3189 | |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 3190 | // Diagnose misuse of /o. |
| 3191 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) { |
| 3192 | if (A->getValue()[0] == '\0') { |
| 3193 | // It has to have a value. |
| 3194 | Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1; |
| 3195 | Args.eraseArg(options::OPT__SLASH_o); |
| 3196 | } |
| 3197 | } |
| 3198 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 3199 | // 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] | 3200 | Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); |
| 3201 | Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3202 | if (YcArg && YuArg && strcmp(YcArg->getValue(), YuArg->getValue()) != 0) { |
| 3203 | Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl); |
| 3204 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3205 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 3206 | YcArg = YuArg = nullptr; |
| 3207 | } |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3208 | if (YcArg && Inputs.size() > 1) { |
| 3209 | Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl); |
| 3210 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3211 | YcArg = nullptr; |
| 3212 | } |
Erich Keane | eaca388 | 2018-08-17 13:43:39 +0000 | [diff] [blame] | 3213 | if (FinalPhase == phases::Preprocess || Args.hasArg(options::OPT__SLASH_Y_)) { |
| 3214 | // If only preprocessing or /Y- is used, all pch handling is disabled. |
| 3215 | // Rather than check for it everywhere, just remove clang-cl pch-related |
| 3216 | // flags here. |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3217 | Args.eraseArg(options::OPT__SLASH_Fp); |
| 3218 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3219 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 3220 | YcArg = YuArg = nullptr; |
| 3221 | } |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 3222 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3223 | // Builder to be used to build offloading actions. |
| 3224 | OffloadingActionBuilder OffloadBuilder(C, Args, Inputs); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3225 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3226 | // Construct the actions to perform. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3227 | HeaderModulePrecompileJobAction *HeaderModuleAction = nullptr; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3228 | ActionList LinkerInputs; |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 3229 | |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3230 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL; |
Artem Belevich | 5bde4e0 | 2015-07-20 20:02:54 +0000 | [diff] [blame] | 3231 | for (auto &I : Inputs) { |
| 3232 | types::ID InputType = I.first; |
| 3233 | const Arg *InputArg = I.second; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3234 | |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3235 | PL.clear(); |
| 3236 | types::getCompilationPhases(InputType, PL); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3237 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3238 | // If the first step comes after the final phase we are doing as part of |
| 3239 | // this compilation, warn the user about it. |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3240 | phases::ID InitialPhase = PL[0]; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3241 | if (InitialPhase > FinalPhase) { |
Martin Storsjo | 665c7a4 | 2018-05-04 06:05:58 +0000 | [diff] [blame] | 3242 | if (InputArg->isClaimed()) |
| 3243 | continue; |
| 3244 | |
Daniel Dunbar | adc5c7c | 2009-03-19 07:57:08 +0000 | [diff] [blame] | 3245 | // Claim here to avoid the more general unused warning. |
| 3246 | InputArg->claim(); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3247 | |
Daniel Dunbar | 2db3e73 | 2011-04-20 15:44:48 +0000 | [diff] [blame] | 3248 | // Suppress all unused style warnings with -Qunused-arguments |
| 3249 | if (Args.hasArg(options::OPT_Qunused_arguments)) |
| 3250 | continue; |
| 3251 | |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3252 | // Special case when final phase determined by binary name, rather than |
| 3253 | // by a command-line argument with a corresponding Arg. |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 3254 | if (CCCIsCPP()) |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3255 | Diag(clang::diag::warn_drv_input_file_unused_by_cpp) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3256 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3257 | // Special case '-E' warning on a previously preprocessed file to make |
| 3258 | // more sense. |
Richard Smith | 403f76e | 2012-08-06 04:09:06 +0000 | [diff] [blame] | 3259 | else if (InitialPhase == phases::Compile && |
| 3260 | FinalPhase == phases::Preprocess && |
| 3261 | getPreprocessedType(InputType) == types::TY_INVALID) |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3262 | Diag(clang::diag::warn_drv_preprocessed_input_file_unused) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3263 | << InputArg->getAsString(Args) << !!FinalPhaseArg |
| 3264 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
Daniel Dunbar | 07806ca | 2009-09-17 04:13:26 +0000 | [diff] [blame] | 3265 | else |
| 3266 | Diag(clang::diag::warn_drv_input_file_unused) |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3267 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase) |
| 3268 | << !!FinalPhaseArg |
| 3269 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3270 | continue; |
| 3271 | } |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3272 | |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3273 | if (YcArg) { |
| 3274 | // Add a separate precompile phase for the compile phase. |
| 3275 | if (FinalPhase >= phases::Compile) { |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3276 | const types::ID HeaderType = lookupHeaderTypeForSourceType(InputType); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3277 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PCHPL; |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3278 | types::getCompilationPhases(HeaderType, PCHPL); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3279 | // Build the pipeline for the pch file. |
Richard Smith | 8cd452d | 2016-08-30 18:55:16 +0000 | [diff] [blame] | 3280 | Action *ClangClPch = |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 3281 | C.MakeAction<InputAction>(*InputArg, HeaderType); |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3282 | for (phases::ID Phase : PCHPL) |
| 3283 | ClangClPch = ConstructPhaseAction(C, Args, Phase, ClangClPch); |
| 3284 | assert(ClangClPch); |
| 3285 | Actions.push_back(ClangClPch); |
| 3286 | // The driver currently exits after the first failed command. This |
| 3287 | // relies on that behavior, to make sure if the pch generation fails, |
| 3288 | // the main compilation won't run. |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 3289 | // FIXME: If the main compilation fails, the PCH generation should |
| 3290 | // probably not be considered successful either. |
Nico Weber | ad2d8f3 | 2016-04-21 19:59:10 +0000 | [diff] [blame] | 3291 | } |
| 3292 | } |
| 3293 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3294 | // Build the pipeline for this file. |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3295 | Action *Current = C.MakeAction<InputAction>(*InputArg, InputType); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3296 | |
| 3297 | // Use the current host action in any of the offloading actions, if |
| 3298 | // required. |
| 3299 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3300 | break; |
| 3301 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3302 | for (SmallVectorImpl<phases::ID>::iterator i = PL.begin(), e = PL.end(); |
| 3303 | i != e; ++i) { |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3304 | phases::ID Phase = *i; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3305 | |
| 3306 | // We are done if this step is past what the user requested. |
| 3307 | if (Phase > FinalPhase) |
| 3308 | break; |
| 3309 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3310 | // Add any offload action the host action depends on. |
| 3311 | Current = OffloadBuilder.addDeviceDependencesToHostAction( |
| 3312 | Current, InputArg, Phase, FinalPhase, PL); |
| 3313 | if (!Current) |
| 3314 | break; |
| 3315 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3316 | // Queue linker inputs. |
| 3317 | if (Phase == phases::Link) { |
Matthew Curtis | 7ab8b2b | 2013-03-07 12:32:26 +0000 | [diff] [blame] | 3318 | assert((i + 1) == e && "linking must be final compilation step."); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3319 | LinkerInputs.push_back(Current); |
| 3320 | Current = nullptr; |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3321 | break; |
| 3322 | } |
| 3323 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3324 | // Each precompiled header file after a module file action is a module |
| 3325 | // header of that same module file, rather than being compiled to a |
| 3326 | // separate PCH. |
| 3327 | if (Phase == phases::Precompile && HeaderModuleAction && |
| 3328 | getPrecompiledType(InputType) == types::TY_PCH) { |
| 3329 | HeaderModuleAction->addModuleHeaderInput(Current); |
| 3330 | Current = nullptr; |
| 3331 | break; |
| 3332 | } |
| 3333 | |
| 3334 | // FIXME: Should we include any prior module file outputs as inputs of |
| 3335 | // later actions in the same command line? |
| 3336 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3337 | // Otherwise construct the appropriate action. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3338 | Action *NewCurrent = ConstructPhaseAction(C, Args, Phase, Current); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3339 | |
| 3340 | // We didn't create a new action, so we will just move to the next phase. |
| 3341 | if (NewCurrent == Current) |
Daniel Dunbar | 1386495 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 3342 | continue; |
| 3343 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3344 | if (auto *HMA = dyn_cast<HeaderModulePrecompileJobAction>(NewCurrent)) |
| 3345 | HeaderModuleAction = HMA; |
| 3346 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3347 | Current = NewCurrent; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3348 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3349 | // Use the current host action in any of the offloading actions, if |
| 3350 | // required. |
| 3351 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3352 | break; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3353 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3354 | if (Current->getType() == types::TY_Nothing) |
| 3355 | break; |
| 3356 | } |
| 3357 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3358 | // If we ended with something, add to the output list. |
| 3359 | if (Current) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3360 | Actions.push_back(Current); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3361 | |
| 3362 | // Add any top level actions generated for offloading. |
| 3363 | OffloadBuilder.appendTopLevelActions(Actions, Current, InputArg); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 3364 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3365 | |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3366 | // Add a link action if necessary. |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3367 | if (!LinkerInputs.empty()) { |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3368 | Action *LA = C.MakeAction<LinkJobAction>(LinkerInputs, types::TY_Image); |
| 3369 | LA = OffloadBuilder.processHostLinkAction(LA); |
| 3370 | Actions.push_back(LA); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3371 | } |
Daniel Dunbar | c7a67b7 | 2009-12-22 23:19:32 +0000 | [diff] [blame] | 3372 | |
| 3373 | // If we are linking, claim any options which are obviously only used for |
| 3374 | // compilation. |
Hans Wennborg | a8ef14f | 2013-09-17 00:03:41 +0000 | [diff] [blame] | 3375 | if (FinalPhase == phases::Link && PL.size() == 1) { |
Daniel Dunbar | c7a67b7 | 2009-12-22 23:19:32 +0000 | [diff] [blame] | 3376 | Args.ClaimAllArgs(options::OPT_CompileOnly_Group); |
Hans Wennborg | a8ef14f | 2013-09-17 00:03:41 +0000 | [diff] [blame] | 3377 | Args.ClaimAllArgs(options::OPT_cl_compile_Group); |
| 3378 | } |
| 3379 | |
Ziang Wan | af857b9 | 2019-06-14 21:42:21 +0000 | [diff] [blame] | 3380 | // If the use specify --print-supported-cpus, clang will only print out |
| 3381 | // supported cpu names without doing compilation. |
| 3382 | if (Arg *A = Args.getLastArg(options::OPT__print_supported_cpus)) { |
| 3383 | Actions.clear(); |
| 3384 | // the compilation now has only two phases: Input and Compile |
| 3385 | // use the --prints-supported-cpus flag as the dummy input to cc1 |
| 3386 | Action *InputAc = C.MakeAction<InputAction>(*A, types::TY_C); |
| 3387 | Actions.push_back( |
| 3388 | C.MakeAction<PrecompileJobAction>(InputAc, types::TY_Nothing)); |
| 3389 | } |
| 3390 | |
Hans Wennborg | a8ef14f | 2013-09-17 00:03:41 +0000 | [diff] [blame] | 3391 | // Claim ignored clang-cl options. |
| 3392 | Args.ClaimAllArgs(options::OPT_cl_ignored_Group); |
Artem Belevich | baae093 | 2015-07-28 21:01:30 +0000 | [diff] [blame] | 3393 | |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 3394 | // Claim --cuda-host-only and --cuda-compile-host-device, which may be passed |
| 3395 | // to non-CUDA compilations and should not trigger warnings there. |
Artem Belevich | baae093 | 2015-07-28 21:01:30 +0000 | [diff] [blame] | 3396 | Args.ClaimAllArgs(options::OPT_cuda_host_only); |
Justin Lebar | dc3c504 | 2016-04-19 02:27:07 +0000 | [diff] [blame] | 3397 | Args.ClaimAllArgs(options::OPT_cuda_compile_host_device); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 3400 | Action *Driver::ConstructPhaseAction( |
| 3401 | Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input, |
| 3402 | Action::OffloadKind TargetDeviceOffloadKind) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 3403 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); |
Samuel Antao | 64e965e | 2016-09-30 15:34:19 +0000 | [diff] [blame] | 3404 | |
| 3405 | // Some types skip the assembler phase (e.g., llvm-bc), but we can't |
| 3406 | // encode this in the steps because the intermediate type depends on |
| 3407 | // arguments. Just special case here. |
| 3408 | if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm) |
| 3409 | return Input; |
| 3410 | |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3411 | // Build the appropriate action. |
| 3412 | switch (Phase) { |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3413 | case phases::Link: |
| 3414 | llvm_unreachable("link action invalid here."); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3415 | case phases::Preprocess: { |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3416 | types::ID OutputTy; |
| 3417 | // -{M, MM} alter the output type. |
Daniel Dunbar | 86aed7d | 2010-12-08 21:33:40 +0000 | [diff] [blame] | 3418 | if (Args.hasArg(options::OPT_M, options::OPT_MM)) { |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3419 | OutputTy = types::TY_Dependencies; |
| 3420 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 3421 | OutputTy = Input->getType(); |
| 3422 | if (!Args.hasFlag(options::OPT_frewrite_includes, |
Justin Bogner | ce8245b | 2014-06-24 08:01:01 +0000 | [diff] [blame] | 3423 | options::OPT_fno_rewrite_includes, false) && |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 3424 | !Args.hasFlag(options::OPT_frewrite_imports, |
| 3425 | options::OPT_fno_rewrite_imports, false) && |
Justin Bogner | ce8245b | 2014-06-24 08:01:01 +0000 | [diff] [blame] | 3426 | !CCGenDiagnostics) |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 3427 | OutputTy = types::getPreprocessedType(OutputTy); |
Daniel Dunbar | d67a322 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 3428 | assert(OutputTy != types::TY_INVALID && |
| 3429 | "Cannot preprocess this input type!"); |
| 3430 | } |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3431 | return C.MakeAction<PreprocessJobAction>(Input, OutputTy); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3432 | } |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3433 | case phases::Precompile: { |
Richard Smith | dd4ad3d | 2016-08-30 19:06:26 +0000 | [diff] [blame] | 3434 | types::ID OutputTy = getPrecompiledType(Input->getType()); |
| 3435 | assert(OutputTy != types::TY_INVALID && |
| 3436 | "Cannot precompile this input type!"); |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3437 | |
| 3438 | // If we're given a module name, precompile header file inputs as a |
| 3439 | // module, not as a precompiled header. |
| 3440 | const char *ModName = nullptr; |
| 3441 | if (OutputTy == types::TY_PCH) { |
| 3442 | if (Arg *A = Args.getLastArg(options::OPT_fmodule_name_EQ)) |
| 3443 | ModName = A->getValue(); |
| 3444 | if (ModName) |
| 3445 | OutputTy = types::TY_ModuleFile; |
| 3446 | } |
| 3447 | |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3448 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 3449 | // Syntax checks should not emit a PCH file |
| 3450 | OutputTy = types::TY_Nothing; |
| 3451 | } |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3452 | |
| 3453 | if (ModName) |
| 3454 | return C.MakeAction<HeaderModulePrecompileJobAction>(Input, OutputTy, |
| 3455 | ModName); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3456 | return C.MakeAction<PrecompileJobAction>(Input, OutputTy); |
Aaron Ballman | 1f10cc5 | 2012-07-31 01:21:00 +0000 | [diff] [blame] | 3457 | } |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3458 | case phases::Compile: { |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3459 | if (Args.hasArg(options::OPT_fsyntax_only)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3460 | return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3461 | if (Args.hasArg(options::OPT_rewrite_objc)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3462 | return C.MakeAction<CompileJobAction>(Input, types::TY_RewrittenObjC); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3463 | if (Args.hasArg(options::OPT_rewrite_legacy_objc)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3464 | return C.MakeAction<CompileJobAction>(Input, |
| 3465 | types::TY_RewrittenLegacyObjC); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3466 | if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3467 | return C.MakeAction<AnalyzeJobAction>(Input, types::TY_Plist); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3468 | if (Args.hasArg(options::OPT__migrate)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3469 | return C.MakeAction<MigrateJobAction>(Input, types::TY_Remap); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3470 | if (Args.hasArg(options::OPT_emit_ast)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3471 | return C.MakeAction<CompileJobAction>(Input, types::TY_AST); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3472 | if (Args.hasArg(options::OPT_module_file_info)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3473 | return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3474 | if (Args.hasArg(options::OPT_verify_pch)) |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3475 | return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing); |
Puyan Lotfi | 68f29da | 2019-06-20 16:59:48 +0000 | [diff] [blame] | 3476 | if (Args.hasArg(options::OPT_emit_iterface_stubs)) |
| 3477 | return C.MakeAction<CompileJobAction>(Input, types::TY_IFS); |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3478 | return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC); |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3479 | } |
| 3480 | case phases::Backend: { |
Artem Belevich | ecb178b | 2018-03-21 22:22:59 +0000 | [diff] [blame] | 3481 | if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3482 | types::ID Output = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3483 | 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] | 3484 | return C.MakeAction<BackendJobAction>(Input, Output); |
David Blaikie | 486f440 | 2014-08-29 07:25:23 +0000 | [diff] [blame] | 3485 | } |
| 3486 | if (Args.hasArg(options::OPT_emit_llvm)) { |
Shuxin Yang | 4b3c7ff | 2013-08-23 21:34:57 +0000 | [diff] [blame] | 3487 | types::ID Output = |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3488 | 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] | 3489 | return C.MakeAction<BackendJobAction>(Input, Output); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3490 | } |
Justin Lebar | 4109461 | 2016-01-11 23:07:27 +0000 | [diff] [blame] | 3491 | return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3492 | } |
| 3493 | case phases::Assemble: |
Justin Lebar | 21e5d4f | 2016-01-14 21:41:27 +0000 | [diff] [blame] | 3494 | return C.MakeAction<AssembleJobAction>(std::move(Input), types::TY_Object); |
Daniel Dunbar | 6522933 | 2009-03-13 11:38:42 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3497 | llvm_unreachable("invalid phase in ConstructPhaseAction"); |
Daniel Dunbar | 1688f1a | 2009-03-12 07:58:46 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Daniel Dunbar | f0eddb8 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 3500 | void Driver::BuildJobs(Compilation &C) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 3501 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3502 | |
| 3503 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 3504 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3505 | // It is an error to provide a -o option if we are making multiple output |
| 3506 | // files. |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3507 | if (FinalOutput) { |
| 3508 | unsigned NumOutputs = 0; |
Saleem Abdulrasool | da3f4e5 | 2014-12-29 21:02:47 +0000 | [diff] [blame] | 3509 | for (const Action *A : C.getActions()) |
| 3510 | if (A->getType() != types::TY_Nothing) |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3511 | ++NumOutputs; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3512 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3513 | if (NumOutputs > 1) { |
| 3514 | Diag(clang::diag::err_drv_output_argument_with_multiple_files); |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3515 | FinalOutput = nullptr; |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3516 | } |
| 3517 | } |
| 3518 | |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3519 | // Collect the list of architectures. |
| 3520 | llvm::StringSet<> ArchNames; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3521 | if (C.getDefaultToolChain().getTriple().isOSBinFormatMachO()) |
| 3522 | for (const Arg *A : C.getArgs()) |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3523 | if (A->getOption().matches(options::OPT_arch)) |
| 3524 | ArchNames.insert(A->getValue()); |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3525 | |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3526 | // Set of (Action, canonical ToolChain triple) pairs we've built jobs for. |
| 3527 | std::map<std::pair<const Action *, std::string>, InputInfo> CachedResults; |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3528 | for (Action *A : C.getActions()) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3529 | // If we are linking an image for multiple archs then the linker wants |
| 3530 | // -arch_multiple and -final_output <final image name>. Unfortunately, this |
| 3531 | // 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] | 3532 | // |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3533 | // FIXME: This is a hack; find a cleaner way to integrate this into the |
| 3534 | // process. |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3535 | const char *LinkingOutput = nullptr; |
Daniel Dunbar | dd76524 | 2009-03-26 16:12:09 +0000 | [diff] [blame] | 3536 | if (isa<LipoJobAction>(A)) { |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3537 | if (FinalOutput) |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 3538 | LinkingOutput = FinalOutput->getValue(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3539 | else |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 3540 | LinkingOutput = getDefaultImageName(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3543 | BuildJobsForAction(C, A, &C.getDefaultToolChain(), |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3544 | /*BoundArch*/ StringRef(), |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3545 | /*AtTopLevel*/ true, |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 3546 | /*MultipleArchs*/ ArchNames.size() > 1, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3547 | /*LinkingOutput*/ LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3548 | /*TargetDeviceOffloadKind*/ Action::OFK_None); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 3549 | } |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3550 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3551 | // If the user passed -Qunused-arguments or there were errors, don't warn |
| 3552 | // about any unused arguments. |
Argyrios Kyrtzidis | 31448a4 | 2010-11-18 21:47:07 +0000 | [diff] [blame] | 3553 | if (Diags.hasErrorOccurred() || |
Daniel Dunbar | a3cfbe3 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 3554 | C.getArgs().hasArg(options::OPT_Qunused_arguments)) |
Daniel Dunbar | d175d97 | 2009-03-18 18:03:46 +0000 | [diff] [blame] | 3555 | return; |
| 3556 | |
Daniel Dunbar | 58399ae | 2009-03-29 22:24:54 +0000 | [diff] [blame] | 3557 | // Claim -### here. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3558 | (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3559 | |
Nico Weber | 47bf505 | 2016-04-25 21:15:49 +0000 | [diff] [blame] | 3560 | // Claim --driver-mode, --rsp-quoting, it was handled earlier. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 3561 | (void)C.getArgs().hasArg(options::OPT_driver_mode); |
Nico Weber | 47bf505 | 2016-04-25 21:15:49 +0000 | [diff] [blame] | 3562 | (void)C.getArgs().hasArg(options::OPT_rsp_quoting); |
Hans Wennborg | 70850d8 | 2013-07-18 20:29:38 +0000 | [diff] [blame] | 3563 | |
Saleem Abdulrasool | 688b6bc | 2014-12-29 19:01:36 +0000 | [diff] [blame] | 3564 | for (Arg *A : C.getArgs()) { |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3565 | // 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] | 3566 | // DiagnosticsEngine, so that extra values, position, and so on could be |
| 3567 | // printed. |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3568 | if (!A->isClaimed()) { |
Michael J. Spencer | 66e2b20 | 2012-10-19 22:37:06 +0000 | [diff] [blame] | 3569 | if (A->getOption().hasFlag(options::NoArgumentUnused)) |
Daniel Dunbar | a3cfbe3 | 2009-04-07 19:04:18 +0000 | [diff] [blame] | 3570 | continue; |
| 3571 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3572 | // Suppress the warning automatically if this is just a flag, and it is an |
| 3573 | // instance of an argument we already claimed. |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3574 | const Option &Opt = A->getOption(); |
Michael J. Spencer | ad3ccc3 | 2012-08-20 21:41:17 +0000 | [diff] [blame] | 3575 | if (Opt.getKind() == Option::FlagClass) { |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3576 | bool DuplicateClaimed = false; |
| 3577 | |
Sean Silva | 14facf3 | 2015-06-09 01:57:17 +0000 | [diff] [blame] | 3578 | for (const Arg *AA : C.getArgs().filtered(&Opt)) { |
| 3579 | if (AA->isClaimed()) { |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3580 | DuplicateClaimed = true; |
| 3581 | break; |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | if (DuplicateClaimed) |
| 3586 | continue; |
| 3587 | } |
| 3588 | |
Ehsan Akhgari | d851833 | 2016-01-25 21:14:52 +0000 | [diff] [blame] | 3589 | // In clang-cl, don't mention unknown arguments here since they have |
| 3590 | // already been warned about. |
| 3591 | if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) |
| 3592 | Diag(clang::diag::warn_drv_unused_argument) |
| 3593 | << A->getAsString(C.getArgs()); |
Daniel Dunbar | 90dd6f4 | 2009-04-04 00:52:26 +0000 | [diff] [blame] | 3594 | } |
Daniel Dunbar | 3ce436d | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 3595 | } |
Daniel Dunbar | 95e6b19 | 2009-03-13 22:12:33 +0000 | [diff] [blame] | 3596 | } |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3597 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3598 | namespace { |
| 3599 | /// Utility class to control the collapse of dependent actions and select the |
| 3600 | /// tools accordingly. |
| 3601 | class ToolSelector final { |
| 3602 | /// The tool chain this selector refers to. |
| 3603 | const ToolChain &TC; |
Daniel Dunbar | f9ff350 | 2010-05-14 02:03:00 +0000 | [diff] [blame] | 3604 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3605 | /// The compilation this selector refers to. |
| 3606 | const Compilation &C; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3607 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3608 | /// The base action this selector refers to. |
| 3609 | const JobAction *BaseAction; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3610 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3611 | /// Set to true if the current toolchain refers to host actions. |
| 3612 | bool IsHostSelector; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3613 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3614 | /// Set to true if save-temps and embed-bitcode functionalities are active. |
| 3615 | bool SaveTemps; |
| 3616 | bool EmbedBitcode; |
| 3617 | |
| 3618 | /// Get previous dependent action or null if that does not exist. If |
| 3619 | /// \a CanBeCollapsed is false, that action must be legal to collapse or |
| 3620 | /// null will be returned. |
| 3621 | const JobAction *getPrevDependentAction(const ActionList &Inputs, |
| 3622 | ActionList &SavedOffloadAction, |
| 3623 | bool CanBeCollapsed = true) { |
| 3624 | // An option can be collapsed only if it has a single input. |
| 3625 | if (Inputs.size() != 1) |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 3626 | return nullptr; |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3627 | |
| 3628 | Action *CurAction = *Inputs.begin(); |
| 3629 | if (CanBeCollapsed && |
| 3630 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3631 | return nullptr; |
| 3632 | |
| 3633 | // If the input action is an offload action. Look through it and save any |
| 3634 | // offload action that can be dropped in the event of a collapse. |
| 3635 | if (auto *OA = dyn_cast<OffloadAction>(CurAction)) { |
| 3636 | // If the dependent action is a device action, we will attempt to collapse |
| 3637 | // only with other device actions. Otherwise, we would do the same but |
| 3638 | // with host actions only. |
| 3639 | if (!IsHostSelector) { |
| 3640 | if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) { |
| 3641 | CurAction = |
| 3642 | OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true); |
| 3643 | if (CanBeCollapsed && |
| 3644 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3645 | return nullptr; |
| 3646 | SavedOffloadAction.push_back(OA); |
| 3647 | return dyn_cast<JobAction>(CurAction); |
| 3648 | } |
| 3649 | } else if (OA->hasHostDependence()) { |
| 3650 | CurAction = OA->getHostDependence(); |
| 3651 | if (CanBeCollapsed && |
| 3652 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3653 | return nullptr; |
| 3654 | SavedOffloadAction.push_back(OA); |
| 3655 | return dyn_cast<JobAction>(CurAction); |
| 3656 | } |
| 3657 | return nullptr; |
| 3658 | } |
| 3659 | |
| 3660 | return dyn_cast<JobAction>(CurAction); |
| 3661 | } |
| 3662 | |
| 3663 | /// Return true if an assemble action can be collapsed. |
| 3664 | bool canCollapseAssembleAction() const { |
| 3665 | return TC.useIntegratedAs() && !SaveTemps && |
| 3666 | !C.getArgs().hasArg(options::OPT_via_file_asm) && |
| 3667 | !C.getArgs().hasArg(options::OPT__SLASH_FA) && |
| 3668 | !C.getArgs().hasArg(options::OPT__SLASH_Fa); |
| 3669 | } |
| 3670 | |
| 3671 | /// Return true if a preprocessor action can be collapsed. |
| 3672 | bool canCollapsePreprocessorAction() const { |
| 3673 | return !C.getArgs().hasArg(options::OPT_no_integrated_cpp) && |
| 3674 | !C.getArgs().hasArg(options::OPT_traditional_cpp) && !SaveTemps && |
| 3675 | !C.getArgs().hasArg(options::OPT_rewrite_objc); |
| 3676 | } |
| 3677 | |
| 3678 | /// Struct that relates an action with the offload actions that would be |
| 3679 | /// collapsed with it. |
| 3680 | struct JobActionInfo final { |
| 3681 | /// The action this info refers to. |
| 3682 | const JobAction *JA = nullptr; |
| 3683 | /// The offload actions we need to take care off if this action is |
| 3684 | /// collapsed. |
| 3685 | ActionList SavedOffloadAction; |
| 3686 | }; |
| 3687 | |
| 3688 | /// Append collapsed offload actions from the give nnumber of elements in the |
| 3689 | /// action info array. |
| 3690 | static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction, |
| 3691 | ArrayRef<JobActionInfo> &ActionInfo, |
| 3692 | unsigned ElementNum) { |
| 3693 | assert(ElementNum <= ActionInfo.size() && "Invalid number of elements."); |
| 3694 | for (unsigned I = 0; I < ElementNum; ++I) |
| 3695 | CollapsedOffloadAction.append(ActionInfo[I].SavedOffloadAction.begin(), |
| 3696 | ActionInfo[I].SavedOffloadAction.end()); |
| 3697 | } |
| 3698 | |
| 3699 | /// Functions that attempt to perform the combining. They detect if that is |
| 3700 | /// legal, and if so they update the inputs \a Inputs and the offload action |
| 3701 | /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with |
| 3702 | /// the combined action is returned. If the combining is not legal or if the |
| 3703 | /// tool does not exist, null is returned. |
| 3704 | /// Currently three kinds of collapsing are supported: |
| 3705 | /// - Assemble + Backend + Compile; |
| 3706 | /// - Assemble + Backend ; |
| 3707 | /// - Backend + Compile. |
| 3708 | const Tool * |
| 3709 | combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3710 | ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3711 | ActionList &CollapsedOffloadAction) { |
| 3712 | if (ActionInfo.size() < 3 || !canCollapseAssembleAction()) |
| 3713 | return nullptr; |
| 3714 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3715 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3716 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[2].JA); |
| 3717 | if (!AJ || !BJ || !CJ) |
| 3718 | return nullptr; |
| 3719 | |
| 3720 | // Get compiler tool. |
| 3721 | const Tool *T = TC.SelectTool(*CJ); |
| 3722 | if (!T) |
| 3723 | return nullptr; |
| 3724 | |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 3725 | // When using -fembed-bitcode, it is required to have the same tool (clang) |
| 3726 | // for both CompilerJA and BackendJA. Otherwise, combine two stages. |
| 3727 | if (EmbedBitcode) { |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3728 | const Tool *BT = TC.SelectTool(*BJ); |
| 3729 | if (BT == T) |
| 3730 | return nullptr; |
Steven Wu | 574b0f2 | 2016-03-01 01:07:58 +0000 | [diff] [blame] | 3731 | } |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3732 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3733 | if (!T->hasIntegratedAssembler()) |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 3734 | return nullptr; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3735 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3736 | Inputs = CJ->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3737 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3738 | /*NumElements=*/3); |
| 3739 | return T; |
| 3740 | } |
| 3741 | const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo, |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3742 | ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3743 | ActionList &CollapsedOffloadAction) { |
| 3744 | if (ActionInfo.size() < 2 || !canCollapseAssembleAction()) |
| 3745 | return nullptr; |
| 3746 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3747 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3748 | if (!AJ || !BJ) |
| 3749 | return nullptr; |
| 3750 | |
| 3751 | // Retrieve the compile job, backend action must always be preceded by one. |
| 3752 | ActionList CompileJobOffloadActions; |
| 3753 | auto *CJ = getPrevDependentAction(BJ->getInputs(), CompileJobOffloadActions, |
| 3754 | /*CanBeCollapsed=*/false); |
| 3755 | if (!AJ || !BJ || !CJ) |
| 3756 | return nullptr; |
| 3757 | |
| 3758 | assert(isa<CompileJobAction>(CJ) && |
| 3759 | "Expecting compile job preceding backend job."); |
| 3760 | |
| 3761 | // Get compiler tool. |
| 3762 | const Tool *T = TC.SelectTool(*CJ); |
| 3763 | if (!T) |
| 3764 | return nullptr; |
| 3765 | |
| 3766 | if (!T->hasIntegratedAssembler()) |
| 3767 | return nullptr; |
| 3768 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3769 | Inputs = BJ->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3770 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3771 | /*NumElements=*/2); |
| 3772 | return T; |
| 3773 | } |
| 3774 | const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3775 | ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3776 | ActionList &CollapsedOffloadAction) { |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3777 | if (ActionInfo.size() < 2) |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3778 | return nullptr; |
| 3779 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[0].JA); |
| 3780 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[1].JA); |
| 3781 | if (!BJ || !CJ) |
| 3782 | return nullptr; |
| 3783 | |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3784 | // Check if the initial input (to the compile job or its predessor if one |
| 3785 | // exists) is LLVM bitcode. In that case, no preprocessor step is required |
| 3786 | // and we can still collapse the compile and backend jobs when we have |
| 3787 | // -save-temps. I.e. there is no need for a separate compile job just to |
| 3788 | // emit unoptimized bitcode. |
| 3789 | bool InputIsBitcode = true; |
| 3790 | for (size_t i = 1; i < ActionInfo.size(); i++) |
| 3791 | if (ActionInfo[i].JA->getType() != types::TY_LLVM_BC && |
| 3792 | ActionInfo[i].JA->getType() != types::TY_LTO_BC) { |
| 3793 | InputIsBitcode = false; |
| 3794 | break; |
| 3795 | } |
| 3796 | if (!InputIsBitcode && !canCollapsePreprocessorAction()) |
| 3797 | return nullptr; |
| 3798 | |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3799 | // Get compiler tool. |
| 3800 | const Tool *T = TC.SelectTool(*CJ); |
| 3801 | if (!T) |
| 3802 | return nullptr; |
| 3803 | |
Teresa Johnson | 9e4321c | 2018-04-17 16:39:25 +0000 | [diff] [blame] | 3804 | if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode)) |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3805 | return nullptr; |
| 3806 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3807 | Inputs = CJ->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3808 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3809 | /*NumElements=*/2); |
| 3810 | return T; |
| 3811 | } |
| 3812 | |
| 3813 | /// Updates the inputs if the obtained tool supports combining with |
| 3814 | /// preprocessor action, and the current input is indeed a preprocessor |
| 3815 | /// action. If combining results in the collapse of offloading actions, those |
| 3816 | /// are appended to \a CollapsedOffloadAction. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3817 | void combineWithPreprocessor(const Tool *T, ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3818 | ActionList &CollapsedOffloadAction) { |
| 3819 | if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP()) |
| 3820 | return; |
| 3821 | |
| 3822 | // Attempt to get a preprocessor action dependence. |
| 3823 | ActionList PreprocessJobOffloadActions; |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3824 | ActionList NewInputs; |
| 3825 | for (Action *A : Inputs) { |
| 3826 | auto *PJ = getPrevDependentAction({A}, PreprocessJobOffloadActions); |
| 3827 | if (!PJ || !isa<PreprocessJobAction>(PJ)) { |
| 3828 | NewInputs.push_back(A); |
| 3829 | continue; |
| 3830 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3831 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3832 | // This is legal to combine. Append any offload action we found and add the |
| 3833 | // current input to preprocessor inputs. |
| 3834 | CollapsedOffloadAction.append(PreprocessJobOffloadActions.begin(), |
| 3835 | PreprocessJobOffloadActions.end()); |
| 3836 | NewInputs.append(PJ->input_begin(), PJ->input_end()); |
| 3837 | } |
| 3838 | Inputs = NewInputs; |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | public: |
| 3842 | ToolSelector(const JobAction *BaseAction, const ToolChain &TC, |
| 3843 | const Compilation &C, bool SaveTemps, bool EmbedBitcode) |
| 3844 | : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps), |
| 3845 | EmbedBitcode(EmbedBitcode) { |
| 3846 | assert(BaseAction && "Invalid base action."); |
| 3847 | IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None; |
| 3848 | } |
| 3849 | |
| 3850 | /// Check if a chain of actions can be combined and return the tool that can |
| 3851 | /// handle the combination of actions. The pointer to the current inputs \a |
| 3852 | /// Inputs and the list of offload actions \a CollapsedOffloadActions |
| 3853 | /// connected to collapsed actions are updated accordingly. The latter enables |
| 3854 | /// the caller of the selector to process them afterwards instead of just |
| 3855 | /// dropping them. If no suitable tool is found, null will be returned. |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3856 | const Tool *getTool(ActionList &Inputs, |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3857 | ActionList &CollapsedOffloadAction) { |
| 3858 | // |
| 3859 | // Get the largest chain of actions that we could combine. |
| 3860 | // |
| 3861 | |
| 3862 | SmallVector<JobActionInfo, 5> ActionChain(1); |
| 3863 | ActionChain.back().JA = BaseAction; |
| 3864 | while (ActionChain.back().JA) { |
| 3865 | const Action *CurAction = ActionChain.back().JA; |
| 3866 | |
| 3867 | // Grow the chain by one element. |
| 3868 | ActionChain.resize(ActionChain.size() + 1); |
| 3869 | JobActionInfo &AI = ActionChain.back(); |
| 3870 | |
| 3871 | // Attempt to fill it with the |
| 3872 | AI.JA = |
| 3873 | getPrevDependentAction(CurAction->getInputs(), AI.SavedOffloadAction); |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3874 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3875 | |
| 3876 | // Pop the last action info as it could not be filled. |
| 3877 | ActionChain.pop_back(); |
| 3878 | |
| 3879 | // |
| 3880 | // Attempt to combine actions. If all combining attempts failed, just return |
| 3881 | // the tool of the provided action. At the end we attempt to combine the |
| 3882 | // action with any preprocessor action it may depend on. |
| 3883 | // |
| 3884 | |
| 3885 | const Tool *T = combineAssembleBackendCompile(ActionChain, Inputs, |
| 3886 | CollapsedOffloadAction); |
| 3887 | if (!T) |
| 3888 | T = combineAssembleBackend(ActionChain, Inputs, CollapsedOffloadAction); |
| 3889 | if (!T) |
| 3890 | T = combineBackendCompile(ActionChain, Inputs, CollapsedOffloadAction); |
| 3891 | if (!T) { |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 3892 | Inputs = BaseAction->getInputs(); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3893 | T = TC.SelectTool(*BaseAction); |
| 3894 | } |
| 3895 | |
| 3896 | combineWithPreprocessor(T, Inputs, CollapsedOffloadAction); |
| 3897 | return T; |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3898 | } |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3899 | }; |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3902 | /// Return a string that uniquely identifies the result of a job. The bound arch |
| 3903 | /// is not necessarily represented in the toolchain's triple -- for example, |
| 3904 | /// armv7 and armv7s both map to the same triple -- so we need both in our map. |
| 3905 | /// Also, we need to add the offloading device kind, as the same tool chain can |
| 3906 | /// be used for host and device for some programming models, e.g. OpenMP. |
| 3907 | static std::string GetTriplePlusArchString(const ToolChain *TC, |
| 3908 | StringRef BoundArch, |
| 3909 | Action::OffloadKind OffloadKind) { |
Justin Lebar | 55c8332 | 2016-01-16 03:30:08 +0000 | [diff] [blame] | 3910 | std::string TriplePlusArch = TC->getTriple().normalize(); |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3911 | if (!BoundArch.empty()) { |
Justin Lebar | 55c8332 | 2016-01-16 03:30:08 +0000 | [diff] [blame] | 3912 | TriplePlusArch += "-"; |
| 3913 | TriplePlusArch += BoundArch; |
| 3914 | } |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 3915 | TriplePlusArch += "-"; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3916 | TriplePlusArch += Action::GetOffloadKindName(OffloadKind); |
| 3917 | return TriplePlusArch; |
| 3918 | } |
| 3919 | |
| 3920 | InputInfo Driver::BuildJobsForAction( |
| 3921 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
| 3922 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
| 3923 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
| 3924 | Action::OffloadKind TargetDeviceOffloadKind) const { |
| 3925 | std::pair<const Action *, std::string> ActionTC = { |
| 3926 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3927 | auto CachedResult = CachedResults.find(ActionTC); |
| 3928 | if (CachedResult != CachedResults.end()) { |
| 3929 | return CachedResult->second; |
| 3930 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3931 | InputInfo Result = BuildJobsForActionNoCache( |
| 3932 | C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3933 | CachedResults, TargetDeviceOffloadKind); |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3934 | CachedResults[ActionTC] = Result; |
| 3935 | return Result; |
| 3936 | } |
| 3937 | |
| 3938 | InputInfo Driver::BuildJobsForActionNoCache( |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 3939 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
Justin Lebar | b44f6fe | 2016-01-14 21:41:21 +0000 | [diff] [blame] | 3940 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3941 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3942 | Action::OffloadKind TargetDeviceOffloadKind) const { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 3943 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
Daniel Dunbar | c4acf9d | 2009-03-18 23:18:19 +0000 | [diff] [blame] | 3944 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3945 | InputInfoList OffloadDependencesInputInfo; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3946 | bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3947 | if (const OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
Alex Lorenz | 4003a98 | 2017-08-08 11:22:21 +0000 | [diff] [blame] | 3948 | // The 'Darwin' toolchain is initialized only when its arguments are |
| 3949 | // computed. Get the default arguments for OFK_None to ensure that |
| 3950 | // initialization is performed before processing the offload action. |
| 3951 | // FIXME: Remove when darwin's toolchain is initialized during construction. |
| 3952 | C.getArgsForToolChain(TC, BoundArch, Action::OFK_None); |
| 3953 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3954 | // The offload action is expected to be used in four different situations. |
| 3955 | // |
| 3956 | // a) Set a toolchain/architecture/kind for a host action: |
| 3957 | // Host Action 1 -> OffloadAction -> Host Action 2 |
| 3958 | // |
| 3959 | // b) Set a toolchain/architecture/kind for a device action; |
| 3960 | // Device Action 1 -> OffloadAction -> Device Action 2 |
| 3961 | // |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 3962 | // c) Specify a device dependence to a host action; |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3963 | // Device Action 1 _ |
| 3964 | // \ |
| 3965 | // Host Action 1 ---> OffloadAction -> Host Action 2 |
| 3966 | // |
| 3967 | // d) Specify a host dependence to a device action. |
| 3968 | // Host Action 1 _ |
| 3969 | // \ |
| 3970 | // Device Action 1 ---> OffloadAction -> Device Action 2 |
| 3971 | // |
| 3972 | // For a) and b), we just return the job generated for the dependence. For |
| 3973 | // c) and d) we override the current action with the host/device dependence |
| 3974 | // if the current toolchain is host/device and set the offload dependences |
| 3975 | // info with the jobs obtained from the device/host dependence(s). |
| 3976 | |
| 3977 | // If there is a single device option, just generate the job for it. |
| 3978 | if (OA->hasSingleDeviceDependence()) { |
| 3979 | InputInfo DevA; |
| 3980 | OA->doOnEachDeviceDependence([&](Action *DepA, const ToolChain *DepTC, |
| 3981 | const char *DepBoundArch) { |
| 3982 | DevA = |
| 3983 | BuildJobsForAction(C, DepA, DepTC, DepBoundArch, AtTopLevel, |
| 3984 | /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3985 | CachedResults, DepA->getOffloadingDeviceKind()); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3986 | }); |
| 3987 | return DevA; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3988 | } |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3989 | |
| 3990 | // If 'Action 2' is host, we generate jobs for the device dependences and |
| 3991 | // override the current action with the host dependence. Otherwise, we |
| 3992 | // generate the host dependences and override the action with the device |
| 3993 | // dependence. The dependences can't therefore be a top-level action. |
| 3994 | OA->doOnEachDependence( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 3995 | /*IsHostDependence=*/BuildingForOffloadDevice, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 3996 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 3997 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
| 3998 | C, DepA, DepTC, DepBoundArch, /*AtTopLevel=*/false, |
| 3999 | /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4000 | DepA->getOffloadingDeviceKind())); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4001 | }); |
| 4002 | |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4003 | A = BuildingForOffloadDevice |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4004 | ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true) |
| 4005 | : OA->getHostDependence(); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4008 | if (const InputAction *IA = dyn_cast<InputAction>(A)) { |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4009 | // FIXME: It would be nice to not claim this here; maybe the old scheme of |
| 4010 | // just using Args was better? |
Daniel Dunbar | 5cdf3e0 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 4011 | const Arg &Input = IA->getInputArg(); |
| 4012 | Input.claim(); |
Daniel Dunbar | 35cbfeb | 2010-06-09 22:31:08 +0000 | [diff] [blame] | 4013 | if (Input.getOption().matches(options::OPT_INPUT)) { |
Richard Smith | bd55daf | 2012-11-01 04:30:05 +0000 | [diff] [blame] | 4014 | const char *Name = Input.getValue(); |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4015 | return InputInfo(A, Name, /* BaseInput = */ Name); |
Douglas Katzman | 678d0cb | 2015-06-16 18:01:24 +0000 | [diff] [blame] | 4016 | } |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4017 | return InputInfo(A, &Input, /* BaseInput = */ ""); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { |
Chad Rosier | a78a6eb | 2012-04-27 16:50:38 +0000 | [diff] [blame] | 4021 | const ToolChain *TC; |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 4022 | StringRef ArchName = BAA->getArchName(); |
Daniel Dunbar | 1ef3f2a | 2009-09-08 23:37:19 +0000 | [diff] [blame] | 4023 | |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 4024 | if (!ArchName.empty()) |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 4025 | TC = &getToolChain(C.getArgs(), |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4026 | computeTargetTriple(*this, TargetTriple, |
Andrey Turetskiy | 6a8b91d | 2016-04-21 10:16:48 +0000 | [diff] [blame] | 4027 | C.getArgs(), ArchName)); |
Chad Rosier | a78a6eb | 2012-04-27 16:50:38 +0000 | [diff] [blame] | 4028 | else |
| 4029 | TC = &C.getDefaultToolChain(); |
Daniel Dunbar | 1ef3f2a | 2009-09-08 23:37:19 +0000 | [diff] [blame] | 4030 | |
Nico Weber | 5a459f8 | 2016-02-23 19:30:43 +0000 | [diff] [blame] | 4031 | return BuildJobsForAction(C, *BAA->input_begin(), TC, ArchName, AtTopLevel, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4032 | MultipleArchs, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4033 | TargetDeviceOffloadKind); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 4036 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 4037 | ActionList Inputs = A->getInputs(); |
Daniel Dunbar | c434394 | 2010-02-03 03:07:56 +0000 | [diff] [blame] | 4038 | |
| 4039 | const JobAction *JA = cast<JobAction>(A); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4040 | ActionList CollapsedOffloadActions; |
| 4041 | |
Mehdi Amini | 6683e22 | 2017-01-24 18:12:25 +0000 | [diff] [blame] | 4042 | ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), |
| 4043 | embedBitcodeInObject() && !isUsingLTO()); |
Samuel Antao | 9c9d9cd | 2016-10-27 16:29:20 +0000 | [diff] [blame] | 4044 | const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions); |
| 4045 | |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 4046 | if (!T) |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 4047 | return InputInfo(); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4048 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4049 | // If we've collapsed action list that contained OffloadAction we |
| 4050 | // need to build jobs for host/device-side inputs it may have held. |
| 4051 | for (const auto *OA : CollapsedOffloadActions) |
| 4052 | cast<OffloadAction>(OA)->doOnEachDependence( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4053 | /*IsHostDependence=*/BuildingForOffloadDevice, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4054 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 4055 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
Artem Belevich | bee2f41 | 2016-08-22 18:50:34 +0000 | [diff] [blame] | 4056 | C, DepA, DepTC, DepBoundArch, /* AtTopLevel */ false, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4057 | /*MultipleArchs=*/!!DepBoundArch, LinkingOutput, CachedResults, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4058 | DepA->getOffloadingDeviceKind())); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4059 | }); |
Artem Belevich | f8144ab | 2015-08-27 18:10:41 +0000 | [diff] [blame] | 4060 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4061 | // Only use pipes when there is exactly one input. |
Daniel Dunbar | 1a093d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 4062 | InputInfoList InputInfos; |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 4063 | for (const Action *Input : Inputs) { |
Eric Christopher | 14668dd | 2013-02-18 00:38:25 +0000 | [diff] [blame] | 4064 | // Treat dsymutil and verify sub-jobs as being at the top-level too, they |
| 4065 | // shouldn't get temporary output names. |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 4066 | // FIXME: Clean this up. |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 4067 | bool SubJobAtTopLevel = |
| 4068 | AtTopLevel && (isa<DsymutilJobAction>(A) || isa<VerifyJobAction>(A)); |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4069 | InputInfos.push_back(BuildJobsForAction( |
| 4070 | C, Input, TC, BoundArch, SubJobAtTopLevel, MultipleArchs, LinkingOutput, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4071 | CachedResults, A->getOffloadingDeviceKind())); |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4074 | // Always use the first input as the base input. |
| 4075 | const char *BaseInput = InputInfos[0].getBaseInput(); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4076 | |
Daniel Dunbar | 6beaf51 | 2010-06-04 18:28:41 +0000 | [diff] [blame] | 4077 | // ... except dsymutil actions, which use their actual input as the base |
| 4078 | // input. |
| 4079 | if (JA->getType() == types::TY_dSYM) |
| 4080 | BaseInput = InputInfos[0].getFilename(); |
| 4081 | |
Richard Smith | cd35eff | 2018-09-15 01:21:16 +0000 | [diff] [blame] | 4082 | // ... and in header module compilations, which use the module name. |
| 4083 | if (auto *ModuleJA = dyn_cast<HeaderModulePrecompileJobAction>(JA)) |
| 4084 | BaseInput = ModuleJA->getModuleName(); |
| 4085 | |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4086 | // Append outputs of offload device jobs to the input list |
| 4087 | if (!OffloadDependencesInputInfo.empty()) |
| 4088 | InputInfos.append(OffloadDependencesInputInfo.begin(), |
| 4089 | OffloadDependencesInputInfo.end()); |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 4090 | |
Vedant Kumar | 18286cf | 2016-07-27 23:02:20 +0000 | [diff] [blame] | 4091 | // Set the effective triple of the toolchain for the duration of this job. |
| 4092 | llvm::Triple EffectiveTriple; |
| 4093 | const ToolChain &ToolTC = T->getToolChain(); |
Samuel Antao | 31fef98 | 2016-10-27 17:39:44 +0000 | [diff] [blame] | 4094 | const ArgList &Args = |
| 4095 | C.getArgsForToolChain(TC, BoundArch, A->getOffloadingDeviceKind()); |
Vedant Kumar | 18286cf | 2016-07-27 23:02:20 +0000 | [diff] [blame] | 4096 | if (InputInfos.size() != 1) { |
| 4097 | EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args)); |
| 4098 | } else { |
| 4099 | // Pass along the input type if it can be unambiguously determined. |
| 4100 | EffectiveTriple = llvm::Triple( |
| 4101 | ToolTC.ComputeEffectiveClangTriple(Args, InputInfos[0].getType())); |
| 4102 | } |
| 4103 | RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple); |
| 4104 | |
Daniel Dunbar | d00272f | 2010-08-02 02:38:15 +0000 | [diff] [blame] | 4105 | // Determine the place to write output to, if any. |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 4106 | InputInfo Result; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4107 | InputInfoList UnbundlingResults; |
| 4108 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(JA)) { |
| 4109 | // If we have an unbundling job, we need to create results for all the |
| 4110 | // outputs. We also update the results cache so that other actions using |
| 4111 | // this unbundling action can get the right results. |
| 4112 | for (auto &UI : UA->getDependentActionsInfo()) { |
| 4113 | assert(UI.DependentOffloadKind != Action::OFK_None && |
| 4114 | "Unbundling with no offloading??"); |
| 4115 | |
| 4116 | // Unbundling actions are never at the top level. When we generate the |
| 4117 | // offloading prefix, we also do that for the host file because the |
| 4118 | // unbundling action does not change the type of the output which can |
| 4119 | // cause a overwrite. |
| 4120 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 4121 | UI.DependentOffloadKind, |
| 4122 | UI.DependentToolChain->getTriple().normalize(), |
| 4123 | /*CreatePrefixForHost=*/true); |
| 4124 | auto CurI = InputInfo( |
Yaxun Liu | 609f752 | 2018-05-11 19:02:18 +0000 | [diff] [blame] | 4125 | UA, |
| 4126 | GetNamedOutputPath(C, *UA, BaseInput, UI.DependentBoundArch, |
| 4127 | /*AtTopLevel=*/false, |
| 4128 | MultipleArchs || |
| 4129 | UI.DependentOffloadKind == Action::OFK_HIP, |
| 4130 | OffloadingPrefix), |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4131 | BaseInput); |
| 4132 | // Save the unbundling result. |
| 4133 | UnbundlingResults.push_back(CurI); |
| 4134 | |
| 4135 | // Get the unique string identifier for this dependence and cache the |
| 4136 | // result. |
Yaxun Liu | 470b833 | 2018-06-06 19:44:10 +0000 | [diff] [blame] | 4137 | StringRef Arch; |
| 4138 | if (TargetDeviceOffloadKind == Action::OFK_HIP) { |
| 4139 | if (UI.DependentOffloadKind == Action::OFK_Host) |
Yaxun Liu | f37b50a | 2018-07-24 01:03:44 +0000 | [diff] [blame] | 4140 | Arch = StringRef(); |
Yaxun Liu | 470b833 | 2018-06-06 19:44:10 +0000 | [diff] [blame] | 4141 | else |
| 4142 | Arch = UI.DependentBoundArch; |
| 4143 | } else |
| 4144 | Arch = BoundArch; |
| 4145 | |
| 4146 | CachedResults[{A, GetTriplePlusArchString(UI.DependentToolChain, Arch, |
| 4147 | UI.DependentOffloadKind)}] = |
| 4148 | CurI; |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | // Now that we have all the results generated, select the one that should be |
| 4152 | // returned for the current depending action. |
| 4153 | std::pair<const Action *, std::string> ActionTC = { |
| 4154 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
| 4155 | assert(CachedResults.find(ActionTC) != CachedResults.end() && |
| 4156 | "Result does not exist??"); |
| 4157 | Result = CachedResults[ActionTC]; |
| 4158 | } else if (JA->getType() == types::TY_Nothing) |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4159 | Result = InputInfo(A, BaseInput); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4160 | else { |
| 4161 | // We only have to generate a prefix for the host if this is not a top-level |
| 4162 | // action. |
| 4163 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 4164 | A->getOffloadingDeviceKind(), TC->getTriple().normalize(), |
| 4165 | /*CreatePrefixForHost=*/!!A->getOffloadingHostActiveKinds() && |
| 4166 | !AtTopLevel); |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4167 | Result = InputInfo(A, GetNamedOutputPath(C, *JA, BaseInput, BoundArch, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4168 | AtTopLevel, MultipleArchs, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4169 | OffloadingPrefix), |
Justin Lebar | d98cea8 | 2016-01-11 23:15:21 +0000 | [diff] [blame] | 4170 | BaseInput); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4171 | } |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4172 | |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 4173 | if (CCCPrintBindings && !CCGenDiagnostics) { |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 4174 | llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"' |
| 4175 | << " - \"" << T->getName() << "\", inputs: ["; |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 4176 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
| 4177 | llvm::errs() << InputInfos[i].getAsString(); |
| 4178 | if (i + 1 != e) |
| 4179 | llvm::errs() << ", "; |
| 4180 | } |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4181 | if (UnbundlingResults.empty()) |
| 4182 | llvm::errs() << "], output: " << Result.getAsString() << "\n"; |
| 4183 | else { |
| 4184 | llvm::errs() << "], outputs: ["; |
| 4185 | for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) { |
| 4186 | llvm::errs() << UnbundlingResults[i].getAsString(); |
| 4187 | if (i + 1 != e) |
| 4188 | llvm::errs() << ", "; |
| 4189 | } |
| 4190 | llvm::errs() << "] \n"; |
| 4191 | } |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 4192 | } else { |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4193 | if (UnbundlingResults.empty()) |
| 4194 | T->ConstructJob( |
| 4195 | C, *JA, Result, InputInfos, |
| 4196 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 4197 | LinkingOutput); |
| 4198 | else |
Samuel Antao | 7108bf3 | 2016-11-03 15:41:50 +0000 | [diff] [blame] | 4199 | T->ConstructJobMultipleOutputs( |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4200 | C, *JA, UnbundlingResults, InputInfos, |
| 4201 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 4202 | LinkingOutput); |
Daniel Dunbar | b39cc52 | 2009-03-17 22:47:06 +0000 | [diff] [blame] | 4203 | } |
Justin Lebar | 9eaa489 | 2016-01-11 23:09:32 +0000 | [diff] [blame] | 4204 | return Result; |
Daniel Dunbar | e75d834 | 2009-03-16 06:56:51 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 4207 | const char *Driver::getDefaultImageName() const { |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4208 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 4209 | return Target.isOSWindows() ? "a.exe" : "a.out"; |
| 4210 | } |
| 4211 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4212 | /// Create output filename based on ArgValue, which could either be a |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4213 | /// full filename, filename without extension, or a directory. If ArgValue |
| 4214 | /// does not provide a filename, then use BaseName, and use the extension |
| 4215 | /// suitable for FileType. |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4216 | static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4217 | StringRef BaseName, |
| 4218 | types::ID FileType) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4219 | SmallString<128> Filename = ArgValue; |
Justin Bogner | 5aaf2e7 | 2014-06-26 20:59:36 +0000 | [diff] [blame] | 4220 | |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 4221 | if (ArgValue.empty()) { |
| 4222 | // If the argument is empty, output to BaseName in the current dir. |
| 4223 | Filename = BaseName; |
| 4224 | } else if (llvm::sys::path::is_separator(Filename.back())) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4225 | // If the argument is a directory, output to BaseName in that dir. |
| 4226 | llvm::sys::path::append(Filename, BaseName); |
| 4227 | } |
| 4228 | |
| 4229 | if (!llvm::sys::path::has_extension(ArgValue)) { |
| 4230 | // If the argument didn't provide an extension, then set it. |
| 4231 | const char *Extension = types::getTypeTempSuffix(FileType, true); |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 4232 | |
| 4233 | if (FileType == types::TY_Image && |
| 4234 | Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) { |
| 4235 | // The output file is a dll. |
| 4236 | Extension = "dll"; |
| 4237 | } |
| 4238 | |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4239 | llvm::sys::path::replace_extension(Filename, Extension); |
| 4240 | } |
| 4241 | |
| 4242 | return Args.MakeArgString(Filename.c_str()); |
| 4243 | } |
| 4244 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4245 | const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4246 | const char *BaseInput, |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 4247 | StringRef BoundArch, bool AtTopLevel, |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 4248 | bool MultipleArchs, |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4249 | StringRef OffloadingPrefix) const { |
Daniel Dunbar | 2608c54 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 4250 | llvm::PrettyStackTraceString CrashInfo("Computing output path"); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4251 | // Output to a user requested destination? |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4252 | if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) { |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4253 | if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4254 | return C.addResultFile(FinalOutput->getValue(), &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 4257 | // For /P, preprocess to file named after BaseInput. |
| 4258 | if (C.getArgs().hasArg(options::OPT__SLASH_P)) { |
| 4259 | assert(AtTopLevel && isa<PreprocessJobAction>(JA)); |
| 4260 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
Hans Wennborg | 04c764f | 2014-06-17 00:19:12 +0000 | [diff] [blame] | 4261 | StringRef NameArg; |
Greg Bedwell | 065f70a | 2015-06-09 10:24:06 +0000 | [diff] [blame] | 4262 | if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi)) |
Hans Wennborg | 04c764f | 2014-06-17 00:19:12 +0000 | [diff] [blame] | 4263 | NameArg = A->getValue(); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4264 | return C.addResultFile( |
| 4265 | MakeCLOutputFilename(C.getArgs(), NameArg, BaseName, types::TY_PP_C), |
| 4266 | &JA); |
Hans Wennborg | e005347 | 2013-12-20 18:40:46 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Nick Lewycky | 6e1ce29 | 2010-09-24 00:46:53 +0000 | [diff] [blame] | 4269 | // Default to writing to stdout? |
David Blaikie | bc023c9 | 2018-06-14 23:09:06 +0000 | [diff] [blame] | 4270 | if (AtTopLevel && !CCGenDiagnostics && isa<PreprocessJobAction>(JA)) |
Nick Lewycky | 6e1ce29 | 2010-09-24 00:46:53 +0000 | [diff] [blame] | 4271 | return "-"; |
| 4272 | |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4273 | // Is this the assembly listing for /FA? |
| 4274 | if (JA.getType() == types::TY_PP_Asm && |
| 4275 | (C.getArgs().hasArg(options::OPT__SLASH_FA) || |
| 4276 | C.getArgs().hasArg(options::OPT__SLASH_Fa))) { |
| 4277 | // Use /Fa and the input filename to determine the asm file name. |
| 4278 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
| 4279 | StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4280 | return C.addResultFile( |
| 4281 | MakeCLOutputFilename(C.getArgs(), FaValue, BaseName, JA.getType()), |
| 4282 | &JA); |
Hans Wennborg | 2c21f74 | 2013-10-17 16:16:23 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4285 | // Output to a temporary file? |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4286 | if ((!AtTopLevel && !isSaveTempsEnabled() && |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4287 | !C.getArgs().hasArg(options::OPT__SLASH_Fo)) || |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 4288 | CCGenDiagnostics) { |
Chad Rosier | 97c3737 | 2011-08-26 22:27:02 +0000 | [diff] [blame] | 4289 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4290 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 4291 | SmallString<128> TmpName; |
| 4292 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
| 4293 | Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir); |
| 4294 | if (CCGenDiagnostics && A) { |
| 4295 | SmallString<128> CrashDirectory(A->getValue()); |
Petr Hosek | 1e39306 | 2019-05-04 00:55:14 +0000 | [diff] [blame] | 4296 | if (!getVFS().exists(CrashDirectory)) |
| 4297 | llvm::sys::fs::create_directories(CrashDirectory); |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 4298 | llvm::sys::path::append(CrashDirectory, Split.first); |
| 4299 | const char *Middle = Suffix ? "-%%%%%%." : "-%%%%%%"; |
Simon Atanasyan | 27c0f20 | 2019-04-02 18:03:15 +0000 | [diff] [blame] | 4300 | std::error_code EC = llvm::sys::fs::createUniqueFile( |
| 4301 | CrashDirectory + Middle + Suffix, TmpName); |
Bob Haarman | 7e4d3ff | 2018-07-09 21:07:20 +0000 | [diff] [blame] | 4302 | if (EC) { |
| 4303 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4304 | return ""; |
| 4305 | } |
| 4306 | } else { |
| 4307 | TmpName = GetTemporaryPath(Split.first, Suffix); |
| 4308 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4309 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4312 | SmallString<128> BasePath(BaseInput); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4313 | StringRef BaseName; |
Daniel Dunbar | 67fea71 | 2011-03-25 18:16:51 +0000 | [diff] [blame] | 4314 | |
| 4315 | // Dsymutil actions should use the full path. |
Eric Christopher | 551ef45 | 2011-08-23 17:56:55 +0000 | [diff] [blame] | 4316 | if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA)) |
Daniel Dunbar | 67fea71 | 2011-03-25 18:16:51 +0000 | [diff] [blame] | 4317 | BaseName = BasePath; |
| 4318 | else |
| 4319 | BaseName = llvm::sys::path::filename(BasePath); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4320 | |
| 4321 | // Determine what the derived output name should be. |
| 4322 | const char *NamedOutput; |
Hans Wennborg | 2b89a26 | 2013-08-06 22:11:28 +0000 | [diff] [blame] | 4323 | |
Hans Wennborg | 625fba8 | 2016-10-04 21:01:04 +0000 | [diff] [blame] | 4324 | if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) && |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 4325 | C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) { |
| 4326 | // The /Fo or /o flag decides the object filename. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4327 | StringRef Val = |
| 4328 | C.getArgs() |
| 4329 | .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o) |
| 4330 | ->getValue(); |
| 4331 | NamedOutput = |
| 4332 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object); |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4333 | } else if (JA.getType() == types::TY_Image && |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4334 | C.getArgs().hasArg(options::OPT__SLASH_Fe, |
| 4335 | options::OPT__SLASH_o)) { |
Ehsan Akhgari | 81f36b7 | 2014-09-11 18:16:21 +0000 | [diff] [blame] | 4336 | // The /Fe or /o flag names the linked file. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4337 | StringRef Val = |
| 4338 | C.getArgs() |
| 4339 | .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o) |
| 4340 | ->getValue(); |
| 4341 | NamedOutput = |
| 4342 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Image); |
Hans Wennborg | f1a7425 | 2013-09-10 20:18:04 +0000 | [diff] [blame] | 4343 | } else if (JA.getType() == types::TY_Image) { |
Hans Wennborg | 207fcf0 | 2013-08-12 21:56:42 +0000 | [diff] [blame] | 4344 | if (IsCLMode()) { |
| 4345 | // clang-cl uses BaseName for the executable name. |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4346 | NamedOutput = |
| 4347 | MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image); |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 4348 | } else { |
Hans Wennborg | a770722 | 2015-01-09 17:38:53 +0000 | [diff] [blame] | 4349 | SmallString<128> Output(getDefaultImageName()); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4350 | Output += OffloadingPrefix; |
Samuel Antao | 59efaed | 2016-10-27 17:31:22 +0000 | [diff] [blame] | 4351 | if (MultipleArchs && !BoundArch.empty()) { |
| 4352 | Output += "-"; |
| 4353 | Output.append(BoundArch); |
| 4354 | } |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4355 | NamedOutput = C.getArgs().MakeArgString(Output.c_str()); |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4356 | } |
| 4357 | } else if (JA.getType() == types::TY_PCH && IsCLMode()) { |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4358 | NamedOutput = C.getArgs().MakeArgString(GetClPchPath(C, BaseName)); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4359 | } else { |
Hans Wennborg | 0a096a0 | 2013-09-05 17:05:56 +0000 | [diff] [blame] | 4360 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4361 | assert(Suffix && "All types used for output should have a suffix."); |
| 4362 | |
| 4363 | std::string::size_type End = std::string::npos; |
| 4364 | if (!types::appendSuffixForType(JA.getType())) |
| 4365 | End = BaseName.rfind('.'); |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4366 | SmallString<128> Suffixed(BaseName.substr(0, End)); |
Samuel Antao | 3b7e38b | 2016-10-27 18:14:55 +0000 | [diff] [blame] | 4367 | Suffixed += OffloadingPrefix; |
Mehdi Amini | c50b1a2 | 2016-10-07 21:27:26 +0000 | [diff] [blame] | 4368 | if (MultipleArchs && !BoundArch.empty()) { |
Chad Rosier | 3576723 | 2013-04-30 22:01:21 +0000 | [diff] [blame] | 4369 | Suffixed += "-"; |
| 4370 | Suffixed.append(BoundArch); |
| 4371 | } |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 4372 | // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for |
| 4373 | // the unoptimized bitcode so that it does not get overwritten by the ".bc" |
| 4374 | // optimized bitcode output. |
| 4375 | if (!AtTopLevel && C.getArgs().hasArg(options::OPT_emit_llvm) && |
| 4376 | JA.getType() == types::TY_LLVM_BC) |
| 4377 | Suffixed += ".tmp"; |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4378 | Suffixed += '.'; |
| 4379 | Suffixed += Suffix; |
| 4380 | NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); |
| 4381 | } |
| 4382 | |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4383 | // Prepend object file path if -save-temps=obj |
| 4384 | if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) && |
| 4385 | JA.getType() != types::TY_PCH) { |
| 4386 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 4387 | SmallString<128> TempPath(FinalOutput->getValue()); |
| 4388 | llvm::sys::path::remove_filename(TempPath); |
| 4389 | StringRef OutputFileName = llvm::sys::path::filename(NamedOutput); |
| 4390 | llvm::sys::path::append(TempPath, OutputFileName); |
| 4391 | NamedOutput = C.getArgs().MakeArgString(TempPath.c_str()); |
| 4392 | } |
| 4393 | |
Chad Rosier | 6213549 | 2012-07-09 17:31:28 +0000 | [diff] [blame] | 4394 | // 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] | 4395 | // then avoid overwriting input file. |
Reid Kleckner | 68eb60b | 2015-02-02 22:41:48 +0000 | [diff] [blame] | 4396 | if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) { |
Chad Rosier | 5b58af0 | 2012-04-20 20:05:08 +0000 | [diff] [blame] | 4397 | bool SameFile = false; |
| 4398 | SmallString<256> Result; |
| 4399 | llvm::sys::fs::current_path(Result); |
| 4400 | llvm::sys::path::append(Result, BaseName); |
| 4401 | llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile); |
| 4402 | // Must share the same path to conflict. |
| 4403 | if (SameFile) { |
| 4404 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4405 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4406 | std::string TmpName = GetTemporaryPath( |
| 4407 | Split.first, types::getTypeTempSuffix(JA.getType(), IsCLMode())); |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 4408 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
Chad Rosier | 5b58af0 | 2012-04-20 20:05:08 +0000 | [diff] [blame] | 4409 | } |
Chad Rosier | f8412cd | 2011-07-15 21:54:29 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4412 | // As an annoying special case, PCH generation doesn't strip the pathname. |
Nico Weber | 8ab9219 | 2016-03-02 23:29:29 +0000 | [diff] [blame] | 4413 | if (JA.getType() == types::TY_PCH && !IsCLMode()) { |
Michael J. Spencer | e169675 | 2010-12-18 00:19:12 +0000 | [diff] [blame] | 4414 | llvm::sys::path::remove_filename(BasePath); |
| 4415 | if (BasePath.empty()) |
Daniel Dunbar | e6c8319 | 2009-03-18 09:58:30 +0000 | [diff] [blame] | 4416 | BasePath = NamedOutput; |
| 4417 | else |
Michael J. Spencer | e169675 | 2010-12-18 00:19:12 +0000 | [diff] [blame] | 4418 | llvm::sys::path::append(BasePath, NamedOutput); |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4419 | return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4420 | } else { |
Chad Rosier | 633dcdc | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 4421 | return C.addResultFile(NamedOutput, &JA); |
Daniel Dunbar | 7a178a4 | 2009-03-17 17:53:55 +0000 | [diff] [blame] | 4422 | } |
| 4423 | } |
| 4424 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4425 | std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const { |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 4426 | // Search for Name in a list of paths. |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame] | 4427 | auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P) |
| 4428 | -> llvm::Optional<std::string> { |
| 4429 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
| 4430 | // attempting to use this prefix when looking for file paths. |
| 4431 | for (const auto &Dir : P) { |
| 4432 | if (Dir.empty()) |
| 4433 | continue; |
| 4434 | SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); |
| 4435 | llvm::sys::path::append(P, Name); |
| 4436 | if (llvm::sys::fs::exists(Twine(P))) |
Simon Pilgrim | 4588fc8 | 2018-09-13 10:10:18 +0000 | [diff] [blame] | 4437 | return P.str().str(); |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame] | 4438 | } |
| 4439 | return None; |
| 4440 | }; |
| 4441 | |
| 4442 | if (auto P = SearchPaths(PrefixDirs)) |
| 4443 | return *P; |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4444 | |
Petr Hosek | 916a467 | 2017-08-10 04:16:38 +0000 | [diff] [blame] | 4445 | SmallString<128> R(ResourceDir); |
| 4446 | llvm::sys::path::append(R, Name); |
| 4447 | if (llvm::sys::fs::exists(Twine(R))) |
| 4448 | return R.str(); |
| 4449 | |
| 4450 | SmallString<128> P(TC.getCompilerRTPath()); |
Rafael Espindola | 609a664 | 2013-06-24 18:33:43 +0000 | [diff] [blame] | 4451 | llvm::sys::path::append(P, Name); |
| 4452 | if (llvm::sys::fs::exists(Twine(P))) |
Peter Collingbourne | fa9771f | 2011-09-06 02:08:31 +0000 | [diff] [blame] | 4453 | return P.str(); |
| 4454 | |
Petr Hosek | 92d706e | 2019-05-29 00:01:05 +0000 | [diff] [blame] | 4455 | SmallString<128> D(Dir); |
| 4456 | llvm::sys::path::append(D, "..", Name); |
| 4457 | if (llvm::sys::fs::exists(Twine(D))) |
| 4458 | return D.str(); |
| 4459 | |
Petr Hosek | 8f2499f | 2018-09-12 03:26:10 +0000 | [diff] [blame] | 4460 | if (auto P = SearchPaths(TC.getLibraryPaths())) |
| 4461 | return *P; |
| 4462 | |
| 4463 | if (auto P = SearchPaths(TC.getFilePaths())) |
| 4464 | return *P; |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4465 | |
Daniel Dunbar | 1ce8153 | 2009-09-09 22:33:00 +0000 | [diff] [blame] | 4466 | return Name; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4469 | void Driver::generatePrefixedToolNames( |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4470 | StringRef Tool, const ToolChain &TC, |
Douglas Katzman | a67e50c | 2015-06-26 15:47:46 +0000 | [diff] [blame] | 4471 | SmallVectorImpl<std::string> &Names) const { |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4472 | // FIXME: Needs a better variable than TargetTriple |
| 4473 | Names.emplace_back((TargetTriple + "-" + Tool).str()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 4474 | Names.emplace_back(Tool); |
Vasileios Kalintiris | c744e12 | 2015-11-12 15:26:54 +0000 | [diff] [blame] | 4475 | |
| 4476 | // Allow the discovery of tools prefixed with LLVM's default target triple. |
Petr Hosek | dd38d93 | 2018-05-29 22:35:39 +0000 | [diff] [blame] | 4477 | std::string DefaultTargetTriple = llvm::sys::getDefaultTargetTriple(); |
| 4478 | if (DefaultTargetTriple != TargetTriple) |
| 4479 | Names.emplace_back((DefaultTargetTriple + "-" + Tool).str()); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Benjamin Kramer | 7111b91 | 2014-11-04 20:26:01 +0000 | [diff] [blame] | 4482 | static bool ScanDirForExecutable(SmallString<128> &Dir, |
| 4483 | ArrayRef<std::string> Names) { |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4484 | for (const auto &Name : Names) { |
| 4485 | llvm::sys::path::append(Dir, Name); |
| 4486 | if (llvm::sys::fs::can_execute(Twine(Dir))) |
| 4487 | return true; |
| 4488 | llvm::sys::path::remove_filename(Dir); |
| 4489 | } |
| 4490 | return false; |
| 4491 | } |
| 4492 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4493 | std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const { |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4494 | SmallVector<std::string, 2> TargetSpecificExecutables; |
| 4495 | generatePrefixedToolNames(Name, TC, TargetSpecificExecutables); |
| 4496 | |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4497 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
Logan Chien | cd679fd | 2012-10-04 08:08:56 +0000 | [diff] [blame] | 4498 | // attempting to use this prefix when looking for program paths. |
Saleem Abdulrasool | 23d99b1 | 2014-09-16 03:48:32 +0000 | [diff] [blame] | 4499 | for (const auto &PrefixDir : PrefixDirs) { |
| 4500 | if (llvm::sys::fs::is_directory(PrefixDir)) { |
| 4501 | SmallString<128> P(PrefixDir); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4502 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4503 | return P.str(); |
Simon Atanasyan | a47ba29 | 2012-10-31 14:39:28 +0000 | [diff] [blame] | 4504 | } else { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4505 | SmallString<128> P((PrefixDir + Name).str()); |
Rafael Espindola | 609a664 | 2013-06-24 18:33:43 +0000 | [diff] [blame] | 4506 | if (llvm::sys::fs::can_execute(Twine(P))) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4507 | return P.str(); |
Simon Atanasyan | 86bdab7 | 2012-10-31 12:01:53 +0000 | [diff] [blame] | 4508 | } |
Chandler Carruth | 8455924 | 2010-03-22 01:52:07 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4511 | const ToolChain::path_list &List = TC.getProgramPaths(); |
Saleem Abdulrasool | 23d99b1 | 2014-09-16 03:48:32 +0000 | [diff] [blame] | 4512 | for (const auto &Path : List) { |
| 4513 | SmallString<128> P(Path); |
Saleem Abdulrasool | f0ba6ce | 2014-10-25 23:33:21 +0000 | [diff] [blame] | 4514 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
Rafael Espindola | 8be5c05 | 2013-06-19 13:24:29 +0000 | [diff] [blame] | 4515 | return P.str(); |
Daniel Dunbar | 68b01a0 | 2009-03-18 20:26:19 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Daniel Dunbar | 76ce741 | 2009-03-23 16:15:50 +0000 | [diff] [blame] | 4518 | // If all else failed, search the path. |
Michael J. Spencer | b011d48 | 2014-11-07 21:30:32 +0000 | [diff] [blame] | 4519 | for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) |
| 4520 | if (llvm::ErrorOr<std::string> P = |
| 4521 | llvm::sys::findProgramByName(TargetSpecificExecutable)) |
Michael J. Spencer | 04162ea | 2014-11-04 01:30:55 +0000 | [diff] [blame] | 4522 | return *P; |
Daniel Dunbar | 6f66877 | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 4523 | |
Daniel Dunbar | 1ce8153 | 2009-09-09 22:33:00 +0000 | [diff] [blame] | 4524 | return Name; |
Daniel Dunbar | 5e0f6af | 2009-03-13 00:51:18 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4527 | std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const { |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4528 | SmallString<128> Path; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 4529 | std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path); |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4530 | if (EC) { |
| 4531 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
Daniel Dunbar | e627c1c | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 4532 | return ""; |
| 4533 | } |
| 4534 | |
Rafael Espindola | 37d229d | 2013-06-25 04:26:55 +0000 | [diff] [blame] | 4535 | return Path.str(); |
Daniel Dunbar | e627c1c | 2009-03-18 19:34:39 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Steven Wu | c3399db6 | 2019-01-11 21:16:04 +0000 | [diff] [blame] | 4538 | std::string Driver::GetTemporaryDirectory(StringRef Prefix) const { |
| 4539 | SmallString<128> Path; |
| 4540 | std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, Path); |
| 4541 | if (EC) { |
| 4542 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4543 | return ""; |
| 4544 | } |
| 4545 | |
| 4546 | return Path.str(); |
| 4547 | } |
| 4548 | |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4549 | std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const { |
| 4550 | SmallString<128> Output; |
| 4551 | if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) { |
| 4552 | // FIXME: If anybody needs it, implement this obscure rule: |
| 4553 | // "If you specify a directory without a file name, the default file name |
| 4554 | // is VCx0.pch., where x is the major version of Visual C++ in use." |
| 4555 | Output = FpArg->getValue(); |
| 4556 | |
| 4557 | // "If you do not specify an extension as part of the path name, an |
| 4558 | // extension of .pch is assumed. " |
| 4559 | if (!llvm::sys::path::has_extension(Output)) |
| 4560 | Output += ".pch"; |
| 4561 | } else { |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 4562 | if (Arg *YcArg = C.getArgs().getLastArg(options::OPT__SLASH_Yc)) |
| 4563 | Output = YcArg->getValue(); |
| 4564 | if (Output.empty()) |
| 4565 | Output = BaseName; |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 4566 | llvm::sys::path::replace_extension(Output, ".pch"); |
| 4567 | } |
| 4568 | return Output.str(); |
| 4569 | } |
| 4570 | |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4571 | const ToolChain &Driver::getToolChain(const ArgList &Args, |
Artem Belevich | 959e054 | 2015-07-10 19:47:55 +0000 | [diff] [blame] | 4572 | const llvm::Triple &Target) const { |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4573 | |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4574 | auto &TC = ToolChains[Target.str()]; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4575 | if (!TC) { |
| 4576 | switch (Target.getOS()) { |
Reid Kleckner | 330fb17 | 2016-05-11 16:19:05 +0000 | [diff] [blame] | 4577 | case llvm::Triple::Haiku: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4578 | TC = llvm::make_unique<toolchains::Haiku>(*this, Target, Args); |
Reid Kleckner | 330fb17 | 2016-05-11 16:19:05 +0000 | [diff] [blame] | 4579 | break; |
Ed Schouten | 4dabea2 | 2017-06-25 08:29:09 +0000 | [diff] [blame] | 4580 | case llvm::Triple::Ananas: |
| 4581 | TC = llvm::make_unique<toolchains::Ananas>(*this, Target, Args); |
| 4582 | break; |
Ed Schouten | 3c3e58c | 2015-03-26 11:13:44 +0000 | [diff] [blame] | 4583 | case llvm::Triple::CloudABI: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4584 | TC = llvm::make_unique<toolchains::CloudABI>(*this, Target, Args); |
Ed Schouten | 3c3e58c | 2015-03-26 11:13:44 +0000 | [diff] [blame] | 4585 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4586 | case llvm::Triple::Darwin: |
| 4587 | case llvm::Triple::MacOSX: |
| 4588 | case llvm::Triple::IOS: |
Tim Northover | 6f3ff22 | 2015-10-30 16:30:27 +0000 | [diff] [blame] | 4589 | case llvm::Triple::TvOS: |
| 4590 | case llvm::Triple::WatchOS: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4591 | TC = llvm::make_unique<toolchains::DarwinClang>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4592 | break; |
| 4593 | case llvm::Triple::DragonFly: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4594 | TC = llvm::make_unique<toolchains::DragonFly>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4595 | break; |
| 4596 | case llvm::Triple::OpenBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4597 | TC = llvm::make_unique<toolchains::OpenBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4598 | break; |
| 4599 | case llvm::Triple::NetBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4600 | TC = llvm::make_unique<toolchains::NetBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4601 | break; |
| 4602 | case llvm::Triple::FreeBSD: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4603 | TC = llvm::make_unique<toolchains::FreeBSD>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4604 | break; |
| 4605 | case llvm::Triple::Minix: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4606 | TC = llvm::make_unique<toolchains::Minix>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4607 | break; |
| 4608 | case llvm::Triple::Linux: |
Andrey Turetskiy | 4798eb6 | 2016-06-16 10:36:09 +0000 | [diff] [blame] | 4609 | case llvm::Triple::ELFIAMCU: |
Chandler Carruth | cf705b2 | 2012-01-25 21:03:58 +0000 | [diff] [blame] | 4610 | if (Target.getArch() == llvm::Triple::hexagon) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4611 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4612 | Args); |
Vasileios Kalintiris | c744e12 | 2015-11-12 15:26:54 +0000 | [diff] [blame] | 4613 | else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) && |
| 4614 | !Target.hasEnvironment()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4615 | TC = llvm::make_unique<toolchains::MipsLLVMToolChain>(*this, Target, |
| 4616 | Args); |
Qiu Chaofan | 19828e3 | 2019-04-23 05:50:24 +0000 | [diff] [blame] | 4617 | else if (Target.getArch() == llvm::Triple::ppc || |
| 4618 | Target.getArch() == llvm::Triple::ppc64 || |
| 4619 | Target.getArch() == llvm::Triple::ppc64le) |
| 4620 | TC = llvm::make_unique<toolchains::PPCLinuxToolChain>(*this, Target, |
| 4621 | Args); |
Chandler Carruth | cf705b2 | 2012-01-25 21:03:58 +0000 | [diff] [blame] | 4622 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4623 | TC = llvm::make_unique<toolchains::Linux>(*this, Target, Args); |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4624 | break; |
Derek Schuff | 6ab52fa | 2015-03-30 20:31:33 +0000 | [diff] [blame] | 4625 | case llvm::Triple::NaCl: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4626 | TC = llvm::make_unique<toolchains::NaClToolChain>(*this, Target, Args); |
Derek Schuff | 6ab52fa | 2015-03-30 20:31:33 +0000 | [diff] [blame] | 4627 | break; |
Petr Hosek | 62e1d23 | 2016-10-06 06:08:09 +0000 | [diff] [blame] | 4628 | case llvm::Triple::Fuchsia: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4629 | TC = llvm::make_unique<toolchains::Fuchsia>(*this, Target, Args); |
Petr Hosek | 62e1d23 | 2016-10-06 06:08:09 +0000 | [diff] [blame] | 4630 | break; |
David Chisnall | f571cde | 2012-02-15 13:39:01 +0000 | [diff] [blame] | 4631 | case llvm::Triple::Solaris: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4632 | TC = llvm::make_unique<toolchains::Solaris>(*this, Target, Args); |
David Chisnall | f571cde | 2012-02-15 13:39:01 +0000 | [diff] [blame] | 4633 | break; |
Tom Stellard | 8fa3309 | 2015-07-18 01:49:05 +0000 | [diff] [blame] | 4634 | case llvm::Triple::AMDHSA: |
Matt Arsenault | 5d0bebc | 2019-06-14 13:39:57 +0000 | [diff] [blame] | 4635 | case llvm::Triple::AMDPAL: |
| 4636 | case llvm::Triple::Mesa3D: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4637 | TC = llvm::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args); |
Tom Stellard | 8fa3309 | 2015-07-18 01:49:05 +0000 | [diff] [blame] | 4638 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4639 | case llvm::Triple::Win32: |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4640 | switch (Target.getEnvironment()) { |
| 4641 | default: |
| 4642 | if (Target.isOSBinFormatELF()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4643 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4644 | else if (Target.isOSBinFormatMachO()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4645 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4646 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4647 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4648 | break; |
| 4649 | case llvm::Triple::GNU: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4650 | TC = llvm::make_unique<toolchains::MinGW>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4651 | break; |
Saleem Abdulrasool | 543a78b | 2014-10-24 03:13:37 +0000 | [diff] [blame] | 4652 | case llvm::Triple::Itanium: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4653 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>(*this, Target, |
| 4654 | Args); |
Saleem Abdulrasool | 543a78b | 2014-10-24 03:13:37 +0000 | [diff] [blame] | 4655 | break; |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4656 | case llvm::Triple::MSVC: |
| 4657 | case llvm::Triple::UnknownEnvironment: |
Dave Lee | f96bedf | 2017-11-06 21:18:05 +0000 | [diff] [blame] | 4658 | if (Args.getLastArgValue(options::OPT_fuse_ld_EQ) |
| 4659 | .startswith_lower("bfd")) |
| 4660 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>( |
| 4661 | *this, Target, Args); |
| 4662 | else |
| 4663 | TC = |
| 4664 | llvm::make_unique<toolchains::MSVCToolChain>(*this, Target, Args); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 4665 | break; |
| 4666 | } |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4667 | break; |
Filipe Cabecinhas | c888e19 | 2015-10-14 12:25:43 +0000 | [diff] [blame] | 4668 | case llvm::Triple::PS4: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4669 | TC = llvm::make_unique<toolchains::PS4CPU>(*this, Target, Args); |
Filipe Cabecinhas | c888e19 | 2015-10-14 12:25:43 +0000 | [diff] [blame] | 4670 | break; |
David L Kreitzer | d397ea4 | 2016-10-14 20:44:33 +0000 | [diff] [blame] | 4671 | case llvm::Triple::Contiki: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4672 | TC = llvm::make_unique<toolchains::Contiki>(*this, Target, Args); |
David L Kreitzer | d397ea4 | 2016-10-14 20:44:33 +0000 | [diff] [blame] | 4673 | break; |
Kristina Brooks | 77a4adc | 2018-11-29 03:49:14 +0000 | [diff] [blame] | 4674 | case llvm::Triple::Hurd: |
| 4675 | TC = llvm::make_unique<toolchains::Hurd>(*this, Target, Args); |
| 4676 | break; |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4677 | default: |
Douglas Katzman | 9f5e70e | 2015-05-26 18:01:33 +0000 | [diff] [blame] | 4678 | // Of these targets, Hexagon is the only one that might have |
| 4679 | // an OS of Linux, in which case it got handled above already. |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4680 | switch (Target.getArch()) { |
| 4681 | case llvm::Triple::tce: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4682 | TC = llvm::make_unique<toolchains::TCEToolChain>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4683 | break; |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 4684 | case llvm::Triple::tcele: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4685 | TC = llvm::make_unique<toolchains::TCELEToolChain>(*this, Target, Args); |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 4686 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4687 | case llvm::Triple::hexagon: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4688 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4689 | Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4690 | break; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 4691 | case llvm::Triple::lanai: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4692 | TC = llvm::make_unique<toolchains::LanaiToolChain>(*this, Target, Args); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 4693 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4694 | case llvm::Triple::xcore: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4695 | TC = llvm::make_unique<toolchains::XCoreToolChain>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4696 | break; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 4697 | case llvm::Triple::wasm32: |
| 4698 | case llvm::Triple::wasm64: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4699 | TC = llvm::make_unique<toolchains::WebAssembly>(*this, Target, Args); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 4700 | break; |
Dylan McKay | 924fa3a | 2017-01-05 05:20:27 +0000 | [diff] [blame] | 4701 | case llvm::Triple::avr: |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4702 | TC = llvm::make_unique<toolchains::AVRToolChain>(*this, Target, Args); |
Dylan McKay | 924fa3a | 2017-01-05 05:20:27 +0000 | [diff] [blame] | 4703 | break; |
Anton Korobeynikov | 93165d6 | 2019-01-15 19:44:05 +0000 | [diff] [blame] | 4704 | case llvm::Triple::msp430: |
| 4705 | TC = |
| 4706 | llvm::make_unique<toolchains::MSP430ToolChain>(*this, Target, Args); |
| 4707 | break; |
David Bolvansky | f4be253 | 2018-07-31 14:21:46 +0000 | [diff] [blame] | 4708 | case llvm::Triple::riscv32: |
| 4709 | case llvm::Triple::riscv64: |
| 4710 | TC = llvm::make_unique<toolchains::RISCVToolChain>(*this, Target, Args); |
| 4711 | break; |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4712 | default: |
Douglas Katzman | d6e597c | 2015-09-17 19:56:40 +0000 | [diff] [blame] | 4713 | if (Target.getVendor() == llvm::Triple::Myriad) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4714 | TC = llvm::make_unique<toolchains::MyriadToolChain>(*this, Target, |
| 4715 | Args); |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 4716 | else if (toolchains::BareMetal::handlesTarget(Target)) |
| 4717 | TC = llvm::make_unique<toolchains::BareMetal>(*this, Target, Args); |
Douglas Katzman | d6e597c | 2015-09-17 19:56:40 +0000 | [diff] [blame] | 4718 | else if (Target.isOSBinFormatELF()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4719 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4720 | else if (Target.isOSBinFormatMachO()) |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4721 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4722 | else |
David Blaikie | 6ad7101 | 2017-01-13 18:53:43 +0000 | [diff] [blame] | 4723 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
Douglas Katzman | 15a63ed | 2015-08-12 18:36:12 +0000 | [diff] [blame] | 4724 | } |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4725 | } |
| 4726 | } |
Justin Lebar | 66c4fd7 | 2016-11-18 00:41:22 +0000 | [diff] [blame] | 4727 | |
| 4728 | // Intentionally omitted from the switch above: llvm::Triple::CUDA. CUDA |
| 4729 | // compiles always need two toolchains, the CUDA toolchain and the host |
| 4730 | // toolchain. So the only valid way to create a CUDA toolchain is via |
| 4731 | // CreateOffloadingDeviceToolChains. |
| 4732 | |
Chandler Carruth | 2ad5de1 | 2012-01-25 11:01:57 +0000 | [diff] [blame] | 4733 | return *TC; |
Daniel Dunbar | 4dff6a4 | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 4734 | } |
Daniel Dunbar | 8fa879d | 2009-03-24 18:57:02 +0000 | [diff] [blame] | 4735 | |
Rafael Espindola | 2f69d40 | 2013-03-18 15:33:26 +0000 | [diff] [blame] | 4736 | bool Driver::ShouldUseClangCompiler(const JobAction &JA) const { |
Douglas Katzman | 0024909 | 2015-06-12 15:45:21 +0000 | [diff] [blame] | 4737 | // 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] | 4738 | if (JA.size() != 1 || |
| 4739 | !types::isAcceptedByClang((*JA.input_begin())->getType())) |
Nick Lewycky | 5cc9ebb | 2012-11-15 05:36:36 +0000 | [diff] [blame] | 4740 | return false; |
| 4741 | |
Douglas Katzman | 0024909 | 2015-06-12 15:45:21 +0000 | [diff] [blame] | 4742 | // 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] | 4743 | if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) && |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 4744 | !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA)) |
Nick Lewycky | 5cc9ebb | 2012-11-15 05:36:36 +0000 | [diff] [blame] | 4745 | return false; |
| 4746 | |
| 4747 | return true; |
| 4748 | } |
| 4749 | |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4750 | /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the |
| 4751 | /// 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] | 4752 | /// |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4753 | /// \return True if the entire string was parsed (9.2), or all groups were |
| 4754 | /// parsed (10.3.5extrastuff). |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4755 | bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor, |
| 4756 | unsigned &Micro, bool &HadExtra) { |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4757 | HadExtra = false; |
| 4758 | |
| 4759 | Major = Minor = Micro = 0; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4760 | if (Str.empty()) |
Bob Wilson | 433cb31 | 2015-04-07 01:03:35 +0000 | [diff] [blame] | 4761 | return false; |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4762 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4763 | if (Str.consumeInteger(10, Major)) |
| 4764 | return false; |
| 4765 | if (Str.empty()) |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4766 | return true; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4767 | if (Str[0] != '.') |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4768 | return false; |
Daniel Dunbar | f26a7ab | 2009-09-08 23:36:43 +0000 | [diff] [blame] | 4769 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4770 | Str = Str.drop_front(1); |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4771 | |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4772 | if (Str.consumeInteger(10, Minor)) |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4773 | return false; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4774 | if (Str.empty()) |
| 4775 | return true; |
| 4776 | if (Str[0] != '.') |
| 4777 | return false; |
| 4778 | Str = Str.drop_front(1); |
| 4779 | |
| 4780 | if (Str.consumeInteger(10, Micro)) |
| 4781 | return false; |
| 4782 | if (!Str.empty()) |
| 4783 | HadExtra = true; |
Daniel Dunbar | c7fd57a | 2009-03-26 15:58:36 +0000 | [diff] [blame] | 4784 | return true; |
| 4785 | } |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4786 | |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4787 | /// Parse digits from a string \p Str and fulfill \p Digits with |
| 4788 | /// the parsed numbers. This method assumes that the max number of |
| 4789 | /// digits to look for is equal to Digits.size(). |
| 4790 | /// |
| 4791 | /// \return True if the entire string was parsed and there are |
| 4792 | /// no extra characters remaining at the end. |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4793 | bool Driver::GetReleaseVersion(StringRef Str, |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4794 | MutableArrayRef<unsigned> Digits) { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4795 | if (Str.empty()) |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4796 | return false; |
| 4797 | |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4798 | unsigned CurDigit = 0; |
| 4799 | while (CurDigit < Digits.size()) { |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4800 | unsigned Digit; |
| 4801 | if (Str.consumeInteger(10, Digit)) |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4802 | return false; |
Mehdi Amini | 1201117 | 2016-10-06 05:11:48 +0000 | [diff] [blame] | 4803 | Digits[CurDigit] = Digit; |
| 4804 | if (Str.empty()) |
| 4805 | return true; |
| 4806 | if (Str[0] != '.') |
| 4807 | return false; |
| 4808 | Str = Str.drop_front(1); |
Bruno Cardoso Lopes | 8ed5cac | 2016-03-31 02:45:46 +0000 | [diff] [blame] | 4809 | CurDigit++; |
| 4810 | } |
| 4811 | |
| 4812 | // More digits than requested, bail out... |
| 4813 | return false; |
| 4814 | } |
| 4815 | |
Simon Atanasyan | 27c0f20 | 2019-04-02 18:03:15 +0000 | [diff] [blame] | 4816 | std::pair<unsigned, unsigned> |
| 4817 | Driver::getIncludeExcludeOptionFlagMasks(bool IsClCompatMode) const { |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4818 | unsigned IncludedFlagsBitmask = 0; |
Rafael Espindola | cc707bc | 2013-09-25 15:54:41 +0000 | [diff] [blame] | 4819 | unsigned ExcludedFlagsBitmask = options::NoDriverOption; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4820 | |
Hans Wennborg | 797004d | 2018-11-08 11:27:04 +0000 | [diff] [blame] | 4821 | if (IsClCompatMode) { |
Hans Wennborg | 1907610 | 2013-07-31 20:51:53 +0000 | [diff] [blame] | 4822 | // Include CL and Core options. |
| 4823 | IncludedFlagsBitmask |= options::CLOption; |
| 4824 | IncludedFlagsBitmask |= options::CoreOption; |
Hans Wennborg | 6ddc690 | 2013-07-27 00:23:45 +0000 | [diff] [blame] | 4825 | } else { |
| 4826 | ExcludedFlagsBitmask |= options::CLOption; |
| 4827 | } |
| 4828 | |
| 4829 | return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask); |
| 4830 | } |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 4831 | |
Douglas Katzman | f08fadf | 2015-06-04 14:40:44 +0000 | [diff] [blame] | 4832 | bool clang::driver::isOptimizationLevelFast(const ArgList &Args) { |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 4833 | return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false); |
| 4834 | } |