Nick Lewycky | 6da9077 | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- ToolChain.cpp - Collections of tools for one platform ------------===// |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 10 | #include "clang/Driver/ToolChain.h" |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 11 | #include "ToolChains/CommonArgs.h" |
| 12 | #include "ToolChains/Arch/ARM.h" |
| 13 | #include "ToolChains/Clang.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Basic/ObjCRuntime.h" |
Pirama Arumuga Nainar | 65a16dd | 2017-03-03 23:20:49 +0000 | [diff] [blame] | 15 | #include "clang/Basic/VirtualFileSystem.h" |
Jonas Hahnfeld | aae8374 | 2016-02-12 07:48:37 +0000 | [diff] [blame] | 16 | #include "clang/Config/config.h" |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Action.h" |
| 18 | #include "clang/Driver/Driver.h" |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 19 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Options.h" |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 21 | #include "clang/Driver/SanitizerArgs.h" |
Logan Chien | eb9162f | 2014-06-26 14:23:45 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 23 | #include "llvm/Option/Arg.h" |
| 24 | #include "llvm/Option/ArgList.h" |
| 25 | #include "llvm/Option/Option.h" |
John McCall | 24fc0de | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Simon Atanasyan | 08450bd | 2013-04-20 08:15:03 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Path.h" |
Alexandros Lamprineas | 89ea433 | 2015-10-28 10:10:03 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetParser.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetRegistry.h" |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 31 | |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 32 | using namespace clang::driver; |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 33 | using namespace clang::driver::tools; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 34 | using namespace clang; |
Alexandros Lamprineas | 89ea433 | 2015-10-28 10:10:03 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 36 | using namespace llvm::opt; |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 37 | |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 38 | static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) { |
| 39 | return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext, |
| 40 | options::OPT_fno_rtti, options::OPT_frtti); |
| 41 | } |
| 42 | |
| 43 | static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args, |
| 44 | const llvm::Triple &Triple, |
| 45 | const Arg *CachedRTTIArg) { |
| 46 | // Explicit rtti/no-rtti args |
| 47 | if (CachedRTTIArg) { |
| 48 | if (CachedRTTIArg->getOption().matches(options::OPT_frtti)) |
| 49 | return ToolChain::RM_EnabledExplicitly; |
| 50 | else |
| 51 | return ToolChain::RM_DisabledExplicitly; |
| 52 | } |
| 53 | |
| 54 | // -frtti is default, except for the PS4 CPU. |
| 55 | if (!Triple.isPS4CPU()) |
| 56 | return ToolChain::RM_EnabledImplicitly; |
| 57 | |
| 58 | // On the PS4, turning on c++ exceptions turns on rtti. |
| 59 | // We're assuming that, if we see -fexceptions, rtti gets turned on. |
Filipe Cabecinhas | 3e707d9 | 2015-03-20 23:33:23 +0000 | [diff] [blame] | 60 | Arg *Exceptions = Args.getLastArgNoClaim( |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 61 | options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, |
| 62 | options::OPT_fexceptions, options::OPT_fno_exceptions); |
| 63 | if (Exceptions && |
| 64 | (Exceptions->getOption().matches(options::OPT_fexceptions) || |
| 65 | Exceptions->getOption().matches(options::OPT_fcxx_exceptions))) |
| 66 | return ToolChain::RM_EnabledImplicitly; |
| 67 | |
| 68 | return ToolChain::RM_DisabledImplicitly; |
| 69 | } |
| 70 | |
Rafael Espindola | 84b588b | 2013-03-18 18:10:27 +0000 | [diff] [blame] | 71 | ToolChain::ToolChain(const Driver &D, const llvm::Triple &T, |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 72 | const ArgList &Args) |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 73 | : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)), |
Vedant Kumar | 18286cf | 2016-07-27 23:02:20 +0000 | [diff] [blame] | 74 | CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)), |
| 75 | EffectiveTriple() { |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 76 | if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) |
| 77 | if (!isThreadModelSupported(A->getValue())) |
| 78 | D.Diag(diag::err_drv_invalid_thread_model_for_target) |
Filipe Cabecinhas | ec5d0e6 | 2015-02-19 01:04:49 +0000 | [diff] [blame] | 79 | << A->getValue() << A->getAsString(Args); |
Pirama Arumuga Nainar | 65a16dd | 2017-03-03 23:20:49 +0000 | [diff] [blame] | 80 | |
| 81 | std::string CandidateLibPath = getArchSpecificLibPath(); |
| 82 | if (getVFS().exists(CandidateLibPath)) |
| 83 | getFilePaths().push_back(CandidateLibPath); |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 86 | ToolChain::~ToolChain() { |
| 87 | } |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 88 | |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 89 | vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); } |
Daniel Dunbar | 083edf7 | 2009-12-21 18:54:17 +0000 | [diff] [blame] | 90 | |
Rafael Espindola | 84b588b | 2013-03-18 18:10:27 +0000 | [diff] [blame] | 91 | bool ToolChain::useIntegratedAs() const { |
Saleem Abdulrasool | cfeb90d | 2014-02-23 00:40:30 +0000 | [diff] [blame] | 92 | return Args.hasFlag(options::OPT_fintegrated_as, |
| 93 | options::OPT_fno_integrated_as, |
Rafael Espindola | 248e219 | 2013-03-18 17:52:57 +0000 | [diff] [blame] | 94 | IsIntegratedAssemblerDefault()); |
| 95 | } |
| 96 | |
Alexey Samsonov | 609213f9 | 2013-08-19 09:14:21 +0000 | [diff] [blame] | 97 | const SanitizerArgs& ToolChain::getSanitizerArgs() const { |
Peter Collingbourne | 3270164 | 2013-11-01 18:16:25 +0000 | [diff] [blame] | 98 | if (!SanitizerArguments.get()) |
| 99 | SanitizerArguments.reset(new SanitizerArgs(*this, Args)); |
| 100 | return *SanitizerArguments.get(); |
Alexey Samsonov | 609213f9 | 2013-08-19 09:14:21 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Eric Christopher | 74a7c5d | 2015-09-25 17:44:31 +0000 | [diff] [blame] | 103 | namespace { |
| 104 | struct DriverSuffix { |
| 105 | const char *Suffix; |
| 106 | const char *ModeFlag; |
| 107 | }; |
| 108 | |
| 109 | const DriverSuffix *FindDriverSuffix(StringRef ProgName) { |
| 110 | // A list of known driver suffixes. Suffixes are compared against the |
| 111 | // program name in order. If there is a match, the frontend type is updated as |
| 112 | // necessary by applying the ModeFlag. |
| 113 | static const DriverSuffix DriverSuffixes[] = { |
| 114 | {"clang", nullptr}, |
| 115 | {"clang++", "--driver-mode=g++"}, |
| 116 | {"clang-c++", "--driver-mode=g++"}, |
| 117 | {"clang-cc", nullptr}, |
| 118 | {"clang-cpp", "--driver-mode=cpp"}, |
| 119 | {"clang-g++", "--driver-mode=g++"}, |
| 120 | {"clang-gcc", nullptr}, |
| 121 | {"clang-cl", "--driver-mode=cl"}, |
| 122 | {"cc", nullptr}, |
| 123 | {"cpp", "--driver-mode=cpp"}, |
| 124 | {"cl", "--driver-mode=cl"}, |
| 125 | {"++", "--driver-mode=g++"}, |
| 126 | }; |
| 127 | |
| 128 | for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i) |
| 129 | if (ProgName.endswith(DriverSuffixes[i].Suffix)) |
| 130 | return &DriverSuffixes[i]; |
| 131 | return nullptr; |
| 132 | } |
| 133 | |
| 134 | /// Normalize the program name from argv[0] by stripping the file extension if |
| 135 | /// present and lower-casing the string on Windows. |
| 136 | std::string normalizeProgramName(llvm::StringRef Argv0) { |
| 137 | std::string ProgName = llvm::sys::path::stem(Argv0); |
| 138 | #ifdef LLVM_ON_WIN32 |
| 139 | // Transform to lowercase for case insensitive file systems. |
| 140 | std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower); |
| 141 | #endif |
| 142 | return ProgName; |
| 143 | } |
| 144 | |
| 145 | const DriverSuffix *parseDriverSuffix(StringRef ProgName) { |
| 146 | // Try to infer frontend type and default target from the program name by |
| 147 | // comparing it against DriverSuffixes in order. |
| 148 | |
| 149 | // If there is a match, the function tries to identify a target as prefix. |
| 150 | // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target |
| 151 | // prefix "x86_64-linux". If such a target prefix is found, it may be |
| 152 | // added via -target as implicit first argument. |
| 153 | const DriverSuffix *DS = FindDriverSuffix(ProgName); |
| 154 | |
| 155 | if (!DS) { |
| 156 | // Try again after stripping any trailing version number: |
| 157 | // clang++3.5 -> clang++ |
| 158 | ProgName = ProgName.rtrim("0123456789."); |
| 159 | DS = FindDriverSuffix(ProgName); |
| 160 | } |
| 161 | |
| 162 | if (!DS) { |
| 163 | // Try again after stripping trailing -component. |
| 164 | // clang++-tot -> clang++ |
| 165 | ProgName = ProgName.slice(0, ProgName.rfind('-')); |
| 166 | DS = FindDriverSuffix(ProgName); |
| 167 | } |
| 168 | return DS; |
| 169 | } |
| 170 | } // anonymous namespace |
| 171 | |
| 172 | std::pair<std::string, std::string> |
| 173 | ToolChain::getTargetAndModeFromProgramName(StringRef PN) { |
| 174 | std::string ProgName = normalizeProgramName(PN); |
| 175 | const DriverSuffix *DS = parseDriverSuffix(ProgName); |
| 176 | if (!DS) |
| 177 | return std::make_pair("", ""); |
| 178 | std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag; |
| 179 | |
| 180 | std::string::size_type LastComponent = |
| 181 | ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix)); |
| 182 | if (LastComponent == std::string::npos) |
| 183 | return std::make_pair("", ModeFlag); |
| 184 | |
| 185 | // Infer target from the prefix. |
| 186 | StringRef Prefix(ProgName); |
| 187 | Prefix = Prefix.slice(0, LastComponent); |
| 188 | std::string IgnoredError; |
| 189 | std::string Target; |
| 190 | if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) { |
| 191 | Target = Prefix; |
| 192 | } |
| 193 | return std::make_pair(Target, ModeFlag); |
| 194 | } |
| 195 | |
Rafael Espindola | 42db11e | 2014-07-25 19:22:51 +0000 | [diff] [blame] | 196 | StringRef ToolChain::getDefaultUniversalArchName() const { |
Daniel Dunbar | c3bd9f5 | 2012-11-08 03:38:26 +0000 | [diff] [blame] | 197 | // In universal driver terms, the arch name accepted by -arch isn't exactly |
| 198 | // the same as the ones that appear in the triple. Roughly speaking, this is |
| 199 | // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the |
| 200 | // only interesting special case is powerpc. |
| 201 | switch (Triple.getArch()) { |
| 202 | case llvm::Triple::ppc: |
| 203 | return "ppc"; |
| 204 | case llvm::Triple::ppc64: |
| 205 | return "ppc64"; |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 206 | case llvm::Triple::ppc64le: |
| 207 | return "ppc64le"; |
Daniel Dunbar | c3bd9f5 | 2012-11-08 03:38:26 +0000 | [diff] [blame] | 208 | default: |
| 209 | return Triple.getArchName(); |
| 210 | } |
| 211 | } |
| 212 | |
Rafael Espindola | 151a957 | 2012-09-23 03:05:41 +0000 | [diff] [blame] | 213 | bool ToolChain::IsUnwindTablesDefault() const { |
| 214 | return false; |
| 215 | } |
| 216 | |
Rafael Espindola | 7cf3221 | 2013-03-20 03:05:54 +0000 | [diff] [blame] | 217 | Tool *ToolChain::getClang() const { |
| 218 | if (!Clang) |
| 219 | Clang.reset(new tools::Clang(*this)); |
| 220 | return Clang.get(); |
| 221 | } |
| 222 | |
| 223 | Tool *ToolChain::buildAssembler() const { |
| 224 | return new tools::ClangAs(*this); |
| 225 | } |
| 226 | |
| 227 | Tool *ToolChain::buildLinker() const { |
| 228 | llvm_unreachable("Linking is not supported by this toolchain"); |
| 229 | } |
| 230 | |
| 231 | Tool *ToolChain::getAssemble() const { |
| 232 | if (!Assemble) |
| 233 | Assemble.reset(buildAssembler()); |
| 234 | return Assemble.get(); |
| 235 | } |
| 236 | |
| 237 | Tool *ToolChain::getClangAs() const { |
| 238 | if (!Assemble) |
| 239 | Assemble.reset(new tools::ClangAs(*this)); |
| 240 | return Assemble.get(); |
| 241 | } |
| 242 | |
| 243 | Tool *ToolChain::getLink() const { |
| 244 | if (!Link) |
| 245 | Link.reset(buildLinker()); |
| 246 | return Link.get(); |
| 247 | } |
| 248 | |
Samuel Antao | 7cab8f1 | 2016-10-27 18:04:42 +0000 | [diff] [blame] | 249 | Tool *ToolChain::getOffloadBundler() const { |
| 250 | if (!OffloadBundler) |
| 251 | OffloadBundler.reset(new tools::OffloadBundler(*this)); |
| 252 | return OffloadBundler.get(); |
| 253 | } |
| 254 | |
Rafael Espindola | 7cf3221 | 2013-03-20 03:05:54 +0000 | [diff] [blame] | 255 | Tool *ToolChain::getTool(Action::ActionClass AC) const { |
Rafael Espindola | d15a891 | 2013-03-19 00:36:57 +0000 | [diff] [blame] | 256 | switch (AC) { |
Rafael Espindola | 7cf3221 | 2013-03-20 03:05:54 +0000 | [diff] [blame] | 257 | case Action::AssembleJobClass: |
| 258 | return getAssemble(); |
| 259 | |
| 260 | case Action::LinkJobClass: |
| 261 | return getLink(); |
| 262 | |
Rafael Espindola | d15a891 | 2013-03-19 00:36:57 +0000 | [diff] [blame] | 263 | case Action::InputClass: |
| 264 | case Action::BindArchClass: |
Samuel Antao | d06239d | 2016-07-15 23:13:27 +0000 | [diff] [blame] | 265 | case Action::OffloadClass: |
Rafael Espindola | d15a891 | 2013-03-19 00:36:57 +0000 | [diff] [blame] | 266 | case Action::LipoJobClass: |
| 267 | case Action::DsymutilJobClass: |
Ben Langmuir | 9b9a8d3 | 2014-02-06 18:53:25 +0000 | [diff] [blame] | 268 | case Action::VerifyDebugInfoJobClass: |
Rafael Espindola | d15a891 | 2013-03-19 00:36:57 +0000 | [diff] [blame] | 269 | llvm_unreachable("Invalid tool kind."); |
| 270 | |
| 271 | case Action::CompileJobClass: |
| 272 | case Action::PrecompileJobClass: |
| 273 | case Action::PreprocessJobClass: |
| 274 | case Action::AnalyzeJobClass: |
| 275 | case Action::MigrateJobClass: |
Ben Langmuir | 9b9a8d3 | 2014-02-06 18:53:25 +0000 | [diff] [blame] | 276 | case Action::VerifyPCHJobClass: |
Bob Wilson | 23a55f1 | 2014-12-21 07:00:00 +0000 | [diff] [blame] | 277 | case Action::BackendJobClass: |
Rafael Espindola | 7cf3221 | 2013-03-20 03:05:54 +0000 | [diff] [blame] | 278 | return getClang(); |
Samuel Antao | 69d6f31 | 2016-10-27 17:50:43 +0000 | [diff] [blame] | 279 | |
| 280 | case Action::OffloadBundlingJobClass: |
Samuel Antao | fab4f37 | 2016-10-27 18:00:51 +0000 | [diff] [blame] | 281 | case Action::OffloadUnbundlingJobClass: |
Samuel Antao | 7cab8f1 | 2016-10-27 18:04:42 +0000 | [diff] [blame] | 282 | return getOffloadBundler(); |
Rafael Espindola | d15a891 | 2013-03-19 00:36:57 +0000 | [diff] [blame] | 283 | } |
Benjamin Kramer | 5fbe262 | 2013-03-21 19:45:46 +0000 | [diff] [blame] | 284 | |
| 285 | llvm_unreachable("Invalid tool kind."); |
Rafael Espindola | d15a891 | 2013-03-19 00:36:57 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 288 | static StringRef getArchNameForCompilerRTLib(const ToolChain &TC, |
| 289 | const ArgList &Args) { |
| 290 | const llvm::Triple &Triple = TC.getTriple(); |
| 291 | bool IsWindows = Triple.isOSWindows(); |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 292 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 293 | if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86) |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 294 | return "i386"; |
| 295 | |
| 296 | if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb) |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 297 | return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows) |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 298 | ? "armhf" |
| 299 | : "arm"; |
| 300 | |
| 301 | return TC.getArchName(); |
| 302 | } |
| 303 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 304 | std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component, |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 305 | bool Shared) const { |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 306 | const llvm::Triple &TT = getTriple(); |
| 307 | const char *Env = TT.isAndroid() ? "-android" : ""; |
Saleem Abdulrasool | 667d630 | 2016-08-31 19:27:07 +0000 | [diff] [blame] | 308 | bool IsITANMSVCWindows = |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 309 | TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment(); |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 310 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 311 | StringRef Arch = getArchNameForCompilerRTLib(*this, Args); |
Saleem Abdulrasool | 667d630 | 2016-08-31 19:27:07 +0000 | [diff] [blame] | 312 | const char *Prefix = IsITANMSVCWindows ? "" : "lib"; |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 313 | const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so") |
Saleem Abdulrasool | 667d630 | 2016-08-31 19:27:07 +0000 | [diff] [blame] | 314 | : (IsITANMSVCWindows ? ".lib" : ".a"); |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 315 | |
| 316 | SmallString<128> Path(getDriver().ResourceDir); |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 317 | StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS(); |
Vasileios Kalintiris | 447e357 | 2015-10-01 16:54:58 +0000 | [diff] [blame] | 318 | llvm::sys::path::append(Path, "lib", OSLibName); |
| 319 | llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" + |
| 320 | Arch + Env + Suffix); |
| 321 | return Path.str(); |
| 322 | } |
| 323 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 324 | const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args, |
| 325 | StringRef Component, |
| 326 | bool Shared) const { |
| 327 | return Args.MakeArgString(getCompilerRT(Args, Component, Shared)); |
Xinliang David Li | 69306c0 | 2015-10-22 06:15:31 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Pirama Arumuga Nainar | 65a16dd | 2017-03-03 23:20:49 +0000 | [diff] [blame] | 330 | std::string ToolChain::getArchSpecificLibPath() const { |
| 331 | SmallString<128> Path(getDriver().ResourceDir); |
| 332 | StringRef OSLibName = getTriple().isOSFreeBSD() ? "freebsd" : getOS(); |
| 333 | llvm::sys::path::append(Path, "lib", OSLibName, |
| 334 | llvm::Triple::getArchTypeName(getArch())); |
| 335 | return Path.str(); |
| 336 | } |
| 337 | |
Xinliang David Li | 69306c0 | 2015-10-22 06:15:31 +0000 | [diff] [blame] | 338 | bool ToolChain::needsProfileRT(const ArgList &Args) { |
| 339 | if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, |
| 340 | false) || |
| 341 | Args.hasArg(options::OPT_fprofile_generate) || |
| 342 | Args.hasArg(options::OPT_fprofile_generate_EQ) || |
| 343 | Args.hasArg(options::OPT_fprofile_instr_generate) || |
| 344 | Args.hasArg(options::OPT_fprofile_instr_generate_EQ) || |
| 345 | Args.hasArg(options::OPT_fcreate_profile) || |
| 346 | Args.hasArg(options::OPT_coverage)) |
| 347 | return true; |
| 348 | |
| 349 | return false; |
| 350 | } |
| 351 | |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 352 | Tool *ToolChain::SelectTool(const JobAction &JA) const { |
Xinliang David Li | 69306c0 | 2015-10-22 06:15:31 +0000 | [diff] [blame] | 353 | if (getDriver().ShouldUseClangCompiler(JA)) return getClang(); |
Rafael Espindola | 7cf3221 | 2013-03-20 03:05:54 +0000 | [diff] [blame] | 354 | Action::ActionClass AC = JA.getKind(); |
| 355 | if (AC == Action::AssembleJobClass && useIntegratedAs()) |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 356 | return getClangAs(); |
| 357 | return getTool(AC); |
Rafael Espindola | 260e28d | 2013-03-18 20:48:54 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Daniel Dunbar | 9c3ed5f | 2010-07-14 18:46:23 +0000 | [diff] [blame] | 360 | std::string ToolChain::GetFilePath(const char *Name) const { |
Chandler Carruth | b65b111 | 2012-01-25 09:12:06 +0000 | [diff] [blame] | 361 | return D.GetFilePath(Name, *this); |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Simon Atanasyan | b16488c | 2012-10-03 19:52:37 +0000 | [diff] [blame] | 364 | std::string ToolChain::GetProgramPath(const char *Name) const { |
| 365 | return D.GetProgramPath(Name, *this); |
Daniel Dunbar | 9e2136d | 2009-03-16 05:25:36 +0000 | [diff] [blame] | 366 | } |
Daniel Dunbar | cc7df6c | 2010-08-02 05:43:56 +0000 | [diff] [blame] | 367 | |
Logan Chien | eb9162f | 2014-06-26 14:23:45 +0000 | [diff] [blame] | 368 | std::string ToolChain::GetLinkerPath() const { |
Petr Hosek | fe2c2b0 | 2016-12-14 16:46:50 +0000 | [diff] [blame] | 369 | const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ); |
| 370 | StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER; |
Logan Chien | eb9162f | 2014-06-26 14:23:45 +0000 | [diff] [blame] | 371 | |
Petr Hosek | fe2c2b0 | 2016-12-14 16:46:50 +0000 | [diff] [blame] | 372 | if (llvm::sys::path::is_absolute(UseLinker)) { |
| 373 | // If we're passed what looks like an absolute path, don't attempt to |
| 374 | // second-guess that. |
| 375 | if (llvm::sys::fs::exists(UseLinker)) |
| 376 | return UseLinker; |
| 377 | } else if (UseLinker.empty() || UseLinker == "ld") { |
| 378 | // If we're passed -fuse-ld= with no argument, or with the argument ld, |
| 379 | // then use whatever the default system linker is. |
| 380 | return GetProgramPath(getDefaultLinker()); |
| 381 | } else { |
| 382 | llvm::SmallString<8> LinkerName("ld."); |
| 383 | LinkerName.append(UseLinker); |
Logan Chien | eb9162f | 2014-06-26 14:23:45 +0000 | [diff] [blame] | 384 | |
Petr Hosek | fe2c2b0 | 2016-12-14 16:46:50 +0000 | [diff] [blame] | 385 | std::string LinkerPath(GetProgramPath(LinkerName.c_str())); |
| 386 | if (llvm::sys::fs::exists(LinkerPath)) |
| 387 | return LinkerPath; |
Logan Chien | eb9162f | 2014-06-26 14:23:45 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Petr Hosek | fe2c2b0 | 2016-12-14 16:46:50 +0000 | [diff] [blame] | 390 | if (A) |
| 391 | getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); |
| 392 | |
| 393 | return GetProgramPath(getDefaultLinker()); |
Logan Chien | eb9162f | 2014-06-26 14:23:45 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Mehdi Amini | 6fcd4eb | 2016-10-07 21:41:00 +0000 | [diff] [blame] | 396 | types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const { |
Daniel Dunbar | cc7df6c | 2010-08-02 05:43:56 +0000 | [diff] [blame] | 397 | return types::lookupTypeForExtension(Ext); |
| 398 | } |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 399 | |
Daniel Dunbar | 62123a1 | 2010-09-17 00:24:52 +0000 | [diff] [blame] | 400 | bool ToolChain::HasNativeLLVMSupport() const { |
| 401 | return false; |
| 402 | } |
| 403 | |
Richard Barton | 5828d7b | 2013-12-17 11:11:25 +0000 | [diff] [blame] | 404 | bool ToolChain::isCrossCompiling() const { |
| 405 | llvm::Triple HostTriple(LLVM_HOST_TRIPLE); |
| 406 | switch (HostTriple.getArch()) { |
Alp Toker | b164a34 | 2013-12-17 17:25:19 +0000 | [diff] [blame] | 407 | // The A32/T32/T16 instruction sets are not separate architectures in this |
| 408 | // context. |
| 409 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 410 | case llvm::Triple::armeb: |
Alp Toker | b164a34 | 2013-12-17 17:25:19 +0000 | [diff] [blame] | 411 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 412 | case llvm::Triple::thumbeb: |
| 413 | return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && |
| 414 | getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; |
Alp Toker | b164a34 | 2013-12-17 17:25:19 +0000 | [diff] [blame] | 415 | default: |
| 416 | return HostTriple.getArch() != getArch(); |
Richard Barton | 5828d7b | 2013-12-17 11:11:25 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 420 | ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 421 | return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 422 | VersionTuple()); |
John McCall | 24fc0de | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 425 | bool ToolChain::isThreadModelSupported(const StringRef Model) const { |
| 426 | if (Model == "single") { |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 427 | // FIXME: 'single' is only supported on ARM and WebAssembly so far. |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 428 | return Triple.getArch() == llvm::Triple::arm || |
| 429 | Triple.getArch() == llvm::Triple::armeb || |
| 430 | Triple.getArch() == llvm::Triple::thumb || |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 431 | Triple.getArch() == llvm::Triple::thumbeb || |
| 432 | Triple.getArch() == llvm::Triple::wasm32 || |
| 433 | Triple.getArch() == llvm::Triple::wasm64; |
Jonathan Roelofs | b140a10 | 2014-10-03 21:57:44 +0000 | [diff] [blame] | 434 | } else if (Model == "posix") |
| 435 | return true; |
| 436 | |
| 437 | return false; |
| 438 | } |
| 439 | |
Jim Grosbach | 82eee26 | 2013-11-16 00:53:35 +0000 | [diff] [blame] | 440 | std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, |
Chad Rosier | d3a0f95 | 2011-09-20 20:44:06 +0000 | [diff] [blame] | 441 | types::ID InputType) const { |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 442 | switch (getTriple().getArch()) { |
| 443 | default: |
| 444 | return getTripleString(); |
| 445 | |
Jim Grosbach | 82eee26 | 2013-11-16 00:53:35 +0000 | [diff] [blame] | 446 | case llvm::Triple::x86_64: { |
| 447 | llvm::Triple Triple = getTriple(); |
Tim Northover | 157d911 | 2014-01-16 08:48:16 +0000 | [diff] [blame] | 448 | if (!Triple.isOSBinFormatMachO()) |
Jim Grosbach | 82eee26 | 2013-11-16 00:53:35 +0000 | [diff] [blame] | 449 | return getTripleString(); |
| 450 | |
| 451 | if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { |
| 452 | // x86_64h goes in the triple. Other -march options just use the |
| 453 | // vanilla triple we already have. |
| 454 | StringRef MArch = A->getValue(); |
| 455 | if (MArch == "x86_64h") |
| 456 | Triple.setArchName(MArch); |
| 457 | } |
| 458 | return Triple.getTriple(); |
| 459 | } |
Tim Northover | 02a979f | 2014-07-24 10:25:34 +0000 | [diff] [blame] | 460 | case llvm::Triple::aarch64: { |
| 461 | llvm::Triple Triple = getTriple(); |
| 462 | if (!Triple.isOSBinFormatMachO()) |
| 463 | return getTripleString(); |
| 464 | |
| 465 | // FIXME: older versions of ld64 expect the "arm64" component in the actual |
| 466 | // triple string and query it to determine whether an LTO file can be |
| 467 | // handled. Remove this when we don't care any more. |
| 468 | Triple.setArchName("arm64"); |
| 469 | return Triple.getTriple(); |
| 470 | } |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 471 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 472 | case llvm::Triple::armeb: |
| 473 | case llvm::Triple::thumb: |
| 474 | case llvm::Triple::thumbeb: { |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 475 | // FIXME: Factor into subclasses. |
| 476 | llvm::Triple Triple = getTriple(); |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 477 | bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || |
| 478 | getTriple().getArch() == llvm::Triple::thumbeb; |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 479 | |
Christian Pirker | ba289f0 | 2014-04-10 13:59:32 +0000 | [diff] [blame] | 480 | // Handle pseudo-target flags '-mlittle-endian'/'-EL' and |
| 481 | // '-mbig-endian'/'-EB'. |
| 482 | if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, |
| 483 | options::OPT_mbig_endian)) { |
David Blaikie | 7a3cbb2 | 2015-03-09 02:02:07 +0000 | [diff] [blame] | 484 | IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); |
Christian Pirker | ba289f0 | 2014-04-10 13:59:32 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 487 | // Thumb2 is the default for V7 on Darwin. |
| 488 | // |
| 489 | // FIXME: Thumb should just be another -target-feaure, not in the triple. |
Renato Golin | e17c580 | 2015-07-27 23:44:42 +0000 | [diff] [blame] | 490 | StringRef MCPU, MArch; |
| 491 | if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) |
| 492 | MCPU = A->getValue(); |
| 493 | if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) |
| 494 | MArch = A->getValue(); |
Chandler Carruth | d96f37a | 2015-08-30 07:51:18 +0000 | [diff] [blame] | 495 | std::string CPU = |
| 496 | Triple.isOSBinFormatMachO() |
| 497 | ? tools::arm::getARMCPUForMArch(MArch, Triple).str() |
| 498 | : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); |
Douglas Katzman | 96ad05e | 2015-08-06 22:36:24 +0000 | [diff] [blame] | 499 | StringRef Suffix = |
Vladimir Sukharev | 64f6824 | 2015-09-23 09:29:32 +0000 | [diff] [blame] | 500 | tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); |
Alexandros Lamprineas | 89ea433 | 2015-10-28 10:10:03 +0000 | [diff] [blame] | 501 | bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M; |
| 502 | bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 && |
| 503 | getTriple().isOSBinFormatMachO()); |
Saleem Abdulrasool | f4c9e49 | 2014-04-04 20:31:19 +0000 | [diff] [blame] | 504 | // FIXME: this is invalid for WindowsCE |
| 505 | if (getTriple().isOSWindows()) |
| 506 | ThumbDefault = true; |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 507 | std::string ArchName; |
| 508 | if (IsBigEndian) |
| 509 | ArchName = "armeb"; |
| 510 | else |
| 511 | ArchName = "arm"; |
Chad Rosier | d3a0f95 | 2011-09-20 20:44:06 +0000 | [diff] [blame] | 512 | |
Alexandros Lamprineas | 89ea433 | 2015-10-28 10:10:03 +0000 | [diff] [blame] | 513 | // Assembly files should start in ARM mode, unless arch is M-profile. |
Renato Golin | 6b5596a | 2016-07-27 14:12:20 +0000 | [diff] [blame] | 514 | // Windows is always thumb. |
Alexandros Lamprineas | f6ecf96 | 2015-11-05 17:11:55 +0000 | [diff] [blame] | 515 | if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb, |
Renato Golin | 6b5596a | 2016-07-27 14:12:20 +0000 | [diff] [blame] | 516 | options::OPT_mno_thumb, ThumbDefault)) || IsMProfile || |
| 517 | getTriple().isOSWindows()) { |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 518 | if (IsBigEndian) |
| 519 | ArchName = "thumbeb"; |
| 520 | else |
| 521 | ArchName = "thumb"; |
| 522 | } |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 523 | Triple.setArchName(ArchName + Suffix.str()); |
| 524 | |
| 525 | return Triple.getTriple(); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Douglas Katzman | 96ad05e | 2015-08-06 22:36:24 +0000 | [diff] [blame] | 530 | std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, |
Chad Rosier | d3a0f95 | 2011-09-20 20:44:06 +0000 | [diff] [blame] | 531 | types::ID InputType) const { |
Chad Rosier | d3a0f95 | 2011-09-20 20:44:06 +0000 | [diff] [blame] | 532 | return ComputeLLVMTriple(Args, InputType); |
Daniel Dunbar | 82eb4ce | 2010-08-23 22:35:37 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Chandler Carruth | 6bfd84f | 2011-11-04 07:12:53 +0000 | [diff] [blame] | 535 | void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 536 | ArgStringList &CC1Args) const { |
| 537 | // Each toolchain should provide the appropriate include flags. |
| 538 | } |
| 539 | |
Chandler Carruth | 05fb585 | 2012-11-21 23:40:23 +0000 | [diff] [blame] | 540 | void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, |
| 541 | ArgStringList &CC1Args) const { |
Rafael Espindola | 66aa045 | 2012-06-19 01:26:10 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Tim Northover | 336f189 | 2014-03-29 13:16:12 +0000 | [diff] [blame] | 544 | void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} |
| 545 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 546 | void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, |
Xinliang David Li | 69306c0 | 2015-10-22 06:15:31 +0000 | [diff] [blame] | 547 | llvm::opt::ArgStringList &CmdArgs) const { |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 548 | if (!needsProfileRT(Args)) return; |
Xinliang David Li | 69306c0 | 2015-10-22 06:15:31 +0000 | [diff] [blame] | 549 | |
Vedant Kumar | 5fb00e4 | 2016-07-27 23:01:55 +0000 | [diff] [blame] | 550 | CmdArgs.push_back(getCompilerRTArgString(Args, "profile")); |
Xinliang David Li | 69306c0 | 2015-10-22 06:15:31 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Daniel Dunbar | f4916cd | 2011-12-07 23:03:15 +0000 | [diff] [blame] | 553 | ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( |
Xinliang David Li | 69306c0 | 2015-10-22 06:15:31 +0000 | [diff] [blame] | 554 | const ArgList &Args) const { |
Jonas Hahnfeld | d196fa5 | 2016-07-27 08:15:54 +0000 | [diff] [blame] | 555 | const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ); |
| 556 | StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB; |
| 557 | |
Jonas Hahnfeld | eb4cc23 | 2016-12-12 07:53:47 +0000 | [diff] [blame] | 558 | // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB! |
Jonas Hahnfeld | d196fa5 | 2016-07-27 08:15:54 +0000 | [diff] [blame] | 559 | if (LibName == "compiler-rt") |
| 560 | return ToolChain::RLT_CompilerRT; |
| 561 | else if (LibName == "libgcc") |
| 562 | return ToolChain::RLT_Libgcc; |
| 563 | else if (LibName == "platform") |
| 564 | return GetDefaultRuntimeLibType(); |
| 565 | |
| 566 | if (A) |
| 567 | getDriver().Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args); |
Daniel Dunbar | f4916cd | 2011-12-07 23:03:15 +0000 | [diff] [blame] | 568 | |
| 569 | return GetDefaultRuntimeLibType(); |
| 570 | } |
| 571 | |
Daniel Dunbar | bf11f79 | 2010-09-14 23:12:35 +0000 | [diff] [blame] | 572 | ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ |
Jonas Hahnfeld | aae8374 | 2016-02-12 07:48:37 +0000 | [diff] [blame] | 573 | const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ); |
Jonas Hahnfeld | eb4cc23 | 2016-12-12 07:53:47 +0000 | [diff] [blame] | 574 | StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB; |
Jonas Hahnfeld | 0995419 | 2016-03-14 14:34:04 +0000 | [diff] [blame] | 575 | |
Jonas Hahnfeld | eb4cc23 | 2016-12-12 07:53:47 +0000 | [diff] [blame] | 576 | // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB! |
| 577 | if (LibName == "libc++") |
| 578 | return ToolChain::CST_Libcxx; |
| 579 | else if (LibName == "libstdc++") |
| 580 | return ToolChain::CST_Libstdcxx; |
| 581 | else if (LibName == "platform") |
| 582 | return GetDefaultCXXStdlibType(); |
Daniel Dunbar | 092b6fb | 2010-09-14 23:12:40 +0000 | [diff] [blame] | 583 | |
Jonas Hahnfeld | eb4cc23 | 2016-12-12 07:53:47 +0000 | [diff] [blame] | 584 | if (A) |
| 585 | getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args); |
Jonas Hahnfeld | aae8374 | 2016-02-12 07:48:37 +0000 | [diff] [blame] | 586 | |
Jonas Hahnfeld | eb4cc23 | 2016-12-12 07:53:47 +0000 | [diff] [blame] | 587 | return GetDefaultCXXStdlibType(); |
Daniel Dunbar | bf11f79 | 2010-09-14 23:12:35 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 590 | /// \brief Utility function to add a system include directory to CC1 arguments. |
| 591 | /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, |
| 592 | ArgStringList &CC1Args, |
| 593 | const Twine &Path) { |
| 594 | CC1Args.push_back("-internal-isystem"); |
| 595 | CC1Args.push_back(DriverArgs.MakeArgString(Path)); |
| 596 | } |
| 597 | |
| 598 | /// \brief Utility function to add a system include directory with extern "C" |
| 599 | /// semantics to CC1 arguments. |
| 600 | /// |
| 601 | /// Note that this should be used rarely, and only for directories that |
| 602 | /// historically and for legacy reasons are treated as having implicit extern |
| 603 | /// "C" semantics. These semantics are *ignored* by and large today, but its |
| 604 | /// important to preserve the preprocessor changes resulting from the |
| 605 | /// classification. |
| 606 | /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, |
| 607 | ArgStringList &CC1Args, |
| 608 | const Twine &Path) { |
| 609 | CC1Args.push_back("-internal-externc-isystem"); |
| 610 | CC1Args.push_back(DriverArgs.MakeArgString(Path)); |
| 611 | } |
| 612 | |
Simon Atanasyan | 08450bd | 2013-04-20 08:15:03 +0000 | [diff] [blame] | 613 | void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, |
| 614 | ArgStringList &CC1Args, |
| 615 | const Twine &Path) { |
| 616 | if (llvm::sys::fs::exists(Path)) |
| 617 | addExternCSystemInclude(DriverArgs, CC1Args, Path); |
| 618 | } |
| 619 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 620 | /// \brief Utility function to add a list of system include directories to CC1. |
| 621 | /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, |
| 622 | ArgStringList &CC1Args, |
| 623 | ArrayRef<StringRef> Paths) { |
Douglas Katzman | 96ad05e | 2015-08-06 22:36:24 +0000 | [diff] [blame] | 624 | for (StringRef Path : Paths) { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 625 | CC1Args.push_back("-internal-isystem"); |
Douglas Katzman | 96ad05e | 2015-08-06 22:36:24 +0000 | [diff] [blame] | 626 | CC1Args.push_back(DriverArgs.MakeArgString(Path)); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Chandler Carruth | 814db37 | 2011-11-04 23:49:01 +0000 | [diff] [blame] | 630 | void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
| 631 | ArgStringList &CC1Args) const { |
Chandler Carruth | 4c81dfa | 2011-11-04 07:43:33 +0000 | [diff] [blame] | 632 | // Header search paths should be handled by each of the subclasses. |
| 633 | // Historically, they have not been, and instead have been handled inside of |
| 634 | // the CC1-layer frontend. As the logic is hoisted out, this generic function |
| 635 | // will slowly stop being called. |
| 636 | // |
| 637 | // While it is being called, replicate a bit of a hack to propagate the |
| 638 | // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ |
| 639 | // header search paths with it. Once all systems are overriding this |
| 640 | // function, the CC1 flag and this line can be removed. |
Chandler Carruth | 814db37 | 2011-11-04 23:49:01 +0000 | [diff] [blame] | 641 | DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); |
Daniel Dunbar | bf11f79 | 2010-09-14 23:12:35 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Daniel Dunbar | 3f7796f | 2010-09-17 01:20:05 +0000 | [diff] [blame] | 644 | void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, |
| 645 | ArgStringList &CmdArgs) const { |
Daniel Dunbar | bf11f79 | 2010-09-14 23:12:35 +0000 | [diff] [blame] | 646 | CXXStdlibType Type = GetCXXStdlibType(Args); |
| 647 | |
| 648 | switch (Type) { |
Daniel Dunbar | 092b6fb | 2010-09-14 23:12:40 +0000 | [diff] [blame] | 649 | case ToolChain::CST_Libcxx: |
| 650 | CmdArgs.push_back("-lc++"); |
| 651 | break; |
| 652 | |
Daniel Dunbar | bf11f79 | 2010-09-14 23:12:35 +0000 | [diff] [blame] | 653 | case ToolChain::CST_Libstdcxx: |
| 654 | CmdArgs.push_back("-lstdc++"); |
| 655 | break; |
| 656 | } |
| 657 | } |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 658 | |
Douglas Katzman | 6059ef9 | 2015-11-17 17:41:23 +0000 | [diff] [blame] | 659 | void ToolChain::AddFilePathLibArgs(const ArgList &Args, |
| 660 | ArgStringList &CmdArgs) const { |
| 661 | for (const auto &LibPath : getFilePaths()) |
Martell Malone | 5cad225 | 2015-11-26 01:02:07 +0000 | [diff] [blame] | 662 | if(LibPath.length() > 0) |
| 663 | CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath)); |
Douglas Katzman | 6059ef9 | 2015-11-17 17:41:23 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Shantonu Sen | afeb03b | 2010-09-17 18:39:08 +0000 | [diff] [blame] | 666 | void ToolChain::AddCCKextLibArgs(const ArgList &Args, |
| 667 | ArgStringList &CmdArgs) const { |
| 668 | CmdArgs.push_back("-lcc_kext"); |
| 669 | } |
Benjamin Kramer | 058666a | 2012-10-04 19:42:20 +0000 | [diff] [blame] | 670 | |
| 671 | bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, |
| 672 | ArgStringList &CmdArgs) const { |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 673 | // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed |
| 674 | // (to keep the linker options consistent with gcc and clang itself). |
| 675 | if (!isOptimizationLevelFast(Args)) { |
| 676 | // Check if -ffast-math or -funsafe-math. |
| 677 | Arg *A = |
| 678 | Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, |
| 679 | options::OPT_funsafe_math_optimizations, |
| 680 | options::OPT_fno_unsafe_math_optimizations); |
Benjamin Kramer | 058666a | 2012-10-04 19:42:20 +0000 | [diff] [blame] | 681 | |
Benjamin Kramer | ab88f62 | 2014-03-25 18:02:07 +0000 | [diff] [blame] | 682 | if (!A || A->getOption().getID() == options::OPT_fno_fast_math || |
| 683 | A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) |
| 684 | return false; |
| 685 | } |
Benjamin Kramer | 058666a | 2012-10-04 19:42:20 +0000 | [diff] [blame] | 686 | // If crtfastmath.o exists add it to the arguments. |
| 687 | std::string Path = GetFilePath("crtfastmath.o"); |
| 688 | if (Path == "crtfastmath.o") // Not found. |
| 689 | return false; |
| 690 | |
| 691 | CmdArgs.push_back(Args.MakeArgString(Path)); |
| 692 | return true; |
| 693 | } |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 694 | |
| 695 | SanitizerMask ToolChain::getSupportedSanitizers() const { |
| 696 | // Return sanitizers which don't require runtime support and are not |
Peter Collingbourne | 24ec4924 | 2015-09-10 19:18:05 +0000 | [diff] [blame] | 697 | // platform dependent. |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 698 | using namespace SanitizerKind; |
Peter Collingbourne | 24ec4924 | 2015-09-10 19:18:05 +0000 | [diff] [blame] | 699 | SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) | |
| 700 | CFICastStrict | UnsignedIntegerOverflow | LocalBounds; |
| 701 | if (getTriple().getArch() == llvm::Triple::x86 || |
Derek Schuff | ef31305 | 2016-08-08 21:14:15 +0000 | [diff] [blame] | 702 | getTriple().getArch() == llvm::Triple::x86_64 || |
Evgeniy Stepanov | da2028b | 2016-11-11 18:49:49 +0000 | [diff] [blame] | 703 | getTriple().getArch() == llvm::Triple::arm || |
| 704 | getTriple().getArch() == llvm::Triple::aarch64 || |
Derek Schuff | ef31305 | 2016-08-08 21:14:15 +0000 | [diff] [blame] | 705 | getTriple().getArch() == llvm::Triple::wasm32 || |
| 706 | getTriple().getArch() == llvm::Triple::wasm64) |
Peter Collingbourne | 24ec4924 | 2015-09-10 19:18:05 +0000 | [diff] [blame] | 707 | Res |= CFIICall; |
| 708 | return Res; |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 709 | } |
Artem Belevich | fa11ab5 | 2015-11-17 22:28:46 +0000 | [diff] [blame] | 710 | |
| 711 | void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, |
| 712 | ArgStringList &CC1Args) const {} |
Andrey Turetskiy | 4798eb6 | 2016-06-16 10:36:09 +0000 | [diff] [blame] | 713 | |
| 714 | void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs, |
| 715 | ArgStringList &CC1Args) const {} |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 716 | |
| 717 | static VersionTuple separateMSVCFullVersion(unsigned Version) { |
| 718 | if (Version < 100) |
| 719 | return VersionTuple(Version); |
| 720 | |
| 721 | if (Version < 10000) |
| 722 | return VersionTuple(Version / 100, Version % 100); |
| 723 | |
| 724 | unsigned Build = 0, Factor = 1; |
| 725 | for (; Version > 10000; Version = Version / 10, Factor = Factor * 10) |
| 726 | Build = Build + (Version % 10) * Factor; |
| 727 | return VersionTuple(Version / 100, Version % 100, Build); |
| 728 | } |
| 729 | |
| 730 | VersionTuple |
| 731 | ToolChain::computeMSVCVersion(const Driver *D, |
| 732 | const llvm::opt::ArgList &Args) const { |
| 733 | const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version); |
| 734 | const Arg *MSCompatibilityVersion = |
| 735 | Args.getLastArg(options::OPT_fms_compatibility_version); |
| 736 | |
| 737 | if (MSCVersion && MSCompatibilityVersion) { |
| 738 | if (D) |
| 739 | D->Diag(diag::err_drv_argument_not_allowed_with) |
| 740 | << MSCVersion->getAsString(Args) |
| 741 | << MSCompatibilityVersion->getAsString(Args); |
| 742 | return VersionTuple(); |
| 743 | } |
| 744 | |
| 745 | if (MSCompatibilityVersion) { |
| 746 | VersionTuple MSVT; |
| 747 | if (MSVT.tryParse(MSCompatibilityVersion->getValue())) { |
| 748 | if (D) |
| 749 | D->Diag(diag::err_drv_invalid_value) |
| 750 | << MSCompatibilityVersion->getAsString(Args) |
| 751 | << MSCompatibilityVersion->getValue(); |
| 752 | } else { |
| 753 | return MSVT; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (MSCVersion) { |
| 758 | unsigned Version = 0; |
| 759 | if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) { |
| 760 | if (D) |
| 761 | D->Diag(diag::err_drv_invalid_value) |
| 762 | << MSCVersion->getAsString(Args) << MSCVersion->getValue(); |
| 763 | } else { |
| 764 | return separateMSVCFullVersion(Version); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | return VersionTuple(); |
| 769 | } |