ToolChain.cpp revision 79cbbdc8affe52591f7ee487a789639aa38331ec
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 "clang/Driver/ToolChain.h" 11 12#include "clang/Driver/Action.h" 13#include "clang/Driver/Arg.h" 14#include "clang/Driver/ArgList.h" 15#include "clang/Driver/Driver.h" 16#include "clang/Driver/DriverDiagnostic.h" 17#include "clang/Driver/HostInfo.h" 18#include "clang/Driver/ObjCRuntime.h" 19#include "clang/Driver/Options.h" 20#include "llvm/Support/ErrorHandling.h" 21using namespace clang::driver; 22using namespace clang; 23 24ToolChain::ToolChain(const HostInfo &H, const llvm::Triple &T) 25 : Host(H), Triple(T) { 26} 27 28ToolChain::~ToolChain() { 29} 30 31const Driver &ToolChain::getDriver() const { 32 return Host.getDriver(); 33} 34 35std::string ToolChain::GetFilePath(const char *Name) const { 36 return Host.getDriver().GetFilePath(Name, *this); 37 38} 39 40std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const { 41 return Host.getDriver().GetProgramPath(Name, *this, WantFile); 42} 43 44types::ID ToolChain::LookupTypeForExtension(const char *Ext) const { 45 return types::lookupTypeForExtension(Ext); 46} 47 48bool ToolChain::HasNativeLLVMSupport() const { 49 return false; 50} 51 52void ToolChain::configureObjCRuntime(ObjCRuntime &runtime) const { 53 switch (runtime.getKind()) { 54 case ObjCRuntime::NeXT: 55 // Assume a minimal NeXT runtime. 56 runtime.HasARC = false; 57 runtime.HasWeak = false; 58 runtime.HasTerminate = false; 59 return; 60 61 case ObjCRuntime::GNU: 62 // Assume a maximal GNU runtime. 63 runtime.HasARC = true; 64 runtime.HasWeak = true; 65 runtime.HasTerminate = false; // to be added 66 return; 67 } 68 llvm_unreachable("invalid runtime kind!"); 69} 70 71/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting. 72// 73// FIXME: tblgen this. 74static const char *getARMTargetCPU(const ArgList &Args, 75 const llvm::Triple &Triple) { 76 // FIXME: Warn on inconsistent use of -mcpu and -march. 77 78 // If we have -mcpu=, use that. 79 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 80 return A->getValue(Args); 81 82 StringRef MArch; 83 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 84 // Otherwise, if we have -march= choose the base CPU for that arch. 85 MArch = A->getValue(Args); 86 } else { 87 // Otherwise, use the Arch from the triple. 88 MArch = Triple.getArchName(); 89 } 90 91 if (MArch == "armv2" || MArch == "armv2a") 92 return "arm2"; 93 if (MArch == "armv3") 94 return "arm6"; 95 if (MArch == "armv3m") 96 return "arm7m"; 97 if (MArch == "armv4" || MArch == "armv4t") 98 return "arm7tdmi"; 99 if (MArch == "armv5" || MArch == "armv5t") 100 return "arm10tdmi"; 101 if (MArch == "armv5e" || MArch == "armv5te") 102 return "arm1026ejs"; 103 if (MArch == "armv5tej") 104 return "arm926ej-s"; 105 if (MArch == "armv6" || MArch == "armv6k") 106 return "arm1136jf-s"; 107 if (MArch == "armv6j") 108 return "arm1136j-s"; 109 if (MArch == "armv6z" || MArch == "armv6zk") 110 return "arm1176jzf-s"; 111 if (MArch == "armv6t2") 112 return "arm1156t2-s"; 113 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a") 114 return "cortex-a8"; 115 if (MArch == "armv7r" || MArch == "armv7-r") 116 return "cortex-r4"; 117 if (MArch == "armv7m" || MArch == "armv7-m") 118 return "cortex-m3"; 119 if (MArch == "ep9312") 120 return "ep9312"; 121 if (MArch == "iwmmxt") 122 return "iwmmxt"; 123 if (MArch == "xscale") 124 return "xscale"; 125 if (MArch == "armv6m" || MArch == "armv6-m") 126 return "cortex-m0"; 127 128 // If all else failed, return the most base CPU LLVM supports. 129 return "arm7tdmi"; 130} 131 132/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular 133/// CPU. 134// 135// FIXME: This is redundant with -mcpu, why does LLVM use this. 136// FIXME: tblgen this, or kill it! 137static const char *getLLVMArchSuffixForARM(StringRef CPU) { 138 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" || 139 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" || 140 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" || 141 CPU == "arm940t" || CPU == "ep9312") 142 return "v4t"; 143 144 if (CPU == "arm10tdmi" || CPU == "arm1020t") 145 return "v5"; 146 147 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" || 148 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" || 149 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" || 150 CPU == "iwmmxt") 151 return "v5e"; 152 153 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" || 154 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore") 155 return "v6"; 156 157 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s") 158 return "v6t2"; 159 160 if (CPU == "cortex-a8" || CPU == "cortex-a9") 161 return "v7"; 162 163 if (CPU == "cortex-m3") 164 return "v7m"; 165 166 if (CPU == "cortex-m0") 167 return "v6m"; 168 169 return ""; 170} 171 172std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 173 types::ID InputType) const { 174 switch (getTriple().getArch()) { 175 default: 176 return getTripleString(); 177 178 case llvm::Triple::arm: 179 case llvm::Triple::thumb: { 180 // FIXME: Factor into subclasses. 181 llvm::Triple Triple = getTriple(); 182 183 // Thumb2 is the default for V7 on Darwin. 184 // 185 // FIXME: Thumb should just be another -target-feaure, not in the triple. 186 StringRef Suffix = 187 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple)); 188 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin()); 189 std::string ArchName = "arm"; 190 191 // Assembly files should start in ARM mode. 192 if (InputType != types::TY_PP_Asm && 193 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault)) 194 ArchName = "thumb"; 195 Triple.setArchName(ArchName + Suffix.str()); 196 197 return Triple.getTriple(); 198 } 199 } 200} 201 202std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 203 types::ID InputType) const { 204 // Diagnose use of Darwin OS deployment target arguments on non-Darwin. 205 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ, 206 options::OPT_miphoneos_version_min_EQ, 207 options::OPT_mios_simulator_version_min_EQ)) 208 getDriver().Diag(diag::err_drv_clang_unsupported) 209 << A->getAsString(Args); 210 211 return ComputeLLVMTriple(Args, InputType); 212} 213 214void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 215 ArgStringList &CC1Args) const { 216 // Each toolchain should provide the appropriate include flags. 217} 218 219ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 220 const ArgList &Args) const 221{ 222 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) { 223 StringRef Value = A->getValue(Args); 224 if (Value == "compiler-rt") 225 return ToolChain::RLT_CompilerRT; 226 if (Value == "libgcc") 227 return ToolChain::RLT_Libgcc; 228 getDriver().Diag(diag::err_drv_invalid_rtlib_name) 229 << A->getAsString(Args); 230 } 231 232 return GetDefaultRuntimeLibType(); 233} 234 235ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 236 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 237 StringRef Value = A->getValue(Args); 238 if (Value == "libc++") 239 return ToolChain::CST_Libcxx; 240 if (Value == "libstdc++") 241 return ToolChain::CST_Libstdcxx; 242 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 243 << A->getAsString(Args); 244 } 245 246 return ToolChain::CST_Libstdcxx; 247} 248 249/// \brief Utility function to add a system include directory to CC1 arguments. 250/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 251 ArgStringList &CC1Args, 252 const Twine &Path) { 253 CC1Args.push_back("-internal-isystem"); 254 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 255} 256 257/// \brief Utility function to add a system include directory with extern "C" 258/// semantics to CC1 arguments. 259/// 260/// Note that this should be used rarely, and only for directories that 261/// historically and for legacy reasons are treated as having implicit extern 262/// "C" semantics. These semantics are *ignored* by and large today, but its 263/// important to preserve the preprocessor changes resulting from the 264/// classification. 265/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 266 ArgStringList &CC1Args, 267 const Twine &Path) { 268 CC1Args.push_back("-internal-externc-isystem"); 269 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 270} 271 272/// \brief Utility function to add a list of system include directories to CC1. 273/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 274 ArgStringList &CC1Args, 275 ArrayRef<StringRef> Paths) { 276 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end(); 277 I != E; ++I) { 278 CC1Args.push_back("-internal-isystem"); 279 CC1Args.push_back(DriverArgs.MakeArgString(*I)); 280 } 281} 282 283void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 284 ArgStringList &CC1Args) const { 285 // Header search paths should be handled by each of the subclasses. 286 // Historically, they have not been, and instead have been handled inside of 287 // the CC1-layer frontend. As the logic is hoisted out, this generic function 288 // will slowly stop being called. 289 // 290 // While it is being called, replicate a bit of a hack to propagate the 291 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 292 // header search paths with it. Once all systems are overriding this 293 // function, the CC1 flag and this line can be removed. 294 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 295} 296 297void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 298 ArgStringList &CmdArgs) const { 299 CXXStdlibType Type = GetCXXStdlibType(Args); 300 301 switch (Type) { 302 case ToolChain::CST_Libcxx: 303 CmdArgs.push_back("-lc++"); 304 break; 305 306 case ToolChain::CST_Libstdcxx: 307 CmdArgs.push_back("-lstdc++"); 308 break; 309 } 310} 311 312void ToolChain::AddCCKextLibArgs(const ArgList &Args, 313 ArgStringList &CmdArgs) const { 314 CmdArgs.push_back("-lcc_kext"); 315} 316