22#include "llvm/ADT/StringExtras.h"
23#include "llvm/Option/ArgList.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/VirtualFileSystem.h"
26#include "llvm/Support/raw_ostream.h"
37 const llvm::Triple &TargetTriple,
43 if (TargetTriple.isRISCV64()) {
47 .
flag(
"-march=rv64imafdc")
52 (Arch ==
"rv64imafdc") || (Arch ==
"rv64gc");
61 return Result.Multilibs.select(Flags, Result.SelectedMultilibs);
63 if (TargetTriple.isRISCV32()) {
70 .
flag(
"-march=rv32im")
73 .
flag(
"-march=rv32iac")
76 .
flag(
"-march=rv32imafc")
77 .
flag(
"-mabi=ilp32f");
80 bool UseI = (Arch ==
"rv32i") || (Arch ==
"rv32ic");
81 bool UseIm = (Arch ==
"rv32im") || (Arch ==
"rv32imc");
82 bool UseImafc = (Arch ==
"rv32imafc") || (Arch ==
"rv32imafdc") ||
95 return Result.Multilibs.select(Flags, Result.SelectedMultilibs);
107 if (!SysRoot.empty()) {
108 for (
const Multilib &M : getOrderedMultilibs()) {
110 llvm::sys::path::append(Dir, M.osSuffix(),
"lib");
119 if (Triple.getArch() != llvm::Triple::arm &&
120 Triple.getArch() != llvm::Triple::thumb &&
121 Triple.getArch() != llvm::Triple::armeb &&
122 Triple.getArch() != llvm::Triple::thumbeb)
125 if (Triple.getVendor() != llvm::Triple::UnknownVendor)
128 if (Triple.getOS() != llvm::Triple::UnknownOS)
131 if (Triple.getEnvironment() != llvm::Triple::EABI &&
132 Triple.getEnvironment() != llvm::Triple::EABIHF)
140 if (Triple.getArch() != llvm::Triple::aarch64 &&
141 Triple.getArch() != llvm::Triple::aarch64_be)
144 if (Triple.getVendor() != llvm::Triple::UnknownVendor)
147 if (Triple.getOS() != llvm::Triple::UnknownOS)
150 return Triple.getEnvironmentName() ==
"elf";
154 if (!Triple.isRISCV())
157 if (Triple.getVendor() != llvm::Triple::UnknownVendor)
160 if (Triple.getOS() != llvm::Triple::UnknownOS)
163 return Triple.getEnvironmentName() ==
"elf";
168 return Triple.isPPC() && Triple.getOS() == llvm::Triple::UnknownOS &&
169 Triple.getEnvironment() == llvm::Triple::EABI;
173 StringRef MultilibPath,
const ArgList &Args,
175 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB =
176 D.getVFS().getBufferForFile(MultilibPath);
180 llvm::ErrorOr<MultilibSet> ErrorOrMultilibSet =
182 if (ErrorOrMultilibSet.getError())
184 Result.Multilibs = ErrorOrMultilibSet.get();
185 if (
Result.Multilibs.select(Flags,
Result.SelectedMultilibs))
187 D.Diag(clang::diag::warn_drv_missing_multilib) << llvm::join(Flags,
" ");
188 std::stringstream ss;
191 D.Diag(clang::diag::note_drv_available_multilibs) << ss.str();
198 const llvm::Triple &Triple) {
199 if (!
D.SysRoot.empty())
203 llvm::sys::path::append(SysRootDir,
"..",
"lib",
"clang-runtimes");
210 if (
D.getVFS().exists(MultilibPath))
211 return std::string(SysRootDir);
214 llvm::sys::path::append(SysRootDir,
D.getTargetTriple());
215 return std::string(SysRootDir);
219 const ArgList &Args) {
225 if (
D.getVFS().exists(MultilibPath)) {
254BareMetal::OrderedMultilibs BareMetal::getOrderedMultilibs()
const {
265 ArgStringList &CC1Args)
const {
266 if (DriverArgs.hasArg(options::OPT_nostdinc))
269 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
271 llvm::sys::path::append(Dir,
"include");
275 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
282 if (!SysRoot.empty()) {
283 for (
const Multilib &M : getOrderedMultilibs()) {
285 llvm::sys::path::append(Dir, M.includeSuffix());
286 llvm::sys::path::append(Dir,
"include");
293 ArgStringList &CC1Args,
295 CC1Args.push_back(
"-nostdsysteminc");
299 ArgStringList &CC1Args)
const {
300 if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
301 options::OPT_nostdincxx))
307 auto AddCXXIncludePath = [&](StringRef
Path) {
315 llvm::sys::path::append(TargetDir,
Target,
"c++", Version);
322 llvm::sys::path::append(Dir,
"c++", Version);
330 llvm::sys::path::append(
P,
"..",
"include");
331 AddCXXIncludePath(
P);
343 for (
const Multilib &M : getOrderedMultilibs()) {
345 llvm::sys::path::append(Dir, M.gccSuffix());
350 llvm::sys::path::append(TargetDir,
"usr",
"include",
"c++",
"v1");
351 if (
D.getVFS().exists(TargetDir)) {
356 llvm::sys::path::append(Dir,
"include",
"c++",
"v1");
361 llvm::sys::path::append(Dir,
"include",
"c++");
365 for (llvm::vfs::directory_iterator
366 LI =
D.getVFS().dir_begin(Dir.str(), EC),
368 !EC && LI != LE; LI = LI.increment(EC)) {
369 StringRef VersionText = llvm::sys::path::filename(LI->path());
371 if (CandidateVersion.Major == -1)
373 if (CandidateVersion <= Version)
375 Version = CandidateVersion;
377 if (Version.
Major != -1) {
378 llvm::sys::path::append(Dir, Version.
Text);
388 ArgStringList &CmdArgs)
const {
391 CmdArgs.push_back(
"-lc++");
392 if (Args.hasArg(options::OPT_fexperimental_library))
393 CmdArgs.push_back(
"-lc++experimental");
394 CmdArgs.push_back(
"-lc++abi");
397 CmdArgs.push_back(
"-lstdc++");
398 CmdArgs.push_back(
"-lsupc++");
401 CmdArgs.push_back(
"-lunwind");
405 ArgStringList &CmdArgs)
const {
413 CmdArgs.push_back(
"-lgcc");
416 llvm_unreachable(
"Unhandled RuntimeLibType.");
423 const char *LinkingOutput)
const {
424 const Driver &
D = getToolChain().getDriver();
427 Args.ClaimAllArgs(options::OPT_g_Group);
429 Args.ClaimAllArgs(options::OPT_emit_llvm);
432 Args.ClaimAllArgs(options::OPT_w);
434 Args.ClaimAllArgs(options::OPT_stdlib_EQ);
437 ArgStringList CmdArgs;
439 CmdArgs.push_back(
"rcsD");
442 for (
const auto &II : Inputs) {
443 if (II.isFilename()) {
444 CmdArgs.push_back(II.getFilename());
451 if (Output.
isFilename() && llvm::sys::fs::exists(OutputFileName)) {
452 if (std::error_code EC = llvm::sys::fs::remove(OutputFileName)) {
453 D.Diag(diag::err_drv_unable_to_remove_file) << EC.message();
458 const char *Exec = Args.MakeArgString(getToolChain().GetStaticLibToolPath());
459 C.addCommand(std::make_unique<Command>(JA, *
this,
461 Exec, CmdArgs, Inputs, Output));
468 const char *LinkingOutput)
const {
469 ArgStringList CmdArgs;
472 const Driver &
D = getToolChain().getDriver();
473 const llvm::Triple::ArchType Arch = TC.getArch();
474 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
478 CmdArgs.push_back(
"-Bstatic");
480 if (TC.getTriple().isRISCV() && Args.hasArg(options::OPT_mno_relax))
481 CmdArgs.push_back(
"--no-relax");
483 if (Triple.isARM() || Triple.isThumb()) {
487 CmdArgs.push_back(IsBigEndian ?
"-EB" :
"-EL");
488 }
else if (Triple.isAArch64()) {
489 CmdArgs.push_back(Arch == llvm::Triple::aarch64_be ?
"-EB" :
"-EL");
492 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
494 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(
"crt0.o")));
497 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
498 options::OPT_s, options::OPT_t, options::OPT_r});
500 TC.AddFilePathLibArgs(Args, CmdArgs);
502 for (
const auto &LibPath : TC.getLibraryPaths())
503 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(
"-L", LibPath)));
505 if (TC.ShouldLinkCXXStdlib(Args))
506 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
508 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
509 CmdArgs.push_back(
"-lc");
510 CmdArgs.push_back(
"-lm");
512 TC.AddLinkRuntimeLib(Args, CmdArgs);
515 if (
D.isUsingLTO()) {
516 assert(!Inputs.empty() &&
"Must have at least one input.");
518 auto Input = llvm::find_if(
520 if (Input == Inputs.end())
523 Input = Inputs.begin();
528 if (TC.getTriple().isRISCV())
529 CmdArgs.push_back(
"-X");
535 CmdArgs.push_back(
"--target2=rel");
537 CmdArgs.push_back(
"-o");
540 C.addCommand(std::make_unique<Command>(
542 Args.MakeArgString(TC.GetLinkerPath()), CmdArgs, Inputs, Output));
549 const bool IsX86_64 =
getTriple().getArch() == llvm::Triple::x86_64;
550 const bool IsAArch64 =
getTriple().getArch() == llvm::Triple::aarch64 ||
551 getTriple().getArch() == llvm::Triple::aarch64_be;
552 const bool IsRISCV64 =
getTriple().getArch() == llvm::Triple::riscv64;
554 Res |= SanitizerKind::Address;
555 Res |= SanitizerKind::KernelAddress;
556 Res |= SanitizerKind::PointerCompare;
557 Res |= SanitizerKind::PointerSubtract;
558 Res |= SanitizerKind::Fuzzer;
559 Res |= SanitizerKind::FuzzerNoLink;
560 Res |= SanitizerKind::Vptr;
561 Res |= SanitizerKind::SafeStack;
562 Res |= SanitizerKind::Thread;
563 Res |= SanitizerKind::Scudo;
564 if (IsX86_64 || IsAArch64 || IsRISCV64) {
565 Res |= SanitizerKind::HWAddress;
566 Res |= SanitizerKind::KernelHWAddress;
llvm::MachO::Target Target
Compilation - A set of tasks to perform for a single driver invocation.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
This corresponds to a single GCC multilib, or a segment of one controlled by a command line flag.
MultilibBuilder & flag(StringRef Flag, bool Disallow=false)
Add a flag to the flags list Flag must be a flag accepted by the driver.
This class can be used to create a MultilibSet, and contains helper functions to add combinations of ...
MultilibSetBuilder & Either(const MultilibBuilder &M1, const MultilibBuilder &M2)
Add a set of mutually incompatible Multilib segments.
MultilibSet makeMultilibSet() const
static llvm::ErrorOr< MultilibSet > parseYaml(llvm::MemoryBufferRef, llvm::SourceMgr::DiagHandlerTy=nullptr, void *DiagHandlerCtxt=nullptr)
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
const flags_list & flags() const
Get the flags that indicate or contraindicate this multilib's use All elements begin with either '-' ...
std::vector< std::string > flags_list
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
static constexpr ResponseFileSupport AtFileCurCP()