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