1 //===--- ToolChains.cpp - ToolChain Implementations -----------------------===// 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 "ToolChains.h" 11 #include "clang/Basic/ObjCRuntime.h" 12 #include "clang/Basic/Version.h" 13 #include "clang/Config/config.h" // for GCC_INSTALL_PREFIX 14 #include "clang/Driver/Compilation.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/STLExtras.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/StringSwitch.h" 23 #include "llvm/Option/Arg.h" 24 #include "llvm/Option/ArgList.h" 25 #include "llvm/Option/OptTable.h" 26 #include "llvm/Option/Option.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/FileSystem.h" 29 #include "llvm/Support/MemoryBuffer.h" 30 #include "llvm/Support/Path.h" 31 #include "llvm/Support/Program.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include <cstdlib> // ::getenv 34 #include <system_error> 35 36 using namespace clang::driver; 37 using namespace clang::driver::toolchains; 38 using namespace clang; 39 using namespace llvm::opt; 40 41 MachO::MachO(const Driver &D, const llvm::Triple &Triple, 42 const ArgList &Args) 43 : ToolChain(D, Triple, Args) { 44 getProgramPaths().push_back(getDriver().getInstalledDir()); 45 if (getDriver().getInstalledDir() != getDriver().Dir) 46 getProgramPaths().push_back(getDriver().Dir); 47 48 // We expect 'as', 'ld', etc. to be adjacent to our install dir. 49 getProgramPaths().push_back(getDriver().getInstalledDir()); 50 if (getDriver().getInstalledDir() != getDriver().Dir) 51 getProgramPaths().push_back(getDriver().Dir); 52 } 53 54 /// Darwin - Darwin tool chain for i386 and x86_64. 55 Darwin::Darwin(const Driver & D, const llvm::Triple & Triple, 56 const ArgList & Args) 57 : MachO(D, Triple, Args), TargetInitialized(false) { 58 // Compute the initial Darwin version from the triple 59 unsigned Major, Minor, Micro; 60 if (!Triple.getMacOSXVersion(Major, Minor, Micro)) 61 getDriver().Diag(diag::err_drv_invalid_darwin_version) << 62 Triple.getOSName(); 63 llvm::raw_string_ostream(MacosxVersionMin) 64 << Major << '.' << Minor << '.' << Micro; 65 66 // FIXME: DarwinVersion is only used to find GCC's libexec directory. 67 // It should be removed when we stop supporting that. 68 DarwinVersion[0] = Minor + 4; 69 DarwinVersion[1] = Micro; 70 DarwinVersion[2] = 0; 71 72 // Compute the initial iOS version from the triple 73 Triple.getiOSVersion(Major, Minor, Micro); 74 llvm::raw_string_ostream(iOSVersionMin) 75 << Major << '.' << Minor << '.' << Micro; 76 } 77 78 types::ID MachO::LookupTypeForExtension(const char *Ext) const { 79 types::ID Ty = types::lookupTypeForExtension(Ext); 80 81 // Darwin always preprocesses assembly files (unless -x is used explicitly). 82 if (Ty == types::TY_PP_Asm) 83 return types::TY_Asm; 84 85 return Ty; 86 } 87 88 bool MachO::HasNativeLLVMSupport() const { 89 return true; 90 } 91 92 /// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0. 93 ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const { 94 if (isTargetIOSBased()) 95 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion); 96 if (isNonFragile) 97 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion); 98 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion); 99 } 100 101 /// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2. 102 bool Darwin::hasBlocksRuntime() const { 103 if (isTargetIOSBased()) 104 return !isIPhoneOSVersionLT(3, 2); 105 else { 106 assert(isTargetMacOS() && "unexpected darwin target"); 107 return !isMacosxVersionLT(10, 6); 108 } 109 } 110 111 static const char *GetArmArchForMArch(StringRef Value) { 112 return llvm::StringSwitch<const char*>(Value) 113 .Case("armv6k", "armv6") 114 .Case("armv6m", "armv6m") 115 .Case("armv5tej", "armv5") 116 .Case("xscale", "xscale") 117 .Case("armv4t", "armv4t") 118 .Case("armv7", "armv7") 119 .Cases("armv7a", "armv7-a", "armv7") 120 .Cases("armv7r", "armv7-r", "armv7") 121 .Cases("armv7em", "armv7e-m", "armv7em") 122 .Cases("armv7k", "armv7-k", "armv7k") 123 .Cases("armv7m", "armv7-m", "armv7m") 124 .Cases("armv7s", "armv7-s", "armv7s") 125 .Default(nullptr); 126 } 127 128 static const char *GetArmArchForMCpu(StringRef Value) { 129 return llvm::StringSwitch<const char *>(Value) 130 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5") 131 .Cases("arm10e", "arm10tdmi", "armv5") 132 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5") 133 .Case("xscale", "xscale") 134 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6") 135 .Case("cortex-m0", "armv6m") 136 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "armv7") 137 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "cortex-a17", "krait", "armv7") 138 .Cases("cortex-r4", "cortex-r5", "armv7r") 139 .Case("cortex-m3", "armv7m") 140 .Cases("cortex-m4", "cortex-m7", "armv7em") 141 .Case("swift", "armv7s") 142 .Default(nullptr); 143 } 144 145 static bool isSoftFloatABI(const ArgList &Args) { 146 Arg *A = Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float, 147 options::OPT_mfloat_abi_EQ); 148 if (!A) 149 return false; 150 151 return A->getOption().matches(options::OPT_msoft_float) || 152 (A->getOption().matches(options::OPT_mfloat_abi_EQ) && 153 A->getValue() == StringRef("soft")); 154 } 155 156 StringRef MachO::getMachOArchName(const ArgList &Args) const { 157 switch (getTriple().getArch()) { 158 default: 159 return getDefaultUniversalArchName(); 160 161 case llvm::Triple::aarch64: 162 return "arm64"; 163 164 case llvm::Triple::thumb: 165 case llvm::Triple::arm: { 166 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 167 if (const char *Arch = GetArmArchForMArch(A->getValue())) 168 return Arch; 169 170 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 171 if (const char *Arch = GetArmArchForMCpu(A->getValue())) 172 return Arch; 173 174 return "arm"; 175 } 176 } 177 } 178 179 Darwin::~Darwin() { 180 } 181 182 MachO::~MachO() { 183 } 184 185 186 std::string MachO::ComputeEffectiveClangTriple(const ArgList &Args, 187 types::ID InputType) const { 188 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType)); 189 190 return Triple.getTriple(); 191 } 192 193 std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args, 194 types::ID InputType) const { 195 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType)); 196 197 // If the target isn't initialized (e.g., an unknown Darwin platform, return 198 // the default triple). 199 if (!isTargetInitialized()) 200 return Triple.getTriple(); 201 202 SmallString<16> Str; 203 Str += isTargetIOSBased() ? "ios" : "macosx"; 204 Str += getTargetVersion().getAsString(); 205 Triple.setOSName(Str); 206 207 return Triple.getTriple(); 208 } 209 210 void Generic_ELF::anchor() {} 211 212 Tool *MachO::getTool(Action::ActionClass AC) const { 213 switch (AC) { 214 case Action::LipoJobClass: 215 if (!Lipo) 216 Lipo.reset(new tools::darwin::Lipo(*this)); 217 return Lipo.get(); 218 case Action::DsymutilJobClass: 219 if (!Dsymutil) 220 Dsymutil.reset(new tools::darwin::Dsymutil(*this)); 221 return Dsymutil.get(); 222 case Action::VerifyDebugInfoJobClass: 223 if (!VerifyDebug) 224 VerifyDebug.reset(new tools::darwin::VerifyDebug(*this)); 225 return VerifyDebug.get(); 226 default: 227 return ToolChain::getTool(AC); 228 } 229 } 230 231 Tool *MachO::buildLinker() const { 232 return new tools::darwin::Link(*this); 233 } 234 235 Tool *MachO::buildAssembler() const { 236 return new tools::darwin::Assemble(*this); 237 } 238 239 DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple, 240 const ArgList &Args) 241 : Darwin(D, Triple, Args) { 242 } 243 244 void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const { 245 // For iOS, 64-bit, promote certain warnings to errors. 246 if (!isTargetMacOS() && getTriple().isArch64Bit()) { 247 // Always enable -Wdeprecated-objc-isa-usage and promote it 248 // to an error. 249 CC1Args.push_back("-Wdeprecated-objc-isa-usage"); 250 CC1Args.push_back("-Werror=deprecated-objc-isa-usage"); 251 252 // Also error about implicit function declarations, as that 253 // can impact calling conventions. 254 CC1Args.push_back("-Werror=implicit-function-declaration"); 255 } 256 } 257 258 /// \brief Determine whether Objective-C automated reference counting is 259 /// enabled. 260 static bool isObjCAutoRefCount(const ArgList &Args) { 261 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false); 262 } 263 264 void DarwinClang::AddLinkARCArgs(const ArgList &Args, 265 ArgStringList &CmdArgs) const { 266 // Avoid linking compatibility stubs on i386 mac. 267 if (isTargetMacOS() && getArch() == llvm::Triple::x86) 268 return; 269 270 ObjCRuntime runtime = getDefaultObjCRuntime(/*nonfragile*/ true); 271 272 if ((runtime.hasNativeARC() || !isObjCAutoRefCount(Args)) && 273 runtime.hasSubscripting()) 274 return; 275 276 CmdArgs.push_back("-force_load"); 277 SmallString<128> P(getDriver().ClangExecutable); 278 llvm::sys::path::remove_filename(P); // 'clang' 279 llvm::sys::path::remove_filename(P); // 'bin' 280 llvm::sys::path::append(P, "lib", "arc", "libarclite_"); 281 // Mash in the platform. 282 if (isTargetIOSSimulator()) 283 P += "iphonesimulator"; 284 else if (isTargetIPhoneOS()) 285 P += "iphoneos"; 286 else 287 P += "macosx"; 288 P += ".a"; 289 290 CmdArgs.push_back(Args.MakeArgString(P)); 291 } 292 293 void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, 294 StringRef DarwinLibName, bool AlwaysLink, 295 bool IsEmbedded, bool AddRPath) const { 296 SmallString<128> Dir(getDriver().ResourceDir); 297 llvm::sys::path::append(Dir, "lib", IsEmbedded ? "macho_embedded" : "darwin"); 298 299 SmallString<128> P(Dir); 300 llvm::sys::path::append(P, DarwinLibName); 301 302 // For now, allow missing resource libraries to support developers who may 303 // not have compiler-rt checked out or integrated into their build (unless 304 // we explicitly force linking with this library). 305 if (AlwaysLink || llvm::sys::fs::exists(P.str())) 306 CmdArgs.push_back(Args.MakeArgString(P.str())); 307 308 // Adding the rpaths might negatively interact when other rpaths are involved, 309 // so we should make sure we add the rpaths last, after all user-specified 310 // rpaths. This is currently true from this place, but we need to be 311 // careful if this function is ever called before user's rpaths are emitted. 312 if (AddRPath) { 313 assert(DarwinLibName.endswith(".dylib") && "must be a dynamic library"); 314 315 // Add @executable_path to rpath to support having the dylib copied with 316 // the executable. 317 CmdArgs.push_back("-rpath"); 318 CmdArgs.push_back("@executable_path"); 319 320 // Add the path to the resource dir to rpath to support using the dylib 321 // from the default location without copying. 322 CmdArgs.push_back("-rpath"); 323 CmdArgs.push_back(Args.MakeArgString(Dir.str())); 324 } 325 } 326 327 void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, 328 ArgStringList &CmdArgs) const { 329 // Darwin only supports the compiler-rt based runtime libraries. 330 switch (GetRuntimeLibType(Args)) { 331 case ToolChain::RLT_CompilerRT: 332 break; 333 default: 334 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform) 335 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin"; 336 return; 337 } 338 339 // Darwin doesn't support real static executables, don't link any runtime 340 // libraries with -static. 341 if (Args.hasArg(options::OPT_static) || 342 Args.hasArg(options::OPT_fapple_kext) || 343 Args.hasArg(options::OPT_mkernel)) 344 return; 345 346 // Reject -static-libgcc for now, we can deal with this when and if someone 347 // cares. This is useful in situations where someone wants to statically link 348 // something like libstdc++, and needs its runtime support routines. 349 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) { 350 getDriver().Diag(diag::err_drv_unsupported_opt) 351 << A->getAsString(Args); 352 return; 353 } 354 355 // If we are building profile support, link that library in. 356 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, 357 false) || 358 Args.hasArg(options::OPT_fprofile_generate) || 359 Args.hasArg(options::OPT_fprofile_instr_generate) || 360 Args.hasArg(options::OPT_fcreate_profile) || 361 Args.hasArg(options::OPT_coverage)) { 362 // Select the appropriate runtime library for the target. 363 if (isTargetIOSBased()) 364 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a"); 365 else 366 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a"); 367 } 368 369 const SanitizerArgs &Sanitize = getSanitizerArgs(); 370 371 // Add Ubsan runtime library, if required. 372 if (Sanitize.needsUbsanRt()) { 373 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds. 374 if (isTargetIOSBased()) { 375 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform) 376 << "-fsanitize=undefined"; 377 } else { 378 assert(isTargetMacOS() && "unexpected non OS X target"); 379 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true); 380 381 // The Ubsan runtime library requires C++. 382 AddCXXStdlibLibArgs(Args, CmdArgs); 383 } 384 } 385 386 // Add ASAN runtime library, if required. Dynamic libraries and bundles 387 // should not be linked with the runtime library. 388 if (Sanitize.needsAsanRt()) { 389 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds. 390 if (isTargetIPhoneOS()) { 391 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform) 392 << "-fsanitize=address"; 393 } else { 394 if (!Args.hasArg(options::OPT_dynamiclib) && 395 !Args.hasArg(options::OPT_bundle)) { 396 // The ASAN runtime library requires C++. 397 AddCXXStdlibLibArgs(Args, CmdArgs); 398 } 399 if (isTargetMacOS()) { 400 AddLinkRuntimeLib(Args, CmdArgs, 401 "libclang_rt.asan_osx_dynamic.dylib", 402 /*AlwaysLink*/ true, /*IsEmbedded*/ false, 403 /*AddRPath*/ true); 404 } else { 405 if (isTargetIOSSimulator()) { 406 AddLinkRuntimeLib(Args, CmdArgs, 407 "libclang_rt.asan_iossim_dynamic.dylib", 408 /*AlwaysLink*/ true, /*IsEmbedded*/ false, 409 /*AddRPath*/ true); 410 } 411 } 412 } 413 } 414 415 // Otherwise link libSystem, then the dynamic runtime library, and finally any 416 // target specific static runtime library. 417 CmdArgs.push_back("-lSystem"); 418 419 // Select the dynamic runtime library and the target specific static library. 420 if (isTargetIOSBased()) { 421 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1, 422 // it never went into the SDK. 423 // Linking against libgcc_s.1 isn't needed for iOS 5.0+ 424 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator() && 425 getTriple().getArch() != llvm::Triple::aarch64) 426 CmdArgs.push_back("-lgcc_s.1"); 427 428 // We currently always need a static runtime library for iOS. 429 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a"); 430 } else { 431 assert(isTargetMacOS() && "unexpected non MacOS platform"); 432 // The dynamic runtime library was merged with libSystem for 10.6 and 433 // beyond; only 10.4 and 10.5 need an additional runtime library. 434 if (isMacosxVersionLT(10, 5)) 435 CmdArgs.push_back("-lgcc_s.10.4"); 436 else if (isMacosxVersionLT(10, 6)) 437 CmdArgs.push_back("-lgcc_s.10.5"); 438 439 // For OS X, we thought we would only need a static runtime library when 440 // targeting 10.4, to provide versions of the static functions which were 441 // omitted from 10.4.dylib. 442 // 443 // Unfortunately, that turned out to not be true, because Darwin system 444 // headers can still use eprintf on i386, and it is not exported from 445 // libSystem. Therefore, we still must provide a runtime library just for 446 // the tiny tiny handful of projects that *might* use that symbol. 447 if (isMacosxVersionLT(10, 5)) { 448 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a"); 449 } else { 450 if (getTriple().getArch() == llvm::Triple::x86) 451 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a"); 452 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a"); 453 } 454 } 455 } 456 457 void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { 458 const OptTable &Opts = getDriver().getOpts(); 459 460 // Support allowing the SDKROOT environment variable used by xcrun and other 461 // Xcode tools to define the default sysroot, by making it the default for 462 // isysroot. 463 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { 464 // Warn if the path does not exist. 465 if (!llvm::sys::fs::exists(A->getValue())) 466 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue(); 467 } else { 468 if (char *env = ::getenv("SDKROOT")) { 469 // We only use this value as the default if it is an absolute path, 470 // exists, and it is not the root path. 471 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) && 472 StringRef(env) != "/") { 473 Args.append(Args.MakeSeparateArg( 474 0, Opts.getOption(options::OPT_isysroot), env)); 475 } 476 } 477 } 478 479 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ); 480 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ); 481 Arg *iOSSimVersion = Args.getLastArg( 482 options::OPT_mios_simulator_version_min_EQ); 483 484 if (OSXVersion && iOSVersion) { 485 getDriver().Diag(diag::err_drv_argument_not_allowed_with) 486 << OSXVersion->getAsString(Args) 487 << iOSVersion->getAsString(Args); 488 iOSVersion = nullptr; 489 } else if (!OSXVersion && !iOSVersion) { 490 // If no deployment target was specified on the command line, check for 491 // environment defines. 492 StringRef OSXTarget; 493 StringRef iOSTarget; 494 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET")) 495 OSXTarget = env; 496 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET")) 497 iOSTarget = env; 498 499 // If no '-miphoneos-version-min' specified on the command line and 500 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default 501 // based on -isysroot. 502 if (iOSTarget.empty()) { 503 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { 504 StringRef first, second; 505 StringRef isysroot = A->getValue(); 506 std::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS")); 507 if (second != "") 508 iOSTarget = second.substr(0,3); 509 } 510 } 511 512 // If no OSX or iOS target has been specified and we're compiling for armv7, 513 // go ahead as assume we're targeting iOS. 514 StringRef MachOArchName = getMachOArchName(Args); 515 if (OSXTarget.empty() && iOSTarget.empty() && 516 (MachOArchName == "armv7" || MachOArchName == "armv7s" || 517 MachOArchName == "arm64")) 518 iOSTarget = iOSVersionMin; 519 520 // Allow conflicts among OSX and iOS for historical reasons, but choose the 521 // default platform. 522 if (!OSXTarget.empty() && !iOSTarget.empty()) { 523 if (getTriple().getArch() == llvm::Triple::arm || 524 getTriple().getArch() == llvm::Triple::aarch64 || 525 getTriple().getArch() == llvm::Triple::thumb) 526 OSXTarget = ""; 527 else 528 iOSTarget = ""; 529 } 530 531 if (!OSXTarget.empty()) { 532 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ); 533 OSXVersion = Args.MakeJoinedArg(nullptr, O, OSXTarget); 534 Args.append(OSXVersion); 535 } else if (!iOSTarget.empty()) { 536 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ); 537 iOSVersion = Args.MakeJoinedArg(nullptr, O, iOSTarget); 538 Args.append(iOSVersion); 539 } else if (MachOArchName != "armv6m" && MachOArchName != "armv7m" && 540 MachOArchName != "armv7em") { 541 // Otherwise, assume we are targeting OS X. 542 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ); 543 OSXVersion = Args.MakeJoinedArg(nullptr, O, MacosxVersionMin); 544 Args.append(OSXVersion); 545 } 546 } 547 548 DarwinPlatformKind Platform; 549 if (OSXVersion) 550 Platform = MacOS; 551 else if (iOSVersion) 552 Platform = IPhoneOS; 553 else 554 llvm_unreachable("Unable to infer Darwin variant"); 555 556 // Set the tool chain target information. 557 unsigned Major, Minor, Micro; 558 bool HadExtra; 559 if (Platform == MacOS) { 560 assert(!iOSVersion && "Unknown target platform!"); 561 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor, 562 Micro, HadExtra) || HadExtra || 563 Major != 10 || Minor >= 100 || Micro >= 100) 564 getDriver().Diag(diag::err_drv_invalid_version_number) 565 << OSXVersion->getAsString(Args); 566 } else if (Platform == IPhoneOS) { 567 assert(iOSVersion && "Unknown target platform!"); 568 if (!Driver::GetReleaseVersion(iOSVersion->getValue(), Major, Minor, 569 Micro, HadExtra) || HadExtra || 570 Major >= 10 || Minor >= 100 || Micro >= 100) 571 getDriver().Diag(diag::err_drv_invalid_version_number) 572 << iOSVersion->getAsString(Args); 573 } else 574 llvm_unreachable("unknown kind of Darwin platform"); 575 576 // Recognize iOS targets with an x86 architecture as the iOS simulator. 577 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 || 578 getTriple().getArch() == llvm::Triple::x86_64)) 579 Platform = IPhoneOSSimulator; 580 581 setTarget(Platform, Major, Minor, Micro); 582 } 583 584 void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args, 585 ArgStringList &CmdArgs) const { 586 CXXStdlibType Type = GetCXXStdlibType(Args); 587 588 switch (Type) { 589 case ToolChain::CST_Libcxx: 590 CmdArgs.push_back("-lc++"); 591 break; 592 593 case ToolChain::CST_Libstdcxx: { 594 // Unfortunately, -lstdc++ doesn't always exist in the standard search path; 595 // it was previously found in the gcc lib dir. However, for all the Darwin 596 // platforms we care about it was -lstdc++.6, so we search for that 597 // explicitly if we can't see an obvious -lstdc++ candidate. 598 599 // Check in the sysroot first. 600 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { 601 SmallString<128> P(A->getValue()); 602 llvm::sys::path::append(P, "usr", "lib", "libstdc++.dylib"); 603 604 if (!llvm::sys::fs::exists(P.str())) { 605 llvm::sys::path::remove_filename(P); 606 llvm::sys::path::append(P, "libstdc++.6.dylib"); 607 if (llvm::sys::fs::exists(P.str())) { 608 CmdArgs.push_back(Args.MakeArgString(P.str())); 609 return; 610 } 611 } 612 } 613 614 // Otherwise, look in the root. 615 // FIXME: This should be removed someday when we don't have to care about 616 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist. 617 if (!llvm::sys::fs::exists("/usr/lib/libstdc++.dylib") && 618 llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib")) { 619 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib"); 620 return; 621 } 622 623 // Otherwise, let the linker search. 624 CmdArgs.push_back("-lstdc++"); 625 break; 626 } 627 } 628 } 629 630 void DarwinClang::AddCCKextLibArgs(const ArgList &Args, 631 ArgStringList &CmdArgs) const { 632 633 // For Darwin platforms, use the compiler-rt-based support library 634 // instead of the gcc-provided one (which is also incidentally 635 // only present in the gcc lib dir, which makes it hard to find). 636 637 SmallString<128> P(getDriver().ResourceDir); 638 llvm::sys::path::append(P, "lib", "darwin"); 639 640 // Use the newer cc_kext for iOS ARM after 6.0. 641 if (!isTargetIPhoneOS() || isTargetIOSSimulator() || 642 getTriple().getArch() == llvm::Triple::aarch64 || 643 !isIPhoneOSVersionLT(6, 0)) { 644 llvm::sys::path::append(P, "libclang_rt.cc_kext.a"); 645 } else { 646 llvm::sys::path::append(P, "libclang_rt.cc_kext_ios5.a"); 647 } 648 649 // For now, allow missing resource libraries to support developers who may 650 // not have compiler-rt checked out or integrated into their build. 651 if (llvm::sys::fs::exists(P.str())) 652 CmdArgs.push_back(Args.MakeArgString(P.str())); 653 } 654 655 DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args, 656 const char *BoundArch) const { 657 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); 658 const OptTable &Opts = getDriver().getOpts(); 659 660 // FIXME: We really want to get out of the tool chain level argument 661 // translation business, as it makes the driver functionality much 662 // more opaque. For now, we follow gcc closely solely for the 663 // purpose of easily achieving feature parity & testability. Once we 664 // have something that works, we should reevaluate each translation 665 // and try to push it down into tool specific logic. 666 667 for (Arg *A : Args) { 668 if (A->getOption().matches(options::OPT_Xarch__)) { 669 // Skip this argument unless the architecture matches either the toolchain 670 // triple arch, or the arch being bound. 671 llvm::Triple::ArchType XarchArch = 672 tools::darwin::getArchTypeForMachOArchName(A->getValue(0)); 673 if (!(XarchArch == getArch() || 674 (BoundArch && XarchArch == 675 tools::darwin::getArchTypeForMachOArchName(BoundArch)))) 676 continue; 677 678 Arg *OriginalArg = A; 679 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1)); 680 unsigned Prev = Index; 681 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index)); 682 683 // If the argument parsing failed or more than one argument was 684 // consumed, the -Xarch_ argument's parameter tried to consume 685 // extra arguments. Emit an error and ignore. 686 // 687 // We also want to disallow any options which would alter the 688 // driver behavior; that isn't going to work in our model. We 689 // use isDriverOption() as an approximation, although things 690 // like -O4 are going to slip through. 691 if (!XarchArg || Index > Prev + 1) { 692 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args) 693 << A->getAsString(Args); 694 continue; 695 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) { 696 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver) 697 << A->getAsString(Args); 698 continue; 699 } 700 701 XarchArg->setBaseArg(A); 702 703 A = XarchArg.release(); 704 DAL->AddSynthesizedArg(A); 705 706 // Linker input arguments require custom handling. The problem is that we 707 // have already constructed the phase actions, so we can not treat them as 708 // "input arguments". 709 if (A->getOption().hasFlag(options::LinkerInput)) { 710 // Convert the argument into individual Zlinker_input_args. 711 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) { 712 DAL->AddSeparateArg(OriginalArg, 713 Opts.getOption(options::OPT_Zlinker_input), 714 A->getValue(i)); 715 716 } 717 continue; 718 } 719 } 720 721 // Sob. These is strictly gcc compatible for the time being. Apple 722 // gcc translates options twice, which means that self-expanding 723 // options add duplicates. 724 switch ((options::ID) A->getOption().getID()) { 725 default: 726 DAL->append(A); 727 break; 728 729 case options::OPT_mkernel: 730 case options::OPT_fapple_kext: 731 DAL->append(A); 732 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static)); 733 break; 734 735 case options::OPT_dependency_file: 736 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF), 737 A->getValue()); 738 break; 739 740 case options::OPT_gfull: 741 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag)); 742 DAL->AddFlagArg(A, 743 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)); 744 break; 745 746 case options::OPT_gused: 747 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag)); 748 DAL->AddFlagArg(A, 749 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)); 750 break; 751 752 case options::OPT_shared: 753 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib)); 754 break; 755 756 case options::OPT_fconstant_cfstrings: 757 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings)); 758 break; 759 760 case options::OPT_fno_constant_cfstrings: 761 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings)); 762 break; 763 764 case options::OPT_Wnonportable_cfstrings: 765 DAL->AddFlagArg(A, 766 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)); 767 break; 768 769 case options::OPT_Wno_nonportable_cfstrings: 770 DAL->AddFlagArg(A, 771 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)); 772 break; 773 774 case options::OPT_fpascal_strings: 775 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings)); 776 break; 777 778 case options::OPT_fno_pascal_strings: 779 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings)); 780 break; 781 } 782 } 783 784 if (getTriple().getArch() == llvm::Triple::x86 || 785 getTriple().getArch() == llvm::Triple::x86_64) 786 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ)) 787 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mtune_EQ), 788 "core2"); 789 790 // Add the arch options based on the particular spelling of -arch, to match 791 // how the driver driver works. 792 if (BoundArch) { 793 StringRef Name = BoundArch; 794 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ); 795 const Option MArch = Opts.getOption(options::OPT_march_EQ); 796 797 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch, 798 // which defines the list of which architectures we accept. 799 if (Name == "ppc") 800 ; 801 else if (Name == "ppc601") 802 DAL->AddJoinedArg(nullptr, MCpu, "601"); 803 else if (Name == "ppc603") 804 DAL->AddJoinedArg(nullptr, MCpu, "603"); 805 else if (Name == "ppc604") 806 DAL->AddJoinedArg(nullptr, MCpu, "604"); 807 else if (Name == "ppc604e") 808 DAL->AddJoinedArg(nullptr, MCpu, "604e"); 809 else if (Name == "ppc750") 810 DAL->AddJoinedArg(nullptr, MCpu, "750"); 811 else if (Name == "ppc7400") 812 DAL->AddJoinedArg(nullptr, MCpu, "7400"); 813 else if (Name == "ppc7450") 814 DAL->AddJoinedArg(nullptr, MCpu, "7450"); 815 else if (Name == "ppc970") 816 DAL->AddJoinedArg(nullptr, MCpu, "970"); 817 818 else if (Name == "ppc64" || Name == "ppc64le") 819 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64)); 820 821 else if (Name == "i386") 822 ; 823 else if (Name == "i486") 824 DAL->AddJoinedArg(nullptr, MArch, "i486"); 825 else if (Name == "i586") 826 DAL->AddJoinedArg(nullptr, MArch, "i586"); 827 else if (Name == "i686") 828 DAL->AddJoinedArg(nullptr, MArch, "i686"); 829 else if (Name == "pentium") 830 DAL->AddJoinedArg(nullptr, MArch, "pentium"); 831 else if (Name == "pentium2") 832 DAL->AddJoinedArg(nullptr, MArch, "pentium2"); 833 else if (Name == "pentpro") 834 DAL->AddJoinedArg(nullptr, MArch, "pentiumpro"); 835 else if (Name == "pentIIm3") 836 DAL->AddJoinedArg(nullptr, MArch, "pentium2"); 837 838 else if (Name == "x86_64") 839 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64)); 840 else if (Name == "x86_64h") { 841 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64)); 842 DAL->AddJoinedArg(nullptr, MArch, "x86_64h"); 843 } 844 845 else if (Name == "arm") 846 DAL->AddJoinedArg(nullptr, MArch, "armv4t"); 847 else if (Name == "armv4t") 848 DAL->AddJoinedArg(nullptr, MArch, "armv4t"); 849 else if (Name == "armv5") 850 DAL->AddJoinedArg(nullptr, MArch, "armv5tej"); 851 else if (Name == "xscale") 852 DAL->AddJoinedArg(nullptr, MArch, "xscale"); 853 else if (Name == "armv6") 854 DAL->AddJoinedArg(nullptr, MArch, "armv6k"); 855 else if (Name == "armv6m") 856 DAL->AddJoinedArg(nullptr, MArch, "armv6m"); 857 else if (Name == "armv7") 858 DAL->AddJoinedArg(nullptr, MArch, "armv7a"); 859 else if (Name == "armv7em") 860 DAL->AddJoinedArg(nullptr, MArch, "armv7em"); 861 else if (Name == "armv7k") 862 DAL->AddJoinedArg(nullptr, MArch, "armv7k"); 863 else if (Name == "armv7m") 864 DAL->AddJoinedArg(nullptr, MArch, "armv7m"); 865 else if (Name == "armv7s") 866 DAL->AddJoinedArg(nullptr, MArch, "armv7s"); 867 } 868 869 return DAL; 870 } 871 872 void MachO::AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, 873 llvm::opt::ArgStringList &CmdArgs) const { 874 // Embedded targets are simple at the moment, not supporting sanitizers and 875 // with different libraries for each member of the product { static, PIC } x 876 // { hard-float, soft-float } 877 llvm::SmallString<32> CompilerRT = StringRef("libclang_rt."); 878 CompilerRT += 879 tools::arm::getARMFloatABI(getDriver(), Args, getTriple()) == "hard" 880 ? "hard" 881 : "soft"; 882 CompilerRT += Args.hasArg(options::OPT_fPIC) ? "_pic.a" : "_static.a"; 883 884 AddLinkRuntimeLib(Args, CmdArgs, CompilerRT, false, true); 885 } 886 887 888 DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args, 889 const char *BoundArch) const { 890 // First get the generic Apple args, before moving onto Darwin-specific ones. 891 DerivedArgList *DAL = MachO::TranslateArgs(Args, BoundArch); 892 const OptTable &Opts = getDriver().getOpts(); 893 894 // If no architecture is bound, none of the translations here are relevant. 895 if (!BoundArch) 896 return DAL; 897 898 // Add an explicit version min argument for the deployment target. We do this 899 // after argument translation because -Xarch_ arguments may add a version min 900 // argument. 901 AddDeploymentTarget(*DAL); 902 903 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext. 904 // FIXME: It would be far better to avoid inserting those -static arguments, 905 // but we can't check the deployment target in the translation code until 906 // it is set here. 907 if (isTargetIOSBased() && !isIPhoneOSVersionLT(6, 0)) { 908 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) { 909 Arg *A = *it; 910 ++it; 911 if (A->getOption().getID() != options::OPT_mkernel && 912 A->getOption().getID() != options::OPT_fapple_kext) 913 continue; 914 assert(it != ie && "unexpected argument translation"); 915 A = *it; 916 assert(A->getOption().getID() == options::OPT_static && 917 "missing expected -static argument"); 918 it = DAL->getArgs().erase(it); 919 } 920 } 921 922 // Default to use libc++ on OS X 10.9+ and iOS 7+. 923 if (((isTargetMacOS() && !isMacosxVersionLT(10, 9)) || 924 (isTargetIOSBased() && !isIPhoneOSVersionLT(7, 0))) && 925 !Args.getLastArg(options::OPT_stdlib_EQ)) 926 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_stdlib_EQ), 927 "libc++"); 928 929 // Validate the C++ standard library choice. 930 CXXStdlibType Type = GetCXXStdlibType(*DAL); 931 if (Type == ToolChain::CST_Libcxx) { 932 // Check whether the target provides libc++. 933 StringRef where; 934 935 // Complain about targeting iOS < 5.0 in any way. 936 if (isTargetIOSBased() && isIPhoneOSVersionLT(5, 0)) 937 where = "iOS 5.0"; 938 939 if (where != StringRef()) { 940 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment) 941 << where; 942 } 943 } 944 945 return DAL; 946 } 947 948 bool MachO::IsUnwindTablesDefault() const { 949 return getArch() == llvm::Triple::x86_64; 950 } 951 952 bool MachO::UseDwarfDebugFlags() const { 953 if (const char *S = ::getenv("RC_DEBUG_OPTIONS")) 954 return S[0] != '\0'; 955 return false; 956 } 957 958 bool Darwin::UseSjLjExceptions() const { 959 // Darwin uses SjLj exceptions on ARM. 960 return (getTriple().getArch() == llvm::Triple::arm || 961 getTriple().getArch() == llvm::Triple::thumb); 962 } 963 964 bool MachO::isPICDefault() const { 965 return true; 966 } 967 968 bool MachO::isPIEDefault() const { 969 return false; 970 } 971 972 bool MachO::isPICDefaultForced() const { 973 return (getArch() == llvm::Triple::x86_64 || 974 getArch() == llvm::Triple::aarch64); 975 } 976 977 bool MachO::SupportsProfiling() const { 978 // Profiling instrumentation is only supported on x86. 979 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64; 980 } 981 982 void Darwin::addMinVersionArgs(const llvm::opt::ArgList &Args, 983 llvm::opt::ArgStringList &CmdArgs) const { 984 VersionTuple TargetVersion = getTargetVersion(); 985 986 if (isTargetIOSSimulator()) 987 CmdArgs.push_back("-ios_simulator_version_min"); 988 else if (isTargetIOSBased()) 989 CmdArgs.push_back("-iphoneos_version_min"); 990 else { 991 assert(isTargetMacOS() && "unexpected target"); 992 CmdArgs.push_back("-macosx_version_min"); 993 } 994 995 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString())); 996 } 997 998 void Darwin::addStartObjectFileArgs(const llvm::opt::ArgList &Args, 999 llvm::opt::ArgStringList &CmdArgs) const { 1000 // Derived from startfile spec. 1001 if (Args.hasArg(options::OPT_dynamiclib)) { 1002 // Derived from darwin_dylib1 spec. 1003 if (isTargetIOSSimulator()) { 1004 ; // iOS simulator does not need dylib1.o. 1005 } else if (isTargetIPhoneOS()) { 1006 if (isIPhoneOSVersionLT(3, 1)) 1007 CmdArgs.push_back("-ldylib1.o"); 1008 } else { 1009 if (isMacosxVersionLT(10, 5)) 1010 CmdArgs.push_back("-ldylib1.o"); 1011 else if (isMacosxVersionLT(10, 6)) 1012 CmdArgs.push_back("-ldylib1.10.5.o"); 1013 } 1014 } else { 1015 if (Args.hasArg(options::OPT_bundle)) { 1016 if (!Args.hasArg(options::OPT_static)) { 1017 // Derived from darwin_bundle1 spec. 1018 if (isTargetIOSSimulator()) { 1019 ; // iOS simulator does not need bundle1.o. 1020 } else if (isTargetIPhoneOS()) { 1021 if (isIPhoneOSVersionLT(3, 1)) 1022 CmdArgs.push_back("-lbundle1.o"); 1023 } else { 1024 if (isMacosxVersionLT(10, 6)) 1025 CmdArgs.push_back("-lbundle1.o"); 1026 } 1027 } 1028 } else { 1029 if (Args.hasArg(options::OPT_pg) && SupportsProfiling()) { 1030 if (Args.hasArg(options::OPT_static) || 1031 Args.hasArg(options::OPT_object) || 1032 Args.hasArg(options::OPT_preload)) { 1033 CmdArgs.push_back("-lgcrt0.o"); 1034 } else { 1035 CmdArgs.push_back("-lgcrt1.o"); 1036 1037 // darwin_crt2 spec is empty. 1038 } 1039 // By default on OS X 10.8 and later, we don't link with a crt1.o 1040 // file and the linker knows to use _main as the entry point. But, 1041 // when compiling with -pg, we need to link with the gcrt1.o file, 1042 // so pass the -no_new_main option to tell the linker to use the 1043 // "start" symbol as the entry point. 1044 if (isTargetMacOS() && !isMacosxVersionLT(10, 8)) 1045 CmdArgs.push_back("-no_new_main"); 1046 } else { 1047 if (Args.hasArg(options::OPT_static) || 1048 Args.hasArg(options::OPT_object) || 1049 Args.hasArg(options::OPT_preload)) { 1050 CmdArgs.push_back("-lcrt0.o"); 1051 } else { 1052 // Derived from darwin_crt1 spec. 1053 if (isTargetIOSSimulator()) { 1054 ; // iOS simulator does not need crt1.o. 1055 } else if (isTargetIPhoneOS()) { 1056 if (getArch() == llvm::Triple::aarch64) 1057 ; // iOS does not need any crt1 files for arm64 1058 else if (isIPhoneOSVersionLT(3, 1)) 1059 CmdArgs.push_back("-lcrt1.o"); 1060 else if (isIPhoneOSVersionLT(6, 0)) 1061 CmdArgs.push_back("-lcrt1.3.1.o"); 1062 } else { 1063 if (isMacosxVersionLT(10, 5)) 1064 CmdArgs.push_back("-lcrt1.o"); 1065 else if (isMacosxVersionLT(10, 6)) 1066 CmdArgs.push_back("-lcrt1.10.5.o"); 1067 else if (isMacosxVersionLT(10, 8)) 1068 CmdArgs.push_back("-lcrt1.10.6.o"); 1069 1070 // darwin_crt2 spec is empty. 1071 } 1072 } 1073 } 1074 } 1075 } 1076 1077 if (!isTargetIPhoneOS() && Args.hasArg(options::OPT_shared_libgcc) && 1078 isMacosxVersionLT(10, 5)) { 1079 const char *Str = Args.MakeArgString(GetFilePath("crt3.o")); 1080 CmdArgs.push_back(Str); 1081 } 1082 } 1083 1084 bool Darwin::SupportsObjCGC() const { 1085 return isTargetMacOS(); 1086 } 1087 1088 void Darwin::CheckObjCARC() const { 1089 if (isTargetIOSBased()|| (isTargetMacOS() && !isMacosxVersionLT(10, 6))) 1090 return; 1091 getDriver().Diag(diag::err_arc_unsupported_on_toolchain); 1092 } 1093 1094 /// Generic_GCC - A tool chain using the 'gcc' command to perform 1095 /// all subcommands; this relies on gcc translating the majority of 1096 /// command line options. 1097 1098 /// \brief Parse a GCCVersion object out of a string of text. 1099 /// 1100 /// This is the primary means of forming GCCVersion objects. 1101 /*static*/ 1102 Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) { 1103 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "", "", "" }; 1104 std::pair<StringRef, StringRef> First = VersionText.split('.'); 1105 std::pair<StringRef, StringRef> Second = First.second.split('.'); 1106 1107 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "", "", "" }; 1108 if (First.first.getAsInteger(10, GoodVersion.Major) || 1109 GoodVersion.Major < 0) 1110 return BadVersion; 1111 GoodVersion.MajorStr = First.first.str(); 1112 if (Second.first.getAsInteger(10, GoodVersion.Minor) || 1113 GoodVersion.Minor < 0) 1114 return BadVersion; 1115 GoodVersion.MinorStr = Second.first.str(); 1116 1117 // First look for a number prefix and parse that if present. Otherwise just 1118 // stash the entire patch string in the suffix, and leave the number 1119 // unspecified. This covers versions strings such as: 1120 // 4.4 1121 // 4.4.0 1122 // 4.4.x 1123 // 4.4.2-rc4 1124 // 4.4.x-patched 1125 // And retains any patch number it finds. 1126 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str(); 1127 if (!PatchText.empty()) { 1128 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) { 1129 // Try to parse the number and any suffix. 1130 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) || 1131 GoodVersion.Patch < 0) 1132 return BadVersion; 1133 GoodVersion.PatchSuffix = PatchText.substr(EndNumber); 1134 } 1135 } 1136 1137 return GoodVersion; 1138 } 1139 1140 /// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering. 1141 bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor, 1142 int RHSPatch, 1143 StringRef RHSPatchSuffix) const { 1144 if (Major != RHSMajor) 1145 return Major < RHSMajor; 1146 if (Minor != RHSMinor) 1147 return Minor < RHSMinor; 1148 if (Patch != RHSPatch) { 1149 // Note that versions without a specified patch sort higher than those with 1150 // a patch. 1151 if (RHSPatch == -1) 1152 return true; 1153 if (Patch == -1) 1154 return false; 1155 1156 // Otherwise just sort on the patch itself. 1157 return Patch < RHSPatch; 1158 } 1159 if (PatchSuffix != RHSPatchSuffix) { 1160 // Sort empty suffixes higher. 1161 if (RHSPatchSuffix.empty()) 1162 return true; 1163 if (PatchSuffix.empty()) 1164 return false; 1165 1166 // Provide a lexicographic sort to make this a total ordering. 1167 return PatchSuffix < RHSPatchSuffix; 1168 } 1169 1170 // The versions are equal. 1171 return false; 1172 } 1173 1174 static llvm::StringRef getGCCToolchainDir(const ArgList &Args) { 1175 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain); 1176 if (A) 1177 return A->getValue(); 1178 return GCC_INSTALL_PREFIX; 1179 } 1180 1181 /// \brief Initialize a GCCInstallationDetector from the driver. 1182 /// 1183 /// This performs all of the autodetection and sets up the various paths. 1184 /// Once constructed, a GCCInstallationDetector is essentially immutable. 1185 /// 1186 /// FIXME: We shouldn't need an explicit TargetTriple parameter here, and 1187 /// should instead pull the target out of the driver. This is currently 1188 /// necessary because the driver doesn't store the final version of the target 1189 /// triple. 1190 void 1191 Generic_GCC::GCCInstallationDetector::init( 1192 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args) { 1193 llvm::Triple BiarchVariantTriple = 1194 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant() 1195 : TargetTriple.get32BitArchVariant(); 1196 // The library directories which may contain GCC installations. 1197 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs; 1198 // The compatible GCC triples for this particular architecture. 1199 SmallVector<StringRef, 16> CandidateTripleAliases; 1200 SmallVector<StringRef, 16> CandidateBiarchTripleAliases; 1201 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs, 1202 CandidateTripleAliases, CandidateBiarchLibDirs, 1203 CandidateBiarchTripleAliases); 1204 1205 // Compute the set of prefixes for our search. 1206 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(), 1207 D.PrefixDirs.end()); 1208 1209 StringRef GCCToolchainDir = getGCCToolchainDir(Args); 1210 if (GCCToolchainDir != "") { 1211 if (GCCToolchainDir.back() == '/') 1212 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the / 1213 1214 Prefixes.push_back(GCCToolchainDir); 1215 } else { 1216 // If we have a SysRoot, try that first. 1217 if (!D.SysRoot.empty()) { 1218 Prefixes.push_back(D.SysRoot); 1219 Prefixes.push_back(D.SysRoot + "/usr"); 1220 } 1221 1222 // Then look for gcc installed alongside clang. 1223 Prefixes.push_back(D.InstalledDir + "/.."); 1224 1225 // And finally in /usr. 1226 if (D.SysRoot.empty()) 1227 Prefixes.push_back("/usr"); 1228 } 1229 1230 // Loop over the various components which exist and select the best GCC 1231 // installation available. GCC installs are ranked by version number. 1232 Version = GCCVersion::Parse("0.0.0"); 1233 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) { 1234 if (!llvm::sys::fs::exists(Prefixes[i])) 1235 continue; 1236 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) { 1237 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str(); 1238 if (!llvm::sys::fs::exists(LibDir)) 1239 continue; 1240 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k) 1241 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, 1242 CandidateTripleAliases[k]); 1243 } 1244 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) { 1245 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str(); 1246 if (!llvm::sys::fs::exists(LibDir)) 1247 continue; 1248 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke; 1249 ++k) 1250 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, 1251 CandidateBiarchTripleAliases[k], 1252 /*NeedsBiarchSuffix=*/ true); 1253 } 1254 } 1255 } 1256 1257 void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const { 1258 for (const auto &InstallPath : CandidateGCCInstallPaths) 1259 OS << "Found candidate GCC installation: " << InstallPath << "\n"; 1260 1261 if (!GCCInstallPath.empty()) 1262 OS << "Selected GCC installation: " << GCCInstallPath << "\n"; 1263 1264 for (const auto &Multilib : Multilibs) 1265 OS << "Candidate multilib: " << Multilib << "\n"; 1266 1267 if (Multilibs.size() != 0 || !SelectedMultilib.isDefault()) 1268 OS << "Selected multilib: " << SelectedMultilib << "\n"; 1269 } 1270 1271 bool Generic_GCC::GCCInstallationDetector::getBiarchSibling(Multilib &M) const { 1272 if (BiarchSibling.hasValue()) { 1273 M = BiarchSibling.getValue(); 1274 return true; 1275 } 1276 return false; 1277 } 1278 1279 /*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples( 1280 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple, 1281 SmallVectorImpl<StringRef> &LibDirs, 1282 SmallVectorImpl<StringRef> &TripleAliases, 1283 SmallVectorImpl<StringRef> &BiarchLibDirs, 1284 SmallVectorImpl<StringRef> &BiarchTripleAliases) { 1285 // Declare a bunch of static data sets that we'll select between below. These 1286 // are specifically designed to always refer to string literals to avoid any 1287 // lifetime or initialization issues. 1288 static const char *const AArch64LibDirs[] = { "/lib64", "/lib" }; 1289 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu", 1290 "aarch64-linux-gnu", 1291 "aarch64-linux-android", 1292 "aarch64-redhat-linux" }; 1293 static const char *const AArch64beLibDirs[] = { "/lib" }; 1294 static const char *const AArch64beTriples[] = { "aarch64_be-none-linux-gnu", 1295 "aarch64_be-linux-gnu" }; 1296 1297 static const char *const ARMLibDirs[] = { "/lib" }; 1298 static const char *const ARMTriples[] = { "arm-linux-gnueabi", 1299 "arm-linux-androideabi" }; 1300 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf", 1301 "armv7hl-redhat-linux-gnueabi" }; 1302 static const char *const ARMebLibDirs[] = { "/lib" }; 1303 static const char *const ARMebTriples[] = { "armeb-linux-gnueabi", 1304 "armeb-linux-androideabi" }; 1305 static const char *const ARMebHFTriples[] = { "armeb-linux-gnueabihf", 1306 "armebv7hl-redhat-linux-gnueabi" }; 1307 1308 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" }; 1309 static const char *const X86_64Triples[] = { 1310 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu", 1311 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux", 1312 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux", 1313 "x86_64-linux-android", "x86_64-unknown-linux" 1314 }; 1315 static const char *const X32LibDirs[] = { "/libx32" }; 1316 static const char *const X86LibDirs[] = { "/lib32", "/lib" }; 1317 static const char *const X86Triples[] = { 1318 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu", 1319 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux", 1320 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux", 1321 "i686-montavista-linux", "i686-linux-android", "i586-linux-gnu" 1322 }; 1323 1324 static const char *const MIPSLibDirs[] = { "/lib" }; 1325 static const char *const MIPSTriples[] = { "mips-linux-gnu", 1326 "mips-mti-linux-gnu", 1327 "mips-img-linux-gnu" }; 1328 static const char *const MIPSELLibDirs[] = { "/lib" }; 1329 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu", 1330 "mipsel-linux-android", 1331 "mips-img-linux-gnu" }; 1332 1333 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" }; 1334 static const char *const MIPS64Triples[] = { "mips64-linux-gnu", 1335 "mips-mti-linux-gnu", 1336 "mips-img-linux-gnu", 1337 "mips64-linux-gnuabi64" }; 1338 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" }; 1339 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu", 1340 "mips-mti-linux-gnu", 1341 "mips-img-linux-gnu", 1342 "mips64el-linux-android", 1343 "mips64el-linux-gnuabi64" }; 1344 1345 static const char *const PPCLibDirs[] = { "/lib32", "/lib" }; 1346 static const char *const PPCTriples[] = { 1347 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe", 1348 "powerpc-suse-linux", "powerpc-montavista-linuxspe" 1349 }; 1350 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" }; 1351 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu", 1352 "powerpc64-unknown-linux-gnu", 1353 "powerpc64-suse-linux", 1354 "ppc64-redhat-linux" }; 1355 static const char *const PPC64LELibDirs[] = { "/lib64", "/lib" }; 1356 static const char *const PPC64LETriples[] = { "powerpc64le-linux-gnu", 1357 "powerpc64le-unknown-linux-gnu", 1358 "powerpc64le-suse-linux", 1359 "ppc64le-redhat-linux" }; 1360 1361 static const char *const SPARCv8LibDirs[] = { "/lib32", "/lib" }; 1362 static const char *const SPARCv8Triples[] = { "sparc-linux-gnu", 1363 "sparcv8-linux-gnu" }; 1364 static const char *const SPARCv9LibDirs[] = { "/lib64", "/lib" }; 1365 static const char *const SPARCv9Triples[] = { "sparc64-linux-gnu", 1366 "sparcv9-linux-gnu" }; 1367 1368 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" }; 1369 static const char *const SystemZTriples[] = { 1370 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu", 1371 "s390x-suse-linux", "s390x-redhat-linux" 1372 }; 1373 1374 using std::begin; 1375 using std::end; 1376 1377 switch (TargetTriple.getArch()) { 1378 case llvm::Triple::aarch64: 1379 LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs)); 1380 TripleAliases.append(begin(AArch64Triples), end(AArch64Triples)); 1381 BiarchLibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs)); 1382 BiarchTripleAliases.append(begin(AArch64Triples), end(AArch64Triples)); 1383 break; 1384 case llvm::Triple::aarch64_be: 1385 LibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs)); 1386 TripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples)); 1387 BiarchLibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs)); 1388 BiarchTripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples)); 1389 break; 1390 case llvm::Triple::arm: 1391 case llvm::Triple::thumb: 1392 LibDirs.append(begin(ARMLibDirs), end(ARMLibDirs)); 1393 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) { 1394 TripleAliases.append(begin(ARMHFTriples), end(ARMHFTriples)); 1395 } else { 1396 TripleAliases.append(begin(ARMTriples), end(ARMTriples)); 1397 } 1398 break; 1399 case llvm::Triple::armeb: 1400 case llvm::Triple::thumbeb: 1401 LibDirs.append(begin(ARMebLibDirs), end(ARMebLibDirs)); 1402 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) { 1403 TripleAliases.append(begin(ARMebHFTriples), end(ARMebHFTriples)); 1404 } else { 1405 TripleAliases.append(begin(ARMebTriples), end(ARMebTriples)); 1406 } 1407 break; 1408 case llvm::Triple::x86_64: 1409 LibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs)); 1410 TripleAliases.append(begin(X86_64Triples), end(X86_64Triples)); 1411 // x32 is always available when x86_64 is available, so adding it as 1412 // secondary arch with x86_64 triples 1413 if (TargetTriple.getEnvironment() == llvm::Triple::GNUX32) { 1414 BiarchLibDirs.append(begin(X32LibDirs), end(X32LibDirs)); 1415 BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples)); 1416 } else { 1417 BiarchLibDirs.append(begin(X86LibDirs), end(X86LibDirs)); 1418 BiarchTripleAliases.append(begin(X86Triples), end(X86Triples)); 1419 } 1420 break; 1421 case llvm::Triple::x86: 1422 LibDirs.append(begin(X86LibDirs), end(X86LibDirs)); 1423 TripleAliases.append(begin(X86Triples), end(X86Triples)); 1424 BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs)); 1425 BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples)); 1426 break; 1427 case llvm::Triple::mips: 1428 LibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs)); 1429 TripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 1430 BiarchLibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs)); 1431 BiarchTripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples)); 1432 break; 1433 case llvm::Triple::mipsel: 1434 LibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs)); 1435 TripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples)); 1436 TripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 1437 BiarchLibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs)); 1438 BiarchTripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples)); 1439 break; 1440 case llvm::Triple::mips64: 1441 LibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs)); 1442 TripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples)); 1443 BiarchLibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs)); 1444 BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 1445 break; 1446 case llvm::Triple::mips64el: 1447 LibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs)); 1448 TripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples)); 1449 BiarchLibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs)); 1450 BiarchTripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples)); 1451 BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 1452 break; 1453 case llvm::Triple::ppc: 1454 LibDirs.append(begin(PPCLibDirs), end(PPCLibDirs)); 1455 TripleAliases.append(begin(PPCTriples), end(PPCTriples)); 1456 BiarchLibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs)); 1457 BiarchTripleAliases.append(begin(PPC64Triples), end(PPC64Triples)); 1458 break; 1459 case llvm::Triple::ppc64: 1460 LibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs)); 1461 TripleAliases.append(begin(PPC64Triples), end(PPC64Triples)); 1462 BiarchLibDirs.append(begin(PPCLibDirs), end(PPCLibDirs)); 1463 BiarchTripleAliases.append(begin(PPCTriples), end(PPCTriples)); 1464 break; 1465 case llvm::Triple::ppc64le: 1466 LibDirs.append(begin(PPC64LELibDirs), end(PPC64LELibDirs)); 1467 TripleAliases.append(begin(PPC64LETriples), end(PPC64LETriples)); 1468 break; 1469 case llvm::Triple::sparc: 1470 LibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs)); 1471 TripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples)); 1472 BiarchLibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs)); 1473 BiarchTripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples)); 1474 break; 1475 case llvm::Triple::sparcv9: 1476 LibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs)); 1477 TripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples)); 1478 BiarchLibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs)); 1479 BiarchTripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples)); 1480 break; 1481 case llvm::Triple::systemz: 1482 LibDirs.append(begin(SystemZLibDirs), end(SystemZLibDirs)); 1483 TripleAliases.append(begin(SystemZTriples), end(SystemZTriples)); 1484 break; 1485 1486 default: 1487 // By default, just rely on the standard lib directories and the original 1488 // triple. 1489 break; 1490 } 1491 1492 // Always append the drivers target triple to the end, in case it doesn't 1493 // match any of our aliases. 1494 TripleAliases.push_back(TargetTriple.str()); 1495 1496 // Also include the multiarch variant if it's different. 1497 if (TargetTriple.str() != BiarchTriple.str()) 1498 BiarchTripleAliases.push_back(BiarchTriple.str()); 1499 } 1500 1501 namespace { 1502 // Filter to remove Multilibs that don't exist as a suffix to Path 1503 class FilterNonExistent : public MultilibSet::FilterCallback { 1504 std::string Base; 1505 public: 1506 FilterNonExistent(std::string Base) : Base(Base) {} 1507 bool operator()(const Multilib &M) const override { 1508 return !llvm::sys::fs::exists(Base + M.gccSuffix() + "/crtbegin.o"); 1509 } 1510 }; 1511 } // end anonymous namespace 1512 1513 static void addMultilibFlag(bool Enabled, const char *const Flag, 1514 std::vector<std::string> &Flags) { 1515 if (Enabled) 1516 Flags.push_back(std::string("+") + Flag); 1517 else 1518 Flags.push_back(std::string("-") + Flag); 1519 } 1520 1521 static bool isMipsArch(llvm::Triple::ArchType Arch) { 1522 return Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel || 1523 Arch == llvm::Triple::mips64 || Arch == llvm::Triple::mips64el; 1524 } 1525 1526 static bool isMips32(llvm::Triple::ArchType Arch) { 1527 return Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel; 1528 } 1529 1530 static bool isMips64(llvm::Triple::ArchType Arch) { 1531 return Arch == llvm::Triple::mips64 || Arch == llvm::Triple::mips64el; 1532 } 1533 1534 static bool isMipsEL(llvm::Triple::ArchType Arch) { 1535 return Arch == llvm::Triple::mipsel || Arch == llvm::Triple::mips64el; 1536 } 1537 1538 static bool isMips16(const ArgList &Args) { 1539 Arg *A = Args.getLastArg(options::OPT_mips16, 1540 options::OPT_mno_mips16); 1541 return A && A->getOption().matches(options::OPT_mips16); 1542 } 1543 1544 static bool isMicroMips(const ArgList &Args) { 1545 Arg *A = Args.getLastArg(options::OPT_mmicromips, 1546 options::OPT_mno_micromips); 1547 return A && A->getOption().matches(options::OPT_mmicromips); 1548 } 1549 1550 struct DetectedMultilibs { 1551 /// The set of multilibs that the detected installation supports. 1552 MultilibSet Multilibs; 1553 1554 /// The primary multilib appropriate for the given flags. 1555 Multilib SelectedMultilib; 1556 1557 /// On Biarch systems, this corresponds to the default multilib when 1558 /// targeting the non-default multilib. Otherwise, it is empty. 1559 llvm::Optional<Multilib> BiarchSibling; 1560 }; 1561 1562 static Multilib makeMultilib(StringRef commonSuffix) { 1563 return Multilib(commonSuffix, commonSuffix, commonSuffix); 1564 } 1565 1566 static bool findMIPSMultilibs(const llvm::Triple &TargetTriple, StringRef Path, 1567 const llvm::opt::ArgList &Args, 1568 DetectedMultilibs &Result) { 1569 // Some MIPS toolchains put libraries and object files compiled 1570 // using different options in to the sub-directoris which names 1571 // reflects the flags used for compilation. For example sysroot 1572 // directory might looks like the following examples: 1573 // 1574 // /usr 1575 // /lib <= crt*.o files compiled with '-mips32' 1576 // /mips16 1577 // /usr 1578 // /lib <= crt*.o files compiled with '-mips16' 1579 // /el 1580 // /usr 1581 // /lib <= crt*.o files compiled with '-mips16 -EL' 1582 // 1583 // or 1584 // 1585 // /usr 1586 // /lib <= crt*.o files compiled with '-mips32r2' 1587 // /mips16 1588 // /usr 1589 // /lib <= crt*.o files compiled with '-mips32r2 -mips16' 1590 // /mips32 1591 // /usr 1592 // /lib <= crt*.o files compiled with '-mips32' 1593 1594 FilterNonExistent NonExistent(Path); 1595 1596 // Check for FSF toolchain multilibs 1597 MultilibSet FSFMipsMultilibs; 1598 { 1599 auto MArchMips32 = makeMultilib("/mips32") 1600 .flag("+m32").flag("-m64").flag("-mmicromips").flag("+march=mips32"); 1601 1602 auto MArchMicroMips = makeMultilib("/micromips") 1603 .flag("+m32").flag("-m64").flag("+mmicromips"); 1604 1605 auto MArchMips64r2 = makeMultilib("/mips64r2") 1606 .flag("-m32").flag("+m64").flag("+march=mips64r2"); 1607 1608 auto MArchMips64 = makeMultilib("/mips64") 1609 .flag("-m32").flag("+m64").flag("-march=mips64r2"); 1610 1611 auto MArchDefault = makeMultilib("") 1612 .flag("+m32").flag("-m64").flag("-mmicromips").flag("+march=mips32r2"); 1613 1614 auto Mips16 = makeMultilib("/mips16") 1615 .flag("+mips16"); 1616 1617 auto UCLibc = makeMultilib("/uclibc") 1618 .flag("+muclibc"); 1619 1620 auto MAbi64 = makeMultilib("/64") 1621 .flag("+mabi=n64").flag("-mabi=n32").flag("-m32"); 1622 1623 auto BigEndian = makeMultilib("") 1624 .flag("+EB").flag("-EL"); 1625 1626 auto LittleEndian = makeMultilib("/el") 1627 .flag("+EL").flag("-EB"); 1628 1629 auto SoftFloat = makeMultilib("/sof") 1630 .flag("+msoft-float"); 1631 1632 auto Nan2008 = makeMultilib("/nan2008") 1633 .flag("+mnan=2008"); 1634 1635 FSFMipsMultilibs = MultilibSet() 1636 .Either(MArchMips32, MArchMicroMips, 1637 MArchMips64r2, MArchMips64, MArchDefault) 1638 .Maybe(UCLibc) 1639 .Maybe(Mips16) 1640 .FilterOut("/mips64/mips16") 1641 .FilterOut("/mips64r2/mips16") 1642 .FilterOut("/micromips/mips16") 1643 .Maybe(MAbi64) 1644 .FilterOut("/micromips/64") 1645 .FilterOut("/mips32/64") 1646 .FilterOut("^/64") 1647 .FilterOut("/mips16/64") 1648 .Either(BigEndian, LittleEndian) 1649 .Maybe(SoftFloat) 1650 .Maybe(Nan2008) 1651 .FilterOut(".*sof/nan2008") 1652 .FilterOut(NonExistent) 1653 .setIncludeDirsCallback([]( 1654 StringRef InstallDir, StringRef TripleStr, const Multilib &M) { 1655 std::vector<std::string> Dirs; 1656 Dirs.push_back((InstallDir + "/include").str()); 1657 std::string SysRootInc = InstallDir.str() + "/../../../../sysroot"; 1658 if (StringRef(M.includeSuffix()).startswith("/uclibc")) 1659 Dirs.push_back(SysRootInc + "/uclibc/usr/include"); 1660 else 1661 Dirs.push_back(SysRootInc + "/usr/include"); 1662 return Dirs; 1663 }); 1664 } 1665 1666 // Check for Code Sourcery toolchain multilibs 1667 MultilibSet CSMipsMultilibs; 1668 { 1669 auto MArchMips16 = makeMultilib("/mips16") 1670 .flag("+m32").flag("+mips16"); 1671 1672 auto MArchMicroMips = makeMultilib("/micromips") 1673 .flag("+m32").flag("+mmicromips"); 1674 1675 auto MArchDefault = makeMultilib("") 1676 .flag("-mips16").flag("-mmicromips"); 1677 1678 auto UCLibc = makeMultilib("/uclibc") 1679 .flag("+muclibc"); 1680 1681 auto SoftFloat = makeMultilib("/soft-float") 1682 .flag("+msoft-float"); 1683 1684 auto Nan2008 = makeMultilib("/nan2008") 1685 .flag("+mnan=2008"); 1686 1687 auto DefaultFloat = makeMultilib("") 1688 .flag("-msoft-float").flag("-mnan=2008"); 1689 1690 auto BigEndian = makeMultilib("") 1691 .flag("+EB").flag("-EL"); 1692 1693 auto LittleEndian = makeMultilib("/el") 1694 .flag("+EL").flag("-EB"); 1695 1696 // Note that this one's osSuffix is "" 1697 auto MAbi64 = makeMultilib("") 1698 .gccSuffix("/64") 1699 .includeSuffix("/64") 1700 .flag("+mabi=n64").flag("-mabi=n32").flag("-m32"); 1701 1702 CSMipsMultilibs = MultilibSet() 1703 .Either(MArchMips16, MArchMicroMips, MArchDefault) 1704 .Maybe(UCLibc) 1705 .Either(SoftFloat, Nan2008, DefaultFloat) 1706 .FilterOut("/micromips/nan2008") 1707 .FilterOut("/mips16/nan2008") 1708 .Either(BigEndian, LittleEndian) 1709 .Maybe(MAbi64) 1710 .FilterOut("/mips16.*/64") 1711 .FilterOut("/micromips.*/64") 1712 .FilterOut(NonExistent) 1713 .setIncludeDirsCallback([]( 1714 StringRef InstallDir, StringRef TripleStr, const Multilib &M) { 1715 std::vector<std::string> Dirs; 1716 Dirs.push_back((InstallDir + "/include").str()); 1717 std::string SysRootInc = 1718 InstallDir.str() + "/../../../../" + TripleStr.str(); 1719 if (StringRef(M.includeSuffix()).startswith("/uclibc")) 1720 Dirs.push_back(SysRootInc + "/libc/uclibc/usr/include"); 1721 else 1722 Dirs.push_back(SysRootInc + "/libc/usr/include"); 1723 return Dirs; 1724 }); 1725 } 1726 1727 MultilibSet AndroidMipsMultilibs = MultilibSet() 1728 .Maybe(Multilib("/mips-r2").flag("+march=mips32r2")) 1729 .Maybe(Multilib("/mips-r6").flag("+march=mips32r6")) 1730 .FilterOut(NonExistent); 1731 1732 MultilibSet DebianMipsMultilibs; 1733 { 1734 Multilib MAbiN32 = Multilib() 1735 .gccSuffix("/n32") 1736 .includeSuffix("/n32") 1737 .flag("+mabi=n32"); 1738 1739 Multilib M64 = Multilib() 1740 .gccSuffix("/64") 1741 .includeSuffix("/64") 1742 .flag("+m64").flag("-m32").flag("-mabi=n32"); 1743 1744 Multilib M32 = Multilib() 1745 .flag("-m64").flag("+m32").flag("-mabi=n32"); 1746 1747 DebianMipsMultilibs = MultilibSet() 1748 .Either(M32, M64, MAbiN32) 1749 .FilterOut(NonExistent); 1750 } 1751 1752 MultilibSet ImgMultilibs; 1753 { 1754 auto Mips64r6 = makeMultilib("/mips64r6") 1755 .flag("+m64").flag("-m32"); 1756 1757 auto LittleEndian = makeMultilib("/el") 1758 .flag("+EL").flag("-EB"); 1759 1760 auto MAbi64 = makeMultilib("/64") 1761 .flag("+mabi=n64").flag("-mabi=n32").flag("-m32"); 1762 1763 ImgMultilibs = MultilibSet() 1764 .Maybe(Mips64r6) 1765 .Maybe(MAbi64) 1766 .Maybe(LittleEndian) 1767 .FilterOut(NonExistent) 1768 .setIncludeDirsCallback([]( 1769 StringRef InstallDir, StringRef TripleStr, const Multilib &M) { 1770 std::vector<std::string> Dirs; 1771 Dirs.push_back((InstallDir + "/include").str()); 1772 Dirs.push_back((InstallDir + "/../../../../sysroot/usr/include").str()); 1773 return Dirs; 1774 }); 1775 } 1776 1777 StringRef CPUName; 1778 StringRef ABIName; 1779 tools::mips::getMipsCPUAndABI(Args, TargetTriple, CPUName, ABIName); 1780 1781 llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); 1782 1783 Multilib::flags_list Flags; 1784 addMultilibFlag(isMips32(TargetArch), "m32", Flags); 1785 addMultilibFlag(isMips64(TargetArch), "m64", Flags); 1786 addMultilibFlag(isMips16(Args), "mips16", Flags); 1787 addMultilibFlag(CPUName == "mips32", "march=mips32", Flags); 1788 addMultilibFlag(CPUName == "mips32r2", "march=mips32r2", Flags); 1789 addMultilibFlag(CPUName == "mips32r6", "march=mips32r6", Flags); 1790 addMultilibFlag(CPUName == "mips64", "march=mips64", Flags); 1791 addMultilibFlag(CPUName == "mips64r2" || CPUName == "octeon", 1792 "march=mips64r2", Flags); 1793 addMultilibFlag(isMicroMips(Args), "mmicromips", Flags); 1794 addMultilibFlag(tools::mips::isUCLibc(Args), "muclibc", Flags); 1795 addMultilibFlag(tools::mips::isNaN2008(Args, TargetTriple), "mnan=2008", 1796 Flags); 1797 addMultilibFlag(ABIName == "n32", "mabi=n32", Flags); 1798 addMultilibFlag(ABIName == "n64", "mabi=n64", Flags); 1799 addMultilibFlag(isSoftFloatABI(Args), "msoft-float", Flags); 1800 addMultilibFlag(!isSoftFloatABI(Args), "mhard-float", Flags); 1801 addMultilibFlag(isMipsEL(TargetArch), "EL", Flags); 1802 addMultilibFlag(!isMipsEL(TargetArch), "EB", Flags); 1803 1804 if (TargetTriple.getEnvironment() == llvm::Triple::Android) { 1805 // Select Android toolchain. It's the only choice in that case. 1806 if (AndroidMipsMultilibs.select(Flags, Result.SelectedMultilib)) { 1807 Result.Multilibs = AndroidMipsMultilibs; 1808 return true; 1809 } 1810 return false; 1811 } 1812 1813 if (TargetTriple.getVendor() == llvm::Triple::ImaginationTechnologies && 1814 TargetTriple.getOS() == llvm::Triple::Linux && 1815 TargetTriple.getEnvironment() == llvm::Triple::GNU) { 1816 // Select mips-img-linux-gnu toolchain. 1817 if (ImgMultilibs.select(Flags, Result.SelectedMultilib)) { 1818 Result.Multilibs = ImgMultilibs; 1819 return true; 1820 } 1821 return false; 1822 } 1823 1824 // Sort candidates. Toolchain that best meets the directories goes first. 1825 // Then select the first toolchains matches command line flags. 1826 MultilibSet *candidates[] = { &DebianMipsMultilibs, &FSFMipsMultilibs, 1827 &CSMipsMultilibs }; 1828 std::sort( 1829 std::begin(candidates), std::end(candidates), 1830 [](MultilibSet *a, MultilibSet *b) { return a->size() > b->size(); }); 1831 for (const auto &candidate : candidates) { 1832 if (candidate->select(Flags, Result.SelectedMultilib)) { 1833 if (candidate == &DebianMipsMultilibs) 1834 Result.BiarchSibling = Multilib(); 1835 Result.Multilibs = *candidate; 1836 return true; 1837 } 1838 } 1839 1840 { 1841 // Fallback to the regular toolchain-tree structure. 1842 Multilib Default; 1843 Result.Multilibs.push_back(Default); 1844 Result.Multilibs.FilterOut(NonExistent); 1845 1846 if (Result.Multilibs.select(Flags, Result.SelectedMultilib)) { 1847 Result.BiarchSibling = Multilib(); 1848 return true; 1849 } 1850 } 1851 1852 return false; 1853 } 1854 1855 static bool findBiarchMultilibs(const llvm::Triple &TargetTriple, 1856 StringRef Path, const ArgList &Args, 1857 bool NeedsBiarchSuffix, 1858 DetectedMultilibs &Result) { 1859 1860 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs 1861 // in what would normally be GCCInstallPath and put the 64-bit 1862 // libs in a subdirectory named 64. The simple logic we follow is that 1863 // *if* there is a subdirectory of the right name with crtbegin.o in it, 1864 // we use that. If not, and if not a biarch triple alias, we look for 1865 // crtbegin.o without the subdirectory. 1866 1867 Multilib Default; 1868 Multilib Alt64 = Multilib() 1869 .gccSuffix("/64") 1870 .includeSuffix("/64") 1871 .flag("-m32").flag("+m64").flag("-mx32"); 1872 Multilib Alt32 = Multilib() 1873 .gccSuffix("/32") 1874 .includeSuffix("/32") 1875 .flag("+m32").flag("-m64").flag("-mx32"); 1876 Multilib Altx32 = Multilib() 1877 .gccSuffix("/x32") 1878 .includeSuffix("/x32") 1879 .flag("-m32").flag("-m64").flag("+mx32"); 1880 1881 FilterNonExistent NonExistent(Path); 1882 1883 // Determine default multilib from: 32, 64, x32 1884 // Also handle cases such as 64 on 32, 32 on 64, etc. 1885 enum { UNKNOWN, WANT32, WANT64, WANTX32 } Want = UNKNOWN; 1886 const bool IsX32 = TargetTriple.getEnvironment() == llvm::Triple::GNUX32; 1887 if (TargetTriple.isArch32Bit() && !NonExistent(Alt32)) 1888 Want = WANT64; 1889 else if (TargetTriple.isArch64Bit() && IsX32 && !NonExistent(Altx32)) 1890 Want = WANT64; 1891 else if (TargetTriple.isArch64Bit() && !IsX32 && !NonExistent(Alt64)) 1892 Want = WANT32; 1893 else { 1894 if (TargetTriple.isArch32Bit()) 1895 Want = NeedsBiarchSuffix ? WANT64 : WANT32; 1896 else if (IsX32) 1897 Want = NeedsBiarchSuffix ? WANT64 : WANTX32; 1898 else 1899 Want = NeedsBiarchSuffix ? WANT32 : WANT64; 1900 } 1901 1902 if (Want == WANT32) 1903 Default.flag("+m32").flag("-m64").flag("-mx32"); 1904 else if (Want == WANT64) 1905 Default.flag("-m32").flag("+m64").flag("-mx32"); 1906 else if (Want == WANTX32) 1907 Default.flag("-m32").flag("-m64").flag("+mx32"); 1908 else 1909 return false; 1910 1911 Result.Multilibs.push_back(Default); 1912 Result.Multilibs.push_back(Alt64); 1913 Result.Multilibs.push_back(Alt32); 1914 Result.Multilibs.push_back(Altx32); 1915 1916 Result.Multilibs.FilterOut(NonExistent); 1917 1918 Multilib::flags_list Flags; 1919 addMultilibFlag(TargetTriple.isArch64Bit() && !IsX32, "m64", Flags); 1920 addMultilibFlag(TargetTriple.isArch32Bit(), "m32", Flags); 1921 addMultilibFlag(TargetTriple.isArch64Bit() && IsX32, "mx32", Flags); 1922 1923 if (!Result.Multilibs.select(Flags, Result.SelectedMultilib)) 1924 return false; 1925 1926 if (Result.SelectedMultilib == Alt64 || 1927 Result.SelectedMultilib == Alt32 || 1928 Result.SelectedMultilib == Altx32) 1929 Result.BiarchSibling = Default; 1930 1931 return true; 1932 } 1933 1934 void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple( 1935 const llvm::Triple &TargetTriple, const ArgList &Args, 1936 const std::string &LibDir, StringRef CandidateTriple, 1937 bool NeedsBiarchSuffix) { 1938 llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); 1939 // There are various different suffixes involving the triple we 1940 // check for. We also record what is necessary to walk from each back 1941 // up to the lib directory. 1942 const std::string LibSuffixes[] = { 1943 "/gcc/" + CandidateTriple.str(), 1944 // Debian puts cross-compilers in gcc-cross 1945 "/gcc-cross/" + CandidateTriple.str(), 1946 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(), 1947 1948 // The Freescale PPC SDK has the gcc libraries in 1949 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well. 1950 "/" + CandidateTriple.str(), 1951 1952 // Ubuntu has a strange mis-matched pair of triples that this happens to 1953 // match. 1954 // FIXME: It may be worthwhile to generalize this and look for a second 1955 // triple. 1956 "/i386-linux-gnu/gcc/" + CandidateTriple.str() 1957 }; 1958 const std::string InstallSuffixes[] = { 1959 "/../../..", // gcc/ 1960 "/../../..", // gcc-cross/ 1961 "/../../../..", // <triple>/gcc/ 1962 "/../..", // <triple>/ 1963 "/../../../.." // i386-linux-gnu/gcc/<triple>/ 1964 }; 1965 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu. 1966 const unsigned NumLibSuffixes = 1967 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86)); 1968 for (unsigned i = 0; i < NumLibSuffixes; ++i) { 1969 StringRef LibSuffix = LibSuffixes[i]; 1970 std::error_code EC; 1971 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE; 1972 !EC && LI != LE; LI = LI.increment(EC)) { 1973 StringRef VersionText = llvm::sys::path::filename(LI->path()); 1974 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText); 1975 if (CandidateVersion.Major != -1) // Filter obviously bad entries. 1976 if (!CandidateGCCInstallPaths.insert(LI->path()).second) 1977 continue; // Saw this path before; no need to look at it again. 1978 if (CandidateVersion.isOlderThan(4, 1, 1)) 1979 continue; 1980 if (CandidateVersion <= Version) 1981 continue; 1982 1983 DetectedMultilibs Detected; 1984 1985 // Debian mips multilibs behave more like the rest of the biarch ones, 1986 // so handle them there 1987 if (isMipsArch(TargetArch)) { 1988 if (!findMIPSMultilibs(TargetTriple, LI->path(), Args, Detected)) 1989 continue; 1990 } else if (!findBiarchMultilibs(TargetTriple, LI->path(), Args, 1991 NeedsBiarchSuffix, Detected)) { 1992 continue; 1993 } 1994 1995 Multilibs = Detected.Multilibs; 1996 SelectedMultilib = Detected.SelectedMultilib; 1997 BiarchSibling = Detected.BiarchSibling; 1998 Version = CandidateVersion; 1999 GCCTriple.setTriple(CandidateTriple); 2000 // FIXME: We hack together the directory name here instead of 2001 // using LI to ensure stable path separators across Windows and 2002 // Linux. 2003 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str(); 2004 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i]; 2005 IsValid = true; 2006 } 2007 } 2008 } 2009 2010 Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple, 2011 const ArgList &Args) 2012 : ToolChain(D, Triple, Args), GCCInstallation() { 2013 getProgramPaths().push_back(getDriver().getInstalledDir()); 2014 if (getDriver().getInstalledDir() != getDriver().Dir) 2015 getProgramPaths().push_back(getDriver().Dir); 2016 } 2017 2018 Generic_GCC::~Generic_GCC() { 2019 } 2020 2021 Tool *Generic_GCC::getTool(Action::ActionClass AC) const { 2022 switch (AC) { 2023 case Action::PreprocessJobClass: 2024 if (!Preprocess) 2025 Preprocess.reset(new tools::gcc::Preprocess(*this)); 2026 return Preprocess.get(); 2027 case Action::CompileJobClass: 2028 if (!Compile) 2029 Compile.reset(new tools::gcc::Compile(*this)); 2030 return Compile.get(); 2031 default: 2032 return ToolChain::getTool(AC); 2033 } 2034 } 2035 2036 Tool *Generic_GCC::buildAssembler() const { 2037 return new tools::gnutools::Assemble(*this); 2038 } 2039 2040 Tool *Generic_GCC::buildLinker() const { 2041 return new tools::gcc::Link(*this); 2042 } 2043 2044 void Generic_GCC::printVerboseInfo(raw_ostream &OS) const { 2045 // Print the information about how we detected the GCC installation. 2046 GCCInstallation.print(OS); 2047 } 2048 2049 bool Generic_GCC::IsUnwindTablesDefault() const { 2050 return getArch() == llvm::Triple::x86_64; 2051 } 2052 2053 bool Generic_GCC::isPICDefault() const { 2054 return false; 2055 } 2056 2057 bool Generic_GCC::isPIEDefault() const { 2058 return false; 2059 } 2060 2061 bool Generic_GCC::isPICDefaultForced() const { 2062 return false; 2063 } 2064 2065 bool Generic_GCC::IsIntegratedAssemblerDefault() const { 2066 return getTriple().getArch() == llvm::Triple::x86 || 2067 getTriple().getArch() == llvm::Triple::x86_64 || 2068 getTriple().getArch() == llvm::Triple::aarch64 || 2069 getTriple().getArch() == llvm::Triple::aarch64_be || 2070 getTriple().getArch() == llvm::Triple::arm || 2071 getTriple().getArch() == llvm::Triple::armeb || 2072 getTriple().getArch() == llvm::Triple::thumb || 2073 getTriple().getArch() == llvm::Triple::thumbeb || 2074 getTriple().getArch() == llvm::Triple::ppc || 2075 getTriple().getArch() == llvm::Triple::ppc64 || 2076 getTriple().getArch() == llvm::Triple::ppc64le || 2077 getTriple().getArch() == llvm::Triple::sparc || 2078 getTriple().getArch() == llvm::Triple::sparcv9 || 2079 getTriple().getArch() == llvm::Triple::systemz; 2080 } 2081 2082 void Generic_ELF::addClangTargetOptions(const ArgList &DriverArgs, 2083 ArgStringList &CC1Args) const { 2084 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion(); 2085 bool UseInitArrayDefault = 2086 getTriple().getArch() == llvm::Triple::aarch64 || 2087 getTriple().getArch() == llvm::Triple::aarch64_be || 2088 (getTriple().getOS() == llvm::Triple::Linux && 2089 (!V.isOlderThan(4, 7, 0) || 2090 getTriple().getEnvironment() == llvm::Triple::Android)); 2091 2092 if (DriverArgs.hasFlag(options::OPT_fuse_init_array, 2093 options::OPT_fno_use_init_array, 2094 UseInitArrayDefault)) 2095 CC1Args.push_back("-fuse-init-array"); 2096 } 2097 2098 /// Hexagon Toolchain 2099 2100 std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir, 2101 const ArgList &Args) { 2102 2103 // Locate the rest of the toolchain ... 2104 std::string GccToolchain = getGCCToolchainDir(Args); 2105 2106 if (!GccToolchain.empty()) 2107 return GccToolchain; 2108 2109 std::string InstallRelDir = InstalledDir + "/../../gnu"; 2110 if (llvm::sys::fs::exists(InstallRelDir)) 2111 return InstallRelDir; 2112 2113 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu"; 2114 if (llvm::sys::fs::exists(PrefixRelDir)) 2115 return PrefixRelDir; 2116 2117 return InstallRelDir; 2118 } 2119 2120 static void GetHexagonLibraryPaths( 2121 const ArgList &Args, 2122 const std::string &Ver, 2123 const std::string &MarchString, 2124 const std::string &InstalledDir, 2125 ToolChain::path_list *LibPaths) 2126 { 2127 bool buildingLib = Args.hasArg(options::OPT_shared); 2128 2129 //---------------------------------------------------------------------------- 2130 // -L Args 2131 //---------------------------------------------------------------------------- 2132 for (arg_iterator 2133 it = Args.filtered_begin(options::OPT_L), 2134 ie = Args.filtered_end(); 2135 it != ie; 2136 ++it) { 2137 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i) 2138 LibPaths->push_back((*it)->getValue(i)); 2139 } 2140 2141 //---------------------------------------------------------------------------- 2142 // Other standard paths 2143 //---------------------------------------------------------------------------- 2144 const std::string MarchSuffix = "/" + MarchString; 2145 const std::string G0Suffix = "/G0"; 2146 const std::string MarchG0Suffix = MarchSuffix + G0Suffix; 2147 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir, Args) + "/"; 2148 2149 // lib/gcc/hexagon/... 2150 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/"; 2151 if (buildingLib) { 2152 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix); 2153 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix); 2154 } 2155 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix); 2156 LibPaths->push_back(LibGCCHexagonDir + Ver); 2157 2158 // lib/gcc/... 2159 LibPaths->push_back(RootDir + "lib/gcc"); 2160 2161 // hexagon/lib/... 2162 std::string HexagonLibDir = RootDir + "hexagon/lib"; 2163 if (buildingLib) { 2164 LibPaths->push_back(HexagonLibDir + MarchG0Suffix); 2165 LibPaths->push_back(HexagonLibDir + G0Suffix); 2166 } 2167 LibPaths->push_back(HexagonLibDir + MarchSuffix); 2168 LibPaths->push_back(HexagonLibDir); 2169 } 2170 2171 Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple, 2172 const ArgList &Args) 2173 : Linux(D, Triple, Args) { 2174 const std::string InstalledDir(getDriver().getInstalledDir()); 2175 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir, Args); 2176 2177 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to 2178 // program paths 2179 const std::string BinDir(GnuDir + "/bin"); 2180 if (llvm::sys::fs::exists(BinDir)) 2181 getProgramPaths().push_back(BinDir); 2182 2183 // Determine version of GCC libraries and headers to use. 2184 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon"); 2185 std::error_code ec; 2186 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0"); 2187 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de; 2188 !ec && di != de; di = di.increment(ec)) { 2189 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path())); 2190 if (MaxVersion < cv) 2191 MaxVersion = cv; 2192 } 2193 GCCLibAndIncVersion = MaxVersion; 2194 2195 ToolChain::path_list *LibPaths= &getFilePaths(); 2196 2197 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets 2198 // 'elf' OS type, so the Linux paths are not appropriate. When we actually 2199 // support 'linux' we'll need to fix this up 2200 LibPaths->clear(); 2201 2202 GetHexagonLibraryPaths( 2203 Args, 2204 GetGCCLibAndIncVersion(), 2205 GetTargetCPU(Args), 2206 InstalledDir, 2207 LibPaths); 2208 } 2209 2210 Hexagon_TC::~Hexagon_TC() { 2211 } 2212 2213 Tool *Hexagon_TC::buildAssembler() const { 2214 return new tools::hexagon::Assemble(*this); 2215 } 2216 2217 Tool *Hexagon_TC::buildLinker() const { 2218 return new tools::hexagon::Link(*this); 2219 } 2220 2221 void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 2222 ArgStringList &CC1Args) const { 2223 const Driver &D = getDriver(); 2224 2225 if (DriverArgs.hasArg(options::OPT_nostdinc) || 2226 DriverArgs.hasArg(options::OPT_nostdlibinc)) 2227 return; 2228 2229 std::string Ver(GetGCCLibAndIncVersion()); 2230 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir, DriverArgs); 2231 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver); 2232 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include"); 2233 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed"); 2234 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include"); 2235 } 2236 2237 void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 2238 ArgStringList &CC1Args) const { 2239 2240 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 2241 DriverArgs.hasArg(options::OPT_nostdincxx)) 2242 return; 2243 2244 const Driver &D = getDriver(); 2245 std::string Ver(GetGCCLibAndIncVersion()); 2246 SmallString<128> IncludeDir( 2247 Hexagon_TC::GetGnuDir(D.InstalledDir, DriverArgs)); 2248 2249 llvm::sys::path::append(IncludeDir, "hexagon/include/c++/"); 2250 llvm::sys::path::append(IncludeDir, Ver); 2251 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str()); 2252 } 2253 2254 ToolChain::CXXStdlibType 2255 Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const { 2256 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ); 2257 if (!A) 2258 return ToolChain::CST_Libstdcxx; 2259 2260 StringRef Value = A->getValue(); 2261 if (Value != "libstdc++") { 2262 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 2263 << A->getAsString(Args); 2264 } 2265 2266 return ToolChain::CST_Libstdcxx; 2267 } 2268 2269 static int getHexagonVersion(const ArgList &Args) { 2270 Arg *A = Args.getLastArg(options::OPT_march_EQ, options::OPT_mcpu_EQ); 2271 // Select the default CPU (v4) if none was given. 2272 if (!A) 2273 return 4; 2274 2275 // FIXME: produce errors if we cannot parse the version. 2276 StringRef WhichHexagon = A->getValue(); 2277 if (WhichHexagon.startswith("hexagonv")) { 2278 int Val; 2279 if (!WhichHexagon.substr(sizeof("hexagonv") - 1).getAsInteger(10, Val)) 2280 return Val; 2281 } 2282 if (WhichHexagon.startswith("v")) { 2283 int Val; 2284 if (!WhichHexagon.substr(1).getAsInteger(10, Val)) 2285 return Val; 2286 } 2287 2288 // FIXME: should probably be an error. 2289 return 4; 2290 } 2291 2292 StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args) 2293 { 2294 int V = getHexagonVersion(Args); 2295 // FIXME: We don't support versions < 4. We should error on them. 2296 switch (V) { 2297 default: 2298 llvm_unreachable("Unexpected version"); 2299 case 5: 2300 return "v5"; 2301 case 4: 2302 return "v4"; 2303 case 3: 2304 return "v3"; 2305 case 2: 2306 return "v2"; 2307 case 1: 2308 return "v1"; 2309 } 2310 } 2311 // End Hexagon 2312 2313 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform 2314 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target. 2315 /// Currently does not support anything else but compilation. 2316 2317 TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple, 2318 const ArgList &Args) 2319 : ToolChain(D, Triple, Args) { 2320 // Path mangling to find libexec 2321 std::string Path(getDriver().Dir); 2322 2323 Path += "/../libexec"; 2324 getProgramPaths().push_back(Path); 2325 } 2326 2327 TCEToolChain::~TCEToolChain() { 2328 } 2329 2330 bool TCEToolChain::IsMathErrnoDefault() const { 2331 return true; 2332 } 2333 2334 bool TCEToolChain::isPICDefault() const { 2335 return false; 2336 } 2337 2338 bool TCEToolChain::isPIEDefault() const { 2339 return false; 2340 } 2341 2342 bool TCEToolChain::isPICDefaultForced() const { 2343 return false; 2344 } 2345 2346 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly. 2347 2348 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 2349 : Generic_ELF(D, Triple, Args) { 2350 getFilePaths().push_back(getDriver().Dir + "/../lib"); 2351 getFilePaths().push_back("/usr/lib"); 2352 } 2353 2354 Tool *OpenBSD::buildAssembler() const { 2355 return new tools::openbsd::Assemble(*this); 2356 } 2357 2358 Tool *OpenBSD::buildLinker() const { 2359 return new tools::openbsd::Link(*this); 2360 } 2361 2362 /// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly. 2363 2364 Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 2365 : Generic_ELF(D, Triple, Args) { 2366 getFilePaths().push_back(getDriver().Dir + "/../lib"); 2367 getFilePaths().push_back("/usr/lib"); 2368 } 2369 2370 Tool *Bitrig::buildAssembler() const { 2371 return new tools::bitrig::Assemble(*this); 2372 } 2373 2374 Tool *Bitrig::buildLinker() const { 2375 return new tools::bitrig::Link(*this); 2376 } 2377 2378 ToolChain::CXXStdlibType 2379 Bitrig::GetCXXStdlibType(const ArgList &Args) const { 2380 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 2381 StringRef Value = A->getValue(); 2382 if (Value == "libstdc++") 2383 return ToolChain::CST_Libstdcxx; 2384 if (Value == "libc++") 2385 return ToolChain::CST_Libcxx; 2386 2387 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 2388 << A->getAsString(Args); 2389 } 2390 return ToolChain::CST_Libcxx; 2391 } 2392 2393 void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 2394 ArgStringList &CC1Args) const { 2395 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 2396 DriverArgs.hasArg(options::OPT_nostdincxx)) 2397 return; 2398 2399 switch (GetCXXStdlibType(DriverArgs)) { 2400 case ToolChain::CST_Libcxx: 2401 addSystemInclude(DriverArgs, CC1Args, 2402 getDriver().SysRoot + "/usr/include/c++/v1"); 2403 break; 2404 case ToolChain::CST_Libstdcxx: 2405 addSystemInclude(DriverArgs, CC1Args, 2406 getDriver().SysRoot + "/usr/include/c++/stdc++"); 2407 addSystemInclude(DriverArgs, CC1Args, 2408 getDriver().SysRoot + "/usr/include/c++/stdc++/backward"); 2409 2410 StringRef Triple = getTriple().str(); 2411 if (Triple.startswith("amd64")) 2412 addSystemInclude(DriverArgs, CC1Args, 2413 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" + 2414 Triple.substr(5)); 2415 else 2416 addSystemInclude(DriverArgs, CC1Args, 2417 getDriver().SysRoot + "/usr/include/c++/stdc++/" + 2418 Triple); 2419 break; 2420 } 2421 } 2422 2423 void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args, 2424 ArgStringList &CmdArgs) const { 2425 switch (GetCXXStdlibType(Args)) { 2426 case ToolChain::CST_Libcxx: 2427 CmdArgs.push_back("-lc++"); 2428 CmdArgs.push_back("-lc++abi"); 2429 CmdArgs.push_back("-lpthread"); 2430 break; 2431 case ToolChain::CST_Libstdcxx: 2432 CmdArgs.push_back("-lstdc++"); 2433 break; 2434 } 2435 } 2436 2437 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly. 2438 2439 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 2440 : Generic_ELF(D, Triple, Args) { 2441 2442 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall 2443 // back to '/usr/lib' if it doesn't exist. 2444 if ((Triple.getArch() == llvm::Triple::x86 || 2445 Triple.getArch() == llvm::Triple::ppc) && 2446 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o")) 2447 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32"); 2448 else 2449 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib"); 2450 } 2451 2452 ToolChain::CXXStdlibType 2453 FreeBSD::GetCXXStdlibType(const ArgList &Args) const { 2454 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 2455 StringRef Value = A->getValue(); 2456 if (Value == "libstdc++") 2457 return ToolChain::CST_Libstdcxx; 2458 if (Value == "libc++") 2459 return ToolChain::CST_Libcxx; 2460 2461 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 2462 << A->getAsString(Args); 2463 } 2464 if (getTriple().getOSMajorVersion() >= 10) 2465 return ToolChain::CST_Libcxx; 2466 return ToolChain::CST_Libstdcxx; 2467 } 2468 2469 void FreeBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 2470 ArgStringList &CC1Args) const { 2471 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 2472 DriverArgs.hasArg(options::OPT_nostdincxx)) 2473 return; 2474 2475 switch (GetCXXStdlibType(DriverArgs)) { 2476 case ToolChain::CST_Libcxx: 2477 addSystemInclude(DriverArgs, CC1Args, 2478 getDriver().SysRoot + "/usr/include/c++/v1"); 2479 break; 2480 case ToolChain::CST_Libstdcxx: 2481 addSystemInclude(DriverArgs, CC1Args, 2482 getDriver().SysRoot + "/usr/include/c++/4.2"); 2483 addSystemInclude(DriverArgs, CC1Args, 2484 getDriver().SysRoot + "/usr/include/c++/4.2/backward"); 2485 break; 2486 } 2487 } 2488 2489 Tool *FreeBSD::buildAssembler() const { 2490 return new tools::freebsd::Assemble(*this); 2491 } 2492 2493 Tool *FreeBSD::buildLinker() const { 2494 return new tools::freebsd::Link(*this); 2495 } 2496 2497 bool FreeBSD::UseSjLjExceptions() const { 2498 // FreeBSD uses SjLj exceptions on ARM oabi. 2499 switch (getTriple().getEnvironment()) { 2500 case llvm::Triple::GNUEABIHF: 2501 case llvm::Triple::GNUEABI: 2502 case llvm::Triple::EABI: 2503 return false; 2504 2505 default: 2506 return (getTriple().getArch() == llvm::Triple::arm || 2507 getTriple().getArch() == llvm::Triple::thumb); 2508 } 2509 } 2510 2511 bool FreeBSD::HasNativeLLVMSupport() const { 2512 return true; 2513 } 2514 2515 bool FreeBSD::isPIEDefault() const { 2516 return getSanitizerArgs().requiresPIE(); 2517 } 2518 2519 /// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly. 2520 2521 NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 2522 : Generic_ELF(D, Triple, Args) { 2523 2524 if (getDriver().UseStdLib) { 2525 // When targeting a 32-bit platform, try the special directory used on 2526 // 64-bit hosts, and only fall back to the main library directory if that 2527 // doesn't work. 2528 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure 2529 // what all logic is needed to emulate the '=' prefix here. 2530 switch (Triple.getArch()) { 2531 case llvm::Triple::x86: 2532 getFilePaths().push_back("=/usr/lib/i386"); 2533 break; 2534 case llvm::Triple::arm: 2535 case llvm::Triple::armeb: 2536 case llvm::Triple::thumb: 2537 case llvm::Triple::thumbeb: 2538 switch (Triple.getEnvironment()) { 2539 case llvm::Triple::EABI: 2540 case llvm::Triple::GNUEABI: 2541 getFilePaths().push_back("=/usr/lib/eabi"); 2542 break; 2543 case llvm::Triple::EABIHF: 2544 case llvm::Triple::GNUEABIHF: 2545 getFilePaths().push_back("=/usr/lib/eabihf"); 2546 break; 2547 default: 2548 getFilePaths().push_back("=/usr/lib/oabi"); 2549 break; 2550 } 2551 break; 2552 case llvm::Triple::mips64: 2553 case llvm::Triple::mips64el: 2554 if (tools::mips::hasMipsAbiArg(Args, "o32")) 2555 getFilePaths().push_back("=/usr/lib/o32"); 2556 else if (tools::mips::hasMipsAbiArg(Args, "64")) 2557 getFilePaths().push_back("=/usr/lib/64"); 2558 break; 2559 case llvm::Triple::ppc: 2560 getFilePaths().push_back("=/usr/lib/powerpc"); 2561 break; 2562 case llvm::Triple::sparc: 2563 getFilePaths().push_back("=/usr/lib/sparc"); 2564 break; 2565 default: 2566 break; 2567 } 2568 2569 getFilePaths().push_back("=/usr/lib"); 2570 } 2571 } 2572 2573 Tool *NetBSD::buildAssembler() const { 2574 return new tools::netbsd::Assemble(*this); 2575 } 2576 2577 Tool *NetBSD::buildLinker() const { 2578 return new tools::netbsd::Link(*this); 2579 } 2580 2581 ToolChain::CXXStdlibType 2582 NetBSD::GetCXXStdlibType(const ArgList &Args) const { 2583 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 2584 StringRef Value = A->getValue(); 2585 if (Value == "libstdc++") 2586 return ToolChain::CST_Libstdcxx; 2587 if (Value == "libc++") 2588 return ToolChain::CST_Libcxx; 2589 2590 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 2591 << A->getAsString(Args); 2592 } 2593 2594 unsigned Major, Minor, Micro; 2595 getTriple().getOSVersion(Major, Minor, Micro); 2596 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 49) || Major == 0) { 2597 switch (getArch()) { 2598 case llvm::Triple::aarch64: 2599 case llvm::Triple::arm: 2600 case llvm::Triple::armeb: 2601 case llvm::Triple::thumb: 2602 case llvm::Triple::thumbeb: 2603 case llvm::Triple::ppc: 2604 case llvm::Triple::ppc64: 2605 case llvm::Triple::ppc64le: 2606 case llvm::Triple::x86: 2607 case llvm::Triple::x86_64: 2608 return ToolChain::CST_Libcxx; 2609 default: 2610 break; 2611 } 2612 } 2613 return ToolChain::CST_Libstdcxx; 2614 } 2615 2616 void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 2617 ArgStringList &CC1Args) const { 2618 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 2619 DriverArgs.hasArg(options::OPT_nostdincxx)) 2620 return; 2621 2622 switch (GetCXXStdlibType(DriverArgs)) { 2623 case ToolChain::CST_Libcxx: 2624 addSystemInclude(DriverArgs, CC1Args, 2625 getDriver().SysRoot + "/usr/include/c++/"); 2626 break; 2627 case ToolChain::CST_Libstdcxx: 2628 addSystemInclude(DriverArgs, CC1Args, 2629 getDriver().SysRoot + "/usr/include/g++"); 2630 addSystemInclude(DriverArgs, CC1Args, 2631 getDriver().SysRoot + "/usr/include/g++/backward"); 2632 break; 2633 } 2634 } 2635 2636 /// Minix - Minix tool chain which can call as(1) and ld(1) directly. 2637 2638 Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 2639 : Generic_ELF(D, Triple, Args) { 2640 2641 if (getDriver().UseStdLib) { 2642 // When targeting a 32-bit platform, try the special directory used on 2643 // 64-bit hosts, and only fall back to the main library directory if that 2644 // doesn't work. 2645 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure 2646 // what all logic is needed to emulate the '=' prefix here. 2647 switch (Triple.getArch()) { 2648 case llvm::Triple::x86: 2649 getFilePaths().push_back("=/usr/lib/i386"); 2650 break; 2651 #if 0 // LSC: Not yet useful for MINIX, but kept to simplify comparison with NetBSD 2652 case llvm::Triple::arm: 2653 case llvm::Triple::armeb: 2654 case llvm::Triple::thumb: 2655 case llvm::Triple::thumbeb: 2656 switch (Triple.getEnvironment()) { 2657 case llvm::Triple::EABI: 2658 case llvm::Triple::GNUEABI: 2659 getFilePaths().push_back("=/usr/lib/eabi"); 2660 break; 2661 case llvm::Triple::EABIHF: 2662 case llvm::Triple::GNUEABIHF: 2663 getFilePaths().push_back("=/usr/lib/eabihf"); 2664 break; 2665 default: 2666 getFilePaths().push_back("=/usr/lib/oabi"); 2667 break; 2668 } 2669 break; 2670 case llvm::Triple::mips64: 2671 case llvm::Triple::mips64el: 2672 if (tools::mips::hasMipsAbiArg(Args, "o32")) 2673 getFilePaths().push_back("=/usr/lib/o32"); 2674 else if (tools::mips::hasMipsAbiArg(Args, "64")) 2675 getFilePaths().push_back("=/usr/lib/64"); 2676 break; 2677 case llvm::Triple::ppc: 2678 getFilePaths().push_back("=/usr/lib/powerpc"); 2679 break; 2680 case llvm::Triple::sparc: 2681 getFilePaths().push_back("=/usr/lib/sparc"); 2682 break; 2683 #endif /* 0 */ 2684 default: 2685 break; 2686 } 2687 2688 getFilePaths().push_back("=/usr/lib"); 2689 } 2690 } 2691 2692 Tool *Minix::buildAssembler() const { 2693 return new tools::minix::Assemble(*this); 2694 } 2695 2696 Tool *Minix::buildLinker() const { 2697 return new tools::minix::Link(*this); 2698 } 2699 2700 ToolChain::CXXStdlibType 2701 Minix::GetCXXStdlibType(const ArgList &Args) const { 2702 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 2703 StringRef Value = A->getValue(); 2704 if (Value == "libstdc++") 2705 return ToolChain::CST_Libstdcxx; 2706 if (Value == "libc++") 2707 return ToolChain::CST_Libcxx; 2708 2709 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 2710 << A->getAsString(Args); 2711 } 2712 2713 #if 0 /* LSC: We only us libcxx by default */ 2714 unsigned Major, Minor, Micro; 2715 getTriple().getOSVersion(Major, Minor, Micro); 2716 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 49) || Major == 0) { 2717 switch (getArch()) { 2718 case llvm::Triple::aarch64: 2719 case llvm::Triple::arm: 2720 case llvm::Triple::armeb: 2721 case llvm::Triple::thumb: 2722 case llvm::Triple::thumbeb: 2723 case llvm::Triple::ppc: 2724 case llvm::Triple::ppc64: 2725 case llvm::Triple::ppc64le: 2726 case llvm::Triple::x86: 2727 case llvm::Triple::x86_64: 2728 return ToolChain::CST_Libcxx; 2729 default: 2730 break; 2731 } 2732 } 2733 return ToolChain::CST_Libstdcxx; 2734 #else 2735 return ToolChain::CST_Libcxx; 2736 #endif /* 0 */ 2737 } 2738 2739 void Minix::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 2740 ArgStringList &CC1Args) const { 2741 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 2742 DriverArgs.hasArg(options::OPT_nostdincxx)) 2743 return; 2744 2745 switch (GetCXXStdlibType(DriverArgs)) { 2746 case ToolChain::CST_Libcxx: 2747 addSystemInclude(DriverArgs, CC1Args, 2748 getDriver().SysRoot + "/usr/include/c++/"); 2749 break; 2750 case ToolChain::CST_Libstdcxx: 2751 addSystemInclude(DriverArgs, CC1Args, 2752 getDriver().SysRoot + "/usr/include/g++"); 2753 addSystemInclude(DriverArgs, CC1Args, 2754 getDriver().SysRoot + "/usr/include/g++/backward"); 2755 break; 2756 } 2757 } 2758 2759 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly. 2760 2761 Solaris::Solaris(const Driver &D, const llvm::Triple& Triple, 2762 const ArgList &Args) 2763 : Generic_GCC(D, Triple, Args) { 2764 2765 getProgramPaths().push_back(getDriver().getInstalledDir()); 2766 if (getDriver().getInstalledDir() != getDriver().Dir) 2767 getProgramPaths().push_back(getDriver().Dir); 2768 2769 getFilePaths().push_back(getDriver().Dir + "/../lib"); 2770 getFilePaths().push_back("/usr/lib"); 2771 } 2772 2773 Tool *Solaris::buildAssembler() const { 2774 return new tools::solaris::Assemble(*this); 2775 } 2776 2777 Tool *Solaris::buildLinker() const { 2778 return new tools::solaris::Link(*this); 2779 } 2780 2781 /// Distribution (very bare-bones at the moment). 2782 2783 enum Distro { 2784 ArchLinux, 2785 DebianLenny, 2786 DebianSqueeze, 2787 DebianWheezy, 2788 DebianJessie, 2789 Exherbo, 2790 RHEL4, 2791 RHEL5, 2792 RHEL6, 2793 Fedora, 2794 OpenSUSE, 2795 UbuntuHardy, 2796 UbuntuIntrepid, 2797 UbuntuJaunty, 2798 UbuntuKarmic, 2799 UbuntuLucid, 2800 UbuntuMaverick, 2801 UbuntuNatty, 2802 UbuntuOneiric, 2803 UbuntuPrecise, 2804 UbuntuQuantal, 2805 UbuntuRaring, 2806 UbuntuSaucy, 2807 UbuntuTrusty, 2808 UnknownDistro 2809 }; 2810 2811 static bool IsRedhat(enum Distro Distro) { 2812 return Distro == Fedora || (Distro >= RHEL4 && Distro <= RHEL6); 2813 } 2814 2815 static bool IsOpenSUSE(enum Distro Distro) { 2816 return Distro == OpenSUSE; 2817 } 2818 2819 static bool IsDebian(enum Distro Distro) { 2820 return Distro >= DebianLenny && Distro <= DebianJessie; 2821 } 2822 2823 static bool IsUbuntu(enum Distro Distro) { 2824 return Distro >= UbuntuHardy && Distro <= UbuntuTrusty; 2825 } 2826 2827 static Distro DetectDistro(llvm::Triple::ArchType Arch) { 2828 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File = 2829 llvm::MemoryBuffer::getFile("/etc/lsb-release"); 2830 if (File) { 2831 StringRef Data = File.get()->getBuffer(); 2832 SmallVector<StringRef, 16> Lines; 2833 Data.split(Lines, "\n"); 2834 Distro Version = UnknownDistro; 2835 for (unsigned i = 0, s = Lines.size(); i != s; ++i) 2836 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME=")) 2837 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17)) 2838 .Case("hardy", UbuntuHardy) 2839 .Case("intrepid", UbuntuIntrepid) 2840 .Case("jaunty", UbuntuJaunty) 2841 .Case("karmic", UbuntuKarmic) 2842 .Case("lucid", UbuntuLucid) 2843 .Case("maverick", UbuntuMaverick) 2844 .Case("natty", UbuntuNatty) 2845 .Case("oneiric", UbuntuOneiric) 2846 .Case("precise", UbuntuPrecise) 2847 .Case("quantal", UbuntuQuantal) 2848 .Case("raring", UbuntuRaring) 2849 .Case("saucy", UbuntuSaucy) 2850 .Case("trusty", UbuntuTrusty) 2851 .Default(UnknownDistro); 2852 return Version; 2853 } 2854 2855 File = llvm::MemoryBuffer::getFile("/etc/redhat-release"); 2856 if (File) { 2857 StringRef Data = File.get()->getBuffer(); 2858 if (Data.startswith("Fedora release")) 2859 return Fedora; 2860 if (Data.startswith("Red Hat Enterprise Linux") || 2861 Data.startswith("CentOS")) { 2862 if (Data.find("release 6") != StringRef::npos) 2863 return RHEL6; 2864 else if (Data.find("release 5") != StringRef::npos) 2865 return RHEL5; 2866 else if (Data.find("release 4") != StringRef::npos) 2867 return RHEL4; 2868 } 2869 return UnknownDistro; 2870 } 2871 2872 File = llvm::MemoryBuffer::getFile("/etc/debian_version"); 2873 if (File) { 2874 StringRef Data = File.get()->getBuffer(); 2875 if (Data[0] == '5') 2876 return DebianLenny; 2877 else if (Data.startswith("squeeze/sid") || Data[0] == '6') 2878 return DebianSqueeze; 2879 else if (Data.startswith("wheezy/sid") || Data[0] == '7') 2880 return DebianWheezy; 2881 else if (Data.startswith("jessie/sid") || Data[0] == '8') 2882 return DebianJessie; 2883 return UnknownDistro; 2884 } 2885 2886 if (llvm::sys::fs::exists("/etc/SuSE-release")) 2887 return OpenSUSE; 2888 2889 if (llvm::sys::fs::exists("/etc/exherbo-release")) 2890 return Exherbo; 2891 2892 if (llvm::sys::fs::exists("/etc/arch-release")) 2893 return ArchLinux; 2894 2895 return UnknownDistro; 2896 } 2897 2898 /// \brief Get our best guess at the multiarch triple for a target. 2899 /// 2900 /// Debian-based systems are starting to use a multiarch setup where they use 2901 /// a target-triple directory in the library and header search paths. 2902 /// Unfortunately, this triple does not align with the vanilla target triple, 2903 /// so we provide a rough mapping here. 2904 static std::string getMultiarchTriple(const llvm::Triple &TargetTriple, 2905 StringRef SysRoot) { 2906 // For most architectures, just use whatever we have rather than trying to be 2907 // clever. 2908 switch (TargetTriple.getArch()) { 2909 default: 2910 return TargetTriple.str(); 2911 2912 // We use the existence of '/lib/<triple>' as a directory to detect some 2913 // common linux triples that don't quite match the Clang triple for both 2914 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these 2915 // regardless of what the actual target triple is. 2916 case llvm::Triple::arm: 2917 case llvm::Triple::thumb: 2918 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) { 2919 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf")) 2920 return "arm-linux-gnueabihf"; 2921 } else { 2922 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi")) 2923 return "arm-linux-gnueabi"; 2924 } 2925 return TargetTriple.str(); 2926 case llvm::Triple::armeb: 2927 case llvm::Triple::thumbeb: 2928 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) { 2929 if (llvm::sys::fs::exists(SysRoot + "/lib/armeb-linux-gnueabihf")) 2930 return "armeb-linux-gnueabihf"; 2931 } else { 2932 if (llvm::sys::fs::exists(SysRoot + "/lib/armeb-linux-gnueabi")) 2933 return "armeb-linux-gnueabi"; 2934 } 2935 return TargetTriple.str(); 2936 case llvm::Triple::x86: 2937 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu")) 2938 return "i386-linux-gnu"; 2939 return TargetTriple.str(); 2940 case llvm::Triple::x86_64: 2941 // We don't want this for x32, otherwise it will match x86_64 libs 2942 if (TargetTriple.getEnvironment() != llvm::Triple::GNUX32 && 2943 llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu")) 2944 return "x86_64-linux-gnu"; 2945 return TargetTriple.str(); 2946 case llvm::Triple::aarch64: 2947 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu")) 2948 return "aarch64-linux-gnu"; 2949 return TargetTriple.str(); 2950 case llvm::Triple::aarch64_be: 2951 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64_be-linux-gnu")) 2952 return "aarch64_be-linux-gnu"; 2953 return TargetTriple.str(); 2954 case llvm::Triple::mips: 2955 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu")) 2956 return "mips-linux-gnu"; 2957 return TargetTriple.str(); 2958 case llvm::Triple::mipsel: 2959 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu")) 2960 return "mipsel-linux-gnu"; 2961 return TargetTriple.str(); 2962 case llvm::Triple::mips64: 2963 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64-linux-gnu")) 2964 return "mips64-linux-gnu"; 2965 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64-linux-gnuabi64")) 2966 return "mips64-linux-gnuabi64"; 2967 return TargetTriple.str(); 2968 case llvm::Triple::mips64el: 2969 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64el-linux-gnu")) 2970 return "mips64el-linux-gnu"; 2971 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64el-linux-gnuabi64")) 2972 return "mips64el-linux-gnuabi64"; 2973 return TargetTriple.str(); 2974 case llvm::Triple::ppc: 2975 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe")) 2976 return "powerpc-linux-gnuspe"; 2977 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu")) 2978 return "powerpc-linux-gnu"; 2979 return TargetTriple.str(); 2980 case llvm::Triple::ppc64: 2981 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu")) 2982 return "powerpc64-linux-gnu"; 2983 case llvm::Triple::ppc64le: 2984 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64le-linux-gnu")) 2985 return "powerpc64le-linux-gnu"; 2986 return TargetTriple.str(); 2987 } 2988 } 2989 2990 static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) { 2991 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str()); 2992 } 2993 2994 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { 2995 if (isMipsArch(Triple.getArch())) { 2996 // lib32 directory has a special meaning on MIPS targets. 2997 // It contains N32 ABI binaries. Use this folder if produce 2998 // code for N32 ABI only. 2999 if (tools::mips::hasMipsAbiArg(Args, "n32")) 3000 return "lib32"; 3001 return Triple.isArch32Bit() ? "lib" : "lib64"; 3002 } 3003 3004 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and 3005 // using that variant while targeting other architectures causes problems 3006 // because the libraries are laid out in shared system roots that can't cope 3007 // with a 'lib32' library search path being considered. So we only enable 3008 // them when we know we may need it. 3009 // 3010 // FIXME: This is a bit of a hack. We should really unify this code for 3011 // reasoning about oslibdir spellings with the lib dir spellings in the 3012 // GCCInstallationDetector, but that is a more significant refactoring. 3013 if (Triple.getArch() == llvm::Triple::x86 || 3014 Triple.getArch() == llvm::Triple::ppc) 3015 return "lib32"; 3016 3017 if (Triple.getArch() == llvm::Triple::x86_64 && 3018 Triple.getEnvironment() == llvm::Triple::GNUX32) 3019 return "libx32"; 3020 3021 return Triple.isArch32Bit() ? "lib" : "lib64"; 3022 } 3023 3024 Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 3025 : Generic_ELF(D, Triple, Args) { 3026 GCCInstallation.init(D, Triple, Args); 3027 Multilibs = GCCInstallation.getMultilibs(); 3028 llvm::Triple::ArchType Arch = Triple.getArch(); 3029 std::string SysRoot = computeSysRoot(); 3030 3031 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at 3032 // least) put various tools in a triple-prefixed directory off of the parent 3033 // of the GCC installation. We use the GCC triple here to ensure that we end 3034 // up with tools that support the same amount of cross compiling as the 3035 // detected GCC installation. For example, if we find a GCC installation 3036 // targeting x86_64, but it is a bi-arch GCC installation, it can also be 3037 // used to target i386. 3038 // FIXME: This seems unlikely to be Linux-specific. 3039 ToolChain::path_list &PPaths = getProgramPaths(); 3040 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" + 3041 GCCInstallation.getTriple().str() + "/bin").str()); 3042 3043 Linker = GetLinkerPath(); 3044 3045 Distro Distro = DetectDistro(Arch); 3046 3047 if (IsOpenSUSE(Distro) || IsUbuntu(Distro)) { 3048 ExtraOpts.push_back("-z"); 3049 ExtraOpts.push_back("relro"); 3050 } 3051 3052 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) 3053 ExtraOpts.push_back("-X"); 3054 3055 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android; 3056 const bool IsMips = isMipsArch(Arch); 3057 3058 if (IsMips && !SysRoot.empty()) 3059 ExtraOpts.push_back("--sysroot=" + SysRoot); 3060 3061 // Do not use 'gnu' hash style for Mips targets because .gnu.hash 3062 // and the MIPS ABI require .dynsym to be sorted in different ways. 3063 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS 3064 // ABI requires a mapping between the GOT and the symbol table. 3065 // Android loader does not support .gnu.hash. 3066 if (!IsMips && !IsAndroid) { 3067 if (IsRedhat(Distro) || IsOpenSUSE(Distro) || 3068 (IsUbuntu(Distro) && Distro >= UbuntuMaverick)) 3069 ExtraOpts.push_back("--hash-style=gnu"); 3070 3071 if (IsDebian(Distro) || IsOpenSUSE(Distro) || Distro == UbuntuLucid || 3072 Distro == UbuntuJaunty || Distro == UbuntuKarmic) 3073 ExtraOpts.push_back("--hash-style=both"); 3074 } 3075 3076 if (IsRedhat(Distro)) 3077 ExtraOpts.push_back("--no-add-needed"); 3078 3079 if (Distro == DebianSqueeze || Distro == DebianWheezy || 3080 Distro == DebianJessie || IsOpenSUSE(Distro) || 3081 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) || 3082 (IsUbuntu(Distro) && Distro >= UbuntuKarmic)) 3083 ExtraOpts.push_back("--build-id"); 3084 3085 if (IsOpenSUSE(Distro)) 3086 ExtraOpts.push_back("--enable-new-dtags"); 3087 3088 // The selection of paths to try here is designed to match the patterns which 3089 // the GCC driver itself uses, as this is part of the GCC-compatible driver. 3090 // This was determined by running GCC in a fake filesystem, creating all 3091 // possible permutations of these directories, and seeing which ones it added 3092 // to the link paths. 3093 path_list &Paths = getFilePaths(); 3094 3095 const std::string OSLibDir = getOSLibDir(Triple, Args); 3096 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot); 3097 3098 // Add the multilib suffixed paths where they are available. 3099 if (GCCInstallation.isValid()) { 3100 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 3101 const std::string &LibPath = GCCInstallation.getParentLibPath(); 3102 const Multilib &Multilib = GCCInstallation.getMultilib(); 3103 3104 // Sourcery CodeBench MIPS toolchain holds some libraries under 3105 // a biarch-like suffix of the GCC installation. 3106 addPathIfExists((GCCInstallation.getInstallPath() + 3107 Multilib.gccSuffix()), 3108 Paths); 3109 3110 // GCC cross compiling toolchains will install target libraries which ship 3111 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as 3112 // any part of the GCC installation in 3113 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat 3114 // debatable, but is the reality today. We need to search this tree even 3115 // when we have a sysroot somewhere else. It is the responsibility of 3116 // whomever is doing the cross build targeting a sysroot using a GCC 3117 // installation that is *not* within the system root to ensure two things: 3118 // 3119 // 1) Any DSOs that are linked in from this tree or from the install path 3120 // above must be present on the system root and found via an 3121 // appropriate rpath. 3122 // 2) There must not be libraries installed into 3123 // <prefix>/<triple>/<libdir> unless they should be preferred over 3124 // those within the system root. 3125 // 3126 // Note that this matches the GCC behavior. See the below comment for where 3127 // Clang diverges from GCC's behavior. 3128 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + OSLibDir + 3129 Multilib.osSuffix(), 3130 Paths); 3131 3132 // If the GCC installation we found is inside of the sysroot, we want to 3133 // prefer libraries installed in the parent prefix of the GCC installation. 3134 // It is important to *not* use these paths when the GCC installation is 3135 // outside of the system root as that can pick up unintended libraries. 3136 // This usually happens when there is an external cross compiler on the 3137 // host system, and a more minimal sysroot available that is the target of 3138 // the cross. Note that GCC does include some of these directories in some 3139 // configurations but this seems somewhere between questionable and simply 3140 // a bug. 3141 if (StringRef(LibPath).startswith(SysRoot)) { 3142 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths); 3143 addPathIfExists(LibPath + "/../" + OSLibDir, Paths); 3144 } 3145 } 3146 3147 // Similar to the logic for GCC above, if we currently running Clang inside 3148 // of the requested system root, add its parent library paths to 3149 // those searched. 3150 // FIXME: It's not clear whether we should use the driver's installed 3151 // directory ('Dir' below) or the ResourceDir. 3152 if (StringRef(D.Dir).startswith(SysRoot)) { 3153 addPathIfExists(D.Dir + "/../lib/" + MultiarchTriple, Paths); 3154 addPathIfExists(D.Dir + "/../" + OSLibDir, Paths); 3155 } 3156 3157 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths); 3158 addPathIfExists(SysRoot + "/lib/../" + OSLibDir, Paths); 3159 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths); 3160 addPathIfExists(SysRoot + "/usr/lib/../" + OSLibDir, Paths); 3161 3162 // Try walking via the GCC triple path in case of biarch or multiarch GCC 3163 // installations with strange symlinks. 3164 if (GCCInstallation.isValid()) { 3165 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() + 3166 "/../../" + OSLibDir, Paths); 3167 3168 // Add the 'other' biarch variant path 3169 Multilib BiarchSibling; 3170 if (GCCInstallation.getBiarchSibling(BiarchSibling)) { 3171 addPathIfExists(GCCInstallation.getInstallPath() + 3172 BiarchSibling.gccSuffix(), Paths); 3173 } 3174 3175 // See comments above on the multilib variant for details of why this is 3176 // included even from outside the sysroot. 3177 const std::string &LibPath = GCCInstallation.getParentLibPath(); 3178 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 3179 const Multilib &Multilib = GCCInstallation.getMultilib(); 3180 addPathIfExists(LibPath + "/../" + GCCTriple.str() + 3181 "/lib" + Multilib.osSuffix(), Paths); 3182 3183 // See comments above on the multilib variant for details of why this is 3184 // only included from within the sysroot. 3185 if (StringRef(LibPath).startswith(SysRoot)) 3186 addPathIfExists(LibPath, Paths); 3187 } 3188 3189 // Similar to the logic for GCC above, if we are currently running Clang 3190 // inside of the requested system root, add its parent library path to those 3191 // searched. 3192 // FIXME: It's not clear whether we should use the driver's installed 3193 // directory ('Dir' below) or the ResourceDir. 3194 if (StringRef(D.Dir).startswith(SysRoot)) 3195 addPathIfExists(D.Dir + "/../lib", Paths); 3196 3197 addPathIfExists(SysRoot + "/lib", Paths); 3198 addPathIfExists(SysRoot + "/usr/lib", Paths); 3199 } 3200 3201 bool Linux::HasNativeLLVMSupport() const { 3202 return true; 3203 } 3204 3205 Tool *Linux::buildLinker() const { 3206 return new tools::gnutools::Link(*this); 3207 } 3208 3209 Tool *Linux::buildAssembler() const { 3210 return new tools::gnutools::Assemble(*this); 3211 } 3212 3213 std::string Linux::computeSysRoot() const { 3214 if (!getDriver().SysRoot.empty()) 3215 return getDriver().SysRoot; 3216 3217 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch())) 3218 return std::string(); 3219 3220 // Standalone MIPS toolchains use different names for sysroot folder 3221 // and put it into different places. Here we try to check some known 3222 // variants. 3223 3224 const StringRef InstallDir = GCCInstallation.getInstallPath(); 3225 const StringRef TripleStr = GCCInstallation.getTriple().str(); 3226 const Multilib &Multilib = GCCInstallation.getMultilib(); 3227 3228 std::string Path = (InstallDir + "/../../../../" + TripleStr + "/libc" + 3229 Multilib.osSuffix()).str(); 3230 3231 if (llvm::sys::fs::exists(Path)) 3232 return Path; 3233 3234 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); 3235 3236 if (llvm::sys::fs::exists(Path)) 3237 return Path; 3238 3239 return std::string(); 3240 } 3241 3242 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 3243 ArgStringList &CC1Args) const { 3244 const Driver &D = getDriver(); 3245 std::string SysRoot = computeSysRoot(); 3246 3247 if (DriverArgs.hasArg(options::OPT_nostdinc)) 3248 return; 3249 3250 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 3251 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 3252 3253 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 3254 SmallString<128> P(D.ResourceDir); 3255 llvm::sys::path::append(P, "include"); 3256 addSystemInclude(DriverArgs, CC1Args, P.str()); 3257 } 3258 3259 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 3260 return; 3261 3262 // Check for configure-time C include directories. 3263 StringRef CIncludeDirs(C_INCLUDE_DIRS); 3264 if (CIncludeDirs != "") { 3265 SmallVector<StringRef, 5> dirs; 3266 CIncludeDirs.split(dirs, ":"); 3267 for (StringRef dir : dirs) { 3268 StringRef Prefix = 3269 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : ""; 3270 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 3271 } 3272 return; 3273 } 3274 3275 // Lacking those, try to detect the correct set of system includes for the 3276 // target triple. 3277 3278 // Add include directories specific to the selected multilib set and multilib. 3279 if (GCCInstallation.isValid()) { 3280 auto Callback = Multilibs.includeDirsCallback(); 3281 if (Callback) { 3282 const auto IncludePaths = Callback(GCCInstallation.getInstallPath(), 3283 GCCInstallation.getTriple().str(), 3284 GCCInstallation.getMultilib()); 3285 for (const auto &Path : IncludePaths) 3286 addExternCSystemIncludeIfExists(DriverArgs, CC1Args, Path); 3287 } 3288 } 3289 3290 // Implement generic Debian multiarch support. 3291 const StringRef X86_64MultiarchIncludeDirs[] = { 3292 "/usr/include/x86_64-linux-gnu", 3293 3294 // FIXME: These are older forms of multiarch. It's not clear that they're 3295 // in use in any released version of Debian, so we should consider 3296 // removing them. 3297 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64" 3298 }; 3299 const StringRef X86MultiarchIncludeDirs[] = { 3300 "/usr/include/i386-linux-gnu", 3301 3302 // FIXME: These are older forms of multiarch. It's not clear that they're 3303 // in use in any released version of Debian, so we should consider 3304 // removing them. 3305 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu", 3306 "/usr/include/i486-linux-gnu" 3307 }; 3308 const StringRef AArch64MultiarchIncludeDirs[] = { 3309 "/usr/include/aarch64-linux-gnu" 3310 }; 3311 const StringRef ARMMultiarchIncludeDirs[] = { 3312 "/usr/include/arm-linux-gnueabi" 3313 }; 3314 const StringRef ARMHFMultiarchIncludeDirs[] = { 3315 "/usr/include/arm-linux-gnueabihf" 3316 }; 3317 const StringRef MIPSMultiarchIncludeDirs[] = { 3318 "/usr/include/mips-linux-gnu" 3319 }; 3320 const StringRef MIPSELMultiarchIncludeDirs[] = { 3321 "/usr/include/mipsel-linux-gnu" 3322 }; 3323 const StringRef MIPS64MultiarchIncludeDirs[] = { 3324 "/usr/include/mips64-linux-gnu", 3325 "/usr/include/mips64-linux-gnuabi64" 3326 }; 3327 const StringRef MIPS64ELMultiarchIncludeDirs[] = { 3328 "/usr/include/mips64el-linux-gnu", 3329 "/usr/include/mips64el-linux-gnuabi64" 3330 }; 3331 const StringRef PPCMultiarchIncludeDirs[] = { 3332 "/usr/include/powerpc-linux-gnu" 3333 }; 3334 const StringRef PPC64MultiarchIncludeDirs[] = { 3335 "/usr/include/powerpc64-linux-gnu" 3336 }; 3337 const StringRef PPC64LEMultiarchIncludeDirs[] = { 3338 "/usr/include/powerpc64le-linux-gnu" 3339 }; 3340 ArrayRef<StringRef> MultiarchIncludeDirs; 3341 if (getTriple().getArch() == llvm::Triple::x86_64) { 3342 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs; 3343 } else if (getTriple().getArch() == llvm::Triple::x86) { 3344 MultiarchIncludeDirs = X86MultiarchIncludeDirs; 3345 } else if (getTriple().getArch() == llvm::Triple::aarch64 || 3346 getTriple().getArch() == llvm::Triple::aarch64_be) { 3347 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs; 3348 } else if (getTriple().getArch() == llvm::Triple::arm) { 3349 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 3350 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs; 3351 else 3352 MultiarchIncludeDirs = ARMMultiarchIncludeDirs; 3353 } else if (getTriple().getArch() == llvm::Triple::mips) { 3354 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs; 3355 } else if (getTriple().getArch() == llvm::Triple::mipsel) { 3356 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs; 3357 } else if (getTriple().getArch() == llvm::Triple::mips64) { 3358 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs; 3359 } else if (getTriple().getArch() == llvm::Triple::mips64el) { 3360 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs; 3361 } else if (getTriple().getArch() == llvm::Triple::ppc) { 3362 MultiarchIncludeDirs = PPCMultiarchIncludeDirs; 3363 } else if (getTriple().getArch() == llvm::Triple::ppc64) { 3364 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs; 3365 } else if (getTriple().getArch() == llvm::Triple::ppc64le) { 3366 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs; 3367 } 3368 for (StringRef Dir : MultiarchIncludeDirs) { 3369 if (llvm::sys::fs::exists(SysRoot + Dir)) { 3370 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir); 3371 break; 3372 } 3373 } 3374 3375 if (getTriple().getOS() == llvm::Triple::RTEMS) 3376 return; 3377 3378 // Add an include of '/include' directly. This isn't provided by default by 3379 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 3380 // add even when Clang is acting as-if it were a system compiler. 3381 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 3382 3383 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 3384 } 3385 3386 /// \brief Helper to add the variant paths of a libstdc++ installation. 3387 /*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix, 3388 StringRef GCCTriple, 3389 StringRef GCCMultiarchTriple, 3390 StringRef TargetMultiarchTriple, 3391 Twine IncludeSuffix, 3392 const ArgList &DriverArgs, 3393 ArgStringList &CC1Args) { 3394 if (!llvm::sys::fs::exists(Base + Suffix)) 3395 return false; 3396 3397 addSystemInclude(DriverArgs, CC1Args, Base + Suffix); 3398 3399 // The vanilla GCC layout of libstdc++ headers uses a triple subdirectory. If 3400 // that path exists or we have neither a GCC nor target multiarch triple, use 3401 // this vanilla search path. 3402 if ((GCCMultiarchTriple.empty() && TargetMultiarchTriple.empty()) || 3403 llvm::sys::fs::exists(Base + Suffix + "/" + GCCTriple + IncludeSuffix)) { 3404 addSystemInclude(DriverArgs, CC1Args, 3405 Base + Suffix + "/" + GCCTriple + IncludeSuffix); 3406 } else { 3407 // Otherwise try to use multiarch naming schemes which have normalized the 3408 // triples and put the triple before the suffix. 3409 // 3410 // GCC surprisingly uses *both* the GCC triple with a multilib suffix and 3411 // the target triple, so we support that here. 3412 addSystemInclude(DriverArgs, CC1Args, 3413 Base + "/" + GCCMultiarchTriple + Suffix + IncludeSuffix); 3414 addSystemInclude(DriverArgs, CC1Args, 3415 Base + "/" + TargetMultiarchTriple + Suffix); 3416 } 3417 3418 addSystemInclude(DriverArgs, CC1Args, Base + Suffix + "/backward"); 3419 return true; 3420 } 3421 3422 void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 3423 ArgStringList &CC1Args) const { 3424 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 3425 DriverArgs.hasArg(options::OPT_nostdincxx)) 3426 return; 3427 3428 // Check if libc++ has been enabled and provide its include paths if so. 3429 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) { 3430 const std::string LibCXXIncludePathCandidates[] = { 3431 // The primary location is within the Clang installation. 3432 // FIXME: We shouldn't hard code 'v1' here to make Clang future proof to 3433 // newer ABI versions. 3434 getDriver().Dir + "/../include/c++/v1", 3435 3436 // We also check the system as for a long time this is the only place Clang looked. 3437 // FIXME: We should really remove this. It doesn't make any sense. 3438 getDriver().SysRoot + "/usr/include/c++/v1" 3439 }; 3440 for (const auto &IncludePath : LibCXXIncludePathCandidates) { 3441 if (!llvm::sys::fs::exists(IncludePath)) 3442 continue; 3443 // Add the first candidate that exists. 3444 addSystemInclude(DriverArgs, CC1Args, IncludePath); 3445 break; 3446 } 3447 return; 3448 } 3449 3450 // We need a detected GCC installation on Linux to provide libstdc++'s 3451 // headers. We handled the libc++ case above. 3452 if (!GCCInstallation.isValid()) 3453 return; 3454 3455 // By default, look for the C++ headers in an include directory adjacent to 3456 // the lib directory of the GCC installation. Note that this is expect to be 3457 // equivalent to '/usr/include/c++/X.Y' in almost all cases. 3458 StringRef LibDir = GCCInstallation.getParentLibPath(); 3459 StringRef InstallDir = GCCInstallation.getInstallPath(); 3460 StringRef TripleStr = GCCInstallation.getTriple().str(); 3461 const Multilib &Multilib = GCCInstallation.getMultilib(); 3462 const std::string GCCMultiarchTriple = 3463 getMultiarchTriple(GCCInstallation.getTriple(), getDriver().SysRoot); 3464 const std::string TargetMultiarchTriple = 3465 getMultiarchTriple(getTriple(), getDriver().SysRoot); 3466 const GCCVersion &Version = GCCInstallation.getVersion(); 3467 3468 // The primary search for libstdc++ supports multiarch variants. 3469 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include", 3470 "/c++/" + Version.Text, TripleStr, GCCMultiarchTriple, 3471 TargetMultiarchTriple, 3472 Multilib.includeSuffix(), DriverArgs, CC1Args)) 3473 return; 3474 3475 // Otherwise, fall back on a bunch of options which don't use multiarch 3476 // layouts for simplicity. 3477 const std::string LibStdCXXIncludePathCandidates[] = { 3478 // Gentoo is weird and places its headers inside the GCC install, so if the 3479 // first attempt to find the headers fails, try these patterns. 3480 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." + 3481 Version.MinorStr, 3482 InstallDir.str() + "/include/g++-v" + Version.MajorStr, 3483 // Android standalone toolchain has C++ headers in yet another place. 3484 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, 3485 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, 3486 // without a subdirectory corresponding to the gcc version. 3487 LibDir.str() + "/../include/c++", 3488 }; 3489 3490 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 3491 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr, 3492 /*GCCMultiarchTriple*/ "", 3493 /*TargetMultiarchTriple*/ "", 3494 Multilib.includeSuffix(), DriverArgs, CC1Args)) 3495 break; 3496 } 3497 } 3498 3499 bool Linux::isPIEDefault() const { 3500 return getSanitizerArgs().requiresPIE(); 3501 } 3502 3503 /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly. 3504 3505 DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args) 3506 : Generic_ELF(D, Triple, Args) { 3507 3508 // Path mangling to find libexec 3509 getProgramPaths().push_back(getDriver().getInstalledDir()); 3510 if (getDriver().getInstalledDir() != getDriver().Dir) 3511 getProgramPaths().push_back(getDriver().Dir); 3512 3513 getFilePaths().push_back(getDriver().Dir + "/../lib"); 3514 getFilePaths().push_back("/usr/lib"); 3515 if (llvm::sys::fs::exists("/usr/lib/gcc47")) 3516 getFilePaths().push_back("/usr/lib/gcc47"); 3517 else 3518 getFilePaths().push_back("/usr/lib/gcc44"); 3519 } 3520 3521 Tool *DragonFly::buildAssembler() const { 3522 return new tools::dragonfly::Assemble(*this); 3523 } 3524 3525 Tool *DragonFly::buildLinker() const { 3526 return new tools::dragonfly::Link(*this); 3527 } 3528 3529 3530 /// XCore tool chain 3531 XCore::XCore(const Driver &D, const llvm::Triple &Triple, 3532 const ArgList &Args) : ToolChain(D, Triple, Args) { 3533 // ProgramPaths are found via 'PATH' environment variable. 3534 } 3535 3536 Tool *XCore::buildAssembler() const { 3537 return new tools::XCore::Assemble(*this); 3538 } 3539 3540 Tool *XCore::buildLinker() const { 3541 return new tools::XCore::Link(*this); 3542 } 3543 3544 bool XCore::isPICDefault() const { 3545 return false; 3546 } 3547 3548 bool XCore::isPIEDefault() const { 3549 return false; 3550 } 3551 3552 bool XCore::isPICDefaultForced() const { 3553 return false; 3554 } 3555 3556 bool XCore::SupportsProfiling() const { 3557 return false; 3558 } 3559 3560 bool XCore::hasBlocksRuntime() const { 3561 return false; 3562 } 3563 3564 void XCore::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 3565 ArgStringList &CC1Args) const { 3566 if (DriverArgs.hasArg(options::OPT_nostdinc) || 3567 DriverArgs.hasArg(options::OPT_nostdlibinc)) 3568 return; 3569 if (const char *cl_include_dir = getenv("XCC_C_INCLUDE_PATH")) { 3570 SmallVector<StringRef, 4> Dirs; 3571 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'}; 3572 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr)); 3573 ArrayRef<StringRef> DirVec(Dirs); 3574 addSystemIncludes(DriverArgs, CC1Args, DirVec); 3575 } 3576 } 3577 3578 void XCore::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, 3579 llvm::opt::ArgStringList &CC1Args) const { 3580 CC1Args.push_back("-nostdsysteminc"); 3581 } 3582 3583 void XCore::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 3584 ArgStringList &CC1Args) const { 3585 if (DriverArgs.hasArg(options::OPT_nostdinc) || 3586 DriverArgs.hasArg(options::OPT_nostdlibinc) || 3587 DriverArgs.hasArg(options::OPT_nostdincxx)) 3588 return; 3589 if (const char *cl_include_dir = getenv("XCC_CPLUS_INCLUDE_PATH")) { 3590 SmallVector<StringRef, 4> Dirs; 3591 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'}; 3592 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr)); 3593 ArrayRef<StringRef> DirVec(Dirs); 3594 addSystemIncludes(DriverArgs, CC1Args, DirVec); 3595 } 3596 } 3597 3598 void XCore::AddCXXStdlibLibArgs(const ArgList &Args, 3599 ArgStringList &CmdArgs) const { 3600 // We don't output any lib args. This is handled by xcc. 3601 } 3602