clang  15.0.0git
Gnu.cpp
Go to the documentation of this file.
1 //===--- Gnu.cpp - Gnu Tool and ToolChain Implementations -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Gnu.h"
10 #include "Arch/ARM.h"
11 #include "Arch/CSKY.h"
12 #include "Arch/Mips.h"
13 #include "Arch/PPC.h"
14 #include "Arch/RISCV.h"
15 #include "Arch/Sparc.h"
16 #include "Arch/SystemZ.h"
17 #include "CommonArgs.h"
18 #include "Linux.h"
19 #include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
21 #include "clang/Driver/Driver.h"
23 #include "clang/Driver/Options.h"
24 #include "clang/Driver/Tool.h"
25 #include "clang/Driver/ToolChain.h"
26 #include "llvm/Option/ArgList.h"
27 #include "llvm/Support/CodeGen.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/TargetParser.h"
30 #include "llvm/Support/VirtualFileSystem.h"
31 #include <system_error>
32 
33 using namespace clang::driver;
34 using namespace clang::driver::toolchains;
35 using namespace clang;
36 using namespace llvm::opt;
37 
40 
41 static bool forwardToGCC(const Option &O) {
42  // LinkerInput options have been forwarded. Don't duplicate.
43  if (O.hasFlag(options::LinkerInput))
44  return false;
45  return O.matches(options::OPT_Link_Group) || O.hasFlag(options::LinkOption);
46 }
47 
48 // Switch CPU names not recognized by GNU assembler to a close CPU that it does
49 // recognize, instead of a lower march from being picked in the absence of a cpu
50 // flag.
51 static void normalizeCPUNamesForAssembler(const ArgList &Args,
52  ArgStringList &CmdArgs) {
53  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
54  StringRef CPUArg(A->getValue());
55  if (CPUArg.equals_insensitive("krait"))
56  CmdArgs.push_back("-mcpu=cortex-a15");
57  else if (CPUArg.equals_insensitive("kryo"))
58  CmdArgs.push_back("-mcpu=cortex-a57");
59  else
60  Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
61  }
62 }
63 
65  const InputInfo &Output,
66  const InputInfoList &Inputs,
67  const ArgList &Args,
68  const char *LinkingOutput) const {
69  const Driver &D = getToolChain().getDriver();
70  ArgStringList CmdArgs;
71 
72  for (const auto &A : Args) {
73  if (forwardToGCC(A->getOption())) {
74  // It is unfortunate that we have to claim here, as this means
75  // we will basically never report anything interesting for
76  // platforms using a generic gcc, even if we are just using gcc
77  // to get to the assembler.
78  A->claim();
79 
80  A->render(Args, CmdArgs);
81  }
82  }
83 
84  RenderExtraToolArgs(JA, CmdArgs);
85 
86  // If using a driver driver, force the arch.
87  if (getToolChain().getTriple().isOSDarwin()) {
88  CmdArgs.push_back("-arch");
89  CmdArgs.push_back(
90  Args.MakeArgString(getToolChain().getDefaultUniversalArchName()));
91  }
92 
93  // Try to force gcc to match the tool chain we want, if we recognize
94  // the arch.
95  //
96  // FIXME: The triple class should directly provide the information we want
97  // here.
98  switch (getToolChain().getArch()) {
99  default:
100  break;
101  case llvm::Triple::x86:
102  case llvm::Triple::ppc:
103  case llvm::Triple::ppcle:
104  CmdArgs.push_back("-m32");
105  break;
106  case llvm::Triple::x86_64:
107  case llvm::Triple::ppc64:
108  case llvm::Triple::ppc64le:
109  CmdArgs.push_back("-m64");
110  break;
111  case llvm::Triple::sparcel:
112  CmdArgs.push_back("-EL");
113  break;
114  }
115 
116  if (Output.isFilename()) {
117  CmdArgs.push_back("-o");
118  CmdArgs.push_back(Output.getFilename());
119  } else {
120  assert(Output.isNothing() && "Unexpected output");
121  CmdArgs.push_back("-fsyntax-only");
122  }
123 
124  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
125 
126  // Only pass -x if gcc will understand it; otherwise hope gcc
127  // understands the suffix correctly. The main use case this would go
128  // wrong in is for linker inputs if they happened to have an odd
129  // suffix; really the only way to get this to happen is a command
130  // like '-x foobar a.c' which will treat a.c like a linker input.
131  //
132  // FIXME: For the linker case specifically, can we safely convert
133  // inputs into '-Wl,' options?
134  for (const auto &II : Inputs) {
135  // Don't try to pass LLVM or AST inputs to a generic gcc.
136  if (types::isLLVMIR(II.getType()))
137  D.Diag(clang::diag::err_drv_no_linker_llvm_support)
138  << getToolChain().getTripleString();
139  else if (II.getType() == types::TY_AST)
140  D.Diag(diag::err_drv_no_ast_support) << getToolChain().getTripleString();
141  else if (II.getType() == types::TY_ModuleFile)
142  D.Diag(diag::err_drv_no_module_support)
143  << getToolChain().getTripleString();
144 
145  if (types::canTypeBeUserSpecified(II.getType())) {
146  CmdArgs.push_back("-x");
147  CmdArgs.push_back(types::getTypeName(II.getType()));
148  }
149 
150  if (II.isFilename())
151  CmdArgs.push_back(II.getFilename());
152  else {
153  const Arg &A = II.getInputArg();
154 
155  // Reverse translate some rewritten options.
156  if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
157  CmdArgs.push_back("-lstdc++");
158  continue;
159  }
160 
161  // Don't render as input, we need gcc to do the translations.
162  A.render(Args, CmdArgs);
163  }
164  }
165 
166  const std::string &customGCCName = D.getCCCGenericGCCName();
167  const char *GCCName;
168  if (!customGCCName.empty())
169  GCCName = customGCCName.c_str();
170  else if (D.CCCIsCXX()) {
171  GCCName = "g++";
172  } else
173  GCCName = "gcc";
174 
175  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
176  C.addCommand(std::make_unique<Command>(JA, *this,
178  Exec, CmdArgs, Inputs, Output));
179 }
180 
182  const JobAction &JA, ArgStringList &CmdArgs) const {
183  CmdArgs.push_back("-E");
184 }
185 
187  ArgStringList &CmdArgs) const {
188  const Driver &D = getToolChain().getDriver();
189 
190  switch (JA.getType()) {
191  // If -flto, etc. are present then make sure not to force assembly output.
192  case types::TY_LLVM_IR:
193  case types::TY_LTO_IR:
194  case types::TY_LLVM_BC:
195  case types::TY_LTO_BC:
196  CmdArgs.push_back("-c");
197  break;
198  // We assume we've got an "integrated" assembler in that gcc will produce an
199  // object file itself.
200  case types::TY_Object:
201  CmdArgs.push_back("-c");
202  break;
203  case types::TY_PP_Asm:
204  CmdArgs.push_back("-S");
205  break;
206  case types::TY_Nothing:
207  CmdArgs.push_back("-fsyntax-only");
208  break;
209  default:
210  D.Diag(diag::err_drv_invalid_gcc_output_type) << getTypeName(JA.getType());
211  }
212 }
213 
215  ArgStringList &CmdArgs) const {
216  // The types are (hopefully) good enough.
217 }
218 
219 // On Arm the endianness of the output file is determined by the target and
220 // can be overridden by the pseudo-target flags '-mlittle-endian'/'-EL' and
221 // '-mbig-endian'/'-EB'. Unlike other targets the flag does not result in a
222 // normalized triple so we must handle the flag here.
223 static bool isArmBigEndian(const llvm::Triple &Triple,
224  const ArgList &Args) {
225  bool IsBigEndian = false;
226  switch (Triple.getArch()) {
227  case llvm::Triple::armeb:
228  case llvm::Triple::thumbeb:
229  IsBigEndian = true;
230  LLVM_FALLTHROUGH;
231  case llvm::Triple::arm:
232  case llvm::Triple::thumb:
233  if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
234  options::OPT_mbig_endian))
235  IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
236  break;
237  default:
238  break;
239  }
240  return IsBigEndian;
241 }
242 
243 static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
244  switch (T.getArch()) {
245  case llvm::Triple::x86:
246  if (T.isOSIAMCU())
247  return "elf_iamcu";
248  return "elf_i386";
249  case llvm::Triple::aarch64:
250  return "aarch64linux";
251  case llvm::Triple::aarch64_be:
252  return "aarch64linuxb";
253  case llvm::Triple::arm:
254  case llvm::Triple::thumb:
255  case llvm::Triple::armeb:
256  case llvm::Triple::thumbeb:
257  return isArmBigEndian(T, Args) ? "armelfb_linux_eabi" : "armelf_linux_eabi";
258  case llvm::Triple::m68k:
259  return "m68kelf";
260  case llvm::Triple::ppc:
261  if (T.isOSLinux())
262  return "elf32ppclinux";
263  return "elf32ppc";
264  case llvm::Triple::ppcle:
265  if (T.isOSLinux())
266  return "elf32lppclinux";
267  return "elf32lppc";
268  case llvm::Triple::ppc64:
269  return "elf64ppc";
270  case llvm::Triple::ppc64le:
271  return "elf64lppc";
272  case llvm::Triple::riscv32:
273  return "elf32lriscv";
274  case llvm::Triple::riscv64:
275  return "elf64lriscv";
276  case llvm::Triple::sparc:
277  case llvm::Triple::sparcel:
278  return "elf32_sparc";
279  case llvm::Triple::sparcv9:
280  return "elf64_sparc";
281  case llvm::Triple::mips:
282  return "elf32btsmip";
283  case llvm::Triple::mipsel:
284  return "elf32ltsmip";
285  case llvm::Triple::mips64:
286  if (tools::mips::hasMipsAbiArg(Args, "n32") ||
287  T.getEnvironment() == llvm::Triple::GNUABIN32)
288  return "elf32btsmipn32";
289  return "elf64btsmip";
290  case llvm::Triple::mips64el:
291  if (tools::mips::hasMipsAbiArg(Args, "n32") ||
292  T.getEnvironment() == llvm::Triple::GNUABIN32)
293  return "elf32ltsmipn32";
294  return "elf64ltsmip";
295  case llvm::Triple::systemz:
296  return "elf64_s390";
297  case llvm::Triple::x86_64:
298  if (T.isX32())
299  return "elf32_x86_64";
300  return "elf_x86_64";
301  case llvm::Triple::ve:
302  return "elf64ve";
303  case llvm::Triple::csky:
304  return "cskyelf_linux";
305  default:
306  return nullptr;
307  }
308 }
309 
310 static bool getPIE(const ArgList &Args, const ToolChain &TC) {
311  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static) ||
312  Args.hasArg(options::OPT_r) || Args.hasArg(options::OPT_static_pie))
313  return false;
314 
315  Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie,
316  options::OPT_nopie);
317  if (!A)
318  return TC.isPIEDefault(Args);
319  return A->getOption().matches(options::OPT_pie);
320 }
321 
322 static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
323  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
324  // -no-pie is an alias for -nopie. So, handling -nopie takes care of
325  // -no-pie as well.
326  if (HasStaticPIE && Args.hasArg(options::OPT_nopie)) {
327  const Driver &D = TC.getDriver();
328  const llvm::opt::OptTable &Opts = D.getOpts();
329  const char *StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
330  const char *NoPIEName = Opts.getOptionName(options::OPT_nopie);
331  D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
332  }
333  return HasStaticPIE;
334 }
335 
336 static bool getStatic(const ArgList &Args) {
337  return Args.hasArg(options::OPT_static) &&
338  !Args.hasArg(options::OPT_static_pie);
339 }
340 
342  Compilation &C, const JobAction &JA, const InputInfo &Output,
343  const InputInfoList &Inputs, const ArgList &Args,
344  const char *LinkingOutput) const {
345  const Driver &D = getToolChain().getDriver();
346 
347  // Silence warning for "clang -g foo.o -o foo"
348  Args.ClaimAllArgs(options::OPT_g_Group);
349  // and "clang -emit-llvm foo.o -o foo"
350  Args.ClaimAllArgs(options::OPT_emit_llvm);
351  // and for "clang -w foo.o -o foo". Other warning options are already
352  // handled somewhere else.
353  Args.ClaimAllArgs(options::OPT_w);
354  // Silence warnings when linking C code with a C++ '-stdlib' argument.
355  Args.ClaimAllArgs(options::OPT_stdlib_EQ);
356 
357  // ar tool command "llvm-ar <options> <output_file> <input_files>".
358  ArgStringList CmdArgs;
359  // Create and insert file members with a deterministic index.
360  CmdArgs.push_back("rcsD");
361  CmdArgs.push_back(Output.getFilename());
362 
363  for (const auto &II : Inputs) {
364  if (II.isFilename()) {
365  CmdArgs.push_back(II.getFilename());
366  }
367  }
368 
369  // Delete old output archive file if it already exists before generating a new
370  // archive file.
371  auto OutputFileName = Output.getFilename();
372  if (Output.isFilename() && llvm::sys::fs::exists(OutputFileName)) {
373  if (std::error_code EC = llvm::sys::fs::remove(OutputFileName)) {
374  D.Diag(diag::err_drv_unable_to_remove_file) << EC.message();
375  return;
376  }
377  }
378 
379  const char *Exec = Args.MakeArgString(getToolChain().GetStaticLibToolPath());
380  C.addCommand(std::make_unique<Command>(JA, *this,
382  Exec, CmdArgs, Inputs, Output));
383 }
384 
386  const InputInfo &Output,
387  const InputInfoList &Inputs,
388  const ArgList &Args,
389  const char *LinkingOutput) const {
390  // FIXME: The Linker class constructor takes a ToolChain and not a
391  // Generic_ELF, so the static_cast might return a reference to a invalid
392  // instance (see PR45061). Ideally, the Linker constructor needs to take a
393  // Generic_ELF instead.
395  static_cast<const toolchains::Generic_ELF &>(getToolChain());
396  const Driver &D = ToolChain.getDriver();
397 
398  const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
399 
400  const llvm::Triple::ArchType Arch = ToolChain.getArch();
401  const bool isAndroid = ToolChain.getTriple().isAndroid();
402  const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU();
403  const bool IsVE = ToolChain.getTriple().isVE();
404  const bool IsPIE = getPIE(Args, ToolChain);
405  const bool IsStaticPIE = getStaticPIE(Args, ToolChain);
406  const bool IsStatic = getStatic(Args);
407  const bool HasCRTBeginEndFiles =
408  ToolChain.getTriple().hasEnvironment() ||
409  (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);
410 
411  ArgStringList CmdArgs;
412 
413  // Silence warning for "clang -g foo.o -o foo"
414  Args.ClaimAllArgs(options::OPT_g_Group);
415  // and "clang -emit-llvm foo.o -o foo"
416  Args.ClaimAllArgs(options::OPT_emit_llvm);
417  // and for "clang -w foo.o -o foo". Other warning options are already
418  // handled somewhere else.
419  Args.ClaimAllArgs(options::OPT_w);
420 
421  if (!D.SysRoot.empty())
422  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
423 
424  if (IsPIE)
425  CmdArgs.push_back("-pie");
426 
427  if (IsStaticPIE) {
428  CmdArgs.push_back("-static");
429  CmdArgs.push_back("-pie");
430  CmdArgs.push_back("--no-dynamic-linker");
431  CmdArgs.push_back("-z");
432  CmdArgs.push_back("text");
433  }
434 
435  if (Args.hasArg(options::OPT_rdynamic))
436  CmdArgs.push_back("-export-dynamic");
437 
438  if (Args.hasArg(options::OPT_s))
439  CmdArgs.push_back("-s");
440 
441  if (Triple.isARM() || Triple.isThumb() || Triple.isAArch64()) {
442  bool IsBigEndian = isArmBigEndian(Triple, Args);
443  if (IsBigEndian)
444  arm::appendBE8LinkFlag(Args, CmdArgs, Triple);
445  IsBigEndian = IsBigEndian || Arch == llvm::Triple::aarch64_be;
446  CmdArgs.push_back(IsBigEndian ? "-EB" : "-EL");
447  }
448 
449  // Most Android ARM64 targets should enable the linker fix for erratum
450  // 843419. Only non-Cortex-A53 devices are allowed to skip this flag.
451  if (Arch == llvm::Triple::aarch64 && isAndroid) {
452  std::string CPU = getCPUName(D, Args, Triple);
453  if (CPU.empty() || CPU == "generic" || CPU == "cortex-a53")
454  CmdArgs.push_back("--fix-cortex-a53-843419");
455  }
456 
457  ToolChain.addExtraOpts(CmdArgs);
458 
459  CmdArgs.push_back("--eh-frame-hdr");
460 
461  if (const char *LDMOption = getLDMOption(ToolChain.getTriple(), Args)) {
462  CmdArgs.push_back("-m");
463  CmdArgs.push_back(LDMOption);
464  } else {
465  D.Diag(diag::err_target_unknown_triple) << Triple.str();
466  return;
467  }
468 
469  if (Args.hasArg(options::OPT_shared))
470  CmdArgs.push_back("-shared");
471 
472  if (IsStatic) {
473  CmdArgs.push_back("-static");
474  } else {
475  if (Args.hasArg(options::OPT_rdynamic))
476  CmdArgs.push_back("-export-dynamic");
477 
478  if (!Args.hasArg(options::OPT_shared) && !IsStaticPIE) {
479  CmdArgs.push_back("-dynamic-linker");
480  CmdArgs.push_back(Args.MakeArgString(Twine(D.DyldPrefix) +
481  ToolChain.getDynamicLinker(Args)));
482  }
483  }
484 
485  CmdArgs.push_back("-o");
486  CmdArgs.push_back(Output.getFilename());
487 
488  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
489  options::OPT_r)) {
490  if (!isAndroid && !IsIAMCU) {
491  const char *crt1 = nullptr;
492  if (!Args.hasArg(options::OPT_shared)) {
493  if (Args.hasArg(options::OPT_pg))
494  crt1 = "gcrt1.o";
495  else if (IsPIE)
496  crt1 = "Scrt1.o";
497  else if (IsStaticPIE)
498  crt1 = "rcrt1.o";
499  else
500  crt1 = "crt1.o";
501  }
502  if (crt1)
503  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
504 
505  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
506  }
507 
508  if (IsVE) {
509  CmdArgs.push_back("-z");
510  CmdArgs.push_back("max-page-size=0x4000000");
511  }
512 
513  if (IsIAMCU)
514  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
515  else if (HasCRTBeginEndFiles) {
516  std::string P;
518  !isAndroid) {
519  std::string crtbegin = ToolChain.getCompilerRT(Args, "crtbegin",
521  if (ToolChain.getVFS().exists(crtbegin))
522  P = crtbegin;
523  }
524  if (P.empty()) {
525  const char *crtbegin;
526  if (Args.hasArg(options::OPT_shared))
527  crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
528  else if (IsStatic)
529  crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
530  else if (IsPIE || IsStaticPIE)
531  crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
532  else
533  crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
534  P = ToolChain.GetFilePath(crtbegin);
535  }
536  CmdArgs.push_back(Args.MakeArgString(P));
537  }
538 
539  // Add crtfastmath.o if available and fast math is enabled.
541  }
542 
543  Args.AddAllArgs(CmdArgs, options::OPT_L);
544  Args.AddAllArgs(CmdArgs, options::OPT_u);
545 
546  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
547 
548  if (D.isUsingLTO()) {
549  assert(!Inputs.empty() && "Must have at least one input.");
550  addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
551  D.getLTOMode() == LTOK_Thin);
552  }
553 
554  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
555  CmdArgs.push_back("--no-demangle");
556 
557  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
558  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
560  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
561 
562  addHIPRuntimeLibArgs(ToolChain, Args, CmdArgs);
563 
564  // The profile runtime also needs access to system libraries.
565  getToolChain().addProfileRTLibs(Args, CmdArgs);
566 
567  if (D.CCCIsCXX() &&
568  !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
569  options::OPT_r)) {
570  if (ToolChain.ShouldLinkCXXStdlib(Args)) {
571  bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
572  !Args.hasArg(options::OPT_static);
573  if (OnlyLibstdcxxStatic)
574  CmdArgs.push_back("-Bstatic");
575  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
576  if (OnlyLibstdcxxStatic)
577  CmdArgs.push_back("-Bdynamic");
578  }
579  CmdArgs.push_back("-lm");
580  }
581 
582  // If we are linking for the device all symbols should be bound locally. The
583  // symbols are already protected which makes this redundant. This is only
584  // necessary to work around a problem in bfd.
585  // TODO: Remove this once 'lld' becomes the only linker for offloading.
587  CmdArgs.push_back("-Bsymbolic");
588 
589  // Silence warnings when linking C code with a C++ '-stdlib' argument.
590  Args.ClaimAllArgs(options::OPT_stdlib_EQ);
591 
592  // Additional linker set-up and flags for Fortran. This is required in order
593  // to generate executables. As Fortran runtime depends on the C runtime,
594  // these dependencies need to be listed before the C runtime below (i.e.
595  // AddRuntTimeLibs).
596  //
597  // NOTE: Generating executables by Flang is considered an "experimental"
598  // feature and hence this is guarded with a command line option.
599  // TODO: Make this work unconditionally once Flang is mature enough.
600  if (D.IsFlangMode() && Args.hasArg(options::OPT_flang_experimental_exec)) {
601  addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
602  addFortranRuntimeLibs(CmdArgs);
603  CmdArgs.push_back("-lm");
604  }
605 
606  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r)) {
607  if (!Args.hasArg(options::OPT_nodefaultlibs)) {
608  if (IsStatic || IsStaticPIE)
609  CmdArgs.push_back("--start-group");
610 
611  if (NeedsSanitizerDeps)
613 
614  if (NeedsXRayDeps)
615  linkXRayRuntimeDeps(ToolChain, CmdArgs);
616 
617  bool WantPthread = Args.hasArg(options::OPT_pthread) ||
618  Args.hasArg(options::OPT_pthreads);
619 
620  // Use the static OpenMP runtime with -static-openmp
621  bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
622  !Args.hasArg(options::OPT_static);
623 
624  // FIXME: Only pass GompNeedsRT = true for platforms with libgomp that
625  // require librt. Most modern Linux platforms do, but some may not.
626  if (addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP,
628  /* GompNeedsRT= */ true))
629  // OpenMP runtimes implies pthreads when using the GNU toolchain.
630  // FIXME: Does this really make sense for all GNU toolchains?
631  WantPthread = true;
632 
633  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
634 
635  if (WantPthread && !isAndroid)
636  CmdArgs.push_back("-lpthread");
637 
638  if (Args.hasArg(options::OPT_fsplit_stack))
639  CmdArgs.push_back("--wrap=pthread_create");
640 
641  if (!Args.hasArg(options::OPT_nolibc))
642  CmdArgs.push_back("-lc");
643 
644  // Add IAMCU specific libs, if needed.
645  if (IsIAMCU)
646  CmdArgs.push_back("-lgloss");
647 
648  if (IsStatic || IsStaticPIE)
649  CmdArgs.push_back("--end-group");
650  else
651  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
652 
653  // Add IAMCU specific libs (outside the group), if needed.
654  if (IsIAMCU) {
655  CmdArgs.push_back("--as-needed");
656  CmdArgs.push_back("-lsoftfp");
657  CmdArgs.push_back("--no-as-needed");
658  }
659  }
660 
661  if (!Args.hasArg(options::OPT_nostartfiles) && !IsIAMCU) {
662  if (HasCRTBeginEndFiles) {
663  std::string P;
665  !isAndroid) {
666  std::string crtend = ToolChain.getCompilerRT(Args, "crtend",
668  if (ToolChain.getVFS().exists(crtend))
669  P = crtend;
670  }
671  if (P.empty()) {
672  const char *crtend;
673  if (Args.hasArg(options::OPT_shared))
674  crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
675  else if (IsPIE || IsStaticPIE)
676  crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
677  else
678  crtend = isAndroid ? "crtend_android.o" : "crtend.o";
679  P = ToolChain.GetFilePath(crtend);
680  }
681  CmdArgs.push_back(Args.MakeArgString(P));
682  }
683  if (!isAndroid)
684  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
685  }
686  }
687 
688  Args.AddAllArgs(CmdArgs, options::OPT_T);
689 
690  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
691  C.addCommand(std::make_unique<Command>(JA, *this,
693  Exec, CmdArgs, Inputs, Output));
694 }
695 
697  const JobAction &JA,
698  const InputInfo &Output,
699  const InputInfoList &Inputs,
700  const ArgList &Args,
701  const char *LinkingOutput) const {
702  const auto &D = getToolChain().getDriver();
703 
704  claimNoWarnArgs(Args);
705 
706  ArgStringList CmdArgs;
707 
708  llvm::Reloc::Model RelocationModel;
709  unsigned PICLevel;
710  bool IsPIE;
711  const char *DefaultAssembler = "as";
712  std::tie(RelocationModel, PICLevel, IsPIE) =
713  ParsePICArgs(getToolChain(), Args);
714 
715  if (const Arg *A = Args.getLastArg(options::OPT_gz, options::OPT_gz_EQ)) {
716  if (A->getOption().getID() == options::OPT_gz) {
717  CmdArgs.push_back("--compress-debug-sections");
718  } else {
719  StringRef Value = A->getValue();
720  if (Value == "none" || Value == "zlib") {
721  CmdArgs.push_back(
722  Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
723  } else {
724  D.Diag(diag::err_drv_unsupported_option_argument)
725  << A->getOption().getName() << Value;
726  }
727  }
728  }
729 
730  switch (getToolChain().getArch()) {
731  default:
732  break;
733  // Add --32/--64 to make sure we get the format we want.
734  // This is incomplete
735  case llvm::Triple::x86:
736  CmdArgs.push_back("--32");
737  break;
738  case llvm::Triple::x86_64:
739  if (getToolChain().getTriple().isX32())
740  CmdArgs.push_back("--x32");
741  else
742  CmdArgs.push_back("--64");
743  break;
744  case llvm::Triple::ppc: {
745  CmdArgs.push_back("-a32");
746  CmdArgs.push_back("-mppc");
747  CmdArgs.push_back("-mbig-endian");
748  CmdArgs.push_back(ppc::getPPCAsmModeForCPU(
749  getCPUName(D, Args, getToolChain().getTriple())));
750  break;
751  }
752  case llvm::Triple::ppcle: {
753  CmdArgs.push_back("-a32");
754  CmdArgs.push_back("-mppc");
755  CmdArgs.push_back("-mlittle-endian");
756  CmdArgs.push_back(ppc::getPPCAsmModeForCPU(
757  getCPUName(D, Args, getToolChain().getTriple())));
758  break;
759  }
760  case llvm::Triple::ppc64: {
761  CmdArgs.push_back("-a64");
762  CmdArgs.push_back("-mppc64");
763  CmdArgs.push_back("-mbig-endian");
764  CmdArgs.push_back(ppc::getPPCAsmModeForCPU(
765  getCPUName(D, Args, getToolChain().getTriple())));
766  break;
767  }
768  case llvm::Triple::ppc64le: {
769  CmdArgs.push_back("-a64");
770  CmdArgs.push_back("-mppc64");
771  CmdArgs.push_back("-mlittle-endian");
772  CmdArgs.push_back(ppc::getPPCAsmModeForCPU(
773  getCPUName(D, Args, getToolChain().getTriple())));
774  break;
775  }
776  case llvm::Triple::riscv32:
777  case llvm::Triple::riscv64: {
778  StringRef ABIName = riscv::getRISCVABI(Args, getToolChain().getTriple());
779  CmdArgs.push_back("-mabi");
780  CmdArgs.push_back(ABIName.data());
781  StringRef MArchName = riscv::getRISCVArch(Args, getToolChain().getTriple());
782  CmdArgs.push_back("-march");
783  CmdArgs.push_back(MArchName.data());
784  if (!Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
785  Args.addOptOutFlag(CmdArgs, options::OPT_mrelax, options::OPT_mno_relax);
786  break;
787  }
788  case llvm::Triple::sparc:
789  case llvm::Triple::sparcel: {
790  CmdArgs.push_back("-32");
791  std::string CPU = getCPUName(D, Args, getToolChain().getTriple());
792  CmdArgs.push_back(
793  sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
794  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
795  break;
796  }
797  case llvm::Triple::sparcv9: {
798  CmdArgs.push_back("-64");
799  std::string CPU = getCPUName(D, Args, getToolChain().getTriple());
800  CmdArgs.push_back(
801  sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
802  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
803  break;
804  }
805  case llvm::Triple::arm:
806  case llvm::Triple::armeb:
807  case llvm::Triple::thumb:
808  case llvm::Triple::thumbeb: {
809  const llvm::Triple &Triple2 = getToolChain().getTriple();
810  CmdArgs.push_back(isArmBigEndian(Triple2, Args) ? "-EB" : "-EL");
811  switch (Triple2.getSubArch()) {
812  case llvm::Triple::ARMSubArch_v7:
813  CmdArgs.push_back("-mfpu=neon");
814  break;
815  case llvm::Triple::ARMSubArch_v8:
816  CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8");
817  break;
818  default:
819  break;
820  }
821 
822  switch (arm::getARMFloatABI(getToolChain(), Args)) {
823  case arm::FloatABI::Invalid: llvm_unreachable("must have an ABI!");
824  case arm::FloatABI::Soft:
825  CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=soft"));
826  break;
827  case arm::FloatABI::SoftFP:
828  CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=softfp"));
829  break;
830  case arm::FloatABI::Hard:
831  CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=hard"));
832  break;
833  }
834 
835  Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
836  normalizeCPUNamesForAssembler(Args, CmdArgs);
837 
838  Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
839  break;
840  }
841  case llvm::Triple::aarch64:
842  case llvm::Triple::aarch64_be: {
843  CmdArgs.push_back(
844  getToolChain().getArch() == llvm::Triple::aarch64_be ? "-EB" : "-EL");
845  Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
846  normalizeCPUNamesForAssembler(Args, CmdArgs);
847 
848  break;
849  }
850  case llvm::Triple::mips:
851  case llvm::Triple::mipsel:
852  case llvm::Triple::mips64:
853  case llvm::Triple::mips64el: {
854  StringRef CPUName;
855  StringRef ABIName;
856  mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
857  ABIName = mips::getGnuCompatibleMipsABIName(ABIName);
858 
859  CmdArgs.push_back("-march");
860  CmdArgs.push_back(CPUName.data());
861 
862  CmdArgs.push_back("-mabi");
863  CmdArgs.push_back(ABIName.data());
864 
865  // -mno-shared should be emitted unless -fpic, -fpie, -fPIC, -fPIE,
866  // or -mshared (not implemented) is in effect.
867  if (RelocationModel == llvm::Reloc::Static)
868  CmdArgs.push_back("-mno-shared");
869 
870  // LLVM doesn't support -mplt yet and acts as if it is always given.
871  // However, -mplt has no effect with the N64 ABI.
872  if (ABIName != "64" && !Args.hasArg(options::OPT_mno_abicalls))
873  CmdArgs.push_back("-call_nonpic");
874 
875  if (getToolChain().getTriple().isLittleEndian())
876  CmdArgs.push_back("-EL");
877  else
878  CmdArgs.push_back("-EB");
879 
880  if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
881  if (StringRef(A->getValue()) == "2008")
882  CmdArgs.push_back(Args.MakeArgString("-mnan=2008"));
883  }
884 
885  // Add the last -mfp32/-mfpxx/-mfp64 or -mfpxx if it is enabled by default.
886  if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
887  options::OPT_mfp64)) {
888  A->claim();
889  A->render(Args, CmdArgs);
890  } else if (mips::shouldUseFPXX(
891  Args, getToolChain().getTriple(), CPUName, ABIName,
892  mips::getMipsFloatABI(getToolChain().getDriver(), Args,
893  getToolChain().getTriple())))
894  CmdArgs.push_back("-mfpxx");
895 
896  // Pass on -mmips16 or -mno-mips16. However, the assembler equivalent of
897  // -mno-mips16 is actually -no-mips16.
898  if (Arg *A =
899  Args.getLastArg(options::OPT_mips16, options::OPT_mno_mips16)) {
900  if (A->getOption().matches(options::OPT_mips16)) {
901  A->claim();
902  A->render(Args, CmdArgs);
903  } else {
904  A->claim();
905  CmdArgs.push_back("-no-mips16");
906  }
907  }
908 
909  Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
910  options::OPT_mno_micromips);
911  Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
912  Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
913 
914  if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) {
915  // Do not use AddLastArg because not all versions of MIPS assembler
916  // support -mmsa / -mno-msa options.
917  if (A->getOption().matches(options::OPT_mmsa))
918  CmdArgs.push_back(Args.MakeArgString("-mmsa"));
919  }
920 
921  Args.AddLastArg(CmdArgs, options::OPT_mhard_float,
922  options::OPT_msoft_float);
923 
924  Args.AddLastArg(CmdArgs, options::OPT_mdouble_float,
925  options::OPT_msingle_float);
926 
927  Args.AddLastArg(CmdArgs, options::OPT_modd_spreg,
928  options::OPT_mno_odd_spreg);
929 
930  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
931  break;
932  }
933  case llvm::Triple::systemz: {
934  // Always pass an -march option, since our default of z10 is later
935  // than the GNU assembler's default.
937  CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
938  break;
939  }
940  case llvm::Triple::ve:
941  DefaultAssembler = "nas";
942  }
943 
944  for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
945  options::OPT_fdebug_prefix_map_EQ)) {
946  StringRef Map = A->getValue();
947  if (!Map.contains('='))
948  D.Diag(diag::err_drv_invalid_argument_to_option)
949  << Map << A->getOption().getName();
950  else {
951  CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map"));
952  CmdArgs.push_back(Args.MakeArgString(Map));
953  }
954  A->claim();
955  }
956 
957  Args.AddAllArgs(CmdArgs, options::OPT_I);
958  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
959 
960  CmdArgs.push_back("-o");
961  CmdArgs.push_back(Output.getFilename());
962 
963  for (const auto &II : Inputs)
964  CmdArgs.push_back(II.getFilename());
965 
966  const char *Exec =
967  Args.MakeArgString(getToolChain().GetProgramPath(DefaultAssembler));
968  C.addCommand(std::make_unique<Command>(JA, *this,
970  Exec, CmdArgs, Inputs, Output));
971 
972  // Handle the debug info splitting at object creation time if we're
973  // creating an object.
974  // TODO: Currently only works on linux with newer objcopy.
975  if (Args.hasArg(options::OPT_gsplit_dwarf) &&
976  getToolChain().getTriple().isOSLinux())
977  SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
978  SplitDebugName(JA, Args, Inputs[0], Output));
979 }
980 
981 namespace {
982 // Filter to remove Multilibs that don't exist as a suffix to Path
983 class FilterNonExistent {
984  StringRef Base, File;
985  llvm::vfs::FileSystem &VFS;
986 
987 public:
988  FilterNonExistent(StringRef Base, StringRef File, llvm::vfs::FileSystem &VFS)
989  : Base(Base), File(File), VFS(VFS) {}
990  bool operator()(const Multilib &M) {
991  return !VFS.exists(Base + M.gccSuffix() + File);
992  }
993 };
994 } // end anonymous namespace
995 
996 static bool isSoftFloatABI(const ArgList &Args) {
997  Arg *A = Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
998  options::OPT_mfloat_abi_EQ);
999  if (!A)
1000  return false;
1001 
1002  return A->getOption().matches(options::OPT_msoft_float) ||
1003  (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
1004  A->getValue() == StringRef("soft"));
1005 }
1006 
1007 static bool isArmOrThumbArch(llvm::Triple::ArchType Arch) {
1008  return Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb;
1009 }
1010 
1011 static bool isMipsEL(llvm::Triple::ArchType Arch) {
1012  return Arch == llvm::Triple::mipsel || Arch == llvm::Triple::mips64el;
1013 }
1014 
1015 static bool isMips16(const ArgList &Args) {
1016  Arg *A = Args.getLastArg(options::OPT_mips16, options::OPT_mno_mips16);
1017  return A && A->getOption().matches(options::OPT_mips16);
1018 }
1019 
1020 static bool isMicroMips(const ArgList &Args) {
1021  Arg *A = Args.getLastArg(options::OPT_mmicromips, options::OPT_mno_micromips);
1022  return A && A->getOption().matches(options::OPT_mmicromips);
1023 }
1024 
1025 static bool isMSP430(llvm::Triple::ArchType Arch) {
1026  return Arch == llvm::Triple::msp430;
1027 }
1028 
1029 static Multilib makeMultilib(StringRef commonSuffix) {
1030  return Multilib(commonSuffix, commonSuffix, commonSuffix);
1031 }
1032 
1034  FilterNonExistent &NonExistent,
1035  DetectedMultilibs &Result) {
1036  // Check for Code Sourcery toolchain multilibs
1037  MultilibSet CSMipsMultilibs;
1038  {
1039  auto MArchMips16 = makeMultilib("/mips16").flag("+m32").flag("+mips16");
1040 
1041  auto MArchMicroMips =
1042  makeMultilib("/micromips").flag("+m32").flag("+mmicromips");
1043 
1044  auto MArchDefault = makeMultilib("").flag("-mips16").flag("-mmicromips");
1045 
1046  auto UCLibc = makeMultilib("/uclibc").flag("+muclibc");
1047 
1048  auto SoftFloat = makeMultilib("/soft-float").flag("+msoft-float");
1049 
1050  auto Nan2008 = makeMultilib("/nan2008").flag("+mnan=2008");
1051 
1052  auto DefaultFloat =
1053  makeMultilib("").flag("-msoft-float").flag("-mnan=2008");
1054 
1055  auto BigEndian = makeMultilib("").flag("+EB").flag("-EL");
1056 
1057  auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB");
1058 
1059  // Note that this one's osSuffix is ""
1060  auto MAbi64 = makeMultilib("")
1061  .gccSuffix("/64")
1062  .includeSuffix("/64")
1063  .flag("+mabi=n64")
1064  .flag("-mabi=n32")
1065  .flag("-m32");
1066 
1067  CSMipsMultilibs =
1068  MultilibSet()
1069  .Either(MArchMips16, MArchMicroMips, MArchDefault)
1070  .Maybe(UCLibc)
1071  .Either(SoftFloat, Nan2008, DefaultFloat)
1072  .FilterOut("/micromips/nan2008")
1073  .FilterOut("/mips16/nan2008")
1074  .Either(BigEndian, LittleEndian)
1075  .Maybe(MAbi64)
1076  .FilterOut("/mips16.*/64")
1077  .FilterOut("/micromips.*/64")
1078  .FilterOut(NonExistent)
1079  .setIncludeDirsCallback([](const Multilib &M) {
1080  std::vector<std::string> Dirs({"/include"});
1081  if (StringRef(M.includeSuffix()).startswith("/uclibc"))
1082  Dirs.push_back(
1083  "/../../../../mips-linux-gnu/libc/uclibc/usr/include");
1084  else
1085  Dirs.push_back("/../../../../mips-linux-gnu/libc/usr/include");
1086  return Dirs;
1087  });
1088  }
1089 
1090  MultilibSet DebianMipsMultilibs;
1091  {
1092  Multilib MAbiN32 =
1093  Multilib().gccSuffix("/n32").includeSuffix("/n32").flag("+mabi=n32");
1094 
1095  Multilib M64 = Multilib()
1096  .gccSuffix("/64")
1097  .includeSuffix("/64")
1098  .flag("+m64")
1099  .flag("-m32")
1100  .flag("-mabi=n32");
1101 
1102  Multilib M32 =
1103  Multilib().gccSuffix("/32").flag("-m64").flag("+m32").flag("-mabi=n32");
1104 
1105  DebianMipsMultilibs =
1106  MultilibSet().Either(M32, M64, MAbiN32).FilterOut(NonExistent);
1107  }
1108 
1109  // Sort candidates. Toolchain that best meets the directories tree goes first.
1110  // Then select the first toolchains matches command line flags.
1111  MultilibSet *Candidates[] = {&CSMipsMultilibs, &DebianMipsMultilibs};
1112  if (CSMipsMultilibs.size() < DebianMipsMultilibs.size())
1113  std::iter_swap(Candidates, Candidates + 1);
1114  for (const MultilibSet *Candidate : Candidates) {
1115  if (Candidate->select(Flags, Result.SelectedMultilib)) {
1116  if (Candidate == &DebianMipsMultilibs)
1117  Result.BiarchSibling = Multilib();
1118  Result.Multilibs = *Candidate;
1119  return true;
1120  }
1121  }
1122  return false;
1123 }
1124 
1125 static bool findMipsAndroidMultilibs(llvm::vfs::FileSystem &VFS, StringRef Path,
1126  const Multilib::flags_list &Flags,
1127  FilterNonExistent &NonExistent,
1128  DetectedMultilibs &Result) {
1129 
1130  MultilibSet AndroidMipsMultilibs =
1131  MultilibSet()
1132  .Maybe(Multilib("/mips-r2").flag("+march=mips32r2"))
1133  .Maybe(Multilib("/mips-r6").flag("+march=mips32r6"))
1134  .FilterOut(NonExistent);
1135 
1136  MultilibSet AndroidMipselMultilibs =
1137  MultilibSet()
1138  .Either(Multilib().flag("+march=mips32"),
1139  Multilib("/mips-r2", "", "/mips-r2").flag("+march=mips32r2"),
1140  Multilib("/mips-r6", "", "/mips-r6").flag("+march=mips32r6"))
1141  .FilterOut(NonExistent);
1142 
1143  MultilibSet AndroidMips64elMultilibs =
1144  MultilibSet()
1145  .Either(
1146  Multilib().flag("+march=mips64r6"),
1147  Multilib("/32/mips-r1", "", "/mips-r1").flag("+march=mips32"),
1148  Multilib("/32/mips-r2", "", "/mips-r2").flag("+march=mips32r2"),
1149  Multilib("/32/mips-r6", "", "/mips-r6").flag("+march=mips32r6"))
1150  .FilterOut(NonExistent);
1151 
1152  MultilibSet *MS = &AndroidMipsMultilibs;
1153  if (VFS.exists(Path + "/mips-r6"))
1154  MS = &AndroidMipselMultilibs;
1155  else if (VFS.exists(Path + "/32"))
1156  MS = &AndroidMips64elMultilibs;
1157  if (MS->select(Flags, Result.SelectedMultilib)) {
1158  Result.Multilibs = *MS;
1159  return true;
1160  }
1161  return false;
1162 }
1163 
1165  FilterNonExistent &NonExistent,
1166  DetectedMultilibs &Result) {
1167  // Musl toolchain multilibs
1168  MultilibSet MuslMipsMultilibs;
1169  {
1170  auto MArchMipsR2 = makeMultilib("")
1171  .osSuffix("/mips-r2-hard-musl")
1172  .flag("+EB")
1173  .flag("-EL")
1174  .flag("+march=mips32r2");
1175 
1176  auto MArchMipselR2 = makeMultilib("/mipsel-r2-hard-musl")
1177  .flag("-EB")
1178  .flag("+EL")
1179  .flag("+march=mips32r2");
1180 
1181  MuslMipsMultilibs = MultilibSet().Either(MArchMipsR2, MArchMipselR2);
1182 
1183  // Specify the callback that computes the include directories.
1184  MuslMipsMultilibs.setIncludeDirsCallback([](const Multilib &M) {
1185  return std::vector<std::string>(
1186  {"/../sysroot" + M.osSuffix() + "/usr/include"});
1187  });
1188  }
1189  if (MuslMipsMultilibs.select(Flags, Result.SelectedMultilib)) {
1190  Result.Multilibs = MuslMipsMultilibs;
1191  return true;
1192  }
1193  return false;
1194 }
1195 
1197  FilterNonExistent &NonExistent,
1198  DetectedMultilibs &Result) {
1199  // CodeScape MTI toolchain v1.2 and early.
1200  MultilibSet MtiMipsMultilibsV1;
1201  {
1202  auto MArchMips32 = makeMultilib("/mips32")
1203  .flag("+m32")
1204  .flag("-m64")
1205  .flag("-mmicromips")
1206  .flag("+march=mips32");
1207 
1208  auto MArchMicroMips = makeMultilib("/micromips")
1209  .flag("+m32")
1210  .flag("-m64")
1211  .flag("+mmicromips");
1212 
1213  auto MArchMips64r2 = makeMultilib("/mips64r2")
1214  .flag("-m32")
1215  .flag("+m64")
1216  .flag("+march=mips64r2");
1217 
1218  auto MArchMips64 = makeMultilib("/mips64").flag("-m32").flag("+m64").flag(
1219  "-march=mips64r2");
1220 
1221  auto MArchDefault = makeMultilib("")
1222  .flag("+m32")
1223  .flag("-m64")
1224  .flag("-mmicromips")
1225  .flag("+march=mips32r2");
1226 
1227  auto Mips16 = makeMultilib("/mips16").flag("+mips16");
1228 
1229  auto UCLibc = makeMultilib("/uclibc").flag("+muclibc");
1230 
1231  auto MAbi64 =
1232  makeMultilib("/64").flag("+mabi=n64").flag("-mabi=n32").flag("-m32");
1233 
1234  auto BigEndian = makeMultilib("").flag("+EB").flag("-EL");
1235 
1236  auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB");
1237 
1238  auto SoftFloat = makeMultilib("/sof").flag("+msoft-float");
1239 
1240  auto Nan2008 = makeMultilib("/nan2008").flag("+mnan=2008");
1241 
1242  MtiMipsMultilibsV1 =
1243  MultilibSet()
1244  .Either(MArchMips32, MArchMicroMips, MArchMips64r2, MArchMips64,
1245  MArchDefault)
1246  .Maybe(UCLibc)
1247  .Maybe(Mips16)
1248  .FilterOut("/mips64/mips16")
1249  .FilterOut("/mips64r2/mips16")
1250  .FilterOut("/micromips/mips16")
1251  .Maybe(MAbi64)
1252  .FilterOut("/micromips/64")
1253  .FilterOut("/mips32/64")
1254  .FilterOut("^/64")
1255  .FilterOut("/mips16/64")
1256  .Either(BigEndian, LittleEndian)
1257  .Maybe(SoftFloat)
1258  .Maybe(Nan2008)
1259  .FilterOut(".*sof/nan2008")
1260  .FilterOut(NonExistent)
1261  .setIncludeDirsCallback([](const Multilib &M) {
1262  std::vector<std::string> Dirs({"/include"});
1263  if (StringRef(M.includeSuffix()).startswith("/uclibc"))
1264  Dirs.push_back("/../../../../sysroot/uclibc/usr/include");
1265  else
1266  Dirs.push_back("/../../../../sysroot/usr/include");
1267  return Dirs;
1268  });
1269  }
1270 
1271  // CodeScape IMG toolchain starting from v1.3.
1272  MultilibSet MtiMipsMultilibsV2;
1273  {
1274  auto BeHard = makeMultilib("/mips-r2-hard")
1275  .flag("+EB")
1276  .flag("-msoft-float")
1277  .flag("-mnan=2008")
1278  .flag("-muclibc");
1279  auto BeSoft = makeMultilib("/mips-r2-soft")
1280  .flag("+EB")
1281  .flag("+msoft-float")
1282  .flag("-mnan=2008");
1283  auto ElHard = makeMultilib("/mipsel-r2-hard")
1284  .flag("+EL")
1285  .flag("-msoft-float")
1286  .flag("-mnan=2008")
1287  .flag("-muclibc");
1288  auto ElSoft = makeMultilib("/mipsel-r2-soft")
1289  .flag("+EL")
1290  .flag("+msoft-float")
1291  .flag("-mnan=2008")
1292  .flag("-mmicromips");
1293  auto BeHardNan = makeMultilib("/mips-r2-hard-nan2008")
1294  .flag("+EB")
1295  .flag("-msoft-float")
1296  .flag("+mnan=2008")
1297  .flag("-muclibc");
1298  auto ElHardNan = makeMultilib("/mipsel-r2-hard-nan2008")
1299  .flag("+EL")
1300  .flag("-msoft-float")
1301  .flag("+mnan=2008")
1302  .flag("-muclibc")
1303  .flag("-mmicromips");
1304  auto BeHardNanUclibc = makeMultilib("/mips-r2-hard-nan2008-uclibc")
1305  .flag("+EB")
1306  .flag("-msoft-float")
1307  .flag("+mnan=2008")
1308  .flag("+muclibc");
1309  auto ElHardNanUclibc = makeMultilib("/mipsel-r2-hard-nan2008-uclibc")
1310  .flag("+EL")
1311  .flag("-msoft-float")
1312  .flag("+mnan=2008")
1313  .flag("+muclibc");
1314  auto BeHardUclibc = makeMultilib("/mips-r2-hard-uclibc")
1315  .flag("+EB")
1316  .flag("-msoft-float")
1317  .flag("-mnan=2008")
1318  .flag("+muclibc");
1319  auto ElHardUclibc = makeMultilib("/mipsel-r2-hard-uclibc")
1320  .flag("+EL")
1321  .flag("-msoft-float")
1322  .flag("-mnan=2008")
1323  .flag("+muclibc");
1324  auto ElMicroHardNan = makeMultilib("/micromipsel-r2-hard-nan2008")
1325  .flag("+EL")
1326  .flag("-msoft-float")
1327  .flag("+mnan=2008")
1328  .flag("+mmicromips");
1329  auto ElMicroSoft = makeMultilib("/micromipsel-r2-soft")
1330  .flag("+EL")
1331  .flag("+msoft-float")
1332  .flag("-mnan=2008")
1333  .flag("+mmicromips");
1334 
1335  auto O32 =
1336  makeMultilib("/lib").osSuffix("").flag("-mabi=n32").flag("-mabi=n64");
1337  auto N32 =
1338  makeMultilib("/lib32").osSuffix("").flag("+mabi=n32").flag("-mabi=n64");
1339  auto N64 =
1340  makeMultilib("/lib64").osSuffix("").flag("-mabi=n32").flag("+mabi=n64");
1341 
1342  MtiMipsMultilibsV2 =
1343  MultilibSet()
1344  .Either({BeHard, BeSoft, ElHard, ElSoft, BeHardNan, ElHardNan,
1345  BeHardNanUclibc, ElHardNanUclibc, BeHardUclibc,
1346  ElHardUclibc, ElMicroHardNan, ElMicroSoft})
1347  .Either(O32, N32, N64)
1348  .FilterOut(NonExistent)
1349  .setIncludeDirsCallback([](const Multilib &M) {
1350  return std::vector<std::string>({"/../../../../sysroot" +
1351  M.includeSuffix() +
1352  "/../usr/include"});
1353  })
1354  .setFilePathsCallback([](const Multilib &M) {
1355  return std::vector<std::string>(
1356  {"/../../../../mips-mti-linux-gnu/lib" + M.gccSuffix()});
1357  });
1358  }
1359  for (auto Candidate : {&MtiMipsMultilibsV1, &MtiMipsMultilibsV2}) {
1360  if (Candidate->select(Flags, Result.SelectedMultilib)) {
1361  Result.Multilibs = *Candidate;
1362  return true;
1363  }
1364  }
1365  return false;
1366 }
1367 
1369  FilterNonExistent &NonExistent,
1370  DetectedMultilibs &Result) {
1371  // CodeScape IMG toolchain v1.2 and early.
1372  MultilibSet ImgMultilibsV1;
1373  {
1374  auto Mips64r6 = makeMultilib("/mips64r6").flag("+m64").flag("-m32");
1375 
1376  auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB");
1377 
1378  auto MAbi64 =
1379  makeMultilib("/64").flag("+mabi=n64").flag("-mabi=n32").flag("-m32");
1380 
1381  ImgMultilibsV1 =
1382  MultilibSet()
1383  .Maybe(Mips64r6)
1384  .Maybe(MAbi64)
1385  .Maybe(LittleEndian)
1386  .FilterOut(NonExistent)
1387  .setIncludeDirsCallback([](const Multilib &M) {
1388  return std::vector<std::string>(
1389  {"/include", "/../../../../sysroot/usr/include"});
1390  });
1391  }
1392 
1393  // CodeScape IMG toolchain starting from v1.3.
1394  MultilibSet ImgMultilibsV2;
1395  {
1396  auto BeHard = makeMultilib("/mips-r6-hard")
1397  .flag("+EB")
1398  .flag("-msoft-float")
1399  .flag("-mmicromips");
1400  auto BeSoft = makeMultilib("/mips-r6-soft")
1401  .flag("+EB")
1402  .flag("+msoft-float")
1403  .flag("-mmicromips");
1404  auto ElHard = makeMultilib("/mipsel-r6-hard")
1405  .flag("+EL")
1406  .flag("-msoft-float")
1407  .flag("-mmicromips");
1408  auto ElSoft = makeMultilib("/mipsel-r6-soft")
1409  .flag("+EL")
1410  .flag("+msoft-float")
1411  .flag("-mmicromips");
1412  auto BeMicroHard = makeMultilib("/micromips-r6-hard")
1413  .flag("+EB")
1414  .flag("-msoft-float")
1415  .flag("+mmicromips");
1416  auto BeMicroSoft = makeMultilib("/micromips-r6-soft")
1417  .flag("+EB")
1418  .flag("+msoft-float")
1419  .flag("+mmicromips");
1420  auto ElMicroHard = makeMultilib("/micromipsel-r6-hard")
1421  .flag("+EL")
1422  .flag("-msoft-float")
1423  .flag("+mmicromips");
1424  auto ElMicroSoft = makeMultilib("/micromipsel-r6-soft")
1425  .flag("+EL")
1426  .flag("+msoft-float")
1427  .flag("+mmicromips");
1428 
1429  auto O32 =
1430  makeMultilib("/lib").osSuffix("").flag("-mabi=n32").flag("-mabi=n64");
1431  auto N32 =
1432  makeMultilib("/lib32").osSuffix("").flag("+mabi=n32").flag("-mabi=n64");
1433  auto N64 =
1434  makeMultilib("/lib64").osSuffix("").flag("-mabi=n32").flag("+mabi=n64");
1435 
1436  ImgMultilibsV2 =
1437  MultilibSet()
1438  .Either({BeHard, BeSoft, ElHard, ElSoft, BeMicroHard, BeMicroSoft,
1439  ElMicroHard, ElMicroSoft})
1440  .Either(O32, N32, N64)
1441  .FilterOut(NonExistent)
1442  .setIncludeDirsCallback([](const Multilib &M) {
1443  return std::vector<std::string>({"/../../../../sysroot" +
1444  M.includeSuffix() +
1445  "/../usr/include"});
1446  })
1447  .setFilePathsCallback([](const Multilib &M) {
1448  return std::vector<std::string>(
1449  {"/../../../../mips-img-linux-gnu/lib" + M.gccSuffix()});
1450  });
1451  }
1452  for (auto Candidate : {&ImgMultilibsV1, &ImgMultilibsV2}) {
1453  if (Candidate->select(Flags, Result.SelectedMultilib)) {
1454  Result.Multilibs = *Candidate;
1455  return true;
1456  }
1457  }
1458  return false;
1459 }
1460 
1462  const llvm::Triple &TargetTriple,
1463  StringRef Path, const ArgList &Args,
1464  DetectedMultilibs &Result) {
1465  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1466 
1467  StringRef CPUName;
1468  StringRef ABIName;
1469  tools::mips::getMipsCPUAndABI(Args, TargetTriple, CPUName, ABIName);
1470 
1471  llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
1472 
1473  Multilib::flags_list Flags;
1474  addMultilibFlag(TargetTriple.isMIPS32(), "m32", Flags);
1475  addMultilibFlag(TargetTriple.isMIPS64(), "m64", Flags);
1476  addMultilibFlag(isMips16(Args), "mips16", Flags);
1477  addMultilibFlag(CPUName == "mips32", "march=mips32", Flags);
1478  addMultilibFlag(CPUName == "mips32r2" || CPUName == "mips32r3" ||
1479  CPUName == "mips32r5" || CPUName == "p5600",
1480  "march=mips32r2", Flags);
1481  addMultilibFlag(CPUName == "mips32r6", "march=mips32r6", Flags);
1482  addMultilibFlag(CPUName == "mips64", "march=mips64", Flags);
1483  addMultilibFlag(CPUName == "mips64r2" || CPUName == "mips64r3" ||
1484  CPUName == "mips64r5" || CPUName == "octeon" ||
1485  CPUName == "octeon+",
1486  "march=mips64r2", Flags);
1487  addMultilibFlag(CPUName == "mips64r6", "march=mips64r6", Flags);
1488  addMultilibFlag(isMicroMips(Args), "mmicromips", Flags);
1489  addMultilibFlag(tools::mips::isUCLibc(Args), "muclibc", Flags);
1490  addMultilibFlag(tools::mips::isNaN2008(D, Args, TargetTriple), "mnan=2008",
1491  Flags);
1492  addMultilibFlag(ABIName == "n32", "mabi=n32", Flags);
1493  addMultilibFlag(ABIName == "n64", "mabi=n64", Flags);
1494  addMultilibFlag(isSoftFloatABI(Args), "msoft-float", Flags);
1495  addMultilibFlag(!isSoftFloatABI(Args), "mhard-float", Flags);
1496  addMultilibFlag(isMipsEL(TargetArch), "EL", Flags);
1497  addMultilibFlag(!isMipsEL(TargetArch), "EB", Flags);
1498 
1499  if (TargetTriple.isAndroid())
1500  return findMipsAndroidMultilibs(D.getVFS(), Path, Flags, NonExistent,
1501  Result);
1502 
1503  if (TargetTriple.getVendor() == llvm::Triple::MipsTechnologies &&
1504  TargetTriple.getOS() == llvm::Triple::Linux &&
1505  TargetTriple.getEnvironment() == llvm::Triple::UnknownEnvironment)
1506  return findMipsMuslMultilibs(Flags, NonExistent, Result);
1507 
1508  if (TargetTriple.getVendor() == llvm::Triple::MipsTechnologies &&
1509  TargetTriple.getOS() == llvm::Triple::Linux &&
1510  TargetTriple.isGNUEnvironment())
1511  return findMipsMtiMultilibs(Flags, NonExistent, Result);
1512 
1513  if (TargetTriple.getVendor() == llvm::Triple::ImaginationTechnologies &&
1514  TargetTriple.getOS() == llvm::Triple::Linux &&
1515  TargetTriple.isGNUEnvironment())
1516  return findMipsImgMultilibs(Flags, NonExistent, Result);
1517 
1518  if (findMipsCsMultilibs(Flags, NonExistent, Result))
1519  return true;
1520 
1521  // Fallback to the regular toolchain-tree structure.
1522  Multilib Default;
1523  Result.Multilibs.push_back(Default);
1524  Result.Multilibs.FilterOut(NonExistent);
1525 
1526  if (Result.Multilibs.select(Flags, Result.SelectedMultilib)) {
1527  Result.BiarchSibling = Multilib();
1528  return true;
1529  }
1530 
1531  return false;
1532 }
1533 
1534 static void findAndroidArmMultilibs(const Driver &D,
1535  const llvm::Triple &TargetTriple,
1536  StringRef Path, const ArgList &Args,
1537  DetectedMultilibs &Result) {
1538  // Find multilibs with subdirectories like armv7-a, thumb, armv7-a/thumb.
1539  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1540  Multilib ArmV7Multilib = makeMultilib("/armv7-a")
1541  .flag("+march=armv7-a")
1542  .flag("-mthumb");
1543  Multilib ThumbMultilib = makeMultilib("/thumb")
1544  .flag("-march=armv7-a")
1545  .flag("+mthumb");
1546  Multilib ArmV7ThumbMultilib = makeMultilib("/armv7-a/thumb")
1547  .flag("+march=armv7-a")
1548  .flag("+mthumb");
1549  Multilib DefaultMultilib = makeMultilib("")
1550  .flag("-march=armv7-a")
1551  .flag("-mthumb");
1552  MultilibSet AndroidArmMultilibs =
1553  MultilibSet()
1554  .Either(ThumbMultilib, ArmV7Multilib,
1555  ArmV7ThumbMultilib, DefaultMultilib)
1556  .FilterOut(NonExistent);
1557 
1558  Multilib::flags_list Flags;
1559  llvm::StringRef Arch = Args.getLastArgValue(options::OPT_march_EQ);
1560  bool IsArmArch = TargetTriple.getArch() == llvm::Triple::arm;
1561  bool IsThumbArch = TargetTriple.getArch() == llvm::Triple::thumb;
1562  bool IsV7SubArch = TargetTriple.getSubArch() == llvm::Triple::ARMSubArch_v7;
1563  bool IsThumbMode = IsThumbArch ||
1564  Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, false) ||
1565  (IsArmArch && llvm::ARM::parseArchISA(Arch) == llvm::ARM::ISAKind::THUMB);
1566  bool IsArmV7Mode = (IsArmArch || IsThumbArch) &&
1567  (llvm::ARM::parseArchVersion(Arch) == 7 ||
1568  (IsArmArch && Arch == "" && IsV7SubArch));
1569  addMultilibFlag(IsArmV7Mode, "march=armv7-a", Flags);
1570  addMultilibFlag(IsThumbMode, "mthumb", Flags);
1571 
1572  if (AndroidArmMultilibs.select(Flags, Result.SelectedMultilib))
1573  Result.Multilibs = AndroidArmMultilibs;
1574 }
1575 
1576 static bool findMSP430Multilibs(const Driver &D,
1577  const llvm::Triple &TargetTriple,
1578  StringRef Path, const ArgList &Args,
1579  DetectedMultilibs &Result) {
1580  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1581  Multilib WithoutExceptions = makeMultilib("/430").flag("-exceptions");
1582  Multilib WithExceptions = makeMultilib("/430/exceptions").flag("+exceptions");
1583 
1584  // FIXME: when clang starts to support msp430x ISA additional logic
1585  // to select between multilib must be implemented
1586  // Multilib MSP430xMultilib = makeMultilib("/large");
1587 
1588  Result.Multilibs.push_back(WithoutExceptions);
1589  Result.Multilibs.push_back(WithExceptions);
1590  Result.Multilibs.FilterOut(NonExistent);
1591 
1592  Multilib::flags_list Flags;
1593  addMultilibFlag(Args.hasFlag(options::OPT_fexceptions,
1594  options::OPT_fno_exceptions, false),
1595  "exceptions", Flags);
1596  if (Result.Multilibs.select(Flags, Result.SelectedMultilib))
1597  return true;
1598 
1599  return false;
1600 }
1601 
1602 static void findCSKYMultilibs(const Driver &D, const llvm::Triple &TargetTriple,
1603  StringRef Path, const ArgList &Args,
1604  DetectedMultilibs &Result) {
1605  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1606 
1607  tools::csky::FloatABI TheFloatABI = tools::csky::getCSKYFloatABI(D, Args);
1609 
1610  if (!Res)
1611  return;
1612  auto ARCHName = *Res;
1613 
1614  Multilib::flags_list Flags;
1615  addMultilibFlag(TheFloatABI == tools::csky::FloatABI::Hard, "hard-fp", Flags);
1616  addMultilibFlag(TheFloatABI == tools::csky::FloatABI::SoftFP, "soft-fp",
1617  Flags);
1618  addMultilibFlag(TheFloatABI == tools::csky::FloatABI::Soft, "soft", Flags);
1619  addMultilibFlag(ARCHName == "ck801", "march=ck801", Flags);
1620  addMultilibFlag(ARCHName == "ck802", "march=ck802", Flags);
1621  addMultilibFlag(ARCHName == "ck803", "march=ck803", Flags);
1622  addMultilibFlag(ARCHName == "ck804", "march=ck804", Flags);
1623  addMultilibFlag(ARCHName == "ck805", "march=ck805", Flags);
1624  addMultilibFlag(ARCHName == "ck807", "march=ck807", Flags);
1625  addMultilibFlag(ARCHName == "ck810", "march=ck810", Flags);
1626  addMultilibFlag(ARCHName == "ck810v", "march=ck810v", Flags);
1627  addMultilibFlag(ARCHName == "ck860", "march=ck860", Flags);
1628  addMultilibFlag(ARCHName == "ck860v", "march=ck860v", Flags);
1629 
1630  bool isBigEndian = false;
1631  if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
1632  options::OPT_mbig_endian))
1633  isBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
1634  addMultilibFlag(isBigEndian, "EB", Flags);
1635 
1636  auto HardFloat = makeMultilib("/hard-fp").flag("+hard-fp");
1637  auto SoftFpFloat = makeMultilib("/soft-fp").flag("+soft-fp");
1638  auto SoftFloat = makeMultilib("").flag("+soft");
1639  auto Arch801 = makeMultilib("/ck801").flag("+march=ck801");
1640  auto Arch802 = makeMultilib("/ck802").flag("+march=ck802");
1641  auto Arch803 = makeMultilib("/ck803").flag("+march=ck803");
1642  // CK804 use the same library as CK803
1643  auto Arch804 = makeMultilib("/ck803").flag("+march=ck804");
1644  auto Arch805 = makeMultilib("/ck805").flag("+march=ck805");
1645  auto Arch807 = makeMultilib("/ck807").flag("+march=ck807");
1646  auto Arch810 = makeMultilib("").flag("+march=ck810");
1647  auto Arch810v = makeMultilib("/ck810v").flag("+march=ck810v");
1648  auto Arch860 = makeMultilib("/ck860").flag("+march=ck860");
1649  auto Arch860v = makeMultilib("/ck860v").flag("+march=ck860v");
1650  auto BigEndian = makeMultilib("/big").flag("+EB");
1651 
1652  MultilibSet CSKYMultilibs =
1653  MultilibSet()
1654  .Maybe(BigEndian)
1655  .Either({Arch801, Arch802, Arch803, Arch804, Arch805, Arch807,
1656  Arch810, Arch810v, Arch860, Arch860v})
1657  .Either(HardFloat, SoftFpFloat, SoftFloat)
1658  .FilterOut(NonExistent);
1659 
1660  if (CSKYMultilibs.select(Flags, Result.SelectedMultilib))
1661  Result.Multilibs = CSKYMultilibs;
1662 }
1663 
1665  const llvm::Triple &TargetTriple,
1666  StringRef Path, const ArgList &Args,
1667  DetectedMultilibs &Result) {
1668  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1669  struct RiscvMultilib {
1670  StringRef march;
1671  StringRef mabi;
1672  };
1673  // currently only support the set of multilibs like riscv-gnu-toolchain does.
1674  // TODO: support MULTILIB_REUSE
1675  constexpr RiscvMultilib RISCVMultilibSet[] = {
1676  {"rv32i", "ilp32"}, {"rv32im", "ilp32"}, {"rv32iac", "ilp32"},
1677  {"rv32imac", "ilp32"}, {"rv32imafc", "ilp32f"}, {"rv64imac", "lp64"},
1678  {"rv64imafdc", "lp64d"}};
1679 
1680  std::vector<Multilib> Ms;
1681  for (auto Element : RISCVMultilibSet) {
1682  // multilib path rule is ${march}/${mabi}
1683  Ms.emplace_back(
1684  makeMultilib((Twine(Element.march) + "/" + Twine(Element.mabi)).str())
1685  .flag(Twine("+march=", Element.march).str())
1686  .flag(Twine("+mabi=", Element.mabi).str()));
1687  }
1688  MultilibSet RISCVMultilibs =
1689  MultilibSet()
1691  .FilterOut(NonExistent)
1692  .setFilePathsCallback([](const Multilib &M) {
1693  return std::vector<std::string>(
1694  {M.gccSuffix(),
1695  "/../../../../riscv64-unknown-elf/lib" + M.gccSuffix(),
1696  "/../../../../riscv32-unknown-elf/lib" + M.gccSuffix()});
1697  });
1698 
1699 
1700  Multilib::flags_list Flags;
1701  llvm::StringSet<> Added_ABIs;
1702  StringRef ABIName = tools::riscv::getRISCVABI(Args, TargetTriple);
1703  StringRef MArch = tools::riscv::getRISCVArch(Args, TargetTriple);
1704  for (auto Element : RISCVMultilibSet) {
1705  addMultilibFlag(MArch == Element.march,
1706  Twine("march=", Element.march).str().c_str(), Flags);
1707  if (!Added_ABIs.count(Element.mabi)) {
1708  Added_ABIs.insert(Element.mabi);
1709  addMultilibFlag(ABIName == Element.mabi,
1710  Twine("mabi=", Element.mabi).str().c_str(), Flags);
1711  }
1712  }
1713 
1714  if (RISCVMultilibs.select(Flags, Result.SelectedMultilib))
1715  Result.Multilibs = RISCVMultilibs;
1716 }
1717 
1718 static void findRISCVMultilibs(const Driver &D,
1719  const llvm::Triple &TargetTriple, StringRef Path,
1720  const ArgList &Args, DetectedMultilibs &Result) {
1721  if (TargetTriple.getOS() == llvm::Triple::UnknownOS)
1722  return findRISCVBareMetalMultilibs(D, TargetTriple, Path, Args, Result);
1723 
1724  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1725  Multilib Ilp32 = makeMultilib("lib32/ilp32").flag("+m32").flag("+mabi=ilp32");
1726  Multilib Ilp32f =
1727  makeMultilib("lib32/ilp32f").flag("+m32").flag("+mabi=ilp32f");
1728  Multilib Ilp32d =
1729  makeMultilib("lib32/ilp32d").flag("+m32").flag("+mabi=ilp32d");
1730  Multilib Lp64 = makeMultilib("lib64/lp64").flag("+m64").flag("+mabi=lp64");
1731  Multilib Lp64f = makeMultilib("lib64/lp64f").flag("+m64").flag("+mabi=lp64f");
1732  Multilib Lp64d = makeMultilib("lib64/lp64d").flag("+m64").flag("+mabi=lp64d");
1733  MultilibSet RISCVMultilibs =
1734  MultilibSet()
1735  .Either({Ilp32, Ilp32f, Ilp32d, Lp64, Lp64f, Lp64d})
1736  .FilterOut(NonExistent);
1737 
1738  Multilib::flags_list Flags;
1739  bool IsRV64 = TargetTriple.getArch() == llvm::Triple::riscv64;
1740  StringRef ABIName = tools::riscv::getRISCVABI(Args, TargetTriple);
1741 
1742  addMultilibFlag(!IsRV64, "m32", Flags);
1743  addMultilibFlag(IsRV64, "m64", Flags);
1744  addMultilibFlag(ABIName == "ilp32", "mabi=ilp32", Flags);
1745  addMultilibFlag(ABIName == "ilp32f", "mabi=ilp32f", Flags);
1746  addMultilibFlag(ABIName == "ilp32d", "mabi=ilp32d", Flags);
1747  addMultilibFlag(ABIName == "lp64", "mabi=lp64", Flags);
1748  addMultilibFlag(ABIName == "lp64f", "mabi=lp64f", Flags);
1749  addMultilibFlag(ABIName == "lp64d", "mabi=lp64d", Flags);
1750 
1751  if (RISCVMultilibs.select(Flags, Result.SelectedMultilib))
1752  Result.Multilibs = RISCVMultilibs;
1753 }
1754 
1755 static bool findBiarchMultilibs(const Driver &D,
1756  const llvm::Triple &TargetTriple,
1757  StringRef Path, const ArgList &Args,
1758  bool NeedsBiarchSuffix,
1759  DetectedMultilibs &Result) {
1760  Multilib Default;
1761 
1762  // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
1763  // in what would normally be GCCInstallPath and put the 64-bit
1764  // libs in a subdirectory named 64. The simple logic we follow is that
1765  // *if* there is a subdirectory of the right name with crtbegin.o in it,
1766  // we use that. If not, and if not a biarch triple alias, we look for
1767  // crtbegin.o without the subdirectory.
1768 
1769  StringRef Suff64 = "/64";
1770  // Solaris uses platform-specific suffixes instead of /64.
1771  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
1772  switch (TargetTriple.getArch()) {
1773  case llvm::Triple::x86:
1774  case llvm::Triple::x86_64:
1775  Suff64 = "/amd64";
1776  break;
1777  case llvm::Triple::sparc:
1778  case llvm::Triple::sparcv9:
1779  Suff64 = "/sparcv9";
1780  break;
1781  default:
1782  break;
1783  }
1784  }
1785 
1786  Multilib Alt64 = Multilib()
1787  .gccSuffix(Suff64)
1788  .includeSuffix(Suff64)
1789  .flag("-m32")
1790  .flag("+m64")
1791  .flag("-mx32");
1792  Multilib Alt32 = Multilib()
1793  .gccSuffix("/32")
1794  .includeSuffix("/32")
1795  .flag("+m32")
1796  .flag("-m64")
1797  .flag("-mx32");
1798  Multilib Altx32 = Multilib()
1799  .gccSuffix("/x32")
1800  .includeSuffix("/x32")
1801  .flag("-m32")
1802  .flag("-m64")
1803  .flag("+mx32");
1804 
1805  // GCC toolchain for IAMCU doesn't have crtbegin.o, so look for libgcc.a.
1806  FilterNonExistent NonExistent(
1807  Path, TargetTriple.isOSIAMCU() ? "/libgcc.a" : "/crtbegin.o", D.getVFS());
1808 
1809  // Determine default multilib from: 32, 64, x32
1810  // Also handle cases such as 64 on 32, 32 on 64, etc.
1811  enum { UNKNOWN, WANT32, WANT64, WANTX32 } Want = UNKNOWN;
1812  const bool IsX32 = TargetTriple.isX32();
1813  if (TargetTriple.isArch32Bit() && !NonExistent(Alt32))
1814  Want = WANT64;
1815  else if (TargetTriple.isArch64Bit() && IsX32 && !NonExistent(Altx32))
1816  Want = WANT64;
1817  else if (TargetTriple.isArch64Bit() && !IsX32 && !NonExistent(Alt64))
1818  Want = WANT32;
1819  else {
1820  if (TargetTriple.isArch32Bit())
1821  Want = NeedsBiarchSuffix ? WANT64 : WANT32;
1822  else if (IsX32)
1823  Want = NeedsBiarchSuffix ? WANT64 : WANTX32;
1824  else
1825  Want = NeedsBiarchSuffix ? WANT32 : WANT64;
1826  }
1827 
1828  if (Want == WANT32)
1829  Default.flag("+m32").flag("-m64").flag("-mx32");
1830  else if (Want == WANT64)
1831  Default.flag("-m32").flag("+m64").flag("-mx32");
1832  else if (Want == WANTX32)
1833  Default.flag("-m32").flag("-m64").flag("+mx32");
1834  else
1835  return false;
1836 
1837  Result.Multilibs.push_back(Default);
1838  Result.Multilibs.push_back(Alt64);
1839  Result.Multilibs.push_back(Alt32);
1840  Result.Multilibs.push_back(Altx32);
1841 
1842  Result.Multilibs.FilterOut(NonExistent);
1843 
1844  Multilib::flags_list Flags;
1845  addMultilibFlag(TargetTriple.isArch64Bit() && !IsX32, "m64", Flags);
1846  addMultilibFlag(TargetTriple.isArch32Bit(), "m32", Flags);
1847  addMultilibFlag(TargetTriple.isArch64Bit() && IsX32, "mx32", Flags);
1848 
1849  if (!Result.Multilibs.select(Flags, Result.SelectedMultilib))
1850  return false;
1851 
1852  if (Result.SelectedMultilib == Alt64 || Result.SelectedMultilib == Alt32 ||
1853  Result.SelectedMultilib == Altx32)
1854  Result.BiarchSibling = Default;
1855 
1856  return true;
1857 }
1858 
1859 /// Generic_GCC - A tool chain using the 'gcc' command to perform
1860 /// all subcommands; this relies on gcc translating the majority of
1861 /// command line options.
1862 
1863 /// Less-than for GCCVersion, implementing a Strict Weak Ordering.
1864 bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor,
1865  int RHSPatch,
1866  StringRef RHSPatchSuffix) const {
1867  if (Major != RHSMajor)
1868  return Major < RHSMajor;
1869  if (Minor != RHSMinor)
1870  return Minor < RHSMinor;
1871  if (Patch != RHSPatch) {
1872  // Note that versions without a specified patch sort higher than those with
1873  // a patch.
1874  if (RHSPatch == -1)
1875  return true;
1876  if (Patch == -1)
1877  return false;
1878 
1879  // Otherwise just sort on the patch itself.
1880  return Patch < RHSPatch;
1881  }
1882  if (PatchSuffix != RHSPatchSuffix) {
1883  // Sort empty suffixes higher.
1884  if (RHSPatchSuffix.empty())
1885  return true;
1886  if (PatchSuffix.empty())
1887  return false;
1888 
1889  // Provide a lexicographic sort to make this a total ordering.
1890  return PatchSuffix < RHSPatchSuffix;
1891  }
1892 
1893  // The versions are equal.
1894  return false;
1895 }
1896 
1897 /// Parse a GCCVersion object out of a string of text.
1898 ///
1899 /// This is the primary means of forming GCCVersion objects.
1900 /*static*/
1902  const GCCVersion BadVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
1903  std::pair<StringRef, StringRef> First = VersionText.split('.');
1904  std::pair<StringRef, StringRef> Second = First.second.split('.');
1905 
1906  GCCVersion GoodVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
1907  if (First.first.getAsInteger(10, GoodVersion.Major) || GoodVersion.Major < 0)
1908  return BadVersion;
1909  GoodVersion.MajorStr = First.first.str();
1910  if (First.second.empty())
1911  return GoodVersion;
1912  StringRef MinorStr = Second.first;
1913  if (Second.second.empty()) {
1914  if (size_t EndNumber = MinorStr.find_first_not_of("0123456789")) {
1915  GoodVersion.PatchSuffix = std::string(MinorStr.substr(EndNumber));
1916  MinorStr = MinorStr.slice(0, EndNumber);
1917  }
1918  }
1919  if (MinorStr.getAsInteger(10, GoodVersion.Minor) || GoodVersion.Minor < 0)
1920  return BadVersion;
1921  GoodVersion.MinorStr = MinorStr.str();
1922 
1923  // First look for a number prefix and parse that if present. Otherwise just
1924  // stash the entire patch string in the suffix, and leave the number
1925  // unspecified. This covers versions strings such as:
1926  // 5 (handled above)
1927  // 4.4
1928  // 4.4-patched
1929  // 4.4.0
1930  // 4.4.x
1931  // 4.4.2-rc4
1932  // 4.4.x-patched
1933  // And retains any patch number it finds.
1934  StringRef PatchText = Second.second;
1935  if (!PatchText.empty()) {
1936  if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
1937  // Try to parse the number and any suffix.
1938  if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1939  GoodVersion.Patch < 0)
1940  return BadVersion;
1941  GoodVersion.PatchSuffix = std::string(PatchText.substr(EndNumber));
1942  }
1943  }
1944 
1945  return GoodVersion;
1946 }
1947 
1948 static llvm::StringRef getGCCToolchainDir(const ArgList &Args,
1949  llvm::StringRef SysRoot) {
1950  const Arg *A = Args.getLastArg(clang::driver::options::OPT_gcc_toolchain);
1951  if (A)
1952  return A->getValue();
1953 
1954  // If we have a SysRoot, ignore GCC_INSTALL_PREFIX.
1955  // GCC_INSTALL_PREFIX specifies the gcc installation for the default
1956  // sysroot and is likely not valid with a different sysroot.
1957  if (!SysRoot.empty())
1958  return "";
1959 
1960  return GCC_INSTALL_PREFIX;
1961 }
1962 
1963 /// Initialize a GCCInstallationDetector from the driver.
1964 ///
1965 /// This performs all of the autodetection and sets up the various paths.
1966 /// Once constructed, a GCCInstallationDetector is essentially immutable.
1967 ///
1968 /// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1969 /// should instead pull the target out of the driver. This is currently
1970 /// necessary because the driver doesn't store the final version of the target
1971 /// triple.
1973  const llvm::Triple &TargetTriple, const ArgList &Args,
1974  ArrayRef<std::string> ExtraTripleAliases) {
1975  llvm::Triple BiarchVariantTriple = TargetTriple.isArch32Bit()
1976  ? TargetTriple.get64BitArchVariant()
1977  : TargetTriple.get32BitArchVariant();
1978  // The library directories which may contain GCC installations.
1979  SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
1980  // The compatible GCC triples for this particular architecture.
1981  SmallVector<StringRef, 16> CandidateTripleAliases;
1982  SmallVector<StringRef, 16> CandidateBiarchTripleAliases;
1983  CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1984  CandidateTripleAliases, CandidateBiarchLibDirs,
1985  CandidateBiarchTripleAliases);
1986 
1987  // Compute the set of prefixes for our search.
1988  SmallVector<std::string, 8> Prefixes;
1989  StringRef GCCToolchainDir = getGCCToolchainDir(Args, D.SysRoot);
1990  if (GCCToolchainDir != "") {
1991  if (GCCToolchainDir.back() == '/')
1992  GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
1993 
1994  Prefixes.push_back(std::string(GCCToolchainDir));
1995  } else {
1996  // If we have a SysRoot, try that first.
1997  if (!D.SysRoot.empty()) {
1998  Prefixes.push_back(D.SysRoot);
1999  AddDefaultGCCPrefixes(TargetTriple, Prefixes, D.SysRoot);
2000  }
2001 
2002  // Then look for gcc installed alongside clang.
2003  Prefixes.push_back(D.InstalledDir + "/..");
2004 
2005  // Next, look for prefix(es) that correspond to distribution-supplied gcc
2006  // installations.
2007  if (D.SysRoot.empty()) {
2008  // Typically /usr.
2009  AddDefaultGCCPrefixes(TargetTriple, Prefixes, D.SysRoot);
2010  }
2011 
2012  // Try to respect gcc-config on Gentoo if --gcc-toolchain is not provided.
2013  // This avoids accidentally enforcing the system GCC version when using a
2014  // custom toolchain.
2015  SmallVector<StringRef, 16> GentooTestTriples;
2016  // Try to match an exact triple as target triple first.
2017  // e.g. crossdev -S x86_64-gentoo-linux-gnu will install gcc libs for
2018  // x86_64-gentoo-linux-gnu. But "clang -target x86_64-gentoo-linux-gnu"
2019  // may pick the libraries for x86_64-pc-linux-gnu even when exact matching
2020  // triple x86_64-gentoo-linux-gnu is present.
2021  GentooTestTriples.push_back(TargetTriple.str());
2022  // Check rest of triples.
2023  GentooTestTriples.append(ExtraTripleAliases.begin(),
2024  ExtraTripleAliases.end());
2025  GentooTestTriples.append(CandidateTripleAliases.begin(),
2026  CandidateTripleAliases.end());
2027  if (ScanGentooConfigs(TargetTriple, Args, GentooTestTriples,
2028  CandidateBiarchTripleAliases))
2029  return;
2030  }
2031 
2032  // Loop over the various components which exist and select the best GCC
2033  // installation available. GCC installs are ranked by version number.
2034  const GCCVersion VersionZero = GCCVersion::Parse("0.0.0");
2035  Version = VersionZero;
2036  for (const std::string &Prefix : Prefixes) {
2037  auto &VFS = D.getVFS();
2038  if (!VFS.exists(Prefix))
2039  continue;
2040  for (StringRef Suffix : CandidateLibDirs) {
2041  const std::string LibDir = Prefix + Suffix.str();
2042  if (!VFS.exists(LibDir))
2043  continue;
2044  // Maybe filter out <libdir>/gcc and <libdir>/gcc-cross.
2045  bool GCCDirExists = VFS.exists(LibDir + "/gcc");
2046  bool GCCCrossDirExists = VFS.exists(LibDir + "/gcc-cross");
2047  // Try to match the exact target triple first.
2048  ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, TargetTriple.str(),
2049  false, GCCDirExists, GCCCrossDirExists);
2050  // Try rest of possible triples.
2051  for (StringRef Candidate : ExtraTripleAliases) // Try these first.
2052  ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate, false,
2053  GCCDirExists, GCCCrossDirExists);
2054  for (StringRef Candidate : CandidateTripleAliases)
2055  ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate, false,
2056  GCCDirExists, GCCCrossDirExists);
2057  }
2058  for (StringRef Suffix : CandidateBiarchLibDirs) {
2059  const std::string LibDir = Prefix + Suffix.str();
2060  if (!VFS.exists(LibDir))
2061  continue;
2062  bool GCCDirExists = VFS.exists(LibDir + "/gcc");
2063  bool GCCCrossDirExists = VFS.exists(LibDir + "/gcc-cross");
2064  for (StringRef Candidate : CandidateBiarchTripleAliases)
2065  ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate, true,
2066  GCCDirExists, GCCCrossDirExists);
2067  }
2068 
2069  // Skip other prefixes once a GCC installation is found.
2070  if (Version > VersionZero)
2071  break;
2072  }
2073 }
2074 
2075 void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const {
2076  for (const auto &InstallPath : CandidateGCCInstallPaths)
2077  OS << "Found candidate GCC installation: " << InstallPath << "\n";
2078 
2079  if (!GCCInstallPath.empty())
2080  OS << "Selected GCC installation: " << GCCInstallPath << "\n";
2081 
2082  for (const auto &Multilib : Multilibs)
2083  OS << "Candidate multilib: " << Multilib << "\n";
2084 
2085  if (Multilibs.size() != 0 || !SelectedMultilib.isDefault())
2086  OS << "Selected multilib: " << SelectedMultilib << "\n";
2087 }
2088 
2090  if (BiarchSibling.hasValue()) {
2091  M = BiarchSibling.getValue();
2092  return true;
2093  }
2094  return false;
2095 }
2096 
2097 void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
2098  const llvm::Triple &TargetTriple, SmallVectorImpl<std::string> &Prefixes,
2099  StringRef SysRoot) {
2100  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
2101  // Solaris is a special case.
2102  // The GCC installation is under
2103  // /usr/gcc/<major>.<minor>/lib/gcc/<triple>/<major>.<minor>.<patch>/
2104  // so we need to find those /usr/gcc/*/lib/gcc libdirs and go with
2105  // /usr/gcc/<version> as a prefix.
2106 
2107  std::string PrefixDir = SysRoot.str() + "/usr/gcc";
2108  std::error_code EC;
2109  for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(PrefixDir, EC),
2110  LE;
2111  !EC && LI != LE; LI = LI.increment(EC)) {
2112  StringRef VersionText = llvm::sys::path::filename(LI->path());
2113  GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
2114 
2115  // Filter out obviously bad entries.
2116  if (CandidateVersion.Major == -1 || CandidateVersion.isOlderThan(4, 1, 1))
2117  continue;
2118 
2119  std::string CandidatePrefix = PrefixDir + "/" + VersionText.str();
2120  std::string CandidateLibPath = CandidatePrefix + "/lib/gcc";
2121  if (!D.getVFS().exists(CandidateLibPath))
2122  continue;
2123 
2124  Prefixes.push_back(CandidatePrefix);
2125  }
2126  return;
2127  }
2128 
2129  // For Linux, if --sysroot is not specified, look for RHEL/CentOS devtoolsets
2130  // and gcc-toolsets.
2131  if (SysRoot.empty() && TargetTriple.getOS() == llvm::Triple::Linux &&
2132  D.getVFS().exists("/opt/rh")) {
2133  Prefixes.push_back("/opt/rh/gcc-toolset-11/root/usr");
2134  Prefixes.push_back("/opt/rh/gcc-toolset-10/root/usr");
2135  Prefixes.push_back("/opt/rh/devtoolset-11/root/usr");
2136  Prefixes.push_back("/opt/rh/devtoolset-10/root/usr");
2137  Prefixes.push_back("/opt/rh/devtoolset-9/root/usr");
2138  Prefixes.push_back("/opt/rh/devtoolset-8/root/usr");
2139  Prefixes.push_back("/opt/rh/devtoolset-7/root/usr");
2140  Prefixes.push_back("/opt/rh/devtoolset-6/root/usr");
2141  Prefixes.push_back("/opt/rh/devtoolset-4/root/usr");
2142  Prefixes.push_back("/opt/rh/devtoolset-3/root/usr");
2143  Prefixes.push_back("/opt/rh/devtoolset-2/root/usr");
2144  }
2145 
2146  // Fall back to /usr which is used by most non-Solaris systems.
2147  Prefixes.push_back(SysRoot.str() + "/usr");
2148 }
2149 
2150 /*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
2151  const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
2152  SmallVectorImpl<StringRef> &LibDirs,
2153  SmallVectorImpl<StringRef> &TripleAliases,
2154  SmallVectorImpl<StringRef> &BiarchLibDirs,
2155  SmallVectorImpl<StringRef> &BiarchTripleAliases) {
2156  // Declare a bunch of static data sets that we'll select between below. These
2157  // are specifically designed to always refer to string literals to avoid any
2158  // lifetime or initialization issues.
2159  //
2160  // The *Triples variables hard code some triples so that, for example,
2161  // --target=aarch64 (incomplete triple) can detect lib/aarch64-linux-gnu.
2162  // They are not needed when the user has correct LLVM_DEFAULT_TARGET_TRIPLE
2163  // and always uses the full --target (e.g. --target=aarch64-linux-gnu). The
2164  // lists should shrink over time. Please don't add more elements to *Triples.
2165  static const char *const AArch64LibDirs[] = {"/lib64", "/lib"};
2166  static const char *const AArch64Triples[] = {
2167  "aarch64-none-linux-gnu", "aarch64-linux-gnu", "aarch64-redhat-linux",
2168  "aarch64-suse-linux"};
2169  static const char *const AArch64beLibDirs[] = {"/lib"};
2170  static const char *const AArch64beTriples[] = {"aarch64_be-none-linux-gnu",
2171  "aarch64_be-linux-gnu"};
2172 
2173  static const char *const ARMLibDirs[] = {"/lib"};
2174  static const char *const ARMTriples[] = {"arm-linux-gnueabi"};
2175  static const char *const ARMHFTriples[] = {"arm-linux-gnueabihf",
2176  "armv7hl-redhat-linux-gnueabi",
2177  "armv6hl-suse-linux-gnueabi",
2178  "armv7hl-suse-linux-gnueabi"};
2179  static const char *const ARMebLibDirs[] = {"/lib"};
2180  static const char *const ARMebTriples[] = {"armeb-linux-gnueabi"};
2181  static const char *const ARMebHFTriples[] = {
2182  "armeb-linux-gnueabihf", "armebv7hl-redhat-linux-gnueabi"};
2183 
2184  static const char *const AVRLibDirs[] = {"/lib"};
2185  static const char *const AVRTriples[] = {"avr"};
2186 
2187  static const char *const CSKYLibDirs[] = {"/lib"};
2188  static const char *const CSKYTriples[] = {
2189  "csky-linux-gnuabiv2", "csky-linux-uclibcabiv2", "csky-elf-noneabiv2"};
2190 
2191  static const char *const X86_64LibDirs[] = {"/lib64", "/lib"};
2192  static const char *const X86_64Triples[] = {
2193  "x86_64-linux-gnu", "x86_64-unknown-linux-gnu",
2194  "x86_64-pc-linux-gnu", "x86_64-redhat-linux6E",
2195  "x86_64-redhat-linux", "x86_64-suse-linux",
2196  "x86_64-manbo-linux-gnu", "x86_64-linux-gnu",
2197  "x86_64-slackware-linux", "x86_64-unknown-linux",
2198  "x86_64-amazon-linux"};
2199  static const char *const X32Triples[] = {"x86_64-linux-gnux32",
2200  "x86_64-pc-linux-gnux32"};
2201  static const char *const X32LibDirs[] = {"/libx32", "/lib"};
2202  static const char *const X86LibDirs[] = {"/lib32", "/lib"};
2203  static const char *const X86Triples[] = {
2204  "i586-linux-gnu", "i686-linux-gnu", "i686-pc-linux-gnu",
2205  "i386-redhat-linux6E", "i686-redhat-linux", "i386-redhat-linux",
2206  "i586-suse-linux", "i686-montavista-linux", "i686-gnu",
2207  };
2208 
2209  static const char *const M68kLibDirs[] = {"/lib"};
2210  static const char *const M68kTriples[] = {
2211  "m68k-linux-gnu", "m68k-unknown-linux-gnu", "m68k-suse-linux"};
2212 
2213  static const char *const MIPSLibDirs[] = {"/libo32", "/lib"};
2214  static const char *const MIPSTriples[] = {
2215  "mips-linux-gnu", "mips-mti-linux", "mips-mti-linux-gnu",
2216  "mips-img-linux-gnu", "mipsisa32r6-linux-gnu"};
2217  static const char *const MIPSELLibDirs[] = {"/libo32", "/lib"};
2218  static const char *const MIPSELTriples[] = {
2219  "mipsel-linux-gnu", "mips-img-linux-gnu", "mipsisa32r6el-linux-gnu"};
2220 
2221  static const char *const MIPS64LibDirs[] = {"/lib64", "/lib"};
2222  static const char *const MIPS64Triples[] = {
2223  "mips64-linux-gnu", "mips-mti-linux-gnu",
2224  "mips-img-linux-gnu", "mips64-linux-gnuabi64",
2225  "mipsisa64r6-linux-gnu", "mipsisa64r6-linux-gnuabi64"};
2226  static const char *const MIPS64ELLibDirs[] = {"/lib64", "/lib"};
2227  static const char *const MIPS64ELTriples[] = {
2228  "mips64el-linux-gnu", "mips-mti-linux-gnu",
2229  "mips-img-linux-gnu", "mips64el-linux-gnuabi64",
2230  "mipsisa64r6el-linux-gnu", "mipsisa64r6el-linux-gnuabi64"};
2231 
2232  static const char *const MIPSN32LibDirs[] = {"/lib32"};
2233  static const char *const MIPSN32Triples[] = {"mips64-linux-gnuabin32",
2234  "mipsisa64r6-linux-gnuabin32"};
2235  static const char *const MIPSN32ELLibDirs[] = {"/lib32"};
2236  static const char *const MIPSN32ELTriples[] = {
2237  "mips64el-linux-gnuabin32", "mipsisa64r6el-linux-gnuabin32"};
2238 
2239  static const char *const MSP430LibDirs[] = {"/lib"};
2240  static const char *const MSP430Triples[] = {"msp430-elf"};
2241 
2242  static const char *const PPCLibDirs[] = {"/lib32", "/lib"};
2243  static const char *const PPCTriples[] = {
2244  "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
2245  // On 32-bit PowerPC systems running SUSE Linux, gcc is configured as a
2246  // 64-bit compiler which defaults to "-m32", hence "powerpc64-suse-linux".
2247  "powerpc64-suse-linux", "powerpc-montavista-linuxspe"};
2248  static const char *const PPCLELibDirs[] = {"/lib32", "/lib"};
2249  static const char *const PPCLETriples[] = {"powerpcle-linux-gnu",
2250  "powerpcle-unknown-linux-gnu",
2251  "powerpcle-linux-musl"};
2252 
2253  static const char *const PPC64LibDirs[] = {"/lib64", "/lib"};
2254  static const char *const PPC64Triples[] = {
2255  "powerpc64-linux-gnu", "powerpc64-unknown-linux-gnu",
2256  "powerpc64-suse-linux", "ppc64-redhat-linux"};
2257  static const char *const PPC64LELibDirs[] = {"/lib64", "/lib"};
2258  static const char *const PPC64LETriples[] = {
2259  "powerpc64le-linux-gnu", "powerpc64le-unknown-linux-gnu",
2260  "powerpc64le-none-linux-gnu", "powerpc64le-suse-linux",
2261  "ppc64le-redhat-linux"};
2262 
2263  static const char *const RISCV32LibDirs[] = {"/lib32", "/lib"};
2264  static const char *const RISCV32Triples[] = {"riscv32-unknown-linux-gnu",
2265  "riscv32-linux-gnu",
2266  "riscv32-unknown-elf"};
2267  static const char *const RISCV64LibDirs[] = {"/lib64", "/lib"};
2268  static const char *const RISCV64Triples[] = {"riscv64-unknown-linux-gnu",
2269  "riscv64-linux-gnu",
2270  "riscv64-unknown-elf"};
2271 
2272  static const char *const SPARCv8LibDirs[] = {"/lib32", "/lib"};
2273  static const char *const SPARCv8Triples[] = {"sparc-linux-gnu",
2274  "sparcv8-linux-gnu"};
2275  static const char *const SPARCv9LibDirs[] = {"/lib64", "/lib"};
2276  static const char *const SPARCv9Triples[] = {"sparc64-linux-gnu",
2277  "sparcv9-linux-gnu"};
2278 
2279  static const char *const SystemZLibDirs[] = {"/lib64", "/lib"};
2280  static const char *const SystemZTriples[] = {
2281  "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
2282  "s390x-suse-linux", "s390x-redhat-linux"};
2283 
2284 
2285  using std::begin;
2286  using std::end;
2287 
2288  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
2289  static const char *const SolarisLibDirs[] = {"/lib"};
2290  static const char *const SolarisSparcV8Triples[] = {
2291  "sparc-sun-solaris2.11", "sparc-sun-solaris2.12"};
2292  static const char *const SolarisSparcV9Triples[] = {
2293  "sparcv9-sun-solaris2.11", "sparcv9-sun-solaris2.12"};
2294  static const char *const SolarisX86Triples[] = {"i386-pc-solaris2.11",
2295  "i386-pc-solaris2.12"};
2296  static const char *const SolarisX86_64Triples[] = {"x86_64-pc-solaris2.11",
2297  "x86_64-pc-solaris2.12"};
2298  LibDirs.append(begin(SolarisLibDirs), end(SolarisLibDirs));
2299  BiarchLibDirs.append(begin(SolarisLibDirs), end(SolarisLibDirs));
2300  switch (TargetTriple.getArch()) {
2301  case llvm::Triple::x86:
2302  TripleAliases.append(begin(SolarisX86Triples), end(SolarisX86Triples));
2303  BiarchTripleAliases.append(begin(SolarisX86_64Triples),
2304  end(SolarisX86_64Triples));
2305  break;
2306  case llvm::Triple::x86_64:
2307  TripleAliases.append(begin(SolarisX86_64Triples),
2308  end(SolarisX86_64Triples));
2309  BiarchTripleAliases.append(begin(SolarisX86Triples),
2310  end(SolarisX86Triples));
2311  break;
2312  case llvm::Triple::sparc:
2313  TripleAliases.append(begin(SolarisSparcV8Triples),
2314  end(SolarisSparcV8Triples));
2315  BiarchTripleAliases.append(begin(SolarisSparcV9Triples),
2316  end(SolarisSparcV9Triples));
2317  break;
2318  case llvm::Triple::sparcv9:
2319  TripleAliases.append(begin(SolarisSparcV9Triples),
2320  end(SolarisSparcV9Triples));
2321  BiarchTripleAliases.append(begin(SolarisSparcV8Triples),
2322  end(SolarisSparcV8Triples));
2323  break;
2324  default:
2325  break;
2326  }
2327  return;
2328  }
2329 
2330  // Android targets should not use GNU/Linux tools or libraries.
2331  if (TargetTriple.isAndroid()) {
2332  static const char *const AArch64AndroidTriples[] = {
2333  "aarch64-linux-android"};
2334  static const char *const ARMAndroidTriples[] = {"arm-linux-androideabi"};
2335  static const char *const MIPSELAndroidTriples[] = {"mipsel-linux-android"};
2336  static const char *const MIPS64ELAndroidTriples[] = {
2337  "mips64el-linux-android"};
2338  static const char *const X86AndroidTriples[] = {"i686-linux-android"};
2339  static const char *const X86_64AndroidTriples[] = {"x86_64-linux-android"};
2340 
2341  switch (TargetTriple.getArch()) {
2342  case llvm::Triple::aarch64:
2343  LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
2344  TripleAliases.append(begin(AArch64AndroidTriples),
2345  end(AArch64AndroidTriples));
2346  break;
2347  case llvm::Triple::arm:
2348  case llvm::Triple::thumb:
2349  LibDirs.append(begin(ARMLibDirs), end(ARMLibDirs));
2350  TripleAliases.append(begin(ARMAndroidTriples), end(ARMAndroidTriples));
2351  break;
2352  case llvm::Triple::mipsel:
2353  LibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
2354  TripleAliases.append(begin(MIPSELAndroidTriples),
2355  end(MIPSELAndroidTriples));
2356  BiarchLibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
2357  BiarchTripleAliases.append(begin(MIPS64ELAndroidTriples),
2358  end(MIPS64ELAndroidTriples));
2359  break;
2360  case llvm::Triple::mips64el:
2361  LibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
2362  TripleAliases.append(begin(MIPS64ELAndroidTriples),
2363  end(MIPS64ELAndroidTriples));
2364  BiarchLibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
2365  BiarchTripleAliases.append(begin(MIPSELAndroidTriples),
2366  end(MIPSELAndroidTriples));
2367  break;
2368  case llvm::Triple::x86_64:
2369  LibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
2370  TripleAliases.append(begin(X86_64AndroidTriples),
2371  end(X86_64AndroidTriples));
2372  BiarchLibDirs.append(begin(X86LibDirs), end(X86LibDirs));
2373  BiarchTripleAliases.append(begin(X86AndroidTriples),
2374  end(X86AndroidTriples));
2375  break;
2376  case llvm::Triple::x86:
2377  LibDirs.append(begin(X86LibDirs), end(X86LibDirs));
2378  TripleAliases.append(begin(X86AndroidTriples), end(X86AndroidTriples));
2379  BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
2380  BiarchTripleAliases.append(begin(X86_64AndroidTriples),
2381  end(X86_64AndroidTriples));
2382  break;
2383  default:
2384  break;
2385  }
2386 
2387  return;
2388  }
2389 
2390  switch (TargetTriple.getArch()) {
2391  case llvm::Triple::aarch64:
2392  LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
2393  TripleAliases.append(begin(AArch64Triples), end(AArch64Triples));
2394  BiarchLibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
2395  BiarchTripleAliases.append(begin(AArch64Triples), end(AArch64Triples));
2396  break;
2397  case llvm::Triple::aarch64_be:
2398  LibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs));
2399  TripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples));
2400  BiarchLibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs));
2401  BiarchTripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples));
2402  break;
2403  case llvm::Triple::arm:
2404  case llvm::Triple::thumb:
2405  LibDirs.append(begin(ARMLibDirs), end(ARMLibDirs));
2406  if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2407  TripleAliases.append(begin(ARMHFTriples), end(ARMHFTriples));
2408  } else {
2409  TripleAliases.append(begin(ARMTriples), end(ARMTriples));
2410  }
2411  break;
2412  case llvm::Triple::armeb:
2413  case llvm::Triple::thumbeb:
2414  LibDirs.append(begin(ARMebLibDirs), end(ARMebLibDirs));
2415  if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2416  TripleAliases.append(begin(ARMebHFTriples), end(ARMebHFTriples));
2417  } else {
2418  TripleAliases.append(begin(ARMebTriples), end(ARMebTriples));
2419  }
2420  break;
2421  case llvm::Triple::avr:
2422  LibDirs.append(begin(AVRLibDirs), end(AVRLibDirs));
2423  TripleAliases.append(begin(AVRTriples), end(AVRTriples));
2424  break;
2425  case llvm::Triple::csky:
2426  LibDirs.append(begin(CSKYLibDirs), end(CSKYLibDirs));
2427  TripleAliases.append(begin(CSKYTriples), end(CSKYTriples));
2428  break;
2429  case llvm::Triple::x86_64:
2430  if (TargetTriple.isX32()) {
2431  LibDirs.append(begin(X32LibDirs), end(X32LibDirs));
2432  TripleAliases.append(begin(X32Triples), end(X32Triples));
2433  BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
2434  BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
2435  } else {
2436  LibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
2437  TripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
2438  BiarchLibDirs.append(begin(X32LibDirs), end(X32LibDirs));
2439  BiarchTripleAliases.append(begin(X32Triples), end(X32Triples));
2440  }
2441  BiarchLibDirs.append(begin(X86LibDirs), end(X86LibDirs));
2442  BiarchTripleAliases.append(begin(X86Triples), end(X86Triples));
2443  break;
2444  case llvm::Triple::x86:
2445  LibDirs.append(begin(X86LibDirs), end(X86LibDirs));
2446  // MCU toolchain is 32 bit only and its triple alias is TargetTriple
2447  // itself, which will be appended below.
2448  if (!TargetTriple.isOSIAMCU()) {
2449  TripleAliases.append(begin(X86Triples), end(X86Triples));
2450  BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
2451  BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
2452  BiarchLibDirs.append(begin(X32LibDirs), end(X32LibDirs));
2453  BiarchTripleAliases.append(begin(X32Triples), end(X32Triples));
2454  }
2455  break;
2456  case llvm::Triple::m68k:
2457  LibDirs.append(begin(M68kLibDirs), end(M68kLibDirs));
2458  TripleAliases.append(begin(M68kTriples), end(M68kTriples));
2459  break;
2460  case llvm::Triple::mips:
2461  LibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs));
2462  TripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2463  BiarchLibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs));
2464  BiarchTripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples));
2465  BiarchLibDirs.append(begin(MIPSN32LibDirs), end(MIPSN32LibDirs));
2466  BiarchTripleAliases.append(begin(MIPSN32Triples), end(MIPSN32Triples));
2467  break;
2468  case llvm::Triple::mipsel:
2469  LibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
2470  TripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples));
2471  TripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2472  BiarchLibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
2473  BiarchTripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples));
2474  BiarchLibDirs.append(begin(MIPSN32ELLibDirs), end(MIPSN32ELLibDirs));
2475  BiarchTripleAliases.append(begin(MIPSN32ELTriples), end(MIPSN32ELTriples));
2476  break;
2477  case llvm::Triple::mips64:
2478  LibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs));
2479  TripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples));
2480  BiarchLibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs));
2481  BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2482  BiarchLibDirs.append(begin(MIPSN32LibDirs), end(MIPSN32LibDirs));
2483  BiarchTripleAliases.append(begin(MIPSN32Triples), end(MIPSN32Triples));
2484  break;
2485  case llvm::Triple::mips64el:
2486  LibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
2487  TripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples));
2488  BiarchLibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
2489  BiarchTripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples));
2490  BiarchLibDirs.append(begin(MIPSN32ELLibDirs), end(MIPSN32ELLibDirs));
2491  BiarchTripleAliases.append(begin(MIPSN32ELTriples), end(MIPSN32ELTriples));
2492  BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2493  break;
2494  case llvm::Triple::msp430:
2495  LibDirs.append(begin(MSP430LibDirs), end(MSP430LibDirs));
2496  TripleAliases.append(begin(MSP430Triples), end(MSP430Triples));
2497  break;
2498  case llvm::Triple::ppc:
2499  LibDirs.append(begin(PPCLibDirs), end(PPCLibDirs));
2500  TripleAliases.append(begin(PPCTriples), end(PPCTriples));
2501  BiarchLibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs));
2502  BiarchTripleAliases.append(begin(PPC64Triples), end(PPC64Triples));
2503  break;
2504  case llvm::Triple::ppcle:
2505  LibDirs.append(begin(PPCLELibDirs), end(PPCLELibDirs));
2506  TripleAliases.append(begin(PPCLETriples), end(PPCLETriples));
2507  BiarchLibDirs.append(begin(PPC64LELibDirs), end(PPC64LELibDirs));
2508  BiarchTripleAliases.append(begin(PPC64LETriples), end(PPC64LETriples));
2509  break;
2510  case llvm::Triple::ppc64:
2511  LibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs));
2512  TripleAliases.append(begin(PPC64Triples), end(PPC64Triples));
2513  BiarchLibDirs.append(begin(PPCLibDirs), end(PPCLibDirs));
2514  BiarchTripleAliases.append(begin(PPCTriples), end(PPCTriples));
2515  break;
2516  case llvm::Triple::ppc64le:
2517  LibDirs.append(begin(PPC64LELibDirs), end(PPC64LELibDirs));
2518  TripleAliases.append(begin(PPC64LETriples), end(PPC64LETriples));
2519  BiarchLibDirs.append(begin(PPCLELibDirs), end(PPCLELibDirs));
2520  BiarchTripleAliases.append(begin(PPCLETriples), end(PPCLETriples));
2521  break;
2522  case llvm::Triple::riscv32:
2523  LibDirs.append(begin(RISCV32LibDirs), end(RISCV32LibDirs));
2524  TripleAliases.append(begin(RISCV32Triples), end(RISCV32Triples));
2525  BiarchLibDirs.append(begin(RISCV64LibDirs), end(RISCV64LibDirs));
2526  BiarchTripleAliases.append(begin(RISCV64Triples), end(RISCV64Triples));
2527  break;
2528  case llvm::Triple::riscv64:
2529  LibDirs.append(begin(RISCV64LibDirs), end(RISCV64LibDirs));
2530  TripleAliases.append(begin(RISCV64Triples), end(RISCV64Triples));
2531  BiarchLibDirs.append(begin(RISCV32LibDirs), end(RISCV32LibDirs));
2532  BiarchTripleAliases.append(begin(RISCV32Triples), end(RISCV32Triples));
2533  break;
2534  case llvm::Triple::sparc:
2535  case llvm::Triple::sparcel:
2536  LibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs));
2537  TripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples));
2538  BiarchLibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs));
2539  BiarchTripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples));
2540  break;
2541  case llvm::Triple::sparcv9:
2542  LibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs));
2543  TripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples));
2544  BiarchLibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs));
2545  BiarchTripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples));
2546  break;
2547  case llvm::Triple::systemz:
2548  LibDirs.append(begin(SystemZLibDirs), end(SystemZLibDirs));
2549  TripleAliases.append(begin(SystemZTriples), end(SystemZTriples));
2550  break;
2551  default:
2552  // By default, just rely on the standard lib directories and the original
2553  // triple.
2554  break;
2555  }
2556 
2557  // Always append the drivers target triple to the end, in case it doesn't
2558  // match any of our aliases.
2559  TripleAliases.push_back(TargetTriple.str());
2560 
2561  // Also include the multiarch variant if it's different.
2562  if (TargetTriple.str() != BiarchTriple.str())
2563  BiarchTripleAliases.push_back(BiarchTriple.str());
2564 }
2565 
2566 bool Generic_GCC::GCCInstallationDetector::ScanGCCForMultilibs(
2567  const llvm::Triple &TargetTriple, const ArgList &Args,
2568  StringRef Path, bool NeedsBiarchSuffix) {
2569  llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
2570  DetectedMultilibs Detected;
2571 
2572  // Android standalone toolchain could have multilibs for ARM and Thumb.
2573  // Debian mips multilibs behave more like the rest of the biarch ones,
2574  // so handle them there
2575  if (isArmOrThumbArch(TargetArch) && TargetTriple.isAndroid()) {
2576  // It should also work without multilibs in a simplified toolchain.
2577  findAndroidArmMultilibs(D, TargetTriple, Path, Args, Detected);
2578  } else if (TargetTriple.isCSKY()) {
2579  findCSKYMultilibs(D, TargetTriple, Path, Args, Detected);
2580  } else if (TargetTriple.isMIPS()) {
2581  if (!findMIPSMultilibs(D, TargetTriple, Path, Args, Detected))
2582  return false;
2583  } else if (TargetTriple.isRISCV()) {
2584  findRISCVMultilibs(D, TargetTriple, Path, Args, Detected);
2585  } else if (isMSP430(TargetArch)) {
2586  findMSP430Multilibs(D, TargetTriple, Path, Args, Detected);
2587  } else if (TargetArch == llvm::Triple::avr) {
2588  // AVR has no multilibs.
2589  } else if (!findBiarchMultilibs(D, TargetTriple, Path, Args,
2590  NeedsBiarchSuffix, Detected)) {
2591  return false;
2592  }
2593 
2594  Multilibs = Detected.Multilibs;
2595  SelectedMultilib = Detected.SelectedMultilib;
2596  BiarchSibling = Detected.BiarchSibling;
2597 
2598  return true;
2599 }
2600 
2601 void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
2602  const llvm::Triple &TargetTriple, const ArgList &Args,
2603  const std::string &LibDir, StringRef CandidateTriple,
2604  bool NeedsBiarchSuffix, bool GCCDirExists, bool GCCCrossDirExists) {
2605  // Locations relative to the system lib directory where GCC's triple-specific
2606  // directories might reside.
2607  struct GCCLibSuffix {
2608  // Path from system lib directory to GCC triple-specific directory.
2609  std::string LibSuffix;
2610  // Path from GCC triple-specific directory back to system lib directory.
2611  // This is one '..' component per component in LibSuffix.
2612  StringRef ReversePath;
2613  // Whether this library suffix is relevant for the triple.
2614  bool Active;
2615  } Suffixes[] = {
2616  // This is the normal place.
2617  {"gcc/" + CandidateTriple.str(), "../..", GCCDirExists},
2618 
2619  // Debian puts cross-compilers in gcc-cross.
2620  {"gcc-cross/" + CandidateTriple.str(), "../..", GCCCrossDirExists},
2621 
2622  // The Freescale PPC SDK has the gcc libraries in
2623  // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well. Only do
2624  // this on Freescale triples, though, since some systems put a *lot* of
2625  // files in that location, not just GCC installation data.
2626  {CandidateTriple.str(), "..",
2627  TargetTriple.getVendor() == llvm::Triple::Freescale ||
2628  TargetTriple.getVendor() == llvm::Triple::OpenEmbedded}};
2629 
2630  for (auto &Suffix : Suffixes) {
2631  if (!Suffix.Active)
2632  continue;
2633 
2634  StringRef LibSuffix = Suffix.LibSuffix;
2635  std::error_code EC;
2636  for (llvm::vfs::directory_iterator
2637  LI = D.getVFS().dir_begin(LibDir + "/" + LibSuffix, EC),
2638  LE;
2639  !EC && LI != LE; LI = LI.increment(EC)) {
2640  StringRef VersionText = llvm::sys::path::filename(LI->path());
2641  GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
2642  if (CandidateVersion.Major != -1) // Filter obviously bad entries.
2643  if (!CandidateGCCInstallPaths.insert(std::string(LI->path())).second)
2644  continue; // Saw this path before; no need to look at it again.
2645  if (CandidateVersion.isOlderThan(4, 1, 1))
2646  continue;
2647  if (CandidateVersion <= Version)
2648  continue;
2649 
2650  if (!ScanGCCForMultilibs(TargetTriple, Args, LI->path(),
2651  NeedsBiarchSuffix))
2652  continue;
2653 
2654  Version = CandidateVersion;
2655  GCCTriple.setTriple(CandidateTriple);
2656  // FIXME: We hack together the directory name here instead of
2657  // using LI to ensure stable path separators across Windows and
2658  // Linux.
2659  GCCInstallPath = (LibDir + "/" + LibSuffix + "/" + VersionText).str();
2660  GCCParentLibPath = (GCCInstallPath + "/../" + Suffix.ReversePath).str();
2661  IsValid = true;
2662  }
2663  }
2664 }
2665 
2666 bool Generic_GCC::GCCInstallationDetector::ScanGentooConfigs(
2667  const llvm::Triple &TargetTriple, const ArgList &Args,
2668  const SmallVectorImpl<StringRef> &CandidateTriples,
2669  const SmallVectorImpl<StringRef> &CandidateBiarchTriples) {
2670  if (!D.getVFS().exists(D.SysRoot + GentooConfigDir))
2671  return false;
2672 
2673  for (StringRef CandidateTriple : CandidateTriples) {
2674  if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple))
2675  return true;
2676  }
2677 
2678  for (StringRef CandidateTriple : CandidateBiarchTriples) {
2679  if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple, true))
2680  return true;
2681  }
2682  return false;
2683 }
2684 
2685 bool Generic_GCC::GCCInstallationDetector::ScanGentooGccConfig(
2686  const llvm::Triple &TargetTriple, const ArgList &Args,
2687  StringRef CandidateTriple, bool NeedsBiarchSuffix) {
2688  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
2689  D.getVFS().getBufferForFile(D.SysRoot + GentooConfigDir + "/config-" +
2690  CandidateTriple.str());
2691  if (File) {
2693  File.get()->getBuffer().split(Lines, "\n");
2694  for (StringRef Line : Lines) {
2695  Line = Line.trim();
2696  // CURRENT=triple-version
2697  if (!Line.consume_front("CURRENT="))
2698  continue;
2699  // Process the config file pointed to by CURRENT.
2700  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ConfigFile =
2701  D.getVFS().getBufferForFile(D.SysRoot + GentooConfigDir + "/" +
2702  Line.str());
2703  std::pair<StringRef, StringRef> ActiveVersion = Line.rsplit('-');
2704  // List of paths to scan for libraries.
2705  SmallVector<StringRef, 4> GentooScanPaths;
2706  // Scan the Config file to find installed GCC libraries path.
2707  // Typical content of the GCC config file:
2708  // LDPATH="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x:/usr/lib/gcc/
2709  // (continued from previous line) x86_64-pc-linux-gnu/4.9.x/32"
2710  // MANPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/man"
2711  // INFOPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/info"
2712  // STDCXX_INCDIR="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4"
2713  // We are looking for the paths listed in LDPATH=... .
2714  if (ConfigFile) {
2715  SmallVector<StringRef, 2> ConfigLines;
2716  ConfigFile.get()->getBuffer().split(ConfigLines, "\n");
2717  for (StringRef ConfLine : ConfigLines) {
2718  ConfLine = ConfLine.trim();
2719  if (ConfLine.consume_front("LDPATH=")) {
2720  // Drop '"' from front and back if present.
2721  ConfLine.consume_back("\"");
2722  ConfLine.consume_front("\"");
2723  // Get all paths sperated by ':'
2724  ConfLine.split(GentooScanPaths, ':', -1, /*AllowEmpty*/ false);
2725  }
2726  }
2727  }
2728  // Test the path based on the version in /etc/env.d/gcc/config-{tuple}.
2729  std::string basePath = "/usr/lib/gcc/" + ActiveVersion.first.str() + "/"
2730  + ActiveVersion.second.str();
2731  GentooScanPaths.push_back(StringRef(basePath));
2732 
2733  // Scan all paths for GCC libraries.
2734  for (const auto &GentooScanPath : GentooScanPaths) {
2735  std::string GentooPath = D.SysRoot + std::string(GentooScanPath);
2736  if (D.getVFS().exists(GentooPath + "/crtbegin.o")) {
2737  if (!ScanGCCForMultilibs(TargetTriple, Args, GentooPath,
2738  NeedsBiarchSuffix))
2739  continue;
2740 
2741  Version = GCCVersion::Parse(ActiveVersion.second);
2742  GCCInstallPath = GentooPath;
2743  GCCParentLibPath = GentooPath + std::string("/../../..");
2744  GCCTriple.setTriple(ActiveVersion.first);
2745  IsValid = true;
2746  return true;
2747  }
2748  }
2749  }
2750  }
2751 
2752  return false;
2753 }
2754 
2755 Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple &Triple,
2756  const ArgList &Args)
2757  : ToolChain(D, Triple, Args), GCCInstallation(D),
2758  CudaInstallation(D, Triple, Args), RocmInstallation(D, Triple, Args) {
2759  getProgramPaths().push_back(getDriver().getInstalledDir());
2760  if (getDriver().getInstalledDir() != getDriver().Dir)
2761  getProgramPaths().push_back(getDriver().Dir);
2762 }
2763 
2765 
2767  switch (AC) {
2769  if (!Preprocess)
2770  Preprocess.reset(new clang::driver::tools::gcc::Preprocessor(*this));
2771  return Preprocess.get();
2773  if (!Compile)
2774  Compile.reset(new tools::gcc::Compiler(*this));
2775  return Compile.get();
2776  default:
2777  return ToolChain::getTool(AC);
2778  }
2779 }
2780 
2782  return new tools::gnutools::Assembler(*this);
2783 }
2784 
2785 Tool *Generic_GCC::buildLinker() const { return new tools::gcc::Linker(*this); }
2786 
2787 void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
2788  // Print the information about how we detected the GCC installation.
2789  GCCInstallation.print(OS);
2790  CudaInstallation.print(OS);
2791  RocmInstallation.print(OS);
2792 }
2793 
2794 bool Generic_GCC::IsUnwindTablesDefault(const ArgList &Args) const {
2795  switch (getArch()) {
2796  case llvm::Triple::aarch64:
2797  case llvm::Triple::ppc:
2798  case llvm::Triple::ppcle:
2799  case llvm::Triple::ppc64:
2800  case llvm::Triple::ppc64le:
2801  case llvm::Triple::x86:
2802  case llvm::Triple::x86_64:
2803  return true;
2804  default:
2805  return false;
2806  }
2807 }
2808 
2810  switch (getArch()) {
2811  case llvm::Triple::x86_64:
2812  return getTriple().isOSWindows();
2813  case llvm::Triple::mips64:
2814  case llvm::Triple::mips64el:
2815  return true;
2816  default:
2817  return false;
2818  }
2819 }
2820 
2821 bool Generic_GCC::isPIEDefault(const llvm::opt::ArgList &Args) const {
2822  return false;
2823 }
2824 
2826  return getArch() == llvm::Triple::x86_64 && getTriple().isOSWindows();
2827 }
2828 
2830  switch (getTriple().getArch()) {
2831  case llvm::Triple::aarch64:
2832  case llvm::Triple::aarch64_be:
2833  case llvm::Triple::arm:
2834  case llvm::Triple::armeb:
2835  case llvm::Triple::avr:
2836  case llvm::Triple::bpfel:
2837  case llvm::Triple::bpfeb:
2838  case llvm::Triple::csky:
2839  case llvm::Triple::hexagon:
2840  case llvm::Triple::lanai:
2841  case llvm::Triple::m68k:
2842  case llvm::Triple::mips:
2843  case llvm::Triple::mipsel:
2844  case llvm::Triple::mips64:
2845  case llvm::Triple::mips64el:
2846  case llvm::Triple::msp430:
2847  case llvm::Triple::ppc:
2848  case llvm::Triple::ppcle:
2849  case llvm::Triple::ppc64:
2850  case llvm::Triple::ppc64le:
2851  case llvm::Triple::riscv32:
2852  case llvm::Triple::riscv64:
2853  case llvm::Triple::sparc:
2854  case llvm::Triple::sparcel:
2855  case llvm::Triple::sparcv9:
2856  case llvm::Triple::systemz:
2857  case llvm::Triple::thumb:
2858  case llvm::Triple::thumbeb:
2859  case llvm::Triple::ve:
2860  case llvm::Triple::x86:
2861  case llvm::Triple::x86_64:
2862  return true;
2863  default:
2864  return false;
2865  }
2866 }
2867 
2869  // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
2870  // least) put various tools in a triple-prefixed directory off of the parent
2871  // of the GCC installation. We use the GCC triple here to ensure that we end
2872  // up with tools that support the same amount of cross compiling as the
2873  // detected GCC installation. For example, if we find a GCC installation
2874  // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2875  // used to target i386.
2876  if (GCCInstallation.isValid()) {
2877  PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
2878  GCCInstallation.getTriple().str() + "/bin")
2879  .str());
2880  }
2881 }
2882 
2884  const std::string &SysRoot,
2885  const std::string &OSLibDir,
2886  const std::string &MultiarchTriple,
2887  path_list &Paths) {
2888  // Add the multilib suffixed paths where they are available.
2889  if (GCCInstallation.isValid()) {
2890  const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
2891  const std::string &LibPath =
2893 
2894  // Sourcery CodeBench MIPS toolchain holds some libraries under
2895  // a biarch-like suffix of the GCC installation.
2896  if (const auto &PathsCallback = Multilibs.filePathsCallback())
2897  for (const auto &Path : PathsCallback(SelectedMultilib))
2898  addPathIfExists(D, GCCInstallation.getInstallPath() + Path, Paths);
2899 
2900  // Add lib/gcc/$triple/$version, with an optional /multilib suffix.
2903  Paths);
2904 
2905  // Add lib/gcc/$triple/$libdir
2906  // For GCC built with --enable-version-specific-runtime-libs.
2907  addPathIfExists(D, GCCInstallation.getInstallPath() + "/../" + OSLibDir,
2908  Paths);
2909 
2910  // GCC cross compiling toolchains will install target libraries which ship
2911  // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
2912  // any part of the GCC installation in
2913  // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
2914  // debatable, but is the reality today. We need to search this tree even
2915  // when we have a sysroot somewhere else. It is the responsibility of
2916  // whomever is doing the cross build targeting a sysroot using a GCC
2917  // installation that is *not* within the system root to ensure two things:
2918  //
2919  // 1) Any DSOs that are linked in from this tree or from the install path
2920  // above must be present on the system root and found via an
2921  // appropriate rpath.
2922  // 2) There must not be libraries installed into
2923  // <prefix>/<triple>/<libdir> unless they should be preferred over
2924  // those within the system root.
2925  //
2926  // Note that this matches the GCC behavior. See the below comment for where
2927  // Clang diverges from GCC's behavior.
2928  addPathIfExists(D,
2929  LibPath + "/../" + GCCTriple.str() + "/lib/../" + OSLibDir +
2931  Paths);
2932 
2933  // If the GCC installation we found is inside of the sysroot, we want to
2934  // prefer libraries installed in the parent prefix of the GCC installation.
2935  // It is important to *not* use these paths when the GCC installation is
2936  // outside of the system root as that can pick up unintended libraries.
2937  // This usually happens when there is an external cross compiler on the
2938  // host system, and a more minimal sysroot available that is the target of
2939  // the cross. Note that GCC does include some of these directories in some
2940  // configurations but this seems somewhere between questionable and simply
2941  // a bug.
2942  if (StringRef(LibPath).startswith(SysRoot))
2943  addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths);
2944  }
2945 }
2946 
2948  const std::string &SysRoot,
2949  const std::string &OSLibDir,
2950  path_list &Paths) {
2951  if (GCCInstallation.isValid()) {
2952  const std::string &LibPath =
2954  const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
2957  D, LibPath + "/../" + GCCTriple.str() + "/lib" + Multilib.osSuffix(),
2958  Paths);
2959  }
2960 }
2961 
2962 void Generic_GCC::AddMultilibIncludeArgs(const ArgList &DriverArgs,
2963  ArgStringList &CC1Args) const {
2964  // Add include directories specific to the selected multilib set and multilib.
2965  if (!GCCInstallation.isValid())
2966  return;
2967  // gcc TOOL_INCLUDE_DIR.
2968  const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
2970  addSystemInclude(DriverArgs, CC1Args,
2971  Twine(LibPath) + "/../" + GCCTriple.str() + "/include");
2972 
2973  const auto &Callback = Multilibs.includeDirsCallback();
2974  if (Callback) {
2975  for (const auto &Path : Callback(GCCInstallation.getMultilib()))
2976  addExternCSystemIncludeIfExists(DriverArgs, CC1Args,
2977  GCCInstallation.getInstallPath() + Path);
2978  }
2979 }
2980 
2981 void Generic_GCC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2982  ArgStringList &CC1Args) const {
2983  if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdincxx,
2984  options::OPT_nostdlibinc))
2985  return;
2986 
2987  switch (GetCXXStdlibType(DriverArgs)) {
2988  case ToolChain::CST_Libcxx:
2989  addLibCxxIncludePaths(DriverArgs, CC1Args);
2990  break;
2991 
2993  addLibStdCxxIncludePaths(DriverArgs, CC1Args);
2994  break;
2995  }
2996 }
2997 
2998 void
2999 Generic_GCC::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
3000  llvm::opt::ArgStringList &CC1Args) const {
3001  const Driver &D = getDriver();
3002  std::string SysRoot = computeSysRoot();
3004 
3005  auto AddIncludePath = [&](std::string Path) {
3006  std::string Version = detectLibcxxVersion(Path);
3007  if (Version.empty())
3008  return false;
3009 
3010  // First add the per-target include path if it exists.
3011  std::string TargetDir = Path + "/" + Target + "/c++/" + Version;
3012  if (D.getVFS().exists(TargetDir))
3013  addSystemInclude(DriverArgs, CC1Args, TargetDir);
3014 
3015  // Second add the generic one.
3016  addSystemInclude(DriverArgs, CC1Args, Path + "/c++/" + Version);
3017  return true;
3018  };
3019 
3020  // Android never uses the libc++ headers installed alongside the toolchain,
3021  // which are generally incompatible with the NDK libraries anyway.
3022  if (!getTriple().isAndroid())
3023  if (AddIncludePath(getDriver().Dir + "/../include"))
3024  return;
3025  // If this is a development, non-installed, clang, libcxx will
3026  // not be found at ../include/c++ but it likely to be found at
3027  // one of the following two locations:
3028  if (AddIncludePath(SysRoot + "/usr/local/include"))
3029  return;
3030  if (AddIncludePath(SysRoot + "/usr/include"))
3031  return;
3032 }
3033 
3034 bool Generic_GCC::addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple,
3035  Twine IncludeSuffix,
3036  const llvm::opt::ArgList &DriverArgs,
3037  llvm::opt::ArgStringList &CC1Args,
3038  bool DetectDebian) const {
3039  if (!getVFS().exists(IncludeDir))
3040  return false;
3041 
3042  // Debian native gcc uses g++-multiarch-incdir.diff which uses
3043  // include/x86_64-linux-gnu/c++/10$IncludeSuffix instead of
3044  // include/c++/10/x86_64-linux-gnu$IncludeSuffix.
3045  std::string Dir = IncludeDir.str();
3046  StringRef Include =
3047  llvm::sys::path::parent_path(llvm::sys::path::parent_path(Dir));
3048  std::string Path =
3049  (Include + "/" + Triple + Dir.substr(Include.size()) + IncludeSuffix)
3050  .str();
3051  if (DetectDebian && !getVFS().exists(Path))
3052  return false;
3053 
3054  // GPLUSPLUS_INCLUDE_DIR
3055  addSystemInclude(DriverArgs, CC1Args, IncludeDir);
3056  // GPLUSPLUS_TOOL_INCLUDE_DIR. If Triple is not empty, add a target-dependent
3057  // include directory.
3058  if (DetectDebian)
3059  addSystemInclude(DriverArgs, CC1Args, Path);
3060  else if (!Triple.empty())
3061  addSystemInclude(DriverArgs, CC1Args,
3062  IncludeDir + "/" + Triple + IncludeSuffix);
3063  // GPLUSPLUS_BACKWARD_INCLUDE_DIR
3064  addSystemInclude(DriverArgs, CC1Args, IncludeDir + "/backward");
3065  return true;
3066 }
3067 
3069  const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
3070  StringRef DebianMultiarch) const {
3071  assert(GCCInstallation.isValid());
3072 
3073  // By default, look for the C++ headers in an include directory adjacent to
3074  // the lib directory of the GCC installation. Note that this is expect to be
3075  // equivalent to '/usr/include/c++/X.Y' in almost all cases.
3076  StringRef LibDir = GCCInstallation.getParentLibPath();
3077  StringRef InstallDir = GCCInstallation.getInstallPath();
3078  StringRef TripleStr = GCCInstallation.getTriple().str();
3080  const GCCVersion &Version = GCCInstallation.getVersion();
3081 
3082  // Try /../$triple/include/c++/$version (gcc --print-multiarch is not empty).
3084  LibDir.str() + "/../" + TripleStr + "/include/c++/" + Version.Text,
3085  TripleStr, Multilib.includeSuffix(), DriverArgs, CC1Args))
3086  return true;
3087 
3088  // Try /gcc/$triple/$version/include/c++/ (gcc --print-multiarch is not
3089  // empty). Like above but for GCC built with
3090  // --enable-version-specific-runtime-libs.
3091  if (addLibStdCXXIncludePaths(LibDir.str() + "/gcc/" + TripleStr + "/" +
3092  Version.Text + "/include/c++/",
3093  TripleStr, Multilib.includeSuffix(), DriverArgs,
3094  CC1Args))
3095  return true;
3096 
3097  // Detect Debian g++-multiarch-incdir.diff.
3098  if (addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text,
3099  DebianMultiarch, Multilib.includeSuffix(),
3100  DriverArgs, CC1Args, /*Debian=*/true))
3101  return true;
3102 
3103  // Try /../include/c++/$version (gcc --print-multiarch is empty).
3104  if (addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text,
3105  TripleStr, Multilib.includeSuffix(), DriverArgs,
3106  CC1Args))
3107  return true;
3108 
3109  // Otherwise, fall back on a bunch of options which don't use multiarch
3110  // layouts for simplicity.
3111  const std::string LibStdCXXIncludePathCandidates[] = {
3112  // Gentoo is weird and places its headers inside the GCC install,
3113  // so if the first attempt to find the headers fails, try these patterns.
3114  InstallDir.str() + "/include/g++-v" + Version.Text,
3115  InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
3116  Version.MinorStr,
3117  InstallDir.str() + "/include/g++-v" + Version.MajorStr,
3118  };
3119 
3120  for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
3121  if (addLibStdCXXIncludePaths(IncludePath, TripleStr,
3122  Multilib.includeSuffix(), DriverArgs, CC1Args))
3123  return true;
3124  }
3125  return false;
3126 }
3127 
3128 void
3129 Generic_GCC::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
3130  llvm::opt::ArgStringList &CC1Args) const {
3131  if (GCCInstallation.isValid()) {
3132  addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args,
3133  GCCInstallation.getTriple().str());
3134  }
3135 }
3136 
3137 llvm::opt::DerivedArgList *
3138 Generic_GCC::TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef,
3139  Action::OffloadKind DeviceOffloadKind) const {
3140 
3141  // If this tool chain is used for an OpenMP offloading device we have to make
3142  // sure we always generate a shared library regardless of the commands the
3143  // user passed to the host. This is required because the runtime library
3144  // is required to load the device image dynamically at run time.
3145  if (DeviceOffloadKind == Action::OFK_OpenMP) {
3146  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
3147  const OptTable &Opts = getDriver().getOpts();
3148 
3149  // Request the shared library. Given that these options are decided
3150  // implicitly, they do not refer to any base argument.
3151  DAL->AddFlagArg(/*BaseArg=*/nullptr, Opts.getOption(options::OPT_shared));
3152  DAL->AddFlagArg(/*BaseArg=*/nullptr, Opts.getOption(options::OPT_fPIC));
3153 
3154  // Filter all the arguments we don't care passing to the offloading
3155  // toolchain as they can mess up with the creation of a shared library.
3156  for (auto *A : Args) {
3157  switch ((options::ID)A->getOption().getID()) {
3158  default:
3159  DAL->append(A);
3160  break;
3161  case options::OPT_shared:
3162  case options::OPT_dynamic:
3163  case options::OPT_static:
3164  case options::OPT_fPIC:
3165  case options::OPT_fno_PIC:
3166  case options::OPT_fpic:
3167  case options::OPT_fno_pic:
3168  case options::OPT_fPIE:
3169  case options::OPT_fno_PIE:
3170  case options::OPT_fpie:
3171  case options::OPT_fno_pie:
3172  break;
3173  }
3174  }
3175  return DAL;
3176  }
3177  return nullptr;
3178 }
3179 
3180 void Generic_ELF::anchor() {}
3181 
3182 void Generic_ELF::addClangTargetOptions(const ArgList &DriverArgs,
3183  ArgStringList &CC1Args,
3184  Action::OffloadKind) const {
3185  if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
3186  options::OPT_fno_use_init_array, true))
3187  CC1Args.push_back("-fno-use-init-array");
3188 }
clang::driver::tools::mips::getGnuCompatibleMipsABIName
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:143
clang::driver::toolchains
Definition: AIX.h:55
Mips.h
clang::driver::Multilib::flags_list
std::vector< std::string > flags_list
Definition: Multilib.h:30
clang::driver::tools::gcc::Linker::RenderExtraToolArgs
void RenderExtraToolArgs(const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const override
RenderExtraToolArgs - Render any arguments necessary to force the particular tool mode.
Definition: Gnu.cpp:214
clang::driver::tools::AddAssemblerKPIC
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
clang::driver::Multilib::isDefault
bool isDefault() const
Check whether the default is selected.
Definition: Multilib.h:106
clang::driver::options::LinkerInput
@ LinkerInput
Definition: Options.h:26
isMips16
static bool isMips16(const ArgList &Args)
Definition: Gnu.cpp:1015
clang::driver::ToolChain::getCompilerRT
virtual std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:483
clang::driver::MultilibSet::Either
MultilibSet & Either(const Multilib &M1, const Multilib &M2)
Add a set of mutually incompatible Multilib segments.
Definition: Multilib.cpp:152
clang::driver::ToolChain::getVFS
llvm::vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:99
clang::driver::tools::ppc::getPPCAsmModeForCPU
const char * getPPCAsmModeForCPU(StringRef Name)
Definition: PPC.cpp:93
isMSP430
static bool isMSP430(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:1025
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:266
clang::driver::tools::addFortranRuntimeLibs
void addFortranRuntimeLibs(llvm::opt::ArgStringList &CmdArgs)
Adds Fortran runtime libraries to CmdArgs.
Definition: CommonArgs.cpp:751
isSoftFloatABI
static bool isSoftFloatABI(const ArgList &Args)
Definition: Gnu.cpp:996
clang::DeclaratorContext::File
@ File
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::toolchains::Generic_GCC::addLibCxxIncludePaths
virtual void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:2999
clang::driver::tools::ParsePICArgs
std::tuple< llvm::Reloc::Model, unsigned, bool > ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args)
clang::driver::toolchains::Generic_GCC::IsUnwindTablesDefault
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override
IsUnwindTablesDefault - Does this tool chain use -funwind-tables by default.
Definition: Gnu.cpp:2794
clang::driver::toolchains::Generic_GCC::TranslateArgs
llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const override
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: Gnu.cpp:3138
clang::driver::MultilibSet
Definition: Multilib.h:114
clang::driver::findMIPSMultilibs
bool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const llvm::opt::ArgList &Args, DetectedMultilibs &Result)
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::tools::csky::FloatABI::Hard
@ Hard
clang::driver::toolchains::Generic_GCC::GCCInstallation
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
clang::driver::tools::arm::getARMFloatABI
FloatABI getARMFloatABI(const ToolChain &TC, const llvm::opt::ArgList &Args)
clang::driver::Action::ActionClass
ActionClass
Definition: Action.h:55
clang::driver::toolchains::Generic_GCC::AddMultiarchPaths
void AddMultiarchPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, path_list &Paths)
Definition: Gnu.cpp:2947
clang::driver::toolchains::Generic_GCC::IsIntegratedAssemblerDefault
bool IsIntegratedAssemblerDefault() const override
IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as by default.
Definition: Gnu.cpp:2829
clang::MultiVersionKind::Target
@ Target
clang::driver::tools::csky::FloatABI::SoftFP
@ SoftFP
clang::driver::toolchains::Generic_GCC::CudaInstallation
CudaInstallationDetector CudaInstallation
Definition: Gnu.h:289
clang::driver::tools::mips::hasMipsAbiArg
bool hasMipsAbiArg(const llvm::opt::ArgList &Args, const char *Value)
clang::driver::MultilibSet::size
unsigned size() const
Definition: Multilib.h:169
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:225
clang::driver::ToolChain::detectLibcxxVersion
virtual std::string detectLibcxxVersion(StringRef IncludePath) const
Definition: ToolChain.cpp:917
makeMultilib
static Multilib makeMultilib(StringRef commonSuffix)
Definition: Gnu.cpp:1029
clang::driver::ToolChain::FT_Object
@ FT_Object
Definition: ToolChain.h:123
clang::driver::ToolChain::getTripleString
std::string getTripleString() const
Definition: ToolChain.h:250
clang::driver::ToolChain::addFastMathRuntimeIfAvailable
bool addFastMathRuntimeIfAvailable(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFastMathRuntimeIfAvailable - If a runtime library exists that sets global flags for unsafe floatin...
Definition: ToolChain.cpp:1020
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::isValid
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
Sparc.h
clang::driver::tools::getCPUName
std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
llvm::Optional< llvm::StringRef >
clang::ComparisonCategoryType::First
@ First
clang::driver::toolchains::Generic_GCC::GCCVersion::Minor
int Minor
Definition: Gnu.h:166
clang::driver::toolchains::Generic_GCC::AddMultilibPaths
void AddMultilibPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, const std::string &MultiarchTriple, path_list &Paths)
Definition: Gnu.cpp:2883
findMSP430Multilibs
static bool findMSP430Multilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, DetectedMultilibs &Result)
Definition: Gnu.cpp:1576
clang::driver::ToolChain::GetFilePath
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:577
clang::driver::MultilibSet::FilterOut
MultilibSet & FilterOut(FilterCallback F)
Filter out some subset of the Multilibs using a user defined callback.
Definition: Multilib.cpp:212
clang::driver::tools::SplitDebugInfo
void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T, const JobAction &JA, const llvm::opt::ArgList &Args, const InputInfo &Output, const char *OutFile)
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::init
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, ArrayRef< std::string > ExtraTripleAliases=None)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:1972
clang::driver::toolchains::Generic_GCC::isPICDefault
bool isPICDefault() const override
Test whether this toolchain defaults to PIC.
Definition: Gnu.cpp:2809
clang::driver::Driver::getOpts
const llvm::opt::OptTable & getOpts() const
Definition: Driver.h:358
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::tools::csky::getCSKYFloatABI
FloatABI getCSKYFloatABI(const Driver &D, const llvm::opt::ArgList &Args)
isMipsEL
static bool isMipsEL(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:1011
RISCV.h
clang::driver::MultilibSet::setFilePathsCallback
MultilibSet & setFilePathsCallback(IncludeDirsFunc F)
Definition: Multilib.h:181
clang::driver::ToolChain::addExternCSystemIncludeIfExists
static void addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Definition: ToolChain.cpp:900
clang::driver::toolchains::Generic_GCC::PushPPaths
void PushPPaths(ToolChain::path_list &PPaths)
Definition: Gnu.cpp:2868
clang::driver::Action::OFK_OpenMP
@ OFK_OpenMP
Definition: Action.h:96
SystemZ.h
clang::driver::toolchains::Generic_GCC::AddMultilibIncludeArgs
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:2962
clang::driver::toolchains::Generic_GCC::buildAssembler
Tool * buildAssembler() const override
Definition: Gnu.cpp:2781
clang::driver::Driver::InstalledDir
std::string InstalledDir
The path to the installed clang directory, if any.
Definition: Driver.h:152
clang::driver::tools::riscv::getRISCVArch
StringRef getRISCVArch(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
Definition: RISCV.cpp:221
Suffixes
static constexpr CPUSuffix Suffixes[]
Definition: Hexagon.cpp:219
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
clang::driver::toolchains::Generic_GCC::GCCVersion
Struct to store and manipulate GCC versions.
Definition: Gnu.h:161
clang::driver::tools::csky::getCSKYArchName
llvm::Optional< llvm::StringRef > getCSKYArchName(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
forwardToGCC
static bool forwardToGCC(const Option &O)
Definition: Gnu.cpp:41
clang::StructuralEquivalenceKind::Default
@ Default
clang::driver::Action::PreprocessJobClass
@ PreprocessJobClass
Definition: Action.h:59
clang::driver::tools::gnutools::Linker::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: Gnu.cpp:385
clang::driver::Driver::getLTOMode
LTOKind getLTOMode(bool IsOffload=false) const
Get the specific kind of LTO being performed.
Definition: Driver.h:624
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:132
clang::driver::ToolChain::isPIEDefault
virtual bool isPIEDefault(const llvm::opt::ArgList &Args) const =0
Test whether this toolchain defaults to PIE.
clang::driver::Multilib::gccSuffix
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
Definition: Multilib.h:45
clang::driver::toolchains::Generic_GCC::getTool
Tool * getTool(Action::ActionClass AC) const override
Definition: Gnu.cpp:2766
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
getStatic
static bool getStatic(const ArgList &Args)
Definition: Gnu.cpp:336
clang::driver::toolchains::Generic_GCC::GCCVersion::isOlderThan
bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch, StringRef RHSPatchSuffix=StringRef()) const
Generic_GCC - A tool chain using the 'gcc' command to perform all subcommands; this relies on gcc tra...
Definition: Gnu.cpp:1864
clang::driver::tools::claimNoWarnArgs
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
clang::driver::toolchains::Generic_ELF
Definition: Gnu.h:362
Options.h
clang::driver::InputInfo::isFilename
bool isFilename() const
Definition: InputInfo.h:75
findRISCVBareMetalMultilibs
static void findRISCVBareMetalMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, DetectedMultilibs &Result)
Definition: Gnu.cpp:1664
clang::driver::toolchains::Generic_GCC::GCCVersion::Text
std::string Text
The unparsed text of the version.
Definition: Gnu.h:163
findMipsAndroidMultilibs
static bool findMipsAndroidMultilibs(llvm::vfs::FileSystem &VFS, StringRef Path, const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1125
clang::driver::toolchains::Generic_GCC::Generic_GCC
Generic_GCC(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Gnu.cpp:2755
clang::driver::Multilib::osSuffix
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Definition: Multilib.h:56
CSKY.h
clang::interp::LE
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:239
clang::driver::Action::getType
types::ID getType() const
Definition: Action.h:149
clang::driver::LTOK_Thin
@ LTOK_Thin
Definition: Driver.h:55
clang::driver::DetectedMultilibs::SelectedMultilib
Multilib SelectedMultilib
The primary multilib appropriate for the given flags.
Definition: Gnu.h:26
clang::driver::MultilibSet::includeDirsCallback
const IncludeDirsFunc & includeDirsCallback() const
Definition: Multilib.h:179
llvm::opt
Definition: DiagnosticOptions.h:19
DriverDiagnostic.h
clang::driver::options::ID
ID
Definition: Options.h:42
clang::driver::ResponseFileSupport::AtFileCurCP
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92
clang::driver::InputInfo::isNothing
bool isNothing() const
Definition: InputInfo.h:74
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getTriple
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:227
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getParentLibPath
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
clang::driver::tools::mips::isNaN2008
bool isNaN2008(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
clang::driver::tools::addPathIfExists
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:122
clang::driver::tools::systemz::getSystemZTargetCPU
std::string getSystemZTargetCPU(const llvm::opt::ArgList &Args)
Tool.h
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:227
findBiarchMultilibs
static bool findBiarchMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, bool NeedsBiarchSuffix, DetectedMultilibs &Result)
Definition: Gnu.cpp:1755
clang::driver::ToolChain::SelectedMultilib
Multilib SelectedMultilib
Definition: ToolChain.h:186
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:171
clang::driver::Action::isHostOffloading
bool isHostOffloading(unsigned int OKind) const
Check if this action have any offload kinds.
Definition: Action.h:219
clang::driver::Multilib
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
Definition: Multilib.h:28
clang::driver::ToolChain::CST_Libcxx
@ CST_Libcxx
Definition: ToolChain.h:96
findRISCVMultilibs
static void findRISCVMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, DetectedMultilibs &Result)
Definition: Gnu.cpp:1718
clang::driver::Driver::CCCIsCXX
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:201
clang::driver::Driver::getCCCGenericGCCName
const std::string & getCCCGenericGCCName() const
Name to use when invoking gcc/g++.
Definition: Driver.h:354
findCSKYMultilibs
static void findCSKYMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, DetectedMultilibs &Result)
Definition: Gnu.cpp:1602
clang::driver::toolchains::Generic_GCC::GCCVersion::PatchSuffix
std::string PatchSuffix
Any textual suffix on the patch number.
Definition: Gnu.h:172
getGCCToolchainDir
static llvm::StringRef getGCCToolchainDir(const ArgList &Args, llvm::StringRef SysRoot)
Definition: Gnu.cpp:1948
clang::driver::tools::addXRayRuntime
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
clang::driver::tools::gcc::Compiler::RenderExtraToolArgs
void RenderExtraToolArgs(const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const override
RenderExtraToolArgs - Render any arguments necessary to force the particular tool mode.
Definition: Gnu.cpp:186
clang::driver::types::isLLVMIR
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:252
Base
clang::driver::toolchains::Generic_GCC::GCCVersion::MajorStr
std::string MajorStr
The text of the parsed major, and major+minor versions.
Definition: Gnu.h:169
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:68
clang::driver::MultilibSet::filePathsCallback
const IncludeDirsFunc & filePathsCallback() const
Definition: Multilib.h:186
clang::driver::tools::gcc::Preprocessor::RenderExtraToolArgs
void RenderExtraToolArgs(const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const override
RenderExtraToolArgs - Render any arguments necessary to force the particular tool mode.
Definition: Gnu.cpp:181
clang::driver::toolchains::Generic_GCC::GCCVersion::Parse
static GCCVersion Parse(StringRef VersionText)
Parse a GCCVersion object out of a string of text.
Definition: Gnu.cpp:1901
findAndroidArmMultilibs
static void findAndroidArmMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, DetectedMultilibs &Result)
Definition: Gnu.cpp:1534
Linux.h
clang::driver::ToolChain::CST_Libstdcxx
@ CST_Libstdcxx
Definition: ToolChain.h:97
findMipsImgMultilibs
static bool findMipsImgMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1368
clang::driver::toolchains::Generic_GCC::~Generic_GCC
~Generic_GCC() override
Definition: Gnu.cpp:2764
clang::driver::toolchains::Generic_GCC::addGCCLibStdCxxIncludePaths
bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, StringRef DebianMultiarch) const
Definition: Gnu.cpp:3068
clang::driver::ToolChain::getArch
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:241
clang::driver::ToolChain::AddFilePathLibArgs
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:988
clang::driver::tools::gcc::Linker
Definition: Gnu.h:126
clang::driver::ToolChain::ShouldLinkCXXStdlib
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:965
clang::driver::options::LinkOption
@ LinkOption
Definition: Options.h:34
clang::driver::RocmInstallationDetector::print
void print(raw_ostream &OS) const
Print information about the detected ROCm installation.
Definition: AMDGPU.cpp:480
clang::driver::types::canTypeBeUserSpecified
bool canTypeBeUserSpecified(ID Id)
canTypeBeUserSpecified - Can this type be specified on the command line (by the type name); this is u...
Definition: Types.cpp:105
clang::driver::tools::addLTOOptions
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
clang::driver::toolchains::Generic_GCC::RocmInstallation
RocmInstallationDetector RocmInstallation
Definition: Gnu.h:290
ToolChain.h
clang::transformer::remove
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: RewriteRule.cpp:146
clang::driver::tools::addHIPRuntimeLibArgs
void addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Definition: CommonArgs.cpp:2114
findMipsMtiMultilibs
static bool findMipsMtiMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1196
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::driver::Driver::isUsingLTO
bool isUsingLTO(bool IsOffload=false) const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:619
clang::driver::ToolChain::getTool
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:344
isArmOrThumbArch
static bool isArmOrThumbArch(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:1007
clang::driver::toolchains::Generic_GCC::printVerboseInfo
void printVerboseInfo(raw_ostream &OS) const override
Dispatch to the specific toolchain for verbose printing.
Definition: Gnu.cpp:2787
clang::driver::ToolChain::AddCXXStdlibLibArgs
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:971
getLDMOption
static const char * getLDMOption(const llvm::Triple &T, const ArgList &Args)
Definition: Gnu.cpp:243
clang::driver::ToolChain::GetRuntimeLibType
virtual RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:790
Compilation.h
Gnu.h
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::ToolChain::GetLinkerPath
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:585
clang::driver::ToolChain::RLT_CompilerRT
@ RLT_CompilerRT
Definition: ToolChain.h:101
llvm::ArrayRef
Definition: LLVM.h:34
Value
Value
Definition: UninitializedValues.cpp:102
clang::driver::tools::csky::FloatABI
FloatABI
Definition: CSKY.h:24
clang::driver::ToolChain::computeSysRoot
virtual std::string computeSysRoot() const
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: ToolChain.cpp:767
clang::driver::tools::gnutools::Assembler
Definition: Gnu.h:41
clang::driver::tools::AddRunTimeLibs
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
clang::driver::tools::gcc::Preprocessor
Definition: Gnu.h:103
isMicroMips
static bool isMicroMips(const ArgList &Args)
Definition: Gnu.cpp:1020
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getInstallPath
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:230
clang::driver::Multilib::flag
Multilib & flag(StringRef F)
Add a flag to the flags list Flag must be a flag accepted by the driver with its leading '-' removed,...
Definition: Multilib.h:92
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getVersion
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:246
clang::driver::Action::CompileJobClass
@ CompileJobClass
Definition: Action.h:65
clang::driver::tools::gnutools::StaticLibTool::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: Gnu.cpp:341
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getBiarchSibling
bool getBiarchSibling(Multilib &M) const
Get the biarch sibling multilib (if it exists).
Definition: Gnu.cpp:2089
findMipsMuslMultilibs
static bool findMipsMuslMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1164
isArmBigEndian
static bool isArmBigEndian(const llvm::Triple &Triple, const ArgList &Args)
Definition: Gnu.cpp:223
clang::driver::Action::isDeviceOffloading
bool isDeviceOffloading(OffloadKind OKind) const
Definition: Action.h:222
clang::driver::ToolChain::GetCXXStdlibType
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:852
clang::driver::toolchains::Generic_GCC::addLibStdCXXIncludePaths
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3034
IsStatic
bool IsStatic
Definition: Format.cpp:2563
clang::driver::toolchains::Generic_GCC::GCCVersion::Major
int Major
The parsed major, minor, and patch numbers.
Definition: Gnu.h:166
clang::driver::toolchains::Generic_GCC::addLibStdCxxIncludePaths
virtual void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:3129
clang::driver::toolchains::Generic_GCC::GCCVersion::MinorStr
std::string MinorStr
Definition: Gnu.h:169
clang::driver::toolchains::Generic_ELF::addClangTargetOptions
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: Gnu.cpp:3182
clang::driver::tools::gnutools::Assembler::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: Gnu.cpp:696
clang::driver::tools::addOpenMPRuntime
bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC, const llvm::opt::ArgList &Args, bool ForceStaticHostRuntime=false, bool IsOffloadingHost=false, bool GompNeedsRT=false)
Returns true, if an OpenMP runtime has been added.
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
clang::driver::tools::mips::getMipsCPUAndABI
void getMipsCPUAndABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef &CPUName, StringRef &ABIName)
clang::driver::tools::linkSanitizerRuntimeDeps
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:71
clang::driver::toolchains::Generic_GCC::isPICDefaultForced
bool isPICDefaultForced() const override
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
Definition: Gnu.cpp:2825
clang::driver::tools::mips::isUCLibc
bool isUCLibc(const llvm::opt::ArgList &Args)
clang::driver::tools::mips::getMipsFloatABI
mips::FloatABI getMipsFloatABI(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getMultilib
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
clang::driver::tools::gcc::Compiler
Definition: Gnu.h:115
clang::driver::toolchains::Generic_GCC::AddClangCXXStdlibIncludeArgs
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: Gnu.cpp:2981
clang::driver::toolchains::Generic_GCC::isPIEDefault
bool isPIEDefault(const llvm::opt::ArgList &Args) const override
Test whether this toolchain defaults to PIE.
Definition: Gnu.cpp:2821
clang::driver::tools::addSanitizerRuntimes
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
clang::driver::tools::addMultilibFlag
void addMultilibFlag(bool Enabled, const char *const Flag, Multilib::flags_list &Flags)
Flag must be a flag accepted by the driver with its leading '-' removed,
Definition: CommonArgs.cpp:1611
clang::driver::ToolChain::Multilibs
MultilibSet Multilibs
Definition: ToolChain.h:185
clang::driver::tools::linkXRayRuntimeDeps
void linkXRayRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
clang::driver::DetectedMultilibs
Definition: Gnu.h:21
clang::driver::Driver::getVFS
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:362
clang::driver::tools::riscv::getRISCVABI
StringRef getRISCVABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
clang::driver::Multilib::includeSuffix
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:67
clang::driver::toolchains::Generic_GCC::GCCVersion::Patch
int Patch
Definition: Gnu.h:166
clang::driver::tools::mips::shouldUseFPXX
bool shouldUseFPXX(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef CPUName, StringRef ABIName, mips::FloatABI FloatABI)
clang::driver::DetectedMultilibs::BiarchSibling
llvm::Optional< Multilib > BiarchSibling
On Biarch systems, this corresponds to the default multilib when targeting the non-default multilib.
Definition: Gnu.h:30
clang::driver::tools::addLinkerCompressDebugSectionsOption
void addLinkerCompressDebugSectionsOption(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Definition: CommonArgs.cpp:289
ARM.h
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:88
clang::driver::MultilibSet::Maybe
MultilibSet & Maybe(const Multilib &M)
Add an optional Multilib segment.
Definition: Multilib.cpp:142
clang::driver
Definition: Action.h:31
clang::driver::tools::arm::appendBE8LinkFlag
void appendBE8LinkFlag(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const llvm::Triple &Triple)
llvm::SmallVectorImpl
Definition: Randstruct.h:18
clang::driver::ToolChain::addSystemInclude
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:878
findMipsCsMultilibs
static bool findMipsCsMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1033
clang::driver::CudaInstallationDetector::print
void print(raw_ostream &OS) const
Print information about the detected CUDA installation.
Definition: Cuda.cpp:325
getPIE
static bool getPIE(const ArgList &Args, const ToolChain &TC)
Definition: Gnu.cpp:310
clang::driver::MultilibSet::select
bool select(const Multilib::flags_list &Flags, Multilib &M) const
Pick the best multilib in the set,.
Definition: Multilib.cpp:244
clang::driver::JobAction
Definition: Action.h:395
clang::driver::Driver::DyldPrefix
std::string DyldPrefix
Dynamic loader prefix, if present.
Definition: Driver.h:174
clang::driver::tools::gcc::Common::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: Gnu.cpp:64
clang::driver::DetectedMultilibs::Multilibs
MultilibSet Multilibs
The set of multilibs that the detected installation supports.
Definition: Gnu.h:23
clang::CudaVersion::UNKNOWN
@ UNKNOWN
getStaticPIE
static bool getStaticPIE(const ArgList &Args, const ToolChain &TC)
Definition: Gnu.cpp:322
clang::driver::tools::sparc::getSparcAsmModeForCPU
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
PPC.h
clang::driver::MultilibSet::setIncludeDirsCallback
MultilibSet & setIncludeDirsCallback(IncludeDirsFunc F)
Definition: Multilib.h:174
clang::RISCV::Invalid
@ Invalid
Definition: RISCVVIntrinsicUtils.h:148
clang::driver::tools::addFortranRuntimeLibraryPath
void addFortranRuntimeLibraryPath(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Adds the path for the Fortran runtime libraries to CmdArgs.
clang::driver::toolchains::Generic_GCC::buildLinker
Tool * buildLinker() const override
Definition: Gnu.cpp:2785
clang::driver::tools::SplitDebugName
const char * SplitDebugName(const JobAction &JA, const llvm::opt::ArgList &Args, const InputInfo &Input, const InputInfo &Output)
normalizeCPUNamesForAssembler
static void normalizeCPUNamesForAssembler(const ArgList &Args, ArgStringList &CmdArgs)
Definition: Gnu.cpp:51
clang::driver::Driver::IsFlangMode
bool IsFlangMode() const
Whether the driver should invoke flang for fortran inputs.
Definition: Driver.h:214
clang::driver::tools::csky::FloatABI::Soft
@ Soft
clang::driver::types::getTypeName
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition: Types.cpp:52
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::print
void print(raw_ostream &OS) const
Print information about the detected GCC installation.
Definition: Gnu.cpp:2075