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/ObjCRuntime.h"
18 #include "clang/Driver/Options.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Support/ErrorHandling.h"
21 using namespace clang::driver;
22 using namespace clang;
24 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T)
28 ToolChain::~ToolChain() {
31 const Driver &ToolChain::getDriver() const {
35 std::string ToolChain::GetFilePath(const char *Name) const {
36 return D.GetFilePath(Name, *this);
40 std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const {
41 return D.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.HasSubscripting = false;
59 runtime.HasTerminate = false;
62 case ObjCRuntime::GNU:
63 // Assume a maximal GNU runtime.
64 runtime.HasARC = true;
65 runtime.HasWeak = true;
66 runtime.HasSubscripting = false; // to be added
67 runtime.HasTerminate = false; // to be added
70 llvm_unreachable("invalid runtime kind!");
73 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
75 // FIXME: tblgen this.
76 static const char *getARMTargetCPU(const ArgList &Args,
77 const llvm::Triple &Triple) {
78 // For Darwin targets, the -arch option (which is translated to a
79 // corresponding -march option) should determine the architecture
80 // (and the Mach-O slice) regardless of any -mcpu options.
81 if (!Triple.isOSDarwin()) {
82 // FIXME: Warn on inconsistent use of -mcpu and -march.
83 // If we have -mcpu=, use that.
84 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
85 return A->getValue(Args);
89 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
90 // Otherwise, if we have -march= choose the base CPU for that arch.
91 MArch = A->getValue(Args);
93 // Otherwise, use the Arch from the triple.
94 MArch = Triple.getArchName();
97 return llvm::StringSwitch<const char *>(MArch)
98 .Cases("armv2", "armv2a","arm2")
99 .Case("armv3", "arm6")
100 .Case("armv3m", "arm7m")
101 .Cases("armv4", "armv4t", "arm7tdmi")
102 .Cases("armv5", "armv5t", "arm10tdmi")
103 .Cases("armv5e", "armv5te", "arm1026ejs")
104 .Case("armv5tej", "arm926ej-s")
105 .Cases("armv6", "armv6k", "arm1136jf-s")
106 .Case("armv6j", "arm1136j-s")
107 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
108 .Case("armv6t2", "arm1156t2-s")
109 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
110 .Cases("armv7r", "armv7-r", "cortex-r4")
111 .Cases("armv7m", "armv7-m", "cortex-m3")
112 .Case("ep9312", "ep9312")
113 .Case("iwmmxt", "iwmmxt")
114 .Case("xscale", "xscale")
115 .Cases("armv6m", "armv6-m", "cortex-m0")
116 // If all else failed, return the most base CPU LLVM supports.
117 .Default("arm7tdmi");
120 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
123 // FIXME: This is redundant with -mcpu, why does LLVM use this.
124 // FIXME: tblgen this, or kill it!
125 static const char *getLLVMArchSuffixForARM(StringRef CPU) {
126 return llvm::StringSwitch<const char *>(CPU)
127 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
128 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
129 .Cases("arm920", "arm920t", "arm922t", "v4t")
130 .Cases("arm940t", "ep9312","v4t")
131 .Cases("arm10tdmi", "arm1020t", "v5")
132 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
133 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
134 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
135 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
136 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
137 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
138 .Cases("cortex-a8", "cortex-a9", "v7")
139 .Case("cortex-m3", "v7m")
140 .Case("cortex-m4", "v7m")
141 .Case("cortex-m0", "v6m")
145 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
146 types::ID InputType) const {
147 switch (getTriple().getArch()) {
149 return getTripleString();
151 case llvm::Triple::arm:
152 case llvm::Triple::thumb: {
153 // FIXME: Factor into subclasses.
154 llvm::Triple Triple = getTriple();
156 // Thumb2 is the default for V7 on Darwin.
158 // FIXME: Thumb should just be another -target-feaure, not in the triple.
160 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
161 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin());
162 std::string ArchName = "arm";
164 // Assembly files should start in ARM mode.
165 if (InputType != types::TY_PP_Asm &&
166 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
168 Triple.setArchName(ArchName + Suffix.str());
170 return Triple.getTriple();
175 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
176 types::ID InputType) const {
177 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
178 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
179 options::OPT_miphoneos_version_min_EQ,
180 options::OPT_mios_simulator_version_min_EQ))
181 getDriver().Diag(diag::err_drv_clang_unsupported)
182 << A->getAsString(Args);
184 return ComputeLLVMTriple(Args, InputType);
187 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
188 ArgStringList &CC1Args) const {
189 // Each toolchain should provide the appropriate include flags.
192 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
193 const ArgList &Args) const
195 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
196 StringRef Value = A->getValue(Args);
197 if (Value == "compiler-rt")
198 return ToolChain::RLT_CompilerRT;
199 if (Value == "libgcc")
200 return ToolChain::RLT_Libgcc;
201 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
202 << A->getAsString(Args);
205 return GetDefaultRuntimeLibType();
208 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
209 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
210 StringRef Value = A->getValue(Args);
211 if (Value == "libc++")
212 return ToolChain::CST_Libcxx;
213 if (Value == "libstdc++")
214 return ToolChain::CST_Libstdcxx;
215 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
216 << A->getAsString(Args);
219 return ToolChain::CST_Libstdcxx;
222 /// \brief Utility function to add a system include directory to CC1 arguments.
223 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
224 ArgStringList &CC1Args,
226 CC1Args.push_back("-internal-isystem");
227 CC1Args.push_back(DriverArgs.MakeArgString(Path));
230 /// \brief Utility function to add a system include directory with extern "C"
231 /// semantics to CC1 arguments.
233 /// Note that this should be used rarely, and only for directories that
234 /// historically and for legacy reasons are treated as having implicit extern
235 /// "C" semantics. These semantics are *ignored* by and large today, but its
236 /// important to preserve the preprocessor changes resulting from the
238 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
239 ArgStringList &CC1Args,
241 CC1Args.push_back("-internal-externc-isystem");
242 CC1Args.push_back(DriverArgs.MakeArgString(Path));
245 /// \brief Utility function to add a list of system include directories to CC1.
246 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
247 ArgStringList &CC1Args,
248 ArrayRef<StringRef> Paths) {
249 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
251 CC1Args.push_back("-internal-isystem");
252 CC1Args.push_back(DriverArgs.MakeArgString(*I));
256 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
257 ArgStringList &CC1Args) const {
258 // Header search paths should be handled by each of the subclasses.
259 // Historically, they have not been, and instead have been handled inside of
260 // the CC1-layer frontend. As the logic is hoisted out, this generic function
261 // will slowly stop being called.
263 // While it is being called, replicate a bit of a hack to propagate the
264 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
265 // header search paths with it. Once all systems are overriding this
266 // function, the CC1 flag and this line can be removed.
267 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
270 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
271 ArgStringList &CmdArgs) const {
272 CXXStdlibType Type = GetCXXStdlibType(Args);
275 case ToolChain::CST_Libcxx:
276 CmdArgs.push_back("-lc++");
279 case ToolChain::CST_Libstdcxx:
280 CmdArgs.push_back("-lstdc++");
285 void ToolChain::AddCCKextLibArgs(const ArgList &Args,
286 ArgStringList &CmdArgs) const {
287 CmdArgs.push_back("-lcc_kext");