16#include "clang/Config/config.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/ProfileData/InstrProf.h"
23#include "llvm/Support/Path.h"
24#include "llvm/Support/ScopedPrinter.h"
25#include "llvm/Support/VirtualFileSystem.h"
26#include <system_error>
42 const llvm::Triple &TargetTriple,
43 StringRef SysRoot)
const {
44 llvm::Triple::EnvironmentType TargetEnvironment =
45 TargetTriple.getEnvironment();
46 bool IsAndroid = TargetTriple.isAndroid();
47 bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6;
48 bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32;
52 switch (TargetTriple.getArch()) {
60 case llvm::Triple::arm:
61 case llvm::Triple::thumb:
63 return "arm-linux-androideabi";
64 if (TargetEnvironment == llvm::Triple::GNUEABIHF ||
65 TargetEnvironment == llvm::Triple::MuslEABIHF ||
66 TargetEnvironment == llvm::Triple::EABIHF)
67 return "arm-linux-gnueabihf";
68 return "arm-linux-gnueabi";
69 case llvm::Triple::armeb:
70 case llvm::Triple::thumbeb:
71 if (TargetEnvironment == llvm::Triple::GNUEABIHF ||
72 TargetEnvironment == llvm::Triple::MuslEABIHF ||
73 TargetEnvironment == llvm::Triple::EABIHF)
74 return "armeb-linux-gnueabihf";
75 return "armeb-linux-gnueabi";
76 case llvm::Triple::x86:
78 return "i686-linux-android";
79 return "i386-linux-gnu";
80 case llvm::Triple::x86_64:
82 return "x86_64-linux-android";
83 if (TargetEnvironment == llvm::Triple::GNUX32)
84 return "x86_64-linux-gnux32";
85 return "x86_64-linux-gnu";
86 case llvm::Triple::aarch64:
88 return "aarch64-linux-android";
91 return "aarch64-linux-pauthtest";
92 return "aarch64-linux-gnu";
93 case llvm::Triple::aarch64_be:
94 return "aarch64_be-linux-gnu";
96 case llvm::Triple::loongarch64: {
100 if (TargetTriple.isGNUEnvironment()) {
102 }
else if (TargetTriple.isMusl()) {
105 return TargetTriple.str();
108 switch (TargetEnvironment) {
110 return TargetTriple.str();
111 case llvm::Triple::GNUSF:
114 case llvm::Triple::GNUF32:
117 case llvm::Triple::GNU:
118 case llvm::Triple::GNUF64:
126 return (Twine(
"loongarch64-linux-") + Libc + FPFlavor).str();
129 case llvm::Triple::m68k:
130 return "m68k-linux-gnu";
132 case llvm::Triple::mips:
133 return IsMipsR6 ?
"mipsisa32r6-linux-gnu" :
"mips-linux-gnu";
134 case llvm::Triple::mipsel:
135 return IsMipsR6 ?
"mipsisa32r6el-linux-gnu" :
"mipsel-linux-gnu";
136 case llvm::Triple::mips64: {
137 std::string MT = std::string(IsMipsR6 ?
"mipsisa64r6" :
"mips64") +
138 "-linux-" + (IsMipsN32Abi ?
"gnuabin32" :
"gnuabi64");
139 if (
D.getVFS().exists(
concat(SysRoot,
"/lib", MT)))
141 if (
D.getVFS().exists(
concat(SysRoot,
"/lib/mips64-linux-gnu")))
142 return "mips64-linux-gnu";
145 case llvm::Triple::mips64el: {
146 std::string MT = std::string(IsMipsR6 ?
"mipsisa64r6el" :
"mips64el") +
147 "-linux-" + (IsMipsN32Abi ?
"gnuabin32" :
"gnuabi64");
148 if (
D.getVFS().exists(
concat(SysRoot,
"/lib", MT)))
150 if (
D.getVFS().exists(
concat(SysRoot,
"/lib/mips64el-linux-gnu")))
151 return "mips64el-linux-gnu";
154 case llvm::Triple::ppc:
155 if (
D.getVFS().exists(
concat(SysRoot,
"/lib/powerpc-linux-gnuspe")))
156 return "powerpc-linux-gnuspe";
157 return "powerpc-linux-gnu";
158 case llvm::Triple::ppcle:
159 return "powerpcle-linux-gnu";
160 case llvm::Triple::ppc64:
161 return "powerpc64-linux-gnu";
162 case llvm::Triple::ppc64le:
163 return "powerpc64le-linux-gnu";
164 case llvm::Triple::riscv64:
166 return "riscv64-linux-android";
167 return "riscv64-linux-gnu";
168 case llvm::Triple::sparc:
169 return "sparc-linux-gnu";
170 case llvm::Triple::sparcv9:
171 return "sparc64-linux-gnu";
172 case llvm::Triple::systemz:
173 return "s390x-linux-gnu";
175 return TargetTriple.str();
178static StringRef
getOSLibDir(
const llvm::Triple &Triple,
const ArgList &Args) {
179 if (Triple.isMIPS()) {
180 if (Triple.isAndroid()) {
184 if (CPUName ==
"mips32r6")
186 if (CPUName ==
"mips32r2")
194 return Triple.isArch32Bit() ?
"lib" :
"lib64";
206 if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() ||
207 Triple.getArch() == llvm::Triple::sparc)
210 if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32())
213 if (Triple.getArch() == llvm::Triple::riscv32)
216 return Triple.isArch32Bit() ?
"lib" :
"lib64";
224 llvm::Triple::ArchType Arch = Triple.getArch();
238 Triple.isAndroid()) {
245 if (Triple.isAndroid()) {
246 if (Triple.isARM()) {
249 ExtraOpts.push_back(
"max-page-size=4096");
250 }
else if (Triple.isAArch64() || Triple.getArch() == llvm::Triple::x86_64) {
254 ExtraOpts.push_back(
"max-page-size=16384");
266 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
269 const bool IsAndroid = Triple.isAndroid();
270 const bool IsMips = Triple.isMIPS();
271 const bool IsHexagon = Arch == llvm::Triple::hexagon;
272 const bool IsRISCV = Triple.isRISCV();
273 const bool IsCSKY = Triple.isCSKY();
278 if ((IsMips || IsCSKY) && !SysRoot.empty())
279 ExtraOpts.push_back(
"--sysroot=" + SysRoot);
287 if (!IsMips && !IsHexagon) {
290 (IsAndroid && Triple.isAndroidVersionLT(23)))
291 ExtraOpts.push_back(
"--hash-style=both");
296#ifdef ENABLE_LINKER_BUILD_ID
307 const std::string OSLibDir = std::string(
getOSLibDir(Triple, Args));
312 if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) {
314 addPathIfExists(
D,
concat(SysRoot,
"/libo32"), Paths);
315 addPathIfExists(
D,
concat(SysRoot,
"/usr/libo32"), Paths);
319 addPathIfExists(
D,
concat(SysRoot,
"/lib", MultiarchTriple), Paths);
320 addPathIfExists(
D,
concat(SysRoot,
"/lib/..", OSLibDir), Paths);
328 concat(SysRoot,
"/usr/lib", MultiarchTriple,
329 llvm::to_string(Triple.getEnvironmentVersion().getMajor())),
333 addPathIfExists(
D,
concat(SysRoot,
"/usr/lib", MultiarchTriple), Paths);
337 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
338 Triple.isArch64Bit())
339 addPathIfExists(
D,
concat(SysRoot,
"/usr", OSLibDir), Paths);
341 addPathIfExists(
D,
concat(SysRoot,
"/usr/lib/..", OSLibDir), Paths);
344 addPathIfExists(
D,
concat(SysRoot,
"/", OSLibDir, ABIName), Paths);
345 addPathIfExists(
D,
concat(SysRoot,
"/usr", OSLibDir, ABIName), Paths);
350 addPathIfExists(
D,
concat(SysRoot,
"/lib"), Paths);
351 addPathIfExists(
D,
concat(SysRoot,
"/usr/lib"), Paths);
392 std::string AndroidSysRootPath = (ClangDir +
"/../sysroot").str();
393 if (
getVFS().exists(AndroidSysRootPath))
394 return AndroidSysRootPath;
400 return std::string();
409 return std::string();
413 return std::string();
435 return std::string();
439 const llvm::Triple::ArchType Arch =
getArch();
440 const llvm::Triple &Triple =
getTriple();
444 if (Triple.isAndroid()) {
446 !Triple.isAndroidVersionLT(34) && Triple.isArch64Bit()) {
451 return "/system/bin/linker_hwasan64";
453 return Triple.isArch64Bit() ?
"/system/bin/linker64" :
"/system/bin/linker";
455 if (Triple.isMusl()) {
456 std::string ArchName;
460 case llvm::Triple::arm:
461 case llvm::Triple::thumb:
465 case llvm::Triple::armeb:
466 case llvm::Triple::thumbeb:
470 case llvm::Triple::x86:
473 case llvm::Triple::x86_64:
474 ArchName = Triple.isX32() ?
"x32" : Triple.getArchName().str();
477 ArchName = Triple.getArchName().str();
480 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
483 if (Arch == llvm::Triple::ppc &&
484 Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
485 ArchName =
"powerpc-sf";
487 return "/lib/ld-musl-" + ArchName +
".so.1";
495 llvm_unreachable(
"unsupported architecture");
497 case llvm::Triple::aarch64:
499 Loader =
"ld-linux-aarch64.so.1";
501 case llvm::Triple::aarch64_be:
503 Loader =
"ld-linux-aarch64_be.so.1";
505 case llvm::Triple::arm:
506 case llvm::Triple::thumb:
507 case llvm::Triple::armeb:
508 case llvm::Triple::thumbeb: {
510 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
514 Loader = HF ?
"ld-linux-armhf.so.3" :
"ld-linux.so.3";
517 case llvm::Triple::loongarch32: {
520 (
"ld-linux-loongarch-" +
525 case llvm::Triple::loongarch64: {
528 (
"ld-linux-loongarch-" +
533 case llvm::Triple::m68k:
537 case llvm::Triple::mips:
538 case llvm::Triple::mipsel:
539 case llvm::Triple::mips64:
540 case llvm::Triple::mips64el: {
546 Loader = IsNaN2008 ?
"ld-uClibc-mipsn8.so.0" :
"ld-uClibc.so.0";
547 else if (!Triple.hasEnvironment() &&
548 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
550 Triple.isLittleEndian() ?
"ld-musl-mipsel.so.1" :
"ld-musl-mips.so.1";
552 Loader = IsNaN2008 ?
"ld-linux-mipsn8.so.1" :
"ld.so.1";
556 case llvm::Triple::ppc:
560 case llvm::Triple::ppcle:
564 case llvm::Triple::ppc64:
569 case llvm::Triple::ppc64le:
574 case llvm::Triple::riscv32:
575 case llvm::Triple::riscv64: {
576 StringRef ArchName = llvm::Triple::getArchTypeName(Arch);
579 Loader = (
"ld-linux-" + ArchName +
"-" + ABIName +
".so.1").str();
582 case llvm::Triple::sparc:
583 case llvm::Triple::sparcel:
585 Loader =
"ld-linux.so.2";
587 case llvm::Triple::sparcv9:
589 Loader =
"ld-linux.so.2";
591 case llvm::Triple::systemz:
593 Loader =
"ld64.so.1";
595 case llvm::Triple::x86:
597 Loader =
"ld-linux.so.2";
599 case llvm::Triple::x86_64: {
600 bool X32 = Triple.isX32();
602 LibDir = X32 ?
"libx32" :
"lib64";
603 Loader = X32 ?
"ld-linux-x32.so.2" :
"ld-linux-x86-64.so.2";
606 case llvm::Triple::ve:
607 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
608 case llvm::Triple::csky: {
616 (Triple.getVendor() == llvm::Triple::UnknownVendor ||
617 Triple.getVendor() == llvm::Triple::PC))
618 return "/usr/" + Triple.str() +
"/lib/" + Loader;
619 return "/" + LibDir +
"/" + Loader;
623 ArgStringList &CC1Args)
const {
627 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
635 llvm::sys::path::append(ResourceDirInclude,
"include");
636 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
637 (!
getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
640 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
649 StringRef CIncludeDirs(C_INCLUDE_DIRS);
650 if (CIncludeDirs !=
"") {
652 CIncludeDirs.split(dirs,
":");
653 for (StringRef dir : dirs) {
655 llvm::sys::path::is_absolute(dir) ?
"" : StringRef(SysRoot);
664 if (!MultiarchIncludeDir.empty() &&
665 D.getVFS().exists(
concat(SysRoot,
"/usr/include", MultiarchIncludeDir)))
668 concat(SysRoot,
"/usr/include", MultiarchIncludeDir));
680 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
getTriple().isMusl())
685 llvm::opt::ArgStringList &CC1Args)
const {
693 StringRef DebianMultiarch =
707 const std::string LibStdCXXIncludePathCandidates[] = {
709 LibDir.str() +
"/../" + TripleStr.str() +
"/include/c++/" + Version.
Text,
712 LibDir.str() +
"/../include/c++",
715 LibDir.str() +
"/../include/g++",
718 for (
const auto &IncludePath : LibStdCXXIncludePathCandidates) {
726 ArgStringList &CC1Args)
const {
731 ArgStringList &CC1Args)
const {
736 ArgStringList &CmdArgs)
const {
740 if (Args.hasFlag(options::OPT_frtlib_add_rpath,
741 options::OPT_fno_rtlib_add_rpath,
false))
745 CmdArgs.push_back(
"-lamdhip64");
749 ArgStringList &CC1Args)
const {
751 CC1Args.push_back(
"-isystem");
752 CC1Args.push_back(DriverArgs.MakeArgString(
759 return CLANG_DEFAULT_PIE_ON_LINUX ||
getTriple().isAndroid() ||
766 assert(
getTriple().isAArch64() &&
"expected AArch64 target!");
783 const bool IsX86 =
getTriple().getArch() == llvm::Triple::x86;
784 const bool IsX86_64 =
getTriple().getArch() == llvm::Triple::x86_64;
785 const bool IsMIPS =
getTriple().isMIPS32();
786 const bool IsMIPS64 =
getTriple().isMIPS64();
787 const bool IsPowerPC64 =
getTriple().getArch() == llvm::Triple::ppc64 ||
788 getTriple().getArch() == llvm::Triple::ppc64le;
789 const bool IsAArch64 =
getTriple().getArch() == llvm::Triple::aarch64 ||
790 getTriple().getArch() == llvm::Triple::aarch64_be;
791 const bool IsArmArch =
getTriple().getArch() == llvm::Triple::arm ||
792 getTriple().getArch() == llvm::Triple::thumb ||
793 getTriple().getArch() == llvm::Triple::armeb ||
794 getTriple().getArch() == llvm::Triple::thumbeb;
795 const bool IsLoongArch64 =
getTriple().getArch() == llvm::Triple::loongarch64;
796 const bool IsRISCV64 =
getTriple().getArch() == llvm::Triple::riscv64;
797 const bool IsSystemZ =
getTriple().getArch() == llvm::Triple::systemz;
798 const bool IsHexagon =
getTriple().getArch() == llvm::Triple::hexagon;
800 Res |= SanitizerKind::Address;
801 Res |= SanitizerKind::PointerCompare;
802 Res |= SanitizerKind::PointerSubtract;
803 Res |= SanitizerKind::Fuzzer;
804 Res |= SanitizerKind::FuzzerNoLink;
805 Res |= SanitizerKind::KernelAddress;
806 Res |= SanitizerKind::Memory;
807 Res |= SanitizerKind::Vptr;
808 Res |= SanitizerKind::SafeStack;
809 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64)
810 Res |= SanitizerKind::DataFlow;
811 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 ||
812 IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64)
813 Res |= SanitizerKind::Leak;
814 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ ||
815 IsLoongArch64 || IsRISCV64)
816 Res |= SanitizerKind::Thread;
817 if (IsX86_64 || IsSystemZ || IsPowerPC64)
818 Res |= SanitizerKind::KernelMemory;
819 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
820 IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64)
821 Res |= SanitizerKind::Scudo;
822 if (IsX86_64 || IsAArch64 || IsRISCV64) {
823 Res |= SanitizerKind::HWAddress;
825 if (IsX86_64 || IsAArch64) {
826 Res |= SanitizerKind::KernelHWAddress;
829 Res |= SanitizerKind::NumericalStability;
838 llvm::opt::ArgStringList &CmdArgs)
const {
842 CmdArgs.push_back(Args.MakeArgString(
843 Twine(
"-u", llvm::getInstrProfRuntimeHookVarName())));
849 CmdArgs.push_back(Opt.c_str());
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args)
Distro - Helper class for detecting and classifying Linux distributions.
bool IsAlpineLinux() const
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
std::string SysRoot
sysroot, if present
std::string Dir
The path the driver executable was in, as invoked from the command line.
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
const std::string & includeSuffix() const
Get the include directory suffix.
The JSON file list parser is used to communicate input to InstallAPI.