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