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/StringSwitch.h" 19 #include "llvm/Option/Arg.h" 20 #include "llvm/Option/ArgList.h" 21 #include "llvm/Option/Option.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/FileSystem.h" 24 using namespace clang::driver; 25 using namespace clang; 26 using namespace llvm::opt; 27 28 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T, 29 const ArgList &A) 30 : D(D), Triple(T), Args(A) { 31 } 32 33 ToolChain::~ToolChain() { 34 } 35 36 const Driver &ToolChain::getDriver() const { 37 return D; 38 } 39 40 bool ToolChain::useIntegratedAs() const { 41 return Args.hasFlag(options::OPT_integrated_as, 42 options::OPT_no_integrated_as, 43 IsIntegratedAssemblerDefault()); 44 } 45 46 const SanitizerArgs& ToolChain::getSanitizerArgs() const { 47 if (!SanitizerArguments.get()) 48 SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 49 return *SanitizerArguments.get(); 50 } 51 52 std::string ToolChain::getDefaultUniversalArchName() const { 53 // In universal driver terms, the arch name accepted by -arch isn't exactly 54 // the same as the ones that appear in the triple. Roughly speaking, this is 55 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the 56 // only interesting special case is powerpc. 57 switch (Triple.getArch()) { 58 case llvm::Triple::ppc: 59 return "ppc"; 60 case llvm::Triple::ppc64: 61 return "ppc64"; 62 case llvm::Triple::ppc64le: 63 return "ppc64le"; 64 default: 65 return Triple.getArchName(); 66 } 67 } 68 69 bool ToolChain::IsUnwindTablesDefault() const { 70 return false; 71 } 72 73 Tool *ToolChain::getClang() const { 74 if (!Clang) 75 Clang.reset(new tools::Clang(*this)); 76 return Clang.get(); 77 } 78 79 Tool *ToolChain::buildAssembler() const { 80 return new tools::ClangAs(*this); 81 } 82 83 Tool *ToolChain::buildLinker() const { 84 llvm_unreachable("Linking is not supported by this toolchain"); 85 } 86 87 Tool *ToolChain::getAssemble() const { 88 if (!Assemble) 89 Assemble.reset(buildAssembler()); 90 return Assemble.get(); 91 } 92 93 Tool *ToolChain::getClangAs() const { 94 if (!Assemble) 95 Assemble.reset(new tools::ClangAs(*this)); 96 return Assemble.get(); 97 } 98 99 Tool *ToolChain::getLink() const { 100 if (!Link) 101 Link.reset(buildLinker()); 102 return Link.get(); 103 } 104 105 Tool *ToolChain::getTool(Action::ActionClass AC) const { 106 switch (AC) { 107 case Action::AssembleJobClass: 108 return getAssemble(); 109 110 case Action::LinkJobClass: 111 return getLink(); 112 113 case Action::InputClass: 114 case Action::BindArchClass: 115 case Action::LipoJobClass: 116 case Action::DsymutilJobClass: 117 case Action::VerifyJobClass: 118 llvm_unreachable("Invalid tool kind."); 119 120 case Action::CompileJobClass: 121 case Action::PrecompileJobClass: 122 case Action::PreprocessJobClass: 123 case Action::AnalyzeJobClass: 124 case Action::MigrateJobClass: 125 return getClang(); 126 } 127 128 llvm_unreachable("Invalid tool kind."); 129 } 130 131 Tool *ToolChain::SelectTool(const JobAction &JA) const { 132 if (getDriver().ShouldUseClangCompiler(JA)) 133 return getClang(); 134 Action::ActionClass AC = JA.getKind(); 135 if (AC == Action::AssembleJobClass && useIntegratedAs()) 136 return getClangAs(); 137 return getTool(AC); 138 } 139 140 std::string ToolChain::GetFilePath(const char *Name) const { 141 return D.GetFilePath(Name, *this); 142 143 } 144 145 std::string ToolChain::GetProgramPath(const char *Name) const { 146 return D.GetProgramPath(Name, *this); 147 } 148 149 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const { 150 return types::lookupTypeForExtension(Ext); 151 } 152 153 bool ToolChain::HasNativeLLVMSupport() const { 154 return false; 155 } 156 157 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 158 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 159 VersionTuple()); 160 } 161 162 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting. 163 // 164 // FIXME: tblgen this. 165 static const char *getARMTargetCPU(const ArgList &Args, 166 const llvm::Triple &Triple) { 167 // For Darwin targets, the -arch option (which is translated to a 168 // corresponding -march option) should determine the architecture 169 // (and the Mach-O slice) regardless of any -mcpu options. 170 if (!Triple.isOSDarwin()) { 171 // FIXME: Warn on inconsistent use of -mcpu and -march. 172 // If we have -mcpu=, use that. 173 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 174 return A->getValue(); 175 } 176 177 StringRef MArch; 178 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 179 // Otherwise, if we have -march= choose the base CPU for that arch. 180 MArch = A->getValue(); 181 } else { 182 // Otherwise, use the Arch from the triple. 183 MArch = Triple.getArchName(); 184 } 185 186 const char *result = llvm::StringSwitch<const char *>(MArch) 187 .Cases("armv2", "armv2a","arm2") 188 .Case("armv3", "arm6") 189 .Case("armv3m", "arm7m") 190 .Case("armv4", "strongarm") 191 .Case("armv4t", "arm7tdmi") 192 .Cases("armv5", "armv5t", "arm10tdmi") 193 .Cases("armv5e", "armv5te", "arm1026ejs") 194 .Case("armv5tej", "arm926ej-s") 195 .Cases("armv6", "armv6k", "arm1136jf-s") 196 .Case("armv6j", "arm1136j-s") 197 .Cases("armv6z", "armv6zk", "arm1176jzf-s") 198 .Case("armv6t2", "arm1156t2-s") 199 .Cases("armv6m", "armv6-m", "cortex-m0") 200 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8") 201 .Cases("armv7l", "armv7-l", "cortex-a8") 202 .Cases("armv7f", "armv7-f", "cortex-a9-mp") 203 .Cases("armv7s", "armv7-s", "swift") 204 .Cases("armv7r", "armv7-r", "cortex-r4") 205 .Cases("armv7m", "armv7-m", "cortex-m3") 206 .Cases("armv7em", "armv7e-m", "cortex-m4") 207 .Cases("armv8", "armv8a", "armv8-a", "cortex-a53") 208 .Case("ep9312", "ep9312") 209 .Case("iwmmxt", "iwmmxt") 210 .Case("xscale", "xscale") 211 // If all else failed, return the most base CPU with thumb interworking 212 // supported by LLVM. 213 .Default(0); 214 215 if (result) 216 return result; 217 218 return 219 Triple.getEnvironment() == llvm::Triple::GNUEABIHF 220 ? "arm1176jzf-s" 221 : "arm7tdmi"; 222 } 223 224 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular 225 /// CPU. 226 // 227 // FIXME: This is redundant with -mcpu, why does LLVM use this. 228 // FIXME: tblgen this, or kill it! 229 static const char *getLLVMArchSuffixForARM(StringRef CPU) { 230 return llvm::StringSwitch<const char *>(CPU) 231 .Case("strongarm", "v4") 232 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t") 233 .Cases("arm720t", "arm9", "arm9tdmi", "v4t") 234 .Cases("arm920", "arm920t", "arm922t", "v4t") 235 .Cases("arm940t", "ep9312","v4t") 236 .Cases("arm10tdmi", "arm1020t", "v5") 237 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e") 238 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e") 239 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e") 240 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6") 241 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6") 242 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2") 243 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7") 244 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "v7") 245 .Cases("cortex-r4", "cortex-r5", "v7r") 246 .Case("cortex-m0", "v6m") 247 .Case("cortex-m3", "v7m") 248 .Case("cortex-m4", "v7em") 249 .Case("cortex-a9-mp", "v7f") 250 .Case("swift", "v7s") 251 .Cases("cortex-a53", "cortex-a57", "v8") 252 .Default(""); 253 } 254 255 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 256 types::ID InputType) const { 257 switch (getTriple().getArch()) { 258 default: 259 return getTripleString(); 260 261 case llvm::Triple::x86_64: { 262 llvm::Triple Triple = getTriple(); 263 if (!Triple.isOSDarwin()) 264 return getTripleString(); 265 266 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 267 // x86_64h goes in the triple. Other -march options just use the 268 // vanilla triple we already have. 269 StringRef MArch = A->getValue(); 270 if (MArch == "x86_64h") 271 Triple.setArchName(MArch); 272 } 273 return Triple.getTriple(); 274 } 275 case llvm::Triple::arm: 276 case llvm::Triple::thumb: { 277 // FIXME: Factor into subclasses. 278 llvm::Triple Triple = getTriple(); 279 280 // Thumb2 is the default for V7 on Darwin. 281 // 282 // FIXME: Thumb should just be another -target-feaure, not in the triple. 283 #if 1 284 // Minix/ARM-specific force to ARMv7 and EABI. 285 StringRef Suffix = "v7"; 286 Triple.setEnvironment(llvm::Triple::EABI); 287 #else 288 StringRef Suffix = 289 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple)); 290 #endif 291 bool ThumbDefault = Suffix.startswith("v6m") || 292 (Suffix.startswith("v7") && getTriple().isOSDarwin()); 293 std::string ArchName = "arm"; 294 295 // Assembly files should start in ARM mode. 296 if (InputType != types::TY_PP_Asm && 297 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault)) 298 ArchName = "thumb"; 299 Triple.setArchName(ArchName + Suffix.str()); 300 301 return Triple.getTriple(); 302 } 303 } 304 } 305 306 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 307 types::ID InputType) const { 308 // Diagnose use of Darwin OS deployment target arguments on non-Darwin. 309 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ, 310 options::OPT_miphoneos_version_min_EQ, 311 options::OPT_mios_simulator_version_min_EQ)) 312 getDriver().Diag(diag::err_drv_clang_unsupported) 313 << A->getAsString(Args); 314 315 return ComputeLLVMTriple(Args, InputType); 316 } 317 318 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 319 ArgStringList &CC1Args) const { 320 // Each toolchain should provide the appropriate include flags. 321 } 322 323 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 324 ArgStringList &CC1Args) const { 325 } 326 327 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 328 const ArgList &Args) const 329 { 330 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) { 331 StringRef Value = A->getValue(); 332 if (Value == "compiler-rt") 333 return ToolChain::RLT_CompilerRT; 334 if (Value == "libgcc") 335 return ToolChain::RLT_Libgcc; 336 getDriver().Diag(diag::err_drv_invalid_rtlib_name) 337 << A->getAsString(Args); 338 } 339 340 return GetDefaultRuntimeLibType(); 341 } 342 343 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 344 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 345 StringRef Value = A->getValue(); 346 if (Value == "libc++") 347 return ToolChain::CST_Libcxx; 348 if (Value == "libstdc++") 349 return ToolChain::CST_Libstdcxx; 350 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 351 << A->getAsString(Args); 352 } 353 354 return ToolChain::CST_Libstdcxx; 355 } 356 357 /// \brief Utility function to add a system include directory to CC1 arguments. 358 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 359 ArgStringList &CC1Args, 360 const Twine &Path) { 361 CC1Args.push_back("-internal-isystem"); 362 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 363 } 364 365 /// \brief Utility function to add a system include directory with extern "C" 366 /// semantics to CC1 arguments. 367 /// 368 /// Note that this should be used rarely, and only for directories that 369 /// historically and for legacy reasons are treated as having implicit extern 370 /// "C" semantics. These semantics are *ignored* by and large today, but its 371 /// important to preserve the preprocessor changes resulting from the 372 /// classification. 373 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 374 ArgStringList &CC1Args, 375 const Twine &Path) { 376 CC1Args.push_back("-internal-externc-isystem"); 377 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 378 } 379 380 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 381 ArgStringList &CC1Args, 382 const Twine &Path) { 383 if (llvm::sys::fs::exists(Path)) 384 addExternCSystemInclude(DriverArgs, CC1Args, Path); 385 } 386 387 /// \brief Utility function to add a list of system include directories to CC1. 388 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 389 ArgStringList &CC1Args, 390 ArrayRef<StringRef> Paths) { 391 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end(); 392 I != E; ++I) { 393 CC1Args.push_back("-internal-isystem"); 394 CC1Args.push_back(DriverArgs.MakeArgString(*I)); 395 } 396 } 397 398 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 399 ArgStringList &CC1Args) const { 400 // Header search paths should be handled by each of the subclasses. 401 // Historically, they have not been, and instead have been handled inside of 402 // the CC1-layer frontend. As the logic is hoisted out, this generic function 403 // will slowly stop being called. 404 // 405 // While it is being called, replicate a bit of a hack to propagate the 406 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 407 // header search paths with it. Once all systems are overriding this 408 // function, the CC1 flag and this line can be removed. 409 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 410 } 411 412 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 413 ArgStringList &CmdArgs) const { 414 CXXStdlibType Type = GetCXXStdlibType(Args); 415 416 switch (Type) { 417 case ToolChain::CST_Libcxx: 418 CmdArgs.push_back("-lc++"); 419 break; 420 421 case ToolChain::CST_Libstdcxx: 422 CmdArgs.push_back("-lstdc++"); 423 break; 424 } 425 } 426 427 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 428 ArgStringList &CmdArgs) const { 429 CmdArgs.push_back("-lcc_kext"); 430 } 431 432 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 433 ArgStringList &CmdArgs) const { 434 // Check if -ffast-math or -funsafe-math is enabled. 435 Arg *A = Args.getLastArg(options::OPT_ffast_math, 436 options::OPT_fno_fast_math, 437 options::OPT_funsafe_math_optimizations, 438 options::OPT_fno_unsafe_math_optimizations); 439 440 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 441 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 442 return false; 443 444 // If crtfastmath.o exists add it to the arguments. 445 std::string Path = GetFilePath("crtfastmath.o"); 446 if (Path == "crtfastmath.o") // Not found. 447 return false; 448 449 CmdArgs.push_back(Args.MakeArgString(Path)); 450 return true; 451 } 452