blob: df09c20c180fc0753d093c1e3fcaa2c2b03928c0 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChain.cpp - Collections of tools for one platform ------------===//
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00002//
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"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000017#include "clang/Driver/Options.h"
Bob Wilson57f6d192012-03-21 17:19:12 +000018#include "llvm/ADT/StringSwitch.h"
John McCall9f084a32011-07-06 00:26:06 +000019#include "llvm/Support/ErrorHandling.h"
John McCall260611a2012-06-20 06:18:46 +000020#include "clang/Basic/ObjCRuntime.h"
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000021using namespace clang::driver;
Chris Lattner5f9e2722011-07-23 10:55:15 +000022using namespace clang;
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000023
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000024ToolChain::ToolChain(const Driver &D, const llvm::Triple &T)
25 : D(D), Triple(T) {
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000026}
27
28ToolChain::~ToolChain() {
29}
30
Daniel Dunbaree788e72009-12-21 18:54:17 +000031const Driver &ToolChain::getDriver() const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000032 return D;
Daniel Dunbaree788e72009-12-21 18:54:17 +000033}
34
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000035std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000036 return D.GetFilePath(Name, *this);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000038}
39
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000040std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000041 return D.GetProgramPath(Name, *this, WantFile);
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000042}
Daniel Dunbar41800112010-08-02 05:43:56 +000043
44types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
45 return types::lookupTypeForExtension(Ext);
46}
Daniel Dunbar00577ad2010-08-23 22:35:37 +000047
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000048bool ToolChain::HasNativeLLVMSupport() const {
49 return false;
50}
51
John McCall260611a2012-06-20 06:18:46 +000052ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnall11d3f4c2012-07-03 20:49:52 +000053 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall260611a2012-06-20 06:18:46 +000054 VersionTuple());
John McCall9f084a32011-07-06 00:26:06 +000055}
56
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000057/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar00577ad2010-08-23 22:35:37 +000058//
59// FIXME: tblgen this.
60static const char *getARMTargetCPU(const ArgList &Args,
61 const llvm::Triple &Triple) {
Bob Wilson677a35b2012-03-21 16:31:37 +000062 // 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 }
Daniel Dunbar00577ad2010-08-23 22:35:37 +000071
Chris Lattner5f9e2722011-07-23 10:55:15 +000072 StringRef MArch;
Daniel Dunbar00577ad2010-08-23 22:35:37 +000073 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
Bob Wilson57f6d192012-03-21 17:19:12 +000081 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");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000102}
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!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000109static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Bob Wilson57f6d192012-03-21 17:19:12 +0000110 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")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000122 .Cases("cortex-a8", "cortex-a9", "cortex-a15", "v7")
Bob Wilson57f6d192012-03-21 17:19:12 +0000123 .Case("cortex-m3", "v7m")
Jim Grosbach69033132012-03-29 19:53:34 +0000124 .Case("cortex-m4", "v7m")
Bob Wilson57f6d192012-03-21 17:19:12 +0000125 .Case("cortex-m0", "v6m")
126 .Default("");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000127}
128
Chad Rosier61ab80a2011-09-20 20:44:06 +0000129std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
130 types::ID InputType) const {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000131 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.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000143 StringRef Suffix =
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000144 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilson905c45f2011-10-14 05:03:44 +0000145 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin());
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000146 std::string ArchName = "arm";
Chad Rosier61ab80a2011-09-20 20:44:06 +0000147
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))
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000151 ArchName = "thumb";
152 Triple.setArchName(ArchName + Suffix.str());
153
154 return Triple.getTriple();
155 }
156 }
157}
158
Chad Rosier61ab80a2011-09-20 20:44:06 +0000159std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
160 types::ID InputType) const {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000161 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000162 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000163 options::OPT_miphoneos_version_min_EQ,
164 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000165 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000166 << A->getAsString(Args);
167
Chad Rosier61ab80a2011-09-20 20:44:06 +0000168 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000169}
170
Chandler Carruth88491fc2011-11-04 07:12:53 +0000171void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
172 ArgStringList &CC1Args) const {
173 // Each toolchain should provide the appropriate include flags.
174}
175
Rafael Espindola8af669f2012-06-19 01:26:10 +0000176void ToolChain::addClangTargetOptions(ArgStringList &CC1Args) const {
177}
178
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000179ToolChain::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
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000195ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000196 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 StringRef Value = A->getValue(Args);
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000198 if (Value == "libc++")
199 return ToolChain::CST_Libcxx;
200 if (Value == "libstdc++")
201 return ToolChain::CST_Libstdcxx;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000202 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000203 << A->getAsString(Args);
204 }
205
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000206 return ToolChain::CST_Libstdcxx;
207}
208
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000209/// \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
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000243void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
244 ArgStringList &CC1Args) const {
Chandler Carrutha4614422011-11-04 07:43:33 +0000245 // 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.
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000254 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000255}
256
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000257void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
258 ArgStringList &CmdArgs) const {
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000259 CXXStdlibType Type = GetCXXStdlibType(Args);
260
261 switch (Type) {
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000262 case ToolChain::CST_Libcxx:
263 CmdArgs.push_back("-lc++");
264 break;
265
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000266 case ToolChain::CST_Libstdcxx:
267 CmdArgs.push_back("-lstdc++");
268 break;
269 }
270}
Shantonu Sen7433fed2010-09-17 18:39:08 +0000271
272void ToolChain::AddCCKextLibArgs(const ArgList &Args,
273 ArgStringList &CmdArgs) const {
274 CmdArgs.push_back("-lcc_kext");
275}