blob: e4611d21b6d533142dd0eea8a0798bf6ed4aa91e [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"
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000026
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000027using namespace clang::driver;
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000028using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000029using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000030using namespace llvm::opt;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000031
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000032static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
33 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
34 options::OPT_fno_rtti, options::OPT_frtti);
35}
36
37static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
38 const llvm::Triple &Triple,
39 const Arg *CachedRTTIArg) {
40 // Explicit rtti/no-rtti args
41 if (CachedRTTIArg) {
42 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
43 return ToolChain::RM_EnabledExplicitly;
44 else
45 return ToolChain::RM_DisabledExplicitly;
46 }
47
48 // -frtti is default, except for the PS4 CPU.
49 if (!Triple.isPS4CPU())
50 return ToolChain::RM_EnabledImplicitly;
51
52 // On the PS4, turning on c++ exceptions turns on rtti.
53 // We're assuming that, if we see -fexceptions, rtti gets turned on.
Filipe Cabecinhas3e707d92015-03-20 23:33:23 +000054 Arg *Exceptions = Args.getLastArgNoClaim(
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000055 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
56 options::OPT_fexceptions, options::OPT_fno_exceptions);
57 if (Exceptions &&
58 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
59 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
60 return ToolChain::RM_EnabledImplicitly;
61
62 return ToolChain::RM_DisabledImplicitly;
63}
64
Rafael Espindola84b588b2013-03-18 18:10:27 +000065ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
Jonathan Roelofsb140a102014-10-03 21:57:44 +000066 const ArgList &Args)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000067 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
68 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
Jonathan Roelofsb140a102014-10-03 21:57:44 +000069 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
70 if (!isThreadModelSupported(A->getValue()))
71 D.Diag(diag::err_drv_invalid_thread_model_for_target)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000072 << A->getValue() << A->getAsString(Args);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000073}
74
Angel Garcia Gomezb5250d32015-10-20 12:52:55 +000075ToolChain::~ToolChain() = default;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000076
Benjamin Kramerd45b2052015-10-07 15:48:01 +000077vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); }
Daniel Dunbar083edf72009-12-21 18:54:17 +000078
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
Eric Christopher74a7c5d2015-09-25 17:44:31 +000091namespace {
92struct DriverSuffix {
93 const char *Suffix;
94 const char *ModeFlag;
95};
96
97const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
98 // A list of known driver suffixes. Suffixes are compared against the
99 // program name in order. If there is a match, the frontend type is updated as
100 // necessary by applying the ModeFlag.
101 static const DriverSuffix DriverSuffixes[] = {
102 {"clang", nullptr},
103 {"clang++", "--driver-mode=g++"},
104 {"clang-c++", "--driver-mode=g++"},
105 {"clang-cc", nullptr},
106 {"clang-cpp", "--driver-mode=cpp"},
107 {"clang-g++", "--driver-mode=g++"},
108 {"clang-gcc", nullptr},
109 {"clang-cl", "--driver-mode=cl"},
110 {"cc", nullptr},
111 {"cpp", "--driver-mode=cpp"},
112 {"cl", "--driver-mode=cl"},
113 {"++", "--driver-mode=g++"},
114 };
115
116 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
117 if (ProgName.endswith(DriverSuffixes[i].Suffix))
118 return &DriverSuffixes[i];
119 return nullptr;
120}
121
122/// Normalize the program name from argv[0] by stripping the file extension if
123/// present and lower-casing the string on Windows.
124std::string normalizeProgramName(llvm::StringRef Argv0) {
125 std::string ProgName = llvm::sys::path::stem(Argv0);
126#ifdef LLVM_ON_WIN32
127 // Transform to lowercase for case insensitive file systems.
128 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
129#endif
130 return ProgName;
131}
132
133const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
134 // Try to infer frontend type and default target from the program name by
135 // comparing it against DriverSuffixes in order.
136
137 // If there is a match, the function tries to identify a target as prefix.
138 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
139 // prefix "x86_64-linux". If such a target prefix is found, it may be
140 // added via -target as implicit first argument.
141 const DriverSuffix *DS = FindDriverSuffix(ProgName);
142
143 if (!DS) {
144 // Try again after stripping any trailing version number:
145 // clang++3.5 -> clang++
146 ProgName = ProgName.rtrim("0123456789.");
147 DS = FindDriverSuffix(ProgName);
148 }
149
150 if (!DS) {
151 // Try again after stripping trailing -component.
152 // clang++-tot -> clang++
153 ProgName = ProgName.slice(0, ProgName.rfind('-'));
154 DS = FindDriverSuffix(ProgName);
155 }
156 return DS;
157}
158} // anonymous namespace
159
160std::pair<std::string, std::string>
161ToolChain::getTargetAndModeFromProgramName(StringRef PN) {
162 std::string ProgName = normalizeProgramName(PN);
163 const DriverSuffix *DS = parseDriverSuffix(ProgName);
164 if (!DS)
165 return std::make_pair("", "");
166 std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag;
167
168 std::string::size_type LastComponent =
169 ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix));
170 if (LastComponent == std::string::npos)
171 return std::make_pair("", ModeFlag);
172
173 // Infer target from the prefix.
174 StringRef Prefix(ProgName);
175 Prefix = Prefix.slice(0, LastComponent);
176 std::string IgnoredError;
177 std::string Target;
178 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
179 Target = Prefix;
180 }
181 return std::make_pair(Target, ModeFlag);
182}
183
Rafael Espindola42db11e2014-07-25 19:22:51 +0000184StringRef ToolChain::getDefaultUniversalArchName() const {
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000185 // In universal driver terms, the arch name accepted by -arch isn't exactly
186 // the same as the ones that appear in the triple. Roughly speaking, this is
187 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
188 // only interesting special case is powerpc.
189 switch (Triple.getArch()) {
190 case llvm::Triple::ppc:
191 return "ppc";
192 case llvm::Triple::ppc64:
193 return "ppc64";
Bill Schmidt778d3872013-07-26 01:36:11 +0000194 case llvm::Triple::ppc64le:
195 return "ppc64le";
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000196 default:
197 return Triple.getArchName();
198 }
199}
200
Rafael Espindola151a9572012-09-23 03:05:41 +0000201bool ToolChain::IsUnwindTablesDefault() const {
202 return false;
203}
204
Rafael Espindola7cf32212013-03-20 03:05:54 +0000205Tool *ToolChain::getClang() const {
206 if (!Clang)
207 Clang.reset(new tools::Clang(*this));
208 return Clang.get();
209}
210
211Tool *ToolChain::buildAssembler() const {
212 return new tools::ClangAs(*this);
213}
214
215Tool *ToolChain::buildLinker() const {
216 llvm_unreachable("Linking is not supported by this toolchain");
217}
218
219Tool *ToolChain::getAssemble() const {
220 if (!Assemble)
221 Assemble.reset(buildAssembler());
222 return Assemble.get();
223}
224
225Tool *ToolChain::getClangAs() const {
226 if (!Assemble)
227 Assemble.reset(new tools::ClangAs(*this));
228 return Assemble.get();
229}
230
231Tool *ToolChain::getLink() const {
232 if (!Link)
233 Link.reset(buildLinker());
234 return Link.get();
235}
236
237Tool *ToolChain::getTool(Action::ActionClass AC) const {
Rafael Espindolad15a8912013-03-19 00:36:57 +0000238 switch (AC) {
Rafael Espindola7cf32212013-03-20 03:05:54 +0000239 case Action::AssembleJobClass:
240 return getAssemble();
241
242 case Action::LinkJobClass:
243 return getLink();
244
Rafael Espindolad15a8912013-03-19 00:36:57 +0000245 case Action::InputClass:
246 case Action::BindArchClass:
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000247 case Action::CudaDeviceClass:
248 case Action::CudaHostClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000249 case Action::LipoJobClass:
250 case Action::DsymutilJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000251 case Action::VerifyDebugInfoJobClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000252 llvm_unreachable("Invalid tool kind.");
253
254 case Action::CompileJobClass:
255 case Action::PrecompileJobClass:
256 case Action::PreprocessJobClass:
257 case Action::AnalyzeJobClass:
258 case Action::MigrateJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000259 case Action::VerifyPCHJobClass:
Bob Wilson23a55f12014-12-21 07:00:00 +0000260 case Action::BackendJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000261 return getClang();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000262 }
Benjamin Kramer5fbe2622013-03-21 19:45:46 +0000263
264 llvm_unreachable("Invalid tool kind.");
Rafael Espindolad15a8912013-03-19 00:36:57 +0000265}
266
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000267static StringRef getArchNameForCompilerRTLib(const ToolChain &TC,
268 const ArgList &Args) {
269 const llvm::Triple &Triple = TC.getTriple();
270 bool IsWindows = Triple.isOSWindows();
271
272 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86)
273 return "i386";
274
275 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb)
276 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows)
277 ? "armhf"
278 : "arm";
279
280 return TC.getArchName();
281}
282
283std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
284 bool Shared) const {
285 const llvm::Triple &TT = getTriple();
Evgeniy Stepanov14deb7b2015-10-08 21:21:44 +0000286 const char *Env = TT.isAndroid() ? "-android" : "";
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000287 bool IsITANMSVCWindows =
288 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
289
290 StringRef Arch = getArchNameForCompilerRTLib(*this, Args);
291 const char *Prefix = IsITANMSVCWindows ? "" : "lib";
292 const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so")
293 : (IsITANMSVCWindows ? ".lib" : ".a");
294
295 SmallString<128> Path(getDriver().ResourceDir);
296 StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS();
297 llvm::sys::path::append(Path, "lib", OSLibName);
298 llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" +
299 Arch + Env + Suffix);
300 return Path.str();
301}
302
Rafael Espindola79764462013-03-24 15:06:53 +0000303Tool *ToolChain::SelectTool(const JobAction &JA) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +0000304 if (getDriver().ShouldUseClangCompiler(JA))
Rafael Espindola79764462013-03-24 15:06:53 +0000305 return getClang();
Rafael Espindola7cf32212013-03-20 03:05:54 +0000306 Action::ActionClass AC = JA.getKind();
307 if (AC == Action::AssembleJobClass && useIntegratedAs())
Rafael Espindola79764462013-03-24 15:06:53 +0000308 return getClangAs();
309 return getTool(AC);
Rafael Espindola260e28d2013-03-18 20:48:54 +0000310}
311
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +0000312std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +0000313 return D.GetFilePath(Name, *this);
Vasileios Kalintirisfdfc0102015-10-05 10:34:46 +0000314
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000315}
316
Simon Atanasyanb16488c2012-10-03 19:52:37 +0000317std::string ToolChain::GetProgramPath(const char *Name) const {
318 return D.GetProgramPath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000319}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000320
Logan Chieneb9162f2014-06-26 14:23:45 +0000321std::string ToolChain::GetLinkerPath() const {
322 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
323 StringRef Suffix = A->getValue();
324
325 // If we're passed -fuse-ld= with no argument, or with the argument ld,
326 // then use whatever the default system linker is.
327 if (Suffix.empty() || Suffix == "ld")
328 return GetProgramPath("ld");
329
330 llvm::SmallString<8> LinkerName("ld.");
331 LinkerName.append(Suffix);
332
333 std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
334 if (llvm::sys::fs::exists(LinkerPath))
335 return LinkerPath;
336
337 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
338 return "";
339 }
340
341 return GetProgramPath("ld");
342}
343
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000344types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
345 return types::lookupTypeForExtension(Ext);
346}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000347
Daniel Dunbar62123a12010-09-17 00:24:52 +0000348bool ToolChain::HasNativeLLVMSupport() const {
349 return false;
350}
351
Richard Barton5828d7b2013-12-17 11:11:25 +0000352bool ToolChain::isCrossCompiling() const {
353 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
354 switch (HostTriple.getArch()) {
Alp Tokerb164a342013-12-17 17:25:19 +0000355 // The A32/T32/T16 instruction sets are not separate architectures in this
356 // context.
357 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000358 case llvm::Triple::armeb:
Alp Tokerb164a342013-12-17 17:25:19 +0000359 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000360 case llvm::Triple::thumbeb:
361 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
362 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
Alp Tokerb164a342013-12-17 17:25:19 +0000363 default:
364 return HostTriple.getArch() != getArch();
Richard Barton5828d7b2013-12-17 11:11:25 +0000365 }
366}
367
John McCall5fb5df92012-06-20 06:18:46 +0000368ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnallb601c962012-07-03 20:49:52 +0000369 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall5fb5df92012-06-20 06:18:46 +0000370 VersionTuple());
John McCall24fc0de2011-07-06 00:26:06 +0000371}
372
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000373bool ToolChain::isThreadModelSupported(const StringRef Model) const {
374 if (Model == "single") {
Dan Gohmanc2853072015-09-03 22:51:53 +0000375 // FIXME: 'single' is only supported on ARM and WebAssembly so far.
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000376 return Triple.getArch() == llvm::Triple::arm ||
377 Triple.getArch() == llvm::Triple::armeb ||
378 Triple.getArch() == llvm::Triple::thumb ||
Dan Gohmanc2853072015-09-03 22:51:53 +0000379 Triple.getArch() == llvm::Triple::thumbeb ||
380 Triple.getArch() == llvm::Triple::wasm32 ||
381 Triple.getArch() == llvm::Triple::wasm64;
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000382 } else if (Model == "posix")
383 return true;
384
385 return false;
386}
387
Jim Grosbach82eee262013-11-16 00:53:35 +0000388std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000389 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000390 switch (getTriple().getArch()) {
391 default:
392 return getTripleString();
393
Jim Grosbach82eee262013-11-16 00:53:35 +0000394 case llvm::Triple::x86_64: {
395 llvm::Triple Triple = getTriple();
Tim Northover157d9112014-01-16 08:48:16 +0000396 if (!Triple.isOSBinFormatMachO())
Jim Grosbach82eee262013-11-16 00:53:35 +0000397 return getTripleString();
398
399 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
400 // x86_64h goes in the triple. Other -march options just use the
401 // vanilla triple we already have.
402 StringRef MArch = A->getValue();
403 if (MArch == "x86_64h")
404 Triple.setArchName(MArch);
405 }
406 return Triple.getTriple();
407 }
Tim Northover02a979f2014-07-24 10:25:34 +0000408 case llvm::Triple::aarch64: {
409 llvm::Triple Triple = getTriple();
410 if (!Triple.isOSBinFormatMachO())
411 return getTripleString();
412
413 // FIXME: older versions of ld64 expect the "arm64" component in the actual
414 // triple string and query it to determine whether an LTO file can be
415 // handled. Remove this when we don't care any more.
416 Triple.setArchName("arm64");
417 return Triple.getTriple();
418 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000419 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000420 case llvm::Triple::armeb:
421 case llvm::Triple::thumb:
422 case llvm::Triple::thumbeb: {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000423 // FIXME: Factor into subclasses.
424 llvm::Triple Triple = getTriple();
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000425 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
426 getTriple().getArch() == llvm::Triple::thumbeb;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000427
Christian Pirkerba289f02014-04-10 13:59:32 +0000428 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
429 // '-mbig-endian'/'-EB'.
430 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
431 options::OPT_mbig_endian)) {
David Blaikie7a3cbb22015-03-09 02:02:07 +0000432 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
Christian Pirkerba289f02014-04-10 13:59:32 +0000433 }
434
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000435 // Thumb2 is the default for V7 on Darwin.
436 //
437 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Renato Goline17c5802015-07-27 23:44:42 +0000438 StringRef MCPU, MArch;
439 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
440 MCPU = A->getValue();
441 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
442 MArch = A->getValue();
Chandler Carruthd96f37a2015-08-30 07:51:18 +0000443 std::string CPU =
444 Triple.isOSBinFormatMachO()
445 ? tools::arm::getARMCPUForMArch(MArch, Triple).str()
446 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000447 StringRef Suffix =
Vladimir Sukharev64f68242015-09-23 09:29:32 +0000448 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
Bernard Ogden8af41b52013-12-12 13:27:04 +0000449 bool ThumbDefault = Suffix.startswith("v6m") || Suffix.startswith("v7m") ||
450 Suffix.startswith("v7em") ||
Tim Northover157d9112014-01-16 08:48:16 +0000451 (Suffix.startswith("v7") && getTriple().isOSBinFormatMachO());
Saleem Abdulrasoolf4c9e492014-04-04 20:31:19 +0000452 // FIXME: this is invalid for WindowsCE
453 if (getTriple().isOSWindows())
454 ThumbDefault = true;
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000455 std::string ArchName;
456 if (IsBigEndian)
457 ArchName = "armeb";
458 else
459 ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000460
461 // Assembly files should start in ARM mode.
462 if (InputType != types::TY_PP_Asm &&
463 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000464 {
465 if (IsBigEndian)
466 ArchName = "thumbeb";
467 else
468 ArchName = "thumb";
469 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000470 Triple.setArchName(ArchName + Suffix.str());
471
472 return Triple.getTriple();
473 }
474 }
475}
476
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000477std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000478 types::ID InputType) const {
Chad Rosierd3a0f952011-09-20 20:44:06 +0000479 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000480}
481
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000482void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
483 ArgStringList &CC1Args) const {
484 // Each toolchain should provide the appropriate include flags.
485}
486
Chandler Carruth05fb5852012-11-21 23:40:23 +0000487void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
488 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +0000489}
490
Tim Northover336f1892014-03-29 13:16:12 +0000491void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
492
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000493ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
494 const ArgList &Args) const
495{
496 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000497 StringRef Value = A->getValue();
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000498 if (Value == "compiler-rt")
499 return ToolChain::RLT_CompilerRT;
500 if (Value == "libgcc")
501 return ToolChain::RLT_Libgcc;
502 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
503 << A->getAsString(Args);
504 }
505
506 return GetDefaultRuntimeLibType();
507}
508
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000509ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000510 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000511 StringRef Value = A->getValue();
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000512 if (Value == "libc++")
513 return ToolChain::CST_Libcxx;
514 if (Value == "libstdc++")
515 return ToolChain::CST_Libstdcxx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000516 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000517 << A->getAsString(Args);
518 }
519
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000520 return ToolChain::CST_Libstdcxx;
521}
522
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000523/// \brief Utility function to add a system include directory to CC1 arguments.
524/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
525 ArgStringList &CC1Args,
526 const Twine &Path) {
527 CC1Args.push_back("-internal-isystem");
528 CC1Args.push_back(DriverArgs.MakeArgString(Path));
529}
530
531/// \brief Utility function to add a system include directory with extern "C"
532/// semantics to CC1 arguments.
533///
534/// Note that this should be used rarely, and only for directories that
535/// historically and for legacy reasons are treated as having implicit extern
536/// "C" semantics. These semantics are *ignored* by and large today, but its
537/// important to preserve the preprocessor changes resulting from the
538/// classification.
539/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
540 ArgStringList &CC1Args,
541 const Twine &Path) {
542 CC1Args.push_back("-internal-externc-isystem");
543 CC1Args.push_back(DriverArgs.MakeArgString(Path));
544}
545
Simon Atanasyan08450bd2013-04-20 08:15:03 +0000546void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
547 ArgStringList &CC1Args,
548 const Twine &Path) {
549 if (llvm::sys::fs::exists(Path))
550 addExternCSystemInclude(DriverArgs, CC1Args, Path);
551}
552
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000553/// \brief Utility function to add a list of system include directories to CC1.
554/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
555 ArgStringList &CC1Args,
556 ArrayRef<StringRef> Paths) {
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000557 for (StringRef Path : Paths) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000558 CC1Args.push_back("-internal-isystem");
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000559 CC1Args.push_back(DriverArgs.MakeArgString(Path));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000560 }
561}
562
Chandler Carruth814db372011-11-04 23:49:01 +0000563void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
564 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000565 // Header search paths should be handled by each of the subclasses.
566 // Historically, they have not been, and instead have been handled inside of
567 // the CC1-layer frontend. As the logic is hoisted out, this generic function
568 // will slowly stop being called.
569 //
570 // While it is being called, replicate a bit of a hack to propagate the
571 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
572 // header search paths with it. Once all systems are overriding this
573 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000574 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000575}
576
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000577void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
578 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000579 CXXStdlibType Type = GetCXXStdlibType(Args);
580
581 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000582 case ToolChain::CST_Libcxx:
583 CmdArgs.push_back("-lc++");
584 break;
585
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000586 case ToolChain::CST_Libstdcxx:
587 CmdArgs.push_back("-lstdc++");
588 break;
589 }
590}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000591
592void ToolChain::AddCCKextLibArgs(const ArgList &Args,
593 ArgStringList &CmdArgs) const {
594 CmdArgs.push_back("-lcc_kext");
595}
Benjamin Kramer058666a2012-10-04 19:42:20 +0000596
597bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
598 ArgStringList &CmdArgs) const {
Benjamin Kramerab88f622014-03-25 18:02:07 +0000599 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
600 // (to keep the linker options consistent with gcc and clang itself).
601 if (!isOptimizationLevelFast(Args)) {
602 // Check if -ffast-math or -funsafe-math.
603 Arg *A =
604 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
605 options::OPT_funsafe_math_optimizations,
606 options::OPT_fno_unsafe_math_optimizations);
Benjamin Kramer058666a2012-10-04 19:42:20 +0000607
Benjamin Kramerab88f622014-03-25 18:02:07 +0000608 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
609 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
610 return false;
611 }
Benjamin Kramer058666a2012-10-04 19:42:20 +0000612 // If crtfastmath.o exists add it to the arguments.
613 std::string Path = GetFilePath("crtfastmath.o");
614 if (Path == "crtfastmath.o") // Not found.
615 return false;
616
617 CmdArgs.push_back(Args.MakeArgString(Path));
618 return true;
619}
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000620
621SanitizerMask ToolChain::getSupportedSanitizers() const {
622 // Return sanitizers which don't require runtime support and are not
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000623 // platform dependent.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000624 using namespace SanitizerKind;
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000625 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
626 CFICastStrict | UnsignedIntegerOverflow | LocalBounds;
627 if (getTriple().getArch() == llvm::Triple::x86 ||
628 getTriple().getArch() == llvm::Triple::x86_64)
629 Res |= CFIICall;
630 return Res;
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000631}