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 //===----------------------------------------------------------------------===//
11 #include "InputInfo.h"
12 #include "ToolChains.h"
13 #include "clang/Basic/ObjCRuntime.h"
14 #include "clang/Basic/Version.h"
15 #include "clang/Driver/Action.h"
16 #include "clang/Driver/Compilation.h"
17 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/DriverDiagnostic.h"
19 #include "clang/Driver/Job.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Driver/SanitizerArgs.h"
22 #include "clang/Driver/ToolChain.h"
23 #include "clang/Driver/Util.h"
24 #include "clang/Sema/SemaDiagnostic.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/ADT/Twine.h"
29 #include "llvm/Option/Arg.h"
30 #include "llvm/Option/ArgList.h"
31 #include "llvm/Option/Option.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Format.h"
35 #include "llvm/Support/Host.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Support/Program.h"
38 #include "llvm/Support/Process.h"
39 #include "llvm/Support/raw_ostream.h"
42 using namespace clang::driver;
43 using namespace clang::driver::tools;
44 using namespace clang;
45 using namespace llvm::opt;
47 /// CheckPreprocessingOptions - Perform some validation of preprocessing
48 /// arguments that is shared with gcc.
49 static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
50 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
51 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP())
52 D.Diag(diag::err_drv_argument_only_allowed_with)
53 << A->getAsString(Args) << "-E";
56 /// CheckCodeGenerationOptions - Perform some validation of code generation
57 /// arguments that is shared with gcc.
58 static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
59 // In gcc, only ARM checks this, but it seems reasonable to check universally.
60 if (Args.hasArg(options::OPT_static))
61 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
62 options::OPT_mdynamic_no_pic))
63 D.Diag(diag::err_drv_argument_not_allowed_with)
64 << A->getAsString(Args) << "-static";
67 // Quote target names for inclusion in GNU Make dependency files.
68 // Only the characters '$', '#', ' ', '\t' are quoted.
69 static void QuoteTarget(StringRef Target,
70 SmallVectorImpl<char> &Res) {
71 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
75 // Escape the preceding backslashes
76 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
79 // Escape the space/tab
92 Res.push_back(Target[i]);
96 static void addDirectoryList(const ArgList &Args,
97 ArgStringList &CmdArgs,
100 const char *DirList = ::getenv(EnvVar);
101 bool CombinedArg = false;
104 return; // Nothing to do.
106 StringRef Name(ArgName);
107 if (Name.equals("-I") || Name.equals("-L"))
110 StringRef Dirs(DirList);
111 if (Dirs.empty()) // Empty string should not add '.'.
114 StringRef::size_type Delim;
115 while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) {
116 if (Delim == 0) { // Leading colon.
118 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
120 CmdArgs.push_back(ArgName);
121 CmdArgs.push_back(".");
125 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim)));
127 CmdArgs.push_back(ArgName);
128 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim)));
131 Dirs = Dirs.substr(Delim + 1);
134 if (Dirs.empty()) { // Trailing colon.
136 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + "."));
138 CmdArgs.push_back(ArgName);
139 CmdArgs.push_back(".");
141 } else { // Add the last path.
143 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs));
145 CmdArgs.push_back(ArgName);
146 CmdArgs.push_back(Args.MakeArgString(Dirs));
151 static void AddLinkerInputs(const ToolChain &TC,
152 const InputInfoList &Inputs, const ArgList &Args,
153 ArgStringList &CmdArgs) {
154 const Driver &D = TC.getDriver();
156 // Add extra linker input arguments which are not treated as inputs
157 // (constructed via -Xarch_).
158 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input);
160 for (InputInfoList::const_iterator
161 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
162 const InputInfo &II = *it;
164 if (!TC.HasNativeLLVMSupport()) {
165 // Don't try to pass LLVM inputs unless we have native support.
166 if (II.getType() == types::TY_LLVM_IR ||
167 II.getType() == types::TY_LTO_IR ||
168 II.getType() == types::TY_LLVM_BC ||
169 II.getType() == types::TY_LTO_BC)
170 D.Diag(diag::err_drv_no_linker_llvm_support)
171 << TC.getTripleString();
174 // Add filenames immediately.
175 if (II.isFilename()) {
176 CmdArgs.push_back(II.getFilename());
180 // Otherwise, this is a linker input argument.
181 const Arg &A = II.getInputArg();
183 // Handle reserved library options.
184 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
185 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
186 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) {
187 TC.AddCCKextLibArgs(Args, CmdArgs);
189 A.renderAsInput(Args, CmdArgs);
192 // LIBRARY_PATH - included following the user specified library paths.
193 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
196 /// \brief Determine whether Objective-C automated reference counting is
198 static bool isObjCAutoRefCount(const ArgList &Args) {
199 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
202 /// \brief Determine whether we are linking the ObjC runtime.
203 static bool isObjCRuntimeLinked(const ArgList &Args) {
204 if (isObjCAutoRefCount(Args)) {
205 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
208 return Args.hasArg(options::OPT_fobjc_link_runtime);
211 static void addProfileRT(const ToolChain &TC, const ArgList &Args,
212 ArgStringList &CmdArgs,
213 llvm::Triple Triple) {
214 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
215 Args.hasArg(options::OPT_fprofile_generate) ||
216 Args.hasArg(options::OPT_fcreate_profile) ||
217 Args.hasArg(options::OPT_coverage)))
220 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to
221 // the link line. We cannot do the same thing because unlike gcov there is a
222 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is
223 // not supported by old linkers.
224 std::string ProfileRT =
225 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a";
227 CmdArgs.push_back(Args.MakeArgString(ProfileRT));
230 static bool forwardToGCC(const Option &O) {
231 // Don't forward inputs from the original command line. They are added from
233 return O.getKind() != Option::InputClass &&
234 !O.hasFlag(options::DriverOption) &&
235 !O.hasFlag(options::LinkerInput);
238 void Clang::AddPreprocessingOptions(Compilation &C,
242 ArgStringList &CmdArgs,
243 const InputInfo &Output,
244 const InputInfoList &Inputs) const {
247 CheckPreprocessingOptions(D, Args);
249 Args.AddLastArg(CmdArgs, options::OPT_C);
250 Args.AddLastArg(CmdArgs, options::OPT_CC);
252 // Handle dependency file generation.
253 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) ||
254 (A = Args.getLastArg(options::OPT_MD)) ||
255 (A = Args.getLastArg(options::OPT_MMD))) {
256 // Determine the output location.
258 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
259 DepFile = MF->getValue();
260 C.addFailureResultFile(DepFile, &JA);
261 } else if (Output.getType() == types::TY_Dependencies) {
262 DepFile = Output.getFilename();
263 } else if (A->getOption().matches(options::OPT_M) ||
264 A->getOption().matches(options::OPT_MM)) {
267 DepFile = getDependencyFileName(Args, Inputs);
268 C.addFailureResultFile(DepFile, &JA);
270 CmdArgs.push_back("-dependency-file");
271 CmdArgs.push_back(DepFile);
273 // Add a default target if one wasn't specified.
274 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
275 const char *DepTarget;
277 // If user provided -o, that is the dependency target, except
278 // when we are only generating a dependency file.
279 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
280 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
281 DepTarget = OutputOpt->getValue();
283 // Otherwise derive from the base input.
285 // FIXME: This should use the computed output file location.
286 SmallString<128> P(Inputs[0].getBaseInput());
287 llvm::sys::path::replace_extension(P, "o");
288 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P));
291 CmdArgs.push_back("-MT");
292 SmallString<128> Quoted;
293 QuoteTarget(DepTarget, Quoted);
294 CmdArgs.push_back(Args.MakeArgString(Quoted));
297 if (A->getOption().matches(options::OPT_M) ||
298 A->getOption().matches(options::OPT_MD))
299 CmdArgs.push_back("-sys-header-deps");
302 if (Args.hasArg(options::OPT_MG)) {
303 if (!A || A->getOption().matches(options::OPT_MD) ||
304 A->getOption().matches(options::OPT_MMD))
305 D.Diag(diag::err_drv_mg_requires_m_or_mm);
306 CmdArgs.push_back("-MG");
309 Args.AddLastArg(CmdArgs, options::OPT_MP);
311 // Convert all -MQ <target> args to -MT <quoted target>
312 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
314 ie = Args.filtered_end(); it != ie; ++it) {
318 if (A->getOption().matches(options::OPT_MQ)) {
319 CmdArgs.push_back("-MT");
320 SmallString<128> Quoted;
321 QuoteTarget(A->getValue(), Quoted);
322 CmdArgs.push_back(Args.MakeArgString(Quoted));
324 // -MT flag - no change
326 A->render(Args, CmdArgs);
330 // Add -i* options, and automatically translate to
331 // -include-pch/-include-pth for transparent PCH support. It's
332 // wonky, but we include looking for .gch so we can support seamless
333 // replacement into a build system already set up to be generating
335 bool RenderedImplicitInclude = false;
336 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
337 ie = Args.filtered_end(); it != ie; ++it) {
340 if (A->getOption().matches(options::OPT_include)) {
341 bool IsFirstImplicitInclude = !RenderedImplicitInclude;
342 RenderedImplicitInclude = true;
344 // Use PCH if the user requested it.
345 bool UsePCH = D.CCCUsePCH;
347 bool FoundPTH = false;
348 bool FoundPCH = false;
349 SmallString<128> P(A->getValue());
350 // We want the files to have a name like foo.h.pch. Add a dummy extension
351 // so that replace_extension does the right thing.
354 llvm::sys::path::replace_extension(P, "pch");
355 if (llvm::sys::fs::exists(P.str()))
360 llvm::sys::path::replace_extension(P, "pth");
361 if (llvm::sys::fs::exists(P.str()))
365 if (!FoundPCH && !FoundPTH) {
366 llvm::sys::path::replace_extension(P, "gch");
367 if (llvm::sys::fs::exists(P.str())) {
373 if (FoundPCH || FoundPTH) {
374 if (IsFirstImplicitInclude) {
377 CmdArgs.push_back("-include-pch");
379 CmdArgs.push_back("-include-pth");
380 CmdArgs.push_back(Args.MakeArgString(P.str()));
383 // Ignore the PCH if not first on command line and emit warning.
384 D.Diag(diag::warn_drv_pch_not_first_include)
385 << P.str() << A->getAsString(Args);
390 // Not translated, render as usual.
392 A->render(Args, CmdArgs);
395 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
396 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F,
397 options::OPT_index_header_map);
399 // Add -Wp, and -Xassembler if using the preprocessor.
401 // FIXME: There is a very unfortunate problem here, some troubled
402 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
403 // really support that we would have to parse and then translate
405 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
406 options::OPT_Xpreprocessor);
408 // -I- is a deprecated GCC feature, reject it.
409 if (Arg *A = Args.getLastArg(options::OPT_I_))
410 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
412 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an
413 // -isysroot to the CC1 invocation.
414 StringRef sysroot = C.getSysRoot();
416 if (!Args.hasArg(options::OPT_isysroot)) {
417 CmdArgs.push_back("-isysroot");
418 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
422 // Parse additional include paths from environment variables.
423 // FIXME: We should probably sink the logic for handling these from the
424 // frontend into the driver. It will allow deleting 4 otherwise unused flags.
425 // CPATH - included following the user specified includes (but prior to
426 // builtin and standard includes).
427 addDirectoryList(Args, CmdArgs, "-I", "CPATH");
428 // C_INCLUDE_PATH - system includes enabled when compiling C.
429 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH");
430 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++.
431 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH");
432 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC.
433 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH");
434 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++.
435 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH");
437 // Add C++ include arguments, if needed.
438 if (types::isCXX(Inputs[0].getType()))
439 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
441 // Add system include arguments.
442 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
445 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
448 // FIXME: This is redundant with -mcpu, why does LLVM use this.
449 // FIXME: tblgen this, or kill it!
450 static const char *getLLVMArchSuffixForARM(StringRef CPU) {
451 return llvm::StringSwitch<const char *>(CPU)
452 .Case("strongarm", "v4")
453 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
454 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
455 .Cases("arm920", "arm920t", "arm922t", "v4t")
456 .Cases("arm940t", "ep9312","v4t")
457 .Cases("arm10tdmi", "arm1020t", "v5")
458 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
459 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
460 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
461 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
462 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
463 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
464 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
465 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "v7")
466 .Cases("cortex-r4", "cortex-r5", "v7r")
467 .Case("cortex-m0", "v6m")
468 .Case("cortex-m3", "v7m")
469 .Case("cortex-m4", "v7em")
470 .Case("cortex-a9-mp", "v7f")
471 .Case("swift", "v7s")
472 .Cases("cortex-a53", "cortex-a57", "v8")
476 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
478 // FIXME: tblgen this.
479 static std::string getARMTargetCPU(const ArgList &Args,
480 const llvm::Triple &Triple) {
481 // FIXME: Warn on inconsistent use of -mcpu and -march.
483 // If we have -mcpu=, use that.
484 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
485 StringRef MCPU = A->getValue();
486 // Handle -mcpu=native.
487 if (MCPU == "native")
488 return llvm::sys::getHostCPUName();
494 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
495 // Otherwise, if we have -march= choose the base CPU for that arch.
496 MArch = A->getValue();
498 // Otherwise, use the Arch from the triple.
499 MArch = Triple.getArchName();
502 if (Triple.getOS() == llvm::Triple::NetBSD ||
503 Triple.getOS() == llvm::Triple::FreeBSD) {
504 if (MArch == "armv6")
505 return "arm1176jzf-s";
508 // Handle -march=native.
509 std::string NativeMArch;
510 if (MArch == "native") {
511 std::string CPU = llvm::sys::getHostCPUName();
512 if (CPU != "generic") {
513 // Translate the native cpu into the architecture. The switch below will
514 // then chose the minimum cpu for that arch.
515 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU);
520 return llvm::StringSwitch<const char *>(MArch)
521 .Cases("armv2", "armv2a","arm2")
522 .Case("armv3", "arm6")
523 .Case("armv3m", "arm7m")
524 .Case("armv4", "strongarm")
525 .Case("armv4t", "arm7tdmi")
526 .Cases("armv5", "armv5t", "arm10tdmi")
527 .Cases("armv5e", "armv5te", "arm1022e")
528 .Case("armv5tej", "arm926ej-s")
529 .Cases("armv6", "armv6k", "arm1136jf-s")
530 .Case("armv6j", "arm1136j-s")
531 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
532 .Case("armv6t2", "arm1156t2-s")
533 .Cases("armv6m", "armv6-m", "cortex-m0")
534 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
535 .Cases("armv7em", "armv7e-m", "cortex-m4")
536 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
537 .Cases("armv7s", "armv7-s", "swift")
538 .Cases("armv7r", "armv7-r", "cortex-r4")
539 .Cases("armv7m", "armv7-m", "cortex-m3")
540 .Cases("armv8", "armv8a", "armv8-a", "cortex-a53")
541 .Case("ep9312", "ep9312")
542 .Case("iwmmxt", "iwmmxt")
543 .Case("xscale", "xscale")
544 // If all else failed, return the most base CPU with thumb interworking
545 // supported by LLVM.
546 .Default("arm7tdmi");
549 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are targeting.
551 // FIXME: tblgen this.
552 static std::string getAArch64TargetCPU(const ArgList &Args,
553 const llvm::Triple &Triple) {
554 // FIXME: Warn on inconsistent use of -mcpu and -march.
556 // If we have -mcpu=, use that.
557 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
558 StringRef MCPU = A->getValue();
559 // Handle -mcpu=native.
560 if (MCPU == "native")
561 return llvm::sys::getHostCPUName();
569 // FIXME: Move to target hook.
570 static bool isSignedCharDefault(const llvm::Triple &Triple) {
571 switch (Triple.getArch()) {
575 case llvm::Triple::aarch64:
576 case llvm::Triple::arm:
577 case llvm::Triple::ppc:
578 case llvm::Triple::ppc64:
579 if (Triple.isOSDarwin())
583 case llvm::Triple::ppc64le:
584 case llvm::Triple::systemz:
585 case llvm::Triple::xcore:
590 static bool isNoCommonDefault(const llvm::Triple &Triple) {
591 switch (Triple.getArch()) {
595 case llvm::Triple::xcore:
602 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
604 static void getAArch64FPUFeatures(const Driver &D, const Arg *A,
606 std::vector<const char *> &Features) {
607 StringRef FPU = A->getValue();
608 if (FPU == "fp-armv8") {
609 Features.push_back("+fp-armv8");
610 } else if (FPU == "neon-fp-armv8") {
611 Features.push_back("+fp-armv8");
612 Features.push_back("+neon");
613 } else if (FPU == "crypto-neon-fp-armv8") {
614 Features.push_back("+fp-armv8");
615 Features.push_back("+neon");
616 Features.push_back("+crypto");
617 } else if (FPU == "neon") {
618 Features.push_back("+neon");
619 } else if (FPU == "none") {
620 Features.push_back("-fp-armv8");
621 Features.push_back("-crypto");
622 Features.push_back("-neon");
624 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
628 static void getARMHWDivFeatures(const Driver &D, const Arg *A,
630 std::vector<const char *> &Features) {
631 StringRef HWDiv = A->getValue();
632 if (HWDiv == "arm") {
633 Features.push_back("+hwdiv-arm");
634 Features.push_back("-hwdiv");
635 } else if (HWDiv == "thumb") {
636 Features.push_back("-hwdiv-arm");
637 Features.push_back("+hwdiv");
638 } else if (HWDiv == "arm,thumb" || HWDiv == "thumb,arm") {
639 Features.push_back("+hwdiv-arm");
640 Features.push_back("+hwdiv");
641 } else if (HWDiv == "none") {
642 Features.push_back("-hwdiv-arm");
643 Features.push_back("-hwdiv");
645 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
650 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
652 static void getARMFPUFeatures(const Driver &D, const Arg *A,
654 std::vector<const char *> &Features) {
655 StringRef FPU = A->getValue();
657 // Set the target features based on the FPU.
658 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
659 // Disable any default FPU support.
660 Features.push_back("-vfp2");
661 Features.push_back("-vfp3");
662 Features.push_back("-neon");
663 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") {
664 Features.push_back("+vfp3");
665 Features.push_back("+d16");
666 Features.push_back("-neon");
667 } else if (FPU == "vfp") {
668 Features.push_back("+vfp2");
669 Features.push_back("-neon");
670 } else if (FPU == "vfp3" || FPU == "vfpv3") {
671 Features.push_back("+vfp3");
672 Features.push_back("-neon");
673 } else if (FPU == "fp-armv8") {
674 Features.push_back("+fp-armv8");
675 Features.push_back("-neon");
676 Features.push_back("-crypto");
677 } else if (FPU == "neon-fp-armv8") {
678 Features.push_back("+fp-armv8");
679 Features.push_back("+neon");
680 Features.push_back("-crypto");
681 } else if (FPU == "crypto-neon-fp-armv8") {
682 Features.push_back("+fp-armv8");
683 Features.push_back("+neon");
684 Features.push_back("+crypto");
685 } else if (FPU == "neon") {
686 Features.push_back("+neon");
687 } else if (FPU == "none") {
688 Features.push_back("-vfp2");
689 Features.push_back("-vfp3");
690 Features.push_back("-vfp4");
691 Features.push_back("-fp-armv8");
692 Features.push_back("-crypto");
693 Features.push_back("-neon");
695 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
698 // Select the float ABI as determined by -msoft-float, -mhard-float, and
700 static StringRef getARMFloatABI(const Driver &D,
702 const llvm::Triple &Triple) {
704 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
705 options::OPT_mhard_float,
706 options::OPT_mfloat_abi_EQ)) {
707 if (A->getOption().matches(options::OPT_msoft_float))
709 else if (A->getOption().matches(options::OPT_mhard_float))
712 FloatABI = A->getValue();
713 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
714 D.Diag(diag::err_drv_invalid_mfloat_abi)
715 << A->getAsString(Args);
721 // If unspecified, choose the default based on the platform.
722 if (FloatABI.empty()) {
723 switch (Triple.getOS()) {
724 case llvm::Triple::Darwin:
725 case llvm::Triple::MacOSX:
726 case llvm::Triple::IOS: {
727 // Darwin defaults to "softfp" for v6 and v7.
729 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
730 std::string ArchName =
731 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
732 if (StringRef(ArchName).startswith("v6") ||
733 StringRef(ArchName).startswith("v7"))
740 case llvm::Triple::FreeBSD:
741 // FreeBSD defaults to soft float
746 switch(Triple.getEnvironment()) {
747 case llvm::Triple::GNUEABIHF:
750 case llvm::Triple::GNUEABI:
753 case llvm::Triple::EABI:
754 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
757 case llvm::Triple::Android: {
758 std::string ArchName =
759 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
760 if (StringRef(ArchName).startswith("v7"))
767 // Assume "soft", but warn the user we are guessing.
769 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
778 static void getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple,
780 std::vector<const char *> &Features) {
781 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
782 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
783 // yet (it uses the -mfloat-abi and -msoft-float options), and it is
784 // stripped out by the ARM target.
785 // Use software floating point operations?
786 if (FloatABI == "soft")
787 Features.push_back("+soft-float");
789 // Use software floating point argument passing?
790 if (FloatABI != "hard")
791 Features.push_back("+soft-float-abi");
794 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
795 getARMFPUFeatures(D, A, Args, Features);
796 if (const Arg *A = Args.getLastArg(options::OPT_mhwdiv_EQ))
797 getARMHWDivFeatures(D, A, Args, Features);
799 // Setting -msoft-float effectively disables NEON because of the GCC
800 // implementation, although the same isn't true of VFP or VFP3.
801 if (FloatABI == "soft")
802 Features.push_back("-neon");
805 if (Arg *A = Args.getLastArg(options::OPT_mcrc,
806 options::OPT_mnocrc)) {
807 if (A->getOption().matches(options::OPT_mcrc))
808 Features.push_back("+crc");
810 Features.push_back("-crc");
814 void Clang::AddARMTargetArgs(const ArgList &Args,
815 ArgStringList &CmdArgs,
816 bool KernelOrKext) const {
817 const Driver &D = getToolChain().getDriver();
818 // Get the effective triple, which takes into account the deployment target.
819 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
820 llvm::Triple Triple(TripleStr);
821 std::string CPUName = getARMTargetCPU(Args, Triple);
823 // Select the ABI to use.
825 // FIXME: Support -meabi.
826 const char *ABIName = 0;
827 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
828 ABIName = A->getValue();
829 } else if (Triple.isOSDarwin()) {
830 // The backend is hardwired to assume AAPCS for M-class processors, ensure
831 // the frontend matches that.
832 if (Triple.getEnvironment() == llvm::Triple::EABI ||
833 StringRef(CPUName).startswith("cortex-m")) {
836 ABIName = "apcs-gnu";
839 // Select the default based on the platform.
840 switch(Triple.getEnvironment()) {
841 case llvm::Triple::Android:
842 case llvm::Triple::GNUEABI:
843 case llvm::Triple::GNUEABIHF:
844 ABIName = "aapcs-linux";
846 case llvm::Triple::EABI:
850 ABIName = "apcs-gnu";
853 CmdArgs.push_back("-target-abi");
854 CmdArgs.push_back(ABIName);
856 // Determine floating point ABI from the options & target defaults.
857 StringRef FloatABI = getARMFloatABI(D, Args, Triple);
858 if (FloatABI == "soft") {
859 // Floating point operations and argument passing are soft.
861 // FIXME: This changes CPP defines, we need -target-soft-float.
862 CmdArgs.push_back("-msoft-float");
863 CmdArgs.push_back("-mfloat-abi");
864 CmdArgs.push_back("soft");
865 } else if (FloatABI == "softfp") {
866 // Floating point operations are hard, but argument passing is soft.
867 CmdArgs.push_back("-mfloat-abi");
868 CmdArgs.push_back("soft");
870 // Floating point operations and argument passing are hard.
871 assert(FloatABI == "hard" && "Invalid float abi!");
872 CmdArgs.push_back("-mfloat-abi");
873 CmdArgs.push_back("hard");
876 // Kernel code has more strict alignment requirements.
878 if (!Triple.isiOS() || Triple.isOSVersionLT(6)) {
879 CmdArgs.push_back("-backend-option");
880 CmdArgs.push_back("-arm-long-calls");
883 CmdArgs.push_back("-backend-option");
884 CmdArgs.push_back("-arm-strict-align");
886 // The kext linker doesn't know how to deal with movw/movt.
887 CmdArgs.push_back("-backend-option");
888 CmdArgs.push_back("-arm-use-movt=0");
891 // Setting -mno-global-merge disables the codegen global merge pass. Setting
892 // -mglobal-merge has no effect as the pass is enabled by default.
893 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
894 options::OPT_mno_global_merge)) {
895 if (A->getOption().matches(options::OPT_mno_global_merge))
896 CmdArgs.push_back("-mno-global-merge");
899 if (!Args.hasFlag(options::OPT_mimplicit_float,
900 options::OPT_mno_implicit_float,
902 CmdArgs.push_back("-no-implicit-float");
904 // llvm does not support reserving registers in general. There is support
905 // for reserving r9 on ARM though (defined as a platform-specific register
907 if (Args.hasArg(options::OPT_ffixed_r9)) {
908 CmdArgs.push_back("-backend-option");
909 CmdArgs.push_back("-arm-reserve-r9");
913 // Get CPU and ABI names. They are not independent
914 // so we have to calculate them together.
915 static void getMipsCPUAndABI(const ArgList &Args,
916 const llvm::Triple &Triple,
918 StringRef &ABIName) {
919 const char *DefMips32CPU = "mips32";
920 const char *DefMips64CPU = "mips64";
922 if (Arg *A = Args.getLastArg(options::OPT_march_EQ,
923 options::OPT_mcpu_EQ))
924 CPUName = A->getValue();
926 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
927 ABIName = A->getValue();
928 // Convert a GNU style Mips ABI name to the name
929 // accepted by LLVM Mips backend.
930 ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName)
936 // Setup default CPU and ABI names.
937 if (CPUName.empty() && ABIName.empty()) {
938 switch (Triple.getArch()) {
940 llvm_unreachable("Unexpected triple arch name");
941 case llvm::Triple::mips:
942 case llvm::Triple::mipsel:
943 CPUName = DefMips32CPU;
945 case llvm::Triple::mips64:
946 case llvm::Triple::mips64el:
947 CPUName = DefMips64CPU;
952 if (!ABIName.empty()) {
953 // Deduce CPU name from ABI name.
954 CPUName = llvm::StringSwitch<const char *>(ABIName)
955 .Cases("32", "o32", "eabi", DefMips32CPU)
956 .Cases("n32", "n64", "64", DefMips64CPU)
959 else if (!CPUName.empty()) {
960 // Deduce ABI name from CPU name.
961 ABIName = llvm::StringSwitch<const char *>(CPUName)
962 .Cases("mips32", "mips32r2", "o32")
963 .Cases("mips64", "mips64r2", "n64")
967 // FIXME: Warn on inconsistent cpu and abi usage.
970 // Convert ABI name to the GNU tools acceptable variant.
971 static StringRef getGnuCompatibleMipsABIName(StringRef ABI) {
972 return llvm::StringSwitch<llvm::StringRef>(ABI)
978 // Select the MIPS float ABI as determined by -msoft-float, -mhard-float,
980 static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) {
982 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
983 options::OPT_mhard_float,
984 options::OPT_mfloat_abi_EQ)) {
985 if (A->getOption().matches(options::OPT_msoft_float))
987 else if (A->getOption().matches(options::OPT_mhard_float))
990 FloatABI = A->getValue();
991 if (FloatABI != "soft" && FloatABI != "hard") {
992 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
998 // If unspecified, choose the default based on the platform.
999 if (FloatABI.empty()) {
1000 // Assume "hard", because it's a default value used by gcc.
1001 // When we start to recognize specific target MIPS processors,
1002 // we will be able to select the default more correctly.
1009 static void AddTargetFeature(const ArgList &Args,
1010 std::vector<const char *> &Features,
1011 OptSpecifier OnOpt, OptSpecifier OffOpt,
1012 StringRef FeatureName) {
1013 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) {
1014 if (A->getOption().matches(OnOpt))
1015 Features.push_back(Args.MakeArgString("+" + FeatureName));
1017 Features.push_back(Args.MakeArgString("-" + FeatureName));
1021 static void getMIPSTargetFeatures(const Driver &D, const ArgList &Args,
1022 std::vector<const char *> &Features) {
1023 StringRef FloatABI = getMipsFloatABI(D, Args);
1024 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL;
1025 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) {
1026 // FIXME: Note, this is a hack. We need to pass the selected float
1027 // mode to the MipsTargetInfoBase to define appropriate macros there.
1028 // Now it is the only method.
1029 Features.push_back("+soft-float");
1032 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
1033 if (StringRef(A->getValue()) == "2008")
1034 Features.push_back("+nan2008");
1037 AddTargetFeature(Args, Features, options::OPT_msingle_float,
1038 options::OPT_mdouble_float, "single-float");
1039 AddTargetFeature(Args, Features, options::OPT_mips16, options::OPT_mno_mips16,
1041 AddTargetFeature(Args, Features, options::OPT_mmicromips,
1042 options::OPT_mno_micromips, "micromips");
1043 AddTargetFeature(Args, Features, options::OPT_mdsp, options::OPT_mno_dsp,
1045 AddTargetFeature(Args, Features, options::OPT_mdspr2, options::OPT_mno_dspr2,
1047 AddTargetFeature(Args, Features, options::OPT_mmsa, options::OPT_mno_msa,
1049 AddTargetFeature(Args, Features, options::OPT_mfp64, options::OPT_mfp32,
1053 void Clang::AddMIPSTargetArgs(const ArgList &Args,
1054 ArgStringList &CmdArgs) const {
1055 const Driver &D = getToolChain().getDriver();
1058 const llvm::Triple &Triple = getToolChain().getTriple();
1059 getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1061 CmdArgs.push_back("-target-abi");
1062 CmdArgs.push_back(ABIName.data());
1064 StringRef FloatABI = getMipsFloatABI(D, Args);
1066 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL;
1068 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) {
1069 // Floating point operations and argument passing are soft.
1070 CmdArgs.push_back("-msoft-float");
1071 CmdArgs.push_back("-mfloat-abi");
1072 CmdArgs.push_back("soft");
1074 if (FloatABI == "hard" && IsMips16) {
1075 CmdArgs.push_back("-mllvm");
1076 CmdArgs.push_back("-mips16-hard-float");
1080 // Floating point operations and argument passing are hard.
1081 assert(FloatABI == "hard" && "Invalid float abi!");
1082 CmdArgs.push_back("-mfloat-abi");
1083 CmdArgs.push_back("hard");
1086 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) {
1087 if (A->getOption().matches(options::OPT_mxgot)) {
1088 CmdArgs.push_back("-mllvm");
1089 CmdArgs.push_back("-mxgot");
1093 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1,
1094 options::OPT_mno_ldc1_sdc1)) {
1095 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) {
1096 CmdArgs.push_back("-mllvm");
1097 CmdArgs.push_back("-mno-ldc1-sdc1");
1101 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division,
1102 options::OPT_mno_check_zero_division)) {
1103 if (A->getOption().matches(options::OPT_mno_check_zero_division)) {
1104 CmdArgs.push_back("-mllvm");
1105 CmdArgs.push_back("-mno-check-zero-division");
1109 if (Arg *A = Args.getLastArg(options::OPT_G)) {
1110 StringRef v = A->getValue();
1111 CmdArgs.push_back("-mllvm");
1112 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v));
1117 /// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting.
1118 static std::string getPPCTargetCPU(const ArgList &Args) {
1119 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1120 StringRef CPUName = A->getValue();
1122 if (CPUName == "native") {
1123 std::string CPU = llvm::sys::getHostCPUName();
1124 if (!CPU.empty() && CPU != "generic")
1130 return llvm::StringSwitch<const char *>(CPUName)
1131 .Case("common", "generic")
1133 .Case("440fp", "440")
1138 .Case("603e", "603e")
1139 .Case("603ev", "603ev")
1141 .Case("604e", "604e")
1143 .Case("630", "pwr3")
1145 .Case("7400", "7400")
1147 .Case("7450", "7450")
1154 .Case("e500mc", "e500mc")
1155 .Case("e5500", "e5500")
1156 .Case("power3", "pwr3")
1157 .Case("power4", "pwr4")
1158 .Case("power5", "pwr5")
1159 .Case("power5x", "pwr5x")
1160 .Case("power6", "pwr6")
1161 .Case("power6x", "pwr6x")
1162 .Case("power7", "pwr7")
1163 .Case("pwr3", "pwr3")
1164 .Case("pwr4", "pwr4")
1165 .Case("pwr5", "pwr5")
1166 .Case("pwr5x", "pwr5x")
1167 .Case("pwr6", "pwr6")
1168 .Case("pwr6x", "pwr6x")
1169 .Case("pwr7", "pwr7")
1170 .Case("powerpc", "ppc")
1171 .Case("powerpc64", "ppc64")
1172 .Case("powerpc64le", "ppc64le")
1179 static void getPPCTargetFeatures(const ArgList &Args,
1180 std::vector<const char *> &Features) {
1181 for (arg_iterator it = Args.filtered_begin(options::OPT_m_ppc_Features_Group),
1182 ie = Args.filtered_end();
1184 StringRef Name = (*it)->getOption().getName();
1188 assert(Name.startswith("m") && "Invalid feature name.");
1189 Name = Name.substr(1);
1191 bool IsNegative = Name.startswith("no-");
1193 Name = Name.substr(3);
1195 // Note that gcc calls this mfcrf and LLVM calls this mfocrf so we
1196 // pass the correct option to the backend while calling the frontend
1198 // TODO: Change the LLVM backend option maybe?
1199 if (Name == "mfcrf")
1202 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1205 // Altivec is a bit weird, allow overriding of the Altivec feature here.
1206 AddTargetFeature(Args, Features, options::OPT_faltivec,
1207 options::OPT_fno_altivec, "altivec");
1210 /// Get the (LLVM) name of the R600 gpu we are targeting.
1211 static std::string getR600TargetGPU(const ArgList &Args) {
1212 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
1213 const char *GPUName = A->getValue();
1214 return llvm::StringSwitch<const char *>(GPUName)
1215 .Cases("rv630", "rv635", "r600")
1216 .Cases("rv610", "rv620", "rs780", "rs880")
1217 .Case("rv740", "rv770")
1218 .Case("palm", "cedar")
1219 .Cases("sumo", "sumo2", "sumo")
1220 .Case("hemlock", "cypress")
1221 .Case("aruba", "cayman")
1227 static void getSparcTargetFeatures(const ArgList &Args,
1228 std::vector<const char *> Features) {
1229 bool SoftFloatABI = true;
1231 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) {
1232 if (A->getOption().matches(options::OPT_mhard_float))
1233 SoftFloatABI = false;
1236 Features.push_back("+soft-float");
1239 void Clang::AddSparcTargetArgs(const ArgList &Args,
1240 ArgStringList &CmdArgs) const {
1241 const Driver &D = getToolChain().getDriver();
1243 // Select the float ABI as determined by -msoft-float, -mhard-float, and
1245 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1246 options::OPT_mhard_float)) {
1247 if (A->getOption().matches(options::OPT_msoft_float))
1249 else if (A->getOption().matches(options::OPT_mhard_float))
1253 // If unspecified, choose the default based on the platform.
1254 if (FloatABI.empty()) {
1255 // Assume "soft", but warn the user we are guessing.
1257 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
1260 if (FloatABI == "soft") {
1261 // Floating point operations and argument passing are soft.
1263 // FIXME: This changes CPP defines, we need -target-soft-float.
1264 CmdArgs.push_back("-msoft-float");
1266 assert(FloatABI == "hard" && "Invalid float abi!");
1267 CmdArgs.push_back("-mhard-float");
1271 static const char *getSystemZTargetCPU(const ArgList &Args) {
1272 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
1273 return A->getValue();
1277 static const char *getX86TargetCPU(const ArgList &Args,
1278 const llvm::Triple &Triple) {
1279 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
1280 if (StringRef(A->getValue()) != "native") {
1281 if (Triple.isOSDarwin() && Triple.getArchName() == "x86_64h")
1284 return A->getValue();
1287 // FIXME: Reject attempts to use -march=native unless the target matches
1290 // FIXME: We should also incorporate the detected target features for use
1292 std::string CPU = llvm::sys::getHostCPUName();
1293 if (!CPU.empty() && CPU != "generic")
1294 return Args.MakeArgString(CPU);
1297 // Select the default CPU if none was given (or detection failed).
1299 if (Triple.getArch() != llvm::Triple::x86_64 &&
1300 Triple.getArch() != llvm::Triple::x86)
1301 return 0; // This routine is only handling x86 targets.
1303 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
1305 // FIXME: Need target hooks.
1306 if (Triple.isOSDarwin()) {
1307 if (Triple.getArchName() == "x86_64h")
1309 return Is64Bit ? "core2" : "yonah";
1312 // All x86 devices running Android have core2 as their common
1313 // denominator. This makes a better choice than pentium4.
1314 if (Triple.getEnvironment() == llvm::Triple::Android)
1317 // Everything else goes to x86-64 in 64-bit mode.
1321 switch (Triple.getOS()) {
1322 case llvm::Triple::FreeBSD:
1323 case llvm::Triple::NetBSD:
1324 case llvm::Triple::OpenBSD:
1326 case llvm::Triple::Haiku:
1328 case llvm::Triple::Bitrig:
1336 static std::string getCPUName(const ArgList &Args, const llvm::Triple &T) {
1337 switch(T.getArch()) {
1341 case llvm::Triple::aarch64:
1342 return getAArch64TargetCPU(Args, T);
1344 case llvm::Triple::arm:
1345 case llvm::Triple::thumb:
1346 return getARMTargetCPU(Args, T);
1348 case llvm::Triple::mips:
1349 case llvm::Triple::mipsel:
1350 case llvm::Triple::mips64:
1351 case llvm::Triple::mips64el: {
1354 getMipsCPUAndABI(Args, T, CPUName, ABIName);
1358 case llvm::Triple::ppc:
1359 case llvm::Triple::ppc64:
1360 case llvm::Triple::ppc64le: {
1361 std::string TargetCPUName = getPPCTargetCPU(Args);
1362 // LLVM may default to generating code for the native CPU,
1363 // but, like gcc, we default to a more generic option for
1364 // each architecture. (except on Darwin)
1365 if (TargetCPUName.empty() && !T.isOSDarwin()) {
1366 if (T.getArch() == llvm::Triple::ppc64)
1367 TargetCPUName = "ppc64";
1368 else if (T.getArch() == llvm::Triple::ppc64le)
1369 TargetCPUName = "ppc64le";
1371 TargetCPUName = "ppc";
1373 return TargetCPUName;
1376 case llvm::Triple::sparc:
1377 case llvm::Triple::sparcv9:
1378 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
1379 return A->getValue();
1382 case llvm::Triple::x86:
1383 case llvm::Triple::x86_64:
1384 return getX86TargetCPU(Args, T);
1386 case llvm::Triple::hexagon:
1387 return "hexagon" + toolchains::Hexagon_TC::GetTargetCPU(Args).str();
1389 case llvm::Triple::systemz:
1390 return getSystemZTargetCPU(Args);
1392 case llvm::Triple::r600:
1393 return getR600TargetGPU(Args);
1397 static void getX86TargetFeatures(const llvm::Triple &Triple,
1398 const ArgList &Args,
1399 std::vector<const char *> &Features) {
1400 if (Triple.getArchName() == "x86_64h") {
1401 // x86_64h implies quite a few of the more modern subtarget features
1402 // for Haswell class CPUs, but not all of them. Opt-out of a few.
1403 Features.push_back("-rdrnd");
1404 Features.push_back("-aes");
1405 Features.push_back("-pclmul");
1406 Features.push_back("-rtm");
1407 Features.push_back("-hle");
1408 Features.push_back("-fsgsbase");
1411 // Now add any that the user explicitly requested on the command line,
1412 // which may override the defaults.
1413 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
1414 ie = Args.filtered_end();
1416 StringRef Name = (*it)->getOption().getName();
1420 assert(Name.startswith("m") && "Invalid feature name.");
1421 Name = Name.substr(1);
1423 bool IsNegative = Name.startswith("no-");
1425 Name = Name.substr(3);
1427 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
1431 void Clang::AddX86TargetArgs(const ArgList &Args,
1432 ArgStringList &CmdArgs) const {
1433 if (!Args.hasFlag(options::OPT_mred_zone,
1434 options::OPT_mno_red_zone,
1436 Args.hasArg(options::OPT_mkernel) ||
1437 Args.hasArg(options::OPT_fapple_kext))
1438 CmdArgs.push_back("-disable-red-zone");
1440 // Default to avoid implicit floating-point for kernel/kext code, but allow
1441 // that to be overridden with -mno-soft-float.
1442 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
1443 Args.hasArg(options::OPT_fapple_kext));
1444 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
1445 options::OPT_mno_soft_float,
1446 options::OPT_mimplicit_float,
1447 options::OPT_mno_implicit_float)) {
1448 const Option &O = A->getOption();
1449 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) ||
1450 O.matches(options::OPT_msoft_float));
1452 if (NoImplicitFloat)
1453 CmdArgs.push_back("-no-implicit-float");
1456 static inline bool HasPICArg(const ArgList &Args) {
1457 return Args.hasArg(options::OPT_fPIC)
1458 || Args.hasArg(options::OPT_fpic);
1461 static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) {
1462 return Args.getLastArg(options::OPT_G,
1464 options::OPT_msmall_data_threshold_EQ);
1467 static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) {
1469 if (HasPICArg(Args))
1471 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) {
1472 value = A->getValue();
1478 void Clang::AddHexagonTargetArgs(const ArgList &Args,
1479 ArgStringList &CmdArgs) const {
1480 CmdArgs.push_back("-fno-signed-char");
1481 CmdArgs.push_back("-mqdsp6-compat");
1482 CmdArgs.push_back("-Wreturn-type");
1484 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
1485 if (!SmallDataThreshold.empty()) {
1486 CmdArgs.push_back ("-mllvm");
1487 CmdArgs.push_back(Args.MakeArgString(
1488 "-hexagon-small-data-threshold=" + SmallDataThreshold));
1491 if (!Args.hasArg(options::OPT_fno_short_enums))
1492 CmdArgs.push_back("-fshort-enums");
1493 if (Args.getLastArg(options::OPT_mieee_rnd_near)) {
1494 CmdArgs.push_back ("-mllvm");
1495 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near");
1497 CmdArgs.push_back ("-mllvm");
1498 CmdArgs.push_back ("-machine-sink-split=0");
1501 static void getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
1502 std::vector<const char *> &Features) {
1504 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ))
1505 getAArch64FPUFeatures(D, A, Args, Features);
1508 static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
1509 const ArgList &Args, ArgStringList &CmdArgs) {
1510 std::vector<const char *> Features;
1511 switch (Triple.getArch()) {
1514 case llvm::Triple::mips:
1515 case llvm::Triple::mipsel:
1516 case llvm::Triple::mips64:
1517 case llvm::Triple::mips64el:
1518 getMIPSTargetFeatures(D, Args, Features);
1521 case llvm::Triple::arm:
1522 case llvm::Triple::thumb:
1523 getARMTargetFeatures(D, Triple, Args, Features);
1526 case llvm::Triple::ppc:
1527 case llvm::Triple::ppc64:
1528 case llvm::Triple::ppc64le:
1529 getPPCTargetFeatures(Args, Features);
1531 case llvm::Triple::sparc:
1532 getSparcTargetFeatures(Args, Features);
1534 case llvm::Triple::aarch64:
1535 getAArch64TargetFeatures(D, Args, Features);
1537 case llvm::Triple::x86:
1538 case llvm::Triple::x86_64:
1539 getX86TargetFeatures(Triple, Args, Features);
1543 // Find the last of each feature.
1544 llvm::StringMap<unsigned> LastOpt;
1545 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1546 const char *Name = Features[I];
1547 assert(Name[0] == '-' || Name[0] == '+');
1548 LastOpt[Name + 1] = I;
1551 for (unsigned I = 0, N = Features.size(); I < N; ++I) {
1552 // If this feature was overridden, ignore it.
1553 const char *Name = Features[I];
1554 llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name + 1);
1555 assert(LastI != LastOpt.end());
1556 unsigned Last = LastI->second;
1560 CmdArgs.push_back("-target-feature");
1561 CmdArgs.push_back(Name);
1566 shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime,
1567 const llvm::Triple &Triple) {
1568 // We use the zero-cost exception tables for Objective-C if the non-fragile
1569 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and
1571 if (runtime.isNonFragile())
1574 if (!Triple.isOSDarwin())
1577 return (!Triple.isMacOSXVersionLT(10,5) &&
1578 (Triple.getArch() == llvm::Triple::x86_64 ||
1579 Triple.getArch() == llvm::Triple::arm));
1582 /// addExceptionArgs - Adds exception related arguments to the driver command
1583 /// arguments. There's a master flag, -fexceptions and also language specific
1584 /// flags to enable/disable C++ and Objective-C exceptions.
1585 /// This makes it possible to for example disable C++ exceptions but enable
1586 /// Objective-C exceptions.
1587 static void addExceptionArgs(const ArgList &Args, types::ID InputType,
1588 const llvm::Triple &Triple,
1590 const ObjCRuntime &objcRuntime,
1591 ArgStringList &CmdArgs) {
1593 // -mkernel and -fapple-kext imply no exceptions, so claim exception related
1594 // arguments now to avoid warnings about unused arguments.
1595 Args.ClaimAllArgs(options::OPT_fexceptions);
1596 Args.ClaimAllArgs(options::OPT_fno_exceptions);
1597 Args.ClaimAllArgs(options::OPT_fobjc_exceptions);
1598 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions);
1599 Args.ClaimAllArgs(options::OPT_fcxx_exceptions);
1600 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions);
1604 // Exceptions are enabled by default.
1605 bool ExceptionsEnabled = true;
1607 // This keeps track of whether exceptions were explicitly turned on or off.
1608 bool DidHaveExplicitExceptionFlag = false;
1610 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
1611 options::OPT_fno_exceptions)) {
1612 if (A->getOption().matches(options::OPT_fexceptions))
1613 ExceptionsEnabled = true;
1615 ExceptionsEnabled = false;
1617 DidHaveExplicitExceptionFlag = true;
1620 bool ShouldUseExceptionTables = false;
1622 // Exception tables and cleanups can be enabled with -fexceptions even if the
1623 // language itself doesn't support exceptions.
1624 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
1625 ShouldUseExceptionTables = true;
1627 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This
1628 // is not necessarily sensible, but follows GCC.
1629 if (types::isObjC(InputType) &&
1630 Args.hasFlag(options::OPT_fobjc_exceptions,
1631 options::OPT_fno_objc_exceptions,
1633 CmdArgs.push_back("-fobjc-exceptions");
1635 ShouldUseExceptionTables |=
1636 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple);
1639 if (types::isCXX(InputType)) {
1640 bool CXXExceptionsEnabled = ExceptionsEnabled;
1642 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions,
1643 options::OPT_fno_cxx_exceptions,
1644 options::OPT_fexceptions,
1645 options::OPT_fno_exceptions)) {
1646 if (A->getOption().matches(options::OPT_fcxx_exceptions))
1647 CXXExceptionsEnabled = true;
1648 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions))
1649 CXXExceptionsEnabled = false;
1652 if (CXXExceptionsEnabled) {
1653 CmdArgs.push_back("-fcxx-exceptions");
1655 ShouldUseExceptionTables = true;
1659 if (ShouldUseExceptionTables)
1660 CmdArgs.push_back("-fexceptions");
1663 static bool ShouldDisableAutolink(const ArgList &Args,
1664 const ToolChain &TC) {
1665 bool Default = true;
1666 if (TC.getTriple().isOSDarwin()) {
1667 // The native darwin assembler doesn't support the linker_option directives,
1668 // so we disable them if we think the .s file will be passed to it.
1669 Default = TC.useIntegratedAs();
1671 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
1675 static bool ShouldDisableCFI(const ArgList &Args,
1676 const ToolChain &TC) {
1677 bool Default = true;
1678 if (TC.getTriple().isOSDarwin()) {
1679 // The native darwin assembler doesn't support cfi directives, so
1680 // we disable them if we think the .s file will be passed to it.
1681 Default = TC.useIntegratedAs();
1683 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm,
1684 options::OPT_fno_dwarf2_cfi_asm,
1688 static bool ShouldDisableDwarfDirectory(const ArgList &Args,
1689 const ToolChain &TC) {
1690 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm,
1691 options::OPT_fno_dwarf_directory_asm,
1692 TC.useIntegratedAs());
1693 return !UseDwarfDirectory;
1696 /// \brief Check whether the given input tree contains any compilation actions.
1697 static bool ContainsCompileAction(const Action *A) {
1698 if (isa<CompileJobAction>(A))
1701 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it)
1702 if (ContainsCompileAction(*it))
1708 /// \brief Check if -relax-all should be passed to the internal assembler.
1709 /// This is done by default when compiling non-assembler source with -O0.
1710 static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
1711 bool RelaxDefault = true;
1713 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1714 RelaxDefault = A->getOption().matches(options::OPT_O0);
1717 RelaxDefault = false;
1718 for (ActionList::const_iterator it = C.getActions().begin(),
1719 ie = C.getActions().end(); it != ie; ++it) {
1720 if (ContainsCompileAction(*it)) {
1721 RelaxDefault = true;
1727 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
1731 static void CollectArgsForIntegratedAssembler(Compilation &C,
1732 const ArgList &Args,
1733 ArgStringList &CmdArgs,
1735 if (UseRelaxAll(C, Args))
1736 CmdArgs.push_back("-mrelax-all");
1738 // When passing -I arguments to the assembler we sometimes need to
1739 // unconditionally take the next argument. For example, when parsing
1740 // '-Wa,-I -Wa,foo' we need to accept the -Wa,foo arg after seeing the
1741 // -Wa,-I arg and when parsing '-Wa,-I,foo' we need to accept the 'foo'
1742 // arg after parsing the '-I' arg.
1743 bool TakeNextArg = false;
1745 // When using an integrated assembler, translate -Wa, and -Xassembler
1747 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA,
1748 options::OPT_Xassembler),
1749 ie = Args.filtered_end(); it != ie; ++it) {
1753 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
1754 StringRef Value = A->getValue(i);
1756 CmdArgs.push_back(Value.data());
1757 TakeNextArg = false;
1761 if (Value == "-force_cpusubtype_ALL") {
1762 // Do nothing, this is the default and we don't support anything else.
1763 } else if (Value == "-L") {
1764 CmdArgs.push_back("-msave-temp-labels");
1765 } else if (Value == "--fatal-warnings") {
1766 CmdArgs.push_back("-mllvm");
1767 CmdArgs.push_back("-fatal-assembler-warnings");
1768 } else if (Value == "--noexecstack") {
1769 CmdArgs.push_back("-mnoexecstack");
1770 } else if (Value.startswith("-I")) {
1771 CmdArgs.push_back(Value.data());
1772 // We need to consume the next argument if the current arg is a plain
1773 // -I. The next arg will be the include directory.
1777 D.Diag(diag::err_drv_unsupported_option_argument)
1778 << A->getOption().getName() << Value;
1784 static void addProfileRTLinux(
1785 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) {
1786 if (!(Args.hasArg(options::OPT_fprofile_arcs) ||
1787 Args.hasArg(options::OPT_fprofile_generate) ||
1788 Args.hasArg(options::OPT_fcreate_profile) ||
1789 Args.hasArg(options::OPT_coverage)))
1792 // The profile runtime is located in the Linux library directory and has name
1793 // "libclang_rt.profile-<ArchName>.a".
1794 SmallString<128> LibProfile(TC.getDriver().ResourceDir);
1795 llvm::sys::path::append(
1796 LibProfile, "lib", "linux",
1797 Twine("libclang_rt.profile-") + TC.getArchName() + ".a");
1799 CmdArgs.push_back(Args.MakeArgString(LibProfile));
1802 static void addSanitizerRTLinkFlagsLinux(
1803 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs,
1804 const StringRef Sanitizer, bool BeforeLibStdCXX,
1805 bool ExportSymbols = true) {
1806 // Sanitizer runtime is located in the Linux library directory and
1807 // has name "libclang_rt.<Sanitizer>-<ArchName>.a".
1808 SmallString<128> LibSanitizer(TC.getDriver().ResourceDir);
1809 llvm::sys::path::append(
1810 LibSanitizer, "lib", "linux",
1811 (Twine("libclang_rt.") + Sanitizer + "-" + TC.getArchName() + ".a"));
1813 // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a,
1814 // etc.) so that the linker picks custom versions of the global 'operator
1815 // new' and 'operator delete' symbols. We take the extreme (but simple)
1816 // strategy of inserting it at the front of the link command. It also
1817 // needs to be forced to end up in the executable, so wrap it in
1819 SmallVector<const char *, 3> LibSanitizerArgs;
1820 LibSanitizerArgs.push_back("-whole-archive");
1821 LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer));
1822 LibSanitizerArgs.push_back("-no-whole-archive");
1824 CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(),
1825 LibSanitizerArgs.begin(), LibSanitizerArgs.end());
1827 CmdArgs.push_back("-lpthread");
1828 CmdArgs.push_back("-lrt");
1829 CmdArgs.push_back("-ldl");
1830 CmdArgs.push_back("-lm");
1832 // If possible, use a dynamic symbols file to export the symbols from the
1833 // runtime library. If we can't do so, use -export-dynamic instead to export
1834 // all symbols from the binary.
1835 if (ExportSymbols) {
1836 if (llvm::sys::fs::exists(LibSanitizer + ".syms"))
1838 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms"));
1840 CmdArgs.push_back("-export-dynamic");
1844 /// If AddressSanitizer is enabled, add appropriate linker flags (Linux).
1845 /// This needs to be called before we add the C run-time (malloc, etc).
1846 static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
1847 ArgStringList &CmdArgs) {
1848 if (TC.getTriple().getEnvironment() == llvm::Triple::Android) {
1849 SmallString<128> LibAsan(TC.getDriver().ResourceDir);
1850 llvm::sys::path::append(LibAsan, "lib", "linux",
1851 (Twine("libclang_rt.asan-") +
1852 TC.getArchName() + "-android.so"));
1853 CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan));
1855 if (!Args.hasArg(options::OPT_shared))
1856 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "asan", true);
1860 /// If ThreadSanitizer is enabled, add appropriate linker flags (Linux).
1861 /// This needs to be called before we add the C run-time (malloc, etc).
1862 static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
1863 ArgStringList &CmdArgs) {
1864 if (!Args.hasArg(options::OPT_shared))
1865 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "tsan", true);
1868 /// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
1869 /// This needs to be called before we add the C run-time (malloc, etc).
1870 static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args,
1871 ArgStringList &CmdArgs) {
1872 if (!Args.hasArg(options::OPT_shared))
1873 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "msan", true);
1876 /// If LeakSanitizer is enabled, add appropriate linker flags (Linux).
1877 /// This needs to be called before we add the C run-time (malloc, etc).
1878 static void addLsanRTLinux(const ToolChain &TC, const ArgList &Args,
1879 ArgStringList &CmdArgs) {
1880 if (!Args.hasArg(options::OPT_shared))
1881 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "lsan", true);
1884 /// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
1886 static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
1887 ArgStringList &CmdArgs, bool IsCXX,
1888 bool HasOtherSanitizerRt) {
1889 // Need a copy of sanitizer_common. This could come from another sanitizer
1890 // runtime; if we're not including one, include our own copy.
1891 if (!HasOtherSanitizerRt)
1892 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "san", true, false);
1894 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan", false);
1896 // Only include the bits of the runtime which need a C++ ABI library if
1897 // we're linking in C++ mode.
1899 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan_cxx", false);
1902 static void addDfsanRTLinux(const ToolChain &TC, const ArgList &Args,
1903 ArgStringList &CmdArgs) {
1904 if (!Args.hasArg(options::OPT_shared))
1905 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "dfsan", true);
1908 static bool shouldUseFramePointerForTarget(const ArgList &Args,
1909 const llvm::Triple &Triple) {
1910 switch (Triple.getArch()) {
1911 // Don't use a frame pointer on linux if optimizing for certain targets.
1912 case llvm::Triple::mips64:
1913 case llvm::Triple::mips64el:
1914 case llvm::Triple::mips:
1915 case llvm::Triple::mipsel:
1916 case llvm::Triple::systemz:
1917 case llvm::Triple::x86:
1918 case llvm::Triple::x86_64:
1919 if (Triple.isOSLinux())
1920 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1921 if (!A->getOption().matches(options::OPT_O0))
1924 case llvm::Triple::xcore:
1931 static bool shouldUseFramePointer(const ArgList &Args,
1932 const llvm::Triple &Triple) {
1933 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer,
1934 options::OPT_fomit_frame_pointer))
1935 return A->getOption().matches(options::OPT_fno_omit_frame_pointer);
1937 return shouldUseFramePointerForTarget(Args, Triple);
1940 static bool shouldUseLeafFramePointer(const ArgList &Args,
1941 const llvm::Triple &Triple) {
1942 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer,
1943 options::OPT_momit_leaf_frame_pointer))
1944 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer);
1946 return shouldUseFramePointerForTarget(Args, Triple);
1949 /// Add a CC1 option to specify the debug compilation directory.
1950 static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
1951 SmallString<128> cwd;
1952 if (!llvm::sys::fs::current_path(cwd)) {
1953 CmdArgs.push_back("-fdebug-compilation-dir");
1954 CmdArgs.push_back(Args.MakeArgString(cwd));
1958 static const char *SplitDebugName(const ArgList &Args,
1959 const InputInfoList &Inputs) {
1960 Arg *FinalOutput = Args.getLastArg(options::OPT_o);
1961 if (FinalOutput && Args.hasArg(options::OPT_c)) {
1962 SmallString<128> T(FinalOutput->getValue());
1963 llvm::sys::path::replace_extension(T, "dwo");
1964 return Args.MakeArgString(T);
1966 // Use the compilation dir.
1967 SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir));
1968 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput()));
1969 llvm::sys::path::replace_extension(F, "dwo");
1971 return Args.MakeArgString(F);
1975 static void SplitDebugInfo(const ToolChain &TC, Compilation &C,
1976 const Tool &T, const JobAction &JA,
1977 const ArgList &Args, const InputInfo &Output,
1978 const char *OutFile) {
1979 ArgStringList ExtractArgs;
1980 ExtractArgs.push_back("--extract-dwo");
1982 ArgStringList StripArgs;
1983 StripArgs.push_back("--strip-dwo");
1985 // Grabbing the output of the earlier compile step.
1986 StripArgs.push_back(Output.getFilename());
1987 ExtractArgs.push_back(Output.getFilename());
1988 ExtractArgs.push_back(OutFile);
1991 Args.MakeArgString(TC.GetProgramPath("objcopy"));
1993 // First extract the dwo sections.
1994 C.addCommand(new Command(JA, T, Exec, ExtractArgs));
1996 // Then remove them from the original .o file.
1997 C.addCommand(new Command(JA, T, Exec, StripArgs));
2000 /// \brief Vectorize at all optimization levels greater than 1 except for -Oz.
2001 static bool shouldEnableVectorizerAtOLevel(const ArgList &Args) {
2002 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
2003 if (A->getOption().matches(options::OPT_O4) ||
2004 A->getOption().matches(options::OPT_Ofast))
2007 if (A->getOption().matches(options::OPT_O0))
2010 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
2013 StringRef S(A->getValue());
2017 // Don't vectorize -Oz.
2021 unsigned OptLevel = 0;
2022 if (S.getAsInteger(10, OptLevel))
2025 return OptLevel > 1;
2031 void Clang::ConstructJob(Compilation &C, const JobAction &JA,
2032 const InputInfo &Output,
2033 const InputInfoList &Inputs,
2034 const ArgList &Args,
2035 const char *LinkingOutput) const {
2036 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
2037 options::OPT_fapple_kext);
2038 const Driver &D = getToolChain().getDriver();
2039 ArgStringList CmdArgs;
2041 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2043 // Invoke ourselves in -cc1 mode.
2045 // FIXME: Implement custom jobs for internal actions.
2046 CmdArgs.push_back("-cc1");
2048 // Add the "effective" target triple.
2049 CmdArgs.push_back("-triple");
2050 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
2051 CmdArgs.push_back(Args.MakeArgString(TripleStr));
2053 // Select the appropriate action.
2054 RewriteKind rewriteKind = RK_None;
2056 if (isa<AnalyzeJobAction>(JA)) {
2057 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
2058 CmdArgs.push_back("-analyze");
2059 } else if (isa<MigrateJobAction>(JA)) {
2060 CmdArgs.push_back("-migrate");
2061 } else if (isa<PreprocessJobAction>(JA)) {
2062 if (Output.getType() == types::TY_Dependencies)
2063 CmdArgs.push_back("-Eonly");
2065 CmdArgs.push_back("-E");
2066 if (Args.hasArg(options::OPT_rewrite_objc) &&
2067 !Args.hasArg(options::OPT_g_Group))
2068 CmdArgs.push_back("-P");
2070 } else if (isa<AssembleJobAction>(JA)) {
2071 CmdArgs.push_back("-emit-obj");
2073 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
2075 // Also ignore explicit -force_cpusubtype_ALL option.
2076 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
2077 } else if (isa<PrecompileJobAction>(JA)) {
2078 // Use PCH if the user requested it.
2079 bool UsePCH = D.CCCUsePCH;
2081 if (JA.getType() == types::TY_Nothing)
2082 CmdArgs.push_back("-fsyntax-only");
2084 CmdArgs.push_back("-emit-pch");
2086 CmdArgs.push_back("-emit-pth");
2088 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
2090 if (JA.getType() == types::TY_Nothing) {
2091 CmdArgs.push_back("-fsyntax-only");
2092 } else if (JA.getType() == types::TY_LLVM_IR ||
2093 JA.getType() == types::TY_LTO_IR) {
2094 CmdArgs.push_back("-emit-llvm");
2095 } else if (JA.getType() == types::TY_LLVM_BC ||
2096 JA.getType() == types::TY_LTO_BC) {
2097 CmdArgs.push_back("-emit-llvm-bc");
2098 } else if (JA.getType() == types::TY_PP_Asm) {
2099 CmdArgs.push_back("-S");
2100 } else if (JA.getType() == types::TY_AST) {
2101 CmdArgs.push_back("-emit-pch");
2102 } else if (JA.getType() == types::TY_ModuleFile) {
2103 CmdArgs.push_back("-module-file-info");
2104 } else if (JA.getType() == types::TY_RewrittenObjC) {
2105 CmdArgs.push_back("-rewrite-objc");
2106 rewriteKind = RK_NonFragile;
2107 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
2108 CmdArgs.push_back("-rewrite-objc");
2109 rewriteKind = RK_Fragile;
2111 assert(JA.getType() == types::TY_PP_Asm &&
2112 "Unexpected output type!");
2116 // The make clang go fast button.
2117 CmdArgs.push_back("-disable-free");
2119 // Disable the verification pass in -asserts builds.
2121 CmdArgs.push_back("-disable-llvm-verifier");
2124 // Set the main file name, so that debug info works even with
2126 CmdArgs.push_back("-main-file-name");
2127 CmdArgs.push_back(getBaseInputName(Args, Inputs));
2129 // Some flags which affect the language (via preprocessor
2131 if (Args.hasArg(options::OPT_static))
2132 CmdArgs.push_back("-static-define");
2134 if (isa<AnalyzeJobAction>(JA)) {
2135 // Enable region store model by default.
2136 CmdArgs.push_back("-analyzer-store=region");
2138 // Treat blocks as analysis entry points.
2139 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
2141 CmdArgs.push_back("-analyzer-eagerly-assume");
2143 // Add default argument set.
2144 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
2145 CmdArgs.push_back("-analyzer-checker=core");
2147 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32)
2148 CmdArgs.push_back("-analyzer-checker=unix");
2150 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple)
2151 CmdArgs.push_back("-analyzer-checker=osx");
2153 CmdArgs.push_back("-analyzer-checker=deadcode");
2155 if (types::isCXX(Inputs[0].getType()))
2156 CmdArgs.push_back("-analyzer-checker=cplusplus");
2158 // Enable the following experimental checkers for testing.
2159 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn");
2160 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw");
2161 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets");
2162 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp");
2163 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp");
2164 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork");
2167 // Set the output format. The default is plist, for (lame) historical
2169 CmdArgs.push_back("-analyzer-output");
2170 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
2171 CmdArgs.push_back(A->getValue());
2173 CmdArgs.push_back("plist");
2175 // Disable the presentation of standard compiler warnings when
2176 // using --analyze. We only want to show static analyzer diagnostics
2177 // or frontend errors.
2178 CmdArgs.push_back("-w");
2180 // Add -Xanalyzer arguments when running as analyzer.
2181 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
2184 CheckCodeGenerationOptions(D, Args);
2186 bool PIE = getToolChain().isPIEDefault();
2187 bool PIC = PIE || getToolChain().isPICDefault();
2188 bool IsPICLevelTwo = PIC;
2190 // For the PIC and PIE flag options, this logic is different from the
2191 // legacy logic in very old versions of GCC, as that logic was just
2192 // a bug no one had ever fixed. This logic is both more rational and
2193 // consistent with GCC's new logic now that the bugs are fixed. The last
2194 // argument relating to either PIC or PIE wins, and no other argument is
2195 // used. If the last argument is any flavor of the '-fno-...' arguments,
2196 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC
2197 // at the same level.
2198 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
2199 options::OPT_fpic, options::OPT_fno_pic,
2200 options::OPT_fPIE, options::OPT_fno_PIE,
2201 options::OPT_fpie, options::OPT_fno_pie);
2202 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
2203 // is forced, then neither PIC nor PIE flags will have no effect.
2204 if (!getToolChain().isPICDefaultForced()) {
2206 Option O = LastPICArg->getOption();
2207 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
2208 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) {
2209 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie);
2210 PIC = PIE || O.matches(options::OPT_fPIC) ||
2211 O.matches(options::OPT_fpic);
2212 IsPICLevelTwo = O.matches(options::OPT_fPIE) ||
2213 O.matches(options::OPT_fPIC);
2220 // Introduce a Darwin-specific hack. If the default is PIC but the flags
2221 // specified while enabling PIC enabled level 1 PIC, just force it back to
2222 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my
2223 // informal testing).
2224 if (PIC && getToolChain().getTriple().isOSDarwin())
2225 IsPICLevelTwo |= getToolChain().isPICDefault();
2227 // Note that these flags are trump-cards. Regardless of the order w.r.t. the
2228 // PIC or PIE options above, if these show up, PIC is disabled.
2229 llvm::Triple Triple(TripleStr);
2231 (!Triple.isiOS() || Triple.isOSVersionLT(6)))
2233 if (Args.hasArg(options::OPT_static))
2236 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) {
2237 // This is a very special mode. It trumps the other modes, almost no one
2238 // uses it, and it isn't even valid on any OS but Darwin.
2239 if (!getToolChain().getTriple().isOSDarwin())
2240 D.Diag(diag::err_drv_unsupported_opt_for_target)
2241 << A->getSpelling() << getToolChain().getTriple().str();
2243 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
2245 CmdArgs.push_back("-mrelocation-model");
2246 CmdArgs.push_back("dynamic-no-pic");
2248 // Only a forced PIC mode can cause the actual compile to have PIC defines
2249 // etc., no flags are sufficient. This behavior was selected to closely
2250 // match that of llvm-gcc and Apple GCC before that.
2251 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) {
2252 CmdArgs.push_back("-pic-level");
2253 CmdArgs.push_back("2");
2256 // Currently, LLVM only knows about PIC vs. static; the PIE differences are
2257 // handled in Clang's IRGen by the -pie-level flag.
2258 CmdArgs.push_back("-mrelocation-model");
2259 CmdArgs.push_back(PIC ? "pic" : "static");
2262 CmdArgs.push_back("-pic-level");
2263 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2265 CmdArgs.push_back("-pie-level");
2266 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1");
2271 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
2272 options::OPT_fno_merge_all_constants))
2273 CmdArgs.push_back("-fno-merge-all-constants");
2275 // LLVM Code Generator Options.
2277 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) {
2278 CmdArgs.push_back("-mregparm");
2279 CmdArgs.push_back(A->getValue());
2282 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return,
2283 options::OPT_freg_struct_return)) {
2284 if (getToolChain().getArch() != llvm::Triple::x86) {
2285 D.Diag(diag::err_drv_unsupported_opt_for_target)
2286 << A->getSpelling() << getToolChain().getTriple().str();
2287 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) {
2288 CmdArgs.push_back("-fpcc-struct-return");
2290 assert(A->getOption().matches(options::OPT_freg_struct_return));
2291 CmdArgs.push_back("-freg-struct-return");
2295 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
2296 CmdArgs.push_back("-mrtd");
2298 if (shouldUseFramePointer(Args, getToolChain().getTriple()))
2299 CmdArgs.push_back("-mdisable-fp-elim");
2300 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
2301 options::OPT_fno_zero_initialized_in_bss))
2302 CmdArgs.push_back("-mno-zero-initialized-in-bss");
2304 bool OFastEnabled = isOptimizationLevelFast(Args);
2305 // If -Ofast is the optimization level, then -fstrict-aliasing should be
2306 // enabled. This alias option is being used to simplify the hasFlag logic.
2307 OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast :
2308 options::OPT_fstrict_aliasing;
2309 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
2310 options::OPT_fno_strict_aliasing, true))
2311 CmdArgs.push_back("-relaxed-aliasing");
2312 if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
2313 options::OPT_fno_struct_path_tbaa))
2314 CmdArgs.push_back("-no-struct-path-tbaa");
2315 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums,
2317 CmdArgs.push_back("-fstrict-enums");
2318 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
2319 options::OPT_fno_optimize_sibling_calls))
2320 CmdArgs.push_back("-mdisable-tail-calls");
2322 // Handle segmented stacks.
2323 if (Args.hasArg(options::OPT_fsplit_stack))
2324 CmdArgs.push_back("-split-stacks");
2326 // If -Ofast is the optimization level, then -ffast-math should be enabled.
2327 // This alias option is being used to simplify the getLastArg logic.
2328 OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast :
2329 options::OPT_ffast_math;
2331 // Handle various floating point optimization flags, mapping them to the
2332 // appropriate LLVM code generation flags. The pattern for all of these is to
2333 // default off the codegen optimizations, and if any flag enables them and no
2334 // flag disables them after the flag enabling them, enable the codegen
2335 // optimization. This is complicated by several "umbrella" flags.
2336 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2337 options::OPT_fno_fast_math,
2338 options::OPT_ffinite_math_only,
2339 options::OPT_fno_finite_math_only,
2340 options::OPT_fhonor_infinities,
2341 options::OPT_fno_honor_infinities))
2342 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2343 A->getOption().getID() != options::OPT_fno_finite_math_only &&
2344 A->getOption().getID() != options::OPT_fhonor_infinities)
2345 CmdArgs.push_back("-menable-no-infs");
2346 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2347 options::OPT_fno_fast_math,
2348 options::OPT_ffinite_math_only,
2349 options::OPT_fno_finite_math_only,
2350 options::OPT_fhonor_nans,
2351 options::OPT_fno_honor_nans))
2352 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2353 A->getOption().getID() != options::OPT_fno_finite_math_only &&
2354 A->getOption().getID() != options::OPT_fhonor_nans)
2355 CmdArgs.push_back("-menable-no-nans");
2357 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
2358 bool MathErrno = getToolChain().IsMathErrnoDefault();
2359 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2360 options::OPT_fno_fast_math,
2361 options::OPT_fmath_errno,
2362 options::OPT_fno_math_errno)) {
2363 // Turning on -ffast_math (with either flag) removes the need for MathErrno.
2364 // However, turning *off* -ffast_math merely restores the toolchain default
2365 // (which may be false).
2366 if (A->getOption().getID() == options::OPT_fno_math_errno ||
2367 A->getOption().getID() == options::OPT_ffast_math ||
2368 A->getOption().getID() == options::OPT_Ofast)
2370 else if (A->getOption().getID() == options::OPT_fmath_errno)
2374 CmdArgs.push_back("-fmath-errno");
2376 // There are several flags which require disabling very specific
2377 // optimizations. Any of these being disabled forces us to turn off the
2378 // entire set of LLVM optimizations, so collect them through all the flag
2380 bool AssociativeMath = false;
2381 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2382 options::OPT_fno_fast_math,
2383 options::OPT_funsafe_math_optimizations,
2384 options::OPT_fno_unsafe_math_optimizations,
2385 options::OPT_fassociative_math,
2386 options::OPT_fno_associative_math))
2387 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2388 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
2389 A->getOption().getID() != options::OPT_fno_associative_math)
2390 AssociativeMath = true;
2391 bool ReciprocalMath = false;
2392 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2393 options::OPT_fno_fast_math,
2394 options::OPT_funsafe_math_optimizations,
2395 options::OPT_fno_unsafe_math_optimizations,
2396 options::OPT_freciprocal_math,
2397 options::OPT_fno_reciprocal_math))
2398 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2399 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
2400 A->getOption().getID() != options::OPT_fno_reciprocal_math)
2401 ReciprocalMath = true;
2402 bool SignedZeros = true;
2403 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2404 options::OPT_fno_fast_math,
2405 options::OPT_funsafe_math_optimizations,
2406 options::OPT_fno_unsafe_math_optimizations,
2407 options::OPT_fsigned_zeros,
2408 options::OPT_fno_signed_zeros))
2409 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2410 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
2411 A->getOption().getID() != options::OPT_fsigned_zeros)
2412 SignedZeros = false;
2413 bool TrappingMath = true;
2414 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2415 options::OPT_fno_fast_math,
2416 options::OPT_funsafe_math_optimizations,
2417 options::OPT_fno_unsafe_math_optimizations,
2418 options::OPT_ftrapping_math,
2419 options::OPT_fno_trapping_math))
2420 if (A->getOption().getID() != options::OPT_fno_fast_math &&
2421 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations &&
2422 A->getOption().getID() != options::OPT_ftrapping_math)
2423 TrappingMath = false;
2424 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
2426 CmdArgs.push_back("-menable-unsafe-fp-math");
2429 // Validate and pass through -fp-contract option.
2430 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2431 options::OPT_fno_fast_math,
2432 options::OPT_ffp_contract)) {
2433 if (A->getOption().getID() == options::OPT_ffp_contract) {
2434 StringRef Val = A->getValue();
2435 if (Val == "fast" || Val == "on" || Val == "off") {
2436 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
2438 D.Diag(diag::err_drv_unsupported_option_argument)
2439 << A->getOption().getName() << Val;
2441 } else if (A->getOption().matches(options::OPT_ffast_math) ||
2442 (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) {
2443 // If fast-math is set then set the fp-contract mode to fast.
2444 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
2448 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags,
2449 // and if we find them, tell the frontend to provide the appropriate
2450 // preprocessor macros. This is distinct from enabling any optimizations as
2451 // these options induce language changes which must survive serialization
2452 // and deserialization, etc.
2453 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
2454 options::OPT_fno_fast_math))
2455 if (!A->getOption().matches(options::OPT_fno_fast_math))
2456 CmdArgs.push_back("-ffast-math");
2457 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math))
2458 if (A->getOption().matches(options::OPT_ffinite_math_only))
2459 CmdArgs.push_back("-ffinite-math-only");
2461 // Decide whether to use verbose asm. Verbose assembly is the default on
2462 // toolchains which have the integrated assembler on by default.
2463 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
2464 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
2465 IsVerboseAsmDefault) ||
2466 Args.hasArg(options::OPT_dA))
2467 CmdArgs.push_back("-masm-verbose");
2469 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
2470 CmdArgs.push_back("-mdebug-pass");
2471 CmdArgs.push_back("Structure");
2473 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
2474 CmdArgs.push_back("-mdebug-pass");
2475 CmdArgs.push_back("Arguments");
2478 // Enable -mconstructor-aliases except on darwin, where we have to
2479 // work around a linker bug; see <rdar://problem/7651567>.
2480 if (!getToolChain().getTriple().isOSDarwin())
2481 CmdArgs.push_back("-mconstructor-aliases");
2483 // Darwin's kernel doesn't support guard variables; just die if we
2485 if (KernelOrKext && getToolChain().getTriple().isOSDarwin())
2486 CmdArgs.push_back("-fforbid-guard-variables");
2488 if (Args.hasArg(options::OPT_mms_bitfields)) {
2489 CmdArgs.push_back("-mms-bitfields");
2492 // This is a coarse approximation of what llvm-gcc actually does, both
2493 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
2494 // complicated ways.
2495 bool AsynchronousUnwindTables =
2496 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
2497 options::OPT_fno_asynchronous_unwind_tables,
2498 getToolChain().IsUnwindTablesDefault() &&
2500 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
2501 AsynchronousUnwindTables))
2502 CmdArgs.push_back("-munwind-tables");
2504 getToolChain().addClangTargetOptions(Args, CmdArgs);
2506 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
2507 CmdArgs.push_back("-mlimit-float-precision");
2508 CmdArgs.push_back(A->getValue());
2511 // FIXME: Handle -mtune=.
2512 (void) Args.hasArg(options::OPT_mtune_EQ);
2514 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
2515 CmdArgs.push_back("-mcode-model");
2516 CmdArgs.push_back(A->getValue());
2519 // Add the target cpu
2520 std::string ETripleStr = getToolChain().ComputeEffectiveClangTriple(Args);
2521 llvm::Triple ETriple(ETripleStr);
2522 std::string CPU = getCPUName(Args, ETriple);
2524 CmdArgs.push_back("-target-cpu");
2525 CmdArgs.push_back(Args.MakeArgString(CPU));
2528 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
2529 CmdArgs.push_back("-mfpmath");
2530 CmdArgs.push_back(A->getValue());
2533 // Add the target features
2534 getTargetFeatures(D, ETriple, Args, CmdArgs);
2536 // Add target specific flags.
2537 switch(getToolChain().getArch()) {
2541 case llvm::Triple::arm:
2542 case llvm::Triple::thumb:
2543 AddARMTargetArgs(Args, CmdArgs, KernelOrKext);
2546 case llvm::Triple::mips:
2547 case llvm::Triple::mipsel:
2548 case llvm::Triple::mips64:
2549 case llvm::Triple::mips64el:
2550 AddMIPSTargetArgs(Args, CmdArgs);
2553 case llvm::Triple::sparc:
2554 AddSparcTargetArgs(Args, CmdArgs);
2557 case llvm::Triple::x86:
2558 case llvm::Triple::x86_64:
2559 AddX86TargetArgs(Args, CmdArgs);
2562 case llvm::Triple::hexagon:
2563 AddHexagonTargetArgs(Args, CmdArgs);
2567 // Add clang-cl arguments.
2568 if (getToolChain().getDriver().IsCLMode())
2569 AddClangCLArgs(Args, CmdArgs);
2571 // Pass the linker version in use.
2572 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
2573 CmdArgs.push_back("-target-linker-version");
2574 CmdArgs.push_back(A->getValue());
2577 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple()))
2578 CmdArgs.push_back("-momit-leaf-frame-pointer");
2580 // Explicitly error on some things we know we don't support and can't just
2582 types::ID InputType = Inputs[0].getType();
2583 if (!Args.hasArg(options::OPT_fallow_unsupported)) {
2585 if (types::isCXX(InputType) &&
2586 getToolChain().getTriple().isOSDarwin() &&
2587 getToolChain().getArch() == llvm::Triple::x86) {
2588 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) ||
2589 (Unsupported = Args.getLastArg(options::OPT_mkernel)))
2590 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
2591 << Unsupported->getOption().getName();
2595 Args.AddAllArgs(CmdArgs, options::OPT_v);
2596 Args.AddLastArg(CmdArgs, options::OPT_H);
2597 if (D.CCPrintHeaders && !D.CCGenDiagnostics) {
2598 CmdArgs.push_back("-header-include-file");
2599 CmdArgs.push_back(D.CCPrintHeadersFilename ?
2600 D.CCPrintHeadersFilename : "-");
2602 Args.AddLastArg(CmdArgs, options::OPT_P);
2603 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
2605 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) {
2606 CmdArgs.push_back("-diagnostic-log-file");
2607 CmdArgs.push_back(D.CCLogDiagnosticsFilename ?
2608 D.CCLogDiagnosticsFilename : "-");
2611 // Use the last option from "-g" group. "-gline-tables-only"
2612 // is preserved, all other debug options are substituted with "-g".
2613 Args.ClaimAllArgs(options::OPT_g_Group);
2614 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
2615 if (A->getOption().matches(options::OPT_gline_tables_only))
2616 CmdArgs.push_back("-gline-tables-only");
2617 else if (A->getOption().matches(options::OPT_gdwarf_2))
2618 CmdArgs.push_back("-gdwarf-2");
2619 else if (A->getOption().matches(options::OPT_gdwarf_3))
2620 CmdArgs.push_back("-gdwarf-3");
2621 else if (A->getOption().matches(options::OPT_gdwarf_4))
2622 CmdArgs.push_back("-gdwarf-4");
2623 else if (!A->getOption().matches(options::OPT_g0) &&
2624 !A->getOption().matches(options::OPT_ggdb0)) {
2625 // Default is dwarf-2 for darwin and FreeBSD.
2626 const llvm::Triple &Triple = getToolChain().getTriple();
2627 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::FreeBSD)
2628 CmdArgs.push_back("-gdwarf-2");
2630 CmdArgs.push_back("-g");
2634 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now.
2635 Args.ClaimAllArgs(options::OPT_g_flags_Group);
2636 if (Args.hasArg(options::OPT_gcolumn_info))
2637 CmdArgs.push_back("-dwarf-column-info");
2639 // FIXME: Move backend command line options to the module.
2640 // -gsplit-dwarf should turn on -g and enable the backend dwarf
2641 // splitting and extraction.
2642 // FIXME: Currently only works on Linux.
2643 if (getToolChain().getTriple().isOSLinux() &&
2644 Args.hasArg(options::OPT_gsplit_dwarf)) {
2645 CmdArgs.push_back("-g");
2646 CmdArgs.push_back("-backend-option");
2647 CmdArgs.push_back("-split-dwarf=Enable");
2650 // -ggnu-pubnames turns on gnu style pubnames in the backend.
2651 if (Args.hasArg(options::OPT_ggnu_pubnames)) {
2652 CmdArgs.push_back("-backend-option");
2653 CmdArgs.push_back("-generate-gnu-dwarf-pub-sections");
2656 Args.AddAllArgs(CmdArgs, options::OPT_fdebug_types_section);
2658 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
2659 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
2661 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
2663 if (Args.hasArg(options::OPT_ftest_coverage) ||
2664 Args.hasArg(options::OPT_coverage))
2665 CmdArgs.push_back("-femit-coverage-notes");
2666 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2667 Args.hasArg(options::OPT_coverage))
2668 CmdArgs.push_back("-femit-coverage-data");
2670 if (C.getArgs().hasArg(options::OPT_c) ||
2671 C.getArgs().hasArg(options::OPT_S)) {
2672 if (Output.isFilename()) {
2673 CmdArgs.push_back("-coverage-file");
2674 SmallString<128> CoverageFilename(Output.getFilename());
2675 if (llvm::sys::path::is_relative(CoverageFilename.str())) {
2676 SmallString<128> Pwd;
2677 if (!llvm::sys::fs::current_path(Pwd)) {
2678 llvm::sys::path::append(Pwd, CoverageFilename.str());
2679 CoverageFilename.swap(Pwd);
2682 CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
2686 // Pass options for controlling the default header search paths.
2687 if (Args.hasArg(options::OPT_nostdinc)) {
2688 CmdArgs.push_back("-nostdsysteminc");
2689 CmdArgs.push_back("-nobuiltininc");
2691 if (Args.hasArg(options::OPT_nostdlibinc))
2692 CmdArgs.push_back("-nostdsysteminc");
2693 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
2694 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
2697 // Pass the path to compiler resource files.
2698 CmdArgs.push_back("-resource-dir");
2699 CmdArgs.push_back(D.ResourceDir.c_str());
2701 Args.AddLastArg(CmdArgs, options::OPT_working_directory);
2703 bool ARCMTEnabled = false;
2704 if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) {
2705 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check,
2706 options::OPT_ccc_arcmt_modify,
2707 options::OPT_ccc_arcmt_migrate)) {
2708 ARCMTEnabled = true;
2709 switch (A->getOption().getID()) {
2711 llvm_unreachable("missed a case");
2712 case options::OPT_ccc_arcmt_check:
2713 CmdArgs.push_back("-arcmt-check");
2715 case options::OPT_ccc_arcmt_modify:
2716 CmdArgs.push_back("-arcmt-modify");
2718 case options::OPT_ccc_arcmt_migrate:
2719 CmdArgs.push_back("-arcmt-migrate");
2720 CmdArgs.push_back("-mt-migrate-directory");
2721 CmdArgs.push_back(A->getValue());
2723 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output);
2724 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors);
2729 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check);
2730 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify);
2731 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate);
2734 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) {
2736 D.Diag(diag::err_drv_argument_not_allowed_with)
2737 << A->getAsString(Args) << "-ccc-arcmt-migrate";
2739 CmdArgs.push_back("-mt-migrate-directory");
2740 CmdArgs.push_back(A->getValue());
2742 if (!Args.hasArg(options::OPT_objcmt_migrate_literals,
2743 options::OPT_objcmt_migrate_subscripting,
2744 options::OPT_objcmt_migrate_property)) {
2745 // None specified, means enable them all.
2746 CmdArgs.push_back("-objcmt-migrate-literals");
2747 CmdArgs.push_back("-objcmt-migrate-subscripting");
2748 CmdArgs.push_back("-objcmt-migrate-property");
2750 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2751 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2752 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2755 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals);
2756 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting);
2757 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property);
2758 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_all);
2759 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readonly_property);
2760 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_readwrite_property);
2761 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_annotation);
2762 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_instancetype);
2763 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_nsmacros);
2764 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_protocol_conformance);
2765 Args.AddLastArg(CmdArgs, options::OPT_objcmt_atomic_property);
2766 Args.AddLastArg(CmdArgs, options::OPT_objcmt_returns_innerpointer_property);
2767 Args.AddLastArg(CmdArgs, options::OPT_objcmt_ns_nonatomic_iosonly);
2768 Args.AddLastArg(CmdArgs, options::OPT_objcmt_white_list_dir_path);
2771 // Add preprocessing options like -I, -D, etc. if we are using the
2774 // FIXME: Support -fpreprocessed
2775 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
2776 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
2778 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
2779 // that "The compiler can only warn and ignore the option if not recognized".
2780 // When building with ccache, it will pass -D options to clang even on
2781 // preprocessed inputs and configure concludes that -fPIC is not supported.
2782 Args.ClaimAllArgs(options::OPT_D);
2784 // Manually translate -O4 to -O3; let clang reject others.
2785 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
2786 if (A->getOption().matches(options::OPT_O4)) {
2787 CmdArgs.push_back("-O3");
2788 D.Diag(diag::warn_O4_is_O3);
2790 A->render(Args, CmdArgs);
2794 // Don't warn about unused -flto. This can happen when we're preprocessing or
2796 Args.ClaimAllArgs(options::OPT_flto);
2798 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
2799 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false))
2800 CmdArgs.push_back("-pedantic");
2801 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
2802 Args.AddLastArg(CmdArgs, options::OPT_w);
2804 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
2805 // (-ansi is equivalent to -std=c89 or -std=c++98).
2807 // If a std is supplied, only add -trigraphs if it follows the
2809 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
2810 if (Std->getOption().matches(options::OPT_ansi))
2811 if (types::isCXX(InputType))
2812 CmdArgs.push_back("-std=c++98");
2814 CmdArgs.push_back("-std=c89");
2816 Std->render(Args, CmdArgs);
2818 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
2819 options::OPT_trigraphs))
2821 A->render(Args, CmdArgs);
2823 // Honor -std-default.
2825 // FIXME: Clang doesn't correctly handle -std= when the input language
2826 // doesn't match. For the time being just ignore this for C++ inputs;
2827 // eventually we want to do all the standard defaulting here instead of
2828 // splitting it between the driver and clang -cc1.
2829 if (!types::isCXX(InputType))
2830 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
2831 "-std=", /*Joined=*/true);
2832 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32)
2833 CmdArgs.push_back("-std=c++11");
2835 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
2838 // GCC's behavior for -Wwrite-strings is a bit strange:
2839 // * In C, this "warning flag" changes the types of string literals from
2840 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning
2841 // for the discarded qualifier.
2842 // * In C++, this is just a normal warning flag.
2844 // Implementing this warning correctly in C is hard, so we follow GCC's
2845 // behavior for now. FIXME: Directly diagnose uses of a string literal as
2846 // a non-const char* in C, rather than using this crude hack.
2847 if (!types::isCXX(InputType)) {
2848 DiagnosticsEngine::Level DiagLevel = D.getDiags().getDiagnosticLevel(
2849 diag::warn_deprecated_string_literal_conversion_c, SourceLocation());
2850 if (DiagLevel > DiagnosticsEngine::Ignored)
2851 CmdArgs.push_back("-fconst-strings");
2854 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active
2855 // during C++ compilation, which it is by default. GCC keeps this define even
2856 // in the presence of '-w', match this behavior bug-for-bug.
2857 if (types::isCXX(InputType) &&
2858 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated,
2860 CmdArgs.push_back("-fdeprecated-macro");
2863 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
2864 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
2865 if (Asm->getOption().matches(options::OPT_fasm))
2866 CmdArgs.push_back("-fgnu-keywords");
2868 CmdArgs.push_back("-fno-gnu-keywords");
2871 if (ShouldDisableCFI(Args, getToolChain()))
2872 CmdArgs.push_back("-fno-dwarf2-cfi-asm");
2874 if (ShouldDisableDwarfDirectory(Args, getToolChain()))
2875 CmdArgs.push_back("-fno-dwarf-directory-asm");
2877 if (ShouldDisableAutolink(Args, getToolChain()))
2878 CmdArgs.push_back("-fno-autolink");
2880 // Add in -fdebug-compilation-dir if necessary.
2881 addDebugCompDirArg(Args, CmdArgs);
2883 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_,
2884 options::OPT_ftemplate_depth_EQ)) {
2885 CmdArgs.push_back("-ftemplate-depth");
2886 CmdArgs.push_back(A->getValue());
2889 if (Arg *A = Args.getLastArg(options::OPT_foperator_arrow_depth_EQ)) {
2890 CmdArgs.push_back("-foperator-arrow-depth");
2891 CmdArgs.push_back(A->getValue());
2894 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) {
2895 CmdArgs.push_back("-fconstexpr-depth");
2896 CmdArgs.push_back(A->getValue());
2899 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) {
2900 CmdArgs.push_back("-fconstexpr-steps");
2901 CmdArgs.push_back(A->getValue());
2904 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) {
2905 CmdArgs.push_back("-fbracket-depth");
2906 CmdArgs.push_back(A->getValue());
2909 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ,
2910 options::OPT_Wlarge_by_value_copy_def)) {
2911 if (A->getNumValues()) {
2912 StringRef bytes = A->getValue();
2913 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes));
2915 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value
2919 if (Args.hasArg(options::OPT_relocatable_pch))
2920 CmdArgs.push_back("-relocatable-pch");
2922 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
2923 CmdArgs.push_back("-fconstant-string-class");
2924 CmdArgs.push_back(A->getValue());
2927 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
2928 CmdArgs.push_back("-ftabstop");
2929 CmdArgs.push_back(A->getValue());
2932 CmdArgs.push_back("-ferror-limit");
2933 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
2934 CmdArgs.push_back(A->getValue());
2936 CmdArgs.push_back("19");
2938 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
2939 CmdArgs.push_back("-fmacro-backtrace-limit");
2940 CmdArgs.push_back(A->getValue());
2943 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
2944 CmdArgs.push_back("-ftemplate-backtrace-limit");
2945 CmdArgs.push_back(A->getValue());
2948 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) {
2949 CmdArgs.push_back("-fconstexpr-backtrace-limit");
2950 CmdArgs.push_back(A->getValue());
2953 // Pass -fmessage-length=.
2954 CmdArgs.push_back("-fmessage-length");
2955 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
2956 CmdArgs.push_back(A->getValue());
2958 // If -fmessage-length=N was not specified, determine whether this is a
2959 // terminal and, if so, implicitly define -fmessage-length appropriately.
2960 unsigned N = llvm::sys::Process::StandardErrColumns();
2961 CmdArgs.push_back(Args.MakeArgString(Twine(N)));
2964 // -fvisibility= and -fvisibility-ms-compat are of a piece.
2965 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
2966 options::OPT_fvisibility_ms_compat)) {
2967 if (A->getOption().matches(options::OPT_fvisibility_EQ)) {
2968 CmdArgs.push_back("-fvisibility");
2969 CmdArgs.push_back(A->getValue());
2971 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat));
2972 CmdArgs.push_back("-fvisibility");
2973 CmdArgs.push_back("hidden");
2974 CmdArgs.push_back("-ftype-visibility");
2975 CmdArgs.push_back("default");
2979 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
2981 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
2983 // -fhosted is default.
2984 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
2986 CmdArgs.push_back("-ffreestanding");
2988 // Forward -f (flag) options which we can pass directly.
2989 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
2990 Args.AddLastArg(CmdArgs, options::OPT_fformat_extensions);
2991 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
2992 Args.AddLastArg(CmdArgs, options::OPT_fstandalone_debug);
2993 Args.AddLastArg(CmdArgs, options::OPT_fno_standalone_debug);
2994 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
2995 // AltiVec language extensions aren't relevant for assembling.
2996 if (!isa<PreprocessJobAction>(JA) ||
2997 Output.getType() != types::TY_PP_Asm)
2998 Args.AddLastArg(CmdArgs, options::OPT_faltivec);
2999 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree);
3000 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type);
3002 const SanitizerArgs &Sanitize = getToolChain().getSanitizerArgs();
3003 Sanitize.addArgs(Args, CmdArgs);
3005 if (!Args.hasFlag(options::OPT_fsanitize_recover,
3006 options::OPT_fno_sanitize_recover,
3008 CmdArgs.push_back("-fno-sanitize-recover");
3010 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) ||
3011 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
3012 options::OPT_fno_sanitize_undefined_trap_on_error, false))
3013 CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
3015 // Report an error for -faltivec on anything other than PowerPC.
3016 if (const Arg *A = Args.getLastArg(options::OPT_faltivec))
3017 if (!(getToolChain().getArch() == llvm::Triple::ppc ||
3018 getToolChain().getArch() == llvm::Triple::ppc64 ||
3019 getToolChain().getArch() == llvm::Triple::ppc64le))
3020 D.Diag(diag::err_drv_argument_only_allowed_with)
3021 << A->getAsString(Args) << "ppc/ppc64/ppc64le";
3023 if (getToolChain().SupportsProfiling())
3024 Args.AddLastArg(CmdArgs, options::OPT_pg);
3026 // -flax-vector-conversions is default.
3027 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
3028 options::OPT_fno_lax_vector_conversions))
3029 CmdArgs.push_back("-fno-lax-vector-conversions");
3031 if (Args.getLastArg(options::OPT_fapple_kext))
3032 CmdArgs.push_back("-fapple-kext");
3034 if (Args.hasFlag(options::OPT_frewrite_includes,
3035 options::OPT_fno_rewrite_includes, false))
3036 CmdArgs.push_back("-frewrite-includes");
3038 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
3039 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
3040 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits);
3041 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
3042 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
3044 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) {
3045 CmdArgs.push_back("-ftrapv-handler");
3046 CmdArgs.push_back(A->getValue());
3049 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ);
3051 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but
3052 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv.
3053 if (Arg *A = Args.getLastArg(options::OPT_fwrapv,
3054 options::OPT_fno_wrapv)) {
3055 if (A->getOption().matches(options::OPT_fwrapv))
3056 CmdArgs.push_back("-fwrapv");
3057 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow,
3058 options::OPT_fno_strict_overflow)) {
3059 if (A->getOption().matches(options::OPT_fno_strict_overflow))
3060 CmdArgs.push_back("-fwrapv");
3063 if (Arg *A = Args.getLastArg(options::OPT_freroll_loops,
3064 options::OPT_fno_reroll_loops))
3065 if (A->getOption().matches(options::OPT_freroll_loops))
3066 CmdArgs.push_back("-freroll-loops");
3068 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
3069 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops,
3070 options::OPT_fno_unroll_loops);
3072 Args.AddLastArg(CmdArgs, options::OPT_pthread);
3075 // -stack-protector=0 is default.
3076 unsigned StackProtectorLevel = 0;
3077 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
3078 options::OPT_fstack_protector_all,
3079 options::OPT_fstack_protector)) {
3080 if (A->getOption().matches(options::OPT_fstack_protector))
3081 StackProtectorLevel = 1;
3082 else if (A->getOption().matches(options::OPT_fstack_protector_all))
3083 StackProtectorLevel = 2;
3085 StackProtectorLevel =
3086 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
3088 if (StackProtectorLevel) {
3089 CmdArgs.push_back("-stack-protector");
3090 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel)));
3093 // --param ssp-buffer-size=
3094 for (arg_iterator it = Args.filtered_begin(options::OPT__param),
3095 ie = Args.filtered_end(); it != ie; ++it) {
3096 StringRef Str((*it)->getValue());
3097 if (Str.startswith("ssp-buffer-size=")) {
3098 if (StackProtectorLevel) {
3099 CmdArgs.push_back("-stack-protector-buffer-size");
3100 // FIXME: Verify the argument is a valid integer.
3101 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16)));
3107 // Translate -mstackrealign
3108 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign,
3110 CmdArgs.push_back("-backend-option");
3111 CmdArgs.push_back("-force-align-stack");
3113 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign,
3115 CmdArgs.push_back(Args.MakeArgString("-mstackrealign"));
3118 if (Args.hasArg(options::OPT_mstack_alignment)) {
3119 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment);
3120 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment));
3122 // -mkernel implies -mstrict-align; don't add the redundant option.
3123 if (!KernelOrKext) {
3124 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
3125 options::OPT_munaligned_access)) {
3126 if (A->getOption().matches(options::OPT_mno_unaligned_access)) {
3127 CmdArgs.push_back("-backend-option");
3128 CmdArgs.push_back("-arm-strict-align");
3130 CmdArgs.push_back("-backend-option");
3131 CmdArgs.push_back("-arm-no-strict-align");
3136 if (Arg *A = Args.getLastArg(options::OPT_mrestrict_it,
3137 options::OPT_mno_restrict_it)) {
3138 if (A->getOption().matches(options::OPT_mrestrict_it)) {
3139 CmdArgs.push_back("-backend-option");
3140 CmdArgs.push_back("-arm-restrict-it");
3142 CmdArgs.push_back("-backend-option");
3143 CmdArgs.push_back("-arm-no-restrict-it");
3147 // Forward -f options with positive and negative forms; we translate
3149 if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ)) {
3150 StringRef fname = A->getValue();
3151 if (!llvm::sys::fs::exists(fname))
3152 D.Diag(diag::err_drv_no_such_file) << fname;
3154 A->render(Args, CmdArgs);
3157 if (Args.hasArg(options::OPT_mkernel)) {
3158 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType))
3159 CmdArgs.push_back("-fapple-kext");
3160 if (!Args.hasArg(options::OPT_fbuiltin))
3161 CmdArgs.push_back("-fno-builtin");
3162 Args.ClaimAllArgs(options::OPT_fno_builtin);
3164 // -fbuiltin is default.
3165 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
3166 CmdArgs.push_back("-fno-builtin");
3168 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
3169 options::OPT_fno_assume_sane_operator_new))
3170 CmdArgs.push_back("-fno-assume-sane-operator-new");
3172 // -fblocks=0 is default.
3173 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
3174 getToolChain().IsBlocksDefault()) ||
3175 (Args.hasArg(options::OPT_fgnu_runtime) &&
3176 Args.hasArg(options::OPT_fobjc_nonfragile_abi) &&
3177 !Args.hasArg(options::OPT_fno_blocks))) {
3178 CmdArgs.push_back("-fblocks");
3180 if (!Args.hasArg(options::OPT_fgnu_runtime) &&
3181 !getToolChain().hasBlocksRuntime())
3182 CmdArgs.push_back("-fblocks-runtime-optional");
3185 // -fmodules enables modules (off by default). However, for C++/Objective-C++,
3186 // users must also pass -fcxx-modules. The latter flag will disappear once the
3187 // modules implementation is solid for C++/Objective-C++ programs as well.
3188 bool HaveModules = false;
3189 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) {
3190 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules,
3191 options::OPT_fno_cxx_modules,
3193 if (AllowedInCXX || !types::isCXX(InputType)) {
3194 CmdArgs.push_back("-fmodules");
3199 // -fmodule-maps enables module map processing (off by default) for header
3200 // checking. It is implied by -fmodules.
3201 if (Args.hasFlag(options::OPT_fmodule_maps, options::OPT_fno_module_maps,
3203 CmdArgs.push_back("-fmodule-maps");
3206 // -fmodules-decluse checks that modules used are declared so (off by
3208 if (Args.hasFlag(options::OPT_fmodules_decluse,
3209 options::OPT_fno_modules_decluse,
3211 CmdArgs.push_back("-fmodules-decluse");
3214 // -fmodule-name specifies the module that is currently being built (or
3215 // used for header checking by -fmodule-maps).
3216 if (Arg *A = Args.getLastArg(options::OPT_fmodule_name)) {
3218 A->render(Args, CmdArgs);
3221 // -fmodule-map-file can be used to specify a file containing module
3223 if (Arg *A = Args.getLastArg(options::OPT_fmodule_map_file)) {
3225 A->render(Args, CmdArgs);
3228 // If a module path was provided, pass it along. Otherwise, use a temporary
3230 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) {
3233 A->render(Args, CmdArgs);
3235 } else if (HaveModules) {
3236 SmallString<128> DefaultModuleCache;
3237 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false,
3238 DefaultModuleCache);
3239 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang");
3240 llvm::sys::path::append(DefaultModuleCache, "ModuleCache");
3241 const char Arg[] = "-fmodules-cache-path=";
3242 DefaultModuleCache.insert(DefaultModuleCache.begin(),
3243 Arg, Arg + strlen(Arg));
3244 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache));
3247 // Pass through all -fmodules-ignore-macro arguments.
3248 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro);
3249 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval);
3250 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after);
3252 // -faccess-control is default.
3253 if (Args.hasFlag(options::OPT_fno_access_control,
3254 options::OPT_faccess_control,
3256 CmdArgs.push_back("-fno-access-control");
3258 // -felide-constructors is the default.
3259 if (Args.hasFlag(options::OPT_fno_elide_constructors,
3260 options::OPT_felide_constructors,
3262 CmdArgs.push_back("-fno-elide-constructors");
3264 // -frtti is default.
3265 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) ||
3267 CmdArgs.push_back("-fno-rtti");
3269 // -fno-rtti cannot usefully be combined with -fsanitize=vptr.
3270 if (Sanitize.sanitizesVptr()) {
3271 std::string NoRttiArg =
3272 Args.getLastArg(options::OPT_mkernel,
3273 options::OPT_fapple_kext,
3274 options::OPT_fno_rtti)->getAsString(Args);
3275 D.Diag(diag::err_drv_argument_not_allowed_with)
3276 << "-fsanitize=vptr" << NoRttiArg;
3280 // -fshort-enums=0 is default for all architectures except Hexagon.
3281 if (Args.hasFlag(options::OPT_fshort_enums,
3282 options::OPT_fno_short_enums,
3283 getToolChain().getArch() ==
3284 llvm::Triple::hexagon))
3285 CmdArgs.push_back("-fshort-enums");
3287 // -fsigned-char is default.
3288 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
3289 isSignedCharDefault(getToolChain().getTriple())))
3290 CmdArgs.push_back("-fno-signed-char");
3292 // -fthreadsafe-static is default.
3293 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
3294 options::OPT_fno_threadsafe_statics))
3295 CmdArgs.push_back("-fno-threadsafe-statics");
3297 // -fuse-cxa-atexit is default.
3299 options::OPT_fuse_cxa_atexit, options::OPT_fno_use_cxa_atexit,
3300 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin &&
3301 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 &&
3302 getToolChain().getArch() != llvm::Triple::hexagon &&
3303 getToolChain().getArch() != llvm::Triple::xcore) ||
3305 CmdArgs.push_back("-fno-use-cxa-atexit");
3307 // -fms-extensions=0 is default.
3308 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
3309 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
3310 CmdArgs.push_back("-fms-extensions");
3312 // -fms-compatibility=0 is default.
3313 if (Args.hasFlag(options::OPT_fms_compatibility,
3314 options::OPT_fno_ms_compatibility,
3315 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 &&
3316 Args.hasFlag(options::OPT_fms_extensions,
3317 options::OPT_fno_ms_extensions,
3319 CmdArgs.push_back("-fms-compatibility");
3321 // -fmsc-version=1700 is default.
3322 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
3323 getToolChain().getTriple().getOS() == llvm::Triple::Win32) ||
3324 Args.hasArg(options::OPT_fmsc_version)) {
3325 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version);
3326 if (msc_ver.empty())
3327 CmdArgs.push_back("-fmsc-version=1700");
3329 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver));
3333 // -fno-borland-extensions is default.
3334 if (Args.hasFlag(options::OPT_fborland_extensions,
3335 options::OPT_fno_borland_extensions, false))
3336 CmdArgs.push_back("-fborland-extensions");
3338 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL
3340 if (Args.hasFlag(options::OPT_fdelayed_template_parsing,
3341 options::OPT_fno_delayed_template_parsing,
3342 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
3343 CmdArgs.push_back("-fdelayed-template-parsing");
3345 // -fgnu-keywords default varies depending on language; only pass if
3347 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
3348 options::OPT_fno_gnu_keywords))
3349 A->render(Args, CmdArgs);
3351 if (Args.hasFlag(options::OPT_fgnu89_inline,
3352 options::OPT_fno_gnu89_inline,
3354 CmdArgs.push_back("-fgnu89-inline");
3356 if (Args.hasArg(options::OPT_fno_inline))
3357 CmdArgs.push_back("-fno-inline");
3359 if (Args.hasArg(options::OPT_fno_inline_functions))
3360 CmdArgs.push_back("-fno-inline-functions");
3362 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind);
3364 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
3365 // legacy is the default. Next runtime is always legacy dispatch and
3366 // -fno-objc-legacy-dispatch gets ignored silently.
3367 if (objcRuntime.isNonFragile() && !objcRuntime.isNeXTFamily()) {
3368 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
3369 options::OPT_fno_objc_legacy_dispatch,
3370 objcRuntime.isLegacyDispatchDefaultForArch(
3371 getToolChain().getArch()))) {
3372 if (getToolChain().UseObjCMixedDispatch())
3373 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
3375 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
3379 // When ObjectiveC legacy runtime is in effect on MacOSX,
3380 // turn on the option to do Array/Dictionary subscripting
3382 if (getToolChain().getTriple().getArch() == llvm::Triple::x86 &&
3383 getToolChain().getTriple().isMacOSX() &&
3384 !getToolChain().getTriple().isMacOSXVersionLT(10, 7) &&
3385 objcRuntime.getKind() == ObjCRuntime::FragileMacOSX &&
3386 objcRuntime.isNeXTFamily())
3387 CmdArgs.push_back("-fobjc-subscripting-legacy-runtime");
3389 // -fencode-extended-block-signature=1 is default.
3390 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) {
3391 CmdArgs.push_back("-fencode-extended-block-signature");
3394 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc.
3395 // NOTE: This logic is duplicated in ToolChains.cpp.
3396 bool ARC = isObjCAutoRefCount(Args);
3398 getToolChain().CheckObjCARC();
3400 CmdArgs.push_back("-fobjc-arc");
3402 // FIXME: It seems like this entire block, and several around it should be
3403 // wrapped in isObjC, but for now we just use it here as this is where it
3404 // was being used previously.
3405 if (types::isCXX(InputType) && types::isObjC(InputType)) {
3406 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
3407 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
3409 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
3412 // Allow the user to enable full exceptions code emission.
3413 // We define off for Objective-CC, on for Objective-C++.
3414 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions,
3415 options::OPT_fno_objc_arc_exceptions,
3416 /*default*/ types::isCXX(InputType)))
3417 CmdArgs.push_back("-fobjc-arc-exceptions");
3420 // -fobjc-infer-related-result-type is the default, except in the Objective-C
3422 if (rewriteKind != RK_None)
3423 CmdArgs.push_back("-fno-objc-infer-related-result-type");
3425 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
3426 // takes precedence.
3427 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
3429 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
3432 D.Diag(diag::err_drv_objc_gc_arr)
3433 << GCArg->getAsString(Args);
3434 } else if (getToolChain().SupportsObjCGC()) {
3435 GCArg->render(Args, CmdArgs);
3437 // FIXME: We should move this to a hard error.
3438 D.Diag(diag::warn_drv_objc_gc_unsupported)
3439 << GCArg->getAsString(Args);
3443 // Add exception args.
3444 addExceptionArgs(Args, InputType, getToolChain().getTriple(),
3445 KernelOrKext, objcRuntime, CmdArgs);
3447 if (getToolChain().UseSjLjExceptions())
3448 CmdArgs.push_back("-fsjlj-exceptions");
3450 // C++ "sane" operator new.
3451 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
3452 options::OPT_fno_assume_sane_operator_new))
3453 CmdArgs.push_back("-fno-assume-sane-operator-new");
3455 // -fconstant-cfstrings is default, and may be subject to argument translation
3457 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
3458 options::OPT_fno_constant_cfstrings) ||
3459 !Args.hasFlag(options::OPT_mconstant_cfstrings,
3460 options::OPT_mno_constant_cfstrings))
3461 CmdArgs.push_back("-fno-constant-cfstrings");
3463 // -fshort-wchar default varies depending on platform; only
3464 // pass if specified.
3465 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
3466 A->render(Args, CmdArgs);
3468 // -fno-pascal-strings is default, only pass non-default.
3469 if (Args.hasFlag(options::OPT_fpascal_strings,
3470 options::OPT_fno_pascal_strings,
3472 CmdArgs.push_back("-fpascal-strings");
3474 // Honor -fpack-struct= and -fpack-struct, if given. Note that
3475 // -fno-pack-struct doesn't apply to -fpack-struct=.
3476 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) {
3477 std::string PackStructStr = "-fpack-struct=";
3478 PackStructStr += A->getValue();
3479 CmdArgs.push_back(Args.MakeArgString(PackStructStr));
3480 } else if (Args.hasFlag(options::OPT_fpack_struct,
3481 options::OPT_fno_pack_struct, false)) {
3482 CmdArgs.push_back("-fpack-struct=1");
3485 if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) {
3486 if (!Args.hasArg(options::OPT_fcommon))
3487 CmdArgs.push_back("-fno-common");
3488 Args.ClaimAllArgs(options::OPT_fno_common);
3491 // -fcommon is default, only pass non-default.
3492 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
3493 CmdArgs.push_back("-fno-common");
3495 // -fsigned-bitfields is default, and clang doesn't yet support
3496 // -funsigned-bitfields.
3497 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
3498 options::OPT_funsigned_bitfields))
3499 D.Diag(diag::warn_drv_clang_unsupported)
3500 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
3502 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope.
3503 if (!Args.hasFlag(options::OPT_ffor_scope,
3504 options::OPT_fno_for_scope))
3505 D.Diag(diag::err_drv_clang_unsupported)
3506 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args);
3508 // -fcaret-diagnostics is default.
3509 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
3510 options::OPT_fno_caret_diagnostics, true))
3511 CmdArgs.push_back("-fno-caret-diagnostics");
3513 // -fdiagnostics-fixit-info is default, only pass non-default.
3514 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
3515 options::OPT_fno_diagnostics_fixit_info))
3516 CmdArgs.push_back("-fno-diagnostics-fixit-info");
3518 // Enable -fdiagnostics-show-option by default.
3519 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
3520 options::OPT_fno_diagnostics_show_option))
3521 CmdArgs.push_back("-fdiagnostics-show-option");
3524 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
3525 CmdArgs.push_back("-fdiagnostics-show-category");
3526 CmdArgs.push_back(A->getValue());
3530 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) {
3531 CmdArgs.push_back("-fdiagnostics-format");
3532 CmdArgs.push_back(A->getValue());
3535 if (Arg *A = Args.getLastArg(
3536 options::OPT_fdiagnostics_show_note_include_stack,
3537 options::OPT_fno_diagnostics_show_note_include_stack)) {
3538 if (A->getOption().matches(
3539 options::OPT_fdiagnostics_show_note_include_stack))
3540 CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
3542 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
3545 // Color diagnostics are the default, unless the terminal doesn't support
3547 // Support both clang's -f[no-]color-diagnostics and gcc's
3548 // -f[no-]diagnostics-colors[=never|always|auto].
3549 enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
3550 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
3552 const Option &O = (*it)->getOption();
3553 if (!O.matches(options::OPT_fcolor_diagnostics) &&
3554 !O.matches(options::OPT_fdiagnostics_color) &&
3555 !O.matches(options::OPT_fno_color_diagnostics) &&
3556 !O.matches(options::OPT_fno_diagnostics_color) &&
3557 !O.matches(options::OPT_fdiagnostics_color_EQ))
3561 if (O.matches(options::OPT_fcolor_diagnostics) ||
3562 O.matches(options::OPT_fdiagnostics_color)) {
3563 ShowColors = Colors_On;
3564 } else if (O.matches(options::OPT_fno_color_diagnostics) ||
3565 O.matches(options::OPT_fno_diagnostics_color)) {
3566 ShowColors = Colors_Off;
3568 assert(O.matches(options::OPT_fdiagnostics_color_EQ));
3569 StringRef value((*it)->getValue());
3570 if (value == "always")
3571 ShowColors = Colors_On;
3572 else if (value == "never")
3573 ShowColors = Colors_Off;
3574 else if (value == "auto")
3575 ShowColors = Colors_Auto;
3577 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3578 << ("-fdiagnostics-color=" + value).str();
3581 if (ShowColors == Colors_On ||
3582 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
3583 CmdArgs.push_back("-fcolor-diagnostics");
3585 if (Args.hasArg(options::OPT_fansi_escape_codes))
3586 CmdArgs.push_back("-fansi-escape-codes");
3588 if (!Args.hasFlag(options::OPT_fshow_source_location,
3589 options::OPT_fno_show_source_location))
3590 CmdArgs.push_back("-fno-show-source-location");
3592 if (!Args.hasFlag(options::OPT_fshow_column,
3593 options::OPT_fno_show_column,
3595 CmdArgs.push_back("-fno-show-column");
3597 if (!Args.hasFlag(options::OPT_fspell_checking,
3598 options::OPT_fno_spell_checking))
3599 CmdArgs.push_back("-fno-spell-checking");
3602 // -fno-asm-blocks is default.
3603 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks,
3605 CmdArgs.push_back("-fasm-blocks");
3607 // Enable vectorization per default according to the optimization level
3608 // selected. For optimization levels that want vectorization we use the alias
3609 // option to simplify the hasFlag logic.
3610 bool EnableVec = shouldEnableVectorizerAtOLevel(Args);
3611 OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group :
3612 options::OPT_fvectorize;
3613 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
3614 options::OPT_fno_vectorize, EnableVec))
3615 CmdArgs.push_back("-vectorize-loops");
3617 // -fslp-vectorize is default.
3618 if (Args.hasFlag(options::OPT_fslp_vectorize,
3619 options::OPT_fno_slp_vectorize, true))
3620 CmdArgs.push_back("-vectorize-slp");
3622 // -fno-slp-vectorize-aggressive is default.
3623 if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive,
3624 options::OPT_fno_slp_vectorize_aggressive, false))
3625 CmdArgs.push_back("-vectorize-slp-aggressive");
3627 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
3628 A->render(Args, CmdArgs);
3630 // -fdollars-in-identifiers default varies depending on platform and
3631 // language; only pass if specified.
3632 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
3633 options::OPT_fno_dollars_in_identifiers)) {
3634 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
3635 CmdArgs.push_back("-fdollars-in-identifiers");
3637 CmdArgs.push_back("-fno-dollars-in-identifiers");
3640 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
3641 // practical purposes.
3642 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
3643 options::OPT_fno_unit_at_a_time)) {
3644 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
3645 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
3648 if (Args.hasFlag(options::OPT_fapple_pragma_pack,
3649 options::OPT_fno_apple_pragma_pack, false))
3650 CmdArgs.push_back("-fapple-pragma-pack");
3652 // le32-specific flags:
3653 // -fno-math-builtin: clang should not convert math builtins to intrinsics
3655 if (getToolChain().getArch() == llvm::Triple::le32) {
3656 CmdArgs.push_back("-fno-math-builtin");
3659 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
3661 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
3663 if (getToolChain().getTriple().isOSDarwin() &&
3664 (getToolChain().getArch() == llvm::Triple::arm ||
3665 getToolChain().getArch() == llvm::Triple::thumb)) {
3666 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
3667 CmdArgs.push_back("-fno-builtin-strcat");
3668 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
3669 CmdArgs.push_back("-fno-builtin-strcpy");
3673 // Only allow -traditional or -traditional-cpp outside in preprocessing modes.
3674 if (Arg *A = Args.getLastArg(options::OPT_traditional,
3675 options::OPT_traditional_cpp)) {
3676 if (isa<PreprocessJobAction>(JA))
3677 CmdArgs.push_back("-traditional-cpp");
3679 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
3682 Args.AddLastArg(CmdArgs, options::OPT_dM);
3683 Args.AddLastArg(CmdArgs, options::OPT_dD);
3685 // Handle serialized diagnostics.
3686 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) {
3687 CmdArgs.push_back("-serialize-diagnostic-file");
3688 CmdArgs.push_back(Args.MakeArgString(A->getValue()));
3691 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
3692 CmdArgs.push_back("-fretain-comments-from-system-headers");
3694 // Forward -fcomment-block-commands to -cc1.
3695 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
3696 // Forward -fparse-all-comments to -cc1.
3697 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments);
3699 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
3701 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
3702 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
3703 ie = Args.filtered_end(); it != ie; ++it) {
3706 // We translate this by hand to the -cc1 argument, since nightly test uses
3707 // it and developers have been trained to spell it with -mllvm.
3708 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns")
3709 CmdArgs.push_back("-disable-llvm-optzns");
3711 (*it)->render(Args, CmdArgs);
3714 if (Output.getType() == types::TY_Dependencies) {
3715 // Handled with other dependency code.
3716 } else if (Output.isFilename()) {
3717 CmdArgs.push_back("-o");
3718 CmdArgs.push_back(Output.getFilename());
3720 assert(Output.isNothing() && "Invalid output.");
3723 for (InputInfoList::const_iterator
3724 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3725 const InputInfo &II = *it;
3726 CmdArgs.push_back("-x");
3727 if (Args.hasArg(options::OPT_rewrite_objc))
3728 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
3730 CmdArgs.push_back(types::getTypeName(II.getType()));
3731 if (II.isFilename())
3732 CmdArgs.push_back(II.getFilename());
3734 II.getInputArg().renderAsInput(Args, CmdArgs);
3737 Args.AddAllArgs(CmdArgs, options::OPT_undef);
3739 const char *Exec = getToolChain().getDriver().getClangProgramPath();
3741 // Optionally embed the -cc1 level arguments into the debug info, for build
3743 if (getToolChain().UseDwarfDebugFlags()) {
3744 ArgStringList OriginalArgs;
3745 for (ArgList::const_iterator it = Args.begin(),
3746 ie = Args.end(); it != ie; ++it)
3747 (*it)->render(Args, OriginalArgs);
3749 SmallString<256> Flags;
3751 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
3753 Flags += OriginalArgs[i];
3755 CmdArgs.push_back("-dwarf-debug-flags");
3756 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
3759 // Add the split debug info name to the command lines here so we
3760 // can propagate it to the backend.
3761 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) &&
3762 getToolChain().getTriple().isOSLinux() &&
3763 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA));
3764 const char *SplitDwarfOut;
3766 CmdArgs.push_back("-split-dwarf-file");
3767 SplitDwarfOut = SplitDebugName(Args, Inputs);
3768 CmdArgs.push_back(SplitDwarfOut);
3771 // Finally add the compile command to the compilation.
3772 if (Args.hasArg(options::OPT__SLASH_fallback)) {
3773 tools::visualstudio::Compile CL(getToolChain());
3774 Command *CLCommand = CL.GetCommand(C, JA, Output, Inputs, Args,
3776 C.addCommand(new FallbackCommand(JA, *this, Exec, CmdArgs, CLCommand));
3778 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
3782 // Handle the debug info splitting at object creation time if we're
3783 // creating an object.
3784 // TODO: Currently only works on linux with newer objcopy.
3785 if (SplitDwarf && !isa<CompileJobAction>(JA))
3786 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut);
3788 if (Arg *A = Args.getLastArg(options::OPT_pg))
3789 if (Args.hasArg(options::OPT_fomit_frame_pointer))
3790 D.Diag(diag::err_drv_argument_not_allowed_with)
3791 << "-fomit-frame-pointer" << A->getAsString(Args);
3793 // Claim some arguments which clang supports automatically.
3795 // -fpch-preprocess is used with gcc to add a special marker in the output to
3796 // include the PCH file. Clang's PTH solution is completely transparent, so we
3797 // do not need to deal with it at all.
3798 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
3800 // Claim some arguments which clang doesn't support, but we don't
3801 // care to warn the user about.
3802 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
3803 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
3805 // Disable warnings for clang -E -emit-llvm foo.c
3806 Args.ClaimAllArgs(options::OPT_emit_llvm);
3809 /// Add options related to the Objective-C runtime/ABI.
3811 /// Returns true if the runtime is non-fragile.
3812 ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args,
3813 ArgStringList &cmdArgs,
3814 RewriteKind rewriteKind) const {
3815 // Look for the controlling runtime option.
3816 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime,
3817 options::OPT_fgnu_runtime,
3818 options::OPT_fobjc_runtime_EQ);
3820 // Just forward -fobjc-runtime= to the frontend. This supercedes
3821 // options about fragility.
3823 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) {
3824 ObjCRuntime runtime;
3825 StringRef value = runtimeArg->getValue();
3826 if (runtime.tryParse(value)) {
3827 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime)
3831 runtimeArg->render(args, cmdArgs);
3835 // Otherwise, we'll need the ABI "version". Version numbers are
3836 // slightly confusing for historical reasons:
3837 // 1 - Traditional "fragile" ABI
3838 // 2 - Non-fragile ABI, version 1
3839 // 3 - Non-fragile ABI, version 2
3840 unsigned objcABIVersion = 1;
3841 // If -fobjc-abi-version= is present, use that to set the version.
3842 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
3843 StringRef value = abiArg->getValue();
3846 else if (value == "2")
3848 else if (value == "3")
3851 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3854 // Otherwise, determine if we are using the non-fragile ABI.
3855 bool nonFragileABIIsDefault =
3856 (rewriteKind == RK_NonFragile ||
3857 (rewriteKind == RK_None &&
3858 getToolChain().IsObjCNonFragileABIDefault()));
3859 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi,
3860 options::OPT_fno_objc_nonfragile_abi,
3861 nonFragileABIIsDefault)) {
3862 // Determine the non-fragile ABI version to use.
3863 #ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO
3864 unsigned nonFragileABIVersion = 1;
3866 unsigned nonFragileABIVersion = 2;
3869 if (Arg *abiArg = args.getLastArg(
3870 options::OPT_fobjc_nonfragile_abi_version_EQ)) {
3871 StringRef value = abiArg->getValue();
3873 nonFragileABIVersion = 1;
3874 else if (value == "2")
3875 nonFragileABIVersion = 2;
3877 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3881 objcABIVersion = 1 + nonFragileABIVersion;
3887 // We don't actually care about the ABI version other than whether
3888 // it's non-fragile.
3889 bool isNonFragile = objcABIVersion != 1;
3891 // If we have no runtime argument, ask the toolchain for its default runtime.
3892 // However, the rewriter only really supports the Mac runtime, so assume that.
3893 ObjCRuntime runtime;
3895 switch (rewriteKind) {
3897 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3900 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple());
3903 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3908 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) {
3909 // On Darwin, make this use the default behavior for the toolchain.
3910 if (getToolChain().getTriple().isOSDarwin()) {
3911 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile);
3913 // Otherwise, build for a generic macosx port.
3915 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple());
3920 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime));
3921 // Legacy behaviour is to target the gnustep runtime if we are i
3922 // non-fragile mode or the GCC runtime in fragile mode.
3924 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6));
3926 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple());
3929 cmdArgs.push_back(args.MakeArgString(
3930 "-fobjc-runtime=" + runtime.getAsString()));
3934 void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
3935 unsigned RTOptionID = options::OPT__SLASH_MT;
3937 if (Args.hasArg(options::OPT__SLASH_LDd))
3938 // The /LDd option implies /MTd. The dependent lib part can be overridden,
3939 // but defining _DEBUG is sticky.
3940 RTOptionID = options::OPT__SLASH_MTd;
3942 if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group))
3943 RTOptionID = A->getOption().getID();
3945 switch(RTOptionID) {
3946 case options::OPT__SLASH_MD:
3947 if (Args.hasArg(options::OPT__SLASH_LDd))
3948 CmdArgs.push_back("-D_DEBUG");
3949 CmdArgs.push_back("-D_MT");
3950 CmdArgs.push_back("-D_DLL");
3951 CmdArgs.push_back("--dependent-lib=msvcrt");
3953 case options::OPT__SLASH_MDd:
3954 CmdArgs.push_back("-D_DEBUG");
3955 CmdArgs.push_back("-D_MT");
3956 CmdArgs.push_back("-D_DLL");
3957 CmdArgs.push_back("--dependent-lib=msvcrtd");
3959 case options::OPT__SLASH_MT:
3960 if (Args.hasArg(options::OPT__SLASH_LDd))
3961 CmdArgs.push_back("-D_DEBUG");
3962 CmdArgs.push_back("-D_MT");
3963 CmdArgs.push_back("--dependent-lib=libcmt");
3965 case options::OPT__SLASH_MTd:
3966 CmdArgs.push_back("-D_DEBUG");
3967 CmdArgs.push_back("-D_MT");
3968 CmdArgs.push_back("--dependent-lib=libcmtd");
3971 llvm_unreachable("Unexpected option ID.");
3974 // This provides POSIX compatibility (maps 'open' to '_open'), which most
3975 // users want. The /Za flag to cl.exe turns this off, but it's not
3976 // implemented in clang.
3977 CmdArgs.push_back("--dependent-lib=oldnames");
3979 // FIXME: Make this default for the win32 triple.
3980 CmdArgs.push_back("-cxx-abi");
3981 CmdArgs.push_back("microsoft");
3983 if (Arg *A = Args.getLastArg(options::OPT_show_includes))
3984 A->render(Args, CmdArgs);
3986 if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) {
3987 CmdArgs.push_back("-fdiagnostics-format");
3988 if (Args.hasArg(options::OPT__SLASH_fallback))
3989 CmdArgs.push_back("msvc-fallback");
3991 CmdArgs.push_back("msvc");
3995 void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
3996 const InputInfo &Output,
3997 const InputInfoList &Inputs,
3998 const ArgList &Args,
3999 const char *LinkingOutput) const {
4000 ArgStringList CmdArgs;
4002 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4003 const InputInfo &Input = Inputs[0];
4005 // Don't warn about "clang -w -c foo.s"
4006 Args.ClaimAllArgs(options::OPT_w);
4007 // and "clang -emit-llvm -c foo.s"
4008 Args.ClaimAllArgs(options::OPT_emit_llvm);
4010 // Invoke ourselves in -cc1as mode.
4012 // FIXME: Implement custom jobs for internal actions.
4013 CmdArgs.push_back("-cc1as");
4015 // Add the "effective" target triple.
4016 CmdArgs.push_back("-triple");
4017 std::string TripleStr =
4018 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType());
4019 CmdArgs.push_back(Args.MakeArgString(TripleStr));
4021 // Set the output mode, we currently only expect to be used as a real
4023 CmdArgs.push_back("-filetype");
4024 CmdArgs.push_back("obj");
4026 // Set the main file name, so that debug info works even with
4027 // -save-temps or preprocessed assembly.
4028 CmdArgs.push_back("-main-file-name");
4029 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs));
4031 // Add the target cpu
4032 const llvm::Triple &Triple = getToolChain().getTriple();
4033 std::string CPU = getCPUName(Args, Triple);
4035 CmdArgs.push_back("-target-cpu");
4036 CmdArgs.push_back(Args.MakeArgString(CPU));
4039 // Add the target features
4040 const Driver &D = getToolChain().getDriver();
4041 getTargetFeatures(D, Triple, Args, CmdArgs);
4043 // Ignore explicit -force_cpusubtype_ALL option.
4044 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL);
4046 // Determine the original source input.
4047 const Action *SourceAction = &JA;
4048 while (SourceAction->getKind() != Action::InputClass) {
4049 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4050 SourceAction = SourceAction->getInputs()[0];
4053 // Forward -g and handle debug info related flags, assuming we are dealing
4054 // with an actual assembly file.
4055 if (SourceAction->getType() == types::TY_Asm ||
4056 SourceAction->getType() == types::TY_PP_Asm) {
4057 Args.ClaimAllArgs(options::OPT_g_Group);
4058 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
4059 if (!A->getOption().matches(options::OPT_g0))
4060 CmdArgs.push_back("-g");
4062 // Add the -fdebug-compilation-dir flag if needed.
4063 addDebugCompDirArg(Args, CmdArgs);
4065 // Set the AT_producer to the clang version when using the integrated
4066 // assembler on assembly source files.
4067 CmdArgs.push_back("-dwarf-debug-producer");
4068 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion()));
4071 // Optionally embed the -cc1as level arguments into the debug info, for build
4073 if (getToolChain().UseDwarfDebugFlags()) {
4074 ArgStringList OriginalArgs;
4075 for (ArgList::const_iterator it = Args.begin(),
4076 ie = Args.end(); it != ie; ++it)
4077 (*it)->render(Args, OriginalArgs);
4079 SmallString<256> Flags;
4080 const char *Exec = getToolChain().getDriver().getClangProgramPath();
4082 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
4084 Flags += OriginalArgs[i];
4086 CmdArgs.push_back("-dwarf-debug-flags");
4087 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
4090 // FIXME: Add -static support, once we have it.
4092 CollectArgsForIntegratedAssembler(C, Args, CmdArgs,
4093 getToolChain().getDriver());
4095 Args.AddAllArgs(CmdArgs, options::OPT_mllvm);
4097 assert(Output.isFilename() && "Unexpected lipo output.");
4098 CmdArgs.push_back("-o");
4099 CmdArgs.push_back(Output.getFilename());
4101 assert(Input.isFilename() && "Invalid input.");
4102 CmdArgs.push_back(Input.getFilename());
4104 const char *Exec = getToolChain().getDriver().getClangProgramPath();
4105 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4107 // Handle the debug info splitting at object creation time if we're
4108 // creating an object.
4109 // TODO: Currently only works on linux with newer objcopy.
4110 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
4111 getToolChain().getTriple().isOSLinux())
4112 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
4113 SplitDebugName(Args, Inputs));
4116 void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
4117 const InputInfo &Output,
4118 const InputInfoList &Inputs,
4119 const ArgList &Args,
4120 const char *LinkingOutput) const {
4121 const Driver &D = getToolChain().getDriver();
4122 ArgStringList CmdArgs;
4124 for (ArgList::const_iterator
4125 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
4127 if (forwardToGCC(A->getOption())) {
4128 // Don't forward any -g arguments to assembly steps.
4129 if (isa<AssembleJobAction>(JA) &&
4130 A->getOption().matches(options::OPT_g_Group))
4133 // Don't forward any -W arguments to assembly and link steps.
4134 if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
4135 A->getOption().matches(options::OPT_W_Group))
4138 // It is unfortunate that we have to claim here, as this means
4139 // we will basically never report anything interesting for
4140 // platforms using a generic gcc, even if we are just using gcc
4141 // to get to the assembler.
4143 A->render(Args, CmdArgs);
4147 RenderExtraToolArgs(JA, CmdArgs);
4149 // If using a driver driver, force the arch.
4150 llvm::Triple::ArchType Arch = getToolChain().getArch();
4151 if (getToolChain().getTriple().isOSDarwin()) {
4152 CmdArgs.push_back("-arch");
4154 // FIXME: Remove these special cases.
4155 if (Arch == llvm::Triple::ppc)
4156 CmdArgs.push_back("ppc");
4157 else if (Arch == llvm::Triple::ppc64)
4158 CmdArgs.push_back("ppc64");
4159 else if (Arch == llvm::Triple::ppc64le)
4160 CmdArgs.push_back("ppc64le");
4162 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName()));
4165 // Try to force gcc to match the tool chain we want, if we recognize
4168 // FIXME: The triple class should directly provide the information we want
4170 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
4171 CmdArgs.push_back("-m32");
4172 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 ||
4173 Arch == llvm::Triple::ppc64le)
4174 CmdArgs.push_back("-m64");
4176 if (Output.isFilename()) {
4177 CmdArgs.push_back("-o");
4178 CmdArgs.push_back(Output.getFilename());
4180 assert(Output.isNothing() && "Unexpected output");
4181 CmdArgs.push_back("-fsyntax-only");
4184 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4185 options::OPT_Xassembler);
4187 // Only pass -x if gcc will understand it; otherwise hope gcc
4188 // understands the suffix correctly. The main use case this would go
4189 // wrong in is for linker inputs if they happened to have an odd
4190 // suffix; really the only way to get this to happen is a command
4191 // like '-x foobar a.c' which will treat a.c like a linker input.
4193 // FIXME: For the linker case specifically, can we safely convert
4194 // inputs into '-Wl,' options?
4195 for (InputInfoList::const_iterator
4196 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4197 const InputInfo &II = *it;
4199 // Don't try to pass LLVM or AST inputs to a generic gcc.
4200 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
4201 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
4202 D.Diag(diag::err_drv_no_linker_llvm_support)
4203 << getToolChain().getTripleString();
4204 else if (II.getType() == types::TY_AST)
4205 D.Diag(diag::err_drv_no_ast_support)
4206 << getToolChain().getTripleString();
4207 else if (II.getType() == types::TY_ModuleFile)
4208 D.Diag(diag::err_drv_no_module_support)
4209 << getToolChain().getTripleString();
4211 if (types::canTypeBeUserSpecified(II.getType())) {
4212 CmdArgs.push_back("-x");
4213 CmdArgs.push_back(types::getTypeName(II.getType()));
4216 if (II.isFilename())
4217 CmdArgs.push_back(II.getFilename());
4219 const Arg &A = II.getInputArg();
4221 // Reverse translate some rewritten options.
4222 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
4223 CmdArgs.push_back("-lstdc++");
4227 // Don't render as input, we need gcc to do the translations.
4228 A.render(Args, CmdArgs);
4232 const std::string customGCCName = D.getCCCGenericGCCName();
4233 const char *GCCName;
4234 if (!customGCCName.empty())
4235 GCCName = customGCCName.c_str();
4236 else if (D.CCCIsCXX()) {
4242 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
4243 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4246 void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
4247 ArgStringList &CmdArgs) const {
4248 CmdArgs.push_back("-E");
4251 void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
4252 ArgStringList &CmdArgs) const {
4253 // The type is good enough.
4256 void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
4257 ArgStringList &CmdArgs) const {
4258 const Driver &D = getToolChain().getDriver();
4260 // If -flto, etc. are present then make sure not to force assembly output.
4261 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
4262 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
4263 CmdArgs.push_back("-c");
4265 if (JA.getType() != types::TY_PP_Asm)
4266 D.Diag(diag::err_drv_invalid_gcc_output_type)
4267 << getTypeName(JA.getType());
4269 CmdArgs.push_back("-S");
4273 void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
4274 ArgStringList &CmdArgs) const {
4275 CmdArgs.push_back("-c");
4278 void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
4279 ArgStringList &CmdArgs) const {
4280 // The types are (hopefully) good enough.
4283 // Hexagon tools start.
4284 void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA,
4285 ArgStringList &CmdArgs) const {
4288 void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4289 const InputInfo &Output,
4290 const InputInfoList &Inputs,
4291 const ArgList &Args,
4292 const char *LinkingOutput) const {
4294 const Driver &D = getToolChain().getDriver();
4295 ArgStringList CmdArgs;
4297 std::string MarchString = "-march=";
4298 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args);
4299 CmdArgs.push_back(Args.MakeArgString(MarchString));
4301 RenderExtraToolArgs(JA, CmdArgs);
4303 if (Output.isFilename()) {
4304 CmdArgs.push_back("-o");
4305 CmdArgs.push_back(Output.getFilename());
4307 assert(Output.isNothing() && "Unexpected output");
4308 CmdArgs.push_back("-fsyntax-only");
4311 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
4312 if (!SmallDataThreshold.empty())
4314 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
4316 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4317 options::OPT_Xassembler);
4319 // Only pass -x if gcc will understand it; otherwise hope gcc
4320 // understands the suffix correctly. The main use case this would go
4321 // wrong in is for linker inputs if they happened to have an odd
4322 // suffix; really the only way to get this to happen is a command
4323 // like '-x foobar a.c' which will treat a.c like a linker input.
4325 // FIXME: For the linker case specifically, can we safely convert
4326 // inputs into '-Wl,' options?
4327 for (InputInfoList::const_iterator
4328 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
4329 const InputInfo &II = *it;
4331 // Don't try to pass LLVM or AST inputs to a generic gcc.
4332 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
4333 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
4334 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
4335 << getToolChain().getTripleString();
4336 else if (II.getType() == types::TY_AST)
4337 D.Diag(clang::diag::err_drv_no_ast_support)
4338 << getToolChain().getTripleString();
4339 else if (II.getType() == types::TY_ModuleFile)
4340 D.Diag(diag::err_drv_no_module_support)
4341 << getToolChain().getTripleString();
4343 if (II.isFilename())
4344 CmdArgs.push_back(II.getFilename());
4346 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ?
4347 II.getInputArg().render(Args, CmdArgs);
4350 const char *GCCName = "hexagon-as";
4352 Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
4353 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4356 void hexagon::Link::RenderExtraToolArgs(const JobAction &JA,
4357 ArgStringList &CmdArgs) const {
4358 // The types are (hopefully) good enough.
4361 void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA,
4362 const InputInfo &Output,
4363 const InputInfoList &Inputs,
4364 const ArgList &Args,
4365 const char *LinkingOutput) const {
4367 const toolchains::Hexagon_TC& ToolChain =
4368 static_cast<const toolchains::Hexagon_TC&>(getToolChain());
4369 const Driver &D = ToolChain.getDriver();
4371 ArgStringList CmdArgs;
4373 //----------------------------------------------------------------------------
4375 //----------------------------------------------------------------------------
4376 bool hasStaticArg = Args.hasArg(options::OPT_static);
4377 bool buildingLib = Args.hasArg(options::OPT_shared);
4378 bool buildPIE = Args.hasArg(options::OPT_pie);
4379 bool incStdLib = !Args.hasArg(options::OPT_nostdlib);
4380 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles);
4381 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
4382 bool useShared = buildingLib && !hasStaticArg;
4384 //----------------------------------------------------------------------------
4385 // Silence warnings for various options
4386 //----------------------------------------------------------------------------
4388 Args.ClaimAllArgs(options::OPT_g_Group);
4389 Args.ClaimAllArgs(options::OPT_emit_llvm);
4390 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
4391 // handled somewhere else.
4392 Args.ClaimAllArgs(options::OPT_static_libgcc);
4394 //----------------------------------------------------------------------------
4396 //----------------------------------------------------------------------------
4397 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
4398 e = ToolChain.ExtraOpts.end();
4400 CmdArgs.push_back(i->c_str());
4402 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args);
4403 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString));
4406 CmdArgs.push_back("-shared");
4407 CmdArgs.push_back("-call_shared"); // should be the default, but doing as
4412 CmdArgs.push_back("-static");
4414 if (buildPIE && !buildingLib)
4415 CmdArgs.push_back("-pie");
4417 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args);
4418 if (!SmallDataThreshold.empty()) {
4420 Args.MakeArgString(std::string("-G") + SmallDataThreshold));
4423 //----------------------------------------------------------------------------
4425 //----------------------------------------------------------------------------
4426 CmdArgs.push_back("-o");
4427 CmdArgs.push_back(Output.getFilename());
4429 const std::string MarchSuffix = "/" + MarchString;
4430 const std::string G0Suffix = "/G0";
4431 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
4432 const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir)
4434 const std::string StartFilesDir = RootDir
4437 ? MarchG0Suffix : MarchSuffix);
4439 //----------------------------------------------------------------------------
4441 //----------------------------------------------------------------------------
4442 std::vector<std::string> oslibs;
4443 bool hasStandalone= false;
4445 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ),
4446 ie = Args.filtered_end(); it != ie; ++it) {
4448 oslibs.push_back((*it)->getValue());
4449 hasStandalone = hasStandalone || (oslibs.back() == "standalone");
4451 if (oslibs.empty()) {
4452 oslibs.push_back("standalone");
4453 hasStandalone = true;
4456 //----------------------------------------------------------------------------
4458 //----------------------------------------------------------------------------
4459 if (incStdLib && incStartFiles) {
4462 if (hasStandalone) {
4464 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o"));
4466 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o"));
4468 std::string initObj = useShared ? "/initS.o" : "/init.o";
4469 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj));
4472 //----------------------------------------------------------------------------
4473 // Library Search Paths
4474 //----------------------------------------------------------------------------
4475 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths();
4476 for (ToolChain::path_list::const_iterator
4477 i = LibPaths.begin(),
4481 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
4483 //----------------------------------------------------------------------------
4485 //----------------------------------------------------------------------------
4486 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
4487 Args.AddAllArgs(CmdArgs, options::OPT_e);
4488 Args.AddAllArgs(CmdArgs, options::OPT_s);
4489 Args.AddAllArgs(CmdArgs, options::OPT_t);
4490 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
4492 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
4494 //----------------------------------------------------------------------------
4496 //----------------------------------------------------------------------------
4497 if (incStdLib && incDefLibs) {
4499 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
4500 CmdArgs.push_back("-lm");
4503 CmdArgs.push_back("--start-group");
4506 for(std::vector<std::string>::iterator i = oslibs.begin(),
4507 e = oslibs.end(); i != e; ++i)
4508 CmdArgs.push_back(Args.MakeArgString("-l" + *i));
4509 CmdArgs.push_back("-lc");
4511 CmdArgs.push_back("-lgcc");
4513 CmdArgs.push_back("--end-group");
4516 //----------------------------------------------------------------------------
4518 //----------------------------------------------------------------------------
4519 if (incStdLib && incStartFiles) {
4520 std::string finiObj = useShared ? "/finiS.o" : "/fini.o";
4521 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj));
4524 std::string Linker = ToolChain.GetProgramPath("hexagon-ld");
4525 C.addCommand(new Command(JA, *this, Args.MakeArgString(Linker), CmdArgs));
4527 // Hexagon tools end.
4529 llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) {
4530 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
4531 // archs which Darwin doesn't use.
4533 // The matching this routine does is fairly pointless, since it is neither the
4534 // complete architecture list, nor a reasonable subset. The problem is that
4535 // historically the driver driver accepts this and also ties its -march=
4536 // handling to the architecture name, so we need to be careful before removing
4539 // This code must be kept in sync with Clang's Darwin specific argument
4542 return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
4543 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
4544 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
4545 .Case("ppc64", llvm::Triple::ppc64)
4546 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
4547 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
4549 .Cases("x86_64", "x86_64h", llvm::Triple::x86_64)
4550 // This is derived from the driver driver.
4551 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
4552 .Cases("armv7", "armv7em", "armv7f", "armv7k", "armv7m", llvm::Triple::arm)
4553 .Cases("armv7s", "xscale", llvm::Triple::arm)
4554 .Case("r600", llvm::Triple::r600)
4555 .Case("nvptx", llvm::Triple::nvptx)
4556 .Case("nvptx64", llvm::Triple::nvptx64)
4557 .Case("amdil", llvm::Triple::amdil)
4558 .Case("spir", llvm::Triple::spir)
4559 .Default(llvm::Triple::UnknownArch);
4562 const char *Clang::getBaseInputName(const ArgList &Args,
4563 const InputInfoList &Inputs) {
4564 return Args.MakeArgString(
4565 llvm::sys::path::filename(Inputs[0].getBaseInput()));
4568 const char *Clang::getBaseInputStem(const ArgList &Args,
4569 const InputInfoList &Inputs) {
4570 const char *Str = getBaseInputName(Args, Inputs);
4572 if (const char *End = strrchr(Str, '.'))
4573 return Args.MakeArgString(std::string(Str, End));
4578 const char *Clang::getDependencyFileName(const ArgList &Args,
4579 const InputInfoList &Inputs) {
4580 // FIXME: Think about this more.
4583 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
4584 std::string Str(OutputOpt->getValue());
4585 Res = Str.substr(0, Str.rfind('.'));
4587 Res = getBaseInputStem(Args, Inputs);
4589 return Args.MakeArgString(Res + ".d");
4592 void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
4593 const InputInfo &Output,
4594 const InputInfoList &Inputs,
4595 const ArgList &Args,
4596 const char *LinkingOutput) const {
4597 ArgStringList CmdArgs;
4599 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
4600 const InputInfo &Input = Inputs[0];
4602 // Determine the original source input.
4603 const Action *SourceAction = &JA;
4604 while (SourceAction->getKind() != Action::InputClass) {
4605 assert(!SourceAction->getInputs().empty() && "unexpected root action!");
4606 SourceAction = SourceAction->getInputs()[0];
4609 // If -no_integrated_as is used add -Q to the darwin assember driver to make
4610 // sure it runs its system assembler not clang's integrated assembler.
4611 // Applicable to darwin11+ and Xcode 4+. darwin<10 lacked integrated-as.
4612 // FIXME: at run-time detect assembler capabilities or rely on version
4613 // information forwarded by -target-assembler-version (future)
4614 if (Args.hasArg(options::OPT_no_integrated_as)) {
4615 const llvm::Triple& t(getToolChain().getTriple());
4616 if (!(t.isMacOSX() && t.isMacOSXVersionLT(10, 7)))
4617 CmdArgs.push_back("-Q");
4620 // Forward -g, assuming we are dealing with an actual assembly file.
4621 if (SourceAction->getType() == types::TY_Asm ||
4622 SourceAction->getType() == types::TY_PP_Asm) {
4623 if (Args.hasArg(options::OPT_gstabs))
4624 CmdArgs.push_back("--gstabs");
4625 else if (Args.hasArg(options::OPT_g_Group))
4626 CmdArgs.push_back("-g");
4629 // Derived from asm spec.
4630 AddDarwinArch(Args, CmdArgs);
4632 // Use -force_cpusubtype_ALL on x86 by default.
4633 if (getToolChain().getArch() == llvm::Triple::x86 ||
4634 getToolChain().getArch() == llvm::Triple::x86_64 ||
4635 Args.hasArg(options::OPT_force__cpusubtype__ALL))
4636 CmdArgs.push_back("-force_cpusubtype_ALL");
4638 if (getToolChain().getArch() != llvm::Triple::x86_64 &&
4639 (((Args.hasArg(options::OPT_mkernel) ||
4640 Args.hasArg(options::OPT_fapple_kext)) &&
4641 (!getDarwinToolChain().isTargetIPhoneOS() ||
4642 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) ||
4643 Args.hasArg(options::OPT_static)))
4644 CmdArgs.push_back("-static");
4646 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
4647 options::OPT_Xassembler);
4649 assert(Output.isFilename() && "Unexpected lipo output.");
4650 CmdArgs.push_back("-o");
4651 CmdArgs.push_back(Output.getFilename());
4653 assert(Input.isFilename() && "Invalid input.");
4654 CmdArgs.push_back(Input.getFilename());
4656 // asm_final spec is empty.
4659 Args.MakeArgString(getToolChain().GetProgramPath("as"));
4660 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4663 void darwin::DarwinTool::anchor() {}
4665 void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
4666 ArgStringList &CmdArgs) const {
4667 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
4669 // Derived from darwin_arch spec.
4670 CmdArgs.push_back("-arch");
4671 CmdArgs.push_back(Args.MakeArgString(ArchName));
4673 // FIXME: Is this needed anymore?
4674 if (ArchName == "arm")
4675 CmdArgs.push_back("-force_cpusubtype_ALL");
4678 bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const {
4679 // We only need to generate a temp path for LTO if we aren't compiling object
4680 // files. When compiling source files, we run 'dsymutil' after linking. We
4681 // don't run 'dsymutil' when compiling object files.
4682 for (InputInfoList::const_iterator
4683 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it)
4684 if (it->getType() != types::TY_Object)
4690 void darwin::Link::AddLinkArgs(Compilation &C,
4691 const ArgList &Args,
4692 ArgStringList &CmdArgs,
4693 const InputInfoList &Inputs) const {
4694 const Driver &D = getToolChain().getDriver();
4695 const toolchains::Darwin &DarwinTC = getDarwinToolChain();
4697 unsigned Version[3] = { 0, 0, 0 };
4698 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
4700 if (!Driver::GetReleaseVersion(A->getValue(), Version[0],
4701 Version[1], Version[2], HadExtra) ||
4703 D.Diag(diag::err_drv_invalid_version_number)
4704 << A->getAsString(Args);
4707 // Newer linkers support -demangle, pass it if supported and not disabled by
4709 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) {
4710 // Don't pass -demangle to ld_classic.
4712 // FIXME: This is a temporary workaround, ld should be handling this.
4713 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 &&
4714 Args.hasArg(options::OPT_static));
4715 if (getToolChain().getArch() == llvm::Triple::x86) {
4716 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker,
4717 options::OPT_Wl_COMMA),
4718 ie = Args.filtered_end(); it != ie; ++it) {
4720 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
4721 if (StringRef(A->getValue(i)) == "-kext")
4722 UsesLdClassic = true;
4726 CmdArgs.push_back("-demangle");
4729 if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137)
4730 CmdArgs.push_back("-export_dynamic");
4732 // If we are using LTO, then automatically create a temporary file path for
4733 // the linker to use, so that it's lifetime will extend past a possible
4735 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) {
4736 const char *TmpPath = C.getArgs().MakeArgString(
4737 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
4738 C.addTempFile(TmpPath);
4739 CmdArgs.push_back("-object_path_lto");
4740 CmdArgs.push_back(TmpPath);
4743 // Derived from the "link" spec.
4744 Args.AddAllArgs(CmdArgs, options::OPT_static);
4745 if (!Args.hasArg(options::OPT_static))
4746 CmdArgs.push_back("-dynamic");
4747 if (Args.hasArg(options::OPT_fgnu_runtime)) {
4748 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
4749 // here. How do we wish to handle such things?
4752 if (!Args.hasArg(options::OPT_dynamiclib)) {
4753 AddDarwinArch(Args, CmdArgs);
4754 // FIXME: Why do this only on this path?
4755 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
4757 Args.AddLastArg(CmdArgs, options::OPT_bundle);
4758 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
4759 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
4762 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
4763 (A = Args.getLastArg(options::OPT_current__version)) ||
4764 (A = Args.getLastArg(options::OPT_install__name)))
4765 D.Diag(diag::err_drv_argument_only_allowed_with)
4766 << A->getAsString(Args) << "-dynamiclib";
4768 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
4769 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
4770 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
4772 CmdArgs.push_back("-dylib");
4775 if ((A = Args.getLastArg(options::OPT_bundle)) ||
4776 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
4777 (A = Args.getLastArg(options::OPT_client__name)) ||
4778 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
4779 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
4780 (A = Args.getLastArg(options::OPT_private__bundle)))
4781 D.Diag(diag::err_drv_argument_not_allowed_with)
4782 << A->getAsString(Args) << "-dynamiclib";
4784 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
4785 "-dylib_compatibility_version");
4786 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
4787 "-dylib_current_version");
4789 AddDarwinArch(Args, CmdArgs);
4791 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
4792 "-dylib_install_name");
4795 Args.AddLastArg(CmdArgs, options::OPT_all__load);
4796 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
4797 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
4798 if (DarwinTC.isTargetIPhoneOS())
4799 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
4800 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
4801 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
4802 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
4803 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
4804 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
4805 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
4806 Args.AddAllArgs(CmdArgs, options::OPT_force__load);
4807 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
4808 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
4809 Args.AddAllArgs(CmdArgs, options::OPT_init);
4811 // Add the deployment target.
4812 VersionTuple TargetVersion = DarwinTC.getTargetVersion();
4814 // If we had an explicit -mios-simulator-version-min argument, honor that,
4815 // otherwise use the traditional deployment targets. We can't just check the
4816 // is-sim attribute because existing code follows this path, and the linker
4817 // may not handle the argument.
4819 // FIXME: We may be able to remove this, once we can verify no one depends on
4821 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
4822 CmdArgs.push_back("-ios_simulator_version_min");
4823 else if (DarwinTC.isTargetIPhoneOS())
4824 CmdArgs.push_back("-iphoneos_version_min");
4826 CmdArgs.push_back("-macosx_version_min");
4827 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
4829 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
4830 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
4831 Args.AddLastArg(CmdArgs, options::OPT_single__module);
4832 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
4833 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
4835 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
4836 options::OPT_fno_pie,
4837 options::OPT_fno_PIE)) {
4838 if (A->getOption().matches(options::OPT_fpie) ||
4839 A->getOption().matches(options::OPT_fPIE))
4840 CmdArgs.push_back("-pie");
4842 CmdArgs.push_back("-no_pie");
4845 Args.AddLastArg(CmdArgs, options::OPT_prebind);
4846 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
4847 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
4848 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
4849 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
4850 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
4851 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
4852 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
4853 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
4854 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
4855 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
4856 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
4857 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
4858 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
4859 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
4860 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
4862 // Give --sysroot= preference, over the Apple specific behavior to also use
4863 // --isysroot as the syslibroot.
4864 StringRef sysroot = C.getSysRoot();
4865 if (sysroot != "") {
4866 CmdArgs.push_back("-syslibroot");
4867 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
4868 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
4869 CmdArgs.push_back("-syslibroot");
4870 CmdArgs.push_back(A->getValue());
4873 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
4874 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
4875 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
4876 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
4877 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
4878 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
4879 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
4880 Args.AddAllArgs(CmdArgs, options::OPT_y);
4881 Args.AddLastArg(CmdArgs, options::OPT_w);
4882 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
4883 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
4884 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
4885 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
4886 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
4887 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
4888 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
4889 Args.AddLastArg(CmdArgs, options::OPT_whyload);
4890 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
4891 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
4892 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
4893 Args.AddLastArg(CmdArgs, options::OPT_Mach);
4896 void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
4897 const InputInfo &Output,
4898 const InputInfoList &Inputs,
4899 const ArgList &Args,
4900 const char *LinkingOutput) const {
4901 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
4903 // The logic here is derived from gcc's behavior; most of which
4904 // comes from specs (starting with link_command). Consult gcc for
4905 // more information.
4906 ArgStringList CmdArgs;
4908 /// Hack(tm) to ignore linking errors when we are doing ARC migration.
4909 if (Args.hasArg(options::OPT_ccc_arcmt_check,
4910 options::OPT_ccc_arcmt_migrate)) {
4911 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I)
4914 Args.MakeArgString(getToolChain().GetProgramPath("touch"));
4915 CmdArgs.push_back(Output.getFilename());
4916 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
4920 // I'm not sure why this particular decomposition exists in gcc, but
4921 // we follow suite for ease of comparison.
4922 AddLinkArgs(C, Args, CmdArgs, Inputs);
4924 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
4925 Args.AddAllArgs(CmdArgs, options::OPT_s);
4926 Args.AddAllArgs(CmdArgs, options::OPT_t);
4927 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
4928 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
4929 Args.AddLastArg(CmdArgs, options::OPT_e);
4930 Args.AddAllArgs(CmdArgs, options::OPT_r);
4932 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
4933 // members of static archive libraries which implement Objective-C classes or
4935 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
4936 CmdArgs.push_back("-ObjC");
4938 CmdArgs.push_back("-o");
4939 CmdArgs.push_back(Output.getFilename());
4941 if (!Args.hasArg(options::OPT_nostdlib) &&
4942 !Args.hasArg(options::OPT_nostartfiles)) {
4943 // Derived from startfile spec.
4944 if (Args.hasArg(options::OPT_dynamiclib)) {
4945 // Derived from darwin_dylib1 spec.
4946 if (getDarwinToolChain().isTargetIOSSimulator()) {
4947 // The simulator doesn't have a versioned crt1 file.
4948 CmdArgs.push_back("-ldylib1.o");
4949 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
4950 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4951 CmdArgs.push_back("-ldylib1.o");
4953 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
4954 CmdArgs.push_back("-ldylib1.o");
4955 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
4956 CmdArgs.push_back("-ldylib1.10.5.o");
4959 if (Args.hasArg(options::OPT_bundle)) {
4960 if (!Args.hasArg(options::OPT_static)) {
4961 // Derived from darwin_bundle1 spec.
4962 if (getDarwinToolChain().isTargetIOSSimulator()) {
4963 // The simulator doesn't have a versioned crt1 file.
4964 CmdArgs.push_back("-lbundle1.o");
4965 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
4966 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
4967 CmdArgs.push_back("-lbundle1.o");
4969 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
4970 CmdArgs.push_back("-lbundle1.o");
4974 if (Args.hasArg(options::OPT_pg) &&
4975 getToolChain().SupportsProfiling()) {
4976 if (Args.hasArg(options::OPT_static) ||
4977 Args.hasArg(options::OPT_object) ||
4978 Args.hasArg(options::OPT_preload)) {
4979 CmdArgs.push_back("-lgcrt0.o");
4981 CmdArgs.push_back("-lgcrt1.o");
4983 // darwin_crt2 spec is empty.
4985 // By default on OS X 10.8 and later, we don't link with a crt1.o
4986 // file and the linker knows to use _main as the entry point. But,
4987 // when compiling with -pg, we need to link with the gcrt1.o file,
4988 // so pass the -no_new_main option to tell the linker to use the
4989 // "start" symbol as the entry point.
4990 if (getDarwinToolChain().isTargetMacOS() &&
4991 !getDarwinToolChain().isMacosxVersionLT(10, 8))
4992 CmdArgs.push_back("-no_new_main");
4994 if (Args.hasArg(options::OPT_static) ||
4995 Args.hasArg(options::OPT_object) ||
4996 Args.hasArg(options::OPT_preload)) {
4997 CmdArgs.push_back("-lcrt0.o");
4999 // Derived from darwin_crt1 spec.
5000 if (getDarwinToolChain().isTargetIOSSimulator()) {
5001 // The simulator doesn't have a versioned crt1 file.
5002 CmdArgs.push_back("-lcrt1.o");
5003 } else if (getDarwinToolChain().isTargetIPhoneOS()) {
5004 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
5005 CmdArgs.push_back("-lcrt1.o");
5006 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0))
5007 CmdArgs.push_back("-lcrt1.3.1.o");
5009 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
5010 CmdArgs.push_back("-lcrt1.o");
5011 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
5012 CmdArgs.push_back("-lcrt1.10.5.o");
5013 else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
5014 CmdArgs.push_back("-lcrt1.10.6.o");
5016 // darwin_crt2 spec is empty.
5023 if (!getDarwinToolChain().isTargetIPhoneOS() &&
5024 Args.hasArg(options::OPT_shared_libgcc) &&
5025 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
5027 Args.MakeArgString(getToolChain().GetFilePath("crt3.o"));
5028 CmdArgs.push_back(Str);
5032 Args.AddAllArgs(CmdArgs, options::OPT_L);
5034 if (Args.hasArg(options::OPT_fopenmp))
5035 // This is more complicated in gcc...
5036 CmdArgs.push_back("-lgomp");
5038 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5040 if (isObjCRuntimeLinked(Args) &&
5041 !Args.hasArg(options::OPT_nostdlib) &&
5042 !Args.hasArg(options::OPT_nodefaultlibs)) {
5043 // Avoid linking compatibility stubs on i386 mac.
5044 if (!getDarwinToolChain().isTargetMacOS() ||
5045 getDarwinToolChain().getArch() != llvm::Triple::x86) {
5046 // If we don't have ARC or subscripting runtime support, link in the
5047 // runtime stubs. We have to do this *before* adding any of the normal
5048 // linker inputs so that its initializer gets run first.
5049 ObjCRuntime runtime =
5050 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true);
5051 // We use arclite library for both ARC and subscripting support.
5052 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) ||
5053 !runtime.hasSubscripting())
5054 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs);
5056 CmdArgs.push_back("-framework");
5057 CmdArgs.push_back("Foundation");
5059 CmdArgs.push_back("-lobjc");
5062 if (LinkingOutput) {
5063 CmdArgs.push_back("-arch_multiple");
5064 CmdArgs.push_back("-final_output");
5065 CmdArgs.push_back(LinkingOutput);
5068 if (Args.hasArg(options::OPT_fnested_functions))
5069 CmdArgs.push_back("-allow_stack_execute");
5071 if (!Args.hasArg(options::OPT_nostdlib) &&
5072 !Args.hasArg(options::OPT_nodefaultlibs)) {
5073 if (getToolChain().getDriver().CCCIsCXX())
5074 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5076 // link_ssp spec is empty.
5078 // Let the tool chain choose which runtime library to link.
5079 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
5082 if (!Args.hasArg(options::OPT_nostdlib) &&
5083 !Args.hasArg(options::OPT_nostartfiles)) {
5084 // endfile_spec is empty.
5087 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5088 Args.AddAllArgs(CmdArgs, options::OPT_F);
5091 Args.MakeArgString(getToolChain().GetLinkerPath());
5092 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5095 void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
5096 const InputInfo &Output,
5097 const InputInfoList &Inputs,
5098 const ArgList &Args,
5099 const char *LinkingOutput) const {
5100 ArgStringList CmdArgs;
5102 CmdArgs.push_back("-create");
5103 assert(Output.isFilename() && "Unexpected lipo output.");
5105 CmdArgs.push_back("-output");
5106 CmdArgs.push_back(Output.getFilename());
5108 for (InputInfoList::const_iterator
5109 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5110 const InputInfo &II = *it;
5111 assert(II.isFilename() && "Unexpected lipo input.");
5112 CmdArgs.push_back(II.getFilename());
5115 Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
5116 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5119 void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
5120 const InputInfo &Output,
5121 const InputInfoList &Inputs,
5122 const ArgList &Args,
5123 const char *LinkingOutput) const {
5124 ArgStringList CmdArgs;
5126 CmdArgs.push_back("-o");
5127 CmdArgs.push_back(Output.getFilename());
5129 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
5130 const InputInfo &Input = Inputs[0];
5131 assert(Input.isFilename() && "Unexpected dsymutil input.");
5132 CmdArgs.push_back(Input.getFilename());
5135 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
5136 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5139 void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
5140 const InputInfo &Output,
5141 const InputInfoList &Inputs,
5142 const ArgList &Args,
5143 const char *LinkingOutput) const {
5144 ArgStringList CmdArgs;
5145 CmdArgs.push_back("--verify");
5146 CmdArgs.push_back("--debug-info");
5147 CmdArgs.push_back("--eh-frame");
5148 CmdArgs.push_back("--quiet");
5150 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
5151 const InputInfo &Input = Inputs[0];
5152 assert(Input.isFilename() && "Unexpected verify input");
5154 // Grabbing the output of the earlier dsymutil run.
5155 CmdArgs.push_back(Input.getFilename());
5158 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
5159 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5162 void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5163 const InputInfo &Output,
5164 const InputInfoList &Inputs,
5165 const ArgList &Args,
5166 const char *LinkingOutput) const {
5167 ArgStringList CmdArgs;
5169 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5170 options::OPT_Xassembler);
5172 CmdArgs.push_back("-o");
5173 CmdArgs.push_back(Output.getFilename());
5175 for (InputInfoList::const_iterator
5176 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5177 const InputInfo &II = *it;
5178 CmdArgs.push_back(II.getFilename());
5182 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5183 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5187 void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA,
5188 const InputInfo &Output,
5189 const InputInfoList &Inputs,
5190 const ArgList &Args,
5191 const char *LinkingOutput) const {
5192 // FIXME: Find a real GCC, don't hard-code versions here
5193 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
5194 const llvm::Triple &T = getToolChain().getTriple();
5195 std::string LibPath = "/usr/lib/";
5196 llvm::Triple::ArchType Arch = T.getArch();
5198 case llvm::Triple::x86:
5200 ("i386-" + T.getVendorName() + "-" + T.getOSName()).str() + "/4.5.2/";
5202 case llvm::Triple::x86_64:
5203 GCCLibPath += ("i386-" + T.getVendorName() + "-" + T.getOSName()).str();
5204 GCCLibPath += "/4.5.2/amd64/";
5205 LibPath += "amd64/";
5208 llvm_unreachable("Unsupported architecture");
5211 ArgStringList CmdArgs;
5213 // Demangle C++ names in errors
5214 CmdArgs.push_back("-C");
5216 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5217 (!Args.hasArg(options::OPT_shared))) {
5218 CmdArgs.push_back("-e");
5219 CmdArgs.push_back("_start");
5222 if (Args.hasArg(options::OPT_static)) {
5223 CmdArgs.push_back("-Bstatic");
5224 CmdArgs.push_back("-dn");
5226 CmdArgs.push_back("-Bdynamic");
5227 if (Args.hasArg(options::OPT_shared)) {
5228 CmdArgs.push_back("-shared");
5230 CmdArgs.push_back("--dynamic-linker");
5231 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
5235 if (Output.isFilename()) {
5236 CmdArgs.push_back("-o");
5237 CmdArgs.push_back(Output.getFilename());
5239 assert(Output.isNothing() && "Invalid output.");
5242 if (!Args.hasArg(options::OPT_nostdlib) &&
5243 !Args.hasArg(options::OPT_nostartfiles)) {
5244 if (!Args.hasArg(options::OPT_shared)) {
5245 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
5246 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
5247 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
5248 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
5250 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
5251 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
5252 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
5254 if (getToolChain().getDriver().CCCIsCXX())
5255 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
5258 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
5260 Args.AddAllArgs(CmdArgs, options::OPT_L);
5261 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5262 Args.AddAllArgs(CmdArgs, options::OPT_e);
5263 Args.AddAllArgs(CmdArgs, options::OPT_r);
5265 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5267 if (!Args.hasArg(options::OPT_nostdlib) &&
5268 !Args.hasArg(options::OPT_nodefaultlibs)) {
5269 if (getToolChain().getDriver().CCCIsCXX())
5270 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5271 CmdArgs.push_back("-lgcc_s");
5272 if (!Args.hasArg(options::OPT_shared)) {
5273 CmdArgs.push_back("-lgcc");
5274 CmdArgs.push_back("-lc");
5275 CmdArgs.push_back("-lm");
5279 if (!Args.hasArg(options::OPT_nostdlib) &&
5280 !Args.hasArg(options::OPT_nostartfiles)) {
5281 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
5283 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
5285 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
5288 Args.MakeArgString(getToolChain().GetLinkerPath());
5289 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5292 void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5293 const InputInfo &Output,
5294 const InputInfoList &Inputs,
5295 const ArgList &Args,
5296 const char *LinkingOutput) const {
5297 ArgStringList CmdArgs;
5299 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5300 options::OPT_Xassembler);
5302 CmdArgs.push_back("-o");
5303 CmdArgs.push_back(Output.getFilename());
5305 for (InputInfoList::const_iterator
5306 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5307 const InputInfo &II = *it;
5308 CmdArgs.push_back(II.getFilename());
5312 Args.MakeArgString(getToolChain().GetProgramPath("gas"));
5313 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5316 void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
5317 const InputInfo &Output,
5318 const InputInfoList &Inputs,
5319 const ArgList &Args,
5320 const char *LinkingOutput) const {
5321 ArgStringList CmdArgs;
5323 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5324 (!Args.hasArg(options::OPT_shared))) {
5325 CmdArgs.push_back("-e");
5326 CmdArgs.push_back("_start");
5329 if (Args.hasArg(options::OPT_static)) {
5330 CmdArgs.push_back("-Bstatic");
5331 CmdArgs.push_back("-dn");
5333 // CmdArgs.push_back("--eh-frame-hdr");
5334 CmdArgs.push_back("-Bdynamic");
5335 if (Args.hasArg(options::OPT_shared)) {
5336 CmdArgs.push_back("-shared");
5338 CmdArgs.push_back("--dynamic-linker");
5339 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
5343 if (Output.isFilename()) {
5344 CmdArgs.push_back("-o");
5345 CmdArgs.push_back(Output.getFilename());
5347 assert(Output.isNothing() && "Invalid output.");
5350 if (!Args.hasArg(options::OPT_nostdlib) &&
5351 !Args.hasArg(options::OPT_nostartfiles)) {
5352 if (!Args.hasArg(options::OPT_shared)) {
5353 CmdArgs.push_back(Args.MakeArgString(
5354 getToolChain().GetFilePath("crt1.o")));
5355 CmdArgs.push_back(Args.MakeArgString(
5356 getToolChain().GetFilePath("crti.o")));
5357 CmdArgs.push_back(Args.MakeArgString(
5358 getToolChain().GetFilePath("crtbegin.o")));
5360 CmdArgs.push_back(Args.MakeArgString(
5361 getToolChain().GetFilePath("crti.o")));
5363 CmdArgs.push_back(Args.MakeArgString(
5364 getToolChain().GetFilePath("crtn.o")));
5367 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
5368 + getToolChain().getTripleString()
5371 Args.AddAllArgs(CmdArgs, options::OPT_L);
5372 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5373 Args.AddAllArgs(CmdArgs, options::OPT_e);
5375 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5377 if (!Args.hasArg(options::OPT_nostdlib) &&
5378 !Args.hasArg(options::OPT_nodefaultlibs)) {
5379 // FIXME: For some reason GCC passes -lgcc before adding
5380 // the default system libraries. Just mimic this for now.
5381 CmdArgs.push_back("-lgcc");
5383 if (Args.hasArg(options::OPT_pthread))
5384 CmdArgs.push_back("-pthread");
5385 if (!Args.hasArg(options::OPT_shared))
5386 CmdArgs.push_back("-lc");
5387 CmdArgs.push_back("-lgcc");
5390 if (!Args.hasArg(options::OPT_nostdlib) &&
5391 !Args.hasArg(options::OPT_nostartfiles)) {
5392 if (!Args.hasArg(options::OPT_shared))
5393 CmdArgs.push_back(Args.MakeArgString(
5394 getToolChain().GetFilePath("crtend.o")));
5397 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
5400 Args.MakeArgString(getToolChain().GetLinkerPath());
5401 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5404 void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5405 const InputInfo &Output,
5406 const InputInfoList &Inputs,
5407 const ArgList &Args,
5408 const char *LinkingOutput) const {
5409 ArgStringList CmdArgs;
5411 // When building 32-bit code on OpenBSD/amd64, we have to explicitly
5412 // instruct as in the base system to assemble 32-bit code.
5413 if (getToolChain().getArch() == llvm::Triple::x86)
5414 CmdArgs.push_back("--32");
5415 else if (getToolChain().getArch() == llvm::Triple::ppc) {
5416 CmdArgs.push_back("-mppc");
5417 CmdArgs.push_back("-many");
5418 } else if (getToolChain().getArch() == llvm::Triple::mips64 ||
5419 getToolChain().getArch() == llvm::Triple::mips64el) {
5422 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
5424 CmdArgs.push_back("-mabi");
5425 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
5427 if (getToolChain().getArch() == llvm::Triple::mips64)
5428 CmdArgs.push_back("-EB");
5430 CmdArgs.push_back("-EL");
5432 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5433 options::OPT_fpic, options::OPT_fno_pic,
5434 options::OPT_fPIE, options::OPT_fno_PIE,
5435 options::OPT_fpie, options::OPT_fno_pie);
5437 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5438 LastPICArg->getOption().matches(options::OPT_fpic) ||
5439 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5440 LastPICArg->getOption().matches(options::OPT_fpie))) {
5441 CmdArgs.push_back("-KPIC");
5445 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5446 options::OPT_Xassembler);
5448 CmdArgs.push_back("-o");
5449 CmdArgs.push_back(Output.getFilename());
5451 for (InputInfoList::const_iterator
5452 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5453 const InputInfo &II = *it;
5454 CmdArgs.push_back(II.getFilename());
5458 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5459 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5462 void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
5463 const InputInfo &Output,
5464 const InputInfoList &Inputs,
5465 const ArgList &Args,
5466 const char *LinkingOutput) const {
5467 const Driver &D = getToolChain().getDriver();
5468 ArgStringList CmdArgs;
5470 // Silence warning for "clang -g foo.o -o foo"
5471 Args.ClaimAllArgs(options::OPT_g_Group);
5472 // and "clang -emit-llvm foo.o -o foo"
5473 Args.ClaimAllArgs(options::OPT_emit_llvm);
5474 // and for "clang -w foo.o -o foo". Other warning options are already
5475 // handled somewhere else.
5476 Args.ClaimAllArgs(options::OPT_w);
5478 if (getToolChain().getArch() == llvm::Triple::mips64)
5479 CmdArgs.push_back("-EB");
5480 else if (getToolChain().getArch() == llvm::Triple::mips64el)
5481 CmdArgs.push_back("-EL");
5483 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5484 (!Args.hasArg(options::OPT_shared))) {
5485 CmdArgs.push_back("-e");
5486 CmdArgs.push_back("__start");
5489 if (Args.hasArg(options::OPT_static)) {
5490 CmdArgs.push_back("-Bstatic");
5492 if (Args.hasArg(options::OPT_rdynamic))
5493 CmdArgs.push_back("-export-dynamic");
5494 CmdArgs.push_back("--eh-frame-hdr");
5495 CmdArgs.push_back("-Bdynamic");
5496 if (Args.hasArg(options::OPT_shared)) {
5497 CmdArgs.push_back("-shared");
5499 CmdArgs.push_back("-dynamic-linker");
5500 CmdArgs.push_back("/usr/libexec/ld.so");
5504 if (Args.hasArg(options::OPT_nopie))
5505 CmdArgs.push_back("-nopie");
5507 if (Output.isFilename()) {
5508 CmdArgs.push_back("-o");
5509 CmdArgs.push_back(Output.getFilename());
5511 assert(Output.isNothing() && "Invalid output.");
5514 if (!Args.hasArg(options::OPT_nostdlib) &&
5515 !Args.hasArg(options::OPT_nostartfiles)) {
5516 if (!Args.hasArg(options::OPT_shared)) {
5517 if (Args.hasArg(options::OPT_pg))
5518 CmdArgs.push_back(Args.MakeArgString(
5519 getToolChain().GetFilePath("gcrt0.o")));
5521 CmdArgs.push_back(Args.MakeArgString(
5522 getToolChain().GetFilePath("crt0.o")));
5523 CmdArgs.push_back(Args.MakeArgString(
5524 getToolChain().GetFilePath("crtbegin.o")));
5526 CmdArgs.push_back(Args.MakeArgString(
5527 getToolChain().GetFilePath("crtbeginS.o")));
5531 std::string Triple = getToolChain().getTripleString();
5532 if (Triple.substr(0, 6) == "x86_64")
5533 Triple.replace(0, 6, "amd64");
5534 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
5537 Args.AddAllArgs(CmdArgs, options::OPT_L);
5538 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5539 Args.AddAllArgs(CmdArgs, options::OPT_e);
5540 Args.AddAllArgs(CmdArgs, options::OPT_s);
5541 Args.AddAllArgs(CmdArgs, options::OPT_t);
5542 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5543 Args.AddAllArgs(CmdArgs, options::OPT_r);
5545 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5547 if (!Args.hasArg(options::OPT_nostdlib) &&
5548 !Args.hasArg(options::OPT_nodefaultlibs)) {
5550 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5551 if (Args.hasArg(options::OPT_pg))
5552 CmdArgs.push_back("-lm_p");
5554 CmdArgs.push_back("-lm");
5557 // FIXME: For some reason GCC passes -lgcc before adding
5558 // the default system libraries. Just mimic this for now.
5559 CmdArgs.push_back("-lgcc");
5561 if (Args.hasArg(options::OPT_pthread)) {
5562 if (!Args.hasArg(options::OPT_shared) &&
5563 Args.hasArg(options::OPT_pg))
5564 CmdArgs.push_back("-lpthread_p");
5566 CmdArgs.push_back("-lpthread");
5569 if (!Args.hasArg(options::OPT_shared)) {
5570 if (Args.hasArg(options::OPT_pg))
5571 CmdArgs.push_back("-lc_p");
5573 CmdArgs.push_back("-lc");
5576 CmdArgs.push_back("-lgcc");
5579 if (!Args.hasArg(options::OPT_nostdlib) &&
5580 !Args.hasArg(options::OPT_nostartfiles)) {
5581 if (!Args.hasArg(options::OPT_shared))
5582 CmdArgs.push_back(Args.MakeArgString(
5583 getToolChain().GetFilePath("crtend.o")));
5585 CmdArgs.push_back(Args.MakeArgString(
5586 getToolChain().GetFilePath("crtendS.o")));
5590 Args.MakeArgString(getToolChain().GetLinkerPath());
5591 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5594 void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5595 const InputInfo &Output,
5596 const InputInfoList &Inputs,
5597 const ArgList &Args,
5598 const char *LinkingOutput) const {
5599 ArgStringList CmdArgs;
5601 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5602 options::OPT_Xassembler);
5604 CmdArgs.push_back("-o");
5605 CmdArgs.push_back(Output.getFilename());
5607 for (InputInfoList::const_iterator
5608 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5609 const InputInfo &II = *it;
5610 CmdArgs.push_back(II.getFilename());
5614 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5615 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5618 void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA,
5619 const InputInfo &Output,
5620 const InputInfoList &Inputs,
5621 const ArgList &Args,
5622 const char *LinkingOutput) const {
5623 const Driver &D = getToolChain().getDriver();
5624 ArgStringList CmdArgs;
5626 if ((!Args.hasArg(options::OPT_nostdlib)) &&
5627 (!Args.hasArg(options::OPT_shared))) {
5628 CmdArgs.push_back("-e");
5629 CmdArgs.push_back("__start");
5632 if (Args.hasArg(options::OPT_static)) {
5633 CmdArgs.push_back("-Bstatic");
5635 if (Args.hasArg(options::OPT_rdynamic))
5636 CmdArgs.push_back("-export-dynamic");
5637 CmdArgs.push_back("--eh-frame-hdr");
5638 CmdArgs.push_back("-Bdynamic");
5639 if (Args.hasArg(options::OPT_shared)) {
5640 CmdArgs.push_back("-shared");
5642 CmdArgs.push_back("-dynamic-linker");
5643 CmdArgs.push_back("/usr/libexec/ld.so");
5647 if (Output.isFilename()) {
5648 CmdArgs.push_back("-o");
5649 CmdArgs.push_back(Output.getFilename());
5651 assert(Output.isNothing() && "Invalid output.");
5654 if (!Args.hasArg(options::OPT_nostdlib) &&
5655 !Args.hasArg(options::OPT_nostartfiles)) {
5656 if (!Args.hasArg(options::OPT_shared)) {
5657 if (Args.hasArg(options::OPT_pg))
5658 CmdArgs.push_back(Args.MakeArgString(
5659 getToolChain().GetFilePath("gcrt0.o")));
5661 CmdArgs.push_back(Args.MakeArgString(
5662 getToolChain().GetFilePath("crt0.o")));
5663 CmdArgs.push_back(Args.MakeArgString(
5664 getToolChain().GetFilePath("crtbegin.o")));
5666 CmdArgs.push_back(Args.MakeArgString(
5667 getToolChain().GetFilePath("crtbeginS.o")));
5671 Args.AddAllArgs(CmdArgs, options::OPT_L);
5672 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5673 Args.AddAllArgs(CmdArgs, options::OPT_e);
5675 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
5677 if (!Args.hasArg(options::OPT_nostdlib) &&
5678 !Args.hasArg(options::OPT_nodefaultlibs)) {
5680 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
5681 if (Args.hasArg(options::OPT_pg))
5682 CmdArgs.push_back("-lm_p");
5684 CmdArgs.push_back("-lm");
5687 if (Args.hasArg(options::OPT_pthread)) {
5688 if (!Args.hasArg(options::OPT_shared) &&
5689 Args.hasArg(options::OPT_pg))
5690 CmdArgs.push_back("-lpthread_p");
5692 CmdArgs.push_back("-lpthread");
5695 if (!Args.hasArg(options::OPT_shared)) {
5696 if (Args.hasArg(options::OPT_pg))
5697 CmdArgs.push_back("-lc_p");
5699 CmdArgs.push_back("-lc");
5703 switch (getToolChain().getTriple().getArch()) {
5704 case llvm::Triple::arm:
5707 case llvm::Triple::x86:
5710 case llvm::Triple::x86_64:
5714 llvm_unreachable("Unsupported architecture");
5716 CmdArgs.push_back(Args.MakeArgString("-lclang_rt." + MyArch));
5719 if (!Args.hasArg(options::OPT_nostdlib) &&
5720 !Args.hasArg(options::OPT_nostartfiles)) {
5721 if (!Args.hasArg(options::OPT_shared))
5722 CmdArgs.push_back(Args.MakeArgString(
5723 getToolChain().GetFilePath("crtend.o")));
5725 CmdArgs.push_back(Args.MakeArgString(
5726 getToolChain().GetFilePath("crtendS.o")));
5730 Args.MakeArgString(getToolChain().GetLinkerPath());
5731 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5734 void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
5735 const InputInfo &Output,
5736 const InputInfoList &Inputs,
5737 const ArgList &Args,
5738 const char *LinkingOutput) const {
5739 ArgStringList CmdArgs;
5741 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5742 // instruct as in the base system to assemble 32-bit code.
5743 if (getToolChain().getArch() == llvm::Triple::x86)
5744 CmdArgs.push_back("--32");
5745 else if (getToolChain().getArch() == llvm::Triple::ppc)
5746 CmdArgs.push_back("-a32");
5747 else if (getToolChain().getArch() == llvm::Triple::mips ||
5748 getToolChain().getArch() == llvm::Triple::mipsel ||
5749 getToolChain().getArch() == llvm::Triple::mips64 ||
5750 getToolChain().getArch() == llvm::Triple::mips64el) {
5753 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
5755 CmdArgs.push_back("-march");
5756 CmdArgs.push_back(CPUName.data());
5758 CmdArgs.push_back("-mabi");
5759 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
5761 if (getToolChain().getArch() == llvm::Triple::mips ||
5762 getToolChain().getArch() == llvm::Triple::mips64)
5763 CmdArgs.push_back("-EB");
5765 CmdArgs.push_back("-EL");
5767 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5768 options::OPT_fpic, options::OPT_fno_pic,
5769 options::OPT_fPIE, options::OPT_fno_PIE,
5770 options::OPT_fpie, options::OPT_fno_pie);
5772 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5773 LastPICArg->getOption().matches(options::OPT_fpic) ||
5774 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5775 LastPICArg->getOption().matches(options::OPT_fpie))) {
5776 CmdArgs.push_back("-KPIC");
5778 } else if (getToolChain().getArch() == llvm::Triple::arm ||
5779 getToolChain().getArch() == llvm::Triple::thumb) {
5780 CmdArgs.push_back("-mfpu=softvfp");
5781 switch(getToolChain().getTriple().getEnvironment()) {
5782 case llvm::Triple::GNUEABI:
5783 case llvm::Triple::EABI:
5784 CmdArgs.push_back("-meabi=5");
5788 CmdArgs.push_back("-matpcs");
5790 } else if (getToolChain().getArch() == llvm::Triple::sparc ||
5791 getToolChain().getArch() == llvm::Triple::sparcv9) {
5792 if (getToolChain().getArch() == llvm::Triple::sparc)
5793 CmdArgs.push_back("-Av8plusa");
5795 CmdArgs.push_back("-Av9a");
5797 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
5798 options::OPT_fpic, options::OPT_fno_pic,
5799 options::OPT_fPIE, options::OPT_fno_PIE,
5800 options::OPT_fpie, options::OPT_fno_pie);
5802 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
5803 LastPICArg->getOption().matches(options::OPT_fpic) ||
5804 LastPICArg->getOption().matches(options::OPT_fPIE) ||
5805 LastPICArg->getOption().matches(options::OPT_fpie))) {
5806 CmdArgs.push_back("-KPIC");
5810 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
5811 options::OPT_Xassembler);
5813 CmdArgs.push_back("-o");
5814 CmdArgs.push_back(Output.getFilename());
5816 for (InputInfoList::const_iterator
5817 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
5818 const InputInfo &II = *it;
5819 CmdArgs.push_back(II.getFilename());
5823 Args.MakeArgString(getToolChain().GetProgramPath("as"));
5824 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
5827 void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
5828 const InputInfo &Output,
5829 const InputInfoList &Inputs,
5830 const ArgList &Args,
5831 const char *LinkingOutput) const {
5832 const toolchains::FreeBSD& ToolChain =
5833 static_cast<const toolchains::FreeBSD&>(getToolChain());
5834 const Driver &D = ToolChain.getDriver();
5835 ArgStringList CmdArgs;
5837 // Silence warning for "clang -g foo.o -o foo"
5838 Args.ClaimAllArgs(options::OPT_g_Group);
5839 // and "clang -emit-llvm foo.o -o foo"
5840 Args.ClaimAllArgs(options::OPT_emit_llvm);
5841 // and for "clang -w foo.o -o foo". Other warning options are already
5842 // handled somewhere else.
5843 Args.ClaimAllArgs(options::OPT_w);
5845 if (!D.SysRoot.empty())
5846 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
5848 if (Args.hasArg(options::OPT_pie))
5849 CmdArgs.push_back("-pie");
5851 if (Args.hasArg(options::OPT_static)) {
5852 CmdArgs.push_back("-Bstatic");
5854 if (Args.hasArg(options::OPT_rdynamic))
5855 CmdArgs.push_back("-export-dynamic");
5856 CmdArgs.push_back("--eh-frame-hdr");
5857 if (Args.hasArg(options::OPT_shared)) {
5858 CmdArgs.push_back("-Bshareable");
5860 CmdArgs.push_back("-dynamic-linker");
5861 CmdArgs.push_back("/libexec/ld-elf.so.1");
5863 if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
5864 llvm::Triple::ArchType Arch = ToolChain.getArch();
5865 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
5866 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
5867 CmdArgs.push_back("--hash-style=both");
5870 CmdArgs.push_back("--enable-new-dtags");
5873 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
5874 // instruct ld in the base system to link 32-bit code.
5875 if (ToolChain.getArch() == llvm::Triple::x86) {
5876 CmdArgs.push_back("-m");
5877 CmdArgs.push_back("elf_i386_fbsd");
5880 if (ToolChain.getArch() == llvm::Triple::ppc) {
5881 CmdArgs.push_back("-m");
5882 CmdArgs.push_back("elf32ppc_fbsd");
5885 if (Output.isFilename()) {
5886 CmdArgs.push_back("-o");
5887 CmdArgs.push_back(Output.getFilename());
5889 assert(Output.isNothing() && "Invalid output.");
5892 if (!Args.hasArg(options::OPT_nostdlib) &&
5893 !Args.hasArg(options::OPT_nostartfiles)) {
5894 const char *crt1 = NULL;
5895 if (!Args.hasArg(options::OPT_shared)) {
5896 if (Args.hasArg(options::OPT_pg))
5898 else if (Args.hasArg(options::OPT_pie))
5904 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
5906 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
5908 const char *crtbegin = NULL;
5909 if (Args.hasArg(options::OPT_static))
5910 crtbegin = "crtbeginT.o";
5911 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
5912 crtbegin = "crtbeginS.o";
5914 crtbegin = "crtbegin.o";
5916 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
5919 Args.AddAllArgs(CmdArgs, options::OPT_L);
5920 const ToolChain::path_list Paths = ToolChain.getFilePaths();
5921 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
5923 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
5924 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
5925 Args.AddAllArgs(CmdArgs, options::OPT_e);
5926 Args.AddAllArgs(CmdArgs, options::OPT_s);
5927 Args.AddAllArgs(CmdArgs, options::OPT_t);
5928 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
5929 Args.AddAllArgs(CmdArgs, options::OPT_r);
5931 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
5932 // as gold requires -plugin to come before any -plugin-opt that -Wl might
5934 if (D.IsUsingLTO(Args)) {
5935 CmdArgs.push_back("-plugin");
5936 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
5937 CmdArgs.push_back(Args.MakeArgString(Plugin));
5939 // Try to pass driver level flags relevant to LTO code generation down to
5942 // Handle flags for selecting CPU variants.
5943 std::string CPU = getCPUName(Args, ToolChain.getTriple());
5946 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
5951 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
5953 if (!Args.hasArg(options::OPT_nostdlib) &&
5954 !Args.hasArg(options::OPT_nodefaultlibs)) {
5956 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
5957 if (Args.hasArg(options::OPT_pg))
5958 CmdArgs.push_back("-lm_p");
5960 CmdArgs.push_back("-lm");
5962 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
5963 // the default system libraries. Just mimic this for now.
5964 if (Args.hasArg(options::OPT_pg))
5965 CmdArgs.push_back("-lgcc_p");
5967 CmdArgs.push_back("-lgcc");
5968 if (Args.hasArg(options::OPT_static)) {
5969 CmdArgs.push_back("-lgcc_eh");
5970 } else if (Args.hasArg(options::OPT_pg)) {
5971 CmdArgs.push_back("-lgcc_eh_p");
5973 CmdArgs.push_back("--as-needed");
5974 CmdArgs.push_back("-lgcc_s");
5975 CmdArgs.push_back("--no-as-needed");
5978 if (Args.hasArg(options::OPT_pthread)) {
5979 if (Args.hasArg(options::OPT_pg))
5980 CmdArgs.push_back("-lpthread_p");
5982 CmdArgs.push_back("-lpthread");
5985 if (Args.hasArg(options::OPT_pg)) {
5986 if (Args.hasArg(options::OPT_shared))
5987 CmdArgs.push_back("-lc");
5989 CmdArgs.push_back("-lc_p");
5990 CmdArgs.push_back("-lgcc_p");
5992 CmdArgs.push_back("-lc");
5993 CmdArgs.push_back("-lgcc");
5996 if (Args.hasArg(options::OPT_static)) {
5997 CmdArgs.push_back("-lgcc_eh");
5998 } else if (Args.hasArg(options::OPT_pg)) {
5999 CmdArgs.push_back("-lgcc_eh_p");
6001 CmdArgs.push_back("--as-needed");
6002 CmdArgs.push_back("-lgcc_s");
6003 CmdArgs.push_back("--no-as-needed");
6007 if (!Args.hasArg(options::OPT_nostdlib) &&
6008 !Args.hasArg(options::OPT_nostartfiles)) {
6009 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
6010 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
6012 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
6013 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
6016 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple());
6019 Args.MakeArgString(ToolChain.GetLinkerPath());
6020 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6023 void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6024 const InputInfo &Output,
6025 const InputInfoList &Inputs,
6026 const ArgList &Args,
6027 const char *LinkingOutput) const {
6028 ArgStringList CmdArgs;
6030 // When building 32-bit code on NetBSD/amd64, we have to explicitly
6031 // instruct as in the base system to assemble 32-bit code.
6032 if (getToolChain().getArch() == llvm::Triple::x86)
6033 CmdArgs.push_back("--32");
6035 // Pass the target CPU to GNU as for ARM, since the source code might
6036 // not have the correct .cpu annotation.
6037 if (getToolChain().getArch() == llvm::Triple::arm) {
6038 std::string MArch(getARMTargetCPU(Args, getToolChain().getTriple()));
6039 CmdArgs.push_back(Args.MakeArgString("-mcpu=" + MArch));
6042 if (getToolChain().getArch() == llvm::Triple::mips ||
6043 getToolChain().getArch() == llvm::Triple::mipsel ||
6044 getToolChain().getArch() == llvm::Triple::mips64 ||
6045 getToolChain().getArch() == llvm::Triple::mips64el) {
6048 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
6050 CmdArgs.push_back("-march");
6051 CmdArgs.push_back(CPUName.data());
6053 CmdArgs.push_back("-mabi");
6054 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
6056 if (getToolChain().getArch() == llvm::Triple::mips ||
6057 getToolChain().getArch() == llvm::Triple::mips64)
6058 CmdArgs.push_back("-EB");
6060 CmdArgs.push_back("-EL");
6062 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
6063 options::OPT_fpic, options::OPT_fno_pic,
6064 options::OPT_fPIE, options::OPT_fno_PIE,
6065 options::OPT_fpie, options::OPT_fno_pie);
6067 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
6068 LastPICArg->getOption().matches(options::OPT_fpic) ||
6069 LastPICArg->getOption().matches(options::OPT_fPIE) ||
6070 LastPICArg->getOption().matches(options::OPT_fpie))) {
6071 CmdArgs.push_back("-KPIC");
6075 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6076 options::OPT_Xassembler);
6078 CmdArgs.push_back("-o");
6079 CmdArgs.push_back(Output.getFilename());
6081 for (InputInfoList::const_iterator
6082 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6083 const InputInfo &II = *it;
6084 CmdArgs.push_back(II.getFilename());
6087 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
6088 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6091 void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
6092 const InputInfo &Output,
6093 const InputInfoList &Inputs,
6094 const ArgList &Args,
6095 const char *LinkingOutput) const {
6096 const Driver &D = getToolChain().getDriver();
6097 ArgStringList CmdArgs;
6099 if (!D.SysRoot.empty())
6100 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6102 if (Args.hasArg(options::OPT_static)) {
6103 CmdArgs.push_back("-Bstatic");
6105 if (Args.hasArg(options::OPT_rdynamic))
6106 CmdArgs.push_back("-export-dynamic");
6107 CmdArgs.push_back("--eh-frame-hdr");
6108 if (Args.hasArg(options::OPT_shared)) {
6109 CmdArgs.push_back("-Bshareable");
6111 CmdArgs.push_back("-dynamic-linker");
6112 CmdArgs.push_back("/libexec/ld.elf_so");
6116 // When building 32-bit code on NetBSD/amd64, we have to explicitly
6117 // instruct ld in the base system to link 32-bit code.
6118 if (getToolChain().getArch() == llvm::Triple::x86) {
6119 CmdArgs.push_back("-m");
6120 CmdArgs.push_back("elf_i386");
6123 if (Output.isFilename()) {
6124 CmdArgs.push_back("-o");
6125 CmdArgs.push_back(Output.getFilename());
6127 assert(Output.isNothing() && "Invalid output.");
6130 if (!Args.hasArg(options::OPT_nostdlib) &&
6131 !Args.hasArg(options::OPT_nostartfiles)) {
6132 if (!Args.hasArg(options::OPT_shared)) {
6133 CmdArgs.push_back(Args.MakeArgString(
6134 getToolChain().GetFilePath("crt0.o")));
6135 CmdArgs.push_back(Args.MakeArgString(
6136 getToolChain().GetFilePath("crti.o")));
6137 CmdArgs.push_back(Args.MakeArgString(
6138 getToolChain().GetFilePath("crtbegin.o")));
6140 CmdArgs.push_back(Args.MakeArgString(
6141 getToolChain().GetFilePath("crti.o")));
6142 CmdArgs.push_back(Args.MakeArgString(
6143 getToolChain().GetFilePath("crtbeginS.o")));
6147 Args.AddAllArgs(CmdArgs, options::OPT_L);
6148 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6149 Args.AddAllArgs(CmdArgs, options::OPT_e);
6150 Args.AddAllArgs(CmdArgs, options::OPT_s);
6151 Args.AddAllArgs(CmdArgs, options::OPT_t);
6152 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
6153 Args.AddAllArgs(CmdArgs, options::OPT_r);
6155 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6157 unsigned Major, Minor, Micro;
6158 getToolChain().getTriple().getOSVersion(Major, Minor, Micro);
6159 bool useLibgcc = true;
6160 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 23) || Major == 0) {
6161 if (getToolChain().getArch() == llvm::Triple::x86 ||
6162 getToolChain().getArch() == llvm::Triple::x86_64)
6166 if (!Args.hasArg(options::OPT_nostdlib) &&
6167 !Args.hasArg(options::OPT_nodefaultlibs)) {
6169 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
6170 CmdArgs.push_back("-lm");
6172 if (Args.hasArg(options::OPT_pthread))
6173 CmdArgs.push_back("-lpthread");
6174 CmdArgs.push_back("-lc");
6177 if (Args.hasArg(options::OPT_static)) {
6178 // libgcc_eh depends on libc, so resolve as much as possible,
6179 // pull in any new requirements from libc and then get the rest
6181 CmdArgs.push_back("-lgcc_eh");
6182 CmdArgs.push_back("-lc");
6183 CmdArgs.push_back("-lgcc");
6185 CmdArgs.push_back("-lgcc");
6186 CmdArgs.push_back("--as-needed");
6187 CmdArgs.push_back("-lgcc_s");
6188 CmdArgs.push_back("--no-as-needed");
6193 if (!Args.hasArg(options::OPT_nostdlib) &&
6194 !Args.hasArg(options::OPT_nostartfiles)) {
6195 if (!Args.hasArg(options::OPT_shared))
6196 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
6199 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
6201 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(
6205 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
6207 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
6208 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6211 void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6212 const InputInfo &Output,
6213 const InputInfoList &Inputs,
6214 const ArgList &Args,
6215 const char *LinkingOutput) const {
6216 ArgStringList CmdArgs;
6217 bool NeedsKPIC = false;
6219 // Add --32/--64 to make sure we get the format we want.
6220 // This is incomplete
6221 if (getToolChain().getArch() == llvm::Triple::x86) {
6222 CmdArgs.push_back("--32");
6223 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
6224 CmdArgs.push_back("--64");
6225 } else if (getToolChain().getArch() == llvm::Triple::ppc) {
6226 CmdArgs.push_back("-a32");
6227 CmdArgs.push_back("-mppc");
6228 CmdArgs.push_back("-many");
6229 } else if (getToolChain().getArch() == llvm::Triple::ppc64) {
6230 CmdArgs.push_back("-a64");
6231 CmdArgs.push_back("-mppc64");
6232 CmdArgs.push_back("-many");
6233 } else if (getToolChain().getArch() == llvm::Triple::ppc64le) {
6234 CmdArgs.push_back("-a64");
6235 CmdArgs.push_back("-mppc64le");
6236 CmdArgs.push_back("-many");
6237 } else if (getToolChain().getArch() == llvm::Triple::sparc) {
6238 CmdArgs.push_back("-32");
6239 CmdArgs.push_back("-Av8plusa");
6241 } else if (getToolChain().getArch() == llvm::Triple::sparcv9) {
6242 CmdArgs.push_back("-64");
6243 CmdArgs.push_back("-Av9a");
6245 } else if (getToolChain().getArch() == llvm::Triple::arm) {
6246 StringRef MArch = getToolChain().getArchName();
6247 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
6248 CmdArgs.push_back("-mfpu=neon");
6249 if (MArch == "armv8" || MArch == "armv8a" || MArch == "armv8-a")
6250 CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8");
6252 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args,
6253 getToolChain().getTriple());
6254 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI));
6256 Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
6257 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
6258 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
6259 } else if (getToolChain().getArch() == llvm::Triple::mips ||
6260 getToolChain().getArch() == llvm::Triple::mipsel ||
6261 getToolChain().getArch() == llvm::Triple::mips64 ||
6262 getToolChain().getArch() == llvm::Triple::mips64el) {
6265 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
6267 CmdArgs.push_back("-march");
6268 CmdArgs.push_back(CPUName.data());
6270 CmdArgs.push_back("-mabi");
6271 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data());
6273 if (getToolChain().getArch() == llvm::Triple::mips ||
6274 getToolChain().getArch() == llvm::Triple::mips64)
6275 CmdArgs.push_back("-EB");
6277 CmdArgs.push_back("-EL");
6279 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
6280 if (StringRef(A->getValue()) == "2008")
6281 CmdArgs.push_back(Args.MakeArgString("-mnan=2008"));
6284 if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfp64)) {
6285 if (A->getOption().matches(options::OPT_mfp32))
6286 CmdArgs.push_back(Args.MakeArgString("-mfp32"));
6288 CmdArgs.push_back(Args.MakeArgString("-mfp64"));
6291 Args.AddLastArg(CmdArgs, options::OPT_mips16, options::OPT_mno_mips16);
6292 Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
6293 options::OPT_mno_micromips);
6294 Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
6295 Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
6297 if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) {
6298 // Do not use AddLastArg because not all versions of MIPS assembler
6299 // support -mmsa / -mno-msa options.
6300 if (A->getOption().matches(options::OPT_mmsa))
6301 CmdArgs.push_back(Args.MakeArgString("-mmsa"));
6305 } else if (getToolChain().getArch() == llvm::Triple::systemz) {
6306 // Always pass an -march option, since our default of z10 is later
6307 // than the GNU assembler's default.
6308 StringRef CPUName = getSystemZTargetCPU(Args);
6309 CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
6313 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
6314 options::OPT_fpic, options::OPT_fno_pic,
6315 options::OPT_fPIE, options::OPT_fno_PIE,
6316 options::OPT_fpie, options::OPT_fno_pie);
6318 (LastPICArg->getOption().matches(options::OPT_fPIC) ||
6319 LastPICArg->getOption().matches(options::OPT_fpic) ||
6320 LastPICArg->getOption().matches(options::OPT_fPIE) ||
6321 LastPICArg->getOption().matches(options::OPT_fpie))) {
6322 CmdArgs.push_back("-KPIC");
6326 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6327 options::OPT_Xassembler);
6329 CmdArgs.push_back("-o");
6330 CmdArgs.push_back(Output.getFilename());
6332 for (InputInfoList::const_iterator
6333 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6334 const InputInfo &II = *it;
6335 CmdArgs.push_back(II.getFilename());
6339 Args.MakeArgString(getToolChain().GetProgramPath("as"));
6340 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6342 // Handle the debug info splitting at object creation time if we're
6343 // creating an object.
6344 // TODO: Currently only works on linux with newer objcopy.
6345 if (Args.hasArg(options::OPT_gsplit_dwarf) &&
6346 getToolChain().getTriple().isOSLinux())
6347 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
6348 SplitDebugName(Args, Inputs));
6351 static void AddLibgcc(llvm::Triple Triple, const Driver &D,
6352 ArgStringList &CmdArgs, const ArgList &Args) {
6353 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android;
6354 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
6355 Args.hasArg(options::OPT_static);
6357 CmdArgs.push_back("-lgcc");
6359 if (StaticLibgcc || isAndroid) {
6361 CmdArgs.push_back("-lgcc");
6364 CmdArgs.push_back("--as-needed");
6365 CmdArgs.push_back("-lgcc_s");
6367 CmdArgs.push_back("--no-as-needed");
6370 if (StaticLibgcc && !isAndroid)
6371 CmdArgs.push_back("-lgcc_eh");
6372 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
6373 CmdArgs.push_back("-lgcc");
6375 // According to Android ABI, we have to link with libdl if we are
6376 // linking with non-static libgcc.
6378 // NOTE: This fixes a link error on Android MIPS as well. The non-static
6379 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl.
6380 if (isAndroid && !StaticLibgcc)
6381 CmdArgs.push_back("-ldl");
6384 static bool hasMipsN32ABIArg(const ArgList &Args) {
6385 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
6386 return A && (A->getValue() == StringRef("n32"));
6389 static StringRef getLinuxDynamicLinker(const ArgList &Args,
6390 const toolchains::Linux &ToolChain) {
6391 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android)
6392 return "/system/bin/linker";
6393 else if (ToolChain.getArch() == llvm::Triple::x86 ||
6394 ToolChain.getArch() == llvm::Triple::sparc)
6395 return "/lib/ld-linux.so.2";
6396 else if (ToolChain.getArch() == llvm::Triple::aarch64)
6397 return "/lib/ld-linux-aarch64.so.1";
6398 else if (ToolChain.getArch() == llvm::Triple::arm ||
6399 ToolChain.getArch() == llvm::Triple::thumb) {
6400 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
6401 return "/lib/ld-linux-armhf.so.3";
6403 return "/lib/ld-linux.so.3";
6404 } else if (ToolChain.getArch() == llvm::Triple::mips ||
6405 ToolChain.getArch() == llvm::Triple::mipsel)
6406 return "/lib/ld.so.1";
6407 else if (ToolChain.getArch() == llvm::Triple::mips64 ||
6408 ToolChain.getArch() == llvm::Triple::mips64el) {
6409 if (hasMipsN32ABIArg(Args))
6410 return "/lib32/ld.so.1";
6412 return "/lib64/ld.so.1";
6413 } else if (ToolChain.getArch() == llvm::Triple::ppc)
6414 return "/lib/ld.so.1";
6415 else if (ToolChain.getArch() == llvm::Triple::ppc64 ||
6416 ToolChain.getArch() == llvm::Triple::ppc64le ||
6417 ToolChain.getArch() == llvm::Triple::systemz)
6418 return "/lib64/ld64.so.1";
6419 else if (ToolChain.getArch() == llvm::Triple::sparcv9)
6420 return "/lib64/ld-linux.so.2";
6422 return "/lib64/ld-linux-x86-64.so.2";
6425 void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA,
6426 const InputInfo &Output,
6427 const InputInfoList &Inputs,
6428 const ArgList &Args,
6429 const char *LinkingOutput) const {
6430 const toolchains::Linux& ToolChain =
6431 static_cast<const toolchains::Linux&>(getToolChain());
6432 const Driver &D = ToolChain.getDriver();
6433 const bool isAndroid =
6434 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android;
6435 const SanitizerArgs &Sanitize = ToolChain.getSanitizerArgs();
6437 !Args.hasArg(options::OPT_shared) &&
6438 (Args.hasArg(options::OPT_pie) || Sanitize.hasZeroBaseShadow());
6440 ArgStringList CmdArgs;
6442 // Silence warning for "clang -g foo.o -o foo"
6443 Args.ClaimAllArgs(options::OPT_g_Group);
6444 // and "clang -emit-llvm foo.o -o foo"
6445 Args.ClaimAllArgs(options::OPT_emit_llvm);
6446 // and for "clang -w foo.o -o foo". Other warning options are already
6447 // handled somewhere else.
6448 Args.ClaimAllArgs(options::OPT_w);
6450 if (!D.SysRoot.empty())
6451 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6454 CmdArgs.push_back("-pie");
6456 if (Args.hasArg(options::OPT_rdynamic))
6457 CmdArgs.push_back("-export-dynamic");
6459 if (Args.hasArg(options::OPT_s))
6460 CmdArgs.push_back("-s");
6462 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(),
6463 e = ToolChain.ExtraOpts.end();
6465 CmdArgs.push_back(i->c_str());
6467 if (!Args.hasArg(options::OPT_static)) {
6468 CmdArgs.push_back("--eh-frame-hdr");
6471 CmdArgs.push_back("-m");
6472 if (ToolChain.getArch() == llvm::Triple::x86)
6473 CmdArgs.push_back("elf_i386");
6474 else if (ToolChain.getArch() == llvm::Triple::aarch64)
6475 CmdArgs.push_back("aarch64linux");
6476 else if (ToolChain.getArch() == llvm::Triple::arm
6477 || ToolChain.getArch() == llvm::Triple::thumb)
6478 CmdArgs.push_back("armelf_linux_eabi");
6479 else if (ToolChain.getArch() == llvm::Triple::ppc)
6480 CmdArgs.push_back("elf32ppclinux");
6481 else if (ToolChain.getArch() == llvm::Triple::ppc64)
6482 CmdArgs.push_back("elf64ppc");
6483 else if (ToolChain.getArch() == llvm::Triple::sparc)
6484 CmdArgs.push_back("elf32_sparc");
6485 else if (ToolChain.getArch() == llvm::Triple::sparcv9)
6486 CmdArgs.push_back("elf64_sparc");
6487 else if (ToolChain.getArch() == llvm::Triple::mips)
6488 CmdArgs.push_back("elf32btsmip");
6489 else if (ToolChain.getArch() == llvm::Triple::mipsel)
6490 CmdArgs.push_back("elf32ltsmip");
6491 else if (ToolChain.getArch() == llvm::Triple::mips64) {
6492 if (hasMipsN32ABIArg(Args))
6493 CmdArgs.push_back("elf32btsmipn32");
6495 CmdArgs.push_back("elf64btsmip");
6497 else if (ToolChain.getArch() == llvm::Triple::mips64el) {
6498 if (hasMipsN32ABIArg(Args))
6499 CmdArgs.push_back("elf32ltsmipn32");
6501 CmdArgs.push_back("elf64ltsmip");
6503 else if (ToolChain.getArch() == llvm::Triple::systemz)
6504 CmdArgs.push_back("elf64_s390");
6506 CmdArgs.push_back("elf_x86_64");
6508 if (Args.hasArg(options::OPT_static)) {
6509 if (ToolChain.getArch() == llvm::Triple::arm
6510 || ToolChain.getArch() == llvm::Triple::thumb)
6511 CmdArgs.push_back("-Bstatic");
6513 CmdArgs.push_back("-static");
6514 } else if (Args.hasArg(options::OPT_shared)) {
6515 CmdArgs.push_back("-shared");
6517 CmdArgs.push_back("-Bsymbolic");
6521 if (ToolChain.getArch() == llvm::Triple::arm ||
6522 ToolChain.getArch() == llvm::Triple::thumb ||
6523 (!Args.hasArg(options::OPT_static) &&
6524 !Args.hasArg(options::OPT_shared))) {
6525 CmdArgs.push_back("-dynamic-linker");
6526 CmdArgs.push_back(Args.MakeArgString(
6527 D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain)));
6530 CmdArgs.push_back("-o");
6531 CmdArgs.push_back(Output.getFilename());
6533 if (!Args.hasArg(options::OPT_nostdlib) &&
6534 !Args.hasArg(options::OPT_nostartfiles)) {
6536 const char *crt1 = NULL;
6537 if (!Args.hasArg(options::OPT_shared)){
6538 if (Args.hasArg(options::OPT_pg))
6546 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
6548 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
6551 const char *crtbegin;
6552 if (Args.hasArg(options::OPT_static))
6553 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
6554 else if (Args.hasArg(options::OPT_shared))
6555 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
6557 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
6559 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
6560 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
6562 // Add crtfastmath.o if available and fast math is enabled.
6563 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
6566 Args.AddAllArgs(CmdArgs, options::OPT_L);
6568 const ToolChain::path_list Paths = ToolChain.getFilePaths();
6570 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end();
6572 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i));
6574 // Tell the linker to load the plugin. This has to come before AddLinkerInputs
6575 // as gold requires -plugin to come before any -plugin-opt that -Wl might
6577 if (D.IsUsingLTO(Args)) {
6578 CmdArgs.push_back("-plugin");
6579 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so";
6580 CmdArgs.push_back(Args.MakeArgString(Plugin));
6582 // Try to pass driver level flags relevant to LTO code generation down to
6585 // Handle flags for selecting CPU variants.
6586 std::string CPU = getCPUName(Args, ToolChain.getTriple());
6589 Args.MakeArgString(Twine("-plugin-opt=mcpu=") +
6595 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
6596 CmdArgs.push_back("--no-demangle");
6598 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs);
6600 // Call these before we add the C++ ABI library.
6601 if (Sanitize.needsUbsanRt())
6602 addUbsanRTLinux(getToolChain(), Args, CmdArgs, D.CCCIsCXX(),
6603 Sanitize.needsAsanRt() || Sanitize.needsTsanRt() ||
6604 Sanitize.needsMsanRt() || Sanitize.needsLsanRt());
6605 if (Sanitize.needsAsanRt())
6606 addAsanRTLinux(getToolChain(), Args, CmdArgs);
6607 if (Sanitize.needsTsanRt())
6608 addTsanRTLinux(getToolChain(), Args, CmdArgs);
6609 if (Sanitize.needsMsanRt())
6610 addMsanRTLinux(getToolChain(), Args, CmdArgs);
6611 if (Sanitize.needsLsanRt())
6612 addLsanRTLinux(getToolChain(), Args, CmdArgs);
6613 if (Sanitize.needsDfsanRt())
6614 addDfsanRTLinux(getToolChain(), Args, CmdArgs);
6616 // The profile runtime also needs access to system libraries.
6617 addProfileRTLinux(getToolChain(), Args, CmdArgs);
6620 !Args.hasArg(options::OPT_nostdlib) &&
6621 !Args.hasArg(options::OPT_nodefaultlibs)) {
6622 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
6623 !Args.hasArg(options::OPT_static);
6624 if (OnlyLibstdcxxStatic)
6625 CmdArgs.push_back("-Bstatic");
6626 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
6627 if (OnlyLibstdcxxStatic)
6628 CmdArgs.push_back("-Bdynamic");
6629 CmdArgs.push_back("-lm");
6632 if (!Args.hasArg(options::OPT_nostdlib)) {
6633 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
6634 if (Args.hasArg(options::OPT_static))
6635 CmdArgs.push_back("--start-group");
6637 bool OpenMP = Args.hasArg(options::OPT_fopenmp);
6639 CmdArgs.push_back("-lgomp");
6641 // FIXME: Exclude this for platforms whith libgomp that doesn't require
6642 // librt. Most modern Linux platfroms require it, but some may not.
6643 CmdArgs.push_back("-lrt");
6646 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6648 if (Args.hasArg(options::OPT_pthread) ||
6649 Args.hasArg(options::OPT_pthreads) || OpenMP)
6650 CmdArgs.push_back("-lpthread");
6652 CmdArgs.push_back("-lc");
6654 if (Args.hasArg(options::OPT_static))
6655 CmdArgs.push_back("--end-group");
6657 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args);
6660 if (!Args.hasArg(options::OPT_nostartfiles)) {
6662 if (Args.hasArg(options::OPT_shared))
6663 crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
6665 crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
6667 crtend = isAndroid ? "crtend_android.o" : "crtend.o";
6669 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
6671 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
6675 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs));
6678 void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6679 const InputInfo &Output,
6680 const InputInfoList &Inputs,
6681 const ArgList &Args,
6682 const char *LinkingOutput) const {
6683 ArgStringList CmdArgs;
6685 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6686 options::OPT_Xassembler);
6688 CmdArgs.push_back("-o");
6689 CmdArgs.push_back(Output.getFilename());
6691 for (InputInfoList::const_iterator
6692 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6693 const InputInfo &II = *it;
6694 CmdArgs.push_back(II.getFilename());
6698 Args.MakeArgString(getToolChain().GetProgramPath("as"));
6699 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6702 void minix::Link::ConstructJob(Compilation &C, const JobAction &JA,
6703 const InputInfo &Output,
6704 const InputInfoList &Inputs,
6705 const ArgList &Args,
6706 const char *LinkingOutput) const {
6707 const Driver &D = getToolChain().getDriver();
6708 ArgStringList CmdArgs;
6710 if (Output.isFilename()) {
6711 CmdArgs.push_back("-o");
6712 CmdArgs.push_back(Output.getFilename());
6714 assert(Output.isNothing() && "Invalid output.");
6717 if (!Args.hasArg(options::OPT_nostdlib) &&
6718 !Args.hasArg(options::OPT_nostartfiles)) {
6719 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
6720 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
6721 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
6722 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
6725 Args.AddAllArgs(CmdArgs, options::OPT_L);
6726 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6727 Args.AddAllArgs(CmdArgs, options::OPT_e);
6729 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6731 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
6733 if (!Args.hasArg(options::OPT_nostdlib) &&
6734 !Args.hasArg(options::OPT_nodefaultlibs)) {
6736 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
6737 CmdArgs.push_back("-lm");
6741 if (!Args.hasArg(options::OPT_nostdlib) &&
6742 !Args.hasArg(options::OPT_nostartfiles)) {
6743 if (Args.hasArg(options::OPT_pthread))
6744 CmdArgs.push_back("-lpthread");
6745 CmdArgs.push_back("-lc");
6746 CmdArgs.push_back("-lCompilerRT-Generic");
6747 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib");
6749 Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
6752 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
6753 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6758 // For now, DragonFly Assemble does just about the same as for
6759 // FreeBSD, but this may change soon.
6760 void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
6761 const InputInfo &Output,
6762 const InputInfoList &Inputs,
6763 const ArgList &Args,
6764 const char *LinkingOutput) const {
6765 ArgStringList CmdArgs;
6767 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6768 // instruct as in the base system to assemble 32-bit code.
6769 if (getToolChain().getArch() == llvm::Triple::x86)
6770 CmdArgs.push_back("--32");
6772 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
6773 options::OPT_Xassembler);
6775 CmdArgs.push_back("-o");
6776 CmdArgs.push_back(Output.getFilename());
6778 for (InputInfoList::const_iterator
6779 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6780 const InputInfo &II = *it;
6781 CmdArgs.push_back(II.getFilename());
6785 Args.MakeArgString(getToolChain().GetProgramPath("as"));
6786 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6789 void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
6790 const InputInfo &Output,
6791 const InputInfoList &Inputs,
6792 const ArgList &Args,
6793 const char *LinkingOutput) const {
6794 bool UseGCC47 = false;
6795 const Driver &D = getToolChain().getDriver();
6796 ArgStringList CmdArgs;
6798 if (llvm::sys::fs::exists("/usr/lib/gcc47", UseGCC47))
6801 if (!D.SysRoot.empty())
6802 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
6804 CmdArgs.push_back("--eh-frame-hdr");
6805 if (Args.hasArg(options::OPT_static)) {
6806 CmdArgs.push_back("-Bstatic");
6808 if (Args.hasArg(options::OPT_rdynamic))
6809 CmdArgs.push_back("-export-dynamic");
6810 if (Args.hasArg(options::OPT_shared))
6811 CmdArgs.push_back("-Bshareable");
6813 CmdArgs.push_back("-dynamic-linker");
6814 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
6816 CmdArgs.push_back("--hash-style=both");
6819 // When building 32-bit code on DragonFly/pc64, we have to explicitly
6820 // instruct ld in the base system to link 32-bit code.
6821 if (getToolChain().getArch() == llvm::Triple::x86) {
6822 CmdArgs.push_back("-m");
6823 CmdArgs.push_back("elf_i386");
6826 if (Output.isFilename()) {
6827 CmdArgs.push_back("-o");
6828 CmdArgs.push_back(Output.getFilename());
6830 assert(Output.isNothing() && "Invalid output.");
6833 if (!Args.hasArg(options::OPT_nostdlib) &&
6834 !Args.hasArg(options::OPT_nostartfiles)) {
6835 if (!Args.hasArg(options::OPT_shared)) {
6836 if (Args.hasArg(options::OPT_pg))
6837 CmdArgs.push_back(Args.MakeArgString(
6838 getToolChain().GetFilePath("gcrt1.o")));
6840 if (Args.hasArg(options::OPT_pie))
6841 CmdArgs.push_back(Args.MakeArgString(
6842 getToolChain().GetFilePath("Scrt1.o")));
6844 CmdArgs.push_back(Args.MakeArgString(
6845 getToolChain().GetFilePath("crt1.o")));
6848 CmdArgs.push_back(Args.MakeArgString(
6849 getToolChain().GetFilePath("crti.o")));
6850 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
6851 CmdArgs.push_back(Args.MakeArgString(
6852 getToolChain().GetFilePath("crtbeginS.o")));
6854 CmdArgs.push_back(Args.MakeArgString(
6855 getToolChain().GetFilePath("crtbegin.o")));
6858 Args.AddAllArgs(CmdArgs, options::OPT_L);
6859 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
6860 Args.AddAllArgs(CmdArgs, options::OPT_e);
6862 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
6864 if (!Args.hasArg(options::OPT_nostdlib) &&
6865 !Args.hasArg(options::OPT_nodefaultlibs)) {
6866 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
6869 CmdArgs.push_back("-L/usr/lib/gcc47");
6871 CmdArgs.push_back("-L/usr/lib/gcc44");
6873 if (!Args.hasArg(options::OPT_static)) {
6875 CmdArgs.push_back("-rpath");
6876 CmdArgs.push_back("/usr/lib/gcc47");
6878 CmdArgs.push_back("-rpath");
6879 CmdArgs.push_back("/usr/lib/gcc44");
6884 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
6885 CmdArgs.push_back("-lm");
6888 if (Args.hasArg(options::OPT_pthread))
6889 CmdArgs.push_back("-lpthread");
6891 if (!Args.hasArg(options::OPT_nolibc)) {
6892 CmdArgs.push_back("-lc");
6896 if (Args.hasArg(options::OPT_static) ||
6897 Args.hasArg(options::OPT_static_libgcc)) {
6898 CmdArgs.push_back("-lgcc");
6899 CmdArgs.push_back("-lgcc_eh");
6901 if (Args.hasArg(options::OPT_shared_libgcc)) {
6902 CmdArgs.push_back("-lgcc_pic");
6903 if (!Args.hasArg(options::OPT_shared))
6904 CmdArgs.push_back("-lgcc");
6906 CmdArgs.push_back("-lgcc");
6907 CmdArgs.push_back("--as-needed");
6908 CmdArgs.push_back("-lgcc_pic");
6909 CmdArgs.push_back("--no-as-needed");
6913 if (Args.hasArg(options::OPT_shared)) {
6914 CmdArgs.push_back("-lgcc_pic");
6916 CmdArgs.push_back("-lgcc");
6921 if (!Args.hasArg(options::OPT_nostdlib) &&
6922 !Args.hasArg(options::OPT_nostartfiles)) {
6923 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
6924 CmdArgs.push_back(Args.MakeArgString(
6925 getToolChain().GetFilePath("crtendS.o")));
6927 CmdArgs.push_back(Args.MakeArgString(
6928 getToolChain().GetFilePath("crtend.o")));
6929 CmdArgs.push_back(Args.MakeArgString(
6930 getToolChain().GetFilePath("crtn.o")));
6933 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple());
6936 Args.MakeArgString(getToolChain().GetLinkerPath());
6937 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
6940 void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA,
6941 const InputInfo &Output,
6942 const InputInfoList &Inputs,
6943 const ArgList &Args,
6944 const char *LinkingOutput) const {
6945 ArgStringList CmdArgs;
6947 if (Output.isFilename()) {
6948 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") +
6949 Output.getFilename()));
6951 assert(Output.isNothing() && "Invalid output.");
6954 if (!Args.hasArg(options::OPT_nostdlib) &&
6955 !Args.hasArg(options::OPT_nostartfiles) &&
6956 !C.getDriver().IsCLMode()) {
6957 CmdArgs.push_back("-defaultlib:libcmt");
6960 CmdArgs.push_back("-nologo");
6962 bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd);
6965 CmdArgs.push_back(Args.MakeArgString("-dll"));
6967 SmallString<128> ImplibName(Output.getFilename());
6968 llvm::sys::path::replace_extension(ImplibName, "lib");
6969 CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") +
6973 if (getToolChain().getSanitizerArgs().needsAsanRt()) {
6974 CmdArgs.push_back(Args.MakeArgString("-debug"));
6975 CmdArgs.push_back(Args.MakeArgString("-incremental:no"));
6976 SmallString<128> LibSanitizer(getToolChain().getDriver().ResourceDir);
6977 llvm::sys::path::append(LibSanitizer, "lib", "windows");
6979 llvm::sys::path::append(LibSanitizer, "clang_rt.asan_dll_thunk-i386.lib");
6981 llvm::sys::path::append(LibSanitizer, "clang_rt.asan-i386.lib");
6983 // FIXME: Handle 64-bit.
6984 CmdArgs.push_back(Args.MakeArgString(LibSanitizer));
6987 Args.AddAllArgValues(CmdArgs, options::OPT_l);
6988 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
6990 // Add filenames immediately.
6991 for (InputInfoList::const_iterator
6992 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
6993 if (it->isFilename())
6994 CmdArgs.push_back(it->getFilename());
6996 it->getInputArg().renderAsInput(Args, CmdArgs);
7000 Args.MakeArgString(getToolChain().GetProgramPath("link.exe"));
7001 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
7004 void visualstudio::Compile::ConstructJob(Compilation &C, const JobAction &JA,
7005 const InputInfo &Output,
7006 const InputInfoList &Inputs,
7007 const ArgList &Args,
7008 const char *LinkingOutput) const {
7009 C.addCommand(GetCommand(C, JA, Output, Inputs, Args, LinkingOutput));
7012 // Try to find FallbackName on PATH that is not identical to ClangProgramPath.
7013 // If one cannot be found, return FallbackName.
7014 // We do this special search to prevent clang-cl from falling back onto itself
7015 // if it's available as cl.exe on the path.
7016 static std::string FindFallback(const char *FallbackName,
7017 const char *ClangProgramPath) {
7018 llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH");
7019 if (!OptPath.hasValue())
7020 return FallbackName;
7022 #ifdef LLVM_ON_WIN32
7023 const StringRef PathSeparators = ";";
7025 const StringRef PathSeparators = ":";
7028 SmallVector<StringRef, 8> PathSegments;
7029 llvm::SplitString(OptPath.getValue(), PathSegments, PathSeparators);
7031 for (size_t i = 0, e = PathSegments.size(); i != e; ++i) {
7032 const StringRef &PathSegment = PathSegments[i];
7033 if (PathSegment.empty())
7036 SmallString<128> FilePath(PathSegment);
7037 llvm::sys::path::append(FilePath, FallbackName);
7038 if (llvm::sys::fs::can_execute(Twine(FilePath)) &&
7039 !llvm::sys::fs::equivalent(Twine(FilePath), ClangProgramPath))
7040 return FilePath.str();
7043 return FallbackName;
7046 Command *visualstudio::Compile::GetCommand(Compilation &C, const JobAction &JA,
7047 const InputInfo &Output,
7048 const InputInfoList &Inputs,
7049 const ArgList &Args,
7050 const char *LinkingOutput) const {
7051 ArgStringList CmdArgs;
7052 CmdArgs.push_back("/nologo");
7053 CmdArgs.push_back("/c"); // Compile only.
7054 CmdArgs.push_back("/W0"); // No warnings.
7056 // The goal is to be able to invoke this tool correctly based on
7057 // any flag accepted by clang-cl.
7059 // These are spelled the same way in clang and cl.exe,.
7060 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
7061 Args.AddAllArgs(CmdArgs, options::OPT_I);
7063 // Optimization level.
7064 if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) {
7065 if (A->getOption().getID() == options::OPT_O0) {
7066 CmdArgs.push_back("/Od");
7068 StringRef OptLevel = A->getValue();
7069 if (OptLevel == "1" || OptLevel == "2" || OptLevel == "s")
7070 A->render(Args, CmdArgs);
7071 else if (OptLevel == "3")
7072 CmdArgs.push_back("/Ox");
7076 // Flags for which clang-cl have an alias.
7077 // FIXME: How can we ensure this stays in sync with relevant clang-cl options?
7079 if (Arg *A = Args.getLastArg(options::OPT_frtti, options::OPT_fno_rtti))
7080 CmdArgs.push_back(A->getOption().getID() == options::OPT_frtti ? "/GR"
7082 if (Args.hasArg(options::OPT_fsyntax_only))
7083 CmdArgs.push_back("/Zs");
7085 std::vector<std::string> Includes = Args.getAllArgValues(options::OPT_include);
7086 for (size_t I = 0, E = Includes.size(); I != E; ++I)
7087 CmdArgs.push_back(Args.MakeArgString(std::string("/FI") + Includes[I]));
7089 // Flags that can simply be passed through.
7090 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LD);
7091 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LDd);
7093 // The order of these flags is relevant, so pick the last one.
7094 if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd,
7095 options::OPT__SLASH_MT, options::OPT__SLASH_MTd))
7096 A->render(Args, CmdArgs);
7100 assert(Inputs.size() == 1);
7101 const InputInfo &II = Inputs[0];
7102 assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX);
7103 CmdArgs.push_back(II.getType() == types::TY_C ? "/Tc" : "/Tp");
7104 if (II.isFilename())
7105 CmdArgs.push_back(II.getFilename());
7107 II.getInputArg().renderAsInput(Args, CmdArgs);
7110 assert(Output.getType() == types::TY_Object);
7111 const char *Fo = Args.MakeArgString(std::string("/Fo") +
7112 Output.getFilename());
7113 CmdArgs.push_back(Fo);
7115 const Driver &D = getToolChain().getDriver();
7116 std::string Exec = FindFallback("cl.exe", D.getClangProgramPath());
7118 return new Command(JA, *this, Args.MakeArgString(Exec), CmdArgs);
7123 // We pass assemble and link construction to the xcc tool.
7125 void XCore::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
7126 const InputInfo &Output,
7127 const InputInfoList &Inputs,
7128 const ArgList &Args,
7129 const char *LinkingOutput) const {
7130 ArgStringList CmdArgs;
7132 CmdArgs.push_back("-o");
7133 CmdArgs.push_back(Output.getFilename());
7135 CmdArgs.push_back("-c");
7137 if (Args.hasArg(options::OPT_g_Group)) {
7138 CmdArgs.push_back("-g");
7141 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
7142 options::OPT_Xassembler);
7144 for (InputInfoList::const_iterator
7145 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
7146 const InputInfo &II = *it;
7147 CmdArgs.push_back(II.getFilename());
7151 Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
7152 C.addCommand(new Command(JA, *this, Exec, CmdArgs));
7155 void XCore::Link::ConstructJob(Compilation &C, const JobAction &JA,
7156 const InputInfo &Output,
7157 const InputInfoList &Inputs,
7158 const ArgList &Args,
7159 const char *LinkingOutput) const {
7160 ArgStringList CmdArgs;
7162 if (Output.isFilename()) {
7163 CmdArgs.push_back("-o");
7164 CmdArgs.push_back(Output.getFilename());
7166 assert(Output.isNothing() && "Invalid output.");
7169 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs);
7172 Args.MakeArgString(getToolChain().GetProgramPath("xcc"));
7173 C.addCommand(new Command(JA, *this, Exec, CmdArgs));