1 //===--- ToolChain.cpp - Collections of tools for one platform ------------===// 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 "clang/Basic/ObjCRuntime.h" 12 #include "clang/Driver/Action.h" 13 #include "clang/Driver/Driver.h" 14 #include "clang/Driver/DriverDiagnostic.h" 15 #include "clang/Driver/Options.h" 16 #include "clang/Driver/SanitizerArgs.h" 17 #include "clang/Driver/ToolChain.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/StringSwitch.h" 20 #include "llvm/Option/Arg.h" 21 #include "llvm/Option/ArgList.h" 22 #include "llvm/Option/Option.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/FileSystem.h" 25 using namespace clang::driver; 26 using namespace clang; 27 using namespace llvm::opt; 28 29 static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) { 30 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext, 31 options::OPT_fno_rtti, options::OPT_frtti); 32 } 33 34 static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args, 35 const llvm::Triple &Triple, 36 const Arg *CachedRTTIArg) { 37 // Explicit rtti/no-rtti args 38 if (CachedRTTIArg) { 39 if (CachedRTTIArg->getOption().matches(options::OPT_frtti)) 40 return ToolChain::RM_EnabledExplicitly; 41 else 42 return ToolChain::RM_DisabledExplicitly; 43 } 44 45 // -frtti is default, except for the PS4 CPU. 46 if (!Triple.isPS4CPU()) 47 return ToolChain::RM_EnabledImplicitly; 48 49 // On the PS4, turning on c++ exceptions turns on rtti. 50 // We're assuming that, if we see -fexceptions, rtti gets turned on. 51 Arg *Exceptions = Args.getLastArgNoClaim( 52 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 53 options::OPT_fexceptions, options::OPT_fno_exceptions); 54 if (Exceptions && 55 (Exceptions->getOption().matches(options::OPT_fexceptions) || 56 Exceptions->getOption().matches(options::OPT_fcxx_exceptions))) 57 return ToolChain::RM_EnabledImplicitly; 58 59 return ToolChain::RM_DisabledImplicitly; 60 } 61 62 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T, 63 const ArgList &Args) 64 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)), 65 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) { 66 if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) 67 if (!isThreadModelSupported(A->getValue())) 68 D.Diag(diag::err_drv_invalid_thread_model_for_target) 69 << A->getValue() << A->getAsString(Args); 70 } 71 72 ToolChain::~ToolChain() { 73 } 74 75 const Driver &ToolChain::getDriver() const { 76 return D; 77 } 78 79 bool ToolChain::useIntegratedAs() const { 80 return Args.hasFlag(options::OPT_fintegrated_as, 81 options::OPT_fno_integrated_as, 82 IsIntegratedAssemblerDefault()); 83 } 84 85 const SanitizerArgs& ToolChain::getSanitizerArgs() const { 86 if (!SanitizerArguments.get()) 87 SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 88 return *SanitizerArguments.get(); 89 } 90 91 StringRef ToolChain::getDefaultUniversalArchName() const { 92 // In universal driver terms, the arch name accepted by -arch isn't exactly 93 // the same as the ones that appear in the triple. Roughly speaking, this is 94 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the 95 // only interesting special case is powerpc. 96 switch (Triple.getArch()) { 97 case llvm::Triple::ppc: 98 return "ppc"; 99 case llvm::Triple::ppc64: 100 return "ppc64"; 101 case llvm::Triple::ppc64le: 102 return "ppc64le"; 103 default: 104 return Triple.getArchName(); 105 } 106 } 107 108 bool ToolChain::IsUnwindTablesDefault() const { 109 return false; 110 } 111 112 Tool *ToolChain::getClang() const { 113 if (!Clang) 114 Clang.reset(new tools::Clang(*this)); 115 return Clang.get(); 116 } 117 118 Tool *ToolChain::buildAssembler() const { 119 return new tools::ClangAs(*this); 120 } 121 122 Tool *ToolChain::buildLinker() const { 123 llvm_unreachable("Linking is not supported by this toolchain"); 124 } 125 126 Tool *ToolChain::getAssemble() const { 127 if (!Assemble) 128 Assemble.reset(buildAssembler()); 129 return Assemble.get(); 130 } 131 132 Tool *ToolChain::getClangAs() const { 133 if (!Assemble) 134 Assemble.reset(new tools::ClangAs(*this)); 135 return Assemble.get(); 136 } 137 138 Tool *ToolChain::getLink() const { 139 if (!Link) 140 Link.reset(buildLinker()); 141 return Link.get(); 142 } 143 144 Tool *ToolChain::getTool(Action::ActionClass AC) const { 145 switch (AC) { 146 case Action::AssembleJobClass: 147 return getAssemble(); 148 149 case Action::LinkJobClass: 150 return getLink(); 151 152 case Action::InputClass: 153 case Action::BindArchClass: 154 case Action::CudaDeviceClass: 155 case Action::CudaHostClass: 156 case Action::LipoJobClass: 157 case Action::DsymutilJobClass: 158 case Action::VerifyDebugInfoJobClass: 159 llvm_unreachable("Invalid tool kind."); 160 161 case Action::CompileJobClass: 162 case Action::PrecompileJobClass: 163 case Action::PreprocessJobClass: 164 case Action::AnalyzeJobClass: 165 case Action::MigrateJobClass: 166 case Action::VerifyPCHJobClass: 167 case Action::BackendJobClass: 168 return getClang(); 169 } 170 171 llvm_unreachable("Invalid tool kind."); 172 } 173 174 Tool *ToolChain::SelectTool(const JobAction &JA) const { 175 if (getDriver().ShouldUseClangCompiler(JA)) 176 return getClang(); 177 Action::ActionClass AC = JA.getKind(); 178 if (AC == Action::AssembleJobClass && useIntegratedAs()) 179 return getClangAs(); 180 return getTool(AC); 181 } 182 183 std::string ToolChain::GetFilePath(const char *Name) const { 184 return D.GetFilePath(Name, *this); 185 186 } 187 188 std::string ToolChain::GetProgramPath(const char *Name) const { 189 return D.GetProgramPath(Name, *this); 190 } 191 192 std::string ToolChain::GetLinkerPath() const { 193 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { 194 StringRef Suffix = A->getValue(); 195 196 // If we're passed -fuse-ld= with no argument, or with the argument ld, 197 // then use whatever the default system linker is. 198 if (Suffix.empty() || Suffix == "ld") 199 return GetProgramPath("ld"); 200 201 llvm::SmallString<8> LinkerName("ld."); 202 LinkerName.append(Suffix); 203 204 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 205 if (llvm::sys::fs::exists(LinkerPath)) 206 return LinkerPath; 207 208 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 209 return ""; 210 } 211 212 return GetProgramPath("ld"); 213 } 214 215 216 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const { 217 return types::lookupTypeForExtension(Ext); 218 } 219 220 bool ToolChain::HasNativeLLVMSupport() const { 221 return false; 222 } 223 224 bool ToolChain::isCrossCompiling() const { 225 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 226 switch (HostTriple.getArch()) { 227 // The A32/T32/T16 instruction sets are not separate architectures in this 228 // context. 229 case llvm::Triple::arm: 230 case llvm::Triple::armeb: 231 case llvm::Triple::thumb: 232 case llvm::Triple::thumbeb: 233 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 234 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 235 default: 236 return HostTriple.getArch() != getArch(); 237 } 238 } 239 240 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 241 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 242 VersionTuple()); 243 } 244 245 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 246 if (Model == "single") { 247 // FIXME: 'single' is only supported on ARM and WebAssembly so far. 248 return Triple.getArch() == llvm::Triple::arm || 249 Triple.getArch() == llvm::Triple::armeb || 250 Triple.getArch() == llvm::Triple::thumb || 251 Triple.getArch() == llvm::Triple::thumbeb || 252 Triple.getArch() == llvm::Triple::wasm32 || 253 Triple.getArch() == llvm::Triple::wasm64; 254 } else if (Model == "posix") 255 return true; 256 257 return false; 258 } 259 260 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 261 types::ID InputType) const { 262 switch (getTriple().getArch()) { 263 default: 264 return getTripleString(); 265 266 case llvm::Triple::x86_64: { 267 llvm::Triple Triple = getTriple(); 268 if (!Triple.isOSBinFormatMachO()) 269 return getTripleString(); 270 271 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 272 // x86_64h goes in the triple. Other -march options just use the 273 // vanilla triple we already have. 274 StringRef MArch = A->getValue(); 275 if (MArch == "x86_64h") 276 Triple.setArchName(MArch); 277 } 278 return Triple.getTriple(); 279 } 280 case llvm::Triple::aarch64: { 281 llvm::Triple Triple = getTriple(); 282 if (!Triple.isOSBinFormatMachO()) 283 return getTripleString(); 284 285 // FIXME: older versions of ld64 expect the "arm64" component in the actual 286 // triple string and query it to determine whether an LTO file can be 287 // handled. Remove this when we don't care any more. 288 Triple.setArchName("arm64"); 289 return Triple.getTriple(); 290 } 291 case llvm::Triple::arm: 292 case llvm::Triple::armeb: 293 case llvm::Triple::thumb: 294 case llvm::Triple::thumbeb: { 295 // FIXME: Factor into subclasses. 296 llvm::Triple Triple = getTriple(); 297 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || 298 getTriple().getArch() == llvm::Triple::thumbeb; 299 300 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 301 // '-mbig-endian'/'-EB'. 302 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 303 options::OPT_mbig_endian)) { 304 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 305 } 306 307 // Thumb2 is the default for V7 on Darwin. 308 // 309 // FIXME: Thumb should just be another -target-feaure, not in the triple. 310 StringRef MCPU, MArch; 311 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 312 MCPU = A->getValue(); 313 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 314 MArch = A->getValue(); 315 std::string CPU = 316 Triple.isOSBinFormatMachO() 317 ? tools::arm::getARMCPUForMArch(MArch, Triple).str() 318 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); 319 StringRef Suffix = 320 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 321 bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") || 322 Suffix.startswith("v7em") || 323 (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO()); 324 // FIXME: this is invalid for WindowsCE 325 if (getTriple().isOSWindows()) 326 ThumbDefault = true; 327 std::string ArchName; 328 if (IsBigEndian) 329 ArchName = "armeb"; 330 else 331 ArchName = "arm"; 332 333 // Assembly files should start in ARM mode. 334 if (InputType != types::TY_PP_Asm && 335 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault)) 336 { 337 if (IsBigEndian) 338 ArchName = "thumbeb"; 339 else 340 ArchName = "thumb"; 341 } 342 Triple.setArchName(ArchName + Suffix.str()); 343 344 return Triple.getTriple(); 345 } 346 } 347 } 348 349 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 350 types::ID InputType) const { 351 return ComputeLLVMTriple(Args, InputType); 352 } 353 354 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 355 ArgStringList &CC1Args) const { 356 // Each toolchain should provide the appropriate include flags. 357 } 358 359 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 360 ArgStringList &CC1Args) const { 361 } 362 363 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 364 365 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 366 const ArgList &Args) const 367 { 368 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) { 369 StringRef Value = A->getValue(); 370 if (Value == "compiler-rt") 371 return ToolChain::RLT_CompilerRT; 372 if (Value == "libgcc") 373 return ToolChain::RLT_Libgcc; 374 getDriver().Diag(diag::err_drv_invalid_rtlib_name) 375 << A->getAsString(Args); 376 } 377 378 return GetDefaultRuntimeLibType(); 379 } 380 381 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 382 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 383 StringRef Value = A->getValue(); 384 if (Value == "libc++") 385 return ToolChain::CST_Libcxx; 386 if (Value == "libstdc++") 387 return ToolChain::CST_Libstdcxx; 388 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 389 << A->getAsString(Args); 390 } 391 392 return ToolChain::CST_Libstdcxx; 393 } 394 395 /// \brief Utility function to add a system include directory to CC1 arguments. 396 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 397 ArgStringList &CC1Args, 398 const Twine &Path) { 399 CC1Args.push_back("-internal-isystem"); 400 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 401 } 402 403 /// \brief Utility function to add a system include directory with extern "C" 404 /// semantics to CC1 arguments. 405 /// 406 /// Note that this should be used rarely, and only for directories that 407 /// historically and for legacy reasons are treated as having implicit extern 408 /// "C" semantics. These semantics are *ignored* by and large today, but its 409 /// important to preserve the preprocessor changes resulting from the 410 /// classification. 411 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 412 ArgStringList &CC1Args, 413 const Twine &Path) { 414 CC1Args.push_back("-internal-externc-isystem"); 415 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 416 } 417 418 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 419 ArgStringList &CC1Args, 420 const Twine &Path) { 421 if (llvm::sys::fs::exists(Path)) 422 addExternCSystemInclude(DriverArgs, CC1Args, Path); 423 } 424 425 /// \brief Utility function to add a list of system include directories to CC1. 426 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 427 ArgStringList &CC1Args, 428 ArrayRef<StringRef> Paths) { 429 for (StringRef Path : Paths) { 430 CC1Args.push_back("-internal-isystem"); 431 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 432 } 433 } 434 435 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 436 ArgStringList &CC1Args) const { 437 // Header search paths should be handled by each of the subclasses. 438 // Historically, they have not been, and instead have been handled inside of 439 // the CC1-layer frontend. As the logic is hoisted out, this generic function 440 // will slowly stop being called. 441 // 442 // While it is being called, replicate a bit of a hack to propagate the 443 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 444 // header search paths with it. Once all systems are overriding this 445 // function, the CC1 flag and this line can be removed. 446 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 447 } 448 449 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 450 ArgStringList &CmdArgs) const { 451 CXXStdlibType Type = GetCXXStdlibType(Args); 452 453 switch (Type) { 454 case ToolChain::CST_Libcxx: 455 CmdArgs.push_back("-lc++"); 456 break; 457 458 case ToolChain::CST_Libstdcxx: 459 CmdArgs.push_back("-lstdc++"); 460 break; 461 } 462 } 463 464 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 465 ArgStringList &CmdArgs) const { 466 CmdArgs.push_back("-lcc_kext"); 467 } 468 469 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 470 ArgStringList &CmdArgs) const { 471 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 472 // (to keep the linker options consistent with gcc and clang itself). 473 if (!isOptimizationLevelFast(Args)) { 474 // Check if -ffast-math or -funsafe-math. 475 Arg *A = 476 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 477 options::OPT_funsafe_math_optimizations, 478 options::OPT_fno_unsafe_math_optimizations); 479 480 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 481 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 482 return false; 483 } 484 // If crtfastmath.o exists add it to the arguments. 485 std::string Path = GetFilePath("crtfastmath.o"); 486 if (Path == "crtfastmath.o") // Not found. 487 return false; 488 489 CmdArgs.push_back(Args.MakeArgString(Path)); 490 return true; 491 } 492 493 SanitizerMask ToolChain::getSupportedSanitizers() const { 494 // Return sanitizers which don't require runtime support and are not 495 // platform dependent. 496 using namespace SanitizerKind; 497 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) | 498 CFICastStrict | UnsignedIntegerOverflow | LocalBounds; 499 if (getTriple().getArch() == llvm::Triple::x86 || 500 getTriple().getArch() == llvm::Triple::x86_64) 501 Res |= CFIICall; 502 return Res; 503 } 504