Tools.cpp revision 6b200b2b698b6ac9e310f2225c69c85d28ae4fb0
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" // FIXME: Remove?
16#include "clang/Driver/DriverDiagnostic.h" // FIXME: Remove?
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/ToolChain.h"
22#include "clang/Driver/Util.h"
23
24#include "llvm/ADT/SmallString.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/Support/Format.h"
27#include "llvm/Support/raw_ostream.h"
28
29#include "InputInfo.h"
30#include "ToolChains.h"
31
32using namespace clang::driver;
33using namespace clang::driver::tools;
34
35static const char *MakeFormattedString(const ArgList &Args,
36                                       const llvm::format_object_base &Fmt) {
37  llvm::SmallString<256> Str;
38  llvm::raw_svector_ostream(Str) << Fmt;
39  return Args.MakeArgString(Str.str());
40}
41
42/// CheckPreprocessingOptions - Perform some validation of preprocessing
43/// arguments that is shared with gcc.
44static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
45  if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
46    if (!Args.hasArg(options::OPT_E))
47      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
48        << A->getAsString(Args) << "-E";
49}
50
51/// CheckCodeGenerationOptions - Perform some validation of code generation
52/// arguments that is shared with gcc.
53static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
54  // In gcc, only ARM checks this, but it seems reasonable to check universally.
55  if (Args.hasArg(options::OPT_static))
56    if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
57                                       options::OPT_mdynamic_no_pic))
58      D.Diag(clang::diag::err_drv_argument_not_allowed_with)
59        << A->getAsString(Args) << "-static";
60}
61
62void Clang::AddPreprocessingOptions(const Driver &D,
63                                    const ArgList &Args,
64                                    ArgStringList &CmdArgs,
65                                    const InputInfo &Output,
66                                    const InputInfoList &Inputs) const {
67  Arg *A;
68
69  CheckPreprocessingOptions(D, Args);
70
71  Args.AddLastArg(CmdArgs, options::OPT_C);
72  Args.AddLastArg(CmdArgs, options::OPT_CC);
73
74  // Handle dependency file generation.
75  if ((A = Args.getLastArg(options::OPT_M)) ||
76      (A = Args.getLastArg(options::OPT_MM)) ||
77      (A = Args.getLastArg(options::OPT_MD)) ||
78      (A = Args.getLastArg(options::OPT_MMD))) {
79    // Determine the output location.
80    const char *DepFile;
81    if (Output.getType() == types::TY_Dependencies) {
82      if (Output.isPipe())
83        DepFile = "-";
84      else
85        DepFile = Output.getFilename();
86    } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
87      DepFile = MF->getValue(Args);
88    } else if (A->getOption().getId() == options::OPT_M ||
89               A->getOption().getId() == options::OPT_MM) {
90      DepFile = "-";
91    } else {
92      DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
93    }
94    CmdArgs.push_back("-dependency-file");
95    CmdArgs.push_back(DepFile);
96
97    // Add an -MT option if the user didn't specify their own.
98    //
99    // FIXME: This should use -MQ, when we support it.
100    if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
101      const char *DepTarget;
102
103      // If user provided -o, that is the dependency target, except
104      // when we are only generating a dependency file.
105      Arg *OutputOpt = Args.getLastArg(options::OPT_o);
106      if (OutputOpt && Output.getType() != types::TY_Dependencies) {
107        DepTarget = OutputOpt->getValue(Args);
108      } else {
109        // Otherwise derive from the base input.
110        //
111        // FIXME: This should use the computed output file location.
112        llvm::sys::Path P(Inputs[0].getBaseInput());
113
114        P.eraseSuffix();
115        P.appendSuffix("o");
116        DepTarget = Args.MakeArgString(P.getLast());
117      }
118
119      CmdArgs.push_back("-MT");
120      CmdArgs.push_back(DepTarget);
121    }
122
123    if (A->getOption().getId() == options::OPT_M ||
124        A->getOption().getId() == options::OPT_MD)
125      CmdArgs.push_back("-sys-header-deps");
126  }
127
128  Args.AddLastArg(CmdArgs, options::OPT_MP);
129  Args.AddAllArgs(CmdArgs, options::OPT_MT);
130
131  // Add -i* options, and automatically translate to
132  // -include-pch/-include-pth for transparent PCH support. It's
133  // wonky, but we include looking for .gch so we can support seamless
134  // replacement into a build system already set up to be generating
135  // .gch files.
136  //
137  // FIXME: Use iterator.
138  for (ArgList::const_iterator
139         it = Args.begin(), ie = Args.end(); it != ie; ++it) {
140    const Arg *A = *it;
141    if (!A->getOption().matches(options::OPT_clang_i_Group))
142      continue;
143
144    if (A->getOption().matches(options::OPT_include)) {
145      bool FoundPTH = false;
146      bool FoundPCH = false;
147      llvm::sys::Path P(A->getValue(Args));
148      if (D.CCCUsePCH) {
149        P.appendSuffix("pch");
150        if (P.exists())
151          FoundPCH = true;
152        else
153          P.eraseSuffix();
154      }
155
156      if (!FoundPCH) {
157        P.appendSuffix("pth");
158        if (P.exists())
159          FoundPTH = true;
160        else
161          P.eraseSuffix();
162      }
163
164      if (!FoundPCH && !FoundPTH) {
165        P.appendSuffix("gch");
166        if (P.exists()) {
167          FoundPCH = D.CCCUsePCH;
168          FoundPTH = !D.CCCUsePCH;
169        }
170        else
171          P.eraseSuffix();
172      }
173
174      if (FoundPCH || FoundPTH) {
175        A->claim();
176        if (D.CCCUsePCH)
177          CmdArgs.push_back("-include-pch");
178        else
179          CmdArgs.push_back("-include-pth");
180        CmdArgs.push_back(Args.MakeArgString(P.str()));
181        continue;
182      }
183    }
184
185    // Not translated, render as usual.
186    A->claim();
187    A->render(Args, CmdArgs);
188  }
189
190  Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
191  Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
192
193  // Add -Wp, and -Xassembler if using the preprocessor.
194
195  // FIXME: There is a very unfortunate problem here, some troubled
196  // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
197  // really support that we would have to parse and then translate
198  // those options. :(
199  Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
200                       options::OPT_Xpreprocessor);
201}
202
203/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targetting.
204//
205// FIXME: tblgen this.
206static llvm::StringRef getARMTargetCPU(const ArgList &Args) {
207  // FIXME: Warn on inconsistent use of -mcpu and -march.
208
209  // If we have -mcpu=, use that.
210  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
211    return A->getValue(Args);
212
213  // Otherwise, if we have -march= choose the base CPU for that arch.
214  if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
215    llvm::StringRef MArch = A->getValue(Args);
216
217    if (MArch == "armv2" || MArch == "armv2a")
218      return "arm2";
219    if (MArch == "armv3")
220      return "arm6";
221    if (MArch == "armv3m")
222      return "arm7m";
223    if (MArch == "armv4" || MArch == "armv4t")
224      return "arm7tdmi";
225    if (MArch == "armv5" || MArch == "armv5t")
226      return "arm10tdmi";
227    if (MArch == "armv5e" || MArch == "armv5te")
228      return "arm1026ejs";
229    if (MArch == "armv5tej")
230      return "arm926ejs";
231    if (MArch == "armv6" || MArch == "armv6k")
232      return "arm1136jf-s";
233    if (MArch == "armv6j")
234      return "arm1136j-s";
235    if (MArch == "armv6z" || MArch == "armv6zk")
236      return "arm1176jzf-s";
237    if (MArch == "armv6t2")
238      return "arm1156t2-s";
239    if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
240      return "cortex-a8";
241    if (MArch == "armv7r" || MArch == "armv7-r")
242      return "cortex-r4";
243    if (MArch == "armv7m" || MArch == "armv7-m")
244      return "cortex-m3";
245    if (MArch == "ep9312")
246      return "ep9312";
247    if (MArch == "iwmmxt")
248      return "iwmmxt";
249    if (MArch == "xscale")
250      return "xscale";
251  }
252
253  // Otherwise return the most base CPU LLVM supports.
254  return "arm7tdmi";
255}
256
257/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
258/// CPU.
259//
260// FIXME: This is redundant with -mcpu, why does LLVM use this.
261// FIXME: tblgen this, or kill it!
262static const char *getLLVMArchSuffixForARM(llvm::StringRef CPU) {
263  if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
264      CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
265      CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
266      CPU == "arm940t" || CPU == "ep9312")
267    return "v4t";
268
269  if (CPU == "arm10tdmi" || CPU == "arm1020t")
270    return "v5";
271
272  if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
273      CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
274      CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
275      CPU == "iwmmxt")
276    return "v5e";
277
278  if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
279      CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
280    return "v6";
281
282  if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
283    return "v6t2";
284
285  if (CPU == "cortex-a8" || CPU == "cortex-a9")
286    return "v7";
287
288  return "";
289}
290
291/// getLLVMTriple - Get the LLVM triple to use for a particular toolchain, which
292/// may depend on command line arguments.
293static std::string getLLVMTriple(const ToolChain &TC, const ArgList &Args) {
294  switch (TC.getTriple().getArch()) {
295  default:
296    return TC.getTripleString();
297
298  case llvm::Triple::arm:
299  case llvm::Triple::thumb: {
300    // FIXME: Factor into subclasses.
301    llvm::Triple Triple = TC.getTriple();
302
303    // Thumb2 is the default for V7 on Darwin.
304    //
305    // FIXME: Thumb should just be another -target-feaure, not in the triple.
306    llvm::StringRef Suffix = getLLVMArchSuffixForARM(getARMTargetCPU(Args));
307    bool ThumbDefault =
308      (Suffix == "v7" && TC.getTriple().getOS() == llvm::Triple::Darwin);
309    std::string ArchName = "arm";
310    if (Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
311      ArchName = "thumb";
312    Triple.setArchName(ArchName + Suffix.str());
313
314    return Triple.getTriple();
315  }
316  }
317}
318
319void Clang::AddARMTargetArgs(const ArgList &Args,
320                             ArgStringList &CmdArgs) const {
321  const Driver &D = getToolChain().getHost().getDriver();
322
323  // Select the ABI to use.
324  //
325  // FIXME: Support -meabi.
326  const char *ABIName = 0;
327  if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
328    ABIName = A->getValue(Args);
329  } else {
330    // Select the default based on the platform.
331    switch (getToolChain().getTriple().getOS()) {
332      // FIXME: Is this right for non-Darwin and non-Linux?
333    default:
334      ABIName = "aapcs";
335      break;
336
337    case llvm::Triple::Darwin:
338      ABIName = "apcs-gnu";
339      break;
340
341    case llvm::Triple::Linux:
342      ABIName = "aapcs-linux";
343      break;
344    }
345  }
346  CmdArgs.push_back("-target-abi");
347  CmdArgs.push_back(ABIName);
348
349  // Set the CPU based on -march= and -mcpu=.
350  CmdArgs.push_back(Args.MakeArgString("-mcpu=" + getARMTargetCPU(Args)));
351
352  // Select the float ABI as determined by -msoft-float, -mhard-float, and
353  // -mfloat-abi=.
354  llvm::StringRef FloatABI;
355  if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
356                               options::OPT_mhard_float,
357                               options::OPT_mfloat_abi_EQ)) {
358    if (A->getOption().matches(options::OPT_msoft_float))
359      FloatABI = "soft";
360    else if (A->getOption().matches(options::OPT_mhard_float))
361      FloatABI = "hard";
362    else {
363      FloatABI = A->getValue(Args);
364      if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
365        D.Diag(clang::diag::err_drv_invalid_mfloat_abi)
366          << A->getAsString(Args);
367        FloatABI = "soft";
368      }
369    }
370  }
371
372  // If unspecified, choose the default based on the platform.
373  if (FloatABI.empty()) {
374    // FIXME: This is wrong for non-Darwin, we don't have a mechanism yet for
375    // distinguishing things like linux-eabi vs linux-elf.
376    switch (getToolChain().getTriple().getOS()) {
377    case llvm::Triple::Darwin: {
378      // Darwin defaults to "softfp" for v6 and v7.
379      //
380      // FIXME: Factor out an ARM class so we can cache the arch somewhere.
381      llvm::StringRef ArchName = getLLVMArchSuffixForARM(getARMTargetCPU(Args));
382      if (ArchName.startswith("v6") || ArchName.startswith("v7"))
383        FloatABI = "softfp";
384      else
385        FloatABI = "soft";
386      break;
387    }
388
389    default:
390      // Assume "soft", but warn the user we are guessing.
391      FloatABI = "soft";
392      D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
393      break;
394    }
395  }
396
397  if (FloatABI == "soft") {
398    // Floating point operations and argument passing are soft.
399    //
400    // FIXME: This changes CPP defines, we need -target-soft-float.
401    CmdArgs.push_back("-soft-float");
402    CmdArgs.push_back("-float-abi=soft");
403  } else if (FloatABI == "softfp") {
404    // Floating point operations are hard, but argument passing is soft.
405    CmdArgs.push_back("-float-abi=soft");
406  } else {
407    // Floating point operations and argument passing are hard.
408    assert(FloatABI == "hard" && "Invalid float abi!");
409    CmdArgs.push_back("-float-abi=hard");
410  }
411}
412
413void Clang::AddX86TargetArgs(const ArgList &Args,
414                             ArgStringList &CmdArgs) const {
415  // FIXME: This needs to change to use a clang-cc option, and set the attribute
416  // on functions.
417  if (!Args.hasFlag(options::OPT_mred_zone,
418                    options::OPT_mno_red_zone,
419                    true) ||
420      Args.hasArg(options::OPT_mkernel) ||
421      Args.hasArg(options::OPT_fapple_kext))
422    CmdArgs.push_back("--disable-red-zone");
423
424  // FIXME: This needs to change to use a clang-cc option, and set the attribute
425  // on functions.
426  if (Args.hasFlag(options::OPT_msoft_float,
427                   options::OPT_mno_soft_float,
428                   false))
429    CmdArgs.push_back("--no-implicit-float");
430
431  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
432    // FIXME: We may need some translation here from the options gcc takes to
433    // names the LLVM backend understand?
434    CmdArgs.push_back("-mcpu");
435    CmdArgs.push_back(A->getValue(Args));
436  } else {
437    // Select default CPU.
438
439    // FIXME: Need target hooks.
440    if (memcmp(getToolChain().getOS().c_str(), "darwin", 6) == 0) {
441      if (getToolChain().getArchName() == "x86_64")
442        CmdArgs.push_back("--mcpu=core2");
443      else if (getToolChain().getArchName() == "i386")
444        CmdArgs.push_back("--mcpu=yonah");
445    } else {
446      if (getToolChain().getArchName() == "x86_64")
447        CmdArgs.push_back("--mcpu=x86-64");
448      else if (getToolChain().getArchName() == "i386")
449        CmdArgs.push_back("--mcpu=pentium4");
450    }
451  }
452
453  // FIXME: Use iterator.
454  for (ArgList::const_iterator
455         it = Args.begin(), ie = Args.end(); it != ie; ++it) {
456    const Arg *A = *it;
457    if (A->getOption().matches(options::OPT_m_x86_Features_Group)) {
458      llvm::StringRef Name = A->getOption().getName();
459
460      // Skip over "-m".
461      assert(Name.startswith("-m") && "Invalid feature name.");
462      Name = Name.substr(2);
463
464      bool IsNegative = Name.startswith("no-");
465      if (IsNegative)
466        Name = Name.substr(3);
467
468      A->claim();
469      CmdArgs.push_back("-target-feature");
470      CmdArgs.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
471    }
472  }
473}
474
475void Clang::ConstructJob(Compilation &C, const JobAction &JA,
476                         Job &Dest,
477                         const InputInfo &Output,
478                         const InputInfoList &Inputs,
479                         const ArgList &Args,
480                         const char *LinkingOutput) const {
481  const Driver &D = getToolChain().getHost().getDriver();
482  ArgStringList CmdArgs;
483
484  assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
485
486  CmdArgs.push_back("-triple");
487
488  const char *TripleStr =
489    Args.MakeArgString(getLLVMTriple(getToolChain(), Args));
490  CmdArgs.push_back(TripleStr);
491
492  if (isa<AnalyzeJobAction>(JA)) {
493    assert(JA.getType() == types::TY_Plist && "Invalid output type.");
494    CmdArgs.push_back("-analyze");
495  } else if (isa<PreprocessJobAction>(JA)) {
496    if (Output.getType() == types::TY_Dependencies)
497      CmdArgs.push_back("-Eonly");
498    else
499      CmdArgs.push_back("-E");
500  } else if (isa<PrecompileJobAction>(JA)) {
501    if (D.CCCUsePCH)
502      CmdArgs.push_back("-emit-pch");
503    else
504      CmdArgs.push_back("-emit-pth");
505  } else {
506    assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
507
508    if (JA.getType() == types::TY_Nothing) {
509      CmdArgs.push_back("-fsyntax-only");
510    } else if (JA.getType() == types::TY_LLVMAsm) {
511      CmdArgs.push_back("-emit-llvm");
512    } else if (JA.getType() == types::TY_LLVMBC) {
513      CmdArgs.push_back("-emit-llvm-bc");
514    } else if (JA.getType() == types::TY_PP_Asm) {
515      CmdArgs.push_back("-S");
516    } else if (JA.getType() == types::TY_AST) {
517      CmdArgs.push_back("-emit-pch");
518    }
519  }
520
521  // The make clang go fast button.
522  CmdArgs.push_back("-disable-free");
523
524  // Set the main file name, so that debug info works even with
525  // -save-temps.
526  CmdArgs.push_back("-main-file-name");
527  CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
528
529  // Some flags which affect the language (via preprocessor
530  // defines). See darwin::CC1::AddCPPArgs.
531  if (Args.hasArg(options::OPT_static))
532    CmdArgs.push_back("-static-define");
533
534  if (isa<AnalyzeJobAction>(JA)) {
535    // Add default argument set.
536    if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
537      CmdArgs.push_back("-warn-dead-stores");
538      CmdArgs.push_back("-warn-security-syntactic");
539      CmdArgs.push_back("-checker-cfref");
540      CmdArgs.push_back("-analyzer-eagerly-assume");
541      CmdArgs.push_back("-warn-objc-methodsigs");
542      // Do not enable the missing -dealloc check.
543      // '-warn-objc-missing-dealloc',
544      CmdArgs.push_back("-warn-objc-unused-ivars");
545    }
546
547    // Set the output format. The default is plist, for (lame) historical
548    // reasons.
549    CmdArgs.push_back("-analyzer-output");
550    if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
551      CmdArgs.push_back(A->getValue(Args));
552    else
553      CmdArgs.push_back("plist");
554
555    // Add -Xanalyzer arguments when running as analyzer.
556    Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
557  }
558
559  CheckCodeGenerationOptions(D, Args);
560
561  // Perform argument translation for LLVM backend. This
562  // takes some care in reconciling with llvm-gcc. The
563  // issue is that llvm-gcc translates these options based on
564  // the values in cc1, whereas we are processing based on
565  // the driver arguments.
566  //
567  // FIXME: This is currently broken for -f flags when -fno
568  // variants are present.
569
570  // This comes from the default translation the driver + cc1
571  // would do to enable flag_pic.
572  //
573  // FIXME: Centralize this code.
574  bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
575                     Args.hasArg(options::OPT_fpic) ||
576                     Args.hasArg(options::OPT_fPIE) ||
577                     Args.hasArg(options::OPT_fpie));
578  bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
579                      Args.hasArg(options::OPT_static));
580  const char *Model = getToolChain().GetForcedPicModel();
581  if (!Model) {
582    if (Args.hasArg(options::OPT_mdynamic_no_pic))
583      Model = "dynamic-no-pic";
584    else if (PICDisabled)
585      Model = "static";
586    else if (PICEnabled)
587      Model = "pic";
588    else
589      Model = getToolChain().GetDefaultRelocationModel();
590  }
591  CmdArgs.push_back("--relocation-model");
592  CmdArgs.push_back(Model);
593
594  // Infer the __PIC__ value.
595  //
596  // FIXME:  This isn't quite right on Darwin, which always sets
597  // __PIC__=2.
598  if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
599    if (Args.hasArg(options::OPT_fPIC))
600      CmdArgs.push_back("-pic-level=2");
601    else
602      CmdArgs.push_back("-pic-level=1");
603  }
604
605  if (Args.hasArg(options::OPT_ftime_report))
606    CmdArgs.push_back("--time-passes");
607  // FIXME: Set --enable-unsafe-fp-math.
608  if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
609                   options::OPT_fomit_frame_pointer))
610    CmdArgs.push_back("--disable-fp-elim");
611  if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
612                    options::OPT_fno_zero_initialized_in_bss,
613                    true))
614    CmdArgs.push_back("--nozero-initialized-in-bss");
615  if (Args.hasArg(options::OPT_dA) || Args.hasArg(options::OPT_fverbose_asm))
616    CmdArgs.push_back("--asm-verbose");
617  if (Args.hasArg(options::OPT_fdebug_pass_structure))
618    CmdArgs.push_back("--debug-pass=Structure");
619  if (Args.hasArg(options::OPT_fdebug_pass_arguments))
620    CmdArgs.push_back("--debug-pass=Arguments");
621  // FIXME: set --inline-threshhold=50 if (optimize_size || optimize
622  // < 3)
623
624  // This is a coarse approximation of what llvm-gcc actually does, both
625  // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
626  // complicated ways.
627  bool AsynchronousUnwindTables =
628    Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
629                 options::OPT_fno_asynchronous_unwind_tables,
630                 getToolChain().IsUnwindTablesDefault() &&
631                 !Args.hasArg(options::OPT_mkernel));
632  if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
633                   AsynchronousUnwindTables))
634    CmdArgs.push_back("--unwind-tables=1");
635  else
636    CmdArgs.push_back("--unwind-tables=0");
637
638  // FIXME: Handle -mtune=.
639  (void) Args.hasArg(options::OPT_mtune_EQ);
640
641  if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
642    CmdArgs.push_back("-code-model");
643    CmdArgs.push_back(A->getValue(Args));
644  }
645
646  // Add target specific cpu and features flags.
647  switch(getToolChain().getTriple().getArch()) {
648  default:
649    break;
650
651  case llvm::Triple::arm:
652  case llvm::Triple::thumb:
653    AddARMTargetArgs(Args, CmdArgs);
654    break;
655
656  case llvm::Triple::x86:
657  case llvm::Triple::x86_64:
658    AddX86TargetArgs(Args, CmdArgs);
659    break;
660  }
661
662  if (Args.hasFlag(options::OPT_fmath_errno,
663                   options::OPT_fno_math_errno,
664                   getToolChain().IsMathErrnoDefault()))
665    CmdArgs.push_back("--fmath-errno=1");
666  else
667    CmdArgs.push_back("--fmath-errno=0");
668
669  if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
670    CmdArgs.push_back("--limit-float-precision");
671    CmdArgs.push_back(A->getValue(Args));
672  }
673
674  // FIXME: Add --stack-protector-buffer-size=<xxx> on
675  // -fstack-protect.
676
677  Arg *Unsupported;
678  if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
679      (Unsupported = Args.getLastArg(options::OPT_MQ)) ||
680      (Unsupported = Args.getLastArg(options::OPT_iframework)))
681    D.Diag(clang::diag::err_drv_clang_unsupported)
682      << Unsupported->getOption().getName();
683
684  Args.AddAllArgs(CmdArgs, options::OPT_v);
685  Args.AddLastArg(CmdArgs, options::OPT_P);
686  Args.AddLastArg(CmdArgs, options::OPT_mmacosx_version_min_EQ);
687  Args.AddLastArg(CmdArgs, options::OPT_miphoneos_version_min_EQ);
688  Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
689
690  // Special case debug options to only pass -g to clang. This is
691  // wrong.
692  if (Args.hasArg(options::OPT_g_Group))
693    CmdArgs.push_back("-g");
694
695  Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
696
697  Args.AddLastArg(CmdArgs, options::OPT_isysroot);
698
699  // Add preprocessing options like -I, -D, etc. if we are using the
700  // preprocessor.
701  //
702  // FIXME: Support -fpreprocessed
703  types::ID InputType = Inputs[0].getType();
704  if (types::getPreprocessedType(InputType) != types::TY_INVALID)
705    AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
706
707  // Manually translate -O to -O2 and -O4 to -O3; let clang reject
708  // others.
709  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
710    if (A->getOption().getId() == options::OPT_O4)
711      CmdArgs.push_back("-O3");
712    else if (A->getValue(Args)[0] == '\0')
713      CmdArgs.push_back("-O2");
714    else
715      A->render(Args, CmdArgs);
716  }
717
718  Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
719  Args.AddLastArg(CmdArgs, options::OPT_w);
720
721  // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
722  // (-ansi is equivalent to -std=c89).
723  //
724  // If a std is supplied, only add -trigraphs if it follows the
725  // option.
726  if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
727    if (Std->getOption().matches(options::OPT_ansi))
728      CmdArgs.push_back("-std=c89");
729    else
730      Std->render(Args, CmdArgs);
731
732    if (Arg *A = Args.getLastArg(options::OPT_trigraphs))
733      if (A->getIndex() > Std->getIndex())
734        A->render(Args, CmdArgs);
735  } else {
736    // Honor -std-default.
737    Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
738                              "-std=", /*Joined=*/true);
739    Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
740  }
741
742  if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
743    CmdArgs.push_back("-ftemplate-depth");
744    CmdArgs.push_back(A->getValue(Args));
745  }
746
747  if (Args.hasArg(options::OPT__relocatable_pch, true))
748    CmdArgs.push_back("--relocatable-pch");
749
750   if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
751     CmdArgs.push_back("-fconstant-string-class");
752     CmdArgs.push_back(A->getValue(Args));
753   }
754
755  // Forward -f options which we can pass directly.
756  Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
757  Args.AddLastArg(CmdArgs, options::OPT_ffreestanding);
758  Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
759  Args.AddLastArg(CmdArgs, options::OPT_fgnu_runtime);
760  Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions);
761  Args.AddLastArg(CmdArgs, options::OPT_fmessage_length_EQ);
762  Args.AddLastArg(CmdArgs, options::OPT_fms_extensions);
763  Args.AddLastArg(CmdArgs, options::OPT_fnext_runtime);
764  Args.AddLastArg(CmdArgs, options::OPT_fno_caret_diagnostics);
765  Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
766  Args.AddLastArg(CmdArgs, options::OPT_fobjc_gc_only);
767  Args.AddLastArg(CmdArgs, options::OPT_fobjc_gc);
768  Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
769  // FIXME: Should we remove this?
770  Args.AddLastArg(CmdArgs, options::OPT_fobjc_nonfragile_abi);
771  Args.AddLastArg(CmdArgs, options::OPT_fobjc_tight_layout);
772  Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
773  Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
774  Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
775  Args.AddLastArg(CmdArgs, options::OPT_fvisibility_EQ);
776  Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
777
778  Args.AddLastArg(CmdArgs, options::OPT_pthread);
779
780  // Forward stack protector flags.
781  if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
782                               options::OPT_fstack_protector_all,
783                               options::OPT_fstack_protector)) {
784    if (A->getOption().matches(options::OPT_fno_stack_protector))
785      CmdArgs.push_back("--stack-protector=0");
786    else if (A->getOption().matches(options::OPT_fstack_protector))
787      CmdArgs.push_back("--stack-protector=1");
788    else
789      CmdArgs.push_back("--stack-protector=2");
790  }
791
792  // Forward -f options with positive and negative forms; we translate
793  // these by hand.
794
795  // -fbuiltin is default, only pass non-default.
796  if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
797    CmdArgs.push_back("-fbuiltin=0");
798
799  // -fblocks default varies depending on platform and language; only
800  // pass if specified.
801  if (Arg *A = Args.getLastArg(options::OPT_fblocks, options::OPT_fno_blocks)) {
802    if (A->getOption().matches(options::OPT_fblocks))
803      CmdArgs.push_back("-fblocks");
804    else
805      CmdArgs.push_back("-fblocks=0");
806  }
807
808  // -fexceptions default varies depending on platform and language; only
809  // pass if specified.
810 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
811                               options::OPT_fno_exceptions)) {
812    if (A->getOption().matches(options::OPT_fexceptions))
813      CmdArgs.push_back("-fexceptions");
814    else
815      CmdArgs.push_back("-fexceptions=0");
816  }
817
818  // -frtti is default, only pass non-default.
819  if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
820    CmdArgs.push_back("-frtti=0");
821
822  // -fsigned-char/-funsigned-char default varies depending on platform; only
823  // pass if specified.
824  if (Arg *A = Args.getLastArg(options::OPT_fsigned_char,
825                               options::OPT_funsigned_char)) {
826    if (A->getOption().matches(options::OPT_fsigned_char))
827      CmdArgs.push_back("-fsigned-char");
828    else
829      CmdArgs.push_back("-fsigned-char=0");
830  }
831
832  // -fno-pascal-strings is default, only pass non-default. If the
833  // -tool chain happened to translate to -mpascal-strings, we want to
834  // -back translate here.
835  //
836  // FIXME: This is gross; that translation should be pulled from the
837  // tool chain.
838  if (Args.hasFlag(options::OPT_fpascal_strings,
839                   options::OPT_fno_pascal_strings,
840                   false) ||
841      Args.hasFlag(options::OPT_mpascal_strings,
842                   options::OPT_mno_pascal_strings,
843                   false))
844    CmdArgs.push_back("-fpascal-strings");
845
846  // -fcommon is default, only pass non-default.
847  if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
848    CmdArgs.push_back("-fno-common");
849
850  // -fsigned-bitfields is default, and clang doesn't yet support
851  // --funsigned-bitfields.
852  if (!Args.hasFlag(options::OPT_fsigned_bitfields,
853                    options::OPT_funsigned_bitfields))
854    D.Diag(clang::diag::warn_drv_clang_unsupported)
855      << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
856
857  // -fdiagnostics-fixit-info is default, only pass non-default.
858  if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
859                    options::OPT_fno_diagnostics_fixit_info))
860    CmdArgs.push_back("-fno-diagnostics-fixit-info");
861
862  // Enable -fdiagnostics-show-option by default.
863  if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
864                   options::OPT_fno_diagnostics_show_option))
865    CmdArgs.push_back("-fdiagnostics-show-option");
866  if (!Args.hasFlag(options::OPT_fcolor_diagnostics,
867                    options::OPT_fno_color_diagnostics))
868    CmdArgs.push_back("-fno-color-diagnostics");
869  if (!Args.hasFlag(options::OPT_fshow_source_location,
870                    options::OPT_fno_show_source_location))
871    CmdArgs.push_back("-fno-show-source-location");
872
873  // -fdollars-in-identifiers default varies depending on platform and
874  // language; only pass if specified.
875  if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
876                               options::OPT_fno_dollars_in_identifiers)) {
877    if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
878      CmdArgs.push_back("-fdollars-in-identifiers=1");
879    else
880      CmdArgs.push_back("-fdollars-in-identifiers=0");
881  }
882
883  // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
884  // practical purposes.
885  if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
886                               options::OPT_fno_unit_at_a_time)) {
887    if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
888      D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
889  }
890
891  // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
892  //
893  // FIXME: This is disabled until clang-cc supports -fno-builtin-foo. PR4941.
894#if 0
895  if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
896      (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
897       getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
898    if (!Args.hasArg(options::OPT_fbuiltin_strcat))
899      CmdArgs.push_back("-fno-builtin-strcat");
900    if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
901      CmdArgs.push_back("-fno-builtin-strcpy");
902  }
903#endif
904
905  if (Arg *A = Args.getLastArg(options::OPT_traditional,
906                               options::OPT_traditional_cpp))
907    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
908
909  Args.AddLastArg(CmdArgs, options::OPT_dM);
910  Args.AddLastArg(CmdArgs, options::OPT_dD);
911
912  Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
913  Args.AddAllArgValues(CmdArgs, options::OPT_mllvm);
914
915  if (Output.getType() == types::TY_Dependencies) {
916    // Handled with other dependency code.
917  } else if (Output.isPipe()) {
918    CmdArgs.push_back("-o");
919    CmdArgs.push_back("-");
920  } else if (Output.isFilename()) {
921    CmdArgs.push_back("-o");
922    CmdArgs.push_back(Output.getFilename());
923  } else {
924    assert(Output.isNothing() && "Invalid output.");
925  }
926
927  for (InputInfoList::const_iterator
928         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
929    const InputInfo &II = *it;
930    CmdArgs.push_back("-x");
931    CmdArgs.push_back(types::getTypeName(II.getType()));
932    if (II.isPipe())
933      CmdArgs.push_back("-");
934    else if (II.isFilename())
935      CmdArgs.push_back(II.getFilename());
936    else
937      II.getInputArg().renderAsInput(Args, CmdArgs);
938  }
939
940  const char *Exec =
941    Args.MakeArgString(getToolChain().GetProgramPath(C, "clang-cc"));
942  Dest.addCommand(new Command(JA, Exec, CmdArgs));
943
944  // Explicitly warn that these options are unsupported, even though
945  // we are allowing compilation to continue.
946  // FIXME: Use iterator.
947  for (ArgList::const_iterator
948         it = Args.begin(), ie = Args.end(); it != ie; ++it) {
949    const Arg *A = *it;
950    if (A->getOption().matches(options::OPT_pg)) {
951      A->claim();
952      D.Diag(clang::diag::warn_drv_clang_unsupported)
953        << A->getAsString(Args);
954    }
955  }
956
957  // Claim some arguments which clang supports automatically.
958
959  // -fpch-preprocess is used with gcc to add a special marker in the
960  // -output to include the PCH file. Clang's PTH solution is
961  // -completely transparent, so we do not need to deal with it at
962  // -all.
963  Args.ClaimAllArgs(options::OPT_fpch_preprocess);
964
965  // Claim some arguments which clang doesn't support, but we don't
966  // care to warn the user about.
967
968  // FIXME: Use iterator.
969  for (ArgList::const_iterator
970         it = Args.begin(), ie = Args.end(); it != ie; ++it) {
971    const Arg *A = *it;
972    if (A->getOption().matches(options::OPT_clang_ignored_f_Group) ||
973        A->getOption().matches(options::OPT_clang_ignored_m_Group))
974      A->claim();
975  }
976}
977
978void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
979                               Job &Dest,
980                               const InputInfo &Output,
981                               const InputInfoList &Inputs,
982                               const ArgList &Args,
983                               const char *LinkingOutput) const {
984  const Driver &D = getToolChain().getHost().getDriver();
985  ArgStringList CmdArgs;
986
987  for (ArgList::const_iterator
988         it = Args.begin(), ie = Args.end(); it != ie; ++it) {
989    Arg *A = *it;
990    if (A->getOption().hasForwardToGCC()) {
991      // It is unfortunate that we have to claim here, as this means
992      // we will basically never report anything interesting for
993      // platforms using a generic gcc, even if we are just using gcc
994      // to get to the assembler.
995      A->claim();
996      A->render(Args, CmdArgs);
997    }
998  }
999
1000  RenderExtraToolArgs(CmdArgs);
1001
1002  // If using a driver driver, force the arch.
1003  const std::string &Arch = getToolChain().getArchName();
1004  if (getToolChain().getHost().useDriverDriver()) {
1005    CmdArgs.push_back("-arch");
1006
1007    // FIXME: Remove these special cases.
1008    if (Arch == "powerpc")
1009      CmdArgs.push_back("ppc");
1010    else if (Arch == "powerpc64")
1011      CmdArgs.push_back("ppc64");
1012    else
1013      CmdArgs.push_back(Args.MakeArgString(Arch));
1014  }
1015
1016  // Try to force gcc to match the tool chain we want, if we recognize
1017  // the arch.
1018  //
1019  // FIXME: The triple class should directly provide the information we want
1020  // here.
1021  if (Arch == "i386" || Arch == "powerpc")
1022    CmdArgs.push_back("-m32");
1023  else if (Arch == "x86_64" || Arch == "powerpc64")
1024    CmdArgs.push_back("-m64");
1025
1026  if (Output.isPipe()) {
1027    CmdArgs.push_back("-o");
1028    CmdArgs.push_back("-");
1029  } else if (Output.isFilename()) {
1030    CmdArgs.push_back("-o");
1031    CmdArgs.push_back(Output.getFilename());
1032  } else {
1033    assert(Output.isNothing() && "Unexpected output");
1034    CmdArgs.push_back("-fsyntax-only");
1035  }
1036
1037
1038  // Only pass -x if gcc will understand it; otherwise hope gcc
1039  // understands the suffix correctly. The main use case this would go
1040  // wrong in is for linker inputs if they happened to have an odd
1041  // suffix; really the only way to get this to happen is a command
1042  // like '-x foobar a.c' which will treat a.c like a linker input.
1043  //
1044  // FIXME: For the linker case specifically, can we safely convert
1045  // inputs into '-Wl,' options?
1046  for (InputInfoList::const_iterator
1047         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1048    const InputInfo &II = *it;
1049
1050    // Don't try to pass LLVM or AST inputs to a generic gcc.
1051    if (II.getType() == types::TY_LLVMBC)
1052      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
1053        << getToolChain().getTripleString();
1054    else if (II.getType() == types::TY_AST)
1055      D.Diag(clang::diag::err_drv_no_ast_support)
1056        << getToolChain().getTripleString();
1057
1058    if (types::canTypeBeUserSpecified(II.getType())) {
1059      CmdArgs.push_back("-x");
1060      CmdArgs.push_back(types::getTypeName(II.getType()));
1061    }
1062
1063    if (II.isPipe())
1064      CmdArgs.push_back("-");
1065    else if (II.isFilename())
1066      CmdArgs.push_back(II.getFilename());
1067    else
1068      // Don't render as input, we need gcc to do the translations.
1069      II.getInputArg().render(Args, CmdArgs);
1070  }
1071
1072  const char *GCCName =
1073    getToolChain().getHost().getDriver().CCCGenericGCCName.c_str();
1074  const char *Exec =
1075    Args.MakeArgString(getToolChain().GetProgramPath(C, GCCName));
1076  Dest.addCommand(new Command(JA, Exec, CmdArgs));
1077}
1078
1079void gcc::Preprocess::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
1080  CmdArgs.push_back("-E");
1081}
1082
1083void gcc::Precompile::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
1084  // The type is good enough.
1085}
1086
1087void gcc::Compile::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
1088  CmdArgs.push_back("-S");
1089}
1090
1091void gcc::Assemble::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
1092  CmdArgs.push_back("-c");
1093}
1094
1095void gcc::Link::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
1096  // The types are (hopefully) good enough.
1097}
1098
1099const char *darwin::CC1::getCC1Name(types::ID Type) const {
1100  switch (Type) {
1101  default:
1102    assert(0 && "Unexpected type for Darwin CC1 tool.");
1103  case types::TY_Asm:
1104  case types::TY_C: case types::TY_CHeader:
1105  case types::TY_PP_C: case types::TY_PP_CHeader:
1106    return "cc1";
1107  case types::TY_ObjC: case types::TY_ObjCHeader:
1108  case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
1109    return "cc1obj";
1110  case types::TY_CXX: case types::TY_CXXHeader:
1111  case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
1112    return "cc1plus";
1113  case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
1114  case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
1115    return "cc1objplus";
1116  }
1117}
1118
1119const char *darwin::CC1::getBaseInputName(const ArgList &Args,
1120                                          const InputInfoList &Inputs) {
1121  llvm::sys::Path P(Inputs[0].getBaseInput());
1122  return Args.MakeArgString(P.getLast());
1123}
1124
1125const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
1126                                          const InputInfoList &Inputs) {
1127  const char *Str = getBaseInputName(Args, Inputs);
1128
1129  if (const char *End = strchr(Str, '.'))
1130    return Args.MakeArgString(std::string(Str, End));
1131
1132  return Str;
1133}
1134
1135const char *
1136darwin::CC1::getDependencyFileName(const ArgList &Args,
1137                                   const InputInfoList &Inputs) {
1138  // FIXME: Think about this more.
1139  std::string Res;
1140
1141  if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1142    std::string Str(OutputOpt->getValue(Args));
1143
1144    Res = Str.substr(0, Str.rfind('.'));
1145  } else
1146    Res = darwin::CC1::getBaseInputStem(Args, Inputs);
1147
1148  return Args.MakeArgString(Res + ".d");
1149}
1150
1151void darwin::CC1::AddCC1Args(const ArgList &Args,
1152                             ArgStringList &CmdArgs) const {
1153  const Driver &D = getToolChain().getHost().getDriver();
1154
1155  CheckCodeGenerationOptions(D, Args);
1156
1157  // Derived from cc1 spec.
1158  if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
1159      !Args.hasArg(options::OPT_mdynamic_no_pic))
1160    CmdArgs.push_back("-fPIC");
1161
1162  if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1163      getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1164    if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1165      CmdArgs.push_back("-fno-builtin-strcat");
1166    if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1167      CmdArgs.push_back("-fno-builtin-strcpy");
1168  }
1169
1170  // gcc has some code here to deal with when no -mmacosx-version-min
1171  // and no -miphoneos-version-min is present, but this never happens
1172  // due to tool chain specific argument translation.
1173
1174  if (Args.hasArg(options::OPT_g_Flag) &&
1175      !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
1176    CmdArgs.push_back("-feliminate-unused-debug-symbols");
1177}
1178
1179void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1180                                    const InputInfoList &Inputs,
1181                                    const ArgStringList &OutputArgs) const {
1182  const Driver &D = getToolChain().getHost().getDriver();
1183
1184  // Derived from cc1_options spec.
1185  if (Args.hasArg(options::OPT_fast) ||
1186      Args.hasArg(options::OPT_fastf) ||
1187      Args.hasArg(options::OPT_fastcp))
1188    CmdArgs.push_back("-O3");
1189
1190  if (Arg *A = Args.getLastArg(options::OPT_pg))
1191    if (Args.hasArg(options::OPT_fomit_frame_pointer))
1192      D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1193        << A->getAsString(Args) << "-fomit-frame-pointer";
1194
1195  AddCC1Args(Args, CmdArgs);
1196
1197  if (!Args.hasArg(options::OPT_Q))
1198    CmdArgs.push_back("-quiet");
1199
1200  CmdArgs.push_back("-dumpbase");
1201  CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1202
1203  Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
1204
1205  Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
1206  Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
1207
1208  // FIXME: The goal is to use the user provided -o if that is our
1209  // final output, otherwise to drive from the original input
1210  // name. Find a clean way to go about this.
1211  if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
1212      Args.hasArg(options::OPT_o)) {
1213    Arg *OutputOpt = Args.getLastArg(options::OPT_o);
1214    CmdArgs.push_back("-auxbase-strip");
1215    CmdArgs.push_back(OutputOpt->getValue(Args));
1216  } else {
1217    CmdArgs.push_back("-auxbase");
1218    CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
1219  }
1220
1221  Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
1222
1223  Args.AddAllArgs(CmdArgs, options::OPT_O);
1224  // FIXME: -Wall is getting some special treatment. Investigate.
1225  Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1226  Args.AddLastArg(CmdArgs, options::OPT_w);
1227  Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
1228                  options::OPT_trigraphs);
1229  if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1230    // Honor -std-default.
1231    Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1232                              "-std=", /*Joined=*/true);
1233  }
1234
1235  if (Args.hasArg(options::OPT_v))
1236    CmdArgs.push_back("-version");
1237  if (Args.hasArg(options::OPT_pg))
1238    CmdArgs.push_back("-p");
1239  Args.AddLastArg(CmdArgs, options::OPT_p);
1240
1241  // The driver treats -fsyntax-only specially.
1242  if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1243      getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1244    // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
1245    // used to inhibit the default -fno-builtin-str{cat,cpy}.
1246    //
1247    // FIXME: Should we grow a better way to deal with "removing" args?
1248    //
1249    // FIXME: Use iterator.
1250    for (ArgList::const_iterator it = Args.begin(),
1251           ie = Args.end(); it != ie; ++it) {
1252      const Arg *A = *it;
1253      if (A->getOption().matches(options::OPT_f_Group) ||
1254          A->getOption().matches(options::OPT_fsyntax_only)) {
1255        if (!A->getOption().matches(options::OPT_fbuiltin_strcat) &&
1256            !A->getOption().matches(options::OPT_fbuiltin_strcpy)) {
1257          A->claim();
1258          A->render(Args, CmdArgs);
1259        }
1260      }
1261    }
1262  } else
1263    Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
1264
1265  Args.AddAllArgs(CmdArgs, options::OPT_undef);
1266  if (Args.hasArg(options::OPT_Qn))
1267    CmdArgs.push_back("-fno-ident");
1268
1269  // FIXME: This isn't correct.
1270  //Args.AddLastArg(CmdArgs, options::OPT__help)
1271  //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
1272
1273  CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1274
1275  // FIXME: Still don't get what is happening here. Investigate.
1276  Args.AddAllArgs(CmdArgs, options::OPT__param);
1277
1278  if (Args.hasArg(options::OPT_fmudflap) ||
1279      Args.hasArg(options::OPT_fmudflapth)) {
1280    CmdArgs.push_back("-fno-builtin");
1281    CmdArgs.push_back("-fno-merge-constants");
1282  }
1283
1284  if (Args.hasArg(options::OPT_coverage)) {
1285    CmdArgs.push_back("-fprofile-arcs");
1286    CmdArgs.push_back("-ftest-coverage");
1287  }
1288
1289  if (types::isCXX(Inputs[0].getType()))
1290    CmdArgs.push_back("-D__private_extern__=extern");
1291}
1292
1293void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1294                                    const InputInfoList &Inputs,
1295                                    const ArgStringList &OutputArgs) const {
1296  // Derived from cpp_options
1297  AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
1298
1299  CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1300
1301  AddCC1Args(Args, CmdArgs);
1302
1303  // NOTE: The code below has some commonality with cpp_options, but
1304  // in classic gcc style ends up sending things in different
1305  // orders. This may be a good merge candidate once we drop pedantic
1306  // compatibility.
1307
1308  Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
1309  Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
1310                  options::OPT_trigraphs);
1311  if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1312    // Honor -std-default.
1313    Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1314                              "-std=", /*Joined=*/true);
1315  }
1316  Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1317  Args.AddLastArg(CmdArgs, options::OPT_w);
1318
1319  // The driver treats -fsyntax-only specially.
1320  Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
1321
1322  if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
1323      !Args.hasArg(options::OPT_fno_working_directory))
1324    CmdArgs.push_back("-fworking-directory");
1325
1326  Args.AddAllArgs(CmdArgs, options::OPT_O);
1327  Args.AddAllArgs(CmdArgs, options::OPT_undef);
1328  if (Args.hasArg(options::OPT_save_temps))
1329    CmdArgs.push_back("-fpch-preprocess");
1330}
1331
1332void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
1333                                          ArgStringList &CmdArgs,
1334                                          const InputInfoList &Inputs) const {
1335  const Driver &D = getToolChain().getHost().getDriver();
1336
1337  CheckPreprocessingOptions(D, Args);
1338
1339  // Derived from cpp_unique_options.
1340  // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
1341  Args.AddLastArg(CmdArgs, options::OPT_C);
1342  Args.AddLastArg(CmdArgs, options::OPT_CC);
1343  if (!Args.hasArg(options::OPT_Q))
1344    CmdArgs.push_back("-quiet");
1345  Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
1346  Args.AddLastArg(CmdArgs, options::OPT_v);
1347  Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
1348  Args.AddLastArg(CmdArgs, options::OPT_P);
1349
1350  // FIXME: Handle %I properly.
1351  if (getToolChain().getArchName() == "x86_64") {
1352    CmdArgs.push_back("-imultilib");
1353    CmdArgs.push_back("x86_64");
1354  }
1355
1356  if (Args.hasArg(options::OPT_MD)) {
1357    CmdArgs.push_back("-MD");
1358    CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1359  }
1360
1361  if (Args.hasArg(options::OPT_MMD)) {
1362    CmdArgs.push_back("-MMD");
1363    CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1364  }
1365
1366  Args.AddLastArg(CmdArgs, options::OPT_M);
1367  Args.AddLastArg(CmdArgs, options::OPT_MM);
1368  Args.AddAllArgs(CmdArgs, options::OPT_MF);
1369  Args.AddLastArg(CmdArgs, options::OPT_MG);
1370  Args.AddLastArg(CmdArgs, options::OPT_MP);
1371  Args.AddAllArgs(CmdArgs, options::OPT_MQ);
1372  Args.AddAllArgs(CmdArgs, options::OPT_MT);
1373  if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
1374      (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
1375    if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1376      CmdArgs.push_back("-MQ");
1377      CmdArgs.push_back(OutputOpt->getValue(Args));
1378    }
1379  }
1380
1381  Args.AddLastArg(CmdArgs, options::OPT_remap);
1382  if (Args.hasArg(options::OPT_g3))
1383    CmdArgs.push_back("-dD");
1384  Args.AddLastArg(CmdArgs, options::OPT_H);
1385
1386  AddCPPArgs(Args, CmdArgs);
1387
1388  Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
1389  Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
1390
1391  for (InputInfoList::const_iterator
1392         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1393    const InputInfo &II = *it;
1394
1395    if (II.isPipe())
1396      CmdArgs.push_back("-");
1397    else
1398      CmdArgs.push_back(II.getFilename());
1399  }
1400
1401  Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
1402                       options::OPT_Xpreprocessor);
1403
1404  if (Args.hasArg(options::OPT_fmudflap)) {
1405    CmdArgs.push_back("-D_MUDFLAP");
1406    CmdArgs.push_back("-include");
1407    CmdArgs.push_back("mf-runtime.h");
1408  }
1409
1410  if (Args.hasArg(options::OPT_fmudflapth)) {
1411    CmdArgs.push_back("-D_MUDFLAP");
1412    CmdArgs.push_back("-D_MUDFLAPTH");
1413    CmdArgs.push_back("-include");
1414    CmdArgs.push_back("mf-runtime.h");
1415  }
1416}
1417
1418void darwin::CC1::AddCPPArgs(const ArgList &Args,
1419                             ArgStringList &CmdArgs) const {
1420  // Derived from cpp spec.
1421
1422  if (Args.hasArg(options::OPT_static)) {
1423    // The gcc spec is broken here, it refers to dynamic but
1424    // that has been translated. Start by being bug compatible.
1425
1426    // if (!Args.hasArg(arglist.parser.dynamicOption))
1427    CmdArgs.push_back("-D__STATIC__");
1428  } else
1429    CmdArgs.push_back("-D__DYNAMIC__");
1430
1431  if (Args.hasArg(options::OPT_pthread))
1432    CmdArgs.push_back("-D_REENTRANT");
1433}
1434
1435void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
1436                                      Job &Dest, const InputInfo &Output,
1437                                      const InputInfoList &Inputs,
1438                                      const ArgList &Args,
1439                                      const char *LinkingOutput) const {
1440  ArgStringList CmdArgs;
1441
1442  assert(Inputs.size() == 1 && "Unexpected number of inputs!");
1443
1444  CmdArgs.push_back("-E");
1445
1446  if (Args.hasArg(options::OPT_traditional) ||
1447      Args.hasArg(options::OPT_traditional_cpp))
1448    CmdArgs.push_back("-traditional-cpp");
1449
1450  ArgStringList OutputArgs;
1451  if (Output.isFilename()) {
1452    OutputArgs.push_back("-o");
1453    OutputArgs.push_back(Output.getFilename());
1454  } else {
1455    assert(Output.isPipe() && "Unexpected CC1 output.");
1456  }
1457
1458  if (Args.hasArg(options::OPT_E)) {
1459    AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
1460  } else {
1461    AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
1462    CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1463  }
1464
1465  Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
1466
1467  const char *CC1Name = getCC1Name(Inputs[0].getType());
1468  const char *Exec =
1469    Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name));
1470  Dest.addCommand(new Command(JA, Exec, CmdArgs));
1471}
1472
1473void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
1474                                   Job &Dest, const InputInfo &Output,
1475                                   const InputInfoList &Inputs,
1476                                   const ArgList &Args,
1477                                   const char *LinkingOutput) const {
1478  const Driver &D = getToolChain().getHost().getDriver();
1479  ArgStringList CmdArgs;
1480
1481  assert(Inputs.size() == 1 && "Unexpected number of inputs!");
1482
1483  types::ID InputType = Inputs[0].getType();
1484  const Arg *A;
1485  if ((A = Args.getLastArg(options::OPT_traditional)))
1486    D.Diag(clang::diag::err_drv_argument_only_allowed_with)
1487      << A->getAsString(Args) << "-E";
1488
1489  if (Output.getType() == types::TY_LLVMAsm)
1490    CmdArgs.push_back("-emit-llvm");
1491  else if (Output.getType() == types::TY_LLVMBC)
1492    CmdArgs.push_back("-emit-llvm-bc");
1493  else if (Output.getType() == types::TY_AST)
1494    D.Diag(clang::diag::err_drv_no_ast_support)
1495      << getToolChain().getTripleString();
1496
1497  ArgStringList OutputArgs;
1498  if (Output.getType() != types::TY_PCH) {
1499    OutputArgs.push_back("-o");
1500    if (Output.isPipe())
1501      OutputArgs.push_back("-");
1502    else if (Output.isNothing())
1503      OutputArgs.push_back("/dev/null");
1504    else
1505      OutputArgs.push_back(Output.getFilename());
1506  }
1507
1508  // There is no need for this level of compatibility, but it makes
1509  // diffing easier.
1510  bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
1511                          Args.hasArg(options::OPT_S));
1512
1513  if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
1514    AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
1515    if (OutputArgsEarly) {
1516      AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
1517    } else {
1518      AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
1519      CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1520    }
1521  } else {
1522    CmdArgs.push_back("-fpreprocessed");
1523
1524    for (InputInfoList::const_iterator
1525           it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1526      const InputInfo &II = *it;
1527
1528      // Reject AST inputs.
1529      if (II.getType() == types::TY_AST) {
1530        D.Diag(clang::diag::err_drv_no_ast_support)
1531          << getToolChain().getTripleString();
1532        return;
1533      }
1534
1535      if (II.isPipe())
1536        CmdArgs.push_back("-");
1537      else
1538        CmdArgs.push_back(II.getFilename());
1539    }
1540
1541    if (OutputArgsEarly) {
1542      AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
1543    } else {
1544      AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
1545      CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1546    }
1547  }
1548
1549  if (Output.getType() == types::TY_PCH) {
1550    assert(Output.isFilename() && "Invalid PCH output.");
1551
1552    CmdArgs.push_back("-o");
1553    // NOTE: gcc uses a temp .s file for this, but there doesn't seem
1554    // to be a good reason.
1555    CmdArgs.push_back("/dev/null");
1556
1557    CmdArgs.push_back("--output-pch=");
1558    CmdArgs.push_back(Output.getFilename());
1559  }
1560
1561  const char *CC1Name = getCC1Name(Inputs[0].getType());
1562  const char *Exec =
1563    Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name));
1564  Dest.addCommand(new Command(JA, Exec, CmdArgs));
1565}
1566
1567void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
1568                                    Job &Dest, const InputInfo &Output,
1569                                    const InputInfoList &Inputs,
1570                                    const ArgList &Args,
1571                                    const char *LinkingOutput) const {
1572  ArgStringList CmdArgs;
1573
1574  assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1575  const InputInfo &Input = Inputs[0];
1576
1577  // Bit of a hack, this is only used for original inputs.
1578  //
1579  // FIXME: This is broken for preprocessed .s inputs.
1580  if (Input.isFilename() &&
1581      strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
1582    if (Args.hasArg(options::OPT_gstabs))
1583      CmdArgs.push_back("--gstabs");
1584    else if (Args.hasArg(options::OPT_g_Group))
1585      CmdArgs.push_back("--gdwarf2");
1586  }
1587
1588  // Derived from asm spec.
1589  AddDarwinArch(Args, CmdArgs);
1590
1591  if (!getDarwinToolChain().isIPhone() ||
1592      Args.hasArg(options::OPT_force__cpusubtype__ALL))
1593    CmdArgs.push_back("-force_cpusubtype_ALL");
1594
1595  if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
1596      (Args.hasArg(options::OPT_mkernel) ||
1597       Args.hasArg(options::OPT_static) ||
1598       Args.hasArg(options::OPT_fapple_kext)))
1599    CmdArgs.push_back("-static");
1600
1601  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1602                       options::OPT_Xassembler);
1603
1604  assert(Output.isFilename() && "Unexpected lipo output.");
1605  CmdArgs.push_back("-o");
1606  CmdArgs.push_back(Output.getFilename());
1607
1608  if (Input.isPipe()) {
1609    CmdArgs.push_back("-");
1610  } else {
1611    assert(Input.isFilename() && "Invalid input.");
1612    CmdArgs.push_back(Input.getFilename());
1613  }
1614
1615  // asm_final spec is empty.
1616
1617  const char *Exec =
1618    Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
1619  Dest.addCommand(new Command(JA, Exec, CmdArgs));
1620}
1621
1622/// Helper routine for seeing if we should use dsymutil; this is a
1623/// gcc compatible hack, we should remove it and use the input
1624/// type information.
1625static bool isSourceSuffix(const char *Str) {
1626  // match: 'C', 'CPP', 'c', 'cc', 'cp', 'c++', 'cpp', 'cxx', 'm',
1627  // 'mm'.
1628  switch (strlen(Str)) {
1629  default:
1630    return false;
1631  case 1:
1632    return (memcmp(Str, "C", 1) == 0 ||
1633            memcmp(Str, "c", 1) == 0 ||
1634            memcmp(Str, "m", 1) == 0);
1635  case 2:
1636    return (memcmp(Str, "cc", 2) == 0 ||
1637            memcmp(Str, "cp", 2) == 0 ||
1638            memcmp(Str, "mm", 2) == 0);
1639  case 3:
1640    return (memcmp(Str, "CPP", 3) == 0 ||
1641            memcmp(Str, "c++", 3) == 0 ||
1642            memcmp(Str, "cpp", 3) == 0 ||
1643            memcmp(Str, "cxx", 3) == 0);
1644  }
1645}
1646
1647static bool isMacosxVersionLT(unsigned (&A)[3], unsigned (&B)[3]) {
1648  for (unsigned i=0; i < 3; ++i) {
1649    if (A[i] > B[i]) return false;
1650    if (A[i] < B[i]) return true;
1651  }
1652  return false;
1653}
1654
1655static bool isMacosxVersionLT(unsigned (&A)[3],
1656                              unsigned V0, unsigned V1=0, unsigned V2=0) {
1657  unsigned B[3] = { V0, V1, V2 };
1658  return isMacosxVersionLT(A, B);
1659}
1660
1661// FIXME: Can we tablegen this?
1662static const char *GetArmArchForMArch(llvm::StringRef Value) {
1663  if (Value == "armv6k")
1664    return "armv6";
1665
1666  if (Value == "armv5tej")
1667    return "armv5";
1668
1669  if (Value == "xscale")
1670    return "xscale";
1671
1672  if (Value == "armv4t")
1673    return "armv4t";
1674
1675  if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
1676      Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
1677      Value == "armv7m")
1678    return "armv7";
1679
1680  return 0;
1681}
1682
1683// FIXME: Can we tablegen this?
1684static const char *GetArmArchForMCpu(llvm::StringRef Value) {
1685  if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
1686      Value == "arm946e-s" || Value == "arm966e-s" ||
1687      Value == "arm968e-s" || Value == "arm10e" ||
1688      Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
1689      Value == "arm1026ej-s")
1690    return "armv5";
1691
1692  if (Value == "xscale")
1693    return "xscale";
1694
1695  if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
1696      Value == "arm1176jz-s" || Value == "arm1176jzf-s")
1697    return "armv6";
1698
1699  if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
1700    return "armv7";
1701
1702  return 0;
1703}
1704
1705void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
1706                                       ArgStringList &CmdArgs) const {
1707  // Derived from darwin_arch spec.
1708  CmdArgs.push_back("-arch");
1709
1710  switch (getToolChain().getTriple().getArch()) {
1711  default:
1712    CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
1713    break;
1714
1715  case llvm::Triple::arm: {
1716    if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
1717      if (const char *Arch = GetArmArchForMArch(A->getValue(Args))) {
1718        CmdArgs.push_back(Arch);
1719        return;
1720      }
1721    }
1722
1723    if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1724      if (const char *Arch = GetArmArchForMCpu(A->getValue(Args))) {
1725        CmdArgs.push_back(Arch);
1726        return;
1727      }
1728    }
1729
1730    CmdArgs.push_back("arm");
1731    CmdArgs.push_back("-force_cpusubtype_ALL");
1732    return;
1733  }
1734  }
1735}
1736
1737void darwin::DarwinTool::AddDarwinSubArch(const ArgList &Args,
1738                                          ArgStringList &CmdArgs) const {
1739  // Derived from darwin_subarch spec, not sure what the distinction
1740  // exists for but at least for this chain it is the same.
1741  AddDarwinArch(Args, CmdArgs);
1742}
1743
1744void darwin::Link::AddLinkArgs(const ArgList &Args,
1745                               ArgStringList &CmdArgs) const {
1746  const Driver &D = getToolChain().getHost().getDriver();
1747
1748  // Derived from the "link" spec.
1749  Args.AddAllArgs(CmdArgs, options::OPT_static);
1750  if (!Args.hasArg(options::OPT_static))
1751    CmdArgs.push_back("-dynamic");
1752  if (Args.hasArg(options::OPT_fgnu_runtime)) {
1753    // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
1754    // here. How do we wish to handle such things?
1755  }
1756
1757  if (!Args.hasArg(options::OPT_dynamiclib)) {
1758    if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) {
1759      AddDarwinArch(Args, CmdArgs);
1760      CmdArgs.push_back("-force_cpusubtype_ALL");
1761    } else
1762      AddDarwinSubArch(Args, CmdArgs);
1763
1764    Args.AddLastArg(CmdArgs, options::OPT_bundle);
1765    Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
1766    Args.AddAllArgs(CmdArgs, options::OPT_client__name);
1767
1768    Arg *A;
1769    if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
1770        (A = Args.getLastArg(options::OPT_current__version)) ||
1771        (A = Args.getLastArg(options::OPT_install__name)))
1772      D.Diag(clang::diag::err_drv_argument_only_allowed_with)
1773        << A->getAsString(Args) << "-dynamiclib";
1774
1775    Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
1776    Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
1777    Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
1778  } else {
1779    CmdArgs.push_back("-dylib");
1780
1781    Arg *A;
1782    if ((A = Args.getLastArg(options::OPT_bundle)) ||
1783        (A = Args.getLastArg(options::OPT_bundle__loader)) ||
1784        (A = Args.getLastArg(options::OPT_client__name)) ||
1785        (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
1786        (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
1787        (A = Args.getLastArg(options::OPT_private__bundle)))
1788      D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1789        << A->getAsString(Args) << "-dynamiclib";
1790
1791    Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
1792                              "-dylib_compatibility_version");
1793    Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
1794                              "-dylib_current_version");
1795
1796    if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) {
1797      AddDarwinArch(Args, CmdArgs);
1798      // NOTE: We don't add -force_cpusubtype_ALL on this path. Ok.
1799    } else
1800      AddDarwinSubArch(Args, CmdArgs);
1801
1802    Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
1803                              "-dylib_install_name");
1804  }
1805
1806  Args.AddLastArg(CmdArgs, options::OPT_all__load);
1807  Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
1808  Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
1809  if (getDarwinToolChain().isIPhone())
1810    Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
1811  Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
1812  Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
1813  Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
1814  Args.AddLastArg(CmdArgs, options::OPT_dynamic);
1815  Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
1816  Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
1817  Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
1818  Args.AddAllArgs(CmdArgs, options::OPT_image__base);
1819  Args.AddAllArgs(CmdArgs, options::OPT_init);
1820
1821  if (!Args.hasArg(options::OPT_mmacosx_version_min_EQ) &&
1822      !Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
1823    // Add default version min.
1824    if (!getDarwinToolChain().isIPhone()) {
1825      CmdArgs.push_back("-macosx_version_min");
1826      CmdArgs.push_back(getDarwinToolChain().getMacosxVersionStr());
1827    } else {
1828      CmdArgs.push_back("-iphoneos_version_min");
1829      CmdArgs.push_back(getDarwinToolChain().getIPhoneOSVersionStr());
1830    }
1831  }
1832
1833  // Adding all arguments doesn't make sense here but this is what
1834  // gcc does.
1835  Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
1836                              "-macosx_version_min");
1837  Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
1838                            "-iphoneos_version_min");
1839  Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
1840  Args.AddLastArg(CmdArgs, options::OPT_multi__module);
1841  Args.AddLastArg(CmdArgs, options::OPT_single__module);
1842  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
1843  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
1844
1845  if (Args.hasArg(options::OPT_fpie))
1846    CmdArgs.push_back("-pie");
1847
1848  Args.AddLastArg(CmdArgs, options::OPT_prebind);
1849  Args.AddLastArg(CmdArgs, options::OPT_noprebind);
1850  Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
1851  Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
1852  Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
1853  Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
1854  Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
1855  Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
1856  Args.AddAllArgs(CmdArgs, options::OPT_segprot);
1857  Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
1858  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
1859  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
1860  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
1861  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
1862  Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
1863  Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
1864
1865  Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
1866  if (getDarwinToolChain().isIPhone()) {
1867    if (!Args.hasArg(options::OPT_isysroot)) {
1868      CmdArgs.push_back("-syslibroot");
1869      CmdArgs.push_back("/Developer/SDKs/Extra");
1870    }
1871  }
1872
1873  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
1874  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
1875  Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
1876  Args.AddAllArgs(CmdArgs, options::OPT_undefined);
1877  Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
1878
1879  Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
1880  if (!Args.hasArg(options::OPT_weak__reference__mismatches)) {
1881    CmdArgs.push_back("-weak_reference_mismatches");
1882    CmdArgs.push_back("non-weak");
1883  }
1884
1885  Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
1886  Args.AddAllArgs(CmdArgs, options::OPT_y);
1887  Args.AddLastArg(CmdArgs, options::OPT_w);
1888  Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
1889  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
1890  Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
1891  Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
1892  Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
1893  Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
1894  Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
1895  Args.AddLastArg(CmdArgs, options::OPT_whyload);
1896  Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
1897  Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
1898  Args.AddLastArg(CmdArgs, options::OPT_dylinker);
1899  Args.AddLastArg(CmdArgs, options::OPT_Mach);
1900}
1901
1902void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
1903                                Job &Dest, const InputInfo &Output,
1904                                const InputInfoList &Inputs,
1905                                const ArgList &Args,
1906                                const char *LinkingOutput) const {
1907  assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
1908
1909  // The logic here is derived from gcc's behavior; most of which
1910  // comes from specs (starting with link_command). Consult gcc for
1911  // more information.
1912  ArgStringList CmdArgs;
1913
1914  // I'm not sure why this particular decomposition exists in gcc, but
1915  // we follow suite for ease of comparison.
1916  AddLinkArgs(Args, CmdArgs);
1917
1918  Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
1919  Args.AddAllArgs(CmdArgs, options::OPT_s);
1920  Args.AddAllArgs(CmdArgs, options::OPT_t);
1921  Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
1922  Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
1923  Args.AddAllArgs(CmdArgs, options::OPT_A);
1924  Args.AddLastArg(CmdArgs, options::OPT_e);
1925  Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
1926  Args.AddAllArgs(CmdArgs, options::OPT_r);
1927
1928  CmdArgs.push_back("-o");
1929  CmdArgs.push_back(Output.getFilename());
1930
1931  unsigned MacosxVersionMin[3];
1932  if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
1933    bool HadExtra;
1934    if (!Driver::GetReleaseVersion(A->getValue(Args), MacosxVersionMin[0],
1935                                   MacosxVersionMin[1], MacosxVersionMin[2],
1936                                   HadExtra) ||
1937        HadExtra) {
1938      const Driver &D = getToolChain().getHost().getDriver();
1939      D.Diag(clang::diag::err_drv_invalid_version_number)
1940        << A->getAsString(Args);
1941    }
1942  } else {
1943    getDarwinToolChain().getMacosxVersion(MacosxVersionMin);
1944  }
1945
1946  if (!Args.hasArg(options::OPT_A) &&
1947      !Args.hasArg(options::OPT_nostdlib) &&
1948      !Args.hasArg(options::OPT_nostartfiles)) {
1949    // Derived from startfile spec.
1950    if (Args.hasArg(options::OPT_dynamiclib)) {
1951      // Derived from darwin_dylib1 spec.
1952      if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
1953        CmdArgs.push_back("-ldylib1.o");
1954      else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
1955        CmdArgs.push_back("-ldylib1.10.5.o");
1956    } else {
1957      if (Args.hasArg(options::OPT_bundle)) {
1958        if (!Args.hasArg(options::OPT_static)) {
1959          // Derived from darwin_bundle1 spec.
1960          if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
1961            CmdArgs.push_back("-lbundle1.o");
1962        }
1963      } else {
1964        if (Args.hasArg(options::OPT_pg)) {
1965          if (Args.hasArg(options::OPT_static) ||
1966              Args.hasArg(options::OPT_object) ||
1967              Args.hasArg(options::OPT_preload)) {
1968            CmdArgs.push_back("-lgcrt0.o");
1969          } else {
1970            CmdArgs.push_back("-lgcrt1.o");
1971
1972            // darwin_crt2 spec is empty.
1973          }
1974        } else {
1975          if (Args.hasArg(options::OPT_static) ||
1976              Args.hasArg(options::OPT_object) ||
1977              Args.hasArg(options::OPT_preload)) {
1978            CmdArgs.push_back("-lcrt0.o");
1979          } else {
1980            // Derived from darwin_crt1 spec.
1981            if (getDarwinToolChain().isIPhone()) {
1982              CmdArgs.push_back("-lcrt1.o");
1983            } else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
1984              CmdArgs.push_back("-lcrt1.o");
1985            else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
1986              CmdArgs.push_back("-lcrt1.10.5.o");
1987            else
1988              CmdArgs.push_back("-lcrt1.10.6.o");
1989
1990            // darwin_crt2 spec is empty.
1991          }
1992        }
1993      }
1994    }
1995
1996    if (Args.hasArg(options::OPT_shared_libgcc) &&
1997        !Args.hasArg(options::OPT_miphoneos_version_min_EQ) &&
1998        isMacosxVersionLT(MacosxVersionMin, 10, 5)) {
1999      const char *Str =
2000        Args.MakeArgString(getToolChain().GetFilePath(C, "crt3.o"));
2001      CmdArgs.push_back(Str);
2002    }
2003  }
2004
2005  Args.AddAllArgs(CmdArgs, options::OPT_L);
2006
2007  if (Args.hasArg(options::OPT_fopenmp))
2008    // This is more complicated in gcc...
2009    CmdArgs.push_back("-lgomp");
2010
2011  getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
2012
2013  for (InputInfoList::const_iterator
2014         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2015    const InputInfo &II = *it;
2016    if (II.isFilename())
2017      CmdArgs.push_back(II.getFilename());
2018    else
2019      II.getInputArg().renderAsInput(Args, CmdArgs);
2020  }
2021
2022  if (LinkingOutput) {
2023    CmdArgs.push_back("-arch_multiple");
2024    CmdArgs.push_back("-final_output");
2025    CmdArgs.push_back(LinkingOutput);
2026  }
2027
2028  if (Args.hasArg(options::OPT_fprofile_arcs) ||
2029      Args.hasArg(options::OPT_fprofile_generate) ||
2030      Args.hasArg(options::OPT_fcreate_profile) ||
2031      Args.hasArg(options::OPT_coverage))
2032    CmdArgs.push_back("-lgcov");
2033
2034  if (Args.hasArg(options::OPT_fnested_functions))
2035    CmdArgs.push_back("-allow_stack_execute");
2036
2037  if (!Args.hasArg(options::OPT_nostdlib) &&
2038      !Args.hasArg(options::OPT_nodefaultlibs)) {
2039    // FIXME: g++ is more complicated here, it tries to put -lstdc++
2040    // before -lm, for example.
2041    if (getToolChain().getHost().getDriver().CCCIsCXX)
2042      CmdArgs.push_back("-lstdc++");
2043
2044    // link_ssp spec is empty.
2045
2046    // Derived from libgcc and lib specs but refactored.
2047    if (Args.hasArg(options::OPT_static)) {
2048      CmdArgs.push_back("-lgcc_static");
2049    } else {
2050      if (Args.hasArg(options::OPT_static_libgcc)) {
2051        CmdArgs.push_back("-lgcc_eh");
2052      } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
2053        // Derived from darwin_iphoneos_libgcc spec.
2054        if (getDarwinToolChain().isIPhone()) {
2055          CmdArgs.push_back("-lgcc_s.1");
2056        } else {
2057          CmdArgs.push_back("-lgcc_s.10.5");
2058        }
2059      } else if (Args.hasArg(options::OPT_shared_libgcc) ||
2060                 // FIXME: -fexceptions -fno-exceptions means no exceptions
2061                 Args.hasArg(options::OPT_fexceptions) ||
2062                 Args.hasArg(options::OPT_fgnu_runtime)) {
2063        // FIXME: This is probably broken on 10.3?
2064        if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
2065          CmdArgs.push_back("-lgcc_s.10.4");
2066        else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
2067          CmdArgs.push_back("-lgcc_s.10.5");
2068      } else {
2069        if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
2070          ; // Do nothing.
2071        else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
2072          CmdArgs.push_back("-lgcc_s.10.4");
2073        else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
2074          CmdArgs.push_back("-lgcc_s.10.5");
2075      }
2076
2077      if (getDarwinToolChain().isIPhone() ||
2078          isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
2079        CmdArgs.push_back("-lgcc");
2080        CmdArgs.push_back("-lSystem");
2081      } else {
2082        CmdArgs.push_back("-lSystem");
2083        CmdArgs.push_back("-lgcc");
2084      }
2085    }
2086  }
2087
2088  if (!Args.hasArg(options::OPT_A) &&
2089      !Args.hasArg(options::OPT_nostdlib) &&
2090      !Args.hasArg(options::OPT_nostartfiles)) {
2091    // endfile_spec is empty.
2092  }
2093
2094  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2095  Args.AddAllArgs(CmdArgs, options::OPT_F);
2096
2097  const char *Exec =
2098    Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2099  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2100
2101  // Find the first non-empty base input (we want to ignore linker
2102  // inputs).
2103  const char *BaseInput = "";
2104  for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
2105    if (Inputs[i].getBaseInput()[0] != '\0') {
2106      BaseInput = Inputs[i].getBaseInput();
2107      break;
2108    }
2109  }
2110
2111  // Run dsymutil if we are making an executable in a single step.
2112  //
2113  // FIXME: Currently we don't want to do this when we are part of a
2114  // universal build step, as this would end up creating stray temp
2115  // files.
2116  if (!LinkingOutput &&
2117      Args.getLastArg(options::OPT_g_Group) &&
2118      !Args.getLastArg(options::OPT_gstabs) &&
2119      !Args.getLastArg(options::OPT_g0)) {
2120    // FIXME: This is gross, but matches gcc. The test only considers
2121    // the suffix (not the -x type), and then only of the first
2122    // source input. Awesome.
2123    const char *Suffix = strrchr(BaseInput, '.');
2124    if (Suffix && isSourceSuffix(Suffix + 1)) {
2125      const char *Exec =
2126        Args.MakeArgString(getToolChain().GetProgramPath(C, "dsymutil"));
2127      ArgStringList CmdArgs;
2128      CmdArgs.push_back(Output.getFilename());
2129      C.getJobs().addCommand(new Command(JA, Exec, CmdArgs));
2130    }
2131  }
2132}
2133
2134void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
2135                                Job &Dest, const InputInfo &Output,
2136                                const InputInfoList &Inputs,
2137                                const ArgList &Args,
2138                                const char *LinkingOutput) const {
2139  ArgStringList CmdArgs;
2140
2141  CmdArgs.push_back("-create");
2142  assert(Output.isFilename() && "Unexpected lipo output.");
2143
2144  CmdArgs.push_back("-output");
2145  CmdArgs.push_back(Output.getFilename());
2146
2147  for (InputInfoList::const_iterator
2148         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2149    const InputInfo &II = *it;
2150    assert(II.isFilename() && "Unexpected lipo input.");
2151    CmdArgs.push_back(II.getFilename());
2152  }
2153  const char *Exec =
2154    Args.MakeArgString(getToolChain().GetProgramPath(C, "lipo"));
2155  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2156}
2157
2158void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2159                                     Job &Dest, const InputInfo &Output,
2160                                     const InputInfoList &Inputs,
2161                                     const ArgList &Args,
2162                                     const char *LinkingOutput) const {
2163  ArgStringList CmdArgs;
2164
2165  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2166                       options::OPT_Xassembler);
2167
2168  CmdArgs.push_back("-o");
2169  if (Output.isPipe())
2170    CmdArgs.push_back("-");
2171  else
2172    CmdArgs.push_back(Output.getFilename());
2173
2174  for (InputInfoList::const_iterator
2175         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2176    const InputInfo &II = *it;
2177    if (II.isPipe())
2178      CmdArgs.push_back("-");
2179    else
2180      CmdArgs.push_back(II.getFilename());
2181  }
2182
2183  const char *Exec =
2184    Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
2185  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2186}
2187
2188void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
2189                                 Job &Dest, const InputInfo &Output,
2190                                 const InputInfoList &Inputs,
2191                                 const ArgList &Args,
2192                                 const char *LinkingOutput) const {
2193  const Driver &D = getToolChain().getHost().getDriver();
2194  ArgStringList CmdArgs;
2195
2196  if ((!Args.hasArg(options::OPT_nostdlib)) &&
2197     (!Args.hasArg(options::OPT_shared))) {
2198    CmdArgs.push_back("-e");
2199    CmdArgs.push_back("__start");
2200  }
2201
2202  if (Args.hasArg(options::OPT_static)) {
2203    CmdArgs.push_back("-Bstatic");
2204  } else {
2205    CmdArgs.push_back("--eh-frame-hdr");
2206    CmdArgs.push_back("-Bdynamic");
2207    if (Args.hasArg(options::OPT_shared)) {
2208      CmdArgs.push_back("-shared");
2209    } else {
2210      CmdArgs.push_back("-dynamic-linker");
2211      CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
2212    }
2213  }
2214
2215  if (Output.isPipe()) {
2216    CmdArgs.push_back("-o");
2217    CmdArgs.push_back("-");
2218  } else if (Output.isFilename()) {
2219    CmdArgs.push_back("-o");
2220    CmdArgs.push_back(Output.getFilename());
2221  } else {
2222    assert(Output.isNothing() && "Invalid output.");
2223  }
2224
2225  if (!Args.hasArg(options::OPT_nostdlib) &&
2226      !Args.hasArg(options::OPT_nostartfiles)) {
2227    if (!Args.hasArg(options::OPT_shared)) {
2228      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt0.o")));
2229      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
2230    } else {
2231      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
2232    }
2233  }
2234
2235  CmdArgs.push_back(MakeFormattedString(Args,
2236                           llvm::format("-L/opt/gcc4/lib/gcc/%s/4.2.4",
2237                           getToolChain().getTripleString().c_str())));
2238
2239  Args.AddAllArgs(CmdArgs, options::OPT_L);
2240  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2241  Args.AddAllArgs(CmdArgs, options::OPT_e);
2242
2243  for (InputInfoList::const_iterator
2244         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2245    const InputInfo &II = *it;
2246
2247    // Don't try to pass LLVM inputs to a generic gcc.
2248    if (II.getType() == types::TY_LLVMBC)
2249      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2250        << getToolChain().getTripleString();
2251
2252    if (II.isPipe())
2253      CmdArgs.push_back("-");
2254    else if (II.isFilename())
2255      CmdArgs.push_back(II.getFilename());
2256    else
2257      II.getInputArg().renderAsInput(Args, CmdArgs);
2258  }
2259
2260  if (!Args.hasArg(options::OPT_nostdlib) &&
2261      !Args.hasArg(options::OPT_nodefaultlibs)) {
2262    // FIXME: For some reason GCC passes -lgcc before adding
2263    // the default system libraries. Just mimic this for now.
2264    CmdArgs.push_back("-lgcc");
2265
2266    if (Args.hasArg(options::OPT_pthread))
2267      CmdArgs.push_back("-pthread");
2268    if (!Args.hasArg(options::OPT_shared))
2269      CmdArgs.push_back("-lc");
2270    CmdArgs.push_back("-lgcc");
2271  }
2272
2273  if (!Args.hasArg(options::OPT_nostdlib) &&
2274      !Args.hasArg(options::OPT_nostartfiles)) {
2275    if (!Args.hasArg(options::OPT_shared))
2276      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
2277    else
2278      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
2279  }
2280
2281  const char *Exec =
2282    Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2283  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2284}
2285
2286void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2287                                     Job &Dest, const InputInfo &Output,
2288                                     const InputInfoList &Inputs,
2289                                     const ArgList &Args,
2290                                     const char *LinkingOutput) const {
2291  ArgStringList CmdArgs;
2292
2293  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2294                       options::OPT_Xassembler);
2295
2296  CmdArgs.push_back("-o");
2297  if (Output.isPipe())
2298    CmdArgs.push_back("-");
2299  else
2300    CmdArgs.push_back(Output.getFilename());
2301
2302  for (InputInfoList::const_iterator
2303         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2304    const InputInfo &II = *it;
2305    if (II.isPipe())
2306      CmdArgs.push_back("-");
2307    else
2308      CmdArgs.push_back(II.getFilename());
2309  }
2310
2311  const char *Exec =
2312    Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
2313  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2314}
2315
2316void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
2317                                 Job &Dest, const InputInfo &Output,
2318                                 const InputInfoList &Inputs,
2319                                 const ArgList &Args,
2320                                 const char *LinkingOutput) const {
2321  const Driver &D = getToolChain().getHost().getDriver();
2322  ArgStringList CmdArgs;
2323
2324  if ((!Args.hasArg(options::OPT_nostdlib)) &&
2325     (!Args.hasArg(options::OPT_shared))) {
2326    CmdArgs.push_back("-e");
2327    CmdArgs.push_back("__start");
2328  }
2329
2330  if (Args.hasArg(options::OPT_static)) {
2331    CmdArgs.push_back("-Bstatic");
2332  } else {
2333    CmdArgs.push_back("--eh-frame-hdr");
2334    CmdArgs.push_back("-Bdynamic");
2335    if (Args.hasArg(options::OPT_shared)) {
2336      CmdArgs.push_back("-shared");
2337    } else {
2338      CmdArgs.push_back("-dynamic-linker");
2339      CmdArgs.push_back("/usr/libexec/ld.so");
2340    }
2341  }
2342
2343  if (Output.isPipe()) {
2344    CmdArgs.push_back("-o");
2345    CmdArgs.push_back("-");
2346  } else if (Output.isFilename()) {
2347    CmdArgs.push_back("-o");
2348    CmdArgs.push_back(Output.getFilename());
2349  } else {
2350    assert(Output.isNothing() && "Invalid output.");
2351  }
2352
2353  if (!Args.hasArg(options::OPT_nostdlib) &&
2354      !Args.hasArg(options::OPT_nostartfiles)) {
2355    if (!Args.hasArg(options::OPT_shared)) {
2356      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt0.o")));
2357      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
2358    } else {
2359      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
2360    }
2361  }
2362
2363  CmdArgs.push_back(MakeFormattedString(Args,
2364                           llvm::format("-L/usr/lib/gcc-lib/%s/3.3.5",
2365                                    getToolChain().getTripleString().c_str())));
2366
2367  Args.AddAllArgs(CmdArgs, options::OPT_L);
2368  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2369  Args.AddAllArgs(CmdArgs, options::OPT_e);
2370
2371  for (InputInfoList::const_iterator
2372         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2373    const InputInfo &II = *it;
2374
2375    // Don't try to pass LLVM inputs to a generic gcc.
2376    if (II.getType() == types::TY_LLVMBC)
2377      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2378        << getToolChain().getTripleString();
2379
2380    if (II.isPipe())
2381      CmdArgs.push_back("-");
2382    else if (II.isFilename())
2383      CmdArgs.push_back(II.getFilename());
2384    else
2385      II.getInputArg().renderAsInput(Args, CmdArgs);
2386  }
2387
2388  if (!Args.hasArg(options::OPT_nostdlib) &&
2389      !Args.hasArg(options::OPT_nodefaultlibs)) {
2390    // FIXME: For some reason GCC passes -lgcc before adding
2391    // the default system libraries. Just mimic this for now.
2392    CmdArgs.push_back("-lgcc");
2393
2394    if (Args.hasArg(options::OPT_pthread))
2395      CmdArgs.push_back("-pthread");
2396    if (!Args.hasArg(options::OPT_shared))
2397      CmdArgs.push_back("-lc");
2398    CmdArgs.push_back("-lgcc");
2399  }
2400
2401  if (!Args.hasArg(options::OPT_nostdlib) &&
2402      !Args.hasArg(options::OPT_nostartfiles)) {
2403    if (!Args.hasArg(options::OPT_shared))
2404      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
2405    else
2406      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
2407  }
2408
2409  const char *Exec =
2410    Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2411  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2412}
2413
2414void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2415                                     Job &Dest, const InputInfo &Output,
2416                                     const InputInfoList &Inputs,
2417                                     const ArgList &Args,
2418                                     const char *LinkingOutput) const {
2419  ArgStringList CmdArgs;
2420
2421  // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2422  // instruct as in the base system to assemble 32-bit code.
2423  if (getToolChain().getArchName() == "i386")
2424    CmdArgs.push_back("--32");
2425
2426  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2427                       options::OPT_Xassembler);
2428
2429  CmdArgs.push_back("-o");
2430  if (Output.isPipe())
2431    CmdArgs.push_back("-");
2432  else
2433    CmdArgs.push_back(Output.getFilename());
2434
2435  for (InputInfoList::const_iterator
2436         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2437    const InputInfo &II = *it;
2438    if (II.isPipe())
2439      CmdArgs.push_back("-");
2440    else
2441      CmdArgs.push_back(II.getFilename());
2442  }
2443
2444  const char *Exec =
2445    Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
2446  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2447}
2448
2449void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
2450                                 Job &Dest, const InputInfo &Output,
2451                                 const InputInfoList &Inputs,
2452                                 const ArgList &Args,
2453                                 const char *LinkingOutput) const {
2454  const Driver &D = getToolChain().getHost().getDriver();
2455  ArgStringList CmdArgs;
2456
2457  if (Args.hasArg(options::OPT_static)) {
2458    CmdArgs.push_back("-Bstatic");
2459  } else {
2460    CmdArgs.push_back("--eh-frame-hdr");
2461    if (Args.hasArg(options::OPT_shared)) {
2462      CmdArgs.push_back("-Bshareable");
2463    } else {
2464      CmdArgs.push_back("-dynamic-linker");
2465      CmdArgs.push_back("/libexec/ld-elf.so.1");
2466    }
2467  }
2468
2469  // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2470  // instruct ld in the base system to link 32-bit code.
2471  if (getToolChain().getArchName() == "i386") {
2472    CmdArgs.push_back("-m");
2473    CmdArgs.push_back("elf_i386_fbsd");
2474  }
2475
2476  if (Output.isPipe()) {
2477    CmdArgs.push_back("-o");
2478    CmdArgs.push_back("-");
2479  } else if (Output.isFilename()) {
2480    CmdArgs.push_back("-o");
2481    CmdArgs.push_back(Output.getFilename());
2482  } else {
2483    assert(Output.isNothing() && "Invalid output.");
2484  }
2485
2486  if (!Args.hasArg(options::OPT_nostdlib) &&
2487      !Args.hasArg(options::OPT_nostartfiles)) {
2488    if (!Args.hasArg(options::OPT_shared)) {
2489      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
2490      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2491      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
2492    } else {
2493      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2494      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
2495    }
2496  }
2497
2498  Args.AddAllArgs(CmdArgs, options::OPT_L);
2499  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2500  Args.AddAllArgs(CmdArgs, options::OPT_e);
2501
2502  for (InputInfoList::const_iterator
2503         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2504    const InputInfo &II = *it;
2505
2506    // Don't try to pass LLVM inputs to a generic gcc.
2507    if (II.getType() == types::TY_LLVMBC)
2508      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2509        << getToolChain().getTripleString();
2510
2511    if (II.isPipe())
2512      CmdArgs.push_back("-");
2513    else if (II.isFilename())
2514      CmdArgs.push_back(II.getFilename());
2515    else
2516      II.getInputArg().renderAsInput(Args, CmdArgs);
2517  }
2518
2519  if (!Args.hasArg(options::OPT_nostdlib) &&
2520      !Args.hasArg(options::OPT_nodefaultlibs)) {
2521    // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
2522    // the default system libraries. Just mimic this for now.
2523    CmdArgs.push_back("-lgcc");
2524    if (Args.hasArg(options::OPT_static)) {
2525      CmdArgs.push_back("-lgcc_eh");
2526    } else {
2527      CmdArgs.push_back("--as-needed");
2528      CmdArgs.push_back("-lgcc_s");
2529      CmdArgs.push_back("--no-as-needed");
2530    }
2531
2532    if (Args.hasArg(options::OPT_pthread))
2533      CmdArgs.push_back("-lpthread");
2534    CmdArgs.push_back("-lc");
2535
2536    CmdArgs.push_back("-lgcc");
2537    if (Args.hasArg(options::OPT_static)) {
2538      CmdArgs.push_back("-lgcc_eh");
2539    } else {
2540      CmdArgs.push_back("--as-needed");
2541      CmdArgs.push_back("-lgcc_s");
2542      CmdArgs.push_back("--no-as-needed");
2543    }
2544  }
2545
2546  if (!Args.hasArg(options::OPT_nostdlib) &&
2547      !Args.hasArg(options::OPT_nostartfiles)) {
2548    if (!Args.hasArg(options::OPT_shared))
2549      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
2550    else
2551      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
2552    CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
2553  }
2554
2555  const char *Exec =
2556    Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2557  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2558}
2559
2560/// DragonFly Tools
2561
2562// For now, DragonFly Assemble does just about the same as for
2563// FreeBSD, but this may change soon.
2564void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2565                                     Job &Dest, const InputInfo &Output,
2566                                     const InputInfoList &Inputs,
2567                                     const ArgList &Args,
2568                                     const char *LinkingOutput) const {
2569  ArgStringList CmdArgs;
2570
2571  // When building 32-bit code on DragonFly/pc64, we have to explicitly
2572  // instruct as in the base system to assemble 32-bit code.
2573  if (getToolChain().getArchName() == "i386")
2574    CmdArgs.push_back("--32");
2575
2576  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2577                       options::OPT_Xassembler);
2578
2579  CmdArgs.push_back("-o");
2580  if (Output.isPipe())
2581    CmdArgs.push_back("-");
2582  else
2583    CmdArgs.push_back(Output.getFilename());
2584
2585  for (InputInfoList::const_iterator
2586         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2587    const InputInfo &II = *it;
2588    if (II.isPipe())
2589      CmdArgs.push_back("-");
2590    else
2591      CmdArgs.push_back(II.getFilename());
2592  }
2593
2594  const char *Exec =
2595    Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
2596  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2597}
2598
2599void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
2600                                 Job &Dest, const InputInfo &Output,
2601                                 const InputInfoList &Inputs,
2602                                 const ArgList &Args,
2603                                 const char *LinkingOutput) const {
2604  const Driver &D = getToolChain().getHost().getDriver();
2605  ArgStringList CmdArgs;
2606
2607  if (Args.hasArg(options::OPT_static)) {
2608    CmdArgs.push_back("-Bstatic");
2609  } else {
2610    if (Args.hasArg(options::OPT_shared))
2611      CmdArgs.push_back("-Bshareable");
2612    else {
2613      CmdArgs.push_back("-dynamic-linker");
2614      CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
2615    }
2616  }
2617
2618  // When building 32-bit code on DragonFly/pc64, we have to explicitly
2619  // instruct ld in the base system to link 32-bit code.
2620  if (getToolChain().getArchName() == "i386") {
2621    CmdArgs.push_back("-m");
2622    CmdArgs.push_back("elf_i386");
2623  }
2624
2625  if (Output.isPipe()) {
2626    CmdArgs.push_back("-o");
2627    CmdArgs.push_back("-");
2628  } else if (Output.isFilename()) {
2629    CmdArgs.push_back("-o");
2630    CmdArgs.push_back(Output.getFilename());
2631  } else {
2632    assert(Output.isNothing() && "Invalid output.");
2633  }
2634
2635  if (!Args.hasArg(options::OPT_nostdlib) &&
2636      !Args.hasArg(options::OPT_nostartfiles)) {
2637    if (!Args.hasArg(options::OPT_shared)) {
2638      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
2639      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2640      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
2641    } else {
2642      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2643      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
2644    }
2645  }
2646
2647  Args.AddAllArgs(CmdArgs, options::OPT_L);
2648  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2649  Args.AddAllArgs(CmdArgs, options::OPT_e);
2650
2651  for (InputInfoList::const_iterator
2652         it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2653    const InputInfo &II = *it;
2654
2655    // Don't try to pass LLVM inputs to a generic gcc.
2656    if (II.getType() == types::TY_LLVMBC)
2657      D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2658        << getToolChain().getTripleString();
2659
2660    if (II.isPipe())
2661      CmdArgs.push_back("-");
2662    else if (II.isFilename())
2663      CmdArgs.push_back(II.getFilename());
2664    else
2665      II.getInputArg().renderAsInput(Args, CmdArgs);
2666  }
2667
2668  if (!Args.hasArg(options::OPT_nostdlib) &&
2669      !Args.hasArg(options::OPT_nodefaultlibs)) {
2670    // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
2671    //         rpaths
2672    CmdArgs.push_back("-L/usr/lib/gcc41");
2673
2674    if (!Args.hasArg(options::OPT_static)) {
2675      CmdArgs.push_back("-rpath");
2676      CmdArgs.push_back("/usr/lib/gcc41");
2677
2678      CmdArgs.push_back("-rpath-link");
2679      CmdArgs.push_back("/usr/lib/gcc41");
2680
2681      CmdArgs.push_back("-rpath");
2682      CmdArgs.push_back("/usr/lib");
2683
2684      CmdArgs.push_back("-rpath-link");
2685      CmdArgs.push_back("/usr/lib");
2686    }
2687
2688    if (Args.hasArg(options::OPT_shared)) {
2689      CmdArgs.push_back("-lgcc_pic");
2690    } else {
2691      CmdArgs.push_back("-lgcc");
2692    }
2693
2694
2695    if (Args.hasArg(options::OPT_pthread))
2696      CmdArgs.push_back("-lthread_xu");
2697
2698    if (!Args.hasArg(options::OPT_nolibc)) {
2699      CmdArgs.push_back("-lc");
2700    }
2701
2702    if (Args.hasArg(options::OPT_shared)) {
2703      CmdArgs.push_back("-lgcc_pic");
2704    } else {
2705      CmdArgs.push_back("-lgcc");
2706    }
2707  }
2708
2709  if (!Args.hasArg(options::OPT_nostdlib) &&
2710      !Args.hasArg(options::OPT_nostartfiles)) {
2711    if (!Args.hasArg(options::OPT_shared))
2712      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
2713    else
2714      CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
2715    CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
2716  }
2717
2718  const char *Exec =
2719    Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2720  Dest.addCommand(new Command(JA, Exec, CmdArgs));
2721}
2722