ToolChains.cpp revision 218893
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 12#include "clang/Driver/Arg.h" 13#include "clang/Driver/ArgList.h" 14#include "clang/Driver/Compilation.h" 15#include "clang/Driver/Driver.h" 16#include "clang/Driver/DriverDiagnostic.h" 17#include "clang/Driver/HostInfo.h" 18#include "clang/Driver/OptTable.h" 19#include "clang/Driver/Option.h" 20#include "clang/Driver/Options.h" 21#include "clang/Basic/Version.h" 22 23#include "llvm/ADT/SmallString.h" 24#include "llvm/ADT/StringExtras.h" 25#include "llvm/Support/ErrorHandling.h" 26#include "llvm/Support/FileSystem.h" 27#include "llvm/Support/MemoryBuffer.h" 28#include "llvm/Support/raw_ostream.h" 29#include "llvm/Support/Path.h" 30#include "llvm/Support/system_error.h" 31 32#include <cstdlib> // ::getenv 33 34#ifndef CLANG_PREFIX 35#define CLANG_PREFIX 36#endif 37 38using namespace clang::driver; 39using namespace clang::driver::toolchains; 40 41/// Darwin - Darwin tool chain for i386 and x86_64. 42 43Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple) 44 : ToolChain(Host, Triple), TargetInitialized(false) 45{ 46 // Compute the initial Darwin version based on the host. 47 bool HadExtra; 48 std::string OSName = Triple.getOSName(); 49 if (!Driver::GetReleaseVersion(&OSName[6], 50 DarwinVersion[0], DarwinVersion[1], 51 DarwinVersion[2], HadExtra)) 52 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName; 53 54 llvm::raw_string_ostream(MacosxVersionMin) 55 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.' 56 << DarwinVersion[1]; 57} 58 59types::ID Darwin::LookupTypeForExtension(const char *Ext) const { 60 types::ID Ty = types::lookupTypeForExtension(Ext); 61 62 // Darwin always preprocesses assembly files (unless -x is used explicitly). 63 if (Ty == types::TY_PP_Asm) 64 return types::TY_Asm; 65 66 return Ty; 67} 68 69bool Darwin::HasNativeLLVMSupport() const { 70 return true; 71} 72 73// FIXME: Can we tablegen this? 74static const char *GetArmArchForMArch(llvm::StringRef Value) { 75 if (Value == "armv6k") 76 return "armv6"; 77 78 if (Value == "armv5tej") 79 return "armv5"; 80 81 if (Value == "xscale") 82 return "xscale"; 83 84 if (Value == "armv4t") 85 return "armv4t"; 86 87 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" || 88 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" || 89 Value == "armv7m") 90 return "armv7"; 91 92 return 0; 93} 94 95// FIXME: Can we tablegen this? 96static const char *GetArmArchForMCpu(llvm::StringRef Value) { 97 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" || 98 Value == "arm946e-s" || Value == "arm966e-s" || 99 Value == "arm968e-s" || Value == "arm10e" || 100 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" || 101 Value == "arm1026ej-s") 102 return "armv5"; 103 104 if (Value == "xscale") 105 return "xscale"; 106 107 if (Value == "arm1136j-s" || Value == "arm1136jf-s" || 108 Value == "arm1176jz-s" || Value == "arm1176jzf-s") 109 return "armv6"; 110 111 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3") 112 return "armv7"; 113 114 return 0; 115} 116 117llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const { 118 switch (getTriple().getArch()) { 119 default: 120 return getArchName(); 121 122 case llvm::Triple::arm: { 123 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 124 if (const char *Arch = GetArmArchForMArch(A->getValue(Args))) 125 return Arch; 126 127 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 128 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args))) 129 return Arch; 130 131 return "arm"; 132 } 133 } 134} 135 136DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple) 137 : Darwin(Host, Triple) 138{ 139 // We can only work with 4.2.1 currently. 140 GCCVersion[0] = 4; 141 GCCVersion[1] = 2; 142 GCCVersion[2] = 1; 143 144 // Set up the tool chain paths to match gcc. 145 ToolChainDir = "i686-apple-darwin"; 146 ToolChainDir += llvm::utostr(DarwinVersion[0]); 147 ToolChainDir += "/"; 148 ToolChainDir += llvm::utostr(GCCVersion[0]); 149 ToolChainDir += '.'; 150 ToolChainDir += llvm::utostr(GCCVersion[1]); 151 ToolChainDir += '.'; 152 ToolChainDir += llvm::utostr(GCCVersion[2]); 153 154 // Try the next major version if that tool chain dir is invalid. 155 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir; 156 bool Exists; 157 if (llvm::sys::fs::exists(Tmp, Exists) || Exists) { 158 std::string Next = "i686-apple-darwin"; 159 Next += llvm::utostr(DarwinVersion[0] + 1); 160 Next += "/"; 161 Next += llvm::utostr(GCCVersion[0]); 162 Next += '.'; 163 Next += llvm::utostr(GCCVersion[1]); 164 Next += '.'; 165 Next += llvm::utostr(GCCVersion[2]); 166 167 // Use that if it exists, otherwise hope the user isn't linking. 168 // 169 // FIXME: Drop dependency on gcc's tool chain. 170 Tmp = "/usr/lib/gcc/" + Next; 171 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists) 172 ToolChainDir = Next; 173 } 174 175 std::string Path; 176 if (getArchName() == "x86_64") { 177 Path = getDriver().Dir; 178 Path += "/../lib/gcc/"; 179 Path += ToolChainDir; 180 Path += "/x86_64"; 181 getFilePaths().push_back(Path); 182 183 Path = "/usr/lib/gcc/"; 184 Path += ToolChainDir; 185 Path += "/x86_64"; 186 getFilePaths().push_back(Path); 187 } 188 189 Path = getDriver().Dir; 190 Path += "/../lib/gcc/"; 191 Path += ToolChainDir; 192 getFilePaths().push_back(Path); 193 194 Path = "/usr/lib/gcc/"; 195 Path += ToolChainDir; 196 getFilePaths().push_back(Path); 197 198 Path = getDriver().Dir; 199 Path += "/../libexec/gcc/"; 200 Path += ToolChainDir; 201 getProgramPaths().push_back(Path); 202 203 Path = "/usr/libexec/gcc/"; 204 Path += ToolChainDir; 205 getProgramPaths().push_back(Path); 206 207 getProgramPaths().push_back(getDriver().getInstalledDir()); 208 if (getDriver().getInstalledDir() != getDriver().Dir) 209 getProgramPaths().push_back(getDriver().Dir); 210} 211 212Darwin::~Darwin() { 213 // Free tool implementations. 214 for (llvm::DenseMap<unsigned, Tool*>::iterator 215 it = Tools.begin(), ie = Tools.end(); it != ie; ++it) 216 delete it->second; 217} 218 219std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const { 220 llvm::Triple Triple(ComputeLLVMTriple(Args)); 221 222 // If the target isn't initialized (e.g., an unknown Darwin platform, return 223 // the default triple). 224 if (!isTargetInitialized()) 225 return Triple.getTriple(); 226 227 unsigned Version[3]; 228 getTargetVersion(Version); 229 230 // Mangle the target version into the OS triple component. For historical 231 // reasons that make little sense, the version passed here is the "darwin" 232 // version, which drops the 10 and offsets by 4. See inverse code when 233 // setting the OS version preprocessor define. 234 if (!isTargetIPhoneOS()) { 235 Version[0] = Version[1] + 4; 236 Version[1] = Version[2]; 237 Version[2] = 0; 238 } else { 239 // Use the environment to communicate that we are targetting iPhoneOS. 240 Triple.setEnvironmentName("iphoneos"); 241 } 242 243 llvm::SmallString<16> Str; 244 llvm::raw_svector_ostream(Str) << "darwin" << Version[0] 245 << "." << Version[1] << "." << Version[2]; 246 Triple.setOSName(Str.str()); 247 248 return Triple.getTriple(); 249} 250 251Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const { 252 Action::ActionClass Key; 253 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 254 Key = Action::AnalyzeJobClass; 255 else 256 Key = JA.getKind(); 257 258 // FIXME: This doesn't belong here, but ideally we will support static soon 259 // anyway. 260 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) || 261 C.getArgs().hasArg(options::OPT_static) || 262 C.getArgs().hasArg(options::OPT_fapple_kext)); 263 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic; 264 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as, 265 options::OPT_no_integrated_as, 266 IsIADefault); 267 268 Tool *&T = Tools[Key]; 269 if (!T) { 270 switch (Key) { 271 case Action::InputClass: 272 case Action::BindArchClass: 273 assert(0 && "Invalid tool kind."); 274 case Action::PreprocessJobClass: 275 T = new tools::darwin::Preprocess(*this); break; 276 case Action::AnalyzeJobClass: 277 T = new tools::Clang(*this); break; 278 case Action::PrecompileJobClass: 279 case Action::CompileJobClass: 280 T = new tools::darwin::Compile(*this); break; 281 case Action::AssembleJobClass: { 282 if (UseIntegratedAs) 283 T = new tools::ClangAs(*this); 284 else 285 T = new tools::darwin::Assemble(*this); 286 break; 287 } 288 case Action::LinkJobClass: 289 T = new tools::darwin::Link(*this); break; 290 case Action::LipoJobClass: 291 T = new tools::darwin::Lipo(*this); break; 292 case Action::DsymutilJobClass: 293 T = new tools::darwin::Dsymutil(*this); break; 294 } 295 } 296 297 return *T; 298} 299 300void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args, 301 ArgStringList &CmdArgs) const { 302 std::string Tmp; 303 304 // FIXME: Derive these correctly. 305 if (getArchName() == "x86_64") { 306 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir + 307 "/x86_64")); 308 // Intentionally duplicated for (temporary) gcc bug compatibility. 309 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir + 310 "/x86_64")); 311 } 312 313 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir)); 314 315 Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir; 316 bool Exists; 317 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists) 318 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp)); 319 Tmp = getDriver().Dir + "/../lib/gcc"; 320 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists) 321 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp)); 322 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir)); 323 // Intentionally duplicated for (temporary) gcc bug compatibility. 324 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir)); 325 Tmp = getDriver().Dir + "/../lib/" + ToolChainDir; 326 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists) 327 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp)); 328 Tmp = getDriver().Dir + "/../lib"; 329 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists) 330 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp)); 331 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir + 332 "/../../../" + ToolChainDir)); 333 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir + 334 "/../../..")); 335} 336 337void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args, 338 ArgStringList &CmdArgs) const { 339 // Note that this routine is only used for targetting OS X. 340 341 // Derived from libgcc and lib specs but refactored. 342 if (Args.hasArg(options::OPT_static)) { 343 CmdArgs.push_back("-lgcc_static"); 344 } else { 345 if (Args.hasArg(options::OPT_static_libgcc)) { 346 CmdArgs.push_back("-lgcc_eh"); 347 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) { 348 // Derived from darwin_iphoneos_libgcc spec. 349 if (isTargetIPhoneOS()) { 350 CmdArgs.push_back("-lgcc_s.1"); 351 } else { 352 CmdArgs.push_back("-lgcc_s.10.5"); 353 } 354 } else if (Args.hasArg(options::OPT_shared_libgcc) || 355 Args.hasFlag(options::OPT_fexceptions, 356 options::OPT_fno_exceptions) || 357 Args.hasArg(options::OPT_fgnu_runtime)) { 358 // FIXME: This is probably broken on 10.3? 359 if (isMacosxVersionLT(10, 5)) 360 CmdArgs.push_back("-lgcc_s.10.4"); 361 else if (isMacosxVersionLT(10, 6)) 362 CmdArgs.push_back("-lgcc_s.10.5"); 363 } else { 364 if (isMacosxVersionLT(10, 3, 9)) 365 ; // Do nothing. 366 else if (isMacosxVersionLT(10, 5)) 367 CmdArgs.push_back("-lgcc_s.10.4"); 368 else if (isMacosxVersionLT(10, 6)) 369 CmdArgs.push_back("-lgcc_s.10.5"); 370 } 371 372 if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) { 373 CmdArgs.push_back("-lgcc"); 374 CmdArgs.push_back("-lSystem"); 375 } else { 376 CmdArgs.push_back("-lSystem"); 377 CmdArgs.push_back("-lgcc"); 378 } 379 } 380} 381 382DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple) 383 : Darwin(Host, Triple) 384{ 385 getProgramPaths().push_back(getDriver().getInstalledDir()); 386 if (getDriver().getInstalledDir() != getDriver().Dir) 387 getProgramPaths().push_back(getDriver().Dir); 388 389 // We expect 'as', 'ld', etc. to be adjacent to our install dir. 390 getProgramPaths().push_back(getDriver().getInstalledDir()); 391 if (getDriver().getInstalledDir() != getDriver().Dir) 392 getProgramPaths().push_back(getDriver().Dir); 393 394 // For fallback, we need to know how to find the GCC cc1 executables, so we 395 // also add the GCC libexec paths. This is legiy code that can be removed once 396 // fallback is no longer useful. 397 std::string ToolChainDir = "i686-apple-darwin"; 398 ToolChainDir += llvm::utostr(DarwinVersion[0]); 399 ToolChainDir += "/4.2.1"; 400 401 std::string Path = getDriver().Dir; 402 Path += "/../libexec/gcc/"; 403 Path += ToolChainDir; 404 getProgramPaths().push_back(Path); 405 406 Path = "/usr/libexec/gcc/"; 407 Path += ToolChainDir; 408 getProgramPaths().push_back(Path); 409} 410 411void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args, 412 ArgStringList &CmdArgs) const { 413 // The Clang toolchain uses explicit paths for internal libraries. 414 415 // Unfortunately, we still might depend on a few of the libraries that are 416 // only available in the gcc library directory (in particular 417 // libstdc++.dylib). For now, hardcode the path to the known install location. 418 llvm::sys::Path P(getDriver().Dir); 419 P.eraseComponent(); // .../usr/bin -> ../usr 420 P.appendComponent("lib"); 421 P.appendComponent("gcc"); 422 switch (getTriple().getArch()) { 423 default: 424 assert(0 && "Invalid Darwin arch!"); 425 case llvm::Triple::x86: 426 case llvm::Triple::x86_64: 427 P.appendComponent("i686-apple-darwin10"); 428 break; 429 case llvm::Triple::arm: 430 case llvm::Triple::thumb: 431 P.appendComponent("arm-apple-darwin10"); 432 break; 433 case llvm::Triple::ppc: 434 case llvm::Triple::ppc64: 435 P.appendComponent("powerpc-apple-darwin10"); 436 break; 437 } 438 P.appendComponent("4.2.1"); 439 440 // Determine the arch specific GCC subdirectory. 441 const char *ArchSpecificDir = 0; 442 switch (getTriple().getArch()) { 443 default: 444 break; 445 case llvm::Triple::arm: 446 case llvm::Triple::thumb: { 447 std::string Triple = ComputeLLVMTriple(Args); 448 llvm::StringRef TripleStr = Triple; 449 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5")) 450 ArchSpecificDir = "v5"; 451 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6")) 452 ArchSpecificDir = "v6"; 453 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7")) 454 ArchSpecificDir = "v7"; 455 break; 456 } 457 case llvm::Triple::ppc64: 458 ArchSpecificDir = "ppc64"; 459 break; 460 case llvm::Triple::x86_64: 461 ArchSpecificDir = "x86_64"; 462 break; 463 } 464 465 if (ArchSpecificDir) { 466 P.appendComponent(ArchSpecificDir); 467 bool Exists; 468 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) 469 CmdArgs.push_back(Args.MakeArgString("-L" + P.str())); 470 P.eraseComponent(); 471 } 472 473 bool Exists; 474 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) 475 CmdArgs.push_back(Args.MakeArgString("-L" + P.str())); 476} 477 478void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, 479 ArgStringList &CmdArgs) const { 480 // Darwin doesn't support real static executables, don't link any runtime 481 // libraries with -static. 482 if (Args.hasArg(options::OPT_static)) 483 return; 484 485 // Reject -static-libgcc for now, we can deal with this when and if someone 486 // cares. This is useful in situations where someone wants to statically link 487 // something like libstdc++, and needs its runtime support routines. 488 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) { 489 getDriver().Diag(clang::diag::err_drv_unsupported_opt) 490 << A->getAsString(Args); 491 return; 492 } 493 494 // Otherwise link libSystem, then the dynamic runtime library, and finally any 495 // target specific static runtime library. 496 CmdArgs.push_back("-lSystem"); 497 498 // Select the dynamic runtime library and the target specific static library. 499 const char *DarwinStaticLib = 0; 500 if (isTargetIPhoneOS()) { 501 CmdArgs.push_back("-lgcc_s.1"); 502 503 // We may need some static functions for armv6/thumb which are required to 504 // be in the same linkage unit as their caller. 505 if (getDarwinArchName(Args) == "armv6") 506 DarwinStaticLib = "libclang_rt.armv6.a"; 507 } else { 508 // The dynamic runtime library was merged with libSystem for 10.6 and 509 // beyond; only 10.4 and 10.5 need an additional runtime library. 510 if (isMacosxVersionLT(10, 5)) 511 CmdArgs.push_back("-lgcc_s.10.4"); 512 else if (isMacosxVersionLT(10, 6)) 513 CmdArgs.push_back("-lgcc_s.10.5"); 514 515 // For OS X, we thought we would only need a static runtime library when 516 // targetting 10.4, to provide versions of the static functions which were 517 // omitted from 10.4.dylib. 518 // 519 // Unfortunately, that turned out to not be true, because Darwin system 520 // headers can still use eprintf on i386, and it is not exported from 521 // libSystem. Therefore, we still must provide a runtime library just for 522 // the tiny tiny handful of projects that *might* use that symbol. 523 if (isMacosxVersionLT(10, 5)) { 524 DarwinStaticLib = "libclang_rt.10.4.a"; 525 } else { 526 if (getTriple().getArch() == llvm::Triple::x86) 527 DarwinStaticLib = "libclang_rt.eprintf.a"; 528 } 529 } 530 531 /// Add the target specific static library, if needed. 532 if (DarwinStaticLib) { 533 llvm::sys::Path P(getDriver().ResourceDir); 534 P.appendComponent("lib"); 535 P.appendComponent("darwin"); 536 P.appendComponent(DarwinStaticLib); 537 538 // For now, allow missing resource libraries to support developers who may 539 // not have compiler-rt checked out or integrated into their build. 540 bool Exists; 541 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) 542 CmdArgs.push_back(Args.MakeArgString(P.str())); 543 } 544} 545 546void Darwin::AddDeploymentTarget(DerivedArgList &Args) const { 547 const OptTable &Opts = getDriver().getOpts(); 548 549 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ); 550 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ); 551 if (OSXVersion && iPhoneVersion) { 552 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with) 553 << OSXVersion->getAsString(Args) 554 << iPhoneVersion->getAsString(Args); 555 iPhoneVersion = 0; 556 } else if (!OSXVersion && !iPhoneVersion) { 557 // If neither OS X nor iPhoneOS targets were specified, check for 558 // environment defines. 559 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET"); 560 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"); 561 562 // Ignore empty strings. 563 if (OSXTarget && OSXTarget[0] == '\0') 564 OSXTarget = 0; 565 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0') 566 iPhoneOSTarget = 0; 567 568 // Diagnose conflicting deployment targets, and choose default platform 569 // based on the tool chain. 570 // 571 // FIXME: Don't hardcode default here. 572 if (OSXTarget && iPhoneOSTarget) { 573 // FIXME: We should see if we can get away with warning or erroring on 574 // this. Perhaps put under -pedantic? 575 if (getTriple().getArch() == llvm::Triple::arm || 576 getTriple().getArch() == llvm::Triple::thumb) 577 OSXTarget = 0; 578 else 579 iPhoneOSTarget = 0; 580 } 581 582 if (OSXTarget) { 583 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ); 584 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget); 585 Args.append(OSXVersion); 586 } else if (iPhoneOSTarget) { 587 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ); 588 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget); 589 Args.append(iPhoneVersion); 590 } else { 591 // Otherwise, assume we are targeting OS X. 592 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ); 593 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin); 594 Args.append(OSXVersion); 595 } 596 } 597 598 // Set the tool chain target information. 599 unsigned Major, Minor, Micro; 600 bool HadExtra; 601 if (OSXVersion) { 602 assert(!iPhoneVersion && "Unknown target platform!"); 603 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor, 604 Micro, HadExtra) || HadExtra || 605 Major != 10 || Minor >= 10 || Micro >= 10) 606 getDriver().Diag(clang::diag::err_drv_invalid_version_number) 607 << OSXVersion->getAsString(Args); 608 } else { 609 assert(iPhoneVersion && "Unknown target platform!"); 610 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor, 611 Micro, HadExtra) || HadExtra || 612 Major >= 10 || Minor >= 100 || Micro >= 100) 613 getDriver().Diag(clang::diag::err_drv_invalid_version_number) 614 << iPhoneVersion->getAsString(Args); 615 } 616 setTarget(iPhoneVersion, Major, Minor, Micro); 617} 618 619void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args, 620 ArgStringList &CmdArgs) const { 621 CXXStdlibType Type = GetCXXStdlibType(Args); 622 623 switch (Type) { 624 case ToolChain::CST_Libcxx: 625 CmdArgs.push_back("-lc++"); 626 break; 627 628 case ToolChain::CST_Libstdcxx: { 629 // Unfortunately, -lstdc++ doesn't always exist in the standard search path; 630 // it was previously found in the gcc lib dir. However, for all the Darwin 631 // platforms we care about it was -lstdc++.6, so we search for that 632 // explicitly if we can't see an obvious -lstdc++ candidate. 633 634 // Check in the sysroot first. 635 bool Exists; 636 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { 637 llvm::sys::Path P(A->getValue(Args)); 638 P.appendComponent("usr"); 639 P.appendComponent("lib"); 640 P.appendComponent("libstdc++.dylib"); 641 642 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) { 643 P.eraseComponent(); 644 P.appendComponent("libstdc++.6.dylib"); 645 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) { 646 CmdArgs.push_back(Args.MakeArgString(P.str())); 647 return; 648 } 649 } 650 } 651 652 // Otherwise, look in the root. 653 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&& 654 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){ 655 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib"); 656 return; 657 } 658 659 // Otherwise, let the linker search. 660 CmdArgs.push_back("-lstdc++"); 661 break; 662 } 663 } 664} 665 666void DarwinClang::AddCCKextLibArgs(const ArgList &Args, 667 ArgStringList &CmdArgs) const { 668 669 // For Darwin platforms, use the compiler-rt-based support library 670 // instead of the gcc-provided one (which is also incidentally 671 // only present in the gcc lib dir, which makes it hard to find). 672 673 llvm::sys::Path P(getDriver().ResourceDir); 674 P.appendComponent("lib"); 675 P.appendComponent("darwin"); 676 P.appendComponent("libclang_rt.cc_kext.a"); 677 678 // For now, allow missing resource libraries to support developers who may 679 // not have compiler-rt checked out or integrated into their build. 680 bool Exists; 681 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) 682 CmdArgs.push_back(Args.MakeArgString(P.str())); 683} 684 685DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args, 686 const char *BoundArch) const { 687 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); 688 const OptTable &Opts = getDriver().getOpts(); 689 690 // FIXME: We really want to get out of the tool chain level argument 691 // translation business, as it makes the driver functionality much 692 // more opaque. For now, we follow gcc closely solely for the 693 // purpose of easily achieving feature parity & testability. Once we 694 // have something that works, we should reevaluate each translation 695 // and try to push it down into tool specific logic. 696 697 for (ArgList::const_iterator it = Args.begin(), 698 ie = Args.end(); it != ie; ++it) { 699 Arg *A = *it; 700 701 if (A->getOption().matches(options::OPT_Xarch__)) { 702 // FIXME: Canonicalize name. 703 if (getArchName() != A->getValue(Args, 0)) 704 continue; 705 706 Arg *OriginalArg = A; 707 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1)); 708 unsigned Prev = Index; 709 Arg *XarchArg = Opts.ParseOneArg(Args, Index); 710 711 // If the argument parsing failed or more than one argument was 712 // consumed, the -Xarch_ argument's parameter tried to consume 713 // extra arguments. Emit an error and ignore. 714 // 715 // We also want to disallow any options which would alter the 716 // driver behavior; that isn't going to work in our model. We 717 // use isDriverOption() as an approximation, although things 718 // like -O4 are going to slip through. 719 if (!XarchArg || Index > Prev + 1 || 720 XarchArg->getOption().isDriverOption()) { 721 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument) 722 << A->getAsString(Args); 723 continue; 724 } 725 726 XarchArg->setBaseArg(A); 727 A = XarchArg; 728 729 DAL->AddSynthesizedArg(A); 730 731 // Linker input arguments require custom handling. The problem is that we 732 // have already constructed the phase actions, so we can not treat them as 733 // "input arguments". 734 if (A->getOption().isLinkerInput()) { 735 // Convert the argument into individual Zlinker_input_args. 736 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) { 737 DAL->AddSeparateArg(OriginalArg, 738 Opts.getOption(options::OPT_Zlinker_input), 739 A->getValue(Args, i)); 740 741 } 742 continue; 743 } 744 } 745 746 // Sob. These is strictly gcc compatible for the time being. Apple 747 // gcc translates options twice, which means that self-expanding 748 // options add duplicates. 749 switch ((options::ID) A->getOption().getID()) { 750 default: 751 DAL->append(A); 752 break; 753 754 case options::OPT_mkernel: 755 case options::OPT_fapple_kext: 756 DAL->append(A); 757 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static)); 758 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static)); 759 break; 760 761 case options::OPT_dependency_file: 762 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF), 763 A->getValue(Args)); 764 break; 765 766 case options::OPT_gfull: 767 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag)); 768 DAL->AddFlagArg(A, 769 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)); 770 break; 771 772 case options::OPT_gused: 773 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag)); 774 DAL->AddFlagArg(A, 775 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)); 776 break; 777 778 case options::OPT_fterminated_vtables: 779 case options::OPT_findirect_virtual_calls: 780 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext)); 781 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static)); 782 break; 783 784 case options::OPT_shared: 785 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib)); 786 break; 787 788 case options::OPT_fconstant_cfstrings: 789 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings)); 790 break; 791 792 case options::OPT_fno_constant_cfstrings: 793 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings)); 794 break; 795 796 case options::OPT_Wnonportable_cfstrings: 797 DAL->AddFlagArg(A, 798 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)); 799 break; 800 801 case options::OPT_Wno_nonportable_cfstrings: 802 DAL->AddFlagArg(A, 803 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)); 804 break; 805 806 case options::OPT_fpascal_strings: 807 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings)); 808 break; 809 810 case options::OPT_fno_pascal_strings: 811 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings)); 812 break; 813 } 814 } 815 816 if (getTriple().getArch() == llvm::Triple::x86 || 817 getTriple().getArch() == llvm::Triple::x86_64) 818 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ)) 819 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2"); 820 821 // Add the arch options based on the particular spelling of -arch, to match 822 // how the driver driver works. 823 if (BoundArch) { 824 llvm::StringRef Name = BoundArch; 825 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ); 826 const Option *MArch = Opts.getOption(options::OPT_march_EQ); 827 828 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch, 829 // which defines the list of which architectures we accept. 830 if (Name == "ppc") 831 ; 832 else if (Name == "ppc601") 833 DAL->AddJoinedArg(0, MCpu, "601"); 834 else if (Name == "ppc603") 835 DAL->AddJoinedArg(0, MCpu, "603"); 836 else if (Name == "ppc604") 837 DAL->AddJoinedArg(0, MCpu, "604"); 838 else if (Name == "ppc604e") 839 DAL->AddJoinedArg(0, MCpu, "604e"); 840 else if (Name == "ppc750") 841 DAL->AddJoinedArg(0, MCpu, "750"); 842 else if (Name == "ppc7400") 843 DAL->AddJoinedArg(0, MCpu, "7400"); 844 else if (Name == "ppc7450") 845 DAL->AddJoinedArg(0, MCpu, "7450"); 846 else if (Name == "ppc970") 847 DAL->AddJoinedArg(0, MCpu, "970"); 848 849 else if (Name == "ppc64") 850 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64)); 851 852 else if (Name == "i386") 853 ; 854 else if (Name == "i486") 855 DAL->AddJoinedArg(0, MArch, "i486"); 856 else if (Name == "i586") 857 DAL->AddJoinedArg(0, MArch, "i586"); 858 else if (Name == "i686") 859 DAL->AddJoinedArg(0, MArch, "i686"); 860 else if (Name == "pentium") 861 DAL->AddJoinedArg(0, MArch, "pentium"); 862 else if (Name == "pentium2") 863 DAL->AddJoinedArg(0, MArch, "pentium2"); 864 else if (Name == "pentpro") 865 DAL->AddJoinedArg(0, MArch, "pentiumpro"); 866 else if (Name == "pentIIm3") 867 DAL->AddJoinedArg(0, MArch, "pentium2"); 868 869 else if (Name == "x86_64") 870 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64)); 871 872 else if (Name == "arm") 873 DAL->AddJoinedArg(0, MArch, "armv4t"); 874 else if (Name == "armv4t") 875 DAL->AddJoinedArg(0, MArch, "armv4t"); 876 else if (Name == "armv5") 877 DAL->AddJoinedArg(0, MArch, "armv5tej"); 878 else if (Name == "xscale") 879 DAL->AddJoinedArg(0, MArch, "xscale"); 880 else if (Name == "armv6") 881 DAL->AddJoinedArg(0, MArch, "armv6k"); 882 else if (Name == "armv7") 883 DAL->AddJoinedArg(0, MArch, "armv7a"); 884 885 else 886 llvm_unreachable("invalid Darwin arch"); 887 } 888 889 // Add an explicit version min argument for the deployment target. We do this 890 // after argument translation because -Xarch_ arguments may add a version min 891 // argument. 892 AddDeploymentTarget(*DAL); 893 894 return DAL; 895} 896 897bool Darwin::IsUnwindTablesDefault() const { 898 // FIXME: Gross; we should probably have some separate target 899 // definition, possibly even reusing the one in clang. 900 return getArchName() == "x86_64"; 901} 902 903bool Darwin::UseDwarfDebugFlags() const { 904 if (const char *S = ::getenv("RC_DEBUG_OPTIONS")) 905 return S[0] != '\0'; 906 return false; 907} 908 909bool Darwin::UseSjLjExceptions() const { 910 // Darwin uses SjLj exceptions on ARM. 911 return (getTriple().getArch() == llvm::Triple::arm || 912 getTriple().getArch() == llvm::Triple::thumb); 913} 914 915const char *Darwin::GetDefaultRelocationModel() const { 916 return "pic"; 917} 918 919const char *Darwin::GetForcedPicModel() const { 920 if (getArchName() == "x86_64") 921 return "pic"; 922 return 0; 923} 924 925bool Darwin::SupportsObjCGC() const { 926 // Garbage collection is supported everywhere except on iPhone OS. 927 return !isTargetIPhoneOS(); 928} 929 930std::string 931Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const { 932 return ComputeLLVMTriple(Args); 933} 934 935/// Generic_GCC - A tool chain using the 'gcc' command to perform 936/// all subcommands; this relies on gcc translating the majority of 937/// command line options. 938 939Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple) 940 : ToolChain(Host, Triple) { 941 getProgramPaths().push_back(getDriver().getInstalledDir()); 942 if (getDriver().getInstalledDir() != getDriver().Dir.c_str()) 943 getProgramPaths().push_back(getDriver().Dir); 944} 945 946Generic_GCC::~Generic_GCC() { 947 // Free tool implementations. 948 for (llvm::DenseMap<unsigned, Tool*>::iterator 949 it = Tools.begin(), ie = Tools.end(); it != ie; ++it) 950 delete it->second; 951} 952 953Tool &Generic_GCC::SelectTool(const Compilation &C, 954 const JobAction &JA) const { 955 Action::ActionClass Key; 956 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 957 Key = Action::AnalyzeJobClass; 958 else 959 Key = JA.getKind(); 960 961 Tool *&T = Tools[Key]; 962 if (!T) { 963 switch (Key) { 964 case Action::InputClass: 965 case Action::BindArchClass: 966 assert(0 && "Invalid tool kind."); 967 case Action::PreprocessJobClass: 968 T = new tools::gcc::Preprocess(*this); break; 969 case Action::PrecompileJobClass: 970 T = new tools::gcc::Precompile(*this); break; 971 case Action::AnalyzeJobClass: 972 T = new tools::Clang(*this); break; 973 case Action::CompileJobClass: 974 T = new tools::gcc::Compile(*this); break; 975 case Action::AssembleJobClass: 976 T = new tools::gcc::Assemble(*this); break; 977 case Action::LinkJobClass: 978 T = new tools::gcc::Link(*this); break; 979 980 // This is a bit ungeneric, but the only platform using a driver 981 // driver is Darwin. 982 case Action::LipoJobClass: 983 T = new tools::darwin::Lipo(*this); break; 984 case Action::DsymutilJobClass: 985 T = new tools::darwin::Dsymutil(*this); break; 986 } 987 } 988 989 return *T; 990} 991 992bool Generic_GCC::IsUnwindTablesDefault() const { 993 // FIXME: Gross; we should probably have some separate target 994 // definition, possibly even reusing the one in clang. 995 return getArchName() == "x86_64"; 996} 997 998const char *Generic_GCC::GetDefaultRelocationModel() const { 999 return "static"; 1000} 1001 1002const char *Generic_GCC::GetForcedPicModel() const { 1003 return 0; 1004} 1005 1006/// TCEToolChain - A tool chain using the llvm bitcode tools to perform 1007/// all subcommands. See http://tce.cs.tut.fi for our peculiar target. 1008/// Currently does not support anything else but compilation. 1009 1010TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple) 1011 : ToolChain(Host, Triple) { 1012 // Path mangling to find libexec 1013 std::string Path(getDriver().Dir); 1014 1015 Path += "/../libexec"; 1016 getProgramPaths().push_back(Path); 1017} 1018 1019TCEToolChain::~TCEToolChain() { 1020 for (llvm::DenseMap<unsigned, Tool*>::iterator 1021 it = Tools.begin(), ie = Tools.end(); it != ie; ++it) 1022 delete it->second; 1023} 1024 1025bool TCEToolChain::IsMathErrnoDefault() const { 1026 return true; 1027} 1028 1029bool TCEToolChain::IsUnwindTablesDefault() const { 1030 return false; 1031} 1032 1033const char *TCEToolChain::GetDefaultRelocationModel() const { 1034 return "static"; 1035} 1036 1037const char *TCEToolChain::GetForcedPicModel() const { 1038 return 0; 1039} 1040 1041Tool &TCEToolChain::SelectTool(const Compilation &C, 1042 const JobAction &JA) const { 1043 Action::ActionClass Key; 1044 Key = Action::AnalyzeJobClass; 1045 1046 Tool *&T = Tools[Key]; 1047 if (!T) { 1048 switch (Key) { 1049 case Action::PreprocessJobClass: 1050 T = new tools::gcc::Preprocess(*this); break; 1051 case Action::AnalyzeJobClass: 1052 T = new tools::Clang(*this); break; 1053 default: 1054 assert(false && "Unsupported action for TCE target."); 1055 } 1056 } 1057 return *T; 1058} 1059 1060/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly. 1061 1062OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple) 1063 : Generic_ELF(Host, Triple) { 1064 getFilePaths().push_back(getDriver().Dir + "/../lib"); 1065 getFilePaths().push_back("/usr/lib"); 1066} 1067 1068Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const { 1069 Action::ActionClass Key; 1070 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1071 Key = Action::AnalyzeJobClass; 1072 else 1073 Key = JA.getKind(); 1074 1075 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as, 1076 options::OPT_no_integrated_as, 1077 IsIntegratedAssemblerDefault()); 1078 1079 Tool *&T = Tools[Key]; 1080 if (!T) { 1081 switch (Key) { 1082 case Action::AssembleJobClass: { 1083 if (UseIntegratedAs) 1084 T = new tools::ClangAs(*this); 1085 else 1086 T = new tools::openbsd::Assemble(*this); 1087 break; 1088 } 1089 case Action::LinkJobClass: 1090 T = new tools::openbsd::Link(*this); break; 1091 default: 1092 T = &Generic_GCC::SelectTool(C, JA); 1093 } 1094 } 1095 1096 return *T; 1097} 1098 1099/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly. 1100 1101FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple) 1102 : Generic_ELF(Host, Triple) { 1103 1104 // Determine if we are compiling 32-bit code on an x86_64 platform. 1105 bool Lib32 = false; 1106 if (Triple.getArch() == llvm::Triple::x86 && 1107 llvm::Triple(getDriver().DefaultHostTriple).getArch() == 1108 llvm::Triple::x86_64) 1109 Lib32 = true; 1110 1111 getProgramPaths().push_back(getDriver().Dir + "/../libexec"); 1112 getProgramPaths().push_back("/usr/libexec"); 1113 if (Lib32) { 1114 getFilePaths().push_back(CLANG_PREFIX "/usr/lib32"); 1115 } else { 1116 getFilePaths().push_back(CLANG_PREFIX "/usr/lib"); 1117 } 1118} 1119 1120Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const { 1121 Action::ActionClass Key; 1122 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1123 Key = Action::AnalyzeJobClass; 1124 else 1125 Key = JA.getKind(); 1126 1127 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as, 1128 options::OPT_no_integrated_as, 1129 IsIntegratedAssemblerDefault()); 1130 1131 Tool *&T = Tools[Key]; 1132 if (!T) { 1133 switch (Key) { 1134 case Action::AssembleJobClass: 1135 if (UseIntegratedAs) 1136 T = new tools::ClangAs(*this); 1137 else 1138 T = new tools::freebsd::Assemble(*this); 1139 break; 1140 case Action::LinkJobClass: 1141 T = new tools::freebsd::Link(*this); break; 1142 default: 1143 T = &Generic_GCC::SelectTool(C, JA); 1144 } 1145 } 1146 1147 return *T; 1148} 1149 1150/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly. 1151 1152NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple) 1153 : Generic_ELF(Host, Triple) { 1154 1155 // Determine if we are compiling 32-bit code on an x86_64 platform. 1156 bool Lib32 = false; 1157 if (Triple.getArch() == llvm::Triple::x86 && 1158 llvm::Triple(getDriver().DefaultHostTriple).getArch() == 1159 llvm::Triple::x86_64) 1160 Lib32 = true; 1161 1162 getProgramPaths().push_back(getDriver().Dir + "/../libexec"); 1163 getProgramPaths().push_back("/usr/libexec"); 1164 if (Lib32) { 1165 getFilePaths().push_back("/usr/lib/i386"); 1166 } else { 1167 getFilePaths().push_back("/usr/lib"); 1168 } 1169} 1170 1171Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA) const { 1172 Action::ActionClass Key; 1173 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1174 Key = Action::AnalyzeJobClass; 1175 else 1176 Key = JA.getKind(); 1177 1178 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as, 1179 options::OPT_no_integrated_as, 1180 IsIntegratedAssemblerDefault()); 1181 1182 Tool *&T = Tools[Key]; 1183 if (!T) { 1184 switch (Key) { 1185 case Action::AssembleJobClass: 1186 if (UseIntegratedAs) 1187 T = new tools::ClangAs(*this); 1188 else 1189 T = new tools::netbsd::Assemble(*this); 1190 break; 1191 case Action::LinkJobClass: 1192 T = new tools::netbsd::Link(*this); break; 1193 default: 1194 T = &Generic_GCC::SelectTool(C, JA); 1195 } 1196 } 1197 1198 return *T; 1199} 1200 1201/// Minix - Minix tool chain which can call as(1) and ld(1) directly. 1202 1203Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple) 1204 : Generic_GCC(Host, Triple) { 1205 getFilePaths().push_back(getDriver().Dir + "/../lib"); 1206 getFilePaths().push_back("/usr/lib"); 1207 getFilePaths().push_back("/usr/gnu/lib"); 1208 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3"); 1209} 1210 1211Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const { 1212 Action::ActionClass Key; 1213 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1214 Key = Action::AnalyzeJobClass; 1215 else 1216 Key = JA.getKind(); 1217 1218 Tool *&T = Tools[Key]; 1219 if (!T) { 1220 switch (Key) { 1221 case Action::AssembleJobClass: 1222 T = new tools::minix::Assemble(*this); break; 1223 case Action::LinkJobClass: 1224 T = new tools::minix::Link(*this); break; 1225 default: 1226 T = &Generic_GCC::SelectTool(C, JA); 1227 } 1228 } 1229 1230 return *T; 1231} 1232 1233/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly. 1234 1235AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple) 1236 : Generic_GCC(Host, Triple) { 1237 1238 getProgramPaths().push_back(getDriver().getInstalledDir()); 1239 if (getDriver().getInstalledDir() != getDriver().Dir.c_str()) 1240 getProgramPaths().push_back(getDriver().Dir); 1241 1242 getFilePaths().push_back(getDriver().Dir + "/../lib"); 1243 getFilePaths().push_back("/usr/lib"); 1244 getFilePaths().push_back("/usr/sfw/lib"); 1245 getFilePaths().push_back("/opt/gcc4/lib"); 1246 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4"); 1247 1248} 1249 1250Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const { 1251 Action::ActionClass Key; 1252 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1253 Key = Action::AnalyzeJobClass; 1254 else 1255 Key = JA.getKind(); 1256 1257 Tool *&T = Tools[Key]; 1258 if (!T) { 1259 switch (Key) { 1260 case Action::AssembleJobClass: 1261 T = new tools::auroraux::Assemble(*this); break; 1262 case Action::LinkJobClass: 1263 T = new tools::auroraux::Link(*this); break; 1264 default: 1265 T = &Generic_GCC::SelectTool(C, JA); 1266 } 1267 } 1268 1269 return *T; 1270} 1271 1272 1273/// Linux toolchain (very bare-bones at the moment). 1274 1275enum LinuxDistro { 1276 DebianLenny, 1277 DebianSqueeze, 1278 Exherbo, 1279 Fedora13, 1280 Fedora14, 1281 OpenSuse11_3, 1282 UbuntuJaunty, 1283 UbuntuKarmic, 1284 UbuntuLucid, 1285 UbuntuMaverick, 1286 UnknownDistro 1287}; 1288 1289static bool IsFedora(enum LinuxDistro Distro) { 1290 return Distro == Fedora13 || Distro == Fedora14; 1291} 1292 1293static bool IsOpenSuse(enum LinuxDistro Distro) { 1294 return Distro == OpenSuse11_3; 1295} 1296 1297static bool IsDebian(enum LinuxDistro Distro) { 1298 return Distro == DebianLenny || Distro == DebianSqueeze; 1299} 1300 1301static bool IsUbuntu(enum LinuxDistro Distro) { 1302 return Distro == UbuntuLucid || Distro == UbuntuMaverick || 1303 Distro == UbuntuJaunty || Distro == UbuntuKarmic; 1304} 1305 1306static bool IsDebianBased(enum LinuxDistro Distro) { 1307 return IsDebian(Distro) || IsUbuntu(Distro); 1308} 1309 1310static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) { 1311 if (Arch == llvm::Triple::x86_64) { 1312 bool Exists; 1313 if (Distro == Exherbo && 1314 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists)) 1315 return false; 1316 1317 return true; 1318 } 1319 if (Arch == llvm::Triple::x86 && IsDebianBased(Distro)) 1320 return true; 1321 return false; 1322} 1323 1324static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) { 1325 llvm::OwningPtr<llvm::MemoryBuffer> File; 1326 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) { 1327 llvm::StringRef Data = File.get()->getBuffer(); 1328 llvm::SmallVector<llvm::StringRef, 8> Lines; 1329 Data.split(Lines, "\n"); 1330 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) { 1331 if (Lines[i] == "DISTRIB_CODENAME=maverick") 1332 return UbuntuMaverick; 1333 else if (Lines[i] == "DISTRIB_CODENAME=lucid") 1334 return UbuntuLucid; 1335 else if (Lines[i] == "DISTRIB_CODENAME=jaunty") 1336 return UbuntuJaunty; 1337 else if (Lines[i] == "DISTRIB_CODENAME=karmic") 1338 return UbuntuKarmic; 1339 } 1340 return UnknownDistro; 1341 } 1342 1343 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) { 1344 llvm::StringRef Data = File.get()->getBuffer(); 1345 if (Data.startswith("Fedora release 14 (Laughlin)")) 1346 return Fedora14; 1347 else if (Data.startswith("Fedora release 13 (Goddard)")) 1348 return Fedora13; 1349 return UnknownDistro; 1350 } 1351 1352 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) { 1353 llvm::StringRef Data = File.get()->getBuffer(); 1354 if (Data[0] == '5') 1355 return DebianLenny; 1356 else if (Data.startswith("squeeze/sid")) 1357 return DebianSqueeze; 1358 return UnknownDistro; 1359 } 1360 1361 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) { 1362 llvm::StringRef Data = File.get()->getBuffer(); 1363 if (Data.startswith("openSUSE 11.3")) 1364 return OpenSuse11_3; 1365 return UnknownDistro; 1366 } 1367 1368 bool Exists; 1369 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists) 1370 return Exherbo; 1371 1372 return UnknownDistro; 1373} 1374 1375Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple) 1376 : Generic_ELF(Host, Triple) { 1377 llvm::Triple::ArchType Arch = 1378 llvm::Triple(getDriver().DefaultHostTriple).getArch(); 1379 1380 std::string Suffix32 = ""; 1381 if (Arch == llvm::Triple::x86_64) 1382 Suffix32 = "/32"; 1383 1384 std::string Suffix64 = ""; 1385 if (Arch == llvm::Triple::x86) 1386 Suffix64 = "/64"; 1387 1388 std::string Lib32 = "lib"; 1389 1390 bool Exists; 1391 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists) 1392 Lib32 = "lib32"; 1393 1394 std::string Lib64 = "lib"; 1395 bool Symlink; 1396 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists && 1397 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink)) 1398 Lib64 = "lib64"; 1399 1400 std::string GccTriple = ""; 1401 if (Arch == llvm::Triple::arm) { 1402 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) && 1403 Exists) 1404 GccTriple = "arm-linux-gnueabi"; 1405 } else if (Arch == llvm::Triple::x86_64) { 1406 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) && 1407 Exists) 1408 GccTriple = "x86_64-linux-gnu"; 1409 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu", 1410 Exists) && Exists) 1411 GccTriple = "x86_64-unknown-linux-gnu"; 1412 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu", 1413 Exists) && Exists) 1414 GccTriple = "x86_64-pc-linux-gnu"; 1415 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux", 1416 Exists) && Exists) 1417 GccTriple = "x86_64-redhat-linux"; 1418 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux", 1419 Exists) && Exists) 1420 GccTriple = "x86_64-suse-linux"; 1421 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu", 1422 Exists) && Exists) 1423 GccTriple = "x86_64-manbo-linux-gnu"; 1424 } else if (Arch == llvm::Triple::x86) { 1425 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists) 1426 GccTriple = "i686-linux-gnu"; 1427 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) && 1428 Exists) 1429 GccTriple = "i686-pc-linux-gnu"; 1430 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) && 1431 Exists) 1432 GccTriple = "i486-linux-gnu"; 1433 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) && 1434 Exists) 1435 GccTriple = "i686-redhat-linux"; 1436 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) && 1437 Exists) 1438 GccTriple = "i586-suse-linux"; 1439 } 1440 1441 const char* GccVersions[] = {"4.5.1", "4.5", "4.4.5", "4.4.4", "4.4.3", "4.4", 1442 "4.3.4", "4.3.3", "4.3.2"}; 1443 std::string Base = ""; 1444 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) { 1445 std::string Suffix = GccTriple + "/" + GccVersions[i]; 1446 std::string t1 = "/usr/lib/gcc/" + Suffix; 1447 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists) { 1448 Base = t1; 1449 break; 1450 } 1451 std::string t2 = "/usr/lib64/gcc/" + Suffix; 1452 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists) { 1453 Base = t2; 1454 break; 1455 } 1456 } 1457 1458 path_list &Paths = getFilePaths(); 1459 bool Is32Bits = getArch() == llvm::Triple::x86; 1460 1461 std::string Suffix; 1462 std::string Lib; 1463 1464 if (Is32Bits) { 1465 Suffix = Suffix32; 1466 Lib = Lib32; 1467 } else { 1468 Suffix = Suffix64; 1469 Lib = Lib64; 1470 } 1471 1472 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld"); 1473 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists) 1474 Linker = LinkerPath.str(); 1475 else 1476 Linker = GetProgramPath("ld"); 1477 1478 LinuxDistro Distro = DetectLinuxDistro(Arch); 1479 1480 if (IsUbuntu(Distro)) { 1481 ExtraOpts.push_back("-z"); 1482 ExtraOpts.push_back("relro"); 1483 } 1484 1485 if (Arch == llvm::Triple::arm) 1486 ExtraOpts.push_back("-X"); 1487 1488 if (IsFedora(Distro) || Distro == UbuntuMaverick) 1489 ExtraOpts.push_back("--hash-style=gnu"); 1490 1491 if (IsDebian(Distro) || Distro == UbuntuLucid || Distro == UbuntuJaunty || 1492 Distro == UbuntuKarmic) 1493 ExtraOpts.push_back("--hash-style=both"); 1494 1495 if (IsFedora(Distro)) 1496 ExtraOpts.push_back("--no-add-needed"); 1497 1498 if (Distro == DebianSqueeze || IsOpenSuse(Distro) || 1499 IsFedora(Distro) || Distro == UbuntuLucid || Distro == UbuntuMaverick || 1500 Distro == UbuntuKarmic) 1501 ExtraOpts.push_back("--build-id"); 1502 1503 Paths.push_back(Base + Suffix); 1504 if (HasMultilib(Arch, Distro)) { 1505 if (IsOpenSuse(Distro) && Is32Bits) 1506 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib"); 1507 Paths.push_back(Base + "/../../../../" + Lib); 1508 Paths.push_back("/lib/../" + Lib); 1509 Paths.push_back("/usr/lib/../" + Lib); 1510 } 1511 if (!Suffix.empty()) 1512 Paths.push_back(Base); 1513 if (IsOpenSuse(Distro)) 1514 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib"); 1515 Paths.push_back(Base + "/../../.."); 1516 if (Arch == getArch() && IsUbuntu(Distro)) 1517 Paths.push_back("/usr/lib/" + GccTriple); 1518} 1519 1520bool Linux::HasNativeLLVMSupport() const { 1521 return true; 1522} 1523 1524Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const { 1525 Action::ActionClass Key; 1526 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1527 Key = Action::AnalyzeJobClass; 1528 else 1529 Key = JA.getKind(); 1530 1531 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as, 1532 options::OPT_no_integrated_as, 1533 IsIntegratedAssemblerDefault()); 1534 1535 Tool *&T = Tools[Key]; 1536 if (!T) { 1537 switch (Key) { 1538 case Action::AssembleJobClass: 1539 if (UseIntegratedAs) 1540 T = new tools::ClangAs(*this); 1541 else 1542 T = new tools::linuxtools::Assemble(*this); 1543 break; 1544 case Action::LinkJobClass: 1545 T = new tools::linuxtools::Link(*this); break; 1546 default: 1547 T = &Generic_GCC::SelectTool(C, JA); 1548 } 1549 } 1550 1551 return *T; 1552} 1553 1554/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly. 1555 1556DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple) 1557 : Generic_ELF(Host, Triple) { 1558 1559 // Path mangling to find libexec 1560 getProgramPaths().push_back(getDriver().getInstalledDir()); 1561 if (getDriver().getInstalledDir() != getDriver().Dir.c_str()) 1562 getProgramPaths().push_back(getDriver().Dir); 1563 1564 getFilePaths().push_back(getDriver().Dir + "/../lib"); 1565 getFilePaths().push_back("/usr/lib"); 1566 getFilePaths().push_back("/usr/lib/gcc41"); 1567} 1568 1569Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const { 1570 Action::ActionClass Key; 1571 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1572 Key = Action::AnalyzeJobClass; 1573 else 1574 Key = JA.getKind(); 1575 1576 Tool *&T = Tools[Key]; 1577 if (!T) { 1578 switch (Key) { 1579 case Action::AssembleJobClass: 1580 T = new tools::dragonfly::Assemble(*this); break; 1581 case Action::LinkJobClass: 1582 T = new tools::dragonfly::Link(*this); break; 1583 default: 1584 T = &Generic_GCC::SelectTool(C, JA); 1585 } 1586 } 1587 1588 return *T; 1589} 1590 1591Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple) 1592 : ToolChain(Host, Triple) { 1593} 1594 1595Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA) const { 1596 Action::ActionClass Key; 1597 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) 1598 Key = Action::AnalyzeJobClass; 1599 else 1600 Key = JA.getKind(); 1601 1602 Tool *&T = Tools[Key]; 1603 if (!T) { 1604 switch (Key) { 1605 case Action::InputClass: 1606 case Action::BindArchClass: 1607 case Action::LipoJobClass: 1608 case Action::DsymutilJobClass: 1609 assert(0 && "Invalid tool kind."); 1610 case Action::PreprocessJobClass: 1611 case Action::PrecompileJobClass: 1612 case Action::AnalyzeJobClass: 1613 case Action::CompileJobClass: 1614 T = new tools::Clang(*this); break; 1615 case Action::AssembleJobClass: 1616 T = new tools::ClangAs(*this); break; 1617 case Action::LinkJobClass: 1618 T = new tools::visualstudio::Link(*this); break; 1619 } 1620 } 1621 1622 return *T; 1623} 1624 1625bool Windows::IsIntegratedAssemblerDefault() const { 1626 return true; 1627} 1628 1629bool Windows::IsUnwindTablesDefault() const { 1630 // FIXME: Gross; we should probably have some separate target 1631 // definition, possibly even reusing the one in clang. 1632 return getArchName() == "x86_64"; 1633} 1634 1635const char *Windows::GetDefaultRelocationModel() const { 1636 return "static"; 1637} 1638 1639const char *Windows::GetForcedPicModel() const { 1640 if (getArchName() == "x86_64") 1641 return "pic"; 1642 return 0; 1643} 1644