xref: /llvm-project/clang/lib/Driver/ToolChains/PPCLinux.cpp (revision f3dcc2351cff7b26c9870d737e5d431551542d9e)
119828e39SQiu Chaofan //===-- PPCLinux.cpp - PowerPC ToolChain Implementations --------*- C++ -*-===//
219828e39SQiu Chaofan //
319828e39SQiu Chaofan // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
419828e39SQiu Chaofan // See https://llvm.org/LICENSE.txt for license information.
519828e39SQiu Chaofan // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
619828e39SQiu Chaofan //
719828e39SQiu Chaofan //===----------------------------------------------------------------------===//
819828e39SQiu Chaofan 
919828e39SQiu Chaofan #include "PPCLinux.h"
1019828e39SQiu Chaofan #include "clang/Driver/Driver.h"
11c5590396SQiu Chaofan #include "clang/Driver/DriverDiagnostic.h"
1219828e39SQiu Chaofan #include "clang/Driver/Options.h"
13c5590396SQiu Chaofan #include "llvm/Support/FileSystem.h"
1419828e39SQiu Chaofan #include "llvm/Support/Path.h"
1519828e39SQiu Chaofan 
16fd71493fSMike Hommey using namespace clang::driver;
1719828e39SQiu Chaofan using namespace clang::driver::toolchains;
1819828e39SQiu Chaofan using namespace llvm::opt;
19c5590396SQiu Chaofan using namespace llvm::sys;
20c5590396SQiu Chaofan 
21c5590396SQiu Chaofan // Glibc older than 2.32 doesn't fully support IEEE float128. Here we check
22c5590396SQiu Chaofan // glibc version by looking at dynamic linker name.
GlibcSupportsFloat128(const std::string & Linker)23c5590396SQiu Chaofan static bool GlibcSupportsFloat128(const std::string &Linker) {
24c5590396SQiu Chaofan   llvm::SmallVector<char, 16> Path;
25c5590396SQiu Chaofan 
26c5590396SQiu Chaofan   // Resolve potential symlinks to linker.
27c5590396SQiu Chaofan   if (fs::real_path(Linker, Path))
28c5590396SQiu Chaofan     return false;
29c5590396SQiu Chaofan   llvm::StringRef LinkerName =
30c5590396SQiu Chaofan       path::filename(llvm::StringRef(Path.data(), Path.size()));
31c5590396SQiu Chaofan 
32c5590396SQiu Chaofan   // Since glibc 2.34, the installed .so file is not symlink anymore. But we can
33c5590396SQiu Chaofan   // still safely assume it's newer than 2.32.
34*f3dcc235SKazu Hirata   if (LinkerName.starts_with("ld64.so"))
35c5590396SQiu Chaofan     return true;
36c5590396SQiu Chaofan 
37*f3dcc235SKazu Hirata   if (!LinkerName.starts_with("ld-2."))
38c5590396SQiu Chaofan     return false;
39c5590396SQiu Chaofan   unsigned Minor = (LinkerName[5] - '0') * 10 + (LinkerName[6] - '0');
40c5590396SQiu Chaofan   if (Minor < 32)
41c5590396SQiu Chaofan     return false;
42c5590396SQiu Chaofan 
43c5590396SQiu Chaofan   return true;
44c5590396SQiu Chaofan }
45c5590396SQiu Chaofan 
PPCLinuxToolChain(const Driver & D,const llvm::Triple & Triple,const llvm::opt::ArgList & Args)46c5590396SQiu Chaofan PPCLinuxToolChain::PPCLinuxToolChain(const Driver &D,
47c5590396SQiu Chaofan                                      const llvm::Triple &Triple,
48c5590396SQiu Chaofan                                      const llvm::opt::ArgList &Args)
49c5590396SQiu Chaofan     : Linux(D, Triple, Args) {
50c5590396SQiu Chaofan   if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
51c5590396SQiu Chaofan     StringRef ABIName = A->getValue();
525f68c411STulio Magno Quites Machado Filho 
535f68c411STulio Magno Quites Machado Filho     if ((ABIName == "ieeelongdouble" &&
545f68c411STulio Magno Quites Machado Filho          !SupportIEEEFloat128(D, Triple, Args)) ||
555f68c411STulio Magno Quites Machado Filho         (ABIName == "ibmlongdouble" && !supportIBMLongDouble(D, Args)))
56c5590396SQiu Chaofan       D.Diag(diag::warn_drv_unsupported_float_abi_by_lib) << ABIName;
57c5590396SQiu Chaofan   }
58c5590396SQiu Chaofan }
5919828e39SQiu Chaofan 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const6019828e39SQiu Chaofan void PPCLinuxToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
6119828e39SQiu Chaofan                                                   ArgStringList &CC1Args) const {
62852d4446SQiu Chaofan   if (!DriverArgs.hasArg(clang::driver::options::OPT_nostdinc) &&
6319828e39SQiu Chaofan       !DriverArgs.hasArg(options::OPT_nobuiltininc)) {
6419828e39SQiu Chaofan     const Driver &D = getDriver();
6519828e39SQiu Chaofan     SmallString<128> P(D.ResourceDir);
6619828e39SQiu Chaofan     llvm::sys::path::append(P, "include", "ppc_wrappers");
6719828e39SQiu Chaofan     addSystemInclude(DriverArgs, CC1Args, P);
6819828e39SQiu Chaofan   }
6919828e39SQiu Chaofan 
7019828e39SQiu Chaofan   Linux::AddClangSystemIncludeArgs(DriverArgs, CC1Args);
7119828e39SQiu Chaofan }
72c5590396SQiu Chaofan 
supportIBMLongDouble(const Driver & D,const llvm::opt::ArgList & Args) const735f68c411STulio Magno Quites Machado Filho bool PPCLinuxToolChain::supportIBMLongDouble(
745f68c411STulio Magno Quites Machado Filho     const Driver &D, const llvm::opt::ArgList &Args) const {
755f68c411STulio Magno Quites Machado Filho   if (Args.hasArg(options::OPT_nostdlib, options::OPT_nostdlibxx))
765f68c411STulio Magno Quites Machado Filho     return true;
775f68c411STulio Magno Quites Machado Filho 
785f68c411STulio Magno Quites Machado Filho   CXXStdlibType StdLib = ToolChain::GetCXXStdlibType(Args);
795f68c411STulio Magno Quites Machado Filho   if (StdLib == CST_Libstdcxx)
805f68c411STulio Magno Quites Machado Filho     return true;
815f68c411STulio Magno Quites Machado Filho 
825f68c411STulio Magno Quites Machado Filho   return StdLib == CST_Libcxx && !defaultToIEEELongDouble();
835f68c411STulio Magno Quites Machado Filho }
845f68c411STulio Magno Quites Machado Filho 
SupportIEEEFloat128(const Driver & D,const llvm::Triple & Triple,const llvm::opt::ArgList & Args) const85c5590396SQiu Chaofan bool PPCLinuxToolChain::SupportIEEEFloat128(
86c5590396SQiu Chaofan     const Driver &D, const llvm::Triple &Triple,
87c5590396SQiu Chaofan     const llvm::opt::ArgList &Args) const {
88c5590396SQiu Chaofan   if (!Triple.isLittleEndian() || !Triple.isPPC64())
89c5590396SQiu Chaofan     return false;
90c5590396SQiu Chaofan 
91c5590396SQiu Chaofan   if (Args.hasArg(options::OPT_nostdlib, options::OPT_nostdlibxx))
92c5590396SQiu Chaofan     return true;
93c5590396SQiu Chaofan 
945b7941adSTimm Bäder   CXXStdlibType StdLib = ToolChain::GetCXXStdlibType(Args);
95c5590396SQiu Chaofan   bool HasUnsupportedCXXLib =
965f68c411STulio Magno Quites Machado Filho       (StdLib == CST_Libcxx && !defaultToIEEELongDouble()) ||
975b7941adSTimm Bäder       (StdLib == CST_Libstdcxx &&
985b7941adSTimm Bäder        GCCInstallation.getVersion().isOlderThan(12, 1, 0));
99c5590396SQiu Chaofan 
1000cbf003cSNikita Popov   std::string Linker = Linux::getDynamicLinker(Args);
1010cbf003cSNikita Popov   return GlibcSupportsFloat128((Twine(D.DyldPrefix) + Linker).str()) &&
102c5590396SQiu Chaofan          !(D.CCCIsCXX() && HasUnsupportedCXXLib);
103c5590396SQiu Chaofan }
104