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 "ToolChains/CommonArgs.h" 12 #include "ToolChains/Arch/ARM.h" 13 #include "ToolChains/Clang.h" 14 #include "clang/Basic/ObjCRuntime.h" 15 #include "clang/Basic/VirtualFileSystem.h" 16 #include "clang/Config/config.h" 17 #include "clang/Driver/Action.h" 18 #include "clang/Driver/Driver.h" 19 #include "clang/Driver/DriverDiagnostic.h" 20 #include "clang/Driver/Options.h" 21 #include "clang/Driver/SanitizerArgs.h" 22 #include "clang/Driver/XRayArgs.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/Option/Arg.h" 25 #include "llvm/Option/ArgList.h" 26 #include "llvm/Option/Option.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/FileSystem.h" 29 #include "llvm/Support/Path.h" 30 #include "llvm/Support/TargetParser.h" 31 #include "llvm/Support/TargetRegistry.h" 32 33 using namespace clang::driver; 34 using namespace clang::driver::tools; 35 using namespace clang; 36 using namespace llvm; 37 using namespace llvm::opt; 38 39 static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) { 40 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext, 41 options::OPT_fno_rtti, options::OPT_frtti); 42 } 43 44 static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args, 45 const llvm::Triple &Triple, 46 const Arg *CachedRTTIArg) { 47 // Explicit rtti/no-rtti args 48 if (CachedRTTIArg) { 49 if (CachedRTTIArg->getOption().matches(options::OPT_frtti)) 50 return ToolChain::RM_EnabledExplicitly; 51 else 52 return ToolChain::RM_DisabledExplicitly; 53 } 54 55 // -frtti is default, except for the PS4 CPU. 56 if (!Triple.isPS4CPU()) 57 return ToolChain::RM_EnabledImplicitly; 58 59 // On the PS4, turning on c++ exceptions turns on rtti. 60 // We're assuming that, if we see -fexceptions, rtti gets turned on. 61 Arg *Exceptions = Args.getLastArgNoClaim( 62 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 63 options::OPT_fexceptions, options::OPT_fno_exceptions); 64 if (Exceptions && 65 (Exceptions->getOption().matches(options::OPT_fexceptions) || 66 Exceptions->getOption().matches(options::OPT_fcxx_exceptions))) 67 return ToolChain::RM_EnabledImplicitly; 68 69 return ToolChain::RM_DisabledImplicitly; 70 } 71 72 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T, 73 const ArgList &Args) 74 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)), 75 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)), 76 EffectiveTriple() { 77 if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) 78 if (!isThreadModelSupported(A->getValue())) 79 D.Diag(diag::err_drv_invalid_thread_model_for_target) 80 << A->getValue() << A->getAsString(Args); 81 82 std::string CandidateLibPath = getArchSpecificLibPath(); 83 if (getVFS().exists(CandidateLibPath)) 84 getFilePaths().push_back(CandidateLibPath); 85 } 86 87 ToolChain::~ToolChain() { 88 } 89 90 vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); } 91 92 bool ToolChain::useIntegratedAs() const { 93 return Args.hasFlag(options::OPT_fintegrated_as, 94 options::OPT_fno_integrated_as, 95 IsIntegratedAssemblerDefault()); 96 } 97 98 const SanitizerArgs& ToolChain::getSanitizerArgs() const { 99 if (!SanitizerArguments.get()) 100 SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 101 return *SanitizerArguments.get(); 102 } 103 104 const XRayArgs& ToolChain::getXRayArgs() const { 105 if (!XRayArguments.get()) 106 XRayArguments.reset(new XRayArgs(*this, Args)); 107 return *XRayArguments.get(); 108 } 109 110 namespace { 111 struct DriverSuffix { 112 const char *Suffix; 113 const char *ModeFlag; 114 }; 115 116 const DriverSuffix *FindDriverSuffix(StringRef ProgName) { 117 // A list of known driver suffixes. Suffixes are compared against the 118 // program name in order. If there is a match, the frontend type is updated as 119 // necessary by applying the ModeFlag. 120 static const DriverSuffix DriverSuffixes[] = { 121 {"clang", nullptr}, 122 {"clang++", "--driver-mode=g++"}, 123 {"clang-c++", "--driver-mode=g++"}, 124 {"clang-cc", nullptr}, 125 {"clang-cpp", "--driver-mode=cpp"}, 126 {"clang-g++", "--driver-mode=g++"}, 127 {"clang-gcc", nullptr}, 128 {"clang-cl", "--driver-mode=cl"}, 129 {"cc", nullptr}, 130 {"cpp", "--driver-mode=cpp"}, 131 {"cl", "--driver-mode=cl"}, 132 {"++", "--driver-mode=g++"}, 133 }; 134 135 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i) 136 if (ProgName.endswith(DriverSuffixes[i].Suffix)) 137 return &DriverSuffixes[i]; 138 return nullptr; 139 } 140 141 /// Normalize the program name from argv[0] by stripping the file extension if 142 /// present and lower-casing the string on Windows. 143 std::string normalizeProgramName(llvm::StringRef Argv0) { 144 std::string ProgName = llvm::sys::path::stem(Argv0); 145 #ifdef LLVM_ON_WIN32 146 // Transform to lowercase for case insensitive file systems. 147 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower); 148 #endif 149 return ProgName; 150 } 151 152 const DriverSuffix *parseDriverSuffix(StringRef ProgName) { 153 // Try to infer frontend type and default target from the program name by 154 // comparing it against DriverSuffixes in order. 155 156 // If there is a match, the function tries to identify a target as prefix. 157 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target 158 // prefix "x86_64-linux". If such a target prefix is found, it may be 159 // added via -target as implicit first argument. 160 const DriverSuffix *DS = FindDriverSuffix(ProgName); 161 162 if (!DS) { 163 // Try again after stripping any trailing version number: 164 // clang++3.5 -> clang++ 165 ProgName = ProgName.rtrim("0123456789."); 166 DS = FindDriverSuffix(ProgName); 167 } 168 169 if (!DS) { 170 // Try again after stripping trailing -component. 171 // clang++-tot -> clang++ 172 ProgName = ProgName.slice(0, ProgName.rfind('-')); 173 DS = FindDriverSuffix(ProgName); 174 } 175 return DS; 176 } 177 } // anonymous namespace 178 179 std::pair<std::string, std::string> 180 ToolChain::getTargetAndModeFromProgramName(StringRef PN) { 181 std::string ProgName = normalizeProgramName(PN); 182 const DriverSuffix *DS = parseDriverSuffix(ProgName); 183 if (!DS) 184 return std::make_pair("", ""); 185 std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag; 186 187 std::string::size_type LastComponent = 188 ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix)); 189 if (LastComponent == std::string::npos) 190 return std::make_pair("", ModeFlag); 191 192 // Infer target from the prefix. 193 StringRef Prefix(ProgName); 194 Prefix = Prefix.slice(0, LastComponent); 195 std::string IgnoredError; 196 std::string Target; 197 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) { 198 Target = Prefix; 199 } 200 return std::make_pair(Target, ModeFlag); 201 } 202 203 StringRef ToolChain::getDefaultUniversalArchName() const { 204 // In universal driver terms, the arch name accepted by -arch isn't exactly 205 // the same as the ones that appear in the triple. Roughly speaking, this is 206 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the 207 // only interesting special case is powerpc. 208 switch (Triple.getArch()) { 209 case llvm::Triple::ppc: 210 return "ppc"; 211 case llvm::Triple::ppc64: 212 return "ppc64"; 213 case llvm::Triple::ppc64le: 214 return "ppc64le"; 215 default: 216 return Triple.getArchName(); 217 } 218 } 219 220 bool ToolChain::IsUnwindTablesDefault(const ArgList &Args) const { 221 return false; 222 } 223 224 Tool *ToolChain::getClang() const { 225 if (!Clang) 226 Clang.reset(new tools::Clang(*this)); 227 return Clang.get(); 228 } 229 230 Tool *ToolChain::buildAssembler() const { 231 return new tools::ClangAs(*this); 232 } 233 234 Tool *ToolChain::buildLinker() const { 235 llvm_unreachable("Linking is not supported by this toolchain"); 236 } 237 238 Tool *ToolChain::getAssemble() const { 239 if (!Assemble) 240 Assemble.reset(buildAssembler()); 241 return Assemble.get(); 242 } 243 244 Tool *ToolChain::getClangAs() const { 245 if (!Assemble) 246 Assemble.reset(new tools::ClangAs(*this)); 247 return Assemble.get(); 248 } 249 250 Tool *ToolChain::getLink() const { 251 if (!Link) 252 Link.reset(buildLinker()); 253 return Link.get(); 254 } 255 256 Tool *ToolChain::getOffloadBundler() const { 257 if (!OffloadBundler) 258 OffloadBundler.reset(new tools::OffloadBundler(*this)); 259 return OffloadBundler.get(); 260 } 261 262 Tool *ToolChain::getTool(Action::ActionClass AC) const { 263 switch (AC) { 264 case Action::AssembleJobClass: 265 return getAssemble(); 266 267 case Action::LinkJobClass: 268 return getLink(); 269 270 case Action::InputClass: 271 case Action::BindArchClass: 272 case Action::OffloadClass: 273 case Action::LipoJobClass: 274 case Action::DsymutilJobClass: 275 case Action::VerifyDebugInfoJobClass: 276 llvm_unreachable("Invalid tool kind."); 277 278 case Action::CompileJobClass: 279 case Action::PrecompileJobClass: 280 case Action::PreprocessJobClass: 281 case Action::AnalyzeJobClass: 282 case Action::MigrateJobClass: 283 case Action::VerifyPCHJobClass: 284 case Action::BackendJobClass: 285 return getClang(); 286 287 case Action::OffloadBundlingJobClass: 288 case Action::OffloadUnbundlingJobClass: 289 return getOffloadBundler(); 290 } 291 292 llvm_unreachable("Invalid tool kind."); 293 } 294 295 static StringRef getArchNameForCompilerRTLib(const ToolChain &TC, 296 const ArgList &Args) { 297 const llvm::Triple &Triple = TC.getTriple(); 298 bool IsWindows = Triple.isOSWindows(); 299 300 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86) 301 return "i386"; 302 303 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb) 304 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows) 305 ? "armhf" 306 : "arm"; 307 308 return TC.getArchName(); 309 } 310 311 std::string ToolChain::getCompilerRTPath() const { 312 SmallString<128> Path(getDriver().ResourceDir); 313 StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS(); 314 llvm::sys::path::append(Path, "lib", OSLibName); 315 return Path.str(); 316 } 317 318 std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component, 319 bool Shared) const { 320 const llvm::Triple &TT = getTriple(); 321 const char *Env = TT.isAndroid() ? "-android" : ""; 322 bool IsITANMSVCWindows = 323 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment(); 324 325 StringRef Arch = getArchNameForCompilerRTLib(*this, Args); 326 const char *Prefix = IsITANMSVCWindows ? "" : "lib"; 327 const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so") 328 : (IsITANMSVCWindows ? ".lib" : ".a"); 329 330 SmallString<128> Path(getCompilerRTPath()); 331 llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" + 332 Arch + Env + Suffix); 333 return Path.str(); 334 } 335 336 const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args, 337 StringRef Component, 338 bool Shared) const { 339 return Args.MakeArgString(getCompilerRT(Args, Component, Shared)); 340 } 341 342 std::string ToolChain::getArchSpecificLibPath() const { 343 SmallString<128> Path(getDriver().ResourceDir); 344 StringRef OSLibName = getTriple().isOSFreeBSD() ? "freebsd" : getOS(); 345 llvm::sys::path::append(Path, "lib", OSLibName, 346 llvm::Triple::getArchTypeName(getArch())); 347 return Path.str(); 348 } 349 350 bool ToolChain::needsProfileRT(const ArgList &Args) { 351 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, 352 false) || 353 Args.hasArg(options::OPT_fprofile_generate) || 354 Args.hasArg(options::OPT_fprofile_generate_EQ) || 355 Args.hasArg(options::OPT_fprofile_instr_generate) || 356 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) || 357 Args.hasArg(options::OPT_fcreate_profile) || 358 Args.hasArg(options::OPT_coverage)) 359 return true; 360 361 return false; 362 } 363 364 Tool *ToolChain::SelectTool(const JobAction &JA) const { 365 if (getDriver().ShouldUseClangCompiler(JA)) return getClang(); 366 Action::ActionClass AC = JA.getKind(); 367 if (AC == Action::AssembleJobClass && useIntegratedAs()) 368 return getClangAs(); 369 return getTool(AC); 370 } 371 372 std::string ToolChain::GetFilePath(const char *Name) const { 373 return D.GetFilePath(Name, *this); 374 } 375 376 std::string ToolChain::GetProgramPath(const char *Name) const { 377 return D.GetProgramPath(Name, *this); 378 } 379 380 std::string ToolChain::GetLinkerPath() const { 381 const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ); 382 StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER; 383 384 if (llvm::sys::path::is_absolute(UseLinker)) { 385 // If we're passed what looks like an absolute path, don't attempt to 386 // second-guess that. 387 if (llvm::sys::fs::exists(UseLinker)) 388 return UseLinker; 389 } else if (UseLinker.empty() || UseLinker == "ld") { 390 // If we're passed -fuse-ld= with no argument, or with the argument ld, 391 // then use whatever the default system linker is. 392 return GetProgramPath(getDefaultLinker()); 393 } else { 394 llvm::SmallString<8> LinkerName("ld."); 395 LinkerName.append(UseLinker); 396 397 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 398 if (llvm::sys::fs::exists(LinkerPath)) 399 return LinkerPath; 400 } 401 402 if (A) 403 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 404 405 return GetProgramPath(getDefaultLinker()); 406 } 407 408 types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const { 409 return types::lookupTypeForExtension(Ext); 410 } 411 412 bool ToolChain::HasNativeLLVMSupport() const { 413 return false; 414 } 415 416 bool ToolChain::isCrossCompiling() const { 417 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 418 switch (HostTriple.getArch()) { 419 // The A32/T32/T16 instruction sets are not separate architectures in this 420 // context. 421 case llvm::Triple::arm: 422 case llvm::Triple::armeb: 423 case llvm::Triple::thumb: 424 case llvm::Triple::thumbeb: 425 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 426 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 427 default: 428 return HostTriple.getArch() != getArch(); 429 } 430 } 431 432 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 433 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 434 VersionTuple()); 435 } 436 437 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 438 if (Model == "single") { 439 // FIXME: 'single' is only supported on ARM and WebAssembly so far. 440 return Triple.getArch() == llvm::Triple::arm || 441 Triple.getArch() == llvm::Triple::armeb || 442 Triple.getArch() == llvm::Triple::thumb || 443 Triple.getArch() == llvm::Triple::thumbeb || 444 Triple.getArch() == llvm::Triple::wasm32 || 445 Triple.getArch() == llvm::Triple::wasm64; 446 } else if (Model == "posix") 447 return true; 448 449 return false; 450 } 451 452 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 453 types::ID InputType) const { 454 switch (getTriple().getArch()) { 455 default: 456 return getTripleString(); 457 458 case llvm::Triple::x86_64: { 459 llvm::Triple Triple = getTriple(); 460 if (!Triple.isOSBinFormatMachO()) 461 return getTripleString(); 462 463 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 464 // x86_64h goes in the triple. Other -march options just use the 465 // vanilla triple we already have. 466 StringRef MArch = A->getValue(); 467 if (MArch == "x86_64h") 468 Triple.setArchName(MArch); 469 } 470 return Triple.getTriple(); 471 } 472 case llvm::Triple::aarch64: { 473 llvm::Triple Triple = getTriple(); 474 if (!Triple.isOSBinFormatMachO()) 475 return getTripleString(); 476 477 // FIXME: older versions of ld64 expect the "arm64" component in the actual 478 // triple string and query it to determine whether an LTO file can be 479 // handled. Remove this when we don't care any more. 480 Triple.setArchName("arm64"); 481 return Triple.getTriple(); 482 } 483 case llvm::Triple::arm: 484 case llvm::Triple::armeb: 485 case llvm::Triple::thumb: 486 case llvm::Triple::thumbeb: { 487 // FIXME: Factor into subclasses. 488 llvm::Triple Triple = getTriple(); 489 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || 490 getTriple().getArch() == llvm::Triple::thumbeb; 491 492 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 493 // '-mbig-endian'/'-EB'. 494 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 495 options::OPT_mbig_endian)) { 496 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 497 } 498 499 // Thumb2 is the default for V7 on Darwin. 500 // 501 // FIXME: Thumb should just be another -target-feaure, not in the triple. 502 StringRef MCPU, MArch; 503 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 504 MCPU = A->getValue(); 505 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 506 MArch = A->getValue(); 507 std::string CPU = 508 Triple.isOSBinFormatMachO() 509 ? tools::arm::getARMCPUForMArch(MArch, Triple).str() 510 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); 511 StringRef Suffix = 512 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 513 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::ProfileKind::M; 514 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 && 515 getTriple().isOSBinFormatMachO()); 516 // FIXME: this is invalid for WindowsCE 517 if (getTriple().isOSWindows()) 518 ThumbDefault = true; 519 std::string ArchName; 520 if (IsBigEndian) 521 ArchName = "armeb"; 522 else 523 ArchName = "arm"; 524 525 // Check if ARM ISA was explicitly selected (using -mno-thumb or -marm) for 526 // M-Class CPUs/architecture variants, which is not supported. 527 bool ARMModeRequested = !Args.hasFlag(options::OPT_mthumb, 528 options::OPT_mno_thumb, ThumbDefault); 529 if (IsMProfile && ARMModeRequested) { 530 if (!MCPU.empty()) 531 getDriver().Diag(diag::err_cpu_unsupported_isa) << CPU << "ARM"; 532 else 533 getDriver().Diag(diag::err_arch_unsupported_isa) 534 << tools::arm::getARMArch(MArch, getTriple()) << "ARM"; 535 } 536 537 // Assembly files should start in ARM mode, unless arch is M-profile. 538 // Windows is always thumb. 539 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb, 540 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile || 541 getTriple().isOSWindows()) { 542 if (IsBigEndian) 543 ArchName = "thumbeb"; 544 else 545 ArchName = "thumb"; 546 } 547 Triple.setArchName(ArchName + Suffix.str()); 548 549 return Triple.getTriple(); 550 } 551 } 552 } 553 554 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 555 types::ID InputType) const { 556 return ComputeLLVMTriple(Args, InputType); 557 } 558 559 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 560 ArgStringList &CC1Args) const { 561 // Each toolchain should provide the appropriate include flags. 562 } 563 564 void ToolChain::addClangTargetOptions( 565 const ArgList &DriverArgs, ArgStringList &CC1Args, 566 Action::OffloadKind DeviceOffloadKind) const {} 567 568 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 569 570 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, 571 llvm::opt::ArgStringList &CmdArgs) const { 572 if (!needsProfileRT(Args)) return; 573 574 CmdArgs.push_back(getCompilerRTArgString(Args, "profile")); 575 } 576 577 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 578 const ArgList &Args) const { 579 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ); 580 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB; 581 582 // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB! 583 if (LibName == "compiler-rt") 584 return ToolChain::RLT_CompilerRT; 585 else if (LibName == "libgcc") 586 return ToolChain::RLT_Libgcc; 587 else if (LibName == "platform") 588 return GetDefaultRuntimeLibType(); 589 590 if (A) 591 getDriver().Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args); 592 593 return GetDefaultRuntimeLibType(); 594 } 595 596 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 597 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ); 598 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB; 599 600 // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB! 601 if (LibName == "libc++") 602 return ToolChain::CST_Libcxx; 603 else if (LibName == "libstdc++") 604 return ToolChain::CST_Libstdcxx; 605 else if (LibName == "platform") 606 return GetDefaultCXXStdlibType(); 607 608 if (A) 609 getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args); 610 611 return GetDefaultCXXStdlibType(); 612 } 613 614 /// \brief Utility function to add a system include directory to CC1 arguments. 615 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 616 ArgStringList &CC1Args, 617 const Twine &Path) { 618 CC1Args.push_back("-internal-isystem"); 619 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 620 } 621 622 /// \brief Utility function to add a system include directory with extern "C" 623 /// semantics to CC1 arguments. 624 /// 625 /// Note that this should be used rarely, and only for directories that 626 /// historically and for legacy reasons are treated as having implicit extern 627 /// "C" semantics. These semantics are *ignored* by and large today, but its 628 /// important to preserve the preprocessor changes resulting from the 629 /// classification. 630 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 631 ArgStringList &CC1Args, 632 const Twine &Path) { 633 CC1Args.push_back("-internal-externc-isystem"); 634 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 635 } 636 637 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 638 ArgStringList &CC1Args, 639 const Twine &Path) { 640 if (llvm::sys::fs::exists(Path)) 641 addExternCSystemInclude(DriverArgs, CC1Args, Path); 642 } 643 644 /// \brief Utility function to add a list of system include directories to CC1. 645 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 646 ArgStringList &CC1Args, 647 ArrayRef<StringRef> Paths) { 648 for (StringRef Path : Paths) { 649 CC1Args.push_back("-internal-isystem"); 650 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 651 } 652 } 653 654 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 655 ArgStringList &CC1Args) const { 656 // Header search paths should be handled by each of the subclasses. 657 // Historically, they have not been, and instead have been handled inside of 658 // the CC1-layer frontend. As the logic is hoisted out, this generic function 659 // will slowly stop being called. 660 // 661 // While it is being called, replicate a bit of a hack to propagate the 662 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 663 // header search paths with it. Once all systems are overriding this 664 // function, the CC1 flag and this line can be removed. 665 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 666 } 667 668 bool ToolChain::ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const { 669 return getDriver().CCCIsCXX() && 670 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, 671 options::OPT_nostdlibxx); 672 } 673 674 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 675 ArgStringList &CmdArgs) const { 676 assert(!Args.hasArg(options::OPT_nostdlibxx) && 677 "should not have called this"); 678 CXXStdlibType Type = GetCXXStdlibType(Args); 679 680 switch (Type) { 681 case ToolChain::CST_Libcxx: 682 CmdArgs.push_back("-lc++"); 683 break; 684 685 case ToolChain::CST_Libstdcxx: 686 CmdArgs.push_back("-lstdc++"); 687 break; 688 } 689 } 690 691 void ToolChain::AddFilePathLibArgs(const ArgList &Args, 692 ArgStringList &CmdArgs) const { 693 for (const auto &LibPath : getFilePaths()) 694 if(LibPath.length() > 0) 695 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath)); 696 } 697 698 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 699 ArgStringList &CmdArgs) const { 700 CmdArgs.push_back("-lcc_kext"); 701 } 702 703 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 704 ArgStringList &CmdArgs) const { 705 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 706 // (to keep the linker options consistent with gcc and clang itself). 707 if (!isOptimizationLevelFast(Args)) { 708 // Check if -ffast-math or -funsafe-math. 709 Arg *A = 710 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 711 options::OPT_funsafe_math_optimizations, 712 options::OPT_fno_unsafe_math_optimizations); 713 714 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 715 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 716 return false; 717 } 718 // If crtfastmath.o exists add it to the arguments. 719 std::string Path = GetFilePath("crtfastmath.o"); 720 if (Path == "crtfastmath.o") // Not found. 721 return false; 722 723 CmdArgs.push_back(Args.MakeArgString(Path)); 724 return true; 725 } 726 727 SanitizerMask ToolChain::getSupportedSanitizers() const { 728 // Return sanitizers which don't require runtime support and are not 729 // platform dependent. 730 using namespace SanitizerKind; 731 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) | 732 CFICastStrict | UnsignedIntegerOverflow | Nullability | 733 LocalBounds; 734 if (getTriple().getArch() == llvm::Triple::x86 || 735 getTriple().getArch() == llvm::Triple::x86_64 || 736 getTriple().getArch() == llvm::Triple::arm || 737 getTriple().getArch() == llvm::Triple::aarch64 || 738 getTriple().getArch() == llvm::Triple::wasm32 || 739 getTriple().getArch() == llvm::Triple::wasm64) 740 Res |= CFIICall; 741 return Res; 742 } 743 744 void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 745 ArgStringList &CC1Args) const {} 746 747 void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 748 ArgStringList &CC1Args) const {} 749 750 static VersionTuple separateMSVCFullVersion(unsigned Version) { 751 if (Version < 100) 752 return VersionTuple(Version); 753 754 if (Version < 10000) 755 return VersionTuple(Version / 100, Version % 100); 756 757 unsigned Build = 0, Factor = 1; 758 for (; Version > 10000; Version = Version / 10, Factor = Factor * 10) 759 Build = Build + (Version % 10) * Factor; 760 return VersionTuple(Version / 100, Version % 100, Build); 761 } 762 763 VersionTuple 764 ToolChain::computeMSVCVersion(const Driver *D, 765 const llvm::opt::ArgList &Args) const { 766 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version); 767 const Arg *MSCompatibilityVersion = 768 Args.getLastArg(options::OPT_fms_compatibility_version); 769 770 if (MSCVersion && MSCompatibilityVersion) { 771 if (D) 772 D->Diag(diag::err_drv_argument_not_allowed_with) 773 << MSCVersion->getAsString(Args) 774 << MSCompatibilityVersion->getAsString(Args); 775 return VersionTuple(); 776 } 777 778 if (MSCompatibilityVersion) { 779 VersionTuple MSVT; 780 if (MSVT.tryParse(MSCompatibilityVersion->getValue())) { 781 if (D) 782 D->Diag(diag::err_drv_invalid_value) 783 << MSCompatibilityVersion->getAsString(Args) 784 << MSCompatibilityVersion->getValue(); 785 } else { 786 return MSVT; 787 } 788 } 789 790 if (MSCVersion) { 791 unsigned Version = 0; 792 if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) { 793 if (D) 794 D->Diag(diag::err_drv_invalid_value) 795 << MSCVersion->getAsString(Args) << MSCVersion->getValue(); 796 } else { 797 return separateMSVCFullVersion(Version); 798 } 799 } 800 801 return VersionTuple(); 802 } 803 804 llvm::opt::DerivedArgList * 805 ToolChain::TranslateOpenMPTargetArgs(const llvm::opt::DerivedArgList &Args, 806 Action::OffloadKind DeviceOffloadKind) const { 807 if (DeviceOffloadKind == Action::OFK_OpenMP) { 808 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); 809 const OptTable &Opts = getDriver().getOpts(); 810 bool NewArgAdded = false; 811 812 // Handle -Xopenmp-target flags 813 for (Arg *A : Args) { 814 // Exclude flags which may only apply to the host toolchain. 815 // Do not exclude flags when the host triple (AuxTriple), 816 // matches the current toolchain triple. 817 if (A->getOption().matches(options::OPT_m_Group)) { 818 if (getAuxTriple() && getAuxTriple()->str() == getTriple().str()) 819 DAL->append(A); 820 continue; 821 } 822 823 unsigned Index; 824 unsigned Prev; 825 bool XOpenMPTargetNoTriple = A->getOption().matches( 826 options::OPT_Xopenmp_target); 827 828 if (A->getOption().matches(options::OPT_Xopenmp_target_EQ)) { 829 // Passing device args: -Xopenmp-target=<triple> -opt=val. 830 if (A->getValue(0) == getTripleString()) 831 Index = Args.getBaseArgs().MakeIndex(A->getValue(1)); 832 else 833 continue; 834 } else if (XOpenMPTargetNoTriple) { 835 // Passing device args: -Xopenmp-target -opt=val. 836 Index = Args.getBaseArgs().MakeIndex(A->getValue(0)); 837 } else { 838 DAL->append(A); 839 continue; 840 } 841 842 // Parse the argument to -Xopenmp-target. 843 Prev = Index; 844 std::unique_ptr<Arg> XOpenMPTargetArg(Opts.ParseOneArg(Args, Index)); 845 if (!XOpenMPTargetArg || Index > Prev + 1) { 846 getDriver().Diag(diag::err_drv_invalid_Xopenmp_target_with_args) 847 << A->getAsString(Args); 848 continue; 849 } 850 if (XOpenMPTargetNoTriple && XOpenMPTargetArg && 851 Args.getAllArgValues( 852 options::OPT_fopenmp_targets_EQ).size() != 1) { 853 getDriver().Diag(diag::err_drv_Xopenmp_target_missing_triple); 854 continue; 855 } 856 XOpenMPTargetArg->setBaseArg(A); 857 A = XOpenMPTargetArg.release(); 858 DAL->append(A); 859 NewArgAdded = true; 860 } 861 862 if (NewArgAdded) { 863 return DAL; 864 } else { 865 delete DAL; 866 } 867 } 868 869 return nullptr; 870 } 871