1e5dd7070Spatrick //===- ToolChain.cpp - Collections of tools for one platform --------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "clang/Driver/ToolChain.h"
10e5dd7070Spatrick #include "ToolChains/Arch/ARM.h"
11e5dd7070Spatrick #include "ToolChains/Clang.h"
12e5dd7070Spatrick #include "ToolChains/Flang.h"
13*12c85518Srobert #include "ToolChains/InterfaceStubs.h"
14e5dd7070Spatrick #include "clang/Basic/ObjCRuntime.h"
15e5dd7070Spatrick #include "clang/Basic/Sanitizers.h"
16e5dd7070Spatrick #include "clang/Config/config.h"
17e5dd7070Spatrick #include "clang/Driver/Action.h"
18e5dd7070Spatrick #include "clang/Driver/Driver.h"
19e5dd7070Spatrick #include "clang/Driver/DriverDiagnostic.h"
20a9ac8606Spatrick #include "clang/Driver/InputInfo.h"
21e5dd7070Spatrick #include "clang/Driver/Job.h"
22e5dd7070Spatrick #include "clang/Driver/Options.h"
23e5dd7070Spatrick #include "clang/Driver/SanitizerArgs.h"
24e5dd7070Spatrick #include "clang/Driver/XRayArgs.h"
25e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
26e5dd7070Spatrick #include "llvm/ADT/SmallString.h"
27e5dd7070Spatrick #include "llvm/ADT/StringRef.h"
28e5dd7070Spatrick #include "llvm/ADT/Triple.h"
29e5dd7070Spatrick #include "llvm/ADT/Twine.h"
30e5dd7070Spatrick #include "llvm/Config/llvm-config.h"
31e5dd7070Spatrick #include "llvm/MC/MCTargetOptions.h"
32*12c85518Srobert #include "llvm/MC/TargetRegistry.h"
33e5dd7070Spatrick #include "llvm/Option/Arg.h"
34e5dd7070Spatrick #include "llvm/Option/ArgList.h"
35e5dd7070Spatrick #include "llvm/Option/OptTable.h"
36e5dd7070Spatrick #include "llvm/Option/Option.h"
37e5dd7070Spatrick #include "llvm/Support/ErrorHandling.h"
38e5dd7070Spatrick #include "llvm/Support/FileSystem.h"
39*12c85518Srobert #include "llvm/Support/FileUtilities.h"
40e5dd7070Spatrick #include "llvm/Support/Path.h"
41e5dd7070Spatrick #include "llvm/Support/TargetParser.h"
42e5dd7070Spatrick #include "llvm/Support/VersionTuple.h"
43e5dd7070Spatrick #include "llvm/Support/VirtualFileSystem.h"
44e5dd7070Spatrick #include <cassert>
45e5dd7070Spatrick #include <cstddef>
46e5dd7070Spatrick #include <cstring>
47e5dd7070Spatrick #include <string>
48e5dd7070Spatrick
49e5dd7070Spatrick using namespace clang;
50e5dd7070Spatrick using namespace driver;
51e5dd7070Spatrick using namespace tools;
52e5dd7070Spatrick using namespace llvm;
53e5dd7070Spatrick using namespace llvm::opt;
54e5dd7070Spatrick
GetRTTIArgument(const ArgList & Args)55e5dd7070Spatrick static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) {
56e5dd7070Spatrick return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext,
57e5dd7070Spatrick options::OPT_fno_rtti, options::OPT_frtti);
58e5dd7070Spatrick }
59e5dd7070Spatrick
CalculateRTTIMode(const ArgList & Args,const llvm::Triple & Triple,const Arg * CachedRTTIArg)60e5dd7070Spatrick static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args,
61e5dd7070Spatrick const llvm::Triple &Triple,
62e5dd7070Spatrick const Arg *CachedRTTIArg) {
63e5dd7070Spatrick // Explicit rtti/no-rtti args
64e5dd7070Spatrick if (CachedRTTIArg) {
65e5dd7070Spatrick if (CachedRTTIArg->getOption().matches(options::OPT_frtti))
66e5dd7070Spatrick return ToolChain::RM_Enabled;
67e5dd7070Spatrick else
68e5dd7070Spatrick return ToolChain::RM_Disabled;
69e5dd7070Spatrick }
70e5dd7070Spatrick
71*12c85518Srobert // -frtti is default, except for the PS4/PS5 and DriverKit.
72*12c85518Srobert bool NoRTTI = Triple.isPS() || Triple.isDriverKit();
73*12c85518Srobert return NoRTTI ? ToolChain::RM_Disabled : ToolChain::RM_Enabled;
74e5dd7070Spatrick }
75e5dd7070Spatrick
ToolChain(const Driver & D,const llvm::Triple & T,const ArgList & Args)76e5dd7070Spatrick ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
77e5dd7070Spatrick const ArgList &Args)
78e5dd7070Spatrick : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)),
79e5dd7070Spatrick CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) {
80*12c85518Srobert auto addIfExists = [this](path_list &List, const std::string &Path) {
81*12c85518Srobert if (getVFS().exists(Path))
82*12c85518Srobert List.push_back(Path);
83*12c85518Srobert };
84e5dd7070Spatrick
85*12c85518Srobert for (const auto &Path : getRuntimePaths())
86*12c85518Srobert addIfExists(getLibraryPaths(), Path);
87*12c85518Srobert for (const auto &Path : getStdlibPaths())
88*12c85518Srobert addIfExists(getFilePaths(), Path);
89*12c85518Srobert addIfExists(getFilePaths(), getArchSpecificLibPath());
90*12c85518Srobert }
91e5dd7070Spatrick
92*12c85518Srobert llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
executeToolChainProgram(StringRef Executable) const93*12c85518Srobert ToolChain::executeToolChainProgram(StringRef Executable) const {
94*12c85518Srobert llvm::SmallString<64> OutputFile;
95*12c85518Srobert llvm::sys::fs::createTemporaryFile("toolchain-program", "txt", OutputFile);
96*12c85518Srobert llvm::FileRemover OutputRemover(OutputFile.c_str());
97*12c85518Srobert std::optional<llvm::StringRef> Redirects[] = {
98*12c85518Srobert {""},
99*12c85518Srobert OutputFile.str(),
100*12c85518Srobert {""},
101*12c85518Srobert };
102*12c85518Srobert
103*12c85518Srobert std::string ErrorMessage;
104*12c85518Srobert if (llvm::sys::ExecuteAndWait(Executable, {}, {}, Redirects,
105*12c85518Srobert /* SecondsToWait */ 0,
106*12c85518Srobert /*MemoryLimit*/ 0, &ErrorMessage))
107*12c85518Srobert return llvm::createStringError(std::error_code(),
108*12c85518Srobert Executable + ": " + ErrorMessage);
109*12c85518Srobert
110*12c85518Srobert llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> OutputBuf =
111*12c85518Srobert llvm::MemoryBuffer::getFile(OutputFile.c_str());
112*12c85518Srobert if (!OutputBuf)
113*12c85518Srobert return llvm::createStringError(OutputBuf.getError(),
114*12c85518Srobert "Failed to read stdout of " + Executable +
115*12c85518Srobert ": " + OutputBuf.getError().message());
116*12c85518Srobert return std::move(*OutputBuf);
117e5dd7070Spatrick }
118e5dd7070Spatrick
setTripleEnvironment(llvm::Triple::EnvironmentType Env)119e5dd7070Spatrick void ToolChain::setTripleEnvironment(llvm::Triple::EnvironmentType Env) {
120e5dd7070Spatrick Triple.setEnvironment(Env);
121e5dd7070Spatrick if (EffectiveTriple != llvm::Triple())
122e5dd7070Spatrick EffectiveTriple.setEnvironment(Env);
123e5dd7070Spatrick }
124e5dd7070Spatrick
125e5dd7070Spatrick ToolChain::~ToolChain() = default;
126e5dd7070Spatrick
getVFS() const127e5dd7070Spatrick llvm::vfs::FileSystem &ToolChain::getVFS() const {
128e5dd7070Spatrick return getDriver().getVFS();
129e5dd7070Spatrick }
130e5dd7070Spatrick
useIntegratedAs() const131e5dd7070Spatrick bool ToolChain::useIntegratedAs() const {
132e5dd7070Spatrick return Args.hasFlag(options::OPT_fintegrated_as,
133e5dd7070Spatrick options::OPT_fno_integrated_as,
134e5dd7070Spatrick IsIntegratedAssemblerDefault());
135e5dd7070Spatrick }
136e5dd7070Spatrick
useIntegratedBackend() const137*12c85518Srobert bool ToolChain::useIntegratedBackend() const {
138*12c85518Srobert assert(
139*12c85518Srobert ((IsIntegratedBackendDefault() && IsIntegratedBackendSupported()) ||
140*12c85518Srobert (!IsIntegratedBackendDefault() || IsNonIntegratedBackendSupported())) &&
141*12c85518Srobert "(Non-)integrated backend set incorrectly!");
142*12c85518Srobert
143*12c85518Srobert bool IBackend = Args.hasFlag(options::OPT_fintegrated_objemitter,
144*12c85518Srobert options::OPT_fno_integrated_objemitter,
145*12c85518Srobert IsIntegratedBackendDefault());
146*12c85518Srobert
147*12c85518Srobert // Diagnose when integrated-objemitter options are not supported by this
148*12c85518Srobert // toolchain.
149*12c85518Srobert unsigned DiagID;
150*12c85518Srobert if ((IBackend && !IsIntegratedBackendSupported()) ||
151*12c85518Srobert (!IBackend && !IsNonIntegratedBackendSupported()))
152*12c85518Srobert DiagID = clang::diag::err_drv_unsupported_opt_for_target;
153*12c85518Srobert else
154*12c85518Srobert DiagID = clang::diag::warn_drv_unsupported_opt_for_target;
155*12c85518Srobert Arg *A = Args.getLastArg(options::OPT_fno_integrated_objemitter);
156*12c85518Srobert if (A && !IsNonIntegratedBackendSupported())
157*12c85518Srobert D.Diag(DiagID) << A->getAsString(Args) << Triple.getTriple();
158*12c85518Srobert A = Args.getLastArg(options::OPT_fintegrated_objemitter);
159*12c85518Srobert if (A && !IsIntegratedBackendSupported())
160*12c85518Srobert D.Diag(DiagID) << A->getAsString(Args) << Triple.getTriple();
161*12c85518Srobert
162*12c85518Srobert return IBackend;
163*12c85518Srobert }
164*12c85518Srobert
useRelaxRelocations() const165e5dd7070Spatrick bool ToolChain::useRelaxRelocations() const {
166e5dd7070Spatrick return ENABLE_X86_RELAX_RELOCATIONS;
167e5dd7070Spatrick }
168e5dd7070Spatrick
defaultToIEEELongDouble() const169*12c85518Srobert bool ToolChain::defaultToIEEELongDouble() const {
170*12c85518Srobert return PPC_LINUX_DEFAULT_IEEELONGDOUBLE && getTriple().isOSLinux();
171e5dd7070Spatrick }
172e5dd7070Spatrick
173*12c85518Srobert SanitizerArgs
getSanitizerArgs(const llvm::opt::ArgList & JobArgs) const174*12c85518Srobert ToolChain::getSanitizerArgs(const llvm::opt::ArgList &JobArgs) const {
175*12c85518Srobert SanitizerArgs SanArgs(*this, JobArgs, !SanitizerArgsChecked);
176*12c85518Srobert SanitizerArgsChecked = true;
177*12c85518Srobert return SanArgs;
178e5dd7070Spatrick }
179e5dd7070Spatrick
getXRayArgs() const180e5dd7070Spatrick const XRayArgs& ToolChain::getXRayArgs() const {
181*12c85518Srobert if (!XRayArguments)
182e5dd7070Spatrick XRayArguments.reset(new XRayArgs(*this, Args));
183*12c85518Srobert return *XRayArguments;
184e5dd7070Spatrick }
185e5dd7070Spatrick
186e5dd7070Spatrick namespace {
187e5dd7070Spatrick
188e5dd7070Spatrick struct DriverSuffix {
189e5dd7070Spatrick const char *Suffix;
190e5dd7070Spatrick const char *ModeFlag;
191e5dd7070Spatrick };
192e5dd7070Spatrick
193e5dd7070Spatrick } // namespace
194e5dd7070Spatrick
FindDriverSuffix(StringRef ProgName,size_t & Pos)195e5dd7070Spatrick static const DriverSuffix *FindDriverSuffix(StringRef ProgName, size_t &Pos) {
196e5dd7070Spatrick // A list of known driver suffixes. Suffixes are compared against the
197e5dd7070Spatrick // program name in order. If there is a match, the frontend type is updated as
198e5dd7070Spatrick // necessary by applying the ModeFlag.
199e5dd7070Spatrick static const DriverSuffix DriverSuffixes[] = {
200e5dd7070Spatrick {"clang", nullptr},
201e5dd7070Spatrick {"clang++", "--driver-mode=g++"},
202e5dd7070Spatrick {"clang-c++", "--driver-mode=g++"},
203e5dd7070Spatrick {"clang-cc", nullptr},
204e5dd7070Spatrick {"clang-cpp", "--driver-mode=cpp"},
205e5dd7070Spatrick {"clang-g++", "--driver-mode=g++"},
206e5dd7070Spatrick {"clang-gcc", nullptr},
207e5dd7070Spatrick {"clang-cl", "--driver-mode=cl"},
208e5dd7070Spatrick {"cc", nullptr},
209e5dd7070Spatrick {"cpp", "--driver-mode=cpp"},
210e5dd7070Spatrick {"cl", "--driver-mode=cl"},
211e5dd7070Spatrick {"++", "--driver-mode=g++"},
212e5dd7070Spatrick {"flang", "--driver-mode=flang"},
213*12c85518Srobert {"clang-dxc", "--driver-mode=dxc"},
214e5dd7070Spatrick };
215e5dd7070Spatrick
216*12c85518Srobert for (const auto &DS : DriverSuffixes) {
217*12c85518Srobert StringRef Suffix(DS.Suffix);
218e5dd7070Spatrick if (ProgName.endswith(Suffix)) {
219e5dd7070Spatrick Pos = ProgName.size() - Suffix.size();
220*12c85518Srobert return &DS;
221e5dd7070Spatrick }
222e5dd7070Spatrick }
223e5dd7070Spatrick return nullptr;
224e5dd7070Spatrick }
225e5dd7070Spatrick
226e5dd7070Spatrick /// Normalize the program name from argv[0] by stripping the file extension if
227e5dd7070Spatrick /// present and lower-casing the string on Windows.
normalizeProgramName(llvm::StringRef Argv0)228e5dd7070Spatrick static std::string normalizeProgramName(llvm::StringRef Argv0) {
229*12c85518Srobert std::string ProgName = std::string(llvm::sys::path::filename(Argv0));
230*12c85518Srobert if (is_style_windows(llvm::sys::path::Style::native)) {
231e5dd7070Spatrick // Transform to lowercase for case insensitive file systems.
232*12c85518Srobert std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(),
233*12c85518Srobert ::tolower);
234*12c85518Srobert }
235e5dd7070Spatrick return ProgName;
236e5dd7070Spatrick }
237e5dd7070Spatrick
parseDriverSuffix(StringRef ProgName,size_t & Pos)238e5dd7070Spatrick static const DriverSuffix *parseDriverSuffix(StringRef ProgName, size_t &Pos) {
239e5dd7070Spatrick // Try to infer frontend type and default target from the program name by
240e5dd7070Spatrick // comparing it against DriverSuffixes in order.
241e5dd7070Spatrick
242e5dd7070Spatrick // If there is a match, the function tries to identify a target as prefix.
243e5dd7070Spatrick // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
244e5dd7070Spatrick // prefix "x86_64-linux". If such a target prefix is found, it may be
245e5dd7070Spatrick // added via -target as implicit first argument.
246e5dd7070Spatrick const DriverSuffix *DS = FindDriverSuffix(ProgName, Pos);
247e5dd7070Spatrick
248*12c85518Srobert if (!DS && ProgName.endswith(".exe")) {
249*12c85518Srobert // Try again after stripping the executable suffix:
250*12c85518Srobert // clang++.exe -> clang++
251*12c85518Srobert ProgName = ProgName.drop_back(StringRef(".exe").size());
252*12c85518Srobert DS = FindDriverSuffix(ProgName, Pos);
253*12c85518Srobert }
254*12c85518Srobert
255e5dd7070Spatrick if (!DS) {
256e5dd7070Spatrick // Try again after stripping any trailing version number:
257e5dd7070Spatrick // clang++3.5 -> clang++
258e5dd7070Spatrick ProgName = ProgName.rtrim("0123456789.");
259e5dd7070Spatrick DS = FindDriverSuffix(ProgName, Pos);
260e5dd7070Spatrick }
261e5dd7070Spatrick
262e5dd7070Spatrick if (!DS) {
263e5dd7070Spatrick // Try again after stripping trailing -component.
264e5dd7070Spatrick // clang++-tot -> clang++
265e5dd7070Spatrick ProgName = ProgName.slice(0, ProgName.rfind('-'));
266e5dd7070Spatrick DS = FindDriverSuffix(ProgName, Pos);
267e5dd7070Spatrick }
268e5dd7070Spatrick return DS;
269e5dd7070Spatrick }
270e5dd7070Spatrick
271e5dd7070Spatrick ParsedClangName
getTargetAndModeFromProgramName(StringRef PN)272e5dd7070Spatrick ToolChain::getTargetAndModeFromProgramName(StringRef PN) {
273e5dd7070Spatrick std::string ProgName = normalizeProgramName(PN);
274e5dd7070Spatrick size_t SuffixPos;
275e5dd7070Spatrick const DriverSuffix *DS = parseDriverSuffix(ProgName, SuffixPos);
276e5dd7070Spatrick if (!DS)
277e5dd7070Spatrick return {};
278e5dd7070Spatrick size_t SuffixEnd = SuffixPos + strlen(DS->Suffix);
279e5dd7070Spatrick
280e5dd7070Spatrick size_t LastComponent = ProgName.rfind('-', SuffixPos);
281e5dd7070Spatrick if (LastComponent == std::string::npos)
282e5dd7070Spatrick return ParsedClangName(ProgName.substr(0, SuffixEnd), DS->ModeFlag);
283e5dd7070Spatrick std::string ModeSuffix = ProgName.substr(LastComponent + 1,
284e5dd7070Spatrick SuffixEnd - LastComponent - 1);
285e5dd7070Spatrick
286e5dd7070Spatrick // Infer target from the prefix.
287e5dd7070Spatrick StringRef Prefix(ProgName);
288e5dd7070Spatrick Prefix = Prefix.slice(0, LastComponent);
289e5dd7070Spatrick std::string IgnoredError;
290ec727ea7Spatrick bool IsRegistered =
291ec727ea7Spatrick llvm::TargetRegistry::lookupTarget(std::string(Prefix), IgnoredError);
292ec727ea7Spatrick return ParsedClangName{std::string(Prefix), ModeSuffix, DS->ModeFlag,
293ec727ea7Spatrick IsRegistered};
294e5dd7070Spatrick }
295e5dd7070Spatrick
getDefaultUniversalArchName() const296e5dd7070Spatrick StringRef ToolChain::getDefaultUniversalArchName() const {
297e5dd7070Spatrick // In universal driver terms, the arch name accepted by -arch isn't exactly
298e5dd7070Spatrick // the same as the ones that appear in the triple. Roughly speaking, this is
299ec727ea7Spatrick // an inverse of the darwin::getArchTypeForDarwinArchName() function.
300e5dd7070Spatrick switch (Triple.getArch()) {
301a9ac8606Spatrick case llvm::Triple::aarch64: {
302a9ac8606Spatrick if (getTriple().isArm64e())
303a9ac8606Spatrick return "arm64e";
304ec727ea7Spatrick return "arm64";
305a9ac8606Spatrick }
306ec727ea7Spatrick case llvm::Triple::aarch64_32:
307ec727ea7Spatrick return "arm64_32";
308e5dd7070Spatrick case llvm::Triple::ppc:
309e5dd7070Spatrick return "ppc";
310a9ac8606Spatrick case llvm::Triple::ppcle:
311a9ac8606Spatrick return "ppcle";
312e5dd7070Spatrick case llvm::Triple::ppc64:
313e5dd7070Spatrick return "ppc64";
314e5dd7070Spatrick case llvm::Triple::ppc64le:
315e5dd7070Spatrick return "ppc64le";
316e5dd7070Spatrick default:
317e5dd7070Spatrick return Triple.getArchName();
318e5dd7070Spatrick }
319e5dd7070Spatrick }
320e5dd7070Spatrick
getInputFilename(const InputInfo & Input) const321e5dd7070Spatrick std::string ToolChain::getInputFilename(const InputInfo &Input) const {
322e5dd7070Spatrick return Input.getFilename();
323e5dd7070Spatrick }
324e5dd7070Spatrick
325*12c85518Srobert ToolChain::UnwindTableLevel
getDefaultUnwindTableLevel(const ArgList & Args) const326*12c85518Srobert ToolChain::getDefaultUnwindTableLevel(const ArgList &Args) const {
327*12c85518Srobert return UnwindTableLevel::None;
328e5dd7070Spatrick }
329e5dd7070Spatrick
getClang() const330e5dd7070Spatrick Tool *ToolChain::getClang() const {
331e5dd7070Spatrick if (!Clang)
332*12c85518Srobert Clang.reset(new tools::Clang(*this, useIntegratedBackend()));
333e5dd7070Spatrick return Clang.get();
334e5dd7070Spatrick }
335e5dd7070Spatrick
getFlang() const336e5dd7070Spatrick Tool *ToolChain::getFlang() const {
337e5dd7070Spatrick if (!Flang)
338e5dd7070Spatrick Flang.reset(new tools::Flang(*this));
339e5dd7070Spatrick return Flang.get();
340e5dd7070Spatrick }
341e5dd7070Spatrick
buildAssembler() const342e5dd7070Spatrick Tool *ToolChain::buildAssembler() const {
343e5dd7070Spatrick return new tools::ClangAs(*this);
344e5dd7070Spatrick }
345e5dd7070Spatrick
buildLinker() const346e5dd7070Spatrick Tool *ToolChain::buildLinker() const {
347e5dd7070Spatrick llvm_unreachable("Linking is not supported by this toolchain");
348e5dd7070Spatrick }
349e5dd7070Spatrick
buildStaticLibTool() const350ec727ea7Spatrick Tool *ToolChain::buildStaticLibTool() const {
351ec727ea7Spatrick llvm_unreachable("Creating static lib is not supported by this toolchain");
352ec727ea7Spatrick }
353ec727ea7Spatrick
getAssemble() const354e5dd7070Spatrick Tool *ToolChain::getAssemble() const {
355e5dd7070Spatrick if (!Assemble)
356e5dd7070Spatrick Assemble.reset(buildAssembler());
357e5dd7070Spatrick return Assemble.get();
358e5dd7070Spatrick }
359e5dd7070Spatrick
getClangAs() const360e5dd7070Spatrick Tool *ToolChain::getClangAs() const {
361e5dd7070Spatrick if (!Assemble)
362e5dd7070Spatrick Assemble.reset(new tools::ClangAs(*this));
363e5dd7070Spatrick return Assemble.get();
364e5dd7070Spatrick }
365e5dd7070Spatrick
getLink() const366e5dd7070Spatrick Tool *ToolChain::getLink() const {
367e5dd7070Spatrick if (!Link)
368e5dd7070Spatrick Link.reset(buildLinker());
369e5dd7070Spatrick return Link.get();
370e5dd7070Spatrick }
371e5dd7070Spatrick
getStaticLibTool() const372ec727ea7Spatrick Tool *ToolChain::getStaticLibTool() const {
373ec727ea7Spatrick if (!StaticLibTool)
374ec727ea7Spatrick StaticLibTool.reset(buildStaticLibTool());
375ec727ea7Spatrick return StaticLibTool.get();
376ec727ea7Spatrick }
377ec727ea7Spatrick
getIfsMerge() const378e5dd7070Spatrick Tool *ToolChain::getIfsMerge() const {
379e5dd7070Spatrick if (!IfsMerge)
380e5dd7070Spatrick IfsMerge.reset(new tools::ifstool::Merger(*this));
381e5dd7070Spatrick return IfsMerge.get();
382e5dd7070Spatrick }
383e5dd7070Spatrick
getOffloadBundler() const384e5dd7070Spatrick Tool *ToolChain::getOffloadBundler() const {
385e5dd7070Spatrick if (!OffloadBundler)
386e5dd7070Spatrick OffloadBundler.reset(new tools::OffloadBundler(*this));
387e5dd7070Spatrick return OffloadBundler.get();
388e5dd7070Spatrick }
389e5dd7070Spatrick
getOffloadPackager() const390*12c85518Srobert Tool *ToolChain::getOffloadPackager() const {
391*12c85518Srobert if (!OffloadPackager)
392*12c85518Srobert OffloadPackager.reset(new tools::OffloadPackager(*this));
393*12c85518Srobert return OffloadPackager.get();
394*12c85518Srobert }
395*12c85518Srobert
getLinkerWrapper() const396*12c85518Srobert Tool *ToolChain::getLinkerWrapper() const {
397*12c85518Srobert if (!LinkerWrapper)
398*12c85518Srobert LinkerWrapper.reset(new tools::LinkerWrapper(*this, getLink()));
399*12c85518Srobert return LinkerWrapper.get();
400e5dd7070Spatrick }
401e5dd7070Spatrick
getTool(Action::ActionClass AC) const402e5dd7070Spatrick Tool *ToolChain::getTool(Action::ActionClass AC) const {
403e5dd7070Spatrick switch (AC) {
404e5dd7070Spatrick case Action::AssembleJobClass:
405e5dd7070Spatrick return getAssemble();
406e5dd7070Spatrick
407e5dd7070Spatrick case Action::IfsMergeJobClass:
408e5dd7070Spatrick return getIfsMerge();
409e5dd7070Spatrick
410e5dd7070Spatrick case Action::LinkJobClass:
411e5dd7070Spatrick return getLink();
412e5dd7070Spatrick
413ec727ea7Spatrick case Action::StaticLibJobClass:
414ec727ea7Spatrick return getStaticLibTool();
415ec727ea7Spatrick
416e5dd7070Spatrick case Action::InputClass:
417e5dd7070Spatrick case Action::BindArchClass:
418e5dd7070Spatrick case Action::OffloadClass:
419e5dd7070Spatrick case Action::LipoJobClass:
420e5dd7070Spatrick case Action::DsymutilJobClass:
421e5dd7070Spatrick case Action::VerifyDebugInfoJobClass:
422e5dd7070Spatrick llvm_unreachable("Invalid tool kind.");
423e5dd7070Spatrick
424e5dd7070Spatrick case Action::CompileJobClass:
425e5dd7070Spatrick case Action::PrecompileJobClass:
426e5dd7070Spatrick case Action::PreprocessJobClass:
427*12c85518Srobert case Action::ExtractAPIJobClass:
428e5dd7070Spatrick case Action::AnalyzeJobClass:
429e5dd7070Spatrick case Action::MigrateJobClass:
430e5dd7070Spatrick case Action::VerifyPCHJobClass:
431e5dd7070Spatrick case Action::BackendJobClass:
432e5dd7070Spatrick return getClang();
433e5dd7070Spatrick
434e5dd7070Spatrick case Action::OffloadBundlingJobClass:
435e5dd7070Spatrick case Action::OffloadUnbundlingJobClass:
436e5dd7070Spatrick return getOffloadBundler();
437e5dd7070Spatrick
438*12c85518Srobert case Action::OffloadPackagerJobClass:
439*12c85518Srobert return getOffloadPackager();
440*12c85518Srobert case Action::LinkerWrapperJobClass:
441*12c85518Srobert return getLinkerWrapper();
442e5dd7070Spatrick }
443e5dd7070Spatrick
444e5dd7070Spatrick llvm_unreachable("Invalid tool kind.");
445e5dd7070Spatrick }
446e5dd7070Spatrick
getArchNameForCompilerRTLib(const ToolChain & TC,const ArgList & Args)447e5dd7070Spatrick static StringRef getArchNameForCompilerRTLib(const ToolChain &TC,
448e5dd7070Spatrick const ArgList &Args) {
449e5dd7070Spatrick const llvm::Triple &Triple = TC.getTriple();
450e5dd7070Spatrick bool IsWindows = Triple.isOSWindows();
451e5dd7070Spatrick
452*12c85518Srobert if (TC.isBareMetal())
453*12c85518Srobert return Triple.getArchName();
454*12c85518Srobert
455e5dd7070Spatrick if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb)
456e5dd7070Spatrick return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows)
457e5dd7070Spatrick ? "armhf"
458e5dd7070Spatrick : "arm";
459e5dd7070Spatrick
460e5dd7070Spatrick // For historic reasons, Android library is using i686 instead of i386.
461e5dd7070Spatrick if (TC.getArch() == llvm::Triple::x86 && Triple.isAndroid())
462e5dd7070Spatrick return "i686";
463e5dd7070Spatrick
464a9ac8606Spatrick if (TC.getArch() == llvm::Triple::x86_64 && Triple.isX32())
465a9ac8606Spatrick return "x32";
466a9ac8606Spatrick
467e5dd7070Spatrick return llvm::Triple::getArchTypeName(TC.getArch());
468e5dd7070Spatrick }
469e5dd7070Spatrick
getOSLibName() const470e5dd7070Spatrick StringRef ToolChain::getOSLibName() const {
471a9ac8606Spatrick if (Triple.isOSDarwin())
472a9ac8606Spatrick return "darwin";
473a9ac8606Spatrick
474e5dd7070Spatrick switch (Triple.getOS()) {
475e5dd7070Spatrick case llvm::Triple::FreeBSD:
476e5dd7070Spatrick return "freebsd";
477e5dd7070Spatrick case llvm::Triple::NetBSD:
478e5dd7070Spatrick return "netbsd";
479e5dd7070Spatrick case llvm::Triple::OpenBSD:
480e5dd7070Spatrick return "openbsd";
481e5dd7070Spatrick case llvm::Triple::Solaris:
482e5dd7070Spatrick return "sunos";
483a9ac8606Spatrick case llvm::Triple::AIX:
484a9ac8606Spatrick return "aix";
485e5dd7070Spatrick default:
486e5dd7070Spatrick return getOS();
487e5dd7070Spatrick }
488e5dd7070Spatrick }
489e5dd7070Spatrick
getCompilerRTPath() const490e5dd7070Spatrick std::string ToolChain::getCompilerRTPath() const {
491e5dd7070Spatrick SmallString<128> Path(getDriver().ResourceDir);
492*12c85518Srobert if (isBareMetal()) {
493*12c85518Srobert llvm::sys::path::append(Path, "lib", getOSLibName());
494*12c85518Srobert Path += SelectedMultilib.gccSuffix();
495*12c85518Srobert } else if (Triple.isOSUnknown()) {
496e5dd7070Spatrick llvm::sys::path::append(Path, "lib");
497e5dd7070Spatrick } else {
498e5dd7070Spatrick llvm::sys::path::append(Path, "lib", getOSLibName());
499e5dd7070Spatrick }
500ec727ea7Spatrick return std::string(Path.str());
501e5dd7070Spatrick }
502e5dd7070Spatrick
getCompilerRTBasename(const ArgList & Args,StringRef Component,FileType Type) const503ec727ea7Spatrick std::string ToolChain::getCompilerRTBasename(const ArgList &Args,
504a9ac8606Spatrick StringRef Component,
505a9ac8606Spatrick FileType Type) const {
506a9ac8606Spatrick std::string CRTAbsolutePath = getCompilerRT(Args, Component, Type);
507a9ac8606Spatrick return llvm::sys::path::filename(CRTAbsolutePath).str();
508a9ac8606Spatrick }
509a9ac8606Spatrick
buildCompilerRTBasename(const llvm::opt::ArgList & Args,StringRef Component,FileType Type,bool AddArch) const510a9ac8606Spatrick std::string ToolChain::buildCompilerRTBasename(const llvm::opt::ArgList &Args,
511a9ac8606Spatrick StringRef Component,
512a9ac8606Spatrick FileType Type,
513ec727ea7Spatrick bool AddArch) const {
514e5dd7070Spatrick const llvm::Triple &TT = getTriple();
515e5dd7070Spatrick bool IsITANMSVCWindows =
516e5dd7070Spatrick TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment();
517e5dd7070Spatrick
518e5dd7070Spatrick const char *Prefix =
519e5dd7070Spatrick IsITANMSVCWindows || Type == ToolChain::FT_Object ? "" : "lib";
520e5dd7070Spatrick const char *Suffix;
521e5dd7070Spatrick switch (Type) {
522e5dd7070Spatrick case ToolChain::FT_Object:
523e5dd7070Spatrick Suffix = IsITANMSVCWindows ? ".obj" : ".o";
524e5dd7070Spatrick break;
525e5dd7070Spatrick case ToolChain::FT_Static:
526e5dd7070Spatrick Suffix = IsITANMSVCWindows ? ".lib" : ".a";
527e5dd7070Spatrick break;
528e5dd7070Spatrick case ToolChain::FT_Shared:
529a9ac8606Spatrick Suffix = TT.isOSWindows()
530a9ac8606Spatrick ? (TT.isWindowsGNUEnvironment() ? ".dll.a" : ".lib")
531e5dd7070Spatrick : ".so";
532e5dd7070Spatrick break;
533e5dd7070Spatrick }
534e5dd7070Spatrick
535ec727ea7Spatrick std::string ArchAndEnv;
536ec727ea7Spatrick if (AddArch) {
537e5dd7070Spatrick StringRef Arch = getArchNameForCompilerRTLib(*this, Args);
538e5dd7070Spatrick const char *Env = TT.isAndroid() ? "-android" : "";
539ec727ea7Spatrick ArchAndEnv = ("-" + Arch + Env).str();
540ec727ea7Spatrick }
541ec727ea7Spatrick return (Prefix + Twine("clang_rt.") + Component + ArchAndEnv + Suffix).str();
542ec727ea7Spatrick }
543ec727ea7Spatrick
getCompilerRT(const ArgList & Args,StringRef Component,FileType Type) const544ec727ea7Spatrick std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
545ec727ea7Spatrick FileType Type) const {
546ec727ea7Spatrick // Check for runtime files in the new layout without the architecture first.
547ec727ea7Spatrick std::string CRTBasename =
548a9ac8606Spatrick buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false);
549ec727ea7Spatrick for (const auto &LibPath : getLibraryPaths()) {
550ec727ea7Spatrick SmallString<128> P(LibPath);
551ec727ea7Spatrick llvm::sys::path::append(P, CRTBasename);
552ec727ea7Spatrick if (getVFS().exists(P))
553ec727ea7Spatrick return std::string(P.str());
554ec727ea7Spatrick }
555ec727ea7Spatrick
556ec727ea7Spatrick // Fall back to the old expected compiler-rt name if the new one does not
557ec727ea7Spatrick // exist.
558a9ac8606Spatrick CRTBasename =
559a9ac8606Spatrick buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/true);
560e5dd7070Spatrick SmallString<128> Path(getCompilerRTPath());
561ec727ea7Spatrick llvm::sys::path::append(Path, CRTBasename);
562ec727ea7Spatrick return std::string(Path.str());
563e5dd7070Spatrick }
564e5dd7070Spatrick
getCompilerRTArgString(const llvm::opt::ArgList & Args,StringRef Component,FileType Type) const565e5dd7070Spatrick const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args,
566e5dd7070Spatrick StringRef Component,
567e5dd7070Spatrick FileType Type) const {
568e5dd7070Spatrick return Args.MakeArgString(getCompilerRT(Args, Component, Type));
569e5dd7070Spatrick }
570e5dd7070Spatrick
getRuntimePaths() const571*12c85518Srobert ToolChain::path_list ToolChain::getRuntimePaths() const {
572*12c85518Srobert path_list Paths;
573*12c85518Srobert auto addPathForTriple = [this, &Paths](const llvm::Triple &Triple) {
574a9ac8606Spatrick SmallString<128> P(D.ResourceDir);
575*12c85518Srobert llvm::sys::path::append(P, "lib", Triple.str());
576*12c85518Srobert Paths.push_back(std::string(P.str()));
577*12c85518Srobert };
578*12c85518Srobert
579*12c85518Srobert addPathForTriple(getTriple());
580*12c85518Srobert
581*12c85518Srobert // Android targets may include an API level at the end. We still want to fall
582*12c85518Srobert // back on a path without the API level.
583*12c85518Srobert if (getTriple().isAndroid() &&
584*12c85518Srobert getTriple().getEnvironmentName() != "android") {
585*12c85518Srobert llvm::Triple TripleWithoutLevel = getTriple();
586*12c85518Srobert TripleWithoutLevel.setEnvironmentName("android");
587*12c85518Srobert addPathForTriple(TripleWithoutLevel);
588e5dd7070Spatrick }
589e5dd7070Spatrick
590*12c85518Srobert return Paths;
591*12c85518Srobert }
592*12c85518Srobert
getStdlibPaths() const593*12c85518Srobert ToolChain::path_list ToolChain::getStdlibPaths() const {
594*12c85518Srobert path_list Paths;
595a9ac8606Spatrick SmallString<128> P(D.Dir);
596a9ac8606Spatrick llvm::sys::path::append(P, "..", "lib", getTripleString());
597*12c85518Srobert Paths.push_back(std::string(P.str()));
598*12c85518Srobert
599*12c85518Srobert return Paths;
600e5dd7070Spatrick }
601e5dd7070Spatrick
getArchSpecificLibPath() const602e5dd7070Spatrick std::string ToolChain::getArchSpecificLibPath() const {
603e5dd7070Spatrick SmallString<128> Path(getDriver().ResourceDir);
604e5dd7070Spatrick llvm::sys::path::append(Path, "lib", getOSLibName(),
605e5dd7070Spatrick llvm::Triple::getArchTypeName(getArch()));
606ec727ea7Spatrick return std::string(Path.str());
607e5dd7070Spatrick }
608e5dd7070Spatrick
needsProfileRT(const ArgList & Args)609e5dd7070Spatrick bool ToolChain::needsProfileRT(const ArgList &Args) {
610e5dd7070Spatrick if (Args.hasArg(options::OPT_noprofilelib))
611e5dd7070Spatrick return false;
612e5dd7070Spatrick
613ec727ea7Spatrick return Args.hasArg(options::OPT_fprofile_generate) ||
614e5dd7070Spatrick Args.hasArg(options::OPT_fprofile_generate_EQ) ||
615e5dd7070Spatrick Args.hasArg(options::OPT_fcs_profile_generate) ||
616e5dd7070Spatrick Args.hasArg(options::OPT_fcs_profile_generate_EQ) ||
617e5dd7070Spatrick Args.hasArg(options::OPT_fprofile_instr_generate) ||
618e5dd7070Spatrick Args.hasArg(options::OPT_fprofile_instr_generate_EQ) ||
619e5dd7070Spatrick Args.hasArg(options::OPT_fcreate_profile) ||
620ec727ea7Spatrick Args.hasArg(options::OPT_forder_file_instrumentation);
621e5dd7070Spatrick }
622e5dd7070Spatrick
needsGCovInstrumentation(const llvm::opt::ArgList & Args)623e5dd7070Spatrick bool ToolChain::needsGCovInstrumentation(const llvm::opt::ArgList &Args) {
624ec727ea7Spatrick return Args.hasArg(options::OPT_coverage) ||
625ec727ea7Spatrick Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
626ec727ea7Spatrick false);
627e5dd7070Spatrick }
628e5dd7070Spatrick
SelectTool(const JobAction & JA) const629e5dd7070Spatrick Tool *ToolChain::SelectTool(const JobAction &JA) const {
630e5dd7070Spatrick if (D.IsFlangMode() && getDriver().ShouldUseFlangCompiler(JA)) return getFlang();
631e5dd7070Spatrick if (getDriver().ShouldUseClangCompiler(JA)) return getClang();
632e5dd7070Spatrick Action::ActionClass AC = JA.getKind();
633e5dd7070Spatrick if (AC == Action::AssembleJobClass && useIntegratedAs())
634e5dd7070Spatrick return getClangAs();
635e5dd7070Spatrick return getTool(AC);
636e5dd7070Spatrick }
637e5dd7070Spatrick
GetFilePath(const char * Name) const638e5dd7070Spatrick std::string ToolChain::GetFilePath(const char *Name) const {
639e5dd7070Spatrick return D.GetFilePath(Name, *this);
640e5dd7070Spatrick }
641e5dd7070Spatrick
GetProgramPath(const char * Name) const642e5dd7070Spatrick std::string ToolChain::GetProgramPath(const char *Name) const {
643e5dd7070Spatrick return D.GetProgramPath(Name, *this);
644e5dd7070Spatrick }
645e5dd7070Spatrick
GetLinkerPath(bool * LinkerIsLLD) const646*12c85518Srobert std::string ToolChain::GetLinkerPath(bool *LinkerIsLLD) const {
647a9ac8606Spatrick if (LinkerIsLLD)
648a9ac8606Spatrick *LinkerIsLLD = false;
649a9ac8606Spatrick
650a9ac8606Spatrick // Get -fuse-ld= first to prevent -Wunused-command-line-argument. -fuse-ld= is
651a9ac8606Spatrick // considered as the linker flavor, e.g. "bfd", "gold", or "lld".
652e5dd7070Spatrick const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ);
653e5dd7070Spatrick StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
654e5dd7070Spatrick
655a9ac8606Spatrick // --ld-path= takes precedence over -fuse-ld= and specifies the executable
656a9ac8606Spatrick // name. -B, COMPILER_PATH and PATH and consulted if the value does not
657a9ac8606Spatrick // contain a path component separator.
658*12c85518Srobert // -fuse-ld=lld can be used with --ld-path= to inform clang that the binary
659*12c85518Srobert // that --ld-path= points to is lld.
660a9ac8606Spatrick if (const Arg *A = Args.getLastArg(options::OPT_ld_path_EQ)) {
661a9ac8606Spatrick std::string Path(A->getValue());
662a9ac8606Spatrick if (!Path.empty()) {
663a9ac8606Spatrick if (llvm::sys::path::parent_path(Path).empty())
664a9ac8606Spatrick Path = GetProgramPath(A->getValue());
665*12c85518Srobert if (llvm::sys::fs::can_execute(Path)) {
666*12c85518Srobert if (LinkerIsLLD)
667*12c85518Srobert *LinkerIsLLD = UseLinker == "lld";
668a9ac8606Spatrick return std::string(Path);
669a9ac8606Spatrick }
670*12c85518Srobert }
671a9ac8606Spatrick getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
672a9ac8606Spatrick return GetProgramPath(getDefaultLinker());
673a9ac8606Spatrick }
674a9ac8606Spatrick // If we're passed -fuse-ld= with no argument, or with the argument ld,
675a9ac8606Spatrick // then use whatever the default system linker is.
676a9ac8606Spatrick if (UseLinker.empty() || UseLinker == "ld") {
677a9ac8606Spatrick const char *DefaultLinker = getDefaultLinker();
678a9ac8606Spatrick if (llvm::sys::path::is_absolute(DefaultLinker))
679a9ac8606Spatrick return std::string(DefaultLinker);
680a9ac8606Spatrick else
681a9ac8606Spatrick return GetProgramPath(DefaultLinker);
682a9ac8606Spatrick }
683a9ac8606Spatrick
684a9ac8606Spatrick // Extending -fuse-ld= to an absolute or relative path is unexpected. Checking
685a9ac8606Spatrick // for the linker flavor is brittle. In addition, prepending "ld." or "ld64."
686a9ac8606Spatrick // to a relative path is surprising. This is more complex due to priorities
687a9ac8606Spatrick // among -B, COMPILER_PATH and PATH. --ld-path= should be used instead.
688*12c85518Srobert if (UseLinker.contains('/'))
689a9ac8606Spatrick getDriver().Diag(diag::warn_drv_fuse_ld_path);
690a9ac8606Spatrick
691e5dd7070Spatrick if (llvm::sys::path::is_absolute(UseLinker)) {
692e5dd7070Spatrick // If we're passed what looks like an absolute path, don't attempt to
693e5dd7070Spatrick // second-guess that.
694e5dd7070Spatrick if (llvm::sys::fs::can_execute(UseLinker))
695ec727ea7Spatrick return std::string(UseLinker);
696e5dd7070Spatrick } else {
697e5dd7070Spatrick llvm::SmallString<8> LinkerName;
698e5dd7070Spatrick if (Triple.isOSDarwin())
699e5dd7070Spatrick LinkerName.append("ld64.");
700e5dd7070Spatrick else
701e5dd7070Spatrick LinkerName.append("ld.");
702e5dd7070Spatrick LinkerName.append(UseLinker);
703e5dd7070Spatrick
704e5dd7070Spatrick std::string LinkerPath(GetProgramPath(LinkerName.c_str()));
705a9ac8606Spatrick if (llvm::sys::fs::can_execute(LinkerPath)) {
706a9ac8606Spatrick if (LinkerIsLLD)
707*12c85518Srobert *LinkerIsLLD = UseLinker == "lld";
708e5dd7070Spatrick return LinkerPath;
709e5dd7070Spatrick }
710a9ac8606Spatrick }
711e5dd7070Spatrick
712e5dd7070Spatrick if (A)
713e5dd7070Spatrick getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
714e5dd7070Spatrick
715e5dd7070Spatrick return GetProgramPath(getDefaultLinker());
716e5dd7070Spatrick }
717e5dd7070Spatrick
GetStaticLibToolPath() const718ec727ea7Spatrick std::string ToolChain::GetStaticLibToolPath() const {
719ec727ea7Spatrick // TODO: Add support for static lib archiving on Windows
720*12c85518Srobert if (Triple.isOSDarwin())
721*12c85518Srobert return GetProgramPath("libtool");
722ec727ea7Spatrick return GetProgramPath("llvm-ar");
723ec727ea7Spatrick }
724ec727ea7Spatrick
LookupTypeForExtension(StringRef Ext) const725e5dd7070Spatrick types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const {
726e5dd7070Spatrick types::ID id = types::lookupTypeForExtension(Ext);
727e5dd7070Spatrick
728e5dd7070Spatrick // Flang always runs the preprocessor and has no notion of "preprocessed
729e5dd7070Spatrick // fortran". Here, TY_PP_Fortran is coerced to TY_Fortran to avoid treating
730e5dd7070Spatrick // them differently.
731e5dd7070Spatrick if (D.IsFlangMode() && id == types::TY_PP_Fortran)
732e5dd7070Spatrick id = types::TY_Fortran;
733e5dd7070Spatrick
734e5dd7070Spatrick return id;
735e5dd7070Spatrick }
736e5dd7070Spatrick
HasNativeLLVMSupport() const737e5dd7070Spatrick bool ToolChain::HasNativeLLVMSupport() const {
738e5dd7070Spatrick return false;
739e5dd7070Spatrick }
740e5dd7070Spatrick
isCrossCompiling() const741e5dd7070Spatrick bool ToolChain::isCrossCompiling() const {
742e5dd7070Spatrick llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
743e5dd7070Spatrick switch (HostTriple.getArch()) {
744e5dd7070Spatrick // The A32/T32/T16 instruction sets are not separate architectures in this
745e5dd7070Spatrick // context.
746e5dd7070Spatrick case llvm::Triple::arm:
747e5dd7070Spatrick case llvm::Triple::armeb:
748e5dd7070Spatrick case llvm::Triple::thumb:
749e5dd7070Spatrick case llvm::Triple::thumbeb:
750e5dd7070Spatrick return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb &&
751e5dd7070Spatrick getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb;
752e5dd7070Spatrick default:
753e5dd7070Spatrick return HostTriple.getArch() != getArch();
754e5dd7070Spatrick }
755e5dd7070Spatrick }
756e5dd7070Spatrick
getDefaultObjCRuntime(bool isNonFragile) const757e5dd7070Spatrick ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
758e5dd7070Spatrick return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
759e5dd7070Spatrick VersionTuple());
760e5dd7070Spatrick }
761e5dd7070Spatrick
762e5dd7070Spatrick llvm::ExceptionHandling
GetExceptionModel(const llvm::opt::ArgList & Args) const763e5dd7070Spatrick ToolChain::GetExceptionModel(const llvm::opt::ArgList &Args) const {
764e5dd7070Spatrick return llvm::ExceptionHandling::None;
765e5dd7070Spatrick }
766e5dd7070Spatrick
isThreadModelSupported(const StringRef Model) const767e5dd7070Spatrick bool ToolChain::isThreadModelSupported(const StringRef Model) const {
768e5dd7070Spatrick if (Model == "single") {
769e5dd7070Spatrick // FIXME: 'single' is only supported on ARM and WebAssembly so far.
770e5dd7070Spatrick return Triple.getArch() == llvm::Triple::arm ||
771e5dd7070Spatrick Triple.getArch() == llvm::Triple::armeb ||
772e5dd7070Spatrick Triple.getArch() == llvm::Triple::thumb ||
773a9ac8606Spatrick Triple.getArch() == llvm::Triple::thumbeb || Triple.isWasm();
774e5dd7070Spatrick } else if (Model == "posix")
775e5dd7070Spatrick return true;
776e5dd7070Spatrick
777e5dd7070Spatrick return false;
778e5dd7070Spatrick }
779e5dd7070Spatrick
ComputeLLVMTriple(const ArgList & Args,types::ID InputType) const780e5dd7070Spatrick std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
781e5dd7070Spatrick types::ID InputType) const {
782e5dd7070Spatrick switch (getTriple().getArch()) {
783e5dd7070Spatrick default:
784e5dd7070Spatrick return getTripleString();
785e5dd7070Spatrick
786e5dd7070Spatrick case llvm::Triple::x86_64: {
787e5dd7070Spatrick llvm::Triple Triple = getTriple();
788e5dd7070Spatrick if (!Triple.isOSBinFormatMachO())
789e5dd7070Spatrick return getTripleString();
790e5dd7070Spatrick
791e5dd7070Spatrick if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
792e5dd7070Spatrick // x86_64h goes in the triple. Other -march options just use the
793e5dd7070Spatrick // vanilla triple we already have.
794e5dd7070Spatrick StringRef MArch = A->getValue();
795e5dd7070Spatrick if (MArch == "x86_64h")
796e5dd7070Spatrick Triple.setArchName(MArch);
797e5dd7070Spatrick }
798e5dd7070Spatrick return Triple.getTriple();
799e5dd7070Spatrick }
800e5dd7070Spatrick case llvm::Triple::aarch64: {
801e5dd7070Spatrick llvm::Triple Triple = getTriple();
802e5dd7070Spatrick if (!Triple.isOSBinFormatMachO())
803e5dd7070Spatrick return getTripleString();
804e5dd7070Spatrick
805a9ac8606Spatrick if (Triple.isArm64e())
806a9ac8606Spatrick return getTripleString();
807a9ac8606Spatrick
808e5dd7070Spatrick // FIXME: older versions of ld64 expect the "arm64" component in the actual
809e5dd7070Spatrick // triple string and query it to determine whether an LTO file can be
810e5dd7070Spatrick // handled. Remove this when we don't care any more.
811e5dd7070Spatrick Triple.setArchName("arm64");
812e5dd7070Spatrick return Triple.getTriple();
813e5dd7070Spatrick }
814e5dd7070Spatrick case llvm::Triple::aarch64_32:
815e5dd7070Spatrick return getTripleString();
816e5dd7070Spatrick case llvm::Triple::arm:
817e5dd7070Spatrick case llvm::Triple::armeb:
818e5dd7070Spatrick case llvm::Triple::thumb:
819e5dd7070Spatrick case llvm::Triple::thumbeb: {
820e5dd7070Spatrick llvm::Triple Triple = getTriple();
821a9ac8606Spatrick tools::arm::setArchNameInTriple(getDriver(), Args, InputType, Triple);
822a9ac8606Spatrick tools::arm::setFloatABIInTriple(getDriver(), Args, Triple);
823e5dd7070Spatrick return Triple.getTriple();
824e5dd7070Spatrick }
825e5dd7070Spatrick }
826e5dd7070Spatrick }
827e5dd7070Spatrick
ComputeEffectiveClangTriple(const ArgList & Args,types::ID InputType) const828e5dd7070Spatrick std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
829e5dd7070Spatrick types::ID InputType) const {
830e5dd7070Spatrick return ComputeLLVMTriple(Args, InputType);
831e5dd7070Spatrick }
832e5dd7070Spatrick
computeSysRoot() const833ec727ea7Spatrick std::string ToolChain::computeSysRoot() const {
834ec727ea7Spatrick return D.SysRoot;
835ec727ea7Spatrick }
836ec727ea7Spatrick
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const837e5dd7070Spatrick void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
838e5dd7070Spatrick ArgStringList &CC1Args) const {
839e5dd7070Spatrick // Each toolchain should provide the appropriate include flags.
840e5dd7070Spatrick }
841e5dd7070Spatrick
addClangTargetOptions(const ArgList & DriverArgs,ArgStringList & CC1Args,Action::OffloadKind DeviceOffloadKind) const842e5dd7070Spatrick void ToolChain::addClangTargetOptions(
843e5dd7070Spatrick const ArgList &DriverArgs, ArgStringList &CC1Args,
844e5dd7070Spatrick Action::OffloadKind DeviceOffloadKind) const {}
845e5dd7070Spatrick
addClangCC1ASTargetOptions(const ArgList & Args,ArgStringList & CC1ASArgs) const846*12c85518Srobert void ToolChain::addClangCC1ASTargetOptions(const ArgList &Args,
847*12c85518Srobert ArgStringList &CC1ASArgs) const {}
848*12c85518Srobert
addClangWarningOptions(ArgStringList & CC1Args) const849e5dd7070Spatrick void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {}
850e5dd7070Spatrick
addProfileRTLibs(const llvm::opt::ArgList & Args,llvm::opt::ArgStringList & CmdArgs) const851e5dd7070Spatrick void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args,
852e5dd7070Spatrick llvm::opt::ArgStringList &CmdArgs) const {
853ec727ea7Spatrick if (!needsProfileRT(Args) && !needsGCovInstrumentation(Args))
854ec727ea7Spatrick return;
855e5dd7070Spatrick
856e5dd7070Spatrick CmdArgs.push_back(getCompilerRTArgString(Args, "profile"));
857e5dd7070Spatrick }
858e5dd7070Spatrick
GetRuntimeLibType(const ArgList & Args) const859e5dd7070Spatrick ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
860e5dd7070Spatrick const ArgList &Args) const {
861a9ac8606Spatrick if (runtimeLibType)
862a9ac8606Spatrick return *runtimeLibType;
863a9ac8606Spatrick
864e5dd7070Spatrick const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ);
865e5dd7070Spatrick StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB;
866e5dd7070Spatrick
867e5dd7070Spatrick // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB!
868e5dd7070Spatrick if (LibName == "compiler-rt")
869a9ac8606Spatrick runtimeLibType = ToolChain::RLT_CompilerRT;
870e5dd7070Spatrick else if (LibName == "libgcc")
871a9ac8606Spatrick runtimeLibType = ToolChain::RLT_Libgcc;
872e5dd7070Spatrick else if (LibName == "platform")
873a9ac8606Spatrick runtimeLibType = GetDefaultRuntimeLibType();
874a9ac8606Spatrick else {
875e5dd7070Spatrick if (A)
876a9ac8606Spatrick getDriver().Diag(diag::err_drv_invalid_rtlib_name)
877a9ac8606Spatrick << A->getAsString(Args);
878e5dd7070Spatrick
879a9ac8606Spatrick runtimeLibType = GetDefaultRuntimeLibType();
880a9ac8606Spatrick }
881a9ac8606Spatrick
882a9ac8606Spatrick return *runtimeLibType;
883e5dd7070Spatrick }
884e5dd7070Spatrick
GetUnwindLibType(const ArgList & Args) const885e5dd7070Spatrick ToolChain::UnwindLibType ToolChain::GetUnwindLibType(
886e5dd7070Spatrick const ArgList &Args) const {
887a9ac8606Spatrick if (unwindLibType)
888a9ac8606Spatrick return *unwindLibType;
889a9ac8606Spatrick
890e5dd7070Spatrick const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ);
891e5dd7070Spatrick StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_UNWINDLIB;
892e5dd7070Spatrick
893e5dd7070Spatrick if (LibName == "none")
894a9ac8606Spatrick unwindLibType = ToolChain::UNW_None;
895e5dd7070Spatrick else if (LibName == "platform" || LibName == "") {
896e5dd7070Spatrick ToolChain::RuntimeLibType RtLibType = GetRuntimeLibType(Args);
897a9ac8606Spatrick if (RtLibType == ToolChain::RLT_CompilerRT) {
898a9ac8606Spatrick if (getTriple().isAndroid() || getTriple().isOSAIX())
899a9ac8606Spatrick unwindLibType = ToolChain::UNW_CompilerRT;
900a9ac8606Spatrick else
901a9ac8606Spatrick unwindLibType = ToolChain::UNW_None;
902a9ac8606Spatrick } else if (RtLibType == ToolChain::RLT_Libgcc)
903a9ac8606Spatrick unwindLibType = ToolChain::UNW_Libgcc;
904e5dd7070Spatrick } else if (LibName == "libunwind") {
905e5dd7070Spatrick if (GetRuntimeLibType(Args) == RLT_Libgcc)
906e5dd7070Spatrick getDriver().Diag(diag::err_drv_incompatible_unwindlib);
907a9ac8606Spatrick unwindLibType = ToolChain::UNW_CompilerRT;
908e5dd7070Spatrick } else if (LibName == "libgcc")
909a9ac8606Spatrick unwindLibType = ToolChain::UNW_Libgcc;
910a9ac8606Spatrick else {
911e5dd7070Spatrick if (A)
912e5dd7070Spatrick getDriver().Diag(diag::err_drv_invalid_unwindlib_name)
913e5dd7070Spatrick << A->getAsString(Args);
914e5dd7070Spatrick
915a9ac8606Spatrick unwindLibType = GetDefaultUnwindLibType();
916a9ac8606Spatrick }
917a9ac8606Spatrick
918a9ac8606Spatrick return *unwindLibType;
919e5dd7070Spatrick }
920e5dd7070Spatrick
GetCXXStdlibType(const ArgList & Args) const921e5dd7070Spatrick ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
922a9ac8606Spatrick if (cxxStdlibType)
923a9ac8606Spatrick return *cxxStdlibType;
924a9ac8606Spatrick
925e5dd7070Spatrick const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
926e5dd7070Spatrick StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB;
927e5dd7070Spatrick
928e5dd7070Spatrick // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB!
929e5dd7070Spatrick if (LibName == "libc++")
930a9ac8606Spatrick cxxStdlibType = ToolChain::CST_Libcxx;
931e5dd7070Spatrick else if (LibName == "libstdc++")
932a9ac8606Spatrick cxxStdlibType = ToolChain::CST_Libstdcxx;
933e5dd7070Spatrick else if (LibName == "platform")
934a9ac8606Spatrick cxxStdlibType = GetDefaultCXXStdlibType();
935a9ac8606Spatrick else {
936e5dd7070Spatrick if (A)
937a9ac8606Spatrick getDriver().Diag(diag::err_drv_invalid_stdlib_name)
938a9ac8606Spatrick << A->getAsString(Args);
939e5dd7070Spatrick
940a9ac8606Spatrick cxxStdlibType = GetDefaultCXXStdlibType();
941a9ac8606Spatrick }
942a9ac8606Spatrick
943a9ac8606Spatrick return *cxxStdlibType;
944e5dd7070Spatrick }
945e5dd7070Spatrick
946e5dd7070Spatrick /// Utility function to add a system include directory to CC1 arguments.
addSystemInclude(const ArgList & DriverArgs,ArgStringList & CC1Args,const Twine & Path)947e5dd7070Spatrick /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
948e5dd7070Spatrick ArgStringList &CC1Args,
949e5dd7070Spatrick const Twine &Path) {
950e5dd7070Spatrick CC1Args.push_back("-internal-isystem");
951e5dd7070Spatrick CC1Args.push_back(DriverArgs.MakeArgString(Path));
952e5dd7070Spatrick }
953e5dd7070Spatrick
954e5dd7070Spatrick /// Utility function to add a system include directory with extern "C"
955e5dd7070Spatrick /// semantics to CC1 arguments.
956e5dd7070Spatrick ///
957e5dd7070Spatrick /// Note that this should be used rarely, and only for directories that
958e5dd7070Spatrick /// historically and for legacy reasons are treated as having implicit extern
959e5dd7070Spatrick /// "C" semantics. These semantics are *ignored* by and large today, but its
960e5dd7070Spatrick /// important to preserve the preprocessor changes resulting from the
961e5dd7070Spatrick /// classification.
addExternCSystemInclude(const ArgList & DriverArgs,ArgStringList & CC1Args,const Twine & Path)962e5dd7070Spatrick /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
963e5dd7070Spatrick ArgStringList &CC1Args,
964e5dd7070Spatrick const Twine &Path) {
965e5dd7070Spatrick CC1Args.push_back("-internal-externc-isystem");
966e5dd7070Spatrick CC1Args.push_back(DriverArgs.MakeArgString(Path));
967e5dd7070Spatrick }
968e5dd7070Spatrick
addExternCSystemIncludeIfExists(const ArgList & DriverArgs,ArgStringList & CC1Args,const Twine & Path)969e5dd7070Spatrick void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
970e5dd7070Spatrick ArgStringList &CC1Args,
971e5dd7070Spatrick const Twine &Path) {
972e5dd7070Spatrick if (llvm::sys::fs::exists(Path))
973e5dd7070Spatrick addExternCSystemInclude(DriverArgs, CC1Args, Path);
974e5dd7070Spatrick }
975e5dd7070Spatrick
976e5dd7070Spatrick /// Utility function to add a list of system include directories to CC1.
addSystemIncludes(const ArgList & DriverArgs,ArgStringList & CC1Args,ArrayRef<StringRef> Paths)977e5dd7070Spatrick /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
978e5dd7070Spatrick ArgStringList &CC1Args,
979e5dd7070Spatrick ArrayRef<StringRef> Paths) {
980e5dd7070Spatrick for (const auto &Path : Paths) {
981e5dd7070Spatrick CC1Args.push_back("-internal-isystem");
982e5dd7070Spatrick CC1Args.push_back(DriverArgs.MakeArgString(Path));
983e5dd7070Spatrick }
984e5dd7070Spatrick }
985e5dd7070Spatrick
concat(StringRef Path,const Twine & A,const Twine & B,const Twine & C,const Twine & D)986*12c85518Srobert /*static*/ std::string ToolChain::concat(StringRef Path, const Twine &A,
987*12c85518Srobert const Twine &B, const Twine &C,
988*12c85518Srobert const Twine &D) {
989*12c85518Srobert SmallString<128> Result(Path);
990*12c85518Srobert llvm::sys::path::append(Result, llvm::sys::path::Style::posix, A, B, C, D);
991*12c85518Srobert return std::string(Result);
992*12c85518Srobert }
993*12c85518Srobert
detectLibcxxVersion(StringRef IncludePath) const994a9ac8606Spatrick std::string ToolChain::detectLibcxxVersion(StringRef IncludePath) const {
995a9ac8606Spatrick std::error_code EC;
996a9ac8606Spatrick int MaxVersion = 0;
997a9ac8606Spatrick std::string MaxVersionString;
998a9ac8606Spatrick SmallString<128> Path(IncludePath);
999a9ac8606Spatrick llvm::sys::path::append(Path, "c++");
1000a9ac8606Spatrick for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
1001a9ac8606Spatrick !EC && LI != LE; LI = LI.increment(EC)) {
1002a9ac8606Spatrick StringRef VersionText = llvm::sys::path::filename(LI->path());
1003a9ac8606Spatrick int Version;
1004a9ac8606Spatrick if (VersionText[0] == 'v' &&
1005a9ac8606Spatrick !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
1006a9ac8606Spatrick if (Version > MaxVersion) {
1007a9ac8606Spatrick MaxVersion = Version;
1008a9ac8606Spatrick MaxVersionString = std::string(VersionText);
1009a9ac8606Spatrick }
1010a9ac8606Spatrick }
1011a9ac8606Spatrick }
1012a9ac8606Spatrick if (!MaxVersion)
1013a9ac8606Spatrick return "";
1014a9ac8606Spatrick return MaxVersionString;
1015a9ac8606Spatrick }
1016a9ac8606Spatrick
AddClangCXXStdlibIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const1017e5dd7070Spatrick void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1018e5dd7070Spatrick ArgStringList &CC1Args) const {
1019e5dd7070Spatrick // Header search paths should be handled by each of the subclasses.
1020e5dd7070Spatrick // Historically, they have not been, and instead have been handled inside of
1021e5dd7070Spatrick // the CC1-layer frontend. As the logic is hoisted out, this generic function
1022e5dd7070Spatrick // will slowly stop being called.
1023e5dd7070Spatrick //
1024e5dd7070Spatrick // While it is being called, replicate a bit of a hack to propagate the
1025e5dd7070Spatrick // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
1026e5dd7070Spatrick // header search paths with it. Once all systems are overriding this
1027e5dd7070Spatrick // function, the CC1 flag and this line can be removed.
1028e5dd7070Spatrick DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
1029e5dd7070Spatrick }
1030e5dd7070Spatrick
AddClangCXXStdlibIsystemArgs(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const1031e5dd7070Spatrick void ToolChain::AddClangCXXStdlibIsystemArgs(
1032e5dd7070Spatrick const llvm::opt::ArgList &DriverArgs,
1033e5dd7070Spatrick llvm::opt::ArgStringList &CC1Args) const {
1034e5dd7070Spatrick DriverArgs.ClaimAllArgs(options::OPT_stdlibxx_isystem);
1035*12c85518Srobert // This intentionally only looks at -nostdinc++, and not -nostdinc or
1036*12c85518Srobert // -nostdlibinc. The purpose of -stdlib++-isystem is to support toolchain
1037*12c85518Srobert // setups with non-standard search logic for the C++ headers, while still
1038*12c85518Srobert // allowing users of the toolchain to bring their own C++ headers. Such a
1039*12c85518Srobert // toolchain likely also has non-standard search logic for the C headers and
1040*12c85518Srobert // uses -nostdinc to suppress the default logic, but -stdlib++-isystem should
1041*12c85518Srobert // still work in that case and only be suppressed by an explicit -nostdinc++
1042*12c85518Srobert // in a project using the toolchain.
1043*12c85518Srobert if (!DriverArgs.hasArg(options::OPT_nostdincxx))
1044e5dd7070Spatrick for (const auto &P :
1045e5dd7070Spatrick DriverArgs.getAllArgValues(options::OPT_stdlibxx_isystem))
1046e5dd7070Spatrick addSystemInclude(DriverArgs, CC1Args, P);
1047e5dd7070Spatrick }
1048e5dd7070Spatrick
ShouldLinkCXXStdlib(const llvm::opt::ArgList & Args) const1049e5dd7070Spatrick bool ToolChain::ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const {
1050e5dd7070Spatrick return getDriver().CCCIsCXX() &&
1051e5dd7070Spatrick !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
1052e5dd7070Spatrick options::OPT_nostdlibxx);
1053e5dd7070Spatrick }
1054e5dd7070Spatrick
AddCXXStdlibLibArgs(const ArgList & Args,ArgStringList & CmdArgs) const1055e5dd7070Spatrick void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
1056e5dd7070Spatrick ArgStringList &CmdArgs) const {
1057e5dd7070Spatrick assert(!Args.hasArg(options::OPT_nostdlibxx) &&
1058e5dd7070Spatrick "should not have called this");
1059e5dd7070Spatrick CXXStdlibType Type = GetCXXStdlibType(Args);
1060e5dd7070Spatrick
1061e5dd7070Spatrick switch (Type) {
1062e5dd7070Spatrick case ToolChain::CST_Libcxx:
1063e5dd7070Spatrick CmdArgs.push_back("-lc++");
1064*12c85518Srobert if (Args.hasArg(options::OPT_fexperimental_library))
1065*12c85518Srobert CmdArgs.push_back("-lc++experimental");
1066e5dd7070Spatrick break;
1067e5dd7070Spatrick
1068e5dd7070Spatrick case ToolChain::CST_Libstdcxx:
1069e5dd7070Spatrick CmdArgs.push_back("-lstdc++");
1070e5dd7070Spatrick break;
1071e5dd7070Spatrick }
1072e5dd7070Spatrick }
1073e5dd7070Spatrick
AddFilePathLibArgs(const ArgList & Args,ArgStringList & CmdArgs) const1074e5dd7070Spatrick void ToolChain::AddFilePathLibArgs(const ArgList &Args,
1075e5dd7070Spatrick ArgStringList &CmdArgs) const {
1076e5dd7070Spatrick for (const auto &LibPath : getFilePaths())
1077e5dd7070Spatrick if(LibPath.length() > 0)
1078e5dd7070Spatrick CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
1079e5dd7070Spatrick }
1080e5dd7070Spatrick
AddCCKextLibArgs(const ArgList & Args,ArgStringList & CmdArgs) const1081e5dd7070Spatrick void ToolChain::AddCCKextLibArgs(const ArgList &Args,
1082e5dd7070Spatrick ArgStringList &CmdArgs) const {
1083e5dd7070Spatrick CmdArgs.push_back("-lcc_kext");
1084e5dd7070Spatrick }
1085e5dd7070Spatrick
isFastMathRuntimeAvailable(const ArgList & Args,std::string & Path) const1086ec727ea7Spatrick bool ToolChain::isFastMathRuntimeAvailable(const ArgList &Args,
1087ec727ea7Spatrick std::string &Path) const {
1088e5dd7070Spatrick // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
1089e5dd7070Spatrick // (to keep the linker options consistent with gcc and clang itself).
1090e5dd7070Spatrick if (!isOptimizationLevelFast(Args)) {
1091e5dd7070Spatrick // Check if -ffast-math or -funsafe-math.
1092e5dd7070Spatrick Arg *A =
1093e5dd7070Spatrick Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
1094e5dd7070Spatrick options::OPT_funsafe_math_optimizations,
1095e5dd7070Spatrick options::OPT_fno_unsafe_math_optimizations);
1096e5dd7070Spatrick
1097e5dd7070Spatrick if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
1098e5dd7070Spatrick A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
1099e5dd7070Spatrick return false;
1100e5dd7070Spatrick }
1101e5dd7070Spatrick // If crtfastmath.o exists add it to the arguments.
1102ec727ea7Spatrick Path = GetFilePath("crtfastmath.o");
1103ec727ea7Spatrick return (Path != "crtfastmath.o"); // Not found.
1104ec727ea7Spatrick }
1105e5dd7070Spatrick
addFastMathRuntimeIfAvailable(const ArgList & Args,ArgStringList & CmdArgs) const1106ec727ea7Spatrick bool ToolChain::addFastMathRuntimeIfAvailable(const ArgList &Args,
1107ec727ea7Spatrick ArgStringList &CmdArgs) const {
1108ec727ea7Spatrick std::string Path;
1109ec727ea7Spatrick if (isFastMathRuntimeAvailable(Args, Path)) {
1110e5dd7070Spatrick CmdArgs.push_back(Args.MakeArgString(Path));
1111e5dd7070Spatrick return true;
1112e5dd7070Spatrick }
1113e5dd7070Spatrick
1114ec727ea7Spatrick return false;
1115ec727ea7Spatrick }
1116ec727ea7Spatrick
1117*12c85518Srobert Expected<SmallVector<std::string>>
getSystemGPUArchs(const llvm::opt::ArgList & Args) const1118*12c85518Srobert ToolChain::getSystemGPUArchs(const llvm::opt::ArgList &Args) const {
1119*12c85518Srobert return SmallVector<std::string>();
1120*12c85518Srobert }
1121*12c85518Srobert
getSupportedSanitizers() const1122e5dd7070Spatrick SanitizerMask ToolChain::getSupportedSanitizers() const {
1123e5dd7070Spatrick // Return sanitizers which don't require runtime support and are not
1124e5dd7070Spatrick // platform dependent.
1125e5dd7070Spatrick
1126a9ac8606Spatrick SanitizerMask Res =
1127a9ac8606Spatrick (SanitizerKind::Undefined & ~SanitizerKind::Vptr &
1128e5dd7070Spatrick ~SanitizerKind::Function) |
1129e5dd7070Spatrick (SanitizerKind::CFI & ~SanitizerKind::CFIICall) |
1130a9ac8606Spatrick SanitizerKind::CFICastStrict | SanitizerKind::FloatDivideByZero |
1131*12c85518Srobert SanitizerKind::KCFI | SanitizerKind::UnsignedIntegerOverflow |
1132a9ac8606Spatrick SanitizerKind::UnsignedShiftBase | SanitizerKind::ImplicitConversion |
1133e5dd7070Spatrick SanitizerKind::Nullability | SanitizerKind::LocalBounds;
1134e5dd7070Spatrick if (getTriple().getArch() == llvm::Triple::x86 ||
1135e5dd7070Spatrick getTriple().getArch() == llvm::Triple::x86_64 ||
1136a9ac8606Spatrick getTriple().getArch() == llvm::Triple::arm || getTriple().isWasm() ||
1137*12c85518Srobert getTriple().isAArch64() || getTriple().isRISCV())
1138e5dd7070Spatrick Res |= SanitizerKind::CFIICall;
1139a9ac8606Spatrick if (getTriple().getArch() == llvm::Triple::x86_64 ||
1140a9ac8606Spatrick getTriple().isAArch64(64) || getTriple().isRISCV())
1141e5dd7070Spatrick Res |= SanitizerKind::ShadowCallStack;
1142a9ac8606Spatrick if (getTriple().isAArch64(64))
1143e5dd7070Spatrick Res |= SanitizerKind::MemTag;
1144e5dd7070Spatrick return Res;
1145e5dd7070Spatrick }
1146e5dd7070Spatrick
AddCudaIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const1147e5dd7070Spatrick void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
1148e5dd7070Spatrick ArgStringList &CC1Args) const {}
1149e5dd7070Spatrick
AddHIPIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const1150ec727ea7Spatrick void ToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
1151ec727ea7Spatrick ArgStringList &CC1Args) const {}
1152ec727ea7Spatrick
1153*12c85518Srobert llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
getDeviceLibs(const ArgList & DriverArgs) const1154*12c85518Srobert ToolChain::getDeviceLibs(const ArgList &DriverArgs) const {
1155a9ac8606Spatrick return {};
1156a9ac8606Spatrick }
1157a9ac8606Spatrick
AddIAMCUIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const1158e5dd7070Spatrick void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
1159e5dd7070Spatrick ArgStringList &CC1Args) const {}
1160e5dd7070Spatrick
separateMSVCFullVersion(unsigned Version)1161e5dd7070Spatrick static VersionTuple separateMSVCFullVersion(unsigned Version) {
1162e5dd7070Spatrick if (Version < 100)
1163e5dd7070Spatrick return VersionTuple(Version);
1164e5dd7070Spatrick
1165e5dd7070Spatrick if (Version < 10000)
1166e5dd7070Spatrick return VersionTuple(Version / 100, Version % 100);
1167e5dd7070Spatrick
1168e5dd7070Spatrick unsigned Build = 0, Factor = 1;
1169e5dd7070Spatrick for (; Version > 10000; Version = Version / 10, Factor = Factor * 10)
1170e5dd7070Spatrick Build = Build + (Version % 10) * Factor;
1171e5dd7070Spatrick return VersionTuple(Version / 100, Version % 100, Build);
1172e5dd7070Spatrick }
1173e5dd7070Spatrick
1174e5dd7070Spatrick VersionTuple
computeMSVCVersion(const Driver * D,const llvm::opt::ArgList & Args) const1175e5dd7070Spatrick ToolChain::computeMSVCVersion(const Driver *D,
1176e5dd7070Spatrick const llvm::opt::ArgList &Args) const {
1177e5dd7070Spatrick const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version);
1178e5dd7070Spatrick const Arg *MSCompatibilityVersion =
1179e5dd7070Spatrick Args.getLastArg(options::OPT_fms_compatibility_version);
1180e5dd7070Spatrick
1181e5dd7070Spatrick if (MSCVersion && MSCompatibilityVersion) {
1182e5dd7070Spatrick if (D)
1183e5dd7070Spatrick D->Diag(diag::err_drv_argument_not_allowed_with)
1184e5dd7070Spatrick << MSCVersion->getAsString(Args)
1185e5dd7070Spatrick << MSCompatibilityVersion->getAsString(Args);
1186e5dd7070Spatrick return VersionTuple();
1187e5dd7070Spatrick }
1188e5dd7070Spatrick
1189e5dd7070Spatrick if (MSCompatibilityVersion) {
1190e5dd7070Spatrick VersionTuple MSVT;
1191e5dd7070Spatrick if (MSVT.tryParse(MSCompatibilityVersion->getValue())) {
1192e5dd7070Spatrick if (D)
1193e5dd7070Spatrick D->Diag(diag::err_drv_invalid_value)
1194e5dd7070Spatrick << MSCompatibilityVersion->getAsString(Args)
1195e5dd7070Spatrick << MSCompatibilityVersion->getValue();
1196e5dd7070Spatrick } else {
1197e5dd7070Spatrick return MSVT;
1198e5dd7070Spatrick }
1199e5dd7070Spatrick }
1200e5dd7070Spatrick
1201e5dd7070Spatrick if (MSCVersion) {
1202e5dd7070Spatrick unsigned Version = 0;
1203e5dd7070Spatrick if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) {
1204e5dd7070Spatrick if (D)
1205e5dd7070Spatrick D->Diag(diag::err_drv_invalid_value)
1206e5dd7070Spatrick << MSCVersion->getAsString(Args) << MSCVersion->getValue();
1207e5dd7070Spatrick } else {
1208e5dd7070Spatrick return separateMSVCFullVersion(Version);
1209e5dd7070Spatrick }
1210e5dd7070Spatrick }
1211e5dd7070Spatrick
1212e5dd7070Spatrick return VersionTuple();
1213e5dd7070Spatrick }
1214e5dd7070Spatrick
TranslateOpenMPTargetArgs(const llvm::opt::DerivedArgList & Args,bool SameTripleAsHost,SmallVectorImpl<llvm::opt::Arg * > & AllocatedArgs) const1215e5dd7070Spatrick llvm::opt::DerivedArgList *ToolChain::TranslateOpenMPTargetArgs(
1216e5dd7070Spatrick const llvm::opt::DerivedArgList &Args, bool SameTripleAsHost,
1217e5dd7070Spatrick SmallVectorImpl<llvm::opt::Arg *> &AllocatedArgs) const {
1218e5dd7070Spatrick DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
1219e5dd7070Spatrick const OptTable &Opts = getDriver().getOpts();
1220e5dd7070Spatrick bool Modified = false;
1221e5dd7070Spatrick
1222e5dd7070Spatrick // Handle -Xopenmp-target flags
1223e5dd7070Spatrick for (auto *A : Args) {
1224e5dd7070Spatrick // Exclude flags which may only apply to the host toolchain.
1225e5dd7070Spatrick // Do not exclude flags when the host triple (AuxTriple)
1226e5dd7070Spatrick // matches the current toolchain triple. If it is not present
1227e5dd7070Spatrick // at all, target and host share a toolchain.
1228e5dd7070Spatrick if (A->getOption().matches(options::OPT_m_Group)) {
1229e5dd7070Spatrick if (SameTripleAsHost)
1230e5dd7070Spatrick DAL->append(A);
1231e5dd7070Spatrick else
1232e5dd7070Spatrick Modified = true;
1233e5dd7070Spatrick continue;
1234e5dd7070Spatrick }
1235e5dd7070Spatrick
1236e5dd7070Spatrick unsigned Index;
1237e5dd7070Spatrick unsigned Prev;
1238e5dd7070Spatrick bool XOpenMPTargetNoTriple =
1239e5dd7070Spatrick A->getOption().matches(options::OPT_Xopenmp_target);
1240e5dd7070Spatrick
1241e5dd7070Spatrick if (A->getOption().matches(options::OPT_Xopenmp_target_EQ)) {
1242*12c85518Srobert llvm::Triple TT(getOpenMPTriple(A->getValue(0)));
1243*12c85518Srobert
1244e5dd7070Spatrick // Passing device args: -Xopenmp-target=<triple> -opt=val.
1245*12c85518Srobert if (TT.getTriple() == getTripleString())
1246e5dd7070Spatrick Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
1247e5dd7070Spatrick else
1248e5dd7070Spatrick continue;
1249e5dd7070Spatrick } else if (XOpenMPTargetNoTriple) {
1250e5dd7070Spatrick // Passing device args: -Xopenmp-target -opt=val.
1251e5dd7070Spatrick Index = Args.getBaseArgs().MakeIndex(A->getValue(0));
1252e5dd7070Spatrick } else {
1253e5dd7070Spatrick DAL->append(A);
1254e5dd7070Spatrick continue;
1255e5dd7070Spatrick }
1256e5dd7070Spatrick
1257e5dd7070Spatrick // Parse the argument to -Xopenmp-target.
1258e5dd7070Spatrick Prev = Index;
1259e5dd7070Spatrick std::unique_ptr<Arg> XOpenMPTargetArg(Opts.ParseOneArg(Args, Index));
1260e5dd7070Spatrick if (!XOpenMPTargetArg || Index > Prev + 1) {
1261e5dd7070Spatrick getDriver().Diag(diag::err_drv_invalid_Xopenmp_target_with_args)
1262e5dd7070Spatrick << A->getAsString(Args);
1263e5dd7070Spatrick continue;
1264e5dd7070Spatrick }
1265e5dd7070Spatrick if (XOpenMPTargetNoTriple && XOpenMPTargetArg &&
1266e5dd7070Spatrick Args.getAllArgValues(options::OPT_fopenmp_targets_EQ).size() != 1) {
1267e5dd7070Spatrick getDriver().Diag(diag::err_drv_Xopenmp_target_missing_triple);
1268e5dd7070Spatrick continue;
1269e5dd7070Spatrick }
1270e5dd7070Spatrick XOpenMPTargetArg->setBaseArg(A);
1271e5dd7070Spatrick A = XOpenMPTargetArg.release();
1272e5dd7070Spatrick AllocatedArgs.push_back(A);
1273e5dd7070Spatrick DAL->append(A);
1274e5dd7070Spatrick Modified = true;
1275e5dd7070Spatrick }
1276e5dd7070Spatrick
1277e5dd7070Spatrick if (Modified)
1278e5dd7070Spatrick return DAL;
1279e5dd7070Spatrick
1280e5dd7070Spatrick delete DAL;
1281e5dd7070Spatrick return nullptr;
1282e5dd7070Spatrick }
1283ec727ea7Spatrick
1284ec727ea7Spatrick // TODO: Currently argument values separated by space e.g.
1285ec727ea7Spatrick // -Xclang -mframe-pointer=no cannot be passed by -Xarch_. This should be
1286ec727ea7Spatrick // fixed.
TranslateXarchArgs(const llvm::opt::DerivedArgList & Args,llvm::opt::Arg * & A,llvm::opt::DerivedArgList * DAL,SmallVectorImpl<llvm::opt::Arg * > * AllocatedArgs) const1287ec727ea7Spatrick void ToolChain::TranslateXarchArgs(
1288ec727ea7Spatrick const llvm::opt::DerivedArgList &Args, llvm::opt::Arg *&A,
1289ec727ea7Spatrick llvm::opt::DerivedArgList *DAL,
1290ec727ea7Spatrick SmallVectorImpl<llvm::opt::Arg *> *AllocatedArgs) const {
1291ec727ea7Spatrick const OptTable &Opts = getDriver().getOpts();
1292ec727ea7Spatrick unsigned ValuePos = 1;
1293ec727ea7Spatrick if (A->getOption().matches(options::OPT_Xarch_device) ||
1294ec727ea7Spatrick A->getOption().matches(options::OPT_Xarch_host))
1295ec727ea7Spatrick ValuePos = 0;
1296ec727ea7Spatrick
1297ec727ea7Spatrick unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(ValuePos));
1298ec727ea7Spatrick unsigned Prev = Index;
1299ec727ea7Spatrick std::unique_ptr<llvm::opt::Arg> XarchArg(Opts.ParseOneArg(Args, Index));
1300ec727ea7Spatrick
1301ec727ea7Spatrick // If the argument parsing failed or more than one argument was
1302ec727ea7Spatrick // consumed, the -Xarch_ argument's parameter tried to consume
1303ec727ea7Spatrick // extra arguments. Emit an error and ignore.
1304ec727ea7Spatrick //
1305ec727ea7Spatrick // We also want to disallow any options which would alter the
1306ec727ea7Spatrick // driver behavior; that isn't going to work in our model. We
1307a9ac8606Spatrick // use options::NoXarchOption to control this.
1308ec727ea7Spatrick if (!XarchArg || Index > Prev + 1) {
1309ec727ea7Spatrick getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
1310ec727ea7Spatrick << A->getAsString(Args);
1311ec727ea7Spatrick return;
1312a9ac8606Spatrick } else if (XarchArg->getOption().hasFlag(options::NoXarchOption)) {
1313a9ac8606Spatrick auto &Diags = getDriver().getDiags();
1314a9ac8606Spatrick unsigned DiagID =
1315a9ac8606Spatrick Diags.getCustomDiagID(DiagnosticsEngine::Error,
1316a9ac8606Spatrick "invalid Xarch argument: '%0', not all driver "
1317a9ac8606Spatrick "options can be forwared via Xarch argument");
1318a9ac8606Spatrick Diags.Report(DiagID) << A->getAsString(Args);
1319ec727ea7Spatrick return;
1320ec727ea7Spatrick }
1321ec727ea7Spatrick XarchArg->setBaseArg(A);
1322ec727ea7Spatrick A = XarchArg.release();
1323ec727ea7Spatrick if (!AllocatedArgs)
1324ec727ea7Spatrick DAL->AddSynthesizedArg(A);
1325ec727ea7Spatrick else
1326ec727ea7Spatrick AllocatedArgs->push_back(A);
1327ec727ea7Spatrick }
1328ec727ea7Spatrick
TranslateXarchArgs(const llvm::opt::DerivedArgList & Args,StringRef BoundArch,Action::OffloadKind OFK,SmallVectorImpl<llvm::opt::Arg * > * AllocatedArgs) const1329ec727ea7Spatrick llvm::opt::DerivedArgList *ToolChain::TranslateXarchArgs(
1330ec727ea7Spatrick const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
1331ec727ea7Spatrick Action::OffloadKind OFK,
1332ec727ea7Spatrick SmallVectorImpl<llvm::opt::Arg *> *AllocatedArgs) const {
1333ec727ea7Spatrick DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
1334ec727ea7Spatrick bool Modified = false;
1335ec727ea7Spatrick
1336*12c85518Srobert bool IsDevice = OFK != Action::OFK_None && OFK != Action::OFK_Host;
1337ec727ea7Spatrick for (Arg *A : Args) {
1338ec727ea7Spatrick bool NeedTrans = false;
1339ec727ea7Spatrick bool Skip = false;
1340ec727ea7Spatrick if (A->getOption().matches(options::OPT_Xarch_device)) {
1341*12c85518Srobert NeedTrans = IsDevice;
1342*12c85518Srobert Skip = !IsDevice;
1343ec727ea7Spatrick } else if (A->getOption().matches(options::OPT_Xarch_host)) {
1344*12c85518Srobert NeedTrans = !IsDevice;
1345*12c85518Srobert Skip = IsDevice;
1346*12c85518Srobert } else if (A->getOption().matches(options::OPT_Xarch__) && IsDevice) {
1347ec727ea7Spatrick // Do not translate -Xarch_ options for non CUDA/HIP toolchain since
1348ec727ea7Spatrick // they may need special translation.
1349ec727ea7Spatrick // Skip this argument unless the architecture matches BoundArch
1350ec727ea7Spatrick if (BoundArch.empty() || A->getValue(0) != BoundArch)
1351ec727ea7Spatrick Skip = true;
1352ec727ea7Spatrick else
1353ec727ea7Spatrick NeedTrans = true;
1354ec727ea7Spatrick }
1355ec727ea7Spatrick if (NeedTrans || Skip)
1356ec727ea7Spatrick Modified = true;
1357ec727ea7Spatrick if (NeedTrans)
1358ec727ea7Spatrick TranslateXarchArgs(Args, A, DAL, AllocatedArgs);
1359ec727ea7Spatrick if (!Skip)
1360ec727ea7Spatrick DAL->append(A);
1361ec727ea7Spatrick }
1362ec727ea7Spatrick
1363ec727ea7Spatrick if (Modified)
1364ec727ea7Spatrick return DAL;
1365ec727ea7Spatrick
1366ec727ea7Spatrick delete DAL;
1367ec727ea7Spatrick return nullptr;
1368ec727ea7Spatrick }
1369