1 //===--- ToolChain.cpp - Collections of tools for one platform ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/ToolChain.h"
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"
21 using namespace clang::driver;
22 using namespace clang;
24 ToolChain::ToolChain(const HostInfo &H, const llvm::Triple &T)
25 : Host(H), Triple(T) {
28 ToolChain::~ToolChain() {
31 const Driver &ToolChain::getDriver() const {
32 return Host.getDriver();
35 std::string ToolChain::GetFilePath(const char *Name) const {
36 return Host.getDriver().GetFilePath(Name, *this);
40 std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const {
41 return Host.getDriver().GetProgramPath(Name, *this, WantFile);
44 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
45 return types::lookupTypeForExtension(Ext);
48 bool ToolChain::HasNativeLLVMSupport() const {
52 void 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;
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
68 llvm_unreachable("invalid runtime kind!");
71 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
73 // FIXME: tblgen this.
74 static const char *getARMTargetCPU(const ArgList &Args,
75 const llvm::Triple &Triple) {
76 // FIXME: Warn on inconsistent use of -mcpu and -march.
78 // If we have -mcpu=, use that.
79 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
80 return A->getValue(Args);
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);
87 // Otherwise, use the Arch from the triple.
88 MArch = Triple.getArchName();
91 if (MArch == "armv2" || MArch == "armv2a")
95 if (MArch == "armv3m")
97 if (MArch == "armv4" || MArch == "armv4t")
99 if (MArch == "armv5" || MArch == "armv5t")
101 if (MArch == "armv5e" || MArch == "armv5te")
103 if (MArch == "armv5tej")
105 if (MArch == "armv6" || MArch == "armv6k")
106 return "arm1136jf-s";
107 if (MArch == "armv6j")
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")
115 if (MArch == "armv7r" || MArch == "armv7-r")
117 if (MArch == "armv7m" || MArch == "armv7-m")
119 if (MArch == "ep9312")
121 if (MArch == "iwmmxt")
123 if (MArch == "xscale")
125 if (MArch == "armv6m" || MArch == "armv6-m")
128 // If all else failed, return the most base CPU LLVM supports.
132 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
135 // FIXME: This is redundant with -mcpu, why does LLVM use this.
136 // FIXME: tblgen this, or kill it!
137 static 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")
144 if (CPU == "arm10tdmi" || CPU == "arm1020t")
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" ||
153 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
154 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
157 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
160 if (CPU == "cortex-a8" || CPU == "cortex-a9")
163 if (CPU == "cortex-m3")
166 if (CPU == "cortex-m0")
172 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
173 types::ID InputType) const {
174 switch (getTriple().getArch()) {
176 return getTripleString();
178 case llvm::Triple::arm:
179 case llvm::Triple::thumb: {
180 // FIXME: Factor into subclasses.
181 llvm::Triple Triple = getTriple();
183 // Thumb2 is the default for V7 on Darwin.
185 // FIXME: Thumb should just be another -target-feaure, not in the triple.
187 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
188 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin());
189 std::string ArchName = "arm";
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))
195 Triple.setArchName(ArchName + Suffix.str());
197 return Triple.getTriple();
202 std::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);
211 return ComputeLLVMTriple(Args, InputType);
214 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
215 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
216 StringRef Value = A->getValue(Args);
217 if (Value == "libc++")
218 return ToolChain::CST_Libcxx;
219 if (Value == "libstdc++")
220 return ToolChain::CST_Libstdcxx;
221 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
222 << A->getAsString(Args);
225 return ToolChain::CST_Libstdcxx;
228 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
229 ArgStringList &CmdArgs,
230 bool ObjCXXAutoRefCount) const {
231 CXXStdlibType Type = GetCXXStdlibType(Args);
233 // Header search paths are handled by the mass of goop in InitHeaderSearch.
236 case ToolChain::CST_Libcxx:
237 if (ObjCXXAutoRefCount)
238 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
241 case ToolChain::CST_Libstdcxx:
242 if (ObjCXXAutoRefCount)
243 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
248 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
249 ArgStringList &CmdArgs) const {
250 CXXStdlibType Type = GetCXXStdlibType(Args);
253 case ToolChain::CST_Libcxx:
254 CmdArgs.push_back("-lc++");
257 case ToolChain::CST_Libstdcxx:
258 CmdArgs.push_back("-lstdc++");
263 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
264 ArgStringList &CmdArgs) const {
265 CmdArgs.push_back("-lcc_kext");