]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Driver/ToolChains.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Driver / ToolChains.cpp
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 #ifdef HAVE_CLANG_CONFIG_H
13 # include "clang/Config/config.h"
14 #endif
15
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"
27
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"
37
38 #include <cstdlib> // ::getenv
39
40 #include "llvm/Config/config.h" // for CXX_INCLUDE_ROOT
41
42 #ifndef CLANG_PREFIX
43 #define CLANG_PREFIX
44 #endif
45
46 using namespace clang::driver;
47 using namespace clang::driver::toolchains;
48
49 /// Darwin - Darwin tool chain for i386 and x86_64.
50
51 Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
52   : ToolChain(Host, Triple), TargetInitialized(false),
53     ARCRuntimeForSimulator(ARCSimulator_None)
54 {
55   // Compute the initial Darwin version based on the host.
56   bool HadExtra;
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;
62
63   llvm::raw_string_ostream(MacosxVersionMin)
64     << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
65     << DarwinVersion[1];
66 }
67
68 types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
69   types::ID Ty = types::lookupTypeForExtension(Ext);
70
71   // Darwin always preprocesses assembly files (unless -x is used explicitly).
72   if (Ty == types::TY_PP_Asm)
73     return types::TY_Asm;
74
75   return Ty;
76 }
77
78 bool Darwin::HasNativeLLVMSupport() const {
79   return true;
80 }
81
82 bool Darwin::hasARCRuntime() const {
83   // FIXME: Remove this once there is a proper way to detect an ARC runtime
84   // for the simulator.
85   switch (ARCRuntimeForSimulator) {
86   case ARCSimulator_None:
87     break;
88   case ARCSimulator_HasARCRuntime:
89     return true;
90   case ARCSimulator_NoARCRuntime:
91     return false;
92   }
93
94   if (isTargetIPhoneOS())
95     return !isIPhoneOSVersionLT(5);
96   else
97     return !isMacosxVersionLT(10, 7);
98 }
99
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);
104
105   runtime.HasARC = runtime.HasWeak = hasARCRuntime();
106
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);
111   else
112     runtime.HasTerminate = false;
113 }
114
115 // FIXME: Can we tablegen this?
116 static const char *GetArmArchForMArch(llvm::StringRef Value) {
117   if (Value == "armv6k")
118     return "armv6";
119
120   if (Value == "armv5tej")
121     return "armv5";
122
123   if (Value == "xscale")
124     return "xscale";
125
126   if (Value == "armv4t")
127     return "armv4t";
128
129   if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
130       Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
131       Value == "armv7m")
132     return "armv7";
133
134   return 0;
135 }
136
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")
144     return "armv5";
145
146   if (Value == "xscale")
147     return "xscale";
148
149   if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
150       Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
151       Value == "cortex-m0" )
152     return "armv6";
153
154   if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
155     return "armv7";
156
157   return 0;
158 }
159
160 llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
161   switch (getTriple().getArch()) {
162   default:
163     return getArchName();
164
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)))
169         return Arch;
170
171     if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
172       if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
173         return Arch;
174
175     return "arm";
176   }
177   }
178 }
179
180 Darwin::~Darwin() {
181   // Free tool implementations.
182   for (llvm::DenseMap<unsigned, Tool*>::iterator
183          it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
184     delete it->second;
185 }
186
187 std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
188   llvm::Triple Triple(ComputeLLVMTriple(Args));
189
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();
194
195   unsigned Version[3];
196   getTargetVersion(Version);
197
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());
203
204   return Triple.getTriple();
205 }
206
207 Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
208                          const ActionList &Inputs) const {
209   Action::ActionClass Key;
210
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))
218       Key = JA.getKind();
219     else
220       Key = Action::AnalyzeJobClass;
221   } else
222     Key = JA.getKind();
223
224   // FIXME: This doesn't belong here, but ideally we will support static soon
225   // anyway.
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,
232                                              IsIADefault);
233
234   Tool *&T = Tools[Key];
235   if (!T) {
236     switch (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: {
248       if (UseIntegratedAs)
249         T = new tools::ClangAs(*this);
250       else
251         T = new tools::darwin::Assemble(*this);
252       break;
253     }
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;
260     }
261   }
262
263   return *T;
264 }
265
266
267 DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
268   : Darwin(Host, Triple)
269 {
270   std::string UsrPrefix = "llvm-gcc-4.2/";
271
272   getProgramPaths().push_back(getDriver().getInstalledDir());
273   if (getDriver().getInstalledDir() != getDriver().Dir)
274     getProgramPaths().push_back(getDriver().Dir);
275
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);
280
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";
287
288   std::string Path = getDriver().Dir;
289   Path += "/../" + UsrPrefix + "libexec/gcc/";
290   Path += ToolChainDir;
291   getProgramPaths().push_back(Path);
292
293   Path = "/usr/" + UsrPrefix + "libexec/gcc/";
294   Path += ToolChainDir;
295   getProgramPaths().push_back(Path);
296 }
297
298 void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
299                                        ArgStringList &CmdArgs) const {
300   // The Clang toolchain uses explicit paths for internal libraries.
301
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()) {
310   default:
311     assert(0 && "Invalid Darwin arch!");
312   case llvm::Triple::x86:
313   case llvm::Triple::x86_64:
314     P.appendComponent("i686-apple-darwin10");
315     break;
316   case llvm::Triple::arm:
317   case llvm::Triple::thumb:
318     P.appendComponent("arm-apple-darwin10");
319     break;
320   case llvm::Triple::ppc:
321   case llvm::Triple::ppc64:
322     P.appendComponent("powerpc-apple-darwin10");
323     break;
324   }
325   P.appendComponent("4.2.1");
326
327   // Determine the arch specific GCC subdirectory.
328   const char *ArchSpecificDir = 0;
329   switch (getTriple().getArch()) {
330   default:
331     break;
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";
342     break;
343   }
344   case llvm::Triple::ppc64:
345     ArchSpecificDir = "ppc64";
346     break;
347   case llvm::Triple::x86_64:
348     ArchSpecificDir = "x86_64";
349     break;
350   }
351
352   if (ArchSpecificDir) {
353     P.appendComponent(ArchSpecificDir);
354     bool Exists;
355     if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
356       CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
357     P.eraseComponent();
358   }
359
360   bool Exists;
361   if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
362     CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
363 }
364
365 void DarwinClang::AddLinkARCArgs(const ArgList &Args,
366                                  ArgStringList &CmdArgs) const {
367   
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())
378     s += "iphoneos";
379   // FIXME: isTargetIphoneOSSimulator() is not returning true.
380   else if (ARCRuntimeForSimulator != ARCSimulator_None)
381     s += "iphonesimulator";
382   else
383     s += "macosx";
384   s += ".a";
385
386   CmdArgs.push_back(Args.MakeArgString(s));
387 }
388
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);
396   
397   // For now, allow missing resource libraries to support developers who may
398   // not have compiler-rt checked out or integrated into their build.
399   bool Exists;
400   if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
401     CmdArgs.push_back(Args.MakeArgString(P.str()));
402 }
403
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))
409     return;
410
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);
417     return;
418   }
419
420   // Otherwise link libSystem, then the dynamic runtime library, and finally any
421   // target specific static runtime library.
422   CmdArgs.push_back("-lSystem");
423
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");
430
431     // We currently always need a static runtime library for iOS.
432     AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
433   } else {
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");
440
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.
444     //
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");
451     } else {
452       if (getTriple().getArch() == llvm::Triple::x86)
453         AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
454       AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
455     }
456   }
457 }
458
459 static inline llvm::StringRef SimulatorVersionDefineName() {
460   return "__IPHONE_OS_VERSION_MIN_REQUIRED";
461 }
462
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,
470                                           unsigned &Micro) {
471   assert(define.startswith(SimulatorVersionDefineName()));
472   llvm::StringRef name, version;
473   llvm::tie(name, version) = define.split('=');
474   if (version.empty())
475     return false;
476   std::string verstr = version.str();
477   char *end;
478   unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
479   if (*end != '\0')
480     return false;
481   Major = num / 10000;
482   num = num % 10000;
483   Minor = num / 100;
484   Micro = num % 100;
485   return true;
486 }
487
488 void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
489   const OptTable &Opts = getDriver().getOpts();
490
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);
495
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.
500   if (!iOSVersion) {
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;
510         }
511         break;
512       }
513     }
514   }
515
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);
525     iOSSimVersion = 0;
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");
532
533     // Ignore empty strings.
534     if (OSXTarget && OSXTarget[0] == '\0')
535       OSXTarget = 0;
536     if (iOSTarget && iOSTarget[0] == '\0')
537       iOSTarget = 0;
538     if (iOSSimTarget && iOSSimTarget[0] == '\0')
539       iOSSimTarget = 0;
540
541     // Handle conflicting deployment targets
542     //
543     // FIXME: Don't hardcode default here.
544
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");
551     }
552
553     // Allow conflicts among OSX and iOS for historical reasons, but choose the
554     // default platform.
555     if (OSXTarget && iOSTarget) {
556       if (getTriple().getArch() == llvm::Triple::arm ||
557           getTriple().getArch() == llvm::Triple::thumb)
558         OSXTarget = 0;
559       else
560         iOSTarget = 0;
561     }
562
563     if (OSXTarget) {
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);
576     } else {
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);
581     }
582   }
583
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);
589   }
590
591   // Set the tool chain target information.
592   unsigned Major, Minor, Micro;
593   bool HadExtra;
594   if (OSXVersion) {
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);
601   } else {
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);
609   }
610
611   bool IsIOSSim = bool(iOSSimVersion);
612
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))
619     IsIOSSim = true;
620
621   setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
622 }
623
624 void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
625                                       ArgStringList &CmdArgs) const {
626   CXXStdlibType Type = GetCXXStdlibType(Args);
627
628   switch (Type) {
629   case ToolChain::CST_Libcxx:
630     CmdArgs.push_back("-lc++");
631     break;
632
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.
638
639     // Check in the sysroot first.
640     bool Exists;
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");
646
647       if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
648         P.eraseComponent();
649         P.appendComponent("libstdc++.6.dylib");
650         if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
651           CmdArgs.push_back(Args.MakeArgString(P.str()));
652           return;
653         }
654       }
655     }
656
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");
661       return;
662     }
663
664     // Otherwise, let the linker search.
665     CmdArgs.push_back("-lstdc++");
666     break;
667   }
668   }
669 }
670
671 void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
672                                    ArgStringList &CmdArgs) const {
673
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).
677
678   llvm::sys::Path P(getDriver().ResourceDir);
679   P.appendComponent("lib");
680   P.appendComponent("darwin");
681   P.appendComponent("libclang_rt.cc_kext.a");
682
683   // For now, allow missing resource libraries to support developers who may
684   // not have compiler-rt checked out or integrated into their build.
685   bool Exists;
686   if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
687     CmdArgs.push_back(Args.MakeArgString(P.str()));
688 }
689
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();
694
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.
701
702   for (ArgList::const_iterator it = Args.begin(),
703          ie = Args.end(); it != ie; ++it) {
704     Arg *A = *it;
705
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.
709       //
710       // FIXME: Canonicalize name.
711       llvm::StringRef XarchArch = A->getValue(Args, 0);
712       if (!(XarchArch == getArchName()  ||
713             (BoundArch && XarchArch == BoundArch)))
714         continue;
715
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);
720
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.
724       //
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);
732         continue;
733       } else if (XarchArg->getOption().isDriverOption()) {
734         getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_isdriver)
735           << A->getAsString(Args);
736         continue;
737       }
738
739       XarchArg->setBaseArg(A);
740       A = XarchArg;
741
742       DAL->AddSynthesizedArg(A);
743
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));
753
754         }
755         continue;
756       }
757     }
758
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()) {
763     default:
764       DAL->append(A);
765       break;
766
767     case options::OPT_mkernel:
768     case options::OPT_fapple_kext:
769       DAL->append(A);
770       DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
771       DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
772       break;
773
774     case options::OPT_dependency_file:
775       DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
776                           A->getValue(Args));
777       break;
778
779     case options::OPT_gfull:
780       DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
781       DAL->AddFlagArg(A,
782                Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
783       break;
784
785     case options::OPT_gused:
786       DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
787       DAL->AddFlagArg(A,
788              Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
789       break;
790
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));
795       break;
796
797     case options::OPT_shared:
798       DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
799       break;
800
801     case options::OPT_fconstant_cfstrings:
802       DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
803       break;
804
805     case options::OPT_fno_constant_cfstrings:
806       DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
807       break;
808
809     case options::OPT_Wnonportable_cfstrings:
810       DAL->AddFlagArg(A,
811                       Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
812       break;
813
814     case options::OPT_Wno_nonportable_cfstrings:
815       DAL->AddFlagArg(A,
816                    Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
817       break;
818
819     case options::OPT_fpascal_strings:
820       DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
821       break;
822
823     case options::OPT_fno_pascal_strings:
824       DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
825       break;
826     }
827   }
828
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");
833
834   // Add the arch options based on the particular spelling of -arch, to match
835   // how the driver driver works.
836   if (BoundArch) {
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);
840
841     // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
842     // which defines the list of which architectures we accept.
843     if (Name == "ppc")
844       ;
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");
861
862     else if (Name == "ppc64")
863       DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
864
865     else if (Name == "i386")
866       ;
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");
881
882     else if (Name == "x86_64")
883       DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
884
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");
897
898     else
899       llvm_unreachable("invalid Darwin arch");
900   }
901
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
904   // argument.
905   AddDeploymentTarget(*DAL);
906
907   return DAL;
908 }
909
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";
914 }
915
916 bool Darwin::UseDwarfDebugFlags() const {
917   if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
918     return S[0] != '\0';
919   return false;
920 }
921
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);
926 }
927
928 const char *Darwin::GetDefaultRelocationModel() const {
929   return "pic";
930 }
931
932 const char *Darwin::GetForcedPicModel() const {
933   if (getArchName() == "x86_64")
934     return "pic";
935   return 0;
936 }
937
938 bool Darwin::SupportsProfiling() const {
939   // Profiling instrumentation is only supported on x86.
940   return getArchName() == "i386" || getArchName() == "x86_64";
941 }
942
943 bool Darwin::SupportsObjCGC() const {
944   // Garbage collection is supported everywhere except on iPhone OS.
945   return !isTargetIPhoneOS();
946 }
947
948 std::string
949 Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
950   return ComputeLLVMTriple(Args);
951 }
952
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.
956
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);
962 }
963
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)
968     delete it->second;
969 }
970
971 Tool &Generic_GCC::SelectTool(const Compilation &C,
972                               const JobAction &JA,
973                               const ActionList &Inputs) const {
974   Action::ActionClass Key;
975   if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
976     Key = Action::AnalyzeJobClass;
977   else
978     Key = JA.getKind();
979
980   Tool *&T = Tools[Key];
981   if (!T) {
982     switch (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;
998
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;
1005     }
1006   }
1007
1008   return *T;
1009 }
1010
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";
1015 }
1016
1017 const char *Generic_GCC::GetDefaultRelocationModel() const {
1018   return "static";
1019 }
1020
1021 const char *Generic_GCC::GetForcedPicModel() const {
1022   return 0;
1023 }
1024
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.
1028
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);
1033
1034   Path += "/../libexec";
1035   getProgramPaths().push_back(Path);
1036 }
1037
1038 TCEToolChain::~TCEToolChain() {
1039   for (llvm::DenseMap<unsigned, Tool*>::iterator
1040            it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1041       delete it->second;
1042 }
1043
1044 bool TCEToolChain::IsMathErrnoDefault() const {
1045   return true;
1046 }
1047
1048 bool TCEToolChain::IsUnwindTablesDefault() const {
1049   return false;
1050 }
1051
1052 const char *TCEToolChain::GetDefaultRelocationModel() const {
1053   return "static";
1054 }
1055
1056 const char *TCEToolChain::GetForcedPicModel() const {
1057   return 0;
1058 }
1059
1060 Tool &TCEToolChain::SelectTool(const Compilation &C,
1061                             const JobAction &JA,
1062                                const ActionList &Inputs) const {
1063   Action::ActionClass Key;
1064   Key = Action::AnalyzeJobClass;
1065
1066   Tool *&T = Tools[Key];
1067   if (!T) {
1068     switch (Key) {
1069     case Action::PreprocessJobClass:
1070       T = new tools::gcc::Preprocess(*this); break;
1071     case Action::AnalyzeJobClass:
1072       T = new tools::Clang(*this); break;
1073     default:
1074      assert(false && "Unsupported action for TCE target.");
1075     }
1076   }
1077   return *T;
1078 }
1079
1080 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1081
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");
1086 }
1087
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;
1093   else
1094     Key = JA.getKind();
1095
1096   bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1097                                              options::OPT_no_integrated_as,
1098                                              IsIntegratedAssemblerDefault());
1099
1100   Tool *&T = Tools[Key];
1101   if (!T) {
1102     switch (Key) {
1103     case Action::AssembleJobClass: {
1104       if (UseIntegratedAs)
1105         T = new tools::ClangAs(*this);
1106       else
1107         T = new tools::openbsd::Assemble(*this);
1108       break;
1109     }
1110     case Action::LinkJobClass:
1111       T = new tools::openbsd::Link(*this); break;
1112     default:
1113       T = &Generic_GCC::SelectTool(C, JA, Inputs);
1114     }
1115   }
1116
1117   return *T;
1118 }
1119
1120 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1121
1122 FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
1123   : Generic_ELF(Host, Triple) {
1124
1125   // Determine if we are compiling 32-bit code on an x86_64 platform.
1126   bool Lib32 = false;
1127   if (Triple.getArch() == llvm::Triple::x86 &&
1128       llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1129         llvm::Triple::x86_64)
1130     Lib32 = true;
1131
1132   if (Triple.getArch() == llvm::Triple::ppc &&
1133       llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1134         llvm::Triple::ppc64)
1135     Lib32 = true;
1136
1137   if (Lib32) {
1138     getFilePaths().push_back(CLANG_PREFIX "/usr/lib32");
1139   } else {
1140     getFilePaths().push_back(CLANG_PREFIX "/usr/lib");
1141   }
1142 }
1143
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;
1149   else
1150     Key = JA.getKind();
1151
1152   bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1153                                              options::OPT_no_integrated_as,
1154                                              IsIntegratedAssemblerDefault());
1155
1156   Tool *&T = Tools[Key];
1157   if (!T) {
1158     switch (Key) {
1159     case Action::AssembleJobClass:
1160       if (UseIntegratedAs)
1161         T = new tools::ClangAs(*this);
1162       else
1163         T = new tools::freebsd::Assemble(*this);
1164       break;
1165     case Action::LinkJobClass:
1166       T = new tools::freebsd::Link(*this); break;
1167     default:
1168       T = &Generic_GCC::SelectTool(C, JA, Inputs);
1169     }
1170   }
1171
1172   return *T;
1173 }
1174
1175 /// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1176
1177 NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
1178                const llvm::Triple& ToolTriple)
1179   : Generic_ELF(Host, Triple), ToolTriple(ToolTriple) {
1180
1181   // Determine if we are compiling 32-bit code on an x86_64 platform.
1182   bool Lib32 = false;
1183   if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
1184       Triple.getArch() == llvm::Triple::x86)
1185     Lib32 = true;
1186
1187   if (getDriver().UseStdLib) {
1188     if (Lib32)
1189       getFilePaths().push_back("=/usr/lib/i386");
1190     else
1191       getFilePaths().push_back("=/usr/lib");
1192   }
1193 }
1194
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;
1200   else
1201     Key = JA.getKind();
1202
1203   bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1204                                              options::OPT_no_integrated_as,
1205                                              IsIntegratedAssemblerDefault());
1206
1207   Tool *&T = Tools[Key];
1208   if (!T) {
1209     switch (Key) {
1210     case Action::AssembleJobClass:
1211       if (UseIntegratedAs)
1212         T = new tools::ClangAs(*this);
1213       else
1214         T = new tools::netbsd::Assemble(*this, ToolTriple);
1215       break;
1216     case Action::LinkJobClass:
1217       T = new tools::netbsd::Link(*this, ToolTriple);
1218       break;
1219     default:
1220       T = &Generic_GCC::SelectTool(C, JA, Inputs);
1221     }
1222   }
1223
1224   return *T;
1225 }
1226
1227 /// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1228
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");
1235 }
1236
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;
1242   else
1243     Key = JA.getKind();
1244
1245   Tool *&T = Tools[Key];
1246   if (!T) {
1247     switch (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;
1252     default:
1253       T = &Generic_GCC::SelectTool(C, JA, Inputs);
1254     }
1255   }
1256
1257   return *T;
1258 }
1259
1260 /// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1261
1262 AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1263   : Generic_GCC(Host, Triple) {
1264
1265   getProgramPaths().push_back(getDriver().getInstalledDir());
1266   if (getDriver().getInstalledDir() != getDriver().Dir)
1267     getProgramPaths().push_back(getDriver().Dir);
1268
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");
1274
1275 }
1276
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;
1282   else
1283     Key = JA.getKind();
1284
1285   Tool *&T = Tools[Key];
1286   if (!T) {
1287     switch (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;
1292     default:
1293       T = &Generic_GCC::SelectTool(C, JA, Inputs);
1294     }
1295   }
1296
1297   return *T;
1298 }
1299
1300
1301 /// Linux toolchain (very bare-bones at the moment).
1302
1303 enum LinuxDistro {
1304   ArchLinux,
1305   DebianLenny,
1306   DebianSqueeze,
1307   DebianWheezy,
1308   Exherbo,
1309   RHEL4,
1310   RHEL5,
1311   RHEL6,
1312   Fedora13,
1313   Fedora14,
1314   Fedora15,
1315   FedoraRawhide,
1316   OpenSuse11_3,
1317   OpenSuse11_4,
1318   OpenSuse12_1,
1319   UbuntuHardy,
1320   UbuntuIntrepid,
1321   UbuntuJaunty,
1322   UbuntuKarmic,
1323   UbuntuLucid,
1324   UbuntuMaverick,
1325   UbuntuNatty,
1326   UbuntuOneiric,
1327   UnknownDistro
1328 };
1329
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;
1334 }
1335
1336 static bool IsOpenSuse(enum LinuxDistro Distro) {
1337   return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1338          Distro == OpenSuse12_1;
1339 }
1340
1341 static bool IsDebian(enum LinuxDistro Distro) {
1342   return Distro == DebianLenny || Distro == DebianSqueeze ||
1343          Distro == DebianWheezy;
1344 }
1345
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;
1351 }
1352
1353 static bool IsDebianBased(enum LinuxDistro Distro) {
1354   return IsDebian(Distro) || IsUbuntu(Distro);
1355 }
1356
1357 static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
1358   if (Arch == llvm::Triple::x86_64) {
1359     bool Exists;
1360     if (Distro == Exherbo &&
1361         (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
1362       return false;
1363
1364     return true;
1365   }
1366   if (Arch == llvm::Triple::ppc64)
1367     return true;
1368   if ((Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) && 
1369       IsDebianBased(Distro))
1370     return true;
1371   return false;
1372 }
1373
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")
1382         return UbuntuHardy;
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")
1390         return UbuntuLucid;
1391       else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1392         return UbuntuMaverick;
1393       else if (Lines[i] == "DISTRIB_CODENAME=natty")
1394         return UbuntuNatty;
1395       else if (Lines[i] == "DISTRIB_CODENAME=oneiric")
1396         return UbuntuOneiric;
1397     }
1398     return UnknownDistro;
1399   }
1400
1401   if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1402     llvm::StringRef Data = File.get()->getBuffer();
1403     if (Data.startswith("Fedora release 15"))
1404       return Fedora15;
1405     else if (Data.startswith("Fedora release 14"))
1406       return Fedora14;
1407     else if (Data.startswith("Fedora release 13"))
1408       return Fedora13;
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)
1414       return RHEL6;
1415     else if ((Data.startswith("Red Hat Enterprise Linux") ||
1416               Data.startswith("CentOS")) &&
1417              Data.find("release 5") != llvm::StringRef::npos)
1418       return RHEL5;
1419     else if ((Data.startswith("Red Hat Enterprise Linux") ||
1420               Data.startswith("CentOS")) &&
1421              Data.find("release 4") != llvm::StringRef::npos)
1422       return RHEL4;
1423     return UnknownDistro;
1424   }
1425
1426   if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1427     llvm::StringRef Data = File.get()->getBuffer();
1428     if (Data[0] == '5')
1429       return DebianLenny;
1430     else if (Data.startswith("squeeze/sid"))
1431       return DebianSqueeze;
1432     else if (Data.startswith("wheezy/sid"))
1433       return DebianWheezy;
1434     return UnknownDistro;
1435   }
1436
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;
1446   }
1447
1448   bool Exists;
1449   if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
1450     return Exherbo;
1451
1452   if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1453     return ArchLinux;
1454
1455   return UnknownDistro;
1456 }
1457
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);
1480     ret.append("/");
1481     ret.append(Version);
1482     return ret;
1483   }
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",
1489                                       "4.2", "4.1.1"};
1490   bool Exists;
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)
1495       return t1;
1496     std::string t2 = "/usr/lib64/gcc/" + Suffix;
1497     if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
1498       return t2;
1499     std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
1500     if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
1501       return t3;
1502   }
1503   return "";
1504 }
1505
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();
1510
1511   std::string Suffix32  = "";
1512   if (Arch == llvm::Triple::x86_64)
1513     Suffix32 = "/32";
1514
1515   std::string Suffix64  = "";
1516   if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
1517     Suffix64 = "/64";
1518
1519   std::string Lib32 = "lib";
1520
1521   bool Exists;
1522   if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
1523     Lib32 = "lib32";
1524
1525   std::string Lib64 = "lib";
1526   bool Symlink;
1527   if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1528       (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
1529     Lib64 = "lib64";
1530
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) &&
1534         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) &&
1538         Exists)
1539       GccTriple = "x86_64-linux-gnu";
1540     else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1541              Exists) && Exists)
1542       GccTriple = "x86_64-unknown-linux-gnu";
1543     else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1544              Exists) && Exists)
1545       GccTriple = "x86_64-pc-linux-gnu";
1546     else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux6E",
1547              Exists) && Exists)
1548       GccTriple = "x86_64-redhat-linux6E";
1549     else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1550              Exists) && Exists)
1551       GccTriple = "x86_64-redhat-linux";
1552     else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1553              Exists) && Exists)
1554       GccTriple = "x86_64-suse-linux";
1555     else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1556              Exists) && Exists)
1557       GccTriple = "x86_64-manbo-linux-gnu";
1558     else if (!llvm::sys::fs::exists("/usr/lib/x86_64-linux-gnu/gcc",
1559              Exists) && Exists)
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) &&
1565              Exists)
1566       GccTriple = "i686-pc-linux-gnu";
1567     else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1568              Exists)
1569       GccTriple = "i486-linux-gnu";
1570     else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1571              Exists)
1572       GccTriple = "i686-redhat-linux";
1573     else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1574              Exists)
1575       GccTriple = "i586-suse-linux";
1576     else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-slackware-linux", Exists)
1577             && 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",
1583                                     Exists) && Exists)
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",
1587                                Exists) && Exists)
1588       GccTriple = "powerpc64-unknown-linux-gnu";
1589     else if (!llvm::sys::fs::exists("/usr/lib64/gcc/"
1590                                     "powerpc64-unknown-linux-gnu", Exists) && 
1591              Exists)
1592       GccTriple = "powerpc64-unknown-linux-gnu";
1593   }
1594
1595   std::string Base = findGCCBaseLibDir(GccTriple);
1596   path_list &Paths = getFilePaths();
1597   bool Is32Bits = (getArch() == llvm::Triple::x86 || 
1598                    getArch() == llvm::Triple::ppc);
1599
1600   std::string Suffix;
1601   std::string Lib;
1602
1603   if (Is32Bits) {
1604     Suffix = Suffix32;
1605     Lib = Lib32;
1606   } else {
1607     Suffix = Suffix64;
1608     Lib = Lib64;
1609   }
1610
1611   llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
1612   if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
1613     Linker = LinkerPath.str();
1614   else
1615     Linker = GetProgramPath("ld");
1616
1617   LinuxDistro Distro = DetectLinuxDistro(Arch);
1618
1619   if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
1620     ExtraOpts.push_back("-z");
1621     ExtraOpts.push_back("relro");
1622   }
1623
1624   if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
1625     ExtraOpts.push_back("-X");
1626
1627   if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
1628       Distro == UbuntuNatty || Distro == UbuntuOneiric)
1629     ExtraOpts.push_back("--hash-style=gnu");
1630
1631   if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
1632       Distro == UbuntuJaunty || Distro == UbuntuKarmic)
1633     ExtraOpts.push_back("--hash-style=both");
1634
1635   if (IsRedhat(Distro))
1636     ExtraOpts.push_back("--no-add-needed");
1637
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");
1645
1646   if (IsOpenSuse(Distro))
1647     ExtraOpts.push_back("--enable-new-dtags");
1648
1649   if (Distro == ArchLinux)
1650     Lib = "lib";
1651
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);
1657   }
1658
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);
1664
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);
1672 }
1673
1674 bool Linux::HasNativeLLVMSupport() const {
1675   return true;
1676 }
1677
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;
1683   else
1684     Key = JA.getKind();
1685
1686   bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1687                                              options::OPT_no_integrated_as,
1688                                              IsIntegratedAssemblerDefault());
1689
1690   Tool *&T = Tools[Key];
1691   if (!T) {
1692     switch (Key) {
1693     case Action::AssembleJobClass:
1694       if (UseIntegratedAs)
1695         T = new tools::ClangAs(*this);
1696       else
1697         T = new tools::linuxtools::Assemble(*this);
1698       break;
1699     case Action::LinkJobClass:
1700       T = new tools::linuxtools::Link(*this); break;
1701     default:
1702       T = &Generic_GCC::SelectTool(C, JA, Inputs);
1703     }
1704   }
1705
1706   return *T;
1707 }
1708
1709 /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1710
1711 DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1712   : Generic_ELF(Host, Triple) {
1713
1714   // Path mangling to find libexec
1715   getProgramPaths().push_back(getDriver().getInstalledDir());
1716   if (getDriver().getInstalledDir() != getDriver().Dir)
1717     getProgramPaths().push_back(getDriver().Dir);
1718
1719   getFilePaths().push_back(getDriver().Dir + "/../lib");
1720   getFilePaths().push_back("/usr/lib");
1721   getFilePaths().push_back("/usr/lib/gcc41");
1722 }
1723
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;
1729   else
1730     Key = JA.getKind();
1731
1732   Tool *&T = Tools[Key];
1733   if (!T) {
1734     switch (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;
1739     default:
1740       T = &Generic_GCC::SelectTool(C, JA, Inputs);
1741     }
1742   }
1743
1744   return *T;
1745 }
1746
1747 Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1748   : ToolChain(Host, Triple) {
1749 }
1750
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;
1756   else
1757     Key = JA.getKind();
1758
1759   Tool *&T = Tools[Key];
1760   if (!T) {
1761     switch (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;
1776     }
1777   }
1778
1779   return *T;
1780 }
1781
1782 bool Windows::IsIntegratedAssemblerDefault() const {
1783   return true;
1784 }
1785
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";
1790 }
1791
1792 const char *Windows::GetDefaultRelocationModel() const {
1793   return "static";
1794 }
1795
1796 const char *Windows::GetForcedPicModel() const {
1797   if (getArchName() == "x86_64")
1798     return "pic";
1799   return 0;
1800 }