Home | History | Annotate | Download | only in Driver
      1 //===--- Tools.cpp - Tools 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 "Tools.h"
     11 
     12 #include "clang/Driver/Action.h"
     13 #include "clang/Driver/Arg.h"
     14 #include "clang/Driver/ArgList.h"
     15 #include "clang/Driver/Driver.h"
     16 #include "clang/Driver/DriverDiagnostic.h"
     17 #include "clang/Driver/Compilation.h"
     18 #include "clang/Driver/Job.h"
     19 #include "clang/Driver/HostInfo.h"
     20 #include "clang/Driver/ObjCRuntime.h"
     21 #include "clang/Driver/Option.h"
     22 #include "clang/Driver/Options.h"
     23 #include "clang/Driver/ToolChain.h"
     24 #include "clang/Driver/Util.h"
     25 
     26 #include "llvm/ADT/SmallString.h"
     27 #include "llvm/ADT/StringSwitch.h"
     28 #include "llvm/ADT/Twine.h"
     29 #include "llvm/Support/FileSystem.h"
     30 #include "llvm/Support/Format.h"
     31 #include "llvm/Support/raw_ostream.h"
     32 #include "llvm/Support/Host.h"
     33 #include "llvm/Support/Process.h"
     34 #include "llvm/Support/ErrorHandling.h"
     35 
     36 #include "InputInfo.h"
     37 #include "ToolChains.h"
     38 
     39 #ifdef __CYGWIN__
     40 #include <cygwin/version.h>
     41 #if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
     42 #define IS_CYGWIN15 1
     43 #endif
     44 #endif
     45 
     46 using namespace clang::driver;
     47 using namespace clang::driver::tools;
     48 using namespace clang;
     49 
     50 /// CheckPreprocessingOptions - Perform some validation of preprocessing
     51 /// arguments that is shared with gcc.
     52 static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
     53   if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
     54     if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP)
     55       D.Diag(diag::err_drv_argument_only_allowed_with)
     56         << A->getAsString(Args) << "-E";
     57 }
     58 
     59 /// CheckCodeGenerationOptions - Perform some validation of code generation
     60 /// arguments that is shared with gcc.
     61 static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
     62   // In gcc, only ARM checks this, but it seems reasonable to check universally.
     63   if (Args.hasArg(options::OPT_static))
     64     if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
     65                                        options::OPT_mdynamic_no_pic))
     66       D.Diag(diag::err_drv_argument_not_allowed_with)
     67         << A->getAsString(Args) << "-static";
     68 }
     69 
     70 // Quote target names for inclusion in GNU Make dependency files.
     71 // Only the characters '$', '#', ' ', '\t' are quoted.
     72 static void QuoteTarget(StringRef Target,
     73                         SmallVectorImpl<char> &Res) {
     74   for (unsigned i = 0, e = Target.size(); i != e; ++i) {
     75     switch (Target[i]) {
     76     case ' ':
     77     case '\t':
     78       // Escape the preceding backslashes
     79       for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
     80         Res.push_back('\\');
     81 
     82       // Escape the space/tab
     83       Res.push_back('\\');
     84       break;
     85     case '$':
     86       Res.push_back('$');
     87       break;
     88     case '#':
     89       Res.push_back('\\');
     90       break;
     91     default:
     92       break;
     93     }
     94 
     95     Res.push_back(Target[i]);
     96   }
     97 }
     98 
     99 static void AddLinkerInputs(const ToolChain &TC,
    100                             const InputInfoList &Inputs, const ArgList &Args,
    101                             ArgStringList &CmdArgs) {
    102   const Driver &D = TC.getDriver();
    103 
    104   // Add extra linker input arguments which are not treated as inputs
    105   // (constructed via -Xarch_).
    106   Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
    107 
    108   for (InputInfoList::const_iterator
    109          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
    110     const InputInfo &II = *it;
    111 
    112     if (!TC.HasNativeLLVMSupport()) {
    113       // Don't try to pass LLVM inputs unless we have native support.
    114       if (II.getType() == types::TY_LLVM_IR ||
    115           II.getType() == types::TY_LTO_IR ||
    116           II.getType() == types::TY_LLVM_BC ||
    117           II.getType() == types::TY_LTO_BC)
    118         D.Diag(diag::err_drv_no_linker_llvm_support)
    119           << TC.getTripleString();
    120     }
    121 
    122     // Add filenames immediately.
    123     if (II.isFilename()) {
    124       CmdArgs.push_back(II.getFilename());
    125       continue;
    126     }
    127 
    128     // Otherwise, this is a linker input argument.
    129     const Arg &A = II.getInputArg();
    130 
    131     // Handle reserved library options.
    132     if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
    133       TC.AddCXXStdlibLibArgs(Args, CmdArgs);
    134     } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
    135       TC.AddCCKextLibArgs(Args, CmdArgs);
    136     } else
    137       A.renderAsInput(Args, CmdArgs);
    138   }
    139 }
    140 
    141 /// \brief Determine whether Objective-C automated reference counting is
    142 /// enabled.
    143 static bool isObjCAutoRefCount(const ArgList &Args) {
    144   return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
    145 }
    146 
    147 static void addProfileRT(const ToolChain &TC, const ArgList &Args,
    148                          ArgStringList &CmdArgs,
    149                          llvm::Triple Triple) {
    150   if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
    151         Args.hasArg(options::OPT_fprofile_generate) ||
    152         Args.hasArg(options::OPT_fcreate_profile) ||
    153         Args.hasArg(options::OPT_coverage)))
    154     return;
    155 
    156   // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
    157   // the link line. We cannot do the same thing because unlike gcov there is a
    158   // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
    159   // not supported by old linkers.
    160   Twine ProfileRT =
    161     Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.a";
    162 
    163   if (Triple.isOSDarwin()) {
    164     // On Darwin, if the static library doesn't exist try the dylib.
    165     bool Exists;
    166     if (llvm::sys::fs::exists(ProfileRT.str(), Exists) || !Exists)
    167       ProfileRT =
    168         Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.dylib";
    169   }
    170 
    171   CmdArgs.push_back(Args.MakeArgString(ProfileRT));
    172 }
    173 
    174 static void AddIncludeDirectoryList(const ArgList &Args,
    175                                     ArgStringList &CmdArgs,
    176                                     const char *ArgName,
    177                                     const char *DirList) {
    178   if (!DirList)
    179     return; // Nothing to do.
    180 
    181   StringRef Dirs(DirList);
    182   if (Dirs.empty()) // Empty string should not add '.'.
    183     return;
    184 
    185   StringRef::size_type Delim;
    186   while ((Delim = Dirs.find(llvm::sys::PathSeparator)) != StringRef::npos) {
    187     if (Delim == 0) { // Leading colon.
    188       CmdArgs.push_back(ArgName);
    189       CmdArgs.push_back(".");
    190     } else {
    191       CmdArgs.push_back(ArgName);
    192       CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
    193     }
    194     Dirs = Dirs.substr(Delim + 1);
    195   }
    196 
    197   if (Dirs.empty()) { // Trailing colon.
    198     CmdArgs.push_back(ArgName);
    199     CmdArgs.push_back(".");
    200   } else { // Add the last path.
    201     CmdArgs.push_back(ArgName);
    202     CmdArgs.push_back(Args.MakeArgString(Dirs));
    203   }
    204 }
    205 
    206 void Clang::AddPreprocessingOptions(const Driver &D,
    207                                     const ArgList &Args,
    208                                     ArgStringList &CmdArgs,
    209                                     const InputInfo &Output,
    210                                     const InputInfoList &Inputs) const {
    211   Arg *A;
    212 
    213   CheckPreprocessingOptions(D, Args);
    214 
    215   Args.AddLastArg(CmdArgs, options::OPT_C);
    216   Args.AddLastArg(CmdArgs, options::OPT_CC);
    217 
    218   // Handle dependency file generation.
    219   if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
    220       (A = Args.getLastArg(options::OPT_MD)) ||
    221       (A = Args.getLastArg(options::OPT_MMD))) {
    222     // Determine the output location.
    223     const char *DepFile;
    224     if (Output.getType() == types::TY_Dependencies) {
    225       DepFile = Output.getFilename();
    226     } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
    227       DepFile = MF->getValue(Args);
    228     } else if (A->getOption().matches(options::OPT_M) ||
    229                A->getOption().matches(options::OPT_MM)) {
    230       DepFile = "-";
    231     } else {
    232       DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
    233     }
    234     CmdArgs.push_back("-dependency-file");
    235     CmdArgs.push_back(DepFile);
    236 
    237     // Add a default target if one wasn't specified.
    238     if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
    239       const char *DepTarget;
    240 
    241       // If user provided -o, that is the dependency target, except
    242       // when we are only generating a dependency file.
    243       Arg *OutputOpt = Args.getLastArg(options::OPT_o);
    244       if (OutputOpt && Output.getType() != types::TY_Dependencies) {
    245         DepTarget = OutputOpt->getValue(Args);
    246       } else {
    247         // Otherwise derive from the base input.
    248         //
    249         // FIXME: This should use the computed output file location.
    250         llvm::SmallString<128> P(Inputs[0].getBaseInput());
    251         llvm::sys::path::replace_extension(P, "o");
    252         DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
    253       }
    254 
    255       CmdArgs.push_back("-MT");
    256       llvm::SmallString<128> Quoted;
    257       QuoteTarget(DepTarget, Quoted);
    258       CmdArgs.push_back(Args.MakeArgString(Quoted));
    259     }
    260 
    261     if (A->getOption().matches(options::OPT_M) ||
    262         A->getOption().matches(options::OPT_MD))
    263       CmdArgs.push_back("-sys-header-deps");
    264   }
    265 
    266   if (Args.hasArg(options::OPT_MG)) {
    267     if (!A || A->getOption().matches(options::OPT_MD) ||
    268               A->getOption().matches(options::OPT_MMD))
    269       D.Diag(diag::err_drv_mg_requires_m_or_mm);
    270     CmdArgs.push_back("-MG");
    271   }
    272 
    273   Args.AddLastArg(CmdArgs, options::OPT_MP);
    274 
    275   // Convert all -MQ <target> args to -MT <quoted target>
    276   for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
    277                                              options::OPT_MQ),
    278          ie = Args.filtered_end(); it != ie; ++it) {
    279     const Arg *A = *it;
    280     A->claim();
    281 
    282     if (A->getOption().matches(options::OPT_MQ)) {
    283       CmdArgs.push_back("-MT");
    284       llvm::SmallString<128> Quoted;
    285       QuoteTarget(A->getValue(Args), Quoted);
    286       CmdArgs.push_back(Args.MakeArgString(Quoted));
    287 
    288     // -MT flag - no change
    289     } else {
    290       A->render(Args, CmdArgs);
    291     }
    292   }
    293 
    294   // Add -i* options, and automatically translate to
    295   // -include-pch/-include-pth for transparent PCH support. It's
    296   // wonky, but we include looking for .gch so we can support seamless
    297   // replacement into a build system already set up to be generating
    298   // .gch files.
    299   bool RenderedImplicitInclude = false;
    300   for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
    301          ie = Args.filtered_end(); it != ie; ++it) {
    302     const Arg *A = it;
    303 
    304     if (A->getOption().matches(options::OPT_include)) {
    305       bool IsFirstImplicitInclude = !RenderedImplicitInclude;
    306       RenderedImplicitInclude = true;
    307 
    308       // Use PCH if the user requested it.
    309       bool UsePCH = D.CCCUsePCH;
    310 
    311       bool FoundPTH = false;
    312       bool FoundPCH = false;
    313       llvm::sys::Path P(A->getValue(Args));
    314       bool Exists;
    315       if (UsePCH) {
    316         P.appendSuffix("pch");
    317         if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
    318           FoundPCH = true;
    319         else
    320           P.eraseSuffix();
    321       }
    322 
    323       if (!FoundPCH) {
    324         P.appendSuffix("pth");
    325         if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
    326           FoundPTH = true;
    327         else
    328           P.eraseSuffix();
    329       }
    330 
    331       if (!FoundPCH && !FoundPTH) {
    332         P.appendSuffix("gch");
    333         if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
    334           FoundPCH = UsePCH;
    335           FoundPTH = !UsePCH;
    336         }
    337         else
    338           P.eraseSuffix();
    339       }
    340 
    341       if (FoundPCH || FoundPTH) {
    342         if (IsFirstImplicitInclude) {
    343           A->claim();
    344           if (UsePCH)
    345             CmdArgs.push_back("-include-pch");
    346           else
    347             CmdArgs.push_back("-include-pth");
    348           CmdArgs.push_back(Args.MakeArgString(P.str()));
    349           continue;
    350         } else {
    351           // Ignore the PCH if not first on command line and emit warning.
    352           D.Diag(diag::warn_drv_pch_not_first_include)
    353               << P.str() << A->getAsString(Args);
    354         }
    355       }
    356     }
    357 
    358     // Not translated, render as usual.
    359     A->claim();
    360     A->render(Args, CmdArgs);
    361   }
    362 
    363   Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
    364   Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
    365                   options::OPT_index_header_map);
    366 
    367   // Add C++ include arguments, if needed.
    368   types::ID InputType = Inputs[0].getType();
    369   if (types::isCXX(InputType)) {
    370     bool ObjCXXAutoRefCount
    371       = types::isObjC(InputType) && isObjCAutoRefCount(Args);
    372     getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs,
    373                                                 ObjCXXAutoRefCount);
    374     Args.AddAllArgs(CmdArgs, options::OPT_stdlib_EQ);
    375   }
    376 
    377   // Add -Wp, and -Xassembler if using the preprocessor.
    378 
    379   // FIXME: There is a very unfortunate problem here, some troubled
    380   // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
    381   // really support that we would have to parse and then translate
    382   // those options. :(
    383   Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
    384                        options::OPT_Xpreprocessor);
    385 
    386   // -I- is a deprecated GCC feature, reject it.
    387   if (Arg *A = Args.getLastArg(options::OPT_I_))
    388     D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
    389 
    390   // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
    391   // -isysroot to the CC1 invocation.
    392   if (Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
    393     if (!Args.hasArg(options::OPT_isysroot)) {
    394       CmdArgs.push_back("-isysroot");
    395       CmdArgs.push_back(A->getValue(Args));
    396     }
    397   }
    398 
    399   // If a module path was provided, pass it along. Otherwise, use a temporary
    400   // directory.
    401   if (Arg *A = Args.getLastArg(options::OPT_fmodule_cache_path)) {
    402     A->claim();
    403     A->render(Args, CmdArgs);
    404   } else {
    405     llvm::SmallString<128> DefaultModuleCache;
    406     llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
    407                                            DefaultModuleCache);
    408     llvm::sys::path::append(DefaultModuleCache, "clang-module-cache");
    409     CmdArgs.push_back("-fmodule-cache-path");
    410     CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
    411   }
    412 
    413   Args.AddAllArgs(CmdArgs, options::OPT_fauto_module_import);
    414 
    415   // Parse additional include paths from environment variables.
    416   // CPATH - included following the user specified includes (but prior to
    417   // builtin and standard includes).
    418   AddIncludeDirectoryList(Args, CmdArgs, "-I", ::getenv("CPATH"));
    419   // C_INCLUDE_PATH - system includes enabled when compiling C.
    420   AddIncludeDirectoryList(Args, CmdArgs, "-c-isystem",
    421                           ::getenv("C_INCLUDE_PATH"));
    422   // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
    423   AddIncludeDirectoryList(Args, CmdArgs, "-cxx-isystem",
    424                           ::getenv("CPLUS_INCLUDE_PATH"));
    425   // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
    426   AddIncludeDirectoryList(Args, CmdArgs, "-objc-isystem",
    427                           ::getenv("OBJC_INCLUDE_PATH"));
    428   // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
    429   AddIncludeDirectoryList(Args, CmdArgs, "-objcxx-isystem",
    430                           ::getenv("OBJCPLUS_INCLUDE_PATH"));
    431 }
    432 
    433 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
    434 //
    435 // FIXME: tblgen this.
    436 static const char *getARMTargetCPU(const ArgList &Args,
    437                                    const llvm::Triple &Triple) {
    438   // FIXME: Warn on inconsistent use of -mcpu and -march.
    439 
    440   // If we have -mcpu=, use that.
    441   if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
    442     return A->getValue(Args);
    443 
    444   StringRef MArch;
    445   if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
    446     // Otherwise, if we have -march= choose the base CPU for that arch.
    447     MArch = A->getValue(Args);
    448   } else {
    449     // Otherwise, use the Arch from the triple.
    450     MArch = Triple.getArchName();
    451   }
    452 
    453   return llvm::StringSwitch<const char *>(MArch)
    454     .Cases("armv2", "armv2a","arm2")
    455     .Case("armv3", "arm6")
    456     .Case("armv3m", "arm7m")
    457     .Cases("armv4", "armv4t", "arm7tdmi")
    458     .Cases("armv5", "armv5t", "arm10tdmi")
    459     .Cases("armv5e", "armv5te", "arm1026ejs")
    460     .Case("armv5tej", "arm926ej-s")
    461     .Cases("armv6", "armv6k", "arm1136jf-s")
    462     .Case("armv6j", "arm1136j-s")
    463     .Cases("armv6z", "armv6zk", "arm1176jzf-s")
    464     .Case("armv6t2", "arm1156t2-s")
    465     .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
    466     .Cases("armv7r", "armv7-r", "cortex-r4")
    467     .Cases("armv7m", "armv7-m", "cortex-m3")
    468     .Case("ep9312", "ep9312")
    469     .Case("iwmmxt", "iwmmxt")
    470     .Case("xscale", "xscale")
    471     .Cases("armv6m", "armv6-m", "cortex-m0")
    472     // If all else failed, return the most base CPU LLVM supports.
    473     .Default("arm7tdmi");
    474 }
    475 
    476 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
    477 /// CPU.
    478 //
    479 // FIXME: This is redundant with -mcpu, why does LLVM use this.
    480 // FIXME: tblgen this, or kill it!
    481 static const char *getLLVMArchSuffixForARM(StringRef CPU) {
    482   return llvm::StringSwitch<const char *>(CPU)
    483     .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
    484     .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
    485     .Cases("arm920", "arm920t", "arm922t", "v4t")
    486     .Cases("arm940t", "ep9312","v4t")
    487     .Cases("arm10tdmi",  "arm1020t", "v5")
    488     .Cases("arm9e",  "arm926ej-s",  "arm946e-s", "v5e")
    489     .Cases("arm966e-s",  "arm968e-s",  "arm10e", "v5e")
    490     .Cases("arm1020e",  "arm1022e",  "xscale", "iwmmxt", "v5e")
    491     .Cases("arm1136j-s",  "arm1136jf-s",  "arm1176jz-s", "v6")
    492     .Cases("arm1176jzf-s",  "mpcorenovfp",  "mpcore", "v6")
    493     .Cases("arm1156t2-s",  "arm1156t2f-s", "v6t2")
    494     .Cases("cortex-a8", "cortex-a9", "v7")
    495     .Default("");
    496 }
    497 
    498 // FIXME: Move to target hook.
    499 static bool isSignedCharDefault(const llvm::Triple &Triple) {
    500   switch (Triple.getArch()) {
    501   default:
    502     return true;
    503 
    504   case llvm::Triple::arm:
    505   case llvm::Triple::ppc:
    506   case llvm::Triple::ppc64:
    507     if (Triple.isOSDarwin())
    508       return true;
    509     return false;
    510 
    511   case llvm::Triple::systemz:
    512     return false;
    513   }
    514 }
    515 
    516 void Clang::AddARMTargetArgs(const ArgList &Args,
    517                              ArgStringList &CmdArgs,
    518                              bool KernelOrKext) const {
    519   const Driver &D = getToolChain().getDriver();
    520   llvm::Triple Triple = getToolChain().getTriple();
    521 
    522   // Select the ABI to use.
    523   //
    524   // FIXME: Support -meabi.
    525   const char *ABIName = 0;
    526   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
    527     ABIName = A->getValue(Args);
    528   } else {
    529     // Select the default based on the platform.
    530     switch(Triple.getEnvironment()) {
    531     case llvm::Triple::GNUEABI:
    532       ABIName = "aapcs-linux";
    533       break;
    534     case llvm::Triple::EABI:
    535       ABIName = "aapcs";
    536       break;
    537     default:
    538       ABIName = "apcs-gnu";
    539     }
    540   }
    541   CmdArgs.push_back("-target-abi");
    542   CmdArgs.push_back(ABIName);
    543 
    544   // Set the CPU based on -march= and -mcpu=.
    545   CmdArgs.push_back("-target-cpu");
    546   CmdArgs.push_back(getARMTargetCPU(Args, Triple));
    547 
    548   // Select the float ABI as determined by -msoft-float, -mhard-float, and
    549   // -mfloat-abi=.
    550   StringRef FloatABI;
    551   if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
    552                                options::OPT_mhard_float,
    553                                options::OPT_mfloat_abi_EQ)) {
    554     if (A->getOption().matches(options::OPT_msoft_float))
    555       FloatABI = "soft";
    556     else if (A->getOption().matches(options::OPT_mhard_float))
    557       FloatABI = "hard";
    558     else {
    559       FloatABI = A->getValue(Args);
    560       if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
    561         D.Diag(diag::err_drv_invalid_mfloat_abi)
    562           << A->getAsString(Args);
    563         FloatABI = "soft";
    564       }
    565     }
    566   }
    567 
    568   // If unspecified, choose the default based on the platform.
    569   if (FloatABI.empty()) {
    570     const llvm::Triple &Triple = getToolChain().getTriple();
    571     switch (Triple.getOS()) {
    572     case llvm::Triple::Darwin:
    573     case llvm::Triple::MacOSX:
    574     case llvm::Triple::IOS: {
    575       // Darwin defaults to "softfp" for v6 and v7.
    576       //
    577       // FIXME: Factor out an ARM class so we can cache the arch somewhere.
    578       StringRef ArchName =
    579         getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
    580       if (ArchName.startswith("v6") || ArchName.startswith("v7"))
    581         FloatABI = "softfp";
    582       else
    583         FloatABI = "soft";
    584       break;
    585     }
    586 
    587     case llvm::Triple::Linux: {
    588       if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUEABI) {
    589         FloatABI = "softfp";
    590         break;
    591       }
    592     }
    593     // fall through
    594 
    595     default:
    596       switch(Triple.getEnvironment()) {
    597       case llvm::Triple::GNUEABI:
    598         FloatABI = "softfp";
    599         break;
    600       case llvm::Triple::EABI:
    601         // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
    602         FloatABI = "softfp";
    603         break;
    604       default:
    605         // Assume "soft", but warn the user we are guessing.
    606         FloatABI = "soft";
    607         D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
    608         break;
    609       }
    610     }
    611   }
    612 
    613   if (FloatABI == "soft") {
    614     // Floating point operations and argument passing are soft.
    615     //
    616     // FIXME: This changes CPP defines, we need -target-soft-float.
    617     CmdArgs.push_back("-msoft-float");
    618     CmdArgs.push_back("-mfloat-abi");
    619     CmdArgs.push_back("soft");
    620   } else if (FloatABI == "softfp") {
    621     // Floating point operations are hard, but argument passing is soft.
    622     CmdArgs.push_back("-mfloat-abi");
    623     CmdArgs.push_back("soft");
    624   } else {
    625     // Floating point operations and argument passing are hard.
    626     assert(FloatABI == "hard" && "Invalid float abi!");
    627     CmdArgs.push_back("-mfloat-abi");
    628     CmdArgs.push_back("hard");
    629   }
    630 
    631   // Set appropriate target features for floating point mode.
    632   //
    633   // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
    634   // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
    635   // stripped out by the ARM target.
    636 
    637   // Use software floating point operations?
    638   if (FloatABI == "soft") {
    639     CmdArgs.push_back("-target-feature");
    640     CmdArgs.push_back("+soft-float");
    641   }
    642 
    643   // Use software floating point argument passing?
    644   if (FloatABI != "hard") {
    645     CmdArgs.push_back("-target-feature");
    646     CmdArgs.push_back("+soft-float-abi");
    647   }
    648 
    649   // Honor -mfpu=.
    650   //
    651   // FIXME: Centralize feature selection, defaulting shouldn't be also in the
    652   // frontend target.
    653   if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
    654     StringRef FPU = A->getValue(Args);
    655 
    656     // Set the target features based on the FPU.
    657     if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
    658       // Disable any default FPU support.
    659       CmdArgs.push_back("-target-feature");
    660       CmdArgs.push_back("-vfp2");
    661       CmdArgs.push_back("-target-feature");
    662       CmdArgs.push_back("-vfp3");
    663       CmdArgs.push_back("-target-feature");
    664       CmdArgs.push_back("-neon");
    665     } else if (FPU == "vfp") {
    666       CmdArgs.push_back("-target-feature");
    667       CmdArgs.push_back("+vfp2");
    668     } else if (FPU == "vfp3") {
    669       CmdArgs.push_back("-target-feature");
    670       CmdArgs.push_back("+vfp3");
    671     } else if (FPU == "neon") {
    672       CmdArgs.push_back("-target-feature");
    673       CmdArgs.push_back("+neon");
    674     } else
    675       D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
    676   }
    677 
    678   // Setting -msoft-float effectively disables NEON because of the GCC
    679   // implementation, although the same isn't true of VFP or VFP3.
    680   if (FloatABI == "soft") {
    681     CmdArgs.push_back("-target-feature");
    682     CmdArgs.push_back("-neon");
    683   }
    684 
    685   // Kernel code has more strict alignment requirements.
    686   if (KernelOrKext) {
    687     CmdArgs.push_back("-backend-option");
    688     CmdArgs.push_back("-arm-long-calls");
    689 
    690     CmdArgs.push_back("-backend-option");
    691     CmdArgs.push_back("-arm-strict-align");
    692 
    693     // The kext linker doesn't know how to deal with movw/movt.
    694     CmdArgs.push_back("-backend-option");
    695     CmdArgs.push_back("-arm-darwin-use-movt=0");
    696   }
    697 
    698   // Setting -mno-global-merge disables the codegen global merge pass. Setting
    699   // -mglobal-merge has no effect as the pass is enabled by default.
    700   if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
    701                                options::OPT_mno_global_merge)) {
    702     if (A->getOption().matches(options::OPT_mno_global_merge))
    703       CmdArgs.push_back("-mno-global-merge");
    704   }
    705 }
    706 
    707 // Get default architecture.
    708 static const char* getMipsArchFromCPU(StringRef CPUName) {
    709   if (CPUName == "mips32r1" || CPUName == "4ke")
    710     return "mips";
    711 
    712   assert((CPUName == "mips64r1" || CPUName == "mips64r2") &&
    713          "Unexpected cpu name.");
    714 
    715   return "mips64";
    716 }
    717 
    718 // Get default target cpu.
    719 static const char* getMipsCPUFromArch(StringRef ArchName, const Driver &D) {
    720   if (ArchName == "mips" || ArchName == "mipsel")
    721     return "mips32r1";
    722   else if (ArchName == "mips64" || ArchName == "mips64el")
    723     return "mips64r1";
    724   else
    725     D.Diag(diag::err_drv_invalid_arch_name) << ArchName;
    726 
    727   return 0;
    728 }
    729 
    730 // Get default ABI.
    731 static const char* getMipsABIFromArch(StringRef ArchName) {
    732     if (ArchName == "mips" || ArchName == "mipsel")
    733       return "o32";
    734 
    735     assert((ArchName == "mips64" || ArchName == "mips64el") &&
    736            "Unexpected arch name.");
    737     return "n64";
    738 }
    739 
    740 void Clang::AddMIPSTargetArgs(const ArgList &Args,
    741                              ArgStringList &CmdArgs) const {
    742   const Driver &D = getToolChain().getDriver();
    743 
    744   StringRef ArchName;
    745   const char *CPUName;
    746 
    747   // Set target cpu and architecture.
    748   if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
    749     CPUName = A->getValue(Args);
    750     ArchName = getMipsArchFromCPU(CPUName);
    751   }
    752   else {
    753     ArchName = Args.MakeArgString(getToolChain().getArchName());
    754     CPUName = getMipsCPUFromArch(ArchName, D);
    755   }
    756 
    757   CmdArgs.push_back("-target-cpu");
    758   CmdArgs.push_back(CPUName);
    759 
    760   // Select the ABI to use.
    761   const char *ABIName = 0;
    762   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
    763     ABIName = A->getValue(Args);
    764   else
    765     ABIName = getMipsABIFromArch(ArchName);
    766 
    767   CmdArgs.push_back("-target-abi");
    768   CmdArgs.push_back(ABIName);
    769 
    770   // Select the float ABI as determined by -msoft-float, -mhard-float, and
    771   StringRef FloatABI;
    772   if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
    773                                options::OPT_mhard_float)) {
    774     if (A->getOption().matches(options::OPT_msoft_float))
    775       FloatABI = "soft";
    776     else if (A->getOption().matches(options::OPT_mhard_float))
    777       FloatABI = "hard";
    778   }
    779 
    780   // If unspecified, choose the default based on the platform.
    781   if (FloatABI.empty()) {
    782     // Assume "soft", but warn the user we are guessing.
    783     FloatABI = "soft";
    784     D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
    785   }
    786 
    787   if (FloatABI == "soft") {
    788     // Floating point operations and argument passing are soft.
    789     //
    790     // FIXME: This changes CPP defines, we need -target-soft-float.
    791     CmdArgs.push_back("-msoft-float");
    792   } else {
    793     assert(FloatABI == "hard" && "Invalid float abi!");
    794     CmdArgs.push_back("-mhard-float");
    795   }
    796 }
    797 
    798 void Clang::AddSparcTargetArgs(const ArgList &Args,
    799                              ArgStringList &CmdArgs) const {
    800   const Driver &D = getToolChain().getDriver();
    801 
    802   if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
    803     StringRef MArch = A->getValue(Args);
    804     CmdArgs.push_back("-target-cpu");
    805     CmdArgs.push_back(MArch.str().c_str());
    806   }
    807 
    808   // Select the float ABI as determined by -msoft-float, -mhard-float, and
    809   StringRef FloatABI;
    810   if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
    811                                options::OPT_mhard_float)) {
    812     if (A->getOption().matches(options::OPT_msoft_float))
    813       FloatABI = "soft";
    814     else if (A->getOption().matches(options::OPT_mhard_float))
    815       FloatABI = "hard";
    816   }
    817 
    818   // If unspecified, choose the default based on the platform.
    819   if (FloatABI.empty()) {
    820     switch (getToolChain().getTriple().getOS()) {
    821     default:
    822       // Assume "soft", but warn the user we are guessing.
    823       FloatABI = "soft";
    824       D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
    825       break;
    826     }
    827   }
    828 
    829   if (FloatABI == "soft") {
    830     // Floating point operations and argument passing are soft.
    831     //
    832     // FIXME: This changes CPP defines, we need -target-soft-float.
    833     CmdArgs.push_back("-msoft-float");
    834     CmdArgs.push_back("-target-feature");
    835     CmdArgs.push_back("+soft-float");
    836   } else {
    837     assert(FloatABI == "hard" && "Invalid float abi!");
    838     CmdArgs.push_back("-mhard-float");
    839   }
    840 }
    841 
    842 void Clang::AddX86TargetArgs(const ArgList &Args,
    843                              ArgStringList &CmdArgs) const {
    844   if (!Args.hasFlag(options::OPT_mred_zone,
    845                     options::OPT_mno_red_zone,
    846                     true) ||
    847       Args.hasArg(options::OPT_mkernel) ||
    848       Args.hasArg(options::OPT_fapple_kext))
    849     CmdArgs.push_back("-disable-red-zone");
    850 
    851   if (Args.hasFlag(options::OPT_msoft_float,
    852                    options::OPT_mno_soft_float,
    853                    false))
    854     CmdArgs.push_back("-no-implicit-float");
    855 
    856   const char *CPUName = 0;
    857   if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
    858     if (StringRef(A->getValue(Args)) == "native") {
    859       // FIXME: Reject attempts to use -march=native unless the target matches
    860       // the host.
    861       //
    862       // FIXME: We should also incorporate the detected target features for use
    863       // with -native.
    864       std::string CPU = llvm::sys::getHostCPUName();
    865       if (!CPU.empty())
    866         CPUName = Args.MakeArgString(CPU);
    867     } else
    868       CPUName = A->getValue(Args);
    869   }
    870 
    871   // Select the default CPU if none was given (or detection failed).
    872   if (!CPUName) {
    873     // FIXME: Need target hooks.
    874     if (getToolChain().getTriple().isOSDarwin()) {
    875       if (getToolChain().getArch() == llvm::Triple::x86_64)
    876         CPUName = "core2";
    877       else if (getToolChain().getArch() == llvm::Triple::x86)
    878         CPUName = "yonah";
    879     } else if (getToolChain().getOS().startswith("haiku"))  {
    880       if (getToolChain().getArch() == llvm::Triple::x86_64)
    881         CPUName = "x86-64";
    882       else if (getToolChain().getArch() == llvm::Triple::x86)
    883         CPUName = "i586";
    884     } else if (getToolChain().getOS().startswith("openbsd"))  {
    885       if (getToolChain().getArch() == llvm::Triple::x86_64)
    886         CPUName = "x86-64";
    887       else if (getToolChain().getArch() == llvm::Triple::x86)
    888         CPUName = "i486";
    889     } else if (getToolChain().getOS().startswith("freebsd"))  {
    890       if (getToolChain().getArch() == llvm::Triple::x86_64)
    891         CPUName = "x86-64";
    892       else if (getToolChain().getArch() == llvm::Triple::x86)
    893         CPUName = "i486";
    894     } else if (getToolChain().getOS().startswith("netbsd"))  {
    895       if (getToolChain().getArch() == llvm::Triple::x86_64)
    896         CPUName = "x86-64";
    897       else if (getToolChain().getArch() == llvm::Triple::x86)
    898         CPUName = "i486";
    899     } else {
    900       if (getToolChain().getArch() == llvm::Triple::x86_64)
    901         CPUName = "x86-64";
    902       else if (getToolChain().getArch() == llvm::Triple::x86)
    903         CPUName = "pentium4";
    904     }
    905   }
    906 
    907   if (CPUName) {
    908     CmdArgs.push_back("-target-cpu");
    909     CmdArgs.push_back(CPUName);
    910   }
    911 
    912   // The required algorithm here is slightly strange: the options are applied
    913   // in order (so -mno-sse -msse2 disables SSE3), but any option that gets
    914   // directly overridden later is ignored (so "-mno-sse -msse2 -mno-sse2 -msse"
    915   // is equivalent to "-mno-sse2 -msse"). The -cc1 handling deals with the
    916   // former correctly, but not the latter; handle directly-overridden
    917   // attributes here.
    918   llvm::StringMap<unsigned> PrevFeature;
    919   std::vector<const char*> Features;
    920   for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
    921          ie = Args.filtered_end(); it != ie; ++it) {
    922     StringRef Name = (*it)->getOption().getName();
    923     (*it)->claim();
    924 
    925     // Skip over "-m".
    926     assert(Name.startswith("-m") && "Invalid feature name.");
    927     Name = Name.substr(2);
    928 
    929     bool IsNegative = Name.startswith("no-");
    930     if (IsNegative)
    931       Name = Name.substr(3);
    932 
    933     unsigned& Prev = PrevFeature[Name];
    934     if (Prev)
    935       Features[Prev - 1] = 0;
    936     Prev = Features.size() + 1;
    937     Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
    938   }
    939   for (unsigned i = 0; i < Features.size(); i++) {
    940     if (Features[i]) {
    941       CmdArgs.push_back("-target-feature");
    942       CmdArgs.push_back(Features[i]);
    943     }
    944   }
    945 }
    946 
    947 static bool
    948 shouldUseExceptionTablesForObjCExceptions(unsigned objcABIVersion,
    949                                           const llvm::Triple &Triple) {
    950   // We use the zero-cost exception tables for Objective-C if the non-fragile
    951   // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
    952   // later.
    953 
    954   if (objcABIVersion >= 2)
    955     return true;
    956 
    957   if (!Triple.isOSDarwin())
    958     return false;
    959 
    960   return (!Triple.isMacOSXVersionLT(10,5) &&
    961           (Triple.getArch() == llvm::Triple::x86_64 ||
    962            Triple.getArch() == llvm::Triple::arm));
    963 }
    964 
    965 /// addExceptionArgs - Adds exception related arguments to the driver command
    966 /// arguments. There's a master flag, -fexceptions and also language specific
    967 /// flags to enable/disable C++ and Objective-C exceptions.
    968 /// This makes it possible to for example disable C++ exceptions but enable
    969 /// Objective-C exceptions.
    970 static void addExceptionArgs(const ArgList &Args, types::ID InputType,
    971                              const llvm::Triple &Triple,
    972                              bool KernelOrKext, bool IsRewriter,
    973                              unsigned objcABIVersion,
    974                              ArgStringList &CmdArgs) {
    975   if (KernelOrKext)
    976     return;
    977 
    978   // Exceptions are enabled by default.
    979   bool ExceptionsEnabled = true;
    980 
    981   // This keeps track of whether exceptions were explicitly turned on or off.
    982   bool DidHaveExplicitExceptionFlag = false;
    983 
    984   if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
    985                                options::OPT_fno_exceptions)) {
    986     if (A->getOption().matches(options::OPT_fexceptions))
    987       ExceptionsEnabled = true;
    988     else
    989       ExceptionsEnabled = false;
    990 
    991     DidHaveExplicitExceptionFlag = true;
    992   }
    993 
    994   bool ShouldUseExceptionTables = false;
    995 
    996   // Exception tables and cleanups can be enabled with -fexceptions even if the
    997   // language itself doesn't support exceptions.
    998   if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
    999     ShouldUseExceptionTables = true;
   1000 
   1001   // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
   1002   // is not necessarily sensible, but follows GCC.
   1003   if (types::isObjC(InputType) &&
   1004       Args.hasFlag(options::OPT_fobjc_exceptions,
   1005                    options::OPT_fno_objc_exceptions,
   1006                    true)) {
   1007     CmdArgs.push_back("-fobjc-exceptions");
   1008 
   1009     ShouldUseExceptionTables |=
   1010       shouldUseExceptionTablesForObjCExceptions(objcABIVersion, Triple);
   1011   }
   1012 
   1013   if (types::isCXX(InputType)) {
   1014     bool CXXExceptionsEnabled = ExceptionsEnabled;
   1015 
   1016     if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
   1017                                  options::OPT_fno_cxx_exceptions,
   1018                                  options::OPT_fexceptions,
   1019                                  options::OPT_fno_exceptions)) {
   1020       if (A->getOption().matches(options::OPT_fcxx_exceptions))
   1021         CXXExceptionsEnabled = true;
   1022       else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
   1023         CXXExceptionsEnabled = false;
   1024     }
   1025 
   1026     if (CXXExceptionsEnabled) {
   1027       CmdArgs.push_back("-fcxx-exceptions");
   1028 
   1029       ShouldUseExceptionTables = true;
   1030     }
   1031   }
   1032 
   1033   if (ShouldUseExceptionTables)
   1034     CmdArgs.push_back("-fexceptions");
   1035 }
   1036 
   1037 static bool ShouldDisableCFI(const ArgList &Args,
   1038                              const ToolChain &TC) {
   1039   if (TC.getTriple().isOSDarwin()) {
   1040     // The native darwin assembler doesn't support cfi directives, so
   1041     // we disable them if we think the .s file will be passed to it.
   1042 
   1043     // FIXME: Duplicated code with ToolChains.cpp
   1044     // FIXME: This doesn't belong here, but ideally we will support static soon
   1045     // anyway.
   1046     bool HasStatic = (Args.hasArg(options::OPT_mkernel) ||
   1047                       Args.hasArg(options::OPT_static) ||
   1048                       Args.hasArg(options::OPT_fapple_kext));
   1049     bool IsIADefault = TC.IsIntegratedAssemblerDefault() && !HasStatic;
   1050     bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
   1051                                         options::OPT_no_integrated_as,
   1052                                         IsIADefault);
   1053     bool UseCFI = Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
   1054                                options::OPT_fno_dwarf2_cfi_asm,
   1055                                UseIntegratedAs);
   1056     return !UseCFI;
   1057   }
   1058 
   1059   // For now we assume that every other assembler support CFI.
   1060   return false;
   1061 }
   1062 
   1063 static bool ShouldDisableDwarfDirectory(const ArgList &Args,
   1064                                         const ToolChain &TC) {
   1065   bool IsIADefault = TC.IsIntegratedAssemblerDefault();
   1066   bool UseIntegratedAs = Args.hasFlag(options::OPT_integrated_as,
   1067                                       options::OPT_no_integrated_as,
   1068                                       IsIADefault);
   1069   bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
   1070                                         options::OPT_fno_dwarf_directory_asm,
   1071                                         UseIntegratedAs);
   1072   return !UseDwarfDirectory;
   1073 }
   1074 
   1075 /// \brief Check whether the given input tree contains any compilation actions.
   1076 static bool ContainsCompileAction(const Action *A) {
   1077   if (isa<CompileJobAction>(A))
   1078     return true;
   1079 
   1080   for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
   1081     if (ContainsCompileAction(*it))
   1082       return true;
   1083 
   1084   return false;
   1085 }
   1086 
   1087 /// \brief Check if -relax-all should be passed to the internal assembler.
   1088 /// This is done by default when compiling non-assembler source with -O0.
   1089 static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
   1090   bool RelaxDefault = true;
   1091 
   1092   if (Arg *A = Args.getLastArg(options::OPT_O_Group))
   1093     RelaxDefault = A->getOption().matches(options::OPT_O0);
   1094 
   1095   if (RelaxDefault) {
   1096     RelaxDefault = false;
   1097     for (ActionList::const_iterator it = C.getActions().begin(),
   1098            ie = C.getActions().end(); it != ie; ++it) {
   1099       if (ContainsCompileAction(*it)) {
   1100         RelaxDefault = true;
   1101         break;
   1102       }
   1103     }
   1104   }
   1105 
   1106   return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
   1107     RelaxDefault);
   1108 }
   1109 
   1110 void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   1111                          const InputInfo &Output,
   1112                          const InputInfoList &Inputs,
   1113                          const ArgList &Args,
   1114                          const char *LinkingOutput) const {
   1115   bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
   1116                                   options::OPT_fapple_kext);
   1117   const Driver &D = getToolChain().getDriver();
   1118   ArgStringList CmdArgs;
   1119 
   1120   assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
   1121 
   1122   // Invoke ourselves in -cc1 mode.
   1123   //
   1124   // FIXME: Implement custom jobs for internal actions.
   1125   CmdArgs.push_back("-cc1");
   1126 
   1127   // Add the "effective" target triple.
   1128   CmdArgs.push_back("-triple");
   1129   std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
   1130   CmdArgs.push_back(Args.MakeArgString(TripleStr));
   1131 
   1132   // Select the appropriate action.
   1133   bool IsRewriter = false;
   1134   if (isa<AnalyzeJobAction>(JA)) {
   1135     assert(JA.getType() == types::TY_Plist && "Invalid output type.");
   1136     CmdArgs.push_back("-analyze");
   1137   } else if (isa<PreprocessJobAction>(JA)) {
   1138     if (Output.getType() == types::TY_Dependencies)
   1139       CmdArgs.push_back("-Eonly");
   1140     else
   1141       CmdArgs.push_back("-E");
   1142   } else if (isa<AssembleJobAction>(JA)) {
   1143     CmdArgs.push_back("-emit-obj");
   1144 
   1145     if (UseRelaxAll(C, Args))
   1146       CmdArgs.push_back("-mrelax-all");
   1147 
   1148     // When using an integrated assembler, translate -Wa, and -Xassembler
   1149     // options.
   1150     for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
   1151                                                options::OPT_Xassembler),
   1152            ie = Args.filtered_end(); it != ie; ++it) {
   1153       const Arg *A = *it;
   1154       A->claim();
   1155 
   1156       for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
   1157         StringRef Value = A->getValue(Args, i);
   1158 
   1159         if (Value == "-force_cpusubtype_ALL") {
   1160           // Do nothing, this is the default and we don't support anything else.
   1161         } else if (Value == "-L") {
   1162           CmdArgs.push_back("-msave-temp-labels");
   1163         } else if (Value == "--fatal-warnings") {
   1164           CmdArgs.push_back("-mllvm");
   1165           CmdArgs.push_back("-fatal-assembler-warnings");
   1166         } else if (Value == "--noexecstack") {
   1167           CmdArgs.push_back("-mnoexecstack");
   1168         } else {
   1169           D.Diag(diag::err_drv_unsupported_option_argument)
   1170             << A->getOption().getName() << Value;
   1171         }
   1172       }
   1173     }
   1174 
   1175     // Also ignore explicit -force_cpusubtype_ALL option.
   1176     (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
   1177   } else if (isa<PrecompileJobAction>(JA)) {
   1178     // Use PCH if the user requested it.
   1179     bool UsePCH = D.CCCUsePCH;
   1180 
   1181     if (UsePCH)
   1182       CmdArgs.push_back("-emit-pch");
   1183     else
   1184       CmdArgs.push_back("-emit-pth");
   1185   } else {
   1186     assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
   1187 
   1188     if (JA.getType() == types::TY_Nothing) {
   1189       CmdArgs.push_back("-fsyntax-only");
   1190     } else if (JA.getType() == types::TY_LLVM_IR ||
   1191                JA.getType() == types::TY_LTO_IR) {
   1192       CmdArgs.push_back("-emit-llvm");
   1193     } else if (JA.getType() == types::TY_LLVM_BC ||
   1194                JA.getType() == types::TY_LTO_BC) {
   1195       CmdArgs.push_back("-emit-llvm-bc");
   1196     } else if (JA.getType() == types::TY_PP_Asm) {
   1197       CmdArgs.push_back("-S");
   1198     } else if (JA.getType() == types::TY_AST) {
   1199       CmdArgs.push_back("-emit-pch");
   1200     } else if (JA.getType() == types::TY_RewrittenObjC) {
   1201       CmdArgs.push_back("-rewrite-objc");
   1202       IsRewriter = true;
   1203     } else {
   1204       assert(JA.getType() == types::TY_PP_Asm &&
   1205              "Unexpected output type!");
   1206     }
   1207   }
   1208 
   1209   // The make clang go fast button.
   1210   CmdArgs.push_back("-disable-free");
   1211 
   1212   // Disable the verification pass in -asserts builds.
   1213 #ifdef NDEBUG
   1214   CmdArgs.push_back("-disable-llvm-verifier");
   1215 #endif
   1216 
   1217   // Set the main file name, so that debug info works even with
   1218   // -save-temps.
   1219   CmdArgs.push_back("-main-file-name");
   1220   CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
   1221 
   1222   // Some flags which affect the language (via preprocessor
   1223   // defines). See darwin::CC1::AddCPPArgs.
   1224   if (Args.hasArg(options::OPT_static))
   1225     CmdArgs.push_back("-static-define");
   1226 
   1227   if (isa<AnalyzeJobAction>(JA)) {
   1228     // Enable region store model by default.
   1229     CmdArgs.push_back("-analyzer-store=region");
   1230 
   1231     // Treat blocks as analysis entry points.
   1232     CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
   1233 
   1234     CmdArgs.push_back("-analyzer-eagerly-assume");
   1235 
   1236     // Add default argument set.
   1237     if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
   1238       CmdArgs.push_back("-analyzer-checker=core");
   1239       CmdArgs.push_back("-analyzer-checker=deadcode");
   1240       CmdArgs.push_back("-analyzer-checker=security");
   1241 
   1242       if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
   1243         CmdArgs.push_back("-analyzer-checker=unix");
   1244 
   1245       if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
   1246         CmdArgs.push_back("-analyzer-checker=osx");
   1247     }
   1248 
   1249     // Set the output format. The default is plist, for (lame) historical
   1250     // reasons.
   1251     CmdArgs.push_back("-analyzer-output");
   1252     if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
   1253       CmdArgs.push_back(A->getValue(Args));
   1254     else
   1255       CmdArgs.push_back("plist");
   1256 
   1257     // Disable the presentation of standard compiler warnings when
   1258     // using --analyze.  We only want to show static analyzer diagnostics
   1259     // or frontend errors.
   1260     CmdArgs.push_back("-w");
   1261 
   1262     // Add -Xanalyzer arguments when running as analyzer.
   1263     Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
   1264   }
   1265 
   1266   CheckCodeGenerationOptions(D, Args);
   1267 
   1268   // Perform argument translation for LLVM backend. This
   1269   // takes some care in reconciling with llvm-gcc. The
   1270   // issue is that llvm-gcc translates these options based on
   1271   // the values in cc1, whereas we are processing based on
   1272   // the driver arguments.
   1273 
   1274   // This comes from the default translation the driver + cc1
   1275   // would do to enable flag_pic.
   1276   //
   1277   // FIXME: Centralize this code.
   1278   bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
   1279                      Args.hasArg(options::OPT_fpic) ||
   1280                      Args.hasArg(options::OPT_fPIE) ||
   1281                      Args.hasArg(options::OPT_fpie));
   1282   bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
   1283                       Args.hasArg(options::OPT_static));
   1284   const char *Model = getToolChain().GetForcedPicModel();
   1285   if (!Model) {
   1286     if (Args.hasArg(options::OPT_mdynamic_no_pic))
   1287       Model = "dynamic-no-pic";
   1288     else if (PICDisabled)
   1289       Model = "static";
   1290     else if (PICEnabled)
   1291       Model = "pic";
   1292     else
   1293       Model = getToolChain().GetDefaultRelocationModel();
   1294   }
   1295   if (StringRef(Model) != "pic") {
   1296     CmdArgs.push_back("-mrelocation-model");
   1297     CmdArgs.push_back(Model);
   1298   }
   1299 
   1300   // Infer the __PIC__ value.
   1301   //
   1302   // FIXME:  This isn't quite right on Darwin, which always sets
   1303   // __PIC__=2.
   1304   if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
   1305     CmdArgs.push_back("-pic-level");
   1306     CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
   1307   }
   1308   if (!Args.hasFlag(options::OPT_fmerge_all_constants,
   1309                     options::OPT_fno_merge_all_constants))
   1310     CmdArgs.push_back("-fno-merge-all-constants");
   1311 
   1312   // LLVM Code Generator Options.
   1313 
   1314   if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
   1315     CmdArgs.push_back("-mregparm");
   1316     CmdArgs.push_back(A->getValue(Args));
   1317   }
   1318 
   1319   if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
   1320     CmdArgs.push_back("-mrtd");
   1321 
   1322   // FIXME: Set --enable-unsafe-fp-math.
   1323   if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
   1324                    options::OPT_fomit_frame_pointer))
   1325     CmdArgs.push_back("-mdisable-fp-elim");
   1326   if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
   1327                     options::OPT_fno_zero_initialized_in_bss))
   1328     CmdArgs.push_back("-mno-zero-initialized-in-bss");
   1329   if (!Args.hasFlag(options::OPT_fstrict_aliasing,
   1330                     options::OPT_fno_strict_aliasing,
   1331                     getToolChain().IsStrictAliasingDefault()))
   1332     CmdArgs.push_back("-relaxed-aliasing");
   1333 
   1334   // Decide whether to use verbose asm. Verbose assembly is the default on
   1335   // toolchains which have the integrated assembler on by default.
   1336   bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
   1337   if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
   1338                    IsVerboseAsmDefault) ||
   1339       Args.hasArg(options::OPT_dA))
   1340     CmdArgs.push_back("-masm-verbose");
   1341 
   1342   if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
   1343     CmdArgs.push_back("-mdebug-pass");
   1344     CmdArgs.push_back("Structure");
   1345   }
   1346   if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
   1347     CmdArgs.push_back("-mdebug-pass");
   1348     CmdArgs.push_back("Arguments");
   1349   }
   1350 
   1351   // Enable -mconstructor-aliases except on darwin, where we have to
   1352   // work around a linker bug;  see <rdar://problem/7651567>.
   1353   if (!getToolChain().getTriple().isOSDarwin())
   1354     CmdArgs.push_back("-mconstructor-aliases");
   1355 
   1356   // Darwin's kernel doesn't support guard variables; just die if we
   1357   // try to use them.
   1358   if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
   1359     CmdArgs.push_back("-fforbid-guard-variables");
   1360 
   1361   if (Args.hasArg(options::OPT_mms_bitfields)) {
   1362     CmdArgs.push_back("-mms-bitfields");
   1363   }
   1364 
   1365   // This is a coarse approximation of what llvm-gcc actually does, both
   1366   // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
   1367   // complicated ways.
   1368   bool AsynchronousUnwindTables =
   1369     Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
   1370                  options::OPT_fno_asynchronous_unwind_tables,
   1371                  getToolChain().IsUnwindTablesDefault() &&
   1372                  !KernelOrKext);
   1373   if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
   1374                    AsynchronousUnwindTables))
   1375     CmdArgs.push_back("-munwind-tables");
   1376 
   1377   if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
   1378     CmdArgs.push_back("-mlimit-float-precision");
   1379     CmdArgs.push_back(A->getValue(Args));
   1380   }
   1381 
   1382   // FIXME: Handle -mtune=.
   1383   (void) Args.hasArg(options::OPT_mtune_EQ);
   1384 
   1385   if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
   1386     CmdArgs.push_back("-mcode-model");
   1387     CmdArgs.push_back(A->getValue(Args));
   1388   }
   1389 
   1390   // Add target specific cpu and features flags.
   1391   switch(getToolChain().getTriple().getArch()) {
   1392   default:
   1393     break;
   1394 
   1395   case llvm::Triple::arm:
   1396   case llvm::Triple::thumb:
   1397     AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
   1398     break;
   1399 
   1400   case llvm::Triple::mips:
   1401   case llvm::Triple::mipsel:
   1402   case llvm::Triple::mips64:
   1403   case llvm::Triple::mips64el:
   1404     AddMIPSTargetArgs(Args, CmdArgs);
   1405     break;
   1406 
   1407   case llvm::Triple::sparc:
   1408     AddSparcTargetArgs(Args, CmdArgs);
   1409     break;
   1410 
   1411   case llvm::Triple::x86:
   1412   case llvm::Triple::x86_64:
   1413     AddX86TargetArgs(Args, CmdArgs);
   1414     break;
   1415   }
   1416 
   1417   // Pass the linker version in use.
   1418   if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
   1419     CmdArgs.push_back("-target-linker-version");
   1420     CmdArgs.push_back(A->getValue(Args));
   1421   }
   1422 
   1423   // -mno-omit-leaf-frame-pointer is the default on Darwin.
   1424   if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
   1425                    options::OPT_mno_omit_leaf_frame_pointer,
   1426                    !getToolChain().getTriple().isOSDarwin()))
   1427     CmdArgs.push_back("-momit-leaf-frame-pointer");
   1428 
   1429   // -fno-math-errno is default.
   1430   if (Args.hasFlag(options::OPT_fmath_errno,
   1431                    options::OPT_fno_math_errno,
   1432                    false))
   1433     CmdArgs.push_back("-fmath-errno");
   1434 
   1435   // Explicitly error on some things we know we don't support and can't just
   1436   // ignore.
   1437   types::ID InputType = Inputs[0].getType();
   1438   if (!Args.hasArg(options::OPT_fallow_unsupported)) {
   1439     Arg *Unsupported;
   1440     if (types::isCXX(InputType) &&
   1441         getToolChain().getTriple().isOSDarwin() &&
   1442         getToolChain().getTriple().getArch() == llvm::Triple::x86) {
   1443       if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
   1444           (Unsupported = Args.getLastArg(options::OPT_mkernel)))
   1445         D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
   1446           << Unsupported->getOption().getName();
   1447     }
   1448   }
   1449 
   1450   Args.AddAllArgs(CmdArgs, options::OPT_v);
   1451   Args.AddLastArg(CmdArgs, options::OPT_H);
   1452   if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
   1453     CmdArgs.push_back("-header-include-file");
   1454     CmdArgs.push_back(D.CCPrintHeadersFilename ?
   1455                       D.CCPrintHeadersFilename : "-");
   1456   }
   1457   Args.AddLastArg(CmdArgs, options::OPT_P);
   1458   Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
   1459 
   1460   if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
   1461     CmdArgs.push_back("-diagnostic-log-file");
   1462     CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
   1463                       D.CCLogDiagnosticsFilename : "-");
   1464   }
   1465 
   1466   // Special case debug options to only pass -g to clang. This is
   1467   // wrong.
   1468   Args.ClaimAllArgs(options::OPT_g_Group);
   1469   if (Arg *A = Args.getLastArg(options::OPT_g_Group))
   1470     if (!A->getOption().matches(options::OPT_g0))
   1471       CmdArgs.push_back("-g");
   1472 
   1473   Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
   1474   Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
   1475 
   1476   Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
   1477 
   1478   if (Args.hasArg(options::OPT_ftest_coverage) ||
   1479       Args.hasArg(options::OPT_coverage))
   1480     CmdArgs.push_back("-femit-coverage-notes");
   1481   if (Args.hasArg(options::OPT_fprofile_arcs) ||
   1482       Args.hasArg(options::OPT_coverage))
   1483     CmdArgs.push_back("-femit-coverage-data");
   1484 
   1485   if (C.getArgs().hasArg(options::OPT_c) ||
   1486       C.getArgs().hasArg(options::OPT_S)) {
   1487     if (Output.isFilename()) {
   1488       CmdArgs.push_back("-coverage-file");
   1489       CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
   1490     }
   1491   }
   1492 
   1493   // Pass options for controlling the default header search paths.
   1494   if (Args.hasArg(options::OPT_nostdinc)) {
   1495     CmdArgs.push_back("-nostdsysteminc");
   1496     CmdArgs.push_back("-nobuiltininc");
   1497   } else {
   1498     if (Args.hasArg(options::OPT_nostdlibinc))
   1499         CmdArgs.push_back("-nostdsysteminc");
   1500     Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
   1501     Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
   1502   }
   1503 
   1504   // Pass the path to compiler resource files.
   1505   CmdArgs.push_back("-resource-dir");
   1506   CmdArgs.push_back(D.ResourceDir.c_str());
   1507 
   1508   Args.AddLastArg(CmdArgs, options::OPT_working_directory);
   1509 
   1510   if (!Args.hasArg(options::OPT_fno_objc_arc)) {
   1511     if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
   1512                                        options::OPT_ccc_arcmt_modify,
   1513                                        options::OPT_ccc_arcmt_migrate)) {
   1514       switch (A->getOption().getID()) {
   1515       default:
   1516         llvm_unreachable("missed a case");
   1517       case options::OPT_ccc_arcmt_check:
   1518         CmdArgs.push_back("-arcmt-check");
   1519         break;
   1520       case options::OPT_ccc_arcmt_modify:
   1521         CmdArgs.push_back("-arcmt-modify");
   1522         break;
   1523       case options::OPT_ccc_arcmt_migrate:
   1524         CmdArgs.push_back("-arcmt-migrate");
   1525         CmdArgs.push_back("-arcmt-migrate-directory");
   1526         CmdArgs.push_back(A->getValue(Args));
   1527 
   1528         Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
   1529         Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
   1530         break;
   1531       }
   1532     }
   1533   }
   1534 
   1535   // Add preprocessing options like -I, -D, etc. if we are using the
   1536   // preprocessor.
   1537   //
   1538   // FIXME: Support -fpreprocessed
   1539   if (types::getPreprocessedType(InputType) != types::TY_INVALID)
   1540     AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
   1541 
   1542   // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
   1543   // that "The compiler can only warn and ignore the option if not recognized".
   1544   // When building with ccache, it will pass -D options to clang even on
   1545   // preprocessed inputs and configure concludes that -fPIC is not supported.
   1546   Args.ClaimAllArgs(options::OPT_D);
   1547 
   1548   // Manually translate -O to -O2 and -O4 to -O3; let clang reject
   1549   // others.
   1550   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
   1551     if (A->getOption().matches(options::OPT_O4))
   1552       CmdArgs.push_back("-O3");
   1553     else if (A->getOption().matches(options::OPT_O) &&
   1554              A->getValue(Args)[0] == '\0')
   1555       CmdArgs.push_back("-O2");
   1556     else
   1557       A->render(Args, CmdArgs);
   1558   }
   1559 
   1560   Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
   1561   Args.AddLastArg(CmdArgs, options::OPT_pedantic);
   1562   Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
   1563   Args.AddLastArg(CmdArgs, options::OPT_w);
   1564 
   1565   // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
   1566   // (-ansi is equivalent to -std=c89).
   1567   //
   1568   // If a std is supplied, only add -trigraphs if it follows the
   1569   // option.
   1570   if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
   1571     if (Std->getOption().matches(options::OPT_ansi))
   1572       if (types::isCXX(InputType))
   1573         CmdArgs.push_back("-std=c++98");
   1574       else
   1575         CmdArgs.push_back("-std=c89");
   1576     else
   1577       Std->render(Args, CmdArgs);
   1578 
   1579     if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
   1580                                  options::OPT_trigraphs))
   1581       if (A != Std)
   1582         A->render(Args, CmdArgs);
   1583   } else {
   1584     // Honor -std-default.
   1585     //
   1586     // FIXME: Clang doesn't correctly handle -std= when the input language
   1587     // doesn't match. For the time being just ignore this for C++ inputs;
   1588     // eventually we want to do all the standard defaulting here instead of
   1589     // splitting it between the driver and clang -cc1.
   1590     if (!types::isCXX(InputType))
   1591         Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
   1592                                   "-std=", /*Joined=*/true);
   1593     Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
   1594   }
   1595 
   1596   // Map the bizarre '-Wwrite-strings' flag to a more sensible
   1597   // '-fconst-strings'; this better indicates its actual behavior.
   1598   if (Args.hasFlag(options::OPT_Wwrite_strings, options::OPT_Wno_write_strings,
   1599                    false)) {
   1600     // For perfect compatibility with GCC, we do this even in the presence of
   1601     // '-w'. This flag names something other than a warning for GCC.
   1602     CmdArgs.push_back("-fconst-strings");
   1603   }
   1604 
   1605   // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
   1606   // during C++ compilation, which it is by default. GCC keeps this define even
   1607   // in the presence of '-w', match this behavior bug-for-bug.
   1608   if (types::isCXX(InputType) &&
   1609       Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
   1610                    true)) {
   1611     CmdArgs.push_back("-fdeprecated-macro");
   1612   }
   1613 
   1614   // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
   1615   if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
   1616     if (Asm->getOption().matches(options::OPT_fasm))
   1617       CmdArgs.push_back("-fgnu-keywords");
   1618     else
   1619       CmdArgs.push_back("-fno-gnu-keywords");
   1620   }
   1621 
   1622   if (ShouldDisableCFI(Args, getToolChain()))
   1623     CmdArgs.push_back("-fno-dwarf2-cfi-asm");
   1624 
   1625   if (ShouldDisableDwarfDirectory(Args, getToolChain()))
   1626     CmdArgs.push_back("-fno-dwarf-directory-asm");
   1627 
   1628   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
   1629     CmdArgs.push_back("-ftemplate-depth");
   1630     CmdArgs.push_back(A->getValue(Args));
   1631   }
   1632 
   1633   if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
   1634                                options::OPT_Wlarge_by_value_copy_def)) {
   1635     CmdArgs.push_back("-Wlarge-by-value-copy");
   1636     if (A->getNumValues())
   1637       CmdArgs.push_back(A->getValue(Args));
   1638     else
   1639       CmdArgs.push_back("64"); // default value for -Wlarge-by-value-copy.
   1640   }
   1641 
   1642   if (Args.hasArg(options::OPT__relocatable_pch))
   1643     CmdArgs.push_back("-relocatable-pch");
   1644 
   1645   if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
   1646     CmdArgs.push_back("-fconstant-string-class");
   1647     CmdArgs.push_back(A->getValue(Args));
   1648   }
   1649 
   1650   if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
   1651     CmdArgs.push_back("-ftabstop");
   1652     CmdArgs.push_back(A->getValue(Args));
   1653   }
   1654 
   1655   CmdArgs.push_back("-ferror-limit");
   1656   if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
   1657     CmdArgs.push_back(A->getValue(Args));
   1658   else
   1659     CmdArgs.push_back("19");
   1660 
   1661   if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
   1662     CmdArgs.push_back("-fmacro-backtrace-limit");
   1663     CmdArgs.push_back(A->getValue(Args));
   1664   }
   1665 
   1666   if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
   1667     CmdArgs.push_back("-ftemplate-backtrace-limit");
   1668     CmdArgs.push_back(A->getValue(Args));
   1669   }
   1670 
   1671   // Pass -fmessage-length=.
   1672   CmdArgs.push_back("-fmessage-length");
   1673   if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
   1674     CmdArgs.push_back(A->getValue(Args));
   1675   } else {
   1676     // If -fmessage-length=N was not specified, determine whether this is a
   1677     // terminal and, if so, implicitly define -fmessage-length appropriately.
   1678     unsigned N = llvm::sys::Process::StandardErrColumns();
   1679     CmdArgs.push_back(Args.MakeArgString(Twine(N)));
   1680   }
   1681 
   1682   if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
   1683     CmdArgs.push_back("-fvisibility");
   1684     CmdArgs.push_back(A->getValue(Args));
   1685   }
   1686 
   1687   Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
   1688 
   1689   // -fhosted is default.
   1690   if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
   1691                                    options::OPT_fhosted,
   1692                                    false))
   1693     CmdArgs.push_back("-ffreestanding");
   1694 
   1695   // Forward -f (flag) options which we can pass directly.
   1696   Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
   1697   Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
   1698   Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
   1699   Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info);
   1700   if (getToolChain().SupportsProfiling())
   1701     Args.AddLastArg(CmdArgs, options::OPT_pg);
   1702 
   1703   // -flax-vector-conversions is default.
   1704   if (!Args.hasFlag(options::OPT_flax_vector_conversions,
   1705                     options::OPT_fno_lax_vector_conversions))
   1706     CmdArgs.push_back("-fno-lax-vector-conversions");
   1707 
   1708   if (Args.getLastArg(options::OPT_fapple_kext))
   1709     CmdArgs.push_back("-fapple-kext");
   1710 
   1711   Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
   1712   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
   1713   Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
   1714   Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
   1715   Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
   1716 
   1717   if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
   1718     CmdArgs.push_back("-ftrapv-handler");
   1719     CmdArgs.push_back(A->getValue(Args));
   1720   }
   1721 
   1722   // Forward -ftrap_function= options to the backend.
   1723   if (Arg *A = Args.getLastArg(options::OPT_ftrap_function_EQ)) {
   1724     StringRef FuncName = A->getValue(Args);
   1725     CmdArgs.push_back("-backend-option");
   1726     CmdArgs.push_back(Args.MakeArgString("-trap-func=" + FuncName));
   1727   }
   1728 
   1729   // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
   1730   // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
   1731   if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
   1732                                options::OPT_fno_wrapv)) {
   1733     if (A->getOption().matches(options::OPT_fwrapv))
   1734       CmdArgs.push_back("-fwrapv");
   1735   } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
   1736                                       options::OPT_fno_strict_overflow)) {
   1737     if (A->getOption().matches(options::OPT_fno_strict_overflow))
   1738       CmdArgs.push_back("-fwrapv");
   1739   }
   1740   Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
   1741   Args.AddLastArg(CmdArgs, options::OPT_funroll_loops);
   1742 
   1743   Args.AddLastArg(CmdArgs, options::OPT_pthread);
   1744 
   1745   // -stack-protector=0 is default.
   1746   unsigned StackProtectorLevel = 0;
   1747   if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
   1748                                options::OPT_fstack_protector_all,
   1749                                options::OPT_fstack_protector)) {
   1750     if (A->getOption().matches(options::OPT_fstack_protector))
   1751       StackProtectorLevel = 1;
   1752     else if (A->getOption().matches(options::OPT_fstack_protector_all))
   1753       StackProtectorLevel = 2;
   1754   } else {
   1755     StackProtectorLevel =
   1756       getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
   1757   }
   1758   if (StackProtectorLevel) {
   1759     CmdArgs.push_back("-stack-protector");
   1760     CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
   1761   }
   1762 
   1763   // Translate -mstackrealign
   1764   if (Args.hasArg(options::OPT_mstackrealign)) {
   1765     CmdArgs.push_back("-backend-option");
   1766     CmdArgs.push_back("-force-align-stack");
   1767   }
   1768 
   1769   // Forward -f options with positive and negative forms; we translate
   1770   // these by hand.
   1771 
   1772   if (Args.hasArg(options::OPT_mkernel)) {
   1773     if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
   1774       CmdArgs.push_back("-fapple-kext");
   1775     if (!Args.hasArg(options::OPT_fbuiltin))
   1776       CmdArgs.push_back("-fno-builtin");
   1777   }
   1778   // -fbuiltin is default.
   1779   else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
   1780     CmdArgs.push_back("-fno-builtin");
   1781 
   1782   if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
   1783                     options::OPT_fno_assume_sane_operator_new))
   1784     CmdArgs.push_back("-fno-assume-sane-operator-new");
   1785 
   1786   // -fblocks=0 is default.
   1787   if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
   1788                    getToolChain().IsBlocksDefault()) ||
   1789         (Args.hasArg(options::OPT_fgnu_runtime) &&
   1790          Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
   1791          !Args.hasArg(options::OPT_fno_blocks))) {
   1792     CmdArgs.push_back("-fblocks");
   1793 
   1794     if (!Args.hasArg(options::OPT_fgnu_runtime) &&
   1795         !getToolChain().hasBlocksRuntime())
   1796       CmdArgs.push_back("-fblocks-runtime-optional");
   1797   }
   1798 
   1799   // -faccess-control is default.
   1800   if (Args.hasFlag(options::OPT_fno_access_control,
   1801                    options::OPT_faccess_control,
   1802                    false))
   1803     CmdArgs.push_back("-fno-access-control");
   1804 
   1805   // -felide-constructors is the default.
   1806   if (Args.hasFlag(options::OPT_fno_elide_constructors,
   1807                    options::OPT_felide_constructors,
   1808                    false))
   1809     CmdArgs.push_back("-fno-elide-constructors");
   1810 
   1811   // -frtti is default.
   1812   if (KernelOrKext ||
   1813       !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
   1814     CmdArgs.push_back("-fno-rtti");
   1815 
   1816   // -fshort-enums=0 is default.
   1817   // FIXME: Are there targers where -fshort-enums is on by default ?
   1818   if (Args.hasFlag(options::OPT_fshort_enums,
   1819                    options::OPT_fno_short_enums, false))
   1820     CmdArgs.push_back("-fshort-enums");
   1821 
   1822   // -fsigned-char is default.
   1823   if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
   1824                     isSignedCharDefault(getToolChain().getTriple())))
   1825     CmdArgs.push_back("-fno-signed-char");
   1826 
   1827   // -fthreadsafe-static is default.
   1828   if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
   1829                     options::OPT_fno_threadsafe_statics))
   1830     CmdArgs.push_back("-fno-threadsafe-statics");
   1831 
   1832   // -fuse-cxa-atexit is default.
   1833   if (KernelOrKext ||
   1834     !Args.hasFlag(options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
   1835                   getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
   1836                   getToolChain().getTriple().getOS() != llvm::Triple::MinGW32))
   1837     CmdArgs.push_back("-fno-use-cxa-atexit");
   1838 
   1839   // -fms-extensions=0 is default.
   1840   if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
   1841                    getToolChain().getTriple().getOS() == llvm::Triple::Win32))
   1842     CmdArgs.push_back("-fms-extensions");
   1843 
   1844   // -fms-compatibility=0 is default.
   1845   if (Args.hasFlag(options::OPT_fms_compatibility, options::OPT_fno_ms_compatibility,
   1846                    getToolChain().getTriple().getOS() == llvm::Triple::Win32))
   1847     CmdArgs.push_back("-fms-compatibility");
   1848 
   1849   // -fmsc-version=1300 is default.
   1850   if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
   1851                    getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
   1852       Args.hasArg(options::OPT_fmsc_version)) {
   1853     StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
   1854     if (msc_ver.empty())
   1855       CmdArgs.push_back("-fmsc-version=1300");
   1856     else
   1857       CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
   1858   }
   1859 
   1860 
   1861   // -fborland-extensions=0 is default.
   1862   if (Args.hasFlag(options::OPT_fborland_extensions,
   1863                    options::OPT_fno_borland_extensions, false))
   1864     CmdArgs.push_back("-fborland-extensions");
   1865 
   1866   // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
   1867   // needs it.
   1868   if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
   1869                    options::OPT_fno_delayed_template_parsing,
   1870                    getToolChain().getTriple().getOS() == llvm::Triple::Win32))
   1871     CmdArgs.push_back("-fdelayed-template-parsing");
   1872 
   1873   // -fgnu-keywords default varies depending on language; only pass if
   1874   // specified.
   1875   if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
   1876                                options::OPT_fno_gnu_keywords))
   1877     A->render(Args, CmdArgs);
   1878 
   1879   if (Args.hasFlag(options::OPT_fgnu89_inline,
   1880                    options::OPT_fno_gnu89_inline,
   1881                    false))
   1882     CmdArgs.push_back("-fgnu89-inline");
   1883 
   1884   // -fobjc-nonfragile-abi=0 is default.
   1885   ObjCRuntime objCRuntime;
   1886   unsigned objcABIVersion = 0;
   1887   bool NeXTRuntimeIsDefault
   1888     = (IsRewriter || getToolChain().getTriple().isOSDarwin());
   1889   if (Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
   1890                    NeXTRuntimeIsDefault)) {
   1891     objCRuntime.setKind(ObjCRuntime::NeXT);
   1892   } else {
   1893     CmdArgs.push_back("-fgnu-runtime");
   1894     objCRuntime.setKind(ObjCRuntime::GNU);
   1895   }
   1896   getToolChain().configureObjCRuntime(objCRuntime);
   1897   if (objCRuntime.HasARC)
   1898     CmdArgs.push_back("-fobjc-runtime-has-arc");
   1899   if (objCRuntime.HasWeak)
   1900     CmdArgs.push_back("-fobjc-runtime-has-weak");
   1901   if (objCRuntime.HasTerminate)
   1902     CmdArgs.push_back("-fobjc-runtime-has-terminate");
   1903 
   1904   // Compute the Objective-C ABI "version" to use. Version numbers are
   1905   // slightly confusing for historical reasons:
   1906   //   1 - Traditional "fragile" ABI
   1907   //   2 - Non-fragile ABI, version 1
   1908   //   3 - Non-fragile ABI, version 2
   1909   objcABIVersion = 1;
   1910   // If -fobjc-abi-version= is present, use that to set the version.
   1911   if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
   1912     if (StringRef(A->getValue(Args)) == "1")
   1913       objcABIVersion = 1;
   1914     else if (StringRef(A->getValue(Args)) == "2")
   1915       objcABIVersion = 2;
   1916     else if (StringRef(A->getValue(Args)) == "3")
   1917       objcABIVersion = 3;
   1918     else
   1919       D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
   1920   } else {
   1921     // Otherwise, determine if we are using the non-fragile ABI.
   1922     bool NonFragileABIIsDefault
   1923       = (!IsRewriter && getToolChain().IsObjCNonFragileABIDefault());
   1924     if (Args.hasFlag(options::OPT_fobjc_nonfragile_abi,
   1925                      options::OPT_fno_objc_nonfragile_abi,
   1926                      NonFragileABIIsDefault)) {
   1927       // Determine the non-fragile ABI version to use.
   1928 #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
   1929       unsigned NonFragileABIVersion = 1;
   1930 #else
   1931       unsigned NonFragileABIVersion = 2;
   1932 #endif
   1933 
   1934       if (Arg *A = Args.getLastArg(
   1935             options::OPT_fobjc_nonfragile_abi_version_EQ)) {
   1936         if (StringRef(A->getValue(Args)) == "1")
   1937           NonFragileABIVersion = 1;
   1938         else if (StringRef(A->getValue(Args)) == "2")
   1939           NonFragileABIVersion = 2;
   1940         else
   1941           D.Diag(diag::err_drv_clang_unsupported)
   1942             << A->getAsString(Args);
   1943       }
   1944 
   1945       objcABIVersion = 1 + NonFragileABIVersion;
   1946     } else {
   1947       objcABIVersion = 1;
   1948     }
   1949   }
   1950 
   1951   if (objcABIVersion == 1) {
   1952     CmdArgs.push_back("-fobjc-fragile-abi");
   1953   } else {
   1954     // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
   1955     // legacy is the default.
   1956     if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
   1957                       options::OPT_fno_objc_legacy_dispatch,
   1958                       getToolChain().IsObjCLegacyDispatchDefault())) {
   1959       if (getToolChain().UseObjCMixedDispatch())
   1960         CmdArgs.push_back("-fobjc-dispatch-method=mixed");
   1961       else
   1962         CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
   1963     }
   1964   }
   1965 
   1966   // -fobjc-default-synthesize-properties=0 is default.
   1967   if (Args.hasFlag(options::OPT_fobjc_default_synthesize_properties,
   1968                    options::OPT_fno_objc_default_synthesize_properties,
   1969                    getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
   1970     CmdArgs.push_back("-fobjc-default-synthesize-properties");
   1971   }
   1972 
   1973   // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
   1974   // NOTE: This logic is duplicated in ToolChains.cpp.
   1975   bool ARC = isObjCAutoRefCount(Args);
   1976   if (ARC) {
   1977     CmdArgs.push_back("-fobjc-arc");
   1978 
   1979     // Allow the user to enable full exceptions code emission.
   1980     // We define off for Objective-CC, on for Objective-C++.
   1981     if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
   1982                      options::OPT_fno_objc_arc_exceptions,
   1983                      /*default*/ types::isCXX(InputType)))
   1984       CmdArgs.push_back("-fobjc-arc-exceptions");
   1985   }
   1986 
   1987   // -fobjc-infer-related-result-type is the default, except in the Objective-C
   1988   // rewriter.
   1989   if (IsRewriter)
   1990     CmdArgs.push_back("-fno-objc-infer-related-result-type");
   1991 
   1992   // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
   1993   // takes precedence.
   1994   const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
   1995   if (!GCArg)
   1996     GCArg = Args.getLastArg(options::OPT_fobjc_gc);
   1997   if (GCArg) {
   1998     if (ARC) {
   1999       D.Diag(diag::err_drv_objc_gc_arr)
   2000         << GCArg->getAsString(Args);
   2001     } else if (getToolChain().SupportsObjCGC()) {
   2002       GCArg->render(Args, CmdArgs);
   2003     } else {
   2004       // FIXME: We should move this to a hard error.
   2005       D.Diag(diag::warn_drv_objc_gc_unsupported)
   2006         << GCArg->getAsString(Args);
   2007     }
   2008   }
   2009 
   2010   // Add exception args.
   2011   addExceptionArgs(Args, InputType, getToolChain().getTriple(),
   2012                    KernelOrKext, IsRewriter, objcABIVersion, CmdArgs);
   2013 
   2014   if (getToolChain().UseSjLjExceptions())
   2015     CmdArgs.push_back("-fsjlj-exceptions");
   2016 
   2017   // C++ "sane" operator new.
   2018   if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
   2019                     options::OPT_fno_assume_sane_operator_new))
   2020     CmdArgs.push_back("-fno-assume-sane-operator-new");
   2021 
   2022   // -fconstant-cfstrings is default, and may be subject to argument translation
   2023   // on Darwin.
   2024   if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
   2025                     options::OPT_fno_constant_cfstrings) ||
   2026       !Args.hasFlag(options::OPT_mconstant_cfstrings,
   2027                     options::OPT_mno_constant_cfstrings))
   2028     CmdArgs.push_back("-fno-constant-cfstrings");
   2029 
   2030   // -fshort-wchar default varies depending on platform; only
   2031   // pass if specified.
   2032   if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
   2033     A->render(Args, CmdArgs);
   2034 
   2035   // -fno-pascal-strings is default, only pass non-default. If the tool chain
   2036   // happened to translate to -mpascal-strings, we want to back translate here.
   2037   //
   2038   // FIXME: This is gross; that translation should be pulled from the
   2039   // tool chain.
   2040   if (Args.hasFlag(options::OPT_fpascal_strings,
   2041                    options::OPT_fno_pascal_strings,
   2042                    false) ||
   2043       Args.hasFlag(options::OPT_mpascal_strings,
   2044                    options::OPT_mno_pascal_strings,
   2045                    false))
   2046     CmdArgs.push_back("-fpascal-strings");
   2047 
   2048   // Honor -fpack-struct= and -fpack-struct, if given. Note that
   2049   // -fno-pack-struct doesn't apply to -fpack-struct=.
   2050   if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
   2051     CmdArgs.push_back("-fpack-struct");
   2052     CmdArgs.push_back(A->getValue(Args));
   2053   } else if (Args.hasFlag(options::OPT_fpack_struct,
   2054                           options::OPT_fno_pack_struct, false)) {
   2055     CmdArgs.push_back("-fpack-struct");
   2056     CmdArgs.push_back("1");
   2057   }
   2058 
   2059   if (Args.hasArg(options::OPT_mkernel) ||
   2060       Args.hasArg(options::OPT_fapple_kext)) {
   2061     if (!Args.hasArg(options::OPT_fcommon))
   2062       CmdArgs.push_back("-fno-common");
   2063   }
   2064 
   2065   // -fcommon is default, only pass non-default.
   2066   else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
   2067     CmdArgs.push_back("-fno-common");
   2068 
   2069   // -fsigned-bitfields is default, and clang doesn't yet support
   2070   // -funsigned-bitfields.
   2071   if (!Args.hasFlag(options::OPT_fsigned_bitfields,
   2072                     options::OPT_funsigned_bitfields))
   2073     D.Diag(diag::warn_drv_clang_unsupported)
   2074       << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
   2075 
   2076   // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
   2077   if (!Args.hasFlag(options::OPT_ffor_scope,
   2078                     options::OPT_fno_for_scope))
   2079     D.Diag(diag::err_drv_clang_unsupported)
   2080       << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
   2081 
   2082   // -fcaret-diagnostics is default.
   2083   if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
   2084                     options::OPT_fno_caret_diagnostics, true))
   2085     CmdArgs.push_back("-fno-caret-diagnostics");
   2086 
   2087   // -fdiagnostics-fixit-info is default, only pass non-default.
   2088   if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
   2089                     options::OPT_fno_diagnostics_fixit_info))
   2090     CmdArgs.push_back("-fno-diagnostics-fixit-info");
   2091 
   2092   // Enable -fdiagnostics-show-name by default.
   2093   if (Args.hasFlag(options::OPT_fdiagnostics_show_name,
   2094                    options::OPT_fno_diagnostics_show_name, false))
   2095     CmdArgs.push_back("-fdiagnostics-show-name");
   2096 
   2097   // Enable -fdiagnostics-show-option by default.
   2098   if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
   2099                    options::OPT_fno_diagnostics_show_option))
   2100     CmdArgs.push_back("-fdiagnostics-show-option");
   2101 
   2102   if (const Arg *A =
   2103         Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
   2104     CmdArgs.push_back("-fdiagnostics-show-category");
   2105     CmdArgs.push_back(A->getValue(Args));
   2106   }
   2107 
   2108   if (const Arg *A =
   2109         Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
   2110     CmdArgs.push_back("-fdiagnostics-format");
   2111     CmdArgs.push_back(A->getValue(Args));
   2112   }
   2113 
   2114   if (Arg *A = Args.getLastArg(
   2115       options::OPT_fdiagnostics_show_note_include_stack,
   2116       options::OPT_fno_diagnostics_show_note_include_stack)) {
   2117     if (A->getOption().matches(
   2118         options::OPT_fdiagnostics_show_note_include_stack))
   2119       CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
   2120     else
   2121       CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
   2122   }
   2123 
   2124   // Color diagnostics are the default, unless the terminal doesn't support
   2125   // them.
   2126   if (Args.hasFlag(options::OPT_fcolor_diagnostics,
   2127                    options::OPT_fno_color_diagnostics,
   2128                    llvm::sys::Process::StandardErrHasColors()))
   2129     CmdArgs.push_back("-fcolor-diagnostics");
   2130 
   2131   if (!Args.hasFlag(options::OPT_fshow_source_location,
   2132                     options::OPT_fno_show_source_location))
   2133     CmdArgs.push_back("-fno-show-source-location");
   2134 
   2135   if (!Args.hasFlag(options::OPT_fshow_column,
   2136                     options::OPT_fno_show_column,
   2137                     true))
   2138     CmdArgs.push_back("-fno-show-column");
   2139 
   2140   if (!Args.hasFlag(options::OPT_fspell_checking,
   2141                     options::OPT_fno_spell_checking))
   2142     CmdArgs.push_back("-fno-spell-checking");
   2143 
   2144 
   2145   // Silently ignore -fasm-blocks for now.
   2146   (void) Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
   2147                       false);
   2148 
   2149   if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
   2150     A->render(Args, CmdArgs);
   2151 
   2152   // -fdollars-in-identifiers default varies depending on platform and
   2153   // language; only pass if specified.
   2154   if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
   2155                                options::OPT_fno_dollars_in_identifiers)) {
   2156     if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
   2157       CmdArgs.push_back("-fdollars-in-identifiers");
   2158     else
   2159       CmdArgs.push_back("-fno-dollars-in-identifiers");
   2160   }
   2161 
   2162   // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
   2163   // practical purposes.
   2164   if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
   2165                                options::OPT_fno_unit_at_a_time)) {
   2166     if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
   2167       D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
   2168   }
   2169 
   2170   // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
   2171   //
   2172   // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
   2173 #if 0
   2174   if (getToolChain().getTriple().isOSDarwin() &&
   2175       (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
   2176        getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
   2177     if (!Args.hasArg(options::OPT_fbuiltin_strcat))
   2178       CmdArgs.push_back("-fno-builtin-strcat");
   2179     if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
   2180       CmdArgs.push_back("-fno-builtin-strcpy");
   2181   }
   2182 #endif
   2183 
   2184   // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
   2185   if (Arg *A = Args.getLastArg(options::OPT_traditional,
   2186                                options::OPT_traditional_cpp)) {
   2187     if (isa<PreprocessJobAction>(JA))
   2188       CmdArgs.push_back("-traditional-cpp");
   2189     else
   2190       D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
   2191   }
   2192 
   2193   Args.AddLastArg(CmdArgs, options::OPT_dM);
   2194   Args.AddLastArg(CmdArgs, options::OPT_dD);
   2195 
   2196   // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
   2197   // parser.
   2198   Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
   2199   for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
   2200          ie = Args.filtered_end(); it != ie; ++it) {
   2201     (*it)->claim();
   2202 
   2203     // We translate this by hand to the -cc1 argument, since nightly test uses
   2204     // it and developers have been trained to spell it with -mllvm.
   2205     if (StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
   2206       CmdArgs.push_back("-disable-llvm-optzns");
   2207     else
   2208       (*it)->render(Args, CmdArgs);
   2209   }
   2210 
   2211   if (Output.getType() == types::TY_Dependencies) {
   2212     // Handled with other dependency code.
   2213   } else if (Output.isFilename()) {
   2214     CmdArgs.push_back("-o");
   2215     CmdArgs.push_back(Output.getFilename());
   2216   } else {
   2217     assert(Output.isNothing() && "Invalid output.");
   2218   }
   2219 
   2220   for (InputInfoList::const_iterator
   2221          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   2222     const InputInfo &II = *it;
   2223     CmdArgs.push_back("-x");
   2224     CmdArgs.push_back(types::getTypeName(II.getType()));
   2225     if (II.isFilename())
   2226       CmdArgs.push_back(II.getFilename());
   2227     else
   2228       II.getInputArg().renderAsInput(Args, CmdArgs);
   2229   }
   2230 
   2231   Args.AddAllArgs(CmdArgs, options::OPT_undef);
   2232 
   2233   const char *Exec = getToolChain().getDriver().getClangProgramPath();
   2234 
   2235   // Optionally embed the -cc1 level arguments into the debug info, for build
   2236   // analysis.
   2237   if (getToolChain().UseDwarfDebugFlags()) {
   2238     ArgStringList OriginalArgs;
   2239     for (ArgList::const_iterator it = Args.begin(),
   2240            ie = Args.end(); it != ie; ++it)
   2241       (*it)->render(Args, OriginalArgs);
   2242 
   2243     llvm::SmallString<256> Flags;
   2244     Flags += Exec;
   2245     for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
   2246       Flags += " ";
   2247       Flags += OriginalArgs[i];
   2248     }
   2249     CmdArgs.push_back("-dwarf-debug-flags");
   2250     CmdArgs.push_back(Args.MakeArgString(Flags.str()));
   2251   }
   2252 
   2253   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   2254 
   2255   if (Arg *A = Args.getLastArg(options::OPT_pg))
   2256     if (Args.hasArg(options::OPT_fomit_frame_pointer))
   2257       D.Diag(diag::err_drv_argument_not_allowed_with)
   2258         << "-fomit-frame-pointer" << A->getAsString(Args);
   2259 
   2260   // Claim some arguments which clang supports automatically.
   2261 
   2262   // -fpch-preprocess is used with gcc to add a special marker in the output to
   2263   // include the PCH file. Clang's PTH solution is completely transparent, so we
   2264   // do not need to deal with it at all.
   2265   Args.ClaimAllArgs(options::OPT_fpch_preprocess);
   2266 
   2267   // Claim some arguments which clang doesn't support, but we don't
   2268   // care to warn the user about.
   2269   Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
   2270   Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
   2271 
   2272   // Disable warnings for clang -E -use-gold-plugin -emit-llvm foo.c
   2273   Args.ClaimAllArgs(options::OPT_use_gold_plugin);
   2274   Args.ClaimAllArgs(options::OPT_emit_llvm);
   2275 }
   2276 
   2277 void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
   2278                            const InputInfo &Output,
   2279                            const InputInfoList &Inputs,
   2280                            const ArgList &Args,
   2281                            const char *LinkingOutput) const {
   2282   ArgStringList CmdArgs;
   2283 
   2284   assert(Inputs.size() == 1 && "Unexpected number of inputs.");
   2285   const InputInfo &Input = Inputs[0];
   2286 
   2287   // Don't warn about "clang -w -c foo.s"
   2288   Args.ClaimAllArgs(options::OPT_w);
   2289   // and "clang -emit-llvm -c foo.s"
   2290   Args.ClaimAllArgs(options::OPT_emit_llvm);
   2291   // and "clang -use-gold-plugin -c foo.s"
   2292   Args.ClaimAllArgs(options::OPT_use_gold_plugin);
   2293 
   2294   // Invoke ourselves in -cc1as mode.
   2295   //
   2296   // FIXME: Implement custom jobs for internal actions.
   2297   CmdArgs.push_back("-cc1as");
   2298 
   2299   // Add the "effective" target triple.
   2300   CmdArgs.push_back("-triple");
   2301   std::string TripleStr =
   2302     getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
   2303   CmdArgs.push_back(Args.MakeArgString(TripleStr));
   2304 
   2305   // Set the output mode, we currently only expect to be used as a real
   2306   // assembler.
   2307   CmdArgs.push_back("-filetype");
   2308   CmdArgs.push_back("obj");
   2309 
   2310   if (UseRelaxAll(C, Args))
   2311     CmdArgs.push_back("-relax-all");
   2312 
   2313   // Ignore explicit -force_cpusubtype_ALL option.
   2314   (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
   2315 
   2316   // FIXME: Add -g support, once we have it.
   2317 
   2318   // FIXME: Add -static support, once we have it.
   2319 
   2320   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   2321                        options::OPT_Xassembler);
   2322   Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
   2323 
   2324   assert(Output.isFilename() && "Unexpected lipo output.");
   2325   CmdArgs.push_back("-o");
   2326   CmdArgs.push_back(Output.getFilename());
   2327 
   2328   assert(Input.isFilename() && "Invalid input.");
   2329   CmdArgs.push_back(Input.getFilename());
   2330 
   2331   const char *Exec = getToolChain().getDriver().getClangProgramPath();
   2332   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   2333 }
   2334 
   2335 void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
   2336                                const InputInfo &Output,
   2337                                const InputInfoList &Inputs,
   2338                                const ArgList &Args,
   2339                                const char *LinkingOutput) const {
   2340   const Driver &D = getToolChain().getDriver();
   2341   ArgStringList CmdArgs;
   2342 
   2343   for (ArgList::const_iterator
   2344          it = Args.begin(), ie = Args.end(); it != ie; ++it) {
   2345     Arg *A = *it;
   2346     if (A->getOption().hasForwardToGCC()) {
   2347       // Don't forward any -g arguments to assembly steps.
   2348       if (isa<AssembleJobAction>(JA) &&
   2349           A->getOption().matches(options::OPT_g_Group))
   2350         continue;
   2351 
   2352       // It is unfortunate that we have to claim here, as this means
   2353       // we will basically never report anything interesting for
   2354       // platforms using a generic gcc, even if we are just using gcc
   2355       // to get to the assembler.
   2356       A->claim();
   2357       A->render(Args, CmdArgs);
   2358     }
   2359   }
   2360 
   2361   RenderExtraToolArgs(JA, CmdArgs);
   2362 
   2363   // If using a driver driver, force the arch.
   2364   const std::string &Arch = getToolChain().getArchName();
   2365   if (getToolChain().getTriple().isOSDarwin()) {
   2366     CmdArgs.push_back("-arch");
   2367 
   2368     // FIXME: Remove these special cases.
   2369     if (Arch == "powerpc")
   2370       CmdArgs.push_back("ppc");
   2371     else if (Arch == "powerpc64")
   2372       CmdArgs.push_back("ppc64");
   2373     else
   2374       CmdArgs.push_back(Args.MakeArgString(Arch));
   2375   }
   2376 
   2377   // Try to force gcc to match the tool chain we want, if we recognize
   2378   // the arch.
   2379   //
   2380   // FIXME: The triple class should directly provide the information we want
   2381   // here.
   2382   if (Arch == "i386" || Arch == "powerpc")
   2383     CmdArgs.push_back("-m32");
   2384   else if (Arch == "x86_64" || Arch == "powerpc64")
   2385     CmdArgs.push_back("-m64");
   2386 
   2387   if (Output.isFilename()) {
   2388     CmdArgs.push_back("-o");
   2389     CmdArgs.push_back(Output.getFilename());
   2390   } else {
   2391     assert(Output.isNothing() && "Unexpected output");
   2392     CmdArgs.push_back("-fsyntax-only");
   2393   }
   2394 
   2395 
   2396   // Only pass -x if gcc will understand it; otherwise hope gcc
   2397   // understands the suffix correctly. The main use case this would go
   2398   // wrong in is for linker inputs if they happened to have an odd
   2399   // suffix; really the only way to get this to happen is a command
   2400   // like '-x foobar a.c' which will treat a.c like a linker input.
   2401   //
   2402   // FIXME: For the linker case specifically, can we safely convert
   2403   // inputs into '-Wl,' options?
   2404   for (InputInfoList::const_iterator
   2405          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   2406     const InputInfo &II = *it;
   2407 
   2408     // Don't try to pass LLVM or AST inputs to a generic gcc.
   2409     if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
   2410         II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
   2411       D.Diag(diag::err_drv_no_linker_llvm_support)
   2412         << getToolChain().getTripleString();
   2413     else if (II.getType() == types::TY_AST)
   2414       D.Diag(diag::err_drv_no_ast_support)
   2415         << getToolChain().getTripleString();
   2416 
   2417     if (types::canTypeBeUserSpecified(II.getType())) {
   2418       CmdArgs.push_back("-x");
   2419       CmdArgs.push_back(types::getTypeName(II.getType()));
   2420     }
   2421 
   2422     if (II.isFilename())
   2423       CmdArgs.push_back(II.getFilename());
   2424     else {
   2425       const Arg &A = II.getInputArg();
   2426 
   2427       // Reverse translate some rewritten options.
   2428       if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
   2429         CmdArgs.push_back("-lstdc++");
   2430         continue;
   2431       }
   2432 
   2433       // Don't render as input, we need gcc to do the translations.
   2434       A.render(Args, CmdArgs);
   2435     }
   2436   }
   2437 
   2438   const std::string customGCCName = D.getCCCGenericGCCName();
   2439   const char *GCCName;
   2440   if (!customGCCName.empty())
   2441     GCCName = customGCCName.c_str();
   2442   else if (D.CCCIsCXX) {
   2443 #ifdef IS_CYGWIN15
   2444     // FIXME: Detect the version of Cygwin at runtime?
   2445     GCCName = "g++-4";
   2446 #else
   2447     GCCName = "g++";
   2448 #endif
   2449   } else
   2450     GCCName = "gcc";
   2451 
   2452   const char *Exec =
   2453     Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
   2454   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   2455 }
   2456 
   2457 void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
   2458                                           ArgStringList &CmdArgs) const {
   2459   CmdArgs.push_back("-E");
   2460 }
   2461 
   2462 void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
   2463                                           ArgStringList &CmdArgs) const {
   2464   // The type is good enough.
   2465 }
   2466 
   2467 void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
   2468                                        ArgStringList &CmdArgs) const {
   2469   const Driver &D = getToolChain().getDriver();
   2470 
   2471   // If -flto, etc. are present then make sure not to force assembly output.
   2472   if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
   2473       JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
   2474     CmdArgs.push_back("-c");
   2475   else {
   2476     if (JA.getType() != types::TY_PP_Asm)
   2477       D.Diag(diag::err_drv_invalid_gcc_output_type)
   2478         << getTypeName(JA.getType());
   2479 
   2480     CmdArgs.push_back("-S");
   2481   }
   2482 }
   2483 
   2484 void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
   2485                                         ArgStringList &CmdArgs) const {
   2486   CmdArgs.push_back("-c");
   2487 }
   2488 
   2489 void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
   2490                                     ArgStringList &CmdArgs) const {
   2491   // The types are (hopefully) good enough.
   2492 }
   2493 
   2494 const char *darwin::CC1::getCC1Name(types::ID Type) const {
   2495   switch (Type) {
   2496   default:
   2497     llvm_unreachable("Unexpected type for Darwin CC1 tool.");
   2498   case types::TY_Asm:
   2499   case types::TY_C: case types::TY_CHeader:
   2500   case types::TY_PP_C: case types::TY_PP_CHeader:
   2501     return "cc1";
   2502   case types::TY_ObjC: case types::TY_ObjCHeader:
   2503   case types::TY_PP_ObjC: case types::TY_PP_ObjC_Alias:
   2504   case types::TY_PP_ObjCHeader:
   2505     return "cc1obj";
   2506   case types::TY_CXX: case types::TY_CXXHeader:
   2507   case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
   2508     return "cc1plus";
   2509   case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
   2510   case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXX_Alias:
   2511   case types::TY_PP_ObjCXXHeader:
   2512     return "cc1objplus";
   2513   }
   2514 }
   2515 
   2516 const char *darwin::CC1::getBaseInputName(const ArgList &Args,
   2517                                           const InputInfoList &Inputs) {
   2518   return Args.MakeArgString(
   2519     llvm::sys::path::filename(Inputs[0].getBaseInput()));
   2520 }
   2521 
   2522 const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
   2523                                           const InputInfoList &Inputs) {
   2524   const char *Str = getBaseInputName(Args, Inputs);
   2525 
   2526   if (const char *End = strrchr(Str, '.'))
   2527     return Args.MakeArgString(std::string(Str, End));
   2528 
   2529   return Str;
   2530 }
   2531 
   2532 const char *
   2533 darwin::CC1::getDependencyFileName(const ArgList &Args,
   2534                                    const InputInfoList &Inputs) {
   2535   // FIXME: Think about this more.
   2536   std::string Res;
   2537 
   2538   if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
   2539     std::string Str(OutputOpt->getValue(Args));
   2540     Res = Str.substr(0, Str.rfind('.'));
   2541   } else {
   2542     Res = darwin::CC1::getBaseInputStem(Args, Inputs);
   2543   }
   2544   return Args.MakeArgString(Res + ".d");
   2545 }
   2546 
   2547 void darwin::CC1::RemoveCC1UnsupportedArgs(ArgStringList &CmdArgs) const {
   2548   for (ArgStringList::iterator it = CmdArgs.begin(), ie = CmdArgs.end();
   2549        it != ie;) {
   2550 
   2551     StringRef Option = *it;
   2552     bool RemoveOption = false;
   2553 
   2554     // Remove -faltivec
   2555     if (Option.equals("-faltivec")) {
   2556       it = CmdArgs.erase(it);
   2557       ie = CmdArgs.end();
   2558       continue;
   2559     }
   2560 
   2561     // Handle machine specific options.
   2562     if (Option.startswith("-m")) {
   2563       RemoveOption = llvm::StringSwitch<bool>(Option)
   2564         .Case("-mthumb", true)
   2565         .Case("-mno-thumb", true)
   2566         .Case("-mno-fused-madd", true)
   2567         .Case("-mlong-branch", true)
   2568         .Case("-mlongcall", true)
   2569         .Case("-mcpu=G4", true)
   2570         .Case("-mcpu=G5", true)
   2571         .Default(false);
   2572     }
   2573 
   2574     // Handle warning options.
   2575     if (Option.startswith("-W")) {
   2576       // Remove -W/-Wno- to reduce the number of cases.
   2577       if (Option.startswith("-Wno-"))
   2578         Option = Option.substr(5);
   2579       else
   2580         Option = Option.substr(2);
   2581 
   2582       RemoveOption = llvm::StringSwitch<bool>(Option)
   2583         .Case("address-of-temporary", true)
   2584         .Case("ambiguous-member-template", true)
   2585         .Case("analyzer-incompatible-plugin", true)
   2586         .Case("array-bounds", true)
   2587         .Case("array-bounds-pointer-arithmetic", true)
   2588         .Case("bind-to-temporary-copy", true)
   2589         .Case("bitwise-op-parentheses", true)
   2590         .Case("bool-conversions", true)
   2591         .Case("builtin-macro-redefined", true)
   2592         .Case("c++-hex-floats", true)
   2593         .Case("c++0x-compat", true)
   2594         .Case("c++0x-extensions", true)
   2595         .Case("c++0x-narrowing", true)
   2596         .Case("c++11-compat", true)
   2597         .Case("c++11-extensions", true)
   2598         .Case("c++11-narrowing", true)
   2599         .Case("conditional-uninitialized", true)
   2600         .Case("constant-conversion", true)
   2601         .Case("CFString-literal", true)
   2602         .Case("constant-logical-operand", true)
   2603         .Case("custom-atomic-properties", true)
   2604         .Case("default-arg-special-member", true)
   2605         .Case("delegating-ctor-cycles", true)
   2606         .Case("delete-non-virtual-dtor", true)
   2607         .Case("deprecated-implementations", true)
   2608         .Case("deprecated-writable-strings", true)
   2609         .Case("distributed-object-modifiers", true)
   2610         .Case("duplicate-method-arg", true)
   2611         .Case("dynamic-class-memaccess", true)
   2612         .Case("enum-compare", true)
   2613         .Case("exit-time-destructors", true)
   2614         .Case("gnu", true)
   2615         .Case("gnu-designator", true)
   2616         .Case("header-hygiene", true)
   2617         .Case("idiomatic-parentheses", true)
   2618         .Case("ignored-qualifiers", true)
   2619         .Case("implicit-atomic-properties", true)
   2620         .Case("incompatible-pointer-types", true)
   2621         .Case("incomplete-implementation", true)
   2622         .Case("initializer-overrides", true)
   2623         .Case("invalid-noreturn", true)
   2624         .Case("invalid-token-paste", true)
   2625         .Case("language-extension-token", true)
   2626         .Case("literal-conversion", true)
   2627         .Case("literal-range", true)
   2628         .Case("local-type-template-args", true)
   2629         .Case("logical-op-parentheses", true)
   2630         .Case("method-signatures", true)
   2631         .Case("microsoft", true)
   2632         .Case("mismatched-tags", true)
   2633         .Case("missing-method-return-type", true)
   2634         .Case("non-pod-varargs", true)
   2635         .Case("nonfragile-abi2", true)
   2636         .Case("null-arithmetic", true)
   2637         .Case("null-dereference", true)
   2638         .Case("out-of-line-declaration", true)
   2639         .Case("overriding-method-mismatch", true)
   2640         .Case("readonly-setter-attrs", true)
   2641         .Case("return-stack-address", true)
   2642         .Case("self-assign", true)
   2643         .Case("semicolon-before-method-body", true)
   2644         .Case("sentinel", true)
   2645         .Case("shift-overflow", true)
   2646         .Case("shift-sign-overflow", true)
   2647         .Case("sign-conversion", true)
   2648         .Case("sizeof-array-argument", true)
   2649         .Case("sizeof-pointer-memaccess", true)
   2650         .Case("string-compare", true)
   2651         .Case("super-class-method-mismatch", true)
   2652         .Case("tautological-compare", true)
   2653         .Case("typedef-redefinition", true)
   2654         .Case("typename-missing", true)
   2655         .Case("undefined-reinterpret-cast", true)
   2656         .Case("unknown-warning-option", true)
   2657         .Case("unnamed-type-template-args", true)
   2658         .Case("unneeded-internal-declaration", true)
   2659         .Case("unneeded-member-function", true)
   2660         .Case("unused-comparison", true)
   2661         .Case("unused-exception-parameter", true)
   2662         .Case("unused-member-function", true)
   2663         .Case("unused-result", true)
   2664         .Case("vector-conversions", true)
   2665         .Case("vla", true)
   2666         .Case("used-but-marked-unused", true)
   2667         .Case("weak-vtables", true)
   2668         .Default(false);
   2669     } // if (Option.startswith("-W"))
   2670     if (RemoveOption) {
   2671       it = CmdArgs.erase(it);
   2672       ie = CmdArgs.end();
   2673     } else {
   2674       ++it;
   2675     }
   2676   }
   2677 }
   2678 
   2679 void darwin::CC1::AddCC1Args(const ArgList &Args,
   2680                              ArgStringList &CmdArgs) const {
   2681   const Driver &D = getToolChain().getDriver();
   2682 
   2683   CheckCodeGenerationOptions(D, Args);
   2684 
   2685   // Derived from cc1 spec.
   2686   if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
   2687       !Args.hasArg(options::OPT_mdynamic_no_pic))
   2688     CmdArgs.push_back("-fPIC");
   2689 
   2690   if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
   2691       getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
   2692     if (!Args.hasArg(options::OPT_fbuiltin_strcat))
   2693       CmdArgs.push_back("-fno-builtin-strcat");
   2694     if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
   2695       CmdArgs.push_back("-fno-builtin-strcpy");
   2696   }
   2697 
   2698   if (Args.hasArg(options::OPT_g_Flag) &&
   2699       !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
   2700     CmdArgs.push_back("-feliminate-unused-debug-symbols");
   2701 }
   2702 
   2703 void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
   2704                                     const InputInfoList &Inputs,
   2705                                     const ArgStringList &OutputArgs) const {
   2706   const Driver &D = getToolChain().getDriver();
   2707 
   2708   // Derived from cc1_options spec.
   2709   if (Args.hasArg(options::OPT_fast) ||
   2710       Args.hasArg(options::OPT_fastf) ||
   2711       Args.hasArg(options::OPT_fastcp))
   2712     CmdArgs.push_back("-O3");
   2713 
   2714   if (Arg *A = Args.getLastArg(options::OPT_pg))
   2715     if (Args.hasArg(options::OPT_fomit_frame_pointer))
   2716       D.Diag(diag::err_drv_argument_not_allowed_with)
   2717         << A->getAsString(Args) << "-fomit-frame-pointer";
   2718 
   2719   AddCC1Args(Args, CmdArgs);
   2720 
   2721   if (!Args.hasArg(options::OPT_Q))
   2722     CmdArgs.push_back("-quiet");
   2723 
   2724   CmdArgs.push_back("-dumpbase");
   2725   CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
   2726 
   2727   Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
   2728 
   2729   Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
   2730   Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
   2731 
   2732   // FIXME: The goal is to use the user provided -o if that is our
   2733   // final output, otherwise to drive from the original input
   2734   // name. Find a clean way to go about this.
   2735   if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
   2736       Args.hasArg(options::OPT_o)) {
   2737     Arg *OutputOpt = Args.getLastArg(options::OPT_o);
   2738     CmdArgs.push_back("-auxbase-strip");
   2739     CmdArgs.push_back(OutputOpt->getValue(Args));
   2740   } else {
   2741     CmdArgs.push_back("-auxbase");
   2742     CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
   2743   }
   2744 
   2745   Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
   2746 
   2747   Args.AddAllArgs(CmdArgs, options::OPT_O);
   2748   // FIXME: -Wall is getting some special treatment. Investigate.
   2749   Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
   2750   Args.AddLastArg(CmdArgs, options::OPT_w);
   2751   Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
   2752                   options::OPT_trigraphs);
   2753   if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
   2754     // Honor -std-default.
   2755     Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
   2756                               "-std=", /*Joined=*/true);
   2757   }
   2758 
   2759   if (Args.hasArg(options::OPT_v))
   2760     CmdArgs.push_back("-version");
   2761   if (Args.hasArg(options::OPT_pg) &&
   2762       getToolChain().SupportsProfiling())
   2763     CmdArgs.push_back("-p");
   2764   Args.AddLastArg(CmdArgs, options::OPT_p);
   2765 
   2766   // The driver treats -fsyntax-only specially.
   2767   if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
   2768       getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
   2769     // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
   2770     // used to inhibit the default -fno-builtin-str{cat,cpy}.
   2771     //
   2772     // FIXME: Should we grow a better way to deal with "removing" args?
   2773     for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
   2774                                                options::OPT_fsyntax_only),
   2775            ie = Args.filtered_end(); it != ie; ++it) {
   2776       if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
   2777           !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
   2778         (*it)->claim();
   2779         (*it)->render(Args, CmdArgs);
   2780       }
   2781     }
   2782   } else
   2783     Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
   2784 
   2785   // Claim Clang only -f options, they aren't worth warning about.
   2786   Args.ClaimAllArgs(options::OPT_f_clang_Group);
   2787 
   2788   Args.AddAllArgs(CmdArgs, options::OPT_undef);
   2789   if (Args.hasArg(options::OPT_Qn))
   2790     CmdArgs.push_back("-fno-ident");
   2791 
   2792   // FIXME: This isn't correct.
   2793   //Args.AddLastArg(CmdArgs, options::OPT__help)
   2794   //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
   2795 
   2796   CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
   2797 
   2798   // FIXME: Still don't get what is happening here. Investigate.
   2799   Args.AddAllArgs(CmdArgs, options::OPT__param);
   2800 
   2801   if (Args.hasArg(options::OPT_fmudflap) ||
   2802       Args.hasArg(options::OPT_fmudflapth)) {
   2803     CmdArgs.push_back("-fno-builtin");
   2804     CmdArgs.push_back("-fno-merge-constants");
   2805   }
   2806 
   2807   if (Args.hasArg(options::OPT_coverage)) {
   2808     CmdArgs.push_back("-fprofile-arcs");
   2809     CmdArgs.push_back("-ftest-coverage");
   2810   }
   2811 
   2812   if (types::isCXX(Inputs[0].getType()))
   2813     CmdArgs.push_back("-D__private_extern__=extern");
   2814 }
   2815 
   2816 void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
   2817                                     const InputInfoList &Inputs,
   2818                                     const ArgStringList &OutputArgs) const {
   2819   // Derived from cpp_options
   2820   AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
   2821 
   2822   CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
   2823 
   2824   AddCC1Args(Args, CmdArgs);
   2825 
   2826   // NOTE: The code below has some commonality with cpp_options, but
   2827   // in classic gcc style ends up sending things in different
   2828   // orders. This may be a good merge candidate once we drop pedantic
   2829   // compatibility.
   2830 
   2831   Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
   2832   Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
   2833                   options::OPT_trigraphs);
   2834   if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
   2835     // Honor -std-default.
   2836     Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
   2837                               "-std=", /*Joined=*/true);
   2838   }
   2839   Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
   2840   Args.AddLastArg(CmdArgs, options::OPT_w);
   2841 
   2842   // The driver treats -fsyntax-only specially.
   2843   Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
   2844 
   2845   // Claim Clang only -f options, they aren't worth warning about.
   2846   Args.ClaimAllArgs(options::OPT_f_clang_Group);
   2847 
   2848   if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
   2849       !Args.hasArg(options::OPT_fno_working_directory))
   2850     CmdArgs.push_back("-fworking-directory");
   2851 
   2852   Args.AddAllArgs(CmdArgs, options::OPT_O);
   2853   Args.AddAllArgs(CmdArgs, options::OPT_undef);
   2854   if (Args.hasArg(options::OPT_save_temps))
   2855     CmdArgs.push_back("-fpch-preprocess");
   2856 }
   2857 
   2858 void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
   2859                                           ArgStringList &CmdArgs,
   2860                                           const InputInfoList &Inputs) const {
   2861   const Driver &D = getToolChain().getDriver();
   2862 
   2863   CheckPreprocessingOptions(D, Args);
   2864 
   2865   // Derived from cpp_unique_options.
   2866   // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
   2867   Args.AddLastArg(CmdArgs, options::OPT_C);
   2868   Args.AddLastArg(CmdArgs, options::OPT_CC);
   2869   if (!Args.hasArg(options::OPT_Q))
   2870     CmdArgs.push_back("-quiet");
   2871   Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
   2872   Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
   2873   Args.AddLastArg(CmdArgs, options::OPT_v);
   2874   Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
   2875   Args.AddLastArg(CmdArgs, options::OPT_P);
   2876 
   2877   // FIXME: Handle %I properly.
   2878   if (getToolChain().getArchName() == "x86_64") {
   2879     CmdArgs.push_back("-imultilib");
   2880     CmdArgs.push_back("x86_64");
   2881   }
   2882 
   2883   if (Args.hasArg(options::OPT_MD)) {
   2884     CmdArgs.push_back("-MD");
   2885     CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
   2886   }
   2887 
   2888   if (Args.hasArg(options::OPT_MMD)) {
   2889     CmdArgs.push_back("-MMD");
   2890     CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
   2891   }
   2892 
   2893   Args.AddLastArg(CmdArgs, options::OPT_M);
   2894   Args.AddLastArg(CmdArgs, options::OPT_MM);
   2895   Args.AddAllArgs(CmdArgs, options::OPT_MF);
   2896   Args.AddLastArg(CmdArgs, options::OPT_MG);
   2897   Args.AddLastArg(CmdArgs, options::OPT_MP);
   2898   Args.AddAllArgs(CmdArgs, options::OPT_MQ);
   2899   Args.AddAllArgs(CmdArgs, options::OPT_MT);
   2900   if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
   2901       (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
   2902     if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
   2903       CmdArgs.push_back("-MQ");
   2904       CmdArgs.push_back(OutputOpt->getValue(Args));
   2905     }
   2906   }
   2907 
   2908   Args.AddLastArg(CmdArgs, options::OPT_remap);
   2909   if (Args.hasArg(options::OPT_g3))
   2910     CmdArgs.push_back("-dD");
   2911   Args.AddLastArg(CmdArgs, options::OPT_H);
   2912 
   2913   AddCPPArgs(Args, CmdArgs);
   2914 
   2915   Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
   2916   Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
   2917 
   2918   for (InputInfoList::const_iterator
   2919          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   2920     const InputInfo &II = *it;
   2921 
   2922     CmdArgs.push_back(II.getFilename());
   2923   }
   2924 
   2925   Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
   2926                        options::OPT_Xpreprocessor);
   2927 
   2928   if (Args.hasArg(options::OPT_fmudflap)) {
   2929     CmdArgs.push_back("-D_MUDFLAP");
   2930     CmdArgs.push_back("-include");
   2931     CmdArgs.push_back("mf-runtime.h");
   2932   }
   2933 
   2934   if (Args.hasArg(options::OPT_fmudflapth)) {
   2935     CmdArgs.push_back("-D_MUDFLAP");
   2936     CmdArgs.push_back("-D_MUDFLAPTH");
   2937     CmdArgs.push_back("-include");
   2938     CmdArgs.push_back("mf-runtime.h");
   2939   }
   2940 }
   2941 
   2942 void darwin::CC1::AddCPPArgs(const ArgList &Args,
   2943                              ArgStringList &CmdArgs) const {
   2944   // Derived from cpp spec.
   2945 
   2946   if (Args.hasArg(options::OPT_static)) {
   2947     // The gcc spec is broken here, it refers to dynamic but
   2948     // that has been translated. Start by being bug compatible.
   2949 
   2950     // if (!Args.hasArg(arglist.parser.dynamicOption))
   2951     CmdArgs.push_back("-D__STATIC__");
   2952   } else
   2953     CmdArgs.push_back("-D__DYNAMIC__");
   2954 
   2955   if (Args.hasArg(options::OPT_pthread))
   2956     CmdArgs.push_back("-D_REENTRANT");
   2957 }
   2958 
   2959 void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
   2960                                       const InputInfo &Output,
   2961                                       const InputInfoList &Inputs,
   2962                                       const ArgList &Args,
   2963                                       const char *LinkingOutput) const {
   2964   ArgStringList CmdArgs;
   2965 
   2966   assert(Inputs.size() == 1 && "Unexpected number of inputs!");
   2967 
   2968   CmdArgs.push_back("-E");
   2969 
   2970   if (Args.hasArg(options::OPT_traditional) ||
   2971       Args.hasArg(options::OPT_traditional_cpp))
   2972     CmdArgs.push_back("-traditional-cpp");
   2973 
   2974   ArgStringList OutputArgs;
   2975   assert(Output.isFilename() && "Unexpected CC1 output.");
   2976   OutputArgs.push_back("-o");
   2977   OutputArgs.push_back(Output.getFilename());
   2978 
   2979   if (Args.hasArg(options::OPT_E) || getToolChain().getDriver().CCCIsCPP) {
   2980     AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
   2981   } else {
   2982     AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
   2983     CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
   2984   }
   2985 
   2986   Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
   2987 
   2988   RemoveCC1UnsupportedArgs(CmdArgs);
   2989 
   2990   const char *CC1Name = getCC1Name(Inputs[0].getType());
   2991   const char *Exec =
   2992     Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
   2993   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   2994 }
   2995 
   2996 void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
   2997                                    const InputInfo &Output,
   2998                                    const InputInfoList &Inputs,
   2999                                    const ArgList &Args,
   3000                                    const char *LinkingOutput) const {
   3001   const Driver &D = getToolChain().getDriver();
   3002   ArgStringList CmdArgs;
   3003 
   3004   assert(Inputs.size() == 1 && "Unexpected number of inputs!");
   3005 
   3006   types::ID InputType = Inputs[0].getType();
   3007   const Arg *A;
   3008   if ((A = Args.getLastArg(options::OPT_traditional)))
   3009     D.Diag(diag::err_drv_argument_only_allowed_with)
   3010       << A->getAsString(Args) << "-E";
   3011 
   3012   if (JA.getType() == types::TY_LLVM_IR ||
   3013       JA.getType() == types::TY_LTO_IR)
   3014     CmdArgs.push_back("-emit-llvm");
   3015   else if (JA.getType() == types::TY_LLVM_BC ||
   3016            JA.getType() == types::TY_LTO_BC)
   3017     CmdArgs.push_back("-emit-llvm-bc");
   3018   else if (Output.getType() == types::TY_AST)
   3019     D.Diag(diag::err_drv_no_ast_support)
   3020       << getToolChain().getTripleString();
   3021   else if (JA.getType() != types::TY_PP_Asm &&
   3022            JA.getType() != types::TY_PCH)
   3023     D.Diag(diag::err_drv_invalid_gcc_output_type)
   3024       << getTypeName(JA.getType());
   3025 
   3026   ArgStringList OutputArgs;
   3027   if (Output.getType() != types::TY_PCH) {
   3028     OutputArgs.push_back("-o");
   3029     if (Output.isNothing())
   3030       OutputArgs.push_back("/dev/null");
   3031     else
   3032       OutputArgs.push_back(Output.getFilename());
   3033   }
   3034 
   3035   // There is no need for this level of compatibility, but it makes
   3036   // diffing easier.
   3037   bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
   3038                           Args.hasArg(options::OPT_S));
   3039 
   3040   if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
   3041     AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
   3042     if (OutputArgsEarly) {
   3043       AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
   3044     } else {
   3045       AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
   3046       CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
   3047     }
   3048   } else {
   3049     CmdArgs.push_back("-fpreprocessed");
   3050 
   3051     for (InputInfoList::const_iterator
   3052            it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   3053       const InputInfo &II = *it;
   3054 
   3055       // Reject AST inputs.
   3056       if (II.getType() == types::TY_AST) {
   3057         D.Diag(diag::err_drv_no_ast_support)
   3058           << getToolChain().getTripleString();
   3059         return;
   3060       }
   3061 
   3062       CmdArgs.push_back(II.getFilename());
   3063     }
   3064 
   3065     if (OutputArgsEarly) {
   3066       AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
   3067     } else {
   3068       AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
   3069       CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
   3070     }
   3071   }
   3072 
   3073   if (Output.getType() == types::TY_PCH) {
   3074     assert(Output.isFilename() && "Invalid PCH output.");
   3075 
   3076     CmdArgs.push_back("-o");
   3077     // NOTE: gcc uses a temp .s file for this, but there doesn't seem
   3078     // to be a good reason.
   3079     const char *TmpPath = C.getArgs().MakeArgString(
   3080       D.GetTemporaryPath("cc", "s"));
   3081     C.addTempFile(TmpPath);
   3082     CmdArgs.push_back(TmpPath);
   3083 
   3084     // If we're emitting a pch file with the last 4 characters of ".pth"
   3085     // and falling back to llvm-gcc we want to use ".gch" instead.
   3086     std::string OutputFile(Output.getFilename());
   3087     size_t loc = OutputFile.rfind(".pth");
   3088     if (loc != std::string::npos)
   3089       OutputFile.replace(loc, 4, ".gch");
   3090     const char *Tmp = C.getArgs().MakeArgString("--output-pch="+OutputFile);
   3091     CmdArgs.push_back(Tmp);
   3092   }
   3093 
   3094   RemoveCC1UnsupportedArgs(CmdArgs);
   3095 
   3096   const char *CC1Name = getCC1Name(Inputs[0].getType());
   3097   const char *Exec =
   3098     Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
   3099   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3100 }
   3101 
   3102 void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   3103                                     const InputInfo &Output,
   3104                                     const InputInfoList &Inputs,
   3105                                     const ArgList &Args,
   3106                                     const char *LinkingOutput) const {
   3107   ArgStringList CmdArgs;
   3108 
   3109   assert(Inputs.size() == 1 && "Unexpected number of inputs.");
   3110   const InputInfo &Input = Inputs[0];
   3111 
   3112   // Determine the original source input.
   3113   const Action *SourceAction = &JA;
   3114   while (SourceAction->getKind() != Action::InputClass) {
   3115     assert(!SourceAction->getInputs().empty() && "unexpected root action!");
   3116     SourceAction = SourceAction->getInputs()[0];
   3117   }
   3118 
   3119   // Forward -g, assuming we are dealing with an actual assembly file.
   3120   if (SourceAction->getType() == types::TY_Asm ||
   3121       SourceAction->getType() == types::TY_PP_Asm) {
   3122     if (Args.hasArg(options::OPT_gstabs))
   3123       CmdArgs.push_back("--gstabs");
   3124     else if (Args.hasArg(options::OPT_g_Group))
   3125       CmdArgs.push_back("--gdwarf2");
   3126   }
   3127 
   3128   // Derived from asm spec.
   3129   AddDarwinArch(Args, CmdArgs);
   3130 
   3131   // Use -force_cpusubtype_ALL on x86 by default.
   3132   if (getToolChain().getTriple().getArch() == llvm::Triple::x86 ||
   3133       getToolChain().getTriple().getArch() == llvm::Triple::x86_64 ||
   3134       Args.hasArg(options::OPT_force__cpusubtype__ALL))
   3135     CmdArgs.push_back("-force_cpusubtype_ALL");
   3136 
   3137   if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
   3138       (Args.hasArg(options::OPT_mkernel) ||
   3139        Args.hasArg(options::OPT_static) ||
   3140        Args.hasArg(options::OPT_fapple_kext)))
   3141     CmdArgs.push_back("-static");
   3142 
   3143   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   3144                        options::OPT_Xassembler);
   3145 
   3146   assert(Output.isFilename() && "Unexpected lipo output.");
   3147   CmdArgs.push_back("-o");
   3148   CmdArgs.push_back(Output.getFilename());
   3149 
   3150   assert(Input.isFilename() && "Invalid input.");
   3151   CmdArgs.push_back(Input.getFilename());
   3152 
   3153   // asm_final spec is empty.
   3154 
   3155   const char *Exec =
   3156     Args.MakeArgString(getToolChain().GetProgramPath("as"));
   3157   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3158 }
   3159 
   3160 void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
   3161                                        ArgStringList &CmdArgs) const {
   3162   StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
   3163 
   3164   // Derived from darwin_arch spec.
   3165   CmdArgs.push_back("-arch");
   3166   CmdArgs.push_back(Args.MakeArgString(ArchName));
   3167 
   3168   // FIXME: Is this needed anymore?
   3169   if (ArchName == "arm")
   3170     CmdArgs.push_back("-force_cpusubtype_ALL");
   3171 }
   3172 
   3173 void darwin::Link::AddLinkArgs(Compilation &C,
   3174                                const ArgList &Args,
   3175                                ArgStringList &CmdArgs) const {
   3176   const Driver &D = getToolChain().getDriver();
   3177   const toolchains::Darwin &DarwinTC = getDarwinToolChain();
   3178 
   3179   unsigned Version[3] = { 0, 0, 0 };
   3180   if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
   3181     bool HadExtra;
   3182     if (!Driver::GetReleaseVersion(A->getValue(Args), Version[0],
   3183                                    Version[1], Version[2], HadExtra) ||
   3184         HadExtra)
   3185       D.Diag(diag::err_drv_invalid_version_number)
   3186         << A->getAsString(Args);
   3187   }
   3188 
   3189   // Newer linkers support -demangle, pass it if supported and not disabled by
   3190   // the user.
   3191   //
   3192   // FIXME: We temporarily avoid passing -demangle to any iOS linker, because
   3193   // unfortunately we can't be guaranteed that the linker version used there
   3194   // will match the linker version detected at configure time. We need the
   3195   // universal driver.
   3196   if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle) &&
   3197       !DarwinTC.isTargetIPhoneOS()) {
   3198     // Don't pass -demangle to ld_classic.
   3199     //
   3200     // FIXME: This is a temporary workaround, ld should be handling this.
   3201     bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
   3202                           Args.hasArg(options::OPT_static));
   3203     if (getToolChain().getArch() == llvm::Triple::x86) {
   3204       for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
   3205                                                  options::OPT_Wl_COMMA),
   3206              ie = Args.filtered_end(); it != ie; ++it) {
   3207         const Arg *A = *it;
   3208         for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
   3209           if (StringRef(A->getValue(Args, i)) == "-kext")
   3210             UsesLdClassic = true;
   3211       }
   3212     }
   3213     if (!UsesLdClassic)
   3214       CmdArgs.push_back("-demangle");
   3215   }
   3216 
   3217   // If we are using LTO, then automatically create a temporary file path for
   3218   // the linker to use, so that it's lifetime will extend past a possible
   3219   // dsymutil step.
   3220   if (Version[0] >= 116 && D.IsUsingLTO(Args)) {
   3221     const char *TmpPath = C.getArgs().MakeArgString(
   3222       D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
   3223     C.addTempFile(TmpPath);
   3224     CmdArgs.push_back("-object_path_lto");
   3225     CmdArgs.push_back(TmpPath);
   3226   }
   3227 
   3228   // Derived from the "link" spec.
   3229   Args.AddAllArgs(CmdArgs, options::OPT_static);
   3230   if (!Args.hasArg(options::OPT_static))
   3231     CmdArgs.push_back("-dynamic");
   3232   if (Args.hasArg(options::OPT_fgnu_runtime)) {
   3233     // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
   3234     // here. How do we wish to handle such things?
   3235   }
   3236 
   3237   if (!Args.hasArg(options::OPT_dynamiclib)) {
   3238     AddDarwinArch(Args, CmdArgs);
   3239     // FIXME: Why do this only on this path?
   3240     Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
   3241 
   3242     Args.AddLastArg(CmdArgs, options::OPT_bundle);
   3243     Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
   3244     Args.AddAllArgs(CmdArgs, options::OPT_client__name);
   3245 
   3246     Arg *A;
   3247     if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
   3248         (A = Args.getLastArg(options::OPT_current__version)) ||
   3249         (A = Args.getLastArg(options::OPT_install__name)))
   3250       D.Diag(diag::err_drv_argument_only_allowed_with)
   3251         << A->getAsString(Args) << "-dynamiclib";
   3252 
   3253     Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
   3254     Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
   3255     Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
   3256   } else {
   3257     CmdArgs.push_back("-dylib");
   3258 
   3259     Arg *A;
   3260     if ((A = Args.getLastArg(options::OPT_bundle)) ||
   3261         (A = Args.getLastArg(options::OPT_bundle__loader)) ||
   3262         (A = Args.getLastArg(options::OPT_client__name)) ||
   3263         (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
   3264         (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
   3265         (A = Args.getLastArg(options::OPT_private__bundle)))
   3266       D.Diag(diag::err_drv_argument_not_allowed_with)
   3267         << A->getAsString(Args) << "-dynamiclib";
   3268 
   3269     Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
   3270                               "-dylib_compatibility_version");
   3271     Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
   3272                               "-dylib_current_version");
   3273 
   3274     AddDarwinArch(Args, CmdArgs);
   3275 
   3276     Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
   3277                               "-dylib_install_name");
   3278   }
   3279 
   3280   Args.AddLastArg(CmdArgs, options::OPT_all__load);
   3281   Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
   3282   Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
   3283   if (DarwinTC.isTargetIPhoneOS())
   3284     Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
   3285   Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
   3286   Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
   3287   Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
   3288   Args.AddLastArg(CmdArgs, options::OPT_dynamic);
   3289   Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
   3290   Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
   3291   Args.AddAllArgs(CmdArgs, options::OPT_force__load);
   3292   Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
   3293   Args.AddAllArgs(CmdArgs, options::OPT_image__base);
   3294   Args.AddAllArgs(CmdArgs, options::OPT_init);
   3295 
   3296   // Add the deployment target.
   3297   unsigned TargetVersion[3];
   3298   DarwinTC.getTargetVersion(TargetVersion);
   3299 
   3300   // If we had an explicit -mios-simulator-version-min argument, honor that,
   3301   // otherwise use the traditional deployment targets. We can't just check the
   3302   // is-sim attribute because existing code follows this path, and the linker
   3303   // may not handle the argument.
   3304   //
   3305   // FIXME: We may be able to remove this, once we can verify no one depends on
   3306   // it.
   3307   if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
   3308     CmdArgs.push_back("-ios_simulator_version_min");
   3309   else if (DarwinTC.isTargetIPhoneOS())
   3310     CmdArgs.push_back("-iphoneos_version_min");
   3311   else
   3312     CmdArgs.push_back("-macosx_version_min");
   3313   CmdArgs.push_back(Args.MakeArgString(Twine(TargetVersion[0]) + "." +
   3314                                        Twine(TargetVersion[1]) + "." +
   3315                                        Twine(TargetVersion[2])));
   3316 
   3317   Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
   3318   Args.AddLastArg(CmdArgs, options::OPT_multi__module);
   3319   Args.AddLastArg(CmdArgs, options::OPT_single__module);
   3320   Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
   3321   Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
   3322 
   3323   if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
   3324                                      options::OPT_fno_pie,
   3325                                      options::OPT_fno_PIE)) {
   3326     if (A->getOption().matches(options::OPT_fpie) ||
   3327         A->getOption().matches(options::OPT_fPIE))
   3328       CmdArgs.push_back("-pie");
   3329     else
   3330       CmdArgs.push_back("-no_pie");
   3331   }
   3332 
   3333   Args.AddLastArg(CmdArgs, options::OPT_prebind);
   3334   Args.AddLastArg(CmdArgs, options::OPT_noprebind);
   3335   Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
   3336   Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
   3337   Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
   3338   Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
   3339   Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
   3340   Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
   3341   Args.AddAllArgs(CmdArgs, options::OPT_segprot);
   3342   Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
   3343   Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
   3344   Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
   3345   Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
   3346   Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
   3347   Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
   3348   Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
   3349 
   3350   // Give --sysroot= preference, over the Apple specific behavior to also use
   3351   // --isysroot as the syslibroot.
   3352   if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) {
   3353     CmdArgs.push_back("-syslibroot");
   3354     CmdArgs.push_back(A->getValue(Args));
   3355   } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
   3356     CmdArgs.push_back("-syslibroot");
   3357     CmdArgs.push_back(A->getValue(Args));
   3358   } else if (getDarwinToolChain().isTargetIPhoneOS()) {
   3359     CmdArgs.push_back("-syslibroot");
   3360     CmdArgs.push_back("/Developer/SDKs/Extra");
   3361   }
   3362 
   3363   Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
   3364   Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
   3365   Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
   3366   Args.AddAllArgs(CmdArgs, options::OPT_undefined);
   3367   Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
   3368   Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
   3369   Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
   3370   Args.AddAllArgs(CmdArgs, options::OPT_y);
   3371   Args.AddLastArg(CmdArgs, options::OPT_w);
   3372   Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
   3373   Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
   3374   Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
   3375   Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
   3376   Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
   3377   Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
   3378   Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
   3379   Args.AddLastArg(CmdArgs, options::OPT_whyload);
   3380   Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
   3381   Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
   3382   Args.AddLastArg(CmdArgs, options::OPT_dylinker);
   3383   Args.AddLastArg(CmdArgs, options::OPT_Mach);
   3384 }
   3385 
   3386 void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
   3387                                 const InputInfo &Output,
   3388                                 const InputInfoList &Inputs,
   3389                                 const ArgList &Args,
   3390                                 const char *LinkingOutput) const {
   3391   assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
   3392 
   3393   // The logic here is derived from gcc's behavior; most of which
   3394   // comes from specs (starting with link_command). Consult gcc for
   3395   // more information.
   3396   ArgStringList CmdArgs;
   3397 
   3398   /// Hack(tm) to ignore linking errors when we are doing ARC migration.
   3399   if (Args.hasArg(options::OPT_ccc_arcmt_check,
   3400                   options::OPT_ccc_arcmt_migrate)) {
   3401     for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
   3402       (*I)->claim();
   3403     const char *Exec =
   3404       Args.MakeArgString(getToolChain().GetProgramPath("touch"));
   3405     CmdArgs.push_back(Output.getFilename());
   3406     C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3407     return;
   3408   }
   3409 
   3410   // I'm not sure why this particular decomposition exists in gcc, but
   3411   // we follow suite for ease of comparison.
   3412   AddLinkArgs(C, Args, CmdArgs);
   3413 
   3414   Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
   3415   Args.AddAllArgs(CmdArgs, options::OPT_s);
   3416   Args.AddAllArgs(CmdArgs, options::OPT_t);
   3417   Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
   3418   Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
   3419   Args.AddAllArgs(CmdArgs, options::OPT_A);
   3420   Args.AddLastArg(CmdArgs, options::OPT_e);
   3421   Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
   3422   Args.AddAllArgs(CmdArgs, options::OPT_r);
   3423 
   3424   // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
   3425   // members of static archive libraries which implement Objective-C classes or
   3426   // categories.
   3427   if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
   3428     CmdArgs.push_back("-ObjC");
   3429 
   3430   CmdArgs.push_back("-o");
   3431   CmdArgs.push_back(Output.getFilename());
   3432 
   3433   if (!Args.hasArg(options::OPT_A) &&
   3434       !Args.hasArg(options::OPT_nostdlib) &&
   3435       !Args.hasArg(options::OPT_nostartfiles)) {
   3436     // Derived from startfile spec.
   3437     if (Args.hasArg(options::OPT_dynamiclib)) {
   3438       // Derived from darwin_dylib1 spec.
   3439       if (getDarwinToolChain().isTargetIOSSimulator()) {
   3440         // The simulator doesn't have a versioned crt1 file.
   3441         CmdArgs.push_back("-ldylib1.o");
   3442       } else if (getDarwinToolChain().isTargetIPhoneOS()) {
   3443         if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
   3444           CmdArgs.push_back("-ldylib1.o");
   3445       } else {
   3446         if (getDarwinToolChain().isMacosxVersionLT(10, 5))
   3447           CmdArgs.push_back("-ldylib1.o");
   3448         else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
   3449           CmdArgs.push_back("-ldylib1.10.5.o");
   3450       }
   3451     } else {
   3452       if (Args.hasArg(options::OPT_bundle)) {
   3453         if (!Args.hasArg(options::OPT_static)) {
   3454           // Derived from darwin_bundle1 spec.
   3455           if (getDarwinToolChain().isTargetIOSSimulator()) {
   3456             // The simulator doesn't have a versioned crt1 file.
   3457             CmdArgs.push_back("-lbundle1.o");
   3458           } else if (getDarwinToolChain().isTargetIPhoneOS()) {
   3459             if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
   3460               CmdArgs.push_back("-lbundle1.o");
   3461           } else {
   3462             if (getDarwinToolChain().isMacosxVersionLT(10, 6))
   3463               CmdArgs.push_back("-lbundle1.o");
   3464           }
   3465         }
   3466       } else {
   3467         if (Args.hasArg(options::OPT_pg) &&
   3468             getToolChain().SupportsProfiling()) {
   3469           if (Args.hasArg(options::OPT_static) ||
   3470               Args.hasArg(options::OPT_object) ||
   3471               Args.hasArg(options::OPT_preload)) {
   3472             CmdArgs.push_back("-lgcrt0.o");
   3473           } else {
   3474             CmdArgs.push_back("-lgcrt1.o");
   3475 
   3476             // darwin_crt2 spec is empty.
   3477           }
   3478         } else {
   3479           if (Args.hasArg(options::OPT_static) ||
   3480               Args.hasArg(options::OPT_object) ||
   3481               Args.hasArg(options::OPT_preload)) {
   3482             CmdArgs.push_back("-lcrt0.o");
   3483           } else {
   3484             // Derived from darwin_crt1 spec.
   3485             if (getDarwinToolChain().isTargetIOSSimulator()) {
   3486               // The simulator doesn't have a versioned crt1 file.
   3487               CmdArgs.push_back("-lcrt1.o");
   3488             } else if (getDarwinToolChain().isTargetIPhoneOS()) {
   3489               if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
   3490                 CmdArgs.push_back("-lcrt1.o");
   3491               else
   3492                 CmdArgs.push_back("-lcrt1.3.1.o");
   3493             } else {
   3494               if (getDarwinToolChain().isMacosxVersionLT(10, 5))
   3495                 CmdArgs.push_back("-lcrt1.o");
   3496               else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
   3497                 CmdArgs.push_back("-lcrt1.10.5.o");
   3498               else
   3499                 CmdArgs.push_back("-lcrt1.10.6.o");
   3500 
   3501               // darwin_crt2 spec is empty.
   3502             }
   3503           }
   3504         }
   3505       }
   3506     }
   3507 
   3508     if (!getDarwinToolChain().isTargetIPhoneOS() &&
   3509         Args.hasArg(options::OPT_shared_libgcc) &&
   3510         getDarwinToolChain().isMacosxVersionLT(10, 5)) {
   3511       const char *Str =
   3512         Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
   3513       CmdArgs.push_back(Str);
   3514     }
   3515   }
   3516 
   3517   Args.AddAllArgs(CmdArgs, options::OPT_L);
   3518 
   3519   if (Args.hasArg(options::OPT_fopenmp))
   3520     // This is more complicated in gcc...
   3521     CmdArgs.push_back("-lgomp");
   3522 
   3523   getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
   3524 
   3525   // In ARC, if we don't have runtime support, link in the runtime
   3526   // stubs.  We have to do this *before* adding any of the normal
   3527   // linker inputs so that its initializer gets run first.
   3528   if (isObjCAutoRefCount(Args)) {
   3529     ObjCRuntime runtime;
   3530     getDarwinToolChain().configureObjCRuntime(runtime);
   3531     if (!runtime.HasARC)
   3532       getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
   3533   }
   3534 
   3535   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   3536 
   3537   if (LinkingOutput) {
   3538     CmdArgs.push_back("-arch_multiple");
   3539     CmdArgs.push_back("-final_output");
   3540     CmdArgs.push_back(LinkingOutput);
   3541   }
   3542 
   3543   if (Args.hasArg(options::OPT_fnested_functions))
   3544     CmdArgs.push_back("-allow_stack_execute");
   3545 
   3546   if (!Args.hasArg(options::OPT_nostdlib) &&
   3547       !Args.hasArg(options::OPT_nodefaultlibs)) {
   3548     if (getToolChain().getDriver().CCCIsCXX)
   3549       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
   3550 
   3551     // link_ssp spec is empty.
   3552 
   3553     // Let the tool chain choose which runtime library to link.
   3554     getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
   3555   }
   3556 
   3557   if (!Args.hasArg(options::OPT_A) &&
   3558       !Args.hasArg(options::OPT_nostdlib) &&
   3559       !Args.hasArg(options::OPT_nostartfiles)) {
   3560     // endfile_spec is empty.
   3561   }
   3562 
   3563   addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
   3564 
   3565   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
   3566   Args.AddAllArgs(CmdArgs, options::OPT_F);
   3567 
   3568   const char *Exec =
   3569     Args.MakeArgString(getToolChain().GetProgramPath("ld"));
   3570   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3571 }
   3572 
   3573 void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
   3574                                 const InputInfo &Output,
   3575                                 const InputInfoList &Inputs,
   3576                                 const ArgList &Args,
   3577                                 const char *LinkingOutput) const {
   3578   ArgStringList CmdArgs;
   3579 
   3580   CmdArgs.push_back("-create");
   3581   assert(Output.isFilename() && "Unexpected lipo output.");
   3582 
   3583   CmdArgs.push_back("-output");
   3584   CmdArgs.push_back(Output.getFilename());
   3585 
   3586   for (InputInfoList::const_iterator
   3587          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   3588     const InputInfo &II = *it;
   3589     assert(II.isFilename() && "Unexpected lipo input.");
   3590     CmdArgs.push_back(II.getFilename());
   3591   }
   3592   const char *Exec =
   3593     Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
   3594   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3595 }
   3596 
   3597 void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
   3598                                     const InputInfo &Output,
   3599                                     const InputInfoList &Inputs,
   3600                                     const ArgList &Args,
   3601                                     const char *LinkingOutput) const {
   3602   ArgStringList CmdArgs;
   3603 
   3604   CmdArgs.push_back("-o");
   3605   CmdArgs.push_back(Output.getFilename());
   3606 
   3607   assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
   3608   const InputInfo &Input = Inputs[0];
   3609   assert(Input.isFilename() && "Unexpected dsymutil input.");
   3610   CmdArgs.push_back(Input.getFilename());
   3611 
   3612   const char *Exec =
   3613     Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
   3614   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3615 }
   3616 
   3617 void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
   3618 				       const InputInfo &Output,
   3619 				       const InputInfoList &Inputs,
   3620 				       const ArgList &Args,
   3621 				       const char *LinkingOutput) const {
   3622   ArgStringList CmdArgs;
   3623   CmdArgs.push_back("--verify");
   3624 
   3625   assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
   3626   const InputInfo &Input = Inputs[0];
   3627   assert(Input.isFilename() && "Unexpected verify input");
   3628 
   3629   // Grabbing the output of the earlier dsymutil run.
   3630   CmdArgs.push_back(Input.getFilename());
   3631 
   3632   const char *Exec =
   3633     Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
   3634   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3635 }
   3636 
   3637 void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   3638                                       const InputInfo &Output,
   3639                                       const InputInfoList &Inputs,
   3640                                       const ArgList &Args,
   3641                                       const char *LinkingOutput) const {
   3642   ArgStringList CmdArgs;
   3643 
   3644   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   3645                        options::OPT_Xassembler);
   3646 
   3647   CmdArgs.push_back("-o");
   3648   CmdArgs.push_back(Output.getFilename());
   3649 
   3650   for (InputInfoList::const_iterator
   3651          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   3652     const InputInfo &II = *it;
   3653     CmdArgs.push_back(II.getFilename());
   3654   }
   3655 
   3656   const char *Exec =
   3657     Args.MakeArgString(getToolChain().GetProgramPath("gas"));
   3658   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3659 }
   3660 
   3661 void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
   3662                                   const InputInfo &Output,
   3663                                   const InputInfoList &Inputs,
   3664                                   const ArgList &Args,
   3665                                   const char *LinkingOutput) const {
   3666   ArgStringList CmdArgs;
   3667 
   3668   if ((!Args.hasArg(options::OPT_nostdlib)) &&
   3669       (!Args.hasArg(options::OPT_shared))) {
   3670     CmdArgs.push_back("-e");
   3671     CmdArgs.push_back("_start");
   3672   }
   3673 
   3674   if (Args.hasArg(options::OPT_static)) {
   3675     CmdArgs.push_back("-Bstatic");
   3676     CmdArgs.push_back("-dn");
   3677   } else {
   3678 //    CmdArgs.push_back("--eh-frame-hdr");
   3679     CmdArgs.push_back("-Bdynamic");
   3680     if (Args.hasArg(options::OPT_shared)) {
   3681       CmdArgs.push_back("-shared");
   3682     } else {
   3683       CmdArgs.push_back("--dynamic-linker");
   3684       CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
   3685     }
   3686   }
   3687 
   3688   if (Output.isFilename()) {
   3689     CmdArgs.push_back("-o");
   3690     CmdArgs.push_back(Output.getFilename());
   3691   } else {
   3692     assert(Output.isNothing() && "Invalid output.");
   3693   }
   3694 
   3695   if (!Args.hasArg(options::OPT_nostdlib) &&
   3696       !Args.hasArg(options::OPT_nostartfiles)) {
   3697     if (!Args.hasArg(options::OPT_shared)) {
   3698       CmdArgs.push_back(Args.MakeArgString(
   3699                                 getToolChain().GetFilePath("crt1.o")));
   3700       CmdArgs.push_back(Args.MakeArgString(
   3701                                 getToolChain().GetFilePath("crti.o")));
   3702       CmdArgs.push_back(Args.MakeArgString(
   3703                                 getToolChain().GetFilePath("crtbegin.o")));
   3704     } else {
   3705       CmdArgs.push_back(Args.MakeArgString(
   3706                                 getToolChain().GetFilePath("crti.o")));
   3707     }
   3708     CmdArgs.push_back(Args.MakeArgString(
   3709                                 getToolChain().GetFilePath("crtn.o")));
   3710   }
   3711 
   3712   CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
   3713                                        + getToolChain().getTripleString()
   3714                                        + "/4.2.4"));
   3715 
   3716   Args.AddAllArgs(CmdArgs, options::OPT_L);
   3717   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
   3718   Args.AddAllArgs(CmdArgs, options::OPT_e);
   3719 
   3720   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   3721 
   3722   if (!Args.hasArg(options::OPT_nostdlib) &&
   3723       !Args.hasArg(options::OPT_nodefaultlibs)) {
   3724     // FIXME: For some reason GCC passes -lgcc before adding
   3725     // the default system libraries. Just mimic this for now.
   3726     CmdArgs.push_back("-lgcc");
   3727 
   3728     if (Args.hasArg(options::OPT_pthread))
   3729       CmdArgs.push_back("-pthread");
   3730     if (!Args.hasArg(options::OPT_shared))
   3731       CmdArgs.push_back("-lc");
   3732     CmdArgs.push_back("-lgcc");
   3733   }
   3734 
   3735   if (!Args.hasArg(options::OPT_nostdlib) &&
   3736       !Args.hasArg(options::OPT_nostartfiles)) {
   3737     if (!Args.hasArg(options::OPT_shared))
   3738       CmdArgs.push_back(Args.MakeArgString(
   3739                                 getToolChain().GetFilePath("crtend.o")));
   3740   }
   3741 
   3742   addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
   3743 
   3744   const char *Exec =
   3745     Args.MakeArgString(getToolChain().GetProgramPath("ld"));
   3746   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3747 }
   3748 
   3749 void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   3750                                      const InputInfo &Output,
   3751                                      const InputInfoList &Inputs,
   3752                                      const ArgList &Args,
   3753                                      const char *LinkingOutput) const {
   3754   ArgStringList CmdArgs;
   3755 
   3756   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   3757                        options::OPT_Xassembler);
   3758 
   3759   CmdArgs.push_back("-o");
   3760   CmdArgs.push_back(Output.getFilename());
   3761 
   3762   for (InputInfoList::const_iterator
   3763          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   3764     const InputInfo &II = *it;
   3765     CmdArgs.push_back(II.getFilename());
   3766   }
   3767 
   3768   const char *Exec =
   3769     Args.MakeArgString(getToolChain().GetProgramPath("as"));
   3770   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3771 }
   3772 
   3773 void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
   3774                                  const InputInfo &Output,
   3775                                  const InputInfoList &Inputs,
   3776                                  const ArgList &Args,
   3777                                  const char *LinkingOutput) const {
   3778   const Driver &D = getToolChain().getDriver();
   3779   ArgStringList CmdArgs;
   3780 
   3781   if ((!Args.hasArg(options::OPT_nostdlib)) &&
   3782       (!Args.hasArg(options::OPT_shared))) {
   3783     CmdArgs.push_back("-e");
   3784     CmdArgs.push_back("__start");
   3785   }
   3786 
   3787   if (Args.hasArg(options::OPT_static)) {
   3788     CmdArgs.push_back("-Bstatic");
   3789   } else {
   3790     if (Args.hasArg(options::OPT_rdynamic))
   3791       CmdArgs.push_back("-export-dynamic");
   3792     CmdArgs.push_back("--eh-frame-hdr");
   3793     CmdArgs.push_back("-Bdynamic");
   3794     if (Args.hasArg(options::OPT_shared)) {
   3795       CmdArgs.push_back("-shared");
   3796     } else {
   3797       CmdArgs.push_back("-dynamic-linker");
   3798       CmdArgs.push_back("/usr/libexec/ld.so");
   3799     }
   3800   }
   3801 
   3802   if (Output.isFilename()) {
   3803     CmdArgs.push_back("-o");
   3804     CmdArgs.push_back(Output.getFilename());
   3805   } else {
   3806     assert(Output.isNothing() && "Invalid output.");
   3807   }
   3808 
   3809   if (!Args.hasArg(options::OPT_nostdlib) &&
   3810       !Args.hasArg(options::OPT_nostartfiles)) {
   3811     if (!Args.hasArg(options::OPT_shared)) {
   3812       CmdArgs.push_back(Args.MakeArgString(
   3813                               getToolChain().GetFilePath("crt0.o")));
   3814       CmdArgs.push_back(Args.MakeArgString(
   3815                               getToolChain().GetFilePath("crtbegin.o")));
   3816     } else {
   3817       CmdArgs.push_back(Args.MakeArgString(
   3818                               getToolChain().GetFilePath("crtbeginS.o")));
   3819     }
   3820   }
   3821 
   3822   std::string Triple = getToolChain().getTripleString();
   3823   if (Triple.substr(0, 6) == "x86_64")
   3824     Triple.replace(0, 6, "amd64");
   3825   CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
   3826                                        "/4.2.1"));
   3827 
   3828   Args.AddAllArgs(CmdArgs, options::OPT_L);
   3829   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
   3830   Args.AddAllArgs(CmdArgs, options::OPT_e);
   3831 
   3832   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   3833 
   3834   if (!Args.hasArg(options::OPT_nostdlib) &&
   3835       !Args.hasArg(options::OPT_nodefaultlibs)) {
   3836     if (D.CCCIsCXX) {
   3837       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
   3838       CmdArgs.push_back("-lm");
   3839     }
   3840 
   3841     // FIXME: For some reason GCC passes -lgcc before adding
   3842     // the default system libraries. Just mimic this for now.
   3843     CmdArgs.push_back("-lgcc");
   3844 
   3845     if (Args.hasArg(options::OPT_pthread))
   3846       CmdArgs.push_back("-lpthread");
   3847     if (!Args.hasArg(options::OPT_shared))
   3848       CmdArgs.push_back("-lc");
   3849     CmdArgs.push_back("-lgcc");
   3850   }
   3851 
   3852   if (!Args.hasArg(options::OPT_nostdlib) &&
   3853       !Args.hasArg(options::OPT_nostartfiles)) {
   3854     if (!Args.hasArg(options::OPT_shared))
   3855       CmdArgs.push_back(Args.MakeArgString(
   3856                               getToolChain().GetFilePath("crtend.o")));
   3857     else
   3858       CmdArgs.push_back(Args.MakeArgString(
   3859                               getToolChain().GetFilePath("crtendS.o")));
   3860   }
   3861 
   3862   const char *Exec =
   3863     Args.MakeArgString(getToolChain().GetProgramPath("ld"));
   3864   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3865 }
   3866 
   3867 void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   3868                                      const InputInfo &Output,
   3869                                      const InputInfoList &Inputs,
   3870                                      const ArgList &Args,
   3871                                      const char *LinkingOutput) const {
   3872   ArgStringList CmdArgs;
   3873 
   3874   // When building 32-bit code on FreeBSD/amd64, we have to explicitly
   3875   // instruct as in the base system to assemble 32-bit code.
   3876   if (getToolChain().getArchName() == "i386")
   3877     CmdArgs.push_back("--32");
   3878 
   3879   if (getToolChain().getArchName() == "powerpc")
   3880     CmdArgs.push_back("-a32");
   3881 
   3882   // Set byte order explicitly
   3883   if (getToolChain().getArchName() == "mips")
   3884     CmdArgs.push_back("-EB");
   3885   else if (getToolChain().getArchName() == "mipsel")
   3886     CmdArgs.push_back("-EL");
   3887 
   3888   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   3889                        options::OPT_Xassembler);
   3890 
   3891   CmdArgs.push_back("-o");
   3892   CmdArgs.push_back(Output.getFilename());
   3893 
   3894   for (InputInfoList::const_iterator
   3895          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   3896     const InputInfo &II = *it;
   3897     CmdArgs.push_back(II.getFilename());
   3898   }
   3899 
   3900   const char *Exec =
   3901     Args.MakeArgString(getToolChain().GetProgramPath("as"));
   3902   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   3903 }
   3904 
   3905 void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
   3906                                  const InputInfo &Output,
   3907                                  const InputInfoList &Inputs,
   3908                                  const ArgList &Args,
   3909                                  const char *LinkingOutput) const {
   3910   const Driver &D = getToolChain().getDriver();
   3911   ArgStringList CmdArgs;
   3912 
   3913   if (!D.SysRoot.empty())
   3914     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
   3915 
   3916   if (Args.hasArg(options::OPT_static)) {
   3917     CmdArgs.push_back("-Bstatic");
   3918   } else {
   3919     if (Args.hasArg(options::OPT_rdynamic))
   3920       CmdArgs.push_back("-export-dynamic");
   3921     CmdArgs.push_back("--eh-frame-hdr");
   3922     if (Args.hasArg(options::OPT_shared)) {
   3923       CmdArgs.push_back("-Bshareable");
   3924     } else {
   3925       CmdArgs.push_back("-dynamic-linker");
   3926       CmdArgs.push_back("/libexec/ld-elf.so.1");
   3927     }
   3928   }
   3929 
   3930   // When building 32-bit code on FreeBSD/amd64, we have to explicitly
   3931   // instruct ld in the base system to link 32-bit code.
   3932   if (getToolChain().getArchName() == "i386") {
   3933     CmdArgs.push_back("-m");
   3934     CmdArgs.push_back("elf_i386_fbsd");
   3935   }
   3936 
   3937   if (getToolChain().getArchName() == "powerpc") {
   3938     CmdArgs.push_back("-m");
   3939     CmdArgs.push_back("elf32ppc");
   3940   }
   3941 
   3942   if (Output.isFilename()) {
   3943     CmdArgs.push_back("-o");
   3944     CmdArgs.push_back(Output.getFilename());
   3945   } else {
   3946     assert(Output.isNothing() && "Invalid output.");
   3947   }
   3948 
   3949   if (!Args.hasArg(options::OPT_nostdlib) &&
   3950       !Args.hasArg(options::OPT_nostartfiles)) {
   3951     if (!Args.hasArg(options::OPT_shared)) {
   3952       if (Args.hasArg(options::OPT_pg))
   3953         CmdArgs.push_back(Args.MakeArgString(
   3954                                 getToolChain().GetFilePath("gcrt1.o")));
   3955       else {
   3956         const char *crt = Args.hasArg(options::OPT_pie) ? "Scrt1.o" : "crt1.o";
   3957         CmdArgs.push_back(Args.MakeArgString(
   3958                                 getToolChain().GetFilePath(crt)));
   3959       }
   3960       CmdArgs.push_back(Args.MakeArgString(
   3961                               getToolChain().GetFilePath("crti.o")));
   3962       CmdArgs.push_back(Args.MakeArgString(
   3963                               getToolChain().GetFilePath("crtbegin.o")));
   3964     } else {
   3965       CmdArgs.push_back(Args.MakeArgString(
   3966                               getToolChain().GetFilePath("crti.o")));
   3967       CmdArgs.push_back(Args.MakeArgString(
   3968                               getToolChain().GetFilePath("crtbeginS.o")));
   3969     }
   3970   }
   3971 
   3972   Args.AddAllArgs(CmdArgs, options::OPT_L);
   3973   const ToolChain::path_list Paths = getToolChain().getFilePaths();
   3974   for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
   3975        i != e; ++i)
   3976     CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
   3977   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
   3978   Args.AddAllArgs(CmdArgs, options::OPT_e);
   3979   Args.AddAllArgs(CmdArgs, options::OPT_s);
   3980   Args.AddAllArgs(CmdArgs, options::OPT_t);
   3981   Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
   3982   Args.AddAllArgs(CmdArgs, options::OPT_r);
   3983 
   3984   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   3985 
   3986   if (!Args.hasArg(options::OPT_nostdlib) &&
   3987       !Args.hasArg(options::OPT_nodefaultlibs)) {
   3988     if (D.CCCIsCXX) {
   3989       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
   3990       if (Args.hasArg(options::OPT_pg))
   3991         CmdArgs.push_back("-lm_p");
   3992       else
   3993         CmdArgs.push_back("-lm");
   3994     }
   3995     // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
   3996     // the default system libraries. Just mimic this for now.
   3997     if (Args.hasArg(options::OPT_pg))
   3998       CmdArgs.push_back("-lgcc_p");
   3999     else
   4000       CmdArgs.push_back("-lgcc");
   4001     if (Args.hasArg(options::OPT_static)) {
   4002       CmdArgs.push_back("-lgcc_eh");
   4003     } else if (Args.hasArg(options::OPT_pg)) {
   4004       CmdArgs.push_back("-lgcc_eh_p");
   4005     } else {
   4006       CmdArgs.push_back("--as-needed");
   4007       CmdArgs.push_back("-lgcc_s");
   4008       CmdArgs.push_back("--no-as-needed");
   4009     }
   4010 
   4011     if (Args.hasArg(options::OPT_pthread)) {
   4012       if (Args.hasArg(options::OPT_pg))
   4013         CmdArgs.push_back("-lpthread_p");
   4014       else
   4015         CmdArgs.push_back("-lpthread");
   4016     }
   4017 
   4018     if (Args.hasArg(options::OPT_pg)) {
   4019       if (Args.hasArg(options::OPT_shared))
   4020         CmdArgs.push_back("-lc");
   4021       else
   4022         CmdArgs.push_back("-lc_p");
   4023       CmdArgs.push_back("-lgcc_p");
   4024     } else {
   4025       CmdArgs.push_back("-lc");
   4026       CmdArgs.push_back("-lgcc");
   4027     }
   4028 
   4029     if (Args.hasArg(options::OPT_static)) {
   4030       CmdArgs.push_back("-lgcc_eh");
   4031     } else if (Args.hasArg(options::OPT_pg)) {
   4032       CmdArgs.push_back("-lgcc_eh_p");
   4033     } else {
   4034       CmdArgs.push_back("--as-needed");
   4035       CmdArgs.push_back("-lgcc_s");
   4036       CmdArgs.push_back("--no-as-needed");
   4037     }
   4038   }
   4039 
   4040   if (!Args.hasArg(options::OPT_nostdlib) &&
   4041       !Args.hasArg(options::OPT_nostartfiles)) {
   4042     if (!Args.hasArg(options::OPT_shared))
   4043       CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4044                                                                   "crtend.o")));
   4045     else
   4046       CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4047                                                                  "crtendS.o")));
   4048     CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4049                                                                     "crtn.o")));
   4050   }
   4051 
   4052   addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
   4053 
   4054   const char *Exec =
   4055     Args.MakeArgString(getToolChain().GetProgramPath("ld"));
   4056   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4057 }
   4058 
   4059 void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   4060                                      const InputInfo &Output,
   4061                                      const InputInfoList &Inputs,
   4062                                      const ArgList &Args,
   4063                                      const char *LinkingOutput) const {
   4064   ArgStringList CmdArgs;
   4065 
   4066   // When building 32-bit code on NetBSD/amd64, we have to explicitly
   4067   // instruct as in the base system to assemble 32-bit code.
   4068   if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
   4069       getToolChain().getArch() == llvm::Triple::x86)
   4070     CmdArgs.push_back("--32");
   4071 
   4072 
   4073   // Set byte order explicitly
   4074   if (getToolChain().getArchName() == "mips")
   4075     CmdArgs.push_back("-EB");
   4076   else if (getToolChain().getArchName() == "mipsel")
   4077     CmdArgs.push_back("-EL");
   4078 
   4079   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   4080                        options::OPT_Xassembler);
   4081 
   4082   CmdArgs.push_back("-o");
   4083   CmdArgs.push_back(Output.getFilename());
   4084 
   4085   for (InputInfoList::const_iterator
   4086          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   4087     const InputInfo &II = *it;
   4088     CmdArgs.push_back(II.getFilename());
   4089   }
   4090 
   4091   const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
   4092   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4093 }
   4094 
   4095 void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
   4096                                  const InputInfo &Output,
   4097                                  const InputInfoList &Inputs,
   4098                                  const ArgList &Args,
   4099                                  const char *LinkingOutput) const {
   4100   const Driver &D = getToolChain().getDriver();
   4101   ArgStringList CmdArgs;
   4102 
   4103   if (!D.SysRoot.empty())
   4104     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
   4105 
   4106   if (Args.hasArg(options::OPT_static)) {
   4107     CmdArgs.push_back("-Bstatic");
   4108   } else {
   4109     if (Args.hasArg(options::OPT_rdynamic))
   4110       CmdArgs.push_back("-export-dynamic");
   4111     CmdArgs.push_back("--eh-frame-hdr");
   4112     if (Args.hasArg(options::OPT_shared)) {
   4113       CmdArgs.push_back("-Bshareable");
   4114     } else {
   4115       CmdArgs.push_back("-dynamic-linker");
   4116       CmdArgs.push_back("/libexec/ld.elf_so");
   4117     }
   4118   }
   4119 
   4120   // When building 32-bit code on NetBSD/amd64, we have to explicitly
   4121   // instruct ld in the base system to link 32-bit code.
   4122   if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
   4123       getToolChain().getArch() == llvm::Triple::x86) {
   4124     CmdArgs.push_back("-m");
   4125     CmdArgs.push_back("elf_i386");
   4126   }
   4127 
   4128   if (Output.isFilename()) {
   4129     CmdArgs.push_back("-o");
   4130     CmdArgs.push_back(Output.getFilename());
   4131   } else {
   4132     assert(Output.isNothing() && "Invalid output.");
   4133   }
   4134 
   4135   if (!Args.hasArg(options::OPT_nostdlib) &&
   4136       !Args.hasArg(options::OPT_nostartfiles)) {
   4137     if (!Args.hasArg(options::OPT_shared)) {
   4138       CmdArgs.push_back(Args.MakeArgString(
   4139                               getToolChain().GetFilePath("crt0.o")));
   4140       CmdArgs.push_back(Args.MakeArgString(
   4141                               getToolChain().GetFilePath("crti.o")));
   4142       CmdArgs.push_back(Args.MakeArgString(
   4143                               getToolChain().GetFilePath("crtbegin.o")));
   4144     } else {
   4145       CmdArgs.push_back(Args.MakeArgString(
   4146                               getToolChain().GetFilePath("crti.o")));
   4147       CmdArgs.push_back(Args.MakeArgString(
   4148                               getToolChain().GetFilePath("crtbeginS.o")));
   4149     }
   4150   }
   4151 
   4152   Args.AddAllArgs(CmdArgs, options::OPT_L);
   4153   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
   4154   Args.AddAllArgs(CmdArgs, options::OPT_e);
   4155   Args.AddAllArgs(CmdArgs, options::OPT_s);
   4156   Args.AddAllArgs(CmdArgs, options::OPT_t);
   4157   Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
   4158   Args.AddAllArgs(CmdArgs, options::OPT_r);
   4159 
   4160   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   4161 
   4162   if (!Args.hasArg(options::OPT_nostdlib) &&
   4163       !Args.hasArg(options::OPT_nodefaultlibs)) {
   4164     if (D.CCCIsCXX) {
   4165       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
   4166       CmdArgs.push_back("-lm");
   4167     }
   4168     // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
   4169     // the default system libraries. Just mimic this for now.
   4170     if (Args.hasArg(options::OPT_static)) {
   4171       CmdArgs.push_back("-lgcc_eh");
   4172     } else {
   4173       CmdArgs.push_back("--as-needed");
   4174       CmdArgs.push_back("-lgcc_s");
   4175       CmdArgs.push_back("--no-as-needed");
   4176     }
   4177     CmdArgs.push_back("-lgcc");
   4178 
   4179     if (Args.hasArg(options::OPT_pthread))
   4180       CmdArgs.push_back("-lpthread");
   4181     CmdArgs.push_back("-lc");
   4182 
   4183     CmdArgs.push_back("-lgcc");
   4184     if (Args.hasArg(options::OPT_static)) {
   4185       CmdArgs.push_back("-lgcc_eh");
   4186     } else {
   4187       CmdArgs.push_back("--as-needed");
   4188       CmdArgs.push_back("-lgcc_s");
   4189       CmdArgs.push_back("--no-as-needed");
   4190     }
   4191   }
   4192 
   4193   if (!Args.hasArg(options::OPT_nostdlib) &&
   4194       !Args.hasArg(options::OPT_nostartfiles)) {
   4195     if (!Args.hasArg(options::OPT_shared))
   4196       CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4197                                                                   "crtend.o")));
   4198     else
   4199       CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4200                                                                  "crtendS.o")));
   4201     CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4202                                                                     "crtn.o")));
   4203   }
   4204 
   4205   addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
   4206 
   4207   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));
   4208   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4209 }
   4210 
   4211 void linuxtools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   4212                                         const InputInfo &Output,
   4213                                         const InputInfoList &Inputs,
   4214                                         const ArgList &Args,
   4215                                         const char *LinkingOutput) const {
   4216   ArgStringList CmdArgs;
   4217 
   4218   // Add --32/--64 to make sure we get the format we want.
   4219   // This is incomplete
   4220   if (getToolChain().getArch() == llvm::Triple::x86) {
   4221     CmdArgs.push_back("--32");
   4222   } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
   4223     CmdArgs.push_back("--64");
   4224   } else if (getToolChain().getArch() == llvm::Triple::arm) {
   4225     StringRef MArch = getToolChain().getArchName();
   4226     if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
   4227       CmdArgs.push_back("-mfpu=neon");
   4228   }
   4229 
   4230   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   4231                        options::OPT_Xassembler);
   4232 
   4233   CmdArgs.push_back("-o");
   4234   CmdArgs.push_back(Output.getFilename());
   4235 
   4236   for (InputInfoList::const_iterator
   4237          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   4238     const InputInfo &II = *it;
   4239     CmdArgs.push_back(II.getFilename());
   4240   }
   4241 
   4242   const char *Exec =
   4243     Args.MakeArgString(getToolChain().GetProgramPath("as"));
   4244   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4245 }
   4246 
   4247 static void AddLibgcc(const Driver &D, ArgStringList &CmdArgs,
   4248                       const ArgList &Args) {
   4249   bool StaticLibgcc = Args.hasArg(options::OPT_static) ||
   4250     Args.hasArg(options::OPT_static_libgcc);
   4251   if (!D.CCCIsCXX)
   4252     CmdArgs.push_back("-lgcc");
   4253 
   4254   if (StaticLibgcc) {
   4255     if (D.CCCIsCXX)
   4256       CmdArgs.push_back("-lgcc");
   4257   } else {
   4258     if (!D.CCCIsCXX)
   4259       CmdArgs.push_back("--as-needed");
   4260     CmdArgs.push_back("-lgcc_s");
   4261     if (!D.CCCIsCXX)
   4262       CmdArgs.push_back("--no-as-needed");
   4263   }
   4264 
   4265   if (StaticLibgcc)
   4266     CmdArgs.push_back("-lgcc_eh");
   4267   else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX)
   4268     CmdArgs.push_back("-lgcc");
   4269 }
   4270 
   4271 void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
   4272                                     const InputInfo &Output,
   4273                                     const InputInfoList &Inputs,
   4274                                     const ArgList &Args,
   4275                                     const char *LinkingOutput) const {
   4276   const toolchains::Linux& ToolChain =
   4277     static_cast<const toolchains::Linux&>(getToolChain());
   4278   const Driver &D = ToolChain.getDriver();
   4279   ArgStringList CmdArgs;
   4280 
   4281   // Silence warning for "clang -g foo.o -o foo"
   4282   Args.ClaimAllArgs(options::OPT_g_Group);
   4283   // and "clang -emit-llvm foo.o -o foo"
   4284   Args.ClaimAllArgs(options::OPT_emit_llvm);
   4285   // and for "clang -g foo.o -o foo". Other warning options are already
   4286   // handled somewhere else.
   4287   Args.ClaimAllArgs(options::OPT_w);
   4288 
   4289   if (!D.SysRoot.empty())
   4290     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
   4291 
   4292   if (Args.hasArg(options::OPT_pie))
   4293     CmdArgs.push_back("-pie");
   4294 
   4295   if (Args.hasArg(options::OPT_rdynamic))
   4296     CmdArgs.push_back("-export-dynamic");
   4297 
   4298   if (Args.hasArg(options::OPT_s))
   4299     CmdArgs.push_back("-s");
   4300 
   4301   for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
   4302          e = ToolChain.ExtraOpts.end();
   4303        i != e; ++i)
   4304     CmdArgs.push_back(i->c_str());
   4305 
   4306   if (!Args.hasArg(options::OPT_static)) {
   4307     CmdArgs.push_back("--eh-frame-hdr");
   4308   }
   4309 
   4310   CmdArgs.push_back("-m");
   4311   if (ToolChain.getArch() == llvm::Triple::x86)
   4312     CmdArgs.push_back("elf_i386");
   4313   else if (ToolChain.getArch() == llvm::Triple::arm
   4314            ||  ToolChain.getArch() == llvm::Triple::thumb)
   4315     CmdArgs.push_back("armelf_linux_eabi");
   4316   else if (ToolChain.getArch() == llvm::Triple::ppc)
   4317     CmdArgs.push_back("elf32ppclinux");
   4318   else if (ToolChain.getArch() == llvm::Triple::ppc64)
   4319     CmdArgs.push_back("elf64ppc");
   4320   else
   4321     CmdArgs.push_back("elf_x86_64");
   4322 
   4323   if (Args.hasArg(options::OPT_static)) {
   4324     if (ToolChain.getArch() == llvm::Triple::arm
   4325         || ToolChain.getArch() == llvm::Triple::thumb)
   4326       CmdArgs.push_back("-Bstatic");
   4327     else
   4328       CmdArgs.push_back("-static");
   4329   } else if (Args.hasArg(options::OPT_shared)) {
   4330     CmdArgs.push_back("-shared");
   4331   }
   4332 
   4333   if (ToolChain.getArch() == llvm::Triple::arm ||
   4334       ToolChain.getArch() == llvm::Triple::thumb ||
   4335       (!Args.hasArg(options::OPT_static) &&
   4336        !Args.hasArg(options::OPT_shared))) {
   4337     CmdArgs.push_back("-dynamic-linker");
   4338     if (ToolChain.getArch() == llvm::Triple::x86)
   4339       CmdArgs.push_back("/lib/ld-linux.so.2");
   4340     else if (ToolChain.getArch() == llvm::Triple::arm ||
   4341              ToolChain.getArch() == llvm::Triple::thumb)
   4342       CmdArgs.push_back("/lib/ld-linux.so.3");
   4343     else if (ToolChain.getArch() == llvm::Triple::ppc)
   4344       CmdArgs.push_back("/lib/ld.so.1");
   4345     else if (ToolChain.getArch() == llvm::Triple::ppc64)
   4346       CmdArgs.push_back("/lib64/ld64.so.1");
   4347     else
   4348       CmdArgs.push_back("/lib64/ld-linux-x86-64.so.2");
   4349   }
   4350 
   4351   CmdArgs.push_back("-o");
   4352   CmdArgs.push_back(Output.getFilename());
   4353 
   4354   if (!Args.hasArg(options::OPT_nostdlib) &&
   4355       !Args.hasArg(options::OPT_nostartfiles)) {
   4356     const char *crt1 = NULL;
   4357     if (!Args.hasArg(options::OPT_shared)){
   4358       if (Args.hasArg(options::OPT_pie))
   4359         crt1 = "Scrt1.o";
   4360       else
   4361         crt1 = "crt1.o";
   4362     }
   4363     if (crt1)
   4364       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
   4365 
   4366     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
   4367 
   4368     const char *crtbegin;
   4369     if (Args.hasArg(options::OPT_static))
   4370       crtbegin = "crtbeginT.o";
   4371     else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
   4372       crtbegin = "crtbeginS.o";
   4373     else
   4374       crtbegin = "crtbegin.o";
   4375     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
   4376   }
   4377 
   4378   Args.AddAllArgs(CmdArgs, options::OPT_L);
   4379 
   4380   const ToolChain::path_list Paths = ToolChain.getFilePaths();
   4381 
   4382   for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
   4383        i != e; ++i)
   4384     CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
   4385 
   4386   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
   4387 
   4388   if (D.CCCIsCXX && !Args.hasArg(options::OPT_nostdlib)) {
   4389     bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
   4390       !Args.hasArg(options::OPT_static);
   4391     if (OnlyLibstdcxxStatic)
   4392       CmdArgs.push_back("-Bstatic");
   4393     ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
   4394     if (OnlyLibstdcxxStatic)
   4395       CmdArgs.push_back("-Bdynamic");
   4396     CmdArgs.push_back("-lm");
   4397   }
   4398 
   4399   if (!Args.hasArg(options::OPT_nostdlib)) {
   4400     if (Args.hasArg(options::OPT_static))
   4401       CmdArgs.push_back("--start-group");
   4402 
   4403     AddLibgcc(D, CmdArgs, Args);
   4404 
   4405     if (Args.hasArg(options::OPT_pthread) ||
   4406         Args.hasArg(options::OPT_pthreads))
   4407       CmdArgs.push_back("-lpthread");
   4408 
   4409     CmdArgs.push_back("-lc");
   4410 
   4411     if (Args.hasArg(options::OPT_static))
   4412       CmdArgs.push_back("--end-group");
   4413     else
   4414       AddLibgcc(D, CmdArgs, Args);
   4415 
   4416 
   4417     if (!Args.hasArg(options::OPT_nostartfiles)) {
   4418       const char *crtend;
   4419       if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
   4420         crtend = "crtendS.o";
   4421       else
   4422         crtend = "crtend.o";
   4423 
   4424       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
   4425       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
   4426     }
   4427   }
   4428 
   4429   addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
   4430 
   4431   if (Args.hasArg(options::OPT_use_gold_plugin)) {
   4432     CmdArgs.push_back("-plugin");
   4433     std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
   4434     CmdArgs.push_back(Args.MakeArgString(Plugin));
   4435   }
   4436 
   4437   C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
   4438 }
   4439 
   4440 void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   4441                                    const InputInfo &Output,
   4442                                    const InputInfoList &Inputs,
   4443                                    const ArgList &Args,
   4444                                    const char *LinkingOutput) const {
   4445   ArgStringList CmdArgs;
   4446 
   4447   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   4448                        options::OPT_Xassembler);
   4449 
   4450   CmdArgs.push_back("-o");
   4451   CmdArgs.push_back(Output.getFilename());
   4452 
   4453   for (InputInfoList::const_iterator
   4454          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   4455     const InputInfo &II = *it;
   4456     CmdArgs.push_back(II.getFilename());
   4457   }
   4458 
   4459   const char *Exec =
   4460     Args.MakeArgString(getToolChain().GetProgramPath("gas"));
   4461   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4462 }
   4463 
   4464 void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
   4465                                const InputInfo &Output,
   4466                                const InputInfoList &Inputs,
   4467                                const ArgList &Args,
   4468                                const char *LinkingOutput) const {
   4469   const Driver &D = getToolChain().getDriver();
   4470   ArgStringList CmdArgs;
   4471 
   4472   if (Output.isFilename()) {
   4473     CmdArgs.push_back("-o");
   4474     CmdArgs.push_back(Output.getFilename());
   4475   } else {
   4476     assert(Output.isNothing() && "Invalid output.");
   4477   }
   4478 
   4479   if (!Args.hasArg(options::OPT_nostdlib) &&
   4480       !Args.hasArg(options::OPT_nostartfiles))
   4481     CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4482                                                       "/usr/gnu/lib/crtso.o")));
   4483 
   4484   Args.AddAllArgs(CmdArgs, options::OPT_L);
   4485   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
   4486   Args.AddAllArgs(CmdArgs, options::OPT_e);
   4487 
   4488   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   4489 
   4490   if (!Args.hasArg(options::OPT_nostdlib) &&
   4491       !Args.hasArg(options::OPT_nodefaultlibs)) {
   4492     if (D.CCCIsCXX) {
   4493       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
   4494       CmdArgs.push_back("-lm");
   4495     }
   4496 
   4497     if (Args.hasArg(options::OPT_pthread))
   4498       CmdArgs.push_back("-lpthread");
   4499     CmdArgs.push_back("-lc");
   4500     CmdArgs.push_back("-lgcc");
   4501     CmdArgs.push_back("-L/usr/gnu/lib");
   4502     // FIXME: fill in the correct search path for the final
   4503     // support libraries.
   4504     CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
   4505   }
   4506 
   4507   if (!Args.hasArg(options::OPT_nostdlib) &&
   4508       !Args.hasArg(options::OPT_nostartfiles)) {
   4509     CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
   4510                                               "/usr/gnu/lib/libend.a")));
   4511   }
   4512 
   4513   addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
   4514 
   4515   const char *Exec =
   4516     Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
   4517   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4518 }
   4519 
   4520 /// DragonFly Tools
   4521 
   4522 // For now, DragonFly Assemble does just about the same as for
   4523 // FreeBSD, but this may change soon.
   4524 void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   4525                                        const InputInfo &Output,
   4526                                        const InputInfoList &Inputs,
   4527                                        const ArgList &Args,
   4528                                        const char *LinkingOutput) const {
   4529   ArgStringList CmdArgs;
   4530 
   4531   // When building 32-bit code on DragonFly/pc64, we have to explicitly
   4532   // instruct as in the base system to assemble 32-bit code.
   4533   if (getToolChain().getArchName() == "i386")
   4534     CmdArgs.push_back("--32");
   4535 
   4536   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
   4537                        options::OPT_Xassembler);
   4538 
   4539   CmdArgs.push_back("-o");
   4540   CmdArgs.push_back(Output.getFilename());
   4541 
   4542   for (InputInfoList::const_iterator
   4543          it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
   4544     const InputInfo &II = *it;
   4545     CmdArgs.push_back(II.getFilename());
   4546   }
   4547 
   4548   const char *Exec =
   4549     Args.MakeArgString(getToolChain().GetProgramPath("as"));
   4550   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4551 }
   4552 
   4553 void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
   4554                                    const InputInfo &Output,
   4555                                    const InputInfoList &Inputs,
   4556                                    const ArgList &Args,
   4557                                    const char *LinkingOutput) const {
   4558   const Driver &D = getToolChain().getDriver();
   4559   ArgStringList CmdArgs;
   4560 
   4561   if (!D.SysRoot.empty())
   4562     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
   4563 
   4564   if (Args.hasArg(options::OPT_static)) {
   4565     CmdArgs.push_back("-Bstatic");
   4566   } else {
   4567     if (Args.hasArg(options::OPT_shared))
   4568       CmdArgs.push_back("-Bshareable");
   4569     else {
   4570       CmdArgs.push_back("-dynamic-linker");
   4571       CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
   4572     }
   4573   }
   4574 
   4575   // When building 32-bit code on DragonFly/pc64, we have to explicitly
   4576   // instruct ld in the base system to link 32-bit code.
   4577   if (getToolChain().getArchName() == "i386") {
   4578     CmdArgs.push_back("-m");
   4579     CmdArgs.push_back("elf_i386");
   4580   }
   4581 
   4582   if (Output.isFilename()) {
   4583     CmdArgs.push_back("-o");
   4584     CmdArgs.push_back(Output.getFilename());
   4585   } else {
   4586     assert(Output.isNothing() && "Invalid output.");
   4587   }
   4588 
   4589   if (!Args.hasArg(options::OPT_nostdlib) &&
   4590       !Args.hasArg(options::OPT_nostartfiles)) {
   4591     if (!Args.hasArg(options::OPT_shared)) {
   4592       CmdArgs.push_back(
   4593             Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
   4594       CmdArgs.push_back(
   4595             Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
   4596       CmdArgs.push_back(
   4597             Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
   4598     } else {
   4599       CmdArgs.push_back(
   4600             Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
   4601       CmdArgs.push_back(
   4602             Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
   4603     }
   4604   }
   4605 
   4606   Args.AddAllArgs(CmdArgs, options::OPT_L);
   4607   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
   4608   Args.AddAllArgs(CmdArgs, options::OPT_e);
   4609 
   4610   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   4611 
   4612   if (!Args.hasArg(options::OPT_nostdlib) &&
   4613       !Args.hasArg(options::OPT_nodefaultlibs)) {
   4614     // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
   4615     //         rpaths
   4616     CmdArgs.push_back("-L/usr/lib/gcc41");
   4617 
   4618     if (!Args.hasArg(options::OPT_static)) {
   4619       CmdArgs.push_back("-rpath");
   4620       CmdArgs.push_back("/usr/lib/gcc41");
   4621 
   4622       CmdArgs.push_back("-rpath-link");
   4623       CmdArgs.push_back("/usr/lib/gcc41");
   4624 
   4625       CmdArgs.push_back("-rpath");
   4626       CmdArgs.push_back("/usr/lib");
   4627 
   4628       CmdArgs.push_back("-rpath-link");
   4629       CmdArgs.push_back("/usr/lib");
   4630     }
   4631 
   4632     if (D.CCCIsCXX) {
   4633       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
   4634       CmdArgs.push_back("-lm");
   4635     }
   4636 
   4637     if (Args.hasArg(options::OPT_shared)) {
   4638       CmdArgs.push_back("-lgcc_pic");
   4639     } else {
   4640       CmdArgs.push_back("-lgcc");
   4641     }
   4642 
   4643 
   4644     if (Args.hasArg(options::OPT_pthread))
   4645       CmdArgs.push_back("-lpthread");
   4646 
   4647     if (!Args.hasArg(options::OPT_nolibc)) {
   4648       CmdArgs.push_back("-lc");
   4649     }
   4650 
   4651     if (Args.hasArg(options::OPT_shared)) {
   4652       CmdArgs.push_back("-lgcc_pic");
   4653     } else {
   4654       CmdArgs.push_back("-lgcc");
   4655     }
   4656   }
   4657 
   4658   if (!Args.hasArg(options::OPT_nostdlib) &&
   4659       !Args.hasArg(options::OPT_nostartfiles)) {
   4660     if (!Args.hasArg(options::OPT_shared))
   4661       CmdArgs.push_back(Args.MakeArgString(
   4662                               getToolChain().GetFilePath("crtend.o")));
   4663     else
   4664       CmdArgs.push_back(Args.MakeArgString(
   4665                               getToolChain().GetFilePath("crtendS.o")));
   4666     CmdArgs.push_back(Args.MakeArgString(
   4667                               getToolChain().GetFilePath("crtn.o")));
   4668   }
   4669 
   4670   addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
   4671 
   4672   const char *Exec =
   4673     Args.MakeArgString(getToolChain().GetProgramPath("ld"));
   4674   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4675 }
   4676 
   4677 void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
   4678                                       const InputInfo &Output,
   4679                                       const InputInfoList &Inputs,
   4680                                       const ArgList &Args,
   4681                                       const char *LinkingOutput) const {
   4682   ArgStringList CmdArgs;
   4683 
   4684   if (Output.isFilename()) {
   4685     CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
   4686                                          Output.getFilename()));
   4687   } else {
   4688     assert(Output.isNothing() && "Invalid output.");
   4689   }
   4690 
   4691   if (!Args.hasArg(options::OPT_nostdlib) &&
   4692     !Args.hasArg(options::OPT_nostartfiles)) {
   4693     CmdArgs.push_back("-defaultlib:libcmt");
   4694   }
   4695 
   4696   CmdArgs.push_back("-nologo");
   4697 
   4698   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
   4699 
   4700   const char *Exec =
   4701     Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
   4702   C.addCommand(new Command(JA, *this, Exec, CmdArgs));
   4703 }
   4704