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