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