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