blob: 0b62f7aa00f90b0905aeddadada9acb499f797bc [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
Rafael Espindola260e28d2013-03-18 20:48:54 +000010#include "Tools.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000011#include "clang/Basic/ObjCRuntime.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000012#include "clang/Driver/Action.h"
13#include "clang/Driver/Driver.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000014#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000015#include "clang/Driver/Options.h"
Peter Collingbourne32701642013-11-01 18:16:25 +000016#include "clang/Driver/SanitizerArgs.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000017#include "clang/Driver/ToolChain.h"
Bob Wilson7f05ca32012-03-21 17:19:12 +000018#include "llvm/ADT/StringSwitch.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000019#include "llvm/Option/Arg.h"
20#include "llvm/Option/ArgList.h"
21#include "llvm/Option/Option.h"
John McCall24fc0de2011-07-06 00:26:06 +000022#include "llvm/Support/ErrorHandling.h"
Simon Atanasyan08450bd2013-04-20 08:15:03 +000023#include "llvm/Support/FileSystem.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000024using namespace clang::driver;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000025using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000026using namespace llvm::opt;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000027
Rafael Espindola84b588b2013-03-18 18:10:27 +000028ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
29 const ArgList &A)
30 : D(D), Triple(T), Args(A) {
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000031}
32
33ToolChain::~ToolChain() {
34}
35
Daniel Dunbar083edf72009-12-21 18:54:17 +000036const Driver &ToolChain::getDriver() const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000037 return D;
Daniel Dunbar083edf72009-12-21 18:54:17 +000038}
39
Rafael Espindola84b588b2013-03-18 18:10:27 +000040bool ToolChain::useIntegratedAs() const {
Rafael Espindola248e2192013-03-18 17:52:57 +000041 return Args.hasFlag(options::OPT_integrated_as,
42 options::OPT_no_integrated_as,
43 IsIntegratedAssemblerDefault());
44}
45
Alexey Samsonov609213f92013-08-19 09:14:21 +000046const SanitizerArgs& ToolChain::getSanitizerArgs() const {
Peter Collingbourne32701642013-11-01 18:16:25 +000047 if (!SanitizerArguments.get())
48 SanitizerArguments.reset(new SanitizerArgs(*this, Args));
49 return *SanitizerArguments.get();
Alexey Samsonov609213f92013-08-19 09:14:21 +000050}
51
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +000052std::string ToolChain::getDefaultUniversalArchName() const {
53 // In universal driver terms, the arch name accepted by -arch isn't exactly
54 // the same as the ones that appear in the triple. Roughly speaking, this is
55 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
56 // only interesting special case is powerpc.
57 switch (Triple.getArch()) {
58 case llvm::Triple::ppc:
59 return "ppc";
60 case llvm::Triple::ppc64:
61 return "ppc64";
Bill Schmidt778d3872013-07-26 01:36:11 +000062 case llvm::Triple::ppc64le:
63 return "ppc64le";
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +000064 default:
65 return Triple.getArchName();
66 }
67}
68
Rafael Espindola151a9572012-09-23 03:05:41 +000069bool ToolChain::IsUnwindTablesDefault() const {
70 return false;
71}
72
Rafael Espindola7cf32212013-03-20 03:05:54 +000073Tool *ToolChain::getClang() const {
74 if (!Clang)
75 Clang.reset(new tools::Clang(*this));
76 return Clang.get();
77}
78
79Tool *ToolChain::buildAssembler() const {
80 return new tools::ClangAs(*this);
81}
82
83Tool *ToolChain::buildLinker() const {
84 llvm_unreachable("Linking is not supported by this toolchain");
85}
86
87Tool *ToolChain::getAssemble() const {
88 if (!Assemble)
89 Assemble.reset(buildAssembler());
90 return Assemble.get();
91}
92
93Tool *ToolChain::getClangAs() const {
94 if (!Assemble)
95 Assemble.reset(new tools::ClangAs(*this));
96 return Assemble.get();
97}
98
99Tool *ToolChain::getLink() const {
100 if (!Link)
101 Link.reset(buildLinker());
102 return Link.get();
103}
104
105Tool *ToolChain::getTool(Action::ActionClass AC) const {
Rafael Espindolad15a8912013-03-19 00:36:57 +0000106 switch (AC) {
Rafael Espindola7cf32212013-03-20 03:05:54 +0000107 case Action::AssembleJobClass:
108 return getAssemble();
109
110 case Action::LinkJobClass:
111 return getLink();
112
Rafael Espindolad15a8912013-03-19 00:36:57 +0000113 case Action::InputClass:
114 case Action::BindArchClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000115 case Action::LipoJobClass:
116 case Action::DsymutilJobClass:
117 case Action::VerifyJobClass:
118 llvm_unreachable("Invalid tool kind.");
119
120 case Action::CompileJobClass:
121 case Action::PrecompileJobClass:
122 case Action::PreprocessJobClass:
123 case Action::AnalyzeJobClass:
124 case Action::MigrateJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000125 return getClang();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000126 }
Benjamin Kramer5fbe2622013-03-21 19:45:46 +0000127
128 llvm_unreachable("Invalid tool kind.");
Rafael Espindolad15a8912013-03-19 00:36:57 +0000129}
130
Rafael Espindola79764462013-03-24 15:06:53 +0000131Tool *ToolChain::SelectTool(const JobAction &JA) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +0000132 if (getDriver().ShouldUseClangCompiler(JA))
Rafael Espindola79764462013-03-24 15:06:53 +0000133 return getClang();
Rafael Espindola7cf32212013-03-20 03:05:54 +0000134 Action::ActionClass AC = JA.getKind();
135 if (AC == Action::AssembleJobClass && useIntegratedAs())
Rafael Espindola79764462013-03-24 15:06:53 +0000136 return getClangAs();
137 return getTool(AC);
Rafael Espindola260e28d2013-03-18 20:48:54 +0000138}
139
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +0000140std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +0000141 return D.GetFilePath(Name, *this);
Mike Stump11289f42009-09-09 15:08:12 +0000142
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000143}
144
Simon Atanasyanb16488c2012-10-03 19:52:37 +0000145std::string ToolChain::GetProgramPath(const char *Name) const {
146 return D.GetProgramPath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000147}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000148
149types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
150 return types::lookupTypeForExtension(Ext);
151}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000152
Daniel Dunbar62123a12010-09-17 00:24:52 +0000153bool ToolChain::HasNativeLLVMSupport() const {
154 return false;
155}
156
John McCall5fb5df92012-06-20 06:18:46 +0000157ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnallb601c962012-07-03 20:49:52 +0000158 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall5fb5df92012-06-20 06:18:46 +0000159 VersionTuple());
John McCall24fc0de2011-07-06 00:26:06 +0000160}
161
Jim Grosbach82eee262013-11-16 00:53:35 +0000162std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000163 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000164 switch (getTriple().getArch()) {
165 default:
166 return getTripleString();
167
Jim Grosbach82eee262013-11-16 00:53:35 +0000168 case llvm::Triple::x86_64: {
169 llvm::Triple Triple = getTriple();
170 if (!Triple.isOSDarwin())
171 return getTripleString();
172
173 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
174 // x86_64h goes in the triple. Other -march options just use the
175 // vanilla triple we already have.
176 StringRef MArch = A->getValue();
177 if (MArch == "x86_64h")
178 Triple.setArchName(MArch);
179 }
180 return Triple.getTriple();
181 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000182 case llvm::Triple::arm:
183 case llvm::Triple::thumb: {
184 // FIXME: Factor into subclasses.
185 llvm::Triple Triple = getTriple();
186
187 // Thumb2 is the default for V7 on Darwin.
188 //
189 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Bernard Ogden31561762013-12-12 13:27:11 +0000190 StringRef Suffix = Triple.isOSDarwin()
191 ? tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMCPUForMArch(Args, Triple))
192 : tools::arm::getLLVMArchSuffixForARM(tools::arm::getARMTargetCPU(Args, Triple));
Bernard Ogden8af41b52013-12-12 13:27:04 +0000193 bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
194 Suffix.startswith("v7em") ||
Bob Wilson743bf672013-03-04 22:37:49 +0000195 (Suffix.startswith("v7") && getTriple().isOSDarwin());
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000196 std::string ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000197
198 // Assembly files should start in ARM mode.
199 if (InputType != types::TY_PP_Asm &&
200 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000201 ArchName = "thumb";
202 Triple.setArchName(ArchName + Suffix.str());
203
204 return Triple.getTriple();
205 }
206 }
207}
208
Chad Rosierd3a0f952011-09-20 20:44:06 +0000209std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
210 types::ID InputType) const {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000211 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000212 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000213 options::OPT_miphoneos_version_min_EQ,
214 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000215 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000216 << A->getAsString(Args);
217
Chad Rosierd3a0f952011-09-20 20:44:06 +0000218 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000219}
220
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000221void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
222 ArgStringList &CC1Args) const {
223 // Each toolchain should provide the appropriate include flags.
224}
225
Chandler Carruth05fb5852012-11-21 23:40:23 +0000226void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
227 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +0000228}
229
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000230ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
231 const ArgList &Args) const
232{
233 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000234 StringRef Value = A->getValue();
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000235 if (Value == "compiler-rt")
236 return ToolChain::RLT_CompilerRT;
237 if (Value == "libgcc")
238 return ToolChain::RLT_Libgcc;
239 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
240 << A->getAsString(Args);
241 }
242
243 return GetDefaultRuntimeLibType();
244}
245
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000246ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000247 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000248 StringRef Value = A->getValue();
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000249 if (Value == "libc++")
250 return ToolChain::CST_Libcxx;
251 if (Value == "libstdc++")
252 return ToolChain::CST_Libstdcxx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000253 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000254 << A->getAsString(Args);
255 }
256
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000257 return ToolChain::CST_Libstdcxx;
258}
259
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000260/// \brief Utility function to add a system include directory to CC1 arguments.
261/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
262 ArgStringList &CC1Args,
263 const Twine &Path) {
264 CC1Args.push_back("-internal-isystem");
265 CC1Args.push_back(DriverArgs.MakeArgString(Path));
266}
267
268/// \brief Utility function to add a system include directory with extern "C"
269/// semantics to CC1 arguments.
270///
271/// Note that this should be used rarely, and only for directories that
272/// historically and for legacy reasons are treated as having implicit extern
273/// "C" semantics. These semantics are *ignored* by and large today, but its
274/// important to preserve the preprocessor changes resulting from the
275/// classification.
276/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
277 ArgStringList &CC1Args,
278 const Twine &Path) {
279 CC1Args.push_back("-internal-externc-isystem");
280 CC1Args.push_back(DriverArgs.MakeArgString(Path));
281}
282
Simon Atanasyan08450bd2013-04-20 08:15:03 +0000283void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
284 ArgStringList &CC1Args,
285 const Twine &Path) {
286 if (llvm::sys::fs::exists(Path))
287 addExternCSystemInclude(DriverArgs, CC1Args, Path);
288}
289
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000290/// \brief Utility function to add a list of system include directories to CC1.
291/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
292 ArgStringList &CC1Args,
293 ArrayRef<StringRef> Paths) {
294 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
295 I != E; ++I) {
296 CC1Args.push_back("-internal-isystem");
297 CC1Args.push_back(DriverArgs.MakeArgString(*I));
298 }
299}
300
Chandler Carruth814db372011-11-04 23:49:01 +0000301void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
302 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000303 // Header search paths should be handled by each of the subclasses.
304 // Historically, they have not been, and instead have been handled inside of
305 // the CC1-layer frontend. As the logic is hoisted out, this generic function
306 // will slowly stop being called.
307 //
308 // While it is being called, replicate a bit of a hack to propagate the
309 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
310 // header search paths with it. Once all systems are overriding this
311 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000312 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000313}
314
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000315void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
316 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000317 CXXStdlibType Type = GetCXXStdlibType(Args);
318
319 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000320 case ToolChain::CST_Libcxx:
321 CmdArgs.push_back("-lc++");
322 break;
323
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000324 case ToolChain::CST_Libstdcxx:
325 CmdArgs.push_back("-lstdc++");
326 break;
327 }
328}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000329
330void ToolChain::AddCCKextLibArgs(const ArgList &Args,
331 ArgStringList &CmdArgs) const {
332 CmdArgs.push_back("-lcc_kext");
333}
Benjamin Kramer058666a2012-10-04 19:42:20 +0000334
335bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
336 ArgStringList &CmdArgs) const {
337 // Check if -ffast-math or -funsafe-math is enabled.
338 Arg *A = Args.getLastArg(options::OPT_ffast_math,
339 options::OPT_fno_fast_math,
340 options::OPT_funsafe_math_optimizations,
341 options::OPT_fno_unsafe_math_optimizations);
342
343 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
344 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
345 return false;
346
347 // If crtfastmath.o exists add it to the arguments.
348 std::string Path = GetFilePath("crtfastmath.o");
349 if (Path == "crtfastmath.o") // Not found.
350 return false;
351
352 CmdArgs.push_back(Args.MakeArgString(Path));
353 return true;
354}