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