blob: 3e7a13f18b2b77a1929de6485eff2a8835cc29c4 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- ToolChain.cpp - Collections of tools for one platform ------------===//
Daniel Dunbar9e2136d2009-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 Dunbar82eb4ce2010-08-23 22:35:37 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000016#include "clang/Driver/DriverDiagnostic.h"
John McCall24fc0de2011-07-06 00:26:06 +000017#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000018#include "clang/Driver/Options.h"
Bob Wilson7f05ca32012-03-21 17:19:12 +000019#include "llvm/ADT/StringSwitch.h"
John McCall24fc0de2011-07-06 00:26:06 +000020#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000021using namespace clang::driver;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000022using namespace clang;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000023
Chandler Carruthd7fa2e02012-01-31 02:21:20 +000024ToolChain::ToolChain(const Driver &D, const llvm::Triple &T)
25 : D(D), Triple(T) {
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000026}
27
28ToolChain::~ToolChain() {
29}
30
Daniel Dunbar083edf72009-12-21 18:54:17 +000031const Driver &ToolChain::getDriver() const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000032 return D;
Daniel Dunbar083edf72009-12-21 18:54:17 +000033}
34
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +000035std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000036 return D.GetFilePath(Name, *this);
Mike Stump11289f42009-09-09 15:08:12 +000037
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000038}
39
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +000040std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000041 return D.GetProgramPath(Name, *this, WantFile);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000042}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +000043
44types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
45 return types::lookupTypeForExtension(Ext);
46}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000047
Daniel Dunbar62123a12010-09-17 00:24:52 +000048bool ToolChain::HasNativeLLVMSupport() const {
49 return false;
50}
51
John McCall24fc0de2011-07-06 00:26:06 +000052void 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;
Ted Kremenekf7639e12012-03-06 20:06:33 +000058 runtime.HasSubscripting = false;
John McCall9de19782011-07-06 01:22:26 +000059 runtime.HasTerminate = false;
John McCall24fc0de2011-07-06 00:26:06 +000060 return;
61
62 case ObjCRuntime::GNU:
63 // Assume a maximal GNU runtime.
64 runtime.HasARC = true;
65 runtime.HasWeak = true;
Ted Kremenekf7639e12012-03-06 20:06:33 +000066 runtime.HasSubscripting = false; // to be added
John McCall9de19782011-07-06 01:22:26 +000067 runtime.HasTerminate = false; // to be added
John McCall24fc0de2011-07-06 00:26:06 +000068 return;
69 }
70 llvm_unreachable("invalid runtime kind!");
71}
72
Chris Lattner57540c52011-04-15 05:22:18 +000073/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000074//
75// FIXME: tblgen this.
76static const char *getARMTargetCPU(const ArgList &Args,
77 const llvm::Triple &Triple) {
Bob Wilsoncc4ab9d2012-03-21 16:31:37 +000078 // 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);
86 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000087
Chris Lattner0e62c1c2011-07-23 10:55:15 +000088 StringRef MArch;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000089 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);
92 } else {
93 // Otherwise, use the Arch from the triple.
94 MArch = Triple.getArchName();
95 }
96
Bob Wilson7f05ca32012-03-21 17:19:12 +000097 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");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000118}
119
120/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
121/// CPU.
122//
123// FIXME: This is redundant with -mcpu, why does LLVM use this.
124// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000125static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Bob Wilson7f05ca32012-03-21 17:19:12 +0000126 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-m0", "v6m")
141 .Default("");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000142}
143
Chad Rosierd3a0f952011-09-20 20:44:06 +0000144std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
145 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000146 switch (getTriple().getArch()) {
147 default:
148 return getTripleString();
149
150 case llvm::Triple::arm:
151 case llvm::Triple::thumb: {
152 // FIXME: Factor into subclasses.
153 llvm::Triple Triple = getTriple();
154
155 // Thumb2 is the default for V7 on Darwin.
156 //
157 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000158 StringRef Suffix =
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000159 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilson6524dd32011-10-14 05:03:44 +0000160 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin());
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000161 std::string ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000162
163 // Assembly files should start in ARM mode.
164 if (InputType != types::TY_PP_Asm &&
165 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000166 ArchName = "thumb";
167 Triple.setArchName(ArchName + Suffix.str());
168
169 return Triple.getTriple();
170 }
171 }
172}
173
Chad Rosierd3a0f952011-09-20 20:44:06 +0000174std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
175 types::ID InputType) const {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000176 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000177 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000178 options::OPT_miphoneos_version_min_EQ,
179 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000180 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000181 << A->getAsString(Args);
182
Chad Rosierd3a0f952011-09-20 20:44:06 +0000183 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000184}
185
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000186void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
187 ArgStringList &CC1Args) const {
188 // Each toolchain should provide the appropriate include flags.
189}
190
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000191ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
192 const ArgList &Args) const
193{
194 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
195 StringRef Value = A->getValue(Args);
196 if (Value == "compiler-rt")
197 return ToolChain::RLT_CompilerRT;
198 if (Value == "libgcc")
199 return ToolChain::RLT_Libgcc;
200 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
201 << A->getAsString(Args);
202 }
203
204 return GetDefaultRuntimeLibType();
205}
206
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000207ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000208 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000209 StringRef Value = A->getValue(Args);
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000210 if (Value == "libc++")
211 return ToolChain::CST_Libcxx;
212 if (Value == "libstdc++")
213 return ToolChain::CST_Libstdcxx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000214 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000215 << A->getAsString(Args);
216 }
217
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000218 return ToolChain::CST_Libstdcxx;
219}
220
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000221/// \brief Utility function to add a system include directory to CC1 arguments.
222/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
223 ArgStringList &CC1Args,
224 const Twine &Path) {
225 CC1Args.push_back("-internal-isystem");
226 CC1Args.push_back(DriverArgs.MakeArgString(Path));
227}
228
229/// \brief Utility function to add a system include directory with extern "C"
230/// semantics to CC1 arguments.
231///
232/// Note that this should be used rarely, and only for directories that
233/// historically and for legacy reasons are treated as having implicit extern
234/// "C" semantics. These semantics are *ignored* by and large today, but its
235/// important to preserve the preprocessor changes resulting from the
236/// classification.
237/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
238 ArgStringList &CC1Args,
239 const Twine &Path) {
240 CC1Args.push_back("-internal-externc-isystem");
241 CC1Args.push_back(DriverArgs.MakeArgString(Path));
242}
243
244/// \brief Utility function to add a list of system include directories to CC1.
245/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
246 ArgStringList &CC1Args,
247 ArrayRef<StringRef> Paths) {
248 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
249 I != E; ++I) {
250 CC1Args.push_back("-internal-isystem");
251 CC1Args.push_back(DriverArgs.MakeArgString(*I));
252 }
253}
254
Chandler Carruth814db372011-11-04 23:49:01 +0000255void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
256 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000257 // Header search paths should be handled by each of the subclasses.
258 // Historically, they have not been, and instead have been handled inside of
259 // the CC1-layer frontend. As the logic is hoisted out, this generic function
260 // will slowly stop being called.
261 //
262 // While it is being called, replicate a bit of a hack to propagate the
263 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
264 // header search paths with it. Once all systems are overriding this
265 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000266 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000267}
268
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000269void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
270 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000271 CXXStdlibType Type = GetCXXStdlibType(Args);
272
273 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000274 case ToolChain::CST_Libcxx:
275 CmdArgs.push_back("-lc++");
276 break;
277
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000278 case ToolChain::CST_Libstdcxx:
279 CmdArgs.push_back("-lstdc++");
280 break;
281 }
282}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000283
284void ToolChain::AddCCKextLibArgs(const ArgList &Args,
285 ArgStringList &CmdArgs) const {
286 CmdArgs.push_back("-lcc_kext");
287}