blob: 221bed0bf118731b2401d7dd5fe0bacf6c4ce51a [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"
Eric Christopher74a7c5d2015-09-25 17:44:31 +000025#include "llvm/Support/TargetRegistry.h"
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +000026#include "llvm/Support/TargetParser.h"
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000027
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000028using namespace clang::driver;
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000029using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000030using namespace clang;
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +000031using namespace llvm;
Reid Kleckner898229a2013-06-14 17:17:23 +000032using namespace llvm::opt;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000033
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000034static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
35 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
36 options::OPT_fno_rtti, options::OPT_frtti);
37}
38
39static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
40 const llvm::Triple &Triple,
41 const Arg *CachedRTTIArg) {
42 // Explicit rtti/no-rtti args
43 if (CachedRTTIArg) {
44 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
45 return ToolChain::RM_EnabledExplicitly;
46 else
47 return ToolChain::RM_DisabledExplicitly;
48 }
49
50 // -frtti is default, except for the PS4 CPU.
51 if (!Triple.isPS4CPU())
52 return ToolChain::RM_EnabledImplicitly;
53
54 // On the PS4, turning on c++ exceptions turns on rtti.
55 // We're assuming that, if we see -fexceptions, rtti gets turned on.
Filipe Cabecinhas3e707d92015-03-20 23:33:23 +000056 Arg *Exceptions = Args.getLastArgNoClaim(
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000057 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
58 options::OPT_fexceptions, options::OPT_fno_exceptions);
59 if (Exceptions &&
60 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
61 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
62 return ToolChain::RM_EnabledImplicitly;
63
64 return ToolChain::RM_DisabledImplicitly;
65}
66
Rafael Espindola84b588b2013-03-18 18:10:27 +000067ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
Jonathan Roelofsb140a102014-10-03 21:57:44 +000068 const ArgList &Args)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000069 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
70 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
Jonathan Roelofsb140a102014-10-03 21:57:44 +000071 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
72 if (!isThreadModelSupported(A->getValue()))
73 D.Diag(diag::err_drv_invalid_thread_model_for_target)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000074 << A->getValue() << A->getAsString(Args);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000075}
76
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000077ToolChain::~ToolChain() {
78}
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000079
Benjamin Kramerd45b2052015-10-07 15:48:01 +000080vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); }
Daniel Dunbar083edf72009-12-21 18:54:17 +000081
Rafael Espindola84b588b2013-03-18 18:10:27 +000082bool ToolChain::useIntegratedAs() const {
Saleem Abdulrasoolcfeb90d2014-02-23 00:40:30 +000083 return Args.hasFlag(options::OPT_fintegrated_as,
84 options::OPT_fno_integrated_as,
Rafael Espindola248e2192013-03-18 17:52:57 +000085 IsIntegratedAssemblerDefault());
86}
87
Alexey Samsonov609213f92013-08-19 09:14:21 +000088const SanitizerArgs& ToolChain::getSanitizerArgs() const {
Peter Collingbourne32701642013-11-01 18:16:25 +000089 if (!SanitizerArguments.get())
90 SanitizerArguments.reset(new SanitizerArgs(*this, Args));
91 return *SanitizerArguments.get();
Alexey Samsonov609213f92013-08-19 09:14:21 +000092}
93
Eric Christopher74a7c5d2015-09-25 17:44:31 +000094namespace {
95struct DriverSuffix {
96 const char *Suffix;
97 const char *ModeFlag;
98};
99
100const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
101 // A list of known driver suffixes. Suffixes are compared against the
102 // program name in order. If there is a match, the frontend type is updated as
103 // necessary by applying the ModeFlag.
104 static const DriverSuffix DriverSuffixes[] = {
105 {"clang", nullptr},
106 {"clang++", "--driver-mode=g++"},
107 {"clang-c++", "--driver-mode=g++"},
108 {"clang-cc", nullptr},
109 {"clang-cpp", "--driver-mode=cpp"},
110 {"clang-g++", "--driver-mode=g++"},
111 {"clang-gcc", nullptr},
112 {"clang-cl", "--driver-mode=cl"},
113 {"cc", nullptr},
114 {"cpp", "--driver-mode=cpp"},
115 {"cl", "--driver-mode=cl"},
116 {"++", "--driver-mode=g++"},
117 };
118
119 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
120 if (ProgName.endswith(DriverSuffixes[i].Suffix))
121 return &DriverSuffixes[i];
122 return nullptr;
123}
124
125/// Normalize the program name from argv[0] by stripping the file extension if
126/// present and lower-casing the string on Windows.
127std::string normalizeProgramName(llvm::StringRef Argv0) {
128 std::string ProgName = llvm::sys::path::stem(Argv0);
129#ifdef LLVM_ON_WIN32
130 // Transform to lowercase for case insensitive file systems.
131 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
132#endif
133 return ProgName;
134}
135
136const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
137 // Try to infer frontend type and default target from the program name by
138 // comparing it against DriverSuffixes in order.
139
140 // If there is a match, the function tries to identify a target as prefix.
141 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
142 // prefix "x86_64-linux". If such a target prefix is found, it may be
143 // added via -target as implicit first argument.
144 const DriverSuffix *DS = FindDriverSuffix(ProgName);
145
146 if (!DS) {
147 // Try again after stripping any trailing version number:
148 // clang++3.5 -> clang++
149 ProgName = ProgName.rtrim("0123456789.");
150 DS = FindDriverSuffix(ProgName);
151 }
152
153 if (!DS) {
154 // Try again after stripping trailing -component.
155 // clang++-tot -> clang++
156 ProgName = ProgName.slice(0, ProgName.rfind('-'));
157 DS = FindDriverSuffix(ProgName);
158 }
159 return DS;
160}
161} // anonymous namespace
162
163std::pair<std::string, std::string>
164ToolChain::getTargetAndModeFromProgramName(StringRef PN) {
165 std::string ProgName = normalizeProgramName(PN);
166 const DriverSuffix *DS = parseDriverSuffix(ProgName);
167 if (!DS)
168 return std::make_pair("", "");
169 std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag;
170
171 std::string::size_type LastComponent =
172 ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix));
173 if (LastComponent == std::string::npos)
174 return std::make_pair("", ModeFlag);
175
176 // Infer target from the prefix.
177 StringRef Prefix(ProgName);
178 Prefix = Prefix.slice(0, LastComponent);
179 std::string IgnoredError;
180 std::string Target;
181 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
182 Target = Prefix;
183 }
184 return std::make_pair(Target, ModeFlag);
185}
186
Rafael Espindola42db11e2014-07-25 19:22:51 +0000187StringRef ToolChain::getDefaultUniversalArchName() const {
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000188 // In universal driver terms, the arch name accepted by -arch isn't exactly
189 // the same as the ones that appear in the triple. Roughly speaking, this is
190 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
191 // only interesting special case is powerpc.
192 switch (Triple.getArch()) {
193 case llvm::Triple::ppc:
194 return "ppc";
195 case llvm::Triple::ppc64:
196 return "ppc64";
Bill Schmidt778d3872013-07-26 01:36:11 +0000197 case llvm::Triple::ppc64le:
198 return "ppc64le";
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000199 default:
200 return Triple.getArchName();
201 }
202}
203
Rafael Espindola151a9572012-09-23 03:05:41 +0000204bool ToolChain::IsUnwindTablesDefault() const {
205 return false;
206}
207
Rafael Espindola7cf32212013-03-20 03:05:54 +0000208Tool *ToolChain::getClang() const {
209 if (!Clang)
210 Clang.reset(new tools::Clang(*this));
211 return Clang.get();
212}
213
214Tool *ToolChain::buildAssembler() const {
215 return new tools::ClangAs(*this);
216}
217
218Tool *ToolChain::buildLinker() const {
219 llvm_unreachable("Linking is not supported by this toolchain");
220}
221
222Tool *ToolChain::getAssemble() const {
223 if (!Assemble)
224 Assemble.reset(buildAssembler());
225 return Assemble.get();
226}
227
228Tool *ToolChain::getClangAs() const {
229 if (!Assemble)
230 Assemble.reset(new tools::ClangAs(*this));
231 return Assemble.get();
232}
233
234Tool *ToolChain::getLink() const {
235 if (!Link)
236 Link.reset(buildLinker());
237 return Link.get();
238}
239
240Tool *ToolChain::getTool(Action::ActionClass AC) const {
Rafael Espindolad15a8912013-03-19 00:36:57 +0000241 switch (AC) {
Rafael Espindola7cf32212013-03-20 03:05:54 +0000242 case Action::AssembleJobClass:
243 return getAssemble();
244
245 case Action::LinkJobClass:
246 return getLink();
247
Rafael Espindolad15a8912013-03-19 00:36:57 +0000248 case Action::InputClass:
249 case Action::BindArchClass:
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000250 case Action::CudaDeviceClass:
251 case Action::CudaHostClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000252 case Action::LipoJobClass:
253 case Action::DsymutilJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000254 case Action::VerifyDebugInfoJobClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000255 llvm_unreachable("Invalid tool kind.");
256
257 case Action::CompileJobClass:
258 case Action::PrecompileJobClass:
259 case Action::PreprocessJobClass:
260 case Action::AnalyzeJobClass:
261 case Action::MigrateJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000262 case Action::VerifyPCHJobClass:
Bob Wilson23a55f12014-12-21 07:00:00 +0000263 case Action::BackendJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000264 return getClang();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000265 }
Benjamin Kramer5fbe2622013-03-21 19:45:46 +0000266
267 llvm_unreachable("Invalid tool kind.");
Rafael Espindolad15a8912013-03-19 00:36:57 +0000268}
269
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000270static StringRef getArchNameForCompilerRTLib(const ToolChain &TC,
271 const ArgList &Args) {
272 const llvm::Triple &Triple = TC.getTriple();
273 bool IsWindows = Triple.isOSWindows();
274
275 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86)
276 return "i386";
277
278 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb)
279 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows)
280 ? "armhf"
281 : "arm";
282
283 return TC.getArchName();
284}
285
286std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
287 bool Shared) const {
288 const llvm::Triple &TT = getTriple();
Evgeniy Stepanov14deb7b2015-10-08 21:21:44 +0000289 const char *Env = TT.isAndroid() ? "-android" : "";
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000290 bool IsITANMSVCWindows =
291 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
292
293 StringRef Arch = getArchNameForCompilerRTLib(*this, Args);
294 const char *Prefix = IsITANMSVCWindows ? "" : "lib";
295 const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so")
296 : (IsITANMSVCWindows ? ".lib" : ".a");
297
298 SmallString<128> Path(getDriver().ResourceDir);
299 StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS();
300 llvm::sys::path::append(Path, "lib", OSLibName);
301 llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" +
302 Arch + Env + Suffix);
303 return Path.str();
304}
305
Xinliang David Li69306c02015-10-22 06:15:31 +0000306const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
307 StringRef Component,
308 bool Shared) const {
309 return Args.MakeArgString(getCompilerRT(Args, Component, Shared));
310}
311
312bool ToolChain::needsProfileRT(const ArgList &Args) {
313 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
314 false) ||
315 Args.hasArg(options::OPT_fprofile_generate) ||
316 Args.hasArg(options::OPT_fprofile_generate_EQ) ||
317 Args.hasArg(options::OPT_fprofile_instr_generate) ||
318 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
319 Args.hasArg(options::OPT_fcreate_profile) ||
320 Args.hasArg(options::OPT_coverage))
321 return true;
322
323 return false;
324}
325
Rafael Espindola79764462013-03-24 15:06:53 +0000326Tool *ToolChain::SelectTool(const JobAction &JA) const {
Xinliang David Li69306c02015-10-22 06:15:31 +0000327 if (getDriver().ShouldUseClangCompiler(JA)) return getClang();
Rafael Espindola7cf32212013-03-20 03:05:54 +0000328 Action::ActionClass AC = JA.getKind();
329 if (AC == Action::AssembleJobClass && useIntegratedAs())
Rafael Espindola79764462013-03-24 15:06:53 +0000330 return getClangAs();
331 return getTool(AC);
Rafael Espindola260e28d2013-03-18 20:48:54 +0000332}
333
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +0000334std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +0000335 return D.GetFilePath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000336}
337
Simon Atanasyanb16488c2012-10-03 19:52:37 +0000338std::string ToolChain::GetProgramPath(const char *Name) const {
339 return D.GetProgramPath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000340}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000341
Logan Chieneb9162f2014-06-26 14:23:45 +0000342std::string ToolChain::GetLinkerPath() const {
343 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
344 StringRef Suffix = A->getValue();
345
346 // If we're passed -fuse-ld= with no argument, or with the argument ld,
347 // then use whatever the default system linker is.
348 if (Suffix.empty() || Suffix == "ld")
349 return GetProgramPath("ld");
350
351 llvm::SmallString<8> LinkerName("ld.");
352 LinkerName.append(Suffix);
353
354 std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
355 if (llvm::sys::fs::exists(LinkerPath))
356 return LinkerPath;
357
358 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
359 return "";
360 }
361
362 return GetProgramPath("ld");
363}
364
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000365types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
366 return types::lookupTypeForExtension(Ext);
367}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000368
Daniel Dunbar62123a12010-09-17 00:24:52 +0000369bool ToolChain::HasNativeLLVMSupport() const {
370 return false;
371}
372
Richard Barton5828d7b2013-12-17 11:11:25 +0000373bool ToolChain::isCrossCompiling() const {
374 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
375 switch (HostTriple.getArch()) {
Alp Tokerb164a342013-12-17 17:25:19 +0000376 // The A32/T32/T16 instruction sets are not separate architectures in this
377 // context.
378 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000379 case llvm::Triple::armeb:
Alp Tokerb164a342013-12-17 17:25:19 +0000380 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000381 case llvm::Triple::thumbeb:
382 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
383 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
Alp Tokerb164a342013-12-17 17:25:19 +0000384 default:
385 return HostTriple.getArch() != getArch();
Richard Barton5828d7b2013-12-17 11:11:25 +0000386 }
387}
388
John McCall5fb5df92012-06-20 06:18:46 +0000389ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnallb601c962012-07-03 20:49:52 +0000390 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall5fb5df92012-06-20 06:18:46 +0000391 VersionTuple());
John McCall24fc0de2011-07-06 00:26:06 +0000392}
393
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000394bool ToolChain::isThreadModelSupported(const StringRef Model) const {
395 if (Model == "single") {
Dan Gohmanc2853072015-09-03 22:51:53 +0000396 // FIXME: 'single' is only supported on ARM and WebAssembly so far.
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000397 return Triple.getArch() == llvm::Triple::arm ||
398 Triple.getArch() == llvm::Triple::armeb ||
399 Triple.getArch() == llvm::Triple::thumb ||
Dan Gohmanc2853072015-09-03 22:51:53 +0000400 Triple.getArch() == llvm::Triple::thumbeb ||
401 Triple.getArch() == llvm::Triple::wasm32 ||
402 Triple.getArch() == llvm::Triple::wasm64;
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000403 } else if (Model == "posix")
404 return true;
405
406 return false;
407}
408
Jim Grosbach82eee262013-11-16 00:53:35 +0000409std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000410 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000411 switch (getTriple().getArch()) {
412 default:
413 return getTripleString();
414
Jim Grosbach82eee262013-11-16 00:53:35 +0000415 case llvm::Triple::x86_64: {
416 llvm::Triple Triple = getTriple();
Tim Northover157d9112014-01-16 08:48:16 +0000417 if (!Triple.isOSBinFormatMachO())
Jim Grosbach82eee262013-11-16 00:53:35 +0000418 return getTripleString();
419
420 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
421 // x86_64h goes in the triple. Other -march options just use the
422 // vanilla triple we already have.
423 StringRef MArch = A->getValue();
424 if (MArch == "x86_64h")
425 Triple.setArchName(MArch);
426 }
427 return Triple.getTriple();
428 }
Tim Northover02a979f2014-07-24 10:25:34 +0000429 case llvm::Triple::aarch64: {
430 llvm::Triple Triple = getTriple();
431 if (!Triple.isOSBinFormatMachO())
432 return getTripleString();
433
434 // FIXME: older versions of ld64 expect the "arm64" component in the actual
435 // triple string and query it to determine whether an LTO file can be
436 // handled. Remove this when we don't care any more.
437 Triple.setArchName("arm64");
438 return Triple.getTriple();
439 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000440 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000441 case llvm::Triple::armeb:
442 case llvm::Triple::thumb:
443 case llvm::Triple::thumbeb: {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000444 // FIXME: Factor into subclasses.
445 llvm::Triple Triple = getTriple();
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000446 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
447 getTriple().getArch() == llvm::Triple::thumbeb;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000448
Christian Pirkerba289f02014-04-10 13:59:32 +0000449 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
450 // '-mbig-endian'/'-EB'.
451 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
452 options::OPT_mbig_endian)) {
David Blaikie7a3cbb22015-03-09 02:02:07 +0000453 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
Christian Pirkerba289f02014-04-10 13:59:32 +0000454 }
455
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000456 // Thumb2 is the default for V7 on Darwin.
457 //
458 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Renato Goline17c5802015-07-27 23:44:42 +0000459 StringRef MCPU, MArch;
460 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
461 MCPU = A->getValue();
462 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
463 MArch = A->getValue();
Chandler Carruthd96f37a2015-08-30 07:51:18 +0000464 std::string CPU =
465 Triple.isOSBinFormatMachO()
466 ? tools::arm::getARMCPUForMArch(MArch, Triple).str()
467 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000468 StringRef Suffix =
Vladimir Sukharev64f68242015-09-23 09:29:32 +0000469 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +0000470 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M;
471 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
472 getTriple().isOSBinFormatMachO());
Saleem Abdulrasoolf4c9e492014-04-04 20:31:19 +0000473 // FIXME: this is invalid for WindowsCE
474 if (getTriple().isOSWindows())
475 ThumbDefault = true;
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000476 std::string ArchName;
477 if (IsBigEndian)
478 ArchName = "armeb";
479 else
480 ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000481
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +0000482 // Assembly files should start in ARM mode, unless arch is M-profile.
Alexandros Lamprineasf6ecf962015-11-05 17:11:55 +0000483 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb,
484 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile) {
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000485 if (IsBigEndian)
486 ArchName = "thumbeb";
487 else
488 ArchName = "thumb";
489 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000490 Triple.setArchName(ArchName + Suffix.str());
491
492 return Triple.getTriple();
493 }
494 }
495}
496
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000497std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000498 types::ID InputType) const {
Chad Rosierd3a0f952011-09-20 20:44:06 +0000499 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000500}
501
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000502void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
503 ArgStringList &CC1Args) const {
504 // Each toolchain should provide the appropriate include flags.
505}
506
Chandler Carruth05fb5852012-11-21 23:40:23 +0000507void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
508 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +0000509}
510
Tim Northover336f1892014-03-29 13:16:12 +0000511void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
512
Xinliang David Li69306c02015-10-22 06:15:31 +0000513void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args,
514 llvm::opt::ArgStringList &CmdArgs) const {
515 if (!needsProfileRT(Args)) return;
516
517 CmdArgs.push_back(getCompilerRTArgString(Args, "profile"));
518 return;
519}
520
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000521ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
Xinliang David Li69306c02015-10-22 06:15:31 +0000522 const ArgList &Args) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000523 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000524 StringRef Value = A->getValue();
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000525 if (Value == "compiler-rt")
526 return ToolChain::RLT_CompilerRT;
527 if (Value == "libgcc")
528 return ToolChain::RLT_Libgcc;
529 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
530 << A->getAsString(Args);
531 }
532
533 return GetDefaultRuntimeLibType();
534}
535
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000536ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000537 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000538 StringRef Value = A->getValue();
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000539 if (Value == "libc++")
540 return ToolChain::CST_Libcxx;
541 if (Value == "libstdc++")
542 return ToolChain::CST_Libstdcxx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000543 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000544 << A->getAsString(Args);
545 }
546
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000547 return ToolChain::CST_Libstdcxx;
548}
549
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000550/// \brief Utility function to add a system include directory to CC1 arguments.
551/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
552 ArgStringList &CC1Args,
553 const Twine &Path) {
554 CC1Args.push_back("-internal-isystem");
555 CC1Args.push_back(DriverArgs.MakeArgString(Path));
556}
557
558/// \brief Utility function to add a system include directory with extern "C"
559/// semantics to CC1 arguments.
560///
561/// Note that this should be used rarely, and only for directories that
562/// historically and for legacy reasons are treated as having implicit extern
563/// "C" semantics. These semantics are *ignored* by and large today, but its
564/// important to preserve the preprocessor changes resulting from the
565/// classification.
566/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
567 ArgStringList &CC1Args,
568 const Twine &Path) {
569 CC1Args.push_back("-internal-externc-isystem");
570 CC1Args.push_back(DriverArgs.MakeArgString(Path));
571}
572
Simon Atanasyan08450bd2013-04-20 08:15:03 +0000573void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
574 ArgStringList &CC1Args,
575 const Twine &Path) {
576 if (llvm::sys::fs::exists(Path))
577 addExternCSystemInclude(DriverArgs, CC1Args, Path);
578}
579
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000580/// \brief Utility function to add a list of system include directories to CC1.
581/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
582 ArgStringList &CC1Args,
583 ArrayRef<StringRef> Paths) {
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000584 for (StringRef Path : Paths) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000585 CC1Args.push_back("-internal-isystem");
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000586 CC1Args.push_back(DriverArgs.MakeArgString(Path));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000587 }
588}
589
Chandler Carruth814db372011-11-04 23:49:01 +0000590void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
591 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000592 // Header search paths should be handled by each of the subclasses.
593 // Historically, they have not been, and instead have been handled inside of
594 // the CC1-layer frontend. As the logic is hoisted out, this generic function
595 // will slowly stop being called.
596 //
597 // While it is being called, replicate a bit of a hack to propagate the
598 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
599 // header search paths with it. Once all systems are overriding this
600 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000601 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000602}
603
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000604void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
605 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000606 CXXStdlibType Type = GetCXXStdlibType(Args);
607
608 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000609 case ToolChain::CST_Libcxx:
610 CmdArgs.push_back("-lc++");
611 break;
612
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000613 case ToolChain::CST_Libstdcxx:
614 CmdArgs.push_back("-lstdc++");
615 break;
616 }
617}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000618
Douglas Katzman6059ef92015-11-17 17:41:23 +0000619void ToolChain::AddFilePathLibArgs(const ArgList &Args,
620 ArgStringList &CmdArgs) const {
621 for (const auto &LibPath : getFilePaths())
622 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
623}
624
Shantonu Senafeb03b2010-09-17 18:39:08 +0000625void ToolChain::AddCCKextLibArgs(const ArgList &Args,
626 ArgStringList &CmdArgs) const {
627 CmdArgs.push_back("-lcc_kext");
628}
Benjamin Kramer058666a2012-10-04 19:42:20 +0000629
630bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
631 ArgStringList &CmdArgs) const {
Benjamin Kramerab88f622014-03-25 18:02:07 +0000632 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
633 // (to keep the linker options consistent with gcc and clang itself).
634 if (!isOptimizationLevelFast(Args)) {
635 // Check if -ffast-math or -funsafe-math.
636 Arg *A =
637 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
638 options::OPT_funsafe_math_optimizations,
639 options::OPT_fno_unsafe_math_optimizations);
Benjamin Kramer058666a2012-10-04 19:42:20 +0000640
Benjamin Kramerab88f622014-03-25 18:02:07 +0000641 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
642 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
643 return false;
644 }
Benjamin Kramer058666a2012-10-04 19:42:20 +0000645 // If crtfastmath.o exists add it to the arguments.
646 std::string Path = GetFilePath("crtfastmath.o");
647 if (Path == "crtfastmath.o") // Not found.
648 return false;
649
650 CmdArgs.push_back(Args.MakeArgString(Path));
651 return true;
652}
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000653
654SanitizerMask ToolChain::getSupportedSanitizers() const {
655 // Return sanitizers which don't require runtime support and are not
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000656 // platform dependent.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000657 using namespace SanitizerKind;
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000658 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
659 CFICastStrict | UnsignedIntegerOverflow | LocalBounds;
660 if (getTriple().getArch() == llvm::Triple::x86 ||
661 getTriple().getArch() == llvm::Triple::x86_64)
662 Res |= CFIICall;
663 return Res;
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000664}