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