1 //===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ToolChains.h"
12 #ifdef HAVE_CLANG_CONFIG_H
13 # include "clang/Config/config.h"
16 #include "clang/Driver/Arg.h"
17 #include "clang/Driver/ArgList.h"
18 #include "clang/Driver/Compilation.h"
19 #include "clang/Driver/Driver.h"
20 #include "clang/Driver/DriverDiagnostic.h"
21 #include "clang/Driver/HostInfo.h"
22 #include "clang/Driver/ObjCRuntime.h"
23 #include "clang/Driver/OptTable.h"
24 #include "clang/Driver/Option.h"
25 #include "clang/Driver/Options.h"
26 #include "clang/Basic/Version.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/system_error.h"
38 #include <cstdlib> // ::getenv
40 #include "llvm/Config/config.h" // for CXX_INCLUDE_ROOT
46 using namespace clang::driver;
47 using namespace clang::driver::toolchains;
49 /// Darwin - Darwin tool chain for i386 and x86_64.
51 Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
52 : ToolChain(Host, Triple), TargetInitialized(false),
53 ARCRuntimeForSimulator(ARCSimulator_None)
55 // Compute the initial Darwin version based on the host.
57 std::string OSName = Triple.getOSName();
58 if (!Driver::GetReleaseVersion(&OSName.c_str()[6],
59 DarwinVersion[0], DarwinVersion[1],
60 DarwinVersion[2], HadExtra))
61 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
63 llvm::raw_string_ostream(MacosxVersionMin)
64 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
68 types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
69 types::ID Ty = types::lookupTypeForExtension(Ext);
71 // Darwin always preprocesses assembly files (unless -x is used explicitly).
72 if (Ty == types::TY_PP_Asm)
78 bool Darwin::HasNativeLLVMSupport() const {
82 bool Darwin::hasARCRuntime() const {
83 // FIXME: Remove this once there is a proper way to detect an ARC runtime
85 switch (ARCRuntimeForSimulator) {
86 case ARCSimulator_None:
88 case ARCSimulator_HasARCRuntime:
90 case ARCSimulator_NoARCRuntime:
94 if (isTargetIPhoneOS())
95 return !isIPhoneOSVersionLT(5);
97 return !isMacosxVersionLT(10, 7);
100 /// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
101 void Darwin::configureObjCRuntime(ObjCRuntime &runtime) const {
102 if (runtime.getKind() != ObjCRuntime::NeXT)
103 return ToolChain::configureObjCRuntime(runtime);
105 runtime.HasARC = runtime.HasWeak = hasARCRuntime();
107 // So far, objc_terminate is only available in iOS 5.
108 // FIXME: do the simulator logic properly.
109 if (!ARCRuntimeForSimulator && isTargetIPhoneOS())
110 runtime.HasTerminate = !isIPhoneOSVersionLT(5);
112 runtime.HasTerminate = false;
115 // FIXME: Can we tablegen this?
116 static const char *GetArmArchForMArch(llvm::StringRef Value) {
117 if (Value == "armv6k")
120 if (Value == "armv5tej")
123 if (Value == "xscale")
126 if (Value == "armv4t")
129 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
130 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
137 // FIXME: Can we tablegen this?
138 static const char *GetArmArchForMCpu(llvm::StringRef Value) {
139 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
140 Value == "arm946e-s" || Value == "arm966e-s" ||
141 Value == "arm968e-s" || Value == "arm10e" ||
142 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
143 Value == "arm1026ej-s")
146 if (Value == "xscale")
149 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
150 Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
151 Value == "cortex-m0" )
154 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
160 llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
161 switch (getTriple().getArch()) {
163 return getArchName();
165 case llvm::Triple::thumb:
166 case llvm::Triple::arm: {
167 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
168 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
171 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
172 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
181 // Free tool implementations.
182 for (llvm::DenseMap<unsigned, Tool*>::iterator
183 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
187 std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
188 llvm::Triple Triple(ComputeLLVMTriple(Args));
190 // If the target isn't initialized (e.g., an unknown Darwin platform, return
191 // the default triple).
192 if (!isTargetInitialized())
193 return Triple.getTriple();
196 getTargetVersion(Version);
198 llvm::SmallString<16> Str;
199 llvm::raw_svector_ostream(Str)
200 << (isTargetIPhoneOS() ? "ios" : "macosx")
201 << Version[0] << "." << Version[1] << "." << Version[2];
202 Triple.setOSName(Str.str());
204 return Triple.getTriple();
207 Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
208 const ActionList &Inputs) const {
209 Action::ActionClass Key;
211 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
212 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
213 if (Inputs.size() == 1 &&
214 types::isCXX(Inputs[0]->getType()) &&
215 getTriple().getOS() == llvm::Triple::Darwin &&
216 getTriple().getArch() == llvm::Triple::x86 &&
217 C.getArgs().getLastArg(options::OPT_fapple_kext))
220 Key = Action::AnalyzeJobClass;
224 // FIXME: This doesn't belong here, but ideally we will support static soon
226 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
227 C.getArgs().hasArg(options::OPT_static) ||
228 C.getArgs().hasArg(options::OPT_fapple_kext));
229 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
230 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
231 options::OPT_no_integrated_as,
234 Tool *&T = Tools[Key];
237 case Action::InputClass:
238 case Action::BindArchClass:
239 assert(0 && "Invalid tool kind.");
240 case Action::PreprocessJobClass:
241 T = new tools::darwin::Preprocess(*this); break;
242 case Action::AnalyzeJobClass:
243 T = new tools::Clang(*this); break;
244 case Action::PrecompileJobClass:
245 case Action::CompileJobClass:
246 T = new tools::darwin::Compile(*this); break;
247 case Action::AssembleJobClass: {
249 T = new tools::ClangAs(*this);
251 T = new tools::darwin::Assemble(*this);
254 case Action::LinkJobClass:
255 T = new tools::darwin::Link(*this); break;
256 case Action::LipoJobClass:
257 T = new tools::darwin::Lipo(*this); break;
258 case Action::DsymutilJobClass:
259 T = new tools::darwin::Dsymutil(*this); break;
267 DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
268 : Darwin(Host, Triple)
270 std::string UsrPrefix = "llvm-gcc-4.2/";
272 getProgramPaths().push_back(getDriver().getInstalledDir());
273 if (getDriver().getInstalledDir() != getDriver().Dir)
274 getProgramPaths().push_back(getDriver().Dir);
276 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
277 getProgramPaths().push_back(getDriver().getInstalledDir());
278 if (getDriver().getInstalledDir() != getDriver().Dir)
279 getProgramPaths().push_back(getDriver().Dir);
281 // For fallback, we need to know how to find the GCC cc1 executables, so we
282 // also add the GCC libexec paths. This is legacy code that can be removed
283 // once fallback is no longer useful.
284 std::string ToolChainDir = "i686-apple-darwin";
285 ToolChainDir += llvm::utostr(DarwinVersion[0]);
286 ToolChainDir += "/4.2.1";
288 std::string Path = getDriver().Dir;
289 Path += "/../" + UsrPrefix + "libexec/gcc/";
290 Path += ToolChainDir;
291 getProgramPaths().push_back(Path);
293 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
294 Path += ToolChainDir;
295 getProgramPaths().push_back(Path);
298 void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
299 ArgStringList &CmdArgs) const {
300 // The Clang toolchain uses explicit paths for internal libraries.
302 // Unfortunately, we still might depend on a few of the libraries that are
303 // only available in the gcc library directory (in particular
304 // libstdc++.dylib). For now, hardcode the path to the known install location.
305 llvm::sys::Path P(getDriver().Dir);
306 P.eraseComponent(); // .../usr/bin -> ../usr
307 P.appendComponent("lib");
308 P.appendComponent("gcc");
309 switch (getTriple().getArch()) {
311 assert(0 && "Invalid Darwin arch!");
312 case llvm::Triple::x86:
313 case llvm::Triple::x86_64:
314 P.appendComponent("i686-apple-darwin10");
316 case llvm::Triple::arm:
317 case llvm::Triple::thumb:
318 P.appendComponent("arm-apple-darwin10");
320 case llvm::Triple::ppc:
321 case llvm::Triple::ppc64:
322 P.appendComponent("powerpc-apple-darwin10");
325 P.appendComponent("4.2.1");
327 // Determine the arch specific GCC subdirectory.
328 const char *ArchSpecificDir = 0;
329 switch (getTriple().getArch()) {
332 case llvm::Triple::arm:
333 case llvm::Triple::thumb: {
334 std::string Triple = ComputeLLVMTriple(Args);
335 llvm::StringRef TripleStr = Triple;
336 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
337 ArchSpecificDir = "v5";
338 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
339 ArchSpecificDir = "v6";
340 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
341 ArchSpecificDir = "v7";
344 case llvm::Triple::ppc64:
345 ArchSpecificDir = "ppc64";
347 case llvm::Triple::x86_64:
348 ArchSpecificDir = "x86_64";
352 if (ArchSpecificDir) {
353 P.appendComponent(ArchSpecificDir);
355 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
356 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
361 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
362 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
365 void DarwinClang::AddLinkARCArgs(const ArgList &Args,
366 ArgStringList &CmdArgs) const {
368 CmdArgs.push_back("-force_load");
369 llvm::sys::Path P(getDriver().ClangExecutable);
370 P.eraseComponent(); // 'clang'
371 P.eraseComponent(); // 'bin'
372 P.appendComponent("lib");
373 P.appendComponent("arc");
374 P.appendComponent("libarclite_");
375 std::string s = P.str();
376 // Mash in the platform.
377 if (isTargetIPhoneOS())
379 // FIXME: isTargetIphoneOSSimulator() is not returning true.
380 else if (ARCRuntimeForSimulator != ARCSimulator_None)
381 s += "iphonesimulator";
386 CmdArgs.push_back(Args.MakeArgString(s));
389 void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
390 ArgStringList &CmdArgs,
391 const char *DarwinStaticLib) const {
392 llvm::sys::Path P(getDriver().ResourceDir);
393 P.appendComponent("lib");
394 P.appendComponent("darwin");
395 P.appendComponent(DarwinStaticLib);
397 // For now, allow missing resource libraries to support developers who may
398 // not have compiler-rt checked out or integrated into their build.
400 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
401 CmdArgs.push_back(Args.MakeArgString(P.str()));
404 void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
405 ArgStringList &CmdArgs) const {
406 // Darwin doesn't support real static executables, don't link any runtime
407 // libraries with -static.
408 if (Args.hasArg(options::OPT_static))
411 // Reject -static-libgcc for now, we can deal with this when and if someone
412 // cares. This is useful in situations where someone wants to statically link
413 // something like libstdc++, and needs its runtime support routines.
414 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
415 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
416 << A->getAsString(Args);
420 // Otherwise link libSystem, then the dynamic runtime library, and finally any
421 // target specific static runtime library.
422 CmdArgs.push_back("-lSystem");
424 // Select the dynamic runtime library and the target specific static library.
425 if (isTargetIPhoneOS()) {
426 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
427 // it never went into the SDK.
428 if (!isTargetIOSSimulator())
429 CmdArgs.push_back("-lgcc_s.1");
431 // We currently always need a static runtime library for iOS.
432 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
434 // The dynamic runtime library was merged with libSystem for 10.6 and
435 // beyond; only 10.4 and 10.5 need an additional runtime library.
436 if (isMacosxVersionLT(10, 5))
437 CmdArgs.push_back("-lgcc_s.10.4");
438 else if (isMacosxVersionLT(10, 6))
439 CmdArgs.push_back("-lgcc_s.10.5");
441 // For OS X, we thought we would only need a static runtime library when
442 // targeting 10.4, to provide versions of the static functions which were
443 // omitted from 10.4.dylib.
445 // Unfortunately, that turned out to not be true, because Darwin system
446 // headers can still use eprintf on i386, and it is not exported from
447 // libSystem. Therefore, we still must provide a runtime library just for
448 // the tiny tiny handful of projects that *might* use that symbol.
449 if (isMacosxVersionLT(10, 5)) {
450 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
452 if (getTriple().getArch() == llvm::Triple::x86)
453 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
454 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
459 static inline llvm::StringRef SimulatorVersionDefineName() {
460 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
463 /// \brief Parse the simulator version define:
464 /// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
465 // and return the grouped values as integers, e.g:
466 // __IPHONE_OS_VERSION_MIN_REQUIRED=40201
467 // will return Major=4, Minor=2, Micro=1.
468 static bool GetVersionFromSimulatorDefine(llvm::StringRef define,
469 unsigned &Major, unsigned &Minor,
471 assert(define.startswith(SimulatorVersionDefineName()));
472 llvm::StringRef name, version;
473 llvm::tie(name, version) = define.split('=');
476 std::string verstr = version.str();
478 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
488 void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
489 const OptTable &Opts = getDriver().getOpts();
491 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
492 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
493 Arg *iOSSimVersion = Args.getLastArg(
494 options::OPT_mios_simulator_version_min_EQ);
496 // FIXME: HACK! When compiling for the simulator we don't get a
497 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
498 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
499 // define passed in command-line.
501 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
502 ie = Args.filtered_end(); it != ie; ++it) {
503 llvm::StringRef define = (*it)->getValue(Args);
504 if (define.startswith(SimulatorVersionDefineName())) {
505 unsigned Major, Minor, Micro;
506 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
507 Major < 10 && Minor < 100 && Micro < 100) {
508 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
509 : ARCSimulator_HasARCRuntime;
516 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
517 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
518 << OSXVersion->getAsString(Args)
519 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
520 iOSVersion = iOSSimVersion = 0;
521 } else if (iOSVersion && iOSSimVersion) {
522 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
523 << iOSVersion->getAsString(Args)
524 << iOSSimVersion->getAsString(Args);
526 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
527 // If not deployment target was specified on the command line, check for
528 // environment defines.
529 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
530 const char *iOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
531 const char *iOSSimTarget = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET");
533 // Ignore empty strings.
534 if (OSXTarget && OSXTarget[0] == '\0')
536 if (iOSTarget && iOSTarget[0] == '\0')
538 if (iOSSimTarget && iOSSimTarget[0] == '\0')
541 // Handle conflicting deployment targets
543 // FIXME: Don't hardcode default here.
545 // Do not allow conflicts with the iOS simulator target.
546 if (iOSSimTarget && (OSXTarget || iOSTarget)) {
547 getDriver().Diag(clang::diag::err_drv_conflicting_deployment_targets)
548 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
549 << (OSXTarget ? "MACOSX_DEPLOYMENT_TARGET" :
550 "IPHONEOS_DEPLOYMENT_TARGET");
553 // Allow conflicts among OSX and iOS for historical reasons, but choose the
555 if (OSXTarget && iOSTarget) {
556 if (getTriple().getArch() == llvm::Triple::arm ||
557 getTriple().getArch() == llvm::Triple::thumb)
564 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
565 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
566 Args.append(OSXVersion);
567 } else if (iOSTarget) {
568 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
569 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
570 Args.append(iOSVersion);
571 } else if (iOSSimTarget) {
572 const Option *O = Opts.getOption(
573 options::OPT_mios_simulator_version_min_EQ);
574 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
575 Args.append(iOSSimVersion);
577 // Otherwise, assume we are targeting OS X.
578 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
579 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
580 Args.append(OSXVersion);
584 // Reject invalid architecture combinations.
585 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
586 getTriple().getArch() != llvm::Triple::x86_64)) {
587 getDriver().Diag(clang::diag::err_drv_invalid_arch_for_deployment_target)
588 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
591 // Set the tool chain target information.
592 unsigned Major, Minor, Micro;
595 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
596 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
597 Micro, HadExtra) || HadExtra ||
598 Major != 10 || Minor >= 100 || Micro >= 100)
599 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
600 << OSXVersion->getAsString(Args);
602 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
603 assert(Version && "Unknown target platform!");
604 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
605 Micro, HadExtra) || HadExtra ||
606 Major >= 10 || Minor >= 100 || Micro >= 100)
607 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
608 << Version->getAsString(Args);
611 bool IsIOSSim = bool(iOSSimVersion);
613 // In GCC, the simulator historically was treated as being OS X in some
614 // contexts, like determining the link logic, despite generally being called
615 // with an iOS deployment target. For compatibility, we detect the
616 // simulator as iOS + x86, and treat it differently in a few contexts.
617 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
618 getTriple().getArch() == llvm::Triple::x86_64))
621 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
624 void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
625 ArgStringList &CmdArgs) const {
626 CXXStdlibType Type = GetCXXStdlibType(Args);
629 case ToolChain::CST_Libcxx:
630 CmdArgs.push_back("-lc++");
633 case ToolChain::CST_Libstdcxx: {
634 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
635 // it was previously found in the gcc lib dir. However, for all the Darwin
636 // platforms we care about it was -lstdc++.6, so we search for that
637 // explicitly if we can't see an obvious -lstdc++ candidate.
639 // Check in the sysroot first.
641 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
642 llvm::sys::Path P(A->getValue(Args));
643 P.appendComponent("usr");
644 P.appendComponent("lib");
645 P.appendComponent("libstdc++.dylib");
647 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
649 P.appendComponent("libstdc++.6.dylib");
650 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
651 CmdArgs.push_back(Args.MakeArgString(P.str()));
657 // Otherwise, look in the root.
658 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
659 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
660 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
664 // Otherwise, let the linker search.
665 CmdArgs.push_back("-lstdc++");
671 void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
672 ArgStringList &CmdArgs) const {
674 // For Darwin platforms, use the compiler-rt-based support library
675 // instead of the gcc-provided one (which is also incidentally
676 // only present in the gcc lib dir, which makes it hard to find).
678 llvm::sys::Path P(getDriver().ResourceDir);
679 P.appendComponent("lib");
680 P.appendComponent("darwin");
681 P.appendComponent("libclang_rt.cc_kext.a");
683 // For now, allow missing resource libraries to support developers who may
684 // not have compiler-rt checked out or integrated into their build.
686 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
687 CmdArgs.push_back(Args.MakeArgString(P.str()));
690 DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
691 const char *BoundArch) const {
692 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
693 const OptTable &Opts = getDriver().getOpts();
695 // FIXME: We really want to get out of the tool chain level argument
696 // translation business, as it makes the driver functionality much
697 // more opaque. For now, we follow gcc closely solely for the
698 // purpose of easily achieving feature parity & testability. Once we
699 // have something that works, we should reevaluate each translation
700 // and try to push it down into tool specific logic.
702 for (ArgList::const_iterator it = Args.begin(),
703 ie = Args.end(); it != ie; ++it) {
706 if (A->getOption().matches(options::OPT_Xarch__)) {
707 // Skip this argument unless the architecture matches either the toolchain
708 // triple arch, or the arch being bound.
710 // FIXME: Canonicalize name.
711 llvm::StringRef XarchArch = A->getValue(Args, 0);
712 if (!(XarchArch == getArchName() ||
713 (BoundArch && XarchArch == BoundArch)))
716 Arg *OriginalArg = A;
717 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
718 unsigned Prev = Index;
719 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
721 // If the argument parsing failed or more than one argument was
722 // consumed, the -Xarch_ argument's parameter tried to consume
723 // extra arguments. Emit an error and ignore.
725 // We also want to disallow any options which would alter the
726 // driver behavior; that isn't going to work in our model. We
727 // use isDriverOption() as an approximation, although things
728 // like -O4 are going to slip through.
729 if (!XarchArg || Index > Prev + 1) {
730 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_with_args)
731 << A->getAsString(Args);
733 } else if (XarchArg->getOption().isDriverOption()) {
734 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_isdriver)
735 << A->getAsString(Args);
739 XarchArg->setBaseArg(A);
742 DAL->AddSynthesizedArg(A);
744 // Linker input arguments require custom handling. The problem is that we
745 // have already constructed the phase actions, so we can not treat them as
746 // "input arguments".
747 if (A->getOption().isLinkerInput()) {
748 // Convert the argument into individual Zlinker_input_args.
749 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
750 DAL->AddSeparateArg(OriginalArg,
751 Opts.getOption(options::OPT_Zlinker_input),
752 A->getValue(Args, i));
759 // Sob. These is strictly gcc compatible for the time being. Apple
760 // gcc translates options twice, which means that self-expanding
761 // options add duplicates.
762 switch ((options::ID) A->getOption().getID()) {
767 case options::OPT_mkernel:
768 case options::OPT_fapple_kext:
770 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
771 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
774 case options::OPT_dependency_file:
775 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
779 case options::OPT_gfull:
780 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
782 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
785 case options::OPT_gused:
786 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
788 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
791 case options::OPT_fterminated_vtables:
792 case options::OPT_findirect_virtual_calls:
793 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
794 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
797 case options::OPT_shared:
798 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
801 case options::OPT_fconstant_cfstrings:
802 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
805 case options::OPT_fno_constant_cfstrings:
806 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
809 case options::OPT_Wnonportable_cfstrings:
811 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
814 case options::OPT_Wno_nonportable_cfstrings:
816 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
819 case options::OPT_fpascal_strings:
820 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
823 case options::OPT_fno_pascal_strings:
824 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
829 if (getTriple().getArch() == llvm::Triple::x86 ||
830 getTriple().getArch() == llvm::Triple::x86_64)
831 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
832 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
834 // Add the arch options based on the particular spelling of -arch, to match
835 // how the driver driver works.
837 llvm::StringRef Name = BoundArch;
838 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
839 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
841 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
842 // which defines the list of which architectures we accept.
845 else if (Name == "ppc601")
846 DAL->AddJoinedArg(0, MCpu, "601");
847 else if (Name == "ppc603")
848 DAL->AddJoinedArg(0, MCpu, "603");
849 else if (Name == "ppc604")
850 DAL->AddJoinedArg(0, MCpu, "604");
851 else if (Name == "ppc604e")
852 DAL->AddJoinedArg(0, MCpu, "604e");
853 else if (Name == "ppc750")
854 DAL->AddJoinedArg(0, MCpu, "750");
855 else if (Name == "ppc7400")
856 DAL->AddJoinedArg(0, MCpu, "7400");
857 else if (Name == "ppc7450")
858 DAL->AddJoinedArg(0, MCpu, "7450");
859 else if (Name == "ppc970")
860 DAL->AddJoinedArg(0, MCpu, "970");
862 else if (Name == "ppc64")
863 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
865 else if (Name == "i386")
867 else if (Name == "i486")
868 DAL->AddJoinedArg(0, MArch, "i486");
869 else if (Name == "i586")
870 DAL->AddJoinedArg(0, MArch, "i586");
871 else if (Name == "i686")
872 DAL->AddJoinedArg(0, MArch, "i686");
873 else if (Name == "pentium")
874 DAL->AddJoinedArg(0, MArch, "pentium");
875 else if (Name == "pentium2")
876 DAL->AddJoinedArg(0, MArch, "pentium2");
877 else if (Name == "pentpro")
878 DAL->AddJoinedArg(0, MArch, "pentiumpro");
879 else if (Name == "pentIIm3")
880 DAL->AddJoinedArg(0, MArch, "pentium2");
882 else if (Name == "x86_64")
883 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
885 else if (Name == "arm")
886 DAL->AddJoinedArg(0, MArch, "armv4t");
887 else if (Name == "armv4t")
888 DAL->AddJoinedArg(0, MArch, "armv4t");
889 else if (Name == "armv5")
890 DAL->AddJoinedArg(0, MArch, "armv5tej");
891 else if (Name == "xscale")
892 DAL->AddJoinedArg(0, MArch, "xscale");
893 else if (Name == "armv6")
894 DAL->AddJoinedArg(0, MArch, "armv6k");
895 else if (Name == "armv7")
896 DAL->AddJoinedArg(0, MArch, "armv7a");
899 llvm_unreachable("invalid Darwin arch");
902 // Add an explicit version min argument for the deployment target. We do this
903 // after argument translation because -Xarch_ arguments may add a version min
905 AddDeploymentTarget(*DAL);
910 bool Darwin::IsUnwindTablesDefault() const {
911 // FIXME: Gross; we should probably have some separate target
912 // definition, possibly even reusing the one in clang.
913 return getArchName() == "x86_64";
916 bool Darwin::UseDwarfDebugFlags() const {
917 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
922 bool Darwin::UseSjLjExceptions() const {
923 // Darwin uses SjLj exceptions on ARM.
924 return (getTriple().getArch() == llvm::Triple::arm ||
925 getTriple().getArch() == llvm::Triple::thumb);
928 const char *Darwin::GetDefaultRelocationModel() const {
932 const char *Darwin::GetForcedPicModel() const {
933 if (getArchName() == "x86_64")
938 bool Darwin::SupportsProfiling() const {
939 // Profiling instrumentation is only supported on x86.
940 return getArchName() == "i386" || getArchName() == "x86_64";
943 bool Darwin::SupportsObjCGC() const {
944 // Garbage collection is supported everywhere except on iPhone OS.
945 return !isTargetIPhoneOS();
949 Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
950 return ComputeLLVMTriple(Args);
953 /// Generic_GCC - A tool chain using the 'gcc' command to perform
954 /// all subcommands; this relies on gcc translating the majority of
955 /// command line options.
957 Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
958 : ToolChain(Host, Triple) {
959 getProgramPaths().push_back(getDriver().getInstalledDir());
960 if (getDriver().getInstalledDir() != getDriver().Dir)
961 getProgramPaths().push_back(getDriver().Dir);
964 Generic_GCC::~Generic_GCC() {
965 // Free tool implementations.
966 for (llvm::DenseMap<unsigned, Tool*>::iterator
967 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
971 Tool &Generic_GCC::SelectTool(const Compilation &C,
973 const ActionList &Inputs) const {
974 Action::ActionClass Key;
975 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
976 Key = Action::AnalyzeJobClass;
980 Tool *&T = Tools[Key];
983 case Action::InputClass:
984 case Action::BindArchClass:
985 assert(0 && "Invalid tool kind.");
986 case Action::PreprocessJobClass:
987 T = new tools::gcc::Preprocess(*this); break;
988 case Action::PrecompileJobClass:
989 T = new tools::gcc::Precompile(*this); break;
990 case Action::AnalyzeJobClass:
991 T = new tools::Clang(*this); break;
992 case Action::CompileJobClass:
993 T = new tools::gcc::Compile(*this); break;
994 case Action::AssembleJobClass:
995 T = new tools::gcc::Assemble(*this); break;
996 case Action::LinkJobClass:
997 T = new tools::gcc::Link(*this); break;
999 // This is a bit ungeneric, but the only platform using a driver
1000 // driver is Darwin.
1001 case Action::LipoJobClass:
1002 T = new tools::darwin::Lipo(*this); break;
1003 case Action::DsymutilJobClass:
1004 T = new tools::darwin::Dsymutil(*this); break;
1011 bool Generic_GCC::IsUnwindTablesDefault() const {
1012 // FIXME: Gross; we should probably have some separate target
1013 // definition, possibly even reusing the one in clang.
1014 return getArchName() == "x86_64";
1017 const char *Generic_GCC::GetDefaultRelocationModel() const {
1021 const char *Generic_GCC::GetForcedPicModel() const {
1025 /// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1026 /// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1027 /// Currently does not support anything else but compilation.
1029 TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
1030 : ToolChain(Host, Triple) {
1031 // Path mangling to find libexec
1032 std::string Path(getDriver().Dir);
1034 Path += "/../libexec";
1035 getProgramPaths().push_back(Path);
1038 TCEToolChain::~TCEToolChain() {
1039 for (llvm::DenseMap<unsigned, Tool*>::iterator
1040 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1044 bool TCEToolChain::IsMathErrnoDefault() const {
1048 bool TCEToolChain::IsUnwindTablesDefault() const {
1052 const char *TCEToolChain::GetDefaultRelocationModel() const {
1056 const char *TCEToolChain::GetForcedPicModel() const {
1060 Tool &TCEToolChain::SelectTool(const Compilation &C,
1061 const JobAction &JA,
1062 const ActionList &Inputs) const {
1063 Action::ActionClass Key;
1064 Key = Action::AnalyzeJobClass;
1066 Tool *&T = Tools[Key];
1069 case Action::PreprocessJobClass:
1070 T = new tools::gcc::Preprocess(*this); break;
1071 case Action::AnalyzeJobClass:
1072 T = new tools::Clang(*this); break;
1074 assert(false && "Unsupported action for TCE target.");
1080 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1082 OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
1083 : Generic_ELF(Host, Triple) {
1084 getFilePaths().push_back(getDriver().Dir + "/../lib");
1085 getFilePaths().push_back("/usr/lib");
1088 Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1089 const ActionList &Inputs) const {
1090 Action::ActionClass Key;
1091 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1092 Key = Action::AnalyzeJobClass;
1096 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1097 options::OPT_no_integrated_as,
1098 IsIntegratedAssemblerDefault());
1100 Tool *&T = Tools[Key];
1103 case Action::AssembleJobClass: {
1104 if (UseIntegratedAs)
1105 T = new tools::ClangAs(*this);
1107 T = new tools::openbsd::Assemble(*this);
1110 case Action::LinkJobClass:
1111 T = new tools::openbsd::Link(*this); break;
1113 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1120 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1122 FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
1123 : Generic_ELF(Host, Triple) {
1125 // Determine if we are compiling 32-bit code on an x86_64 platform.
1127 if (Triple.getArch() == llvm::Triple::x86 &&
1128 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1129 llvm::Triple::x86_64)
1132 if (Triple.getArch() == llvm::Triple::ppc &&
1133 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1134 llvm::Triple::ppc64)
1138 getFilePaths().push_back(CLANG_PREFIX "/usr/lib32");
1140 getFilePaths().push_back(CLANG_PREFIX "/usr/lib");
1144 Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1145 const ActionList &Inputs) const {
1146 Action::ActionClass Key;
1147 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1148 Key = Action::AnalyzeJobClass;
1152 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1153 options::OPT_no_integrated_as,
1154 IsIntegratedAssemblerDefault());
1156 Tool *&T = Tools[Key];
1159 case Action::AssembleJobClass:
1160 if (UseIntegratedAs)
1161 T = new tools::ClangAs(*this);
1163 T = new tools::freebsd::Assemble(*this);
1165 case Action::LinkJobClass:
1166 T = new tools::freebsd::Link(*this); break;
1168 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1175 /// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1177 NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
1178 const llvm::Triple& ToolTriple)
1179 : Generic_ELF(Host, Triple), ToolTriple(ToolTriple) {
1181 // Determine if we are compiling 32-bit code on an x86_64 platform.
1183 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
1184 Triple.getArch() == llvm::Triple::x86)
1187 if (getDriver().UseStdLib) {
1189 getFilePaths().push_back("=/usr/lib/i386");
1191 getFilePaths().push_back("=/usr/lib");
1195 Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1196 const ActionList &Inputs) const {
1197 Action::ActionClass Key;
1198 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1199 Key = Action::AnalyzeJobClass;
1203 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1204 options::OPT_no_integrated_as,
1205 IsIntegratedAssemblerDefault());
1207 Tool *&T = Tools[Key];
1210 case Action::AssembleJobClass:
1211 if (UseIntegratedAs)
1212 T = new tools::ClangAs(*this);
1214 T = new tools::netbsd::Assemble(*this, ToolTriple);
1216 case Action::LinkJobClass:
1217 T = new tools::netbsd::Link(*this, ToolTriple);
1220 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1227 /// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1229 Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1230 : Generic_GCC(Host, Triple) {
1231 getFilePaths().push_back(getDriver().Dir + "/../lib");
1232 getFilePaths().push_back("/usr/lib");
1233 getFilePaths().push_back("/usr/gnu/lib");
1234 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1237 Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1238 const ActionList &Inputs) const {
1239 Action::ActionClass Key;
1240 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1241 Key = Action::AnalyzeJobClass;
1245 Tool *&T = Tools[Key];
1248 case Action::AssembleJobClass:
1249 T = new tools::minix::Assemble(*this); break;
1250 case Action::LinkJobClass:
1251 T = new tools::minix::Link(*this); break;
1253 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1260 /// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1262 AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1263 : Generic_GCC(Host, Triple) {
1265 getProgramPaths().push_back(getDriver().getInstalledDir());
1266 if (getDriver().getInstalledDir() != getDriver().Dir)
1267 getProgramPaths().push_back(getDriver().Dir);
1269 getFilePaths().push_back(getDriver().Dir + "/../lib");
1270 getFilePaths().push_back("/usr/lib");
1271 getFilePaths().push_back("/usr/sfw/lib");
1272 getFilePaths().push_back("/opt/gcc4/lib");
1273 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
1277 Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1278 const ActionList &Inputs) const {
1279 Action::ActionClass Key;
1280 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1281 Key = Action::AnalyzeJobClass;
1285 Tool *&T = Tools[Key];
1288 case Action::AssembleJobClass:
1289 T = new tools::auroraux::Assemble(*this); break;
1290 case Action::LinkJobClass:
1291 T = new tools::auroraux::Link(*this); break;
1293 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1301 /// Linux toolchain (very bare-bones at the moment).
1330 static bool IsRedhat(enum LinuxDistro Distro) {
1331 return Distro == Fedora13 || Distro == Fedora14 ||
1332 Distro == Fedora15 || Distro == FedoraRawhide ||
1333 Distro == RHEL4 || Distro == RHEL5 || Distro == RHEL6;
1336 static bool IsOpenSuse(enum LinuxDistro Distro) {
1337 return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1338 Distro == OpenSuse12_1;
1341 static bool IsDebian(enum LinuxDistro Distro) {
1342 return Distro == DebianLenny || Distro == DebianSqueeze ||
1343 Distro == DebianWheezy;
1346 static bool IsUbuntu(enum LinuxDistro Distro) {
1347 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
1348 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
1349 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
1350 Distro == UbuntuNatty || Distro == UbuntuOneiric;
1353 static bool IsDebianBased(enum LinuxDistro Distro) {
1354 return IsDebian(Distro) || IsUbuntu(Distro);
1357 static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
1358 if (Arch == llvm::Triple::x86_64) {
1360 if (Distro == Exherbo &&
1361 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
1366 if (Arch == llvm::Triple::ppc64)
1368 if ((Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) &&
1369 IsDebianBased(Distro))
1374 static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
1375 llvm::OwningPtr<llvm::MemoryBuffer> File;
1376 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1377 llvm::StringRef Data = File.get()->getBuffer();
1378 llvm::SmallVector<llvm::StringRef, 8> Lines;
1379 Data.split(Lines, "\n");
1380 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
1381 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1383 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1384 return UbuntuIntrepid;
1385 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
1386 return UbuntuJaunty;
1387 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1388 return UbuntuKarmic;
1389 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1391 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1392 return UbuntuMaverick;
1393 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1395 else if (Lines[i] == "DISTRIB_CODENAME=oneiric")
1396 return UbuntuOneiric;
1398 return UnknownDistro;
1401 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1402 llvm::StringRef Data = File.get()->getBuffer();
1403 if (Data.startswith("Fedora release 15"))
1405 else if (Data.startswith("Fedora release 14"))
1407 else if (Data.startswith("Fedora release 13"))
1409 else if (Data.startswith("Fedora release") &&
1410 Data.find("Rawhide") != llvm::StringRef::npos)
1411 return FedoraRawhide;
1412 else if (Data.startswith("Red Hat Enterprise Linux") &&
1413 Data.find("release 6") != llvm::StringRef::npos)
1415 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1416 Data.startswith("CentOS")) &&
1417 Data.find("release 5") != llvm::StringRef::npos)
1419 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1420 Data.startswith("CentOS")) &&
1421 Data.find("release 4") != llvm::StringRef::npos)
1423 return UnknownDistro;
1426 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1427 llvm::StringRef Data = File.get()->getBuffer();
1430 else if (Data.startswith("squeeze/sid"))
1431 return DebianSqueeze;
1432 else if (Data.startswith("wheezy/sid"))
1433 return DebianWheezy;
1434 return UnknownDistro;
1437 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1438 llvm::StringRef Data = File.get()->getBuffer();
1439 if (Data.startswith("openSUSE 11.3"))
1440 return OpenSuse11_3;
1441 else if (Data.startswith("openSUSE 11.4"))
1442 return OpenSuse11_4;
1443 else if (Data.startswith("openSUSE 12.1"))
1444 return OpenSuse12_1;
1445 return UnknownDistro;
1449 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
1452 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1455 return UnknownDistro;
1458 static std::string findGCCBaseLibDir(const std::string &GccTriple) {
1459 // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
1460 // avoids adding yet another option to configure/cmake.
1461 // It would probably be cleaner to break it in two variables
1462 // CXX_GCC_ROOT with just /foo/bar
1463 // CXX_GCC_VER with 4.5.2
1464 // Then we would have
1465 // CXX_INCLUDE_ROOT = CXX_GCC_ROOT/include/c++/CXX_GCC_VER
1466 // and this function would return
1467 // CXX_GCC_ROOT/lib/gcc/CXX_INCLUDE_ARCH/CXX_GCC_VER
1468 llvm::SmallString<128> CxxIncludeRoot(CXX_INCLUDE_ROOT);
1469 if (CxxIncludeRoot != "") {
1470 // This is of the form /foo/bar/include/c++/4.5.2/
1471 if (CxxIncludeRoot.back() == '/')
1472 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the /
1473 llvm::StringRef Version = llvm::sys::path::filename(CxxIncludeRoot);
1474 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the version
1475 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the c++
1476 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the include
1477 std::string ret(CxxIncludeRoot.c_str());
1478 ret.append("/lib/gcc/");
1479 ret.append(CXX_INCLUDE_ARCH);
1481 ret.append(Version);
1484 static const char* GccVersions[] = {"4.6.1", "4.6.0", "4.6",
1485 "4.5.2", "4.5.1", "4.5",
1486 "4.4.5", "4.4.4", "4.4.3", "4.4",
1487 "4.3.4", "4.3.3", "4.3.2", "4.3",
1488 "4.2.4", "4.2.3", "4.2.2", "4.2.1",
1491 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1492 std::string Suffix = GccTriple + "/" + GccVersions[i];
1493 std::string t1 = "/usr/lib/gcc/" + Suffix;
1494 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists)
1496 std::string t2 = "/usr/lib64/gcc/" + Suffix;
1497 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
1499 std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
1500 if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
1506 Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
1507 : Generic_ELF(Host, Triple) {
1508 llvm::Triple::ArchType Arch =
1509 llvm::Triple(getDriver().DefaultHostTriple).getArch();
1511 std::string Suffix32 = "";
1512 if (Arch == llvm::Triple::x86_64)
1515 std::string Suffix64 = "";
1516 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
1519 std::string Lib32 = "lib";
1522 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
1525 std::string Lib64 = "lib";
1527 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1528 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
1531 std::string GccTriple = "";
1532 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
1533 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1535 GccTriple = "arm-linux-gnueabi";
1536 } else if (Arch == llvm::Triple::x86_64) {
1537 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1539 GccTriple = "x86_64-linux-gnu";
1540 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1542 GccTriple = "x86_64-unknown-linux-gnu";
1543 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1545 GccTriple = "x86_64-pc-linux-gnu";
1546 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux6E",
1548 GccTriple = "x86_64-redhat-linux6E";
1549 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1551 GccTriple = "x86_64-redhat-linux";
1552 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1554 GccTriple = "x86_64-suse-linux";
1555 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1557 GccTriple = "x86_64-manbo-linux-gnu";
1558 else if (!llvm::sys::fs::exists("/usr/lib/x86_64-linux-gnu/gcc",
1560 GccTriple = "x86_64-linux-gnu";
1561 } else if (Arch == llvm::Triple::x86) {
1562 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
1563 GccTriple = "i686-linux-gnu";
1564 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1566 GccTriple = "i686-pc-linux-gnu";
1567 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1569 GccTriple = "i486-linux-gnu";
1570 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1572 GccTriple = "i686-redhat-linux";
1573 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1575 GccTriple = "i586-suse-linux";
1576 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-slackware-linux", Exists)
1578 GccTriple = "i486-slackware-linux";
1579 } else if (Arch == llvm::Triple::ppc) {
1580 if (!llvm::sys::fs::exists("/usr/lib/powerpc-linux-gnu", Exists) && Exists)
1581 GccTriple = "powerpc-linux-gnu";
1582 else if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc-unknown-linux-gnu",
1584 GccTriple = "powerpc-unknown-linux-gnu";
1585 } else if (Arch == llvm::Triple::ppc64) {
1586 if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc64-unknown-linux-gnu",
1588 GccTriple = "powerpc64-unknown-linux-gnu";
1589 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/"
1590 "powerpc64-unknown-linux-gnu", Exists) &&
1592 GccTriple = "powerpc64-unknown-linux-gnu";
1595 std::string Base = findGCCBaseLibDir(GccTriple);
1596 path_list &Paths = getFilePaths();
1597 bool Is32Bits = (getArch() == llvm::Triple::x86 ||
1598 getArch() == llvm::Triple::ppc);
1611 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
1612 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
1613 Linker = LinkerPath.str();
1615 Linker = GetProgramPath("ld");
1617 LinuxDistro Distro = DetectLinuxDistro(Arch);
1619 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
1620 ExtraOpts.push_back("-z");
1621 ExtraOpts.push_back("relro");
1624 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
1625 ExtraOpts.push_back("-X");
1627 if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
1628 Distro == UbuntuNatty || Distro == UbuntuOneiric)
1629 ExtraOpts.push_back("--hash-style=gnu");
1631 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
1632 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
1633 ExtraOpts.push_back("--hash-style=both");
1635 if (IsRedhat(Distro))
1636 ExtraOpts.push_back("--no-add-needed");
1638 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
1639 IsOpenSuse(Distro) ||
1640 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
1641 Distro == UbuntuLucid ||
1642 Distro == UbuntuMaverick || Distro == UbuntuKarmic ||
1643 Distro == UbuntuNatty || Distro == UbuntuOneiric)
1644 ExtraOpts.push_back("--build-id");
1646 if (IsOpenSuse(Distro))
1647 ExtraOpts.push_back("--enable-new-dtags");
1649 if (Distro == ArchLinux)
1652 Paths.push_back(Base + Suffix);
1653 if (HasMultilib(Arch, Distro)) {
1654 if (IsOpenSuse(Distro) && Is32Bits)
1655 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1656 Paths.push_back(Base + "/../../../../" + Lib);
1659 // FIXME: This is in here to find crt1.o. It is provided by libc, and
1660 // libc (like gcc), can be installed in any directory. Once we are
1661 // fetching this from a config file, we should have a libc prefix.
1662 Paths.push_back("/lib/../" + Lib);
1663 Paths.push_back("/usr/lib/../" + Lib);
1665 if (!Suffix.empty())
1666 Paths.push_back(Base);
1667 if (IsOpenSuse(Distro))
1668 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1669 Paths.push_back(Base + "/../../..");
1670 if (Arch == getArch() && IsUbuntu(Distro))
1671 Paths.push_back("/usr/lib/" + GccTriple);
1674 bool Linux::HasNativeLLVMSupport() const {
1678 Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1679 const ActionList &Inputs) const {
1680 Action::ActionClass Key;
1681 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1682 Key = Action::AnalyzeJobClass;
1686 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1687 options::OPT_no_integrated_as,
1688 IsIntegratedAssemblerDefault());
1690 Tool *&T = Tools[Key];
1693 case Action::AssembleJobClass:
1694 if (UseIntegratedAs)
1695 T = new tools::ClangAs(*this);
1697 T = new tools::linuxtools::Assemble(*this);
1699 case Action::LinkJobClass:
1700 T = new tools::linuxtools::Link(*this); break;
1702 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1709 /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1711 DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1712 : Generic_ELF(Host, Triple) {
1714 // Path mangling to find libexec
1715 getProgramPaths().push_back(getDriver().getInstalledDir());
1716 if (getDriver().getInstalledDir() != getDriver().Dir)
1717 getProgramPaths().push_back(getDriver().Dir);
1719 getFilePaths().push_back(getDriver().Dir + "/../lib");
1720 getFilePaths().push_back("/usr/lib");
1721 getFilePaths().push_back("/usr/lib/gcc41");
1724 Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1725 const ActionList &Inputs) const {
1726 Action::ActionClass Key;
1727 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1728 Key = Action::AnalyzeJobClass;
1732 Tool *&T = Tools[Key];
1735 case Action::AssembleJobClass:
1736 T = new tools::dragonfly::Assemble(*this); break;
1737 case Action::LinkJobClass:
1738 T = new tools::dragonfly::Link(*this); break;
1740 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1747 Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1748 : ToolChain(Host, Triple) {
1751 Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1752 const ActionList &Inputs) const {
1753 Action::ActionClass Key;
1754 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1755 Key = Action::AnalyzeJobClass;
1759 Tool *&T = Tools[Key];
1762 case Action::InputClass:
1763 case Action::BindArchClass:
1764 case Action::LipoJobClass:
1765 case Action::DsymutilJobClass:
1766 assert(0 && "Invalid tool kind.");
1767 case Action::PreprocessJobClass:
1768 case Action::PrecompileJobClass:
1769 case Action::AnalyzeJobClass:
1770 case Action::CompileJobClass:
1771 T = new tools::Clang(*this); break;
1772 case Action::AssembleJobClass:
1773 T = new tools::ClangAs(*this); break;
1774 case Action::LinkJobClass:
1775 T = new tools::visualstudio::Link(*this); break;
1782 bool Windows::IsIntegratedAssemblerDefault() const {
1786 bool Windows::IsUnwindTablesDefault() const {
1787 // FIXME: Gross; we should probably have some separate target
1788 // definition, possibly even reusing the one in clang.
1789 return getArchName() == "x86_64";
1792 const char *Windows::GetDefaultRelocationModel() const {
1796 const char *Windows::GetForcedPicModel() const {
1797 if (getArchName() == "x86_64")