1 //===--- ToolChain.cpp - Collections of tools for one platform ------------===// 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 10 #include "Tools.h" 11 #include "clang/Basic/ObjCRuntime.h" 12 #include "clang/Config/config.h" 13 #include "clang/Driver/Action.h" 14 #include "clang/Driver/Driver.h" 15 #include "clang/Driver/DriverDiagnostic.h" 16 #include "clang/Driver/Options.h" 17 #include "clang/Driver/SanitizerArgs.h" 18 #include "clang/Driver/ToolChain.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/Option/Arg.h" 22 #include "llvm/Option/ArgList.h" 23 #include "llvm/Option/Option.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/TargetRegistry.h" 27 #include "llvm/Support/TargetParser.h" 28 29 using namespace clang::driver; 30 using namespace clang::driver::tools; 31 using namespace clang; 32 using namespace llvm; 33 using namespace llvm::opt; 34 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. 57 Arg *Exceptions = Args.getLastArgNoClaim( 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 68 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T, 69 const ArgList &Args) 70 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)), 71 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) { 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) 75 << A->getValue() << A->getAsString(Args); 76 } 77 78 ToolChain::~ToolChain() { 79 } 80 81 vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); } 82 83 bool ToolChain::useIntegratedAs() const { 84 return Args.hasFlag(options::OPT_fintegrated_as, 85 options::OPT_fno_integrated_as, 86 IsIntegratedAssemblerDefault()); 87 } 88 89 const SanitizerArgs& ToolChain::getSanitizerArgs() const { 90 if (!SanitizerArguments.get()) 91 SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 92 return *SanitizerArguments.get(); 93 } 94 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 188 StringRef ToolChain::getDefaultUniversalArchName() const { 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"; 198 case llvm::Triple::ppc64le: 199 return "ppc64le"; 200 default: 201 return Triple.getArchName(); 202 } 203 } 204 205 bool ToolChain::IsUnwindTablesDefault() const { 206 return false; 207 } 208 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 { 242 switch (AC) { 243 case Action::AssembleJobClass: 244 return getAssemble(); 245 246 case Action::LinkJobClass: 247 return getLink(); 248 249 case Action::InputClass: 250 case Action::BindArchClass: 251 case Action::CudaDeviceClass: 252 case Action::CudaHostClass: 253 case Action::LipoJobClass: 254 case Action::DsymutilJobClass: 255 case Action::VerifyDebugInfoJobClass: 256 llvm_unreachable("Invalid tool kind."); 257 258 case Action::CompileJobClass: 259 case Action::PrecompileJobClass: 260 case Action::PreprocessJobClass: 261 case Action::AnalyzeJobClass: 262 case Action::MigrateJobClass: 263 case Action::VerifyPCHJobClass: 264 case Action::BackendJobClass: 265 return getClang(); 266 } 267 268 llvm_unreachable("Invalid tool kind."); 269 } 270 271 static StringRef getArchNameForCompilerRTLib(const ToolChain &TC, 272 const ArgList &Args) { 273 const llvm::Triple &Triple = TC.getTriple(); 274 bool IsWindows = Triple.isOSWindows(); 275 276 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86) 277 return "i386"; 278 279 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb) 280 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows) 281 ? "armhf" 282 : "arm"; 283 284 return TC.getArchName(); 285 } 286 287 std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component, 288 bool Shared) const { 289 const llvm::Triple &TT = getTriple(); 290 const char *Env = TT.isAndroid() ? "-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 307 const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args, 308 StringRef Component, 309 bool Shared) const { 310 return Args.MakeArgString(getCompilerRT(Args, Component, Shared)); 311 } 312 313 bool ToolChain::needsProfileRT(const ArgList &Args) { 314 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, 315 false) || 316 Args.hasArg(options::OPT_fprofile_generate) || 317 Args.hasArg(options::OPT_fprofile_generate_EQ) || 318 Args.hasArg(options::OPT_fprofile_instr_generate) || 319 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) || 320 Args.hasArg(options::OPT_fcreate_profile) || 321 Args.hasArg(options::OPT_coverage)) 322 return true; 323 324 return false; 325 } 326 327 Tool *ToolChain::SelectTool(const JobAction &JA) const { 328 if (getDriver().ShouldUseClangCompiler(JA)) return getClang(); 329 Action::ActionClass AC = JA.getKind(); 330 if (AC == Action::AssembleJobClass && useIntegratedAs()) 331 return getClangAs(); 332 return getTool(AC); 333 } 334 335 std::string ToolChain::GetFilePath(const char *Name) const { 336 return D.GetFilePath(Name, *this); 337 } 338 339 std::string ToolChain::GetProgramPath(const char *Name) const { 340 return D.GetProgramPath(Name, *this); 341 } 342 343 std::string ToolChain::GetLinkerPath() const { 344 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { 345 StringRef Suffix = A->getValue(); 346 347 // If we're passed -fuse-ld= with no argument, or with the argument ld, 348 // then use whatever the default system linker is. 349 if (Suffix.empty() || Suffix == "ld") 350 return GetProgramPath("ld"); 351 352 llvm::SmallString<8> LinkerName("ld."); 353 LinkerName.append(Suffix); 354 355 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 356 if (llvm::sys::fs::exists(LinkerPath)) 357 return LinkerPath; 358 359 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 360 return ""; 361 } 362 363 return GetProgramPath(DefaultLinker); 364 } 365 366 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const { 367 return types::lookupTypeForExtension(Ext); 368 } 369 370 bool ToolChain::HasNativeLLVMSupport() const { 371 return false; 372 } 373 374 bool ToolChain::isCrossCompiling() const { 375 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 376 switch (HostTriple.getArch()) { 377 // The A32/T32/T16 instruction sets are not separate architectures in this 378 // context. 379 case llvm::Triple::arm: 380 case llvm::Triple::armeb: 381 case llvm::Triple::thumb: 382 case llvm::Triple::thumbeb: 383 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 384 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 385 default: 386 return HostTriple.getArch() != getArch(); 387 } 388 } 389 390 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 391 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 392 VersionTuple()); 393 } 394 395 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 396 if (Model == "single") { 397 // FIXME: 'single' is only supported on ARM and WebAssembly so far. 398 return Triple.getArch() == llvm::Triple::arm || 399 Triple.getArch() == llvm::Triple::armeb || 400 Triple.getArch() == llvm::Triple::thumb || 401 Triple.getArch() == llvm::Triple::thumbeb || 402 Triple.getArch() == llvm::Triple::wasm32 || 403 Triple.getArch() == llvm::Triple::wasm64; 404 } else if (Model == "posix") 405 return true; 406 407 return false; 408 } 409 410 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 411 types::ID InputType) const { 412 switch (getTriple().getArch()) { 413 default: 414 return getTripleString(); 415 416 case llvm::Triple::x86_64: { 417 llvm::Triple Triple = getTriple(); 418 if (!Triple.isOSBinFormatMachO()) 419 return getTripleString(); 420 421 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 422 // x86_64h goes in the triple. Other -march options just use the 423 // vanilla triple we already have. 424 StringRef MArch = A->getValue(); 425 if (MArch == "x86_64h") 426 Triple.setArchName(MArch); 427 } 428 return Triple.getTriple(); 429 } 430 case llvm::Triple::aarch64: { 431 llvm::Triple Triple = getTriple(); 432 if (!Triple.isOSBinFormatMachO()) 433 return getTripleString(); 434 435 // FIXME: older versions of ld64 expect the "arm64" component in the actual 436 // triple string and query it to determine whether an LTO file can be 437 // handled. Remove this when we don't care any more. 438 Triple.setArchName("arm64"); 439 return Triple.getTriple(); 440 } 441 case llvm::Triple::arm: 442 case llvm::Triple::armeb: 443 case llvm::Triple::thumb: 444 case llvm::Triple::thumbeb: { 445 // FIXME: Factor into subclasses. 446 llvm::Triple Triple = getTriple(); 447 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || 448 getTriple().getArch() == llvm::Triple::thumbeb; 449 450 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 451 // '-mbig-endian'/'-EB'. 452 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 453 options::OPT_mbig_endian)) { 454 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 455 } 456 457 // Thumb2 is the default for V7 on Darwin. 458 // 459 // FIXME: Thumb should just be another -target-feaure, not in the triple. 460 StringRef MCPU, MArch; 461 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 462 MCPU = A->getValue(); 463 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 464 MArch = A->getValue(); 465 std::string CPU = 466 Triple.isOSBinFormatMachO() 467 ? tools::arm::getARMCPUForMArch(MArch, Triple).str() 468 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); 469 StringRef Suffix = 470 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 471 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M; 472 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 && 473 getTriple().isOSBinFormatMachO()); 474 // FIXME: this is invalid for WindowsCE 475 if (getTriple().isOSWindows()) 476 ThumbDefault = true; 477 std::string ArchName; 478 if (IsBigEndian) 479 ArchName = "armeb"; 480 else 481 ArchName = "arm"; 482 483 // Assembly files should start in ARM mode, unless arch is M-profile. 484 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb, 485 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile) { 486 if (IsBigEndian) 487 ArchName = "thumbeb"; 488 else 489 ArchName = "thumb"; 490 } 491 Triple.setArchName(ArchName + Suffix.str()); 492 493 return Triple.getTriple(); 494 } 495 } 496 } 497 498 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 499 types::ID InputType) const { 500 return ComputeLLVMTriple(Args, InputType); 501 } 502 503 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 504 ArgStringList &CC1Args) const { 505 // Each toolchain should provide the appropriate include flags. 506 } 507 508 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 509 ArgStringList &CC1Args) const { 510 } 511 512 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 513 514 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, 515 llvm::opt::ArgStringList &CmdArgs) const { 516 if (!needsProfileRT(Args)) return; 517 518 CmdArgs.push_back(getCompilerRTArgString(Args, "profile")); 519 } 520 521 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 522 const ArgList &Args) const { 523 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) { 524 StringRef Value = A->getValue(); 525 if (Value == "compiler-rt") 526 return ToolChain::RLT_CompilerRT; 527 if (Value == "libgcc") 528 return ToolChain::RLT_Libgcc; 529 getDriver().Diag(diag::err_drv_invalid_rtlib_name) 530 << A->getAsString(Args); 531 } 532 533 return GetDefaultRuntimeLibType(); 534 } 535 536 static bool ParseCXXStdlibType(const StringRef& Name, 537 ToolChain::CXXStdlibType& Type) { 538 if (Name == "libc++") 539 Type = ToolChain::CST_Libcxx; 540 else if (Name == "libstdc++") 541 Type = ToolChain::CST_Libstdcxx; 542 else 543 return false; 544 545 return true; 546 } 547 548 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 549 ToolChain::CXXStdlibType Type; 550 bool HasValidType = false; 551 552 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ); 553 if (A) { 554 HasValidType = ParseCXXStdlibType(A->getValue(), Type); 555 if (!HasValidType) 556 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 557 << A->getAsString(Args); 558 } 559 560 if (!HasValidType && !ParseCXXStdlibType(CLANG_DEFAULT_CXX_STDLIB, Type)) 561 Type = GetDefaultCXXStdlibType(); 562 563 return Type; 564 } 565 566 /// \brief Utility function to add a system include directory to CC1 arguments. 567 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 568 ArgStringList &CC1Args, 569 const Twine &Path) { 570 CC1Args.push_back("-internal-isystem"); 571 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 572 } 573 574 /// \brief Utility function to add a system include directory with extern "C" 575 /// semantics to CC1 arguments. 576 /// 577 /// Note that this should be used rarely, and only for directories that 578 /// historically and for legacy reasons are treated as having implicit extern 579 /// "C" semantics. These semantics are *ignored* by and large today, but its 580 /// important to preserve the preprocessor changes resulting from the 581 /// classification. 582 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 583 ArgStringList &CC1Args, 584 const Twine &Path) { 585 CC1Args.push_back("-internal-externc-isystem"); 586 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 587 } 588 589 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 590 ArgStringList &CC1Args, 591 const Twine &Path) { 592 if (llvm::sys::fs::exists(Path)) 593 addExternCSystemInclude(DriverArgs, CC1Args, Path); 594 } 595 596 /// \brief Utility function to add a list of system include directories to CC1. 597 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 598 ArgStringList &CC1Args, 599 ArrayRef<StringRef> Paths) { 600 for (StringRef Path : Paths) { 601 CC1Args.push_back("-internal-isystem"); 602 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 603 } 604 } 605 606 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 607 ArgStringList &CC1Args) const { 608 // Header search paths should be handled by each of the subclasses. 609 // Historically, they have not been, and instead have been handled inside of 610 // the CC1-layer frontend. As the logic is hoisted out, this generic function 611 // will slowly stop being called. 612 // 613 // While it is being called, replicate a bit of a hack to propagate the 614 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 615 // header search paths with it. Once all systems are overriding this 616 // function, the CC1 flag and this line can be removed. 617 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 618 } 619 620 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 621 ArgStringList &CmdArgs) const { 622 CXXStdlibType Type = GetCXXStdlibType(Args); 623 624 switch (Type) { 625 case ToolChain::CST_Libcxx: 626 CmdArgs.push_back("-lc++"); 627 break; 628 629 case ToolChain::CST_Libstdcxx: 630 CmdArgs.push_back("-lstdc++"); 631 break; 632 } 633 } 634 635 void ToolChain::AddFilePathLibArgs(const ArgList &Args, 636 ArgStringList &CmdArgs) const { 637 for (const auto &LibPath : getFilePaths()) 638 if(LibPath.length() > 0) 639 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath)); 640 } 641 642 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 643 ArgStringList &CmdArgs) const { 644 CmdArgs.push_back("-lcc_kext"); 645 } 646 647 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 648 ArgStringList &CmdArgs) const { 649 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 650 // (to keep the linker options consistent with gcc and clang itself). 651 if (!isOptimizationLevelFast(Args)) { 652 // Check if -ffast-math or -funsafe-math. 653 Arg *A = 654 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 655 options::OPT_funsafe_math_optimizations, 656 options::OPT_fno_unsafe_math_optimizations); 657 658 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 659 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 660 return false; 661 } 662 // If crtfastmath.o exists add it to the arguments. 663 std::string Path = GetFilePath("crtfastmath.o"); 664 if (Path == "crtfastmath.o") // Not found. 665 return false; 666 667 CmdArgs.push_back(Args.MakeArgString(Path)); 668 return true; 669 } 670 671 SanitizerMask ToolChain::getSupportedSanitizers() const { 672 // Return sanitizers which don't require runtime support and are not 673 // platform dependent. 674 using namespace SanitizerKind; 675 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) | 676 CFICastStrict | UnsignedIntegerOverflow | LocalBounds; 677 if (getTriple().getArch() == llvm::Triple::x86 || 678 getTriple().getArch() == llvm::Triple::x86_64) 679 Res |= CFIICall; 680 return Res; 681 } 682 683 void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 684 ArgStringList &CC1Args) const {} 685