blob: b8dd37ca5776ceb53fdf2e10375fde8906dda4e2 [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
Mehdi Amini9670f842016-07-18 19:02:11 +000010#include "clang/Driver/ToolChain.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000011#include "ToolChains/CommonArgs.h"
12#include "ToolChains/Arch/ARM.h"
13#include "ToolChains/Clang.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Basic/ObjCRuntime.h"
Pirama Arumuga Nainar65a16dd2017-03-03 23:20:49 +000015#include "clang/Basic/VirtualFileSystem.h"
Jonas Hahnfeldaae83742016-02-12 07:48:37 +000016#include "clang/Config/config.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000017#include "clang/Driver/Action.h"
18#include "clang/Driver/Driver.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000019#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000020#include "clang/Driver/Options.h"
Peter Collingbourne32701642013-11-01 18:16:25 +000021#include "clang/Driver/SanitizerArgs.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000022#include "clang/Driver/XRayArgs.h"
Logan Chieneb9162f2014-06-26 14:23:45 +000023#include "llvm/ADT/SmallString.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000024#include "llvm/Option/Arg.h"
25#include "llvm/Option/ArgList.h"
26#include "llvm/Option/Option.h"
John McCall24fc0de2011-07-06 00:26:06 +000027#include "llvm/Support/ErrorHandling.h"
Simon Atanasyan08450bd2013-04-20 08:15:03 +000028#include "llvm/Support/FileSystem.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000029#include "llvm/Support/Path.h"
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +000030#include "llvm/Support/TargetParser.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000031#include "llvm/Support/TargetRegistry.h"
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000032
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000033using namespace clang::driver;
Vasileios Kalintiris447e3572015-10-01 16:54:58 +000034using namespace clang::driver::tools;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035using namespace clang;
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +000036using namespace llvm;
Reid Kleckner898229a2013-06-14 17:17:23 +000037using namespace llvm::opt;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000038
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000039static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
40 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
41 options::OPT_fno_rtti, options::OPT_frtti);
42}
43
44static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
45 const llvm::Triple &Triple,
46 const Arg *CachedRTTIArg) {
47 // Explicit rtti/no-rtti args
48 if (CachedRTTIArg) {
49 if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
50 return ToolChain::RM_EnabledExplicitly;
51 else
52 return ToolChain::RM_DisabledExplicitly;
53 }
54
55 // -frtti is default, except for the PS4 CPU.
56 if (!Triple.isPS4CPU())
57 return ToolChain::RM_EnabledImplicitly;
58
59 // On the PS4, turning on c++ exceptions turns on rtti.
60 // We're assuming that, if we see -fexceptions, rtti gets turned on.
Filipe Cabecinhas3e707d92015-03-20 23:33:23 +000061 Arg *Exceptions = Args.getLastArgNoClaim(
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000062 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions,
63 options::OPT_fexceptions, options::OPT_fno_exceptions);
64 if (Exceptions &&
65 (Exceptions->getOption().matches(options::OPT_fexceptions) ||
66 Exceptions->getOption().matches(options::OPT_fcxx_exceptions)))
67 return ToolChain::RM_EnabledImplicitly;
68
69 return ToolChain::RM_DisabledImplicitly;
70}
71
Rafael Espindola84b588b2013-03-18 18:10:27 +000072ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
Jonathan Roelofsb140a102014-10-03 21:57:44 +000073 const ArgList &Args)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000074 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
Vedant Kumar18286cf2016-07-27 23:02:20 +000075 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)),
76 EffectiveTriple() {
Jonathan Roelofsb140a102014-10-03 21:57:44 +000077 if (Arg *A = Args.getLastArg(options::OPT_mthread_model))
78 if (!isThreadModelSupported(A->getValue()))
79 D.Diag(diag::err_drv_invalid_thread_model_for_target)
Filipe Cabecinhasec5d0e62015-02-19 01:04:49 +000080 << A->getValue() << A->getAsString(Args);
Pirama Arumuga Nainar65a16dd2017-03-03 23:20:49 +000081
82 std::string CandidateLibPath = getArchSpecificLibPath();
83 if (getVFS().exists(CandidateLibPath))
84 getFilePaths().push_back(CandidateLibPath);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000085}
86
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000087ToolChain::~ToolChain() {
88}
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000089
Benjamin Kramerd45b2052015-10-07 15:48:01 +000090vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); }
Daniel Dunbar083edf72009-12-21 18:54:17 +000091
Rafael Espindola84b588b2013-03-18 18:10:27 +000092bool ToolChain::useIntegratedAs() const {
Saleem Abdulrasoolcfeb90d2014-02-23 00:40:30 +000093 return Args.hasFlag(options::OPT_fintegrated_as,
94 options::OPT_fno_integrated_as,
Rafael Espindola248e2192013-03-18 17:52:57 +000095 IsIntegratedAssemblerDefault());
96}
97
Alexey Samsonov609213f92013-08-19 09:14:21 +000098const SanitizerArgs& ToolChain::getSanitizerArgs() const {
Peter Collingbourne32701642013-11-01 18:16:25 +000099 if (!SanitizerArguments.get())
100 SanitizerArguments.reset(new SanitizerArgs(*this, Args));
101 return *SanitizerArguments.get();
Alexey Samsonov609213f92013-08-19 09:14:21 +0000102}
103
Dean Michael Berris835832d2017-03-30 00:29:36 +0000104const XRayArgs& ToolChain::getXRayArgs() const {
105 if (!XRayArguments.get())
106 XRayArguments.reset(new XRayArgs(*this, Args));
107 return *XRayArguments.get();
108}
109
Eric Christopher74a7c5d2015-09-25 17:44:31 +0000110namespace {
111struct DriverSuffix {
112 const char *Suffix;
113 const char *ModeFlag;
114};
115
116const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
117 // A list of known driver suffixes. Suffixes are compared against the
118 // program name in order. If there is a match, the frontend type is updated as
119 // necessary by applying the ModeFlag.
120 static const DriverSuffix DriverSuffixes[] = {
121 {"clang", nullptr},
122 {"clang++", "--driver-mode=g++"},
123 {"clang-c++", "--driver-mode=g++"},
124 {"clang-cc", nullptr},
125 {"clang-cpp", "--driver-mode=cpp"},
126 {"clang-g++", "--driver-mode=g++"},
127 {"clang-gcc", nullptr},
128 {"clang-cl", "--driver-mode=cl"},
129 {"cc", nullptr},
130 {"cpp", "--driver-mode=cpp"},
131 {"cl", "--driver-mode=cl"},
132 {"++", "--driver-mode=g++"},
133 };
134
135 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
136 if (ProgName.endswith(DriverSuffixes[i].Suffix))
137 return &DriverSuffixes[i];
138 return nullptr;
139}
140
141/// Normalize the program name from argv[0] by stripping the file extension if
142/// present and lower-casing the string on Windows.
143std::string normalizeProgramName(llvm::StringRef Argv0) {
144 std::string ProgName = llvm::sys::path::stem(Argv0);
145#ifdef LLVM_ON_WIN32
146 // Transform to lowercase for case insensitive file systems.
147 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
148#endif
149 return ProgName;
150}
151
152const DriverSuffix *parseDriverSuffix(StringRef ProgName) {
153 // Try to infer frontend type and default target from the program name by
154 // comparing it against DriverSuffixes in order.
155
156 // If there is a match, the function tries to identify a target as prefix.
157 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
158 // prefix "x86_64-linux". If such a target prefix is found, it may be
159 // added via -target as implicit first argument.
160 const DriverSuffix *DS = FindDriverSuffix(ProgName);
161
162 if (!DS) {
163 // Try again after stripping any trailing version number:
164 // clang++3.5 -> clang++
165 ProgName = ProgName.rtrim("0123456789.");
166 DS = FindDriverSuffix(ProgName);
167 }
168
169 if (!DS) {
170 // Try again after stripping trailing -component.
171 // clang++-tot -> clang++
172 ProgName = ProgName.slice(0, ProgName.rfind('-'));
173 DS = FindDriverSuffix(ProgName);
174 }
175 return DS;
176}
177} // anonymous namespace
178
179std::pair<std::string, std::string>
180ToolChain::getTargetAndModeFromProgramName(StringRef PN) {
181 std::string ProgName = normalizeProgramName(PN);
182 const DriverSuffix *DS = parseDriverSuffix(ProgName);
183 if (!DS)
184 return std::make_pair("", "");
185 std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag;
186
187 std::string::size_type LastComponent =
188 ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix));
189 if (LastComponent == std::string::npos)
190 return std::make_pair("", ModeFlag);
191
192 // Infer target from the prefix.
193 StringRef Prefix(ProgName);
194 Prefix = Prefix.slice(0, LastComponent);
195 std::string IgnoredError;
196 std::string Target;
197 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
198 Target = Prefix;
199 }
200 return std::make_pair(Target, ModeFlag);
201}
202
Rafael Espindola42db11e2014-07-25 19:22:51 +0000203StringRef ToolChain::getDefaultUniversalArchName() const {
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000204 // In universal driver terms, the arch name accepted by -arch isn't exactly
205 // the same as the ones that appear in the triple. Roughly speaking, this is
206 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
207 // only interesting special case is powerpc.
208 switch (Triple.getArch()) {
209 case llvm::Triple::ppc:
210 return "ppc";
211 case llvm::Triple::ppc64:
212 return "ppc64";
Bill Schmidt778d3872013-07-26 01:36:11 +0000213 case llvm::Triple::ppc64le:
214 return "ppc64le";
Daniel Dunbarc3bd9f52012-11-08 03:38:26 +0000215 default:
216 return Triple.getArchName();
217 }
218}
219
Rafael Espindola151a9572012-09-23 03:05:41 +0000220bool ToolChain::IsUnwindTablesDefault() const {
221 return false;
222}
223
Rafael Espindola7cf32212013-03-20 03:05:54 +0000224Tool *ToolChain::getClang() const {
225 if (!Clang)
226 Clang.reset(new tools::Clang(*this));
227 return Clang.get();
228}
229
230Tool *ToolChain::buildAssembler() const {
231 return new tools::ClangAs(*this);
232}
233
234Tool *ToolChain::buildLinker() const {
235 llvm_unreachable("Linking is not supported by this toolchain");
236}
237
238Tool *ToolChain::getAssemble() const {
239 if (!Assemble)
240 Assemble.reset(buildAssembler());
241 return Assemble.get();
242}
243
244Tool *ToolChain::getClangAs() const {
245 if (!Assemble)
246 Assemble.reset(new tools::ClangAs(*this));
247 return Assemble.get();
248}
249
250Tool *ToolChain::getLink() const {
251 if (!Link)
252 Link.reset(buildLinker());
253 return Link.get();
254}
255
Samuel Antao7cab8f12016-10-27 18:04:42 +0000256Tool *ToolChain::getOffloadBundler() const {
257 if (!OffloadBundler)
258 OffloadBundler.reset(new tools::OffloadBundler(*this));
259 return OffloadBundler.get();
260}
261
Rafael Espindola7cf32212013-03-20 03:05:54 +0000262Tool *ToolChain::getTool(Action::ActionClass AC) const {
Rafael Espindolad15a8912013-03-19 00:36:57 +0000263 switch (AC) {
Rafael Espindola7cf32212013-03-20 03:05:54 +0000264 case Action::AssembleJobClass:
265 return getAssemble();
266
267 case Action::LinkJobClass:
268 return getLink();
269
Rafael Espindolad15a8912013-03-19 00:36:57 +0000270 case Action::InputClass:
271 case Action::BindArchClass:
Samuel Antaod06239d2016-07-15 23:13:27 +0000272 case Action::OffloadClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000273 case Action::LipoJobClass:
274 case Action::DsymutilJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000275 case Action::VerifyDebugInfoJobClass:
Rafael Espindolad15a8912013-03-19 00:36:57 +0000276 llvm_unreachable("Invalid tool kind.");
277
278 case Action::CompileJobClass:
279 case Action::PrecompileJobClass:
280 case Action::PreprocessJobClass:
281 case Action::AnalyzeJobClass:
282 case Action::MigrateJobClass:
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000283 case Action::VerifyPCHJobClass:
Bob Wilson23a55f12014-12-21 07:00:00 +0000284 case Action::BackendJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000285 return getClang();
Samuel Antao69d6f312016-10-27 17:50:43 +0000286
287 case Action::OffloadBundlingJobClass:
Samuel Antaofab4f372016-10-27 18:00:51 +0000288 case Action::OffloadUnbundlingJobClass:
Samuel Antao7cab8f12016-10-27 18:04:42 +0000289 return getOffloadBundler();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000290 }
Benjamin Kramer5fbe2622013-03-21 19:45:46 +0000291
292 llvm_unreachable("Invalid tool kind.");
Rafael Espindolad15a8912013-03-19 00:36:57 +0000293}
294
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000295static StringRef getArchNameForCompilerRTLib(const ToolChain &TC,
296 const ArgList &Args) {
297 const llvm::Triple &Triple = TC.getTriple();
298 bool IsWindows = Triple.isOSWindows();
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000299
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000300 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86)
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000301 return "i386";
302
303 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb)
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000304 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows)
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000305 ? "armhf"
306 : "arm";
307
308 return TC.getArchName();
309}
310
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000311std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000312 bool Shared) const {
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000313 const llvm::Triple &TT = getTriple();
314 const char *Env = TT.isAndroid() ? "-android" : "";
Saleem Abdulrasool667d6302016-08-31 19:27:07 +0000315 bool IsITANMSVCWindows =
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000316 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000317
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000318 StringRef Arch = getArchNameForCompilerRTLib(*this, Args);
Saleem Abdulrasool667d6302016-08-31 19:27:07 +0000319 const char *Prefix = IsITANMSVCWindows ? "" : "lib";
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000320 const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so")
Saleem Abdulrasool667d6302016-08-31 19:27:07 +0000321 : (IsITANMSVCWindows ? ".lib" : ".a");
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000322
323 SmallString<128> Path(getDriver().ResourceDir);
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000324 StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS();
Vasileios Kalintiris447e3572015-10-01 16:54:58 +0000325 llvm::sys::path::append(Path, "lib", OSLibName);
326 llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" +
327 Arch + Env + Suffix);
328 return Path.str();
329}
330
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000331const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
332 StringRef Component,
333 bool Shared) const {
334 return Args.MakeArgString(getCompilerRT(Args, Component, Shared));
Xinliang David Li69306c02015-10-22 06:15:31 +0000335}
336
Pirama Arumuga Nainar65a16dd2017-03-03 23:20:49 +0000337std::string ToolChain::getArchSpecificLibPath() const {
338 SmallString<128> Path(getDriver().ResourceDir);
339 StringRef OSLibName = getTriple().isOSFreeBSD() ? "freebsd" : getOS();
340 llvm::sys::path::append(Path, "lib", OSLibName,
341 llvm::Triple::getArchTypeName(getArch()));
342 return Path.str();
343}
344
Xinliang David Li69306c02015-10-22 06:15:31 +0000345bool ToolChain::needsProfileRT(const ArgList &Args) {
346 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
347 false) ||
348 Args.hasArg(options::OPT_fprofile_generate) ||
349 Args.hasArg(options::OPT_fprofile_generate_EQ) ||
350 Args.hasArg(options::OPT_fprofile_instr_generate) ||
351 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
352 Args.hasArg(options::OPT_fcreate_profile) ||
353 Args.hasArg(options::OPT_coverage))
354 return true;
355
356 return false;
357}
358
Rafael Espindola79764462013-03-24 15:06:53 +0000359Tool *ToolChain::SelectTool(const JobAction &JA) const {
Xinliang David Li69306c02015-10-22 06:15:31 +0000360 if (getDriver().ShouldUseClangCompiler(JA)) return getClang();
Rafael Espindola7cf32212013-03-20 03:05:54 +0000361 Action::ActionClass AC = JA.getKind();
362 if (AC == Action::AssembleJobClass && useIntegratedAs())
Rafael Espindola79764462013-03-24 15:06:53 +0000363 return getClangAs();
364 return getTool(AC);
Rafael Espindola260e28d2013-03-18 20:48:54 +0000365}
366
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +0000367std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +0000368 return D.GetFilePath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000369}
370
Simon Atanasyanb16488c2012-10-03 19:52:37 +0000371std::string ToolChain::GetProgramPath(const char *Name) const {
372 return D.GetProgramPath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +0000373}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000374
Logan Chieneb9162f2014-06-26 14:23:45 +0000375std::string ToolChain::GetLinkerPath() const {
Petr Hosekfe2c2b02016-12-14 16:46:50 +0000376 const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
377 StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
Logan Chieneb9162f2014-06-26 14:23:45 +0000378
Petr Hosekfe2c2b02016-12-14 16:46:50 +0000379 if (llvm::sys::path::is_absolute(UseLinker)) {
380 // If we're passed what looks like an absolute path, don't attempt to
381 // second-guess that.
382 if (llvm::sys::fs::exists(UseLinker))
383 return UseLinker;
384 } else if (UseLinker.empty() || UseLinker == "ld") {
385 // If we're passed -fuse-ld= with no argument, or with the argument ld,
386 // then use whatever the default system linker is.
387 return GetProgramPath(getDefaultLinker());
388 } else {
389 llvm::SmallString<8> LinkerName("ld.");
390 LinkerName.append(UseLinker);
Logan Chieneb9162f2014-06-26 14:23:45 +0000391
Petr Hosekfe2c2b02016-12-14 16:46:50 +0000392 std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
393 if (llvm::sys::fs::exists(LinkerPath))
394 return LinkerPath;
Logan Chieneb9162f2014-06-26 14:23:45 +0000395 }
396
Petr Hosekfe2c2b02016-12-14 16:46:50 +0000397 if (A)
398 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
399
400 return GetProgramPath(getDefaultLinker());
Logan Chieneb9162f2014-06-26 14:23:45 +0000401}
402
Mehdi Amini6fcd4eb2016-10-07 21:41:00 +0000403types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const {
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +0000404 return types::lookupTypeForExtension(Ext);
405}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000406
Daniel Dunbar62123a12010-09-17 00:24:52 +0000407bool ToolChain::HasNativeLLVMSupport() const {
408 return false;
409}
410
Richard Barton5828d7b2013-12-17 11:11:25 +0000411bool ToolChain::isCrossCompiling() const {
412 llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
413 switch (HostTriple.getArch()) {
Alp Tokerb164a342013-12-17 17:25:19 +0000414 // The A32/T32/T16 instruction sets are not separate architectures in this
415 // context.
416 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000417 case llvm::Triple::armeb:
Alp Tokerb164a342013-12-17 17:25:19 +0000418 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000419 case llvm::Triple::thumbeb:
420 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
421 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
Alp Tokerb164a342013-12-17 17:25:19 +0000422 default:
423 return HostTriple.getArch() != getArch();
Richard Barton5828d7b2013-12-17 11:11:25 +0000424 }
425}
426
John McCall5fb5df92012-06-20 06:18:46 +0000427ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnallb601c962012-07-03 20:49:52 +0000428 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall5fb5df92012-06-20 06:18:46 +0000429 VersionTuple());
John McCall24fc0de2011-07-06 00:26:06 +0000430}
431
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000432bool ToolChain::isThreadModelSupported(const StringRef Model) const {
433 if (Model == "single") {
Dan Gohmanc2853072015-09-03 22:51:53 +0000434 // FIXME: 'single' is only supported on ARM and WebAssembly so far.
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000435 return Triple.getArch() == llvm::Triple::arm ||
436 Triple.getArch() == llvm::Triple::armeb ||
437 Triple.getArch() == llvm::Triple::thumb ||
Dan Gohmanc2853072015-09-03 22:51:53 +0000438 Triple.getArch() == llvm::Triple::thumbeb ||
439 Triple.getArch() == llvm::Triple::wasm32 ||
440 Triple.getArch() == llvm::Triple::wasm64;
Jonathan Roelofsb140a102014-10-03 21:57:44 +0000441 } else if (Model == "posix")
442 return true;
443
444 return false;
445}
446
Jim Grosbach82eee262013-11-16 00:53:35 +0000447std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000448 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000449 switch (getTriple().getArch()) {
450 default:
451 return getTripleString();
452
Jim Grosbach82eee262013-11-16 00:53:35 +0000453 case llvm::Triple::x86_64: {
454 llvm::Triple Triple = getTriple();
Tim Northover157d9112014-01-16 08:48:16 +0000455 if (!Triple.isOSBinFormatMachO())
Jim Grosbach82eee262013-11-16 00:53:35 +0000456 return getTripleString();
457
458 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
459 // x86_64h goes in the triple. Other -march options just use the
460 // vanilla triple we already have.
461 StringRef MArch = A->getValue();
462 if (MArch == "x86_64h")
463 Triple.setArchName(MArch);
464 }
465 return Triple.getTriple();
466 }
Tim Northover02a979f2014-07-24 10:25:34 +0000467 case llvm::Triple::aarch64: {
468 llvm::Triple Triple = getTriple();
469 if (!Triple.isOSBinFormatMachO())
470 return getTripleString();
471
472 // FIXME: older versions of ld64 expect the "arm64" component in the actual
473 // triple string and query it to determine whether an LTO file can be
474 // handled. Remove this when we don't care any more.
475 Triple.setArchName("arm64");
476 return Triple.getTriple();
477 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000478 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000479 case llvm::Triple::armeb:
480 case llvm::Triple::thumb:
481 case llvm::Triple::thumbeb: {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000482 // FIXME: Factor into subclasses.
483 llvm::Triple Triple = getTriple();
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000484 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb ||
485 getTriple().getArch() == llvm::Triple::thumbeb;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000486
Christian Pirkerba289f02014-04-10 13:59:32 +0000487 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
488 // '-mbig-endian'/'-EB'.
489 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
490 options::OPT_mbig_endian)) {
David Blaikie7a3cbb22015-03-09 02:02:07 +0000491 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
Christian Pirkerba289f02014-04-10 13:59:32 +0000492 }
493
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000494 // Thumb2 is the default for V7 on Darwin.
495 //
496 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Renato Goline17c5802015-07-27 23:44:42 +0000497 StringRef MCPU, MArch;
498 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
499 MCPU = A->getValue();
500 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
501 MArch = A->getValue();
Chandler Carruthd96f37a2015-08-30 07:51:18 +0000502 std::string CPU =
503 Triple.isOSBinFormatMachO()
504 ? tools::arm::getARMCPUForMArch(MArch, Triple).str()
505 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000506 StringRef Suffix =
Vladimir Sukharev64f68242015-09-23 09:29:32 +0000507 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +0000508 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M;
509 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 &&
510 getTriple().isOSBinFormatMachO());
Saleem Abdulrasoolf4c9e492014-04-04 20:31:19 +0000511 // FIXME: this is invalid for WindowsCE
512 if (getTriple().isOSWindows())
513 ThumbDefault = true;
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000514 std::string ArchName;
515 if (IsBigEndian)
516 ArchName = "armeb";
517 else
518 ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000519
Alexandros Lamprineas89ea4332015-10-28 10:10:03 +0000520 // Assembly files should start in ARM mode, unless arch is M-profile.
Renato Golin6b5596a2016-07-27 14:12:20 +0000521 // Windows is always thumb.
Alexandros Lamprineasf6ecf962015-11-05 17:11:55 +0000522 if ((InputType != types::TY_PP_Asm && Args.hasFlag(options::OPT_mthumb,
Renato Golin6b5596a2016-07-27 14:12:20 +0000523 options::OPT_mno_thumb, ThumbDefault)) || IsMProfile ||
524 getTriple().isOSWindows()) {
Christian Pirkerf01cd6f2014-03-28 14:40:46 +0000525 if (IsBigEndian)
526 ArchName = "thumbeb";
527 else
528 ArchName = "thumb";
529 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000530 Triple.setArchName(ArchName + Suffix.str());
531
532 return Triple.getTriple();
533 }
534 }
535}
536
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000537std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
Chad Rosierd3a0f952011-09-20 20:44:06 +0000538 types::ID InputType) const {
Chad Rosierd3a0f952011-09-20 20:44:06 +0000539 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000540}
541
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000542void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
543 ArgStringList &CC1Args) const {
544 // Each toolchain should provide the appropriate include flags.
545}
546
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000547void ToolChain::addClangTargetOptions(
548 const ArgList &DriverArgs, ArgStringList &CC1Args,
549 Action::OffloadKind DeviceOffloadKind) const {}
Rafael Espindola66aa0452012-06-19 01:26:10 +0000550
Tim Northover336f1892014-03-29 13:16:12 +0000551void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
552
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000553void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args,
Xinliang David Li69306c02015-10-22 06:15:31 +0000554 llvm::opt::ArgStringList &CmdArgs) const {
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000555 if (!needsProfileRT(Args)) return;
Xinliang David Li69306c02015-10-22 06:15:31 +0000556
Vedant Kumar5fb00e42016-07-27 23:01:55 +0000557 CmdArgs.push_back(getCompilerRTArgString(Args, "profile"));
Xinliang David Li69306c02015-10-22 06:15:31 +0000558}
559
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000560ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
Xinliang David Li69306c02015-10-22 06:15:31 +0000561 const ArgList &Args) const {
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000562 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ);
563 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB;
564
Jonas Hahnfeldeb4cc232016-12-12 07:53:47 +0000565 // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB!
Jonas Hahnfeldd196fa52016-07-27 08:15:54 +0000566 if (LibName == "compiler-rt")
567 return ToolChain::RLT_CompilerRT;
568 else if (LibName == "libgcc")
569 return ToolChain::RLT_Libgcc;
570 else if (LibName == "platform")
571 return GetDefaultRuntimeLibType();
572
573 if (A)
574 getDriver().Diag(diag::err_drv_invalid_rtlib_name) << A->getAsString(Args);
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000575
576 return GetDefaultRuntimeLibType();
577}
578
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000579ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Jonas Hahnfeldaae83742016-02-12 07:48:37 +0000580 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
Jonas Hahnfeldeb4cc232016-12-12 07:53:47 +0000581 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB;
Jonas Hahnfeld09954192016-03-14 14:34:04 +0000582
Jonas Hahnfeldeb4cc232016-12-12 07:53:47 +0000583 // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB!
584 if (LibName == "libc++")
585 return ToolChain::CST_Libcxx;
586 else if (LibName == "libstdc++")
587 return ToolChain::CST_Libstdcxx;
588 else if (LibName == "platform")
589 return GetDefaultCXXStdlibType();
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000590
Jonas Hahnfeldeb4cc232016-12-12 07:53:47 +0000591 if (A)
592 getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
Jonas Hahnfeldaae83742016-02-12 07:48:37 +0000593
Jonas Hahnfeldeb4cc232016-12-12 07:53:47 +0000594 return GetDefaultCXXStdlibType();
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000595}
596
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000597/// \brief Utility function to add a system include directory to CC1 arguments.
598/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
599 ArgStringList &CC1Args,
600 const Twine &Path) {
601 CC1Args.push_back("-internal-isystem");
602 CC1Args.push_back(DriverArgs.MakeArgString(Path));
603}
604
605/// \brief Utility function to add a system include directory with extern "C"
606/// semantics to CC1 arguments.
607///
608/// Note that this should be used rarely, and only for directories that
609/// historically and for legacy reasons are treated as having implicit extern
610/// "C" semantics. These semantics are *ignored* by and large today, but its
611/// important to preserve the preprocessor changes resulting from the
612/// classification.
613/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
614 ArgStringList &CC1Args,
615 const Twine &Path) {
616 CC1Args.push_back("-internal-externc-isystem");
617 CC1Args.push_back(DriverArgs.MakeArgString(Path));
618}
619
Simon Atanasyan08450bd2013-04-20 08:15:03 +0000620void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
621 ArgStringList &CC1Args,
622 const Twine &Path) {
623 if (llvm::sys::fs::exists(Path))
624 addExternCSystemInclude(DriverArgs, CC1Args, Path);
625}
626
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000627/// \brief Utility function to add a list of system include directories to CC1.
628/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
629 ArgStringList &CC1Args,
630 ArrayRef<StringRef> Paths) {
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000631 for (StringRef Path : Paths) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000632 CC1Args.push_back("-internal-isystem");
Douglas Katzman96ad05e2015-08-06 22:36:24 +0000633 CC1Args.push_back(DriverArgs.MakeArgString(Path));
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000634 }
635}
636
Chandler Carruth814db372011-11-04 23:49:01 +0000637void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
638 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000639 // Header search paths should be handled by each of the subclasses.
640 // Historically, they have not been, and instead have been handled inside of
641 // the CC1-layer frontend. As the logic is hoisted out, this generic function
642 // will slowly stop being called.
643 //
644 // While it is being called, replicate a bit of a hack to propagate the
645 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
646 // header search paths with it. Once all systems are overriding this
647 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000648 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000649}
650
Nico Weber0ee47d92017-07-25 18:02:57 +0000651bool ToolChain::ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const {
652 return getDriver().CCCIsCXX() &&
653 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
654 options::OPT_nostdlibxx);
655}
656
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000657void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
658 ArgStringList &CmdArgs) const {
Nico Weber0ee47d92017-07-25 18:02:57 +0000659 assert(!Args.hasArg(options::OPT_nostdlibxx) &&
660 "should not have called this");
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000661 CXXStdlibType Type = GetCXXStdlibType(Args);
662
663 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000664 case ToolChain::CST_Libcxx:
665 CmdArgs.push_back("-lc++");
666 break;
667
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000668 case ToolChain::CST_Libstdcxx:
669 CmdArgs.push_back("-lstdc++");
670 break;
671 }
672}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000673
Douglas Katzman6059ef92015-11-17 17:41:23 +0000674void ToolChain::AddFilePathLibArgs(const ArgList &Args,
675 ArgStringList &CmdArgs) const {
676 for (const auto &LibPath : getFilePaths())
Martell Malone5cad2252015-11-26 01:02:07 +0000677 if(LibPath.length() > 0)
678 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
Douglas Katzman6059ef92015-11-17 17:41:23 +0000679}
680
Shantonu Senafeb03b2010-09-17 18:39:08 +0000681void ToolChain::AddCCKextLibArgs(const ArgList &Args,
682 ArgStringList &CmdArgs) const {
683 CmdArgs.push_back("-lcc_kext");
684}
Benjamin Kramer058666a2012-10-04 19:42:20 +0000685
686bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
687 ArgStringList &CmdArgs) const {
Benjamin Kramerab88f622014-03-25 18:02:07 +0000688 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
689 // (to keep the linker options consistent with gcc and clang itself).
690 if (!isOptimizationLevelFast(Args)) {
691 // Check if -ffast-math or -funsafe-math.
692 Arg *A =
693 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
694 options::OPT_funsafe_math_optimizations,
695 options::OPT_fno_unsafe_math_optimizations);
Benjamin Kramer058666a2012-10-04 19:42:20 +0000696
Benjamin Kramerab88f622014-03-25 18:02:07 +0000697 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
698 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
699 return false;
700 }
Benjamin Kramer058666a2012-10-04 19:42:20 +0000701 // If crtfastmath.o exists add it to the arguments.
702 std::string Path = GetFilePath("crtfastmath.o");
703 if (Path == "crtfastmath.o") // Not found.
704 return false;
705
706 CmdArgs.push_back(Args.MakeArgString(Path));
707 return true;
708}
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000709
710SanitizerMask ToolChain::getSupportedSanitizers() const {
711 // Return sanitizers which don't require runtime support and are not
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000712 // platform dependent.
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000713 using namespace SanitizerKind;
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000714 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) |
Vedant Kumar42c17ec2017-03-14 01:56:34 +0000715 CFICastStrict | UnsignedIntegerOverflow | Nullability |
716 LocalBounds;
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000717 if (getTriple().getArch() == llvm::Triple::x86 ||
Derek Schuffef313052016-08-08 21:14:15 +0000718 getTriple().getArch() == llvm::Triple::x86_64 ||
Evgeniy Stepanovda2028b2016-11-11 18:49:49 +0000719 getTriple().getArch() == llvm::Triple::arm ||
720 getTriple().getArch() == llvm::Triple::aarch64 ||
Derek Schuffef313052016-08-08 21:14:15 +0000721 getTriple().getArch() == llvm::Triple::wasm32 ||
722 getTriple().getArch() == llvm::Triple::wasm64)
Peter Collingbourne24ec49242015-09-10 19:18:05 +0000723 Res |= CFIICall;
724 return Res;
Alexey Samsonov7f2a0d22015-06-19 21:36:47 +0000725}
Artem Belevichfa11ab52015-11-17 22:28:46 +0000726
727void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
728 ArgStringList &CC1Args) const {}
Andrey Turetskiy4798eb62016-06-16 10:36:09 +0000729
730void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
731 ArgStringList &CC1Args) const {}
David L. Jones24fb20c2016-12-07 23:41:58 +0000732
733static VersionTuple separateMSVCFullVersion(unsigned Version) {
734 if (Version < 100)
735 return VersionTuple(Version);
736
737 if (Version < 10000)
738 return VersionTuple(Version / 100, Version % 100);
739
740 unsigned Build = 0, Factor = 1;
741 for (; Version > 10000; Version = Version / 10, Factor = Factor * 10)
742 Build = Build + (Version % 10) * Factor;
743 return VersionTuple(Version / 100, Version % 100, Build);
744}
745
746VersionTuple
747ToolChain::computeMSVCVersion(const Driver *D,
748 const llvm::opt::ArgList &Args) const {
749 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
750 const Arg *MSCompatibilityVersion =
751 Args.getLastArg(options::OPT_fms_compatibility_version);
752
753 if (MSCVersion && MSCompatibilityVersion) {
754 if (D)
755 D->Diag(diag::err_drv_argument_not_allowed_with)
756 << MSCVersion->getAsString(Args)
757 << MSCompatibilityVersion->getAsString(Args);
758 return VersionTuple();
759 }
760
761 if (MSCompatibilityVersion) {
762 VersionTuple MSVT;
763 if (MSVT.tryParse(MSCompatibilityVersion->getValue())) {
764 if (D)
765 D->Diag(diag::err_drv_invalid_value)
766 << MSCompatibilityVersion->getAsString(Args)
767 << MSCompatibilityVersion->getValue();
768 } else {
769 return MSVT;
770 }
771 }
772
773 if (MSCVersion) {
774 unsigned Version = 0;
775 if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) {
776 if (D)
777 D->Diag(diag::err_drv_invalid_value)
778 << MSCVersion->getAsString(Args) << MSCVersion->getValue();
779 } else {
780 return separateMSVCFullVersion(Version);
781 }
782 }
783
784 return VersionTuple();
785}