ToolChains.cpp revision 211573
1193323Sed//===--- ToolChains.cpp - ToolChain Implementations ---------------------*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed
10193323Sed#include "ToolChains.h"
11193323Sed
12193323Sed#include "clang/Driver/Arg.h"
13193323Sed#include "clang/Driver/ArgList.h"
14193323Sed#include "clang/Driver/Compilation.h"
15193323Sed#include "clang/Driver/Driver.h"
16193323Sed#include "clang/Driver/DriverDiagnostic.h"
17193323Sed#include "clang/Driver/HostInfo.h"
18193323Sed#include "clang/Driver/OptTable.h"
19193323Sed#include "clang/Driver/Option.h"
20193323Sed#include "clang/Driver/Options.h"
21193323Sed
22193323Sed#include "llvm/ADT/StringExtras.h"
23193323Sed#include "llvm/Support/ErrorHandling.h"
24234353Sdim#include "llvm/Support/raw_ostream.h"
25234353Sdim#include "llvm/System/Path.h"
26193323Sed
27193323Sed#include <cstdlib> // ::getenv
28193323Sed
29193323Sed#ifndef CLANG_PREFIX
30193323Sed#define CLANG_PREFIX
31193323Sed#endif
32193323Sed
33193323Sedusing namespace clang::driver;
34193323Sedusing namespace clang::driver::toolchains;
35263508Sdim
36263508Sdim/// Darwin - Darwin tool chain for i386 and x86_64.
37263508Sdim
38193323SedDarwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple,
39193323Sed               const unsigned (&_DarwinVersion)[3])
40193323Sed  : ToolChain(Host, Triple), TargetInitialized(false)
41193323Sed{
42193323Sed  llvm::raw_string_ostream(MacosxVersionMin)
43249423Sdim    << "10." << std::max(0, (int)_DarwinVersion[0] - 4) << '.'
44249423Sdim    << _DarwinVersion[1];
45249423Sdim}
46249423Sdim
47249423Sdim// FIXME: Can we tablegen this?
48249423Sdimstatic const char *GetArmArchForMArch(llvm::StringRef Value) {
49249423Sdim  if (Value == "armv6k")
50193323Sed    return "armv6";
51193323Sed
52193323Sed  if (Value == "armv5tej")
53249423Sdim    return "armv5";
54249423Sdim
55249423Sdim  if (Value == "xscale")
56193323Sed    return "xscale";
57193323Sed
58193323Sed  if (Value == "armv4t")
59193323Sed    return "armv4t";
60193323Sed
61193323Sed  if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
62193323Sed      Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
63193323Sed      Value == "armv7m")
64193323Sed    return "armv7";
65193323Sed
66193323Sed  return 0;
67193323Sed}
68193323Sed
69193323Sed// FIXME: Can we tablegen this?
70193323Sedstatic const char *GetArmArchForMCpu(llvm::StringRef Value) {
71193323Sed  if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
72193323Sed      Value == "arm946e-s" || Value == "arm966e-s" ||
73193323Sed      Value == "arm968e-s" || Value == "arm10e" ||
74193323Sed      Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
75207618Srdivacky      Value == "arm1026ej-s")
76207618Srdivacky    return "armv5";
77207618Srdivacky
78207618Srdivacky  if (Value == "xscale")
79207618Srdivacky    return "xscale";
80207618Srdivacky
81207618Srdivacky  if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
82207618Srdivacky      Value == "arm1176jz-s" || Value == "arm1176jzf-s")
83207618Srdivacky    return "armv6";
84207618Srdivacky
85207618Srdivacky  if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
86207618Srdivacky    return "armv7";
87249423Sdim
88249423Sdim  return 0;
89249423Sdim}
90251662Sdim
91251662Sdimllvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
92251662Sdim  switch (getTriple().getArch()) {
93251662Sdim  default:
94251662Sdim    return getArchName();
95193323Sed
96193574Sed  case llvm::Triple::arm: {
97193323Sed    if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
98193323Sed      if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
99263508Sdim        return Arch;
100249423Sdim
101249423Sdim    if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
102193323Sed      if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
103249423Sdim        return Arch;
104193323Sed
105193323Sed    return "arm";
106193323Sed  }
107207618Srdivacky  }
108207618Srdivacky}
109207618Srdivacky
110207618SrdivackyDarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
111249423Sdim                     const unsigned (&DarwinVersion)[3],
112249423Sdim                     const unsigned (&_GCCVersion)[3])
113193323Sed  : Darwin(Host, Triple, DarwinVersion)
114193323Sed{
115193323Sed  GCCVersion[0] = _GCCVersion[0];
116193323Sed  GCCVersion[1] = _GCCVersion[1];
117193323Sed  GCCVersion[2] = _GCCVersion[2];
118193323Sed
119193323Sed  // Set up the tool chain paths to match gcc.
120263508Sdim  ToolChainDir = "i686-apple-darwin";
121263508Sdim  ToolChainDir += llvm::utostr(DarwinVersion[0]);
122263508Sdim  ToolChainDir += "/";
123193323Sed  ToolChainDir += llvm::utostr(GCCVersion[0]);
124193323Sed  ToolChainDir += '.';
125193323Sed  ToolChainDir += llvm::utostr(GCCVersion[1]);
126193323Sed  ToolChainDir += '.';
127193323Sed  ToolChainDir += llvm::utostr(GCCVersion[2]);
128193323Sed
129193323Sed  // Try the next major version if that tool chain dir is invalid.
130193323Sed  std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
131193323Sed  if (!llvm::sys::Path(Tmp).exists()) {
132193323Sed    std::string Next = "i686-apple-darwin";
133193323Sed    Next += llvm::utostr(DarwinVersion[0] + 1);
134193323Sed    Next += "/";
135193323Sed    Next += llvm::utostr(GCCVersion[0]);
136193323Sed    Next += '.';
137249423Sdim    Next += llvm::utostr(GCCVersion[1]);
138249423Sdim    Next += '.';
139249423Sdim    Next += llvm::utostr(GCCVersion[2]);
140249423Sdim
141249423Sdim    // Use that if it exists, otherwise hope the user isn't linking.
142249423Sdim    //
143193323Sed    // FIXME: Drop dependency on gcc's tool chain.
144193323Sed    Tmp = "/usr/lib/gcc/" + Next;
145193323Sed    if (llvm::sys::Path(Tmp).exists())
146249423Sdim      ToolChainDir = Next;
147249423Sdim  }
148249423Sdim
149193323Sed  std::string Path;
150193323Sed  if (getArchName() == "x86_64") {
151193323Sed    Path = getDriver().Dir;
152193323Sed    Path += "/../lib/gcc/";
153193323Sed    Path += ToolChainDir;
154207618Srdivacky    Path += "/x86_64";
155207618Srdivacky    getFilePaths().push_back(Path);
156207618Srdivacky
157207618Srdivacky    Path = "/usr/lib/gcc/";
158207618Srdivacky    Path += ToolChainDir;
159207618Srdivacky    Path += "/x86_64";
160207618Srdivacky    getFilePaths().push_back(Path);
161207618Srdivacky  }
162207618Srdivacky
163207618Srdivacky  Path = getDriver().Dir;
164207618Srdivacky  Path += "/../lib/gcc/";
165207618Srdivacky  Path += ToolChainDir;
166249423Sdim  getFilePaths().push_back(Path);
167249423Sdim
168249423Sdim  Path = "/usr/lib/gcc/";
169251662Sdim  Path += ToolChainDir;
170263508Sdim  getFilePaths().push_back(Path);
171251662Sdim
172251662Sdim  Path = getDriver().Dir;
173193323Sed  Path += "/../libexec/gcc/";
174193323Sed  Path += ToolChainDir;
175193323Sed  getProgramPaths().push_back(Path);
176193323Sed
177193323Sed  Path = "/usr/libexec/gcc/";
178193323Sed  Path += ToolChainDir;
179  getProgramPaths().push_back(Path);
180
181  getProgramPaths().push_back(getDriver().Dir);
182}
183
184Darwin::~Darwin() {
185  // Free tool implementations.
186  for (llvm::DenseMap<unsigned, Tool*>::iterator
187         it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
188    delete it->second;
189}
190
191Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
192  Action::ActionClass Key;
193  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
194    Key = Action::AnalyzeJobClass;
195  else
196    Key = JA.getKind();
197
198  // FIXME: This doesn't belong here, but ideally we will support static soon
199  // anyway.
200  bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
201                    C.getArgs().hasArg(options::OPT_static) ||
202                    C.getArgs().hasArg(options::OPT_fapple_kext));
203  bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
204  bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
205                                             options::OPT_no_integrated_as,
206                                             IsIADefault);
207
208  Tool *&T = Tools[Key];
209  if (!T) {
210    switch (Key) {
211    case Action::InputClass:
212    case Action::BindArchClass:
213      assert(0 && "Invalid tool kind.");
214    case Action::PreprocessJobClass:
215      T = new tools::darwin::Preprocess(*this); break;
216    case Action::AnalyzeJobClass:
217      T = new tools::Clang(*this); break;
218    case Action::PrecompileJobClass:
219    case Action::CompileJobClass:
220      T = new tools::darwin::Compile(*this); break;
221    case Action::AssembleJobClass: {
222      if (UseIntegratedAs)
223        T = new tools::ClangAs(*this);
224      else
225        T = new tools::darwin::Assemble(*this);
226      break;
227    }
228    case Action::LinkJobClass:
229      T = new tools::darwin::Link(*this); break;
230    case Action::LipoJobClass:
231      T = new tools::darwin::Lipo(*this); break;
232    case Action::DsymutilJobClass:
233      T = new tools::darwin::Dsymutil(*this); break;
234    }
235  }
236
237  return *T;
238}
239
240void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
241                                      ArgStringList &CmdArgs) const {
242  std::string Tmp;
243
244  // FIXME: Derive these correctly.
245  if (getArchName() == "x86_64") {
246    CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
247                                         "/x86_64"));
248    // Intentionally duplicated for (temporary) gcc bug compatibility.
249    CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
250                                         "/x86_64"));
251  }
252
253  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
254
255  Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
256  if (llvm::sys::Path(Tmp).exists())
257    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
258  Tmp = getDriver().Dir + "/../lib/gcc";
259  if (llvm::sys::Path(Tmp).exists())
260    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
261  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
262  // Intentionally duplicated for (temporary) gcc bug compatibility.
263  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
264  Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
265  if (llvm::sys::Path(Tmp).exists())
266    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
267  Tmp = getDriver().Dir + "/../lib";
268  if (llvm::sys::Path(Tmp).exists())
269    CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
270  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
271                                       "/../../../" + ToolChainDir));
272  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
273                                       "/../../.."));
274}
275
276void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
277                                      ArgStringList &CmdArgs) const {
278  // Note that this routine is only used for targetting OS X.
279
280  // Derived from libgcc and lib specs but refactored.
281  if (Args.hasArg(options::OPT_static)) {
282    CmdArgs.push_back("-lgcc_static");
283  } else {
284    if (Args.hasArg(options::OPT_static_libgcc)) {
285      CmdArgs.push_back("-lgcc_eh");
286    } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
287      // Derived from darwin_iphoneos_libgcc spec.
288      if (isTargetIPhoneOS()) {
289        CmdArgs.push_back("-lgcc_s.1");
290      } else {
291        CmdArgs.push_back("-lgcc_s.10.5");
292      }
293    } else if (Args.hasArg(options::OPT_shared_libgcc) ||
294               Args.hasFlag(options::OPT_fexceptions,
295                            options::OPT_fno_exceptions) ||
296               Args.hasArg(options::OPT_fgnu_runtime)) {
297      // FIXME: This is probably broken on 10.3?
298      if (isMacosxVersionLT(10, 5))
299        CmdArgs.push_back("-lgcc_s.10.4");
300      else if (isMacosxVersionLT(10, 6))
301        CmdArgs.push_back("-lgcc_s.10.5");
302    } else {
303      if (isMacosxVersionLT(10, 3, 9))
304        ; // Do nothing.
305      else if (isMacosxVersionLT(10, 5))
306        CmdArgs.push_back("-lgcc_s.10.4");
307      else if (isMacosxVersionLT(10, 6))
308        CmdArgs.push_back("-lgcc_s.10.5");
309    }
310
311    if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
312      CmdArgs.push_back("-lgcc");
313      CmdArgs.push_back("-lSystem");
314    } else {
315      CmdArgs.push_back("-lSystem");
316      CmdArgs.push_back("-lgcc");
317    }
318  }
319}
320
321DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
322                         const unsigned (&DarwinVersion)[3])
323  : Darwin(Host, Triple, DarwinVersion)
324{
325  // We expect 'as', 'ld', etc. to be adjacent to our install dir.
326  getProgramPaths().push_back(getDriver().Dir);
327}
328
329void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
330                                       ArgStringList &CmdArgs) const {
331  // The Clang toolchain uses explicit paths for internal libraries.
332
333  // Unfortunately, we still might depend on a few of the libraries that are
334  // only available in the gcc library directory (in particular
335  // libstdc++.dylib). For now, hardcode the path to the known install location.
336  llvm::sys::Path P(getDriver().Dir);
337  P.eraseComponent(); // .../usr/bin -> ../usr
338  P.appendComponent("lib");
339  P.appendComponent("gcc");
340  switch (getTriple().getArch()) {
341  default:
342    assert(0 && "Invalid Darwin arch!");
343  case llvm::Triple::x86:
344  case llvm::Triple::x86_64:
345    P.appendComponent("i686-apple-darwin10");
346    break;
347  case llvm::Triple::arm:
348  case llvm::Triple::thumb:
349    P.appendComponent("arm-apple-darwin10");
350    break;
351  case llvm::Triple::ppc:
352  case llvm::Triple::ppc64:
353    P.appendComponent("powerpc-apple-darwin10");
354    break;
355  }
356  P.appendComponent("4.2.1");
357  if (P.exists())
358    CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
359}
360
361void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
362                                        ArgStringList &CmdArgs) const {
363  // Darwin doesn't support real static executables, don't link any runtime
364  // libraries with -static.
365  if (Args.hasArg(options::OPT_static))
366    return;
367
368  // Reject -static-libgcc for now, we can deal with this when and if someone
369  // cares. This is useful in situations where someone wants to statically link
370  // something like libstdc++, and needs its runtime support routines.
371  if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
372    getDriver().Diag(clang::diag::err_drv_unsupported_opt)
373      << A->getAsString(Args);
374    return;
375  }
376
377  // Otherwise link libSystem, then the dynamic runtime library, and finally any
378  // target specific static runtime library.
379  CmdArgs.push_back("-lSystem");
380
381  // Select the dynamic runtime library and the target specific static library.
382  const char *DarwinStaticLib = 0;
383  if (isTargetIPhoneOS()) {
384    CmdArgs.push_back("-lgcc_s.1");
385
386    // We may need some static functions for armv6/thumb which are required to
387    // be in the same linkage unit as their caller.
388    if (getDarwinArchName(Args) == "armv6")
389      DarwinStaticLib = "libclang_rt.armv6.a";
390  } else {
391    // The dynamic runtime library was merged with libSystem for 10.6 and
392    // beyond; only 10.4 and 10.5 need an additional runtime library.
393    if (isMacosxVersionLT(10, 5))
394      CmdArgs.push_back("-lgcc_s.10.4");
395    else if (isMacosxVersionLT(10, 6))
396      CmdArgs.push_back("-lgcc_s.10.5");
397
398    // For OS X, we only need a static runtime library when targetting 10.4, to
399    // provide versions of the static functions which were omitted from
400    // 10.4.dylib.
401    if (isMacosxVersionLT(10, 5))
402      DarwinStaticLib = "libclang_rt.10.4.a";
403  }
404
405  /// Add the target specific static library, if needed.
406  if (DarwinStaticLib) {
407    llvm::sys::Path P(getDriver().ResourceDir);
408    P.appendComponent("lib");
409    P.appendComponent("darwin");
410    P.appendComponent(DarwinStaticLib);
411
412    // For now, allow missing resource libraries to support developers who may
413    // not have compiler-rt checked out or integrated into their build.
414    if (!P.exists())
415      getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
416        << P.str();
417    else
418      CmdArgs.push_back(Args.MakeArgString(P.str()));
419  }
420}
421
422DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
423                                      const char *BoundArch) const {
424  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
425  const OptTable &Opts = getDriver().getOpts();
426
427  // FIXME: We really want to get out of the tool chain level argument
428  // translation business, as it makes the driver functionality much
429  // more opaque. For now, we follow gcc closely solely for the
430  // purpose of easily achieving feature parity & testability. Once we
431  // have something that works, we should reevaluate each translation
432  // and try to push it down into tool specific logic.
433
434  Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
435  Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
436  if (OSXVersion && iPhoneVersion) {
437    getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
438          << OSXVersion->getAsString(Args)
439          << iPhoneVersion->getAsString(Args);
440    iPhoneVersion = 0;
441  } else if (!OSXVersion && !iPhoneVersion) {
442    // If neither OS X nor iPhoneOS targets were specified, check for
443    // environment defines.
444    const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
445    const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
446
447    // Ignore empty strings.
448    if (OSXTarget && OSXTarget[0] == '\0')
449      OSXTarget = 0;
450    if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
451      iPhoneOSTarget = 0;
452
453    // Diagnose conflicting deployment targets, and choose default platform
454    // based on the tool chain.
455    //
456    // FIXME: Don't hardcode default here.
457    if (OSXTarget && iPhoneOSTarget) {
458      // FIXME: We should see if we can get away with warning or erroring on
459      // this. Perhaps put under -pedantic?
460      if (getTriple().getArch() == llvm::Triple::arm ||
461          getTriple().getArch() == llvm::Triple::thumb)
462        OSXTarget = 0;
463      else
464        iPhoneOSTarget = 0;
465    }
466
467    if (OSXTarget) {
468      const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
469      OSXVersion = DAL->MakeJoinedArg(0, O, OSXTarget);
470      DAL->append(OSXVersion);
471    } else if (iPhoneOSTarget) {
472      const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
473      iPhoneVersion = DAL->MakeJoinedArg(0, O, iPhoneOSTarget);
474      DAL->append(iPhoneVersion);
475    } else {
476      // Otherwise, assume we are targeting OS X.
477      const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
478      OSXVersion = DAL->MakeJoinedArg(0, O, MacosxVersionMin);
479      DAL->append(OSXVersion);
480    }
481  }
482
483  // Set the tool chain target information.
484  unsigned Major, Minor, Micro;
485  bool HadExtra;
486  if (OSXVersion) {
487    assert(!iPhoneVersion && "Unknown target platform!");
488    if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
489                                   Micro, HadExtra) || HadExtra ||
490        Major != 10 || Minor >= 10 || Micro >= 10)
491      getDriver().Diag(clang::diag::err_drv_invalid_version_number)
492        << OSXVersion->getAsString(Args);
493  } else {
494    assert(iPhoneVersion && "Unknown target platform!");
495    if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
496                                   Micro, HadExtra) || HadExtra ||
497        Major >= 10 || Minor >= 100 || Micro >= 100)
498      getDriver().Diag(clang::diag::err_drv_invalid_version_number)
499        << iPhoneVersion->getAsString(Args);
500  }
501  setTarget(iPhoneVersion, Major, Minor, Micro);
502
503  for (ArgList::const_iterator it = Args.begin(),
504         ie = Args.end(); it != ie; ++it) {
505    Arg *A = *it;
506
507    if (A->getOption().matches(options::OPT_Xarch__)) {
508      // FIXME: Canonicalize name.
509      if (getArchName() != A->getValue(Args, 0))
510        continue;
511
512      unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
513      unsigned Prev = Index;
514      Arg *XarchArg = Opts.ParseOneArg(Args, Index);
515
516      // If the argument parsing failed or more than one argument was
517      // consumed, the -Xarch_ argument's parameter tried to consume
518      // extra arguments. Emit an error and ignore.
519      //
520      // We also want to disallow any options which would alter the
521      // driver behavior; that isn't going to work in our model. We
522      // use isDriverOption() as an approximation, although things
523      // like -O4 are going to slip through.
524      if (!XarchArg || Index > Prev + 1 ||
525          XarchArg->getOption().isDriverOption()) {
526       getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
527          << A->getAsString(Args);
528        continue;
529      }
530
531      XarchArg->setBaseArg(A);
532      A = XarchArg;
533
534      DAL->AddSynthesizedArg(A);
535    }
536
537    // Sob. These is strictly gcc compatible for the time being. Apple
538    // gcc translates options twice, which means that self-expanding
539    // options add duplicates.
540    switch ((options::ID) A->getOption().getID()) {
541    default:
542      DAL->append(A);
543      break;
544
545    case options::OPT_mkernel:
546    case options::OPT_fapple_kext:
547      DAL->append(A);
548      DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
549      DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
550      break;
551
552    case options::OPT_dependency_file:
553      DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
554                          A->getValue(Args));
555      break;
556
557    case options::OPT_gfull:
558      DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
559      DAL->AddFlagArg(A,
560               Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
561      break;
562
563    case options::OPT_gused:
564      DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
565      DAL->AddFlagArg(A,
566             Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
567      break;
568
569    case options::OPT_fterminated_vtables:
570    case options::OPT_findirect_virtual_calls:
571      DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
572      DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
573      break;
574
575    case options::OPT_shared:
576      DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
577      break;
578
579    case options::OPT_fconstant_cfstrings:
580      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
581      break;
582
583    case options::OPT_fno_constant_cfstrings:
584      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
585      break;
586
587    case options::OPT_Wnonportable_cfstrings:
588      DAL->AddFlagArg(A,
589                      Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
590      break;
591
592    case options::OPT_Wno_nonportable_cfstrings:
593      DAL->AddFlagArg(A,
594                   Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
595      break;
596
597    case options::OPT_fpascal_strings:
598      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
599      break;
600
601    case options::OPT_fno_pascal_strings:
602      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
603      break;
604    }
605  }
606
607  if (getTriple().getArch() == llvm::Triple::x86 ||
608      getTriple().getArch() == llvm::Triple::x86_64)
609    if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
610      DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
611
612  // Add the arch options based on the particular spelling of -arch, to match
613  // how the driver driver works.
614  if (BoundArch) {
615    llvm::StringRef Name = BoundArch;
616    const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
617    const Option *MArch = Opts.getOption(options::OPT_march_EQ);
618
619    // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
620    // which defines the list of which architectures we accept.
621    if (Name == "ppc")
622      ;
623    else if (Name == "ppc601")
624      DAL->AddJoinedArg(0, MCpu, "601");
625    else if (Name == "ppc603")
626      DAL->AddJoinedArg(0, MCpu, "603");
627    else if (Name == "ppc604")
628      DAL->AddJoinedArg(0, MCpu, "604");
629    else if (Name == "ppc604e")
630      DAL->AddJoinedArg(0, MCpu, "604e");
631    else if (Name == "ppc750")
632      DAL->AddJoinedArg(0, MCpu, "750");
633    else if (Name == "ppc7400")
634      DAL->AddJoinedArg(0, MCpu, "7400");
635    else if (Name == "ppc7450")
636      DAL->AddJoinedArg(0, MCpu, "7450");
637    else if (Name == "ppc970")
638      DAL->AddJoinedArg(0, MCpu, "970");
639
640    else if (Name == "ppc64")
641      DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
642
643    else if (Name == "i386")
644      ;
645    else if (Name == "i486")
646      DAL->AddJoinedArg(0, MArch, "i486");
647    else if (Name == "i586")
648      DAL->AddJoinedArg(0, MArch, "i586");
649    else if (Name == "i686")
650      DAL->AddJoinedArg(0, MArch, "i686");
651    else if (Name == "pentium")
652      DAL->AddJoinedArg(0, MArch, "pentium");
653    else if (Name == "pentium2")
654      DAL->AddJoinedArg(0, MArch, "pentium2");
655    else if (Name == "pentpro")
656      DAL->AddJoinedArg(0, MArch, "pentiumpro");
657    else if (Name == "pentIIm3")
658      DAL->AddJoinedArg(0, MArch, "pentium2");
659
660    else if (Name == "x86_64")
661      DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
662
663    else if (Name == "arm")
664      DAL->AddJoinedArg(0, MArch, "armv4t");
665    else if (Name == "armv4t")
666      DAL->AddJoinedArg(0, MArch, "armv4t");
667    else if (Name == "armv5")
668      DAL->AddJoinedArg(0, MArch, "armv5tej");
669    else if (Name == "xscale")
670      DAL->AddJoinedArg(0, MArch, "xscale");
671    else if (Name == "armv6")
672      DAL->AddJoinedArg(0, MArch, "armv6k");
673    else if (Name == "armv7")
674      DAL->AddJoinedArg(0, MArch, "armv7a");
675
676    else
677      llvm_unreachable("invalid Darwin arch");
678  }
679
680  return DAL;
681}
682
683bool Darwin::IsUnwindTablesDefault() const {
684  // FIXME: Gross; we should probably have some separate target
685  // definition, possibly even reusing the one in clang.
686  return getArchName() == "x86_64";
687}
688
689bool Darwin::UseDwarfDebugFlags() const {
690  if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
691    return S[0] != '\0';
692  return false;
693}
694
695bool Darwin::UseSjLjExceptions() const {
696  // Darwin uses SjLj exceptions on ARM.
697  return (getTriple().getArch() == llvm::Triple::arm ||
698          getTriple().getArch() == llvm::Triple::thumb);
699}
700
701const char *Darwin::GetDefaultRelocationModel() const {
702  return "pic";
703}
704
705const char *Darwin::GetForcedPicModel() const {
706  if (getArchName() == "x86_64")
707    return "pic";
708  return 0;
709}
710
711bool Darwin::SupportsObjCGC() const {
712  // Garbage collection is supported everywhere except on iPhone OS.
713  return !isTargetIPhoneOS();
714}
715
716/// Generic_GCC - A tool chain using the 'gcc' command to perform
717/// all subcommands; this relies on gcc translating the majority of
718/// command line options.
719
720Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
721  : ToolChain(Host, Triple) {
722  getProgramPaths().push_back(getDriver().Dir);
723}
724
725Generic_GCC::~Generic_GCC() {
726  // Free tool implementations.
727  for (llvm::DenseMap<unsigned, Tool*>::iterator
728         it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
729    delete it->second;
730}
731
732Tool &Generic_GCC::SelectTool(const Compilation &C,
733                              const JobAction &JA) const {
734  Action::ActionClass Key;
735  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
736    Key = Action::AnalyzeJobClass;
737  else
738    Key = JA.getKind();
739
740  Tool *&T = Tools[Key];
741  if (!T) {
742    switch (Key) {
743    case Action::InputClass:
744    case Action::BindArchClass:
745      assert(0 && "Invalid tool kind.");
746    case Action::PreprocessJobClass:
747      T = new tools::gcc::Preprocess(*this); break;
748    case Action::PrecompileJobClass:
749      T = new tools::gcc::Precompile(*this); break;
750    case Action::AnalyzeJobClass:
751      T = new tools::Clang(*this); break;
752    case Action::CompileJobClass:
753      T = new tools::gcc::Compile(*this); break;
754    case Action::AssembleJobClass:
755      T = new tools::gcc::Assemble(*this); break;
756    case Action::LinkJobClass:
757      T = new tools::gcc::Link(*this); break;
758
759      // This is a bit ungeneric, but the only platform using a driver
760      // driver is Darwin.
761    case Action::LipoJobClass:
762      T = new tools::darwin::Lipo(*this); break;
763    case Action::DsymutilJobClass:
764      T = new tools::darwin::Dsymutil(*this); break;
765    }
766  }
767
768  return *T;
769}
770
771bool Generic_GCC::IsUnwindTablesDefault() const {
772  // FIXME: Gross; we should probably have some separate target
773  // definition, possibly even reusing the one in clang.
774  return getArchName() == "x86_64";
775}
776
777const char *Generic_GCC::GetDefaultRelocationModel() const {
778  return "static";
779}
780
781const char *Generic_GCC::GetForcedPicModel() const {
782  return 0;
783}
784
785/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
786/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
787/// Currently does not support anything else but compilation.
788
789TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
790  : ToolChain(Host, Triple) {
791  // Path mangling to find libexec
792  std::string Path(getDriver().Dir);
793
794  Path += "/../libexec";
795  getProgramPaths().push_back(Path);
796}
797
798TCEToolChain::~TCEToolChain() {
799  for (llvm::DenseMap<unsigned, Tool*>::iterator
800           it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
801      delete it->second;
802}
803
804bool TCEToolChain::IsMathErrnoDefault() const {
805  return true;
806}
807
808bool TCEToolChain::IsUnwindTablesDefault() const {
809  return false;
810}
811
812const char *TCEToolChain::GetDefaultRelocationModel() const {
813  return "static";
814}
815
816const char *TCEToolChain::GetForcedPicModel() const {
817  return 0;
818}
819
820Tool &TCEToolChain::SelectTool(const Compilation &C,
821                            const JobAction &JA) const {
822  Action::ActionClass Key;
823  Key = Action::AnalyzeJobClass;
824
825  Tool *&T = Tools[Key];
826  if (!T) {
827    switch (Key) {
828    case Action::PreprocessJobClass:
829      T = new tools::gcc::Preprocess(*this); break;
830    case Action::AnalyzeJobClass:
831      T = new tools::Clang(*this); break;
832    default:
833     assert(false && "Unsupported action for TCE target.");
834    }
835  }
836  return *T;
837}
838
839/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
840
841OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
842  : Generic_GCC(Host, Triple) {
843  getFilePaths().push_back(getDriver().Dir + "/../lib");
844  getFilePaths().push_back("/usr/lib");
845}
846
847Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
848  Action::ActionClass Key;
849  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
850    Key = Action::AnalyzeJobClass;
851  else
852    Key = JA.getKind();
853
854  Tool *&T = Tools[Key];
855  if (!T) {
856    switch (Key) {
857    case Action::AssembleJobClass:
858      T = new tools::openbsd::Assemble(*this); break;
859    case Action::LinkJobClass:
860      T = new tools::openbsd::Link(*this); break;
861    default:
862      T = &Generic_GCC::SelectTool(C, JA);
863    }
864  }
865
866  return *T;
867}
868
869/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
870
871FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
872  : Generic_GCC(Host, Triple) {
873  getProgramPaths().push_back(getDriver().Dir + "/../libexec");
874  getProgramPaths().push_back("/usr/libexec");
875  if (Lib32) {
876    getFilePaths().push_back(CLANG_PREFIX "/usr/lib32");
877  } else {
878    getFilePaths().push_back(CLANG_PREFIX "/usr/lib");
879  }
880}
881
882Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
883  Action::ActionClass Key;
884  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
885    Key = Action::AnalyzeJobClass;
886  else
887    Key = JA.getKind();
888
889  Tool *&T = Tools[Key];
890  if (!T) {
891    switch (Key) {
892    case Action::AssembleJobClass:
893      T = new tools::freebsd::Assemble(*this); break;
894    case Action::LinkJobClass:
895      T = new tools::freebsd::Link(*this); break;
896    default:
897      T = &Generic_GCC::SelectTool(C, JA);
898    }
899  }
900
901  return *T;
902}
903
904/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
905
906Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
907  : Generic_GCC(Host, Triple) {
908  getFilePaths().push_back(getDriver().Dir + "/../lib");
909  getFilePaths().push_back("/usr/lib");
910  getFilePaths().push_back("/usr/gnu/lib");
911  getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
912}
913
914Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
915  Action::ActionClass Key;
916  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
917    Key = Action::AnalyzeJobClass;
918  else
919    Key = JA.getKind();
920
921  Tool *&T = Tools[Key];
922  if (!T) {
923    switch (Key) {
924    case Action::AssembleJobClass:
925      T = new tools::minix::Assemble(*this); break;
926    case Action::LinkJobClass:
927      T = new tools::minix::Link(*this); break;
928    default:
929      T = &Generic_GCC::SelectTool(C, JA);
930    }
931  }
932
933  return *T;
934}
935
936/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
937
938AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
939  : Generic_GCC(Host, Triple) {
940
941  getProgramPaths().push_back(getDriver().Dir);
942
943  getFilePaths().push_back(getDriver().Dir + "/../lib");
944  getFilePaths().push_back("/usr/lib");
945  getFilePaths().push_back("/usr/sfw/lib");
946  getFilePaths().push_back("/opt/gcc4/lib");
947  getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
948
949}
950
951Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
952  Action::ActionClass Key;
953  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
954    Key = Action::AnalyzeJobClass;
955  else
956    Key = JA.getKind();
957
958  Tool *&T = Tools[Key];
959  if (!T) {
960    switch (Key) {
961    case Action::AssembleJobClass:
962      T = new tools::auroraux::Assemble(*this); break;
963    case Action::LinkJobClass:
964      T = new tools::auroraux::Link(*this); break;
965    default:
966      T = &Generic_GCC::SelectTool(C, JA);
967    }
968  }
969
970  return *T;
971}
972
973
974/// Linux toolchain (very bare-bones at the moment).
975
976Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
977  : Generic_GCC(Host, Triple) {
978  getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
979  getFilePaths().push_back("/lib/");
980  getFilePaths().push_back("/usr/lib/");
981
982  // Depending on the Linux distribution, any combination of lib{,32,64} is
983  // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
984  // openSUSE uses lib and lib64 for the same purpose.
985  getFilePaths().push_back("/lib32/");
986  getFilePaths().push_back("/usr/lib32/");
987  getFilePaths().push_back("/lib64/");
988  getFilePaths().push_back("/usr/lib64/");
989
990  // FIXME: Figure out some way to get gcc's libdir
991  // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
992  // crtbegin.o/crtend.o/etc., and want static versions of various
993  // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
994  // get away with using shared versions in /usr/lib, though.
995  // We could fall back to the approach we used for includes (a massive
996  // list), but that's messy at best.
997}
998
999/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1000
1001DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1002  : Generic_GCC(Host, Triple) {
1003
1004  // Path mangling to find libexec
1005  getProgramPaths().push_back(getDriver().Dir);
1006
1007  getFilePaths().push_back(getDriver().Dir + "/../lib");
1008  getFilePaths().push_back("/usr/lib");
1009  getFilePaths().push_back("/usr/lib/gcc41");
1010}
1011
1012Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1013  Action::ActionClass Key;
1014  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1015    Key = Action::AnalyzeJobClass;
1016  else
1017    Key = JA.getKind();
1018
1019  Tool *&T = Tools[Key];
1020  if (!T) {
1021    switch (Key) {
1022    case Action::AssembleJobClass:
1023      T = new tools::dragonfly::Assemble(*this); break;
1024    case Action::LinkJobClass:
1025      T = new tools::dragonfly::Link(*this); break;
1026    default:
1027      T = &Generic_GCC::SelectTool(C, JA);
1028    }
1029  }
1030
1031  return *T;
1032}
1033