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