Tools.cpp revision 210299
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/Option.h"
21#include "clang/Driver/Options.h"
22#include "clang/Driver/ToolChain.h"
23#include "clang/Driver/Util.h"
24
25#include "llvm/ADT/SmallString.h"
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/ADT/Twine.h"
28#include "llvm/Support/Format.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/System/Host.h"
31#include "llvm/System/Process.h"
32
33#include "InputInfo.h"
34#include "ToolChains.h"
35
36using namespace clang::driver;
37using namespace clang::driver::tools;
38
39/// CheckPreprocessingOptions - Perform some validation of preprocessing
40/// arguments that is shared with gcc.
41static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
42  if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
43    if (!Args.hasArg(options::OPT_E))
44      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
45        << A->getAsString(Args) << "-E";
46}
47
48/// CheckCodeGenerationOptions - Perform some validation of code generation
49/// arguments that is shared with gcc.
50static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
51  // In gcc, only ARM checks this, but it seems reasonable to check universally.
52  if (Args.hasArg(options::OPT_static))
53    if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
54                                       options::OPT_mdynamic_no_pic))
55      D.Diag(clang::diag::err_drv_argument_not_allowed_with)
56        << A->getAsString(Args) << "-static";
57}
58
59// Quote target names for inclusion in GNU Make dependency files.
60// Only the characters '$', '#', ' ', '\t' are quoted.
61static void QuoteTarget(llvm::StringRef Target,
62                        llvm::SmallVectorImpl<char> &Res) {
63  for (unsigned i = 0, e = Target.size(); i != e; ++i) {
64    switch (Target[i]) {
65    case ' ':
66    case '\t':
67      // Escape the preceding backslashes
68      for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
69        Res.push_back('\\');
70
71      // Escape the space/tab
72      Res.push_back('\\');
73      break;
74    case '$':
75      Res.push_back('$');
76      break;
77    case '#':
78      Res.push_back('\\');
79      break;
80    default:
81      break;
82    }
83
84    Res.push_back(Target[i]);
85  }
86}
87
88void Clang::AddPreprocessingOptions(const Driver &D,
89                                    const ArgList &Args,
90                                    ArgStringList &CmdArgs,
91                                    const InputInfo &Output,
92                                    const InputInfoList &Inputs) const {
93  Arg *A;
94
95  CheckPreprocessingOptions(D, Args);
96
97  Args.AddLastArg(CmdArgs, options::OPT_C);
98  Args.AddLastArg(CmdArgs, options::OPT_CC);
99
100  // Handle dependency file generation.
101  if ((A = Args.getLastArg(options::OPT_M)) ||
102      (A = Args.getLastArg(options::OPT_MM)) ||
103      (A = Args.getLastArg(options::OPT_MD)) ||
104      (A = Args.getLastArg(options::OPT_MMD))) {
105    // Determine the output location.
106    const char *DepFile;
107    if (Output.getType() == types::TY_Dependencies) {
108      if (Output.isPipe())
109        DepFile = "-";
110      else
111        DepFile = Output.getFilename();
112    } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
113      DepFile = MF->getValue(Args);
114    } else if (A->getOption().matches(options::OPT_M) ||
115               A->getOption().matches(options::OPT_MM)) {
116      DepFile = "-";
117    } else {
118      DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
119    }
120    CmdArgs.push_back("-dependency-file");
121    CmdArgs.push_back(DepFile);
122
123    // Add a default target if one wasn't specified.
124    if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
125      const char *DepTarget;
126
127      // If user provided -o, that is the dependency target, except
128      // when we are only generating a dependency file.
129      Arg *OutputOpt = Args.getLastArg(options::OPT_o);
130      if (OutputOpt && Output.getType() != types::TY_Dependencies) {
131        DepTarget = OutputOpt->getValue(Args);
132      } else {
133        // Otherwise derive from the base input.
134        //
135        // FIXME: This should use the computed output file location.
136        llvm::sys::Path P(Inputs[0].getBaseInput());
137
138        P.eraseSuffix();
139        P.appendSuffix("o");
140        DepTarget = Args.MakeArgString(P.getLast());
141      }
142
143      CmdArgs.push_back("-MT");
144      llvm::SmallString<128> Quoted;
145      QuoteTarget(DepTarget, Quoted);
146      CmdArgs.push_back(Args.MakeArgString(Quoted));
147    }
148
149    if (A->getOption().matches(options::OPT_M) ||
150        A->getOption().matches(options::OPT_MD))
151      CmdArgs.push_back("-sys-header-deps");
152  }
153
154  Args.AddLastArg(CmdArgs, options::OPT_MP);
155
156  // Convert all -MQ <target> args to -MT <quoted target>
157  for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
158                                             options::OPT_MQ),
159         ie = Args.filtered_end(); it != ie; ++it) {
160    const Arg *A = *it;
161    A->claim();
162
163    if (A->getOption().matches(options::OPT_MQ)) {
164      CmdArgs.push_back("-MT");
165      llvm::SmallString<128> Quoted;
166      QuoteTarget(A->getValue(Args), Quoted);
167      CmdArgs.push_back(Args.MakeArgString(Quoted));
168
169    // -MT flag - no change
170    } else {
171      A->render(Args, CmdArgs);
172    }
173  }
174
175  // Add -i* options, and automatically translate to
176  // -include-pch/-include-pth for transparent PCH support. It's
177  // wonky, but we include looking for .gch so we can support seamless
178  // replacement into a build system already set up to be generating
179  // .gch files.
180  for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
181         ie = Args.filtered_end(); it != ie; ++it) {
182    const Arg *A = it;
183
184    if (A->getOption().matches(options::OPT_include)) {
185      // Use PCH if the user requested it, except for C++ (for now).
186      bool UsePCH = D.CCCUsePCH;
187      if (types::isCXX(Inputs[0].getType()))
188        UsePCH = false;
189
190      bool FoundPTH = false;
191      bool FoundPCH = false;
192      llvm::sys::Path P(A->getValue(Args));
193      if (UsePCH) {
194        P.appendSuffix("pch");
195        if (P.exists())
196          FoundPCH = true;
197        else
198          P.eraseSuffix();
199      }
200
201      if (!FoundPCH) {
202        P.appendSuffix("pth");
203        if (P.exists())
204          FoundPTH = true;
205        else
206          P.eraseSuffix();
207      }
208
209      if (!FoundPCH && !FoundPTH) {
210        P.appendSuffix("gch");
211        if (P.exists()) {
212          FoundPCH = UsePCH;
213          FoundPTH = !UsePCH;
214        }
215        else
216          P.eraseSuffix();
217      }
218
219      if (FoundPCH || FoundPTH) {
220        A->claim();
221        if (UsePCH)
222          CmdArgs.push_back("-include-pch");
223        else
224          CmdArgs.push_back("-include-pth");
225        CmdArgs.push_back(Args.MakeArgString(P.str()));
226        continue;
227      }
228    }
229
230    // Not translated, render as usual.
231    A->claim();
232    A->render(Args, CmdArgs);
233  }
234
235  Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
236  Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
237
238  // Add -Wp, and -Xassembler if using the preprocessor.
239
240  // FIXME: There is a very unfortunate problem here, some troubled
241  // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
242  // really support that we would have to parse and then translate
243  // those options. :(
244  Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
245                       options::OPT_Xpreprocessor);
246
247  // -I- is a deprecated GCC feature, reject it.
248  if (Arg *A = Args.getLastArg(options::OPT_I_))
249    D.Diag(clang::diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
250}
251
252/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targetting.
253//
254// FIXME: tblgen this.
255static const char *getARMTargetCPU(const ArgList &Args,
256                                   const llvm::Triple &Triple) {
257  // FIXME: Warn on inconsistent use of -mcpu and -march.
258
259  // If we have -mcpu=, use that.
260  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
261    return A->getValue(Args);
262
263  llvm::StringRef MArch;
264  if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
265    // Otherwise, if we have -march= choose the base CPU for that arch.
266    MArch = A->getValue(Args);
267  } else {
268    // Otherwise, use the Arch from the triple.
269    MArch = Triple.getArchName();
270  }
271
272  if (MArch == "armv2" || MArch == "armv2a")
273    return "arm2";
274  if (MArch == "armv3")
275    return "arm6";
276  if (MArch == "armv3m")
277    return "arm7m";
278  if (MArch == "armv4" || MArch == "armv4t")
279    return "arm7tdmi";
280  if (MArch == "armv5" || MArch == "armv5t")
281    return "arm10tdmi";
282  if (MArch == "armv5e" || MArch == "armv5te")
283    return "arm1026ejs";
284  if (MArch == "armv5tej")
285    return "arm926ej-s";
286  if (MArch == "armv6" || MArch == "armv6k")
287    return "arm1136jf-s";
288  if (MArch == "armv6j")
289    return "arm1136j-s";
290  if (MArch == "armv6z" || MArch == "armv6zk")
291    return "arm1176jzf-s";
292  if (MArch == "armv6t2")
293    return "arm1156t2-s";
294  if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
295    return "cortex-a8";
296  if (MArch == "armv7r" || MArch == "armv7-r")
297    return "cortex-r4";
298  if (MArch == "armv7m" || MArch == "armv7-m")
299    return "cortex-m3";
300  if (MArch == "ep9312")
301    return "ep9312";
302  if (MArch == "iwmmxt")
303    return "iwmmxt";
304  if (MArch == "xscale")
305    return "xscale";
306
307  // If all else failed, return the most base CPU LLVM supports.
308  return "arm7tdmi";
309}
310
311/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
312/// CPU.
313//
314// FIXME: This is redundant with -mcpu, why does LLVM use this.
315// FIXME: tblgen this, or kill it!
316static const char *getLLVMArchSuffixForARM(llvm::StringRef CPU) {
317  if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
318      CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
319      CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
320      CPU == "arm940t" || CPU == "ep9312")
321    return "v4t";
322
323  if (CPU == "arm10tdmi" || CPU == "arm1020t")
324    return "v5";
325
326  if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
327      CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
328      CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
329      CPU == "iwmmxt")
330    return "v5e";
331
332  if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
333      CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
334    return "v6";
335
336  if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
337    return "v6t2";
338
339  if (CPU == "cortex-a8" || CPU == "cortex-a9")
340    return "v7";
341
342  return "";
343}
344
345/// getLLVMTriple - Get the LLVM triple to use for a particular toolchain, which
346/// may depend on command line arguments.
347static std::string getLLVMTriple(const ToolChain &TC, const ArgList &Args) {
348  switch (TC.getTriple().getArch()) {
349  default:
350    return TC.getTripleString();
351
352  case llvm::Triple::arm:
353  case llvm::Triple::thumb: {
354    // FIXME: Factor into subclasses.
355    llvm::Triple Triple = TC.getTriple();
356
357    // Thumb2 is the default for V7 on Darwin.
358    //
359    // FIXME: Thumb should just be another -target-feaure, not in the triple.
360    llvm::StringRef Suffix =
361      getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
362    bool ThumbDefault =
363      (Suffix == "v7" && TC.getTriple().getOS() == llvm::Triple::Darwin);
364    std::string ArchName = "arm";
365    if (Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
366      ArchName = "thumb";
367    Triple.setArchName(ArchName + Suffix.str());
368
369    return Triple.getTriple();
370  }
371  }
372}
373
374// FIXME: Move to target hook.
375static bool isSignedCharDefault(const llvm::Triple &Triple) {
376  switch (Triple.getArch()) {
377  default:
378    return true;
379
380  case llvm::Triple::ppc:
381  case llvm::Triple::ppc64:
382    if (Triple.getOS() == llvm::Triple::Darwin)
383      return true;
384    return false;
385
386  case llvm::Triple::systemz:
387    return false;
388  }
389}
390
391void Clang::AddARMTargetArgs(const ArgList &Args,
392                             ArgStringList &CmdArgs) const {
393  const Driver &D = getToolChain().getDriver();
394  llvm::Triple Triple = getToolChain().getTriple();
395
396  // Select the ABI to use.
397  //
398  // FIXME: Support -meabi.
399  const char *ABIName = 0;
400  if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
401    ABIName = A->getValue(Args);
402  } else {
403    // Select the default based on the platform.
404    llvm::StringRef env = Triple.getEnvironmentName();
405    if (env == "gnueabi")
406      ABIName = "aapcs-linux";
407    else if (env == "eabi")
408      ABIName = "aapcs";
409    else
410      ABIName = "apcs-gnu";
411  }
412  CmdArgs.push_back("-target-abi");
413  CmdArgs.push_back(ABIName);
414
415  // Set the CPU based on -march= and -mcpu=.
416  CmdArgs.push_back("-target-cpu");
417  CmdArgs.push_back(getARMTargetCPU(Args, Triple));
418
419  // Select the float ABI as determined by -msoft-float, -mhard-float, and
420  // -mfloat-abi=.
421  llvm::StringRef FloatABI;
422  if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
423                               options::OPT_mhard_float,
424                               options::OPT_mfloat_abi_EQ)) {
425    if (A->getOption().matches(options::OPT_msoft_float))
426      FloatABI = "soft";
427    else if (A->getOption().matches(options::OPT_mhard_float))
428      FloatABI = "hard";
429    else {
430      FloatABI = A->getValue(Args);
431      if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
432        D.Diag(clang::diag::err_drv_invalid_mfloat_abi)
433          << A->getAsString(Args);
434        FloatABI = "soft";
435      }
436    }
437  }
438
439  // If unspecified, choose the default based on the platform.
440  if (FloatABI.empty()) {
441    const llvm::Triple &Triple = getToolChain().getTriple();
442    switch (Triple.getOS()) {
443    case llvm::Triple::Darwin: {
444      // Darwin defaults to "softfp" for v6 and v7.
445      //
446      // FIXME: Factor out an ARM class so we can cache the arch somewhere.
447      llvm::StringRef ArchName =
448        getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
449      if (ArchName.startswith("v6") || ArchName.startswith("v7"))
450        FloatABI = "softfp";
451      else
452        FloatABI = "soft";
453      break;
454    }
455
456    case llvm::Triple::Linux: {
457      llvm::StringRef Env = getToolChain().getTriple().getEnvironmentName();
458      if (Env == "gnueabi") {
459        FloatABI = "softfp";
460        break;
461      }
462    }
463    // fall through
464
465    default:
466      // Assume "soft", but warn the user we are guessing.
467      FloatABI = "soft";
468      D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
469      break;
470    }
471  }
472
473  if (FloatABI == "soft") {
474    // Floating point operations and argument passing are soft.
475    //
476    // FIXME: This changes CPP defines, we need -target-soft-float.
477    CmdArgs.push_back("-msoft-float");
478    CmdArgs.push_back("-mfloat-abi");
479    CmdArgs.push_back("soft");
480  } else if (FloatABI == "softfp") {
481    // Floating point operations are hard, but argument passing is soft.
482    CmdArgs.push_back("-mfloat-abi");
483    CmdArgs.push_back("soft");
484  } else {
485    // Floating point operations and argument passing are hard.
486    assert(FloatABI == "hard" && "Invalid float abi!");
487    CmdArgs.push_back("-mfloat-abi");
488    CmdArgs.push_back("hard");
489  }
490
491  // Set appropriate target features for floating point mode.
492  //
493  // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
494  // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
495  // stripped out by the ARM target.
496
497  // Use software floating point operations?
498  if (FloatABI == "soft") {
499    CmdArgs.push_back("-target-feature");
500    CmdArgs.push_back("+soft-float");
501  }
502
503  // Use software floating point argument passing?
504  if (FloatABI != "hard") {
505    CmdArgs.push_back("-target-feature");
506    CmdArgs.push_back("+soft-float-abi");
507  }
508
509  // Honor -mfpu=.
510  //
511  // FIXME: Centralize feature selection, defaulting shouldn't be also in the
512  // frontend target.
513  if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
514    llvm::StringRef FPU = A->getValue(Args);
515
516    // Set the target features based on the FPU.
517    if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
518      // Disable any default FPU support.
519      CmdArgs.push_back("-target-feature");
520      CmdArgs.push_back("-vfp2");
521      CmdArgs.push_back("-target-feature");
522      CmdArgs.push_back("-vfp3");
523      CmdArgs.push_back("-target-feature");
524      CmdArgs.push_back("-neon");
525    } else if (FPU == "vfp") {
526      CmdArgs.push_back("-target-feature");
527      CmdArgs.push_back("+vfp2");
528    } else if (FPU == "vfp3") {
529      CmdArgs.push_back("-target-feature");
530      CmdArgs.push_back("+vfp3");
531    } else if (FPU == "neon") {
532      CmdArgs.push_back("-target-feature");
533      CmdArgs.push_back("+neon");
534    } else
535      D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
536  }
537}
538
539void Clang::AddMIPSTargetArgs(const ArgList &Args,
540                             ArgStringList &CmdArgs) const {
541  const Driver &D = getToolChain().getDriver();
542
543  // Select the ABI to use.
544  const char *ABIName = 0;
545  if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
546    ABIName = A->getValue(Args);
547  } else {
548    ABIName = "o32";
549  }
550
551  CmdArgs.push_back("-target-abi");
552  CmdArgs.push_back(ABIName);
553
554  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
555    llvm::StringRef MArch = A->getValue(Args);
556    CmdArgs.push_back("-target-cpu");
557
558    if ((MArch == "r2000") || (MArch == "r3000"))
559      CmdArgs.push_back("mips1");
560    else if (MArch == "r6000")
561      CmdArgs.push_back("mips2");
562    else
563      CmdArgs.push_back(MArch.str().c_str());
564  }
565
566  // Select the float ABI as determined by -msoft-float, -mhard-float, and
567  llvm::StringRef FloatABI;
568  if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
569                               options::OPT_mhard_float)) {
570    if (A->getOption().matches(options::OPT_msoft_float))
571      FloatABI = "soft";
572    else if (A->getOption().matches(options::OPT_mhard_float))
573      FloatABI = "hard";
574  }
575
576  // If unspecified, choose the default based on the platform.
577  if (FloatABI.empty()) {
578    // Assume "soft", but warn the user we are guessing.
579    FloatABI = "soft";
580    D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
581  }
582
583  if (FloatABI == "soft") {
584    // Floating point operations and argument passing are soft.
585    //
586    // FIXME: This changes CPP defines, we need -target-soft-float.
587    CmdArgs.push_back("-msoft-float");
588  } else {
589    assert(FloatABI == "hard" && "Invalid float abi!");
590    CmdArgs.push_back("-mhard-float");
591  }
592}
593
594void Clang::AddX86TargetArgs(const ArgList &Args,
595                             ArgStringList &CmdArgs) const {
596  if (!Args.hasFlag(options::OPT_mred_zone,
597                    options::OPT_mno_red_zone,
598                    true) ||
599      Args.hasArg(options::OPT_mkernel) ||
600      Args.hasArg(options::OPT_fapple_kext))
601    CmdArgs.push_back("-disable-red-zone");
602
603  if (Args.hasFlag(options::OPT_msoft_float,
604                   options::OPT_mno_soft_float,
605                   false))
606    CmdArgs.push_back("-no-implicit-float");
607
608  const char *CPUName = 0;
609  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
610    if (llvm::StringRef(A->getValue(Args)) == "native") {
611      // FIXME: Reject attempts to use -march=native unless the target matches
612      // the host.
613      //
614      // FIXME: We should also incorporate the detected target features for use
615      // with -native.
616      std::string CPU = llvm::sys::getHostCPUName();
617      if (!CPU.empty())
618        CPUName = Args.MakeArgString(CPU);
619    } else
620      CPUName = A->getValue(Args);
621  }
622
623  // Select the default CPU if none was given (or detection failed).
624  if (!CPUName) {
625    // FIXME: Need target hooks.
626    if (getToolChain().getOS().startswith("darwin")) {
627      if (getToolChain().getArchName() == "x86_64")
628        CPUName = "core2";
629      else if (getToolChain().getArchName() == "i386")
630        CPUName = "yonah";
631    } else if (getToolChain().getOS().startswith("haiku"))  {
632      if (getToolChain().getArchName() == "x86_64")
633        CPUName = "x86-64";
634      else if (getToolChain().getArchName() == "i386")
635        CPUName = "i586";
636    } else {
637      if (getToolChain().getArchName() == "x86_64")
638        CPUName = "x86-64";
639      else if (getToolChain().getArchName() == "i386")
640        CPUName = "i486";
641    }
642  }
643
644  if (CPUName) {
645    CmdArgs.push_back("-target-cpu");
646    CmdArgs.push_back(CPUName);
647  }
648
649  for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
650         ie = Args.filtered_end(); it != ie; ++it) {
651    llvm::StringRef Name = (*it)->getOption().getName();
652    (*it)->claim();
653
654    // Skip over "-m".
655    assert(Name.startswith("-m") && "Invalid feature name.");
656    Name = Name.substr(2);
657
658    bool IsNegative = Name.startswith("no-");
659    if (IsNegative)
660      Name = Name.substr(3);
661
662    CmdArgs.push_back("-target-feature");
663    CmdArgs.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
664  }
665}
666
667static bool needsExceptions(const ArgList &Args,  types::ID InputType,
668                            const llvm::Triple &Triple) {
669  if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
670                               options::OPT_fno_exceptions)) {
671    if (A->getOption().matches(options::OPT_fexceptions))
672      return true;
673    else
674      return false;
675  }
676  switch (InputType) {
677  case types::TY_CXX: case types::TY_CXXHeader:
678  case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
679  case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
680  case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
681    return true;
682
683  case types::TY_ObjC: case types::TY_ObjCHeader:
684  case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
685    if (Args.hasArg(options::OPT_fobjc_nonfragile_abi))
686      return true;
687    if (Triple.getOS() != llvm::Triple::Darwin)
688      return false;
689    return (Triple.getDarwinMajorNumber() >= 9 &&
690            Triple.getArch() == llvm::Triple::x86_64);
691
692  default:
693    return false;
694  }
695}
696
697/// getEffectiveClangTriple - Get the "effective" target triple, which is the
698/// triple for the target but with the OS version potentially modified for
699/// Darwin's -mmacosx-version-min.
700static std::string getEffectiveClangTriple(const Driver &D,
701                                           const ToolChain &TC,
702                                           const ArgList &Args) {
703  llvm::Triple Triple(getLLVMTriple(TC, Args));
704
705  // Handle -mmacosx-version-min and -miphoneos-version-min.
706  if (Triple.getOS() != llvm::Triple::Darwin) {
707    // Diagnose use of -mmacosx-version-min and -miphoneos-version-min on
708    // non-Darwin.
709    if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
710                                 options::OPT_miphoneos_version_min_EQ))
711      D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
712  } else {
713    const toolchains::Darwin &DarwinTC(
714      reinterpret_cast<const toolchains::Darwin&>(TC));
715
716    // If the target isn't initialized (e.g., an unknown Darwin platform, return
717    // the default triple).
718    if (!DarwinTC.isTargetInitialized())
719      return Triple.getTriple();
720
721    unsigned Version[3];
722    DarwinTC.getTargetVersion(Version);
723
724    // Mangle the target version into the OS triple component.  For historical
725    // reasons that make little sense, the version passed here is the "darwin"
726    // version, which drops the 10 and offsets by 4. See inverse code when
727    // setting the OS version preprocessor define.
728    if (!DarwinTC.isTargetIPhoneOS()) {
729      Version[0] = Version[1] + 4;
730      Version[1] = Version[2];
731      Version[2] = 0;
732    } else {
733      // Use the environment to communicate that we are targetting iPhoneOS.
734      Triple.setEnvironmentName("iphoneos");
735    }
736
737    llvm::SmallString<16> Str;
738    llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
739                                   << "." << Version[1] << "." << Version[2];
740    Triple.setOSName(Str.str());
741  }
742
743  return Triple.getTriple();
744}
745
746void Clang::ConstructJob(Compilation &C, const JobAction &JA,
747                         Job &Dest,
748                         const InputInfo &Output,
749                         const InputInfoList &Inputs,
750                         const ArgList &Args,
751                         const char *LinkingOutput) const {
752  bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
753                                  options::OPT_fapple_kext);
754  const Driver &D = getToolChain().getDriver();
755  ArgStringList CmdArgs;
756
757  assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
758
759  // Invoke ourselves in -cc1 mode.
760  //
761  // FIXME: Implement custom jobs for internal actions.
762  CmdArgs.push_back("-cc1");
763
764  // Add the "effective" target triple.
765  CmdArgs.push_back("-triple");
766  std::string TripleStr = getEffectiveClangTriple(D, getToolChain(), Args);
767  CmdArgs.push_back(Args.MakeArgString(TripleStr));
768
769  // Select the appropriate action.
770  if (isa<AnalyzeJobAction>(JA)) {
771    assert(JA.getType() == types::TY_Plist && "Invalid output type.");
772    CmdArgs.push_back("-analyze");
773  } else if (isa<PreprocessJobAction>(JA)) {
774    if (Output.getType() == types::TY_Dependencies)
775      CmdArgs.push_back("-Eonly");
776    else
777      CmdArgs.push_back("-E");
778  } else if (isa<AssembleJobAction>(JA)) {
779    CmdArgs.push_back("-emit-obj");
780
781    // At -O0, we use -mrelax-all by default.
782    bool IsOpt = false;
783    if (Arg *A = Args.getLastArg(options::OPT_O_Group))
784      IsOpt = !A->getOption().matches(options::OPT_O0);
785    if (Args.hasFlag(options::OPT_mrelax_all,
786                      options::OPT_mno_relax_all,
787                      !IsOpt))
788      CmdArgs.push_back("-mrelax-all");
789  } else if (isa<PrecompileJobAction>(JA)) {
790    // Use PCH if the user requested it, except for C++ (for now).
791    bool UsePCH = D.CCCUsePCH;
792    if (types::isCXX(Inputs[0].getType()))
793      UsePCH = false;
794
795    if (UsePCH)
796      CmdArgs.push_back("-emit-pch");
797    else
798      CmdArgs.push_back("-emit-pth");
799  } else {
800    assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
801
802    if (JA.getType() == types::TY_Nothing) {
803      CmdArgs.push_back("-fsyntax-only");
804    } else if (JA.getType() == types::TY_LLVM_IR ||
805               JA.getType() == types::TY_LTO_IR) {
806      CmdArgs.push_back("-emit-llvm");
807    } else if (JA.getType() == types::TY_LLVM_BC ||
808               JA.getType() == types::TY_LTO_BC) {
809      CmdArgs.push_back("-emit-llvm-bc");
810    } else if (JA.getType() == types::TY_PP_Asm) {
811      CmdArgs.push_back("-S");
812    } else if (JA.getType() == types::TY_AST) {
813      CmdArgs.push_back("-emit-pch");
814    } else if (JA.getType() == types::TY_RewrittenObjC) {
815      CmdArgs.push_back("-rewrite-objc");
816    } else {
817      assert(JA.getType() == types::TY_PP_Asm &&
818             "Unexpected output type!");
819    }
820  }
821
822  // The make clang go fast button.
823  CmdArgs.push_back("-disable-free");
824
825  // Disable the verification pass in -asserts builds.
826#ifdef NDEBUG
827  CmdArgs.push_back("-disable-llvm-verifier");
828#endif
829
830  // Set the main file name, so that debug info works even with
831  // -save-temps.
832  CmdArgs.push_back("-main-file-name");
833  CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
834
835  // Some flags which affect the language (via preprocessor
836  // defines). See darwin::CC1::AddCPPArgs.
837  if (Args.hasArg(options::OPT_static))
838    CmdArgs.push_back("-static-define");
839
840  if (isa<AnalyzeJobAction>(JA)) {
841    // Enable region store model by default.
842    CmdArgs.push_back("-analyzer-store=region");
843
844    // Treat blocks as analysis entry points.
845    CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
846
847    // Add default argument set.
848    if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
849      CmdArgs.push_back("-analyzer-check-dead-stores");
850      // Do not enable the security-syntatic check since it
851      // it needs to be refined (known issues).
852      // CmdArgs.push_back("-analyzer-check-security-syntactic");
853      CmdArgs.push_back("-analyzer-check-objc-mem");
854      CmdArgs.push_back("-analyzer-eagerly-assume");
855      CmdArgs.push_back("-analyzer-check-objc-methodsigs");
856      // Do not enable the missing -dealloc check.
857      // '-analyzer-check-objc-missing-dealloc',
858      CmdArgs.push_back("-analyzer-check-objc-unused-ivars");
859    }
860
861    // Set the output format. The default is plist, for (lame) historical
862    // reasons.
863    CmdArgs.push_back("-analyzer-output");
864    if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
865      CmdArgs.push_back(A->getValue(Args));
866    else
867      CmdArgs.push_back("plist");
868
869    // Disable the presentation of standard compiler warnings when
870    // using --analyze.  We only want to show static analyzer diagnostics
871    // or frontend errors.
872    CmdArgs.push_back("-w");
873
874    // Add -Xanalyzer arguments when running as analyzer.
875    Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
876  }
877
878  CheckCodeGenerationOptions(D, Args);
879
880  // Perform argument translation for LLVM backend. This
881  // takes some care in reconciling with llvm-gcc. The
882  // issue is that llvm-gcc translates these options based on
883  // the values in cc1, whereas we are processing based on
884  // the driver arguments.
885
886  // This comes from the default translation the driver + cc1
887  // would do to enable flag_pic.
888  //
889  // FIXME: Centralize this code.
890  bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
891                     Args.hasArg(options::OPT_fpic) ||
892                     Args.hasArg(options::OPT_fPIE) ||
893                     Args.hasArg(options::OPT_fpie));
894  bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
895                      Args.hasArg(options::OPT_static));
896  const char *Model = getToolChain().GetForcedPicModel();
897  if (!Model) {
898    if (Args.hasArg(options::OPT_mdynamic_no_pic))
899      Model = "dynamic-no-pic";
900    else if (PICDisabled)
901      Model = "static";
902    else if (PICEnabled)
903      Model = "pic";
904    else
905      Model = getToolChain().GetDefaultRelocationModel();
906  }
907  if (llvm::StringRef(Model) != "pic") {
908    CmdArgs.push_back("-mrelocation-model");
909    CmdArgs.push_back(Model);
910  }
911
912  // Infer the __PIC__ value.
913  //
914  // FIXME:  This isn't quite right on Darwin, which always sets
915  // __PIC__=2.
916  if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
917    CmdArgs.push_back("-pic-level");
918    CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
919  }
920  if (!Args.hasFlag(options::OPT_fmerge_all_constants,
921                    options::OPT_fno_merge_all_constants))
922    CmdArgs.push_back("-no-merge-all-constants");
923
924  // LLVM Code Generator Options.
925
926  // FIXME: Set --enable-unsafe-fp-math.
927  if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
928                   options::OPT_fomit_frame_pointer))
929    CmdArgs.push_back("-mdisable-fp-elim");
930  if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
931                    options::OPT_fno_zero_initialized_in_bss))
932    CmdArgs.push_back("-mno-zero-initialized-in-bss");
933
934  // Decide whether to use verbose asm. Verbose assembly is the default on
935  // toolchains which have the integrated assembler on by default.
936  bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
937  if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
938                   IsVerboseAsmDefault) ||
939      Args.hasArg(options::OPT_dA))
940    CmdArgs.push_back("-masm-verbose");
941
942  if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
943    CmdArgs.push_back("-mdebug-pass");
944    CmdArgs.push_back("Structure");
945  }
946  if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
947    CmdArgs.push_back("-mdebug-pass");
948    CmdArgs.push_back("Arguments");
949  }
950
951  // Enable -mconstructor-aliases except on darwin, where we have to
952  // work around a linker bug;  see <rdar://problem/7651567>.
953  if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
954    CmdArgs.push_back("-mconstructor-aliases");
955
956  // This is a coarse approximation of what llvm-gcc actually does, both
957  // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
958  // complicated ways.
959  bool AsynchronousUnwindTables =
960    Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
961                 options::OPT_fno_asynchronous_unwind_tables,
962                 getToolChain().IsUnwindTablesDefault() &&
963                 !KernelOrKext);
964  if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
965                   AsynchronousUnwindTables))
966    CmdArgs.push_back("-munwind-tables");
967
968  if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
969    CmdArgs.push_back("-mlimit-float-precision");
970    CmdArgs.push_back(A->getValue(Args));
971  }
972
973  // FIXME: Handle -mtune=.
974  (void) Args.hasArg(options::OPT_mtune_EQ);
975
976  if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
977    CmdArgs.push_back("-mcode-model");
978    CmdArgs.push_back(A->getValue(Args));
979  }
980
981  // Add target specific cpu and features flags.
982  switch(getToolChain().getTriple().getArch()) {
983  default:
984    break;
985
986  case llvm::Triple::arm:
987  case llvm::Triple::thumb:
988    AddARMTargetArgs(Args, CmdArgs);
989    break;
990
991  case llvm::Triple::mips:
992  case llvm::Triple::mipsel:
993    AddMIPSTargetArgs(Args, CmdArgs);
994    break;
995
996  case llvm::Triple::x86:
997  case llvm::Triple::x86_64:
998    AddX86TargetArgs(Args, CmdArgs);
999    break;
1000  }
1001
1002  // -mno-omit-leaf-frame-pointer is default.
1003  if (Args.hasFlag(options::OPT_momit_leaf_frame_pointer,
1004                   options::OPT_mno_omit_leaf_frame_pointer, false))
1005    CmdArgs.push_back("-momit-leaf-frame-pointer");
1006
1007  // -fno-math-errno is default.
1008  if (Args.hasFlag(options::OPT_fmath_errno,
1009                   options::OPT_fno_math_errno,
1010                   false))
1011    CmdArgs.push_back("-fmath-errno");
1012
1013  // Explicitly error on some things we know we don't support and can't just
1014  // ignore.
1015  types::ID InputType = Inputs[0].getType();
1016  Arg *Unsupported;
1017  if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
1018      (Unsupported = Args.getLastArg(options::OPT_iframework)) ||
1019      (Unsupported = Args.getLastArg(options::OPT_fshort_enums)))
1020    D.Diag(clang::diag::err_drv_clang_unsupported)
1021      << Unsupported->getOption().getName();
1022
1023  if (types::isCXX(InputType) &&
1024      getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1025      getToolChain().getTriple().getArch() == llvm::Triple::x86) {
1026    if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)))
1027      D.Diag(clang::diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
1028        << Unsupported->getOption().getName();
1029  }
1030
1031  Args.AddAllArgs(CmdArgs, options::OPT_v);
1032  Args.AddLastArg(CmdArgs, options::OPT_P);
1033  Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
1034
1035  // Special case debug options to only pass -g to clang. This is
1036  // wrong.
1037  Args.ClaimAllArgs(options::OPT_g_Group);
1038  if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1039    if (!A->getOption().matches(options::OPT_g0))
1040      CmdArgs.push_back("-g");
1041
1042  Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1043  Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1044
1045  Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1046
1047  Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
1048  Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
1049  Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
1050
1051  // Pass the path to compiler resource files.
1052  CmdArgs.push_back("-resource-dir");
1053  CmdArgs.push_back(D.ResourceDir.c_str());
1054
1055  // Add preprocessing options like -I, -D, etc. if we are using the
1056  // preprocessor.
1057  //
1058  // FIXME: Support -fpreprocessed
1059  if (types::getPreprocessedType(InputType) != types::TY_INVALID)
1060    AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
1061
1062  // Manually translate -O to -O2 and -O4 to -O3; let clang reject
1063  // others.
1064  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
1065    if (A->getOption().matches(options::OPT_O4))
1066      CmdArgs.push_back("-O3");
1067    else if (A->getOption().matches(options::OPT_O) &&
1068             A->getValue(Args)[0] == '\0')
1069      CmdArgs.push_back("-O2");
1070    else
1071      A->render(Args, CmdArgs);
1072  }
1073
1074  Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1075  Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1076  Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
1077  Args.AddLastArg(CmdArgs, options::OPT_w);
1078
1079  // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1080  // (-ansi is equivalent to -std=c89).
1081  //
1082  // If a std is supplied, only add -trigraphs if it follows the
1083  // option.
1084  if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1085    if (Std->getOption().matches(options::OPT_ansi))
1086      if (types::isCXX(InputType))
1087        CmdArgs.push_back("-std=c++98");
1088      else
1089        CmdArgs.push_back("-std=c89");
1090    else
1091      Std->render(Args, CmdArgs);
1092
1093    if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1094                                 options::OPT_trigraphs))
1095      if (A != Std)
1096        A->render(Args, CmdArgs);
1097  } else {
1098    // Honor -std-default.
1099    //
1100    // FIXME: Clang doesn't correctly handle -std= when the input language
1101    // doesn't match. For the time being just ignore this for C++ inputs;
1102    // eventually we want to do all the standard defaulting here instead of
1103    // splitting it between the driver and clang -cc1.
1104    if (!types::isCXX(InputType))
1105        Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1106                                  "-std=", /*Joined=*/true);
1107    Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
1108  }
1109
1110  // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1111  if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1112    if (Asm->getOption().matches(options::OPT_fasm))
1113      CmdArgs.push_back("-fgnu-keywords");
1114    else
1115      CmdArgs.push_back("-fno-gnu-keywords");
1116  }
1117
1118  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1119    CmdArgs.push_back("-ftemplate-depth");
1120    CmdArgs.push_back(A->getValue(Args));
1121  }
1122
1123  if (Args.hasArg(options::OPT__relocatable_pch))
1124    CmdArgs.push_back("-relocatable-pch");
1125
1126  if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1127    CmdArgs.push_back("-fconstant-string-class");
1128    CmdArgs.push_back(A->getValue(Args));
1129  }
1130
1131  if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1132    CmdArgs.push_back("-ftabstop");
1133    CmdArgs.push_back(A->getValue(Args));
1134  }
1135
1136  CmdArgs.push_back("-ferror-limit");
1137  if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1138    CmdArgs.push_back(A->getValue(Args));
1139  else
1140    CmdArgs.push_back("19");
1141
1142  if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1143    CmdArgs.push_back("-fmacro-backtrace-limit");
1144    CmdArgs.push_back(A->getValue(Args));
1145  }
1146
1147  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1148    CmdArgs.push_back("-ftemplate-backtrace-limit");
1149    CmdArgs.push_back(A->getValue(Args));
1150  }
1151
1152  // Pass -fmessage-length=.
1153  CmdArgs.push_back("-fmessage-length");
1154  if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
1155    CmdArgs.push_back(A->getValue(Args));
1156  } else {
1157    // If -fmessage-length=N was not specified, determine whether this is a
1158    // terminal and, if so, implicitly define -fmessage-length appropriately.
1159    unsigned N = llvm::sys::Process::StandardErrColumns();
1160    CmdArgs.push_back(Args.MakeArgString(llvm::Twine(N)));
1161  }
1162
1163  if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1164    CmdArgs.push_back("-fvisibility");
1165    CmdArgs.push_back(A->getValue(Args));
1166  }
1167
1168  Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
1169
1170  // -fhosted is default.
1171  if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1172                                   options::OPT_fhosted,
1173                                   false))
1174    CmdArgs.push_back("-ffreestanding");
1175
1176  // Forward -f (flag) options which we can pass directly.
1177  Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
1178  Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
1179  Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
1180  Args.AddLastArg(CmdArgs, options::OPT_fformat_extensions);
1181
1182  // -flax-vector-conversions is default.
1183  if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1184                    options::OPT_fno_lax_vector_conversions))
1185    CmdArgs.push_back("-fno-lax-vector-conversions");
1186
1187  // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1188  // takes precedence.
1189  const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1190  if (!GCArg)
1191    GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1192  if (GCArg) {
1193    if (getToolChain().SupportsObjCGC()) {
1194      GCArg->render(Args, CmdArgs);
1195    } else {
1196      // FIXME: We should move this to a hard error.
1197      D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1198        << GCArg->getAsString(Args);
1199    }
1200  }
1201
1202  Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
1203  Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
1204  Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
1205  Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1206  Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
1207  Args.AddLastArg(CmdArgs, options::OPT_fwrapv);
1208  Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
1209
1210  Args.AddLastArg(CmdArgs, options::OPT_pthread);
1211
1212  // -stack-protector=0 is default.
1213  unsigned StackProtectorLevel = 0;
1214  if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1215                               options::OPT_fstack_protector_all,
1216                               options::OPT_fstack_protector)) {
1217    if (A->getOption().matches(options::OPT_fstack_protector))
1218      StackProtectorLevel = 1;
1219    else if (A->getOption().matches(options::OPT_fstack_protector_all))
1220      StackProtectorLevel = 2;
1221  } else
1222    StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel();
1223  if (StackProtectorLevel) {
1224    CmdArgs.push_back("-stack-protector");
1225    CmdArgs.push_back(Args.MakeArgString(llvm::Twine(StackProtectorLevel)));
1226  }
1227
1228  // Forward -f options with positive and negative forms; we translate
1229  // these by hand.
1230
1231  // -fbuiltin is default.
1232  if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
1233    CmdArgs.push_back("-fno-builtin");
1234
1235  if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1236                    options::OPT_fno_assume_sane_operator_new))
1237    CmdArgs.push_back("-fno-assume-sane-operator-new");
1238
1239  // -fblocks=0 is default.
1240  if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
1241                   getToolChain().IsBlocksDefault())) {
1242    CmdArgs.push_back("-fblocks");
1243  }
1244
1245  // -faccess-control is default.
1246  if (Args.hasFlag(options::OPT_fno_access_control,
1247                   options::OPT_faccess_control,
1248                   false))
1249    CmdArgs.push_back("-fno-access-control");
1250
1251  // -fexceptions=0 is default.
1252  if (needsExceptions(Args, InputType, getToolChain().getTriple()))
1253    CmdArgs.push_back("-fexceptions");
1254
1255  if (getToolChain().UseSjLjExceptions())
1256    CmdArgs.push_back("-fsjlj-exceptions");
1257
1258  // -frtti is default.
1259  if (KernelOrKext ||
1260      !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
1261    CmdArgs.push_back("-fno-rtti");
1262
1263  // -fsigned-char is default.
1264  if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
1265                    isSignedCharDefault(getToolChain().getTriple())))
1266    CmdArgs.push_back("-fno-signed-char");
1267
1268  // -fthreadsafe-static is default.
1269  if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
1270                    options::OPT_fno_threadsafe_statics))
1271    CmdArgs.push_back("-fno-threadsafe-statics");
1272
1273  // -fuse-cxa-atexit is default.
1274  if (KernelOrKext || !Args.hasFlag(options::OPT_fuse_cxa_atexit,
1275                                    options::OPT_fno_use_cxa_atexit))
1276    CmdArgs.push_back("-fno-use-cxa-atexit");
1277
1278  // -fms-extensions=0 is default.
1279  if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1280                   getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1281    CmdArgs.push_back("-fms-extensions");
1282
1283  // -fgnu-keywords default varies depending on language; only pass if
1284  // specified.
1285  if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
1286                               options::OPT_fno_gnu_keywords))
1287    A->render(Args, CmdArgs);
1288
1289  // -fnext-runtime is default.
1290  if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
1291                    getToolChain().getTriple().getOS() == llvm::Triple::Darwin))
1292    CmdArgs.push_back("-fgnu-runtime");
1293
1294  // -fobjc-nonfragile-abi=0 is default.
1295  if (types::isObjC(InputType)) {
1296    unsigned Version = 1;
1297    if (Args.hasArg(options::OPT_fobjc_nonfragile_abi) ||
1298        getToolChain().IsObjCNonFragileABIDefault())
1299      Version = 2;
1300    if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1301      if (llvm::StringRef(A->getValue(Args)) == "1")
1302        Version = 1;
1303      else if (llvm::StringRef(A->getValue(Args)) == "2")
1304        Version = 2;
1305      else
1306        D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1307    }
1308
1309    if (Version == 2) {
1310      CmdArgs.push_back("-fobjc-nonfragile-abi");
1311
1312      // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1313      // legacy is the default.
1314      if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1315                        options::OPT_fno_objc_legacy_dispatch,
1316                        getToolChain().IsObjCLegacyDispatchDefault())) {
1317        if (getToolChain().UseObjCMixedDispatch())
1318          CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1319        else
1320          CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1321      }
1322    }
1323
1324    // FIXME: -fobjc-nonfragile-abi2 is a transient option meant to expose
1325    // features in testing.  It will eventually be removed.
1326    if (Args.hasArg(options::OPT_fobjc_nonfragile_abi2))
1327      CmdArgs.push_back("-fobjc-nonfragile-abi2");
1328  }
1329
1330  if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1331                    options::OPT_fno_assume_sane_operator_new))
1332    CmdArgs.push_back("-fno-assume-sane-operator-new");
1333
1334  // -fconstant-cfstrings is default, and may be subject to argument translation
1335  // on Darwin.
1336  if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1337                    options::OPT_fno_constant_cfstrings) ||
1338      !Args.hasFlag(options::OPT_mconstant_cfstrings,
1339                    options::OPT_mno_constant_cfstrings))
1340    CmdArgs.push_back("-fno-constant-cfstrings");
1341
1342  // -fshort-wchar default varies depending on platform; only
1343  // pass if specified.
1344  if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1345    A->render(Args, CmdArgs);
1346
1347  // -fno-pascal-strings is default, only pass non-default. If the tool chain
1348  // happened to translate to -mpascal-strings, we want to back translate here.
1349  //
1350  // FIXME: This is gross; that translation should be pulled from the
1351  // tool chain.
1352  if (Args.hasFlag(options::OPT_fpascal_strings,
1353                   options::OPT_fno_pascal_strings,
1354                   false) ||
1355      Args.hasFlag(options::OPT_mpascal_strings,
1356                   options::OPT_mno_pascal_strings,
1357                   false))
1358    CmdArgs.push_back("-fpascal-strings");
1359
1360  // -fcommon is default, only pass non-default.
1361  if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
1362    CmdArgs.push_back("-fno-common");
1363
1364  // -fsigned-bitfields is default, and clang doesn't yet support
1365  // --funsigned-bitfields.
1366  if (!Args.hasFlag(options::OPT_fsigned_bitfields,
1367                    options::OPT_funsigned_bitfields))
1368    D.Diag(clang::diag::warn_drv_clang_unsupported)
1369      << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1370
1371  // -fcaret-diagnostics is default.
1372  if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1373                    options::OPT_fno_caret_diagnostics, true))
1374    CmdArgs.push_back("-fno-caret-diagnostics");
1375
1376  // -fdiagnostics-fixit-info is default, only pass non-default.
1377  if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
1378                    options::OPT_fno_diagnostics_fixit_info))
1379    CmdArgs.push_back("-fno-diagnostics-fixit-info");
1380
1381  Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_binary);
1382
1383  // Enable -fdiagnostics-show-option by default.
1384  if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
1385                   options::OPT_fno_diagnostics_show_option))
1386    CmdArgs.push_back("-fdiagnostics-show-option");
1387
1388  if (const Arg *A =
1389        Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
1390    CmdArgs.push_back("-fdiagnostics-show-category");
1391    CmdArgs.push_back(A->getValue(Args));
1392  }
1393
1394  // Color diagnostics are the default, unless the terminal doesn't support
1395  // them.
1396  if (Args.hasFlag(options::OPT_fcolor_diagnostics,
1397                   options::OPT_fno_color_diagnostics) &&
1398      llvm::sys::Process::StandardErrHasColors())
1399    CmdArgs.push_back("-fcolor-diagnostics");
1400
1401  if (!Args.hasFlag(options::OPT_fshow_source_location,
1402                    options::OPT_fno_show_source_location))
1403    CmdArgs.push_back("-fno-show-source-location");
1404
1405  if (!Args.hasFlag(options::OPT_fspell_checking,
1406                    options::OPT_fno_spell_checking))
1407    CmdArgs.push_back("-fno-spell-checking");
1408
1409  if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
1410    A->render(Args, CmdArgs);
1411
1412  // -fdollars-in-identifiers default varies depending on platform and
1413  // language; only pass if specified.
1414  if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
1415                               options::OPT_fno_dollars_in_identifiers)) {
1416    if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
1417      CmdArgs.push_back("-fdollars-in-identifiers");
1418    else
1419      CmdArgs.push_back("-fno-dollars-in-identifiers");
1420  }
1421
1422  // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
1423  // practical purposes.
1424  if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
1425                               options::OPT_fno_unit_at_a_time)) {
1426    if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
1427      D.Diag(clang::diag::warn_drv_clang_unsupported) << A->getAsString(Args);
1428  }
1429
1430  // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
1431  //
1432  // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
1433#if 0
1434  if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1435      (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1436       getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
1437    if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1438      CmdArgs.push_back("-fno-builtin-strcat");
1439    if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1440      CmdArgs.push_back("-fno-builtin-strcpy");
1441  }
1442#endif
1443
1444  if (Arg *A = Args.getLastArg(options::OPT_traditional,
1445                               options::OPT_traditional_cpp))
1446    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1447
1448  Args.AddLastArg(CmdArgs, options::OPT_dM);
1449  Args.AddLastArg(CmdArgs, options::OPT_dD);
1450
1451  // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
1452  // parser.
1453  Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
1454  for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
1455         ie = Args.filtered_end(); it != ie; ++it) {
1456    (*it)->claim();
1457
1458    // We translate this by hand to the -cc1 argument, since nightly test uses
1459    // it and developers have been trained to spell it with -mllvm.
1460    if (llvm::StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
1461      CmdArgs.push_back("-disable-llvm-optzns");
1462    else
1463      (*it)->render(Args, CmdArgs);
1464  }
1465
1466  if (Output.getType() == types::TY_Dependencies) {
1467    // Handled with other dependency code.
1468  } else if (Output.isPipe()) {
1469    CmdArgs.push_back("-o");
1470    CmdArgs.push_back("-");
1471  } else if (Output.isFilename()) {
1472    CmdArgs.push_back("-o");
1473    CmdArgs.push_back(Output.getFilename());
1474  } else {
1475    assert(Output.isNothing() && "Invalid output.");
1476  }
1477
1478  for (InputInfoList::const_iterator
1479         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1480    const InputInfo &II = *it;
1481    CmdArgs.push_back("-x");
1482    CmdArgs.push_back(types::getTypeName(II.getType()));
1483    if (II.isPipe())
1484      CmdArgs.push_back("-");
1485    else if (II.isFilename())
1486      CmdArgs.push_back(II.getFilename());
1487    else
1488      II.getInputArg().renderAsInput(Args, CmdArgs);
1489  }
1490
1491  Args.AddAllArgs(CmdArgs, options::OPT_undef);
1492
1493  std::string Exec = getToolChain().getDriver().getClangProgramPath();
1494
1495  // Optionally embed the -cc1 level arguments into the debug info, for build
1496  // analysis.
1497  if (getToolChain().UseDwarfDebugFlags()) {
1498    ArgStringList OriginalArgs;
1499    for (ArgList::const_iterator it = Args.begin(),
1500           ie = Args.end(); it != ie; ++it)
1501      (*it)->render(Args, OriginalArgs);
1502
1503    llvm::SmallString<256> Flags;
1504    Flags += Exec;
1505    for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
1506      Flags += " ";
1507      Flags += OriginalArgs[i];
1508    }
1509    CmdArgs.push_back("-dwarf-debug-flags");
1510    CmdArgs.push_back(Args.MakeArgString(Flags.str()));
1511  }
1512
1513  Dest.addCommand(new Command(JA, *this, Exec.c_str(), CmdArgs));
1514
1515  // Explicitly warn that these options are unsupported, even though
1516  // we are allowing compilation to continue.
1517  for (arg_iterator it = Args.filtered_begin(options::OPT_pg),
1518         ie = Args.filtered_end(); it != ie; ++it) {
1519    (*it)->claim();
1520    D.Diag(clang::diag::warn_drv_clang_unsupported) << (*it)->getAsString(Args);
1521  }
1522
1523  // Claim some arguments which clang supports automatically.
1524
1525  // -fpch-preprocess is used with gcc to add a special marker in the output to
1526  // include the PCH file. Clang's PTH solution is completely transparent, so we
1527  // do not need to deal with it at all.
1528  Args.ClaimAllArgs(options::OPT_fpch_preprocess);
1529
1530  // Claim some arguments which clang doesn't support, but we don't
1531  // care to warn the user about.
1532  Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
1533  Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
1534}
1535
1536void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
1537                           Job &Dest,
1538                           const InputInfo &Output,
1539                           const InputInfoList &Inputs,
1540                           const ArgList &Args,
1541                           const char *LinkingOutput) const {
1542  const Driver &D = getToolChain().getDriver();
1543  ArgStringList CmdArgs;
1544
1545  assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1546  const InputInfo &Input = Inputs[0];
1547
1548  // Invoke ourselves in -cc1as mode.
1549  //
1550  // FIXME: Implement custom jobs for internal actions.
1551  CmdArgs.push_back("-cc1as");
1552
1553  // Add the "effective" target triple.
1554  CmdArgs.push_back("-triple");
1555  std::string TripleStr = getEffectiveClangTriple(D, getToolChain(), Args);
1556  CmdArgs.push_back(Args.MakeArgString(TripleStr));
1557
1558  // Set the output mode, we currently only expect to be used as a real
1559  // assembler.
1560  CmdArgs.push_back("-filetype");
1561  CmdArgs.push_back("obj");
1562
1563  // At -O0, we use -mrelax-all by default.
1564  bool IsOpt = false;
1565  if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1566    IsOpt = !A->getOption().matches(options::OPT_O0);
1567  if (Args.hasFlag(options::OPT_mrelax_all,
1568                    options::OPT_mno_relax_all,
1569                    !IsOpt))
1570    CmdArgs.push_back("-relax-all");
1571
1572  // FIXME: Add -force_cpusubtype_ALL support, once we have it.
1573
1574  // FIXME: Add -g support, once we have it.
1575
1576  // FIXME: Add -static support, once we have it.
1577
1578  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1579                       options::OPT_Xassembler);
1580
1581  assert(Output.isFilename() && "Unexpected lipo output.");
1582  CmdArgs.push_back("-o");
1583  CmdArgs.push_back(Output.getFilename());
1584
1585  if (Input.isPipe()) {
1586    CmdArgs.push_back("-");
1587  } else {
1588    assert(Input.isFilename() && "Invalid input.");
1589    CmdArgs.push_back(Input.getFilename());
1590  }
1591
1592  std::string Exec = getToolChain().getDriver().getClangProgramPath();
1593  Dest.addCommand(new Command(JA, *this, Exec.c_str(), CmdArgs));
1594}
1595
1596void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
1597                               Job &Dest,
1598                               const InputInfo &Output,
1599                               const InputInfoList &Inputs,
1600                               const ArgList &Args,
1601                               const char *LinkingOutput) const {
1602  const Driver &D = getToolChain().getDriver();
1603  ArgStringList CmdArgs;
1604
1605  for (ArgList::const_iterator
1606         it = Args.begin(), ie = Args.end(); it != ie; ++it) {
1607    Arg *A = *it;
1608    if (A->getOption().hasForwardToGCC()) {
1609      // It is unfortunate that we have to claim here, as this means
1610      // we will basically never report anything interesting for
1611      // platforms using a generic gcc, even if we are just using gcc
1612      // to get to the assembler.
1613      A->claim();
1614      A->render(Args, CmdArgs);
1615    }
1616  }
1617
1618  RenderExtraToolArgs(JA, CmdArgs);
1619
1620  // If using a driver driver, force the arch.
1621  const std::string &Arch = getToolChain().getArchName();
1622  if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
1623    CmdArgs.push_back("-arch");
1624
1625    // FIXME: Remove these special cases.
1626    if (Arch == "powerpc")
1627      CmdArgs.push_back("ppc");
1628    else if (Arch == "powerpc64")
1629      CmdArgs.push_back("ppc64");
1630    else
1631      CmdArgs.push_back(Args.MakeArgString(Arch));
1632  }
1633
1634  // Try to force gcc to match the tool chain we want, if we recognize
1635  // the arch.
1636  //
1637  // FIXME: The triple class should directly provide the information we want
1638  // here.
1639  if (Arch == "i386" || Arch == "powerpc")
1640    CmdArgs.push_back("-m32");
1641  else if (Arch == "x86_64" || Arch == "powerpc64")
1642    CmdArgs.push_back("-m64");
1643
1644  if (Output.isPipe()) {
1645    CmdArgs.push_back("-o");
1646    CmdArgs.push_back("-");
1647  } else if (Output.isFilename()) {
1648    CmdArgs.push_back("-o");
1649    CmdArgs.push_back(Output.getFilename());
1650  } else {
1651    assert(Output.isNothing() && "Unexpected output");
1652    CmdArgs.push_back("-fsyntax-only");
1653  }
1654
1655
1656  // Only pass -x if gcc will understand it; otherwise hope gcc
1657  // understands the suffix correctly. The main use case this would go
1658  // wrong in is for linker inputs if they happened to have an odd
1659  // suffix; really the only way to get this to happen is a command
1660  // like '-x foobar a.c' which will treat a.c like a linker input.
1661  //
1662  // FIXME: For the linker case specifically, can we safely convert
1663  // inputs into '-Wl,' options?
1664  for (InputInfoList::const_iterator
1665         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1666    const InputInfo &II = *it;
1667
1668    // Don't try to pass LLVM or AST inputs to a generic gcc.
1669    if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
1670        II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
1671      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
1672        << getToolChain().getTripleString();
1673    else if (II.getType() == types::TY_AST)
1674      D.Diag(clang::diag::err_drv_no_ast_support)
1675        << getToolChain().getTripleString();
1676
1677    if (types::canTypeBeUserSpecified(II.getType())) {
1678      CmdArgs.push_back("-x");
1679      CmdArgs.push_back(types::getTypeName(II.getType()));
1680    }
1681
1682    if (II.isPipe())
1683      CmdArgs.push_back("-");
1684    else if (II.isFilename())
1685      CmdArgs.push_back(II.getFilename());
1686    else
1687      // Don't render as input, we need gcc to do the translations.
1688      II.getInputArg().render(Args, CmdArgs);
1689  }
1690
1691  const char *GCCName = getToolChain().getDriver().CCCGenericGCCName.c_str();
1692  const char *Exec =
1693    Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
1694  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
1695}
1696
1697void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
1698                                          ArgStringList &CmdArgs) const {
1699  CmdArgs.push_back("-E");
1700}
1701
1702void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
1703                                          ArgStringList &CmdArgs) const {
1704  // The type is good enough.
1705}
1706
1707void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
1708                                       ArgStringList &CmdArgs) const {
1709  const Driver &D = getToolChain().getDriver();
1710
1711  // If -flto, etc. are present then make sure not to force assembly output.
1712  if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
1713      JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
1714    CmdArgs.push_back("-c");
1715  else {
1716    if (JA.getType() != types::TY_PP_Asm)
1717      D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
1718        << getTypeName(JA.getType());
1719
1720    CmdArgs.push_back("-S");
1721  }
1722}
1723
1724void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
1725                                        ArgStringList &CmdArgs) const {
1726  CmdArgs.push_back("-c");
1727}
1728
1729void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
1730                                    ArgStringList &CmdArgs) const {
1731  // The types are (hopefully) good enough.
1732}
1733
1734const char *darwin::CC1::getCC1Name(types::ID Type) const {
1735  switch (Type) {
1736  default:
1737    assert(0 && "Unexpected type for Darwin CC1 tool.");
1738  case types::TY_Asm:
1739  case types::TY_C: case types::TY_CHeader:
1740  case types::TY_PP_C: case types::TY_PP_CHeader:
1741    return "cc1";
1742  case types::TY_ObjC: case types::TY_ObjCHeader:
1743  case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
1744    return "cc1obj";
1745  case types::TY_CXX: case types::TY_CXXHeader:
1746  case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
1747    return "cc1plus";
1748  case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
1749  case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
1750    return "cc1objplus";
1751  }
1752}
1753
1754const char *darwin::CC1::getBaseInputName(const ArgList &Args,
1755                                          const InputInfoList &Inputs) {
1756  llvm::sys::Path P(Inputs[0].getBaseInput());
1757  return Args.MakeArgString(P.getLast());
1758}
1759
1760const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
1761                                          const InputInfoList &Inputs) {
1762  const char *Str = getBaseInputName(Args, Inputs);
1763
1764  if (const char *End = strchr(Str, '.'))
1765    return Args.MakeArgString(std::string(Str, End));
1766
1767  return Str;
1768}
1769
1770const char *
1771darwin::CC1::getDependencyFileName(const ArgList &Args,
1772                                   const InputInfoList &Inputs) {
1773  // FIXME: Think about this more.
1774  std::string Res;
1775
1776  if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1777    std::string Str(OutputOpt->getValue(Args));
1778
1779    Res = Str.substr(0, Str.rfind('.'));
1780  } else
1781    Res = darwin::CC1::getBaseInputStem(Args, Inputs);
1782
1783  return Args.MakeArgString(Res + ".d");
1784}
1785
1786void darwin::CC1::AddCC1Args(const ArgList &Args,
1787                             ArgStringList &CmdArgs) const {
1788  const Driver &D = getToolChain().getDriver();
1789
1790  CheckCodeGenerationOptions(D, Args);
1791
1792  // Derived from cc1 spec.
1793  if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
1794      !Args.hasArg(options::OPT_mdynamic_no_pic))
1795    CmdArgs.push_back("-fPIC");
1796
1797  if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1798      getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1799    if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1800      CmdArgs.push_back("-fno-builtin-strcat");
1801    if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1802      CmdArgs.push_back("-fno-builtin-strcpy");
1803  }
1804
1805  // gcc has some code here to deal with when no -mmacosx-version-min
1806  // and no -miphoneos-version-min is present, but this never happens
1807  // due to tool chain specific argument translation.
1808
1809  if (Args.hasArg(options::OPT_g_Flag) &&
1810      !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
1811    CmdArgs.push_back("-feliminate-unused-debug-symbols");
1812}
1813
1814void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1815                                    const InputInfoList &Inputs,
1816                                    const ArgStringList &OutputArgs) const {
1817  const Driver &D = getToolChain().getDriver();
1818
1819  // Derived from cc1_options spec.
1820  if (Args.hasArg(options::OPT_fast) ||
1821      Args.hasArg(options::OPT_fastf) ||
1822      Args.hasArg(options::OPT_fastcp))
1823    CmdArgs.push_back("-O3");
1824
1825  if (Arg *A = Args.getLastArg(options::OPT_pg))
1826    if (Args.hasArg(options::OPT_fomit_frame_pointer))
1827      D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1828        << A->getAsString(Args) << "-fomit-frame-pointer";
1829
1830  AddCC1Args(Args, CmdArgs);
1831
1832  if (!Args.hasArg(options::OPT_Q))
1833    CmdArgs.push_back("-quiet");
1834
1835  CmdArgs.push_back("-dumpbase");
1836  CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1837
1838  Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
1839
1840  Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
1841  Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
1842
1843  // FIXME: The goal is to use the user provided -o if that is our
1844  // final output, otherwise to drive from the original input
1845  // name. Find a clean way to go about this.
1846  if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
1847      Args.hasArg(options::OPT_o)) {
1848    Arg *OutputOpt = Args.getLastArg(options::OPT_o);
1849    CmdArgs.push_back("-auxbase-strip");
1850    CmdArgs.push_back(OutputOpt->getValue(Args));
1851  } else {
1852    CmdArgs.push_back("-auxbase");
1853    CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
1854  }
1855
1856  Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
1857
1858  Args.AddAllArgs(CmdArgs, options::OPT_O);
1859  // FIXME: -Wall is getting some special treatment. Investigate.
1860  Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1861  Args.AddLastArg(CmdArgs, options::OPT_w);
1862  Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
1863                  options::OPT_trigraphs);
1864  if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1865    // Honor -std-default.
1866    Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1867                              "-std=", /*Joined=*/true);
1868  }
1869
1870  if (Args.hasArg(options::OPT_v))
1871    CmdArgs.push_back("-version");
1872  if (Args.hasArg(options::OPT_pg))
1873    CmdArgs.push_back("-p");
1874  Args.AddLastArg(CmdArgs, options::OPT_p);
1875
1876  // The driver treats -fsyntax-only specially.
1877  if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1878      getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1879    // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
1880    // used to inhibit the default -fno-builtin-str{cat,cpy}.
1881    //
1882    // FIXME: Should we grow a better way to deal with "removing" args?
1883    for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
1884                                               options::OPT_fsyntax_only),
1885           ie = Args.filtered_end(); it != ie; ++it) {
1886      if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
1887          !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
1888        (*it)->claim();
1889        (*it)->render(Args, CmdArgs);
1890      }
1891    }
1892  } else
1893    Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
1894
1895  Args.AddAllArgs(CmdArgs, options::OPT_undef);
1896  if (Args.hasArg(options::OPT_Qn))
1897    CmdArgs.push_back("-fno-ident");
1898
1899  // FIXME: This isn't correct.
1900  //Args.AddLastArg(CmdArgs, options::OPT__help)
1901  //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
1902
1903  CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1904
1905  // FIXME: Still don't get what is happening here. Investigate.
1906  Args.AddAllArgs(CmdArgs, options::OPT__param);
1907
1908  if (Args.hasArg(options::OPT_fmudflap) ||
1909      Args.hasArg(options::OPT_fmudflapth)) {
1910    CmdArgs.push_back("-fno-builtin");
1911    CmdArgs.push_back("-fno-merge-constants");
1912  }
1913
1914  if (Args.hasArg(options::OPT_coverage)) {
1915    CmdArgs.push_back("-fprofile-arcs");
1916    CmdArgs.push_back("-ftest-coverage");
1917  }
1918
1919  if (types::isCXX(Inputs[0].getType()))
1920    CmdArgs.push_back("-D__private_extern__=extern");
1921}
1922
1923void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1924                                    const InputInfoList &Inputs,
1925                                    const ArgStringList &OutputArgs) const {
1926  // Derived from cpp_options
1927  AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
1928
1929  CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1930
1931  AddCC1Args(Args, CmdArgs);
1932
1933  // NOTE: The code below has some commonality with cpp_options, but
1934  // in classic gcc style ends up sending things in different
1935  // orders. This may be a good merge candidate once we drop pedantic
1936  // compatibility.
1937
1938  Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
1939  Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
1940                  options::OPT_trigraphs);
1941  if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1942    // Honor -std-default.
1943    Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1944                              "-std=", /*Joined=*/true);
1945  }
1946  Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1947  Args.AddLastArg(CmdArgs, options::OPT_w);
1948
1949  // The driver treats -fsyntax-only specially.
1950  Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
1951
1952  if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
1953      !Args.hasArg(options::OPT_fno_working_directory))
1954    CmdArgs.push_back("-fworking-directory");
1955
1956  Args.AddAllArgs(CmdArgs, options::OPT_O);
1957  Args.AddAllArgs(CmdArgs, options::OPT_undef);
1958  if (Args.hasArg(options::OPT_save_temps))
1959    CmdArgs.push_back("-fpch-preprocess");
1960}
1961
1962void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
1963                                          ArgStringList &CmdArgs,
1964                                          const InputInfoList &Inputs) const {
1965  const Driver &D = getToolChain().getDriver();
1966
1967  CheckPreprocessingOptions(D, Args);
1968
1969  // Derived from cpp_unique_options.
1970  // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
1971  Args.AddLastArg(CmdArgs, options::OPT_C);
1972  Args.AddLastArg(CmdArgs, options::OPT_CC);
1973  if (!Args.hasArg(options::OPT_Q))
1974    CmdArgs.push_back("-quiet");
1975  Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
1976  Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
1977  Args.AddLastArg(CmdArgs, options::OPT_v);
1978  Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
1979  Args.AddLastArg(CmdArgs, options::OPT_P);
1980
1981  // FIXME: Handle %I properly.
1982  if (getToolChain().getArchName() == "x86_64") {
1983    CmdArgs.push_back("-imultilib");
1984    CmdArgs.push_back("x86_64");
1985  }
1986
1987  if (Args.hasArg(options::OPT_MD)) {
1988    CmdArgs.push_back("-MD");
1989    CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1990  }
1991
1992  if (Args.hasArg(options::OPT_MMD)) {
1993    CmdArgs.push_back("-MMD");
1994    CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1995  }
1996
1997  Args.AddLastArg(CmdArgs, options::OPT_M);
1998  Args.AddLastArg(CmdArgs, options::OPT_MM);
1999  Args.AddAllArgs(CmdArgs, options::OPT_MF);
2000  Args.AddLastArg(CmdArgs, options::OPT_MG);
2001  Args.AddLastArg(CmdArgs, options::OPT_MP);
2002  Args.AddAllArgs(CmdArgs, options::OPT_MQ);
2003  Args.AddAllArgs(CmdArgs, options::OPT_MT);
2004  if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
2005      (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
2006    if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
2007      CmdArgs.push_back("-MQ");
2008      CmdArgs.push_back(OutputOpt->getValue(Args));
2009    }
2010  }
2011
2012  Args.AddLastArg(CmdArgs, options::OPT_remap);
2013  if (Args.hasArg(options::OPT_g3))
2014    CmdArgs.push_back("-dD");
2015  Args.AddLastArg(CmdArgs, options::OPT_H);
2016
2017  AddCPPArgs(Args, CmdArgs);
2018
2019  Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2020  Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2021
2022  for (InputInfoList::const_iterator
2023         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2024    const InputInfo &II = *it;
2025
2026    if (II.isPipe())
2027      CmdArgs.push_back("-");
2028    else
2029      CmdArgs.push_back(II.getFilename());
2030  }
2031
2032  Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2033                       options::OPT_Xpreprocessor);
2034
2035  if (Args.hasArg(options::OPT_fmudflap)) {
2036    CmdArgs.push_back("-D_MUDFLAP");
2037    CmdArgs.push_back("-include");
2038    CmdArgs.push_back("mf-runtime.h");
2039  }
2040
2041  if (Args.hasArg(options::OPT_fmudflapth)) {
2042    CmdArgs.push_back("-D_MUDFLAP");
2043    CmdArgs.push_back("-D_MUDFLAPTH");
2044    CmdArgs.push_back("-include");
2045    CmdArgs.push_back("mf-runtime.h");
2046  }
2047}
2048
2049void darwin::CC1::AddCPPArgs(const ArgList &Args,
2050                             ArgStringList &CmdArgs) const {
2051  // Derived from cpp spec.
2052
2053  if (Args.hasArg(options::OPT_static)) {
2054    // The gcc spec is broken here, it refers to dynamic but
2055    // that has been translated. Start by being bug compatible.
2056
2057    // if (!Args.hasArg(arglist.parser.dynamicOption))
2058    CmdArgs.push_back("-D__STATIC__");
2059  } else
2060    CmdArgs.push_back("-D__DYNAMIC__");
2061
2062  if (Args.hasArg(options::OPT_pthread))
2063    CmdArgs.push_back("-D_REENTRANT");
2064}
2065
2066void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
2067                                      Job &Dest, const InputInfo &Output,
2068                                      const InputInfoList &Inputs,
2069                                      const ArgList &Args,
2070                                      const char *LinkingOutput) const {
2071  ArgStringList CmdArgs;
2072
2073  assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2074
2075  CmdArgs.push_back("-E");
2076
2077  if (Args.hasArg(options::OPT_traditional) ||
2078      Args.hasArg(options::OPT_traditional_cpp))
2079    CmdArgs.push_back("-traditional-cpp");
2080
2081  ArgStringList OutputArgs;
2082  if (Output.isFilename()) {
2083    OutputArgs.push_back("-o");
2084    OutputArgs.push_back(Output.getFilename());
2085  } else {
2086    assert(Output.isPipe() && "Unexpected CC1 output.");
2087  }
2088
2089  if (Args.hasArg(options::OPT_E)) {
2090    AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2091  } else {
2092    AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2093    CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2094  }
2095
2096  Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2097
2098  const char *CC1Name = getCC1Name(Inputs[0].getType());
2099  const char *Exec =
2100    Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
2101  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2102}
2103
2104void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
2105                                   Job &Dest, const InputInfo &Output,
2106                                   const InputInfoList &Inputs,
2107                                   const ArgList &Args,
2108                                   const char *LinkingOutput) const {
2109  const Driver &D = getToolChain().getDriver();
2110  ArgStringList CmdArgs;
2111
2112  assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2113
2114  types::ID InputType = Inputs[0].getType();
2115  const Arg *A;
2116  if ((A = Args.getLastArg(options::OPT_traditional)))
2117    D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2118      << A->getAsString(Args) << "-E";
2119
2120  if (JA.getType() == types::TY_LLVM_IR ||
2121      JA.getType() == types::TY_LTO_IR)
2122    CmdArgs.push_back("-emit-llvm");
2123  else if (JA.getType() == types::TY_LLVM_BC ||
2124           JA.getType() == types::TY_LTO_BC)
2125    CmdArgs.push_back("-emit-llvm-bc");
2126  else if (Output.getType() == types::TY_AST)
2127    D.Diag(clang::diag::err_drv_no_ast_support)
2128      << getToolChain().getTripleString();
2129  else if (JA.getType() != types::TY_PP_Asm &&
2130           JA.getType() != types::TY_PCH)
2131    D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2132      << getTypeName(JA.getType());
2133
2134  ArgStringList OutputArgs;
2135  if (Output.getType() != types::TY_PCH) {
2136    OutputArgs.push_back("-o");
2137    if (Output.isPipe())
2138      OutputArgs.push_back("-");
2139    else if (Output.isNothing())
2140      OutputArgs.push_back("/dev/null");
2141    else
2142      OutputArgs.push_back(Output.getFilename());
2143  }
2144
2145  // There is no need for this level of compatibility, but it makes
2146  // diffing easier.
2147  bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2148                          Args.hasArg(options::OPT_S));
2149
2150  if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
2151    AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
2152    if (OutputArgsEarly) {
2153      AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2154    } else {
2155      AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2156      CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2157    }
2158  } else {
2159    CmdArgs.push_back("-fpreprocessed");
2160
2161    for (InputInfoList::const_iterator
2162           it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2163      const InputInfo &II = *it;
2164
2165      // Reject AST inputs.
2166      if (II.getType() == types::TY_AST) {
2167        D.Diag(clang::diag::err_drv_no_ast_support)
2168          << getToolChain().getTripleString();
2169        return;
2170      }
2171
2172      if (II.isPipe())
2173        CmdArgs.push_back("-");
2174      else
2175        CmdArgs.push_back(II.getFilename());
2176    }
2177
2178    if (OutputArgsEarly) {
2179      AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2180    } else {
2181      AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2182      CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2183    }
2184  }
2185
2186  if (Output.getType() == types::TY_PCH) {
2187    assert(Output.isFilename() && "Invalid PCH output.");
2188
2189    CmdArgs.push_back("-o");
2190    // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2191    // to be a good reason.
2192    CmdArgs.push_back("/dev/null");
2193
2194    CmdArgs.push_back("--output-pch=");
2195    CmdArgs.push_back(Output.getFilename());
2196  }
2197
2198  const char *CC1Name = getCC1Name(Inputs[0].getType());
2199  const char *Exec =
2200    Args.MakeArgString(getToolChain().GetProgramPath(CC1Name));
2201  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2202}
2203
2204void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2205                                    Job &Dest, const InputInfo &Output,
2206                                    const InputInfoList &Inputs,
2207                                    const ArgList &Args,
2208                                    const char *LinkingOutput) const {
2209  ArgStringList CmdArgs;
2210
2211  assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2212  const InputInfo &Input = Inputs[0];
2213
2214  // Bit of a hack, this is only used for original inputs.
2215  //
2216  // FIXME: This is broken for preprocessed .s inputs.
2217  if (Input.isFilename() &&
2218      strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
2219    if (Args.hasArg(options::OPT_gstabs))
2220      CmdArgs.push_back("--gstabs");
2221    else if (Args.hasArg(options::OPT_g_Group))
2222      CmdArgs.push_back("--gdwarf2");
2223  }
2224
2225  // Derived from asm spec.
2226  AddDarwinArch(Args, CmdArgs);
2227
2228  if (!getDarwinToolChain().isTargetIPhoneOS() ||
2229      Args.hasArg(options::OPT_force__cpusubtype__ALL))
2230    CmdArgs.push_back("-force_cpusubtype_ALL");
2231
2232  if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
2233      (Args.hasArg(options::OPT_mkernel) ||
2234       Args.hasArg(options::OPT_static) ||
2235       Args.hasArg(options::OPT_fapple_kext)))
2236    CmdArgs.push_back("-static");
2237
2238  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2239                       options::OPT_Xassembler);
2240
2241  assert(Output.isFilename() && "Unexpected lipo output.");
2242  CmdArgs.push_back("-o");
2243  CmdArgs.push_back(Output.getFilename());
2244
2245  if (Input.isPipe()) {
2246    CmdArgs.push_back("-");
2247  } else {
2248    assert(Input.isFilename() && "Invalid input.");
2249    CmdArgs.push_back(Input.getFilename());
2250  }
2251
2252  // asm_final spec is empty.
2253
2254  const char *Exec =
2255    Args.MakeArgString(getToolChain().GetProgramPath("as"));
2256  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2257}
2258
2259void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
2260                                       ArgStringList &CmdArgs) const {
2261  llvm::StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
2262
2263  // Derived from darwin_arch spec.
2264  CmdArgs.push_back("-arch");
2265  CmdArgs.push_back(Args.MakeArgString(ArchName));
2266
2267  // FIXME: Is this needed anymore?
2268  if (ArchName == "arm")
2269    CmdArgs.push_back("-force_cpusubtype_ALL");
2270}
2271
2272void darwin::Link::AddLinkArgs(const ArgList &Args,
2273                               ArgStringList &CmdArgs) const {
2274  const Driver &D = getToolChain().getDriver();
2275
2276  // Derived from the "link" spec.
2277  Args.AddAllArgs(CmdArgs, options::OPT_static);
2278  if (!Args.hasArg(options::OPT_static))
2279    CmdArgs.push_back("-dynamic");
2280  if (Args.hasArg(options::OPT_fgnu_runtime)) {
2281    // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
2282    // here. How do we wish to handle such things?
2283  }
2284
2285  if (!Args.hasArg(options::OPT_dynamiclib)) {
2286    AddDarwinArch(Args, CmdArgs);
2287    // FIXME: Why do this only on this path?
2288    Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
2289
2290    Args.AddLastArg(CmdArgs, options::OPT_bundle);
2291    Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
2292    Args.AddAllArgs(CmdArgs, options::OPT_client__name);
2293
2294    Arg *A;
2295    if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
2296        (A = Args.getLastArg(options::OPT_current__version)) ||
2297        (A = Args.getLastArg(options::OPT_install__name)))
2298      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2299        << A->getAsString(Args) << "-dynamiclib";
2300
2301    Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
2302    Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
2303    Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
2304  } else {
2305    CmdArgs.push_back("-dylib");
2306
2307    Arg *A;
2308    if ((A = Args.getLastArg(options::OPT_bundle)) ||
2309        (A = Args.getLastArg(options::OPT_bundle__loader)) ||
2310        (A = Args.getLastArg(options::OPT_client__name)) ||
2311        (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
2312        (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
2313        (A = Args.getLastArg(options::OPT_private__bundle)))
2314      D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2315        << A->getAsString(Args) << "-dynamiclib";
2316
2317    Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
2318                              "-dylib_compatibility_version");
2319    Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
2320                              "-dylib_current_version");
2321
2322    AddDarwinArch(Args, CmdArgs);
2323
2324    Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
2325                              "-dylib_install_name");
2326  }
2327
2328  Args.AddLastArg(CmdArgs, options::OPT_all__load);
2329  Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
2330  Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
2331  if (getDarwinToolChain().isTargetIPhoneOS())
2332    Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
2333  Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
2334  Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
2335  Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
2336  Args.AddLastArg(CmdArgs, options::OPT_dynamic);
2337  Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
2338  Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
2339  Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
2340  Args.AddAllArgs(CmdArgs, options::OPT_image__base);
2341  Args.AddAllArgs(CmdArgs, options::OPT_init);
2342
2343  // Adding all arguments doesn't make sense here but this is what gcc does. One
2344  // of this should always be present thanks to argument translation.
2345  assert((Args.hasArg(options::OPT_mmacosx_version_min_EQ) ||
2346          Args.hasArg(options::OPT_miphoneos_version_min_EQ)) &&
2347         "Missing version argument (lost in translation)?");
2348  Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
2349                            "-macosx_version_min");
2350  Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
2351                            "-iphoneos_version_min");
2352  Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
2353  Args.AddLastArg(CmdArgs, options::OPT_multi__module);
2354  Args.AddLastArg(CmdArgs, options::OPT_single__module);
2355  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
2356  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
2357
2358  if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
2359                                     options::OPT_fno_pie,
2360                                     options::OPT_fno_PIE)) {
2361    if (A->getOption().matches(options::OPT_fpie) ||
2362        A->getOption().matches(options::OPT_fPIE))
2363      CmdArgs.push_back("-pie");
2364    else
2365      CmdArgs.push_back("-no_pie");
2366  }
2367
2368  Args.AddLastArg(CmdArgs, options::OPT_prebind);
2369  Args.AddLastArg(CmdArgs, options::OPT_noprebind);
2370  Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
2371  Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
2372  Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
2373  Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
2374  Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
2375  Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
2376  Args.AddAllArgs(CmdArgs, options::OPT_segprot);
2377  Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
2378  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
2379  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
2380  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
2381  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
2382  Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
2383  Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
2384
2385  Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
2386  if (getDarwinToolChain().isTargetIPhoneOS()) {
2387    if (!Args.hasArg(options::OPT_isysroot)) {
2388      CmdArgs.push_back("-syslibroot");
2389      CmdArgs.push_back("/Developer/SDKs/Extra");
2390    }
2391  }
2392
2393  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
2394  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
2395  Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
2396  Args.AddAllArgs(CmdArgs, options::OPT_undefined);
2397  Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
2398  Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
2399  Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
2400  Args.AddAllArgs(CmdArgs, options::OPT_y);
2401  Args.AddLastArg(CmdArgs, options::OPT_w);
2402  Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
2403  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
2404  Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
2405  Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
2406  Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
2407  Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
2408  Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
2409  Args.AddLastArg(CmdArgs, options::OPT_whyload);
2410  Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
2411  Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
2412  Args.AddLastArg(CmdArgs, options::OPT_dylinker);
2413  Args.AddLastArg(CmdArgs, options::OPT_Mach);
2414}
2415
2416void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
2417                                Job &Dest, const InputInfo &Output,
2418                                const InputInfoList &Inputs,
2419                                const ArgList &Args,
2420                                const char *LinkingOutput) const {
2421  assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
2422
2423  // The logic here is derived from gcc's behavior; most of which
2424  // comes from specs (starting with link_command). Consult gcc for
2425  // more information.
2426  ArgStringList CmdArgs;
2427
2428  // I'm not sure why this particular decomposition exists in gcc, but
2429  // we follow suite for ease of comparison.
2430  AddLinkArgs(Args, CmdArgs);
2431
2432  Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
2433  Args.AddAllArgs(CmdArgs, options::OPT_s);
2434  Args.AddAllArgs(CmdArgs, options::OPT_t);
2435  Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2436  Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
2437  Args.AddAllArgs(CmdArgs, options::OPT_A);
2438  Args.AddLastArg(CmdArgs, options::OPT_e);
2439  Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
2440  Args.AddAllArgs(CmdArgs, options::OPT_r);
2441
2442  CmdArgs.push_back("-o");
2443  CmdArgs.push_back(Output.getFilename());
2444
2445  if (!Args.hasArg(options::OPT_A) &&
2446      !Args.hasArg(options::OPT_nostdlib) &&
2447      !Args.hasArg(options::OPT_nostartfiles)) {
2448    // Derived from startfile spec.
2449    if (Args.hasArg(options::OPT_dynamiclib)) {
2450      // Derived from darwin_dylib1 spec.
2451      if (getDarwinToolChain().isTargetIPhoneOS()) {
2452        if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2453          CmdArgs.push_back("-ldylib1.o");
2454      } else {
2455        if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2456          CmdArgs.push_back("-ldylib1.o");
2457        else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2458          CmdArgs.push_back("-ldylib1.10.5.o");
2459      }
2460    } else {
2461      if (Args.hasArg(options::OPT_bundle)) {
2462        if (!Args.hasArg(options::OPT_static)) {
2463          // Derived from darwin_bundle1 spec.
2464          if (getDarwinToolChain().isTargetIPhoneOS()) {
2465            if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2466              CmdArgs.push_back("-lbundle1.o");
2467          } else {
2468            if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2469              CmdArgs.push_back("-lbundle1.o");
2470          }
2471        }
2472      } else {
2473        if (Args.hasArg(options::OPT_pg)) {
2474          if (Args.hasArg(options::OPT_static) ||
2475              Args.hasArg(options::OPT_object) ||
2476              Args.hasArg(options::OPT_preload)) {
2477            CmdArgs.push_back("-lgcrt0.o");
2478          } else {
2479            CmdArgs.push_back("-lgcrt1.o");
2480
2481            // darwin_crt2 spec is empty.
2482          }
2483        } else {
2484          if (Args.hasArg(options::OPT_static) ||
2485              Args.hasArg(options::OPT_object) ||
2486              Args.hasArg(options::OPT_preload)) {
2487            CmdArgs.push_back("-lcrt0.o");
2488          } else {
2489            // Derived from darwin_crt1 spec.
2490            if (getDarwinToolChain().isTargetIPhoneOS()) {
2491              if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2492                CmdArgs.push_back("-lcrt1.o");
2493              else
2494                CmdArgs.push_back("-lcrt1.3.1.o");
2495            } else {
2496              if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2497                CmdArgs.push_back("-lcrt1.o");
2498              else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2499                CmdArgs.push_back("-lcrt1.10.5.o");
2500              else
2501                CmdArgs.push_back("-lcrt1.10.6.o");
2502
2503              // darwin_crt2 spec is empty.
2504            }
2505          }
2506        }
2507      }
2508    }
2509
2510    if (!getDarwinToolChain().isTargetIPhoneOS() &&
2511        Args.hasArg(options::OPT_shared_libgcc) &&
2512        getDarwinToolChain().isMacosxVersionLT(10, 5)) {
2513      const char *Str =
2514        Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
2515      CmdArgs.push_back(Str);
2516    }
2517  }
2518
2519  Args.AddAllArgs(CmdArgs, options::OPT_L);
2520
2521  if (Args.hasArg(options::OPT_fopenmp))
2522    // This is more complicated in gcc...
2523    CmdArgs.push_back("-lgomp");
2524
2525  getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
2526
2527  for (InputInfoList::const_iterator
2528         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2529    const InputInfo &II = *it;
2530    if (II.isFilename())
2531      CmdArgs.push_back(II.getFilename());
2532    else
2533      II.getInputArg().renderAsInput(Args, CmdArgs);
2534  }
2535
2536  if (LinkingOutput) {
2537    CmdArgs.push_back("-arch_multiple");
2538    CmdArgs.push_back("-final_output");
2539    CmdArgs.push_back(LinkingOutput);
2540  }
2541
2542  if (Args.hasArg(options::OPT_fprofile_arcs) ||
2543      Args.hasArg(options::OPT_fprofile_generate) ||
2544      Args.hasArg(options::OPT_fcreate_profile) ||
2545      Args.hasArg(options::OPT_coverage))
2546    CmdArgs.push_back("-lgcov");
2547
2548  if (Args.hasArg(options::OPT_fnested_functions))
2549    CmdArgs.push_back("-allow_stack_execute");
2550
2551  if (!Args.hasArg(options::OPT_nostdlib) &&
2552      !Args.hasArg(options::OPT_nodefaultlibs)) {
2553    // FIXME: g++ is more complicated here, it tries to put -lstdc++
2554    // before -lm, for example.
2555    if (getToolChain().getDriver().CCCIsCXX)
2556      CmdArgs.push_back("-lstdc++");
2557
2558    // link_ssp spec is empty.
2559
2560    // Let the tool chain choose which runtime library to link.
2561    getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
2562  }
2563
2564  if (!Args.hasArg(options::OPT_A) &&
2565      !Args.hasArg(options::OPT_nostdlib) &&
2566      !Args.hasArg(options::OPT_nostartfiles)) {
2567    // endfile_spec is empty.
2568  }
2569
2570  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2571  Args.AddAllArgs(CmdArgs, options::OPT_F);
2572
2573  const char *Exec =
2574    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
2575  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2576}
2577
2578void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
2579                                Job &Dest, const InputInfo &Output,
2580                                const InputInfoList &Inputs,
2581                                const ArgList &Args,
2582                                const char *LinkingOutput) const {
2583  ArgStringList CmdArgs;
2584
2585  CmdArgs.push_back("-create");
2586  assert(Output.isFilename() && "Unexpected lipo output.");
2587
2588  CmdArgs.push_back("-output");
2589  CmdArgs.push_back(Output.getFilename());
2590
2591  for (InputInfoList::const_iterator
2592         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2593    const InputInfo &II = *it;
2594    assert(II.isFilename() && "Unexpected lipo input.");
2595    CmdArgs.push_back(II.getFilename());
2596  }
2597  const char *Exec =
2598    Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
2599  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2600}
2601
2602void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
2603                                    Job &Dest, const InputInfo &Output,
2604                                    const InputInfoList &Inputs,
2605                                    const ArgList &Args,
2606                                    const char *LinkingOutput) const {
2607  ArgStringList CmdArgs;
2608
2609  assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2610  const InputInfo &Input = Inputs[0];
2611  assert(Input.isFilename() && "Unexpected dsymutil input.");
2612  CmdArgs.push_back(Input.getFilename());
2613
2614  CmdArgs.push_back("-o");
2615  CmdArgs.push_back(Output.getFilename());
2616
2617  const char *Exec =
2618    Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
2619  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2620}
2621
2622void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2623                                      Job &Dest, const InputInfo &Output,
2624                                      const InputInfoList &Inputs,
2625                                      const ArgList &Args,
2626                                      const char *LinkingOutput) const {
2627  ArgStringList CmdArgs;
2628
2629  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2630                       options::OPT_Xassembler);
2631
2632  CmdArgs.push_back("-o");
2633  if (Output.isPipe())
2634    CmdArgs.push_back("-");
2635  else
2636    CmdArgs.push_back(Output.getFilename());
2637
2638  for (InputInfoList::const_iterator
2639         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2640    const InputInfo &II = *it;
2641    if (II.isPipe())
2642      CmdArgs.push_back("-");
2643    else
2644      CmdArgs.push_back(II.getFilename());
2645  }
2646
2647  const char *Exec =
2648    Args.MakeArgString(getToolChain().GetProgramPath("gas"));
2649  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2650}
2651
2652void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
2653                                  Job &Dest, const InputInfo &Output,
2654                                  const InputInfoList &Inputs,
2655                                  const ArgList &Args,
2656                                  const char *LinkingOutput) const {
2657  const Driver &D = getToolChain().getDriver();
2658  ArgStringList CmdArgs;
2659
2660  if ((!Args.hasArg(options::OPT_nostdlib)) &&
2661      (!Args.hasArg(options::OPT_shared))) {
2662    CmdArgs.push_back("-e");
2663    CmdArgs.push_back("_start");
2664  }
2665
2666  if (Args.hasArg(options::OPT_static)) {
2667    CmdArgs.push_back("-Bstatic");
2668    CmdArgs.push_back("-dn");
2669  } else {
2670//    CmdArgs.push_back("--eh-frame-hdr");
2671    CmdArgs.push_back("-Bdynamic");
2672    if (Args.hasArg(options::OPT_shared)) {
2673      CmdArgs.push_back("-shared");
2674    } else {
2675      CmdArgs.push_back("--dynamic-linker");
2676      CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
2677    }
2678  }
2679
2680  if (Output.isPipe()) {
2681    CmdArgs.push_back("-o");
2682    CmdArgs.push_back("-");
2683  } else if (Output.isFilename()) {
2684    CmdArgs.push_back("-o");
2685    CmdArgs.push_back(Output.getFilename());
2686  } else {
2687    assert(Output.isNothing() && "Invalid output.");
2688  }
2689
2690  if (!Args.hasArg(options::OPT_nostdlib) &&
2691      !Args.hasArg(options::OPT_nostartfiles)) {
2692    if (!Args.hasArg(options::OPT_shared)) {
2693      CmdArgs.push_back(Args.MakeArgString(
2694                                getToolChain().GetFilePath("crt1.o")));
2695      CmdArgs.push_back(Args.MakeArgString(
2696                                getToolChain().GetFilePath("crti.o")));
2697      CmdArgs.push_back(Args.MakeArgString(
2698                                getToolChain().GetFilePath("crtbegin.o")));
2699    } else {
2700      CmdArgs.push_back(Args.MakeArgString(
2701                                getToolChain().GetFilePath("crti.o")));
2702    }
2703    CmdArgs.push_back(Args.MakeArgString(
2704                                getToolChain().GetFilePath("crtn.o")));
2705  }
2706
2707  CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
2708                                       + getToolChain().getTripleString()
2709                                       + "/4.2.4"));
2710
2711  Args.AddAllArgs(CmdArgs, options::OPT_L);
2712  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2713  Args.AddAllArgs(CmdArgs, options::OPT_e);
2714
2715  for (InputInfoList::const_iterator
2716         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2717    const InputInfo &II = *it;
2718
2719    // Don't try to pass LLVM inputs to a generic gcc.
2720    if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2721        II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
2722      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2723        << getToolChain().getTripleString();
2724
2725    if (II.isPipe())
2726      CmdArgs.push_back("-");
2727    else if (II.isFilename())
2728      CmdArgs.push_back(II.getFilename());
2729    else
2730      II.getInputArg().renderAsInput(Args, CmdArgs);
2731  }
2732
2733  if (!Args.hasArg(options::OPT_nostdlib) &&
2734      !Args.hasArg(options::OPT_nodefaultlibs)) {
2735    // FIXME: For some reason GCC passes -lgcc before adding
2736    // the default system libraries. Just mimic this for now.
2737    CmdArgs.push_back("-lgcc");
2738
2739    if (Args.hasArg(options::OPT_pthread))
2740      CmdArgs.push_back("-pthread");
2741    if (!Args.hasArg(options::OPT_shared))
2742      CmdArgs.push_back("-lc");
2743    CmdArgs.push_back("-lgcc");
2744  }
2745
2746  if (!Args.hasArg(options::OPT_nostdlib) &&
2747      !Args.hasArg(options::OPT_nostartfiles)) {
2748    if (!Args.hasArg(options::OPT_shared))
2749      CmdArgs.push_back(Args.MakeArgString(
2750                                getToolChain().GetFilePath("crtend.o")));
2751  }
2752
2753  const char *Exec =
2754    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
2755  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2756}
2757
2758void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2759                                     Job &Dest, const InputInfo &Output,
2760                                     const InputInfoList &Inputs,
2761                                     const ArgList &Args,
2762                                     const char *LinkingOutput) const {
2763  ArgStringList CmdArgs;
2764
2765  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2766                       options::OPT_Xassembler);
2767
2768  CmdArgs.push_back("-o");
2769  if (Output.isPipe())
2770    CmdArgs.push_back("-");
2771  else
2772    CmdArgs.push_back(Output.getFilename());
2773
2774  for (InputInfoList::const_iterator
2775         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2776    const InputInfo &II = *it;
2777    if (II.isPipe())
2778      CmdArgs.push_back("-");
2779    else
2780      CmdArgs.push_back(II.getFilename());
2781  }
2782
2783  const char *Exec =
2784    Args.MakeArgString(getToolChain().GetProgramPath("as"));
2785  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2786}
2787
2788void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
2789                                 Job &Dest, const InputInfo &Output,
2790                                 const InputInfoList &Inputs,
2791                                 const ArgList &Args,
2792                                 const char *LinkingOutput) const {
2793  const Driver &D = getToolChain().getDriver();
2794  ArgStringList CmdArgs;
2795
2796  if ((!Args.hasArg(options::OPT_nostdlib)) &&
2797      (!Args.hasArg(options::OPT_shared))) {
2798    CmdArgs.push_back("-e");
2799    CmdArgs.push_back("__start");
2800  }
2801
2802  if (Args.hasArg(options::OPT_static)) {
2803    CmdArgs.push_back("-Bstatic");
2804  } else {
2805    CmdArgs.push_back("--eh-frame-hdr");
2806    CmdArgs.push_back("-Bdynamic");
2807    if (Args.hasArg(options::OPT_shared)) {
2808      CmdArgs.push_back("-shared");
2809    } else {
2810      CmdArgs.push_back("-dynamic-linker");
2811      CmdArgs.push_back("/usr/libexec/ld.so");
2812    }
2813  }
2814
2815  if (Output.isPipe()) {
2816    CmdArgs.push_back("-o");
2817    CmdArgs.push_back("-");
2818  } else if (Output.isFilename()) {
2819    CmdArgs.push_back("-o");
2820    CmdArgs.push_back(Output.getFilename());
2821  } else {
2822    assert(Output.isNothing() && "Invalid output.");
2823  }
2824
2825  if (!Args.hasArg(options::OPT_nostdlib) &&
2826      !Args.hasArg(options::OPT_nostartfiles)) {
2827    if (!Args.hasArg(options::OPT_shared)) {
2828      CmdArgs.push_back(Args.MakeArgString(
2829                              getToolChain().GetFilePath("crt0.o")));
2830      CmdArgs.push_back(Args.MakeArgString(
2831                              getToolChain().GetFilePath("crtbegin.o")));
2832    } else {
2833      CmdArgs.push_back(Args.MakeArgString(
2834                              getToolChain().GetFilePath("crtbeginS.o")));
2835    }
2836  }
2837
2838  std::string Triple = getToolChain().getTripleString();
2839  if (Triple.substr(0, 6) == "x86_64")
2840    Triple.replace(0, 6, "amd64");
2841  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
2842                                       "/3.3.5"));
2843
2844  Args.AddAllArgs(CmdArgs, options::OPT_L);
2845  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2846  Args.AddAllArgs(CmdArgs, options::OPT_e);
2847
2848  for (InputInfoList::const_iterator
2849         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2850    const InputInfo &II = *it;
2851
2852    // Don't try to pass LLVM inputs to a generic gcc.
2853    if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2854        II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
2855      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2856        << getToolChain().getTripleString();
2857
2858    if (II.isPipe())
2859      CmdArgs.push_back("-");
2860    else if (II.isFilename())
2861      CmdArgs.push_back(II.getFilename());
2862    else
2863      II.getInputArg().renderAsInput(Args, CmdArgs);
2864  }
2865
2866  if (!Args.hasArg(options::OPT_nostdlib) &&
2867      !Args.hasArg(options::OPT_nodefaultlibs)) {
2868    // FIXME: For some reason GCC passes -lgcc before adding
2869    // the default system libraries. Just mimic this for now.
2870    CmdArgs.push_back("-lgcc");
2871
2872    if (Args.hasArg(options::OPT_pthread))
2873      CmdArgs.push_back("-pthread");
2874    if (!Args.hasArg(options::OPT_shared))
2875      CmdArgs.push_back("-lc");
2876    CmdArgs.push_back("-lgcc");
2877  }
2878
2879  if (!Args.hasArg(options::OPT_nostdlib) &&
2880      !Args.hasArg(options::OPT_nostartfiles)) {
2881    if (!Args.hasArg(options::OPT_shared))
2882      CmdArgs.push_back(Args.MakeArgString(
2883                              getToolChain().GetFilePath("crtend.o")));
2884    else
2885      CmdArgs.push_back(Args.MakeArgString(
2886                              getToolChain().GetFilePath("crtendS.o")));
2887  }
2888
2889  const char *Exec =
2890    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
2891  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2892}
2893
2894void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2895                                     Job &Dest, const InputInfo &Output,
2896                                     const InputInfoList &Inputs,
2897                                     const ArgList &Args,
2898                                     const char *LinkingOutput) const {
2899  ArgStringList CmdArgs;
2900
2901  // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2902  // instruct as in the base system to assemble 32-bit code.
2903  if (getToolChain().getArchName() == "i386")
2904    CmdArgs.push_back("--32");
2905
2906
2907  // Set byte order explicitly
2908  if (getToolChain().getArchName() == "mips")
2909    CmdArgs.push_back("-EB");
2910  else if (getToolChain().getArchName() == "mipsel")
2911    CmdArgs.push_back("-EL");
2912
2913  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2914                       options::OPT_Xassembler);
2915
2916  CmdArgs.push_back("-o");
2917  if (Output.isPipe())
2918    CmdArgs.push_back("-");
2919  else
2920    CmdArgs.push_back(Output.getFilename());
2921
2922  for (InputInfoList::const_iterator
2923         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2924    const InputInfo &II = *it;
2925    if (II.isPipe())
2926      CmdArgs.push_back("-");
2927    else
2928      CmdArgs.push_back(II.getFilename());
2929  }
2930
2931  const char *Exec =
2932    Args.MakeArgString(getToolChain().GetProgramPath("as"));
2933  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2934}
2935
2936void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
2937                                 Job &Dest, const InputInfo &Output,
2938                                 const InputInfoList &Inputs,
2939                                 const ArgList &Args,
2940                                 const char *LinkingOutput) const {
2941  const Driver &D = getToolChain().getDriver();
2942  ArgStringList CmdArgs;
2943
2944  if (Args.hasArg(options::OPT_static)) {
2945    CmdArgs.push_back("-Bstatic");
2946  } else {
2947    CmdArgs.push_back("--eh-frame-hdr");
2948    if (Args.hasArg(options::OPT_shared)) {
2949      CmdArgs.push_back("-Bshareable");
2950    } else {
2951      CmdArgs.push_back("-dynamic-linker");
2952      CmdArgs.push_back("/libexec/ld-elf.so.1");
2953    }
2954  }
2955
2956  // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2957  // instruct ld in the base system to link 32-bit code.
2958  if (getToolChain().getArchName() == "i386") {
2959    CmdArgs.push_back("-m");
2960    CmdArgs.push_back("elf_i386_fbsd");
2961  }
2962
2963  if (Output.isPipe()) {
2964    CmdArgs.push_back("-o");
2965    CmdArgs.push_back("-");
2966  } else if (Output.isFilename()) {
2967    CmdArgs.push_back("-o");
2968    CmdArgs.push_back(Output.getFilename());
2969  } else {
2970    assert(Output.isNothing() && "Invalid output.");
2971  }
2972
2973  if (!Args.hasArg(options::OPT_nostdlib) &&
2974      !Args.hasArg(options::OPT_nostartfiles)) {
2975    if (!Args.hasArg(options::OPT_shared)) {
2976      CmdArgs.push_back(Args.MakeArgString(
2977                              getToolChain().GetFilePath("crt1.o")));
2978      CmdArgs.push_back(Args.MakeArgString(
2979                              getToolChain().GetFilePath("crti.o")));
2980      CmdArgs.push_back(Args.MakeArgString(
2981                              getToolChain().GetFilePath("crtbegin.o")));
2982    } else {
2983      CmdArgs.push_back(Args.MakeArgString(
2984                              getToolChain().GetFilePath("crti.o")));
2985      CmdArgs.push_back(Args.MakeArgString(
2986                              getToolChain().GetFilePath("crtbeginS.o")));
2987    }
2988  }
2989
2990  Args.AddAllArgs(CmdArgs, options::OPT_L);
2991  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2992  Args.AddAllArgs(CmdArgs, options::OPT_e);
2993
2994  for (InputInfoList::const_iterator
2995         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2996    const InputInfo &II = *it;
2997
2998    // Don't try to pass LLVM inputs to a generic gcc.
2999    if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3000        II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3001      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3002        << getToolChain().getTripleString();
3003
3004    if (II.isPipe())
3005      CmdArgs.push_back("-");
3006    else if (II.isFilename())
3007      CmdArgs.push_back(II.getFilename());
3008    else
3009      II.getInputArg().renderAsInput(Args, CmdArgs);
3010  }
3011
3012  if (!Args.hasArg(options::OPT_nostdlib) &&
3013      !Args.hasArg(options::OPT_nodefaultlibs)) {
3014    if (D.CCCIsCXX) {
3015      CmdArgs.push_back("-lstdc++");
3016      CmdArgs.push_back("-lm");
3017    }
3018    // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3019    // the default system libraries. Just mimic this for now.
3020    CmdArgs.push_back("-lgcc");
3021    if (Args.hasArg(options::OPT_static)) {
3022      CmdArgs.push_back("-lgcc_eh");
3023    } else {
3024      CmdArgs.push_back("--as-needed");
3025      CmdArgs.push_back("-lgcc_s");
3026      CmdArgs.push_back("--no-as-needed");
3027    }
3028
3029    if (Args.hasArg(options::OPT_pthread))
3030      CmdArgs.push_back("-lpthread");
3031    CmdArgs.push_back("-lc");
3032
3033    CmdArgs.push_back("-lgcc");
3034    if (Args.hasArg(options::OPT_static)) {
3035      CmdArgs.push_back("-lgcc_eh");
3036    } else {
3037      CmdArgs.push_back("--as-needed");
3038      CmdArgs.push_back("-lgcc_s");
3039      CmdArgs.push_back("--no-as-needed");
3040    }
3041  }
3042
3043  if (!Args.hasArg(options::OPT_nostdlib) &&
3044      !Args.hasArg(options::OPT_nostartfiles)) {
3045    if (!Args.hasArg(options::OPT_shared))
3046      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3047                                                                  "crtend.o")));
3048    else
3049      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3050                                                                 "crtendS.o")));
3051    CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3052                                                                    "crtn.o")));
3053  }
3054
3055  const char *Exec =
3056    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
3057  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
3058}
3059
3060void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3061                                     Job &Dest, const InputInfo &Output,
3062                                     const InputInfoList &Inputs,
3063                                     const ArgList &Args,
3064                                     const char *LinkingOutput) const {
3065  ArgStringList CmdArgs;
3066
3067  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3068                       options::OPT_Xassembler);
3069
3070  CmdArgs.push_back("-o");
3071  if (Output.isPipe())
3072    CmdArgs.push_back("-");
3073  else
3074    CmdArgs.push_back(Output.getFilename());
3075
3076  for (InputInfoList::const_iterator
3077         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3078    const InputInfo &II = *it;
3079    if (II.isPipe())
3080      CmdArgs.push_back("-");
3081    else
3082      CmdArgs.push_back(II.getFilename());
3083  }
3084
3085  const char *Exec =
3086    Args.MakeArgString(getToolChain().GetProgramPath("gas"));
3087  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
3088}
3089
3090void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
3091                                 Job &Dest, const InputInfo &Output,
3092                                 const InputInfoList &Inputs,
3093                                 const ArgList &Args,
3094                                 const char *LinkingOutput) const {
3095  const Driver &D = getToolChain().getDriver();
3096  ArgStringList CmdArgs;
3097
3098  if (Output.isPipe()) {
3099    CmdArgs.push_back("-o");
3100    CmdArgs.push_back("-");
3101  } else if (Output.isFilename()) {
3102    CmdArgs.push_back("-o");
3103    CmdArgs.push_back(Output.getFilename());
3104  } else {
3105    assert(Output.isNothing() && "Invalid output.");
3106  }
3107
3108  if (!Args.hasArg(options::OPT_nostdlib) &&
3109      !Args.hasArg(options::OPT_nostartfiles))
3110    CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3111                                                      "/usr/gnu/lib/crtso.o")));
3112
3113  Args.AddAllArgs(CmdArgs, options::OPT_L);
3114  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3115  Args.AddAllArgs(CmdArgs, options::OPT_e);
3116
3117  for (InputInfoList::const_iterator
3118         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3119    const InputInfo &II = *it;
3120
3121    // Don't try to pass LLVM inputs to a generic gcc.
3122    if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3123        II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3124      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3125        << getToolChain().getTripleString();
3126
3127    if (II.isPipe())
3128      CmdArgs.push_back("-");
3129    else if (II.isFilename())
3130      CmdArgs.push_back(II.getFilename());
3131    else
3132      II.getInputArg().renderAsInput(Args, CmdArgs);
3133  }
3134
3135  if (!Args.hasArg(options::OPT_nostdlib) &&
3136      !Args.hasArg(options::OPT_nodefaultlibs)) {
3137    if (D.CCCIsCXX) {
3138      CmdArgs.push_back("-lstdc++");
3139      CmdArgs.push_back("-lm");
3140    }
3141
3142    if (Args.hasArg(options::OPT_pthread))
3143      CmdArgs.push_back("-lpthread");
3144    CmdArgs.push_back("-lc");
3145    CmdArgs.push_back("-lgcc");
3146    CmdArgs.push_back("-L/usr/gnu/lib");
3147    // FIXME: fill in the correct search path for the final
3148    // support libraries.
3149    CmdArgs.push_back("-L/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
3150  }
3151
3152  if (!Args.hasArg(options::OPT_nostdlib) &&
3153      !Args.hasArg(options::OPT_nostartfiles)) {
3154    CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
3155                                              "/usr/gnu/lib/libend.a")));
3156  }
3157
3158  const char *Exec =
3159    Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld"));
3160  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
3161}
3162
3163/// DragonFly Tools
3164
3165// For now, DragonFly Assemble does just about the same as for
3166// FreeBSD, but this may change soon.
3167void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3168                                       Job &Dest, const InputInfo &Output,
3169                                       const InputInfoList &Inputs,
3170                                       const ArgList &Args,
3171                                       const char *LinkingOutput) const {
3172  ArgStringList CmdArgs;
3173
3174  // When building 32-bit code on DragonFly/pc64, we have to explicitly
3175  // instruct as in the base system to assemble 32-bit code.
3176  if (getToolChain().getArchName() == "i386")
3177    CmdArgs.push_back("--32");
3178
3179  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3180                       options::OPT_Xassembler);
3181
3182  CmdArgs.push_back("-o");
3183  if (Output.isPipe())
3184    CmdArgs.push_back("-");
3185  else
3186    CmdArgs.push_back(Output.getFilename());
3187
3188  for (InputInfoList::const_iterator
3189         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3190    const InputInfo &II = *it;
3191    if (II.isPipe())
3192      CmdArgs.push_back("-");
3193    else
3194      CmdArgs.push_back(II.getFilename());
3195  }
3196
3197  const char *Exec =
3198    Args.MakeArgString(getToolChain().GetProgramPath("as"));
3199  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
3200}
3201
3202void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
3203                                 Job &Dest, const InputInfo &Output,
3204                                 const InputInfoList &Inputs,
3205                                 const ArgList &Args,
3206                                 const char *LinkingOutput) const {
3207  const Driver &D = getToolChain().getDriver();
3208  ArgStringList CmdArgs;
3209
3210  if (Args.hasArg(options::OPT_static)) {
3211    CmdArgs.push_back("-Bstatic");
3212  } else {
3213    if (Args.hasArg(options::OPT_shared))
3214      CmdArgs.push_back("-Bshareable");
3215    else {
3216      CmdArgs.push_back("-dynamic-linker");
3217      CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
3218    }
3219  }
3220
3221  // When building 32-bit code on DragonFly/pc64, we have to explicitly
3222  // instruct ld in the base system to link 32-bit code.
3223  if (getToolChain().getArchName() == "i386") {
3224    CmdArgs.push_back("-m");
3225    CmdArgs.push_back("elf_i386");
3226  }
3227
3228  if (Output.isPipe()) {
3229    CmdArgs.push_back("-o");
3230    CmdArgs.push_back("-");
3231  } else if (Output.isFilename()) {
3232    CmdArgs.push_back("-o");
3233    CmdArgs.push_back(Output.getFilename());
3234  } else {
3235    assert(Output.isNothing() && "Invalid output.");
3236  }
3237
3238  if (!Args.hasArg(options::OPT_nostdlib) &&
3239      !Args.hasArg(options::OPT_nostartfiles)) {
3240    if (!Args.hasArg(options::OPT_shared)) {
3241      CmdArgs.push_back(
3242            Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
3243      CmdArgs.push_back(
3244            Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
3245      CmdArgs.push_back(
3246            Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
3247    } else {
3248      CmdArgs.push_back(
3249            Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
3250      CmdArgs.push_back(
3251            Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
3252    }
3253  }
3254
3255  Args.AddAllArgs(CmdArgs, options::OPT_L);
3256  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3257  Args.AddAllArgs(CmdArgs, options::OPT_e);
3258
3259  for (InputInfoList::const_iterator
3260         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3261    const InputInfo &II = *it;
3262
3263    // Don't try to pass LLVM inputs to a generic gcc.
3264    if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3265        II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
3266      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3267        << getToolChain().getTripleString();
3268
3269    if (II.isPipe())
3270      CmdArgs.push_back("-");
3271    else if (II.isFilename())
3272      CmdArgs.push_back(II.getFilename());
3273    else
3274      II.getInputArg().renderAsInput(Args, CmdArgs);
3275  }
3276
3277  if (!Args.hasArg(options::OPT_nostdlib) &&
3278      !Args.hasArg(options::OPT_nodefaultlibs)) {
3279    // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
3280    //         rpaths
3281    CmdArgs.push_back("-L/usr/lib/gcc41");
3282
3283    if (!Args.hasArg(options::OPT_static)) {
3284      CmdArgs.push_back("-rpath");
3285      CmdArgs.push_back("/usr/lib/gcc41");
3286
3287      CmdArgs.push_back("-rpath-link");
3288      CmdArgs.push_back("/usr/lib/gcc41");
3289
3290      CmdArgs.push_back("-rpath");
3291      CmdArgs.push_back("/usr/lib");
3292
3293      CmdArgs.push_back("-rpath-link");
3294      CmdArgs.push_back("/usr/lib");
3295    }
3296
3297    if (Args.hasArg(options::OPT_shared)) {
3298      CmdArgs.push_back("-lgcc_pic");
3299    } else {
3300      CmdArgs.push_back("-lgcc");
3301    }
3302
3303
3304    if (Args.hasArg(options::OPT_pthread))
3305      CmdArgs.push_back("-lpthread");
3306
3307    if (!Args.hasArg(options::OPT_nolibc)) {
3308      CmdArgs.push_back("-lc");
3309    }
3310
3311    if (Args.hasArg(options::OPT_shared)) {
3312      CmdArgs.push_back("-lgcc_pic");
3313    } else {
3314      CmdArgs.push_back("-lgcc");
3315    }
3316  }
3317
3318  if (!Args.hasArg(options::OPT_nostdlib) &&
3319      !Args.hasArg(options::OPT_nostartfiles)) {
3320    if (!Args.hasArg(options::OPT_shared))
3321      CmdArgs.push_back(Args.MakeArgString(
3322                              getToolChain().GetFilePath("crtend.o")));
3323    else
3324      CmdArgs.push_back(Args.MakeArgString(
3325                              getToolChain().GetFilePath("crtendS.o")));
3326    CmdArgs.push_back(Args.MakeArgString(
3327                              getToolChain().GetFilePath("crtn.o")));
3328  }
3329
3330  const char *Exec =
3331    Args.MakeArgString(getToolChain().GetProgramPath("ld"));
3332  Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
3333}
3334