1 //===--- Tools.cpp - Tools Implementations ------------------------------*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "clang/Driver/Action.h"
13 #include "clang/Driver/Arg.h"
14 #include "clang/Driver/ArgList.h"
15 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/DriverDiagnostic.h"
17 #include "clang/Driver/Compilation.h"
18 #include "clang/Driver/Job.h"
19 #include "clang/Driver/HostInfo.h"
20 #include "clang/Driver/Option.h"
21 #include "clang/Driver/Options.h"
22 #include "clang/Driver/ToolChain.h"
23 #include "clang/Driver/Util.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/ADT/Twine.h"
28 #include "llvm/Support/Format.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/System/Host.h"
31 #include "llvm/System/Process.h"
33 #include "InputInfo.h"
34 #include "ToolChains.h"
36 using namespace clang::driver;
37 using namespace clang::driver::tools;
39 /// CheckPreprocessingOptions - Perform some validation of preprocessing
40 /// arguments that is shared with gcc.
41 static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
42 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
43 if (!Args.hasArg(options::OPT_E))
44 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
45 << A->getAsString(Args) << "-E";
48 /// CheckCodeGenerationOptions - Perform some validation of code generation
49 /// arguments that is shared with gcc.
50 static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
51 // In gcc, only ARM checks this, but it seems reasonable to check universally.
52 if (Args.hasArg(options::OPT_static))
53 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
54 options::OPT_mdynamic_no_pic))
55 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
56 << A->getAsString(Args) << "-static";
59 // Quote target names for inclusion in GNU Make dependency files.
60 // Only the characters '$', '#', ' ', '\t' are quoted.
61 static void QuoteTarget(llvm::StringRef Target,
62 llvm::SmallVectorImpl<char> &Res) {
63 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
67 // Escape the preceding backslashes
68 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
71 // Escape the space/tab
84 Res.push_back(Target[i]);
88 void Clang::AddPreprocessingOptions(const Driver &D,
90 ArgStringList &CmdArgs,
91 const InputInfo &Output,
92 const InputInfoList &Inputs) const {
95 CheckPreprocessingOptions(D, Args);
97 Args.AddLastArg(CmdArgs, options::OPT_C);
98 Args.AddLastArg(CmdArgs, options::OPT_CC);
100 // Handle dependency file generation.
101 if ((A = Args.getLastArg(options::OPT_M)) ||
102 (A = Args.getLastArg(options::OPT_MM)) ||
103 (A = Args.getLastArg(options::OPT_MD)) ||
104 (A = Args.getLastArg(options::OPT_MMD))) {
105 // Determine the output location.
107 if (Output.getType() == types::TY_Dependencies) {
111 DepFile = Output.getFilename();
112 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
113 DepFile = MF->getValue(Args);
114 } else if (A->getOption().matches(options::OPT_M) ||
115 A->getOption().matches(options::OPT_MM)) {
118 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
120 CmdArgs.push_back("-dependency-file");
121 CmdArgs.push_back(DepFile);
123 // Add a default target if one wasn't specified.
124 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
125 const char *DepTarget;
127 // If user provided -o, that is the dependency target, except
128 // when we are only generating a dependency file.
129 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
130 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
131 DepTarget = OutputOpt->getValue(Args);
133 // Otherwise derive from the base input.
135 // FIXME: This should use the computed output file location.
136 llvm::sys::Path P(Inputs[0].getBaseInput());
140 DepTarget = Args.MakeArgString(P.getLast());
143 CmdArgs.push_back("-MT");
144 llvm::SmallString<128> Quoted;
145 QuoteTarget(DepTarget, Quoted);
146 CmdArgs.push_back(Args.MakeArgString(Quoted));
149 if (A->getOption().matches(options::OPT_M) ||
150 A->getOption().matches(options::OPT_MD))
151 CmdArgs.push_back("-sys-header-deps");
154 Args.AddLastArg(CmdArgs, options::OPT_MP);
156 // Convert all -MQ <target> args to -MT <quoted target>
157 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
159 ie = Args.filtered_end(); it != ie; ++it) {
163 if (it->getOption().matches(options::OPT_MQ)) {
164 CmdArgs.push_back("-MT");
165 llvm::SmallString<128> Quoted;
166 QuoteTarget(it->getValue(Args), Quoted);
167 CmdArgs.push_back(Args.MakeArgString(Quoted));
169 // -MT flag - no change
171 it->render(Args, CmdArgs);
175 // Add -i* options, and automatically translate to
176 // -include-pch/-include-pth for transparent PCH support. It's
177 // wonky, but we include looking for .gch so we can support seamless
178 // replacement into a build system already set up to be generating
180 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
181 ie = Args.filtered_end(); it != ie; ++it) {
184 if (A->getOption().matches(options::OPT_include)) {
185 // Use PCH if the user requested it, except for C++ (for now).
186 bool UsePCH = D.CCCUsePCH;
187 if (types::isCXX(Inputs[0].getType()))
190 bool FoundPTH = false;
191 bool FoundPCH = false;
192 llvm::sys::Path P(A->getValue(Args));
194 P.appendSuffix("pch");
202 P.appendSuffix("pth");
209 if (!FoundPCH && !FoundPTH) {
210 P.appendSuffix("gch");
219 if (FoundPCH || FoundPTH) {
222 CmdArgs.push_back("-include-pch");
224 CmdArgs.push_back("-include-pth");
225 CmdArgs.push_back(Args.MakeArgString(P.str()));
230 // Not translated, render as usual.
232 A->render(Args, CmdArgs);
235 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
236 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
238 // Add -Wp, and -Xassembler if using the preprocessor.
240 // FIXME: There is a very unfortunate problem here, some troubled
241 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
242 // really support that we would have to parse and then translate
244 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
245 options::OPT_Xpreprocessor);
247 // -I- is a deprecated GCC feature, reject it.
248 if (Arg *A = Args.getLastArg(options::OPT_I_))
249 D.Diag(clang::diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
252 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targetting.
254 // FIXME: tblgen this.
255 static const char *getARMTargetCPU(const ArgList &Args) {
256 // FIXME: Warn on inconsistent use of -mcpu and -march.
258 // If we have -mcpu=, use that.
259 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
260 return A->getValue(Args);
262 // Otherwise, if we have -march= choose the base CPU for that arch.
263 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
264 llvm::StringRef MArch = A->getValue(Args);
266 if (MArch == "armv2" || MArch == "armv2a")
268 if (MArch == "armv3")
270 if (MArch == "armv3m")
272 if (MArch == "armv4" || MArch == "armv4t")
274 if (MArch == "armv5" || MArch == "armv5t")
276 if (MArch == "armv5e" || MArch == "armv5te")
278 if (MArch == "armv5tej")
280 if (MArch == "armv6" || MArch == "armv6k")
281 return "arm1136jf-s";
282 if (MArch == "armv6j")
284 if (MArch == "armv6z" || MArch == "armv6zk")
285 return "arm1176jzf-s";
286 if (MArch == "armv6t2")
287 return "arm1156t2-s";
288 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
290 if (MArch == "armv7r" || MArch == "armv7-r")
292 if (MArch == "armv7m" || MArch == "armv7-m")
294 if (MArch == "ep9312")
296 if (MArch == "iwmmxt")
298 if (MArch == "xscale")
302 // Otherwise return the most base CPU LLVM supports.
306 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
309 // FIXME: This is redundant with -mcpu, why does LLVM use this.
310 // FIXME: tblgen this, or kill it!
311 static const char *getLLVMArchSuffixForARM(llvm::StringRef CPU) {
312 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
313 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
314 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
315 CPU == "arm940t" || CPU == "ep9312")
318 if (CPU == "arm10tdmi" || CPU == "arm1020t")
321 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
322 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
323 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
327 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
328 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
331 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
334 if (CPU == "cortex-a8" || CPU == "cortex-a9")
340 /// getLLVMTriple - Get the LLVM triple to use for a particular toolchain, which
341 /// may depend on command line arguments.
342 static std::string getLLVMTriple(const ToolChain &TC, const ArgList &Args) {
343 switch (TC.getTriple().getArch()) {
345 return TC.getTripleString();
347 case llvm::Triple::arm:
348 case llvm::Triple::thumb: {
349 // FIXME: Factor into subclasses.
350 llvm::Triple Triple = TC.getTriple();
352 // Thumb2 is the default for V7 on Darwin.
354 // FIXME: Thumb should just be another -target-feaure, not in the triple.
355 llvm::StringRef Suffix = getLLVMArchSuffixForARM(getARMTargetCPU(Args));
357 (Suffix == "v7" && TC.getTriple().getOS() == llvm::Triple::Darwin);
358 std::string ArchName = "arm";
359 if (Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
361 Triple.setArchName(ArchName + Suffix.str());
363 return Triple.getTriple();
368 // FIXME: Move to target hook.
369 static bool isSignedCharDefault(const llvm::Triple &Triple) {
370 switch (Triple.getArch()) {
374 case llvm::Triple::ppc:
375 case llvm::Triple::ppc64:
376 if (Triple.getOS() == llvm::Triple::Darwin)
380 case llvm::Triple::systemz:
385 void Clang::AddARMTargetArgs(const ArgList &Args,
386 ArgStringList &CmdArgs) const {
387 const Driver &D = getToolChain().getDriver();
389 // Select the ABI to use.
391 // FIXME: Support -meabi.
392 const char *ABIName = 0;
393 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
394 ABIName = A->getValue(Args);
396 // Select the default based on the platform.
397 switch (getToolChain().getTriple().getOS()) {
398 // FIXME: Is this right for non-Darwin and non-Linux?
403 case llvm::Triple::Darwin:
404 ABIName = "apcs-gnu";
407 case llvm::Triple::Linux:
408 ABIName = "aapcs-linux";
412 CmdArgs.push_back("-target-abi");
413 CmdArgs.push_back(ABIName);
415 // Set the CPU based on -march= and -mcpu=.
416 CmdArgs.push_back("-target-cpu");
417 CmdArgs.push_back(getARMTargetCPU(Args));
419 // Select the float ABI as determined by -msoft-float, -mhard-float, and
421 llvm::StringRef FloatABI;
422 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
423 options::OPT_mhard_float,
424 options::OPT_mfloat_abi_EQ)) {
425 if (A->getOption().matches(options::OPT_msoft_float))
427 else if (A->getOption().matches(options::OPT_mhard_float))
430 FloatABI = A->getValue(Args);
431 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
432 D.Diag(clang::diag::err_drv_invalid_mfloat_abi)
433 << A->getAsString(Args);
439 // If unspecified, choose the default based on the platform.
440 if (FloatABI.empty()) {
441 // FIXME: This is wrong for non-Darwin, we don't have a mechanism yet for
442 // distinguishing things like linux-eabi vs linux-elf.
443 switch (getToolChain().getTriple().getOS()) {
444 case llvm::Triple::Darwin: {
445 // Darwin defaults to "softfp" for v6 and v7.
447 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
448 llvm::StringRef ArchName = getLLVMArchSuffixForARM(getARMTargetCPU(Args));
449 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
457 // Assume "soft", but warn the user we are guessing.
459 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
464 if (FloatABI == "soft") {
465 // Floating point operations and argument passing are soft.
467 // FIXME: This changes CPP defines, we need -target-soft-float.
468 CmdArgs.push_back("-msoft-float");
469 CmdArgs.push_back("-mfloat-abi");
470 CmdArgs.push_back("soft");
471 } else if (FloatABI == "softfp") {
472 // Floating point operations are hard, but argument passing is soft.
473 CmdArgs.push_back("-mfloat-abi");
474 CmdArgs.push_back("soft");
476 // Floating point operations and argument passing are hard.
477 assert(FloatABI == "hard" && "Invalid float abi!");
478 CmdArgs.push_back("-mfloat-abi");
479 CmdArgs.push_back("hard");
482 // Set appropriate target features for floating point mode.
484 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
485 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
486 // stripped out by the ARM target.
488 // Use software floating point operations?
489 if (FloatABI == "soft") {
490 CmdArgs.push_back("-target-feature");
491 CmdArgs.push_back("+soft-float");
494 // Use software floating point argument passing?
495 if (FloatABI != "hard") {
496 CmdArgs.push_back("-target-feature");
497 CmdArgs.push_back("+soft-float-abi");
502 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
504 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
505 llvm::StringRef FPU = A->getValue(Args);
507 // Set the target features based on the FPU.
508 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
509 // Disable any default FPU support.
510 CmdArgs.push_back("-target-feature");
511 CmdArgs.push_back("-vfp2");
512 CmdArgs.push_back("-target-feature");
513 CmdArgs.push_back("-vfp3");
514 CmdArgs.push_back("-target-feature");
515 CmdArgs.push_back("-neon");
516 } else if (FPU == "vfp") {
517 CmdArgs.push_back("-target-feature");
518 CmdArgs.push_back("+vfp2");
519 } else if (FPU == "vfp3") {
520 CmdArgs.push_back("-target-feature");
521 CmdArgs.push_back("+vfp3");
522 } else if (FPU == "neon") {
523 CmdArgs.push_back("-target-feature");
524 CmdArgs.push_back("+neon");
526 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
530 void Clang::AddMIPSTargetArgs(const ArgList &Args,
531 ArgStringList &CmdArgs) const {
532 const Driver &D = getToolChain().getDriver();
534 // Select the ABI to use.
535 const char *ABIName = 0;
536 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
537 ABIName = A->getValue(Args);
542 CmdArgs.push_back("-target-abi");
543 CmdArgs.push_back(ABIName);
545 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
546 llvm::StringRef MArch = A->getValue(Args);
547 CmdArgs.push_back("-target-cpu");
549 if ((MArch == "r2000") || (MArch == "r3000"))
550 CmdArgs.push_back("mips1");
551 else if (MArch == "r6000")
552 CmdArgs.push_back("mips2");
554 CmdArgs.push_back(MArch.str().c_str());
557 // Select the float ABI as determined by -msoft-float, -mhard-float, and
558 llvm::StringRef FloatABI;
559 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
560 options::OPT_mhard_float)) {
561 if (A->getOption().matches(options::OPT_msoft_float))
563 else if (A->getOption().matches(options::OPT_mhard_float))
567 // If unspecified, choose the default based on the platform.
568 if (FloatABI.empty()) {
569 // Assume "soft", but warn the user we are guessing.
571 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
574 if (FloatABI == "soft") {
575 // Floating point operations and argument passing are soft.
577 // FIXME: This changes CPP defines, we need -target-soft-float.
578 CmdArgs.push_back("-msoft-float");
580 assert(FloatABI == "hard" && "Invalid float abi!");
581 CmdArgs.push_back("-mhard-float");
585 void Clang::AddX86TargetArgs(const ArgList &Args,
586 ArgStringList &CmdArgs) const {
587 if (!Args.hasFlag(options::OPT_mred_zone,
588 options::OPT_mno_red_zone,
590 Args.hasArg(options::OPT_mkernel) ||
591 Args.hasArg(options::OPT_fapple_kext))
592 CmdArgs.push_back("-disable-red-zone");
594 if (Args.hasFlag(options::OPT_msoft_float,
595 options::OPT_mno_soft_float,
597 CmdArgs.push_back("-no-implicit-float");
599 const char *CPUName = 0;
600 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
601 if (llvm::StringRef(A->getValue(Args)) == "native") {
602 // FIXME: Reject attempts to use -march=native unless the target matches
605 // FIXME: We should also incorporate the detected target features for use
607 std::string CPU = llvm::sys::getHostCPUName();
609 CPUName = Args.MakeArgString(CPU);
611 CPUName = A->getValue(Args);
614 // Select the default CPU if none was given (or detection failed).
616 // FIXME: Need target hooks.
617 if (getToolChain().getOS().startswith("darwin")) {
618 if (getToolChain().getArchName() == "x86_64")
620 else if (getToolChain().getArchName() == "i386")
622 } else if (getToolChain().getOS().startswith("haiku")) {
623 if (getToolChain().getArchName() == "x86_64")
625 else if (getToolChain().getArchName() == "i386")
628 if (getToolChain().getArchName() == "x86_64")
630 else if (getToolChain().getArchName() == "i386")
631 CPUName = "pentium4";
636 CmdArgs.push_back("-target-cpu");
637 CmdArgs.push_back(CPUName);
640 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
641 ie = Args.filtered_end(); it != ie; ++it) {
642 llvm::StringRef Name = it->getOption().getName();
646 assert(Name.startswith("-m") && "Invalid feature name.");
647 Name = Name.substr(2);
649 bool IsNegative = Name.startswith("no-");
651 Name = Name.substr(3);
653 CmdArgs.push_back("-target-feature");
654 CmdArgs.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
658 static bool needsExceptions(const ArgList &Args, types::ID InputType,
659 const llvm::Triple &Triple) {
660 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
661 options::OPT_fno_exceptions)) {
662 if (A->getOption().matches(options::OPT_fexceptions))
668 case types::TY_CXX: case types::TY_CXXHeader:
669 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
670 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
671 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
674 case types::TY_ObjC: case types::TY_ObjCHeader:
675 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
676 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi))
678 if (Triple.getOS() != llvm::Triple::Darwin)
680 return (Triple.getDarwinMajorNumber() >= 9 &&
681 Triple.getArch() == llvm::Triple::x86_64);
688 /// getEffectiveClangTriple - Get the "effective" target triple, which is the
689 /// triple for the target but with the OS version potentially modified for
690 /// Darwin's -mmacosx-version-min.
691 static std::string getEffectiveClangTriple(const Driver &D,
693 const ArgList &Args) {
694 llvm::Triple Triple(getLLVMTriple(TC, Args));
696 // Handle -mmacosx-version-min and -miphoneos-version-min.
697 if (Triple.getOS() != llvm::Triple::Darwin) {
698 // Diagnose use of -mmacosx-version-min and -miphoneos-version-min on
700 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
701 options::OPT_miphoneos_version_min_EQ))
702 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
704 const toolchains::Darwin &DarwinTC(
705 reinterpret_cast<const toolchains::Darwin&>(TC));
707 // If the target isn't initialized (e.g., an unknown Darwin platform, return
708 // the default triple).
709 if (!DarwinTC.isTargetInitialized())
710 return Triple.getTriple();
713 DarwinTC.getTargetVersion(Version);
715 // Mangle the target version into the OS triple component. For historical
716 // reasons that make little sense, the version passed here is the "darwin"
717 // version, which drops the 10 and offsets by 4. See inverse code when
718 // setting the OS version preprocessor define.
719 if (!DarwinTC.isTargetIPhoneOS()) {
720 Version[0] = Version[1] + 4;
721 Version[1] = Version[2];
724 // Use the environment to communicate that we are targetting iPhoneOS.
725 Triple.setEnvironmentName("iphoneos");
728 llvm::SmallString<16> Str;
729 llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
730 << "." << Version[1] << "." << Version[2];
731 Triple.setOSName(Str.str());
734 return Triple.getTriple();
737 void Clang::ConstructJob(Compilation &C, const JobAction &JA,
739 const InputInfo &Output,
740 const InputInfoList &Inputs,
742 const char *LinkingOutput) const {
743 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
744 options::OPT_fapple_kext);
745 const Driver &D = getToolChain().getDriver();
746 ArgStringList CmdArgs;
748 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
750 // Invoke ourselves in -cc1 mode.
752 // FIXME: Implement custom jobs for internal actions.
753 CmdArgs.push_back("-cc1");
755 // Add the "effective" target triple.
756 CmdArgs.push_back("-triple");
757 std::string TripleStr = getEffectiveClangTriple(D, getToolChain(), Args);
758 CmdArgs.push_back(Args.MakeArgString(TripleStr));
760 // Select the appropriate action.
761 if (isa<AnalyzeJobAction>(JA)) {
762 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
763 CmdArgs.push_back("-analyze");
764 } else if (isa<PreprocessJobAction>(JA)) {
765 if (Output.getType() == types::TY_Dependencies)
766 CmdArgs.push_back("-Eonly");
768 CmdArgs.push_back("-E");
769 } else if (isa<AssembleJobAction>(JA)) {
770 CmdArgs.push_back("-emit-obj");
772 // At -O0, we use -mrelax-all by default.
774 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
775 IsOpt = !A->getOption().matches(options::OPT_O0);
776 if (Args.hasFlag(options::OPT_mrelax_all,
777 options::OPT_mno_relax_all,
779 CmdArgs.push_back("-mrelax-all");
780 } else if (isa<PrecompileJobAction>(JA)) {
781 // Use PCH if the user requested it, except for C++ (for now).
782 bool UsePCH = D.CCCUsePCH;
783 if (types::isCXX(Inputs[0].getType()))
787 CmdArgs.push_back("-emit-pch");
789 CmdArgs.push_back("-emit-pth");
791 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
793 if (JA.getType() == types::TY_Nothing) {
794 CmdArgs.push_back("-fsyntax-only");
795 } else if (JA.getType() == types::TY_LLVMAsm) {
796 CmdArgs.push_back("-emit-llvm");
797 } else if (JA.getType() == types::TY_LLVMBC) {
798 CmdArgs.push_back("-emit-llvm-bc");
799 } else if (JA.getType() == types::TY_PP_Asm) {
800 CmdArgs.push_back("-S");
801 } else if (JA.getType() == types::TY_AST) {
802 CmdArgs.push_back("-emit-pch");
803 } else if (JA.getType() == types::TY_RewrittenObjC) {
804 CmdArgs.push_back("-rewrite-objc");
806 assert(JA.getType() == types::TY_PP_Asm &&
807 "Unexpected output type!");
811 // The make clang go fast button.
812 CmdArgs.push_back("-disable-free");
814 // Disable the verification pass in -asserts builds.
816 CmdArgs.push_back("-disable-llvm-verifier");
819 // Set the main file name, so that debug info works even with
821 CmdArgs.push_back("-main-file-name");
822 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
824 // Some flags which affect the language (via preprocessor
825 // defines). See darwin::CC1::AddCPPArgs.
826 if (Args.hasArg(options::OPT_static))
827 CmdArgs.push_back("-static-define");
829 if (isa<AnalyzeJobAction>(JA)) {
830 // Enable region store model by default.
831 CmdArgs.push_back("-analyzer-store=region");
833 // Treat blocks as analysis entry points.
834 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
836 // Add default argument set.
837 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
838 CmdArgs.push_back("-analyzer-check-dead-stores");
839 // Do not enable the security-syntatic check since it
840 // it needs to be refined (known issues).
841 // CmdArgs.push_back("-analyzer-check-security-syntactic");
842 CmdArgs.push_back("-analyzer-check-objc-mem");
843 CmdArgs.push_back("-analyzer-eagerly-assume");
844 CmdArgs.push_back("-analyzer-check-objc-methodsigs");
845 // Do not enable the missing -dealloc check.
846 // '-analyzer-check-objc-missing-dealloc',
847 CmdArgs.push_back("-analyzer-check-objc-unused-ivars");
850 // Set the output format. The default is plist, for (lame) historical
852 CmdArgs.push_back("-analyzer-output");
853 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
854 CmdArgs.push_back(A->getValue(Args));
856 CmdArgs.push_back("plist");
858 // Disable the presentation of standard compiler warnings when
859 // using --analyze. We only want to show static analyzer diagnostics
860 // or frontend errors.
861 CmdArgs.push_back("-w");
863 // Add -Xanalyzer arguments when running as analyzer.
864 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
867 CheckCodeGenerationOptions(D, Args);
869 // Perform argument translation for LLVM backend. This
870 // takes some care in reconciling with llvm-gcc. The
871 // issue is that llvm-gcc translates these options based on
872 // the values in cc1, whereas we are processing based on
873 // the driver arguments.
875 // This comes from the default translation the driver + cc1
876 // would do to enable flag_pic.
878 // FIXME: Centralize this code.
879 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
880 Args.hasArg(options::OPT_fpic) ||
881 Args.hasArg(options::OPT_fPIE) ||
882 Args.hasArg(options::OPT_fpie));
883 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
884 Args.hasArg(options::OPT_static));
885 const char *Model = getToolChain().GetForcedPicModel();
887 if (Args.hasArg(options::OPT_mdynamic_no_pic))
888 Model = "dynamic-no-pic";
889 else if (PICDisabled)
894 Model = getToolChain().GetDefaultRelocationModel();
896 if (llvm::StringRef(Model) != "pic") {
897 CmdArgs.push_back("-mrelocation-model");
898 CmdArgs.push_back(Model);
901 // Infer the __PIC__ value.
903 // FIXME: This isn't quite right on Darwin, which always sets
905 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
906 CmdArgs.push_back("-pic-level");
907 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
909 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
910 options::OPT_fno_merge_all_constants))
911 CmdArgs.push_back("-no-merge-all-constants");
913 // LLVM Code Generator Options.
915 // FIXME: Set --enable-unsafe-fp-math.
916 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
917 options::OPT_fomit_frame_pointer))
918 CmdArgs.push_back("-mdisable-fp-elim");
919 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
920 options::OPT_fno_zero_initialized_in_bss))
921 CmdArgs.push_back("-mno-zero-initialized-in-bss");
923 // Decide whether to use verbose asm. Verbose assembly is the default on
924 // toolchains which have the integrated assembler on by default.
925 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
926 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
927 IsVerboseAsmDefault) ||
928 Args.hasArg(options::OPT_dA))
929 CmdArgs.push_back("-masm-verbose");
931 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
932 CmdArgs.push_back("-mdebug-pass");
933 CmdArgs.push_back("Structure");
935 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
936 CmdArgs.push_back("-mdebug-pass");
937 CmdArgs.push_back("Arguments");
940 // Enable -mconstructor-aliases except on darwin, where we have to
941 // work around a linker bug; see <rdar://problem/7651567>.
942 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
943 CmdArgs.push_back("-mconstructor-aliases");
945 // This is a coarse approximation of what llvm-gcc actually does, both
946 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
948 bool AsynchronousUnwindTables =
949 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
950 options::OPT_fno_asynchronous_unwind_tables,
951 getToolChain().IsUnwindTablesDefault() &&
953 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
954 AsynchronousUnwindTables))
955 CmdArgs.push_back("-munwind-tables");
957 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
958 CmdArgs.push_back("-mlimit-float-precision");
959 CmdArgs.push_back(A->getValue(Args));
962 // FIXME: Handle -mtune=.
963 (void) Args.hasArg(options::OPT_mtune_EQ);
965 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
966 CmdArgs.push_back("-mcode-model");
967 CmdArgs.push_back(A->getValue(Args));
970 // Add target specific cpu and features flags.
971 switch(getToolChain().getTriple().getArch()) {
975 case llvm::Triple::arm:
976 case llvm::Triple::thumb:
977 AddARMTargetArgs(Args, CmdArgs);
980 case llvm::Triple::mips:
981 case llvm::Triple::mipsel:
982 AddMIPSTargetArgs(Args, CmdArgs);
985 case llvm::Triple::x86:
986 case llvm::Triple::x86_64:
987 AddX86TargetArgs(Args, CmdArgs);
991 // -fno-math-errno is default.
992 if (Args.hasFlag(options::OPT_fmath_errno,
993 options::OPT_fno_math_errno,
995 CmdArgs.push_back("-fmath-errno");
997 // Explicitly error on some things we know we don't support and can't just
999 types::ID InputType = Inputs[0].getType();
1001 if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
1002 (Unsupported = Args.getLastArg(options::OPT_iframework)) ||
1003 (Unsupported = Args.getLastArg(options::OPT_fshort_enums)))
1004 D.Diag(clang::diag::err_drv_clang_unsupported)
1005 << Unsupported->getOption().getName();
1007 if (types::isCXX(InputType) &&
1008 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1009 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
1010 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)))
1011 D.Diag(clang::diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
1012 << Unsupported->getOption().getName();
1015 Args.AddAllArgs(CmdArgs, options::OPT_v);
1016 Args.AddLastArg(CmdArgs, options::OPT_P);
1017 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
1019 // Special case debug options to only pass -g to clang. This is
1021 Args.ClaimAllArgs(options::OPT_g_Group);
1022 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1023 if (!A->getOption().matches(options::OPT_g0))
1024 CmdArgs.push_back("-g");
1026 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1027 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1029 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
1030 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
1031 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
1033 // Pass the path to compiler resource files.
1034 CmdArgs.push_back("-resource-dir");
1035 CmdArgs.push_back(D.ResourceDir.c_str());
1037 // Add preprocessing options like -I, -D, etc. if we are using the
1040 // FIXME: Support -fpreprocessed
1041 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
1042 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
1044 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
1046 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
1047 if (A->getOption().matches(options::OPT_O4))
1048 CmdArgs.push_back("-O3");
1049 else if (A->getOption().matches(options::OPT_O) &&
1050 A->getValue(Args)[0] == '\0')
1051 CmdArgs.push_back("-O2");
1053 A->render(Args, CmdArgs);
1056 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1057 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1058 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
1059 Args.AddLastArg(CmdArgs, options::OPT_w);
1061 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1062 // (-ansi is equivalent to -std=c89).
1064 // If a std is supplied, only add -trigraphs if it follows the
1066 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1067 if (Std->getOption().matches(options::OPT_ansi))
1068 if (types::isCXX(InputType))
1069 CmdArgs.push_back("-std=c++98");
1071 CmdArgs.push_back("-std=c89");
1073 Std->render(Args, CmdArgs);
1075 if (Arg *A = Args.getLastArg(options::OPT_trigraphs))
1076 if (A->getIndex() > Std->getIndex())
1077 A->render(Args, CmdArgs);
1079 // Honor -std-default.
1081 // FIXME: Clang doesn't correctly handle -std= when the input language
1082 // doesn't match. For the time being just ignore this for C++ inputs;
1083 // eventually we want to do all the standard defaulting here instead of
1084 // splitting it between the driver and clang -cc1.
1085 if (!types::isCXX(InputType))
1086 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1087 "-std=", /*Joined=*/true);
1088 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
1091 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1092 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1093 if (Asm->getOption().matches(options::OPT_fasm))
1094 CmdArgs.push_back("-fgnu-keywords");
1096 CmdArgs.push_back("-fno-gnu-keywords");
1099 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1100 CmdArgs.push_back("-ftemplate-depth");
1101 CmdArgs.push_back(A->getValue(Args));
1104 if (Args.hasArg(options::OPT__relocatable_pch))
1105 CmdArgs.push_back("-relocatable-pch");
1107 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1108 CmdArgs.push_back("-fconstant-string-class");
1109 CmdArgs.push_back(A->getValue(Args));
1112 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1113 CmdArgs.push_back("-ftabstop");
1114 CmdArgs.push_back(A->getValue(Args));
1117 CmdArgs.push_back("-ferror-limit");
1118 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1119 CmdArgs.push_back(A->getValue(Args));
1121 CmdArgs.push_back("19");
1123 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1124 CmdArgs.push_back("-fmacro-backtrace-limit");
1125 CmdArgs.push_back(A->getValue(Args));
1128 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1129 CmdArgs.push_back("-ftemplate-backtrace-limit");
1130 CmdArgs.push_back(A->getValue(Args));
1133 // Pass -fmessage-length=.
1134 CmdArgs.push_back("-fmessage-length");
1135 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
1136 CmdArgs.push_back(A->getValue(Args));
1138 // If -fmessage-length=N was not specified, determine whether this is a
1139 // terminal and, if so, implicitly define -fmessage-length appropriately.
1140 unsigned N = llvm::sys::Process::StandardErrColumns();
1141 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(N)));
1144 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1145 CmdArgs.push_back("-fvisibility");
1146 CmdArgs.push_back(A->getValue(Args));
1149 // -fhosted is default.
1150 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1151 options::OPT_fhosted,
1153 CmdArgs.push_back("-ffreestanding");
1155 // Forward -f (flag) options which we can pass directly.
1156 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
1157 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
1158 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
1160 // -flax-vector-conversions is default.
1161 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1162 options::OPT_fno_lax_vector_conversions))
1163 CmdArgs.push_back("-fno-lax-vector-conversions");
1165 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1166 // takes precedence.
1167 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1169 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1171 if (getToolChain().SupportsObjCGC()) {
1172 GCArg->render(Args, CmdArgs);
1174 // FIXME: We should move this to a hard error.
1175 D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1176 << GCArg->getAsString(Args);
1180 Args.AddLastArg(CmdArgs, options::OPT_fno_caret_diagnostics);
1181 Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
1182 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
1183 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
1184 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1185 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
1186 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
1188 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1190 // -stack-protector=0 is default.
1191 unsigned StackProtectorLevel = 0;
1192 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1193 options::OPT_fstack_protector_all,
1194 options::OPT_fstack_protector)) {
1195 if (A->getOption().matches(options::OPT_fstack_protector))
1196 StackProtectorLevel = 1;
1197 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1198 StackProtectorLevel = 2;
1200 StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel();
1201 if (StackProtectorLevel) {
1202 CmdArgs.push_back("-stack-protector");
1203 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(StackProtectorLevel)));
1206 // Forward -f options with positive and negative forms; we translate
1209 // -fbuiltin is default.
1210 if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
1211 CmdArgs.push_back("-fno-builtin");
1213 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1214 options::OPT_fno_assume_sane_operator_new))
1215 CmdArgs.push_back("-fno-assume-sane-operator-new");
1217 // -fblocks=0 is default.
1218 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
1219 getToolChain().IsBlocksDefault())) {
1220 CmdArgs.push_back("-fblocks");
1223 // -faccess-control is default.
1224 if (Args.hasFlag(options::OPT_fno_access_control,
1225 options::OPT_faccess_control,
1227 CmdArgs.push_back("-fno-access-control");
1229 // -fexceptions=0 is default.
1230 if (needsExceptions(Args, InputType, getToolChain().getTriple()))
1231 CmdArgs.push_back("-fexceptions");
1233 if (getToolChain().UseSjLjExceptions())
1234 CmdArgs.push_back("-fsjlj-exceptions");
1236 // -frtti is default.
1238 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
1239 CmdArgs.push_back("-fno-rtti");
1241 // -fsigned-char is default.
1242 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
1243 isSignedCharDefault(getToolChain().getTriple())))
1244 CmdArgs.push_back("-fno-signed-char");
1246 // -fthreadsafe-static is default.
1247 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
1248 options::OPT_fno_threadsafe_statics))
1249 CmdArgs.push_back("-fno-threadsafe-statics");
1251 // -fuse-cxa-atexit is default.
1252 if (KernelOrKext || !Args.hasFlag(options::OPT_fuse_cxa_atexit,
1253 options::OPT_fno_use_cxa_atexit))
1254 CmdArgs.push_back("-fno-use-cxa-atexit");
1256 // -fms-extensions=0 is default.
1257 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
1258 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1259 CmdArgs.push_back("-fms-extensions");
1261 // -fgnu-keywords default varies depending on language; only pass if
1263 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
1264 options::OPT_fno_gnu_keywords))
1265 A->render(Args, CmdArgs);
1267 // -fnext-runtime is default.
1268 if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
1269 getToolChain().getTriple().getOS() == llvm::Triple::Darwin))
1270 CmdArgs.push_back("-fgnu-runtime");
1272 // -fobjc-nonfragile-abi=0 is default.
1273 if (types::isObjC(InputType)) {
1274 unsigned Version = 1;
1275 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi) ||
1276 getToolChain().IsObjCNonFragileABIDefault())
1278 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1279 if (llvm::StringRef(A->getValue(Args)) == "1")
1281 else if (llvm::StringRef(A->getValue(Args)) == "2")
1284 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1288 CmdArgs.push_back("-fobjc-nonfragile-abi");
1290 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1291 // legacy is the default.
1292 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1293 options::OPT_fno_objc_legacy_dispatch,
1294 getToolChain().IsObjCLegacyDispatchDefault())) {
1295 if (getToolChain().UseObjCMixedDispatch())
1296 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1298 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1302 // FIXME: -fobjc-nonfragile-abi2 is a transient option meant to expose
1303 // features in testing. It will eventually be removed.
1304 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi2))
1305 CmdArgs.push_back("-fobjc-nonfragile-abi2");
1308 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1309 options::OPT_fno_assume_sane_operator_new))
1310 CmdArgs.push_back("-fno-assume-sane-operator-new");
1312 // -fconstant-cfstrings is default, and may be subject to argument translation
1314 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1315 options::OPT_fno_constant_cfstrings) ||
1316 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1317 options::OPT_mno_constant_cfstrings))
1318 CmdArgs.push_back("-fno-constant-cfstrings");
1320 // -fshort-wchar default varies depending on platform; only
1321 // pass if specified.
1322 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1323 A->render(Args, CmdArgs);
1325 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1326 // happened to translate to -mpascal-strings, we want to back translate here.
1328 // FIXME: This is gross; that translation should be pulled from the
1330 if (Args.hasFlag(options::OPT_fpascal_strings,
1331 options::OPT_fno_pascal_strings,
1333 Args.hasFlag(options::OPT_mpascal_strings,
1334 options::OPT_mno_pascal_strings,
1336 CmdArgs.push_back("-fpascal-strings");
1338 // -fcommon is default, only pass non-default.
1339 if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
1340 CmdArgs.push_back("-fno-common");
1342 // -fsigned-bitfields is default, and clang doesn't yet support
1343 // --funsigned-bitfields.
1344 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
1345 options::OPT_funsigned_bitfields))
1346 D.Diag(clang::diag::warn_drv_clang_unsupported)
1347 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1349 // -fdiagnostics-fixit-info is default, only pass non-default.
1350 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
1351 options::OPT_fno_diagnostics_fixit_info))
1352 CmdArgs.push_back("-fno-diagnostics-fixit-info");
1354 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_binary);
1356 // Enable -fdiagnostics-show-option by default.
1357 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
1358 options::OPT_fno_diagnostics_show_option))
1359 CmdArgs.push_back("-fdiagnostics-show-option");
1362 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
1363 CmdArgs.push_back("-fdiagnostics-show-category");
1364 CmdArgs.push_back(A->getValue(Args));
1367 // Color diagnostics are the default, unless the terminal doesn't support
1369 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
1370 options::OPT_fno_color_diagnostics) &&
1371 llvm::sys::Process::StandardErrHasColors())
1372 CmdArgs.push_back("-fcolor-diagnostics");
1374 if (!Args.hasFlag(options::OPT_fshow_source_location,
1375 options::OPT_fno_show_source_location))
1376 CmdArgs.push_back("-fno-show-source-location");
1378 // -fdollars-in-identifiers default varies depending on platform and
1379 // language; only pass if specified.
1380 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
1381 options::OPT_fno_dollars_in_identifiers)) {
1382 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
1383 CmdArgs.push_back("-fdollars-in-identifiers");
1385 CmdArgs.push_back("-fno-dollars-in-identifiers");
1388 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
1389 // practical purposes.
1390 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
1391 options::OPT_fno_unit_at_a_time)) {
1392 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
1393 D.Diag(clang::diag::warn_drv_clang_unsupported) << A->getAsString(Args);
1396 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
1398 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
1400 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1401 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1402 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
1403 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1404 CmdArgs.push_back("-fno-builtin-strcat");
1405 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1406 CmdArgs.push_back("-fno-builtin-strcpy");
1410 if (Arg *A = Args.getLastArg(options::OPT_traditional,
1411 options::OPT_traditional_cpp))
1412 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1414 Args.AddLastArg(CmdArgs, options::OPT_dM);
1415 Args.AddLastArg(CmdArgs, options::OPT_dD);
1417 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
1419 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
1420 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
1421 ie = Args.filtered_end(); it != ie; ++it) {
1424 // We translate this by hand to the -cc1 argument, since nightly test uses
1425 // it and developers have been trained to spell it with -mllvm.
1426 if (llvm::StringRef(it->getValue(Args, 0)) == "-disable-llvm-optzns")
1427 CmdArgs.push_back("-disable-llvm-optzns");
1429 it->render(Args, CmdArgs);
1432 if (Output.getType() == types::TY_Dependencies) {
1433 // Handled with other dependency code.
1434 } else if (Output.isPipe()) {
1435 CmdArgs.push_back("-o");
1436 CmdArgs.push_back("-");
1437 } else if (Output.isFilename()) {
1438 CmdArgs.push_back("-o");
1439 CmdArgs.push_back(Output.getFilename());
1441 assert(Output.isNothing() && "Invalid output.");
1444 for (InputInfoList::const_iterator
1445 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1446 const InputInfo &II = *it;
1447 CmdArgs.push_back("-x");
1448 CmdArgs.push_back(types::getTypeName(II.getType()));
1450 CmdArgs.push_back("-");
1451 else if (II.isFilename())
1452 CmdArgs.push_back(II.getFilename());
1454 II.getInputArg().renderAsInput(Args, CmdArgs);
1457 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1460 Args.MakeArgString(getToolChain().GetProgramPath(C, "clang"));
1462 // Optionally embed the -cc1 level arguments into the debug info, for build
1464 if (getToolChain().UseDwarfDebugFlags()) {
1465 llvm::SmallString<256> Flags;
1467 for (unsigned i = 0, e = CmdArgs.size(); i != e; ++i) {
1469 Flags += CmdArgs[i];
1471 CmdArgs.push_back("-dwarf-debug-flags");
1472 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
1475 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
1477 // Explicitly warn that these options are unsupported, even though
1478 // we are allowing compilation to continue.
1479 for (arg_iterator it = Args.filtered_begin(options::OPT_pg),
1480 ie = Args.filtered_end(); it != ie; ++it) {
1482 D.Diag(clang::diag::warn_drv_clang_unsupported) << it->getAsString(Args);
1485 // Claim some arguments which clang supports automatically.
1487 // -fpch-preprocess is used with gcc to add a special marker in the output to
1488 // include the PCH file. Clang's PTH solution is completely transparent, so we
1489 // do not need to deal with it at all.
1490 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
1492 // Claim some arguments which clang doesn't support, but we don't
1493 // care to warn the user about.
1494 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
1495 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
1498 void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
1500 const InputInfo &Output,
1501 const InputInfoList &Inputs,
1502 const ArgList &Args,
1503 const char *LinkingOutput) const {
1504 const Driver &D = getToolChain().getDriver();
1505 ArgStringList CmdArgs;
1507 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1508 const InputInfo &Input = Inputs[0];
1510 // Invoke ourselves in -cc1as mode.
1512 // FIXME: Implement custom jobs for internal actions.
1513 CmdArgs.push_back("-cc1as");
1515 // Add the "effective" target triple.
1516 CmdArgs.push_back("-triple");
1517 std::string TripleStr = getEffectiveClangTriple(D, getToolChain(), Args);
1518 CmdArgs.push_back(Args.MakeArgString(TripleStr));
1520 // Set the output mode, we currently only expect to be used as a real
1522 CmdArgs.push_back("-filetype");
1523 CmdArgs.push_back("obj");
1525 // At -O0, we use -mrelax-all by default.
1527 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1528 IsOpt = !A->getOption().matches(options::OPT_O0);
1529 if (Args.hasFlag(options::OPT_mrelax_all,
1530 options::OPT_mno_relax_all,
1532 CmdArgs.push_back("-mrelax-all");
1534 // FIXME: Add -force_cpusubtype_ALL support, once we have it.
1536 // FIXME: Add -g support, once we have it.
1538 // FIXME: Add -static support, once we have it.
1540 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1541 options::OPT_Xassembler);
1543 assert(Output.isFilename() && "Unexpected lipo output.");
1544 CmdArgs.push_back("-o");
1545 CmdArgs.push_back(Output.getFilename());
1547 if (Input.isPipe()) {
1548 CmdArgs.push_back("-");
1550 assert(Input.isFilename() && "Invalid input.");
1551 CmdArgs.push_back(Input.getFilename());
1555 Args.MakeArgString(getToolChain().GetProgramPath(C, "clang"));
1556 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
1559 void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
1561 const InputInfo &Output,
1562 const InputInfoList &Inputs,
1563 const ArgList &Args,
1564 const char *LinkingOutput) const {
1565 const Driver &D = getToolChain().getDriver();
1566 ArgStringList CmdArgs;
1568 for (ArgList::const_iterator
1569 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
1571 if (A->getOption().hasForwardToGCC()) {
1572 // It is unfortunate that we have to claim here, as this means
1573 // we will basically never report anything interesting for
1574 // platforms using a generic gcc, even if we are just using gcc
1575 // to get to the assembler.
1577 A->render(Args, CmdArgs);
1581 RenderExtraToolArgs(JA, CmdArgs);
1583 // If using a driver driver, force the arch.
1584 const std::string &Arch = getToolChain().getArchName();
1585 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
1586 CmdArgs.push_back("-arch");
1588 // FIXME: Remove these special cases.
1589 if (Arch == "powerpc")
1590 CmdArgs.push_back("ppc");
1591 else if (Arch == "powerpc64")
1592 CmdArgs.push_back("ppc64");
1594 CmdArgs.push_back(Args.MakeArgString(Arch));
1597 // Try to force gcc to match the tool chain we want, if we recognize
1600 // FIXME: The triple class should directly provide the information we want
1602 if (Arch == "i386" || Arch == "powerpc")
1603 CmdArgs.push_back("-m32");
1604 else if (Arch == "x86_64" || Arch == "powerpc64")
1605 CmdArgs.push_back("-m64");
1607 if (Output.isPipe()) {
1608 CmdArgs.push_back("-o");
1609 CmdArgs.push_back("-");
1610 } else if (Output.isFilename()) {
1611 CmdArgs.push_back("-o");
1612 CmdArgs.push_back(Output.getFilename());
1614 assert(Output.isNothing() && "Unexpected output");
1615 CmdArgs.push_back("-fsyntax-only");
1619 // Only pass -x if gcc will understand it; otherwise hope gcc
1620 // understands the suffix correctly. The main use case this would go
1621 // wrong in is for linker inputs if they happened to have an odd
1622 // suffix; really the only way to get this to happen is a command
1623 // like '-x foobar a.c' which will treat a.c like a linker input.
1625 // FIXME: For the linker case specifically, can we safely convert
1626 // inputs into '-Wl,' options?
1627 for (InputInfoList::const_iterator
1628 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1629 const InputInfo &II = *it;
1631 // Don't try to pass LLVM or AST inputs to a generic gcc.
1632 if (II.getType() == types::TY_LLVMBC)
1633 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
1634 << getToolChain().getTripleString();
1635 else if (II.getType() == types::TY_AST)
1636 D.Diag(clang::diag::err_drv_no_ast_support)
1637 << getToolChain().getTripleString();
1639 if (types::canTypeBeUserSpecified(II.getType())) {
1640 CmdArgs.push_back("-x");
1641 CmdArgs.push_back(types::getTypeName(II.getType()));
1645 CmdArgs.push_back("-");
1646 else if (II.isFilename())
1647 CmdArgs.push_back(II.getFilename());
1649 // Don't render as input, we need gcc to do the translations.
1650 II.getInputArg().render(Args, CmdArgs);
1653 const char *GCCName = getToolChain().getDriver().CCCGenericGCCName.c_str();
1655 Args.MakeArgString(getToolChain().GetProgramPath(C, GCCName));
1656 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
1659 void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
1660 ArgStringList &CmdArgs) const {
1661 CmdArgs.push_back("-E");
1664 void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
1665 ArgStringList &CmdArgs) const {
1666 // The type is good enough.
1669 void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
1670 ArgStringList &CmdArgs) const {
1671 const Driver &D = getToolChain().getDriver();
1673 // If -flto, etc. are present then make sure not to force assembly output.
1674 if (JA.getType() == types::TY_LLVMBC)
1675 CmdArgs.push_back("-c");
1677 if (JA.getType() != types::TY_PP_Asm)
1678 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
1679 << getTypeName(JA.getType());
1681 CmdArgs.push_back("-S");
1685 void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
1686 ArgStringList &CmdArgs) const {
1687 CmdArgs.push_back("-c");
1690 void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
1691 ArgStringList &CmdArgs) const {
1692 // The types are (hopefully) good enough.
1695 const char *darwin::CC1::getCC1Name(types::ID Type) const {
1698 assert(0 && "Unexpected type for Darwin CC1 tool.");
1700 case types::TY_C: case types::TY_CHeader:
1701 case types::TY_PP_C: case types::TY_PP_CHeader:
1703 case types::TY_ObjC: case types::TY_ObjCHeader:
1704 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
1706 case types::TY_CXX: case types::TY_CXXHeader:
1707 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
1709 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
1710 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
1711 return "cc1objplus";
1715 const char *darwin::CC1::getBaseInputName(const ArgList &Args,
1716 const InputInfoList &Inputs) {
1717 llvm::sys::Path P(Inputs[0].getBaseInput());
1718 return Args.MakeArgString(P.getLast());
1721 const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
1722 const InputInfoList &Inputs) {
1723 const char *Str = getBaseInputName(Args, Inputs);
1725 if (const char *End = strchr(Str, '.'))
1726 return Args.MakeArgString(std::string(Str, End));
1732 darwin::CC1::getDependencyFileName(const ArgList &Args,
1733 const InputInfoList &Inputs) {
1734 // FIXME: Think about this more.
1737 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1738 std::string Str(OutputOpt->getValue(Args));
1740 Res = Str.substr(0, Str.rfind('.'));
1742 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
1744 return Args.MakeArgString(Res + ".d");
1747 void darwin::CC1::AddCC1Args(const ArgList &Args,
1748 ArgStringList &CmdArgs) const {
1749 const Driver &D = getToolChain().getDriver();
1751 CheckCodeGenerationOptions(D, Args);
1753 // Derived from cc1 spec.
1754 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
1755 !Args.hasArg(options::OPT_mdynamic_no_pic))
1756 CmdArgs.push_back("-fPIC");
1758 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1759 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1760 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1761 CmdArgs.push_back("-fno-builtin-strcat");
1762 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1763 CmdArgs.push_back("-fno-builtin-strcpy");
1766 // gcc has some code here to deal with when no -mmacosx-version-min
1767 // and no -miphoneos-version-min is present, but this never happens
1768 // due to tool chain specific argument translation.
1770 if (Args.hasArg(options::OPT_g_Flag) &&
1771 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
1772 CmdArgs.push_back("-feliminate-unused-debug-symbols");
1775 void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1776 const InputInfoList &Inputs,
1777 const ArgStringList &OutputArgs) const {
1778 const Driver &D = getToolChain().getDriver();
1780 // Derived from cc1_options spec.
1781 if (Args.hasArg(options::OPT_fast) ||
1782 Args.hasArg(options::OPT_fastf) ||
1783 Args.hasArg(options::OPT_fastcp))
1784 CmdArgs.push_back("-O3");
1786 if (Arg *A = Args.getLastArg(options::OPT_pg))
1787 if (Args.hasArg(options::OPT_fomit_frame_pointer))
1788 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1789 << A->getAsString(Args) << "-fomit-frame-pointer";
1791 AddCC1Args(Args, CmdArgs);
1793 if (!Args.hasArg(options::OPT_Q))
1794 CmdArgs.push_back("-quiet");
1796 CmdArgs.push_back("-dumpbase");
1797 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
1799 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
1801 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
1802 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
1804 // FIXME: The goal is to use the user provided -o if that is our
1805 // final output, otherwise to drive from the original input
1806 // name. Find a clean way to go about this.
1807 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
1808 Args.hasArg(options::OPT_o)) {
1809 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
1810 CmdArgs.push_back("-auxbase-strip");
1811 CmdArgs.push_back(OutputOpt->getValue(Args));
1813 CmdArgs.push_back("-auxbase");
1814 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
1817 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
1819 Args.AddAllArgs(CmdArgs, options::OPT_O);
1820 // FIXME: -Wall is getting some special treatment. Investigate.
1821 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1822 Args.AddLastArg(CmdArgs, options::OPT_w);
1823 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
1824 options::OPT_trigraphs);
1825 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1826 // Honor -std-default.
1827 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1828 "-std=", /*Joined=*/true);
1831 if (Args.hasArg(options::OPT_v))
1832 CmdArgs.push_back("-version");
1833 if (Args.hasArg(options::OPT_pg))
1834 CmdArgs.push_back("-p");
1835 Args.AddLastArg(CmdArgs, options::OPT_p);
1837 // The driver treats -fsyntax-only specially.
1838 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1839 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1840 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
1841 // used to inhibit the default -fno-builtin-str{cat,cpy}.
1843 // FIXME: Should we grow a better way to deal with "removing" args?
1844 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
1845 options::OPT_fsyntax_only),
1846 ie = Args.filtered_end(); it != ie; ++it) {
1847 if (!it->getOption().matches(options::OPT_fbuiltin_strcat) &&
1848 !it->getOption().matches(options::OPT_fbuiltin_strcpy)) {
1850 it->render(Args, CmdArgs);
1854 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
1856 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1857 if (Args.hasArg(options::OPT_Qn))
1858 CmdArgs.push_back("-fno-ident");
1860 // FIXME: This isn't correct.
1861 //Args.AddLastArg(CmdArgs, options::OPT__help)
1862 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
1864 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1866 // FIXME: Still don't get what is happening here. Investigate.
1867 Args.AddAllArgs(CmdArgs, options::OPT__param);
1869 if (Args.hasArg(options::OPT_fmudflap) ||
1870 Args.hasArg(options::OPT_fmudflapth)) {
1871 CmdArgs.push_back("-fno-builtin");
1872 CmdArgs.push_back("-fno-merge-constants");
1875 if (Args.hasArg(options::OPT_coverage)) {
1876 CmdArgs.push_back("-fprofile-arcs");
1877 CmdArgs.push_back("-ftest-coverage");
1880 if (types::isCXX(Inputs[0].getType()))
1881 CmdArgs.push_back("-D__private_extern__=extern");
1884 void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1885 const InputInfoList &Inputs,
1886 const ArgStringList &OutputArgs) const {
1887 // Derived from cpp_options
1888 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
1890 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1892 AddCC1Args(Args, CmdArgs);
1894 // NOTE: The code below has some commonality with cpp_options, but
1895 // in classic gcc style ends up sending things in different
1896 // orders. This may be a good merge candidate once we drop pedantic
1899 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
1900 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
1901 options::OPT_trigraphs);
1902 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1903 // Honor -std-default.
1904 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1905 "-std=", /*Joined=*/true);
1907 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1908 Args.AddLastArg(CmdArgs, options::OPT_w);
1910 // The driver treats -fsyntax-only specially.
1911 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
1913 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
1914 !Args.hasArg(options::OPT_fno_working_directory))
1915 CmdArgs.push_back("-fworking-directory");
1917 Args.AddAllArgs(CmdArgs, options::OPT_O);
1918 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1919 if (Args.hasArg(options::OPT_save_temps))
1920 CmdArgs.push_back("-fpch-preprocess");
1923 void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
1924 ArgStringList &CmdArgs,
1925 const InputInfoList &Inputs) const {
1926 const Driver &D = getToolChain().getDriver();
1928 CheckPreprocessingOptions(D, Args);
1930 // Derived from cpp_unique_options.
1931 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
1932 Args.AddLastArg(CmdArgs, options::OPT_C);
1933 Args.AddLastArg(CmdArgs, options::OPT_CC);
1934 if (!Args.hasArg(options::OPT_Q))
1935 CmdArgs.push_back("-quiet");
1936 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
1937 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
1938 Args.AddLastArg(CmdArgs, options::OPT_v);
1939 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
1940 Args.AddLastArg(CmdArgs, options::OPT_P);
1942 // FIXME: Handle %I properly.
1943 if (getToolChain().getArchName() == "x86_64") {
1944 CmdArgs.push_back("-imultilib");
1945 CmdArgs.push_back("x86_64");
1948 if (Args.hasArg(options::OPT_MD)) {
1949 CmdArgs.push_back("-MD");
1950 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1953 if (Args.hasArg(options::OPT_MMD)) {
1954 CmdArgs.push_back("-MMD");
1955 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1958 Args.AddLastArg(CmdArgs, options::OPT_M);
1959 Args.AddLastArg(CmdArgs, options::OPT_MM);
1960 Args.AddAllArgs(CmdArgs, options::OPT_MF);
1961 Args.AddLastArg(CmdArgs, options::OPT_MG);
1962 Args.AddLastArg(CmdArgs, options::OPT_MP);
1963 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
1964 Args.AddAllArgs(CmdArgs, options::OPT_MT);
1965 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
1966 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
1967 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1968 CmdArgs.push_back("-MQ");
1969 CmdArgs.push_back(OutputOpt->getValue(Args));
1973 Args.AddLastArg(CmdArgs, options::OPT_remap);
1974 if (Args.hasArg(options::OPT_g3))
1975 CmdArgs.push_back("-dD");
1976 Args.AddLastArg(CmdArgs, options::OPT_H);
1978 AddCPPArgs(Args, CmdArgs);
1980 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
1981 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
1983 for (InputInfoList::const_iterator
1984 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1985 const InputInfo &II = *it;
1988 CmdArgs.push_back("-");
1990 CmdArgs.push_back(II.getFilename());
1993 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
1994 options::OPT_Xpreprocessor);
1996 if (Args.hasArg(options::OPT_fmudflap)) {
1997 CmdArgs.push_back("-D_MUDFLAP");
1998 CmdArgs.push_back("-include");
1999 CmdArgs.push_back("mf-runtime.h");
2002 if (Args.hasArg(options::OPT_fmudflapth)) {
2003 CmdArgs.push_back("-D_MUDFLAP");
2004 CmdArgs.push_back("-D_MUDFLAPTH");
2005 CmdArgs.push_back("-include");
2006 CmdArgs.push_back("mf-runtime.h");
2010 void darwin::CC1::AddCPPArgs(const ArgList &Args,
2011 ArgStringList &CmdArgs) const {
2012 // Derived from cpp spec.
2014 if (Args.hasArg(options::OPT_static)) {
2015 // The gcc spec is broken here, it refers to dynamic but
2016 // that has been translated. Start by being bug compatible.
2018 // if (!Args.hasArg(arglist.parser.dynamicOption))
2019 CmdArgs.push_back("-D__STATIC__");
2021 CmdArgs.push_back("-D__DYNAMIC__");
2023 if (Args.hasArg(options::OPT_pthread))
2024 CmdArgs.push_back("-D_REENTRANT");
2027 void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
2028 Job &Dest, const InputInfo &Output,
2029 const InputInfoList &Inputs,
2030 const ArgList &Args,
2031 const char *LinkingOutput) const {
2032 ArgStringList CmdArgs;
2034 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2036 CmdArgs.push_back("-E");
2038 if (Args.hasArg(options::OPT_traditional) ||
2039 Args.hasArg(options::OPT_traditional_cpp))
2040 CmdArgs.push_back("-traditional-cpp");
2042 ArgStringList OutputArgs;
2043 if (Output.isFilename()) {
2044 OutputArgs.push_back("-o");
2045 OutputArgs.push_back(Output.getFilename());
2047 assert(Output.isPipe() && "Unexpected CC1 output.");
2050 if (Args.hasArg(options::OPT_E)) {
2051 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2053 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2054 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2057 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2059 const char *CC1Name = getCC1Name(Inputs[0].getType());
2061 Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name));
2062 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2065 void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
2066 Job &Dest, const InputInfo &Output,
2067 const InputInfoList &Inputs,
2068 const ArgList &Args,
2069 const char *LinkingOutput) const {
2070 const Driver &D = getToolChain().getDriver();
2071 ArgStringList CmdArgs;
2073 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2075 types::ID InputType = Inputs[0].getType();
2077 if ((A = Args.getLastArg(options::OPT_traditional)))
2078 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2079 << A->getAsString(Args) << "-E";
2081 if (Output.getType() == types::TY_LLVMAsm)
2082 CmdArgs.push_back("-emit-llvm");
2083 else if (Output.getType() == types::TY_LLVMBC)
2084 CmdArgs.push_back("-emit-llvm-bc");
2085 else if (Output.getType() == types::TY_AST)
2086 D.Diag(clang::diag::err_drv_no_ast_support)
2087 << getToolChain().getTripleString();
2088 else if (JA.getType() != types::TY_PP_Asm &&
2089 JA.getType() != types::TY_PCH)
2090 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2091 << getTypeName(JA.getType());
2093 ArgStringList OutputArgs;
2094 if (Output.getType() != types::TY_PCH) {
2095 OutputArgs.push_back("-o");
2096 if (Output.isPipe())
2097 OutputArgs.push_back("-");
2098 else if (Output.isNothing())
2099 OutputArgs.push_back("/dev/null");
2101 OutputArgs.push_back(Output.getFilename());
2104 // There is no need for this level of compatibility, but it makes
2106 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2107 Args.hasArg(options::OPT_S));
2109 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
2110 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
2111 if (OutputArgsEarly) {
2112 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2114 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2115 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2118 CmdArgs.push_back("-fpreprocessed");
2120 for (InputInfoList::const_iterator
2121 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2122 const InputInfo &II = *it;
2124 // Reject AST inputs.
2125 if (II.getType() == types::TY_AST) {
2126 D.Diag(clang::diag::err_drv_no_ast_support)
2127 << getToolChain().getTripleString();
2132 CmdArgs.push_back("-");
2134 CmdArgs.push_back(II.getFilename());
2137 if (OutputArgsEarly) {
2138 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2140 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2141 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2145 if (Output.getType() == types::TY_PCH) {
2146 assert(Output.isFilename() && "Invalid PCH output.");
2148 CmdArgs.push_back("-o");
2149 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2150 // to be a good reason.
2151 CmdArgs.push_back("/dev/null");
2153 CmdArgs.push_back("--output-pch=");
2154 CmdArgs.push_back(Output.getFilename());
2157 const char *CC1Name = getCC1Name(Inputs[0].getType());
2159 Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name));
2160 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2163 void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2164 Job &Dest, const InputInfo &Output,
2165 const InputInfoList &Inputs,
2166 const ArgList &Args,
2167 const char *LinkingOutput) const {
2168 ArgStringList CmdArgs;
2170 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2171 const InputInfo &Input = Inputs[0];
2173 // Bit of a hack, this is only used for original inputs.
2175 // FIXME: This is broken for preprocessed .s inputs.
2176 if (Input.isFilename() &&
2177 strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
2178 if (Args.hasArg(options::OPT_gstabs))
2179 CmdArgs.push_back("--gstabs");
2180 else if (Args.hasArg(options::OPT_g_Group))
2181 CmdArgs.push_back("--gdwarf2");
2184 // Derived from asm spec.
2185 AddDarwinArch(Args, CmdArgs);
2187 if (!getDarwinToolChain().isTargetIPhoneOS() ||
2188 Args.hasArg(options::OPT_force__cpusubtype__ALL))
2189 CmdArgs.push_back("-force_cpusubtype_ALL");
2191 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
2192 (Args.hasArg(options::OPT_mkernel) ||
2193 Args.hasArg(options::OPT_static) ||
2194 Args.hasArg(options::OPT_fapple_kext)))
2195 CmdArgs.push_back("-static");
2197 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2198 options::OPT_Xassembler);
2200 assert(Output.isFilename() && "Unexpected lipo output.");
2201 CmdArgs.push_back("-o");
2202 CmdArgs.push_back(Output.getFilename());
2204 if (Input.isPipe()) {
2205 CmdArgs.push_back("-");
2207 assert(Input.isFilename() && "Invalid input.");
2208 CmdArgs.push_back(Input.getFilename());
2211 // asm_final spec is empty.
2214 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
2215 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2218 /// Helper routine for seeing if we should use dsymutil; this is a
2219 /// gcc compatible hack, we should remove it and use the input
2220 /// type information.
2221 static bool isSourceSuffix(const char *Str) {
2222 // match: 'C', 'CPP', 'c', 'cc', 'cp', 'c++', 'cpp', 'cxx', 'm',
2224 return llvm::StringSwitch<bool>(Str)
2238 void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
2239 ArgStringList &CmdArgs) const {
2240 llvm::StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
2242 // Derived from darwin_arch spec.
2243 CmdArgs.push_back("-arch");
2244 CmdArgs.push_back(Args.MakeArgString(ArchName));
2246 // FIXME: Is this needed anymore?
2247 if (ArchName == "arm")
2248 CmdArgs.push_back("-force_cpusubtype_ALL");
2251 void darwin::Link::AddLinkArgs(const ArgList &Args,
2252 ArgStringList &CmdArgs) const {
2253 const Driver &D = getToolChain().getDriver();
2255 // Derived from the "link" spec.
2256 Args.AddAllArgs(CmdArgs, options::OPT_static);
2257 if (!Args.hasArg(options::OPT_static))
2258 CmdArgs.push_back("-dynamic");
2259 if (Args.hasArg(options::OPT_fgnu_runtime)) {
2260 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
2261 // here. How do we wish to handle such things?
2264 if (!Args.hasArg(options::OPT_dynamiclib)) {
2265 AddDarwinArch(Args, CmdArgs);
2266 // FIXME: Why do this only on this path?
2267 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
2269 Args.AddLastArg(CmdArgs, options::OPT_bundle);
2270 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
2271 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
2274 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
2275 (A = Args.getLastArg(options::OPT_current__version)) ||
2276 (A = Args.getLastArg(options::OPT_install__name)))
2277 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2278 << A->getAsString(Args) << "-dynamiclib";
2280 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
2281 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
2282 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
2284 CmdArgs.push_back("-dylib");
2287 if ((A = Args.getLastArg(options::OPT_bundle)) ||
2288 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
2289 (A = Args.getLastArg(options::OPT_client__name)) ||
2290 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
2291 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
2292 (A = Args.getLastArg(options::OPT_private__bundle)))
2293 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2294 << A->getAsString(Args) << "-dynamiclib";
2296 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
2297 "-dylib_compatibility_version");
2298 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
2299 "-dylib_current_version");
2301 AddDarwinArch(Args, CmdArgs);
2303 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
2304 "-dylib_install_name");
2307 Args.AddLastArg(CmdArgs, options::OPT_all__load);
2308 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
2309 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
2310 if (getDarwinToolChain().isTargetIPhoneOS())
2311 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
2312 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
2313 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
2314 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
2315 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
2316 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
2317 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
2318 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
2319 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
2320 Args.AddAllArgs(CmdArgs, options::OPT_init);
2322 // Adding all arguments doesn't make sense here but this is what gcc does. One
2323 // of this should always be present thanks to argument translation.
2324 assert((Args.hasArg(options::OPT_mmacosx_version_min_EQ) ||
2325 Args.hasArg(options::OPT_miphoneos_version_min_EQ)) &&
2326 "Missing version argument (lost in translation)?");
2327 Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
2328 "-macosx_version_min");
2329 Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
2330 "-iphoneos_version_min");
2331 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
2332 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
2333 Args.AddLastArg(CmdArgs, options::OPT_single__module);
2334 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
2335 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
2337 if (Args.hasArg(options::OPT_fpie))
2338 CmdArgs.push_back("-pie");
2340 Args.AddLastArg(CmdArgs, options::OPT_prebind);
2341 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
2342 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
2343 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
2344 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
2345 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
2346 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
2347 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
2348 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
2349 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
2350 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
2351 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
2352 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
2353 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
2354 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
2355 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
2357 Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
2358 if (getDarwinToolChain().isTargetIPhoneOS()) {
2359 if (!Args.hasArg(options::OPT_isysroot)) {
2360 CmdArgs.push_back("-syslibroot");
2361 CmdArgs.push_back("/Developer/SDKs/Extra");
2365 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
2366 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
2367 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
2368 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
2369 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
2370 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
2371 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
2372 Args.AddAllArgs(CmdArgs, options::OPT_y);
2373 Args.AddLastArg(CmdArgs, options::OPT_w);
2374 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
2375 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
2376 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
2377 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
2378 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
2379 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
2380 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
2381 Args.AddLastArg(CmdArgs, options::OPT_whyload);
2382 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
2383 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
2384 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
2385 Args.AddLastArg(CmdArgs, options::OPT_Mach);
2388 void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
2389 Job &Dest, const InputInfo &Output,
2390 const InputInfoList &Inputs,
2391 const ArgList &Args,
2392 const char *LinkingOutput) const {
2393 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
2395 // The logic here is derived from gcc's behavior; most of which
2396 // comes from specs (starting with link_command). Consult gcc for
2397 // more information.
2398 ArgStringList CmdArgs;
2400 // I'm not sure why this particular decomposition exists in gcc, but
2401 // we follow suite for ease of comparison.
2402 AddLinkArgs(Args, CmdArgs);
2404 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
2405 Args.AddAllArgs(CmdArgs, options::OPT_s);
2406 Args.AddAllArgs(CmdArgs, options::OPT_t);
2407 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2408 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
2409 Args.AddAllArgs(CmdArgs, options::OPT_A);
2410 Args.AddLastArg(CmdArgs, options::OPT_e);
2411 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
2412 Args.AddAllArgs(CmdArgs, options::OPT_r);
2414 CmdArgs.push_back("-o");
2415 CmdArgs.push_back(Output.getFilename());
2417 if (!Args.hasArg(options::OPT_A) &&
2418 !Args.hasArg(options::OPT_nostdlib) &&
2419 !Args.hasArg(options::OPT_nostartfiles)) {
2420 // Derived from startfile spec.
2421 if (Args.hasArg(options::OPT_dynamiclib)) {
2422 // Derived from darwin_dylib1 spec.
2423 if (getDarwinToolChain().isTargetIPhoneOS()) {
2424 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2425 CmdArgs.push_back("-ldylib1.o");
2427 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2428 CmdArgs.push_back("-ldylib1.o");
2429 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2430 CmdArgs.push_back("-ldylib1.10.5.o");
2433 if (Args.hasArg(options::OPT_bundle)) {
2434 if (!Args.hasArg(options::OPT_static)) {
2435 // Derived from darwin_bundle1 spec.
2436 if (getDarwinToolChain().isTargetIPhoneOS()) {
2437 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2438 CmdArgs.push_back("-lbundle1.o");
2440 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2441 CmdArgs.push_back("-lbundle1.o");
2445 if (Args.hasArg(options::OPT_pg)) {
2446 if (Args.hasArg(options::OPT_static) ||
2447 Args.hasArg(options::OPT_object) ||
2448 Args.hasArg(options::OPT_preload)) {
2449 CmdArgs.push_back("-lgcrt0.o");
2451 CmdArgs.push_back("-lgcrt1.o");
2453 // darwin_crt2 spec is empty.
2456 if (Args.hasArg(options::OPT_static) ||
2457 Args.hasArg(options::OPT_object) ||
2458 Args.hasArg(options::OPT_preload)) {
2459 CmdArgs.push_back("-lcrt0.o");
2461 // Derived from darwin_crt1 spec.
2462 if (getDarwinToolChain().isTargetIPhoneOS()) {
2463 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2464 CmdArgs.push_back("-lcrt1.o");
2466 CmdArgs.push_back("-lcrt1.3.1.o");
2468 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2469 CmdArgs.push_back("-lcrt1.o");
2470 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2471 CmdArgs.push_back("-lcrt1.10.5.o");
2473 CmdArgs.push_back("-lcrt1.10.6.o");
2475 // darwin_crt2 spec is empty.
2482 if (!getDarwinToolChain().isTargetIPhoneOS() &&
2483 Args.hasArg(options::OPT_shared_libgcc) &&
2484 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
2486 Args.MakeArgString(getToolChain().GetFilePath(C, "crt3.o"));
2487 CmdArgs.push_back(Str);
2491 Args.AddAllArgs(CmdArgs, options::OPT_L);
2493 if (Args.hasArg(options::OPT_fopenmp))
2494 // This is more complicated in gcc...
2495 CmdArgs.push_back("-lgomp");
2497 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
2499 for (InputInfoList::const_iterator
2500 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2501 const InputInfo &II = *it;
2502 if (II.isFilename())
2503 CmdArgs.push_back(II.getFilename());
2505 II.getInputArg().renderAsInput(Args, CmdArgs);
2508 if (LinkingOutput) {
2509 CmdArgs.push_back("-arch_multiple");
2510 CmdArgs.push_back("-final_output");
2511 CmdArgs.push_back(LinkingOutput);
2514 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2515 Args.hasArg(options::OPT_fprofile_generate) ||
2516 Args.hasArg(options::OPT_fcreate_profile) ||
2517 Args.hasArg(options::OPT_coverage))
2518 CmdArgs.push_back("-lgcov");
2520 if (Args.hasArg(options::OPT_fnested_functions))
2521 CmdArgs.push_back("-allow_stack_execute");
2523 if (!Args.hasArg(options::OPT_nostdlib) &&
2524 !Args.hasArg(options::OPT_nodefaultlibs)) {
2525 // FIXME: g++ is more complicated here, it tries to put -lstdc++
2526 // before -lm, for example.
2527 if (getToolChain().getDriver().CCCIsCXX)
2528 CmdArgs.push_back("-lstdc++");
2530 // link_ssp spec is empty.
2532 // Let the tool chain choose which runtime library to link.
2533 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
2536 if (!Args.hasArg(options::OPT_A) &&
2537 !Args.hasArg(options::OPT_nostdlib) &&
2538 !Args.hasArg(options::OPT_nostartfiles)) {
2539 // endfile_spec is empty.
2542 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2543 Args.AddAllArgs(CmdArgs, options::OPT_F);
2546 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2547 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2549 // Find the first non-empty base input (we want to ignore linker
2551 const char *BaseInput = "";
2552 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
2553 if (Inputs[i].getBaseInput()[0] != '\0') {
2554 BaseInput = Inputs[i].getBaseInput();
2559 // Run dsymutil if we are making an executable in a single step.
2561 // FIXME: Currently we don't want to do this when we are part of a
2562 // universal build step, as this would end up creating stray temp
2564 if (!LinkingOutput &&
2565 Args.getLastArg(options::OPT_g_Group) &&
2566 !Args.getLastArg(options::OPT_gstabs) &&
2567 !Args.getLastArg(options::OPT_g0)) {
2568 // FIXME: This is gross, but matches gcc. The test only considers
2569 // the suffix (not the -x type), and then only of the first
2570 // source input. Awesome.
2571 const char *Suffix = strrchr(BaseInput, '.');
2572 if (Suffix && isSourceSuffix(Suffix + 1)) {
2574 Args.MakeArgString(getToolChain().GetProgramPath(C, "dsymutil"));
2575 ArgStringList CmdArgs;
2576 CmdArgs.push_back(Output.getFilename());
2577 C.getJobs().addCommand(new Command(JA, *this, Exec, CmdArgs));
2582 void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
2583 Job &Dest, const InputInfo &Output,
2584 const InputInfoList &Inputs,
2585 const ArgList &Args,
2586 const char *LinkingOutput) const {
2587 ArgStringList CmdArgs;
2589 CmdArgs.push_back("-create");
2590 assert(Output.isFilename() && "Unexpected lipo output.");
2592 CmdArgs.push_back("-output");
2593 CmdArgs.push_back(Output.getFilename());
2595 for (InputInfoList::const_iterator
2596 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2597 const InputInfo &II = *it;
2598 assert(II.isFilename() && "Unexpected lipo input.");
2599 CmdArgs.push_back(II.getFilename());
2602 Args.MakeArgString(getToolChain().GetProgramPath(C, "lipo"));
2603 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2606 void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2607 Job &Dest, const InputInfo &Output,
2608 const InputInfoList &Inputs,
2609 const ArgList &Args,
2610 const char *LinkingOutput) const {
2611 ArgStringList CmdArgs;
2613 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2614 options::OPT_Xassembler);
2616 CmdArgs.push_back("-o");
2617 if (Output.isPipe())
2618 CmdArgs.push_back("-");
2620 CmdArgs.push_back(Output.getFilename());
2622 for (InputInfoList::const_iterator
2623 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2624 const InputInfo &II = *it;
2626 CmdArgs.push_back("-");
2628 CmdArgs.push_back(II.getFilename());
2632 Args.MakeArgString(getToolChain().GetProgramPath(C, "gas"));
2633 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2636 void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
2637 Job &Dest, const InputInfo &Output,
2638 const InputInfoList &Inputs,
2639 const ArgList &Args,
2640 const char *LinkingOutput) const {
2641 const Driver &D = getToolChain().getDriver();
2642 ArgStringList CmdArgs;
2644 if ((!Args.hasArg(options::OPT_nostdlib)) &&
2645 (!Args.hasArg(options::OPT_shared))) {
2646 CmdArgs.push_back("-e");
2647 CmdArgs.push_back("_start");
2650 if (Args.hasArg(options::OPT_static)) {
2651 CmdArgs.push_back("-Bstatic");
2652 CmdArgs.push_back("-dn");
2654 // CmdArgs.push_back("--eh-frame-hdr");
2655 CmdArgs.push_back("-Bdynamic");
2656 if (Args.hasArg(options::OPT_shared)) {
2657 CmdArgs.push_back("-shared");
2659 CmdArgs.push_back("--dynamic-linker");
2660 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
2664 if (Output.isPipe()) {
2665 CmdArgs.push_back("-o");
2666 CmdArgs.push_back("-");
2667 } else if (Output.isFilename()) {
2668 CmdArgs.push_back("-o");
2669 CmdArgs.push_back(Output.getFilename());
2671 assert(Output.isNothing() && "Invalid output.");
2674 if (!Args.hasArg(options::OPT_nostdlib) &&
2675 !Args.hasArg(options::OPT_nostartfiles)) {
2676 if (!Args.hasArg(options::OPT_shared)) {
2677 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
2678 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2679 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
2681 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2683 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
2686 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
2687 + getToolChain().getTripleString()
2690 Args.AddAllArgs(CmdArgs, options::OPT_L);
2691 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2692 Args.AddAllArgs(CmdArgs, options::OPT_e);
2694 for (InputInfoList::const_iterator
2695 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2696 const InputInfo &II = *it;
2698 // Don't try to pass LLVM inputs to a generic gcc.
2699 if (II.getType() == types::TY_LLVMBC)
2700 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2701 << getToolChain().getTripleString();
2704 CmdArgs.push_back("-");
2705 else if (II.isFilename())
2706 CmdArgs.push_back(II.getFilename());
2708 II.getInputArg().renderAsInput(Args, CmdArgs);
2711 if (!Args.hasArg(options::OPT_nostdlib) &&
2712 !Args.hasArg(options::OPT_nodefaultlibs)) {
2713 // FIXME: For some reason GCC passes -lgcc before adding
2714 // the default system libraries. Just mimic this for now.
2715 CmdArgs.push_back("-lgcc");
2717 if (Args.hasArg(options::OPT_pthread))
2718 CmdArgs.push_back("-pthread");
2719 if (!Args.hasArg(options::OPT_shared))
2720 CmdArgs.push_back("-lc");
2721 CmdArgs.push_back("-lgcc");
2724 if (!Args.hasArg(options::OPT_nostdlib) &&
2725 !Args.hasArg(options::OPT_nostartfiles)) {
2726 if (!Args.hasArg(options::OPT_shared))
2727 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
2729 // CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
2733 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2734 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2737 void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2738 Job &Dest, const InputInfo &Output,
2739 const InputInfoList &Inputs,
2740 const ArgList &Args,
2741 const char *LinkingOutput) const {
2742 ArgStringList CmdArgs;
2744 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2745 options::OPT_Xassembler);
2747 CmdArgs.push_back("-o");
2748 if (Output.isPipe())
2749 CmdArgs.push_back("-");
2751 CmdArgs.push_back(Output.getFilename());
2753 for (InputInfoList::const_iterator
2754 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2755 const InputInfo &II = *it;
2757 CmdArgs.push_back("-");
2759 CmdArgs.push_back(II.getFilename());
2763 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
2764 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2767 void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
2768 Job &Dest, const InputInfo &Output,
2769 const InputInfoList &Inputs,
2770 const ArgList &Args,
2771 const char *LinkingOutput) const {
2772 const Driver &D = getToolChain().getDriver();
2773 ArgStringList CmdArgs;
2775 if ((!Args.hasArg(options::OPT_nostdlib)) &&
2776 (!Args.hasArg(options::OPT_shared))) {
2777 CmdArgs.push_back("-e");
2778 CmdArgs.push_back("__start");
2781 if (Args.hasArg(options::OPT_static)) {
2782 CmdArgs.push_back("-Bstatic");
2784 CmdArgs.push_back("--eh-frame-hdr");
2785 CmdArgs.push_back("-Bdynamic");
2786 if (Args.hasArg(options::OPT_shared)) {
2787 CmdArgs.push_back("-shared");
2789 CmdArgs.push_back("-dynamic-linker");
2790 CmdArgs.push_back("/usr/libexec/ld.so");
2794 if (Output.isPipe()) {
2795 CmdArgs.push_back("-o");
2796 CmdArgs.push_back("-");
2797 } else if (Output.isFilename()) {
2798 CmdArgs.push_back("-o");
2799 CmdArgs.push_back(Output.getFilename());
2801 assert(Output.isNothing() && "Invalid output.");
2804 if (!Args.hasArg(options::OPT_nostdlib) &&
2805 !Args.hasArg(options::OPT_nostartfiles)) {
2806 if (!Args.hasArg(options::OPT_shared)) {
2807 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt0.o")));
2808 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
2810 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
2814 std::string Triple = getToolChain().getTripleString();
2815 if (Triple.substr(0, 6) == "x86_64")
2816 Triple.replace(0, 6, "amd64");
2817 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
2820 Args.AddAllArgs(CmdArgs, options::OPT_L);
2821 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2822 Args.AddAllArgs(CmdArgs, options::OPT_e);
2824 for (InputInfoList::const_iterator
2825 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2826 const InputInfo &II = *it;
2828 // Don't try to pass LLVM inputs to a generic gcc.
2829 if (II.getType() == types::TY_LLVMBC)
2830 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2831 << getToolChain().getTripleString();
2834 CmdArgs.push_back("-");
2835 else if (II.isFilename())
2836 CmdArgs.push_back(II.getFilename());
2838 II.getInputArg().renderAsInput(Args, CmdArgs);
2841 if (!Args.hasArg(options::OPT_nostdlib) &&
2842 !Args.hasArg(options::OPT_nodefaultlibs)) {
2843 // FIXME: For some reason GCC passes -lgcc before adding
2844 // the default system libraries. Just mimic this for now.
2845 CmdArgs.push_back("-lgcc");
2847 if (Args.hasArg(options::OPT_pthread))
2848 CmdArgs.push_back("-pthread");
2849 if (!Args.hasArg(options::OPT_shared))
2850 CmdArgs.push_back("-lc");
2851 CmdArgs.push_back("-lgcc");
2854 if (!Args.hasArg(options::OPT_nostdlib) &&
2855 !Args.hasArg(options::OPT_nostartfiles)) {
2856 if (!Args.hasArg(options::OPT_shared))
2857 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
2859 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
2863 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
2864 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2867 void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2868 Job &Dest, const InputInfo &Output,
2869 const InputInfoList &Inputs,
2870 const ArgList &Args,
2871 const char *LinkingOutput) const {
2872 ArgStringList CmdArgs;
2874 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2875 // instruct as in the base system to assemble 32-bit code.
2876 if (getToolChain().getArchName() == "i386")
2877 CmdArgs.push_back("--32");
2880 // Set byte order explicitly
2881 if (getToolChain().getArchName() == "mips")
2882 CmdArgs.push_back("-EB");
2883 else if (getToolChain().getArchName() == "mipsel")
2884 CmdArgs.push_back("-EL");
2886 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2887 options::OPT_Xassembler);
2889 CmdArgs.push_back("-o");
2890 if (Output.isPipe())
2891 CmdArgs.push_back("-");
2893 CmdArgs.push_back(Output.getFilename());
2895 for (InputInfoList::const_iterator
2896 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2897 const InputInfo &II = *it;
2899 CmdArgs.push_back("-");
2901 CmdArgs.push_back(II.getFilename());
2905 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
2906 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2909 void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
2910 Job &Dest, const InputInfo &Output,
2911 const InputInfoList &Inputs,
2912 const ArgList &Args,
2913 const char *LinkingOutput) const {
2914 const Driver &D = getToolChain().getDriver();
2915 ArgStringList CmdArgs;
2917 if (Args.hasArg(options::OPT_static)) {
2918 CmdArgs.push_back("-Bstatic");
2920 CmdArgs.push_back("--eh-frame-hdr");
2921 if (Args.hasArg(options::OPT_shared)) {
2922 CmdArgs.push_back("-Bshareable");
2924 CmdArgs.push_back("-dynamic-linker");
2925 CmdArgs.push_back("/libexec/ld-elf.so.1");
2929 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2930 // instruct ld in the base system to link 32-bit code.
2931 if (getToolChain().getArchName() == "i386") {
2932 CmdArgs.push_back("-m");
2933 CmdArgs.push_back("elf_i386_fbsd");
2936 if (Output.isPipe()) {
2937 CmdArgs.push_back("-o");
2938 CmdArgs.push_back("-");
2939 } else if (Output.isFilename()) {
2940 CmdArgs.push_back("-o");
2941 CmdArgs.push_back(Output.getFilename());
2943 assert(Output.isNothing() && "Invalid output.");
2946 if (!Args.hasArg(options::OPT_nostdlib) &&
2947 !Args.hasArg(options::OPT_nostartfiles)) {
2948 if (!Args.hasArg(options::OPT_shared)) {
2949 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
2950 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2951 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
2953 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2954 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
2958 Args.AddAllArgs(CmdArgs, options::OPT_L);
2959 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2960 Args.AddAllArgs(CmdArgs, options::OPT_e);
2962 for (InputInfoList::const_iterator
2963 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2964 const InputInfo &II = *it;
2966 // Don't try to pass LLVM inputs to a generic gcc.
2967 if (II.getType() == types::TY_LLVMBC)
2968 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2969 << getToolChain().getTripleString();
2972 CmdArgs.push_back("-");
2973 else if (II.isFilename())
2974 CmdArgs.push_back(II.getFilename());
2976 II.getInputArg().renderAsInput(Args, CmdArgs);
2979 if (!Args.hasArg(options::OPT_nostdlib) &&
2980 !Args.hasArg(options::OPT_nodefaultlibs)) {
2982 CmdArgs.push_back("-lstdc++");
2983 CmdArgs.push_back("-lm");
2985 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
2986 // the default system libraries. Just mimic this for now.
2987 CmdArgs.push_back("-lgcc");
2988 if (Args.hasArg(options::OPT_static)) {
2989 CmdArgs.push_back("-lgcc_eh");
2991 CmdArgs.push_back("--as-needed");
2992 CmdArgs.push_back("-lgcc_s");
2993 CmdArgs.push_back("--no-as-needed");
2996 if (Args.hasArg(options::OPT_pthread))
2997 CmdArgs.push_back("-lpthread");
2998 CmdArgs.push_back("-lc");
3000 CmdArgs.push_back("-lgcc");
3001 if (Args.hasArg(options::OPT_static)) {
3002 CmdArgs.push_back("-lgcc_eh");
3004 CmdArgs.push_back("--as-needed");
3005 CmdArgs.push_back("-lgcc_s");
3006 CmdArgs.push_back("--no-as-needed");
3010 if (!Args.hasArg(options::OPT_nostdlib) &&
3011 !Args.hasArg(options::OPT_nostartfiles)) {
3012 if (!Args.hasArg(options::OPT_shared))
3013 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
3015 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
3016 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
3020 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
3021 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
3026 // For now, DragonFly Assemble does just about the same as for
3027 // FreeBSD, but this may change soon.
3028 void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
3029 Job &Dest, const InputInfo &Output,
3030 const InputInfoList &Inputs,
3031 const ArgList &Args,
3032 const char *LinkingOutput) const {
3033 ArgStringList CmdArgs;
3035 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3036 // instruct as in the base system to assemble 32-bit code.
3037 if (getToolChain().getArchName() == "i386")
3038 CmdArgs.push_back("--32");
3040 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3041 options::OPT_Xassembler);
3043 CmdArgs.push_back("-o");
3044 if (Output.isPipe())
3045 CmdArgs.push_back("-");
3047 CmdArgs.push_back(Output.getFilename());
3049 for (InputInfoList::const_iterator
3050 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3051 const InputInfo &II = *it;
3053 CmdArgs.push_back("-");
3055 CmdArgs.push_back(II.getFilename());
3059 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
3060 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
3063 void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
3064 Job &Dest, const InputInfo &Output,
3065 const InputInfoList &Inputs,
3066 const ArgList &Args,
3067 const char *LinkingOutput) const {
3068 const Driver &D = getToolChain().getDriver();
3069 ArgStringList CmdArgs;
3071 if (Args.hasArg(options::OPT_static)) {
3072 CmdArgs.push_back("-Bstatic");
3074 if (Args.hasArg(options::OPT_shared))
3075 CmdArgs.push_back("-Bshareable");
3077 CmdArgs.push_back("-dynamic-linker");
3078 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
3082 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3083 // instruct ld in the base system to link 32-bit code.
3084 if (getToolChain().getArchName() == "i386") {
3085 CmdArgs.push_back("-m");
3086 CmdArgs.push_back("elf_i386");
3089 if (Output.isPipe()) {
3090 CmdArgs.push_back("-o");
3091 CmdArgs.push_back("-");
3092 } else if (Output.isFilename()) {
3093 CmdArgs.push_back("-o");
3094 CmdArgs.push_back(Output.getFilename());
3096 assert(Output.isNothing() && "Invalid output.");
3099 if (!Args.hasArg(options::OPT_nostdlib) &&
3100 !Args.hasArg(options::OPT_nostartfiles)) {
3101 if (!Args.hasArg(options::OPT_shared)) {
3102 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
3103 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
3104 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
3106 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
3107 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
3111 Args.AddAllArgs(CmdArgs, options::OPT_L);
3112 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3113 Args.AddAllArgs(CmdArgs, options::OPT_e);
3115 for (InputInfoList::const_iterator
3116 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3117 const InputInfo &II = *it;
3119 // Don't try to pass LLVM inputs to a generic gcc.
3120 if (II.getType() == types::TY_LLVMBC)
3121 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
3122 << getToolChain().getTripleString();
3125 CmdArgs.push_back("-");
3126 else if (II.isFilename())
3127 CmdArgs.push_back(II.getFilename());
3129 II.getInputArg().renderAsInput(Args, CmdArgs);
3132 if (!Args.hasArg(options::OPT_nostdlib) &&
3133 !Args.hasArg(options::OPT_nodefaultlibs)) {
3134 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
3136 CmdArgs.push_back("-L/usr/lib/gcc41");
3138 if (!Args.hasArg(options::OPT_static)) {
3139 CmdArgs.push_back("-rpath");
3140 CmdArgs.push_back("/usr/lib/gcc41");
3142 CmdArgs.push_back("-rpath-link");
3143 CmdArgs.push_back("/usr/lib/gcc41");
3145 CmdArgs.push_back("-rpath");
3146 CmdArgs.push_back("/usr/lib");
3148 CmdArgs.push_back("-rpath-link");
3149 CmdArgs.push_back("/usr/lib");
3152 if (Args.hasArg(options::OPT_shared)) {
3153 CmdArgs.push_back("-lgcc_pic");
3155 CmdArgs.push_back("-lgcc");
3159 if (Args.hasArg(options::OPT_pthread))
3160 CmdArgs.push_back("-lpthread");
3162 if (!Args.hasArg(options::OPT_nolibc)) {
3163 CmdArgs.push_back("-lc");
3166 if (Args.hasArg(options::OPT_shared)) {
3167 CmdArgs.push_back("-lgcc_pic");
3169 CmdArgs.push_back("-lgcc");
3173 if (!Args.hasArg(options::OPT_nostdlib) &&
3174 !Args.hasArg(options::OPT_nostartfiles)) {
3175 if (!Args.hasArg(options::OPT_shared))
3176 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
3178 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
3179 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
3183 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
3184 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));