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