blob: 5855beff502eda2b9251c584663da6281d9ce4b8 [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"
Jonas Hahnfeldaae83742016-02-12 07:48:37 +000012#include "clang/Config/config.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000013#include "clang/Driver/Action.h"
14#include "clang/Driver/Driver.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000015#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000016#include "clang/Driver/Options.h"
Peter Collingbourne32701642013-11-01 18:16:25 +000017#include "clang/Driver/SanitizerArgs.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000018#include "clang/Driver/ToolChain.h"
Logan Chieneb9162f2014-06-26 14:23:45 +000019#include "llvm/ADT/SmallString.h"
Bob Wilson7f05ca32012-03-21 17:19:12 +000020#include "llvm/ADT/StringSwitch.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000021#include "llvm/Option/Arg.h"
22#include "llvm/Option/ArgList.h"
23#include "llvm/Option/Option.h"
John McCall24fc0de2011-07-06 00:26:06 +000024#include "llvm/Support/ErrorHandling.h"
Simon Atanasyan08450bd2013-04-20 08:15:03 +000025#include "llvm/Support/FileSystem.h"
Eric Christopher74a7c5d2015-09-25 17:44:31 +000026#include "llvm/Support/TargetRegistry.h"
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +000027#include "llvm/Support/TargetParser.h"
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000028
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000029using namespace clang::driver;
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000030using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000031using namespace clang;
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +000032using namespace llvm;
Reid Kleckner898229a2013-06-14 17:17:23 +000033using namespace llvm::opt;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000034
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000035static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
36 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
37 options::OPT_fno_rtti, options::OPT_frtti);
38}
39
40static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
41 const llvm::Triple &Triple,
42 const Arg *CachedRTTIArg) {
43 // Explicit rtti/no-rtti args
44 if (CachedRTTIArg) {
45 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
46 return ToolChain::RM_EnabledExplicitly;
47 else
48 return ToolChain::RM_DisabledExplicitly;
49 }
50
51 // -frtti is default, except for the PS4 CPU.
52 if (!Triple.isPS4CPU())
53 return ToolChain::RM_EnabledImplicitly;
54
55 // On the PS4, turning on c++ exceptions turns on rtti.
56 // We're assuming that, if we see -fexceptions, rtti gets turned on.
Filipe Cabecinhas3e707d92015-03-20 23:33:23 +000057 Arg *Exceptions = Args.getLastArgNoClaim(
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000058 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
59 options::OPT_fexceptions, options::OPT_fno_exceptions);
60 if (Exceptions &&
61 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
62 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
63 return ToolChain::RM_EnabledImplicitly;
64
65 return ToolChain::RM_DisabledImplicitly;
66}
67
Rafael Espindola84b588b2013-03-18 18:10:27 +000068ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
Jonathan Roelofsb140a102014-10-03 21:57:44 +000069 const ArgList &Args)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000070 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
71 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
Jonathan Roelofsb140a102014-10-03 21:57:44 +000072 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
73 if (!isThreadModelSupported(A->getValue()))
74 D.Diag(diag::err_drv_invalid_thread_model_for_target)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000075 << A->getValue() << A->getAsString(Args);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000076}
77
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000078ToolChain::~ToolChain() {
79}
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000080
Benjamin Kramerd45b2052015-10-07 15:48:01 +000081vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); }
Daniel Dunbar083edf72009-12-21 18:54:17 +000082
Rafael Espindola84b588b2013-03-18 18:10:27 +000083bool ToolChain::useIntegratedAs() const {
Saleem Abdulrasoolcfeb90d2014-02-23 00:40:30 +000084 return Args.hasFlag(options::OPT_fintegrated_as,
85 options::OPT_fno_integrated_as,
Rafael Espindola248e2192013-03-18 17:52:57 +000086 IsIntegratedAssemblerDefault());
87}
88
Alexey Samsonov609213f92013-08-19 09:14:21 +000089const SanitizerArgs& ToolChain::getSanitizerArgs() const {
Peter Collingbourne32701642013-11-01 18:16:25 +000090 if (!SanitizerArguments.get())
91 SanitizerArguments.reset(new SanitizerArgs(*this, Args));
92 return *SanitizerArguments.get();
Alexey Samsonov609213f92013-08-19 09:14:21 +000093}
94
Eric Christopher74a7c5d2015-09-25 17:44:31 +000095namespace {
96struct DriverSuffix {
97 const char *Suffix;
98 const char *ModeFlag;
99};
100
101const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
102 // A list of known driver suffixes. Suffixes are compared against the
103 // program name in order. If there is a match, the frontend type is updated as
104 // necessary by applying the ModeFlag.
105 static const DriverSuffix DriverSuffixes[] = {
106 {"clang", nullptr},
107 {"clang++", "--driver-mode=g++"},
108 {"clang-c++", "--driver-mode=g++"},
109 {"clang-cc", nullptr},
110 {"clang-cpp", "--driver-mode=cpp"},
111 {"clang-g++", "--driver-mode=g++"},
112 {"clang-gcc", nullptr},
113 {"clang-cl", "--driver-mode=cl"},
114 {"cc", nullptr},
115 {"cpp", "--driver-mode=cpp"},
116 {"cl", "--driver-mode=cl"},
117 {"++", "--driver-mode=g++"},
118 };
119
120 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
121 if (ProgName.endswith(DriverSuffixes[i].Suffix))
122 return &DriverSuffixes[i];
123 return nullptr;
124}
125
126/// Normalize the program name from argv[0] by stripping the file extension if
127/// present and lower-casing the string on Windows.
128std::string normalizeProgramName(llvm::StringRef Argv0) {
129 std::string ProgName = llvm::sys::path::stem(Argv0);
130#ifdef LLVM_ON_WIN32
131 // Transform to lowercase for case insensitive file systems.
132 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
133#endif
134 return ProgName;
135}
136
137const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
138 // Try to infer frontend type and default target from the program name by
139 // comparing it against DriverSuffixes in order.
140
141 // If there is a match, the function tries to identify a target as prefix.
142 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
143 // prefix "x86_64-linux". If such a target prefix is found, it may be
144 // added via -target as implicit first argument.
145 const DriverSuffix *DS = FindDriverSuffix(ProgName);
146
147 if (!DS) {
148 // Try again after stripping any trailing version number:
149 // clang++3.5 -> clang++
150 ProgName = ProgName.rtrim("0123456789.");
151 DS = FindDriverSuffix(ProgName);
152 }
153
154 if (!DS) {
155 // Try again after stripping trailing -component.
156 // clang++-tot -> clang++
157 ProgName = ProgName.slice(0, ProgName.rfind('-'));
158 DS = FindDriverSuffix(ProgName);
159 }
160 return DS;
161}
162} // anonymous namespace
163
164std::pair<std::string, std::string>
165ToolChain::getTargetAndModeFromProgramName(StringRef PN) {
166 std::string ProgName = normalizeProgramName(PN);
167 const DriverSuffix *DS = parseDriverSuffix(ProgName);
168 if (!DS)
169 return std::make_pair("", "");
170 std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag;
171
172 std::string::size_type LastComponent =
173 ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix));
174 if (LastComponent == std::string::npos)
175 return std::make_pair("", ModeFlag);
176
177 // Infer target from the prefix.
178 StringRef Prefix(ProgName);
179 Prefix = Prefix.slice(0, LastComponent);
180 std::string IgnoredError;
181 std::string Target;
182 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
183 Target = Prefix;
184 }
185 return std::make_pair(Target, ModeFlag);
186}
187
Rafael Espindola42db11e2014-07-25 19:22:51 +0000188StringRef ToolChain::getDefaultUniversalArchName() const {
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000189 // In universal driver terms, the arch name accepted by -arch isn't exactly
190 // the same as the ones that appear in the triple. Roughly speaking, this is
191 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
192 // only interesting special case is powerpc.
193 switch (Triple.getArch()) {
194 case llvm::Triple::ppc:
195 return "ppc";
196 case llvm::Triple::ppc64:
197 return "ppc64";
Bill Schmidt778d3872013-07-26 01:36:11 +0000198 case llvm::Triple::ppc64le:
199 return "ppc64le";
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000200 default:
201 return Triple.getArchName();
202 }
203}
204
Rafael Espindola151a9572012-09-23 03:05:41 +0000205bool ToolChain::IsUnwindTablesDefault() const {
206 return false;
207}
208
Rafael Espindola7cf32212013-03-20 03:05:54 +0000209Tool *ToolChain::getClang() const {
210 if (!Clang)
211 Clang.reset(new tools::Clang(*this));
212 return Clang.get();
213}
214
215Tool *ToolChain::buildAssembler() const {
216 return new tools::ClangAs(*this);
217}
218
219Tool *ToolChain::buildLinker() const {
220 llvm_unreachable("Linking is not supported by this toolchain");
221}
222
223Tool *ToolChain::getAssemble() const {
224 if (!Assemble)
225 Assemble.reset(buildAssembler());
226 return Assemble.get();
227}
228
229Tool *ToolChain::getClangAs() const {
230 if (!Assemble)
231 Assemble.reset(new tools::ClangAs(*this));
232 return Assemble.get();
233}
234
235Tool *ToolChain::getLink() const {
236 if (!Link)
237 Link.reset(buildLinker());
238 return Link.get();
239}
240
241Tool *ToolChain::getTool(Action::ActionClass AC) const {
Rafael Espindolad15a8912013-03-19 00:36:57 +0000242 switch (AC) {
Rafael Espindola7cf32212013-03-20 03:05:54 +0000243 case Action::AssembleJobClass:
244 return getAssemble();
245
246 case Action::LinkJobClass:
247 return getLink();
248
Rafael Espindolad15a8912013-03-19 00:36:57 +0000249 case Action::InputClass:
250 case Action::BindArchClass:
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000251 case Action::CudaDeviceClass:
252 case Action::CudaHostClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000253 case Action::LipoJobClass:
254 case Action::DsymutilJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000255 case Action::VerifyDebugInfoJobClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000256 llvm_unreachable("Invalid tool kind.");
257
258 case Action::CompileJobClass:
259 case Action::PrecompileJobClass:
260 case Action::PreprocessJobClass:
261 case Action::AnalyzeJobClass:
262 case Action::MigrateJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000263 case Action::VerifyPCHJobClass:
Bob Wilson23a55f12014-12-21 07:00:00 +0000264 case Action::BackendJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000265 return getClang();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000266 }
Benjamin Kramer5fbe2622013-03-21 19:45:46 +0000267
268 llvm_unreachable("Invalid tool kind.");
Rafael Espindolad15a8912013-03-19 00:36:57 +0000269}
270
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000271static StringRef getArchNameForCompilerRTLib(const ToolChain &TC,
272 const ArgList &Args) {
273 const llvm::Triple &Triple = TC.getTriple();
274 bool IsWindows = Triple.isOSWindows();
275
276 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86)
277 return "i386";
278
279 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb)
280 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows)
281 ? "armhf"
282 : "arm";
283
284 return TC.getArchName();
285}
286
287std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
288 bool Shared) const {
289 const llvm::Triple &TT = getTriple();
Evgeniy Stepanov14deb7b2015-10-08 21:21:44 +0000290 const char *Env = TT.isAndroid() ? "-android" : "";
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000291 bool IsITANMSVCWindows =
292 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
293
294 StringRef Arch = getArchNameForCompilerRTLib(*this, Args);
295 const char *Prefix = IsITANMSVCWindows ? "" : "lib";
296 const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so")
297 : (IsITANMSVCWindows ? ".lib" : ".a");
298
299 SmallString<128> Path(getDriver().ResourceDir);
300 StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS();
301 llvm::sys::path::append(Path, "lib", OSLibName);
302 llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" +
303 Arch + Env + Suffix);
304 return Path.str();
305}
306
Xinliang David Li69306c02015-10-22 06:15:31 +0000307const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
308 StringRef Component,
309 bool Shared) const {
310 return Args.MakeArgString(getCompilerRT(Args, Component, Shared));
311}
312
313bool ToolChain::needsProfileRT(const ArgList &Args) {
314 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
315 false) ||
316 Args.hasArg(options::OPT_fprofile_generate) ||
317 Args.hasArg(options::OPT_fprofile_generate_EQ) ||
318 Args.hasArg(options::OPT_fprofile_instr_generate) ||
319 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
320 Args.hasArg(options::OPT_fcreate_profile) ||
321 Args.hasArg(options::OPT_coverage))
322 return true;
323
324 return false;
325}
326
Rafael Espindola79764462013-03-24 15:06:53 +0000327Tool *ToolChain::SelectTool(const JobAction &JA) const {
Xinliang David Li69306c02015-10-22 06:15:31 +0000328 if (getDriver().ShouldUseClangCompiler(JA)) return getClang();
Rafael Espindola7cf32212013-03-20 03:05:54 +0000329 Action::ActionClass AC = JA.getKind();
330 if (AC == Action::AssembleJobClass && useIntegratedAs())
Rafael Espindola79764462013-03-24 15:06:53 +0000331 return getClangAs();
332 return getTool(AC);
Rafael Espindola260e28d2013-03-18 20:48:54 +0000333}
334
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +0000335std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +0000336 return D.GetFilePath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000337}
338
Simon Atanasyanb16488c2012-10-03 19:52:37 +0000339std::string ToolChain::GetProgramPath(const char *Name) const {
340 return D.GetProgramPath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000341}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000342
Logan Chieneb9162f2014-06-26 14:23:45 +0000343std::string ToolChain::GetLinkerPath() const {
344 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
Peter Zotove43b7412016-03-09 05:18:16 +0000345 StringRef UseLinker = A->getValue();
Logan Chieneb9162f2014-06-26 14:23:45 +0000346
Peter Zotove43b7412016-03-09 05:18:16 +0000347 if (llvm::sys::path::is_absolute(UseLinker)) {
348 // If we're passed -fuse-ld= with what looks like an absolute path,
349 // don't attempt to second-guess that.
350 if (llvm::sys::fs::exists(UseLinker))
351 return UseLinker;
352 } else {
353 // If we're passed -fuse-ld= with no argument, or with the argument ld,
354 // then use whatever the default system linker is.
355 if (UseLinker.empty() || UseLinker == "ld")
356 return GetProgramPath("ld");
Logan Chieneb9162f2014-06-26 14:23:45 +0000357
Peter Zotove43b7412016-03-09 05:18:16 +0000358 llvm::SmallString<8> LinkerName("ld.");
359 LinkerName.append(UseLinker);
Logan Chieneb9162f2014-06-26 14:23:45 +0000360
Peter Zotove43b7412016-03-09 05:18:16 +0000361 std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
362 if (llvm::sys::fs::exists(LinkerPath))
363 return LinkerPath;
364 }
Logan Chieneb9162f2014-06-26 14:23:45 +0000365
366 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
367 return "";
368 }
369
Peter Collingbourne39719a72015-11-20 20:49:39 +0000370 return GetProgramPath(DefaultLinker);
Logan Chieneb9162f2014-06-26 14:23:45 +0000371}
372
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000373types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
374 return types::lookupTypeForExtension(Ext);
375}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000376
Daniel Dunbar62123a12010-09-17 00:24:52 +0000377bool ToolChain::HasNativeLLVMSupport() const {
378 return false;
379}
380
Richard Barton5828d7b2013-12-17 11:11:25 +0000381bool ToolChain::isCrossCompiling() const {
382 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
383 switch (HostTriple.getArch()) {
Alp Tokerb164a342013-12-17 17:25:19 +0000384 // The A32/T32/T16 instruction sets are not separate architectures in this
385 // context.
386 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000387 case llvm::Triple::armeb:
Alp Tokerb164a342013-12-17 17:25:19 +0000388 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000389 case llvm::Triple::thumbeb:
390 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
391 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
Alp Tokerb164a342013-12-17 17:25:19 +0000392 default:
393 return HostTriple.getArch() != getArch();
Richard Barton5828d7b2013-12-17 11:11:25 +0000394 }
395}
396
John McCall5fb5df92012-06-20 06:18:46 +0000397ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnallb601c962012-07-03 20:49:52 +0000398 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall5fb5df92012-06-20 06:18:46 +0000399 VersionTuple());
John McCall24fc0de2011-07-06 00:26:06 +0000400}
401
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000402bool ToolChain::isThreadModelSupported(const StringRef Model) const {
403 if (Model == "single") {
Dan Gohmanc2853072015-09-03 22:51:53 +0000404 // FIXME: 'single' is only supported on ARM and WebAssembly so far.
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000405 return Triple.getArch() == llvm::Triple::arm ||
406 Triple.getArch() == llvm::Triple::armeb ||
407 Triple.getArch() == llvm::Triple::thumb ||
Dan Gohmanc2853072015-09-03 22:51:53 +0000408 Triple.getArch() == llvm::Triple::thumbeb ||
409 Triple.getArch() == llvm::Triple::wasm32 ||
410 Triple.getArch() == llvm::Triple::wasm64;
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000411 } else if (Model == "posix")
412 return true;
413
414 return false;
415}
416
Jim Grosbach82eee262013-11-16 00:53:35 +0000417std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000418 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000419 switch (getTriple().getArch()) {
420 default:
421 return getTripleString();
422
Jim Grosbach82eee262013-11-16 00:53:35 +0000423 case llvm::Triple::x86_64: {
424 llvm::Triple Triple = getTriple();
Tim Northover157d9112014-01-16 08:48:16 +0000425 if (!Triple.isOSBinFormatMachO())
Jim Grosbach82eee262013-11-16 00:53:35 +0000426 return getTripleString();
427
428 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
429 // x86_64h goes in the triple. Other -march options just use the
430 // vanilla triple we already have.
431 StringRef MArch = A->getValue();
432 if (MArch == "x86_64h")
433 Triple.setArchName(MArch);
434 }
435 return Triple.getTriple();
436 }
Tim Northover02a979f2014-07-24 10:25:34 +0000437 case llvm::Triple::aarch64: {
438 llvm::Triple Triple = getTriple();
439 if (!Triple.isOSBinFormatMachO())
440 return getTripleString();
441
442 // FIXME: older versions of ld64 expect the "arm64" component in the actual
443 // triple string and query it to determine whether an LTO file can be
444 // handled. Remove this when we don't care any more.
445 Triple.setArchName("arm64");
446 return Triple.getTriple();
447 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000448 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000449 case llvm::Triple::armeb:
450 case llvm::Triple::thumb:
451 case llvm::Triple::thumbeb: {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000452 // FIXME: Factor into subclasses.
453 llvm::Triple Triple = getTriple();
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000454 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
455 getTriple().getArch() == llvm::Triple::thumbeb;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000456
Christian Pirkerba289f02014-04-10 13:59:32 +0000457 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
458 // '-mbig-endian'/'-EB'.
459 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
460 options::OPT_mbig_endian)) {
David Blaikie7a3cbb22015-03-09 02:02:07 +0000461 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
Christian Pirkerba289f02014-04-10 13:59:32 +0000462 }
463
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000464 // Thumb2 is the default for V7 on Darwin.
465 //
466 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Renato Goline17c5802015-07-27 23:44:42 +0000467 StringRef MCPU, MArch;
468 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
469 MCPU = A->getValue();
470 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
471 MArch = A->getValue();
Chandler Carruthd96f37a2015-08-30 07:51:18 +0000472 std::string CPU =
473 Triple.isOSBinFormatMachO()
474 ? tools::arm::getARMCPUForMArch(MArch, Triple).str()
475 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000476 StringRef Suffix =
Vladimir Sukharev64f68242015-09-23 09:29:32 +0000477 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +0000478 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M;
479 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
480 getTriple().isOSBinFormatMachO());
Saleem Abdulrasoolf4c9e492014-04-04 20:31:19 +0000481 // FIXME: this is invalid for WindowsCE
482 if (getTriple().isOSWindows())
483 ThumbDefault = true;
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000484 std::string ArchName;
485 if (IsBigEndian)
486 ArchName = "armeb";
487 else
488 ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000489
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +0000490 // Assembly files should start in ARM mode, unless arch is M-profile.
Alexandros Lamprineasf6ecf962015-11-05 17:11:55 +0000491 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb,
492 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile) {
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000493 if (IsBigEndian)
494 ArchName = "thumbeb";
495 else
496 ArchName = "thumb";
497 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000498 Triple.setArchName(ArchName + Suffix.str());
499
500 return Triple.getTriple();
501 }
502 }
503}
504
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000505std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000506 types::ID InputType) const {
Chad Rosierd3a0f952011-09-20 20:44:06 +0000507 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000508}
509
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000510void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
511 ArgStringList &CC1Args) const {
512 // Each toolchain should provide the appropriate include flags.
513}
514
Chandler Carruth05fb5852012-11-21 23:40:23 +0000515void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
516 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +0000517}
518
Tim Northover336f1892014-03-29 13:16:12 +0000519void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
520
Xinliang David Li69306c02015-10-22 06:15:31 +0000521void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args,
522 llvm::opt::ArgStringList &CmdArgs) const {
523 if (!needsProfileRT(Args)) return;
524
525 CmdArgs.push_back(getCompilerRTArgString(Args, "profile"));
Xinliang David Li69306c02015-10-22 06:15:31 +0000526}
527
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000528ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
Xinliang David Li69306c02015-10-22 06:15:31 +0000529 const ArgList &Args) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000530 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000531 StringRef Value = A->getValue();
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000532 if (Value == "compiler-rt")
533 return ToolChain::RLT_CompilerRT;
534 if (Value == "libgcc")
535 return ToolChain::RLT_Libgcc;
536 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
537 << A->getAsString(Args);
538 }
539
540 return GetDefaultRuntimeLibType();
541}
542
Jonas Hahnfeldaae83742016-02-12 07:48:37 +0000543static bool ParseCXXStdlibType(const StringRef& Name,
544 ToolChain::CXXStdlibType& Type) {
545 if (Name == "libc++")
546 Type = ToolChain::CST_Libcxx;
547 else if (Name == "libstdc++")
548 Type = ToolChain::CST_Libstdcxx;
549 else
550 return false;
551
552 return true;
553}
554
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000555ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Jonas Hahnfeldaae83742016-02-12 07:48:37 +0000556 ToolChain::CXXStdlibType Type;
557 bool HasValidType = false;
Jonas Hahnfeld09954192016-03-14 14:34:04 +0000558 bool ForcePlatformDefault = false;
Jonas Hahnfeldaae83742016-02-12 07:48:37 +0000559
560 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
561 if (A) {
Jonas Hahnfeld09954192016-03-14 14:34:04 +0000562 StringRef Value = A->getValue();
563 HasValidType = ParseCXXStdlibType(Value, Type);
564
565 // Only use in tests to override CLANG_DEFAULT_CXX_STDLIB!
566 if (Value == "platform")
567 ForcePlatformDefault = true;
568 else if (!HasValidType)
Jonas Hahnfeldaae83742016-02-12 07:48:37 +0000569 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
570 << A->getAsString(Args);
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000571 }
572
Jonas Hahnfeld09954192016-03-14 14:34:04 +0000573 if (!HasValidType && (ForcePlatformDefault ||
574 !ParseCXXStdlibType(CLANG_DEFAULT_CXX_STDLIB, Type)))
Jonas Hahnfeldaae83742016-02-12 07:48:37 +0000575 Type = GetDefaultCXXStdlibType();
576
577 return Type;
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000578}
579
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000580/// \brief Utility function to add a system include directory to CC1 arguments.
581/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
582 ArgStringList &CC1Args,
583 const Twine &Path) {
584 CC1Args.push_back("-internal-isystem");
585 CC1Args.push_back(DriverArgs.MakeArgString(Path));
586}
587
588/// \brief Utility function to add a system include directory with extern "C"
589/// semantics to CC1 arguments.
590///
591/// Note that this should be used rarely, and only for directories that
592/// historically and for legacy reasons are treated as having implicit extern
593/// "C" semantics. These semantics are *ignored* by and large today, but its
594/// important to preserve the preprocessor changes resulting from the
595/// classification.
596/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
597 ArgStringList &CC1Args,
598 const Twine &Path) {
599 CC1Args.push_back("-internal-externc-isystem");
600 CC1Args.push_back(DriverArgs.MakeArgString(Path));
601}
602
Simon Atanasyan08450bd2013-04-20 08:15:03 +0000603void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
604 ArgStringList &CC1Args,
605 const Twine &Path) {
606 if (llvm::sys::fs::exists(Path))
607 addExternCSystemInclude(DriverArgs, CC1Args, Path);
608}
609
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000610/// \brief Utility function to add a list of system include directories to CC1.
611/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
612 ArgStringList &CC1Args,
613 ArrayRef<StringRef> Paths) {
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000614 for (StringRef Path : Paths) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000615 CC1Args.push_back("-internal-isystem");
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000616 CC1Args.push_back(DriverArgs.MakeArgString(Path));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000617 }
618}
619
Chandler Carruth814db372011-11-04 23:49:01 +0000620void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
621 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000622 // Header search paths should be handled by each of the subclasses.
623 // Historically, they have not been, and instead have been handled inside of
624 // the CC1-layer frontend. As the logic is hoisted out, this generic function
625 // will slowly stop being called.
626 //
627 // While it is being called, replicate a bit of a hack to propagate the
628 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
629 // header search paths with it. Once all systems are overriding this
630 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000631 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000632}
633
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000634void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
635 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000636 CXXStdlibType Type = GetCXXStdlibType(Args);
637
638 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000639 case ToolChain::CST_Libcxx:
640 CmdArgs.push_back("-lc++");
641 break;
642
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000643 case ToolChain::CST_Libstdcxx:
644 CmdArgs.push_back("-lstdc++");
645 break;
646 }
647}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000648
Douglas Katzman6059ef92015-11-17 17:41:23 +0000649void ToolChain::AddFilePathLibArgs(const ArgList &Args,
650 ArgStringList &CmdArgs) const {
651 for (const auto &LibPath : getFilePaths())
Martell Malone5cad2252015-11-26 01:02:07 +0000652 if(LibPath.length() > 0)
653 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
Douglas Katzman6059ef92015-11-17 17:41:23 +0000654}
655
Shantonu Senafeb03b2010-09-17 18:39:08 +0000656void ToolChain::AddCCKextLibArgs(const ArgList &Args,
657 ArgStringList &CmdArgs) const {
658 CmdArgs.push_back("-lcc_kext");
659}
Benjamin Kramer058666a2012-10-04 19:42:20 +0000660
661bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
662 ArgStringList &CmdArgs) const {
Benjamin Kramerab88f622014-03-25 18:02:07 +0000663 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
664 // (to keep the linker options consistent with gcc and clang itself).
665 if (!isOptimizationLevelFast(Args)) {
666 // Check if -ffast-math or -funsafe-math.
667 Arg *A =
668 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
669 options::OPT_funsafe_math_optimizations,
670 options::OPT_fno_unsafe_math_optimizations);
Benjamin Kramer058666a2012-10-04 19:42:20 +0000671
Benjamin Kramerab88f622014-03-25 18:02:07 +0000672 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
673 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
674 return false;
675 }
Benjamin Kramer058666a2012-10-04 19:42:20 +0000676 // If crtfastmath.o exists add it to the arguments.
677 std::string Path = GetFilePath("crtfastmath.o");
678 if (Path == "crtfastmath.o") // Not found.
679 return false;
680
681 CmdArgs.push_back(Args.MakeArgString(Path));
682 return true;
683}
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000684
685SanitizerMask ToolChain::getSupportedSanitizers() const {
686 // Return sanitizers which don't require runtime support and are not
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000687 // platform dependent.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000688 using namespace SanitizerKind;
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000689 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
690 CFICastStrict | UnsignedIntegerOverflow | LocalBounds;
691 if (getTriple().getArch() == llvm::Triple::x86 ||
692 getTriple().getArch() == llvm::Triple::x86_64)
693 Res |= CFIICall;
694 return Res;
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000695}
Artem Belevichfa11ab52015-11-17 22:28:46 +0000696
697void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
698 ArgStringList &CC1Args) const {}