blob: e034f72a26592714594964ee577709f1cac812b2 [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"
Logan Chieneb9162f2014-06-26 14:23:45 +000018#include "llvm/ADT/SmallString.h"
Bob Wilson7f05ca32012-03-21 17:19:12 +000019#include "llvm/ADT/StringSwitch.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000020#include "llvm/Option/Arg.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Option/Option.h"
John McCall24fc0de2011-07-06 00:26:06 +000023#include "llvm/Support/ErrorHandling.h"
Simon Atanasyan08450bd2013-04-20 08:15:03 +000024#include "llvm/Support/FileSystem.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000025using namespace clang::driver;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000026using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000027using namespace llvm::opt;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000028
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000029static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
30 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
31 options::OPT_fno_rtti, options::OPT_frtti);
32}
33
34static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
35 const llvm::Triple &Triple,
36 const Arg *CachedRTTIArg) {
37 // Explicit rtti/no-rtti args
38 if (CachedRTTIArg) {
39 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
40 return ToolChain::RM_EnabledExplicitly;
41 else
42 return ToolChain::RM_DisabledExplicitly;
43 }
44
45 // -frtti is default, except for the PS4 CPU.
46 if (!Triple.isPS4CPU())
47 return ToolChain::RM_EnabledImplicitly;
48
49 // On the PS4, turning on c++ exceptions turns on rtti.
50 // We're assuming that, if we see -fexceptions, rtti gets turned on.
Filipe Cabecinhas3e707d92015-03-20 23:33:23 +000051 Arg *Exceptions = Args.getLastArgNoClaim(
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000052 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
53 options::OPT_fexceptions, options::OPT_fno_exceptions);
54 if (Exceptions &&
55 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
56 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
57 return ToolChain::RM_EnabledImplicitly;
58
59 return ToolChain::RM_DisabledImplicitly;
60}
61
Rafael Espindola84b588b2013-03-18 18:10:27 +000062ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
Jonathan Roelofsb140a102014-10-03 21:57:44 +000063 const ArgList &Args)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000064 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
65 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
Jonathan Roelofsb140a102014-10-03 21:57:44 +000066 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
67 if (!isThreadModelSupported(A->getValue()))
68 D.Diag(diag::err_drv_invalid_thread_model_for_target)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000069 << A->getValue() << A->getAsString(Args);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000070}
71
72ToolChain::~ToolChain() {
73}
74
Daniel Dunbar083edf72009-12-21 18:54:17 +000075const Driver &ToolChain::getDriver() const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000076 return D;
Daniel Dunbar083edf72009-12-21 18:54:17 +000077}
78
Rafael Espindola84b588b2013-03-18 18:10:27 +000079bool ToolChain::useIntegratedAs() const {
Saleem Abdulrasoolcfeb90d2014-02-23 00:40:30 +000080 return Args.hasFlag(options::OPT_fintegrated_as,
81 options::OPT_fno_integrated_as,
Rafael Espindola248e2192013-03-18 17:52:57 +000082 IsIntegratedAssemblerDefault());
83}
84
Alexey Samsonov609213f92013-08-19 09:14:21 +000085const SanitizerArgs& ToolChain::getSanitizerArgs() const {
Peter Collingbourne32701642013-11-01 18:16:25 +000086 if (!SanitizerArguments.get())
87 SanitizerArguments.reset(new SanitizerArgs(*this, Args));
88 return *SanitizerArguments.get();
Alexey Samsonov609213f92013-08-19 09:14:21 +000089}
90
Rafael Espindola42db11e2014-07-25 19:22:51 +000091StringRef ToolChain::getDefaultUniversalArchName() const {
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +000092 // In universal driver terms, the arch name accepted by -arch isn't exactly
93 // the same as the ones that appear in the triple. Roughly speaking, this is
94 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
95 // only interesting special case is powerpc.
96 switch (Triple.getArch()) {
97 case llvm::Triple::ppc:
98 return "ppc";
99 case llvm::Triple::ppc64:
100 return "ppc64";
Bill Schmidt778d3872013-07-26 01:36:11 +0000101 case llvm::Triple::ppc64le:
102 return "ppc64le";
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000103 default:
104 return Triple.getArchName();
105 }
106}
107
Rafael Espindola151a9572012-09-23 03:05:41 +0000108bool ToolChain::IsUnwindTablesDefault() const {
109 return false;
110}
111
Rafael Espindola7cf32212013-03-20 03:05:54 +0000112Tool *ToolChain::getClang() const {
113 if (!Clang)
114 Clang.reset(new tools::Clang(*this));
115 return Clang.get();
116}
117
118Tool *ToolChain::buildAssembler() const {
119 return new tools::ClangAs(*this);
120}
121
122Tool *ToolChain::buildLinker() const {
123 llvm_unreachable("Linking is not supported by this toolchain");
124}
125
126Tool *ToolChain::getAssemble() const {
127 if (!Assemble)
128 Assemble.reset(buildAssembler());
129 return Assemble.get();
130}
131
132Tool *ToolChain::getClangAs() const {
133 if (!Assemble)
134 Assemble.reset(new tools::ClangAs(*this));
135 return Assemble.get();
136}
137
138Tool *ToolChain::getLink() const {
139 if (!Link)
140 Link.reset(buildLinker());
141 return Link.get();
142}
143
144Tool *ToolChain::getTool(Action::ActionClass AC) const {
Rafael Espindolad15a8912013-03-19 00:36:57 +0000145 switch (AC) {
Rafael Espindola7cf32212013-03-20 03:05:54 +0000146 case Action::AssembleJobClass:
147 return getAssemble();
148
149 case Action::LinkJobClass:
150 return getLink();
151
Rafael Espindolad15a8912013-03-19 00:36:57 +0000152 case Action::InputClass:
153 case Action::BindArchClass:
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000154 case Action::CudaDeviceClass:
155 case Action::CudaHostClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000156 case Action::LipoJobClass:
157 case Action::DsymutilJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000158 case Action::VerifyDebugInfoJobClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000159 llvm_unreachable("Invalid tool kind.");
160
161 case Action::CompileJobClass:
162 case Action::PrecompileJobClass:
163 case Action::PreprocessJobClass:
164 case Action::AnalyzeJobClass:
165 case Action::MigrateJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000166 case Action::VerifyPCHJobClass:
Bob Wilson23a55f12014-12-21 07:00:00 +0000167 case Action::BackendJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000168 return getClang();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000169 }
Benjamin Kramer5fbe2622013-03-21 19:45:46 +0000170
171 llvm_unreachable("Invalid tool kind.");
Rafael Espindolad15a8912013-03-19 00:36:57 +0000172}
173
Rafael Espindola79764462013-03-24 15:06:53 +0000174Tool *ToolChain::SelectTool(const JobAction &JA) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +0000175 if (getDriver().ShouldUseClangCompiler(JA))
Rafael Espindola79764462013-03-24 15:06:53 +0000176 return getClang();
Rafael Espindola7cf32212013-03-20 03:05:54 +0000177 Action::ActionClass AC = JA.getKind();
178 if (AC == Action::AssembleJobClass && useIntegratedAs())
Rafael Espindola79764462013-03-24 15:06:53 +0000179 return getClangAs();
180 return getTool(AC);
Rafael Espindola260e28d2013-03-18 20:48:54 +0000181}
182
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +0000183std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +0000184 return D.GetFilePath(Name, *this);
Mike Stump11289f42009-09-09 15:08:12 +0000185
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000186}
187
Simon Atanasyanb16488c2012-10-03 19:52:37 +0000188std::string ToolChain::GetProgramPath(const char *Name) const {
189 return D.GetProgramPath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000190}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000191
Logan Chieneb9162f2014-06-26 14:23:45 +0000192std::string ToolChain::GetLinkerPath() const {
193 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
194 StringRef Suffix = A->getValue();
195
196 // If we're passed -fuse-ld= with no argument, or with the argument ld,
197 // then use whatever the default system linker is.
198 if (Suffix.empty() || Suffix == "ld")
199 return GetProgramPath("ld");
200
201 llvm::SmallString<8> LinkerName("ld.");
202 LinkerName.append(Suffix);
203
204 std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
205 if (llvm::sys::fs::exists(LinkerPath))
206 return LinkerPath;
207
208 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
209 return "";
210 }
211
212 return GetProgramPath("ld");
213}
214
215
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000216types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
217 return types::lookupTypeForExtension(Ext);
218}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000219
Daniel Dunbar62123a12010-09-17 00:24:52 +0000220bool ToolChain::HasNativeLLVMSupport() const {
221 return false;
222}
223
Richard Barton5828d7b2013-12-17 11:11:25 +0000224bool ToolChain::isCrossCompiling() const {
225 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
226 switch (HostTriple.getArch()) {
Alp Tokerb164a342013-12-17 17:25:19 +0000227 // The A32/T32/T16 instruction sets are not separate architectures in this
228 // context.
229 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000230 case llvm::Triple::armeb:
Alp Tokerb164a342013-12-17 17:25:19 +0000231 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000232 case llvm::Triple::thumbeb:
233 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
234 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
Alp Tokerb164a342013-12-17 17:25:19 +0000235 default:
236 return HostTriple.getArch() != getArch();
Richard Barton5828d7b2013-12-17 11:11:25 +0000237 }
238}
239
John McCall5fb5df92012-06-20 06:18:46 +0000240ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnallb601c962012-07-03 20:49:52 +0000241 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall5fb5df92012-06-20 06:18:46 +0000242 VersionTuple());
John McCall24fc0de2011-07-06 00:26:06 +0000243}
244
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000245bool ToolChain::isThreadModelSupported(const StringRef Model) const {
246 if (Model == "single") {
Dan Gohmanc2853072015-09-03 22:51:53 +0000247 // FIXME: 'single' is only supported on ARM and WebAssembly so far.
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000248 return Triple.getArch() == llvm::Triple::arm ||
249 Triple.getArch() == llvm::Triple::armeb ||
250 Triple.getArch() == llvm::Triple::thumb ||
Dan Gohmanc2853072015-09-03 22:51:53 +0000251 Triple.getArch() == llvm::Triple::thumbeb ||
252 Triple.getArch() == llvm::Triple::wasm32 ||
253 Triple.getArch() == llvm::Triple::wasm64;
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000254 } else if (Model == "posix")
255 return true;
256
257 return false;
258}
259
Jim Grosbach82eee262013-11-16 00:53:35 +0000260std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000261 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000262 switch (getTriple().getArch()) {
263 default:
264 return getTripleString();
265
Jim Grosbach82eee262013-11-16 00:53:35 +0000266 case llvm::Triple::x86_64: {
267 llvm::Triple Triple = getTriple();
Tim Northover157d9112014-01-16 08:48:16 +0000268 if (!Triple.isOSBinFormatMachO())
Jim Grosbach82eee262013-11-16 00:53:35 +0000269 return getTripleString();
270
271 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
272 // x86_64h goes in the triple. Other -march options just use the
273 // vanilla triple we already have.
274 StringRef MArch = A->getValue();
275 if (MArch == "x86_64h")
276 Triple.setArchName(MArch);
277 }
278 return Triple.getTriple();
279 }
Tim Northover02a979f2014-07-24 10:25:34 +0000280 case llvm::Triple::aarch64: {
281 llvm::Triple Triple = getTriple();
282 if (!Triple.isOSBinFormatMachO())
283 return getTripleString();
284
285 // FIXME: older versions of ld64 expect the "arm64" component in the actual
286 // triple string and query it to determine whether an LTO file can be
287 // handled. Remove this when we don't care any more.
288 Triple.setArchName("arm64");
289 return Triple.getTriple();
290 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000291 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000292 case llvm::Triple::armeb:
293 case llvm::Triple::thumb:
294 case llvm::Triple::thumbeb: {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000295 // FIXME: Factor into subclasses.
296 llvm::Triple Triple = getTriple();
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000297 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
298 getTriple().getArch() == llvm::Triple::thumbeb;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000299
Christian Pirkerba289f02014-04-10 13:59:32 +0000300 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
301 // '-mbig-endian'/'-EB'.
302 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
303 options::OPT_mbig_endian)) {
David Blaikie7a3cbb22015-03-09 02:02:07 +0000304 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
Christian Pirkerba289f02014-04-10 13:59:32 +0000305 }
306
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000307 // Thumb2 is the default for V7 on Darwin.
308 //
309 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Renato Goline17c5802015-07-27 23:44:42 +0000310 StringRef MCPU, MArch;
311 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
312 MCPU = A->getValue();
313 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
314 MArch = A->getValue();
Chandler Carruthd96f37a2015-08-30 07:51:18 +0000315 std::string CPU =
316 Triple.isOSBinFormatMachO()
317 ? tools::arm::getARMCPUForMArch(MArch, Triple).str()
318 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000319 StringRef Suffix =
Vladimir Sukharevc6dab752015-05-14 08:25:18 +0000320 tools::arm::getLLVMArchSuffixForARM(CPU,
Renato Goline17c5802015-07-27 23:44:42 +0000321 tools::arm::getARMArch(MArch, Triple));
Bernard Ogden8af41b52013-12-12 13:27:04 +0000322 bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
323 Suffix.startswith("v7em") ||
Tim Northover157d9112014-01-16 08:48:16 +0000324 (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO());
Saleem Abdulrasoolf4c9e492014-04-04 20:31:19 +0000325 // FIXME: this is invalid for WindowsCE
326 if (getTriple().isOSWindows())
327 ThumbDefault = true;
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000328 std::string ArchName;
329 if (IsBigEndian)
330 ArchName = "armeb";
331 else
332 ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000333
334 // Assembly files should start in ARM mode.
335 if (InputType != types::TY_PP_Asm &&
336 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000337 {
338 if (IsBigEndian)
339 ArchName = "thumbeb";
340 else
341 ArchName = "thumb";
342 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000343 Triple.setArchName(ArchName + Suffix.str());
344
345 return Triple.getTriple();
346 }
347 }
348}
349
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000350std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000351 types::ID InputType) const {
Chad Rosierd3a0f952011-09-20 20:44:06 +0000352 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000353}
354
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000355void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
356 ArgStringList &CC1Args) const {
357 // Each toolchain should provide the appropriate include flags.
358}
359
Chandler Carruth05fb5852012-11-21 23:40:23 +0000360void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
361 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +0000362}
363
Tim Northover336f1892014-03-29 13:16:12 +0000364void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
365
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000366ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
367 const ArgList &Args) const
368{
369 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000370 StringRef Value = A->getValue();
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000371 if (Value == "compiler-rt")
372 return ToolChain::RLT_CompilerRT;
373 if (Value == "libgcc")
374 return ToolChain::RLT_Libgcc;
375 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
376 << A->getAsString(Args);
377 }
378
379 return GetDefaultRuntimeLibType();
380}
381
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000382ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000383 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000384 StringRef Value = A->getValue();
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000385 if (Value == "libc++")
386 return ToolChain::CST_Libcxx;
387 if (Value == "libstdc++")
388 return ToolChain::CST_Libstdcxx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000389 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000390 << A->getAsString(Args);
391 }
392
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000393 return ToolChain::CST_Libstdcxx;
394}
395
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000396/// \brief Utility function to add a system include directory to CC1 arguments.
397/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
398 ArgStringList &CC1Args,
399 const Twine &Path) {
400 CC1Args.push_back("-internal-isystem");
401 CC1Args.push_back(DriverArgs.MakeArgString(Path));
402}
403
404/// \brief Utility function to add a system include directory with extern "C"
405/// semantics to CC1 arguments.
406///
407/// Note that this should be used rarely, and only for directories that
408/// historically and for legacy reasons are treated as having implicit extern
409/// "C" semantics. These semantics are *ignored* by and large today, but its
410/// important to preserve the preprocessor changes resulting from the
411/// classification.
412/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
413 ArgStringList &CC1Args,
414 const Twine &Path) {
415 CC1Args.push_back("-internal-externc-isystem");
416 CC1Args.push_back(DriverArgs.MakeArgString(Path));
417}
418
Simon Atanasyan08450bd2013-04-20 08:15:03 +0000419void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
420 ArgStringList &CC1Args,
421 const Twine &Path) {
422 if (llvm::sys::fs::exists(Path))
423 addExternCSystemInclude(DriverArgs, CC1Args, Path);
424}
425
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000426/// \brief Utility function to add a list of system include directories to CC1.
427/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
428 ArgStringList &CC1Args,
429 ArrayRef<StringRef> Paths) {
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000430 for (StringRef Path : Paths) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000431 CC1Args.push_back("-internal-isystem");
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000432 CC1Args.push_back(DriverArgs.MakeArgString(Path));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000433 }
434}
435
Chandler Carruth814db372011-11-04 23:49:01 +0000436void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
437 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000438 // Header search paths should be handled by each of the subclasses.
439 // Historically, they have not been, and instead have been handled inside of
440 // the CC1-layer frontend. As the logic is hoisted out, this generic function
441 // will slowly stop being called.
442 //
443 // While it is being called, replicate a bit of a hack to propagate the
444 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
445 // header search paths with it. Once all systems are overriding this
446 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000447 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000448}
449
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000450void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
451 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000452 CXXStdlibType Type = GetCXXStdlibType(Args);
453
454 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000455 case ToolChain::CST_Libcxx:
456 CmdArgs.push_back("-lc++");
457 break;
458
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000459 case ToolChain::CST_Libstdcxx:
460 CmdArgs.push_back("-lstdc++");
461 break;
462 }
463}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000464
465void ToolChain::AddCCKextLibArgs(const ArgList &Args,
466 ArgStringList &CmdArgs) const {
467 CmdArgs.push_back("-lcc_kext");
468}
Benjamin Kramer058666a2012-10-04 19:42:20 +0000469
470bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
471 ArgStringList &CmdArgs) const {
Benjamin Kramerab88f622014-03-25 18:02:07 +0000472 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
473 // (to keep the linker options consistent with gcc and clang itself).
474 if (!isOptimizationLevelFast(Args)) {
475 // Check if -ffast-math or -funsafe-math.
476 Arg *A =
477 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
478 options::OPT_funsafe_math_optimizations,
479 options::OPT_fno_unsafe_math_optimizations);
Benjamin Kramer058666a2012-10-04 19:42:20 +0000480
Benjamin Kramerab88f622014-03-25 18:02:07 +0000481 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
482 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
483 return false;
484 }
Benjamin Kramer058666a2012-10-04 19:42:20 +0000485 // If crtfastmath.o exists add it to the arguments.
486 std::string Path = GetFilePath("crtfastmath.o");
487 if (Path == "crtfastmath.o") // Not found.
488 return false;
489
490 CmdArgs.push_back(Args.MakeArgString(Path));
491 return true;
492}
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000493
494SanitizerMask ToolChain::getSupportedSanitizers() const {
495 // Return sanitizers which don't require runtime support and are not
496 // platform or architecture-dependent.
497 using namespace SanitizerKind;
498 return (Undefined & ~Vptr & ~Function) | CFI | CFICastStrict |
499 UnsignedIntegerOverflow | LocalBounds;
500}