1281SN/A//===--- ToolChain.cpp - Collections of tools for one platform ------------===// 2281SN/A// 3281SN/A// The LLVM Compiler Infrastructure 4281SN/A// 5281SN/A// This file is distributed under the University of Illinois Open Source 6281SN/A// License. See LICENSE.TXT for details. 7281SN/A// 8281SN/A//===----------------------------------------------------------------------===// 9281SN/A 10281SN/A#include "Tools.h" 11281SN/A#include "clang/Basic/ObjCRuntime.h" 12281SN/A#include "clang/Driver/Action.h" 13281SN/A#include "clang/Driver/Driver.h" 14281SN/A#include "clang/Driver/DriverDiagnostic.h" 15281SN/A#include "clang/Driver/Options.h" 16281SN/A#include "clang/Driver/SanitizerArgs.h" 17281SN/A#include "clang/Driver/ToolChain.h" 18281SN/A#include "llvm/ADT/StringSwitch.h" 19281SN/A#include "llvm/Option/Arg.h" 20281SN/A#include "llvm/Option/ArgList.h" 21281SN/A#include "llvm/Option/Option.h" 22281SN/A#include "llvm/Support/ErrorHandling.h" 23281SN/A#include "llvm/Support/FileSystem.h" 24281SN/Ausing namespace clang::driver; 25281SN/Ausing namespace clang; 26281SN/Ausing namespace llvm::opt; 27281SN/A 28281SN/AToolChain::ToolChain(const Driver &D, const llvm::Triple &T, 29281SN/A const ArgList &A) 30281SN/A : D(D), Triple(T), Args(A) { 31281SN/A} 32281SN/A 33281SN/AToolChain::~ToolChain() { 34281SN/A} 35281SN/A 36281SN/Aconst Driver &ToolChain::getDriver() const { 37281SN/A return D; 38281SN/A} 39281SN/A 40281SN/Abool ToolChain::useIntegratedAs() const { 41281SN/A return Args.hasFlag(options::OPT_integrated_as, 42281SN/A options::OPT_no_integrated_as, 43281SN/A IsIntegratedAssemblerDefault()); 44281SN/A} 45281SN/A 46281SN/Aconst SanitizerArgs& ToolChain::getSanitizerArgs() const { 47281SN/A if (!SanitizerArguments.get()) 48281SN/A SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 49281SN/A return *SanitizerArguments.get(); 50281SN/A} 51281SN/A 52281SN/Astd::string ToolChain::getDefaultUniversalArchName() const { 53281SN/A // In universal driver terms, the arch name accepted by -arch isn't exactly 54281SN/A // the same as the ones that appear in the triple. Roughly speaking, this is 55281SN/A // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the 56281SN/A // only interesting special case is powerpc. 57281SN/A switch (Triple.getArch()) { 58281SN/A case llvm::Triple::ppc: 59281SN/A return "ppc"; 60605SN/A case llvm::Triple::ppc64: 61281SN/A return "ppc64"; 62281SN/A case llvm::Triple::ppc64le: 63281SN/A return "ppc64le"; 64281SN/A default: 65281SN/A return Triple.getArchName(); 66281SN/A } 67281SN/A} 68281SN/A 69281SN/Abool ToolChain::IsUnwindTablesDefault() const { 70281SN/A return false; 71281SN/A} 72281SN/A 73281SN/ATool *ToolChain::getClang() const { 74281SN/A if (!Clang) 75281SN/A Clang.reset(new tools::Clang(*this)); 76281SN/A return Clang.get(); 77281SN/A} 78281SN/A 79281SN/ATool *ToolChain::buildAssembler() const { 80281SN/A return new tools::ClangAs(*this); 81281SN/A} 82281SN/A 83281SN/ATool *ToolChain::buildLinker() const { 84281SN/A llvm_unreachable("Linking is not supported by this toolchain"); 85281SN/A} 86281SN/A 87281SN/ATool *ToolChain::getAssemble() const { 88281SN/A if (!Assemble) 89281SN/A Assemble.reset(buildAssembler()); 90281SN/A return Assemble.get(); 91281SN/A} 92281SN/A 93281SN/ATool *ToolChain::getClangAs() const { 94281SN/A if (!Assemble) 95281SN/A Assemble.reset(new tools::ClangAs(*this)); 96281SN/A return Assemble.get(); 97281SN/A} 98281SN/A 99281SN/ATool *ToolChain::getLink() const { 100281SN/A if (!Link) 101281SN/A Link.reset(buildLinker()); 102281SN/A return Link.get(); 103281SN/A} 104281SN/A 105281SN/ATool *ToolChain::getTool(Action::ActionClass AC) const { 106281SN/A switch (AC) { 107281SN/A case Action::AssembleJobClass: 108281SN/A return getAssemble(); 109281SN/A 110281SN/A case Action::LinkJobClass: 111281SN/A return getLink(); 112281SN/A 113281SN/A case Action::InputClass: 114281SN/A case Action::BindArchClass: 115281SN/A case Action::LipoJobClass: 116281SN/A case Action::DsymutilJobClass: 117281SN/A case Action::VerifyJobClass: 118281SN/A llvm_unreachable("Invalid tool kind."); 119281SN/A 120281SN/A case Action::CompileJobClass: 121281SN/A case Action::PrecompileJobClass: 122281SN/A case Action::PreprocessJobClass: 123281SN/A case Action::AnalyzeJobClass: 124281SN/A case Action::MigrateJobClass: 125281SN/A return getClang(); 126281SN/A } 127281SN/A 128281SN/A llvm_unreachable("Invalid tool kind."); 129281SN/A} 130281SN/A 131281SN/ATool *ToolChain::SelectTool(const JobAction &JA) const { 132281SN/A if (getDriver().ShouldUseClangCompiler(JA)) 133281SN/A return getClang(); 134281SN/A Action::ActionClass AC = JA.getKind(); 135281SN/A if (AC == Action::AssembleJobClass && useIntegratedAs()) 136281SN/A return getClangAs(); 137281SN/A return getTool(AC); 138281SN/A} 139281SN/A 140281SN/Astd::string ToolChain::GetFilePath(const char *Name) const { 141281SN/A return D.GetFilePath(Name, *this); 142281SN/A 143281SN/A} 144281SN/A 145281SN/Astd::string ToolChain::GetProgramPath(const char *Name) const { 146281SN/A return D.GetProgramPath(Name, *this); 147281SN/A} 148281SN/A 149281SN/Atypes::ID ToolChain::LookupTypeForExtension(const char *Ext) const { 150281SN/A return types::lookupTypeForExtension(Ext); 151281SN/A} 152281SN/A 153281SN/Abool ToolChain::HasNativeLLVMSupport() const { 154281SN/A return false; 155281SN/A} 156281SN/A 157281SN/AObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 158281SN/A return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 159281SN/A VersionTuple()); 160281SN/A} 161281SN/A 162281SN/A/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting. 163281SN/A// 164281SN/A// FIXME: tblgen this. 165281SN/Astatic const char *getARMTargetCPU(const ArgList &Args, 166281SN/A const llvm::Triple &Triple) { 167281SN/A // For Darwin targets, the -arch option (which is translated to a 168281SN/A // corresponding -march option) should determine the architecture 169281SN/A // (and the Mach-O slice) regardless of any -mcpu options. 170281SN/A if (!Triple.isOSDarwin()) { 171281SN/A // FIXME: Warn on inconsistent use of -mcpu and -march. 172281SN/A // If we have -mcpu=, use that. 173281SN/A if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 174281SN/A return A->getValue(); 175281SN/A } 176281SN/A 177281SN/A StringRef MArch; 178281SN/A if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 179281SN/A // Otherwise, if we have -march= choose the base CPU for that arch. 180281SN/A MArch = A->getValue(); 181281SN/A } else { 182281SN/A // Otherwise, use the Arch from the triple. 183281SN/A MArch = Triple.getArchName(); 184281SN/A } 185281SN/A 186281SN/A if (Triple.getOS() == llvm::Triple::NetBSD) { 187281SN/A if (MArch == "armv6") 188281SN/A return "arm1176jzf-s"; 189281SN/A } 190281SN/A 191281SN/A const char *result = llvm::StringSwitch<const char *>(MArch) 192281SN/A .Cases("armv2", "armv2a","arm2") 193281SN/A .Case("armv3", "arm6") 194281SN/A .Case("armv3m", "arm7m") 195281SN/A .Case("armv4", "strongarm") 196281SN/A .Case("armv4t", "arm7tdmi") 197281SN/A .Cases("armv5", "armv5t", "arm10tdmi") 198281SN/A .Cases("armv5e", "armv5te", "arm1026ejs") 199281SN/A .Case("armv5tej", "arm926ej-s") 200281SN/A .Cases("armv6", "armv6k", "arm1136jf-s") 201281SN/A .Case("armv6j", "arm1136j-s") 202281SN/A .Cases("armv6z", "armv6zk", "arm1176jzf-s") 203281SN/A .Case("armv6t2", "arm1156t2-s") 204281SN/A .Cases("armv6m", "armv6-m", "cortex-m0") 205281SN/A .Cases("armv7", "armv7a", "armv7-a", "cortex-a8") 206281SN/A .Cases("armv7l", "armv7-l", "cortex-a8") 207281SN/A .Cases("armv7f", "armv7-f", "cortex-a9-mp") 208281SN/A .Cases("armv7s", "armv7-s", "swift") 209281SN/A .Cases("armv7r", "armv7-r", "cortex-r4") 210281SN/A .Cases("armv7m", "armv7-m", "cortex-m3") 211281SN/A .Cases("armv7em", "armv7e-m", "cortex-m4") 212281SN/A .Cases("armv8", "armv8a", "armv8-a", "cortex-a53") 213281SN/A .Case("ep9312", "ep9312") 214281SN/A .Case("iwmmxt", "iwmmxt") 215281SN/A .Case("xscale", "xscale") 216281SN/A // If all else failed, return the most base CPU with thumb interworking 217281SN/A // supported by LLVM. 218281SN/A .Default(0); 219281SN/A 220281SN/A if (result) 221281SN/A return result; 222281SN/A 223281SN/A return 224281SN/A Triple.getEnvironment() == llvm::Triple::GNUEABIHF 225281SN/A ? "arm1176jzf-s" 226281SN/A : "arm7tdmi"; 227281SN/A} 228281SN/A 229281SN/A/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular 230281SN/A/// CPU. 231281SN/A// 232281SN/A// FIXME: This is redundant with -mcpu, why does LLVM use this. 233281SN/A// FIXME: tblgen this, or kill it! 234281SN/Astatic const char *getLLVMArchSuffixForARM(StringRef CPU) { 235281SN/A return llvm::StringSwitch<const char *>(CPU) 236281SN/A .Case("strongarm", "v4") 237281SN/A .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t") 238281SN/A .Cases("arm720t", "arm9", "arm9tdmi", "v4t") 239281SN/A .Cases("arm920", "arm920t", "arm922t", "v4t") 240281SN/A .Cases("arm940t", "ep9312","v4t") 241281SN/A .Cases("arm10tdmi", "arm1020t", "v5") 242281SN/A .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e") 243281SN/A .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e") 244281SN/A .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e") 245281SN/A .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6") 246281SN/A .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6") 247281SN/A .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2") 248281SN/A .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7") 249281SN/A .Cases("cortex-a9", "cortex-a12", "cortex-a15", "v7") 250281SN/A .Cases("cortex-r4", "cortex-r5", "v7r") 251281SN/A .Case("cortex-m0", "v6m") 252281SN/A .Case("cortex-m3", "v7m") 253281SN/A .Case("cortex-m4", "v7em") 254281SN/A .Case("cortex-a9-mp", "v7f") 255281SN/A .Case("swift", "v7s") 256281SN/A .Cases("cortex-a53", "cortex-a57", "v8") 257281SN/A .Default(""); 258281SN/A} 259281SN/A 260281SN/Astd::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 261281SN/A types::ID InputType) const { 262281SN/A switch (getTriple().getArch()) { 263281SN/A default: 264281SN/A return getTripleString(); 265281SN/A 266281SN/A case llvm::Triple::x86_64: { 267281SN/A llvm::Triple Triple = getTriple(); 268281SN/A if (!Triple.isOSDarwin()) 269281SN/A return getTripleString(); 270281SN/A 271281SN/A if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 272281SN/A // x86_64h goes in the triple. Other -march options just use the 273281SN/A // vanilla triple we already have. 274281SN/A StringRef MArch = A->getValue(); 275281SN/A if (MArch == "x86_64h") 276281SN/A Triple.setArchName(MArch); 277281SN/A } 278281SN/A return Triple.getTriple(); 279281SN/A } 280281SN/A case llvm::Triple::arm: 281281SN/A case llvm::Triple::thumb: { 282281SN/A // FIXME: Factor into subclasses. 283281SN/A llvm::Triple Triple = getTriple(); 284281SN/A 285281SN/A // Thumb2 is the default for V7 on Darwin. 286281SN/A // 287281SN/A // FIXME: Thumb should just be another -target-feaure, not in the triple. 288281SN/A StringRef Suffix = 289281SN/A getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple)); 290281SN/A bool ThumbDefault = Suffix.startswith("v6m") || 291281SN/A (Suffix.startswith("v7") && getTriple().isOSDarwin()); 292281SN/A std::string ArchName = "arm"; 293281SN/A 294281SN/A // Assembly files should start in ARM mode. 295281SN/A if (InputType != types::TY_PP_Asm && 296281SN/A Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault)) 297281SN/A ArchName = "thumb"; 298281SN/A Triple.setArchName(ArchName + Suffix.str()); 299281SN/A 300281SN/A return Triple.getTriple(); 301281SN/A } 302281SN/A } 303281SN/A} 304281SN/A 305281SN/Astd::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 306281SN/A types::ID InputType) const { 307281SN/A // Diagnose use of Darwin OS deployment target arguments on non-Darwin. 308281SN/A if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ, 309281SN/A options::OPT_miphoneos_version_min_EQ, 310281SN/A options::OPT_mios_simulator_version_min_EQ)) 311281SN/A getDriver().Diag(diag::err_drv_clang_unsupported) 312281SN/A << A->getAsString(Args); 313281SN/A 314281SN/A return ComputeLLVMTriple(Args, InputType); 315281SN/A} 316281SN/A 317281SN/Avoid ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 318281SN/A ArgStringList &CC1Args) const { 319281SN/A // Each toolchain should provide the appropriate include flags. 320281SN/A} 321281SN/A 322281SN/Avoid ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 323281SN/A ArgStringList &CC1Args) const { 324281SN/A} 325281SN/A 326281SN/AToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 327281SN/A const ArgList &Args) const 328281SN/A{ 329281SN/A if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) { 330281SN/A StringRef Value = A->getValue(); 331281SN/A if (Value == "compiler-rt") 332281SN/A return ToolChain::RLT_CompilerRT; 333281SN/A if (Value == "libgcc") 334281SN/A return ToolChain::RLT_Libgcc; 335281SN/A getDriver().Diag(diag::err_drv_invalid_rtlib_name) 336281SN/A << A->getAsString(Args); 337281SN/A } 338281SN/A 339281SN/A return GetDefaultRuntimeLibType(); 340281SN/A} 341281SN/A 342281SN/AToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 343281SN/A if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 344281SN/A StringRef Value = A->getValue(); 345281SN/A if (Value == "libc++") 346281SN/A return ToolChain::CST_Libcxx; 347281SN/A if (Value == "libstdc++") 348281SN/A return ToolChain::CST_Libstdcxx; 349281SN/A getDriver().Diag(diag::err_drv_invalid_stdlib_name) 350 << A->getAsString(Args); 351 } 352 353 return ToolChain::CST_Libstdcxx; 354} 355 356/// \brief Utility function to add a system include directory to CC1 arguments. 357/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 358 ArgStringList &CC1Args, 359 const Twine &Path) { 360 CC1Args.push_back("-internal-isystem"); 361 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 362} 363 364/// \brief Utility function to add a system include directory with extern "C" 365/// semantics to CC1 arguments. 366/// 367/// Note that this should be used rarely, and only for directories that 368/// historically and for legacy reasons are treated as having implicit extern 369/// "C" semantics. These semantics are *ignored* by and large today, but its 370/// important to preserve the preprocessor changes resulting from the 371/// classification. 372/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 373 ArgStringList &CC1Args, 374 const Twine &Path) { 375 CC1Args.push_back("-internal-externc-isystem"); 376 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 377} 378 379void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 380 ArgStringList &CC1Args, 381 const Twine &Path) { 382 if (llvm::sys::fs::exists(Path)) 383 addExternCSystemInclude(DriverArgs, CC1Args, Path); 384} 385 386/// \brief Utility function to add a list of system include directories to CC1. 387/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 388 ArgStringList &CC1Args, 389 ArrayRef<StringRef> Paths) { 390 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end(); 391 I != E; ++I) { 392 CC1Args.push_back("-internal-isystem"); 393 CC1Args.push_back(DriverArgs.MakeArgString(*I)); 394 } 395} 396 397void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 398 ArgStringList &CC1Args) const { 399 // Header search paths should be handled by each of the subclasses. 400 // Historically, they have not been, and instead have been handled inside of 401 // the CC1-layer frontend. As the logic is hoisted out, this generic function 402 // will slowly stop being called. 403 // 404 // While it is being called, replicate a bit of a hack to propagate the 405 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 406 // header search paths with it. Once all systems are overriding this 407 // function, the CC1 flag and this line can be removed. 408 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 409} 410 411void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 412 ArgStringList &CmdArgs) const { 413 CXXStdlibType Type = GetCXXStdlibType(Args); 414 415 switch (Type) { 416 case ToolChain::CST_Libcxx: 417 CmdArgs.push_back("-lc++"); 418 break; 419 420 case ToolChain::CST_Libstdcxx: 421 CmdArgs.push_back("-lstdc++"); 422 break; 423 } 424} 425 426void ToolChain::AddCCKextLibArgs(const ArgList &Args, 427 ArgStringList &CmdArgs) const { 428 CmdArgs.push_back("-lcc_kext"); 429} 430 431bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 432 ArgStringList &CmdArgs) const { 433 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 434 // (to keep the linker options consistent with gcc and clang itself). 435 if (!isOptimizationLevelFast(Args)) { 436 // Check if -ffast-math or -funsafe-math. 437 Arg *A = 438 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 439 options::OPT_funsafe_math_optimizations, 440 options::OPT_fno_unsafe_math_optimizations); 441 442 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 443 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 444 return false; 445 } 446 // If crtfastmath.o exists add it to the arguments. 447 std::string Path = GetFilePath("crtfastmath.o"); 448 if (Path == "crtfastmath.o") // Not found. 449 return false; 450 451 CmdArgs.push_back(Args.MakeArgString(Path)); 452 return true; 453} 454