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