ToolChains.cpp revision 205408
14Srgrimes//===--- ToolChains.cpp - ToolChain Implementations ---------------------*-===//
24Srgrimes//
34Srgrimes//                     The LLVM Compiler Infrastructure
44Srgrimes//
54Srgrimes// This file is distributed under the University of Illinois Open Source
64Srgrimes// License. See LICENSE.TXT for details.
74Srgrimes//
84Srgrimes//===----------------------------------------------------------------------===//
94Srgrimes
104Srgrimes#include "ToolChains.h"
114Srgrimes
124Srgrimes#include "clang/Driver/Arg.h"
134Srgrimes#include "clang/Driver/ArgList.h"
144Srgrimes#include "clang/Driver/Driver.h"
154Srgrimes#include "clang/Driver/DriverDiagnostic.h"
164Srgrimes#include "clang/Driver/HostInfo.h"
174Srgrimes#include "clang/Driver/OptTable.h"
184Srgrimes#include "clang/Driver/Option.h"
194Srgrimes#include "clang/Driver/Options.h"
204Srgrimes
214Srgrimes#include "llvm/ADT/StringExtras.h"
224Srgrimes#include "llvm/Support/ErrorHandling.h"
234Srgrimes#include "llvm/Support/raw_ostream.h"
244Srgrimes#include "llvm/System/Path.h"
254Srgrimes
262056Swollman#include <cstdlib> // ::getenv
274Srgrimes
28623Srgrimesusing namespace clang::driver;
294Srgrimesusing namespace clang::driver::toolchains;
304Srgrimes
314Srgrimes/// Darwin - Darwin tool chain for i386 and x86_64.
324Srgrimes
334SrgrimesDarwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple,
344Srgrimes               const unsigned (&_DarwinVersion)[3])
354Srgrimes  : ToolChain(Host, Triple), TargetInitialized(false)
364Srgrimes{
372056Swollman  llvm::raw_string_ostream(MacosxVersionMin)
382056Swollman    << "10." << std::max(0, (int)_DarwinVersion[0] - 4) << '.'
392056Swollman    << _DarwinVersion[1];
402056Swollman}
414Srgrimes
424Srgrimes// FIXME: Can we tablegen this?
434Srgrimesstatic const char *GetArmArchForMArch(llvm::StringRef Value) {
444Srgrimes  if (Value == "armv6k")
454Srgrimes    return "armv6";
464Srgrimes
474Srgrimes  if (Value == "armv5tej")
484Srgrimes    return "armv5";
494Srgrimes
504Srgrimes  if (Value == "xscale")
514Srgrimes    return "xscale";
524Srgrimes
534Srgrimes  if (Value == "armv4t")
544Srgrimes    return "armv4t";
554Srgrimes
564Srgrimes  if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
574Srgrimes      Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
584Srgrimes      Value == "armv7m")
594Srgrimes    return "armv7";
604Srgrimes
614Srgrimes  return 0;
624Srgrimes}
634Srgrimes
644Srgrimes// FIXME: Can we tablegen this?
654Srgrimesstatic const char *GetArmArchForMCpu(llvm::StringRef Value) {
664Srgrimes  if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
674Srgrimes      Value == "arm946e-s" || Value == "arm966e-s" ||
684Srgrimes      Value == "arm968e-s" || Value == "arm10e" ||
694Srgrimes      Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
704Srgrimes      Value == "arm1026ej-s")
714Srgrimes    return "armv5";
724Srgrimes
734Srgrimes  if (Value == "xscale")
744Srgrimes    return "xscale";
754Srgrimes
764Srgrimes  if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
774Srgrimes      Value == "arm1176jz-s" || Value == "arm1176jzf-s")
784Srgrimes    return "armv6";
794Srgrimes
804Srgrimes  if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
814Srgrimes    return "armv7";
824Srgrimes
834Srgrimes  return 0;
844Srgrimes}
854Srgrimes
864Srgrimesllvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
874Srgrimes  switch (getTriple().getArch()) {
884Srgrimes  default:
894Srgrimes    return getArchName();
904Srgrimes
914Srgrimes  case llvm::Triple::arm: {
924Srgrimes    if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
934Srgrimes      if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
944Srgrimes        return Arch;
954Srgrimes
964Srgrimes    if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
974Srgrimes      if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
984Srgrimes        return Arch;
994Srgrimes
1004Srgrimes    return "arm";
1014Srgrimes  }
1024Srgrimes  }
1034Srgrimes}
1044Srgrimes
1054SrgrimesDarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
1064Srgrimes                     const unsigned (&DarwinVersion)[3],
1074Srgrimes                     const unsigned (&_GCCVersion)[3])
1084Srgrimes  : Darwin(Host, Triple, DarwinVersion)
1094Srgrimes{
1104Srgrimes  GCCVersion[0] = _GCCVersion[0];
1114Srgrimes  GCCVersion[1] = _GCCVersion[1];
1124Srgrimes  GCCVersion[2] = _GCCVersion[2];
1134Srgrimes
1144Srgrimes  // Set up the tool chain paths to match gcc.
1154Srgrimes  ToolChainDir = "i686-apple-darwin";
1164Srgrimes  ToolChainDir += llvm::utostr(DarwinVersion[0]);
1174Srgrimes  ToolChainDir += "/";
1184Srgrimes  ToolChainDir += llvm::utostr(GCCVersion[0]);
1194Srgrimes  ToolChainDir += '.';
1204Srgrimes  ToolChainDir += llvm::utostr(GCCVersion[1]);
1214Srgrimes  ToolChainDir += '.';
1224Srgrimes  ToolChainDir += llvm::utostr(GCCVersion[2]);
1234Srgrimes
1244Srgrimes  // Try the next major version if that tool chain dir is invalid.
1254Srgrimes  std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
1264Srgrimes  if (!llvm::sys::Path(Tmp).exists()) {
1274Srgrimes    std::string Next = "i686-apple-darwin";
1284Srgrimes    Next += llvm::utostr(DarwinVersion[0] + 1);
1294Srgrimes    Next += "/";
1304Srgrimes    Next += llvm::utostr(GCCVersion[0]);
1314Srgrimes    Next += '.';
1324Srgrimes    Next += llvm::utostr(GCCVersion[1]);
1334Srgrimes    Next += '.';
1344Srgrimes    Next += llvm::utostr(GCCVersion[2]);
1354Srgrimes
1364Srgrimes    // Use that if it exists, otherwise hope the user isn't linking.
1374Srgrimes    //
1384Srgrimes    // FIXME: Drop dependency on gcc's tool chain.
1394Srgrimes    Tmp = "/usr/lib/gcc/" + Next;
1404Srgrimes    if (llvm::sys::Path(Tmp).exists())
1414Srgrimes      ToolChainDir = Next;
1424Srgrimes  }
1434Srgrimes
1444Srgrimes  std::string Path;
1454Srgrimes  if (getArchName() == "x86_64") {
1464Srgrimes    Path = getDriver().Dir;
1474Srgrimes    Path += "/../lib/gcc/";
1484Srgrimes    Path += ToolChainDir;
1494Srgrimes    Path += "/x86_64";
1504Srgrimes    getFilePaths().push_back(Path);
1514Srgrimes
1524Srgrimes    Path = "/usr/lib/gcc/";
1534Srgrimes    Path += ToolChainDir;
1544Srgrimes    Path += "/x86_64";
1554Srgrimes    getFilePaths().push_back(Path);
1564Srgrimes  }
1574Srgrimes
1584Srgrimes  Path = getDriver().Dir;
1594Srgrimes  Path += "/../lib/gcc/";
1604Srgrimes  Path += ToolChainDir;
1614Srgrimes  getFilePaths().push_back(Path);
1624Srgrimes
1634Srgrimes  Path = "/usr/lib/gcc/";
1644Srgrimes  Path += ToolChainDir;
1654Srgrimes  getFilePaths().push_back(Path);
1664Srgrimes
1674Srgrimes  Path = getDriver().Dir;
1684Srgrimes  Path += "/../libexec/gcc/";
1694Srgrimes  Path += ToolChainDir;
1704Srgrimes  getProgramPaths().push_back(Path);
1714Srgrimes
1724Srgrimes  Path = "/usr/libexec/gcc/";
1734Srgrimes  Path += ToolChainDir;
1744Srgrimes  getProgramPaths().push_back(Path);
1754Srgrimes
1764Srgrimes  getProgramPaths().push_back(getDriver().Dir);
1774Srgrimes}
1784Srgrimes
1794SrgrimesDarwin::~Darwin() {
1804Srgrimes  // Free tool implementations.
1814Srgrimes  for (llvm::DenseMap<unsigned, Tool*>::iterator
1824Srgrimes         it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1834Srgrimes    delete it->second;
1844Srgrimes}
1854Srgrimes
1864SrgrimesTool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
1874Srgrimes  Action::ActionClass Key;
1884Srgrimes  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1894Srgrimes    Key = Action::AnalyzeJobClass;
1904Srgrimes  else
1914Srgrimes    Key = JA.getKind();
1924Srgrimes
1934Srgrimes  Tool *&T = Tools[Key];
1944Srgrimes  if (!T) {
1954Srgrimes    switch (Key) {
1964Srgrimes    case Action::InputClass:
1974Srgrimes    case Action::BindArchClass:
1984Srgrimes      assert(0 && "Invalid tool kind.");
1994Srgrimes    case Action::PreprocessJobClass:
2004Srgrimes      T = new tools::darwin::Preprocess(*this); break;
2014Srgrimes    case Action::AnalyzeJobClass:
2024Srgrimes      T = new tools::Clang(*this); break;
2034Srgrimes    case Action::PrecompileJobClass:
2044Srgrimes    case Action::CompileJobClass:
2054Srgrimes      T = new tools::darwin::Compile(*this); break;
2064Srgrimes    case Action::AssembleJobClass:
2074Srgrimes      T = new tools::darwin::Assemble(*this); break;
2084Srgrimes    case Action::LinkJobClass:
2094Srgrimes      T = new tools::darwin::Link(*this); break;
2104Srgrimes    case Action::LipoJobClass:
2114Srgrimes      T = new tools::darwin::Lipo(*this); break;
2124Srgrimes    }
2134Srgrimes  }
2144Srgrimes
2154Srgrimes  return *T;
2164Srgrimes}
2174Srgrimes
2184Srgrimesvoid DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
2194Srgrimes                                      ArgStringList &CmdArgs) const {
2204Srgrimes  // FIXME: Derive these correctly.
2214Srgrimes  if (getArchName() == "x86_64") {
2224Srgrimes    CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
2234Srgrimes                                         "/x86_64"));
2244Srgrimes    // Intentionally duplicated for (temporary) gcc bug compatibility.
2254Srgrimes    CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
2264Srgrimes                                         "/x86_64"));
2274Srgrimes  }
2284Srgrimes  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
2294Srgrimes  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
2304Srgrimes  // Intentionally duplicated for (temporary) gcc bug compatibility.
2314Srgrimes  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
2324Srgrimes  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
2334Srgrimes                                       "/../../../" + ToolChainDir));
2344Srgrimes  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
2354Srgrimes                                       "/../../.."));
2364Srgrimes}
2374Srgrimes
2384Srgrimesvoid DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
2394Srgrimes                                      ArgStringList &CmdArgs) const {
2404Srgrimes  // Note that this routine is only used for targetting OS X.
2414Srgrimes
2424Srgrimes  // Derived from libgcc and lib specs but refactored.
2434Srgrimes  if (Args.hasArg(options::OPT_static)) {
2444Srgrimes    CmdArgs.push_back("-lgcc_static");
2454Srgrimes  } else {
2464Srgrimes    if (Args.hasArg(options::OPT_static_libgcc)) {
2474Srgrimes      CmdArgs.push_back("-lgcc_eh");
2484Srgrimes    } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
2494Srgrimes      // Derived from darwin_iphoneos_libgcc spec.
2504Srgrimes      if (isTargetIPhoneOS()) {
2514Srgrimes        CmdArgs.push_back("-lgcc_s.1");
2524Srgrimes      } else {
2534Srgrimes        CmdArgs.push_back("-lgcc_s.10.5");
2544Srgrimes      }
2554Srgrimes    } else if (Args.hasArg(options::OPT_shared_libgcc) ||
2564Srgrimes               Args.hasFlag(options::OPT_fexceptions,
2574Srgrimes                            options::OPT_fno_exceptions) ||
2584Srgrimes               Args.hasArg(options::OPT_fgnu_runtime)) {
2594Srgrimes      // FIXME: This is probably broken on 10.3?
2604Srgrimes      if (isMacosxVersionLT(10, 5))
2614Srgrimes        CmdArgs.push_back("-lgcc_s.10.4");
2624Srgrimes      else if (isMacosxVersionLT(10, 6))
2634Srgrimes        CmdArgs.push_back("-lgcc_s.10.5");
2644Srgrimes    } else {
2654Srgrimes      if (isMacosxVersionLT(10, 3, 9))
2664Srgrimes        ; // Do nothing.
2674Srgrimes      else if (isMacosxVersionLT(10, 5))
2684Srgrimes        CmdArgs.push_back("-lgcc_s.10.4");
2694Srgrimes      else if (isMacosxVersionLT(10, 6))
2704Srgrimes        CmdArgs.push_back("-lgcc_s.10.5");
2714Srgrimes    }
2724Srgrimes
2734Srgrimes    if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
2744Srgrimes      CmdArgs.push_back("-lgcc");
2754Srgrimes      CmdArgs.push_back("-lSystem");
2764Srgrimes    } else {
2774Srgrimes      CmdArgs.push_back("-lSystem");
2784Srgrimes      CmdArgs.push_back("-lgcc");
2794Srgrimes    }
2804Srgrimes  }
2814Srgrimes}
2824Srgrimes
2834SrgrimesDarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
2844Srgrimes                         const unsigned (&DarwinVersion)[3])
2854Srgrimes  : Darwin(Host, Triple, DarwinVersion)
2864Srgrimes{
2874Srgrimes  // We expect 'as', 'ld', etc. to be adjacent to our install dir.
2884Srgrimes  getProgramPaths().push_back(getDriver().Dir);
2894Srgrimes}
2904Srgrimes
2914Srgrimesvoid DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
2924Srgrimes                                       ArgStringList &CmdArgs) const {
2934Srgrimes  // The Clang toolchain uses explicit paths for internal libraries.
2944Srgrimes}
2954Srgrimes
2964Srgrimesvoid DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
2974Srgrimes                                        ArgStringList &CmdArgs) const {
2984Srgrimes  // Darwin doesn't support real static executables, don't link any runtime
2994Srgrimes  // libraries with -static.
3004Srgrimes  if (Args.hasArg(options::OPT_static))
3014Srgrimes    return;
3024Srgrimes
3034Srgrimes  // Reject -static-libgcc for now, we can deal with this when and if someone
3044Srgrimes  // cares. This is useful in situations where someone wants to statically link
3054Srgrimes  // something like libstdc++, and needs its runtime support routines.
3064Srgrimes  if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
3074Srgrimes    getDriver().Diag(clang::diag::err_drv_unsupported_opt)
3084Srgrimes      << A->getAsString(Args);
3094Srgrimes    return;
3104Srgrimes  }
3114Srgrimes
3124Srgrimes  // Otherwise link libSystem, then the dynamic runtime library, and finally any
3134Srgrimes  // target specific static runtime library.
3144Srgrimes  CmdArgs.push_back("-lSystem");
3154Srgrimes
3164Srgrimes  // Select the dynamic runtime library and the target specific static library.
3174Srgrimes  const char *DarwinStaticLib = 0;
3184Srgrimes  if (isTargetIPhoneOS()) {
3194Srgrimes    CmdArgs.push_back("-lgcc_s.1");
3204Srgrimes
3214Srgrimes    // We may need some static functions for armv6/thumb which are required to
3224Srgrimes    // be in the same linkage unit as their caller.
3234Srgrimes    if (getDarwinArchName(Args) == "armv6")
3244Srgrimes      DarwinStaticLib = "libclang_rt.armv6.a";
3254Srgrimes  } else {
3264Srgrimes    // The dynamic runtime library was merged with libSystem for 10.6 and
3274Srgrimes    // beyond; only 10.4 and 10.5 need an additional runtime library.
3284Srgrimes    if (isMacosxVersionLT(10, 5))
3294Srgrimes      CmdArgs.push_back("-lgcc_s.10.4");
3304Srgrimes    else if (isMacosxVersionLT(10, 6))
3314Srgrimes      CmdArgs.push_back("-lgcc_s.10.5");
3324Srgrimes
3334Srgrimes    // For OS X, we only need a static runtime library when targetting 10.4, to
3344Srgrimes    // provide versions of the static functions which were omitted from
3354Srgrimes    // 10.4.dylib.
3364Srgrimes    if (isMacosxVersionLT(10, 5))
3374Srgrimes      DarwinStaticLib = "libclang_rt.10.4.a";
3384Srgrimes  }
3394Srgrimes
3404Srgrimes  /// Add the target specific static library, if needed.
3414Srgrimes  if (DarwinStaticLib) {
3424Srgrimes    llvm::sys::Path P(getDriver().ResourceDir);
3434Srgrimes    P.appendComponent("lib");
3444Srgrimes    P.appendComponent("darwin");
3454Srgrimes    P.appendComponent(DarwinStaticLib);
3464Srgrimes
3474Srgrimes    // For now, allow missing resource libraries to support developers who may
3484Srgrimes    // not have compiler-rt checked out or integrated into their build.
3494Srgrimes    if (!P.exists())
3504Srgrimes      getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
3514Srgrimes        << P.str();
3524Srgrimes    else
3534Srgrimes      CmdArgs.push_back(Args.MakeArgString(P.str()));
3544Srgrimes  }
3554Srgrimes}
3564Srgrimes
3574SrgrimesDerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
3584Srgrimes                                      const char *BoundArch) const {
3594Srgrimes  DerivedArgList *DAL = new DerivedArgList(Args, false);
3604Srgrimes  const OptTable &Opts = getDriver().getOpts();
3614Srgrimes
3624Srgrimes  // FIXME: We really want to get out of the tool chain level argument
3634Srgrimes  // translation business, as it makes the driver functionality much
3644Srgrimes  // more opaque. For now, we follow gcc closely solely for the
3654Srgrimes  // purpose of easily achieving feature parity & testability. Once we
3664Srgrimes  // have something that works, we should reevaluate each translation
3674Srgrimes  // and try to push it down into tool specific logic.
3684Srgrimes
3694Srgrimes  Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
3704Srgrimes  Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
3714Srgrimes  if (OSXVersion && iPhoneVersion) {
3724Srgrimes    getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
3734Srgrimes          << OSXVersion->getAsString(Args)
3744Srgrimes          << iPhoneVersion->getAsString(Args);
3754Srgrimes    iPhoneVersion = 0;
3764Srgrimes  } else if (!OSXVersion && !iPhoneVersion) {
3774Srgrimes    // If neither OS X nor iPhoneOS targets were specified, check for
3784Srgrimes    // environment defines.
3794Srgrimes    const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
3804Srgrimes    const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
3814Srgrimes
3824Srgrimes    // Ignore empty strings.
3834Srgrimes    if (OSXTarget && OSXTarget[0] == '\0')
3844Srgrimes      OSXTarget = 0;
3854Srgrimes    if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
3864Srgrimes      iPhoneOSTarget = 0;
3874Srgrimes
3884Srgrimes    // Diagnose conflicting deployment targets, and choose default platform
3894Srgrimes    // based on the tool chain.
3904Srgrimes    //
3914Srgrimes    // FIXME: Don't hardcode default here.
392    if (OSXTarget && iPhoneOSTarget) {
393      // FIXME: We should see if we can get away with warning or erroring on
394      // this. Perhaps put under -pedantic?
395      if (getTriple().getArch() == llvm::Triple::arm ||
396          getTriple().getArch() == llvm::Triple::thumb)
397        OSXTarget = 0;
398      else
399        iPhoneOSTarget = 0;
400    }
401
402    if (OSXTarget) {
403      const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
404      OSXVersion = DAL->MakeJoinedArg(0, O, OSXTarget);
405      DAL->append(OSXVersion);
406    } else if (iPhoneOSTarget) {
407      const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
408      iPhoneVersion = DAL->MakeJoinedArg(0, O, iPhoneOSTarget);
409      DAL->append(iPhoneVersion);
410    } else {
411      // Otherwise, choose a default platform based on the tool chain.
412      //
413      // FIXME: Don't hardcode default here.
414      if (getTriple().getArch() == llvm::Triple::arm ||
415          getTriple().getArch() == llvm::Triple::thumb) {
416        const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
417        iPhoneVersion = DAL->MakeJoinedArg(0, O, "3.0");
418        DAL->append(iPhoneVersion);
419      } else {
420        const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
421        OSXVersion = DAL->MakeJoinedArg(0, O, MacosxVersionMin);
422        DAL->append(OSXVersion);
423      }
424    }
425  }
426
427  // Set the tool chain target information.
428  unsigned Major, Minor, Micro;
429  bool HadExtra;
430  if (OSXVersion) {
431    assert(!iPhoneVersion && "Unknown target platform!");
432    if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
433                                   Micro, HadExtra) || HadExtra ||
434        Major != 10 || Minor >= 10 || Micro >= 10)
435      getDriver().Diag(clang::diag::err_drv_invalid_version_number)
436        << OSXVersion->getAsString(Args);
437  } else {
438    assert(iPhoneVersion && "Unknown target platform!");
439    if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
440                                   Micro, HadExtra) || HadExtra ||
441        Major >= 10 || Minor >= 100 || Micro >= 100)
442      getDriver().Diag(clang::diag::err_drv_invalid_version_number)
443        << iPhoneVersion->getAsString(Args);
444  }
445  setTarget(iPhoneVersion, Major, Minor, Micro);
446
447  for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
448    Arg *A = *it;
449
450    if (A->getOption().matches(options::OPT_Xarch__)) {
451      // FIXME: Canonicalize name.
452      if (getArchName() != A->getValue(Args, 0))
453        continue;
454
455      // FIXME: The arg is leaked here, and we should have a nicer
456      // interface for this.
457      unsigned Prev, Index = Prev = A->getIndex() + 1;
458      Arg *XarchArg = Opts.ParseOneArg(Args, Index);
459
460      // If the argument parsing failed or more than one argument was
461      // consumed, the -Xarch_ argument's parameter tried to consume
462      // extra arguments. Emit an error and ignore.
463      //
464      // We also want to disallow any options which would alter the
465      // driver behavior; that isn't going to work in our model. We
466      // use isDriverOption() as an approximation, although things
467      // like -O4 are going to slip through.
468      if (!XarchArg || Index > Prev + 1 ||
469          XarchArg->getOption().isDriverOption()) {
470       getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
471          << A->getAsString(Args);
472        continue;
473      }
474
475      XarchArg->setBaseArg(A);
476      A = XarchArg;
477    }
478
479    // Sob. These is strictly gcc compatible for the time being. Apple
480    // gcc translates options twice, which means that self-expanding
481    // options add duplicates.
482    switch ((options::ID) A->getOption().getID()) {
483    default:
484      DAL->append(A);
485      break;
486
487    case options::OPT_mkernel:
488    case options::OPT_fapple_kext:
489      DAL->append(A);
490      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
491      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
492      break;
493
494    case options::OPT_dependency_file:
495      DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
496                                       A->getValue(Args)));
497      break;
498
499    case options::OPT_gfull:
500      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
501      DAL->append(DAL->MakeFlagArg(A,
502             Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
503      break;
504
505    case options::OPT_gused:
506      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
507      DAL->append(DAL->MakeFlagArg(A,
508             Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
509      break;
510
511    case options::OPT_fterminated_vtables:
512    case options::OPT_findirect_virtual_calls:
513      DAL->append(DAL->MakeFlagArg(A,
514                                   Opts.getOption(options::OPT_fapple_kext)));
515      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
516      break;
517
518    case options::OPT_shared:
519      DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
520      break;
521
522    case options::OPT_fconstant_cfstrings:
523      DAL->append(DAL->MakeFlagArg(A,
524                             Opts.getOption(options::OPT_mconstant_cfstrings)));
525      break;
526
527    case options::OPT_fno_constant_cfstrings:
528      DAL->append(DAL->MakeFlagArg(A,
529                          Opts.getOption(options::OPT_mno_constant_cfstrings)));
530      break;
531
532    case options::OPT_Wnonportable_cfstrings:
533      DAL->append(DAL->MakeFlagArg(A,
534                     Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
535      break;
536
537    case options::OPT_Wno_nonportable_cfstrings:
538      DAL->append(DAL->MakeFlagArg(A,
539                  Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
540      break;
541
542    case options::OPT_fpascal_strings:
543      DAL->append(DAL->MakeFlagArg(A,
544                                 Opts.getOption(options::OPT_mpascal_strings)));
545      break;
546
547    case options::OPT_fno_pascal_strings:
548      DAL->append(DAL->MakeFlagArg(A,
549                              Opts.getOption(options::OPT_mno_pascal_strings)));
550      break;
551    }
552  }
553
554  if (getTriple().getArch() == llvm::Triple::x86 ||
555      getTriple().getArch() == llvm::Triple::x86_64)
556    if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
557      DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
558                                     "core2"));
559
560  // Add the arch options based on the particular spelling of -arch, to match
561  // how the driver driver works.
562  if (BoundArch) {
563    llvm::StringRef Name = BoundArch;
564    const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
565    const Option *MArch = Opts.getOption(options::OPT_march_EQ);
566
567    // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
568    // which defines the list of which architectures we accept.
569    if (Name == "ppc")
570      ;
571    else if (Name == "ppc601")
572      DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
573    else if (Name == "ppc603")
574      DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
575    else if (Name == "ppc604")
576      DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
577    else if (Name == "ppc604e")
578      DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
579    else if (Name == "ppc750")
580      DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
581    else if (Name == "ppc7400")
582      DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
583    else if (Name == "ppc7450")
584      DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
585    else if (Name == "ppc970")
586      DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
587
588    else if (Name == "ppc64")
589      DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
590
591    else if (Name == "i386")
592      ;
593    else if (Name == "i486")
594      DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
595    else if (Name == "i586")
596      DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
597    else if (Name == "i686")
598      DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
599    else if (Name == "pentium")
600      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
601    else if (Name == "pentium2")
602      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
603    else if (Name == "pentpro")
604      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
605    else if (Name == "pentIIm3")
606      DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
607
608    else if (Name == "x86_64")
609      DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
610
611    else if (Name == "arm")
612      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
613    else if (Name == "armv4t")
614      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
615    else if (Name == "armv5")
616      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
617    else if (Name == "xscale")
618      DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
619    else if (Name == "armv6")
620      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
621    else if (Name == "armv7")
622      DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
623
624    else
625      llvm_unreachable("invalid Darwin arch");
626  }
627
628  return DAL;
629}
630
631bool Darwin::IsUnwindTablesDefault() const {
632  // FIXME: Gross; we should probably have some separate target
633  // definition, possibly even reusing the one in clang.
634  return getArchName() == "x86_64";
635}
636
637bool Darwin::UseDwarfDebugFlags() const {
638  if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
639    return S[0] != '\0';
640  return false;
641}
642
643bool Darwin::UseSjLjExceptions() const {
644  // Darwin uses SjLj exceptions on ARM.
645  return (getTriple().getArch() == llvm::Triple::arm ||
646          getTriple().getArch() == llvm::Triple::thumb);
647}
648
649const char *Darwin::GetDefaultRelocationModel() const {
650  return "pic";
651}
652
653const char *Darwin::GetForcedPicModel() const {
654  if (getArchName() == "x86_64")
655    return "pic";
656  return 0;
657}
658
659/// Generic_GCC - A tool chain using the 'gcc' command to perform
660/// all subcommands; this relies on gcc translating the majority of
661/// command line options.
662
663Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
664  : ToolChain(Host, Triple) {
665  getProgramPaths().push_back(getDriver().Dir);
666}
667
668Generic_GCC::~Generic_GCC() {
669  // Free tool implementations.
670  for (llvm::DenseMap<unsigned, Tool*>::iterator
671         it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
672    delete it->second;
673}
674
675Tool &Generic_GCC::SelectTool(const Compilation &C,
676                              const JobAction &JA) const {
677  Action::ActionClass Key;
678  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
679    Key = Action::AnalyzeJobClass;
680  else
681    Key = JA.getKind();
682
683  Tool *&T = Tools[Key];
684  if (!T) {
685    switch (Key) {
686    case Action::InputClass:
687    case Action::BindArchClass:
688      assert(0 && "Invalid tool kind.");
689    case Action::PreprocessJobClass:
690      T = new tools::gcc::Preprocess(*this); break;
691    case Action::PrecompileJobClass:
692      T = new tools::gcc::Precompile(*this); break;
693    case Action::AnalyzeJobClass:
694      T = new tools::Clang(*this); break;
695    case Action::CompileJobClass:
696      T = new tools::gcc::Compile(*this); break;
697    case Action::AssembleJobClass:
698      T = new tools::gcc::Assemble(*this); break;
699    case Action::LinkJobClass:
700      T = new tools::gcc::Link(*this); break;
701
702      // This is a bit ungeneric, but the only platform using a driver
703      // driver is Darwin.
704    case Action::LipoJobClass:
705      T = new tools::darwin::Lipo(*this); break;
706    }
707  }
708
709  return *T;
710}
711
712bool Generic_GCC::IsUnwindTablesDefault() const {
713  // FIXME: Gross; we should probably have some separate target
714  // definition, possibly even reusing the one in clang.
715  return getArchName() == "x86_64";
716}
717
718const char *Generic_GCC::GetDefaultRelocationModel() const {
719  return "static";
720}
721
722const char *Generic_GCC::GetForcedPicModel() const {
723  return 0;
724}
725
726DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
727                                           const char *BoundArch) const {
728  return new DerivedArgList(Args, true);
729}
730
731
732/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
733/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
734/// Currently does not support anything else but compilation.
735
736TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
737  : ToolChain(Host, Triple) {
738  // Path mangling to find libexec
739  std::string Path(getDriver().Dir);
740
741  Path += "/../libexec";
742  getProgramPaths().push_back(Path);
743}
744
745TCEToolChain::~TCEToolChain() {
746  for (llvm::DenseMap<unsigned, Tool*>::iterator
747           it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
748      delete it->second;
749}
750
751bool TCEToolChain::IsMathErrnoDefault() const {
752  return true;
753}
754
755bool TCEToolChain::IsUnwindTablesDefault() const {
756  return false;
757}
758
759const char *TCEToolChain::GetDefaultRelocationModel() const {
760  return "static";
761}
762
763const char *TCEToolChain::GetForcedPicModel() const {
764  return 0;
765}
766
767Tool &TCEToolChain::SelectTool(const Compilation &C,
768                            const JobAction &JA) const {
769  Action::ActionClass Key;
770  Key = Action::AnalyzeJobClass;
771
772  Tool *&T = Tools[Key];
773  if (!T) {
774    switch (Key) {
775    case Action::PreprocessJobClass:
776      T = new tools::gcc::Preprocess(*this); break;
777    case Action::AnalyzeJobClass:
778      T = new tools::Clang(*this); break;
779    default:
780     assert(false && "Unsupported action for TCE target.");
781    }
782  }
783  return *T;
784}
785
786DerivedArgList *TCEToolChain::TranslateArgs(InputArgList &Args,
787                                            const char *BoundArch) const {
788  return new DerivedArgList(Args, true);
789}
790
791/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
792
793OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
794  : Generic_GCC(Host, Triple) {
795  getFilePaths().push_back(getDriver().Dir + "/../lib");
796  getFilePaths().push_back("/usr/lib");
797}
798
799Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
800  Action::ActionClass Key;
801  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
802    Key = Action::AnalyzeJobClass;
803  else
804    Key = JA.getKind();
805
806  Tool *&T = Tools[Key];
807  if (!T) {
808    switch (Key) {
809    case Action::AssembleJobClass:
810      T = new tools::openbsd::Assemble(*this); break;
811    case Action::LinkJobClass:
812      T = new tools::openbsd::Link(*this); break;
813    default:
814      T = &Generic_GCC::SelectTool(C, JA);
815    }
816  }
817
818  return *T;
819}
820
821/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
822
823FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
824  : Generic_GCC(Host, Triple) {
825  if (Lib32) {
826    getFilePaths().push_back(getDriver().Dir + "/../lib32");
827    getFilePaths().push_back("/usr/lib32");
828  } else {
829    getFilePaths().push_back(getDriver().Dir + "/../lib");
830    getFilePaths().push_back("/usr/lib");
831  }
832}
833
834Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
835  Action::ActionClass Key;
836  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
837    Key = Action::AnalyzeJobClass;
838  else
839    Key = JA.getKind();
840
841  Tool *&T = Tools[Key];
842  if (!T) {
843    switch (Key) {
844    case Action::AssembleJobClass:
845      T = new tools::freebsd::Assemble(*this); break;
846    case Action::LinkJobClass:
847      T = new tools::freebsd::Link(*this); break;
848    default:
849      T = &Generic_GCC::SelectTool(C, JA);
850    }
851  }
852
853  return *T;
854}
855
856/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
857
858AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
859  : Generic_GCC(Host, Triple) {
860
861  getProgramPaths().push_back(getDriver().Dir);
862
863  getFilePaths().push_back(getDriver().Dir + "/../lib");
864  getFilePaths().push_back("/usr/lib");
865  getFilePaths().push_back("/usr/sfw/lib");
866  getFilePaths().push_back("/opt/gcc4/lib");
867  getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
868
869}
870
871Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
872  Action::ActionClass Key;
873  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
874    Key = Action::AnalyzeJobClass;
875  else
876    Key = JA.getKind();
877
878  Tool *&T = Tools[Key];
879  if (!T) {
880    switch (Key) {
881    case Action::AssembleJobClass:
882      T = new tools::auroraux::Assemble(*this); break;
883    case Action::LinkJobClass:
884      T = new tools::auroraux::Link(*this); break;
885    default:
886      T = &Generic_GCC::SelectTool(C, JA);
887    }
888  }
889
890  return *T;
891}
892
893
894/// Linux toolchain (very bare-bones at the moment).
895
896Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
897  : Generic_GCC(Host, Triple) {
898  getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
899  getFilePaths().push_back("/lib/");
900  getFilePaths().push_back("/usr/lib/");
901
902  // Depending on the Linux distribution, any combination of lib{,32,64} is
903  // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
904  // openSUSE uses lib and lib64 for the same purpose.
905  getFilePaths().push_back("/lib32/");
906  getFilePaths().push_back("/usr/lib32/");
907  getFilePaths().push_back("/lib64/");
908  getFilePaths().push_back("/usr/lib64/");
909
910  // FIXME: Figure out some way to get gcc's libdir
911  // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
912  // crtbegin.o/crtend.o/etc., and want static versions of various
913  // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
914  // get away with using shared versions in /usr/lib, though.
915  // We could fall back to the approach we used for includes (a massive
916  // list), but that's messy at best.
917}
918
919/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
920
921DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
922  : Generic_GCC(Host, Triple) {
923
924  // Path mangling to find libexec
925  getProgramPaths().push_back(getDriver().Dir);
926
927  getFilePaths().push_back(getDriver().Dir + "/../lib");
928  getFilePaths().push_back("/usr/lib");
929  getFilePaths().push_back("/usr/lib/gcc41");
930}
931
932Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
933  Action::ActionClass Key;
934  if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
935    Key = Action::AnalyzeJobClass;
936  else
937    Key = JA.getKind();
938
939  Tool *&T = Tools[Key];
940  if (!T) {
941    switch (Key) {
942    case Action::AssembleJobClass:
943      T = new tools::dragonfly::Assemble(*this); break;
944    case Action::LinkJobClass:
945      T = new tools::dragonfly::Link(*this); break;
946    default:
947      T = &Generic_GCC::SelectTool(C, JA);
948    }
949  }
950
951  return *T;
952}
953