ToolChains.cpp revision 279c1dbebf37cd128f3c73c70741a6b8c35ad025
1//===--- ToolChains.cpp - ToolChain Implementations ---------------------*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11
12#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
14#include "clang/Driver/Compilation.h"
15#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
17#include "clang/Driver/HostInfo.h"
18#include "clang/Driver/OptTable.h"
19#include "clang/Driver/Option.h"
20#include "clang/Driver/Options.h"
21
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/System/Path.h"
26
27#include <cstdlib> // ::getenv
28
29using namespace clang::driver;
30using namespace clang::driver::toolchains;
31
32/// Darwin - Darwin tool chain for i386 and x86_64.
33
34Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple,
35               const unsigned (&_DarwinVersion)[3])
36  : ToolChain(Host, Triple), TargetInitialized(false)
37{
38  llvm::raw_string_ostream(MacosxVersionMin)
39    << "10." << std::max(0, (int)_DarwinVersion[0] - 4) << '.'
40    << _DarwinVersion[1];
41}
42
43// FIXME: Can we tablegen this?
44static const char *GetArmArchForMArch(llvm::StringRef Value) {
45  if (Value == "armv6k")
46    return "armv6";
47
48  if (Value == "armv5tej")
49    return "armv5";
50
51  if (Value == "xscale")
52    return "xscale";
53
54  if (Value == "armv4t")
55    return "armv4t";
56
57  if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
58      Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
59      Value == "armv7m")
60    return "armv7";
61
62  return 0;
63}
64
65// FIXME: Can we tablegen this?
66static const char *GetArmArchForMCpu(llvm::StringRef Value) {
67  if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
68      Value == "arm946e-s" || Value == "arm966e-s" ||
69      Value == "arm968e-s" || Value == "arm10e" ||
70      Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
71      Value == "arm1026ej-s")
72    return "armv5";
73
74  if (Value == "xscale")
75    return "xscale";
76
77  if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
78      Value == "arm1176jz-s" || Value == "arm1176jzf-s")
79    return "armv6";
80
81  if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
82    return "armv7";
83
84  return 0;
85}
86
87llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
88  switch (getTriple().getArch()) {
89  default:
90    return getArchName();
91
92  case llvm::Triple::arm: {
93    if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
94      if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
95        return Arch;
96
97    if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
98      if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
99        return Arch;
100
101    return "arm";
102  }
103  }
104}
105
106DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
107                     const unsigned (&DarwinVersion)[3],
108                     const unsigned (&_GCCVersion)[3])
109  : Darwin(Host, Triple, DarwinVersion)
110{
111  GCCVersion[0] = _GCCVersion[0];
112  GCCVersion[1] = _GCCVersion[1];
113  GCCVersion[2] = _GCCVersion[2];
114
115  // Set up the tool chain paths to match gcc.
116  ToolChainDir = "i686-apple-darwin";
117  ToolChainDir += llvm::utostr(DarwinVersion[0]);
118  ToolChainDir += "/";
119  ToolChainDir += llvm::utostr(GCCVersion[0]);
120  ToolChainDir += '.';
121  ToolChainDir += llvm::utostr(GCCVersion[1]);
122  ToolChainDir += '.';
123  ToolChainDir += llvm::utostr(GCCVersion[2]);
124
125  // Try the next major version if that tool chain dir is invalid.
126  std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
127  if (!llvm::sys::Path(Tmp).exists()) {
128    std::string Next = "i686-apple-darwin";
129    Next += llvm::utostr(DarwinVersion[0] + 1);
130    Next += "/";
131    Next += llvm::utostr(GCCVersion[0]);
132    Next += '.';
133    Next += llvm::utostr(GCCVersion[1]);
134    Next += '.';
135    Next += llvm::utostr(GCCVersion[2]);
136
137    // Use that if it exists, otherwise hope the user isn't linking.
138    //
139    // FIXME: Drop dependency on gcc's tool chain.
140    Tmp = "/usr/lib/gcc/" + Next;
141    if (llvm::sys::Path(Tmp).exists())
142      ToolChainDir = Next;
143  }
144
145  std::string Path;
146  if (getArchName() == "x86_64") {
147    Path = getDriver().Dir;
148    Path += "/../lib/gcc/";
149    Path += ToolChainDir;
150    Path += "/x86_64";
151    getFilePaths().push_back(Path);
152
153    Path = "/usr/lib/gcc/";
154    Path += ToolChainDir;
155    Path += "/x86_64";
156    getFilePaths().push_back(Path);
157  }
158
159  Path = getDriver().Dir;
160  Path += "/../lib/gcc/";
161  Path += ToolChainDir;
162  getFilePaths().push_back(Path);
163
164  Path = "/usr/lib/gcc/";
165  Path += ToolChainDir;
166  getFilePaths().push_back(Path);
167
168  Path = getDriver().Dir;
169  Path += "/../libexec/gcc/";
170  Path += ToolChainDir;
171  getProgramPaths().push_back(Path);
172
173  Path = "/usr/libexec/gcc/";
174  Path += ToolChainDir;
175  getProgramPaths().push_back(Path);
176
177  getProgramPaths().push_back(getDriver().Dir);
178}
179
180Darwin::~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
187Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
188  Action::ActionClass Key;
189  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
190    Key = Action::AnalyzeJobClass;
191  else
192    Key = JA.getKind();
193
194  // FIXME: This doesn't belong here, but ideally we will support static soon
195  // anyway.
196  bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
197                    C.getArgs().hasArg(options::OPT_static) ||
198                    C.getArgs().hasArg(options::OPT_fapple_kext));
199  bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
200  bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
201                                             options::OPT_no_integrated_as,
202                                             IsIADefault);
203
204  Tool *&T = Tools[Key];
205  if (!T) {
206    switch (Key) {
207    case Action::InputClass:
208    case Action::BindArchClass:
209      assert(0 && "Invalid tool kind.");
210    case Action::PreprocessJobClass:
211      T = new tools::darwin::Preprocess(*this); break;
212    case Action::AnalyzeJobClass:
213      T = new tools::Clang(*this); break;
214    case Action::PrecompileJobClass:
215    case Action::CompileJobClass:
216      T = new tools::darwin::Compile(*this); break;
217    case Action::AssembleJobClass: {
218      if (UseIntegratedAs)
219        T = new tools::ClangAs(*this);
220      else
221        T = new tools::darwin::Assemble(*this);
222      break;
223    }
224    case Action::LinkJobClass:
225      T = new tools::darwin::Link(*this); break;
226    case Action::LipoJobClass:
227      T = new tools::darwin::Lipo(*this); break;
228    case Action::DsymutilJobClass:
229      T = new tools::darwin::Dsymutil(*this); break;
230    }
231  }
232
233  return *T;
234}
235
236void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
237                                      ArgStringList &CmdArgs) const {
238  std::string Tmp;
239
240  // FIXME: Derive these correctly.
241  if (getArchName() == "x86_64") {
242    CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
243                                         "/x86_64"));
244    // Intentionally duplicated for (temporary) gcc bug compatibility.
245    CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
246                                         "/x86_64"));
247  }
248
249  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
250
251  Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
252  if (llvm::sys::Path(Tmp).exists())
253    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
254  Tmp = getDriver().Dir + "/../lib/gcc";
255  if (llvm::sys::Path(Tmp).exists())
256    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
257  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
258  // Intentionally duplicated for (temporary) gcc bug compatibility.
259  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
260  Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
261  if (llvm::sys::Path(Tmp).exists())
262    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
263  Tmp = getDriver().Dir + "/../lib";
264  if (llvm::sys::Path(Tmp).exists())
265    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
266  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
267                                       "/../../../" + ToolChainDir));
268  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
269                                       "/../../.."));
270}
271
272void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
273                                      ArgStringList &CmdArgs) const {
274  // Note that this routine is only used for targetting OS X.
275
276  // Derived from libgcc and lib specs but refactored.
277  if (Args.hasArg(options::OPT_static)) {
278    CmdArgs.push_back("-lgcc_static");
279  } else {
280    if (Args.hasArg(options::OPT_static_libgcc)) {
281      CmdArgs.push_back("-lgcc_eh");
282    } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
283      // Derived from darwin_iphoneos_libgcc spec.
284      if (isTargetIPhoneOS()) {
285        CmdArgs.push_back("-lgcc_s.1");
286      } else {
287        CmdArgs.push_back("-lgcc_s.10.5");
288      }
289    } else if (Args.hasArg(options::OPT_shared_libgcc) ||
290               Args.hasFlag(options::OPT_fexceptions,
291                            options::OPT_fno_exceptions) ||
292               Args.hasArg(options::OPT_fgnu_runtime)) {
293      // FIXME: This is probably broken on 10.3?
294      if (isMacosxVersionLT(10, 5))
295        CmdArgs.push_back("-lgcc_s.10.4");
296      else if (isMacosxVersionLT(10, 6))
297        CmdArgs.push_back("-lgcc_s.10.5");
298    } else {
299      if (isMacosxVersionLT(10, 3, 9))
300        ; // Do nothing.
301      else if (isMacosxVersionLT(10, 5))
302        CmdArgs.push_back("-lgcc_s.10.4");
303      else if (isMacosxVersionLT(10, 6))
304        CmdArgs.push_back("-lgcc_s.10.5");
305    }
306
307    if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
308      CmdArgs.push_back("-lgcc");
309      CmdArgs.push_back("-lSystem");
310    } else {
311      CmdArgs.push_back("-lSystem");
312      CmdArgs.push_back("-lgcc");
313    }
314  }
315}
316
317DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
318                         const unsigned (&DarwinVersion)[3])
319  : Darwin(Host, Triple, DarwinVersion)
320{
321  // We expect 'as', 'ld', etc. to be adjacent to our install dir.
322  getProgramPaths().push_back(getDriver().Dir);
323}
324
325void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
326                                       ArgStringList &CmdArgs) const {
327  // The Clang toolchain uses explicit paths for internal libraries.
328}
329
330void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
331                                        ArgStringList &CmdArgs) const {
332  // Darwin doesn't support real static executables, don't link any runtime
333  // libraries with -static.
334  if (Args.hasArg(options::OPT_static))
335    return;
336
337  // Reject -static-libgcc for now, we can deal with this when and if someone
338  // cares. This is useful in situations where someone wants to statically link
339  // something like libstdc++, and needs its runtime support routines.
340  if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
341    getDriver().Diag(clang::diag::err_drv_unsupported_opt)
342      << A->getAsString(Args);
343    return;
344  }
345
346  // Otherwise link libSystem, then the dynamic runtime library, and finally any
347  // target specific static runtime library.
348  CmdArgs.push_back("-lSystem");
349
350  // Select the dynamic runtime library and the target specific static library.
351  const char *DarwinStaticLib = 0;
352  if (isTargetIPhoneOS()) {
353    CmdArgs.push_back("-lgcc_s.1");
354
355    // We may need some static functions for armv6/thumb which are required to
356    // be in the same linkage unit as their caller.
357    if (getDarwinArchName(Args) == "armv6")
358      DarwinStaticLib = "libclang_rt.armv6.a";
359  } else {
360    // The dynamic runtime library was merged with libSystem for 10.6 and
361    // beyond; only 10.4 and 10.5 need an additional runtime library.
362    if (isMacosxVersionLT(10, 5))
363      CmdArgs.push_back("-lgcc_s.10.4");
364    else if (isMacosxVersionLT(10, 6))
365      CmdArgs.push_back("-lgcc_s.10.5");
366
367    // For OS X, we only need a static runtime library when targetting 10.4, to
368    // provide versions of the static functions which were omitted from
369    // 10.4.dylib.
370    if (isMacosxVersionLT(10, 5))
371      DarwinStaticLib = "libclang_rt.10.4.a";
372  }
373
374  /// Add the target specific static library, if needed.
375  if (DarwinStaticLib) {
376    llvm::sys::Path P(getDriver().ResourceDir);
377    P.appendComponent("lib");
378    P.appendComponent("darwin");
379    P.appendComponent(DarwinStaticLib);
380
381    // For now, allow missing resource libraries to support developers who may
382    // not have compiler-rt checked out or integrated into their build.
383    if (!P.exists())
384      getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
385        << P.str();
386    else
387      CmdArgs.push_back(Args.MakeArgString(P.str()));
388  }
389}
390
391DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
392                                      const char *BoundArch) const {
393  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
394  const OptTable &Opts = getDriver().getOpts();
395
396  // FIXME: We really want to get out of the tool chain level argument
397  // translation business, as it makes the driver functionality much
398  // more opaque. For now, we follow gcc closely solely for the
399  // purpose of easily achieving feature parity & testability. Once we
400  // have something that works, we should reevaluate each translation
401  // and try to push it down into tool specific logic.
402
403  Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
404  Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
405  if (OSXVersion && iPhoneVersion) {
406    getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
407          << OSXVersion->getAsString(Args)
408          << iPhoneVersion->getAsString(Args);
409    iPhoneVersion = 0;
410  } else if (!OSXVersion && !iPhoneVersion) {
411    // If neither OS X nor iPhoneOS targets were specified, check for
412    // environment defines.
413    const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
414    const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
415
416    // Ignore empty strings.
417    if (OSXTarget && OSXTarget[0] == '\0')
418      OSXTarget = 0;
419    if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
420      iPhoneOSTarget = 0;
421
422    // Diagnose conflicting deployment targets, and choose default platform
423    // based on the tool chain.
424    //
425    // FIXME: Don't hardcode default here.
426    if (OSXTarget && iPhoneOSTarget) {
427      // FIXME: We should see if we can get away with warning or erroring on
428      // this. Perhaps put under -pedantic?
429      if (getTriple().getArch() == llvm::Triple::arm ||
430          getTriple().getArch() == llvm::Triple::thumb)
431        OSXTarget = 0;
432      else
433        iPhoneOSTarget = 0;
434    }
435
436    if (OSXTarget) {
437      const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
438      OSXVersion = DAL->MakeJoinedArg(0, O, OSXTarget);
439      DAL->append(OSXVersion);
440    } else if (iPhoneOSTarget) {
441      const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
442      iPhoneVersion = DAL->MakeJoinedArg(0, O, iPhoneOSTarget);
443      DAL->append(iPhoneVersion);
444    } else {
445      // Otherwise, choose a default platform based on the tool chain.
446      //
447      // FIXME: Don't hardcode default here.
448      if (getTriple().getArch() == llvm::Triple::arm ||
449          getTriple().getArch() == llvm::Triple::thumb) {
450        const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
451        iPhoneVersion = DAL->MakeJoinedArg(0, O, "3.0");
452        DAL->append(iPhoneVersion);
453      } else {
454        const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
455        OSXVersion = DAL->MakeJoinedArg(0, O, MacosxVersionMin);
456        DAL->append(OSXVersion);
457      }
458    }
459  }
460
461  // Set the tool chain target information.
462  unsigned Major, Minor, Micro;
463  bool HadExtra;
464  if (OSXVersion) {
465    assert(!iPhoneVersion && "Unknown target platform!");
466    if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
467                                   Micro, HadExtra) || HadExtra ||
468        Major != 10 || Minor >= 10 || Micro >= 10)
469      getDriver().Diag(clang::diag::err_drv_invalid_version_number)
470        << OSXVersion->getAsString(Args);
471  } else {
472    assert(iPhoneVersion && "Unknown target platform!");
473    if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
474                                   Micro, HadExtra) || HadExtra ||
475        Major >= 10 || Minor >= 100 || Micro >= 100)
476      getDriver().Diag(clang::diag::err_drv_invalid_version_number)
477        << iPhoneVersion->getAsString(Args);
478  }
479  setTarget(iPhoneVersion, Major, Minor, Micro);
480
481  for (ArgList::const_iterator it = Args.begin(),
482         ie = Args.end(); it != ie; ++it) {
483    Arg *A = *it;
484
485    if (A->getOption().matches(options::OPT_Xarch__)) {
486      // FIXME: Canonicalize name.
487      if (getArchName() != A->getValue(Args, 0))
488        continue;
489
490      // FIXME: The arg is leaked here, and we should have a nicer
491      // interface for this.
492      unsigned Prev, Index = Prev = A->getIndex() + 1;
493      Arg *XarchArg = Opts.ParseOneArg(Args, Index);
494
495      // If the argument parsing failed or more than one argument was
496      // consumed, the -Xarch_ argument's parameter tried to consume
497      // extra arguments. Emit an error and ignore.
498      //
499      // We also want to disallow any options which would alter the
500      // driver behavior; that isn't going to work in our model. We
501      // use isDriverOption() as an approximation, although things
502      // like -O4 are going to slip through.
503      if (!XarchArg || Index > Prev + 1 ||
504          XarchArg->getOption().isDriverOption()) {
505       getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
506          << A->getAsString(Args);
507        continue;
508      }
509
510      XarchArg->setBaseArg(A);
511      A = XarchArg;
512    }
513
514    // Sob. These is strictly gcc compatible for the time being. Apple
515    // gcc translates options twice, which means that self-expanding
516    // options add duplicates.
517    switch ((options::ID) A->getOption().getID()) {
518    default:
519      DAL->append(A);
520      break;
521
522    case options::OPT_mkernel:
523    case options::OPT_fapple_kext:
524      DAL->append(A);
525      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
526      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
527      break;
528
529    case options::OPT_dependency_file:
530      DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
531                                       A->getValue(Args)));
532      break;
533
534    case options::OPT_gfull:
535      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
536      DAL->append(DAL->MakeFlagArg(A,
537             Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
538      break;
539
540    case options::OPT_gused:
541      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
542      DAL->append(DAL->MakeFlagArg(A,
543             Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
544      break;
545
546    case options::OPT_fterminated_vtables:
547    case options::OPT_findirect_virtual_calls:
548      DAL->append(DAL->MakeFlagArg(A,
549                                   Opts.getOption(options::OPT_fapple_kext)));
550      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
551      break;
552
553    case options::OPT_shared:
554      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
555      break;
556
557    case options::OPT_fconstant_cfstrings:
558      DAL->append(DAL->MakeFlagArg(A,
559                             Opts.getOption(options::OPT_mconstant_cfstrings)));
560      break;
561
562    case options::OPT_fno_constant_cfstrings:
563      DAL->append(DAL->MakeFlagArg(A,
564                          Opts.getOption(options::OPT_mno_constant_cfstrings)));
565      break;
566
567    case options::OPT_Wnonportable_cfstrings:
568      DAL->append(DAL->MakeFlagArg(A,
569                     Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
570      break;
571
572    case options::OPT_Wno_nonportable_cfstrings:
573      DAL->append(DAL->MakeFlagArg(A,
574                  Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
575      break;
576
577    case options::OPT_fpascal_strings:
578      DAL->append(DAL->MakeFlagArg(A,
579                                 Opts.getOption(options::OPT_mpascal_strings)));
580      break;
581
582    case options::OPT_fno_pascal_strings:
583      DAL->append(DAL->MakeFlagArg(A,
584                              Opts.getOption(options::OPT_mno_pascal_strings)));
585      break;
586    }
587  }
588
589  if (getTriple().getArch() == llvm::Triple::x86 ||
590      getTriple().getArch() == llvm::Triple::x86_64)
591    if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
592      DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
593                                     "core2"));
594
595  // Add the arch options based on the particular spelling of -arch, to match
596  // how the driver driver works.
597  if (BoundArch) {
598    llvm::StringRef Name = BoundArch;
599    const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
600    const Option *MArch = Opts.getOption(options::OPT_march_EQ);
601
602    // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
603    // which defines the list of which architectures we accept.
604    if (Name == "ppc")
605      ;
606    else if (Name == "ppc601")
607      DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
608    else if (Name == "ppc603")
609      DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
610    else if (Name == "ppc604")
611      DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
612    else if (Name == "ppc604e")
613      DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
614    else if (Name == "ppc750")
615      DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
616    else if (Name == "ppc7400")
617      DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
618    else if (Name == "ppc7450")
619      DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
620    else if (Name == "ppc970")
621      DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
622
623    else if (Name == "ppc64")
624      DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
625
626    else if (Name == "i386")
627      ;
628    else if (Name == "i486")
629      DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
630    else if (Name == "i586")
631      DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
632    else if (Name == "i686")
633      DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
634    else if (Name == "pentium")
635      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
636    else if (Name == "pentium2")
637      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
638    else if (Name == "pentpro")
639      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
640    else if (Name == "pentIIm3")
641      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
642
643    else if (Name == "x86_64")
644      DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
645
646    else if (Name == "arm")
647      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
648    else if (Name == "armv4t")
649      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
650    else if (Name == "armv5")
651      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
652    else if (Name == "xscale")
653      DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
654    else if (Name == "armv6")
655      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
656    else if (Name == "armv7")
657      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
658
659    else
660      llvm_unreachable("invalid Darwin arch");
661  }
662
663  return DAL;
664}
665
666bool Darwin::IsUnwindTablesDefault() const {
667  // FIXME: Gross; we should probably have some separate target
668  // definition, possibly even reusing the one in clang.
669  return getArchName() == "x86_64";
670}
671
672bool Darwin::UseDwarfDebugFlags() const {
673  if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
674    return S[0] != '\0';
675  return false;
676}
677
678bool Darwin::UseSjLjExceptions() const {
679  // Darwin uses SjLj exceptions on ARM.
680  return (getTriple().getArch() == llvm::Triple::arm ||
681          getTriple().getArch() == llvm::Triple::thumb);
682}
683
684const char *Darwin::GetDefaultRelocationModel() const {
685  return "pic";
686}
687
688const char *Darwin::GetForcedPicModel() const {
689  if (getArchName() == "x86_64")
690    return "pic";
691  return 0;
692}
693
694bool Darwin::SupportsObjCGC() const {
695  // Garbage collection is supported everywhere except on iPhone OS.
696  return !isTargetIPhoneOS();
697}
698
699/// Generic_GCC - A tool chain using the 'gcc' command to perform
700/// all subcommands; this relies on gcc translating the majority of
701/// command line options.
702
703Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
704  : ToolChain(Host, Triple) {
705  getProgramPaths().push_back(getDriver().Dir);
706}
707
708Generic_GCC::~Generic_GCC() {
709  // Free tool implementations.
710  for (llvm::DenseMap<unsigned, Tool*>::iterator
711         it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
712    delete it->second;
713}
714
715Tool &Generic_GCC::SelectTool(const Compilation &C,
716                              const JobAction &JA) const {
717  Action::ActionClass Key;
718  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
719    Key = Action::AnalyzeJobClass;
720  else
721    Key = JA.getKind();
722
723  Tool *&T = Tools[Key];
724  if (!T) {
725    switch (Key) {
726    case Action::InputClass:
727    case Action::BindArchClass:
728      assert(0 && "Invalid tool kind.");
729    case Action::PreprocessJobClass:
730      T = new tools::gcc::Preprocess(*this); break;
731    case Action::PrecompileJobClass:
732      T = new tools::gcc::Precompile(*this); break;
733    case Action::AnalyzeJobClass:
734      T = new tools::Clang(*this); break;
735    case Action::CompileJobClass:
736      T = new tools::gcc::Compile(*this); break;
737    case Action::AssembleJobClass:
738      T = new tools::gcc::Assemble(*this); break;
739    case Action::LinkJobClass:
740      T = new tools::gcc::Link(*this); break;
741
742      // This is a bit ungeneric, but the only platform using a driver
743      // driver is Darwin.
744    case Action::LipoJobClass:
745      T = new tools::darwin::Lipo(*this); break;
746    case Action::DsymutilJobClass:
747      T = new tools::darwin::Dsymutil(*this); break;
748    }
749  }
750
751  return *T;
752}
753
754bool Generic_GCC::IsUnwindTablesDefault() const {
755  // FIXME: Gross; we should probably have some separate target
756  // definition, possibly even reusing the one in clang.
757  return getArchName() == "x86_64";
758}
759
760const char *Generic_GCC::GetDefaultRelocationModel() const {
761  return "static";
762}
763
764const char *Generic_GCC::GetForcedPicModel() const {
765  return 0;
766}
767
768/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
769/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
770/// Currently does not support anything else but compilation.
771
772TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
773  : ToolChain(Host, Triple) {
774  // Path mangling to find libexec
775  std::string Path(getDriver().Dir);
776
777  Path += "/../libexec";
778  getProgramPaths().push_back(Path);
779}
780
781TCEToolChain::~TCEToolChain() {
782  for (llvm::DenseMap<unsigned, Tool*>::iterator
783           it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
784      delete it->second;
785}
786
787bool TCEToolChain::IsMathErrnoDefault() const {
788  return true;
789}
790
791bool TCEToolChain::IsUnwindTablesDefault() const {
792  return false;
793}
794
795const char *TCEToolChain::GetDefaultRelocationModel() const {
796  return "static";
797}
798
799const char *TCEToolChain::GetForcedPicModel() const {
800  return 0;
801}
802
803Tool &TCEToolChain::SelectTool(const Compilation &C,
804                            const JobAction &JA) const {
805  Action::ActionClass Key;
806  Key = Action::AnalyzeJobClass;
807
808  Tool *&T = Tools[Key];
809  if (!T) {
810    switch (Key) {
811    case Action::PreprocessJobClass:
812      T = new tools::gcc::Preprocess(*this); break;
813    case Action::AnalyzeJobClass:
814      T = new tools::Clang(*this); break;
815    default:
816     assert(false && "Unsupported action for TCE target.");
817    }
818  }
819  return *T;
820}
821
822/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
823
824OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
825  : Generic_GCC(Host, Triple) {
826  getFilePaths().push_back(getDriver().Dir + "/../lib");
827  getFilePaths().push_back("/usr/lib");
828}
829
830Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
831  Action::ActionClass Key;
832  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
833    Key = Action::AnalyzeJobClass;
834  else
835    Key = JA.getKind();
836
837  Tool *&T = Tools[Key];
838  if (!T) {
839    switch (Key) {
840    case Action::AssembleJobClass:
841      T = new tools::openbsd::Assemble(*this); break;
842    case Action::LinkJobClass:
843      T = new tools::openbsd::Link(*this); break;
844    default:
845      T = &Generic_GCC::SelectTool(C, JA);
846    }
847  }
848
849  return *T;
850}
851
852/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
853
854FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
855  : Generic_GCC(Host, Triple) {
856  if (Lib32) {
857    getFilePaths().push_back(getDriver().Dir + "/../lib32");
858    getFilePaths().push_back("/usr/lib32");
859  } else {
860    getFilePaths().push_back(getDriver().Dir + "/../lib");
861    getFilePaths().push_back("/usr/lib");
862  }
863}
864
865Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
866  Action::ActionClass Key;
867  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
868    Key = Action::AnalyzeJobClass;
869  else
870    Key = JA.getKind();
871
872  Tool *&T = Tools[Key];
873  if (!T) {
874    switch (Key) {
875    case Action::AssembleJobClass:
876      T = new tools::freebsd::Assemble(*this); break;
877    case Action::LinkJobClass:
878      T = new tools::freebsd::Link(*this); break;
879    default:
880      T = &Generic_GCC::SelectTool(C, JA);
881    }
882  }
883
884  return *T;
885}
886
887/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
888
889AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
890  : Generic_GCC(Host, Triple) {
891
892  getProgramPaths().push_back(getDriver().Dir);
893
894  getFilePaths().push_back(getDriver().Dir + "/../lib");
895  getFilePaths().push_back("/usr/lib");
896  getFilePaths().push_back("/usr/sfw/lib");
897  getFilePaths().push_back("/opt/gcc4/lib");
898  getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
899
900}
901
902Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
903  Action::ActionClass Key;
904  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
905    Key = Action::AnalyzeJobClass;
906  else
907    Key = JA.getKind();
908
909  Tool *&T = Tools[Key];
910  if (!T) {
911    switch (Key) {
912    case Action::AssembleJobClass:
913      T = new tools::auroraux::Assemble(*this); break;
914    case Action::LinkJobClass:
915      T = new tools::auroraux::Link(*this); break;
916    default:
917      T = &Generic_GCC::SelectTool(C, JA);
918    }
919  }
920
921  return *T;
922}
923
924
925/// Linux toolchain (very bare-bones at the moment).
926
927Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
928  : Generic_GCC(Host, Triple) {
929  getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
930  getFilePaths().push_back("/lib/");
931  getFilePaths().push_back("/usr/lib/");
932
933  // Depending on the Linux distribution, any combination of lib{,32,64} is
934  // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
935  // openSUSE uses lib and lib64 for the same purpose.
936  getFilePaths().push_back("/lib32/");
937  getFilePaths().push_back("/usr/lib32/");
938  getFilePaths().push_back("/lib64/");
939  getFilePaths().push_back("/usr/lib64/");
940
941  // FIXME: Figure out some way to get gcc's libdir
942  // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
943  // crtbegin.o/crtend.o/etc., and want static versions of various
944  // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
945  // get away with using shared versions in /usr/lib, though.
946  // We could fall back to the approach we used for includes (a massive
947  // list), but that's messy at best.
948}
949
950/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
951
952DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
953  : Generic_GCC(Host, Triple) {
954
955  // Path mangling to find libexec
956  getProgramPaths().push_back(getDriver().Dir);
957
958  getFilePaths().push_back(getDriver().Dir + "/../lib");
959  getFilePaths().push_back("/usr/lib");
960  getFilePaths().push_back("/usr/lib/gcc41");
961}
962
963Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
964  Action::ActionClass Key;
965  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
966    Key = Action::AnalyzeJobClass;
967  else
968    Key = JA.getKind();
969
970  Tool *&T = Tools[Key];
971  if (!T) {
972    switch (Key) {
973    case Action::AssembleJobClass:
974      T = new tools::dragonfly::Assemble(*this); break;
975    case Action::LinkJobClass:
976      T = new tools::dragonfly::Link(*this); break;
977    default:
978      T = &Generic_GCC::SelectTool(C, JA);
979    }
980  }
981
982  return *T;
983}
984