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::getOffloadBundler() const { 243 if (!OffloadBundler) 244 OffloadBundler.reset(new tools::OffloadBundler(*this)); 245 return OffloadBundler.get(); 246 } 247 248 Tool *ToolChain::getTool(Action::ActionClass AC) const { 249 switch (AC) { 250 case Action::AssembleJobClass: 251 return getAssemble(); 252 253 case Action::LinkJobClass: 254 return getLink(); 255 256 case Action::InputClass: 257 case Action::BindArchClass: 258 case Action::OffloadClass: 259 case Action::LipoJobClass: 260 case Action::DsymutilJobClass: 261 case Action::VerifyDebugInfoJobClass: 262 llvm_unreachable("Invalid tool kind."); 263 264 case Action::CompileJobClass: 265 case Action::PrecompileJobClass: 266 case Action::PreprocessJobClass: 267 case Action::AnalyzeJobClass: 268 case Action::MigrateJobClass: 269 case Action::VerifyPCHJobClass: 270 case Action::BackendJobClass: 271 return getClang(); 272 273 case Action::OffloadBundlingJobClass: 274 case Action::OffloadUnbundlingJobClass: 275 return getOffloadBundler(); 276 } 277 278 llvm_unreachable("Invalid tool kind."); 279 } 280 281 static StringRef getArchNameForCompilerRTLib(const ToolChain &TC, 282 const ArgList &Args) { 283 const llvm::Triple &Triple = TC.getTriple(); 284 bool IsWindows = Triple.isOSWindows(); 285 286 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86) 287 return "i386"; 288 289 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb) 290 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows) 291 ? "armhf" 292 : "arm"; 293 294 return TC.getArchName(); 295 } 296 297 std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component, 298 bool Shared) const { 299 const llvm::Triple &TT = getTriple(); 300 const char *Env = TT.isAndroid() ? "-android" : ""; 301 bool IsITANMSVCWindows = 302 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment(); 303 304 StringRef Arch = getArchNameForCompilerRTLib(*this, Args); 305 const char *Prefix = IsITANMSVCWindows ? "" : "lib"; 306 const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so") 307 : (IsITANMSVCWindows ? ".lib" : ".a"); 308 309 SmallString<128> Path(getDriver().ResourceDir); 310 StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS(); 311 llvm::sys::path::append(Path, "lib", OSLibName); 312 llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" + 313 Arch + Env + Suffix); 314 return Path.str(); 315 } 316 317 const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args, 318 StringRef Component, 319 bool Shared) const { 320 return Args.MakeArgString(getCompilerRT(Args, Component, Shared)); 321 } 322 323 bool ToolChain::needsProfileRT(const ArgList &Args) { 324 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, 325 false) || 326 Args.hasArg(options::OPT_fprofile_generate) || 327 Args.hasArg(options::OPT_fprofile_generate_EQ) || 328 Args.hasArg(options::OPT_fprofile_instr_generate) || 329 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) || 330 Args.hasArg(options::OPT_fcreate_profile) || 331 Args.hasArg(options::OPT_coverage)) 332 return true; 333 334 return false; 335 } 336 337 Tool *ToolChain::SelectTool(const JobAction &JA) const { 338 if (getDriver().ShouldUseClangCompiler(JA)) return getClang(); 339 Action::ActionClass AC = JA.getKind(); 340 if (AC == Action::AssembleJobClass && useIntegratedAs()) 341 return getClangAs(); 342 return getTool(AC); 343 } 344 345 std::string ToolChain::GetFilePath(const char *Name) const { 346 return D.GetFilePath(Name, *this); 347 } 348 349 std::string ToolChain::GetProgramPath(const char *Name) const { 350 return D.GetProgramPath(Name, *this); 351 } 352 353 std::string ToolChain::GetLinkerPath() const { 354 const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ); 355 StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER; 356 357 if (llvm::sys::path::is_absolute(UseLinker)) { 358 // If we're passed what looks like an absolute path, don't attempt to 359 // second-guess that. 360 if (llvm::sys::fs::exists(UseLinker)) 361 return UseLinker; 362 } else if (UseLinker.empty() || UseLinker == "ld") { 363 // If we're passed -fuse-ld= with no argument, or with the argument ld, 364 // then use whatever the default system linker is. 365 return GetProgramPath(getDefaultLinker()); 366 } else { 367 llvm::SmallString<8> LinkerName("ld."); 368 LinkerName.append(UseLinker); 369 370 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 371 if (llvm::sys::fs::exists(LinkerPath)) 372 return LinkerPath; 373 } 374 375 if (A) 376 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 377 378 return GetProgramPath(getDefaultLinker()); 379 } 380 381 types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const { 382 return types::lookupTypeForExtension(Ext); 383 } 384 385 bool ToolChain::HasNativeLLVMSupport() const { 386 return false; 387 } 388 389 bool ToolChain::isCrossCompiling() const { 390 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 391 switch (HostTriple.getArch()) { 392 // The A32/T32/T16 instruction sets are not separate architectures in this 393 // context. 394 case llvm::Triple::arm: 395 case llvm::Triple::armeb: 396 case llvm::Triple::thumb: 397 case llvm::Triple::thumbeb: 398 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 399 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 400 default: 401 return HostTriple.getArch() != getArch(); 402 } 403 } 404 405 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 406 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 407 VersionTuple()); 408 } 409 410 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 411 if (Model == "single") { 412 // FIXME: 'single' is only supported on ARM and WebAssembly so far. 413 return Triple.getArch() == llvm::Triple::arm || 414 Triple.getArch() == llvm::Triple::armeb || 415 Triple.getArch() == llvm::Triple::thumb || 416 Triple.getArch() == llvm::Triple::thumbeb || 417 Triple.getArch() == llvm::Triple::wasm32 || 418 Triple.getArch() == llvm::Triple::wasm64; 419 } else if (Model == "posix") 420 return true; 421 422 return false; 423 } 424 425 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 426 types::ID InputType) const { 427 switch (getTriple().getArch()) { 428 default: 429 return getTripleString(); 430 431 case llvm::Triple::x86_64: { 432 llvm::Triple Triple = getTriple(); 433 if (!Triple.isOSBinFormatMachO()) 434 return getTripleString(); 435 436 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 437 // x86_64h goes in the triple. Other -march options just use the 438 // vanilla triple we already have. 439 StringRef MArch = A->getValue(); 440 if (MArch == "x86_64h") 441 Triple.setArchName(MArch); 442 } 443 return Triple.getTriple(); 444 } 445 case llvm::Triple::aarch64: { 446 llvm::Triple Triple = getTriple(); 447 if (!Triple.isOSBinFormatMachO()) 448 return getTripleString(); 449 450 // FIXME: older versions of ld64 expect the "arm64" component in the actual 451 // triple string and query it to determine whether an LTO file can be 452 // handled. Remove this when we don't care any more. 453 Triple.setArchName("arm64"); 454 return Triple.getTriple(); 455 } 456 case llvm::Triple::arm: 457 case llvm::Triple::armeb: 458 case llvm::Triple::thumb: 459 case llvm::Triple::thumbeb: { 460 // FIXME: Factor into subclasses. 461 llvm::Triple Triple = getTriple(); 462 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || 463 getTriple().getArch() == llvm::Triple::thumbeb; 464 465 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 466 // '-mbig-endian'/'-EB'. 467 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 468 options::OPT_mbig_endian)) { 469 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 470 } 471 472 // Thumb2 is the default for V7 on Darwin. 473 // 474 // FIXME: Thumb should just be another -target-feaure, not in the triple. 475 StringRef MCPU, MArch; 476 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 477 MCPU = A->getValue(); 478 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 479 MArch = A->getValue(); 480 std::string CPU = 481 Triple.isOSBinFormatMachO() 482 ? tools::arm::getARMCPUForMArch(MArch, Triple).str() 483 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); 484 StringRef Suffix = 485 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 486 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M; 487 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 && 488 getTriple().isOSBinFormatMachO()); 489 // FIXME: this is invalid for WindowsCE 490 if (getTriple().isOSWindows()) 491 ThumbDefault = true; 492 std::string ArchName; 493 if (IsBigEndian) 494 ArchName = "armeb"; 495 else 496 ArchName = "arm"; 497 498 // Assembly files should start in ARM mode, unless arch is M-profile. 499 // Windows is always thumb. 500 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb, 501 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile || 502 getTriple().isOSWindows()) { 503 if (IsBigEndian) 504 ArchName = "thumbeb"; 505 else 506 ArchName = "thumb"; 507 } 508 Triple.setArchName(ArchName + Suffix.str()); 509 510 return Triple.getTriple(); 511 } 512 } 513 } 514 515 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 516 types::ID InputType) const { 517 return ComputeLLVMTriple(Args, InputType); 518 } 519 520 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 521 ArgStringList &CC1Args) const { 522 // Each toolchain should provide the appropriate include flags. 523 } 524 525 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 526 ArgStringList &CC1Args) const { 527 } 528 529 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 530 531 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, 532 llvm::opt::ArgStringList &CmdArgs) const { 533 if (!needsProfileRT(Args)) return; 534 535 CmdArgs.push_back(getCompilerRTArgString(Args, "profile")); 536 } 537 538 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 539 const ArgList &Args) const { 540 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ); 541 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB; 542 543 // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB! 544 if (LibName == "compiler-rt") 545 return ToolChain::RLT_CompilerRT; 546 else if (LibName == "libgcc") 547 return ToolChain::RLT_Libgcc; 548 else if (LibName == "platform") 549 return GetDefaultRuntimeLibType(); 550 551 if (A) 552 getDriver().Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args); 553 554 return GetDefaultRuntimeLibType(); 555 } 556 557 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 558 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ); 559 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB; 560 561 // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB! 562 if (LibName == "libc++") 563 return ToolChain::CST_Libcxx; 564 else if (LibName == "libstdc++") 565 return ToolChain::CST_Libstdcxx; 566 else if (LibName == "platform") 567 return GetDefaultCXXStdlibType(); 568 569 if (A) 570 getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args); 571 572 return GetDefaultCXXStdlibType(); 573 } 574 575 /// \brief Utility function to add a system include directory to CC1 arguments. 576 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 577 ArgStringList &CC1Args, 578 const Twine &Path) { 579 CC1Args.push_back("-internal-isystem"); 580 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 581 } 582 583 /// \brief Utility function to add a system include directory with extern "C" 584 /// semantics to CC1 arguments. 585 /// 586 /// Note that this should be used rarely, and only for directories that 587 /// historically and for legacy reasons are treated as having implicit extern 588 /// "C" semantics. These semantics are *ignored* by and large today, but its 589 /// important to preserve the preprocessor changes resulting from the 590 /// classification. 591 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 592 ArgStringList &CC1Args, 593 const Twine &Path) { 594 CC1Args.push_back("-internal-externc-isystem"); 595 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 596 } 597 598 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 599 ArgStringList &CC1Args, 600 const Twine &Path) { 601 if (llvm::sys::fs::exists(Path)) 602 addExternCSystemInclude(DriverArgs, CC1Args, Path); 603 } 604 605 /// \brief Utility function to add a list of system include directories to CC1. 606 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 607 ArgStringList &CC1Args, 608 ArrayRef<StringRef> Paths) { 609 for (StringRef Path : Paths) { 610 CC1Args.push_back("-internal-isystem"); 611 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 612 } 613 } 614 615 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 616 ArgStringList &CC1Args) const { 617 // Header search paths should be handled by each of the subclasses. 618 // Historically, they have not been, and instead have been handled inside of 619 // the CC1-layer frontend. As the logic is hoisted out, this generic function 620 // will slowly stop being called. 621 // 622 // While it is being called, replicate a bit of a hack to propagate the 623 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 624 // header search paths with it. Once all systems are overriding this 625 // function, the CC1 flag and this line can be removed. 626 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 627 } 628 629 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 630 ArgStringList &CmdArgs) const { 631 CXXStdlibType Type = GetCXXStdlibType(Args); 632 633 switch (Type) { 634 case ToolChain::CST_Libcxx: 635 CmdArgs.push_back("-lc++"); 636 break; 637 638 case ToolChain::CST_Libstdcxx: 639 CmdArgs.push_back("-lstdc++"); 640 break; 641 } 642 } 643 644 void ToolChain::AddFilePathLibArgs(const ArgList &Args, 645 ArgStringList &CmdArgs) const { 646 for (const auto &LibPath : getFilePaths()) 647 if(LibPath.length() > 0) 648 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath)); 649 } 650 651 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 652 ArgStringList &CmdArgs) const { 653 CmdArgs.push_back("-lcc_kext"); 654 } 655 656 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 657 ArgStringList &CmdArgs) const { 658 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 659 // (to keep the linker options consistent with gcc and clang itself). 660 if (!isOptimizationLevelFast(Args)) { 661 // Check if -ffast-math or -funsafe-math. 662 Arg *A = 663 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 664 options::OPT_funsafe_math_optimizations, 665 options::OPT_fno_unsafe_math_optimizations); 666 667 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 668 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 669 return false; 670 } 671 // If crtfastmath.o exists add it to the arguments. 672 std::string Path = GetFilePath("crtfastmath.o"); 673 if (Path == "crtfastmath.o") // Not found. 674 return false; 675 676 CmdArgs.push_back(Args.MakeArgString(Path)); 677 return true; 678 } 679 680 SanitizerMask ToolChain::getSupportedSanitizers() const { 681 // Return sanitizers which don't require runtime support and are not 682 // platform dependent. 683 using namespace SanitizerKind; 684 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) | 685 CFICastStrict | UnsignedIntegerOverflow | LocalBounds; 686 if (getTriple().getArch() == llvm::Triple::x86 || 687 getTriple().getArch() == llvm::Triple::x86_64 || 688 getTriple().getArch() == llvm::Triple::arm || 689 getTriple().getArch() == llvm::Triple::aarch64 || 690 getTriple().getArch() == llvm::Triple::wasm32 || 691 getTriple().getArch() == llvm::Triple::wasm64) 692 Res |= CFIICall; 693 return Res; 694 } 695 696 void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 697 ArgStringList &CC1Args) const {} 698 699 void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 700 ArgStringList &CC1Args) const {} 701 702 static VersionTuple separateMSVCFullVersion(unsigned Version) { 703 if (Version < 100) 704 return VersionTuple(Version); 705 706 if (Version < 10000) 707 return VersionTuple(Version / 100, Version % 100); 708 709 unsigned Build = 0, Factor = 1; 710 for (; Version > 10000; Version = Version / 10, Factor = Factor * 10) 711 Build = Build + (Version % 10) * Factor; 712 return VersionTuple(Version / 100, Version % 100, Build); 713 } 714 715 VersionTuple 716 ToolChain::computeMSVCVersion(const Driver *D, 717 const llvm::opt::ArgList &Args) const { 718 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version); 719 const Arg *MSCompatibilityVersion = 720 Args.getLastArg(options::OPT_fms_compatibility_version); 721 722 if (MSCVersion && MSCompatibilityVersion) { 723 if (D) 724 D->Diag(diag::err_drv_argument_not_allowed_with) 725 << MSCVersion->getAsString(Args) 726 << MSCompatibilityVersion->getAsString(Args); 727 return VersionTuple(); 728 } 729 730 if (MSCompatibilityVersion) { 731 VersionTuple MSVT; 732 if (MSVT.tryParse(MSCompatibilityVersion->getValue())) { 733 if (D) 734 D->Diag(diag::err_drv_invalid_value) 735 << MSCompatibilityVersion->getAsString(Args) 736 << MSCompatibilityVersion->getValue(); 737 } else { 738 return MSVT; 739 } 740 } 741 742 if (MSCVersion) { 743 unsigned Version = 0; 744 if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) { 745 if (D) 746 D->Diag(diag::err_drv_invalid_value) 747 << MSCVersion->getAsString(Args) << MSCVersion->getValue(); 748 } else { 749 return separateMSVCFullVersion(Version); 750 } 751 } 752 753 return VersionTuple(); 754 } 755