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 "clang/Driver/ToolChain.h" 11 #include "Tools.h" 12 #include "clang/Basic/ObjCRuntime.h" 13 #include "clang/Config/config.h" 14 #include "clang/Driver/Action.h" 15 #include "clang/Driver/Driver.h" 16 #include "clang/Driver/DriverDiagnostic.h" 17 #include "clang/Driver/Options.h" 18 #include "clang/Driver/SanitizerArgs.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/Option/Arg.h" 21 #include "llvm/Option/ArgList.h" 22 #include "llvm/Option/Option.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/Path.h" 26 #include "llvm/Support/TargetParser.h" 27 #include "llvm/Support/TargetRegistry.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 EffectiveTriple() { 73 if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) 74 if (!isThreadModelSupported(A->getValue())) 75 D.Diag(diag::err_drv_invalid_thread_model_for_target) 76 << A->getValue() << A->getAsString(Args); 77 } 78 79 ToolChain::~ToolChain() { 80 } 81 82 vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); } 83 84 bool ToolChain::useIntegratedAs() const { 85 return Args.hasFlag(options::OPT_fintegrated_as, 86 options::OPT_fno_integrated_as, 87 IsIntegratedAssemblerDefault()); 88 } 89 90 const SanitizerArgs& ToolChain::getSanitizerArgs() const { 91 if (!SanitizerArguments.get()) 92 SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 93 return *SanitizerArguments.get(); 94 } 95 96 namespace { 97 struct DriverSuffix { 98 const char *Suffix; 99 const char *ModeFlag; 100 }; 101 102 const DriverSuffix *FindDriverSuffix(StringRef ProgName) { 103 // A list of known driver suffixes. Suffixes are compared against the 104 // program name in order. If there is a match, the frontend type is updated as 105 // necessary by applying the ModeFlag. 106 static const DriverSuffix DriverSuffixes[] = { 107 {"clang", nullptr}, 108 {"clang++", "--driver-mode=g++"}, 109 {"clang-c++", "--driver-mode=g++"}, 110 {"clang-cc", nullptr}, 111 {"clang-cpp", "--driver-mode=cpp"}, 112 {"clang-g++", "--driver-mode=g++"}, 113 {"clang-gcc", nullptr}, 114 {"clang-cl", "--driver-mode=cl"}, 115 {"cc", nullptr}, 116 {"cpp", "--driver-mode=cpp"}, 117 {"cl", "--driver-mode=cl"}, 118 {"++", "--driver-mode=g++"}, 119 }; 120 121 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i) 122 if (ProgName.endswith(DriverSuffixes[i].Suffix)) 123 return &DriverSuffixes[i]; 124 return nullptr; 125 } 126 127 /// Normalize the program name from argv[0] by stripping the file extension if 128 /// present and lower-casing the string on Windows. 129 std::string normalizeProgramName(llvm::StringRef Argv0) { 130 std::string ProgName = llvm::sys::path::stem(Argv0); 131 #ifdef LLVM_ON_WIN32 132 // Transform to lowercase for case insensitive file systems. 133 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower); 134 #endif 135 return ProgName; 136 } 137 138 const DriverSuffix *parseDriverSuffix(StringRef ProgName) { 139 // Try to infer frontend type and default target from the program name by 140 // comparing it against DriverSuffixes in order. 141 142 // If there is a match, the function tries to identify a target as prefix. 143 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target 144 // prefix "x86_64-linux". If such a target prefix is found, it may be 145 // added via -target as implicit first argument. 146 const DriverSuffix *DS = FindDriverSuffix(ProgName); 147 148 if (!DS) { 149 // Try again after stripping any trailing version number: 150 // clang++3.5 -> clang++ 151 ProgName = ProgName.rtrim("0123456789."); 152 DS = FindDriverSuffix(ProgName); 153 } 154 155 if (!DS) { 156 // Try again after stripping trailing -component. 157 // clang++-tot -> clang++ 158 ProgName = ProgName.slice(0, ProgName.rfind('-')); 159 DS = FindDriverSuffix(ProgName); 160 } 161 return DS; 162 } 163 } // anonymous namespace 164 165 std::pair<std::string, std::string> 166 ToolChain::getTargetAndModeFromProgramName(StringRef PN) { 167 std::string ProgName = normalizeProgramName(PN); 168 const DriverSuffix *DS = parseDriverSuffix(ProgName); 169 if (!DS) 170 return std::make_pair("", ""); 171 std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag; 172 173 std::string::size_type LastComponent = 174 ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix)); 175 if (LastComponent == std::string::npos) 176 return std::make_pair("", ModeFlag); 177 178 // Infer target from the prefix. 179 StringRef Prefix(ProgName); 180 Prefix = Prefix.slice(0, LastComponent); 181 std::string IgnoredError; 182 std::string Target; 183 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) { 184 Target = Prefix; 185 } 186 return std::make_pair(Target, ModeFlag); 187 } 188 189 StringRef ToolChain::getDefaultUniversalArchName() const { 190 // In universal driver terms, the arch name accepted by -arch isn't exactly 191 // the same as the ones that appear in the triple. Roughly speaking, this is 192 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the 193 // only interesting special case is powerpc. 194 switch (Triple.getArch()) { 195 case llvm::Triple::ppc: 196 return "ppc"; 197 case llvm::Triple::ppc64: 198 return "ppc64"; 199 case llvm::Triple::ppc64le: 200 return "ppc64le"; 201 default: 202 return Triple.getArchName(); 203 } 204 } 205 206 bool ToolChain::IsUnwindTablesDefault() const { 207 return false; 208 } 209 210 Tool *ToolChain::getClang() const { 211 if (!Clang) 212 Clang.reset(new tools::Clang(*this)); 213 return Clang.get(); 214 } 215 216 Tool *ToolChain::buildAssembler() const { 217 return new tools::ClangAs(*this); 218 } 219 220 Tool *ToolChain::buildLinker() const { 221 llvm_unreachable("Linking is not supported by this toolchain"); 222 } 223 224 Tool *ToolChain::getAssemble() const { 225 if (!Assemble) 226 Assemble.reset(buildAssembler()); 227 return Assemble.get(); 228 } 229 230 Tool *ToolChain::getClangAs() const { 231 if (!Assemble) 232 Assemble.reset(new tools::ClangAs(*this)); 233 return Assemble.get(); 234 } 235 236 Tool *ToolChain::getLink() const { 237 if (!Link) 238 Link.reset(buildLinker()); 239 return Link.get(); 240 } 241 242 Tool *ToolChain::getTool(Action::ActionClass AC) const { 243 switch (AC) { 244 case Action::AssembleJobClass: 245 return getAssemble(); 246 247 case Action::LinkJobClass: 248 return getLink(); 249 250 case Action::InputClass: 251 case Action::BindArchClass: 252 case Action::OffloadClass: 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 UseLinker = A->getValue(); 346 347 if (llvm::sys::path::is_absolute(UseLinker)) { 348 // If we're passed -fuse-ld= with what looks like an absolute path, 349 // don't attempt to second-guess that. 350 if (llvm::sys::fs::exists(UseLinker)) 351 return UseLinker; 352 } else { 353 // If we're passed -fuse-ld= with no argument, or with the argument ld, 354 // then use whatever the default system linker is. 355 if (UseLinker.empty() || UseLinker == "ld") 356 return GetProgramPath("ld"); 357 358 llvm::SmallString<8> LinkerName("ld."); 359 LinkerName.append(UseLinker); 360 361 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 362 if (llvm::sys::fs::exists(LinkerPath)) 363 return LinkerPath; 364 } 365 366 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 367 return ""; 368 } 369 370 return GetProgramPath(DefaultLinker); 371 } 372 373 types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const { 374 return types::lookupTypeForExtension(Ext); 375 } 376 377 bool ToolChain::HasNativeLLVMSupport() const { 378 return false; 379 } 380 381 bool ToolChain::isCrossCompiling() const { 382 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 383 switch (HostTriple.getArch()) { 384 // The A32/T32/T16 instruction sets are not separate architectures in this 385 // context. 386 case llvm::Triple::arm: 387 case llvm::Triple::armeb: 388 case llvm::Triple::thumb: 389 case llvm::Triple::thumbeb: 390 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 391 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 392 default: 393 return HostTriple.getArch() != getArch(); 394 } 395 } 396 397 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 398 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 399 VersionTuple()); 400 } 401 402 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 403 if (Model == "single") { 404 // FIXME: 'single' is only supported on ARM and WebAssembly so far. 405 return Triple.getArch() == llvm::Triple::arm || 406 Triple.getArch() == llvm::Triple::armeb || 407 Triple.getArch() == llvm::Triple::thumb || 408 Triple.getArch() == llvm::Triple::thumbeb || 409 Triple.getArch() == llvm::Triple::wasm32 || 410 Triple.getArch() == llvm::Triple::wasm64; 411 } else if (Model == "posix") 412 return true; 413 414 return false; 415 } 416 417 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 418 types::ID InputType) const { 419 switch (getTriple().getArch()) { 420 default: 421 return getTripleString(); 422 423 case llvm::Triple::x86_64: { 424 llvm::Triple Triple = getTriple(); 425 if (!Triple.isOSBinFormatMachO()) 426 return getTripleString(); 427 428 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 429 // x86_64h goes in the triple. Other -march options just use the 430 // vanilla triple we already have. 431 StringRef MArch = A->getValue(); 432 if (MArch == "x86_64h") 433 Triple.setArchName(MArch); 434 } 435 return Triple.getTriple(); 436 } 437 case llvm::Triple::aarch64: { 438 llvm::Triple Triple = getTriple(); 439 if (!Triple.isOSBinFormatMachO()) 440 return getTripleString(); 441 442 // FIXME: older versions of ld64 expect the "arm64" component in the actual 443 // triple string and query it to determine whether an LTO file can be 444 // handled. Remove this when we don't care any more. 445 Triple.setArchName("arm64"); 446 return Triple.getTriple(); 447 } 448 case llvm::Triple::arm: 449 case llvm::Triple::armeb: 450 case llvm::Triple::thumb: 451 case llvm::Triple::thumbeb: { 452 // FIXME: Factor into subclasses. 453 llvm::Triple Triple = getTriple(); 454 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || 455 getTriple().getArch() == llvm::Triple::thumbeb; 456 457 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 458 // '-mbig-endian'/'-EB'. 459 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 460 options::OPT_mbig_endian)) { 461 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 462 } 463 464 // Thumb2 is the default for V7 on Darwin. 465 // 466 // FIXME: Thumb should just be another -target-feaure, not in the triple. 467 StringRef MCPU, MArch; 468 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 469 MCPU = A->getValue(); 470 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 471 MArch = A->getValue(); 472 std::string CPU = 473 Triple.isOSBinFormatMachO() 474 ? tools::arm::getARMCPUForMArch(MArch, Triple).str() 475 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); 476 StringRef Suffix = 477 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 478 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M; 479 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 && 480 getTriple().isOSBinFormatMachO()); 481 // FIXME: this is invalid for WindowsCE 482 if (getTriple().isOSWindows()) 483 ThumbDefault = true; 484 std::string ArchName; 485 if (IsBigEndian) 486 ArchName = "armeb"; 487 else 488 ArchName = "arm"; 489 490 // Assembly files should start in ARM mode, unless arch is M-profile. 491 // Windows is always thumb. 492 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb, 493 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile || 494 getTriple().isOSWindows()) { 495 if (IsBigEndian) 496 ArchName = "thumbeb"; 497 else 498 ArchName = "thumb"; 499 } 500 Triple.setArchName(ArchName + Suffix.str()); 501 502 return Triple.getTriple(); 503 } 504 } 505 } 506 507 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 508 types::ID InputType) const { 509 return ComputeLLVMTriple(Args, InputType); 510 } 511 512 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 513 ArgStringList &CC1Args) const { 514 // Each toolchain should provide the appropriate include flags. 515 } 516 517 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 518 ArgStringList &CC1Args) const { 519 } 520 521 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 522 523 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, 524 llvm::opt::ArgStringList &CmdArgs) const { 525 if (!needsProfileRT(Args)) return; 526 527 CmdArgs.push_back(getCompilerRTArgString(Args, "profile")); 528 } 529 530 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 531 const ArgList &Args) const { 532 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ); 533 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB; 534 535 // "platform" is only used in tests to override CLANG_DEFAULT_RTLIB 536 if (LibName == "compiler-rt") 537 return ToolChain::RLT_CompilerRT; 538 else if (LibName == "libgcc") 539 return ToolChain::RLT_Libgcc; 540 else if (LibName == "platform") 541 return GetDefaultRuntimeLibType(); 542 543 if (A) 544 getDriver().Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args); 545 546 return GetDefaultRuntimeLibType(); 547 } 548 549 static bool ParseCXXStdlibType(const StringRef& Name, 550 ToolChain::CXXStdlibType& Type) { 551 if (Name == "libc++") 552 Type = ToolChain::CST_Libcxx; 553 else if (Name == "libstdc++") 554 Type = ToolChain::CST_Libstdcxx; 555 else 556 return false; 557 558 return true; 559 } 560 561 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 562 ToolChain::CXXStdlibType Type; 563 bool HasValidType = false; 564 bool ForcePlatformDefault = false; 565 566 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ); 567 if (A) { 568 StringRef Value = A->getValue(); 569 HasValidType = ParseCXXStdlibType(Value, Type); 570 571 // Only use in tests to override CLANG_DEFAULT_CXX_STDLIB! 572 if (Value == "platform") 573 ForcePlatformDefault = true; 574 else if (!HasValidType) 575 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 576 << A->getAsString(Args); 577 } 578 579 // If no argument was provided or its value was invalid, look for the 580 // default unless forced or configured to take the platform default. 581 if (!HasValidType && (ForcePlatformDefault || 582 !ParseCXXStdlibType(CLANG_DEFAULT_CXX_STDLIB, Type))) 583 Type = GetDefaultCXXStdlibType(); 584 585 return Type; 586 } 587 588 /// \brief Utility function to add a system include directory to CC1 arguments. 589 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 590 ArgStringList &CC1Args, 591 const Twine &Path) { 592 CC1Args.push_back("-internal-isystem"); 593 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 594 } 595 596 /// \brief Utility function to add a system include directory with extern "C" 597 /// semantics to CC1 arguments. 598 /// 599 /// Note that this should be used rarely, and only for directories that 600 /// historically and for legacy reasons are treated as having implicit extern 601 /// "C" semantics. These semantics are *ignored* by and large today, but its 602 /// important to preserve the preprocessor changes resulting from the 603 /// classification. 604 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 605 ArgStringList &CC1Args, 606 const Twine &Path) { 607 CC1Args.push_back("-internal-externc-isystem"); 608 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 609 } 610 611 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 612 ArgStringList &CC1Args, 613 const Twine &Path) { 614 if (llvm::sys::fs::exists(Path)) 615 addExternCSystemInclude(DriverArgs, CC1Args, Path); 616 } 617 618 /// \brief Utility function to add a list of system include directories to CC1. 619 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 620 ArgStringList &CC1Args, 621 ArrayRef<StringRef> Paths) { 622 for (StringRef Path : Paths) { 623 CC1Args.push_back("-internal-isystem"); 624 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 625 } 626 } 627 628 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 629 ArgStringList &CC1Args) const { 630 // Header search paths should be handled by each of the subclasses. 631 // Historically, they have not been, and instead have been handled inside of 632 // the CC1-layer frontend. As the logic is hoisted out, this generic function 633 // will slowly stop being called. 634 // 635 // While it is being called, replicate a bit of a hack to propagate the 636 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 637 // header search paths with it. Once all systems are overriding this 638 // function, the CC1 flag and this line can be removed. 639 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 640 } 641 642 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 643 ArgStringList &CmdArgs) const { 644 CXXStdlibType Type = GetCXXStdlibType(Args); 645 646 switch (Type) { 647 case ToolChain::CST_Libcxx: 648 CmdArgs.push_back("-lc++"); 649 break; 650 651 case ToolChain::CST_Libstdcxx: 652 CmdArgs.push_back("-lstdc++"); 653 break; 654 } 655 } 656 657 void ToolChain::AddFilePathLibArgs(const ArgList &Args, 658 ArgStringList &CmdArgs) const { 659 for (const auto &LibPath : getFilePaths()) 660 if(LibPath.length() > 0) 661 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath)); 662 } 663 664 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 665 ArgStringList &CmdArgs) const { 666 CmdArgs.push_back("-lcc_kext"); 667 } 668 669 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 670 ArgStringList &CmdArgs) const { 671 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 672 // (to keep the linker options consistent with gcc and clang itself). 673 if (!isOptimizationLevelFast(Args)) { 674 // Check if -ffast-math or -funsafe-math. 675 Arg *A = 676 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 677 options::OPT_funsafe_math_optimizations, 678 options::OPT_fno_unsafe_math_optimizations); 679 680 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 681 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 682 return false; 683 } 684 // If crtfastmath.o exists add it to the arguments. 685 std::string Path = GetFilePath("crtfastmath.o"); 686 if (Path == "crtfastmath.o") // Not found. 687 return false; 688 689 CmdArgs.push_back(Args.MakeArgString(Path)); 690 return true; 691 } 692 693 SanitizerMask ToolChain::getSupportedSanitizers() const { 694 // Return sanitizers which don't require runtime support and are not 695 // platform dependent. 696 using namespace SanitizerKind; 697 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) | 698 CFICastStrict | UnsignedIntegerOverflow | LocalBounds; 699 if (getTriple().getArch() == llvm::Triple::x86 || 700 getTriple().getArch() == llvm::Triple::x86_64 || 701 getTriple().getArch() == llvm::Triple::wasm32 || 702 getTriple().getArch() == llvm::Triple::wasm64) 703 Res |= CFIICall; 704 return Res; 705 } 706 707 void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 708 ArgStringList &CC1Args) const {} 709 710 void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 711 ArgStringList &CC1Args) const {} 712