Tools.cpp revision c427249cb79b77c3cd7a0855f854d2ff81618ddf
1//===--- Tools.cpp - Tools Implementations --------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "Tools.h" 11#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 "llvm/ADT/SmallString.h" 25#include "llvm/ADT/StringExtras.h" 26#include "llvm/ADT/StringSwitch.h" 27#include "llvm/ADT/Twine.h" 28#include "llvm/Option/Arg.h" 29#include "llvm/Option/ArgList.h" 30#include "llvm/Option/Option.h" 31#include "llvm/Support/ErrorHandling.h" 32#include "llvm/Support/FileSystem.h" 33#include "llvm/Support/Format.h" 34#include "llvm/Support/Host.h" 35#include "llvm/Support/Path.h" 36#include "llvm/Support/Program.h" 37#include "llvm/Support/Process.h" 38#include "llvm/Support/raw_ostream.h" 39#include <sys/stat.h> 40 41using namespace clang::driver; 42using namespace clang::driver::tools; 43using namespace clang; 44using namespace llvm::opt; 45 46/// CheckPreprocessingOptions - Perform some validation of preprocessing 47/// arguments that is shared with gcc. 48static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) { 49 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC)) 50 if (!Args.hasArg(options::OPT_E) && !D.CCCIsCPP()) 51 D.Diag(diag::err_drv_argument_only_allowed_with) 52 << A->getAsString(Args) << "-E"; 53} 54 55/// CheckCodeGenerationOptions - Perform some validation of code generation 56/// arguments that is shared with gcc. 57static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) { 58 // In gcc, only ARM checks this, but it seems reasonable to check universally. 59 if (Args.hasArg(options::OPT_static)) 60 if (const Arg *A = Args.getLastArg(options::OPT_dynamic, 61 options::OPT_mdynamic_no_pic)) 62 D.Diag(diag::err_drv_argument_not_allowed_with) 63 << A->getAsString(Args) << "-static"; 64} 65 66// Quote target names for inclusion in GNU Make dependency files. 67// Only the characters '$', '#', ' ', '\t' are quoted. 68static void QuoteTarget(StringRef Target, 69 SmallVectorImpl<char> &Res) { 70 for (unsigned i = 0, e = Target.size(); i != e; ++i) { 71 switch (Target[i]) { 72 case ' ': 73 case '\t': 74 // Escape the preceding backslashes 75 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j) 76 Res.push_back('\\'); 77 78 // Escape the space/tab 79 Res.push_back('\\'); 80 break; 81 case '$': 82 Res.push_back('$'); 83 break; 84 case '#': 85 Res.push_back('\\'); 86 break; 87 default: 88 break; 89 } 90 91 Res.push_back(Target[i]); 92 } 93} 94 95static void addDirectoryList(const ArgList &Args, 96 ArgStringList &CmdArgs, 97 const char *ArgName, 98 const char *EnvVar) { 99 const char *DirList = ::getenv(EnvVar); 100 bool CombinedArg = false; 101 102 if (!DirList) 103 return; // Nothing to do. 104 105 StringRef Name(ArgName); 106 if (Name.equals("-I") || Name.equals("-L")) 107 CombinedArg = true; 108 109 StringRef Dirs(DirList); 110 if (Dirs.empty()) // Empty string should not add '.'. 111 return; 112 113 StringRef::size_type Delim; 114 while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) { 115 if (Delim == 0) { // Leading colon. 116 if (CombinedArg) { 117 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); 118 } else { 119 CmdArgs.push_back(ArgName); 120 CmdArgs.push_back("."); 121 } 122 } else { 123 if (CombinedArg) { 124 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim))); 125 } else { 126 CmdArgs.push_back(ArgName); 127 CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim))); 128 } 129 } 130 Dirs = Dirs.substr(Delim + 1); 131 } 132 133 if (Dirs.empty()) { // Trailing colon. 134 if (CombinedArg) { 135 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); 136 } else { 137 CmdArgs.push_back(ArgName); 138 CmdArgs.push_back("."); 139 } 140 } else { // Add the last path. 141 if (CombinedArg) { 142 CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs)); 143 } else { 144 CmdArgs.push_back(ArgName); 145 CmdArgs.push_back(Args.MakeArgString(Dirs)); 146 } 147 } 148} 149 150static void AddLinkerInputs(const ToolChain &TC, 151 const InputInfoList &Inputs, const ArgList &Args, 152 ArgStringList &CmdArgs) { 153 const Driver &D = TC.getDriver(); 154 155 // Add extra linker input arguments which are not treated as inputs 156 // (constructed via -Xarch_). 157 Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input); 158 159 for (InputInfoList::const_iterator 160 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 161 const InputInfo &II = *it; 162 163 if (!TC.HasNativeLLVMSupport()) { 164 // Don't try to pass LLVM inputs unless we have native support. 165 if (II.getType() == types::TY_LLVM_IR || 166 II.getType() == types::TY_LTO_IR || 167 II.getType() == types::TY_LLVM_BC || 168 II.getType() == types::TY_LTO_BC) 169 D.Diag(diag::err_drv_no_linker_llvm_support) 170 << TC.getTripleString(); 171 } 172 173 // Add filenames immediately. 174 if (II.isFilename()) { 175 CmdArgs.push_back(II.getFilename()); 176 continue; 177 } 178 179 // Otherwise, this is a linker input argument. 180 const Arg &A = II.getInputArg(); 181 182 // Handle reserved library options. 183 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) { 184 TC.AddCXXStdlibLibArgs(Args, CmdArgs); 185 } else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) { 186 TC.AddCCKextLibArgs(Args, CmdArgs); 187 } else 188 A.renderAsInput(Args, CmdArgs); 189 } 190 191 // LIBRARY_PATH - included following the user specified library paths. 192 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); 193} 194 195/// \brief Determine whether Objective-C automated reference counting is 196/// enabled. 197static bool isObjCAutoRefCount(const ArgList &Args) { 198 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false); 199} 200 201/// \brief Determine whether we are linking the ObjC runtime. 202static bool isObjCRuntimeLinked(const ArgList &Args) { 203 if (isObjCAutoRefCount(Args)) { 204 Args.ClaimAllArgs(options::OPT_fobjc_link_runtime); 205 return true; 206 } 207 return Args.hasArg(options::OPT_fobjc_link_runtime); 208} 209 210static void addProfileRT(const ToolChain &TC, const ArgList &Args, 211 ArgStringList &CmdArgs, 212 llvm::Triple Triple) { 213 if (!(Args.hasArg(options::OPT_fprofile_arcs) || 214 Args.hasArg(options::OPT_fprofile_generate) || 215 Args.hasArg(options::OPT_fcreate_profile) || 216 Args.hasArg(options::OPT_coverage))) 217 return; 218 219 // GCC links libgcov.a by adding -L<inst>/gcc/lib/gcc/<triple>/<ver> -lgcov to 220 // the link line. We cannot do the same thing because unlike gcov there is a 221 // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is 222 // not supported by old linkers. 223 std::string ProfileRT = 224 std::string(TC.getDriver().Dir) + "/../lib/libprofile_rt.a"; 225 226 CmdArgs.push_back(Args.MakeArgString(ProfileRT)); 227} 228 229static bool forwardToGCC(const Option &O) { 230 // Don't forward inputs from the original command line. They are added from 231 // InputInfoList. 232 return O.getKind() != Option::InputClass && 233 !O.hasFlag(options::DriverOption) && 234 !O.hasFlag(options::LinkerInput); 235} 236 237void Clang::AddPreprocessingOptions(Compilation &C, 238 const JobAction &JA, 239 const Driver &D, 240 const ArgList &Args, 241 ArgStringList &CmdArgs, 242 const InputInfo &Output, 243 const InputInfoList &Inputs) const { 244 Arg *A; 245 246 CheckPreprocessingOptions(D, Args); 247 248 Args.AddLastArg(CmdArgs, options::OPT_C); 249 Args.AddLastArg(CmdArgs, options::OPT_CC); 250 251 // Handle dependency file generation. 252 if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) || 253 (A = Args.getLastArg(options::OPT_MD)) || 254 (A = Args.getLastArg(options::OPT_MMD))) { 255 // Determine the output location. 256 const char *DepFile; 257 if (Arg *MF = Args.getLastArg(options::OPT_MF)) { 258 DepFile = MF->getValue(); 259 C.addFailureResultFile(DepFile, &JA); 260 } else if (Output.getType() == types::TY_Dependencies) { 261 DepFile = Output.getFilename(); 262 } else if (A->getOption().matches(options::OPT_M) || 263 A->getOption().matches(options::OPT_MM)) { 264 DepFile = "-"; 265 } else { 266 DepFile = getDependencyFileName(Args, Inputs); 267 C.addFailureResultFile(DepFile, &JA); 268 } 269 CmdArgs.push_back("-dependency-file"); 270 CmdArgs.push_back(DepFile); 271 272 // Add a default target if one wasn't specified. 273 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) { 274 const char *DepTarget; 275 276 // If user provided -o, that is the dependency target, except 277 // when we are only generating a dependency file. 278 Arg *OutputOpt = Args.getLastArg(options::OPT_o); 279 if (OutputOpt && Output.getType() != types::TY_Dependencies) { 280 DepTarget = OutputOpt->getValue(); 281 } else { 282 // Otherwise derive from the base input. 283 // 284 // FIXME: This should use the computed output file location. 285 SmallString<128> P(Inputs[0].getBaseInput()); 286 llvm::sys::path::replace_extension(P, "o"); 287 DepTarget = Args.MakeArgString(llvm::sys::path::filename(P)); 288 } 289 290 CmdArgs.push_back("-MT"); 291 SmallString<128> Quoted; 292 QuoteTarget(DepTarget, Quoted); 293 CmdArgs.push_back(Args.MakeArgString(Quoted)); 294 } 295 296 if (A->getOption().matches(options::OPT_M) || 297 A->getOption().matches(options::OPT_MD)) 298 CmdArgs.push_back("-sys-header-deps"); 299 } 300 301 if (Args.hasArg(options::OPT_MG)) { 302 if (!A || A->getOption().matches(options::OPT_MD) || 303 A->getOption().matches(options::OPT_MMD)) 304 D.Diag(diag::err_drv_mg_requires_m_or_mm); 305 CmdArgs.push_back("-MG"); 306 } 307 308 Args.AddLastArg(CmdArgs, options::OPT_MP); 309 310 // Convert all -MQ <target> args to -MT <quoted target> 311 for (arg_iterator it = Args.filtered_begin(options::OPT_MT, 312 options::OPT_MQ), 313 ie = Args.filtered_end(); it != ie; ++it) { 314 const Arg *A = *it; 315 A->claim(); 316 317 if (A->getOption().matches(options::OPT_MQ)) { 318 CmdArgs.push_back("-MT"); 319 SmallString<128> Quoted; 320 QuoteTarget(A->getValue(), Quoted); 321 CmdArgs.push_back(Args.MakeArgString(Quoted)); 322 323 // -MT flag - no change 324 } else { 325 A->render(Args, CmdArgs); 326 } 327 } 328 329 // Add -i* options, and automatically translate to 330 // -include-pch/-include-pth for transparent PCH support. It's 331 // wonky, but we include looking for .gch so we can support seamless 332 // replacement into a build system already set up to be generating 333 // .gch files. 334 bool RenderedImplicitInclude = false; 335 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group), 336 ie = Args.filtered_end(); it != ie; ++it) { 337 const Arg *A = it; 338 339 if (A->getOption().matches(options::OPT_include)) { 340 bool IsFirstImplicitInclude = !RenderedImplicitInclude; 341 RenderedImplicitInclude = true; 342 343 // Use PCH if the user requested it. 344 bool UsePCH = D.CCCUsePCH; 345 346 bool FoundPTH = false; 347 bool FoundPCH = false; 348 SmallString<128> P(A->getValue()); 349 // We want the files to have a name like foo.h.pch. Add a dummy extension 350 // so that replace_extension does the right thing. 351 P += ".dummy"; 352 if (UsePCH) { 353 llvm::sys::path::replace_extension(P, "pch"); 354 if (llvm::sys::fs::exists(P.str())) 355 FoundPCH = true; 356 } 357 358 if (!FoundPCH) { 359 llvm::sys::path::replace_extension(P, "pth"); 360 if (llvm::sys::fs::exists(P.str())) 361 FoundPTH = true; 362 } 363 364 if (!FoundPCH && !FoundPTH) { 365 llvm::sys::path::replace_extension(P, "gch"); 366 if (llvm::sys::fs::exists(P.str())) { 367 FoundPCH = UsePCH; 368 FoundPTH = !UsePCH; 369 } 370 } 371 372 if (FoundPCH || FoundPTH) { 373 if (IsFirstImplicitInclude) { 374 A->claim(); 375 if (UsePCH) 376 CmdArgs.push_back("-include-pch"); 377 else 378 CmdArgs.push_back("-include-pth"); 379 CmdArgs.push_back(Args.MakeArgString(P.str())); 380 continue; 381 } else { 382 // Ignore the PCH if not first on command line and emit warning. 383 D.Diag(diag::warn_drv_pch_not_first_include) 384 << P.str() << A->getAsString(Args); 385 } 386 } 387 } 388 389 // Not translated, render as usual. 390 A->claim(); 391 A->render(Args, CmdArgs); 392 } 393 394 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U); 395 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F, 396 options::OPT_index_header_map); 397 398 // Add -Wp, and -Xassembler if using the preprocessor. 399 400 // FIXME: There is a very unfortunate problem here, some troubled 401 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To 402 // really support that we would have to parse and then translate 403 // those options. :( 404 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, 405 options::OPT_Xpreprocessor); 406 407 // -I- is a deprecated GCC feature, reject it. 408 if (Arg *A = Args.getLastArg(options::OPT_I_)) 409 D.Diag(diag::err_drv_I_dash_not_supported) << A->getAsString(Args); 410 411 // If we have a --sysroot, and don't have an explicit -isysroot flag, add an 412 // -isysroot to the CC1 invocation. 413 StringRef sysroot = C.getSysRoot(); 414 if (sysroot != "") { 415 if (!Args.hasArg(options::OPT_isysroot)) { 416 CmdArgs.push_back("-isysroot"); 417 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 418 } 419 } 420 421 // Parse additional include paths from environment variables. 422 // FIXME: We should probably sink the logic for handling these from the 423 // frontend into the driver. It will allow deleting 4 otherwise unused flags. 424 // CPATH - included following the user specified includes (but prior to 425 // builtin and standard includes). 426 addDirectoryList(Args, CmdArgs, "-I", "CPATH"); 427 // C_INCLUDE_PATH - system includes enabled when compiling C. 428 addDirectoryList(Args, CmdArgs, "-c-isystem", "C_INCLUDE_PATH"); 429 // CPLUS_INCLUDE_PATH - system includes enabled when compiling C++. 430 addDirectoryList(Args, CmdArgs, "-cxx-isystem", "CPLUS_INCLUDE_PATH"); 431 // OBJC_INCLUDE_PATH - system includes enabled when compiling ObjC. 432 addDirectoryList(Args, CmdArgs, "-objc-isystem", "OBJC_INCLUDE_PATH"); 433 // OBJCPLUS_INCLUDE_PATH - system includes enabled when compiling ObjC++. 434 addDirectoryList(Args, CmdArgs, "-objcxx-isystem", "OBJCPLUS_INCLUDE_PATH"); 435 436 // Add C++ include arguments, if needed. 437 if (types::isCXX(Inputs[0].getType())) 438 getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs); 439 440 // Add system include arguments. 441 getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs); 442} 443 444/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular 445/// CPU. 446// 447// FIXME: This is redundant with -mcpu, why does LLVM use this. 448// FIXME: tblgen this, or kill it! 449static const char *getLLVMArchSuffixForARM(StringRef CPU) { 450 return llvm::StringSwitch<const char *>(CPU) 451 .Case("strongarm", "v4") 452 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t") 453 .Cases("arm720t", "arm9", "arm9tdmi", "v4t") 454 .Cases("arm920", "arm920t", "arm922t", "v4t") 455 .Cases("arm940t", "ep9312","v4t") 456 .Cases("arm10tdmi", "arm1020t", "v5") 457 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e") 458 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e") 459 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e") 460 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6") 461 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6") 462 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2") 463 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7") 464 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "v7") 465 .Cases("cortex-r4", "cortex-r5", "v7r") 466 .Case("cortex-m0", "v6m") 467 .Case("cortex-m3", "v7m") 468 .Case("cortex-m4", "v7em") 469 .Case("cortex-a9-mp", "v7f") 470 .Case("swift", "v7s") 471 .Cases("cortex-a53", "cortex-a57", "v8") 472 .Default(""); 473} 474 475/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting. 476// 477// FIXME: tblgen this. 478static std::string getARMTargetCPU(const ArgList &Args, 479 const llvm::Triple &Triple) { 480 // FIXME: Warn on inconsistent use of -mcpu and -march. 481 482 // If we have -mcpu=, use that. 483 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 484 StringRef MCPU = A->getValue(); 485 // Handle -mcpu=native. 486 if (MCPU == "native") 487 return llvm::sys::getHostCPUName(); 488 else 489 return MCPU; 490 } 491 492 StringRef MArch; 493 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 494 // Otherwise, if we have -march= choose the base CPU for that arch. 495 MArch = A->getValue(); 496 } else { 497 // Otherwise, use the Arch from the triple. 498 MArch = Triple.getArchName(); 499 } 500 501 // Handle -march=native. 502 std::string NativeMArch; 503 if (MArch == "native") { 504 std::string CPU = llvm::sys::getHostCPUName(); 505 if (CPU != "generic") { 506 // Translate the native cpu into the architecture. The switch below will 507 // then chose the minimum cpu for that arch. 508 NativeMArch = std::string("arm") + getLLVMArchSuffixForARM(CPU); 509 MArch = NativeMArch; 510 } 511 } 512 513 return llvm::StringSwitch<const char *>(MArch) 514 .Cases("armv2", "armv2a","arm2") 515 .Case("armv3", "arm6") 516 .Case("armv3m", "arm7m") 517 .Case("armv4", "strongarm") 518 .Case("armv4t", "arm7tdmi") 519 .Cases("armv5", "armv5t", "arm10tdmi") 520 .Cases("armv5e", "armv5te", "arm1022e") 521 .Case("armv5tej", "arm926ej-s") 522 .Cases("armv6", "armv6k", "arm1136jf-s") 523 .Case("armv6j", "arm1136j-s") 524 .Cases("armv6z", "armv6zk", "arm1176jzf-s") 525 .Case("armv6t2", "arm1156t2-s") 526 .Cases("armv6m", "armv6-m", "cortex-m0") 527 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8") 528 .Cases("armv7em", "armv7e-m", "cortex-m4") 529 .Cases("armv7f", "armv7-f", "cortex-a9-mp") 530 .Cases("armv7s", "armv7-s", "swift") 531 .Cases("armv7r", "armv7-r", "cortex-r4") 532 .Cases("armv7m", "armv7-m", "cortex-m3") 533 .Cases("armv8", "armv8a", "armv8-a", "cortex-a53") 534 .Case("ep9312", "ep9312") 535 .Case("iwmmxt", "iwmmxt") 536 .Case("xscale", "xscale") 537 // If all else failed, return the most base CPU with thumb interworking 538 // supported by LLVM. 539 .Default("arm7tdmi"); 540} 541 542// FIXME: Move to target hook. 543static bool isSignedCharDefault(const llvm::Triple &Triple) { 544 switch (Triple.getArch()) { 545 default: 546 return true; 547 548 case llvm::Triple::aarch64: 549 case llvm::Triple::arm: 550 case llvm::Triple::ppc: 551 case llvm::Triple::ppc64: 552 if (Triple.isOSDarwin()) 553 return true; 554 return false; 555 556 case llvm::Triple::ppc64le: 557 case llvm::Triple::systemz: 558 case llvm::Triple::xcore: 559 return false; 560 } 561} 562 563static bool isNoCommonDefault(const llvm::Triple &Triple) { 564 switch (Triple.getArch()) { 565 default: 566 return false; 567 568 case llvm::Triple::xcore: 569 return true; 570 } 571} 572 573// Handle -mfpu=. 574// 575// FIXME: Centralize feature selection, defaulting shouldn't be also in the 576// frontend target. 577static void getAArch64FPUFeatures(const Driver &D, const Arg *A, 578 const ArgList &Args, 579 std::vector<const char *> &Features) { 580 StringRef FPU = A->getValue(); 581 if (FPU == "fp-armv8") { 582 Features.push_back("+fp-armv8"); 583 } else if (FPU == "neon-fp-armv8") { 584 Features.push_back("+fp-armv8"); 585 Features.push_back("+neon"); 586 } else if (FPU == "crypto-neon-fp-armv8") { 587 Features.push_back("+fp-armv8"); 588 Features.push_back("+neon"); 589 Features.push_back("+crypto"); 590 } else if (FPU == "neon") { 591 Features.push_back("+neon"); 592 } else if (FPU == "none") { 593 Features.push_back("-fp-armv8"); 594 Features.push_back("-crypto"); 595 Features.push_back("-neon"); 596 } else 597 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 598} 599 600// Handle -mhwdiv=. 601static void getARMHWDivFeatures(const Driver &D, const Arg *A, 602 const ArgList &Args, 603 std::vector<const char *> &Features) { 604 StringRef HWDiv = A->getValue(); 605 if (HWDiv == "arm") { 606 Features.push_back("+hwdiv-arm"); 607 Features.push_back("-hwdiv"); 608 } else if (HWDiv == "thumb") { 609 Features.push_back("-hwdiv-arm"); 610 Features.push_back("+hwdiv"); 611 } else if (HWDiv == "arm,thumb" || HWDiv == "thumb,arm") { 612 Features.push_back("+hwdiv-arm"); 613 Features.push_back("+hwdiv"); 614 } else if (HWDiv == "none") { 615 Features.push_back("-hwdiv-arm"); 616 Features.push_back("-hwdiv"); 617 } else 618 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 619} 620 621// Handle -mfpu=. 622// 623// FIXME: Centralize feature selection, defaulting shouldn't be also in the 624// frontend target. 625static void getARMFPUFeatures(const Driver &D, const Arg *A, 626 const ArgList &Args, 627 std::vector<const char *> &Features) { 628 StringRef FPU = A->getValue(); 629 630 // Set the target features based on the FPU. 631 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") { 632 // Disable any default FPU support. 633 Features.push_back("-vfp2"); 634 Features.push_back("-vfp3"); 635 Features.push_back("-neon"); 636 } else if (FPU == "vfp3-d16" || FPU == "vfpv3-d16") { 637 Features.push_back("+vfp3"); 638 Features.push_back("+d16"); 639 Features.push_back("-neon"); 640 } else if (FPU == "vfp") { 641 Features.push_back("+vfp2"); 642 Features.push_back("-neon"); 643 } else if (FPU == "vfp3" || FPU == "vfpv3") { 644 Features.push_back("+vfp3"); 645 Features.push_back("-neon"); 646 } else if (FPU == "fp-armv8") { 647 Features.push_back("+fp-armv8"); 648 } else if (FPU == "neon-fp-armv8") { 649 Features.push_back("+fp-armv8"); 650 Features.push_back("+neon"); 651 } else if (FPU == "crypto-neon-fp-armv8") { 652 Features.push_back("+crypto"); 653 Features.push_back("+fp-armv8"); 654 } else if (FPU == "neon") { 655 Features.push_back("+neon"); 656 } else if (FPU == "none") { 657 Features.push_back("-vfp2"); 658 Features.push_back("-vfp3"); 659 Features.push_back("-vfp4"); 660 Features.push_back("-fp-armv8"); 661 Features.push_back("-crypto"); 662 Features.push_back("-neon"); 663 } else 664 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 665} 666 667// Select the float ABI as determined by -msoft-float, -mhard-float, and 668// -mfloat-abi=. 669static StringRef getARMFloatABI(const Driver &D, 670 const ArgList &Args, 671 const llvm::Triple &Triple) { 672 StringRef FloatABI; 673 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 674 options::OPT_mhard_float, 675 options::OPT_mfloat_abi_EQ)) { 676 if (A->getOption().matches(options::OPT_msoft_float)) 677 FloatABI = "soft"; 678 else if (A->getOption().matches(options::OPT_mhard_float)) 679 FloatABI = "hard"; 680 else { 681 FloatABI = A->getValue(); 682 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") { 683 D.Diag(diag::err_drv_invalid_mfloat_abi) 684 << A->getAsString(Args); 685 FloatABI = "soft"; 686 } 687 } 688 } 689 690 // If unspecified, choose the default based on the platform. 691 if (FloatABI.empty()) { 692 switch (Triple.getOS()) { 693 case llvm::Triple::Darwin: 694 case llvm::Triple::MacOSX: 695 case llvm::Triple::IOS: { 696 // Darwin defaults to "softfp" for v6 and v7. 697 // 698 // FIXME: Factor out an ARM class so we can cache the arch somewhere. 699 std::string ArchName = 700 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple)); 701 if (StringRef(ArchName).startswith("v6") || 702 StringRef(ArchName).startswith("v7")) 703 FloatABI = "softfp"; 704 else 705 FloatABI = "soft"; 706 break; 707 } 708 709 case llvm::Triple::FreeBSD: 710 // FreeBSD defaults to soft float 711 FloatABI = "soft"; 712 break; 713 714 default: 715 switch(Triple.getEnvironment()) { 716 case llvm::Triple::GNUEABIHF: 717 FloatABI = "hard"; 718 break; 719 case llvm::Triple::GNUEABI: 720 FloatABI = "softfp"; 721 break; 722 case llvm::Triple::EABI: 723 // EABI is always AAPCS, and if it was not marked 'hard', it's softfp 724 FloatABI = "softfp"; 725 break; 726 case llvm::Triple::Android: { 727 std::string ArchName = 728 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple)); 729 if (StringRef(ArchName).startswith("v7")) 730 FloatABI = "softfp"; 731 else 732 FloatABI = "soft"; 733 break; 734 } 735 default: 736 // Assume "soft", but warn the user we are guessing. 737 FloatABI = "soft"; 738 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft"; 739 break; 740 } 741 } 742 } 743 744 return FloatABI; 745} 746 747static void getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple, 748 const ArgList &Args, 749 std::vector<const char *> &Features) { 750 StringRef FloatABI = getARMFloatABI(D, Args, Triple); 751 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these 752 // yet (it uses the -mfloat-abi and -msoft-float options), and it is 753 // stripped out by the ARM target. 754 // Use software floating point operations? 755 if (FloatABI == "soft") 756 Features.push_back("+soft-float"); 757 758 // Use software floating point argument passing? 759 if (FloatABI != "hard") 760 Features.push_back("+soft-float-abi"); 761 762 // Honor -mfpu=. 763 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) 764 getARMFPUFeatures(D, A, Args, Features); 765 if (const Arg *A = Args.getLastArg(options::OPT_mhwdiv_EQ)) 766 getARMHWDivFeatures(D, A, Args, Features); 767 768 // Setting -msoft-float effectively disables NEON because of the GCC 769 // implementation, although the same isn't true of VFP or VFP3. 770 if (FloatABI == "soft") 771 Features.push_back("-neon"); 772} 773 774void Clang::AddARMTargetArgs(const ArgList &Args, 775 ArgStringList &CmdArgs, 776 bool KernelOrKext) const { 777 const Driver &D = getToolChain().getDriver(); 778 // Get the effective triple, which takes into account the deployment target. 779 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args); 780 llvm::Triple Triple(TripleStr); 781 std::string CPUName = getARMTargetCPU(Args, Triple); 782 783 // Select the ABI to use. 784 // 785 // FIXME: Support -meabi. 786 const char *ABIName = 0; 787 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 788 ABIName = A->getValue(); 789 } else if (Triple.isOSDarwin()) { 790 // The backend is hardwired to assume AAPCS for M-class processors, ensure 791 // the frontend matches that. 792 if (Triple.getEnvironment() == llvm::Triple::EABI || 793 StringRef(CPUName).startswith("cortex-m")) { 794 ABIName = "aapcs"; 795 } else { 796 ABIName = "apcs-gnu"; 797 } 798 } else { 799 // Select the default based on the platform. 800 switch(Triple.getEnvironment()) { 801 case llvm::Triple::Android: 802 case llvm::Triple::GNUEABI: 803 case llvm::Triple::GNUEABIHF: 804 ABIName = "aapcs-linux"; 805 break; 806 case llvm::Triple::EABI: 807 ABIName = "aapcs"; 808 break; 809 default: 810 ABIName = "apcs-gnu"; 811 } 812 } 813 CmdArgs.push_back("-target-abi"); 814 CmdArgs.push_back(ABIName); 815 816 // Determine floating point ABI from the options & target defaults. 817 StringRef FloatABI = getARMFloatABI(D, Args, Triple); 818 if (FloatABI == "soft") { 819 // Floating point operations and argument passing are soft. 820 // 821 // FIXME: This changes CPP defines, we need -target-soft-float. 822 CmdArgs.push_back("-msoft-float"); 823 CmdArgs.push_back("-mfloat-abi"); 824 CmdArgs.push_back("soft"); 825 } else if (FloatABI == "softfp") { 826 // Floating point operations are hard, but argument passing is soft. 827 CmdArgs.push_back("-mfloat-abi"); 828 CmdArgs.push_back("soft"); 829 } else { 830 // Floating point operations and argument passing are hard. 831 assert(FloatABI == "hard" && "Invalid float abi!"); 832 CmdArgs.push_back("-mfloat-abi"); 833 CmdArgs.push_back("hard"); 834 } 835 836 // Kernel code has more strict alignment requirements. 837 if (KernelOrKext) { 838 if (!Triple.isiOS() || Triple.isOSVersionLT(6)) { 839 CmdArgs.push_back("-backend-option"); 840 CmdArgs.push_back("-arm-long-calls"); 841 } 842 843 CmdArgs.push_back("-backend-option"); 844 CmdArgs.push_back("-arm-strict-align"); 845 846 // The kext linker doesn't know how to deal with movw/movt. 847 CmdArgs.push_back("-backend-option"); 848 CmdArgs.push_back("-arm-use-movt=0"); 849 } 850 851 // Setting -mno-global-merge disables the codegen global merge pass. Setting 852 // -mglobal-merge has no effect as the pass is enabled by default. 853 if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge, 854 options::OPT_mno_global_merge)) { 855 if (A->getOption().matches(options::OPT_mno_global_merge)) 856 CmdArgs.push_back("-mno-global-merge"); 857 } 858 859 if (!Args.hasFlag(options::OPT_mimplicit_float, 860 options::OPT_mno_implicit_float, 861 true)) 862 CmdArgs.push_back("-no-implicit-float"); 863 864 // llvm does not support reserving registers in general. There is support 865 // for reserving r9 on ARM though (defined as a platform-specific register 866 // in ARM EABI). 867 if (Args.hasArg(options::OPT_ffixed_r9)) { 868 CmdArgs.push_back("-backend-option"); 869 CmdArgs.push_back("-arm-reserve-r9"); 870 } 871} 872 873// Get CPU and ABI names. They are not independent 874// so we have to calculate them together. 875static void getMipsCPUAndABI(const ArgList &Args, 876 const llvm::Triple &Triple, 877 StringRef &CPUName, 878 StringRef &ABIName) { 879 const char *DefMips32CPU = "mips32"; 880 const char *DefMips64CPU = "mips64"; 881 882 if (Arg *A = Args.getLastArg(options::OPT_march_EQ, 883 options::OPT_mcpu_EQ)) 884 CPUName = A->getValue(); 885 886 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) { 887 ABIName = A->getValue(); 888 // Convert a GNU style Mips ABI name to the name 889 // accepted by LLVM Mips backend. 890 ABIName = llvm::StringSwitch<llvm::StringRef>(ABIName) 891 .Case("32", "o32") 892 .Case("64", "n64") 893 .Default(ABIName); 894 } 895 896 // Setup default CPU and ABI names. 897 if (CPUName.empty() && ABIName.empty()) { 898 switch (Triple.getArch()) { 899 default: 900 llvm_unreachable("Unexpected triple arch name"); 901 case llvm::Triple::mips: 902 case llvm::Triple::mipsel: 903 CPUName = DefMips32CPU; 904 break; 905 case llvm::Triple::mips64: 906 case llvm::Triple::mips64el: 907 CPUName = DefMips64CPU; 908 break; 909 } 910 } 911 912 if (!ABIName.empty()) { 913 // Deduce CPU name from ABI name. 914 CPUName = llvm::StringSwitch<const char *>(ABIName) 915 .Cases("32", "o32", "eabi", DefMips32CPU) 916 .Cases("n32", "n64", "64", DefMips64CPU) 917 .Default(""); 918 } 919 else if (!CPUName.empty()) { 920 // Deduce ABI name from CPU name. 921 ABIName = llvm::StringSwitch<const char *>(CPUName) 922 .Cases("mips32", "mips32r2", "o32") 923 .Cases("mips64", "mips64r2", "n64") 924 .Default(""); 925 } 926 927 // FIXME: Warn on inconsistent cpu and abi usage. 928} 929 930// Convert ABI name to the GNU tools acceptable variant. 931static StringRef getGnuCompatibleMipsABIName(StringRef ABI) { 932 return llvm::StringSwitch<llvm::StringRef>(ABI) 933 .Case("o32", "32") 934 .Case("n64", "64") 935 .Default(ABI); 936} 937 938// Select the MIPS float ABI as determined by -msoft-float, -mhard-float, 939// and -mfloat-abi=. 940static StringRef getMipsFloatABI(const Driver &D, const ArgList &Args) { 941 StringRef FloatABI; 942 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 943 options::OPT_mhard_float, 944 options::OPT_mfloat_abi_EQ)) { 945 if (A->getOption().matches(options::OPT_msoft_float)) 946 FloatABI = "soft"; 947 else if (A->getOption().matches(options::OPT_mhard_float)) 948 FloatABI = "hard"; 949 else { 950 FloatABI = A->getValue(); 951 if (FloatABI != "soft" && FloatABI != "hard") { 952 D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args); 953 FloatABI = "hard"; 954 } 955 } 956 } 957 958 // If unspecified, choose the default based on the platform. 959 if (FloatABI.empty()) { 960 // Assume "hard", because it's a default value used by gcc. 961 // When we start to recognize specific target MIPS processors, 962 // we will be able to select the default more correctly. 963 FloatABI = "hard"; 964 } 965 966 return FloatABI; 967} 968 969static void AddTargetFeature(const ArgList &Args, 970 std::vector<const char *> &Features, 971 OptSpecifier OnOpt, OptSpecifier OffOpt, 972 StringRef FeatureName) { 973 if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) { 974 if (A->getOption().matches(OnOpt)) 975 Features.push_back(Args.MakeArgString("+" + FeatureName)); 976 else 977 Features.push_back(Args.MakeArgString("-" + FeatureName)); 978 } 979} 980 981static void getMIPSTargetFeatures(const Driver &D, const ArgList &Args, 982 std::vector<const char *> &Features) { 983 StringRef FloatABI = getMipsFloatABI(D, Args); 984 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL; 985 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) { 986 // FIXME: Note, this is a hack. We need to pass the selected float 987 // mode to the MipsTargetInfoBase to define appropriate macros there. 988 // Now it is the only method. 989 Features.push_back("+soft-float"); 990 } 991 992 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) { 993 if (StringRef(A->getValue()) == "2008") 994 Features.push_back("+nan2008"); 995 } 996 997 AddTargetFeature(Args, Features, options::OPT_msingle_float, 998 options::OPT_mdouble_float, "single-float"); 999 AddTargetFeature(Args, Features, options::OPT_mips16, options::OPT_mno_mips16, 1000 "mips16"); 1001 AddTargetFeature(Args, Features, options::OPT_mmicromips, 1002 options::OPT_mno_micromips, "micromips"); 1003 AddTargetFeature(Args, Features, options::OPT_mdsp, options::OPT_mno_dsp, 1004 "dsp"); 1005 AddTargetFeature(Args, Features, options::OPT_mdspr2, options::OPT_mno_dspr2, 1006 "dspr2"); 1007 AddTargetFeature(Args, Features, options::OPT_mmsa, options::OPT_mno_msa, 1008 "msa"); 1009 AddTargetFeature(Args, Features, options::OPT_mfp64, options::OPT_mfp32, 1010 "fp64"); 1011} 1012 1013void Clang::AddMIPSTargetArgs(const ArgList &Args, 1014 ArgStringList &CmdArgs) const { 1015 const Driver &D = getToolChain().getDriver(); 1016 StringRef CPUName; 1017 StringRef ABIName; 1018 const llvm::Triple &Triple = getToolChain().getTriple(); 1019 getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 1020 1021 CmdArgs.push_back("-target-abi"); 1022 CmdArgs.push_back(ABIName.data()); 1023 1024 StringRef FloatABI = getMipsFloatABI(D, Args); 1025 1026 bool IsMips16 = Args.getLastArg(options::OPT_mips16) != NULL; 1027 1028 if (FloatABI == "soft" || (FloatABI == "hard" && IsMips16)) { 1029 // Floating point operations and argument passing are soft. 1030 CmdArgs.push_back("-msoft-float"); 1031 CmdArgs.push_back("-mfloat-abi"); 1032 CmdArgs.push_back("soft"); 1033 1034 if (FloatABI == "hard" && IsMips16) { 1035 CmdArgs.push_back("-mllvm"); 1036 CmdArgs.push_back("-mips16-hard-float"); 1037 } 1038 } 1039 else { 1040 // Floating point operations and argument passing are hard. 1041 assert(FloatABI == "hard" && "Invalid float abi!"); 1042 CmdArgs.push_back("-mfloat-abi"); 1043 CmdArgs.push_back("hard"); 1044 } 1045 1046 if (Arg *A = Args.getLastArg(options::OPT_mxgot, options::OPT_mno_xgot)) { 1047 if (A->getOption().matches(options::OPT_mxgot)) { 1048 CmdArgs.push_back("-mllvm"); 1049 CmdArgs.push_back("-mxgot"); 1050 } 1051 } 1052 1053 if (Arg *A = Args.getLastArg(options::OPT_mldc1_sdc1, 1054 options::OPT_mno_ldc1_sdc1)) { 1055 if (A->getOption().matches(options::OPT_mno_ldc1_sdc1)) { 1056 CmdArgs.push_back("-mllvm"); 1057 CmdArgs.push_back("-mno-ldc1-sdc1"); 1058 } 1059 } 1060 1061 if (Arg *A = Args.getLastArg(options::OPT_mcheck_zero_division, 1062 options::OPT_mno_check_zero_division)) { 1063 if (A->getOption().matches(options::OPT_mno_check_zero_division)) { 1064 CmdArgs.push_back("-mllvm"); 1065 CmdArgs.push_back("-mno-check-zero-division"); 1066 } 1067 } 1068 1069 if (Arg *A = Args.getLastArg(options::OPT_G)) { 1070 StringRef v = A->getValue(); 1071 CmdArgs.push_back("-mllvm"); 1072 CmdArgs.push_back(Args.MakeArgString("-mips-ssection-threshold=" + v)); 1073 A->claim(); 1074 } 1075} 1076 1077/// getPPCTargetCPU - Get the (LLVM) name of the PowerPC cpu we are targeting. 1078static std::string getPPCTargetCPU(const ArgList &Args) { 1079 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 1080 StringRef CPUName = A->getValue(); 1081 1082 if (CPUName == "native") { 1083 std::string CPU = llvm::sys::getHostCPUName(); 1084 if (!CPU.empty() && CPU != "generic") 1085 return CPU; 1086 else 1087 return ""; 1088 } 1089 1090 return llvm::StringSwitch<const char *>(CPUName) 1091 .Case("common", "generic") 1092 .Case("440", "440") 1093 .Case("440fp", "440") 1094 .Case("450", "450") 1095 .Case("601", "601") 1096 .Case("602", "602") 1097 .Case("603", "603") 1098 .Case("603e", "603e") 1099 .Case("603ev", "603ev") 1100 .Case("604", "604") 1101 .Case("604e", "604e") 1102 .Case("620", "620") 1103 .Case("630", "pwr3") 1104 .Case("G3", "g3") 1105 .Case("7400", "7400") 1106 .Case("G4", "g4") 1107 .Case("7450", "7450") 1108 .Case("G4+", "g4+") 1109 .Case("750", "750") 1110 .Case("970", "970") 1111 .Case("G5", "g5") 1112 .Case("a2", "a2") 1113 .Case("a2q", "a2q") 1114 .Case("e500mc", "e500mc") 1115 .Case("e5500", "e5500") 1116 .Case("power3", "pwr3") 1117 .Case("power4", "pwr4") 1118 .Case("power5", "pwr5") 1119 .Case("power5x", "pwr5x") 1120 .Case("power6", "pwr6") 1121 .Case("power6x", "pwr6x") 1122 .Case("power7", "pwr7") 1123 .Case("pwr3", "pwr3") 1124 .Case("pwr4", "pwr4") 1125 .Case("pwr5", "pwr5") 1126 .Case("pwr5x", "pwr5x") 1127 .Case("pwr6", "pwr6") 1128 .Case("pwr6x", "pwr6x") 1129 .Case("pwr7", "pwr7") 1130 .Case("powerpc", "ppc") 1131 .Case("powerpc64", "ppc64") 1132 .Case("powerpc64le", "ppc64le") 1133 .Default(""); 1134 } 1135 1136 return ""; 1137} 1138 1139static void getPPCTargetFeatures(const ArgList &Args, 1140 std::vector<const char *> &Features) { 1141 for (arg_iterator it = Args.filtered_begin(options::OPT_m_ppc_Features_Group), 1142 ie = Args.filtered_end(); 1143 it != ie; ++it) { 1144 StringRef Name = (*it)->getOption().getName(); 1145 (*it)->claim(); 1146 1147 // Skip over "-m". 1148 assert(Name.startswith("m") && "Invalid feature name."); 1149 Name = Name.substr(1); 1150 1151 bool IsNegative = Name.startswith("no-"); 1152 if (IsNegative) 1153 Name = Name.substr(3); 1154 1155 // Note that gcc calls this mfcrf and LLVM calls this mfocrf so we 1156 // pass the correct option to the backend while calling the frontend 1157 // option the same. 1158 // TODO: Change the LLVM backend option maybe? 1159 if (Name == "mfcrf") 1160 Name = "mfocrf"; 1161 1162 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name)); 1163 } 1164 1165 // Altivec is a bit weird, allow overriding of the Altivec feature here. 1166 AddTargetFeature(Args, Features, options::OPT_faltivec, 1167 options::OPT_fno_altivec, "altivec"); 1168} 1169 1170/// Get the (LLVM) name of the R600 gpu we are targeting. 1171static std::string getR600TargetGPU(const ArgList &Args) { 1172 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 1173 const char *GPUName = A->getValue(); 1174 return llvm::StringSwitch<const char *>(GPUName) 1175 .Cases("rv630", "rv635", "r600") 1176 .Cases("rv610", "rv620", "rs780", "rs880") 1177 .Case("rv740", "rv770") 1178 .Case("palm", "cedar") 1179 .Cases("sumo", "sumo2", "sumo") 1180 .Case("hemlock", "cypress") 1181 .Case("aruba", "cayman") 1182 .Default(GPUName); 1183 } 1184 return ""; 1185} 1186 1187static void getSparcTargetFeatures(const ArgList &Args, 1188 std::vector<const char *> Features) { 1189 bool SoftFloatABI = true; 1190 if (Arg *A = 1191 Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) { 1192 if (A->getOption().matches(options::OPT_mhard_float)) 1193 SoftFloatABI = false; 1194 } 1195 if (SoftFloatABI) 1196 Features.push_back("+soft-float"); 1197} 1198 1199void Clang::AddSparcTargetArgs(const ArgList &Args, 1200 ArgStringList &CmdArgs) const { 1201 const Driver &D = getToolChain().getDriver(); 1202 1203 // Select the float ABI as determined by -msoft-float, -mhard-float, and 1204 StringRef FloatABI; 1205 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 1206 options::OPT_mhard_float)) { 1207 if (A->getOption().matches(options::OPT_msoft_float)) 1208 FloatABI = "soft"; 1209 else if (A->getOption().matches(options::OPT_mhard_float)) 1210 FloatABI = "hard"; 1211 } 1212 1213 // If unspecified, choose the default based on the platform. 1214 if (FloatABI.empty()) { 1215 // Assume "soft", but warn the user we are guessing. 1216 FloatABI = "soft"; 1217 D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft"; 1218 } 1219 1220 if (FloatABI == "soft") { 1221 // Floating point operations and argument passing are soft. 1222 // 1223 // FIXME: This changes CPP defines, we need -target-soft-float. 1224 CmdArgs.push_back("-msoft-float"); 1225 } else { 1226 assert(FloatABI == "hard" && "Invalid float abi!"); 1227 CmdArgs.push_back("-mhard-float"); 1228 } 1229} 1230 1231static const char *getSystemZTargetCPU(const ArgList &Args) { 1232 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 1233 return A->getValue(); 1234 return "z10"; 1235} 1236 1237static const char *getX86TargetCPU(const ArgList &Args, 1238 const llvm::Triple &Triple) { 1239 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 1240 if (StringRef(A->getValue()) != "native") 1241 return A->getValue(); 1242 1243 // FIXME: Reject attempts to use -march=native unless the target matches 1244 // the host. 1245 // 1246 // FIXME: We should also incorporate the detected target features for use 1247 // with -native. 1248 std::string CPU = llvm::sys::getHostCPUName(); 1249 if (!CPU.empty() && CPU != "generic") 1250 return Args.MakeArgString(CPU); 1251 } 1252 1253 // Select the default CPU if none was given (or detection failed). 1254 1255 if (Triple.getArch() != llvm::Triple::x86_64 && 1256 Triple.getArch() != llvm::Triple::x86) 1257 return 0; // This routine is only handling x86 targets. 1258 1259 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64; 1260 1261 // FIXME: Need target hooks. 1262 if (Triple.isOSDarwin()) 1263 return Is64Bit ? "core2" : "yonah"; 1264 1265 // All x86 devices running Android have core2 as their common 1266 // denominator. This makes a better choice than pentium4. 1267 if (Triple.getEnvironment() == llvm::Triple::Android) 1268 return "core2"; 1269 1270 // Everything else goes to x86-64 in 64-bit mode. 1271 if (Is64Bit) 1272 return "x86-64"; 1273 1274 switch (Triple.getOS()) { 1275 case llvm::Triple::FreeBSD: 1276 case llvm::Triple::NetBSD: 1277 case llvm::Triple::OpenBSD: 1278 return "i486"; 1279 case llvm::Triple::Haiku: 1280 return "i586"; 1281 case llvm::Triple::Bitrig: 1282 return "i686"; 1283 default: 1284 // Fallback to p4. 1285 return "pentium4"; 1286 } 1287} 1288 1289static std::string getCPUName(const ArgList &Args, const llvm::Triple &T) { 1290 switch(T.getArch()) { 1291 default: 1292 return ""; 1293 1294 case llvm::Triple::arm: 1295 case llvm::Triple::thumb: 1296 return getARMTargetCPU(Args, T); 1297 1298 case llvm::Triple::mips: 1299 case llvm::Triple::mipsel: 1300 case llvm::Triple::mips64: 1301 case llvm::Triple::mips64el: { 1302 StringRef CPUName; 1303 StringRef ABIName; 1304 getMipsCPUAndABI(Args, T, CPUName, ABIName); 1305 return CPUName; 1306 } 1307 1308 case llvm::Triple::ppc: 1309 case llvm::Triple::ppc64: 1310 case llvm::Triple::ppc64le: { 1311 std::string TargetCPUName = getPPCTargetCPU(Args); 1312 // LLVM may default to generating code for the native CPU, 1313 // but, like gcc, we default to a more generic option for 1314 // each architecture. (except on Darwin) 1315 if (TargetCPUName.empty() && !T.isOSDarwin()) { 1316 if (T.getArch() == llvm::Triple::ppc64) 1317 TargetCPUName = "ppc64"; 1318 else if (T.getArch() == llvm::Triple::ppc64le) 1319 TargetCPUName = "ppc64le"; 1320 else 1321 TargetCPUName = "ppc"; 1322 } 1323 return TargetCPUName; 1324 } 1325 1326 case llvm::Triple::sparc: 1327 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 1328 return A->getValue(); 1329 return ""; 1330 1331 case llvm::Triple::x86: 1332 case llvm::Triple::x86_64: 1333 return getX86TargetCPU(Args, T); 1334 1335 case llvm::Triple::hexagon: 1336 return "hexagon" + toolchains::Hexagon_TC::GetTargetCPU(Args).str(); 1337 1338 case llvm::Triple::systemz: 1339 return getSystemZTargetCPU(Args); 1340 1341 case llvm::Triple::r600: 1342 return getR600TargetGPU(Args); 1343 } 1344} 1345 1346static void getX86TargetFeatures(const ArgList &Args, 1347 std::vector<const char *> &Features) { 1348 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group), 1349 ie = Args.filtered_end(); 1350 it != ie; ++it) { 1351 StringRef Name = (*it)->getOption().getName(); 1352 (*it)->claim(); 1353 1354 // Skip over "-m". 1355 assert(Name.startswith("m") && "Invalid feature name."); 1356 Name = Name.substr(1); 1357 1358 bool IsNegative = Name.startswith("no-"); 1359 if (IsNegative) 1360 Name = Name.substr(3); 1361 1362 Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name)); 1363 } 1364} 1365 1366void Clang::AddX86TargetArgs(const ArgList &Args, 1367 ArgStringList &CmdArgs) const { 1368 if (!Args.hasFlag(options::OPT_mred_zone, 1369 options::OPT_mno_red_zone, 1370 true) || 1371 Args.hasArg(options::OPT_mkernel) || 1372 Args.hasArg(options::OPT_fapple_kext)) 1373 CmdArgs.push_back("-disable-red-zone"); 1374 1375 // Default to avoid implicit floating-point for kernel/kext code, but allow 1376 // that to be overridden with -mno-soft-float. 1377 bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) || 1378 Args.hasArg(options::OPT_fapple_kext)); 1379 if (Arg *A = Args.getLastArg(options::OPT_msoft_float, 1380 options::OPT_mno_soft_float, 1381 options::OPT_mimplicit_float, 1382 options::OPT_mno_implicit_float)) { 1383 const Option &O = A->getOption(); 1384 NoImplicitFloat = (O.matches(options::OPT_mno_implicit_float) || 1385 O.matches(options::OPT_msoft_float)); 1386 } 1387 if (NoImplicitFloat) 1388 CmdArgs.push_back("-no-implicit-float"); 1389} 1390 1391static inline bool HasPICArg(const ArgList &Args) { 1392 return Args.hasArg(options::OPT_fPIC) 1393 || Args.hasArg(options::OPT_fpic); 1394} 1395 1396static Arg *GetLastSmallDataThresholdArg(const ArgList &Args) { 1397 return Args.getLastArg(options::OPT_G, 1398 options::OPT_G_EQ, 1399 options::OPT_msmall_data_threshold_EQ); 1400} 1401 1402static std::string GetHexagonSmallDataThresholdValue(const ArgList &Args) { 1403 std::string value; 1404 if (HasPICArg(Args)) 1405 value = "0"; 1406 else if (Arg *A = GetLastSmallDataThresholdArg(Args)) { 1407 value = A->getValue(); 1408 A->claim(); 1409 } 1410 return value; 1411} 1412 1413void Clang::AddHexagonTargetArgs(const ArgList &Args, 1414 ArgStringList &CmdArgs) const { 1415 CmdArgs.push_back("-fno-signed-char"); 1416 CmdArgs.push_back("-mqdsp6-compat"); 1417 CmdArgs.push_back("-Wreturn-type"); 1418 1419 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args); 1420 if (!SmallDataThreshold.empty()) { 1421 CmdArgs.push_back ("-mllvm"); 1422 CmdArgs.push_back(Args.MakeArgString( 1423 "-hexagon-small-data-threshold=" + SmallDataThreshold)); 1424 } 1425 1426 if (!Args.hasArg(options::OPT_fno_short_enums)) 1427 CmdArgs.push_back("-fshort-enums"); 1428 if (Args.getLastArg(options::OPT_mieee_rnd_near)) { 1429 CmdArgs.push_back ("-mllvm"); 1430 CmdArgs.push_back ("-enable-hexagon-ieee-rnd-near"); 1431 } 1432 CmdArgs.push_back ("-mllvm"); 1433 CmdArgs.push_back ("-machine-sink-split=0"); 1434} 1435 1436static void getAArch64TargetFeatures(const Driver &D, const ArgList &Args, 1437 std::vector<const char *> &Features) { 1438 // Honor -mfpu=. 1439 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) 1440 getAArch64FPUFeatures(D, A, Args, Features); 1441} 1442 1443static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, 1444 const ArgList &Args, ArgStringList &CmdArgs) { 1445 std::vector<const char *> Features; 1446 switch (Triple.getArch()) { 1447 default: 1448 break; 1449 case llvm::Triple::mips: 1450 case llvm::Triple::mipsel: 1451 case llvm::Triple::mips64: 1452 case llvm::Triple::mips64el: 1453 getMIPSTargetFeatures(D, Args, Features); 1454 break; 1455 1456 case llvm::Triple::arm: 1457 case llvm::Triple::thumb: 1458 getARMTargetFeatures(D, Triple, Args, Features); 1459 break; 1460 1461 case llvm::Triple::ppc: 1462 case llvm::Triple::ppc64: 1463 case llvm::Triple::ppc64le: 1464 getPPCTargetFeatures(Args, Features); 1465 break; 1466 case llvm::Triple::sparc: 1467 getSparcTargetFeatures(Args, Features); 1468 break; 1469 case llvm::Triple::aarch64: 1470 getAArch64TargetFeatures(D, Args, Features); 1471 break; 1472 case llvm::Triple::x86: 1473 case llvm::Triple::x86_64: 1474 getX86TargetFeatures(Args, Features); 1475 break; 1476 } 1477 1478 // Find the last of each feature. 1479 llvm::StringMap<unsigned> LastOpt; 1480 for (unsigned I = 0, N = Features.size(); I < N; ++I) { 1481 const char *Name = Features[I]; 1482 assert(Name[0] == '-' || Name[0] == '+'); 1483 LastOpt[Name + 1] = I; 1484 } 1485 1486 for (unsigned I = 0, N = Features.size(); I < N; ++I) { 1487 // If this feature was overridden, ignore it. 1488 const char *Name = Features[I]; 1489 llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name + 1); 1490 assert(LastI != LastOpt.end()); 1491 unsigned Last = LastI->second; 1492 if (Last != I) 1493 continue; 1494 1495 CmdArgs.push_back("-target-feature"); 1496 CmdArgs.push_back(Name); 1497 } 1498} 1499 1500static bool 1501shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, 1502 const llvm::Triple &Triple) { 1503 // We use the zero-cost exception tables for Objective-C if the non-fragile 1504 // ABI is enabled or when compiling for x86_64 and ARM on Snow Leopard and 1505 // later. 1506 if (runtime.isNonFragile()) 1507 return true; 1508 1509 if (!Triple.isOSDarwin()) 1510 return false; 1511 1512 return (!Triple.isMacOSXVersionLT(10,5) && 1513 (Triple.getArch() == llvm::Triple::x86_64 || 1514 Triple.getArch() == llvm::Triple::arm)); 1515} 1516 1517/// addExceptionArgs - Adds exception related arguments to the driver command 1518/// arguments. There's a master flag, -fexceptions and also language specific 1519/// flags to enable/disable C++ and Objective-C exceptions. 1520/// This makes it possible to for example disable C++ exceptions but enable 1521/// Objective-C exceptions. 1522static void addExceptionArgs(const ArgList &Args, types::ID InputType, 1523 const llvm::Triple &Triple, 1524 bool KernelOrKext, 1525 const ObjCRuntime &objcRuntime, 1526 ArgStringList &CmdArgs) { 1527 if (KernelOrKext) { 1528 // -mkernel and -fapple-kext imply no exceptions, so claim exception related 1529 // arguments now to avoid warnings about unused arguments. 1530 Args.ClaimAllArgs(options::OPT_fexceptions); 1531 Args.ClaimAllArgs(options::OPT_fno_exceptions); 1532 Args.ClaimAllArgs(options::OPT_fobjc_exceptions); 1533 Args.ClaimAllArgs(options::OPT_fno_objc_exceptions); 1534 Args.ClaimAllArgs(options::OPT_fcxx_exceptions); 1535 Args.ClaimAllArgs(options::OPT_fno_cxx_exceptions); 1536 return; 1537 } 1538 1539 // Exceptions are enabled by default. 1540 bool ExceptionsEnabled = true; 1541 1542 // This keeps track of whether exceptions were explicitly turned on or off. 1543 bool DidHaveExplicitExceptionFlag = false; 1544 1545 if (Arg *A = Args.getLastArg(options::OPT_fexceptions, 1546 options::OPT_fno_exceptions)) { 1547 if (A->getOption().matches(options::OPT_fexceptions)) 1548 ExceptionsEnabled = true; 1549 else 1550 ExceptionsEnabled = false; 1551 1552 DidHaveExplicitExceptionFlag = true; 1553 } 1554 1555 bool ShouldUseExceptionTables = false; 1556 1557 // Exception tables and cleanups can be enabled with -fexceptions even if the 1558 // language itself doesn't support exceptions. 1559 if (ExceptionsEnabled && DidHaveExplicitExceptionFlag) 1560 ShouldUseExceptionTables = true; 1561 1562 // Obj-C exceptions are enabled by default, regardless of -fexceptions. This 1563 // is not necessarily sensible, but follows GCC. 1564 if (types::isObjC(InputType) && 1565 Args.hasFlag(options::OPT_fobjc_exceptions, 1566 options::OPT_fno_objc_exceptions, 1567 true)) { 1568 CmdArgs.push_back("-fobjc-exceptions"); 1569 1570 ShouldUseExceptionTables |= 1571 shouldUseExceptionTablesForObjCExceptions(objcRuntime, Triple); 1572 } 1573 1574 if (types::isCXX(InputType)) { 1575 bool CXXExceptionsEnabled = ExceptionsEnabled; 1576 1577 if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions, 1578 options::OPT_fno_cxx_exceptions, 1579 options::OPT_fexceptions, 1580 options::OPT_fno_exceptions)) { 1581 if (A->getOption().matches(options::OPT_fcxx_exceptions)) 1582 CXXExceptionsEnabled = true; 1583 else if (A->getOption().matches(options::OPT_fno_cxx_exceptions)) 1584 CXXExceptionsEnabled = false; 1585 } 1586 1587 if (CXXExceptionsEnabled) { 1588 CmdArgs.push_back("-fcxx-exceptions"); 1589 1590 ShouldUseExceptionTables = true; 1591 } 1592 } 1593 1594 if (ShouldUseExceptionTables) 1595 CmdArgs.push_back("-fexceptions"); 1596} 1597 1598static bool ShouldDisableAutolink(const ArgList &Args, 1599 const ToolChain &TC) { 1600 bool Default = true; 1601 if (TC.getTriple().isOSDarwin()) { 1602 // The native darwin assembler doesn't support the linker_option directives, 1603 // so we disable them if we think the .s file will be passed to it. 1604 Default = TC.useIntegratedAs(); 1605 } 1606 return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink, 1607 Default); 1608} 1609 1610static bool ShouldDisableCFI(const ArgList &Args, 1611 const ToolChain &TC) { 1612 bool Default = true; 1613 if (TC.getTriple().isOSDarwin()) { 1614 // The native darwin assembler doesn't support cfi directives, so 1615 // we disable them if we think the .s file will be passed to it. 1616 Default = TC.useIntegratedAs(); 1617 } 1618 return !Args.hasFlag(options::OPT_fdwarf2_cfi_asm, 1619 options::OPT_fno_dwarf2_cfi_asm, 1620 Default); 1621} 1622 1623static bool ShouldDisableDwarfDirectory(const ArgList &Args, 1624 const ToolChain &TC) { 1625 bool UseDwarfDirectory = Args.hasFlag(options::OPT_fdwarf_directory_asm, 1626 options::OPT_fno_dwarf_directory_asm, 1627 TC.useIntegratedAs()); 1628 return !UseDwarfDirectory; 1629} 1630 1631/// \brief Check whether the given input tree contains any compilation actions. 1632static bool ContainsCompileAction(const Action *A) { 1633 if (isa<CompileJobAction>(A)) 1634 return true; 1635 1636 for (Action::const_iterator it = A->begin(), ie = A->end(); it != ie; ++it) 1637 if (ContainsCompileAction(*it)) 1638 return true; 1639 1640 return false; 1641} 1642 1643/// \brief Check if -relax-all should be passed to the internal assembler. 1644/// This is done by default when compiling non-assembler source with -O0. 1645static bool UseRelaxAll(Compilation &C, const ArgList &Args) { 1646 bool RelaxDefault = true; 1647 1648 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 1649 RelaxDefault = A->getOption().matches(options::OPT_O0); 1650 1651 if (RelaxDefault) { 1652 RelaxDefault = false; 1653 for (ActionList::const_iterator it = C.getActions().begin(), 1654 ie = C.getActions().end(); it != ie; ++it) { 1655 if (ContainsCompileAction(*it)) { 1656 RelaxDefault = true; 1657 break; 1658 } 1659 } 1660 } 1661 1662 return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all, 1663 RelaxDefault); 1664} 1665 1666static void CollectArgsForIntegratedAssembler(Compilation &C, 1667 const ArgList &Args, 1668 ArgStringList &CmdArgs, 1669 const Driver &D) { 1670 if (UseRelaxAll(C, Args)) 1671 CmdArgs.push_back("-mrelax-all"); 1672 1673 // When using an integrated assembler, translate -Wa, and -Xassembler 1674 // options. 1675 for (arg_iterator it = Args.filtered_begin(options::OPT_Wa_COMMA, 1676 options::OPT_Xassembler), 1677 ie = Args.filtered_end(); it != ie; ++it) { 1678 const Arg *A = *it; 1679 A->claim(); 1680 1681 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) { 1682 StringRef Value = A->getValue(i); 1683 1684 if (Value == "-force_cpusubtype_ALL") { 1685 // Do nothing, this is the default and we don't support anything else. 1686 } else if (Value == "-L") { 1687 CmdArgs.push_back("-msave-temp-labels"); 1688 } else if (Value == "--fatal-warnings") { 1689 CmdArgs.push_back("-mllvm"); 1690 CmdArgs.push_back("-fatal-assembler-warnings"); 1691 } else if (Value == "--noexecstack") { 1692 CmdArgs.push_back("-mnoexecstack"); 1693 } else { 1694 D.Diag(diag::err_drv_unsupported_option_argument) 1695 << A->getOption().getName() << Value; 1696 } 1697 } 1698 } 1699} 1700 1701static void addProfileRTLinux( 1702 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs) { 1703 if (!(Args.hasArg(options::OPT_fprofile_arcs) || 1704 Args.hasArg(options::OPT_fprofile_generate) || 1705 Args.hasArg(options::OPT_fcreate_profile) || 1706 Args.hasArg(options::OPT_coverage))) 1707 return; 1708 1709 // The profile runtime is located in the Linux library directory and has name 1710 // "libclang_rt.profile-<ArchName>.a". 1711 SmallString<128> LibProfile(TC.getDriver().ResourceDir); 1712 llvm::sys::path::append( 1713 LibProfile, "lib", "linux", 1714 Twine("libclang_rt.profile-") + TC.getArchName() + ".a"); 1715 1716 CmdArgs.push_back(Args.MakeArgString(LibProfile)); 1717} 1718 1719static void addSanitizerRTLinkFlagsLinux( 1720 const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs, 1721 const StringRef Sanitizer, bool BeforeLibStdCXX, 1722 bool ExportSymbols = true) { 1723 // Sanitizer runtime is located in the Linux library directory and 1724 // has name "libclang_rt.<Sanitizer>-<ArchName>.a". 1725 SmallString<128> LibSanitizer(TC.getDriver().ResourceDir); 1726 llvm::sys::path::append( 1727 LibSanitizer, "lib", "linux", 1728 (Twine("libclang_rt.") + Sanitizer + "-" + TC.getArchName() + ".a")); 1729 1730 // Sanitizer runtime may need to come before -lstdc++ (or -lc++, libstdc++.a, 1731 // etc.) so that the linker picks custom versions of the global 'operator 1732 // new' and 'operator delete' symbols. We take the extreme (but simple) 1733 // strategy of inserting it at the front of the link command. It also 1734 // needs to be forced to end up in the executable, so wrap it in 1735 // whole-archive. 1736 SmallVector<const char *, 3> LibSanitizerArgs; 1737 LibSanitizerArgs.push_back("-whole-archive"); 1738 LibSanitizerArgs.push_back(Args.MakeArgString(LibSanitizer)); 1739 LibSanitizerArgs.push_back("-no-whole-archive"); 1740 1741 CmdArgs.insert(BeforeLibStdCXX ? CmdArgs.begin() : CmdArgs.end(), 1742 LibSanitizerArgs.begin(), LibSanitizerArgs.end()); 1743 1744 CmdArgs.push_back("-lpthread"); 1745 CmdArgs.push_back("-lrt"); 1746 CmdArgs.push_back("-ldl"); 1747 1748 // If possible, use a dynamic symbols file to export the symbols from the 1749 // runtime library. If we can't do so, use -export-dynamic instead to export 1750 // all symbols from the binary. 1751 if (ExportSymbols) { 1752 if (llvm::sys::fs::exists(LibSanitizer + ".syms")) 1753 CmdArgs.push_back( 1754 Args.MakeArgString("--dynamic-list=" + LibSanitizer + ".syms")); 1755 else 1756 CmdArgs.push_back("-export-dynamic"); 1757 } 1758} 1759 1760/// If AddressSanitizer is enabled, add appropriate linker flags (Linux). 1761/// This needs to be called before we add the C run-time (malloc, etc). 1762static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args, 1763 ArgStringList &CmdArgs) { 1764 if (TC.getTriple().getEnvironment() == llvm::Triple::Android) { 1765 SmallString<128> LibAsan(TC.getDriver().ResourceDir); 1766 llvm::sys::path::append(LibAsan, "lib", "linux", 1767 (Twine("libclang_rt.asan-") + 1768 TC.getArchName() + "-android.so")); 1769 CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan)); 1770 } else { 1771 if (!Args.hasArg(options::OPT_shared)) 1772 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "asan", true); 1773 } 1774} 1775 1776/// If ThreadSanitizer is enabled, add appropriate linker flags (Linux). 1777/// This needs to be called before we add the C run-time (malloc, etc). 1778static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args, 1779 ArgStringList &CmdArgs) { 1780 if (!Args.hasArg(options::OPT_shared)) 1781 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "tsan", true); 1782} 1783 1784/// If MemorySanitizer is enabled, add appropriate linker flags (Linux). 1785/// This needs to be called before we add the C run-time (malloc, etc). 1786static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args, 1787 ArgStringList &CmdArgs) { 1788 if (!Args.hasArg(options::OPT_shared)) 1789 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "msan", true); 1790} 1791 1792/// If LeakSanitizer is enabled, add appropriate linker flags (Linux). 1793/// This needs to be called before we add the C run-time (malloc, etc). 1794static void addLsanRTLinux(const ToolChain &TC, const ArgList &Args, 1795 ArgStringList &CmdArgs) { 1796 if (!Args.hasArg(options::OPT_shared)) 1797 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "lsan", true); 1798} 1799 1800/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags 1801/// (Linux). 1802static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args, 1803 ArgStringList &CmdArgs, bool IsCXX, 1804 bool HasOtherSanitizerRt) { 1805 // Need a copy of sanitizer_common. This could come from another sanitizer 1806 // runtime; if we're not including one, include our own copy. 1807 if (!HasOtherSanitizerRt) 1808 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "san", true, false); 1809 1810 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan", false); 1811 1812 // Only include the bits of the runtime which need a C++ ABI library if 1813 // we're linking in C++ mode. 1814 if (IsCXX) 1815 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "ubsan_cxx", false); 1816} 1817 1818static void addDfsanRTLinux(const ToolChain &TC, const ArgList &Args, 1819 ArgStringList &CmdArgs) { 1820 if (!Args.hasArg(options::OPT_shared)) 1821 addSanitizerRTLinkFlagsLinux(TC, Args, CmdArgs, "dfsan", true); 1822} 1823 1824static bool shouldUseFramePointerForTarget(const ArgList &Args, 1825 const llvm::Triple &Triple) { 1826 switch (Triple.getArch()) { 1827 // Don't use a frame pointer on linux if optimizing for certain targets. 1828 case llvm::Triple::mips64: 1829 case llvm::Triple::mips64el: 1830 case llvm::Triple::mips: 1831 case llvm::Triple::mipsel: 1832 case llvm::Triple::systemz: 1833 case llvm::Triple::x86: 1834 case llvm::Triple::x86_64: 1835 if (Triple.isOSLinux()) 1836 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 1837 if (!A->getOption().matches(options::OPT_O0)) 1838 return false; 1839 return true; 1840 case llvm::Triple::xcore: 1841 return false; 1842 default: 1843 return true; 1844 } 1845} 1846 1847static bool shouldUseFramePointer(const ArgList &Args, 1848 const llvm::Triple &Triple) { 1849 if (Arg *A = Args.getLastArg(options::OPT_fno_omit_frame_pointer, 1850 options::OPT_fomit_frame_pointer)) 1851 return A->getOption().matches(options::OPT_fno_omit_frame_pointer); 1852 1853 return shouldUseFramePointerForTarget(Args, Triple); 1854} 1855 1856static bool shouldUseLeafFramePointer(const ArgList &Args, 1857 const llvm::Triple &Triple) { 1858 if (Arg *A = Args.getLastArg(options::OPT_mno_omit_leaf_frame_pointer, 1859 options::OPT_momit_leaf_frame_pointer)) 1860 return A->getOption().matches(options::OPT_mno_omit_leaf_frame_pointer); 1861 1862 return shouldUseFramePointerForTarget(Args, Triple); 1863} 1864 1865/// Add a CC1 option to specify the debug compilation directory. 1866static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) { 1867 SmallString<128> cwd; 1868 if (!llvm::sys::fs::current_path(cwd)) { 1869 CmdArgs.push_back("-fdebug-compilation-dir"); 1870 CmdArgs.push_back(Args.MakeArgString(cwd)); 1871 } 1872} 1873 1874static const char *SplitDebugName(const ArgList &Args, 1875 const InputInfoList &Inputs) { 1876 Arg *FinalOutput = Args.getLastArg(options::OPT_o); 1877 if (FinalOutput && Args.hasArg(options::OPT_c)) { 1878 SmallString<128> T(FinalOutput->getValue()); 1879 llvm::sys::path::replace_extension(T, "dwo"); 1880 return Args.MakeArgString(T); 1881 } else { 1882 // Use the compilation dir. 1883 SmallString<128> T(Args.getLastArgValue(options::OPT_fdebug_compilation_dir)); 1884 SmallString<128> F(llvm::sys::path::stem(Inputs[0].getBaseInput())); 1885 llvm::sys::path::replace_extension(F, "dwo"); 1886 T += F; 1887 return Args.MakeArgString(F); 1888 } 1889} 1890 1891static void SplitDebugInfo(const ToolChain &TC, Compilation &C, 1892 const Tool &T, const JobAction &JA, 1893 const ArgList &Args, const InputInfo &Output, 1894 const char *OutFile) { 1895 ArgStringList ExtractArgs; 1896 ExtractArgs.push_back("--extract-dwo"); 1897 1898 ArgStringList StripArgs; 1899 StripArgs.push_back("--strip-dwo"); 1900 1901 // Grabbing the output of the earlier compile step. 1902 StripArgs.push_back(Output.getFilename()); 1903 ExtractArgs.push_back(Output.getFilename()); 1904 ExtractArgs.push_back(OutFile); 1905 1906 const char *Exec = 1907 Args.MakeArgString(TC.GetProgramPath("objcopy")); 1908 1909 // First extract the dwo sections. 1910 C.addCommand(new Command(JA, T, Exec, ExtractArgs)); 1911 1912 // Then remove them from the original .o file. 1913 C.addCommand(new Command(JA, T, Exec, StripArgs)); 1914} 1915 1916static bool isOptimizationLevelFast(const ArgList &Args) { 1917 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) 1918 if (A->getOption().matches(options::OPT_Ofast)) 1919 return true; 1920 return false; 1921} 1922 1923/// \brief Vectorize at all optimization levels greater than 1 except for -Oz. 1924static bool shouldEnableVectorizerAtOLevel(const ArgList &Args) { 1925 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 1926 if (A->getOption().matches(options::OPT_O4) || 1927 A->getOption().matches(options::OPT_Ofast)) 1928 return true; 1929 1930 if (A->getOption().matches(options::OPT_O0)) 1931 return false; 1932 1933 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag"); 1934 1935 // Vectorize -Os. 1936 StringRef S(A->getValue()); 1937 if (S == "s") 1938 return true; 1939 1940 // Don't vectorize -Oz. 1941 if (S == "z") 1942 return false; 1943 1944 unsigned OptLevel = 0; 1945 if (S.getAsInteger(10, OptLevel)) 1946 return false; 1947 1948 return OptLevel > 1; 1949 } 1950 1951 return false; 1952} 1953 1954void Clang::ConstructJob(Compilation &C, const JobAction &JA, 1955 const InputInfo &Output, 1956 const InputInfoList &Inputs, 1957 const ArgList &Args, 1958 const char *LinkingOutput) const { 1959 bool KernelOrKext = Args.hasArg(options::OPT_mkernel, 1960 options::OPT_fapple_kext); 1961 const Driver &D = getToolChain().getDriver(); 1962 ArgStringList CmdArgs; 1963 1964 assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); 1965 1966 // Invoke ourselves in -cc1 mode. 1967 // 1968 // FIXME: Implement custom jobs for internal actions. 1969 CmdArgs.push_back("-cc1"); 1970 1971 // Add the "effective" target triple. 1972 CmdArgs.push_back("-triple"); 1973 std::string TripleStr = getToolChain().ComputeEffectiveClangTriple(Args); 1974 CmdArgs.push_back(Args.MakeArgString(TripleStr)); 1975 1976 // Select the appropriate action. 1977 RewriteKind rewriteKind = RK_None; 1978 1979 if (isa<AnalyzeJobAction>(JA)) { 1980 assert(JA.getType() == types::TY_Plist && "Invalid output type."); 1981 CmdArgs.push_back("-analyze"); 1982 } else if (isa<MigrateJobAction>(JA)) { 1983 CmdArgs.push_back("-migrate"); 1984 } else if (isa<PreprocessJobAction>(JA)) { 1985 if (Output.getType() == types::TY_Dependencies) 1986 CmdArgs.push_back("-Eonly"); 1987 else { 1988 CmdArgs.push_back("-E"); 1989 if (Args.hasArg(options::OPT_rewrite_objc) && 1990 !Args.hasArg(options::OPT_g_Group)) 1991 CmdArgs.push_back("-P"); 1992 } 1993 } else if (isa<AssembleJobAction>(JA)) { 1994 CmdArgs.push_back("-emit-obj"); 1995 1996 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D); 1997 1998 // Also ignore explicit -force_cpusubtype_ALL option. 1999 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL); 2000 } else if (isa<PrecompileJobAction>(JA)) { 2001 // Use PCH if the user requested it. 2002 bool UsePCH = D.CCCUsePCH; 2003 2004 if (JA.getType() == types::TY_Nothing) 2005 CmdArgs.push_back("-fsyntax-only"); 2006 else if (UsePCH) 2007 CmdArgs.push_back("-emit-pch"); 2008 else 2009 CmdArgs.push_back("-emit-pth"); 2010 } else { 2011 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool."); 2012 2013 if (JA.getType() == types::TY_Nothing) { 2014 CmdArgs.push_back("-fsyntax-only"); 2015 } else if (JA.getType() == types::TY_LLVM_IR || 2016 JA.getType() == types::TY_LTO_IR) { 2017 CmdArgs.push_back("-emit-llvm"); 2018 } else if (JA.getType() == types::TY_LLVM_BC || 2019 JA.getType() == types::TY_LTO_BC) { 2020 CmdArgs.push_back("-emit-llvm-bc"); 2021 } else if (JA.getType() == types::TY_PP_Asm) { 2022 CmdArgs.push_back("-S"); 2023 } else if (JA.getType() == types::TY_AST) { 2024 CmdArgs.push_back("-emit-pch"); 2025 } else if (JA.getType() == types::TY_ModuleFile) { 2026 CmdArgs.push_back("-module-file-info"); 2027 } else if (JA.getType() == types::TY_RewrittenObjC) { 2028 CmdArgs.push_back("-rewrite-objc"); 2029 rewriteKind = RK_NonFragile; 2030 } else if (JA.getType() == types::TY_RewrittenLegacyObjC) { 2031 CmdArgs.push_back("-rewrite-objc"); 2032 rewriteKind = RK_Fragile; 2033 } else { 2034 assert(JA.getType() == types::TY_PP_Asm && 2035 "Unexpected output type!"); 2036 } 2037 } 2038 2039 // The make clang go fast button. 2040 CmdArgs.push_back("-disable-free"); 2041 2042 // Disable the verification pass in -asserts builds. 2043#ifdef NDEBUG 2044 CmdArgs.push_back("-disable-llvm-verifier"); 2045#endif 2046 2047 // Set the main file name, so that debug info works even with 2048 // -save-temps. 2049 CmdArgs.push_back("-main-file-name"); 2050 CmdArgs.push_back(getBaseInputName(Args, Inputs)); 2051 2052 // Some flags which affect the language (via preprocessor 2053 // defines). 2054 if (Args.hasArg(options::OPT_static)) 2055 CmdArgs.push_back("-static-define"); 2056 2057 if (isa<AnalyzeJobAction>(JA)) { 2058 // Enable region store model by default. 2059 CmdArgs.push_back("-analyzer-store=region"); 2060 2061 // Treat blocks as analysis entry points. 2062 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks"); 2063 2064 CmdArgs.push_back("-analyzer-eagerly-assume"); 2065 2066 // Add default argument set. 2067 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) { 2068 CmdArgs.push_back("-analyzer-checker=core"); 2069 2070 if (getToolChain().getTriple().getOS() != llvm::Triple::Win32) 2071 CmdArgs.push_back("-analyzer-checker=unix"); 2072 2073 if (getToolChain().getTriple().getVendor() == llvm::Triple::Apple) 2074 CmdArgs.push_back("-analyzer-checker=osx"); 2075 2076 CmdArgs.push_back("-analyzer-checker=deadcode"); 2077 2078 if (types::isCXX(Inputs[0].getType())) 2079 CmdArgs.push_back("-analyzer-checker=cplusplus"); 2080 2081 // Enable the following experimental checkers for testing. 2082 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.UncheckedReturn"); 2083 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.getpw"); 2084 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.gets"); 2085 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mktemp"); 2086 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.mkstemp"); 2087 CmdArgs.push_back("-analyzer-checker=security.insecureAPI.vfork"); 2088 } 2089 2090 // Set the output format. The default is plist, for (lame) historical 2091 // reasons. 2092 CmdArgs.push_back("-analyzer-output"); 2093 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output)) 2094 CmdArgs.push_back(A->getValue()); 2095 else 2096 CmdArgs.push_back("plist"); 2097 2098 // Disable the presentation of standard compiler warnings when 2099 // using --analyze. We only want to show static analyzer diagnostics 2100 // or frontend errors. 2101 CmdArgs.push_back("-w"); 2102 2103 // Add -Xanalyzer arguments when running as analyzer. 2104 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); 2105 } 2106 2107 CheckCodeGenerationOptions(D, Args); 2108 2109 bool PIE = getToolChain().isPIEDefault(); 2110 bool PIC = PIE || getToolChain().isPICDefault(); 2111 bool IsPICLevelTwo = PIC; 2112 2113 // For the PIC and PIE flag options, this logic is different from the 2114 // legacy logic in very old versions of GCC, as that logic was just 2115 // a bug no one had ever fixed. This logic is both more rational and 2116 // consistent with GCC's new logic now that the bugs are fixed. The last 2117 // argument relating to either PIC or PIE wins, and no other argument is 2118 // used. If the last argument is any flavor of the '-fno-...' arguments, 2119 // both PIC and PIE are disabled. Any PIE option implicitly enables PIC 2120 // at the same level. 2121 Arg *LastPICArg =Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, 2122 options::OPT_fpic, options::OPT_fno_pic, 2123 options::OPT_fPIE, options::OPT_fno_PIE, 2124 options::OPT_fpie, options::OPT_fno_pie); 2125 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness 2126 // is forced, then neither PIC nor PIE flags will have no effect. 2127 if (!getToolChain().isPICDefaultForced()) { 2128 if (LastPICArg) { 2129 Option O = LastPICArg->getOption(); 2130 if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) || 2131 O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) { 2132 PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie); 2133 PIC = PIE || O.matches(options::OPT_fPIC) || 2134 O.matches(options::OPT_fpic); 2135 IsPICLevelTwo = O.matches(options::OPT_fPIE) || 2136 O.matches(options::OPT_fPIC); 2137 } else { 2138 PIE = PIC = false; 2139 } 2140 } 2141 } 2142 2143 // Introduce a Darwin-specific hack. If the default is PIC but the flags 2144 // specified while enabling PIC enabled level 1 PIC, just force it back to 2145 // level 2 PIC instead. This matches the behavior of Darwin GCC (based on my 2146 // informal testing). 2147 if (PIC && getToolChain().getTriple().isOSDarwin()) 2148 IsPICLevelTwo |= getToolChain().isPICDefault(); 2149 2150 // Note that these flags are trump-cards. Regardless of the order w.r.t. the 2151 // PIC or PIE options above, if these show up, PIC is disabled. 2152 llvm::Triple Triple(TripleStr); 2153 if (KernelOrKext && 2154 (!Triple.isiOS() || Triple.isOSVersionLT(6))) 2155 PIC = PIE = false; 2156 if (Args.hasArg(options::OPT_static)) 2157 PIC = PIE = false; 2158 2159 if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) { 2160 // This is a very special mode. It trumps the other modes, almost no one 2161 // uses it, and it isn't even valid on any OS but Darwin. 2162 if (!getToolChain().getTriple().isOSDarwin()) 2163 D.Diag(diag::err_drv_unsupported_opt_for_target) 2164 << A->getSpelling() << getToolChain().getTriple().str(); 2165 2166 // FIXME: Warn when this flag trumps some other PIC or PIE flag. 2167 2168 CmdArgs.push_back("-mrelocation-model"); 2169 CmdArgs.push_back("dynamic-no-pic"); 2170 2171 // Only a forced PIC mode can cause the actual compile to have PIC defines 2172 // etc., no flags are sufficient. This behavior was selected to closely 2173 // match that of llvm-gcc and Apple GCC before that. 2174 if (getToolChain().isPICDefault() && getToolChain().isPICDefaultForced()) { 2175 CmdArgs.push_back("-pic-level"); 2176 CmdArgs.push_back("2"); 2177 } 2178 } else { 2179 // Currently, LLVM only knows about PIC vs. static; the PIE differences are 2180 // handled in Clang's IRGen by the -pie-level flag. 2181 CmdArgs.push_back("-mrelocation-model"); 2182 CmdArgs.push_back(PIC ? "pic" : "static"); 2183 2184 if (PIC) { 2185 CmdArgs.push_back("-pic-level"); 2186 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1"); 2187 if (PIE) { 2188 CmdArgs.push_back("-pie-level"); 2189 CmdArgs.push_back(IsPICLevelTwo ? "2" : "1"); 2190 } 2191 } 2192 } 2193 2194 if (!Args.hasFlag(options::OPT_fmerge_all_constants, 2195 options::OPT_fno_merge_all_constants)) 2196 CmdArgs.push_back("-fno-merge-all-constants"); 2197 2198 // LLVM Code Generator Options. 2199 2200 if (Arg *A = Args.getLastArg(options::OPT_mregparm_EQ)) { 2201 CmdArgs.push_back("-mregparm"); 2202 CmdArgs.push_back(A->getValue()); 2203 } 2204 2205 if (Arg *A = Args.getLastArg(options::OPT_fpcc_struct_return, 2206 options::OPT_freg_struct_return)) { 2207 if (getToolChain().getArch() != llvm::Triple::x86) { 2208 D.Diag(diag::err_drv_unsupported_opt_for_target) 2209 << A->getSpelling() << getToolChain().getTriple().str(); 2210 } else if (A->getOption().matches(options::OPT_fpcc_struct_return)) { 2211 CmdArgs.push_back("-fpcc-struct-return"); 2212 } else { 2213 assert(A->getOption().matches(options::OPT_freg_struct_return)); 2214 CmdArgs.push_back("-freg-struct-return"); 2215 } 2216 } 2217 2218 if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) 2219 CmdArgs.push_back("-mrtd"); 2220 2221 if (shouldUseFramePointer(Args, getToolChain().getTriple())) 2222 CmdArgs.push_back("-mdisable-fp-elim"); 2223 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss, 2224 options::OPT_fno_zero_initialized_in_bss)) 2225 CmdArgs.push_back("-mno-zero-initialized-in-bss"); 2226 2227 bool OFastEnabled = isOptimizationLevelFast(Args); 2228 // If -Ofast is the optimization level, then -fstrict-aliasing should be 2229 // enabled. This alias option is being used to simplify the hasFlag logic. 2230 OptSpecifier StrictAliasingAliasOption = OFastEnabled ? options::OPT_Ofast : 2231 options::OPT_fstrict_aliasing; 2232 if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption, 2233 options::OPT_fno_strict_aliasing, 2234 getToolChain().IsStrictAliasingDefault())) 2235 CmdArgs.push_back("-relaxed-aliasing"); 2236 if (!Args.hasFlag(options::OPT_fstruct_path_tbaa, 2237 options::OPT_fno_struct_path_tbaa)) 2238 CmdArgs.push_back("-no-struct-path-tbaa"); 2239 if (Args.hasFlag(options::OPT_fstrict_enums, options::OPT_fno_strict_enums, 2240 false)) 2241 CmdArgs.push_back("-fstrict-enums"); 2242 if (!Args.hasFlag(options::OPT_foptimize_sibling_calls, 2243 options::OPT_fno_optimize_sibling_calls)) 2244 CmdArgs.push_back("-mdisable-tail-calls"); 2245 2246 // Handle segmented stacks. 2247 if (Args.hasArg(options::OPT_fsplit_stack)) 2248 CmdArgs.push_back("-split-stacks"); 2249 2250 // If -Ofast is the optimization level, then -ffast-math should be enabled. 2251 // This alias option is being used to simplify the getLastArg logic. 2252 OptSpecifier FastMathAliasOption = OFastEnabled ? options::OPT_Ofast : 2253 options::OPT_ffast_math; 2254 2255 // Handle various floating point optimization flags, mapping them to the 2256 // appropriate LLVM code generation flags. The pattern for all of these is to 2257 // default off the codegen optimizations, and if any flag enables them and no 2258 // flag disables them after the flag enabling them, enable the codegen 2259 // optimization. This is complicated by several "umbrella" flags. 2260 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2261 options::OPT_fno_fast_math, 2262 options::OPT_ffinite_math_only, 2263 options::OPT_fno_finite_math_only, 2264 options::OPT_fhonor_infinities, 2265 options::OPT_fno_honor_infinities)) 2266 if (A->getOption().getID() != options::OPT_fno_fast_math && 2267 A->getOption().getID() != options::OPT_fno_finite_math_only && 2268 A->getOption().getID() != options::OPT_fhonor_infinities) 2269 CmdArgs.push_back("-menable-no-infs"); 2270 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2271 options::OPT_fno_fast_math, 2272 options::OPT_ffinite_math_only, 2273 options::OPT_fno_finite_math_only, 2274 options::OPT_fhonor_nans, 2275 options::OPT_fno_honor_nans)) 2276 if (A->getOption().getID() != options::OPT_fno_fast_math && 2277 A->getOption().getID() != options::OPT_fno_finite_math_only && 2278 A->getOption().getID() != options::OPT_fhonor_nans) 2279 CmdArgs.push_back("-menable-no-nans"); 2280 2281 // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes. 2282 bool MathErrno = getToolChain().IsMathErrnoDefault(); 2283 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2284 options::OPT_fno_fast_math, 2285 options::OPT_fmath_errno, 2286 options::OPT_fno_math_errno)) { 2287 // Turning on -ffast_math (with either flag) removes the need for MathErrno. 2288 // However, turning *off* -ffast_math merely restores the toolchain default 2289 // (which may be false). 2290 if (A->getOption().getID() == options::OPT_fno_math_errno || 2291 A->getOption().getID() == options::OPT_ffast_math || 2292 A->getOption().getID() == options::OPT_Ofast) 2293 MathErrno = false; 2294 else if (A->getOption().getID() == options::OPT_fmath_errno) 2295 MathErrno = true; 2296 } 2297 if (MathErrno) 2298 CmdArgs.push_back("-fmath-errno"); 2299 2300 // There are several flags which require disabling very specific 2301 // optimizations. Any of these being disabled forces us to turn off the 2302 // entire set of LLVM optimizations, so collect them through all the flag 2303 // madness. 2304 bool AssociativeMath = false; 2305 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2306 options::OPT_fno_fast_math, 2307 options::OPT_funsafe_math_optimizations, 2308 options::OPT_fno_unsafe_math_optimizations, 2309 options::OPT_fassociative_math, 2310 options::OPT_fno_associative_math)) 2311 if (A->getOption().getID() != options::OPT_fno_fast_math && 2312 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2313 A->getOption().getID() != options::OPT_fno_associative_math) 2314 AssociativeMath = true; 2315 bool ReciprocalMath = false; 2316 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2317 options::OPT_fno_fast_math, 2318 options::OPT_funsafe_math_optimizations, 2319 options::OPT_fno_unsafe_math_optimizations, 2320 options::OPT_freciprocal_math, 2321 options::OPT_fno_reciprocal_math)) 2322 if (A->getOption().getID() != options::OPT_fno_fast_math && 2323 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2324 A->getOption().getID() != options::OPT_fno_reciprocal_math) 2325 ReciprocalMath = true; 2326 bool SignedZeros = true; 2327 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2328 options::OPT_fno_fast_math, 2329 options::OPT_funsafe_math_optimizations, 2330 options::OPT_fno_unsafe_math_optimizations, 2331 options::OPT_fsigned_zeros, 2332 options::OPT_fno_signed_zeros)) 2333 if (A->getOption().getID() != options::OPT_fno_fast_math && 2334 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2335 A->getOption().getID() != options::OPT_fsigned_zeros) 2336 SignedZeros = false; 2337 bool TrappingMath = true; 2338 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2339 options::OPT_fno_fast_math, 2340 options::OPT_funsafe_math_optimizations, 2341 options::OPT_fno_unsafe_math_optimizations, 2342 options::OPT_ftrapping_math, 2343 options::OPT_fno_trapping_math)) 2344 if (A->getOption().getID() != options::OPT_fno_fast_math && 2345 A->getOption().getID() != options::OPT_fno_unsafe_math_optimizations && 2346 A->getOption().getID() != options::OPT_ftrapping_math) 2347 TrappingMath = false; 2348 if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros && 2349 !TrappingMath) 2350 CmdArgs.push_back("-menable-unsafe-fp-math"); 2351 2352 2353 // Validate and pass through -fp-contract option. 2354 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2355 options::OPT_fno_fast_math, 2356 options::OPT_ffp_contract)) { 2357 if (A->getOption().getID() == options::OPT_ffp_contract) { 2358 StringRef Val = A->getValue(); 2359 if (Val == "fast" || Val == "on" || Val == "off") { 2360 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val)); 2361 } else { 2362 D.Diag(diag::err_drv_unsupported_option_argument) 2363 << A->getOption().getName() << Val; 2364 } 2365 } else if (A->getOption().matches(options::OPT_ffast_math) || 2366 (OFastEnabled && A->getOption().matches(options::OPT_Ofast))) { 2367 // If fast-math is set then set the fp-contract mode to fast. 2368 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast")); 2369 } 2370 } 2371 2372 // We separately look for the '-ffast-math' and '-ffinite-math-only' flags, 2373 // and if we find them, tell the frontend to provide the appropriate 2374 // preprocessor macros. This is distinct from enabling any optimizations as 2375 // these options induce language changes which must survive serialization 2376 // and deserialization, etc. 2377 if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption, 2378 options::OPT_fno_fast_math)) 2379 if (!A->getOption().matches(options::OPT_fno_fast_math)) 2380 CmdArgs.push_back("-ffast-math"); 2381 if (Arg *A = Args.getLastArg(options::OPT_ffinite_math_only, options::OPT_fno_fast_math)) 2382 if (A->getOption().matches(options::OPT_ffinite_math_only)) 2383 CmdArgs.push_back("-ffinite-math-only"); 2384 2385 // Decide whether to use verbose asm. Verbose assembly is the default on 2386 // toolchains which have the integrated assembler on by default. 2387 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault(); 2388 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm, 2389 IsVerboseAsmDefault) || 2390 Args.hasArg(options::OPT_dA)) 2391 CmdArgs.push_back("-masm-verbose"); 2392 2393 if (Args.hasArg(options::OPT_fdebug_pass_structure)) { 2394 CmdArgs.push_back("-mdebug-pass"); 2395 CmdArgs.push_back("Structure"); 2396 } 2397 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) { 2398 CmdArgs.push_back("-mdebug-pass"); 2399 CmdArgs.push_back("Arguments"); 2400 } 2401 2402 // Enable -mconstructor-aliases except on darwin, where we have to 2403 // work around a linker bug; see <rdar://problem/7651567>. 2404 if (!getToolChain().getTriple().isOSDarwin()) 2405 CmdArgs.push_back("-mconstructor-aliases"); 2406 2407 // Darwin's kernel doesn't support guard variables; just die if we 2408 // try to use them. 2409 if (KernelOrKext && getToolChain().getTriple().isOSDarwin()) 2410 CmdArgs.push_back("-fforbid-guard-variables"); 2411 2412 if (Args.hasArg(options::OPT_mms_bitfields)) { 2413 CmdArgs.push_back("-mms-bitfields"); 2414 } 2415 2416 // This is a coarse approximation of what llvm-gcc actually does, both 2417 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more 2418 // complicated ways. 2419 bool AsynchronousUnwindTables = 2420 Args.hasFlag(options::OPT_fasynchronous_unwind_tables, 2421 options::OPT_fno_asynchronous_unwind_tables, 2422 getToolChain().IsUnwindTablesDefault() && 2423 !KernelOrKext); 2424 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables, 2425 AsynchronousUnwindTables)) 2426 CmdArgs.push_back("-munwind-tables"); 2427 2428 getToolChain().addClangTargetOptions(Args, CmdArgs); 2429 2430 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { 2431 CmdArgs.push_back("-mlimit-float-precision"); 2432 CmdArgs.push_back(A->getValue()); 2433 } 2434 2435 // FIXME: Handle -mtune=. 2436 (void) Args.hasArg(options::OPT_mtune_EQ); 2437 2438 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) { 2439 CmdArgs.push_back("-mcode-model"); 2440 CmdArgs.push_back(A->getValue()); 2441 } 2442 2443 // Add the target cpu 2444 std::string ETripleStr = getToolChain().ComputeEffectiveClangTriple(Args); 2445 llvm::Triple ETriple(ETripleStr); 2446 std::string CPU = getCPUName(Args, ETriple); 2447 if (!CPU.empty()) { 2448 CmdArgs.push_back("-target-cpu"); 2449 CmdArgs.push_back(Args.MakeArgString(CPU)); 2450 } 2451 2452 if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) { 2453 CmdArgs.push_back("-mfpmath"); 2454 CmdArgs.push_back(A->getValue()); 2455 } 2456 2457 // Add the target features 2458 getTargetFeatures(D, ETriple, Args, CmdArgs); 2459 2460 // Add target specific flags. 2461 switch(getToolChain().getArch()) { 2462 default: 2463 break; 2464 2465 case llvm::Triple::arm: 2466 case llvm::Triple::thumb: 2467 AddARMTargetArgs(Args, CmdArgs, KernelOrKext); 2468 break; 2469 2470 case llvm::Triple::mips: 2471 case llvm::Triple::mipsel: 2472 case llvm::Triple::mips64: 2473 case llvm::Triple::mips64el: 2474 AddMIPSTargetArgs(Args, CmdArgs); 2475 break; 2476 2477 case llvm::Triple::sparc: 2478 AddSparcTargetArgs(Args, CmdArgs); 2479 break; 2480 2481 case llvm::Triple::x86: 2482 case llvm::Triple::x86_64: 2483 AddX86TargetArgs(Args, CmdArgs); 2484 break; 2485 2486 case llvm::Triple::hexagon: 2487 AddHexagonTargetArgs(Args, CmdArgs); 2488 break; 2489 } 2490 2491 // Add clang-cl arguments. 2492 if (getToolChain().getDriver().IsCLMode()) 2493 AddClangCLArgs(Args, CmdArgs); 2494 2495 // Pass the linker version in use. 2496 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 2497 CmdArgs.push_back("-target-linker-version"); 2498 CmdArgs.push_back(A->getValue()); 2499 } 2500 2501 if (!shouldUseLeafFramePointer(Args, getToolChain().getTriple())) 2502 CmdArgs.push_back("-momit-leaf-frame-pointer"); 2503 2504 // Explicitly error on some things we know we don't support and can't just 2505 // ignore. 2506 types::ID InputType = Inputs[0].getType(); 2507 if (!Args.hasArg(options::OPT_fallow_unsupported)) { 2508 Arg *Unsupported; 2509 if (types::isCXX(InputType) && 2510 getToolChain().getTriple().isOSDarwin() && 2511 getToolChain().getArch() == llvm::Triple::x86) { 2512 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)) || 2513 (Unsupported = Args.getLastArg(options::OPT_mkernel))) 2514 D.Diag(diag::err_drv_clang_unsupported_opt_cxx_darwin_i386) 2515 << Unsupported->getOption().getName(); 2516 } 2517 } 2518 2519 Args.AddAllArgs(CmdArgs, options::OPT_v); 2520 Args.AddLastArg(CmdArgs, options::OPT_H); 2521 if (D.CCPrintHeaders && !D.CCGenDiagnostics) { 2522 CmdArgs.push_back("-header-include-file"); 2523 CmdArgs.push_back(D.CCPrintHeadersFilename ? 2524 D.CCPrintHeadersFilename : "-"); 2525 } 2526 Args.AddLastArg(CmdArgs, options::OPT_P); 2527 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout); 2528 2529 if (D.CCLogDiagnostics && !D.CCGenDiagnostics) { 2530 CmdArgs.push_back("-diagnostic-log-file"); 2531 CmdArgs.push_back(D.CCLogDiagnosticsFilename ? 2532 D.CCLogDiagnosticsFilename : "-"); 2533 } 2534 2535 // Use the last option from "-g" group. "-gline-tables-only" 2536 // is preserved, all other debug options are substituted with "-g". 2537 Args.ClaimAllArgs(options::OPT_g_Group); 2538 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) { 2539 if (A->getOption().matches(options::OPT_gline_tables_only)) 2540 CmdArgs.push_back("-gline-tables-only"); 2541 else if (A->getOption().matches(options::OPT_gdwarf_2)) 2542 CmdArgs.push_back("-gdwarf-2"); 2543 else if (A->getOption().matches(options::OPT_gdwarf_3)) 2544 CmdArgs.push_back("-gdwarf-3"); 2545 else if (A->getOption().matches(options::OPT_gdwarf_4)) 2546 CmdArgs.push_back("-gdwarf-4"); 2547 else if (!A->getOption().matches(options::OPT_g0) && 2548 !A->getOption().matches(options::OPT_ggdb0)) { 2549 // Default is dwarf-2 for darwin. 2550 if (getToolChain().getTriple().isOSDarwin()) 2551 CmdArgs.push_back("-gdwarf-2"); 2552 else 2553 CmdArgs.push_back("-g"); 2554 } 2555 } 2556 2557 // We ignore flags -gstrict-dwarf and -grecord-gcc-switches for now. 2558 Args.ClaimAllArgs(options::OPT_g_flags_Group); 2559 if (Args.hasArg(options::OPT_gcolumn_info)) 2560 CmdArgs.push_back("-dwarf-column-info"); 2561 2562 // FIXME: Move backend command line options to the module. 2563 // -gsplit-dwarf should turn on -g and enable the backend dwarf 2564 // splitting and extraction. 2565 // FIXME: Currently only works on Linux. 2566 if (getToolChain().getTriple().isOSLinux() && 2567 Args.hasArg(options::OPT_gsplit_dwarf)) { 2568 CmdArgs.push_back("-g"); 2569 CmdArgs.push_back("-backend-option"); 2570 CmdArgs.push_back("-split-dwarf=Enable"); 2571 } 2572 2573 // -ggnu-pubnames turns on gnu style pubnames in the backend. 2574 if (Args.hasArg(options::OPT_ggnu_pubnames)) { 2575 CmdArgs.push_back("-backend-option"); 2576 CmdArgs.push_back("-generate-gnu-dwarf-pub-sections"); 2577 } 2578 2579 Args.AddAllArgs(CmdArgs, options::OPT_fdebug_types_section); 2580 2581 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections); 2582 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections); 2583 2584 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions); 2585 2586 if (Args.hasArg(options::OPT_ftest_coverage) || 2587 Args.hasArg(options::OPT_coverage)) 2588 CmdArgs.push_back("-femit-coverage-notes"); 2589 if (Args.hasArg(options::OPT_fprofile_arcs) || 2590 Args.hasArg(options::OPT_coverage)) 2591 CmdArgs.push_back("-femit-coverage-data"); 2592 2593 if (C.getArgs().hasArg(options::OPT_c) || 2594 C.getArgs().hasArg(options::OPT_S)) { 2595 if (Output.isFilename()) { 2596 CmdArgs.push_back("-coverage-file"); 2597 SmallString<128> CoverageFilename(Output.getFilename()); 2598 if (llvm::sys::path::is_relative(CoverageFilename.str())) { 2599 SmallString<128> Pwd; 2600 if (!llvm::sys::fs::current_path(Pwd)) { 2601 llvm::sys::path::append(Pwd, CoverageFilename.str()); 2602 CoverageFilename.swap(Pwd); 2603 } 2604 } 2605 CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); 2606 } 2607 } 2608 2609 // Pass options for controlling the default header search paths. 2610 if (Args.hasArg(options::OPT_nostdinc)) { 2611 CmdArgs.push_back("-nostdsysteminc"); 2612 CmdArgs.push_back("-nobuiltininc"); 2613 } else { 2614 if (Args.hasArg(options::OPT_nostdlibinc)) 2615 CmdArgs.push_back("-nostdsysteminc"); 2616 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx); 2617 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc); 2618 } 2619 2620 // Pass the path to compiler resource files. 2621 CmdArgs.push_back("-resource-dir"); 2622 CmdArgs.push_back(D.ResourceDir.c_str()); 2623 2624 Args.AddLastArg(CmdArgs, options::OPT_working_directory); 2625 2626 bool ARCMTEnabled = false; 2627 if (!Args.hasArg(options::OPT_fno_objc_arc, options::OPT_fobjc_arc)) { 2628 if (const Arg *A = Args.getLastArg(options::OPT_ccc_arcmt_check, 2629 options::OPT_ccc_arcmt_modify, 2630 options::OPT_ccc_arcmt_migrate)) { 2631 ARCMTEnabled = true; 2632 switch (A->getOption().getID()) { 2633 default: 2634 llvm_unreachable("missed a case"); 2635 case options::OPT_ccc_arcmt_check: 2636 CmdArgs.push_back("-arcmt-check"); 2637 break; 2638 case options::OPT_ccc_arcmt_modify: 2639 CmdArgs.push_back("-arcmt-modify"); 2640 break; 2641 case options::OPT_ccc_arcmt_migrate: 2642 CmdArgs.push_back("-arcmt-migrate"); 2643 CmdArgs.push_back("-mt-migrate-directory"); 2644 CmdArgs.push_back(A->getValue()); 2645 2646 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_report_output); 2647 Args.AddLastArg(CmdArgs, options::OPT_arcmt_migrate_emit_arc_errors); 2648 break; 2649 } 2650 } 2651 } else { 2652 Args.ClaimAllArgs(options::OPT_ccc_arcmt_check); 2653 Args.ClaimAllArgs(options::OPT_ccc_arcmt_modify); 2654 Args.ClaimAllArgs(options::OPT_ccc_arcmt_migrate); 2655 } 2656 2657 if (const Arg *A = Args.getLastArg(options::OPT_ccc_objcmt_migrate)) { 2658 if (ARCMTEnabled) { 2659 D.Diag(diag::err_drv_argument_not_allowed_with) 2660 << A->getAsString(Args) << "-ccc-arcmt-migrate"; 2661 } 2662 CmdArgs.push_back("-mt-migrate-directory"); 2663 CmdArgs.push_back(A->getValue()); 2664 2665 if (!Args.hasArg(options::OPT_objcmt_migrate_literals, 2666 options::OPT_objcmt_migrate_subscripting, 2667 options::OPT_objcmt_migrate_property)) { 2668 // None specified, means enable them all. 2669 CmdArgs.push_back("-objcmt-migrate-literals"); 2670 CmdArgs.push_back("-objcmt-migrate-subscripting"); 2671 CmdArgs.push_back("-objcmt-migrate-property"); 2672 } else { 2673 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_literals); 2674 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_subscripting); 2675 Args.AddLastArg(CmdArgs, options::OPT_objcmt_migrate_property); 2676 } 2677 } 2678 2679 // Add preprocessing options like -I, -D, etc. if we are using the 2680 // preprocessor. 2681 // 2682 // FIXME: Support -fpreprocessed 2683 if (types::getPreprocessedType(InputType) != types::TY_INVALID) 2684 AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs); 2685 2686 // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes 2687 // that "The compiler can only warn and ignore the option if not recognized". 2688 // When building with ccache, it will pass -D options to clang even on 2689 // preprocessed inputs and configure concludes that -fPIC is not supported. 2690 Args.ClaimAllArgs(options::OPT_D); 2691 2692 // Manually translate -O4 to -O3; let clang reject others. 2693 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 2694 if (A->getOption().matches(options::OPT_O4)) { 2695 CmdArgs.push_back("-O3"); 2696 D.Diag(diag::warn_O4_is_O3); 2697 } else { 2698 A->render(Args, CmdArgs); 2699 } 2700 } 2701 2702 // Don't warn about unused -flto. This can happen when we're preprocessing or 2703 // precompiling. 2704 Args.ClaimAllArgs(options::OPT_flto); 2705 2706 Args.AddAllArgs(CmdArgs, options::OPT_W_Group); 2707 if (Args.hasFlag(options::OPT_pedantic, options::OPT_no_pedantic, false)) 2708 CmdArgs.push_back("-pedantic"); 2709 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors); 2710 Args.AddLastArg(CmdArgs, options::OPT_w); 2711 2712 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} 2713 // (-ansi is equivalent to -std=c89 or -std=c++98). 2714 // 2715 // If a std is supplied, only add -trigraphs if it follows the 2716 // option. 2717 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) { 2718 if (Std->getOption().matches(options::OPT_ansi)) 2719 if (types::isCXX(InputType)) 2720 CmdArgs.push_back("-std=c++98"); 2721 else 2722 CmdArgs.push_back("-std=c89"); 2723 else 2724 Std->render(Args, CmdArgs); 2725 2726 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi, 2727 options::OPT_trigraphs)) 2728 if (A != Std) 2729 A->render(Args, CmdArgs); 2730 } else { 2731 // Honor -std-default. 2732 // 2733 // FIXME: Clang doesn't correctly handle -std= when the input language 2734 // doesn't match. For the time being just ignore this for C++ inputs; 2735 // eventually we want to do all the standard defaulting here instead of 2736 // splitting it between the driver and clang -cc1. 2737 if (!types::isCXX(InputType)) 2738 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ, 2739 "-std=", /*Joined=*/true); 2740 else if (getToolChain().getTriple().getOS() == llvm::Triple::Win32) 2741 CmdArgs.push_back("-std=c++11"); 2742 2743 Args.AddLastArg(CmdArgs, options::OPT_trigraphs); 2744 } 2745 2746 // GCC's behavior for -Wwrite-strings is a bit strange: 2747 // * In C, this "warning flag" changes the types of string literals from 2748 // 'char[N]' to 'const char[N]', and thus triggers an unrelated warning 2749 // for the discarded qualifier. 2750 // * In C++, this is just a normal warning flag. 2751 // 2752 // Implementing this warning correctly in C is hard, so we follow GCC's 2753 // behavior for now. FIXME: Directly diagnose uses of a string literal as 2754 // a non-const char* in C, rather than using this crude hack. 2755 if (!types::isCXX(InputType)) { 2756 // FIXME: This should behave just like a warning flag, and thus should also 2757 // respect -Weverything, -Wno-everything, -Werror=write-strings, and so on. 2758 Arg *WriteStrings = 2759 Args.getLastArg(options::OPT_Wwrite_strings, 2760 options::OPT_Wno_write_strings, options::OPT_w); 2761 if (WriteStrings && 2762 WriteStrings->getOption().matches(options::OPT_Wwrite_strings)) 2763 CmdArgs.push_back("-fconst-strings"); 2764 } 2765 2766 // GCC provides a macro definition '__DEPRECATED' when -Wdeprecated is active 2767 // during C++ compilation, which it is by default. GCC keeps this define even 2768 // in the presence of '-w', match this behavior bug-for-bug. 2769 if (types::isCXX(InputType) && 2770 Args.hasFlag(options::OPT_Wdeprecated, options::OPT_Wno_deprecated, 2771 true)) { 2772 CmdArgs.push_back("-fdeprecated-macro"); 2773 } 2774 2775 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'. 2776 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) { 2777 if (Asm->getOption().matches(options::OPT_fasm)) 2778 CmdArgs.push_back("-fgnu-keywords"); 2779 else 2780 CmdArgs.push_back("-fno-gnu-keywords"); 2781 } 2782 2783 if (ShouldDisableCFI(Args, getToolChain())) 2784 CmdArgs.push_back("-fno-dwarf2-cfi-asm"); 2785 2786 if (ShouldDisableDwarfDirectory(Args, getToolChain())) 2787 CmdArgs.push_back("-fno-dwarf-directory-asm"); 2788 2789 if (ShouldDisableAutolink(Args, getToolChain())) 2790 CmdArgs.push_back("-fno-autolink"); 2791 2792 // Add in -fdebug-compilation-dir if necessary. 2793 addDebugCompDirArg(Args, CmdArgs); 2794 2795 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_, 2796 options::OPT_ftemplate_depth_EQ)) { 2797 CmdArgs.push_back("-ftemplate-depth"); 2798 CmdArgs.push_back(A->getValue()); 2799 } 2800 2801 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_depth_EQ)) { 2802 CmdArgs.push_back("-fconstexpr-depth"); 2803 CmdArgs.push_back(A->getValue()); 2804 } 2805 2806 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_steps_EQ)) { 2807 CmdArgs.push_back("-fconstexpr-steps"); 2808 CmdArgs.push_back(A->getValue()); 2809 } 2810 2811 if (Arg *A = Args.getLastArg(options::OPT_fbracket_depth_EQ)) { 2812 CmdArgs.push_back("-fbracket-depth"); 2813 CmdArgs.push_back(A->getValue()); 2814 } 2815 2816 if (Arg *A = Args.getLastArg(options::OPT_Wlarge_by_value_copy_EQ, 2817 options::OPT_Wlarge_by_value_copy_def)) { 2818 if (A->getNumValues()) { 2819 StringRef bytes = A->getValue(); 2820 CmdArgs.push_back(Args.MakeArgString("-Wlarge-by-value-copy=" + bytes)); 2821 } else 2822 CmdArgs.push_back("-Wlarge-by-value-copy=64"); // default value 2823 } 2824 2825 2826 if (Args.hasArg(options::OPT_relocatable_pch)) 2827 CmdArgs.push_back("-relocatable-pch"); 2828 2829 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) { 2830 CmdArgs.push_back("-fconstant-string-class"); 2831 CmdArgs.push_back(A->getValue()); 2832 } 2833 2834 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) { 2835 CmdArgs.push_back("-ftabstop"); 2836 CmdArgs.push_back(A->getValue()); 2837 } 2838 2839 CmdArgs.push_back("-ferror-limit"); 2840 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ)) 2841 CmdArgs.push_back(A->getValue()); 2842 else 2843 CmdArgs.push_back("19"); 2844 2845 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) { 2846 CmdArgs.push_back("-fmacro-backtrace-limit"); 2847 CmdArgs.push_back(A->getValue()); 2848 } 2849 2850 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) { 2851 CmdArgs.push_back("-ftemplate-backtrace-limit"); 2852 CmdArgs.push_back(A->getValue()); 2853 } 2854 2855 if (Arg *A = Args.getLastArg(options::OPT_fconstexpr_backtrace_limit_EQ)) { 2856 CmdArgs.push_back("-fconstexpr-backtrace-limit"); 2857 CmdArgs.push_back(A->getValue()); 2858 } 2859 2860 // Pass -fmessage-length=. 2861 CmdArgs.push_back("-fmessage-length"); 2862 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) { 2863 CmdArgs.push_back(A->getValue()); 2864 } else { 2865 // If -fmessage-length=N was not specified, determine whether this is a 2866 // terminal and, if so, implicitly define -fmessage-length appropriately. 2867 unsigned N = llvm::sys::Process::StandardErrColumns(); 2868 CmdArgs.push_back(Args.MakeArgString(Twine(N))); 2869 } 2870 2871 // -fvisibility= and -fvisibility-ms-compat are of a piece. 2872 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ, 2873 options::OPT_fvisibility_ms_compat)) { 2874 if (A->getOption().matches(options::OPT_fvisibility_EQ)) { 2875 CmdArgs.push_back("-fvisibility"); 2876 CmdArgs.push_back(A->getValue()); 2877 } else { 2878 assert(A->getOption().matches(options::OPT_fvisibility_ms_compat)); 2879 CmdArgs.push_back("-fvisibility"); 2880 CmdArgs.push_back("hidden"); 2881 CmdArgs.push_back("-ftype-visibility"); 2882 CmdArgs.push_back("default"); 2883 } 2884 } 2885 2886 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden); 2887 2888 Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ); 2889 2890 // -fhosted is default. 2891 if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) || 2892 KernelOrKext) 2893 CmdArgs.push_back("-ffreestanding"); 2894 2895 // Forward -f (flag) options which we can pass directly. 2896 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); 2897 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); 2898 Args.AddLastArg(CmdArgs, options::OPT_flimit_debug_info); 2899 Args.AddLastArg(CmdArgs, options::OPT_fno_limit_debug_info); 2900 Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names); 2901 // AltiVec language extensions aren't relevant for assembling. 2902 if (!isa<PreprocessJobAction>(JA) || 2903 Output.getType() != types::TY_PP_Asm) 2904 Args.AddLastArg(CmdArgs, options::OPT_faltivec); 2905 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_show_template_tree); 2906 Args.AddLastArg(CmdArgs, options::OPT_fno_elide_type); 2907 2908 const SanitizerArgs &Sanitize = D.getOrParseSanitizerArgs(Args); 2909 Sanitize.addArgs(getToolChain(), Args, CmdArgs); 2910 2911 if (!Args.hasFlag(options::OPT_fsanitize_recover, 2912 options::OPT_fno_sanitize_recover, 2913 true)) 2914 CmdArgs.push_back("-fno-sanitize-recover"); 2915 2916 if (Args.hasArg(options::OPT_fcatch_undefined_behavior) || 2917 Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error, 2918 options::OPT_fno_sanitize_undefined_trap_on_error, false)) 2919 CmdArgs.push_back("-fsanitize-undefined-trap-on-error"); 2920 2921 // Report an error for -faltivec on anything other than PowerPC. 2922 if (const Arg *A = Args.getLastArg(options::OPT_faltivec)) 2923 if (!(getToolChain().getArch() == llvm::Triple::ppc || 2924 getToolChain().getArch() == llvm::Triple::ppc64 || 2925 getToolChain().getArch() == llvm::Triple::ppc64le)) 2926 D.Diag(diag::err_drv_argument_only_allowed_with) 2927 << A->getAsString(Args) << "ppc/ppc64/ppc64le"; 2928 2929 if (getToolChain().SupportsProfiling()) 2930 Args.AddLastArg(CmdArgs, options::OPT_pg); 2931 2932 // -flax-vector-conversions is default. 2933 if (!Args.hasFlag(options::OPT_flax_vector_conversions, 2934 options::OPT_fno_lax_vector_conversions)) 2935 CmdArgs.push_back("-fno-lax-vector-conversions"); 2936 2937 if (Args.getLastArg(options::OPT_fapple_kext)) 2938 CmdArgs.push_back("-fapple-kext"); 2939 2940 if (Args.hasFlag(options::OPT_frewrite_includes, 2941 options::OPT_fno_rewrite_includes, false)) 2942 CmdArgs.push_back("-frewrite-includes"); 2943 2944 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch); 2945 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info); 2946 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_parseable_fixits); 2947 Args.AddLastArg(CmdArgs, options::OPT_ftime_report); 2948 Args.AddLastArg(CmdArgs, options::OPT_ftrapv); 2949 2950 if (Arg *A = Args.getLastArg(options::OPT_ftrapv_handler_EQ)) { 2951 CmdArgs.push_back("-ftrapv-handler"); 2952 CmdArgs.push_back(A->getValue()); 2953 } 2954 2955 Args.AddLastArg(CmdArgs, options::OPT_ftrap_function_EQ); 2956 2957 // -fno-strict-overflow implies -fwrapv if it isn't disabled, but 2958 // -fstrict-overflow won't turn off an explicitly enabled -fwrapv. 2959 if (Arg *A = Args.getLastArg(options::OPT_fwrapv, 2960 options::OPT_fno_wrapv)) { 2961 if (A->getOption().matches(options::OPT_fwrapv)) 2962 CmdArgs.push_back("-fwrapv"); 2963 } else if (Arg *A = Args.getLastArg(options::OPT_fstrict_overflow, 2964 options::OPT_fno_strict_overflow)) { 2965 if (A->getOption().matches(options::OPT_fno_strict_overflow)) 2966 CmdArgs.push_back("-fwrapv"); 2967 } 2968 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); 2969 Args.AddLastArg(CmdArgs, options::OPT_funroll_loops, 2970 options::OPT_fno_unroll_loops); 2971 2972 Args.AddLastArg(CmdArgs, options::OPT_pthread); 2973 2974 2975 // -stack-protector=0 is default. 2976 unsigned StackProtectorLevel = 0; 2977 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector, 2978 options::OPT_fstack_protector_all, 2979 options::OPT_fstack_protector)) { 2980 if (A->getOption().matches(options::OPT_fstack_protector)) 2981 StackProtectorLevel = 1; 2982 else if (A->getOption().matches(options::OPT_fstack_protector_all)) 2983 StackProtectorLevel = 2; 2984 } else { 2985 StackProtectorLevel = 2986 getToolChain().GetDefaultStackProtectorLevel(KernelOrKext); 2987 } 2988 if (StackProtectorLevel) { 2989 CmdArgs.push_back("-stack-protector"); 2990 CmdArgs.push_back(Args.MakeArgString(Twine(StackProtectorLevel))); 2991 } 2992 2993 // --param ssp-buffer-size= 2994 for (arg_iterator it = Args.filtered_begin(options::OPT__param), 2995 ie = Args.filtered_end(); it != ie; ++it) { 2996 StringRef Str((*it)->getValue()); 2997 if (Str.startswith("ssp-buffer-size=")) { 2998 if (StackProtectorLevel) { 2999 CmdArgs.push_back("-stack-protector-buffer-size"); 3000 // FIXME: Verify the argument is a valid integer. 3001 CmdArgs.push_back(Args.MakeArgString(Str.drop_front(16))); 3002 } 3003 (*it)->claim(); 3004 } 3005 } 3006 3007 // Translate -mstackrealign 3008 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign, 3009 false)) { 3010 CmdArgs.push_back("-backend-option"); 3011 CmdArgs.push_back("-force-align-stack"); 3012 } 3013 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign, 3014 false)) { 3015 CmdArgs.push_back(Args.MakeArgString("-mstackrealign")); 3016 } 3017 3018 if (Args.hasArg(options::OPT_mstack_alignment)) { 3019 StringRef alignment = Args.getLastArgValue(options::OPT_mstack_alignment); 3020 CmdArgs.push_back(Args.MakeArgString("-mstack-alignment=" + alignment)); 3021 } 3022 // -mkernel implies -mstrict-align; don't add the redundant option. 3023 if (!KernelOrKext) { 3024 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access, 3025 options::OPT_munaligned_access)) { 3026 if (A->getOption().matches(options::OPT_mno_unaligned_access)) { 3027 CmdArgs.push_back("-backend-option"); 3028 CmdArgs.push_back("-arm-strict-align"); 3029 } else { 3030 CmdArgs.push_back("-backend-option"); 3031 CmdArgs.push_back("-arm-no-strict-align"); 3032 } 3033 } 3034 } 3035 3036 // Forward -f options with positive and negative forms; we translate 3037 // these by hand. 3038 3039 if (Args.hasArg(options::OPT_mkernel)) { 3040 if (!Args.hasArg(options::OPT_fapple_kext) && types::isCXX(InputType)) 3041 CmdArgs.push_back("-fapple-kext"); 3042 if (!Args.hasArg(options::OPT_fbuiltin)) 3043 CmdArgs.push_back("-fno-builtin"); 3044 Args.ClaimAllArgs(options::OPT_fno_builtin); 3045 } 3046 // -fbuiltin is default. 3047 else if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin)) 3048 CmdArgs.push_back("-fno-builtin"); 3049 3050 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, 3051 options::OPT_fno_assume_sane_operator_new)) 3052 CmdArgs.push_back("-fno-assume-sane-operator-new"); 3053 3054 // -fblocks=0 is default. 3055 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks, 3056 getToolChain().IsBlocksDefault()) || 3057 (Args.hasArg(options::OPT_fgnu_runtime) && 3058 Args.hasArg(options::OPT_fobjc_nonfragile_abi) && 3059 !Args.hasArg(options::OPT_fno_blocks))) { 3060 CmdArgs.push_back("-fblocks"); 3061 3062 if (!Args.hasArg(options::OPT_fgnu_runtime) && 3063 !getToolChain().hasBlocksRuntime()) 3064 CmdArgs.push_back("-fblocks-runtime-optional"); 3065 } 3066 3067 // -fmodules enables modules (off by default). However, for C++/Objective-C++, 3068 // users must also pass -fcxx-modules. The latter flag will disappear once the 3069 // modules implementation is solid for C++/Objective-C++ programs as well. 3070 bool HaveModules = false; 3071 if (Args.hasFlag(options::OPT_fmodules, options::OPT_fno_modules, false)) { 3072 bool AllowedInCXX = Args.hasFlag(options::OPT_fcxx_modules, 3073 options::OPT_fno_cxx_modules, 3074 false); 3075 if (AllowedInCXX || !types::isCXX(InputType)) { 3076 CmdArgs.push_back("-fmodules"); 3077 HaveModules = true; 3078 } 3079 } 3080 3081 // -fmodule-maps enables module map processing (off by default) for header 3082 // checking. It is implied by -fmodules. 3083 if (Args.hasFlag(options::OPT_fmodule_maps, options::OPT_fno_module_maps, 3084 false)) { 3085 CmdArgs.push_back("-fmodule-maps"); 3086 } 3087 3088 // -fmodules-decluse checks that modules used are declared so (off by 3089 // default). 3090 if (Args.hasFlag(options::OPT_fmodules_decluse, 3091 options::OPT_fno_modules_decluse, 3092 false)) { 3093 CmdArgs.push_back("-fmodules-decluse"); 3094 } 3095 3096 // -fmodule-name specifies the module that is currently being built (or 3097 // used for header checking by -fmodule-maps). 3098 if (Arg *A = Args.getLastArg(options::OPT_fmodule_name)) { 3099 A->claim(); 3100 A->render(Args, CmdArgs); 3101 } 3102 3103 // -fmodule-map-file can be used to specify a file containing module 3104 // definitions. 3105 if (Arg *A = Args.getLastArg(options::OPT_fmodule_map_file)) { 3106 A->claim(); 3107 A->render(Args, CmdArgs); 3108 } 3109 3110 // If a module path was provided, pass it along. Otherwise, use a temporary 3111 // directory. 3112 if (Arg *A = Args.getLastArg(options::OPT_fmodules_cache_path)) { 3113 A->claim(); 3114 if (HaveModules) { 3115 A->render(Args, CmdArgs); 3116 } 3117 } else if (HaveModules) { 3118 SmallString<128> DefaultModuleCache; 3119 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/false, 3120 DefaultModuleCache); 3121 llvm::sys::path::append(DefaultModuleCache, "org.llvm.clang"); 3122 llvm::sys::path::append(DefaultModuleCache, "ModuleCache"); 3123 const char Arg[] = "-fmodules-cache-path="; 3124 DefaultModuleCache.insert(DefaultModuleCache.begin(), 3125 Arg, Arg + strlen(Arg)); 3126 CmdArgs.push_back(Args.MakeArgString(DefaultModuleCache)); 3127 } 3128 3129 // Pass through all -fmodules-ignore-macro arguments. 3130 Args.AddAllArgs(CmdArgs, options::OPT_fmodules_ignore_macro); 3131 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_interval); 3132 Args.AddLastArg(CmdArgs, options::OPT_fmodules_prune_after); 3133 3134 // -faccess-control is default. 3135 if (Args.hasFlag(options::OPT_fno_access_control, 3136 options::OPT_faccess_control, 3137 false)) 3138 CmdArgs.push_back("-fno-access-control"); 3139 3140 // -felide-constructors is the default. 3141 if (Args.hasFlag(options::OPT_fno_elide_constructors, 3142 options::OPT_felide_constructors, 3143 false)) 3144 CmdArgs.push_back("-fno-elide-constructors"); 3145 3146 // -frtti is default. 3147 if (!Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti) || 3148 KernelOrKext) { 3149 CmdArgs.push_back("-fno-rtti"); 3150 3151 // -fno-rtti cannot usefully be combined with -fsanitize=vptr. 3152 if (Sanitize.sanitizesVptr()) { 3153 std::string NoRttiArg = 3154 Args.getLastArg(options::OPT_mkernel, 3155 options::OPT_fapple_kext, 3156 options::OPT_fno_rtti)->getAsString(Args); 3157 D.Diag(diag::err_drv_argument_not_allowed_with) 3158 << "-fsanitize=vptr" << NoRttiArg; 3159 } 3160 } 3161 3162 // -fshort-enums=0 is default for all architectures except Hexagon. 3163 if (Args.hasFlag(options::OPT_fshort_enums, 3164 options::OPT_fno_short_enums, 3165 getToolChain().getArch() == 3166 llvm::Triple::hexagon)) 3167 CmdArgs.push_back("-fshort-enums"); 3168 3169 // -fsigned-char is default. 3170 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char, 3171 isSignedCharDefault(getToolChain().getTriple()))) 3172 CmdArgs.push_back("-fno-signed-char"); 3173 3174 // -fthreadsafe-static is default. 3175 if (!Args.hasFlag(options::OPT_fthreadsafe_statics, 3176 options::OPT_fno_threadsafe_statics)) 3177 CmdArgs.push_back("-fno-threadsafe-statics"); 3178 3179 // -fuse-cxa-atexit is default. 3180 if (!Args.hasFlag(options::OPT_fuse_cxa_atexit, 3181 options::OPT_fno_use_cxa_atexit, 3182 getToolChain().getTriple().getOS() != llvm::Triple::Cygwin && 3183 getToolChain().getTriple().getOS() != llvm::Triple::MinGW32 && 3184 getToolChain().getArch() != llvm::Triple::hexagon) || 3185 KernelOrKext) 3186 CmdArgs.push_back("-fno-use-cxa-atexit"); 3187 3188 // -fms-extensions=0 is default. 3189 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 3190 getToolChain().getTriple().getOS() == llvm::Triple::Win32)) 3191 CmdArgs.push_back("-fms-extensions"); 3192 3193 // -fms-compatibility=0 is default. 3194 if (Args.hasFlag(options::OPT_fms_compatibility, 3195 options::OPT_fno_ms_compatibility, 3196 (getToolChain().getTriple().getOS() == llvm::Triple::Win32 && 3197 Args.hasFlag(options::OPT_fms_extensions, 3198 options::OPT_fno_ms_extensions, 3199 true)))) 3200 CmdArgs.push_back("-fms-compatibility"); 3201 3202 // -fmsc-version=1700 is default. 3203 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, 3204 getToolChain().getTriple().getOS() == llvm::Triple::Win32) || 3205 Args.hasArg(options::OPT_fmsc_version)) { 3206 StringRef msc_ver = Args.getLastArgValue(options::OPT_fmsc_version); 3207 if (msc_ver.empty()) 3208 CmdArgs.push_back("-fmsc-version=1700"); 3209 else 3210 CmdArgs.push_back(Args.MakeArgString("-fmsc-version=" + msc_ver)); 3211 } 3212 3213 3214 // -fno-borland-extensions is default. 3215 if (Args.hasFlag(options::OPT_fborland_extensions, 3216 options::OPT_fno_borland_extensions, false)) 3217 CmdArgs.push_back("-fborland-extensions"); 3218 3219 // -fno-delayed-template-parsing is default, except for Windows where MSVC STL 3220 // needs it. 3221 if (Args.hasFlag(options::OPT_fdelayed_template_parsing, 3222 options::OPT_fno_delayed_template_parsing, 3223 getToolChain().getTriple().getOS() == llvm::Triple::Win32)) 3224 CmdArgs.push_back("-fdelayed-template-parsing"); 3225 3226 // -fgnu-keywords default varies depending on language; only pass if 3227 // specified. 3228 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords, 3229 options::OPT_fno_gnu_keywords)) 3230 A->render(Args, CmdArgs); 3231 3232 if (Args.hasFlag(options::OPT_fgnu89_inline, 3233 options::OPT_fno_gnu89_inline, 3234 false)) 3235 CmdArgs.push_back("-fgnu89-inline"); 3236 3237 if (Args.hasArg(options::OPT_fno_inline)) 3238 CmdArgs.push_back("-fno-inline"); 3239 3240 if (Args.hasArg(options::OPT_fno_inline_functions)) 3241 CmdArgs.push_back("-fno-inline-functions"); 3242 3243 ObjCRuntime objcRuntime = AddObjCRuntimeArgs(Args, CmdArgs, rewriteKind); 3244 3245 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and 3246 // legacy is the default. Next runtime is always legacy dispatch and 3247 // -fno-objc-legacy-dispatch gets ignored silently. 3248 if (objcRuntime.isNonFragile() && !objcRuntime.isNeXTFamily()) { 3249 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch, 3250 options::OPT_fno_objc_legacy_dispatch, 3251 objcRuntime.isLegacyDispatchDefaultForArch( 3252 getToolChain().getArch()))) { 3253 if (getToolChain().UseObjCMixedDispatch()) 3254 CmdArgs.push_back("-fobjc-dispatch-method=mixed"); 3255 else 3256 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy"); 3257 } 3258 } 3259 3260 // -fencode-extended-block-signature=1 is default. 3261 if (getToolChain().IsEncodeExtendedBlockSignatureDefault()) { 3262 CmdArgs.push_back("-fencode-extended-block-signature"); 3263 } 3264 3265 // Allow -fno-objc-arr to trump -fobjc-arr/-fobjc-arc. 3266 // NOTE: This logic is duplicated in ToolChains.cpp. 3267 bool ARC = isObjCAutoRefCount(Args); 3268 if (ARC) { 3269 getToolChain().CheckObjCARC(); 3270 3271 CmdArgs.push_back("-fobjc-arc"); 3272 3273 // FIXME: It seems like this entire block, and several around it should be 3274 // wrapped in isObjC, but for now we just use it here as this is where it 3275 // was being used previously. 3276 if (types::isCXX(InputType) && types::isObjC(InputType)) { 3277 if (getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) 3278 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++"); 3279 else 3280 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++"); 3281 } 3282 3283 // Allow the user to enable full exceptions code emission. 3284 // We define off for Objective-CC, on for Objective-C++. 3285 if (Args.hasFlag(options::OPT_fobjc_arc_exceptions, 3286 options::OPT_fno_objc_arc_exceptions, 3287 /*default*/ types::isCXX(InputType))) 3288 CmdArgs.push_back("-fobjc-arc-exceptions"); 3289 } 3290 3291 // -fobjc-infer-related-result-type is the default, except in the Objective-C 3292 // rewriter. 3293 if (rewriteKind != RK_None) 3294 CmdArgs.push_back("-fno-objc-infer-related-result-type"); 3295 3296 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only 3297 // takes precedence. 3298 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only); 3299 if (!GCArg) 3300 GCArg = Args.getLastArg(options::OPT_fobjc_gc); 3301 if (GCArg) { 3302 if (ARC) { 3303 D.Diag(diag::err_drv_objc_gc_arr) 3304 << GCArg->getAsString(Args); 3305 } else if (getToolChain().SupportsObjCGC()) { 3306 GCArg->render(Args, CmdArgs); 3307 } else { 3308 // FIXME: We should move this to a hard error. 3309 D.Diag(diag::warn_drv_objc_gc_unsupported) 3310 << GCArg->getAsString(Args); 3311 } 3312 } 3313 3314 // Add exception args. 3315 addExceptionArgs(Args, InputType, getToolChain().getTriple(), 3316 KernelOrKext, objcRuntime, CmdArgs); 3317 3318 if (getToolChain().UseSjLjExceptions()) 3319 CmdArgs.push_back("-fsjlj-exceptions"); 3320 3321 // C++ "sane" operator new. 3322 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new, 3323 options::OPT_fno_assume_sane_operator_new)) 3324 CmdArgs.push_back("-fno-assume-sane-operator-new"); 3325 3326 // -fconstant-cfstrings is default, and may be subject to argument translation 3327 // on Darwin. 3328 if (!Args.hasFlag(options::OPT_fconstant_cfstrings, 3329 options::OPT_fno_constant_cfstrings) || 3330 !Args.hasFlag(options::OPT_mconstant_cfstrings, 3331 options::OPT_mno_constant_cfstrings)) 3332 CmdArgs.push_back("-fno-constant-cfstrings"); 3333 3334 // -fshort-wchar default varies depending on platform; only 3335 // pass if specified. 3336 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar)) 3337 A->render(Args, CmdArgs); 3338 3339 // -fno-pascal-strings is default, only pass non-default. 3340 if (Args.hasFlag(options::OPT_fpascal_strings, 3341 options::OPT_fno_pascal_strings, 3342 false)) 3343 CmdArgs.push_back("-fpascal-strings"); 3344 3345 // Honor -fpack-struct= and -fpack-struct, if given. Note that 3346 // -fno-pack-struct doesn't apply to -fpack-struct=. 3347 if (Arg *A = Args.getLastArg(options::OPT_fpack_struct_EQ)) { 3348 std::string PackStructStr = "-fpack-struct="; 3349 PackStructStr += A->getValue(); 3350 CmdArgs.push_back(Args.MakeArgString(PackStructStr)); 3351 } else if (Args.hasFlag(options::OPT_fpack_struct, 3352 options::OPT_fno_pack_struct, false)) { 3353 CmdArgs.push_back("-fpack-struct=1"); 3354 } 3355 3356 if (KernelOrKext || isNoCommonDefault(getToolChain().getTriple())) { 3357 if (!Args.hasArg(options::OPT_fcommon)) 3358 CmdArgs.push_back("-fno-common"); 3359 Args.ClaimAllArgs(options::OPT_fno_common); 3360 } 3361 3362 // -fcommon is default, only pass non-default. 3363 else if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common)) 3364 CmdArgs.push_back("-fno-common"); 3365 3366 // -fsigned-bitfields is default, and clang doesn't yet support 3367 // -funsigned-bitfields. 3368 if (!Args.hasFlag(options::OPT_fsigned_bitfields, 3369 options::OPT_funsigned_bitfields)) 3370 D.Diag(diag::warn_drv_clang_unsupported) 3371 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args); 3372 3373 // -fsigned-bitfields is default, and clang doesn't support -fno-for-scope. 3374 if (!Args.hasFlag(options::OPT_ffor_scope, 3375 options::OPT_fno_for_scope)) 3376 D.Diag(diag::err_drv_clang_unsupported) 3377 << Args.getLastArg(options::OPT_fno_for_scope)->getAsString(Args); 3378 3379 // -fcaret-diagnostics is default. 3380 if (!Args.hasFlag(options::OPT_fcaret_diagnostics, 3381 options::OPT_fno_caret_diagnostics, true)) 3382 CmdArgs.push_back("-fno-caret-diagnostics"); 3383 3384 // -fdiagnostics-fixit-info is default, only pass non-default. 3385 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info, 3386 options::OPT_fno_diagnostics_fixit_info)) 3387 CmdArgs.push_back("-fno-diagnostics-fixit-info"); 3388 3389 // Enable -fdiagnostics-show-option by default. 3390 if (Args.hasFlag(options::OPT_fdiagnostics_show_option, 3391 options::OPT_fno_diagnostics_show_option)) 3392 CmdArgs.push_back("-fdiagnostics-show-option"); 3393 3394 if (const Arg *A = 3395 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) { 3396 CmdArgs.push_back("-fdiagnostics-show-category"); 3397 CmdArgs.push_back(A->getValue()); 3398 } 3399 3400 if (const Arg *A = 3401 Args.getLastArg(options::OPT_fdiagnostics_format_EQ)) { 3402 CmdArgs.push_back("-fdiagnostics-format"); 3403 CmdArgs.push_back(A->getValue()); 3404 } 3405 3406 if (Arg *A = Args.getLastArg( 3407 options::OPT_fdiagnostics_show_note_include_stack, 3408 options::OPT_fno_diagnostics_show_note_include_stack)) { 3409 if (A->getOption().matches( 3410 options::OPT_fdiagnostics_show_note_include_stack)) 3411 CmdArgs.push_back("-fdiagnostics-show-note-include-stack"); 3412 else 3413 CmdArgs.push_back("-fno-diagnostics-show-note-include-stack"); 3414 } 3415 3416 // Color diagnostics are the default, unless the terminal doesn't support 3417 // them. 3418 // Support both clang's -f[no-]color-diagnostics and gcc's 3419 // -f[no-]diagnostics-colors[=never|always|auto]. 3420 enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto; 3421 for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); 3422 it != ie; ++it) { 3423 const Option &O = (*it)->getOption(); 3424 if (!O.matches(options::OPT_fcolor_diagnostics) && 3425 !O.matches(options::OPT_fdiagnostics_color) && 3426 !O.matches(options::OPT_fno_color_diagnostics) && 3427 !O.matches(options::OPT_fno_diagnostics_color) && 3428 !O.matches(options::OPT_fdiagnostics_color_EQ)) 3429 continue; 3430 3431 (*it)->claim(); 3432 if (O.matches(options::OPT_fcolor_diagnostics) || 3433 O.matches(options::OPT_fdiagnostics_color)) { 3434 ShowColors = Colors_On; 3435 } else if (O.matches(options::OPT_fno_color_diagnostics) || 3436 O.matches(options::OPT_fno_diagnostics_color)) { 3437 ShowColors = Colors_Off; 3438 } else { 3439 assert(O.matches(options::OPT_fdiagnostics_color_EQ)); 3440 StringRef value((*it)->getValue()); 3441 if (value == "always") 3442 ShowColors = Colors_On; 3443 else if (value == "never") 3444 ShowColors = Colors_Off; 3445 else if (value == "auto") 3446 ShowColors = Colors_Auto; 3447 else 3448 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 3449 << ("-fdiagnostics-color=" + value).str(); 3450 } 3451 } 3452 if (ShowColors == Colors_On || 3453 (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors())) 3454 CmdArgs.push_back("-fcolor-diagnostics"); 3455 3456 if (Args.hasArg(options::OPT_fansi_escape_codes)) 3457 CmdArgs.push_back("-fansi-escape-codes"); 3458 3459 if (!Args.hasFlag(options::OPT_fshow_source_location, 3460 options::OPT_fno_show_source_location)) 3461 CmdArgs.push_back("-fno-show-source-location"); 3462 3463 if (!Args.hasFlag(options::OPT_fshow_column, 3464 options::OPT_fno_show_column, 3465 true)) 3466 CmdArgs.push_back("-fno-show-column"); 3467 3468 if (!Args.hasFlag(options::OPT_fspell_checking, 3469 options::OPT_fno_spell_checking)) 3470 CmdArgs.push_back("-fno-spell-checking"); 3471 3472 3473 // -fno-asm-blocks is default. 3474 if (Args.hasFlag(options::OPT_fasm_blocks, options::OPT_fno_asm_blocks, 3475 false)) 3476 CmdArgs.push_back("-fasm-blocks"); 3477 3478 // Enable vectorization per default according to the optimization level 3479 // selected. For optimization levels that want vectorization we use the alias 3480 // option to simplify the hasFlag logic. 3481 bool EnableVec = shouldEnableVectorizerAtOLevel(Args); 3482 OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group : 3483 options::OPT_fvectorize; 3484 if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption, 3485 options::OPT_fno_vectorize, EnableVec)) 3486 CmdArgs.push_back("-vectorize-loops"); 3487 3488 // -fslp-vectorize is default. 3489 if (Args.hasFlag(options::OPT_fslp_vectorize, 3490 options::OPT_fno_slp_vectorize, true)) 3491 CmdArgs.push_back("-vectorize-slp"); 3492 3493 // -fno-slp-vectorize-aggressive is default. 3494 if (Args.hasFlag(options::OPT_fslp_vectorize_aggressive, 3495 options::OPT_fno_slp_vectorize_aggressive, false)) 3496 CmdArgs.push_back("-vectorize-slp-aggressive"); 3497 3498 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ)) 3499 A->render(Args, CmdArgs); 3500 3501 // -fdollars-in-identifiers default varies depending on platform and 3502 // language; only pass if specified. 3503 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers, 3504 options::OPT_fno_dollars_in_identifiers)) { 3505 if (A->getOption().matches(options::OPT_fdollars_in_identifiers)) 3506 CmdArgs.push_back("-fdollars-in-identifiers"); 3507 else 3508 CmdArgs.push_back("-fno-dollars-in-identifiers"); 3509 } 3510 3511 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for 3512 // practical purposes. 3513 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time, 3514 options::OPT_fno_unit_at_a_time)) { 3515 if (A->getOption().matches(options::OPT_fno_unit_at_a_time)) 3516 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args); 3517 } 3518 3519 if (Args.hasFlag(options::OPT_fapple_pragma_pack, 3520 options::OPT_fno_apple_pragma_pack, false)) 3521 CmdArgs.push_back("-fapple-pragma-pack"); 3522 3523 // le32-specific flags: 3524 // -fno-math-builtin: clang should not convert math builtins to intrinsics 3525 // by default. 3526 if (getToolChain().getArch() == llvm::Triple::le32) { 3527 CmdArgs.push_back("-fno-math-builtin"); 3528 } 3529 3530 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM. 3531 // 3532 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941. 3533#if 0 3534 if (getToolChain().getTriple().isOSDarwin() && 3535 (getToolChain().getArch() == llvm::Triple::arm || 3536 getToolChain().getArch() == llvm::Triple::thumb)) { 3537 if (!Args.hasArg(options::OPT_fbuiltin_strcat)) 3538 CmdArgs.push_back("-fno-builtin-strcat"); 3539 if (!Args.hasArg(options::OPT_fbuiltin_strcpy)) 3540 CmdArgs.push_back("-fno-builtin-strcpy"); 3541 } 3542#endif 3543 3544 // Only allow -traditional or -traditional-cpp outside in preprocessing modes. 3545 if (Arg *A = Args.getLastArg(options::OPT_traditional, 3546 options::OPT_traditional_cpp)) { 3547 if (isa<PreprocessJobAction>(JA)) 3548 CmdArgs.push_back("-traditional-cpp"); 3549 else 3550 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 3551 } 3552 3553 Args.AddLastArg(CmdArgs, options::OPT_dM); 3554 Args.AddLastArg(CmdArgs, options::OPT_dD); 3555 3556 // Handle serialized diagnostics. 3557 if (Arg *A = Args.getLastArg(options::OPT__serialize_diags)) { 3558 CmdArgs.push_back("-serialize-diagnostic-file"); 3559 CmdArgs.push_back(Args.MakeArgString(A->getValue())); 3560 } 3561 3562 if (Args.hasArg(options::OPT_fretain_comments_from_system_headers)) 3563 CmdArgs.push_back("-fretain-comments-from-system-headers"); 3564 3565 // Forward -fcomment-block-commands to -cc1. 3566 Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); 3567 // Forward -fparse-all-comments to -cc1. 3568 Args.AddAllArgs(CmdArgs, options::OPT_fparse_all_comments); 3569 3570 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option 3571 // parser. 3572 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); 3573 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm), 3574 ie = Args.filtered_end(); it != ie; ++it) { 3575 (*it)->claim(); 3576 3577 // We translate this by hand to the -cc1 argument, since nightly test uses 3578 // it and developers have been trained to spell it with -mllvm. 3579 if (StringRef((*it)->getValue(0)) == "-disable-llvm-optzns") 3580 CmdArgs.push_back("-disable-llvm-optzns"); 3581 else 3582 (*it)->render(Args, CmdArgs); 3583 } 3584 3585 if (Output.getType() == types::TY_Dependencies) { 3586 // Handled with other dependency code. 3587 } else if (Output.isFilename()) { 3588 CmdArgs.push_back("-o"); 3589 CmdArgs.push_back(Output.getFilename()); 3590 } else { 3591 assert(Output.isNothing() && "Invalid output."); 3592 } 3593 3594 for (InputInfoList::const_iterator 3595 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 3596 const InputInfo &II = *it; 3597 CmdArgs.push_back("-x"); 3598 if (Args.hasArg(options::OPT_rewrite_objc)) 3599 CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX)); 3600 else 3601 CmdArgs.push_back(types::getTypeName(II.getType())); 3602 if (II.isFilename()) 3603 CmdArgs.push_back(II.getFilename()); 3604 else 3605 II.getInputArg().renderAsInput(Args, CmdArgs); 3606 } 3607 3608 Args.AddAllArgs(CmdArgs, options::OPT_undef); 3609 3610 const char *Exec = getToolChain().getDriver().getClangProgramPath(); 3611 3612 // Optionally embed the -cc1 level arguments into the debug info, for build 3613 // analysis. 3614 if (getToolChain().UseDwarfDebugFlags()) { 3615 ArgStringList OriginalArgs; 3616 for (ArgList::const_iterator it = Args.begin(), 3617 ie = Args.end(); it != ie; ++it) 3618 (*it)->render(Args, OriginalArgs); 3619 3620 SmallString<256> Flags; 3621 Flags += Exec; 3622 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) { 3623 Flags += " "; 3624 Flags += OriginalArgs[i]; 3625 } 3626 CmdArgs.push_back("-dwarf-debug-flags"); 3627 CmdArgs.push_back(Args.MakeArgString(Flags.str())); 3628 } 3629 3630 // Add the split debug info name to the command lines here so we 3631 // can propagate it to the backend. 3632 bool SplitDwarf = Args.hasArg(options::OPT_gsplit_dwarf) && 3633 getToolChain().getTriple().isOSLinux() && 3634 (isa<AssembleJobAction>(JA) || isa<CompileJobAction>(JA)); 3635 const char *SplitDwarfOut; 3636 if (SplitDwarf) { 3637 CmdArgs.push_back("-split-dwarf-file"); 3638 SplitDwarfOut = SplitDebugName(Args, Inputs); 3639 CmdArgs.push_back(SplitDwarfOut); 3640 } 3641 3642 // Finally add the compile command to the compilation. 3643 if (Args.hasArg(options::OPT__SLASH_fallback)) { 3644 tools::visualstudio::Compile CL(getToolChain()); 3645 Command *CLCommand = CL.GetCommand(C, JA, Output, Inputs, Args, 3646 LinkingOutput); 3647 C.addCommand(new FallbackCommand(JA, *this, Exec, CmdArgs, CLCommand)); 3648 } else { 3649 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 3650 } 3651 3652 3653 // Handle the debug info splitting at object creation time if we're 3654 // creating an object. 3655 // TODO: Currently only works on linux with newer objcopy. 3656 if (SplitDwarf && !isa<CompileJobAction>(JA)) 3657 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, SplitDwarfOut); 3658 3659 if (Arg *A = Args.getLastArg(options::OPT_pg)) 3660 if (Args.hasArg(options::OPT_fomit_frame_pointer)) 3661 D.Diag(diag::err_drv_argument_not_allowed_with) 3662 << "-fomit-frame-pointer" << A->getAsString(Args); 3663 3664 // Claim some arguments which clang supports automatically. 3665 3666 // -fpch-preprocess is used with gcc to add a special marker in the output to 3667 // include the PCH file. Clang's PTH solution is completely transparent, so we 3668 // do not need to deal with it at all. 3669 Args.ClaimAllArgs(options::OPT_fpch_preprocess); 3670 3671 // Claim some arguments which clang doesn't support, but we don't 3672 // care to warn the user about. 3673 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group); 3674 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group); 3675 3676 // Disable warnings for clang -E -emit-llvm foo.c 3677 Args.ClaimAllArgs(options::OPT_emit_llvm); 3678} 3679 3680/// Add options related to the Objective-C runtime/ABI. 3681/// 3682/// Returns true if the runtime is non-fragile. 3683ObjCRuntime Clang::AddObjCRuntimeArgs(const ArgList &args, 3684 ArgStringList &cmdArgs, 3685 RewriteKind rewriteKind) const { 3686 // Look for the controlling runtime option. 3687 Arg *runtimeArg = args.getLastArg(options::OPT_fnext_runtime, 3688 options::OPT_fgnu_runtime, 3689 options::OPT_fobjc_runtime_EQ); 3690 3691 // Just forward -fobjc-runtime= to the frontend. This supercedes 3692 // options about fragility. 3693 if (runtimeArg && 3694 runtimeArg->getOption().matches(options::OPT_fobjc_runtime_EQ)) { 3695 ObjCRuntime runtime; 3696 StringRef value = runtimeArg->getValue(); 3697 if (runtime.tryParse(value)) { 3698 getToolChain().getDriver().Diag(diag::err_drv_unknown_objc_runtime) 3699 << value; 3700 } 3701 3702 runtimeArg->render(args, cmdArgs); 3703 return runtime; 3704 } 3705 3706 // Otherwise, we'll need the ABI "version". Version numbers are 3707 // slightly confusing for historical reasons: 3708 // 1 - Traditional "fragile" ABI 3709 // 2 - Non-fragile ABI, version 1 3710 // 3 - Non-fragile ABI, version 2 3711 unsigned objcABIVersion = 1; 3712 // If -fobjc-abi-version= is present, use that to set the version. 3713 if (Arg *abiArg = args.getLastArg(options::OPT_fobjc_abi_version_EQ)) { 3714 StringRef value = abiArg->getValue(); 3715 if (value == "1") 3716 objcABIVersion = 1; 3717 else if (value == "2") 3718 objcABIVersion = 2; 3719 else if (value == "3") 3720 objcABIVersion = 3; 3721 else 3722 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 3723 << value; 3724 } else { 3725 // Otherwise, determine if we are using the non-fragile ABI. 3726 bool nonFragileABIIsDefault = 3727 (rewriteKind == RK_NonFragile || 3728 (rewriteKind == RK_None && 3729 getToolChain().IsObjCNonFragileABIDefault())); 3730 if (args.hasFlag(options::OPT_fobjc_nonfragile_abi, 3731 options::OPT_fno_objc_nonfragile_abi, 3732 nonFragileABIIsDefault)) { 3733 // Determine the non-fragile ABI version to use. 3734#ifdef DISABLE_DEFAULT_NONFRAGILEABI_TWO 3735 unsigned nonFragileABIVersion = 1; 3736#else 3737 unsigned nonFragileABIVersion = 2; 3738#endif 3739 3740 if (Arg *abiArg = args.getLastArg( 3741 options::OPT_fobjc_nonfragile_abi_version_EQ)) { 3742 StringRef value = abiArg->getValue(); 3743 if (value == "1") 3744 nonFragileABIVersion = 1; 3745 else if (value == "2") 3746 nonFragileABIVersion = 2; 3747 else 3748 getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) 3749 << value; 3750 } 3751 3752 objcABIVersion = 1 + nonFragileABIVersion; 3753 } else { 3754 objcABIVersion = 1; 3755 } 3756 } 3757 3758 // We don't actually care about the ABI version other than whether 3759 // it's non-fragile. 3760 bool isNonFragile = objcABIVersion != 1; 3761 3762 // If we have no runtime argument, ask the toolchain for its default runtime. 3763 // However, the rewriter only really supports the Mac runtime, so assume that. 3764 ObjCRuntime runtime; 3765 if (!runtimeArg) { 3766 switch (rewriteKind) { 3767 case RK_None: 3768 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 3769 break; 3770 case RK_Fragile: 3771 runtime = ObjCRuntime(ObjCRuntime::FragileMacOSX, VersionTuple()); 3772 break; 3773 case RK_NonFragile: 3774 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 3775 break; 3776 } 3777 3778 // -fnext-runtime 3779 } else if (runtimeArg->getOption().matches(options::OPT_fnext_runtime)) { 3780 // On Darwin, make this use the default behavior for the toolchain. 3781 if (getToolChain().getTriple().isOSDarwin()) { 3782 runtime = getToolChain().getDefaultObjCRuntime(isNonFragile); 3783 3784 // Otherwise, build for a generic macosx port. 3785 } else { 3786 runtime = ObjCRuntime(ObjCRuntime::MacOSX, VersionTuple()); 3787 } 3788 3789 // -fgnu-runtime 3790 } else { 3791 assert(runtimeArg->getOption().matches(options::OPT_fgnu_runtime)); 3792 // Legacy behaviour is to target the gnustep runtime if we are i 3793 // non-fragile mode or the GCC runtime in fragile mode. 3794 if (isNonFragile) 3795 runtime = ObjCRuntime(ObjCRuntime::GNUstep, VersionTuple(1,6)); 3796 else 3797 runtime = ObjCRuntime(ObjCRuntime::GCC, VersionTuple()); 3798 } 3799 3800 cmdArgs.push_back(args.MakeArgString( 3801 "-fobjc-runtime=" + runtime.getAsString())); 3802 return runtime; 3803} 3804 3805void Clang::AddClangCLArgs(const ArgList &Args, ArgStringList &CmdArgs) const { 3806 unsigned RTOptionID = options::OPT__SLASH_MT; 3807 3808 if (Args.hasArg(options::OPT__SLASH_LDd)) 3809 // The /LDd option implies /MTd. The dependent lib part can be overridden, 3810 // but defining _DEBUG is sticky. 3811 RTOptionID = options::OPT__SLASH_MTd; 3812 3813 if (Arg *A = Args.getLastArg(options::OPT__SLASH_M_Group)) 3814 RTOptionID = A->getOption().getID(); 3815 3816 switch(RTOptionID) { 3817 case options::OPT__SLASH_MD: 3818 if (Args.hasArg(options::OPT__SLASH_LDd)) 3819 CmdArgs.push_back("-D_DEBUG"); 3820 CmdArgs.push_back("-D_MT"); 3821 CmdArgs.push_back("-D_DLL"); 3822 CmdArgs.push_back("--dependent-lib=msvcrt"); 3823 break; 3824 case options::OPT__SLASH_MDd: 3825 CmdArgs.push_back("-D_DEBUG"); 3826 CmdArgs.push_back("-D_MT"); 3827 CmdArgs.push_back("-D_DLL"); 3828 CmdArgs.push_back("--dependent-lib=msvcrtd"); 3829 break; 3830 case options::OPT__SLASH_MT: 3831 if (Args.hasArg(options::OPT__SLASH_LDd)) 3832 CmdArgs.push_back("-D_DEBUG"); 3833 CmdArgs.push_back("-D_MT"); 3834 CmdArgs.push_back("--dependent-lib=libcmt"); 3835 break; 3836 case options::OPT__SLASH_MTd: 3837 CmdArgs.push_back("-D_DEBUG"); 3838 CmdArgs.push_back("-D_MT"); 3839 CmdArgs.push_back("--dependent-lib=libcmtd"); 3840 break; 3841 default: 3842 llvm_unreachable("Unexpected option ID."); 3843 } 3844 3845 // This provides POSIX compatibility (maps 'open' to '_open'), which most 3846 // users want. The /Za flag to cl.exe turns this off, but it's not 3847 // implemented in clang. 3848 CmdArgs.push_back("--dependent-lib=oldnames"); 3849 3850 // FIXME: Make this default for the win32 triple. 3851 CmdArgs.push_back("-cxx-abi"); 3852 CmdArgs.push_back("microsoft"); 3853 3854 if (Arg *A = Args.getLastArg(options::OPT_show_includes)) 3855 A->render(Args, CmdArgs); 3856 3857 if (!Args.hasArg(options::OPT_fdiagnostics_format_EQ)) { 3858 CmdArgs.push_back("-fdiagnostics-format"); 3859 if (Args.hasArg(options::OPT__SLASH_fallback)) 3860 CmdArgs.push_back("msvc-fallback"); 3861 else 3862 CmdArgs.push_back("msvc"); 3863 } 3864} 3865 3866void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, 3867 const InputInfo &Output, 3868 const InputInfoList &Inputs, 3869 const ArgList &Args, 3870 const char *LinkingOutput) const { 3871 ArgStringList CmdArgs; 3872 3873 assert(Inputs.size() == 1 && "Unexpected number of inputs."); 3874 const InputInfo &Input = Inputs[0]; 3875 3876 // Don't warn about "clang -w -c foo.s" 3877 Args.ClaimAllArgs(options::OPT_w); 3878 // and "clang -emit-llvm -c foo.s" 3879 Args.ClaimAllArgs(options::OPT_emit_llvm); 3880 3881 // Invoke ourselves in -cc1as mode. 3882 // 3883 // FIXME: Implement custom jobs for internal actions. 3884 CmdArgs.push_back("-cc1as"); 3885 3886 // Add the "effective" target triple. 3887 CmdArgs.push_back("-triple"); 3888 std::string TripleStr = 3889 getToolChain().ComputeEffectiveClangTriple(Args, Input.getType()); 3890 CmdArgs.push_back(Args.MakeArgString(TripleStr)); 3891 3892 // Set the output mode, we currently only expect to be used as a real 3893 // assembler. 3894 CmdArgs.push_back("-filetype"); 3895 CmdArgs.push_back("obj"); 3896 3897 // Set the main file name, so that debug info works even with 3898 // -save-temps or preprocessed assembly. 3899 CmdArgs.push_back("-main-file-name"); 3900 CmdArgs.push_back(Clang::getBaseInputName(Args, Inputs)); 3901 3902 // Add the target cpu 3903 const llvm::Triple &Triple = getToolChain().getTriple(); 3904 std::string CPU = getCPUName(Args, Triple); 3905 if (!CPU.empty()) { 3906 CmdArgs.push_back("-target-cpu"); 3907 CmdArgs.push_back(Args.MakeArgString(CPU)); 3908 } 3909 3910 // Add the target features 3911 const Driver &D = getToolChain().getDriver(); 3912 getTargetFeatures(D, Triple, Args, CmdArgs); 3913 3914 // Ignore explicit -force_cpusubtype_ALL option. 3915 (void) Args.hasArg(options::OPT_force__cpusubtype__ALL); 3916 3917 // Determine the original source input. 3918 const Action *SourceAction = &JA; 3919 while (SourceAction->getKind() != Action::InputClass) { 3920 assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 3921 SourceAction = SourceAction->getInputs()[0]; 3922 } 3923 3924 // Forward -g and handle debug info related flags, assuming we are dealing 3925 // with an actual assembly file. 3926 if (SourceAction->getType() == types::TY_Asm || 3927 SourceAction->getType() == types::TY_PP_Asm) { 3928 Args.ClaimAllArgs(options::OPT_g_Group); 3929 if (Arg *A = Args.getLastArg(options::OPT_g_Group)) 3930 if (!A->getOption().matches(options::OPT_g0)) 3931 CmdArgs.push_back("-g"); 3932 3933 // Add the -fdebug-compilation-dir flag if needed. 3934 addDebugCompDirArg(Args, CmdArgs); 3935 3936 // Set the AT_producer to the clang version when using the integrated 3937 // assembler on assembly source files. 3938 CmdArgs.push_back("-dwarf-debug-producer"); 3939 CmdArgs.push_back(Args.MakeArgString(getClangFullVersion())); 3940 } 3941 3942 // Optionally embed the -cc1as level arguments into the debug info, for build 3943 // analysis. 3944 if (getToolChain().UseDwarfDebugFlags()) { 3945 ArgStringList OriginalArgs; 3946 for (ArgList::const_iterator it = Args.begin(), 3947 ie = Args.end(); it != ie; ++it) 3948 (*it)->render(Args, OriginalArgs); 3949 3950 SmallString<256> Flags; 3951 const char *Exec = getToolChain().getDriver().getClangProgramPath(); 3952 Flags += Exec; 3953 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) { 3954 Flags += " "; 3955 Flags += OriginalArgs[i]; 3956 } 3957 CmdArgs.push_back("-dwarf-debug-flags"); 3958 CmdArgs.push_back(Args.MakeArgString(Flags.str())); 3959 } 3960 3961 // FIXME: Add -static support, once we have it. 3962 3963 CollectArgsForIntegratedAssembler(C, Args, CmdArgs, 3964 getToolChain().getDriver()); 3965 3966 Args.AddAllArgs(CmdArgs, options::OPT_mllvm); 3967 3968 assert(Output.isFilename() && "Unexpected lipo output."); 3969 CmdArgs.push_back("-o"); 3970 CmdArgs.push_back(Output.getFilename()); 3971 3972 assert(Input.isFilename() && "Invalid input."); 3973 CmdArgs.push_back(Input.getFilename()); 3974 3975 const char *Exec = getToolChain().getDriver().getClangProgramPath(); 3976 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 3977 3978 // Handle the debug info splitting at object creation time if we're 3979 // creating an object. 3980 // TODO: Currently only works on linux with newer objcopy. 3981 if (Args.hasArg(options::OPT_gsplit_dwarf) && 3982 getToolChain().getTriple().isOSLinux()) 3983 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, 3984 SplitDebugName(Args, Inputs)); 3985} 3986 3987void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA, 3988 const InputInfo &Output, 3989 const InputInfoList &Inputs, 3990 const ArgList &Args, 3991 const char *LinkingOutput) const { 3992 const Driver &D = getToolChain().getDriver(); 3993 ArgStringList CmdArgs; 3994 3995 for (ArgList::const_iterator 3996 it = Args.begin(), ie = Args.end(); it != ie; ++it) { 3997 Arg *A = *it; 3998 if (forwardToGCC(A->getOption())) { 3999 // Don't forward any -g arguments to assembly steps. 4000 if (isa<AssembleJobAction>(JA) && 4001 A->getOption().matches(options::OPT_g_Group)) 4002 continue; 4003 4004 // Don't forward any -W arguments to assembly and link steps. 4005 if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) && 4006 A->getOption().matches(options::OPT_W_Group)) 4007 continue; 4008 4009 // It is unfortunate that we have to claim here, as this means 4010 // we will basically never report anything interesting for 4011 // platforms using a generic gcc, even if we are just using gcc 4012 // to get to the assembler. 4013 A->claim(); 4014 A->render(Args, CmdArgs); 4015 } 4016 } 4017 4018 RenderExtraToolArgs(JA, CmdArgs); 4019 4020 // If using a driver driver, force the arch. 4021 llvm::Triple::ArchType Arch = getToolChain().getArch(); 4022 if (getToolChain().getTriple().isOSDarwin()) { 4023 CmdArgs.push_back("-arch"); 4024 4025 // FIXME: Remove these special cases. 4026 if (Arch == llvm::Triple::ppc) 4027 CmdArgs.push_back("ppc"); 4028 else if (Arch == llvm::Triple::ppc64) 4029 CmdArgs.push_back("ppc64"); 4030 else if (Arch == llvm::Triple::ppc64le) 4031 CmdArgs.push_back("ppc64le"); 4032 else 4033 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName())); 4034 } 4035 4036 // Try to force gcc to match the tool chain we want, if we recognize 4037 // the arch. 4038 // 4039 // FIXME: The triple class should directly provide the information we want 4040 // here. 4041 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) 4042 CmdArgs.push_back("-m32"); 4043 else if (Arch == llvm::Triple::x86_64 || Arch == llvm::Triple::ppc64 || 4044 Arch == llvm::Triple::ppc64le) 4045 CmdArgs.push_back("-m64"); 4046 4047 if (Output.isFilename()) { 4048 CmdArgs.push_back("-o"); 4049 CmdArgs.push_back(Output.getFilename()); 4050 } else { 4051 assert(Output.isNothing() && "Unexpected output"); 4052 CmdArgs.push_back("-fsyntax-only"); 4053 } 4054 4055 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 4056 options::OPT_Xassembler); 4057 4058 // Only pass -x if gcc will understand it; otherwise hope gcc 4059 // understands the suffix correctly. The main use case this would go 4060 // wrong in is for linker inputs if they happened to have an odd 4061 // suffix; really the only way to get this to happen is a command 4062 // like '-x foobar a.c' which will treat a.c like a linker input. 4063 // 4064 // FIXME: For the linker case specifically, can we safely convert 4065 // inputs into '-Wl,' options? 4066 for (InputInfoList::const_iterator 4067 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 4068 const InputInfo &II = *it; 4069 4070 // Don't try to pass LLVM or AST inputs to a generic gcc. 4071 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR || 4072 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC) 4073 D.Diag(diag::err_drv_no_linker_llvm_support) 4074 << getToolChain().getTripleString(); 4075 else if (II.getType() == types::TY_AST) 4076 D.Diag(diag::err_drv_no_ast_support) 4077 << getToolChain().getTripleString(); 4078 else if (II.getType() == types::TY_ModuleFile) 4079 D.Diag(diag::err_drv_no_module_support) 4080 << getToolChain().getTripleString(); 4081 4082 if (types::canTypeBeUserSpecified(II.getType())) { 4083 CmdArgs.push_back("-x"); 4084 CmdArgs.push_back(types::getTypeName(II.getType())); 4085 } 4086 4087 if (II.isFilename()) 4088 CmdArgs.push_back(II.getFilename()); 4089 else { 4090 const Arg &A = II.getInputArg(); 4091 4092 // Reverse translate some rewritten options. 4093 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) { 4094 CmdArgs.push_back("-lstdc++"); 4095 continue; 4096 } 4097 4098 // Don't render as input, we need gcc to do the translations. 4099 A.render(Args, CmdArgs); 4100 } 4101 } 4102 4103 const std::string customGCCName = D.getCCCGenericGCCName(); 4104 const char *GCCName; 4105 if (!customGCCName.empty()) 4106 GCCName = customGCCName.c_str(); 4107 else if (D.CCCIsCXX()) { 4108 GCCName = "g++"; 4109 } else 4110 GCCName = "gcc"; 4111 4112 const char *Exec = 4113 Args.MakeArgString(getToolChain().GetProgramPath(GCCName)); 4114 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4115} 4116 4117void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA, 4118 ArgStringList &CmdArgs) const { 4119 CmdArgs.push_back("-E"); 4120} 4121 4122void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA, 4123 ArgStringList &CmdArgs) const { 4124 // The type is good enough. 4125} 4126 4127void gcc::Compile::RenderExtraToolArgs(const JobAction &JA, 4128 ArgStringList &CmdArgs) const { 4129 const Driver &D = getToolChain().getDriver(); 4130 4131 // If -flto, etc. are present then make sure not to force assembly output. 4132 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR || 4133 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC) 4134 CmdArgs.push_back("-c"); 4135 else { 4136 if (JA.getType() != types::TY_PP_Asm) 4137 D.Diag(diag::err_drv_invalid_gcc_output_type) 4138 << getTypeName(JA.getType()); 4139 4140 CmdArgs.push_back("-S"); 4141 } 4142} 4143 4144void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA, 4145 ArgStringList &CmdArgs) const { 4146 CmdArgs.push_back("-c"); 4147} 4148 4149void gcc::Link::RenderExtraToolArgs(const JobAction &JA, 4150 ArgStringList &CmdArgs) const { 4151 // The types are (hopefully) good enough. 4152} 4153 4154// Hexagon tools start. 4155void hexagon::Assemble::RenderExtraToolArgs(const JobAction &JA, 4156 ArgStringList &CmdArgs) const { 4157 4158} 4159void hexagon::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 4160 const InputInfo &Output, 4161 const InputInfoList &Inputs, 4162 const ArgList &Args, 4163 const char *LinkingOutput) const { 4164 4165 const Driver &D = getToolChain().getDriver(); 4166 ArgStringList CmdArgs; 4167 4168 std::string MarchString = "-march="; 4169 MarchString += toolchains::Hexagon_TC::GetTargetCPU(Args); 4170 CmdArgs.push_back(Args.MakeArgString(MarchString)); 4171 4172 RenderExtraToolArgs(JA, CmdArgs); 4173 4174 if (Output.isFilename()) { 4175 CmdArgs.push_back("-o"); 4176 CmdArgs.push_back(Output.getFilename()); 4177 } else { 4178 assert(Output.isNothing() && "Unexpected output"); 4179 CmdArgs.push_back("-fsyntax-only"); 4180 } 4181 4182 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args); 4183 if (!SmallDataThreshold.empty()) 4184 CmdArgs.push_back( 4185 Args.MakeArgString(std::string("-G") + SmallDataThreshold)); 4186 4187 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 4188 options::OPT_Xassembler); 4189 4190 // Only pass -x if gcc will understand it; otherwise hope gcc 4191 // understands the suffix correctly. The main use case this would go 4192 // wrong in is for linker inputs if they happened to have an odd 4193 // suffix; really the only way to get this to happen is a command 4194 // like '-x foobar a.c' which will treat a.c like a linker input. 4195 // 4196 // FIXME: For the linker case specifically, can we safely convert 4197 // inputs into '-Wl,' options? 4198 for (InputInfoList::const_iterator 4199 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 4200 const InputInfo &II = *it; 4201 4202 // Don't try to pass LLVM or AST inputs to a generic gcc. 4203 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR || 4204 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC) 4205 D.Diag(clang::diag::err_drv_no_linker_llvm_support) 4206 << getToolChain().getTripleString(); 4207 else if (II.getType() == types::TY_AST) 4208 D.Diag(clang::diag::err_drv_no_ast_support) 4209 << getToolChain().getTripleString(); 4210 else if (II.getType() == types::TY_ModuleFile) 4211 D.Diag(diag::err_drv_no_module_support) 4212 << getToolChain().getTripleString(); 4213 4214 if (II.isFilename()) 4215 CmdArgs.push_back(II.getFilename()); 4216 else 4217 // Don't render as input, we need gcc to do the translations. FIXME: Pranav: What is this ? 4218 II.getInputArg().render(Args, CmdArgs); 4219 } 4220 4221 const char *GCCName = "hexagon-as"; 4222 const char *Exec = 4223 Args.MakeArgString(getToolChain().GetProgramPath(GCCName)); 4224 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4225 4226} 4227void hexagon::Link::RenderExtraToolArgs(const JobAction &JA, 4228 ArgStringList &CmdArgs) const { 4229 // The types are (hopefully) good enough. 4230} 4231 4232void hexagon::Link::ConstructJob(Compilation &C, const JobAction &JA, 4233 const InputInfo &Output, 4234 const InputInfoList &Inputs, 4235 const ArgList &Args, 4236 const char *LinkingOutput) const { 4237 4238 const toolchains::Hexagon_TC& ToolChain = 4239 static_cast<const toolchains::Hexagon_TC&>(getToolChain()); 4240 const Driver &D = ToolChain.getDriver(); 4241 4242 ArgStringList CmdArgs; 4243 4244 //---------------------------------------------------------------------------- 4245 // 4246 //---------------------------------------------------------------------------- 4247 bool hasStaticArg = Args.hasArg(options::OPT_static); 4248 bool buildingLib = Args.hasArg(options::OPT_shared); 4249 bool buildPIE = Args.hasArg(options::OPT_pie); 4250 bool incStdLib = !Args.hasArg(options::OPT_nostdlib); 4251 bool incStartFiles = !Args.hasArg(options::OPT_nostartfiles); 4252 bool incDefLibs = !Args.hasArg(options::OPT_nodefaultlibs); 4253 bool useShared = buildingLib && !hasStaticArg; 4254 4255 //---------------------------------------------------------------------------- 4256 // Silence warnings for various options 4257 //---------------------------------------------------------------------------- 4258 4259 Args.ClaimAllArgs(options::OPT_g_Group); 4260 Args.ClaimAllArgs(options::OPT_emit_llvm); 4261 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already 4262 // handled somewhere else. 4263 Args.ClaimAllArgs(options::OPT_static_libgcc); 4264 4265 //---------------------------------------------------------------------------- 4266 // 4267 //---------------------------------------------------------------------------- 4268 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(), 4269 e = ToolChain.ExtraOpts.end(); 4270 i != e; ++i) 4271 CmdArgs.push_back(i->c_str()); 4272 4273 std::string MarchString = toolchains::Hexagon_TC::GetTargetCPU(Args); 4274 CmdArgs.push_back(Args.MakeArgString("-m" + MarchString)); 4275 4276 if (buildingLib) { 4277 CmdArgs.push_back("-shared"); 4278 CmdArgs.push_back("-call_shared"); // should be the default, but doing as 4279 // hexagon-gcc does 4280 } 4281 4282 if (hasStaticArg) 4283 CmdArgs.push_back("-static"); 4284 4285 if (buildPIE && !buildingLib) 4286 CmdArgs.push_back("-pie"); 4287 4288 std::string SmallDataThreshold = GetHexagonSmallDataThresholdValue(Args); 4289 if (!SmallDataThreshold.empty()) { 4290 CmdArgs.push_back( 4291 Args.MakeArgString(std::string("-G") + SmallDataThreshold)); 4292 } 4293 4294 //---------------------------------------------------------------------------- 4295 // 4296 //---------------------------------------------------------------------------- 4297 CmdArgs.push_back("-o"); 4298 CmdArgs.push_back(Output.getFilename()); 4299 4300 const std::string MarchSuffix = "/" + MarchString; 4301 const std::string G0Suffix = "/G0"; 4302 const std::string MarchG0Suffix = MarchSuffix + G0Suffix; 4303 const std::string RootDir = toolchains::Hexagon_TC::GetGnuDir(D.InstalledDir) 4304 + "/"; 4305 const std::string StartFilesDir = RootDir 4306 + "hexagon/lib" 4307 + (buildingLib 4308 ? MarchG0Suffix : MarchSuffix); 4309 4310 //---------------------------------------------------------------------------- 4311 // moslib 4312 //---------------------------------------------------------------------------- 4313 std::vector<std::string> oslibs; 4314 bool hasStandalone= false; 4315 4316 for (arg_iterator it = Args.filtered_begin(options::OPT_moslib_EQ), 4317 ie = Args.filtered_end(); it != ie; ++it) { 4318 (*it)->claim(); 4319 oslibs.push_back((*it)->getValue()); 4320 hasStandalone = hasStandalone || (oslibs.back() == "standalone"); 4321 } 4322 if (oslibs.empty()) { 4323 oslibs.push_back("standalone"); 4324 hasStandalone = true; 4325 } 4326 4327 //---------------------------------------------------------------------------- 4328 // Start Files 4329 //---------------------------------------------------------------------------- 4330 if (incStdLib && incStartFiles) { 4331 4332 if (!buildingLib) { 4333 if (hasStandalone) { 4334 CmdArgs.push_back( 4335 Args.MakeArgString(StartFilesDir + "/crt0_standalone.o")); 4336 } 4337 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + "/crt0.o")); 4338 } 4339 std::string initObj = useShared ? "/initS.o" : "/init.o"; 4340 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + initObj)); 4341 } 4342 4343 //---------------------------------------------------------------------------- 4344 // Library Search Paths 4345 //---------------------------------------------------------------------------- 4346 const ToolChain::path_list &LibPaths = ToolChain.getFilePaths(); 4347 for (ToolChain::path_list::const_iterator 4348 i = LibPaths.begin(), 4349 e = LibPaths.end(); 4350 i != e; 4351 ++i) 4352 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i)); 4353 4354 //---------------------------------------------------------------------------- 4355 // 4356 //---------------------------------------------------------------------------- 4357 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 4358 Args.AddAllArgs(CmdArgs, options::OPT_e); 4359 Args.AddAllArgs(CmdArgs, options::OPT_s); 4360 Args.AddAllArgs(CmdArgs, options::OPT_t); 4361 Args.AddAllArgs(CmdArgs, options::OPT_u_Group); 4362 4363 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); 4364 4365 //---------------------------------------------------------------------------- 4366 // Libraries 4367 //---------------------------------------------------------------------------- 4368 if (incStdLib && incDefLibs) { 4369 if (D.CCCIsCXX()) { 4370 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 4371 CmdArgs.push_back("-lm"); 4372 } 4373 4374 CmdArgs.push_back("--start-group"); 4375 4376 if (!buildingLib) { 4377 for(std::vector<std::string>::iterator i = oslibs.begin(), 4378 e = oslibs.end(); i != e; ++i) 4379 CmdArgs.push_back(Args.MakeArgString("-l" + *i)); 4380 CmdArgs.push_back("-lc"); 4381 } 4382 CmdArgs.push_back("-lgcc"); 4383 4384 CmdArgs.push_back("--end-group"); 4385 } 4386 4387 //---------------------------------------------------------------------------- 4388 // End files 4389 //---------------------------------------------------------------------------- 4390 if (incStdLib && incStartFiles) { 4391 std::string finiObj = useShared ? "/finiS.o" : "/fini.o"; 4392 CmdArgs.push_back(Args.MakeArgString(StartFilesDir + finiObj)); 4393 } 4394 4395 std::string Linker = ToolChain.GetProgramPath("hexagon-ld"); 4396 C.addCommand(new Command(JA, *this, Args.MakeArgString(Linker), CmdArgs)); 4397} 4398// Hexagon tools end. 4399 4400llvm::Triple::ArchType darwin::getArchTypeForDarwinArchName(StringRef Str) { 4401 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for 4402 // archs which Darwin doesn't use. 4403 4404 // The matching this routine does is fairly pointless, since it is neither the 4405 // complete architecture list, nor a reasonable subset. The problem is that 4406 // historically the driver driver accepts this and also ties its -march= 4407 // handling to the architecture name, so we need to be careful before removing 4408 // support for it. 4409 4410 // This code must be kept in sync with Clang's Darwin specific argument 4411 // translation. 4412 4413 return llvm::StringSwitch<llvm::Triple::ArchType>(Str) 4414 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc) 4415 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc) 4416 .Case("ppc64", llvm::Triple::ppc64) 4417 .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86) 4418 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4", 4419 llvm::Triple::x86) 4420 .Case("x86_64", llvm::Triple::x86_64) 4421 // This is derived from the driver driver. 4422 .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm) 4423 .Cases("armv7", "armv7em", "armv7f", "armv7k", "armv7m", llvm::Triple::arm) 4424 .Cases("armv7s", "xscale", llvm::Triple::arm) 4425 .Case("r600", llvm::Triple::r600) 4426 .Case("nvptx", llvm::Triple::nvptx) 4427 .Case("nvptx64", llvm::Triple::nvptx64) 4428 .Case("amdil", llvm::Triple::amdil) 4429 .Case("spir", llvm::Triple::spir) 4430 .Default(llvm::Triple::UnknownArch); 4431} 4432 4433const char *Clang::getBaseInputName(const ArgList &Args, 4434 const InputInfoList &Inputs) { 4435 return Args.MakeArgString( 4436 llvm::sys::path::filename(Inputs[0].getBaseInput())); 4437} 4438 4439const char *Clang::getBaseInputStem(const ArgList &Args, 4440 const InputInfoList &Inputs) { 4441 const char *Str = getBaseInputName(Args, Inputs); 4442 4443 if (const char *End = strrchr(Str, '.')) 4444 return Args.MakeArgString(std::string(Str, End)); 4445 4446 return Str; 4447} 4448 4449const char *Clang::getDependencyFileName(const ArgList &Args, 4450 const InputInfoList &Inputs) { 4451 // FIXME: Think about this more. 4452 std::string Res; 4453 4454 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { 4455 std::string Str(OutputOpt->getValue()); 4456 Res = Str.substr(0, Str.rfind('.')); 4457 } else { 4458 Res = getBaseInputStem(Args, Inputs); 4459 } 4460 return Args.MakeArgString(Res + ".d"); 4461} 4462 4463void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 4464 const InputInfo &Output, 4465 const InputInfoList &Inputs, 4466 const ArgList &Args, 4467 const char *LinkingOutput) const { 4468 ArgStringList CmdArgs; 4469 4470 assert(Inputs.size() == 1 && "Unexpected number of inputs."); 4471 const InputInfo &Input = Inputs[0]; 4472 4473 // Determine the original source input. 4474 const Action *SourceAction = &JA; 4475 while (SourceAction->getKind() != Action::InputClass) { 4476 assert(!SourceAction->getInputs().empty() && "unexpected root action!"); 4477 SourceAction = SourceAction->getInputs()[0]; 4478 } 4479 4480 // Forward -g, assuming we are dealing with an actual assembly file. 4481 if (SourceAction->getType() == types::TY_Asm || 4482 SourceAction->getType() == types::TY_PP_Asm) { 4483 if (Args.hasArg(options::OPT_gstabs)) 4484 CmdArgs.push_back("--gstabs"); 4485 else if (Args.hasArg(options::OPT_g_Group)) 4486 CmdArgs.push_back("-g"); 4487 } 4488 4489 // Derived from asm spec. 4490 AddDarwinArch(Args, CmdArgs); 4491 4492 // Use -force_cpusubtype_ALL on x86 by default. 4493 if (getToolChain().getArch() == llvm::Triple::x86 || 4494 getToolChain().getArch() == llvm::Triple::x86_64 || 4495 Args.hasArg(options::OPT_force__cpusubtype__ALL)) 4496 CmdArgs.push_back("-force_cpusubtype_ALL"); 4497 4498 if (getToolChain().getArch() != llvm::Triple::x86_64 && 4499 (((Args.hasArg(options::OPT_mkernel) || 4500 Args.hasArg(options::OPT_fapple_kext)) && 4501 (!getDarwinToolChain().isTargetIPhoneOS() || 4502 getDarwinToolChain().isIPhoneOSVersionLT(6, 0))) || 4503 Args.hasArg(options::OPT_static))) 4504 CmdArgs.push_back("-static"); 4505 4506 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 4507 options::OPT_Xassembler); 4508 4509 assert(Output.isFilename() && "Unexpected lipo output."); 4510 CmdArgs.push_back("-o"); 4511 CmdArgs.push_back(Output.getFilename()); 4512 4513 assert(Input.isFilename() && "Invalid input."); 4514 CmdArgs.push_back(Input.getFilename()); 4515 4516 // asm_final spec is empty. 4517 4518 const char *Exec = 4519 Args.MakeArgString(getToolChain().GetProgramPath("as")); 4520 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4521} 4522 4523void darwin::DarwinTool::anchor() {} 4524 4525void darwin::DarwinTool::AddDarwinArch(const ArgList &Args, 4526 ArgStringList &CmdArgs) const { 4527 StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args); 4528 4529 // Derived from darwin_arch spec. 4530 CmdArgs.push_back("-arch"); 4531 CmdArgs.push_back(Args.MakeArgString(ArchName)); 4532 4533 // FIXME: Is this needed anymore? 4534 if (ArchName == "arm") 4535 CmdArgs.push_back("-force_cpusubtype_ALL"); 4536} 4537 4538bool darwin::Link::NeedsTempPath(const InputInfoList &Inputs) const { 4539 // We only need to generate a temp path for LTO if we aren't compiling object 4540 // files. When compiling source files, we run 'dsymutil' after linking. We 4541 // don't run 'dsymutil' when compiling object files. 4542 for (InputInfoList::const_iterator 4543 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) 4544 if (it->getType() != types::TY_Object) 4545 return true; 4546 4547 return false; 4548} 4549 4550void darwin::Link::AddLinkArgs(Compilation &C, 4551 const ArgList &Args, 4552 ArgStringList &CmdArgs, 4553 const InputInfoList &Inputs) const { 4554 const Driver &D = getToolChain().getDriver(); 4555 const toolchains::Darwin &DarwinTC = getDarwinToolChain(); 4556 4557 unsigned Version[3] = { 0, 0, 0 }; 4558 if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) { 4559 bool HadExtra; 4560 if (!Driver::GetReleaseVersion(A->getValue(), Version[0], 4561 Version[1], Version[2], HadExtra) || 4562 HadExtra) 4563 D.Diag(diag::err_drv_invalid_version_number) 4564 << A->getAsString(Args); 4565 } 4566 4567 // Newer linkers support -demangle, pass it if supported and not disabled by 4568 // the user. 4569 if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) { 4570 // Don't pass -demangle to ld_classic. 4571 // 4572 // FIXME: This is a temporary workaround, ld should be handling this. 4573 bool UsesLdClassic = (getToolChain().getArch() == llvm::Triple::x86 && 4574 Args.hasArg(options::OPT_static)); 4575 if (getToolChain().getArch() == llvm::Triple::x86) { 4576 for (arg_iterator it = Args.filtered_begin(options::OPT_Xlinker, 4577 options::OPT_Wl_COMMA), 4578 ie = Args.filtered_end(); it != ie; ++it) { 4579 const Arg *A = *it; 4580 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) 4581 if (StringRef(A->getValue(i)) == "-kext") 4582 UsesLdClassic = true; 4583 } 4584 } 4585 if (!UsesLdClassic) 4586 CmdArgs.push_back("-demangle"); 4587 } 4588 4589 if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137) 4590 CmdArgs.push_back("-export_dynamic"); 4591 4592 // If we are using LTO, then automatically create a temporary file path for 4593 // the linker to use, so that it's lifetime will extend past a possible 4594 // dsymutil step. 4595 if (Version[0] >= 116 && D.IsUsingLTO(Args) && NeedsTempPath(Inputs)) { 4596 const char *TmpPath = C.getArgs().MakeArgString( 4597 D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object))); 4598 C.addTempFile(TmpPath); 4599 CmdArgs.push_back("-object_path_lto"); 4600 CmdArgs.push_back(TmpPath); 4601 } 4602 4603 // Derived from the "link" spec. 4604 Args.AddAllArgs(CmdArgs, options::OPT_static); 4605 if (!Args.hasArg(options::OPT_static)) 4606 CmdArgs.push_back("-dynamic"); 4607 if (Args.hasArg(options::OPT_fgnu_runtime)) { 4608 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu 4609 // here. How do we wish to handle such things? 4610 } 4611 4612 if (!Args.hasArg(options::OPT_dynamiclib)) { 4613 AddDarwinArch(Args, CmdArgs); 4614 // FIXME: Why do this only on this path? 4615 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL); 4616 4617 Args.AddLastArg(CmdArgs, options::OPT_bundle); 4618 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader); 4619 Args.AddAllArgs(CmdArgs, options::OPT_client__name); 4620 4621 Arg *A; 4622 if ((A = Args.getLastArg(options::OPT_compatibility__version)) || 4623 (A = Args.getLastArg(options::OPT_current__version)) || 4624 (A = Args.getLastArg(options::OPT_install__name))) 4625 D.Diag(diag::err_drv_argument_only_allowed_with) 4626 << A->getAsString(Args) << "-dynamiclib"; 4627 4628 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace); 4629 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs); 4630 Args.AddLastArg(CmdArgs, options::OPT_private__bundle); 4631 } else { 4632 CmdArgs.push_back("-dylib"); 4633 4634 Arg *A; 4635 if ((A = Args.getLastArg(options::OPT_bundle)) || 4636 (A = Args.getLastArg(options::OPT_bundle__loader)) || 4637 (A = Args.getLastArg(options::OPT_client__name)) || 4638 (A = Args.getLastArg(options::OPT_force__flat__namespace)) || 4639 (A = Args.getLastArg(options::OPT_keep__private__externs)) || 4640 (A = Args.getLastArg(options::OPT_private__bundle))) 4641 D.Diag(diag::err_drv_argument_not_allowed_with) 4642 << A->getAsString(Args) << "-dynamiclib"; 4643 4644 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version, 4645 "-dylib_compatibility_version"); 4646 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version, 4647 "-dylib_current_version"); 4648 4649 AddDarwinArch(Args, CmdArgs); 4650 4651 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name, 4652 "-dylib_install_name"); 4653 } 4654 4655 Args.AddLastArg(CmdArgs, options::OPT_all__load); 4656 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client); 4657 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load); 4658 if (DarwinTC.isTargetIPhoneOS()) 4659 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal); 4660 Args.AddLastArg(CmdArgs, options::OPT_dead__strip); 4661 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms); 4662 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file); 4663 Args.AddLastArg(CmdArgs, options::OPT_dynamic); 4664 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list); 4665 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace); 4666 Args.AddAllArgs(CmdArgs, options::OPT_force__load); 4667 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names); 4668 Args.AddAllArgs(CmdArgs, options::OPT_image__base); 4669 Args.AddAllArgs(CmdArgs, options::OPT_init); 4670 4671 // Add the deployment target. 4672 VersionTuple TargetVersion = DarwinTC.getTargetVersion(); 4673 4674 // If we had an explicit -mios-simulator-version-min argument, honor that, 4675 // otherwise use the traditional deployment targets. We can't just check the 4676 // is-sim attribute because existing code follows this path, and the linker 4677 // may not handle the argument. 4678 // 4679 // FIXME: We may be able to remove this, once we can verify no one depends on 4680 // it. 4681 if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ)) 4682 CmdArgs.push_back("-ios_simulator_version_min"); 4683 else if (DarwinTC.isTargetIPhoneOS()) 4684 CmdArgs.push_back("-iphoneos_version_min"); 4685 else 4686 CmdArgs.push_back("-macosx_version_min"); 4687 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString())); 4688 4689 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs); 4690 Args.AddLastArg(CmdArgs, options::OPT_multi__module); 4691 Args.AddLastArg(CmdArgs, options::OPT_single__module); 4692 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined); 4693 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused); 4694 4695 if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE, 4696 options::OPT_fno_pie, 4697 options::OPT_fno_PIE)) { 4698 if (A->getOption().matches(options::OPT_fpie) || 4699 A->getOption().matches(options::OPT_fPIE)) 4700 CmdArgs.push_back("-pie"); 4701 else 4702 CmdArgs.push_back("-no_pie"); 4703 } 4704 4705 Args.AddLastArg(CmdArgs, options::OPT_prebind); 4706 Args.AddLastArg(CmdArgs, options::OPT_noprebind); 4707 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding); 4708 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules); 4709 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs); 4710 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate); 4711 Args.AddAllArgs(CmdArgs, options::OPT_sectorder); 4712 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr); 4713 Args.AddAllArgs(CmdArgs, options::OPT_segprot); 4714 Args.AddAllArgs(CmdArgs, options::OPT_segaddr); 4715 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr); 4716 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr); 4717 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table); 4718 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename); 4719 Args.AddAllArgs(CmdArgs, options::OPT_sub__library); 4720 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella); 4721 4722 // Give --sysroot= preference, over the Apple specific behavior to also use 4723 // --isysroot as the syslibroot. 4724 StringRef sysroot = C.getSysRoot(); 4725 if (sysroot != "") { 4726 CmdArgs.push_back("-syslibroot"); 4727 CmdArgs.push_back(C.getArgs().MakeArgString(sysroot)); 4728 } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { 4729 CmdArgs.push_back("-syslibroot"); 4730 CmdArgs.push_back(A->getValue()); 4731 } 4732 4733 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace); 4734 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints); 4735 Args.AddAllArgs(CmdArgs, options::OPT_umbrella); 4736 Args.AddAllArgs(CmdArgs, options::OPT_undefined); 4737 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list); 4738 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches); 4739 Args.AddLastArg(CmdArgs, options::OPT_X_Flag); 4740 Args.AddAllArgs(CmdArgs, options::OPT_y); 4741 Args.AddLastArg(CmdArgs, options::OPT_w); 4742 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size); 4743 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__); 4744 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit); 4745 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit); 4746 Args.AddAllArgs(CmdArgs, options::OPT_sectalign); 4747 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols); 4748 Args.AddAllArgs(CmdArgs, options::OPT_segcreate); 4749 Args.AddLastArg(CmdArgs, options::OPT_whyload); 4750 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded); 4751 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name); 4752 Args.AddLastArg(CmdArgs, options::OPT_dylinker); 4753 Args.AddLastArg(CmdArgs, options::OPT_Mach); 4754} 4755 4756void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, 4757 const InputInfo &Output, 4758 const InputInfoList &Inputs, 4759 const ArgList &Args, 4760 const char *LinkingOutput) const { 4761 assert(Output.getType() == types::TY_Image && "Invalid linker output type."); 4762 4763 // The logic here is derived from gcc's behavior; most of which 4764 // comes from specs (starting with link_command). Consult gcc for 4765 // more information. 4766 ArgStringList CmdArgs; 4767 4768 /// Hack(tm) to ignore linking errors when we are doing ARC migration. 4769 if (Args.hasArg(options::OPT_ccc_arcmt_check, 4770 options::OPT_ccc_arcmt_migrate)) { 4771 for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) 4772 (*I)->claim(); 4773 const char *Exec = 4774 Args.MakeArgString(getToolChain().GetProgramPath("touch")); 4775 CmdArgs.push_back(Output.getFilename()); 4776 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4777 return; 4778 } 4779 4780 // I'm not sure why this particular decomposition exists in gcc, but 4781 // we follow suite for ease of comparison. 4782 AddLinkArgs(C, Args, CmdArgs, Inputs); 4783 4784 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag); 4785 Args.AddAllArgs(CmdArgs, options::OPT_s); 4786 Args.AddAllArgs(CmdArgs, options::OPT_t); 4787 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 4788 Args.AddAllArgs(CmdArgs, options::OPT_u_Group); 4789 Args.AddLastArg(CmdArgs, options::OPT_e); 4790 Args.AddAllArgs(CmdArgs, options::OPT_r); 4791 4792 // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading 4793 // members of static archive libraries which implement Objective-C classes or 4794 // categories. 4795 if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX)) 4796 CmdArgs.push_back("-ObjC"); 4797 4798 CmdArgs.push_back("-o"); 4799 CmdArgs.push_back(Output.getFilename()); 4800 4801 if (!Args.hasArg(options::OPT_nostdlib) && 4802 !Args.hasArg(options::OPT_nostartfiles)) { 4803 // Derived from startfile spec. 4804 if (Args.hasArg(options::OPT_dynamiclib)) { 4805 // Derived from darwin_dylib1 spec. 4806 if (getDarwinToolChain().isTargetIOSSimulator()) { 4807 // The simulator doesn't have a versioned crt1 file. 4808 CmdArgs.push_back("-ldylib1.o"); 4809 } else if (getDarwinToolChain().isTargetIPhoneOS()) { 4810 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1)) 4811 CmdArgs.push_back("-ldylib1.o"); 4812 } else { 4813 if (getDarwinToolChain().isMacosxVersionLT(10, 5)) 4814 CmdArgs.push_back("-ldylib1.o"); 4815 else if (getDarwinToolChain().isMacosxVersionLT(10, 6)) 4816 CmdArgs.push_back("-ldylib1.10.5.o"); 4817 } 4818 } else { 4819 if (Args.hasArg(options::OPT_bundle)) { 4820 if (!Args.hasArg(options::OPT_static)) { 4821 // Derived from darwin_bundle1 spec. 4822 if (getDarwinToolChain().isTargetIOSSimulator()) { 4823 // The simulator doesn't have a versioned crt1 file. 4824 CmdArgs.push_back("-lbundle1.o"); 4825 } else if (getDarwinToolChain().isTargetIPhoneOS()) { 4826 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1)) 4827 CmdArgs.push_back("-lbundle1.o"); 4828 } else { 4829 if (getDarwinToolChain().isMacosxVersionLT(10, 6)) 4830 CmdArgs.push_back("-lbundle1.o"); 4831 } 4832 } 4833 } else { 4834 if (Args.hasArg(options::OPT_pg) && 4835 getToolChain().SupportsProfiling()) { 4836 if (Args.hasArg(options::OPT_static) || 4837 Args.hasArg(options::OPT_object) || 4838 Args.hasArg(options::OPT_preload)) { 4839 CmdArgs.push_back("-lgcrt0.o"); 4840 } else { 4841 CmdArgs.push_back("-lgcrt1.o"); 4842 4843 // darwin_crt2 spec is empty. 4844 } 4845 // By default on OS X 10.8 and later, we don't link with a crt1.o 4846 // file and the linker knows to use _main as the entry point. But, 4847 // when compiling with -pg, we need to link with the gcrt1.o file, 4848 // so pass the -no_new_main option to tell the linker to use the 4849 // "start" symbol as the entry point. 4850 if (getDarwinToolChain().isTargetMacOS() && 4851 !getDarwinToolChain().isMacosxVersionLT(10, 8)) 4852 CmdArgs.push_back("-no_new_main"); 4853 } else { 4854 if (Args.hasArg(options::OPT_static) || 4855 Args.hasArg(options::OPT_object) || 4856 Args.hasArg(options::OPT_preload)) { 4857 CmdArgs.push_back("-lcrt0.o"); 4858 } else { 4859 // Derived from darwin_crt1 spec. 4860 if (getDarwinToolChain().isTargetIOSSimulator()) { 4861 // The simulator doesn't have a versioned crt1 file. 4862 CmdArgs.push_back("-lcrt1.o"); 4863 } else if (getDarwinToolChain().isTargetIPhoneOS()) { 4864 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1)) 4865 CmdArgs.push_back("-lcrt1.o"); 4866 else if (getDarwinToolChain().isIPhoneOSVersionLT(6, 0)) 4867 CmdArgs.push_back("-lcrt1.3.1.o"); 4868 } else { 4869 if (getDarwinToolChain().isMacosxVersionLT(10, 5)) 4870 CmdArgs.push_back("-lcrt1.o"); 4871 else if (getDarwinToolChain().isMacosxVersionLT(10, 6)) 4872 CmdArgs.push_back("-lcrt1.10.5.o"); 4873 else if (getDarwinToolChain().isMacosxVersionLT(10, 8)) 4874 CmdArgs.push_back("-lcrt1.10.6.o"); 4875 4876 // darwin_crt2 spec is empty. 4877 } 4878 } 4879 } 4880 } 4881 } 4882 4883 if (!getDarwinToolChain().isTargetIPhoneOS() && 4884 Args.hasArg(options::OPT_shared_libgcc) && 4885 getDarwinToolChain().isMacosxVersionLT(10, 5)) { 4886 const char *Str = 4887 Args.MakeArgString(getToolChain().GetFilePath("crt3.o")); 4888 CmdArgs.push_back(Str); 4889 } 4890 } 4891 4892 Args.AddAllArgs(CmdArgs, options::OPT_L); 4893 4894 if (Args.hasArg(options::OPT_fopenmp)) 4895 // This is more complicated in gcc... 4896 CmdArgs.push_back("-lgomp"); 4897 4898 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 4899 4900 if (isObjCRuntimeLinked(Args) && 4901 !Args.hasArg(options::OPT_nostdlib) && 4902 !Args.hasArg(options::OPT_nodefaultlibs)) { 4903 // Avoid linking compatibility stubs on i386 mac. 4904 if (!getDarwinToolChain().isTargetMacOS() || 4905 getDarwinToolChain().getArch() != llvm::Triple::x86) { 4906 // If we don't have ARC or subscripting runtime support, link in the 4907 // runtime stubs. We have to do this *before* adding any of the normal 4908 // linker inputs so that its initializer gets run first. 4909 ObjCRuntime runtime = 4910 getDarwinToolChain().getDefaultObjCRuntime(/*nonfragile*/ true); 4911 // We use arclite library for both ARC and subscripting support. 4912 if ((!runtime.hasNativeARC() && isObjCAutoRefCount(Args)) || 4913 !runtime.hasSubscripting()) 4914 getDarwinToolChain().AddLinkARCArgs(Args, CmdArgs); 4915 } 4916 CmdArgs.push_back("-framework"); 4917 CmdArgs.push_back("Foundation"); 4918 // Link libobj. 4919 CmdArgs.push_back("-lobjc"); 4920 } 4921 4922 if (LinkingOutput) { 4923 CmdArgs.push_back("-arch_multiple"); 4924 CmdArgs.push_back("-final_output"); 4925 CmdArgs.push_back(LinkingOutput); 4926 } 4927 4928 if (Args.hasArg(options::OPT_fnested_functions)) 4929 CmdArgs.push_back("-allow_stack_execute"); 4930 4931 if (!Args.hasArg(options::OPT_nostdlib) && 4932 !Args.hasArg(options::OPT_nodefaultlibs)) { 4933 if (getToolChain().getDriver().CCCIsCXX()) 4934 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 4935 4936 // link_ssp spec is empty. 4937 4938 // Let the tool chain choose which runtime library to link. 4939 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs); 4940 } 4941 4942 if (!Args.hasArg(options::OPT_nostdlib) && 4943 !Args.hasArg(options::OPT_nostartfiles)) { 4944 // endfile_spec is empty. 4945 } 4946 4947 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 4948 Args.AddAllArgs(CmdArgs, options::OPT_F); 4949 4950 const char *Exec = 4951 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 4952 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4953} 4954 4955void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA, 4956 const InputInfo &Output, 4957 const InputInfoList &Inputs, 4958 const ArgList &Args, 4959 const char *LinkingOutput) const { 4960 ArgStringList CmdArgs; 4961 4962 CmdArgs.push_back("-create"); 4963 assert(Output.isFilename() && "Unexpected lipo output."); 4964 4965 CmdArgs.push_back("-output"); 4966 CmdArgs.push_back(Output.getFilename()); 4967 4968 for (InputInfoList::const_iterator 4969 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 4970 const InputInfo &II = *it; 4971 assert(II.isFilename() && "Unexpected lipo input."); 4972 CmdArgs.push_back(II.getFilename()); 4973 } 4974 const char *Exec = 4975 Args.MakeArgString(getToolChain().GetProgramPath("lipo")); 4976 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4977} 4978 4979void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA, 4980 const InputInfo &Output, 4981 const InputInfoList &Inputs, 4982 const ArgList &Args, 4983 const char *LinkingOutput) const { 4984 ArgStringList CmdArgs; 4985 4986 CmdArgs.push_back("-o"); 4987 CmdArgs.push_back(Output.getFilename()); 4988 4989 assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); 4990 const InputInfo &Input = Inputs[0]; 4991 assert(Input.isFilename() && "Unexpected dsymutil input."); 4992 CmdArgs.push_back(Input.getFilename()); 4993 4994 const char *Exec = 4995 Args.MakeArgString(getToolChain().GetProgramPath("dsymutil")); 4996 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 4997} 4998 4999void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA, 5000 const InputInfo &Output, 5001 const InputInfoList &Inputs, 5002 const ArgList &Args, 5003 const char *LinkingOutput) const { 5004 ArgStringList CmdArgs; 5005 CmdArgs.push_back("--verify"); 5006 CmdArgs.push_back("--debug-info"); 5007 CmdArgs.push_back("--eh-frame"); 5008 CmdArgs.push_back("--quiet"); 5009 5010 assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); 5011 const InputInfo &Input = Inputs[0]; 5012 assert(Input.isFilename() && "Unexpected verify input"); 5013 5014 // Grabbing the output of the earlier dsymutil run. 5015 CmdArgs.push_back(Input.getFilename()); 5016 5017 const char *Exec = 5018 Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump")); 5019 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5020} 5021 5022void solaris::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5023 const InputInfo &Output, 5024 const InputInfoList &Inputs, 5025 const ArgList &Args, 5026 const char *LinkingOutput) const { 5027 ArgStringList CmdArgs; 5028 5029 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5030 options::OPT_Xassembler); 5031 5032 CmdArgs.push_back("-o"); 5033 CmdArgs.push_back(Output.getFilename()); 5034 5035 for (InputInfoList::const_iterator 5036 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5037 const InputInfo &II = *it; 5038 CmdArgs.push_back(II.getFilename()); 5039 } 5040 5041 const char *Exec = 5042 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5043 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5044} 5045 5046 5047void solaris::Link::ConstructJob(Compilation &C, const JobAction &JA, 5048 const InputInfo &Output, 5049 const InputInfoList &Inputs, 5050 const ArgList &Args, 5051 const char *LinkingOutput) const { 5052 // FIXME: Find a real GCC, don't hard-code versions here 5053 std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/"; 5054 const llvm::Triple &T = getToolChain().getTriple(); 5055 std::string LibPath = "/usr/lib/"; 5056 llvm::Triple::ArchType Arch = T.getArch(); 5057 switch (Arch) { 5058 case llvm::Triple::x86: 5059 GCCLibPath += 5060 ("i386-" + T.getVendorName() + "-" + T.getOSName()).str() + "/4.5.2/"; 5061 break; 5062 case llvm::Triple::x86_64: 5063 GCCLibPath += ("i386-" + T.getVendorName() + "-" + T.getOSName()).str(); 5064 GCCLibPath += "/4.5.2/amd64/"; 5065 LibPath += "amd64/"; 5066 break; 5067 default: 5068 llvm_unreachable("Unsupported architecture"); 5069 } 5070 5071 ArgStringList CmdArgs; 5072 5073 // Demangle C++ names in errors 5074 CmdArgs.push_back("-C"); 5075 5076 if ((!Args.hasArg(options::OPT_nostdlib)) && 5077 (!Args.hasArg(options::OPT_shared))) { 5078 CmdArgs.push_back("-e"); 5079 CmdArgs.push_back("_start"); 5080 } 5081 5082 if (Args.hasArg(options::OPT_static)) { 5083 CmdArgs.push_back("-Bstatic"); 5084 CmdArgs.push_back("-dn"); 5085 } else { 5086 CmdArgs.push_back("-Bdynamic"); 5087 if (Args.hasArg(options::OPT_shared)) { 5088 CmdArgs.push_back("-shared"); 5089 } else { 5090 CmdArgs.push_back("--dynamic-linker"); 5091 CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1")); 5092 } 5093 } 5094 5095 if (Output.isFilename()) { 5096 CmdArgs.push_back("-o"); 5097 CmdArgs.push_back(Output.getFilename()); 5098 } else { 5099 assert(Output.isNothing() && "Invalid output."); 5100 } 5101 5102 if (!Args.hasArg(options::OPT_nostdlib) && 5103 !Args.hasArg(options::OPT_nostartfiles)) { 5104 if (!Args.hasArg(options::OPT_shared)) { 5105 CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o")); 5106 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o")); 5107 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o")); 5108 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o")); 5109 } else { 5110 CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o")); 5111 CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o")); 5112 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o")); 5113 } 5114 if (getToolChain().getDriver().CCCIsCXX()) 5115 CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o")); 5116 } 5117 5118 CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath)); 5119 5120 Args.AddAllArgs(CmdArgs, options::OPT_L); 5121 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5122 Args.AddAllArgs(CmdArgs, options::OPT_e); 5123 Args.AddAllArgs(CmdArgs, options::OPT_r); 5124 5125 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5126 5127 if (!Args.hasArg(options::OPT_nostdlib) && 5128 !Args.hasArg(options::OPT_nodefaultlibs)) { 5129 if (getToolChain().getDriver().CCCIsCXX()) 5130 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5131 CmdArgs.push_back("-lgcc_s"); 5132 if (!Args.hasArg(options::OPT_shared)) { 5133 CmdArgs.push_back("-lgcc"); 5134 CmdArgs.push_back("-lc"); 5135 CmdArgs.push_back("-lm"); 5136 } 5137 } 5138 5139 if (!Args.hasArg(options::OPT_nostdlib) && 5140 !Args.hasArg(options::OPT_nostartfiles)) { 5141 CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o")); 5142 } 5143 CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o")); 5144 5145 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 5146 5147 const char *Exec = 5148 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5149 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5150} 5151 5152void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5153 const InputInfo &Output, 5154 const InputInfoList &Inputs, 5155 const ArgList &Args, 5156 const char *LinkingOutput) const { 5157 ArgStringList CmdArgs; 5158 5159 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5160 options::OPT_Xassembler); 5161 5162 CmdArgs.push_back("-o"); 5163 CmdArgs.push_back(Output.getFilename()); 5164 5165 for (InputInfoList::const_iterator 5166 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5167 const InputInfo &II = *it; 5168 CmdArgs.push_back(II.getFilename()); 5169 } 5170 5171 const char *Exec = 5172 Args.MakeArgString(getToolChain().GetProgramPath("gas")); 5173 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5174} 5175 5176void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA, 5177 const InputInfo &Output, 5178 const InputInfoList &Inputs, 5179 const ArgList &Args, 5180 const char *LinkingOutput) const { 5181 ArgStringList CmdArgs; 5182 5183 if ((!Args.hasArg(options::OPT_nostdlib)) && 5184 (!Args.hasArg(options::OPT_shared))) { 5185 CmdArgs.push_back("-e"); 5186 CmdArgs.push_back("_start"); 5187 } 5188 5189 if (Args.hasArg(options::OPT_static)) { 5190 CmdArgs.push_back("-Bstatic"); 5191 CmdArgs.push_back("-dn"); 5192 } else { 5193// CmdArgs.push_back("--eh-frame-hdr"); 5194 CmdArgs.push_back("-Bdynamic"); 5195 if (Args.hasArg(options::OPT_shared)) { 5196 CmdArgs.push_back("-shared"); 5197 } else { 5198 CmdArgs.push_back("--dynamic-linker"); 5199 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1 5200 } 5201 } 5202 5203 if (Output.isFilename()) { 5204 CmdArgs.push_back("-o"); 5205 CmdArgs.push_back(Output.getFilename()); 5206 } else { 5207 assert(Output.isNothing() && "Invalid output."); 5208 } 5209 5210 if (!Args.hasArg(options::OPT_nostdlib) && 5211 !Args.hasArg(options::OPT_nostartfiles)) { 5212 if (!Args.hasArg(options::OPT_shared)) { 5213 CmdArgs.push_back(Args.MakeArgString( 5214 getToolChain().GetFilePath("crt1.o"))); 5215 CmdArgs.push_back(Args.MakeArgString( 5216 getToolChain().GetFilePath("crti.o"))); 5217 CmdArgs.push_back(Args.MakeArgString( 5218 getToolChain().GetFilePath("crtbegin.o"))); 5219 } else { 5220 CmdArgs.push_back(Args.MakeArgString( 5221 getToolChain().GetFilePath("crti.o"))); 5222 } 5223 CmdArgs.push_back(Args.MakeArgString( 5224 getToolChain().GetFilePath("crtn.o"))); 5225 } 5226 5227 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/" 5228 + getToolChain().getTripleString() 5229 + "/4.2.4")); 5230 5231 Args.AddAllArgs(CmdArgs, options::OPT_L); 5232 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5233 Args.AddAllArgs(CmdArgs, options::OPT_e); 5234 5235 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5236 5237 if (!Args.hasArg(options::OPT_nostdlib) && 5238 !Args.hasArg(options::OPT_nodefaultlibs)) { 5239 // FIXME: For some reason GCC passes -lgcc before adding 5240 // the default system libraries. Just mimic this for now. 5241 CmdArgs.push_back("-lgcc"); 5242 5243 if (Args.hasArg(options::OPT_pthread)) 5244 CmdArgs.push_back("-pthread"); 5245 if (!Args.hasArg(options::OPT_shared)) 5246 CmdArgs.push_back("-lc"); 5247 CmdArgs.push_back("-lgcc"); 5248 } 5249 5250 if (!Args.hasArg(options::OPT_nostdlib) && 5251 !Args.hasArg(options::OPT_nostartfiles)) { 5252 if (!Args.hasArg(options::OPT_shared)) 5253 CmdArgs.push_back(Args.MakeArgString( 5254 getToolChain().GetFilePath("crtend.o"))); 5255 } 5256 5257 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 5258 5259 const char *Exec = 5260 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5261 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5262} 5263 5264void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5265 const InputInfo &Output, 5266 const InputInfoList &Inputs, 5267 const ArgList &Args, 5268 const char *LinkingOutput) const { 5269 ArgStringList CmdArgs; 5270 5271 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5272 options::OPT_Xassembler); 5273 5274 CmdArgs.push_back("-o"); 5275 CmdArgs.push_back(Output.getFilename()); 5276 5277 for (InputInfoList::const_iterator 5278 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5279 const InputInfo &II = *it; 5280 CmdArgs.push_back(II.getFilename()); 5281 } 5282 5283 const char *Exec = 5284 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5285 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5286} 5287 5288void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA, 5289 const InputInfo &Output, 5290 const InputInfoList &Inputs, 5291 const ArgList &Args, 5292 const char *LinkingOutput) const { 5293 const Driver &D = getToolChain().getDriver(); 5294 ArgStringList CmdArgs; 5295 5296 // Silence warning for "clang -g foo.o -o foo" 5297 Args.ClaimAllArgs(options::OPT_g_Group); 5298 // and "clang -emit-llvm foo.o -o foo" 5299 Args.ClaimAllArgs(options::OPT_emit_llvm); 5300 // and for "clang -w foo.o -o foo". Other warning options are already 5301 // handled somewhere else. 5302 Args.ClaimAllArgs(options::OPT_w); 5303 5304 if ((!Args.hasArg(options::OPT_nostdlib)) && 5305 (!Args.hasArg(options::OPT_shared))) { 5306 CmdArgs.push_back("-e"); 5307 CmdArgs.push_back("__start"); 5308 } 5309 5310 if (Args.hasArg(options::OPT_static)) { 5311 CmdArgs.push_back("-Bstatic"); 5312 } else { 5313 if (Args.hasArg(options::OPT_rdynamic)) 5314 CmdArgs.push_back("-export-dynamic"); 5315 CmdArgs.push_back("--eh-frame-hdr"); 5316 CmdArgs.push_back("-Bdynamic"); 5317 if (Args.hasArg(options::OPT_shared)) { 5318 CmdArgs.push_back("-shared"); 5319 } else { 5320 CmdArgs.push_back("-dynamic-linker"); 5321 CmdArgs.push_back("/usr/libexec/ld.so"); 5322 } 5323 } 5324 5325 if (Args.hasArg(options::OPT_nopie)) 5326 CmdArgs.push_back("-nopie"); 5327 5328 if (Output.isFilename()) { 5329 CmdArgs.push_back("-o"); 5330 CmdArgs.push_back(Output.getFilename()); 5331 } else { 5332 assert(Output.isNothing() && "Invalid output."); 5333 } 5334 5335 if (!Args.hasArg(options::OPT_nostdlib) && 5336 !Args.hasArg(options::OPT_nostartfiles)) { 5337 if (!Args.hasArg(options::OPT_shared)) { 5338 if (Args.hasArg(options::OPT_pg)) 5339 CmdArgs.push_back(Args.MakeArgString( 5340 getToolChain().GetFilePath("gcrt0.o"))); 5341 else 5342 CmdArgs.push_back(Args.MakeArgString( 5343 getToolChain().GetFilePath("crt0.o"))); 5344 CmdArgs.push_back(Args.MakeArgString( 5345 getToolChain().GetFilePath("crtbegin.o"))); 5346 } else { 5347 CmdArgs.push_back(Args.MakeArgString( 5348 getToolChain().GetFilePath("crtbeginS.o"))); 5349 } 5350 } 5351 5352 std::string Triple = getToolChain().getTripleString(); 5353 if (Triple.substr(0, 6) == "x86_64") 5354 Triple.replace(0, 6, "amd64"); 5355 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple + 5356 "/4.2.1")); 5357 5358 Args.AddAllArgs(CmdArgs, options::OPT_L); 5359 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5360 Args.AddAllArgs(CmdArgs, options::OPT_e); 5361 Args.AddAllArgs(CmdArgs, options::OPT_s); 5362 Args.AddAllArgs(CmdArgs, options::OPT_t); 5363 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 5364 Args.AddAllArgs(CmdArgs, options::OPT_r); 5365 5366 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5367 5368 if (!Args.hasArg(options::OPT_nostdlib) && 5369 !Args.hasArg(options::OPT_nodefaultlibs)) { 5370 if (D.CCCIsCXX()) { 5371 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5372 if (Args.hasArg(options::OPT_pg)) 5373 CmdArgs.push_back("-lm_p"); 5374 else 5375 CmdArgs.push_back("-lm"); 5376 } 5377 5378 // FIXME: For some reason GCC passes -lgcc before adding 5379 // the default system libraries. Just mimic this for now. 5380 CmdArgs.push_back("-lgcc"); 5381 5382 if (Args.hasArg(options::OPT_pthread)) { 5383 if (!Args.hasArg(options::OPT_shared) && 5384 Args.hasArg(options::OPT_pg)) 5385 CmdArgs.push_back("-lpthread_p"); 5386 else 5387 CmdArgs.push_back("-lpthread"); 5388 } 5389 5390 if (!Args.hasArg(options::OPT_shared)) { 5391 if (Args.hasArg(options::OPT_pg)) 5392 CmdArgs.push_back("-lc_p"); 5393 else 5394 CmdArgs.push_back("-lc"); 5395 } 5396 5397 CmdArgs.push_back("-lgcc"); 5398 } 5399 5400 if (!Args.hasArg(options::OPT_nostdlib) && 5401 !Args.hasArg(options::OPT_nostartfiles)) { 5402 if (!Args.hasArg(options::OPT_shared)) 5403 CmdArgs.push_back(Args.MakeArgString( 5404 getToolChain().GetFilePath("crtend.o"))); 5405 else 5406 CmdArgs.push_back(Args.MakeArgString( 5407 getToolChain().GetFilePath("crtendS.o"))); 5408 } 5409 5410 const char *Exec = 5411 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5412 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5413} 5414 5415void bitrig::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5416 const InputInfo &Output, 5417 const InputInfoList &Inputs, 5418 const ArgList &Args, 5419 const char *LinkingOutput) const { 5420 ArgStringList CmdArgs; 5421 5422 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5423 options::OPT_Xassembler); 5424 5425 CmdArgs.push_back("-o"); 5426 CmdArgs.push_back(Output.getFilename()); 5427 5428 for (InputInfoList::const_iterator 5429 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5430 const InputInfo &II = *it; 5431 CmdArgs.push_back(II.getFilename()); 5432 } 5433 5434 const char *Exec = 5435 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5436 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5437} 5438 5439void bitrig::Link::ConstructJob(Compilation &C, const JobAction &JA, 5440 const InputInfo &Output, 5441 const InputInfoList &Inputs, 5442 const ArgList &Args, 5443 const char *LinkingOutput) const { 5444 const Driver &D = getToolChain().getDriver(); 5445 ArgStringList CmdArgs; 5446 5447 if ((!Args.hasArg(options::OPT_nostdlib)) && 5448 (!Args.hasArg(options::OPT_shared))) { 5449 CmdArgs.push_back("-e"); 5450 CmdArgs.push_back("__start"); 5451 } 5452 5453 if (Args.hasArg(options::OPT_static)) { 5454 CmdArgs.push_back("-Bstatic"); 5455 } else { 5456 if (Args.hasArg(options::OPT_rdynamic)) 5457 CmdArgs.push_back("-export-dynamic"); 5458 CmdArgs.push_back("--eh-frame-hdr"); 5459 CmdArgs.push_back("-Bdynamic"); 5460 if (Args.hasArg(options::OPT_shared)) { 5461 CmdArgs.push_back("-shared"); 5462 } else { 5463 CmdArgs.push_back("-dynamic-linker"); 5464 CmdArgs.push_back("/usr/libexec/ld.so"); 5465 } 5466 } 5467 5468 if (Output.isFilename()) { 5469 CmdArgs.push_back("-o"); 5470 CmdArgs.push_back(Output.getFilename()); 5471 } else { 5472 assert(Output.isNothing() && "Invalid output."); 5473 } 5474 5475 if (!Args.hasArg(options::OPT_nostdlib) && 5476 !Args.hasArg(options::OPT_nostartfiles)) { 5477 if (!Args.hasArg(options::OPT_shared)) { 5478 if (Args.hasArg(options::OPT_pg)) 5479 CmdArgs.push_back(Args.MakeArgString( 5480 getToolChain().GetFilePath("gcrt0.o"))); 5481 else 5482 CmdArgs.push_back(Args.MakeArgString( 5483 getToolChain().GetFilePath("crt0.o"))); 5484 CmdArgs.push_back(Args.MakeArgString( 5485 getToolChain().GetFilePath("crtbegin.o"))); 5486 } else { 5487 CmdArgs.push_back(Args.MakeArgString( 5488 getToolChain().GetFilePath("crtbeginS.o"))); 5489 } 5490 } 5491 5492 Args.AddAllArgs(CmdArgs, options::OPT_L); 5493 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5494 Args.AddAllArgs(CmdArgs, options::OPT_e); 5495 5496 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5497 5498 if (!Args.hasArg(options::OPT_nostdlib) && 5499 !Args.hasArg(options::OPT_nodefaultlibs)) { 5500 if (D.CCCIsCXX()) { 5501 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5502 if (Args.hasArg(options::OPT_pg)) 5503 CmdArgs.push_back("-lm_p"); 5504 else 5505 CmdArgs.push_back("-lm"); 5506 } 5507 5508 if (Args.hasArg(options::OPT_pthread)) { 5509 if (!Args.hasArg(options::OPT_shared) && 5510 Args.hasArg(options::OPT_pg)) 5511 CmdArgs.push_back("-lpthread_p"); 5512 else 5513 CmdArgs.push_back("-lpthread"); 5514 } 5515 5516 if (!Args.hasArg(options::OPT_shared)) { 5517 if (Args.hasArg(options::OPT_pg)) 5518 CmdArgs.push_back("-lc_p"); 5519 else 5520 CmdArgs.push_back("-lc"); 5521 } 5522 5523 StringRef MyArch; 5524 switch (getToolChain().getTriple().getArch()) { 5525 case llvm::Triple::arm: 5526 MyArch = "arm"; 5527 break; 5528 case llvm::Triple::x86: 5529 MyArch = "i386"; 5530 break; 5531 case llvm::Triple::x86_64: 5532 MyArch = "amd64"; 5533 break; 5534 default: 5535 llvm_unreachable("Unsupported architecture"); 5536 } 5537 CmdArgs.push_back(Args.MakeArgString("-lclang_rt." + MyArch)); 5538 } 5539 5540 if (!Args.hasArg(options::OPT_nostdlib) && 5541 !Args.hasArg(options::OPT_nostartfiles)) { 5542 if (!Args.hasArg(options::OPT_shared)) 5543 CmdArgs.push_back(Args.MakeArgString( 5544 getToolChain().GetFilePath("crtend.o"))); 5545 else 5546 CmdArgs.push_back(Args.MakeArgString( 5547 getToolChain().GetFilePath("crtendS.o"))); 5548 } 5549 5550 const char *Exec = 5551 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5552 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5553} 5554 5555void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5556 const InputInfo &Output, 5557 const InputInfoList &Inputs, 5558 const ArgList &Args, 5559 const char *LinkingOutput) const { 5560 ArgStringList CmdArgs; 5561 5562 // When building 32-bit code on FreeBSD/amd64, we have to explicitly 5563 // instruct as in the base system to assemble 32-bit code. 5564 if (getToolChain().getArch() == llvm::Triple::x86) 5565 CmdArgs.push_back("--32"); 5566 else if (getToolChain().getArch() == llvm::Triple::ppc) 5567 CmdArgs.push_back("-a32"); 5568 else if (getToolChain().getArch() == llvm::Triple::mips || 5569 getToolChain().getArch() == llvm::Triple::mipsel || 5570 getToolChain().getArch() == llvm::Triple::mips64 || 5571 getToolChain().getArch() == llvm::Triple::mips64el) { 5572 StringRef CPUName; 5573 StringRef ABIName; 5574 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName); 5575 5576 CmdArgs.push_back("-march"); 5577 CmdArgs.push_back(CPUName.data()); 5578 5579 CmdArgs.push_back("-mabi"); 5580 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data()); 5581 5582 if (getToolChain().getArch() == llvm::Triple::mips || 5583 getToolChain().getArch() == llvm::Triple::mips64) 5584 CmdArgs.push_back("-EB"); 5585 else 5586 CmdArgs.push_back("-EL"); 5587 5588 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, 5589 options::OPT_fpic, options::OPT_fno_pic, 5590 options::OPT_fPIE, options::OPT_fno_PIE, 5591 options::OPT_fpie, options::OPT_fno_pie); 5592 if (LastPICArg && 5593 (LastPICArg->getOption().matches(options::OPT_fPIC) || 5594 LastPICArg->getOption().matches(options::OPT_fpic) || 5595 LastPICArg->getOption().matches(options::OPT_fPIE) || 5596 LastPICArg->getOption().matches(options::OPT_fpie))) { 5597 CmdArgs.push_back("-KPIC"); 5598 } 5599 } else if (getToolChain().getArch() == llvm::Triple::arm || 5600 getToolChain().getArch() == llvm::Triple::thumb) { 5601 CmdArgs.push_back("-mfpu=softvfp"); 5602 switch(getToolChain().getTriple().getEnvironment()) { 5603 case llvm::Triple::GNUEABI: 5604 case llvm::Triple::EABI: 5605 CmdArgs.push_back("-meabi=5"); 5606 break; 5607 5608 default: 5609 CmdArgs.push_back("-matpcs"); 5610 } 5611 } 5612 5613 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5614 options::OPT_Xassembler); 5615 5616 CmdArgs.push_back("-o"); 5617 CmdArgs.push_back(Output.getFilename()); 5618 5619 for (InputInfoList::const_iterator 5620 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5621 const InputInfo &II = *it; 5622 CmdArgs.push_back(II.getFilename()); 5623 } 5624 5625 const char *Exec = 5626 Args.MakeArgString(getToolChain().GetProgramPath("as")); 5627 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5628} 5629 5630void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA, 5631 const InputInfo &Output, 5632 const InputInfoList &Inputs, 5633 const ArgList &Args, 5634 const char *LinkingOutput) const { 5635 const toolchains::FreeBSD& ToolChain = 5636 static_cast<const toolchains::FreeBSD&>(getToolChain()); 5637 const Driver &D = ToolChain.getDriver(); 5638 ArgStringList CmdArgs; 5639 5640 // Silence warning for "clang -g foo.o -o foo" 5641 Args.ClaimAllArgs(options::OPT_g_Group); 5642 // and "clang -emit-llvm foo.o -o foo" 5643 Args.ClaimAllArgs(options::OPT_emit_llvm); 5644 // and for "clang -w foo.o -o foo". Other warning options are already 5645 // handled somewhere else. 5646 Args.ClaimAllArgs(options::OPT_w); 5647 5648 if (!D.SysRoot.empty()) 5649 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 5650 5651 if (Args.hasArg(options::OPT_pie)) 5652 CmdArgs.push_back("-pie"); 5653 5654 if (Args.hasArg(options::OPT_static)) { 5655 CmdArgs.push_back("-Bstatic"); 5656 } else { 5657 if (Args.hasArg(options::OPT_rdynamic)) 5658 CmdArgs.push_back("-export-dynamic"); 5659 CmdArgs.push_back("--eh-frame-hdr"); 5660 if (Args.hasArg(options::OPT_shared)) { 5661 CmdArgs.push_back("-Bshareable"); 5662 } else { 5663 CmdArgs.push_back("-dynamic-linker"); 5664 CmdArgs.push_back("/libexec/ld-elf.so.1"); 5665 } 5666 if (ToolChain.getTriple().getOSMajorVersion() >= 9) { 5667 llvm::Triple::ArchType Arch = ToolChain.getArch(); 5668 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc || 5669 Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) { 5670 CmdArgs.push_back("--hash-style=both"); 5671 } 5672 } 5673 CmdArgs.push_back("--enable-new-dtags"); 5674 } 5675 5676 // When building 32-bit code on FreeBSD/amd64, we have to explicitly 5677 // instruct ld in the base system to link 32-bit code. 5678 if (ToolChain.getArch() == llvm::Triple::x86) { 5679 CmdArgs.push_back("-m"); 5680 CmdArgs.push_back("elf_i386_fbsd"); 5681 } 5682 5683 if (ToolChain.getArch() == llvm::Triple::ppc) { 5684 CmdArgs.push_back("-m"); 5685 CmdArgs.push_back("elf32ppc_fbsd"); 5686 } 5687 5688 if (Output.isFilename()) { 5689 CmdArgs.push_back("-o"); 5690 CmdArgs.push_back(Output.getFilename()); 5691 } else { 5692 assert(Output.isNothing() && "Invalid output."); 5693 } 5694 5695 if (!Args.hasArg(options::OPT_nostdlib) && 5696 !Args.hasArg(options::OPT_nostartfiles)) { 5697 const char *crt1 = NULL; 5698 if (!Args.hasArg(options::OPT_shared)) { 5699 if (Args.hasArg(options::OPT_pg)) 5700 crt1 = "gcrt1.o"; 5701 else if (Args.hasArg(options::OPT_pie)) 5702 crt1 = "Scrt1.o"; 5703 else 5704 crt1 = "crt1.o"; 5705 } 5706 if (crt1) 5707 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1))); 5708 5709 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 5710 5711 const char *crtbegin = NULL; 5712 if (Args.hasArg(options::OPT_static)) 5713 crtbegin = "crtbeginT.o"; 5714 else if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 5715 crtbegin = "crtbeginS.o"; 5716 else 5717 crtbegin = "crtbegin.o"; 5718 5719 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 5720 } 5721 5722 Args.AddAllArgs(CmdArgs, options::OPT_L); 5723 const ToolChain::path_list Paths = ToolChain.getFilePaths(); 5724 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end(); 5725 i != e; ++i) 5726 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i)); 5727 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5728 Args.AddAllArgs(CmdArgs, options::OPT_e); 5729 Args.AddAllArgs(CmdArgs, options::OPT_s); 5730 Args.AddAllArgs(CmdArgs, options::OPT_t); 5731 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 5732 Args.AddAllArgs(CmdArgs, options::OPT_r); 5733 5734 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); 5735 5736 if (!Args.hasArg(options::OPT_nostdlib) && 5737 !Args.hasArg(options::OPT_nodefaultlibs)) { 5738 if (D.CCCIsCXX()) { 5739 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 5740 if (Args.hasArg(options::OPT_pg)) 5741 CmdArgs.push_back("-lm_p"); 5742 else 5743 CmdArgs.push_back("-lm"); 5744 } 5745 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding 5746 // the default system libraries. Just mimic this for now. 5747 if (Args.hasArg(options::OPT_pg)) 5748 CmdArgs.push_back("-lgcc_p"); 5749 else 5750 CmdArgs.push_back("-lgcc"); 5751 if (Args.hasArg(options::OPT_static)) { 5752 CmdArgs.push_back("-lgcc_eh"); 5753 } else if (Args.hasArg(options::OPT_pg)) { 5754 CmdArgs.push_back("-lgcc_eh_p"); 5755 } else { 5756 CmdArgs.push_back("--as-needed"); 5757 CmdArgs.push_back("-lgcc_s"); 5758 CmdArgs.push_back("--no-as-needed"); 5759 } 5760 5761 if (Args.hasArg(options::OPT_pthread)) { 5762 if (Args.hasArg(options::OPT_pg)) 5763 CmdArgs.push_back("-lpthread_p"); 5764 else 5765 CmdArgs.push_back("-lpthread"); 5766 } 5767 5768 if (Args.hasArg(options::OPT_pg)) { 5769 if (Args.hasArg(options::OPT_shared)) 5770 CmdArgs.push_back("-lc"); 5771 else 5772 CmdArgs.push_back("-lc_p"); 5773 CmdArgs.push_back("-lgcc_p"); 5774 } else { 5775 CmdArgs.push_back("-lc"); 5776 CmdArgs.push_back("-lgcc"); 5777 } 5778 5779 if (Args.hasArg(options::OPT_static)) { 5780 CmdArgs.push_back("-lgcc_eh"); 5781 } else if (Args.hasArg(options::OPT_pg)) { 5782 CmdArgs.push_back("-lgcc_eh_p"); 5783 } else { 5784 CmdArgs.push_back("--as-needed"); 5785 CmdArgs.push_back("-lgcc_s"); 5786 CmdArgs.push_back("--no-as-needed"); 5787 } 5788 } 5789 5790 if (!Args.hasArg(options::OPT_nostdlib) && 5791 !Args.hasArg(options::OPT_nostartfiles)) { 5792 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 5793 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o"))); 5794 else 5795 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); 5796 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 5797 } 5798 5799 addProfileRT(ToolChain, Args, CmdArgs, ToolChain.getTriple()); 5800 5801 const char *Exec = 5802 Args.MakeArgString(ToolChain.GetProgramPath("ld")); 5803 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5804} 5805 5806void netbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5807 const InputInfo &Output, 5808 const InputInfoList &Inputs, 5809 const ArgList &Args, 5810 const char *LinkingOutput) const { 5811 ArgStringList CmdArgs; 5812 5813 // When building 32-bit code on NetBSD/amd64, we have to explicitly 5814 // instruct as in the base system to assemble 32-bit code. 5815 if (getToolChain().getArch() == llvm::Triple::x86) 5816 CmdArgs.push_back("--32"); 5817 5818 // Set byte order explicitly 5819 if (getToolChain().getArch() == llvm::Triple::mips) 5820 CmdArgs.push_back("-EB"); 5821 else if (getToolChain().getArch() == llvm::Triple::mipsel) 5822 CmdArgs.push_back("-EL"); 5823 5824 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 5825 options::OPT_Xassembler); 5826 5827 CmdArgs.push_back("-o"); 5828 CmdArgs.push_back(Output.getFilename()); 5829 5830 for (InputInfoList::const_iterator 5831 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 5832 const InputInfo &II = *it; 5833 CmdArgs.push_back(II.getFilename()); 5834 } 5835 5836 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as"))); 5837 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5838} 5839 5840void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA, 5841 const InputInfo &Output, 5842 const InputInfoList &Inputs, 5843 const ArgList &Args, 5844 const char *LinkingOutput) const { 5845 const Driver &D = getToolChain().getDriver(); 5846 ArgStringList CmdArgs; 5847 5848 if (!D.SysRoot.empty()) 5849 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 5850 5851 if (Args.hasArg(options::OPT_static)) { 5852 CmdArgs.push_back("-Bstatic"); 5853 } else { 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"); 5859 } else { 5860 CmdArgs.push_back("-dynamic-linker"); 5861 CmdArgs.push_back("/libexec/ld.elf_so"); 5862 } 5863 } 5864 5865 // When building 32-bit code on NetBSD/amd64, we have to explicitly 5866 // instruct ld in the base system to link 32-bit code. 5867 if (getToolChain().getArch() == llvm::Triple::x86) { 5868 CmdArgs.push_back("-m"); 5869 CmdArgs.push_back("elf_i386"); 5870 } 5871 5872 if (Output.isFilename()) { 5873 CmdArgs.push_back("-o"); 5874 CmdArgs.push_back(Output.getFilename()); 5875 } else { 5876 assert(Output.isNothing() && "Invalid output."); 5877 } 5878 5879 if (!Args.hasArg(options::OPT_nostdlib) && 5880 !Args.hasArg(options::OPT_nostartfiles)) { 5881 if (!Args.hasArg(options::OPT_shared)) { 5882 CmdArgs.push_back(Args.MakeArgString( 5883 getToolChain().GetFilePath("crt0.o"))); 5884 CmdArgs.push_back(Args.MakeArgString( 5885 getToolChain().GetFilePath("crti.o"))); 5886 CmdArgs.push_back(Args.MakeArgString( 5887 getToolChain().GetFilePath("crtbegin.o"))); 5888 } else { 5889 CmdArgs.push_back(Args.MakeArgString( 5890 getToolChain().GetFilePath("crti.o"))); 5891 CmdArgs.push_back(Args.MakeArgString( 5892 getToolChain().GetFilePath("crtbeginS.o"))); 5893 } 5894 } 5895 5896 Args.AddAllArgs(CmdArgs, options::OPT_L); 5897 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 5898 Args.AddAllArgs(CmdArgs, options::OPT_e); 5899 Args.AddAllArgs(CmdArgs, options::OPT_s); 5900 Args.AddAllArgs(CmdArgs, options::OPT_t); 5901 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 5902 Args.AddAllArgs(CmdArgs, options::OPT_r); 5903 5904 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 5905 5906 unsigned Major, Minor, Micro; 5907 getToolChain().getTriple().getOSVersion(Major, Minor, Micro); 5908 bool useLibgcc = true; 5909 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 23) || Major == 0) { 5910 if (getToolChain().getArch() == llvm::Triple::x86 || 5911 getToolChain().getArch() == llvm::Triple::x86_64) 5912 useLibgcc = false; 5913 } 5914 5915 if (!Args.hasArg(options::OPT_nostdlib) && 5916 !Args.hasArg(options::OPT_nodefaultlibs)) { 5917 if (D.CCCIsCXX()) { 5918 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 5919 CmdArgs.push_back("-lm"); 5920 } 5921 if (Args.hasArg(options::OPT_pthread)) 5922 CmdArgs.push_back("-lpthread"); 5923 CmdArgs.push_back("-lc"); 5924 5925 if (useLibgcc) { 5926 if (Args.hasArg(options::OPT_static)) { 5927 // libgcc_eh depends on libc, so resolve as much as possible, 5928 // pull in any new requirements from libc and then get the rest 5929 // of libgcc. 5930 CmdArgs.push_back("-lgcc_eh"); 5931 CmdArgs.push_back("-lc"); 5932 CmdArgs.push_back("-lgcc"); 5933 } else { 5934 CmdArgs.push_back("-lgcc"); 5935 CmdArgs.push_back("--as-needed"); 5936 CmdArgs.push_back("-lgcc_s"); 5937 CmdArgs.push_back("--no-as-needed"); 5938 } 5939 } 5940 } 5941 5942 if (!Args.hasArg(options::OPT_nostdlib) && 5943 !Args.hasArg(options::OPT_nostartfiles)) { 5944 if (!Args.hasArg(options::OPT_shared)) 5945 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath( 5946 "crtend.o"))); 5947 else 5948 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath( 5949 "crtendS.o"))); 5950 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath( 5951 "crtn.o"))); 5952 } 5953 5954 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 5955 5956 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld")); 5957 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 5958} 5959 5960void gnutools::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 5961 const InputInfo &Output, 5962 const InputInfoList &Inputs, 5963 const ArgList &Args, 5964 const char *LinkingOutput) const { 5965 ArgStringList CmdArgs; 5966 5967 // Add --32/--64 to make sure we get the format we want. 5968 // This is incomplete 5969 if (getToolChain().getArch() == llvm::Triple::x86) { 5970 CmdArgs.push_back("--32"); 5971 } else if (getToolChain().getArch() == llvm::Triple::x86_64) { 5972 CmdArgs.push_back("--64"); 5973 } else if (getToolChain().getArch() == llvm::Triple::ppc) { 5974 CmdArgs.push_back("-a32"); 5975 CmdArgs.push_back("-mppc"); 5976 CmdArgs.push_back("-many"); 5977 } else if (getToolChain().getArch() == llvm::Triple::ppc64) { 5978 CmdArgs.push_back("-a64"); 5979 CmdArgs.push_back("-mppc64"); 5980 CmdArgs.push_back("-many"); 5981 } else if (getToolChain().getArch() == llvm::Triple::ppc64le) { 5982 CmdArgs.push_back("-a64"); 5983 CmdArgs.push_back("-mppc64le"); 5984 CmdArgs.push_back("-many"); 5985 } else if (getToolChain().getArch() == llvm::Triple::arm) { 5986 StringRef MArch = getToolChain().getArchName(); 5987 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a") 5988 CmdArgs.push_back("-mfpu=neon"); 5989 5990 StringRef ARMFloatABI = getARMFloatABI(getToolChain().getDriver(), Args, 5991 getToolChain().getTriple()); 5992 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=" + ARMFloatABI)); 5993 5994 Args.AddLastArg(CmdArgs, options::OPT_march_EQ); 5995 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ); 5996 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ); 5997 } else if (getToolChain().getArch() == llvm::Triple::mips || 5998 getToolChain().getArch() == llvm::Triple::mipsel || 5999 getToolChain().getArch() == llvm::Triple::mips64 || 6000 getToolChain().getArch() == llvm::Triple::mips64el) { 6001 StringRef CPUName; 6002 StringRef ABIName; 6003 getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName); 6004 6005 CmdArgs.push_back("-march"); 6006 CmdArgs.push_back(CPUName.data()); 6007 6008 CmdArgs.push_back("-mabi"); 6009 CmdArgs.push_back(getGnuCompatibleMipsABIName(ABIName).data()); 6010 6011 if (getToolChain().getArch() == llvm::Triple::mips || 6012 getToolChain().getArch() == llvm::Triple::mips64) 6013 CmdArgs.push_back("-EB"); 6014 else 6015 CmdArgs.push_back("-EL"); 6016 6017 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) { 6018 if (StringRef(A->getValue()) == "2008") 6019 CmdArgs.push_back(Args.MakeArgString("-mnan=2008")); 6020 } 6021 6022 Args.AddLastArg(CmdArgs, options::OPT_mips16, options::OPT_mno_mips16); 6023 Args.AddLastArg(CmdArgs, options::OPT_mmicromips, 6024 options::OPT_mno_micromips); 6025 Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp); 6026 Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2); 6027 6028 Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, 6029 options::OPT_fpic, options::OPT_fno_pic, 6030 options::OPT_fPIE, options::OPT_fno_PIE, 6031 options::OPT_fpie, options::OPT_fno_pie); 6032 if (LastPICArg && 6033 (LastPICArg->getOption().matches(options::OPT_fPIC) || 6034 LastPICArg->getOption().matches(options::OPT_fpic) || 6035 LastPICArg->getOption().matches(options::OPT_fPIE) || 6036 LastPICArg->getOption().matches(options::OPT_fpie))) { 6037 CmdArgs.push_back("-KPIC"); 6038 } 6039 } else if (getToolChain().getArch() == llvm::Triple::systemz) { 6040 // Always pass an -march option, since our default of z10 is later 6041 // than the GNU assembler's default. 6042 StringRef CPUName = getSystemZTargetCPU(Args); 6043 CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName)); 6044 } 6045 6046 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 6047 options::OPT_Xassembler); 6048 6049 CmdArgs.push_back("-o"); 6050 CmdArgs.push_back(Output.getFilename()); 6051 6052 for (InputInfoList::const_iterator 6053 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6054 const InputInfo &II = *it; 6055 CmdArgs.push_back(II.getFilename()); 6056 } 6057 6058 const char *Exec = 6059 Args.MakeArgString(getToolChain().GetProgramPath("as")); 6060 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6061 6062 // Handle the debug info splitting at object creation time if we're 6063 // creating an object. 6064 // TODO: Currently only works on linux with newer objcopy. 6065 if (Args.hasArg(options::OPT_gsplit_dwarf) && 6066 getToolChain().getTriple().isOSLinux()) 6067 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, 6068 SplitDebugName(Args, Inputs)); 6069} 6070 6071static void AddLibgcc(llvm::Triple Triple, const Driver &D, 6072 ArgStringList &CmdArgs, const ArgList &Args) { 6073 bool isAndroid = Triple.getEnvironment() == llvm::Triple::Android; 6074 bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) || 6075 Args.hasArg(options::OPT_static); 6076 if (!D.CCCIsCXX()) 6077 CmdArgs.push_back("-lgcc"); 6078 6079 if (StaticLibgcc || isAndroid) { 6080 if (D.CCCIsCXX()) 6081 CmdArgs.push_back("-lgcc"); 6082 } else { 6083 if (!D.CCCIsCXX()) 6084 CmdArgs.push_back("--as-needed"); 6085 CmdArgs.push_back("-lgcc_s"); 6086 if (!D.CCCIsCXX()) 6087 CmdArgs.push_back("--no-as-needed"); 6088 } 6089 6090 if (StaticLibgcc && !isAndroid) 6091 CmdArgs.push_back("-lgcc_eh"); 6092 else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX()) 6093 CmdArgs.push_back("-lgcc"); 6094 6095 // According to Android ABI, we have to link with libdl if we are 6096 // linking with non-static libgcc. 6097 // 6098 // NOTE: This fixes a link error on Android MIPS as well. The non-static 6099 // libgcc for MIPS relies on _Unwind_Find_FDE and dl_iterate_phdr from libdl. 6100 if (isAndroid && !StaticLibgcc) 6101 CmdArgs.push_back("-ldl"); 6102} 6103 6104static bool hasMipsN32ABIArg(const ArgList &Args) { 6105 Arg *A = Args.getLastArg(options::OPT_mabi_EQ); 6106 return A && (A->getValue() == StringRef("n32")); 6107} 6108 6109static StringRef getLinuxDynamicLinker(const ArgList &Args, 6110 const toolchains::Linux &ToolChain) { 6111 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android) 6112 return "/system/bin/linker"; 6113 else if (ToolChain.getArch() == llvm::Triple::x86) 6114 return "/lib/ld-linux.so.2"; 6115 else if (ToolChain.getArch() == llvm::Triple::aarch64) 6116 return "/lib/ld-linux-aarch64.so.1"; 6117 else if (ToolChain.getArch() == llvm::Triple::arm || 6118 ToolChain.getArch() == llvm::Triple::thumb) { 6119 if (ToolChain.getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 6120 return "/lib/ld-linux-armhf.so.3"; 6121 else 6122 return "/lib/ld-linux.so.3"; 6123 } else if (ToolChain.getArch() == llvm::Triple::mips || 6124 ToolChain.getArch() == llvm::Triple::mipsel) 6125 return "/lib/ld.so.1"; 6126 else if (ToolChain.getArch() == llvm::Triple::mips64 || 6127 ToolChain.getArch() == llvm::Triple::mips64el) { 6128 if (hasMipsN32ABIArg(Args)) 6129 return "/lib32/ld.so.1"; 6130 else 6131 return "/lib64/ld.so.1"; 6132 } else if (ToolChain.getArch() == llvm::Triple::ppc) 6133 return "/lib/ld.so.1"; 6134 else if (ToolChain.getArch() == llvm::Triple::ppc64 || 6135 ToolChain.getArch() == llvm::Triple::ppc64le || 6136 ToolChain.getArch() == llvm::Triple::systemz) 6137 return "/lib64/ld64.so.1"; 6138 else 6139 return "/lib64/ld-linux-x86-64.so.2"; 6140} 6141 6142void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA, 6143 const InputInfo &Output, 6144 const InputInfoList &Inputs, 6145 const ArgList &Args, 6146 const char *LinkingOutput) const { 6147 const toolchains::Linux& ToolChain = 6148 static_cast<const toolchains::Linux&>(getToolChain()); 6149 const Driver &D = ToolChain.getDriver(); 6150 const bool isAndroid = 6151 ToolChain.getTriple().getEnvironment() == llvm::Triple::Android; 6152 const SanitizerArgs &Sanitize = D.getOrParseSanitizerArgs(Args); 6153 const bool IsPIE = 6154 !Args.hasArg(options::OPT_shared) && 6155 (Args.hasArg(options::OPT_pie) || Sanitize.hasZeroBaseShadow(ToolChain)); 6156 6157 ArgStringList CmdArgs; 6158 6159 // Silence warning for "clang -g foo.o -o foo" 6160 Args.ClaimAllArgs(options::OPT_g_Group); 6161 // and "clang -emit-llvm foo.o -o foo" 6162 Args.ClaimAllArgs(options::OPT_emit_llvm); 6163 // and for "clang -w foo.o -o foo". Other warning options are already 6164 // handled somewhere else. 6165 Args.ClaimAllArgs(options::OPT_w); 6166 6167 if (!D.SysRoot.empty()) 6168 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 6169 6170 if (IsPIE) 6171 CmdArgs.push_back("-pie"); 6172 6173 if (Args.hasArg(options::OPT_rdynamic)) 6174 CmdArgs.push_back("-export-dynamic"); 6175 6176 if (Args.hasArg(options::OPT_s)) 6177 CmdArgs.push_back("-s"); 6178 6179 for (std::vector<std::string>::const_iterator i = ToolChain.ExtraOpts.begin(), 6180 e = ToolChain.ExtraOpts.end(); 6181 i != e; ++i) 6182 CmdArgs.push_back(i->c_str()); 6183 6184 if (!Args.hasArg(options::OPT_static)) { 6185 CmdArgs.push_back("--eh-frame-hdr"); 6186 } 6187 6188 CmdArgs.push_back("-m"); 6189 if (ToolChain.getArch() == llvm::Triple::x86) 6190 CmdArgs.push_back("elf_i386"); 6191 else if (ToolChain.getArch() == llvm::Triple::aarch64) 6192 CmdArgs.push_back("aarch64linux"); 6193 else if (ToolChain.getArch() == llvm::Triple::arm 6194 || ToolChain.getArch() == llvm::Triple::thumb) 6195 CmdArgs.push_back("armelf_linux_eabi"); 6196 else if (ToolChain.getArch() == llvm::Triple::ppc) 6197 CmdArgs.push_back("elf32ppclinux"); 6198 else if (ToolChain.getArch() == llvm::Triple::ppc64) 6199 CmdArgs.push_back("elf64ppc"); 6200 else if (ToolChain.getArch() == llvm::Triple::mips) 6201 CmdArgs.push_back("elf32btsmip"); 6202 else if (ToolChain.getArch() == llvm::Triple::mipsel) 6203 CmdArgs.push_back("elf32ltsmip"); 6204 else if (ToolChain.getArch() == llvm::Triple::mips64) { 6205 if (hasMipsN32ABIArg(Args)) 6206 CmdArgs.push_back("elf32btsmipn32"); 6207 else 6208 CmdArgs.push_back("elf64btsmip"); 6209 } 6210 else if (ToolChain.getArch() == llvm::Triple::mips64el) { 6211 if (hasMipsN32ABIArg(Args)) 6212 CmdArgs.push_back("elf32ltsmipn32"); 6213 else 6214 CmdArgs.push_back("elf64ltsmip"); 6215 } 6216 else if (ToolChain.getArch() == llvm::Triple::systemz) 6217 CmdArgs.push_back("elf64_s390"); 6218 else 6219 CmdArgs.push_back("elf_x86_64"); 6220 6221 if (Args.hasArg(options::OPT_static)) { 6222 if (ToolChain.getArch() == llvm::Triple::arm 6223 || ToolChain.getArch() == llvm::Triple::thumb) 6224 CmdArgs.push_back("-Bstatic"); 6225 else 6226 CmdArgs.push_back("-static"); 6227 } else if (Args.hasArg(options::OPT_shared)) { 6228 CmdArgs.push_back("-shared"); 6229 if (isAndroid) { 6230 CmdArgs.push_back("-Bsymbolic"); 6231 } 6232 } 6233 6234 if (ToolChain.getArch() == llvm::Triple::arm || 6235 ToolChain.getArch() == llvm::Triple::thumb || 6236 (!Args.hasArg(options::OPT_static) && 6237 !Args.hasArg(options::OPT_shared))) { 6238 CmdArgs.push_back("-dynamic-linker"); 6239 CmdArgs.push_back(Args.MakeArgString( 6240 D.DyldPrefix + getLinuxDynamicLinker(Args, ToolChain))); 6241 } 6242 6243 CmdArgs.push_back("-o"); 6244 CmdArgs.push_back(Output.getFilename()); 6245 6246 if (!Args.hasArg(options::OPT_nostdlib) && 6247 !Args.hasArg(options::OPT_nostartfiles)) { 6248 if (!isAndroid) { 6249 const char *crt1 = NULL; 6250 if (!Args.hasArg(options::OPT_shared)){ 6251 if (Args.hasArg(options::OPT_pg)) 6252 crt1 = "gcrt1.o"; 6253 else if (IsPIE) 6254 crt1 = "Scrt1.o"; 6255 else 6256 crt1 = "crt1.o"; 6257 } 6258 if (crt1) 6259 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1))); 6260 6261 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 6262 } 6263 6264 const char *crtbegin; 6265 if (Args.hasArg(options::OPT_static)) 6266 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o"; 6267 else if (Args.hasArg(options::OPT_shared)) 6268 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o"; 6269 else if (IsPIE) 6270 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o"; 6271 else 6272 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o"; 6273 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 6274 6275 // Add crtfastmath.o if available and fast math is enabled. 6276 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs); 6277 } 6278 6279 Args.AddAllArgs(CmdArgs, options::OPT_L); 6280 6281 const ToolChain::path_list Paths = ToolChain.getFilePaths(); 6282 6283 for (ToolChain::path_list::const_iterator i = Paths.begin(), e = Paths.end(); 6284 i != e; ++i) 6285 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + *i)); 6286 6287 // Tell the linker to load the plugin. This has to come before AddLinkerInputs 6288 // as gold requires -plugin to come before any -plugin-opt that -Wl might 6289 // forward. 6290 if (D.IsUsingLTO(Args)) { 6291 CmdArgs.push_back("-plugin"); 6292 std::string Plugin = ToolChain.getDriver().Dir + "/../lib/LLVMgold.so"; 6293 CmdArgs.push_back(Args.MakeArgString(Plugin)); 6294 6295 // Try to pass driver level flags relevant to LTO code generation down to 6296 // the plugin. 6297 6298 // Handle flags for selecting CPU variants. 6299 std::string CPU = getCPUName(Args, ToolChain.getTriple()); 6300 if (!CPU.empty()) { 6301 CmdArgs.push_back( 6302 Args.MakeArgString(Twine("-plugin-opt=mcpu=") + 6303 CPU)); 6304 } 6305 } 6306 6307 6308 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) 6309 CmdArgs.push_back("--no-demangle"); 6310 6311 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); 6312 6313 // Call these before we add the C++ ABI library. 6314 if (Sanitize.needsUbsanRt()) 6315 addUbsanRTLinux(getToolChain(), Args, CmdArgs, D.CCCIsCXX(), 6316 Sanitize.needsAsanRt() || Sanitize.needsTsanRt() || 6317 Sanitize.needsMsanRt() || Sanitize.needsLsanRt()); 6318 if (Sanitize.needsAsanRt()) 6319 addAsanRTLinux(getToolChain(), Args, CmdArgs); 6320 if (Sanitize.needsTsanRt()) 6321 addTsanRTLinux(getToolChain(), Args, CmdArgs); 6322 if (Sanitize.needsMsanRt()) 6323 addMsanRTLinux(getToolChain(), Args, CmdArgs); 6324 if (Sanitize.needsLsanRt()) 6325 addLsanRTLinux(getToolChain(), Args, CmdArgs); 6326 if (Sanitize.needsDfsanRt()) 6327 addDfsanRTLinux(getToolChain(), Args, CmdArgs); 6328 6329 // The profile runtime also needs access to system libraries. 6330 addProfileRTLinux(getToolChain(), Args, CmdArgs); 6331 6332 if (D.CCCIsCXX() && 6333 !Args.hasArg(options::OPT_nostdlib) && 6334 !Args.hasArg(options::OPT_nodefaultlibs)) { 6335 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && 6336 !Args.hasArg(options::OPT_static); 6337 if (OnlyLibstdcxxStatic) 6338 CmdArgs.push_back("-Bstatic"); 6339 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 6340 if (OnlyLibstdcxxStatic) 6341 CmdArgs.push_back("-Bdynamic"); 6342 CmdArgs.push_back("-lm"); 6343 } 6344 6345 if (!Args.hasArg(options::OPT_nostdlib)) { 6346 if (!Args.hasArg(options::OPT_nodefaultlibs)) { 6347 if (Args.hasArg(options::OPT_static)) 6348 CmdArgs.push_back("--start-group"); 6349 6350 bool OpenMP = Args.hasArg(options::OPT_fopenmp); 6351 if (OpenMP) { 6352 CmdArgs.push_back("-lgomp"); 6353 6354 // FIXME: Exclude this for platforms whith libgomp that doesn't require 6355 // librt. Most modern Linux platfroms require it, but some may not. 6356 CmdArgs.push_back("-lrt"); 6357 } 6358 6359 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args); 6360 6361 if (Args.hasArg(options::OPT_pthread) || 6362 Args.hasArg(options::OPT_pthreads) || OpenMP) 6363 CmdArgs.push_back("-lpthread"); 6364 6365 CmdArgs.push_back("-lc"); 6366 6367 if (Args.hasArg(options::OPT_static)) 6368 CmdArgs.push_back("--end-group"); 6369 else 6370 AddLibgcc(ToolChain.getTriple(), D, CmdArgs, Args); 6371 } 6372 6373 if (!Args.hasArg(options::OPT_nostartfiles)) { 6374 const char *crtend; 6375 if (Args.hasArg(options::OPT_shared)) 6376 crtend = isAndroid ? "crtend_so.o" : "crtendS.o"; 6377 else if (IsPIE) 6378 crtend = isAndroid ? "crtend_android.o" : "crtendS.o"; 6379 else 6380 crtend = isAndroid ? "crtend_android.o" : "crtend.o"; 6381 6382 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); 6383 if (!isAndroid) 6384 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 6385 } 6386 } 6387 6388 C.addCommand(new Command(JA, *this, ToolChain.Linker.c_str(), CmdArgs)); 6389} 6390 6391void minix::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 6392 const InputInfo &Output, 6393 const InputInfoList &Inputs, 6394 const ArgList &Args, 6395 const char *LinkingOutput) const { 6396 ArgStringList CmdArgs; 6397 6398 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 6399 options::OPT_Xassembler); 6400 6401 CmdArgs.push_back("-o"); 6402 CmdArgs.push_back(Output.getFilename()); 6403 6404 for (InputInfoList::const_iterator 6405 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6406 const InputInfo &II = *it; 6407 CmdArgs.push_back(II.getFilename()); 6408 } 6409 6410 const char *Exec = 6411 Args.MakeArgString(getToolChain().GetProgramPath("as")); 6412 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6413} 6414 6415void minix::Link::ConstructJob(Compilation &C, const JobAction &JA, 6416 const InputInfo &Output, 6417 const InputInfoList &Inputs, 6418 const ArgList &Args, 6419 const char *LinkingOutput) const { 6420 const Driver &D = getToolChain().getDriver(); 6421 ArgStringList CmdArgs; 6422 6423 if (Output.isFilename()) { 6424 CmdArgs.push_back("-o"); 6425 CmdArgs.push_back(Output.getFilename()); 6426 } else { 6427 assert(Output.isNothing() && "Invalid output."); 6428 } 6429 6430 if (!Args.hasArg(options::OPT_nostdlib) && 6431 !Args.hasArg(options::OPT_nostartfiles)) { 6432 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crt1.o"))); 6433 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o"))); 6434 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o"))); 6435 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o"))); 6436 } 6437 6438 Args.AddAllArgs(CmdArgs, options::OPT_L); 6439 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 6440 Args.AddAllArgs(CmdArgs, options::OPT_e); 6441 6442 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 6443 6444 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 6445 6446 if (!Args.hasArg(options::OPT_nostdlib) && 6447 !Args.hasArg(options::OPT_nodefaultlibs)) { 6448 if (D.CCCIsCXX()) { 6449 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 6450 CmdArgs.push_back("-lm"); 6451 } 6452 } 6453 6454 if (!Args.hasArg(options::OPT_nostdlib) && 6455 !Args.hasArg(options::OPT_nostartfiles)) { 6456 if (Args.hasArg(options::OPT_pthread)) 6457 CmdArgs.push_back("-lpthread"); 6458 CmdArgs.push_back("-lc"); 6459 CmdArgs.push_back("-lCompilerRT-Generic"); 6460 CmdArgs.push_back("-L/usr/pkg/compiler-rt/lib"); 6461 CmdArgs.push_back( 6462 Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); 6463 } 6464 6465 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld")); 6466 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6467} 6468 6469/// DragonFly Tools 6470 6471// For now, DragonFly Assemble does just about the same as for 6472// FreeBSD, but this may change soon. 6473void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 6474 const InputInfo &Output, 6475 const InputInfoList &Inputs, 6476 const ArgList &Args, 6477 const char *LinkingOutput) const { 6478 ArgStringList CmdArgs; 6479 6480 // When building 32-bit code on DragonFly/pc64, we have to explicitly 6481 // instruct as in the base system to assemble 32-bit code. 6482 if (getToolChain().getArch() == llvm::Triple::x86) 6483 CmdArgs.push_back("--32"); 6484 6485 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 6486 options::OPT_Xassembler); 6487 6488 CmdArgs.push_back("-o"); 6489 CmdArgs.push_back(Output.getFilename()); 6490 6491 for (InputInfoList::const_iterator 6492 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6493 const InputInfo &II = *it; 6494 CmdArgs.push_back(II.getFilename()); 6495 } 6496 6497 const char *Exec = 6498 Args.MakeArgString(getToolChain().GetProgramPath("as")); 6499 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6500} 6501 6502void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA, 6503 const InputInfo &Output, 6504 const InputInfoList &Inputs, 6505 const ArgList &Args, 6506 const char *LinkingOutput) const { 6507 bool UseGCC47 = false; 6508 const Driver &D = getToolChain().getDriver(); 6509 ArgStringList CmdArgs; 6510 6511 if (llvm::sys::fs::exists("/usr/lib/gcc47", UseGCC47)) 6512 UseGCC47 = false; 6513 6514 if (!D.SysRoot.empty()) 6515 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 6516 6517 CmdArgs.push_back("--eh-frame-hdr"); 6518 if (Args.hasArg(options::OPT_static)) { 6519 CmdArgs.push_back("-Bstatic"); 6520 } else { 6521 if (Args.hasArg(options::OPT_rdynamic)) 6522 CmdArgs.push_back("-export-dynamic"); 6523 if (Args.hasArg(options::OPT_shared)) 6524 CmdArgs.push_back("-Bshareable"); 6525 else { 6526 CmdArgs.push_back("-dynamic-linker"); 6527 CmdArgs.push_back("/usr/libexec/ld-elf.so.2"); 6528 } 6529 CmdArgs.push_back("--hash-style=both"); 6530 } 6531 6532 // When building 32-bit code on DragonFly/pc64, we have to explicitly 6533 // instruct ld in the base system to link 32-bit code. 6534 if (getToolChain().getArch() == llvm::Triple::x86) { 6535 CmdArgs.push_back("-m"); 6536 CmdArgs.push_back("elf_i386"); 6537 } 6538 6539 if (Output.isFilename()) { 6540 CmdArgs.push_back("-o"); 6541 CmdArgs.push_back(Output.getFilename()); 6542 } else { 6543 assert(Output.isNothing() && "Invalid output."); 6544 } 6545 6546 if (!Args.hasArg(options::OPT_nostdlib) && 6547 !Args.hasArg(options::OPT_nostartfiles)) { 6548 if (!Args.hasArg(options::OPT_shared)) { 6549 if (Args.hasArg(options::OPT_pg)) 6550 CmdArgs.push_back(Args.MakeArgString( 6551 getToolChain().GetFilePath("gcrt1.o"))); 6552 else { 6553 if (Args.hasArg(options::OPT_pie)) 6554 CmdArgs.push_back(Args.MakeArgString( 6555 getToolChain().GetFilePath("Scrt1.o"))); 6556 else 6557 CmdArgs.push_back(Args.MakeArgString( 6558 getToolChain().GetFilePath("crt1.o"))); 6559 } 6560 } 6561 CmdArgs.push_back(Args.MakeArgString( 6562 getToolChain().GetFilePath("crti.o"))); 6563 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 6564 CmdArgs.push_back(Args.MakeArgString( 6565 getToolChain().GetFilePath("crtbeginS.o"))); 6566 else 6567 CmdArgs.push_back(Args.MakeArgString( 6568 getToolChain().GetFilePath("crtbegin.o"))); 6569 } 6570 6571 Args.AddAllArgs(CmdArgs, options::OPT_L); 6572 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 6573 Args.AddAllArgs(CmdArgs, options::OPT_e); 6574 6575 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 6576 6577 if (!Args.hasArg(options::OPT_nostdlib) && 6578 !Args.hasArg(options::OPT_nodefaultlibs)) { 6579 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of 6580 // rpaths 6581 if (UseGCC47) 6582 CmdArgs.push_back("-L/usr/lib/gcc47"); 6583 else 6584 CmdArgs.push_back("-L/usr/lib/gcc44"); 6585 6586 if (!Args.hasArg(options::OPT_static)) { 6587 if (UseGCC47) { 6588 CmdArgs.push_back("-rpath"); 6589 CmdArgs.push_back("/usr/lib/gcc47"); 6590 } else { 6591 CmdArgs.push_back("-rpath"); 6592 CmdArgs.push_back("/usr/lib/gcc44"); 6593 } 6594 } 6595 6596 if (D.CCCIsCXX()) { 6597 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 6598 CmdArgs.push_back("-lm"); 6599 } 6600 6601 if (Args.hasArg(options::OPT_pthread)) 6602 CmdArgs.push_back("-lpthread"); 6603 6604 if (!Args.hasArg(options::OPT_nolibc)) { 6605 CmdArgs.push_back("-lc"); 6606 } 6607 6608 if (UseGCC47) { 6609 if (Args.hasArg(options::OPT_static) || 6610 Args.hasArg(options::OPT_static_libgcc)) { 6611 CmdArgs.push_back("-lgcc"); 6612 CmdArgs.push_back("-lgcc_eh"); 6613 } else { 6614 if (Args.hasArg(options::OPT_shared_libgcc)) { 6615 CmdArgs.push_back("-lgcc_pic"); 6616 if (!Args.hasArg(options::OPT_shared)) 6617 CmdArgs.push_back("-lgcc"); 6618 } else { 6619 CmdArgs.push_back("-lgcc"); 6620 CmdArgs.push_back("--as-needed"); 6621 CmdArgs.push_back("-lgcc_pic"); 6622 CmdArgs.push_back("--no-as-needed"); 6623 } 6624 } 6625 } else { 6626 if (Args.hasArg(options::OPT_shared)) { 6627 CmdArgs.push_back("-lgcc_pic"); 6628 } else { 6629 CmdArgs.push_back("-lgcc"); 6630 } 6631 } 6632 } 6633 6634 if (!Args.hasArg(options::OPT_nostdlib) && 6635 !Args.hasArg(options::OPT_nostartfiles)) { 6636 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 6637 CmdArgs.push_back(Args.MakeArgString( 6638 getToolChain().GetFilePath("crtendS.o"))); 6639 else 6640 CmdArgs.push_back(Args.MakeArgString( 6641 getToolChain().GetFilePath("crtend.o"))); 6642 CmdArgs.push_back(Args.MakeArgString( 6643 getToolChain().GetFilePath("crtn.o"))); 6644 } 6645 6646 addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); 6647 6648 const char *Exec = 6649 Args.MakeArgString(getToolChain().GetProgramPath("ld")); 6650 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6651} 6652 6653void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA, 6654 const InputInfo &Output, 6655 const InputInfoList &Inputs, 6656 const ArgList &Args, 6657 const char *LinkingOutput) const { 6658 ArgStringList CmdArgs; 6659 6660 if (Output.isFilename()) { 6661 CmdArgs.push_back(Args.MakeArgString(std::string("-out:") + 6662 Output.getFilename())); 6663 } else { 6664 assert(Output.isNothing() && "Invalid output."); 6665 } 6666 6667 if (!Args.hasArg(options::OPT_nostdlib) && 6668 !Args.hasArg(options::OPT_nostartfiles) && 6669 !C.getDriver().IsCLMode()) { 6670 CmdArgs.push_back("-defaultlib:libcmt"); 6671 } 6672 6673 CmdArgs.push_back("-nologo"); 6674 6675 bool DLL = Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd); 6676 6677 if (DLL) { 6678 CmdArgs.push_back(Args.MakeArgString("-dll")); 6679 6680 SmallString<128> ImplibName(Output.getFilename()); 6681 llvm::sys::path::replace_extension(ImplibName, "lib"); 6682 CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") + 6683 ImplibName.str())); 6684 } 6685 6686 if (getToolChain().getDriver().getOrParseSanitizerArgs(Args).needsAsanRt()) { 6687 CmdArgs.push_back(Args.MakeArgString("-debug")); 6688 CmdArgs.push_back(Args.MakeArgString("-incremental:no")); 6689 SmallString<128> LibSanitizer(getToolChain().getDriver().ResourceDir); 6690 llvm::sys::path::append(LibSanitizer, "lib", "windows"); 6691 if (DLL) { 6692 llvm::sys::path::append(LibSanitizer, "clang_rt.asan_dll_thunk-i386.lib"); 6693 } else { 6694 llvm::sys::path::append(LibSanitizer, "clang_rt.asan-i386.lib"); 6695 } 6696 // FIXME: Handle 64-bit. 6697 CmdArgs.push_back(Args.MakeArgString(LibSanitizer)); 6698 } 6699 6700 Args.AddAllArgValues(CmdArgs, options::OPT_l); 6701 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link); 6702 6703 // Add filenames immediately. 6704 for (InputInfoList::const_iterator 6705 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6706 if (it->isFilename()) 6707 CmdArgs.push_back(it->getFilename()); 6708 else 6709 it->getInputArg().renderAsInput(Args, CmdArgs); 6710 } 6711 6712 const char *Exec = 6713 Args.MakeArgString(getToolChain().GetProgramPath("link.exe")); 6714 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6715} 6716 6717void visualstudio::Compile::ConstructJob(Compilation &C, const JobAction &JA, 6718 const InputInfo &Output, 6719 const InputInfoList &Inputs, 6720 const ArgList &Args, 6721 const char *LinkingOutput) const { 6722 C.addCommand(GetCommand(C, JA, Output, Inputs, Args, LinkingOutput)); 6723} 6724 6725// Try to find FallbackName on PATH that is not identical to ClangProgramPath. 6726// If one cannot be found, return FallbackName. 6727// We do this special search to prevent clang-cl from falling back onto itself 6728// if it's available as cl.exe on the path. 6729static std::string FindFallback(const char *FallbackName, 6730 const char *ClangProgramPath) { 6731 llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH"); 6732 if (!OptPath.hasValue()) 6733 return FallbackName; 6734 6735#ifdef LLVM_ON_WIN32 6736 const StringRef PathSeparators = ";"; 6737#else 6738 const StringRef PathSeparators = ":"; 6739#endif 6740 6741 SmallVector<StringRef, 8> PathSegments; 6742 llvm::SplitString(OptPath.getValue(), PathSegments, PathSeparators); 6743 6744 for (size_t i = 0, e = PathSegments.size(); i != e; ++i) { 6745 const StringRef &PathSegment = PathSegments[i]; 6746 if (PathSegment.empty()) 6747 continue; 6748 6749 SmallString<128> FilePath(PathSegment); 6750 llvm::sys::path::append(FilePath, FallbackName); 6751 if (llvm::sys::fs::can_execute(Twine(FilePath)) && 6752 !llvm::sys::fs::equivalent(Twine(FilePath), ClangProgramPath)) 6753 return FilePath.str(); 6754 } 6755 6756 return FallbackName; 6757} 6758 6759Command *visualstudio::Compile::GetCommand(Compilation &C, const JobAction &JA, 6760 const InputInfo &Output, 6761 const InputInfoList &Inputs, 6762 const ArgList &Args, 6763 const char *LinkingOutput) const { 6764 ArgStringList CmdArgs; 6765 CmdArgs.push_back("/nologo"); 6766 CmdArgs.push_back("/c"); // Compile only. 6767 CmdArgs.push_back("/W0"); // No warnings. 6768 6769 // The goal is to be able to invoke this tool correctly based on 6770 // any flag accepted by clang-cl. 6771 6772 // These are spelled the same way in clang and cl.exe,. 6773 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U); 6774 Args.AddAllArgs(CmdArgs, options::OPT_I); 6775 6776 // Optimization level. 6777 if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) { 6778 if (A->getOption().getID() == options::OPT_O0) { 6779 CmdArgs.push_back("/Od"); 6780 } else { 6781 StringRef OptLevel = A->getValue(); 6782 if (OptLevel == "1" || OptLevel == "2" || OptLevel == "s") 6783 A->render(Args, CmdArgs); 6784 else if (OptLevel == "3") 6785 CmdArgs.push_back("/Ox"); 6786 } 6787 } 6788 6789 // Flags for which clang-cl have an alias. 6790 // FIXME: How can we ensure this stays in sync with relevant clang-cl options? 6791 6792 if (Arg *A = Args.getLastArg(options::OPT_frtti, options::OPT_fno_rtti)) 6793 CmdArgs.push_back(A->getOption().getID() == options::OPT_frtti ? "/GR" 6794 : "/GR-"); 6795 if (Args.hasArg(options::OPT_fsyntax_only)) 6796 CmdArgs.push_back("/Zs"); 6797 6798 std::vector<std::string> Includes = Args.getAllArgValues(options::OPT_include); 6799 for (size_t I = 0, E = Includes.size(); I != E; ++I) 6800 CmdArgs.push_back(Args.MakeArgString(std::string("/FI") + Includes[I])); 6801 6802 // Flags that can simply be passed through. 6803 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LD); 6804 Args.AddAllArgs(CmdArgs, options::OPT__SLASH_LDd); 6805 6806 // The order of these flags is relevant, so pick the last one. 6807 if (Arg *A = Args.getLastArg(options::OPT__SLASH_MD, options::OPT__SLASH_MDd, 6808 options::OPT__SLASH_MT, options::OPT__SLASH_MTd)) 6809 A->render(Args, CmdArgs); 6810 6811 6812 // Input filename. 6813 assert(Inputs.size() == 1); 6814 const InputInfo &II = Inputs[0]; 6815 assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX); 6816 CmdArgs.push_back(II.getType() == types::TY_C ? "/Tc" : "/Tp"); 6817 if (II.isFilename()) 6818 CmdArgs.push_back(II.getFilename()); 6819 else 6820 II.getInputArg().renderAsInput(Args, CmdArgs); 6821 6822 // Output filename. 6823 assert(Output.getType() == types::TY_Object); 6824 const char *Fo = Args.MakeArgString(std::string("/Fo") + 6825 Output.getFilename()); 6826 CmdArgs.push_back(Fo); 6827 6828 const Driver &D = getToolChain().getDriver(); 6829 std::string Exec = FindFallback("cl.exe", D.getClangProgramPath()); 6830 6831 return new Command(JA, *this, Args.MakeArgString(Exec), CmdArgs); 6832} 6833 6834 6835/// XCore Tools 6836// We pass assemble and link construction to the xcc tool. 6837 6838void XCore::Assemble::ConstructJob(Compilation &C, const JobAction &JA, 6839 const InputInfo &Output, 6840 const InputInfoList &Inputs, 6841 const ArgList &Args, 6842 const char *LinkingOutput) const { 6843 ArgStringList CmdArgs; 6844 6845 CmdArgs.push_back("-o"); 6846 CmdArgs.push_back(Output.getFilename()); 6847 6848 CmdArgs.push_back("-c"); 6849 6850 if (Args.hasArg(options::OPT_g_Group)) { 6851 CmdArgs.push_back("-g"); 6852 } 6853 6854 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, 6855 options::OPT_Xassembler); 6856 6857 for (InputInfoList::const_iterator 6858 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { 6859 const InputInfo &II = *it; 6860 CmdArgs.push_back(II.getFilename()); 6861 } 6862 6863 const char *Exec = 6864 Args.MakeArgString(getToolChain().GetProgramPath("xcc")); 6865 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6866} 6867 6868void XCore::Link::ConstructJob(Compilation &C, const JobAction &JA, 6869 const InputInfo &Output, 6870 const InputInfoList &Inputs, 6871 const ArgList &Args, 6872 const char *LinkingOutput) const { 6873 ArgStringList CmdArgs; 6874 6875 if (Output.isFilename()) { 6876 CmdArgs.push_back("-o"); 6877 CmdArgs.push_back(Output.getFilename()); 6878 } else { 6879 assert(Output.isNothing() && "Invalid output."); 6880 } 6881 6882 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); 6883 6884 const char *Exec = 6885 Args.MakeArgString(getToolChain().GetProgramPath("xcc")); 6886 C.addCommand(new Command(JA, *this, Exec, CmdArgs)); 6887} 6888