clang  7.0.0svn
Gnu.cpp
Go to the documentation of this file.
1 //===--- Gnu.cpp - Gnu Tool and ToolChain Implementations -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Gnu.h"
11 #include "Arch/ARM.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"
20 #include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
22 #include "clang/Driver/Driver.h"
24 #include "clang/Driver/Options.h"
25 #include "clang/Driver/Tool.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 <system_error>
31 
32 using namespace clang::driver;
33 using namespace clang::driver::toolchains;
34 using namespace clang;
35 using namespace llvm::opt;
36 
37 void tools::GnuTool::anchor() {}
38 
39 static bool forwardToGCC(const Option &O) {
40  // Don't forward inputs from the original command line. They are added from
41  // InputInfoList.
42  return O.getKind() != Option::InputClass &&
43  !O.hasFlag(options::DriverOption) && !O.hasFlag(options::LinkerInput);
44 }
45 
46 // Switch CPU names not recognized by GNU assembler to a close CPU that it does
47 // recognize, instead of a lower march from being picked in the absence of a cpu
48 // flag.
49 static void normalizeCPUNamesForAssembler(const ArgList &Args,
50  ArgStringList &CmdArgs) {
51  if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
52  StringRef CPUArg(A->getValue());
53  if (CPUArg.equals_lower("krait"))
54  CmdArgs.push_back("-mcpu=cortex-a15");
55  else if(CPUArg.equals_lower("kryo"))
56  CmdArgs.push_back("-mcpu=cortex-a57");
57  else
58  Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ);
59  }
60 }
61 
63  const InputInfo &Output,
64  const InputInfoList &Inputs,
65  const ArgList &Args,
66  const char *LinkingOutput) const {
67  const Driver &D = getToolChain().getDriver();
68  ArgStringList CmdArgs;
69 
70  for (const auto &A : Args) {
71  if (forwardToGCC(A->getOption())) {
72  // It is unfortunate that we have to claim here, as this means
73  // we will basically never report anything interesting for
74  // platforms using a generic gcc, even if we are just using gcc
75  // to get to the assembler.
76  A->claim();
77 
78  // Don't forward any -g arguments to assembly steps.
79  if (isa<AssembleJobAction>(JA) &&
80  A->getOption().matches(options::OPT_g_Group))
81  continue;
82 
83  // Don't forward any -W arguments to assembly and link steps.
84  if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) &&
85  A->getOption().matches(options::OPT_W_Group))
86  continue;
87 
88  A->render(Args, CmdArgs);
89  }
90  }
91 
92  RenderExtraToolArgs(JA, CmdArgs);
93 
94  // If using a driver driver, force the arch.
95  if (getToolChain().getTriple().isOSDarwin()) {
96  CmdArgs.push_back("-arch");
97  CmdArgs.push_back(
98  Args.MakeArgString(getToolChain().getDefaultUniversalArchName()));
99  }
100 
101  // Try to force gcc to match the tool chain we want, if we recognize
102  // the arch.
103  //
104  // FIXME: The triple class should directly provide the information we want
105  // here.
106  switch (getToolChain().getArch()) {
107  default:
108  break;
109  case llvm::Triple::x86:
110  case llvm::Triple::ppc:
111  CmdArgs.push_back("-m32");
112  break;
113  case llvm::Triple::x86_64:
114  case llvm::Triple::ppc64:
115  case llvm::Triple::ppc64le:
116  CmdArgs.push_back("-m64");
117  break;
118  case llvm::Triple::sparcel:
119  CmdArgs.push_back("-EL");
120  break;
121  }
122 
123  if (Output.isFilename()) {
124  CmdArgs.push_back("-o");
125  CmdArgs.push_back(Output.getFilename());
126  } else {
127  assert(Output.isNothing() && "Unexpected output");
128  CmdArgs.push_back("-fsyntax-only");
129  }
130 
131  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
132 
133  // Only pass -x if gcc will understand it; otherwise hope gcc
134  // understands the suffix correctly. The main use case this would go
135  // wrong in is for linker inputs if they happened to have an odd
136  // suffix; really the only way to get this to happen is a command
137  // like '-x foobar a.c' which will treat a.c like a linker input.
138  //
139  // FIXME: For the linker case specifically, can we safely convert
140  // inputs into '-Wl,' options?
141  for (const auto &II : Inputs) {
142  // Don't try to pass LLVM or AST inputs to a generic gcc.
143  if (types::isLLVMIR(II.getType()))
144  D.Diag(clang::diag::err_drv_no_linker_llvm_support)
145  << getToolChain().getTripleString();
146  else if (II.getType() == types::TY_AST)
147  D.Diag(diag::err_drv_no_ast_support) << getToolChain().getTripleString();
148  else if (II.getType() == types::TY_ModuleFile)
149  D.Diag(diag::err_drv_no_module_support)
150  << getToolChain().getTripleString();
151 
152  if (types::canTypeBeUserSpecified(II.getType())) {
153  CmdArgs.push_back("-x");
154  CmdArgs.push_back(types::getTypeName(II.getType()));
155  }
156 
157  if (II.isFilename())
158  CmdArgs.push_back(II.getFilename());
159  else {
160  const Arg &A = II.getInputArg();
161 
162  // Reverse translate some rewritten options.
163  if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) {
164  CmdArgs.push_back("-lstdc++");
165  continue;
166  }
167 
168  // Don't render as input, we need gcc to do the translations.
169  A.render(Args, CmdArgs);
170  }
171  }
172 
173  const std::string &customGCCName = D.getCCCGenericGCCName();
174  const char *GCCName;
175  if (!customGCCName.empty())
176  GCCName = customGCCName.c_str();
177  else if (D.CCCIsCXX()) {
178  GCCName = "g++";
179  } else
180  GCCName = "gcc";
181 
182  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(GCCName));
183  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
184 }
185 
187  const JobAction &JA, ArgStringList &CmdArgs) const {
188  CmdArgs.push_back("-E");
189 }
190 
192  ArgStringList &CmdArgs) const {
193  const Driver &D = getToolChain().getDriver();
194 
195  switch (JA.getType()) {
196  // If -flto, etc. are present then make sure not to force assembly output.
197  case types::TY_LLVM_IR:
198  case types::TY_LTO_IR:
199  case types::TY_LLVM_BC:
200  case types::TY_LTO_BC:
201  CmdArgs.push_back("-c");
202  break;
203  // We assume we've got an "integrated" assembler in that gcc will produce an
204  // object file itself.
205  case types::TY_Object:
206  CmdArgs.push_back("-c");
207  break;
208  case types::TY_PP_Asm:
209  CmdArgs.push_back("-S");
210  break;
211  case types::TY_Nothing:
212  CmdArgs.push_back("-fsyntax-only");
213  break;
214  default:
215  D.Diag(diag::err_drv_invalid_gcc_output_type) << getTypeName(JA.getType());
216  }
217 }
218 
220  ArgStringList &CmdArgs) const {
221  // The types are (hopefully) good enough.
222 }
223 
224 static bool addXRayRuntime(const ToolChain &TC, const ArgList &Args,
225  ArgStringList &CmdArgs) {
226  // Do not add the XRay runtime to shared libraries.
227  if (Args.hasArg(options::OPT_shared))
228  return false;
229 
230  if (Args.hasFlag(options::OPT_fxray_instrument,
231  options::OPT_fnoxray_instrument, false)) {
232  CmdArgs.push_back("-whole-archive");
233  CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray", false));
234  CmdArgs.push_back("-no-whole-archive");
235  return true;
236  }
237 
238  return false;
239 }
240 
241 static void linkXRayRuntimeDeps(const ToolChain &TC, const ArgList &Args,
242  ArgStringList &CmdArgs) {
243  CmdArgs.push_back("--no-as-needed");
244  CmdArgs.push_back("-lpthread");
245  CmdArgs.push_back("-lrt");
246  CmdArgs.push_back("-lm");
247 
248  if (TC.getTriple().getOS() != llvm::Triple::FreeBSD &&
249  TC.getTriple().getOS() != llvm::Triple::NetBSD)
250  CmdArgs.push_back("-ldl");
251 }
252 
253 static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) {
254  switch (T.getArch()) {
255  case llvm::Triple::x86:
256  if (T.isOSIAMCU())
257  return "elf_iamcu";
258  return "elf_i386";
259  case llvm::Triple::aarch64:
260  return "aarch64linux";
261  case llvm::Triple::aarch64_be:
262  return "aarch64_be_linux";
263  case llvm::Triple::arm:
264  case llvm::Triple::thumb:
265  return "armelf_linux_eabi";
266  case llvm::Triple::armeb:
267  case llvm::Triple::thumbeb:
268  return "armelfb_linux_eabi";
269  case llvm::Triple::ppc:
270  return "elf32ppclinux";
271  case llvm::Triple::ppc64:
272  return "elf64ppc";
273  case llvm::Triple::ppc64le:
274  return "elf64lppc";
275  case llvm::Triple::riscv32:
276  return "elf32lriscv";
277  case llvm::Triple::riscv64:
278  return "elf64lriscv";
279  case llvm::Triple::sparc:
280  case llvm::Triple::sparcel:
281  return "elf32_sparc";
282  case llvm::Triple::sparcv9:
283  return "elf64_sparc";
284  case llvm::Triple::mips:
285  return "elf32btsmip";
286  case llvm::Triple::mipsel:
287  return "elf32ltsmip";
288  case llvm::Triple::mips64:
289  if (tools::mips::hasMipsAbiArg(Args, "n32"))
290  return "elf32btsmipn32";
291  return "elf64btsmip";
292  case llvm::Triple::mips64el:
293  if (tools::mips::hasMipsAbiArg(Args, "n32"))
294  return "elf32ltsmipn32";
295  return "elf64ltsmip";
296  case llvm::Triple::systemz:
297  return "elf64_s390";
298  case llvm::Triple::x86_64:
299  if (T.getEnvironment() == llvm::Triple::GNUX32)
300  return "elf32_x86_64";
301  return "elf_x86_64";
302  default:
303  return nullptr;
304  }
305 }
306 
307 static bool getPIE(const ArgList &Args, const toolchains::Linux &ToolChain) {
308  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static))
309  return false;
310 
311  Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie,
312  options::OPT_nopie);
313  if (!A)
314  return ToolChain.isPIEDefault();
315  return A->getOption().matches(options::OPT_pie);
316 }
317 
319  const InputInfo &Output,
320  const InputInfoList &Inputs,
321  const ArgList &Args,
322  const char *LinkingOutput) const {
324  static_cast<const toolchains::Linux &>(getToolChain());
325  const Driver &D = ToolChain.getDriver();
326 
327  const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
328 
329  const llvm::Triple::ArchType Arch = ToolChain.getArch();
330  const bool isAndroid = ToolChain.getTriple().isAndroid();
331  const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU();
332  const bool IsPIE = getPIE(Args, ToolChain);
333  const bool HasCRTBeginEndFiles =
334  ToolChain.getTriple().hasEnvironment() ||
335  (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);
336 
337  ArgStringList CmdArgs;
338 
339  // Silence warning for "clang -g foo.o -o foo"
340  Args.ClaimAllArgs(options::OPT_g_Group);
341  // and "clang -emit-llvm foo.o -o foo"
342  Args.ClaimAllArgs(options::OPT_emit_llvm);
343  // and for "clang -w foo.o -o foo". Other warning options are already
344  // handled somewhere else.
345  Args.ClaimAllArgs(options::OPT_w);
346 
347  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
348  if (llvm::sys::path::stem(Exec) == "lld") {
349  CmdArgs.push_back("-flavor");
350  CmdArgs.push_back("old-gnu");
351  CmdArgs.push_back("-target");
352  CmdArgs.push_back(Args.MakeArgString(getToolChain().getTripleString()));
353  }
354 
355  if (!D.SysRoot.empty())
356  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
357 
358  if (IsPIE)
359  CmdArgs.push_back("-pie");
360 
361  if (Args.hasArg(options::OPT_rdynamic))
362  CmdArgs.push_back("-export-dynamic");
363 
364  if (Args.hasArg(options::OPT_s))
365  CmdArgs.push_back("-s");
366 
367  if (Arch == llvm::Triple::armeb || Arch == llvm::Triple::thumbeb)
368  arm::appendEBLinkFlags(Args, CmdArgs, Triple);
369 
370  // Most Android ARM64 targets should enable the linker fix for erratum
371  // 843419. Only non-Cortex-A53 devices are allowed to skip this flag.
372  if (Arch == llvm::Triple::aarch64 && isAndroid) {
373  std::string CPU = getCPUName(Args, Triple);
374  if (CPU.empty() || CPU == "generic" || CPU == "cortex-a53")
375  CmdArgs.push_back("--fix-cortex-a53-843419");
376  }
377 
378  for (const auto &Opt : ToolChain.ExtraOpts)
379  CmdArgs.push_back(Opt.c_str());
380 
381  if (!Args.hasArg(options::OPT_static)) {
382  CmdArgs.push_back("--eh-frame-hdr");
383  }
384 
385  if (const char *LDMOption = getLDMOption(ToolChain.getTriple(), Args)) {
386  CmdArgs.push_back("-m");
387  CmdArgs.push_back(LDMOption);
388  } else {
389  D.Diag(diag::err_target_unknown_triple) << Triple.str();
390  return;
391  }
392 
393  if (Args.hasArg(options::OPT_static)) {
394  if (Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||
395  Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb)
396  CmdArgs.push_back("-Bstatic");
397  else
398  CmdArgs.push_back("-static");
399  } else if (Args.hasArg(options::OPT_shared)) {
400  CmdArgs.push_back("-shared");
401  }
402 
403  if (!Args.hasArg(options::OPT_static)) {
404  if (Args.hasArg(options::OPT_rdynamic))
405  CmdArgs.push_back("-export-dynamic");
406 
407  if (!Args.hasArg(options::OPT_shared)) {
408  const std::string Loader =
409  D.DyldPrefix + ToolChain.getDynamicLinker(Args);
410  CmdArgs.push_back("-dynamic-linker");
411  CmdArgs.push_back(Args.MakeArgString(Loader));
412  }
413  }
414 
415  CmdArgs.push_back("-o");
416  CmdArgs.push_back(Output.getFilename());
417 
418  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
419  if (!isAndroid && !IsIAMCU) {
420  const char *crt1 = nullptr;
421  if (!Args.hasArg(options::OPT_shared)) {
422  if (Args.hasArg(options::OPT_pg))
423  crt1 = "gcrt1.o";
424  else if (IsPIE)
425  crt1 = "Scrt1.o";
426  else
427  crt1 = "crt1.o";
428  }
429  if (crt1)
430  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
431 
432  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
433  }
434 
435  if (IsIAMCU)
436  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
437  else {
438  const char *crtbegin;
439  if (Args.hasArg(options::OPT_static))
440  crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o";
441  else if (Args.hasArg(options::OPT_shared))
442  crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o";
443  else if (IsPIE)
444  crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o";
445  else
446  crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o";
447 
448  if (HasCRTBeginEndFiles)
449  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
450  }
451 
452  // Add crtfastmath.o if available and fast math is enabled.
453  ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs);
454  }
455 
456  Args.AddAllArgs(CmdArgs, options::OPT_L);
457  Args.AddAllArgs(CmdArgs, options::OPT_u);
458 
459  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
460 
461  if (D.isUsingLTO())
462  AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D);
463 
464  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
465  CmdArgs.push_back("--no-demangle");
466 
467  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
468  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
469  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
470  // The profile runtime also needs access to system libraries.
471  getToolChain().addProfileRTLibs(Args, CmdArgs);
472 
473  if (D.CCCIsCXX() &&
474  !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
475  if (ToolChain.ShouldLinkCXXStdlib(Args)) {
476  bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
477  !Args.hasArg(options::OPT_static);
478  if (OnlyLibstdcxxStatic)
479  CmdArgs.push_back("-Bstatic");
480  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
481  if (OnlyLibstdcxxStatic)
482  CmdArgs.push_back("-Bdynamic");
483  }
484  CmdArgs.push_back("-lm");
485  }
486  // Silence warnings when linking C code with a C++ '-stdlib' argument.
487  Args.ClaimAllArgs(options::OPT_stdlib_EQ);
488 
489  if (!Args.hasArg(options::OPT_nostdlib)) {
490  if (!Args.hasArg(options::OPT_nodefaultlibs)) {
491  if (Args.hasArg(options::OPT_static))
492  CmdArgs.push_back("--start-group");
493 
494  if (NeedsSanitizerDeps)
495  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
496 
497  if (NeedsXRayDeps)
498  linkXRayRuntimeDeps(ToolChain, Args, CmdArgs);
499 
500  bool WantPthread = Args.hasArg(options::OPT_pthread) ||
501  Args.hasArg(options::OPT_pthreads);
502 
503  // FIXME: Only pass GompNeedsRT = true for platforms with libgomp that
504  // require librt. Most modern Linux platforms do, but some may not.
505  if (addOpenMPRuntime(CmdArgs, ToolChain, Args,
507  /* GompNeedsRT= */ true))
508  // OpenMP runtimes implies pthreads when using the GNU toolchain.
509  // FIXME: Does this really make sense for all GNU toolchains?
510  WantPthread = true;
511 
512  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
513 
514  if (WantPthread && !isAndroid)
515  CmdArgs.push_back("-lpthread");
516 
517  if (Args.hasArg(options::OPT_fsplit_stack))
518  CmdArgs.push_back("--wrap=pthread_create");
519 
520  CmdArgs.push_back("-lc");
521 
522  // Add IAMCU specific libs, if needed.
523  if (IsIAMCU)
524  CmdArgs.push_back("-lgloss");
525 
526  if (Args.hasArg(options::OPT_static))
527  CmdArgs.push_back("--end-group");
528  else
529  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
530 
531  // Add IAMCU specific libs (outside the group), if needed.
532  if (IsIAMCU) {
533  CmdArgs.push_back("--as-needed");
534  CmdArgs.push_back("-lsoftfp");
535  CmdArgs.push_back("--no-as-needed");
536  }
537  }
538 
539  if (!Args.hasArg(options::OPT_nostartfiles) && !IsIAMCU) {
540  const char *crtend;
541  if (Args.hasArg(options::OPT_shared))
542  crtend = isAndroid ? "crtend_so.o" : "crtendS.o";
543  else if (IsPIE)
544  crtend = isAndroid ? "crtend_android.o" : "crtendS.o";
545  else
546  crtend = isAndroid ? "crtend_android.o" : "crtend.o";
547 
548  if (HasCRTBeginEndFiles)
549  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
550  if (!isAndroid)
551  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
552  }
553  }
554 
555  // Add OpenMP offloading linker script args if required.
556  AddOpenMPLinkerScript(getToolChain(), C, Output, Inputs, Args, CmdArgs, JA);
557 
558  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
559 }
560 
562  const JobAction &JA,
563  const InputInfo &Output,
564  const InputInfoList &Inputs,
565  const ArgList &Args,
566  const char *LinkingOutput) const {
567  const auto &D = getToolChain().getDriver();
568 
569  claimNoWarnArgs(Args);
570 
571  ArgStringList CmdArgs;
572 
573  llvm::Reloc::Model RelocationModel;
574  unsigned PICLevel;
575  bool IsPIE;
576  std::tie(RelocationModel, PICLevel, IsPIE) =
577  ParsePICArgs(getToolChain(), Args);
578 
579  if (const Arg *A = Args.getLastArg(options::OPT_gz, options::OPT_gz_EQ)) {
580  if (A->getOption().getID() == options::OPT_gz) {
581  CmdArgs.push_back("-compress-debug-sections");
582  } else {
583  StringRef Value = A->getValue();
584  if (Value == "none") {
585  CmdArgs.push_back("-compress-debug-sections=none");
586  } else if (Value == "zlib" || Value == "zlib-gnu") {
587  CmdArgs.push_back(
588  Args.MakeArgString("-compress-debug-sections=" + Twine(Value)));
589  } else {
590  D.Diag(diag::err_drv_unsupported_option_argument)
591  << A->getOption().getName() << Value;
592  }
593  }
594  }
595 
596  switch (getToolChain().getArch()) {
597  default:
598  break;
599  // Add --32/--64 to make sure we get the format we want.
600  // This is incomplete
601  case llvm::Triple::x86:
602  CmdArgs.push_back("--32");
603  break;
604  case llvm::Triple::x86_64:
605  if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUX32)
606  CmdArgs.push_back("--x32");
607  else
608  CmdArgs.push_back("--64");
609  break;
610  case llvm::Triple::ppc: {
611  CmdArgs.push_back("-a32");
612  CmdArgs.push_back("-mppc");
613  CmdArgs.push_back(
614  ppc::getPPCAsmModeForCPU(getCPUName(Args, getToolChain().getTriple())));
615  break;
616  }
617  case llvm::Triple::ppc64: {
618  CmdArgs.push_back("-a64");
619  CmdArgs.push_back("-mppc64");
620  CmdArgs.push_back(
621  ppc::getPPCAsmModeForCPU(getCPUName(Args, getToolChain().getTriple())));
622  break;
623  }
624  case llvm::Triple::ppc64le: {
625  CmdArgs.push_back("-a64");
626  CmdArgs.push_back("-mppc64");
627  CmdArgs.push_back("-mlittle-endian");
628  CmdArgs.push_back(
629  ppc::getPPCAsmModeForCPU(getCPUName(Args, getToolChain().getTriple())));
630  break;
631  }
632  case llvm::Triple::riscv32:
633  case llvm::Triple::riscv64: {
634  StringRef ABIName = riscv::getRISCVABI(Args, getToolChain().getTriple());
635  CmdArgs.push_back("-mabi");
636  CmdArgs.push_back(ABIName.data());
637  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
638  StringRef MArch = A->getValue();
639  CmdArgs.push_back("-march");
640  CmdArgs.push_back(MArch.data());
641  }
642  break;
643  }
644  case llvm::Triple::sparc:
645  case llvm::Triple::sparcel: {
646  CmdArgs.push_back("-32");
647  std::string CPU = getCPUName(Args, getToolChain().getTriple());
648  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
649  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
650  break;
651  }
652  case llvm::Triple::sparcv9: {
653  CmdArgs.push_back("-64");
654  std::string CPU = getCPUName(Args, getToolChain().getTriple());
655  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
656  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
657  break;
658  }
659  case llvm::Triple::arm:
660  case llvm::Triple::armeb:
661  case llvm::Triple::thumb:
662  case llvm::Triple::thumbeb: {
663  const llvm::Triple &Triple2 = getToolChain().getTriple();
664  switch (Triple2.getSubArch()) {
665  case llvm::Triple::ARMSubArch_v7:
666  CmdArgs.push_back("-mfpu=neon");
667  break;
668  case llvm::Triple::ARMSubArch_v8:
669  CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8");
670  break;
671  default:
672  break;
673  }
674 
675  switch (arm::getARMFloatABI(getToolChain(), Args)) {
676  case arm::FloatABI::Invalid: llvm_unreachable("must have an ABI!");
677  case arm::FloatABI::Soft:
678  CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=soft"));
679  break;
680  case arm::FloatABI::SoftFP:
681  CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=softfp"));
682  break;
683  case arm::FloatABI::Hard:
684  CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=hard"));
685  break;
686  }
687 
688  Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
689  normalizeCPUNamesForAssembler(Args, CmdArgs);
690 
691  Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ);
692  break;
693  }
694  case llvm::Triple::aarch64:
695  case llvm::Triple::aarch64_be: {
696  Args.AddLastArg(CmdArgs, options::OPT_march_EQ);
697  normalizeCPUNamesForAssembler(Args, CmdArgs);
698 
699  break;
700  }
701  case llvm::Triple::mips:
702  case llvm::Triple::mipsel:
703  case llvm::Triple::mips64:
704  case llvm::Triple::mips64el: {
705  StringRef CPUName;
706  StringRef ABIName;
707  mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
708  ABIName = mips::getGnuCompatibleMipsABIName(ABIName);
709 
710  CmdArgs.push_back("-march");
711  CmdArgs.push_back(CPUName.data());
712 
713  CmdArgs.push_back("-mabi");
714  CmdArgs.push_back(ABIName.data());
715 
716  // -mno-shared should be emitted unless -fpic, -fpie, -fPIC, -fPIE,
717  // or -mshared (not implemented) is in effect.
718  if (RelocationModel == llvm::Reloc::Static)
719  CmdArgs.push_back("-mno-shared");
720 
721  // LLVM doesn't support -mplt yet and acts as if it is always given.
722  // However, -mplt has no effect with the N64 ABI.
723  if (ABIName != "64" && !Args.hasArg(options::OPT_mno_abicalls))
724  CmdArgs.push_back("-call_nonpic");
725 
726  if (getToolChain().getArch() == llvm::Triple::mips ||
727  getToolChain().getArch() == llvm::Triple::mips64)
728  CmdArgs.push_back("-EB");
729  else
730  CmdArgs.push_back("-EL");
731 
732  if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) {
733  if (StringRef(A->getValue()) == "2008")
734  CmdArgs.push_back(Args.MakeArgString("-mnan=2008"));
735  }
736 
737  // Add the last -mfp32/-mfpxx/-mfp64 or -mfpxx if it is enabled by default.
738  if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx,
739  options::OPT_mfp64)) {
740  A->claim();
741  A->render(Args, CmdArgs);
742  } else if (mips::shouldUseFPXX(
743  Args, getToolChain().getTriple(), CPUName, ABIName,
744  mips::getMipsFloatABI(getToolChain().getDriver(), Args)))
745  CmdArgs.push_back("-mfpxx");
746 
747  // Pass on -mmips16 or -mno-mips16. However, the assembler equivalent of
748  // -mno-mips16 is actually -no-mips16.
749  if (Arg *A =
750  Args.getLastArg(options::OPT_mips16, options::OPT_mno_mips16)) {
751  if (A->getOption().matches(options::OPT_mips16)) {
752  A->claim();
753  A->render(Args, CmdArgs);
754  } else {
755  A->claim();
756  CmdArgs.push_back("-no-mips16");
757  }
758  }
759 
760  Args.AddLastArg(CmdArgs, options::OPT_mmicromips,
761  options::OPT_mno_micromips);
762  Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp);
763  Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2);
764 
765  if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) {
766  // Do not use AddLastArg because not all versions of MIPS assembler
767  // support -mmsa / -mno-msa options.
768  if (A->getOption().matches(options::OPT_mmsa))
769  CmdArgs.push_back(Args.MakeArgString("-mmsa"));
770  }
771 
772  Args.AddLastArg(CmdArgs, options::OPT_mhard_float,
773  options::OPT_msoft_float);
774 
775  Args.AddLastArg(CmdArgs, options::OPT_mdouble_float,
776  options::OPT_msingle_float);
777 
778  Args.AddLastArg(CmdArgs, options::OPT_modd_spreg,
779  options::OPT_mno_odd_spreg);
780 
781  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
782  break;
783  }
784  case llvm::Triple::systemz: {
785  // Always pass an -march option, since our default of z10 is later
786  // than the GNU assembler's default.
787  StringRef CPUName = systemz::getSystemZTargetCPU(Args);
788  CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName));
789  break;
790  }
791  }
792 
793  Args.AddAllArgs(CmdArgs, options::OPT_I);
794  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
795 
796  CmdArgs.push_back("-o");
797  CmdArgs.push_back(Output.getFilename());
798 
799  for (const auto &II : Inputs)
800  CmdArgs.push_back(II.getFilename());
801 
802  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
803  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
804 
805  // Handle the debug info splitting at object creation time if we're
806  // creating an object.
807  // TODO: Currently only works on linux with newer objcopy.
808  if (Args.hasArg(options::OPT_gsplit_dwarf) &&
809  getToolChain().getTriple().isOSLinux())
810  SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
811  SplitDebugName(Args, Inputs[0]));
812 }
813 
814 namespace {
815 // Filter to remove Multilibs that don't exist as a suffix to Path
816 class FilterNonExistent {
817  StringRef Base, File;
818  vfs::FileSystem &VFS;
819 
820 public:
821  FilterNonExistent(StringRef Base, StringRef File, vfs::FileSystem &VFS)
822  : Base(Base), File(File), VFS(VFS) {}
823  bool operator()(const Multilib &M) {
824  return !VFS.exists(Base + M.gccSuffix() + File);
825  }
826 };
827 } // end anonymous namespace
828 
829 static bool isSoftFloatABI(const ArgList &Args) {
830  Arg *A = Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
831  options::OPT_mfloat_abi_EQ);
832  if (!A)
833  return false;
834 
835  return A->getOption().matches(options::OPT_msoft_float) ||
836  (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
837  A->getValue() == StringRef("soft"));
838 }
839 
840 /// \p Flag must be a flag accepted by the driver with its leading '-' removed,
841 // otherwise '-print-multi-lib' will not emit them correctly.
842 static void addMultilibFlag(bool Enabled, const char *const Flag,
843  std::vector<std::string> &Flags) {
844  if (Enabled)
845  Flags.push_back(std::string("+") + Flag);
846  else
847  Flags.push_back(std::string("-") + Flag);
848 }
849 
850 static bool isArmOrThumbArch(llvm::Triple::ArchType Arch) {
851  return Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb;
852 }
853 
854 static bool isMips32(llvm::Triple::ArchType Arch) {
855  return Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel;
856 }
857 
858 static bool isMips64(llvm::Triple::ArchType Arch) {
859  return Arch == llvm::Triple::mips64 || Arch == llvm::Triple::mips64el;
860 }
861 
862 static bool isMipsEL(llvm::Triple::ArchType Arch) {
863  return Arch == llvm::Triple::mipsel || Arch == llvm::Triple::mips64el;
864 }
865 
866 static bool isMips16(const ArgList &Args) {
867  Arg *A = Args.getLastArg(options::OPT_mips16, options::OPT_mno_mips16);
868  return A && A->getOption().matches(options::OPT_mips16);
869 }
870 
871 static bool isMicroMips(const ArgList &Args) {
872  Arg *A = Args.getLastArg(options::OPT_mmicromips, options::OPT_mno_micromips);
873  return A && A->getOption().matches(options::OPT_mmicromips);
874 }
875 
876 static bool isRISCV(llvm::Triple::ArchType Arch) {
877  return Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64;
878 }
879 
880 static Multilib makeMultilib(StringRef commonSuffix) {
881  return Multilib(commonSuffix, commonSuffix, commonSuffix);
882 }
883 
884 static bool findMipsCsMultilibs(const Multilib::flags_list &Flags,
885  FilterNonExistent &NonExistent,
886  DetectedMultilibs &Result) {
887  // Check for Code Sourcery toolchain multilibs
888  MultilibSet CSMipsMultilibs;
889  {
890  auto MArchMips16 = makeMultilib("/mips16").flag("+m32").flag("+mips16");
891 
892  auto MArchMicroMips =
893  makeMultilib("/micromips").flag("+m32").flag("+mmicromips");
894 
895  auto MArchDefault = makeMultilib("").flag("-mips16").flag("-mmicromips");
896 
897  auto UCLibc = makeMultilib("/uclibc").flag("+muclibc");
898 
899  auto SoftFloat = makeMultilib("/soft-float").flag("+msoft-float");
900 
901  auto Nan2008 = makeMultilib("/nan2008").flag("+mnan=2008");
902 
903  auto DefaultFloat =
904  makeMultilib("").flag("-msoft-float").flag("-mnan=2008");
905 
906  auto BigEndian = makeMultilib("").flag("+EB").flag("-EL");
907 
908  auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB");
909 
910  // Note that this one's osSuffix is ""
911  auto MAbi64 = makeMultilib("")
912  .gccSuffix("/64")
913  .includeSuffix("/64")
914  .flag("+mabi=n64")
915  .flag("-mabi=n32")
916  .flag("-m32");
917 
918  CSMipsMultilibs =
919  MultilibSet()
920  .Either(MArchMips16, MArchMicroMips, MArchDefault)
921  .Maybe(UCLibc)
922  .Either(SoftFloat, Nan2008, DefaultFloat)
923  .FilterOut("/micromips/nan2008")
924  .FilterOut("/mips16/nan2008")
925  .Either(BigEndian, LittleEndian)
926  .Maybe(MAbi64)
927  .FilterOut("/mips16.*/64")
928  .FilterOut("/micromips.*/64")
929  .FilterOut(NonExistent)
930  .setIncludeDirsCallback([](const Multilib &M) {
931  std::vector<std::string> Dirs({"/include"});
932  if (StringRef(M.includeSuffix()).startswith("/uclibc"))
933  Dirs.push_back(
934  "/../../../../mips-linux-gnu/libc/uclibc/usr/include");
935  else
936  Dirs.push_back("/../../../../mips-linux-gnu/libc/usr/include");
937  return Dirs;
938  });
939  }
940 
941  MultilibSet DebianMipsMultilibs;
942  {
943  Multilib MAbiN32 =
944  Multilib().gccSuffix("/n32").includeSuffix("/n32").flag("+mabi=n32");
945 
946  Multilib M64 = Multilib()
947  .gccSuffix("/64")
948  .includeSuffix("/64")
949  .flag("+m64")
950  .flag("-m32")
951  .flag("-mabi=n32");
952 
953  Multilib M32 = Multilib().flag("-m64").flag("+m32").flag("-mabi=n32");
954 
955  DebianMipsMultilibs =
956  MultilibSet().Either(M32, M64, MAbiN32).FilterOut(NonExistent);
957  }
958 
959  // Sort candidates. Toolchain that best meets the directories tree goes first.
960  // Then select the first toolchains matches command line flags.
961  MultilibSet *Candidates[] = {&CSMipsMultilibs, &DebianMipsMultilibs};
962  if (CSMipsMultilibs.size() < DebianMipsMultilibs.size())
963  std::iter_swap(Candidates, Candidates + 1);
964  for (const MultilibSet *Candidate : Candidates) {
965  if (Candidate->select(Flags, Result.SelectedMultilib)) {
966  if (Candidate == &DebianMipsMultilibs)
967  Result.BiarchSibling = Multilib();
968  Result.Multilibs = *Candidate;
969  return true;
970  }
971  }
972  return false;
973 }
974 
975 static bool findMipsAndroidMultilibs(vfs::FileSystem &VFS, StringRef Path,
976  const Multilib::flags_list &Flags,
977  FilterNonExistent &NonExistent,
978  DetectedMultilibs &Result) {
979 
980  MultilibSet AndroidMipsMultilibs =
981  MultilibSet()
982  .Maybe(Multilib("/mips-r2").flag("+march=mips32r2"))
983  .Maybe(Multilib("/mips-r6").flag("+march=mips32r6"))
984  .FilterOut(NonExistent);
985 
986  MultilibSet AndroidMipselMultilibs =
987  MultilibSet()
988  .Either(Multilib().flag("+march=mips32"),
989  Multilib("/mips-r2", "", "/mips-r2").flag("+march=mips32r2"),
990  Multilib("/mips-r6", "", "/mips-r6").flag("+march=mips32r6"))
991  .FilterOut(NonExistent);
992 
993  MultilibSet AndroidMips64elMultilibs =
994  MultilibSet()
995  .Either(
996  Multilib().flag("+march=mips64r6"),
997  Multilib("/32/mips-r1", "", "/mips-r1").flag("+march=mips32"),
998  Multilib("/32/mips-r2", "", "/mips-r2").flag("+march=mips32r2"),
999  Multilib("/32/mips-r6", "", "/mips-r6").flag("+march=mips32r6"))
1000  .FilterOut(NonExistent);
1001 
1002  MultilibSet *MS = &AndroidMipsMultilibs;
1003  if (VFS.exists(Path + "/mips-r6"))
1004  MS = &AndroidMipselMultilibs;
1005  else if (VFS.exists(Path + "/32"))
1006  MS = &AndroidMips64elMultilibs;
1007  if (MS->select(Flags, Result.SelectedMultilib)) {
1008  Result.Multilibs = *MS;
1009  return true;
1010  }
1011  return false;
1012 }
1013 
1015  FilterNonExistent &NonExistent,
1016  DetectedMultilibs &Result) {
1017  // Musl toolchain multilibs
1018  MultilibSet MuslMipsMultilibs;
1019  {
1020  auto MArchMipsR2 = makeMultilib("")
1021  .osSuffix("/mips-r2-hard-musl")
1022  .flag("+EB")
1023  .flag("-EL")
1024  .flag("+march=mips32r2");
1025 
1026  auto MArchMipselR2 = makeMultilib("/mipsel-r2-hard-musl")
1027  .flag("-EB")
1028  .flag("+EL")
1029  .flag("+march=mips32r2");
1030 
1031  MuslMipsMultilibs = MultilibSet().Either(MArchMipsR2, MArchMipselR2);
1032 
1033  // Specify the callback that computes the include directories.
1034  MuslMipsMultilibs.setIncludeDirsCallback([](const Multilib &M) {
1035  return std::vector<std::string>(
1036  {"/../sysroot" + M.osSuffix() + "/usr/include"});
1037  });
1038  }
1039  if (MuslMipsMultilibs.select(Flags, Result.SelectedMultilib)) {
1040  Result.Multilibs = MuslMipsMultilibs;
1041  return true;
1042  }
1043  return false;
1044 }
1045 
1047  FilterNonExistent &NonExistent,
1048  DetectedMultilibs &Result) {
1049  // CodeScape MTI toolchain v1.2 and early.
1050  MultilibSet MtiMipsMultilibsV1;
1051  {
1052  auto MArchMips32 = makeMultilib("/mips32")
1053  .flag("+m32")
1054  .flag("-m64")
1055  .flag("-mmicromips")
1056  .flag("+march=mips32");
1057 
1058  auto MArchMicroMips = makeMultilib("/micromips")
1059  .flag("+m32")
1060  .flag("-m64")
1061  .flag("+mmicromips");
1062 
1063  auto MArchMips64r2 = makeMultilib("/mips64r2")
1064  .flag("-m32")
1065  .flag("+m64")
1066  .flag("+march=mips64r2");
1067 
1068  auto MArchMips64 = makeMultilib("/mips64").flag("-m32").flag("+m64").flag(
1069  "-march=mips64r2");
1070 
1071  auto MArchDefault = makeMultilib("")
1072  .flag("+m32")
1073  .flag("-m64")
1074  .flag("-mmicromips")
1075  .flag("+march=mips32r2");
1076 
1077  auto Mips16 = makeMultilib("/mips16").flag("+mips16");
1078 
1079  auto UCLibc = makeMultilib("/uclibc").flag("+muclibc");
1080 
1081  auto MAbi64 =
1082  makeMultilib("/64").flag("+mabi=n64").flag("-mabi=n32").flag("-m32");
1083 
1084  auto BigEndian = makeMultilib("").flag("+EB").flag("-EL");
1085 
1086  auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB");
1087 
1088  auto SoftFloat = makeMultilib("/sof").flag("+msoft-float");
1089 
1090  auto Nan2008 = makeMultilib("/nan2008").flag("+mnan=2008");
1091 
1092  MtiMipsMultilibsV1 =
1093  MultilibSet()
1094  .Either(MArchMips32, MArchMicroMips, MArchMips64r2, MArchMips64,
1095  MArchDefault)
1096  .Maybe(UCLibc)
1097  .Maybe(Mips16)
1098  .FilterOut("/mips64/mips16")
1099  .FilterOut("/mips64r2/mips16")
1100  .FilterOut("/micromips/mips16")
1101  .Maybe(MAbi64)
1102  .FilterOut("/micromips/64")
1103  .FilterOut("/mips32/64")
1104  .FilterOut("^/64")
1105  .FilterOut("/mips16/64")
1106  .Either(BigEndian, LittleEndian)
1107  .Maybe(SoftFloat)
1108  .Maybe(Nan2008)
1109  .FilterOut(".*sof/nan2008")
1110  .FilterOut(NonExistent)
1111  .setIncludeDirsCallback([](const Multilib &M) {
1112  std::vector<std::string> Dirs({"/include"});
1113  if (StringRef(M.includeSuffix()).startswith("/uclibc"))
1114  Dirs.push_back("/../../../../sysroot/uclibc/usr/include");
1115  else
1116  Dirs.push_back("/../../../../sysroot/usr/include");
1117  return Dirs;
1118  });
1119  }
1120 
1121  // CodeScape IMG toolchain starting from v1.3.
1122  MultilibSet MtiMipsMultilibsV2;
1123  {
1124  auto BeHard = makeMultilib("/mips-r2-hard")
1125  .flag("+EB")
1126  .flag("-msoft-float")
1127  .flag("-mnan=2008")
1128  .flag("-muclibc");
1129  auto BeSoft = makeMultilib("/mips-r2-soft")
1130  .flag("+EB")
1131  .flag("+msoft-float")
1132  .flag("-mnan=2008");
1133  auto ElHard = makeMultilib("/mipsel-r2-hard")
1134  .flag("+EL")
1135  .flag("-msoft-float")
1136  .flag("-mnan=2008")
1137  .flag("-muclibc");
1138  auto ElSoft = makeMultilib("/mipsel-r2-soft")
1139  .flag("+EL")
1140  .flag("+msoft-float")
1141  .flag("-mnan=2008")
1142  .flag("-mmicromips");
1143  auto BeHardNan = makeMultilib("/mips-r2-hard-nan2008")
1144  .flag("+EB")
1145  .flag("-msoft-float")
1146  .flag("+mnan=2008")
1147  .flag("-muclibc");
1148  auto ElHardNan = makeMultilib("/mipsel-r2-hard-nan2008")
1149  .flag("+EL")
1150  .flag("-msoft-float")
1151  .flag("+mnan=2008")
1152  .flag("-muclibc")
1153  .flag("-mmicromips");
1154  auto BeHardNanUclibc = makeMultilib("/mips-r2-hard-nan2008-uclibc")
1155  .flag("+EB")
1156  .flag("-msoft-float")
1157  .flag("+mnan=2008")
1158  .flag("+muclibc");
1159  auto ElHardNanUclibc = makeMultilib("/mipsel-r2-hard-nan2008-uclibc")
1160  .flag("+EL")
1161  .flag("-msoft-float")
1162  .flag("+mnan=2008")
1163  .flag("+muclibc");
1164  auto BeHardUclibc = makeMultilib("/mips-r2-hard-uclibc")
1165  .flag("+EB")
1166  .flag("-msoft-float")
1167  .flag("-mnan=2008")
1168  .flag("+muclibc");
1169  auto ElHardUclibc = makeMultilib("/mipsel-r2-hard-uclibc")
1170  .flag("+EL")
1171  .flag("-msoft-float")
1172  .flag("-mnan=2008")
1173  .flag("+muclibc");
1174  auto ElMicroHardNan = makeMultilib("/micromipsel-r2-hard-nan2008")
1175  .flag("+EL")
1176  .flag("-msoft-float")
1177  .flag("+mnan=2008")
1178  .flag("+mmicromips");
1179  auto ElMicroSoft = makeMultilib("/micromipsel-r2-soft")
1180  .flag("+EL")
1181  .flag("+msoft-float")
1182  .flag("-mnan=2008")
1183  .flag("+mmicromips");
1184 
1185  auto O32 =
1186  makeMultilib("/lib").osSuffix("").flag("-mabi=n32").flag("-mabi=n64");
1187  auto N32 =
1188  makeMultilib("/lib32").osSuffix("").flag("+mabi=n32").flag("-mabi=n64");
1189  auto N64 =
1190  makeMultilib("/lib64").osSuffix("").flag("-mabi=n32").flag("+mabi=n64");
1191 
1192  MtiMipsMultilibsV2 =
1193  MultilibSet()
1194  .Either({BeHard, BeSoft, ElHard, ElSoft, BeHardNan, ElHardNan,
1195  BeHardNanUclibc, ElHardNanUclibc, BeHardUclibc,
1196  ElHardUclibc, ElMicroHardNan, ElMicroSoft})
1197  .Either(O32, N32, N64)
1198  .FilterOut(NonExistent)
1199  .setIncludeDirsCallback([](const Multilib &M) {
1200  return std::vector<std::string>({"/../../../../sysroot" +
1201  M.includeSuffix() +
1202  "/../usr/include"});
1203  })
1204  .setFilePathsCallback([](const Multilib &M) {
1205  return std::vector<std::string>(
1206  {"/../../../../mips-mti-linux-gnu/lib" + M.gccSuffix()});
1207  });
1208  }
1209  for (auto Candidate : {&MtiMipsMultilibsV1, &MtiMipsMultilibsV2}) {
1210  if (Candidate->select(Flags, Result.SelectedMultilib)) {
1211  Result.Multilibs = *Candidate;
1212  return true;
1213  }
1214  }
1215  return false;
1216 }
1217 
1219  FilterNonExistent &NonExistent,
1220  DetectedMultilibs &Result) {
1221  // CodeScape IMG toolchain v1.2 and early.
1222  MultilibSet ImgMultilibsV1;
1223  {
1224  auto Mips64r6 = makeMultilib("/mips64r6").flag("+m64").flag("-m32");
1225 
1226  auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB");
1227 
1228  auto MAbi64 =
1229  makeMultilib("/64").flag("+mabi=n64").flag("-mabi=n32").flag("-m32");
1230 
1231  ImgMultilibsV1 =
1232  MultilibSet()
1233  .Maybe(Mips64r6)
1234  .Maybe(MAbi64)
1235  .Maybe(LittleEndian)
1236  .FilterOut(NonExistent)
1237  .setIncludeDirsCallback([](const Multilib &M) {
1238  return std::vector<std::string>(
1239  {"/include", "/../../../../sysroot/usr/include"});
1240  });
1241  }
1242 
1243  // CodeScape IMG toolchain starting from v1.3.
1244  MultilibSet ImgMultilibsV2;
1245  {
1246  auto BeHard = makeMultilib("/mips-r6-hard")
1247  .flag("+EB")
1248  .flag("-msoft-float")
1249  .flag("-mmicromips");
1250  auto BeSoft = makeMultilib("/mips-r6-soft")
1251  .flag("+EB")
1252  .flag("+msoft-float")
1253  .flag("-mmicromips");
1254  auto ElHard = makeMultilib("/mipsel-r6-hard")
1255  .flag("+EL")
1256  .flag("-msoft-float")
1257  .flag("-mmicromips");
1258  auto ElSoft = makeMultilib("/mipsel-r6-soft")
1259  .flag("+EL")
1260  .flag("+msoft-float")
1261  .flag("-mmicromips");
1262  auto BeMicroHard = makeMultilib("/micromips-r6-hard")
1263  .flag("+EB")
1264  .flag("-msoft-float")
1265  .flag("+mmicromips");
1266  auto BeMicroSoft = makeMultilib("/micromips-r6-soft")
1267  .flag("+EB")
1268  .flag("+msoft-float")
1269  .flag("+mmicromips");
1270  auto ElMicroHard = makeMultilib("/micromipsel-r6-hard")
1271  .flag("+EL")
1272  .flag("-msoft-float")
1273  .flag("+mmicromips");
1274  auto ElMicroSoft = makeMultilib("/micromipsel-r6-soft")
1275  .flag("+EL")
1276  .flag("+msoft-float")
1277  .flag("+mmicromips");
1278 
1279  auto O32 =
1280  makeMultilib("/lib").osSuffix("").flag("-mabi=n32").flag("-mabi=n64");
1281  auto N32 =
1282  makeMultilib("/lib32").osSuffix("").flag("+mabi=n32").flag("-mabi=n64");
1283  auto N64 =
1284  makeMultilib("/lib64").osSuffix("").flag("-mabi=n32").flag("+mabi=n64");
1285 
1286  ImgMultilibsV2 =
1287  MultilibSet()
1288  .Either({BeHard, BeSoft, ElHard, ElSoft, BeMicroHard, BeMicroSoft,
1289  ElMicroHard, ElMicroSoft})
1290  .Either(O32, N32, N64)
1291  .FilterOut(NonExistent)
1292  .setIncludeDirsCallback([](const Multilib &M) {
1293  return std::vector<std::string>({"/../../../../sysroot" +
1294  M.includeSuffix() +
1295  "/../usr/include"});
1296  })
1297  .setFilePathsCallback([](const Multilib &M) {
1298  return std::vector<std::string>(
1299  {"/../../../../mips-img-linux-gnu/lib" + M.gccSuffix()});
1300  });
1301  }
1302  for (auto Candidate : {&ImgMultilibsV1, &ImgMultilibsV2}) {
1303  if (Candidate->select(Flags, Result.SelectedMultilib)) {
1304  Result.Multilibs = *Candidate;
1305  return true;
1306  }
1307  }
1308  return false;
1309 }
1310 
1312  const llvm::Triple &TargetTriple,
1313  StringRef Path, const ArgList &Args,
1314  DetectedMultilibs &Result) {
1315  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1316 
1317  StringRef CPUName;
1318  StringRef ABIName;
1319  tools::mips::getMipsCPUAndABI(Args, TargetTriple, CPUName, ABIName);
1320 
1321  llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
1322 
1323  Multilib::flags_list Flags;
1324  addMultilibFlag(isMips32(TargetArch), "m32", Flags);
1325  addMultilibFlag(isMips64(TargetArch), "m64", Flags);
1326  addMultilibFlag(isMips16(Args), "mips16", Flags);
1327  addMultilibFlag(CPUName == "mips32", "march=mips32", Flags);
1328  addMultilibFlag(CPUName == "mips32r2" || CPUName == "mips32r3" ||
1329  CPUName == "mips32r5" || CPUName == "p5600",
1330  "march=mips32r2", Flags);
1331  addMultilibFlag(CPUName == "mips32r6", "march=mips32r6", Flags);
1332  addMultilibFlag(CPUName == "mips64", "march=mips64", Flags);
1333  addMultilibFlag(CPUName == "mips64r2" || CPUName == "mips64r3" ||
1334  CPUName == "mips64r5" || CPUName == "octeon",
1335  "march=mips64r2", Flags);
1336  addMultilibFlag(CPUName == "mips64r6", "march=mips64r6", Flags);
1337  addMultilibFlag(isMicroMips(Args), "mmicromips", Flags);
1338  addMultilibFlag(tools::mips::isUCLibc(Args), "muclibc", Flags);
1339  addMultilibFlag(tools::mips::isNaN2008(Args, TargetTriple), "mnan=2008",
1340  Flags);
1341  addMultilibFlag(ABIName == "n32", "mabi=n32", Flags);
1342  addMultilibFlag(ABIName == "n64", "mabi=n64", Flags);
1343  addMultilibFlag(isSoftFloatABI(Args), "msoft-float", Flags);
1344  addMultilibFlag(!isSoftFloatABI(Args), "mhard-float", Flags);
1345  addMultilibFlag(isMipsEL(TargetArch), "EL", Flags);
1346  addMultilibFlag(!isMipsEL(TargetArch), "EB", Flags);
1347 
1348  if (TargetTriple.isAndroid())
1349  return findMipsAndroidMultilibs(D.getVFS(), Path, Flags, NonExistent,
1350  Result);
1351 
1352  if (TargetTriple.getVendor() == llvm::Triple::MipsTechnologies &&
1353  TargetTriple.getOS() == llvm::Triple::Linux &&
1354  TargetTriple.getEnvironment() == llvm::Triple::UnknownEnvironment)
1355  return findMipsMuslMultilibs(Flags, NonExistent, Result);
1356 
1357  if (TargetTriple.getVendor() == llvm::Triple::MipsTechnologies &&
1358  TargetTriple.getOS() == llvm::Triple::Linux &&
1359  TargetTriple.isGNUEnvironment())
1360  return findMipsMtiMultilibs(Flags, NonExistent, Result);
1361 
1362  if (TargetTriple.getVendor() == llvm::Triple::ImaginationTechnologies &&
1363  TargetTriple.getOS() == llvm::Triple::Linux &&
1364  TargetTriple.isGNUEnvironment())
1365  return findMipsImgMultilibs(Flags, NonExistent, Result);
1366 
1367  if (findMipsCsMultilibs(Flags, NonExistent, Result))
1368  return true;
1369 
1370  // Fallback to the regular toolchain-tree structure.
1371  Multilib Default;
1372  Result.Multilibs.push_back(Default);
1373  Result.Multilibs.FilterOut(NonExistent);
1374 
1375  if (Result.Multilibs.select(Flags, Result.SelectedMultilib)) {
1376  Result.BiarchSibling = Multilib();
1377  return true;
1378  }
1379 
1380  return false;
1381 }
1382 
1383 static void findAndroidArmMultilibs(const Driver &D,
1384  const llvm::Triple &TargetTriple,
1385  StringRef Path, const ArgList &Args,
1386  DetectedMultilibs &Result) {
1387  // Find multilibs with subdirectories like armv7-a, thumb, armv7-a/thumb.
1388  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1389  Multilib ArmV7Multilib = makeMultilib("/armv7-a")
1390  .flag("+march=armv7-a")
1391  .flag("-mthumb");
1392  Multilib ThumbMultilib = makeMultilib("/thumb")
1393  .flag("-march=armv7-a")
1394  .flag("+mthumb");
1395  Multilib ArmV7ThumbMultilib = makeMultilib("/armv7-a/thumb")
1396  .flag("+march=armv7-a")
1397  .flag("+mthumb");
1398  Multilib DefaultMultilib = makeMultilib("")
1399  .flag("-march=armv7-a")
1400  .flag("-mthumb");
1401  MultilibSet AndroidArmMultilibs =
1402  MultilibSet()
1403  .Either(ThumbMultilib, ArmV7Multilib,
1404  ArmV7ThumbMultilib, DefaultMultilib)
1405  .FilterOut(NonExistent);
1406 
1407  Multilib::flags_list Flags;
1408  llvm::StringRef Arch = Args.getLastArgValue(options::OPT_march_EQ);
1409  bool IsArmArch = TargetTriple.getArch() == llvm::Triple::arm;
1410  bool IsThumbArch = TargetTriple.getArch() == llvm::Triple::thumb;
1411  bool IsV7SubArch = TargetTriple.getSubArch() == llvm::Triple::ARMSubArch_v7;
1412  bool IsThumbMode = IsThumbArch ||
1413  Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, false) ||
1414  (IsArmArch && llvm::ARM::parseArchISA(Arch) == llvm::ARM::ISAKind::THUMB);
1415  bool IsArmV7Mode = (IsArmArch || IsThumbArch) &&
1416  (llvm::ARM::parseArchVersion(Arch) == 7 ||
1417  (IsArmArch && Arch == "" && IsV7SubArch));
1418  addMultilibFlag(IsArmV7Mode, "march=armv7-a", Flags);
1419  addMultilibFlag(IsThumbMode, "mthumb", Flags);
1420 
1421  if (AndroidArmMultilibs.select(Flags, Result.SelectedMultilib))
1422  Result.Multilibs = AndroidArmMultilibs;
1423 }
1424 
1425 static void findRISCVMultilibs(const Driver &D,
1426  const llvm::Triple &TargetTriple, StringRef Path,
1427  const ArgList &Args, DetectedMultilibs &Result) {
1428 
1429  FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS());
1430  Multilib Ilp32 = makeMultilib("lib32/ilp32").flag("+m32").flag("+mabi=ilp32");
1431  Multilib Ilp32f =
1432  makeMultilib("lib32/ilp32f").flag("+m32").flag("+mabi=ilp32f");
1433  Multilib Ilp32d =
1434  makeMultilib("lib32/ilp32d").flag("+m32").flag("+mabi=ilp32d");
1435  Multilib Lp64 = makeMultilib("lib64/lp64").flag("+m64").flag("+mabi=lp64");
1436  Multilib Lp64f = makeMultilib("lib64/lp64f").flag("+m64").flag("+mabi=lp64f");
1437  Multilib Lp64d = makeMultilib("lib64/lp64d").flag("+m64").flag("+mabi=lp64d");
1438  MultilibSet RISCVMultilibs =
1439  MultilibSet()
1440  .Either({Ilp32, Ilp32f, Ilp32d, Lp64, Lp64f, Lp64d})
1441  .FilterOut(NonExistent);
1442 
1443  Multilib::flags_list Flags;
1444  bool IsRV64 = TargetTriple.getArch() == llvm::Triple::riscv64;
1445  StringRef ABIName = tools::riscv::getRISCVABI(Args, TargetTriple);
1446 
1447  addMultilibFlag(!IsRV64, "m32", Flags);
1448  addMultilibFlag(IsRV64, "m64", Flags);
1449  addMultilibFlag(ABIName == "ilp32", "mabi=ilp32", Flags);
1450  addMultilibFlag(ABIName == "ilp32f", "mabi=ilp32f", Flags);
1451  addMultilibFlag(ABIName == "ilp32d", "mabi=ilp32d", Flags);
1452  addMultilibFlag(ABIName == "lp64", "mabi=lp64", Flags);
1453  addMultilibFlag(ABIName == "lp64f", "mabi=lp64f", Flags);
1454  addMultilibFlag(ABIName == "lp64d", "mabi=lp64d", Flags);
1455 
1456  if (RISCVMultilibs.select(Flags, Result.SelectedMultilib))
1457  Result.Multilibs = RISCVMultilibs;
1458 }
1459 
1460 static bool findBiarchMultilibs(const Driver &D,
1461  const llvm::Triple &TargetTriple,
1462  StringRef Path, const ArgList &Args,
1463  bool NeedsBiarchSuffix,
1464  DetectedMultilibs &Result) {
1465  Multilib Default;
1466 
1467  // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
1468  // in what would normally be GCCInstallPath and put the 64-bit
1469  // libs in a subdirectory named 64. The simple logic we follow is that
1470  // *if* there is a subdirectory of the right name with crtbegin.o in it,
1471  // we use that. If not, and if not a biarch triple alias, we look for
1472  // crtbegin.o without the subdirectory.
1473 
1474  StringRef Suff64 = "/64";
1475  // Solaris uses platform-specific suffixes instead of /64.
1476  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
1477  switch (TargetTriple.getArch()) {
1478  case llvm::Triple::x86:
1479  case llvm::Triple::x86_64:
1480  Suff64 = "/amd64";
1481  break;
1482  case llvm::Triple::sparc:
1483  case llvm::Triple::sparcv9:
1484  Suff64 = "/sparcv9";
1485  break;
1486  default:
1487  break;
1488  }
1489  }
1490 
1491  Multilib Alt64 = Multilib()
1492  .gccSuffix(Suff64)
1493  .includeSuffix(Suff64)
1494  .flag("-m32")
1495  .flag("+m64")
1496  .flag("-mx32");
1497  Multilib Alt32 = Multilib()
1498  .gccSuffix("/32")
1499  .includeSuffix("/32")
1500  .flag("+m32")
1501  .flag("-m64")
1502  .flag("-mx32");
1503  Multilib Altx32 = Multilib()
1504  .gccSuffix("/x32")
1505  .includeSuffix("/x32")
1506  .flag("-m32")
1507  .flag("-m64")
1508  .flag("+mx32");
1509 
1510  // GCC toolchain for IAMCU doesn't have crtbegin.o, so look for libgcc.a.
1511  FilterNonExistent NonExistent(
1512  Path, TargetTriple.isOSIAMCU() ? "/libgcc.a" : "/crtbegin.o", D.getVFS());
1513 
1514  // Determine default multilib from: 32, 64, x32
1515  // Also handle cases such as 64 on 32, 32 on 64, etc.
1516  enum { UNKNOWN, WANT32, WANT64, WANTX32 } Want = UNKNOWN;
1517  const bool IsX32 = TargetTriple.getEnvironment() == llvm::Triple::GNUX32;
1518  if (TargetTriple.isArch32Bit() && !NonExistent(Alt32))
1519  Want = WANT64;
1520  else if (TargetTriple.isArch64Bit() && IsX32 && !NonExistent(Altx32))
1521  Want = WANT64;
1522  else if (TargetTriple.isArch64Bit() && !IsX32 && !NonExistent(Alt64))
1523  Want = WANT32;
1524  else {
1525  if (TargetTriple.isArch32Bit())
1526  Want = NeedsBiarchSuffix ? WANT64 : WANT32;
1527  else if (IsX32)
1528  Want = NeedsBiarchSuffix ? WANT64 : WANTX32;
1529  else
1530  Want = NeedsBiarchSuffix ? WANT32 : WANT64;
1531  }
1532 
1533  if (Want == WANT32)
1534  Default.flag("+m32").flag("-m64").flag("-mx32");
1535  else if (Want == WANT64)
1536  Default.flag("-m32").flag("+m64").flag("-mx32");
1537  else if (Want == WANTX32)
1538  Default.flag("-m32").flag("-m64").flag("+mx32");
1539  else
1540  return false;
1541 
1542  Result.Multilibs.push_back(Default);
1543  Result.Multilibs.push_back(Alt64);
1544  Result.Multilibs.push_back(Alt32);
1545  Result.Multilibs.push_back(Altx32);
1546 
1547  Result.Multilibs.FilterOut(NonExistent);
1548 
1549  Multilib::flags_list Flags;
1550  addMultilibFlag(TargetTriple.isArch64Bit() && !IsX32, "m64", Flags);
1551  addMultilibFlag(TargetTriple.isArch32Bit(), "m32", Flags);
1552  addMultilibFlag(TargetTriple.isArch64Bit() && IsX32, "mx32", Flags);
1553 
1554  if (!Result.Multilibs.select(Flags, Result.SelectedMultilib))
1555  return false;
1556 
1557  if (Result.SelectedMultilib == Alt64 || Result.SelectedMultilib == Alt32 ||
1558  Result.SelectedMultilib == Altx32)
1559  Result.BiarchSibling = Default;
1560 
1561  return true;
1562 }
1563 
1564 /// Generic_GCC - A tool chain using the 'gcc' command to perform
1565 /// all subcommands; this relies on gcc translating the majority of
1566 /// command line options.
1567 
1568 /// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1569 bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor,
1570  int RHSPatch,
1571  StringRef RHSPatchSuffix) const {
1572  if (Major != RHSMajor)
1573  return Major < RHSMajor;
1574  if (Minor != RHSMinor)
1575  return Minor < RHSMinor;
1576  if (Patch != RHSPatch) {
1577  // Note that versions without a specified patch sort higher than those with
1578  // a patch.
1579  if (RHSPatch == -1)
1580  return true;
1581  if (Patch == -1)
1582  return false;
1583 
1584  // Otherwise just sort on the patch itself.
1585  return Patch < RHSPatch;
1586  }
1587  if (PatchSuffix != RHSPatchSuffix) {
1588  // Sort empty suffixes higher.
1589  if (RHSPatchSuffix.empty())
1590  return true;
1591  if (PatchSuffix.empty())
1592  return false;
1593 
1594  // Provide a lexicographic sort to make this a total ordering.
1595  return PatchSuffix < RHSPatchSuffix;
1596  }
1597 
1598  // The versions are equal.
1599  return false;
1600 }
1601 
1602 /// \brief Parse a GCCVersion object out of a string of text.
1603 ///
1604 /// This is the primary means of forming GCCVersion objects.
1605 /*static*/
1607  const GCCVersion BadVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
1608  std::pair<StringRef, StringRef> First = VersionText.split('.');
1609  std::pair<StringRef, StringRef> Second = First.second.split('.');
1610 
1611  GCCVersion GoodVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
1612  if (First.first.getAsInteger(10, GoodVersion.Major) || GoodVersion.Major < 0)
1613  return BadVersion;
1614  GoodVersion.MajorStr = First.first.str();
1615  if (First.second.empty())
1616  return GoodVersion;
1617  if (Second.first.getAsInteger(10, GoodVersion.Minor) || GoodVersion.Minor < 0)
1618  return BadVersion;
1619  GoodVersion.MinorStr = Second.first.str();
1620 
1621  // First look for a number prefix and parse that if present. Otherwise just
1622  // stash the entire patch string in the suffix, and leave the number
1623  // unspecified. This covers versions strings such as:
1624  // 5 (handled above)
1625  // 4.4
1626  // 4.4.0
1627  // 4.4.x
1628  // 4.4.2-rc4
1629  // 4.4.x-patched
1630  // And retains any patch number it finds.
1631  StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1632  if (!PatchText.empty()) {
1633  if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
1634  // Try to parse the number and any suffix.
1635  if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1636  GoodVersion.Patch < 0)
1637  return BadVersion;
1638  GoodVersion.PatchSuffix = PatchText.substr(EndNumber);
1639  }
1640  }
1641 
1642  return GoodVersion;
1643 }
1644 
1645 static llvm::StringRef getGCCToolchainDir(const ArgList &Args) {
1646  const Arg *A = Args.getLastArg(clang::driver::options::OPT_gcc_toolchain);
1647  if (A)
1648  return A->getValue();
1649  return GCC_INSTALL_PREFIX;
1650 }
1651 
1652 /// \brief Initialize a GCCInstallationDetector from the driver.
1653 ///
1654 /// This performs all of the autodetection and sets up the various paths.
1655 /// Once constructed, a GCCInstallationDetector is essentially immutable.
1656 ///
1657 /// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1658 /// should instead pull the target out of the driver. This is currently
1659 /// necessary because the driver doesn't store the final version of the target
1660 /// triple.
1662  const llvm::Triple &TargetTriple, const ArgList &Args,
1663  ArrayRef<std::string> ExtraTripleAliases) {
1664  llvm::Triple BiarchVariantTriple = TargetTriple.isArch32Bit()
1665  ? TargetTriple.get64BitArchVariant()
1666  : TargetTriple.get32BitArchVariant();
1667  // The library directories which may contain GCC installations.
1668  SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
1669  // The compatible GCC triples for this particular architecture.
1670  SmallVector<StringRef, 16> CandidateTripleAliases;
1671  SmallVector<StringRef, 16> CandidateBiarchTripleAliases;
1672  CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1673  CandidateTripleAliases, CandidateBiarchLibDirs,
1674  CandidateBiarchTripleAliases);
1675 
1676  // Compute the set of prefixes for our search.
1677  SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1678  D.PrefixDirs.end());
1679 
1680  StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1681  if (GCCToolchainDir != "") {
1682  if (GCCToolchainDir.back() == '/')
1683  GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
1684 
1685  Prefixes.push_back(GCCToolchainDir);
1686  } else {
1687  // If we have a SysRoot, try that first.
1688  if (!D.SysRoot.empty()) {
1689  Prefixes.push_back(D.SysRoot);
1690  AddDefaultGCCPrefixes(TargetTriple, Prefixes, D.SysRoot);
1691  }
1692 
1693  // Then look for gcc installed alongside clang.
1694  Prefixes.push_back(D.InstalledDir + "/..");
1695 
1696  // Next, look for prefix(es) that correspond to distribution-supplied gcc
1697  // installations.
1698  if (D.SysRoot.empty()) {
1699  // Typically /usr.
1700  AddDefaultGCCPrefixes(TargetTriple, Prefixes, D.SysRoot);
1701  }
1702  }
1703 
1704  // Try to respect gcc-config on Gentoo. However, do that only
1705  // if --gcc-toolchain is not provided or equal to the Gentoo install
1706  // in /usr. This avoids accidentally enforcing the system GCC version
1707  // when using a custom toolchain.
1708  if (GCCToolchainDir == "" || GCCToolchainDir == D.SysRoot + "/usr") {
1709  for (StringRef CandidateTriple : ExtraTripleAliases) {
1710  if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple))
1711  return;
1712  }
1713  for (StringRef CandidateTriple : CandidateTripleAliases) {
1714  if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple))
1715  return;
1716  }
1717  for (StringRef CandidateTriple : CandidateBiarchTripleAliases) {
1718  if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple, true))
1719  return;
1720  }
1721  }
1722 
1723  // Loop over the various components which exist and select the best GCC
1724  // installation available. GCC installs are ranked by version number.
1725  Version = GCCVersion::Parse("0.0.0");
1726  for (const std::string &Prefix : Prefixes) {
1727  if (!D.getVFS().exists(Prefix))
1728  continue;
1729  for (StringRef Suffix : CandidateLibDirs) {
1730  const std::string LibDir = Prefix + Suffix.str();
1731  if (!D.getVFS().exists(LibDir))
1732  continue;
1733  for (StringRef Candidate : ExtraTripleAliases) // Try these first.
1734  ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate);
1735  for (StringRef Candidate : CandidateTripleAliases)
1736  ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate);
1737  }
1738  for (StringRef Suffix : CandidateBiarchLibDirs) {
1739  const std::string LibDir = Prefix + Suffix.str();
1740  if (!D.getVFS().exists(LibDir))
1741  continue;
1742  for (StringRef Candidate : CandidateBiarchTripleAliases)
1743  ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate,
1744  /*NeedsBiarchSuffix=*/ true);
1745  }
1746  }
1747 }
1748 
1749 void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const {
1750  for (const auto &InstallPath : CandidateGCCInstallPaths)
1751  OS << "Found candidate GCC installation: " << InstallPath << "\n";
1752 
1753  if (!GCCInstallPath.empty())
1754  OS << "Selected GCC installation: " << GCCInstallPath << "\n";
1755 
1756  for (const auto &Multilib : Multilibs)
1757  OS << "Candidate multilib: " << Multilib << "\n";
1758 
1759  if (Multilibs.size() != 0 || !SelectedMultilib.isDefault())
1760  OS << "Selected multilib: " << SelectedMultilib << "\n";
1761 }
1762 
1764  if (BiarchSibling.hasValue()) {
1765  M = BiarchSibling.getValue();
1766  return true;
1767  }
1768  return false;
1769 }
1770 
1771 void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
1772  const llvm::Triple &TargetTriple, SmallVectorImpl<std::string> &Prefixes,
1773  StringRef SysRoot) {
1774  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
1775  // Solaris is a special case.
1776  // The GCC installation is under
1777  // /usr/gcc/<major>.<minor>/lib/gcc/<triple>/<major>.<minor>.<patch>/
1778  // so we need to find those /usr/gcc/*/lib/gcc libdirs and go with
1779  // /usr/gcc/<version> as a prefix.
1780 
1781  std::string PrefixDir = SysRoot.str() + "/usr/gcc";
1782  std::error_code EC;
1783  for (vfs::directory_iterator LI = D.getVFS().dir_begin(PrefixDir, EC), LE;
1784  !EC && LI != LE; LI = LI.increment(EC)) {
1785  StringRef VersionText = llvm::sys::path::filename(LI->getName());
1786  GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1787 
1788  // Filter out obviously bad entries.
1789  if (CandidateVersion.Major == -1 || CandidateVersion.isOlderThan(4, 1, 1))
1790  continue;
1791 
1792  std::string CandidatePrefix = PrefixDir + "/" + VersionText.str();
1793  std::string CandidateLibPath = CandidatePrefix + "/lib/gcc";
1794  if (!D.getVFS().exists(CandidateLibPath))
1795  continue;
1796 
1797  Prefixes.push_back(CandidatePrefix);
1798  }
1799  return;
1800  }
1801 
1802  // Non-Solaris is much simpler - most systems just go with "/usr".
1803  if (SysRoot.empty() && TargetTriple.getOS() == llvm::Triple::Linux) {
1804  // Yet, still look for RHEL devtoolsets.
1805  Prefixes.push_back("/opt/rh/devtoolset-6/root/usr");
1806  Prefixes.push_back("/opt/rh/devtoolset-4/root/usr");
1807  Prefixes.push_back("/opt/rh/devtoolset-3/root/usr");
1808  Prefixes.push_back("/opt/rh/devtoolset-2/root/usr");
1809  }
1810  Prefixes.push_back(SysRoot.str() + "/usr");
1811 }
1812 
1813 /*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
1814  const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
1815  SmallVectorImpl<StringRef> &LibDirs,
1816  SmallVectorImpl<StringRef> &TripleAliases,
1817  SmallVectorImpl<StringRef> &BiarchLibDirs,
1818  SmallVectorImpl<StringRef> &BiarchTripleAliases) {
1819  // Declare a bunch of static data sets that we'll select between below. These
1820  // are specifically designed to always refer to string literals to avoid any
1821  // lifetime or initialization issues.
1822  static const char *const AArch64LibDirs[] = {"/lib64", "/lib"};
1823  static const char *const AArch64Triples[] = {
1824  "aarch64-none-linux-gnu", "aarch64-linux-gnu", "aarch64-linux-android",
1825  "aarch64-redhat-linux", "aarch64-suse-linux"};
1826  static const char *const AArch64beLibDirs[] = {"/lib"};
1827  static const char *const AArch64beTriples[] = {"aarch64_be-none-linux-gnu",
1828  "aarch64_be-linux-gnu"};
1829 
1830  static const char *const ARMLibDirs[] = {"/lib"};
1831  static const char *const ARMTriples[] = {"arm-linux-gnueabi",
1832  "arm-linux-androideabi"};
1833  static const char *const ARMHFTriples[] = {"arm-linux-gnueabihf",
1834  "armv7hl-redhat-linux-gnueabi",
1835  "armv6hl-suse-linux-gnueabi",
1836  "armv7hl-suse-linux-gnueabi"};
1837  static const char *const ARMebLibDirs[] = {"/lib"};
1838  static const char *const ARMebTriples[] = {"armeb-linux-gnueabi",
1839  "armeb-linux-androideabi"};
1840  static const char *const ARMebHFTriples[] = {
1841  "armeb-linux-gnueabihf", "armebv7hl-redhat-linux-gnueabi"};
1842 
1843  static const char *const X86_64LibDirs[] = {"/lib64", "/lib"};
1844  static const char *const X86_64Triples[] = {
1845  "x86_64-linux-gnu", "x86_64-unknown-linux-gnu",
1846  "x86_64-pc-linux-gnu", "x86_64-redhat-linux6E",
1847  "x86_64-redhat-linux", "x86_64-suse-linux",
1848  "x86_64-manbo-linux-gnu", "x86_64-linux-gnu",
1849  "x86_64-slackware-linux", "x86_64-linux-android",
1850  "x86_64-unknown-linux"};
1851  static const char *const X32LibDirs[] = {"/libx32"};
1852  static const char *const X86LibDirs[] = {"/lib32", "/lib"};
1853  static const char *const X86Triples[] = {
1854  "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu",
1855  "i386-linux-gnu", "i386-redhat-linux6E", "i686-redhat-linux",
1856  "i586-redhat-linux", "i386-redhat-linux", "i586-suse-linux",
1857  "i486-slackware-linux", "i686-montavista-linux", "i686-linux-android",
1858  "i586-linux-gnu"};
1859 
1860  static const char *const MIPSLibDirs[] = {"/lib"};
1861  static const char *const MIPSTriples[] = {"mips-linux-gnu", "mips-mti-linux",
1862  "mips-mti-linux-gnu",
1863  "mips-img-linux-gnu"};
1864  static const char *const MIPSELLibDirs[] = {"/lib"};
1865  static const char *const MIPSELTriples[] = {"mipsel-linux-gnu",
1866  "mips-img-linux-gnu"};
1867 
1868  static const char *const MIPS64LibDirs[] = {"/lib64", "/lib"};
1869  static const char *const MIPS64Triples[] = {
1870  "mips64-linux-gnu", "mips-mti-linux-gnu", "mips-img-linux-gnu",
1871  "mips64-linux-gnuabi64"};
1872  static const char *const MIPS64ELLibDirs[] = {"/lib64", "/lib"};
1873  static const char *const MIPS64ELTriples[] = {
1874  "mips64el-linux-gnu", "mips-mti-linux-gnu", "mips-img-linux-gnu",
1875  "mips64el-linux-gnuabi64"};
1876 
1877  static const char *const MIPSELAndroidLibDirs[] = {"/lib", "/libr2",
1878  "/libr6"};
1879  static const char *const MIPSELAndroidTriples[] = {"mipsel-linux-android"};
1880  static const char *const MIPS64ELAndroidLibDirs[] = {"/lib64", "/lib",
1881  "/libr2", "/libr6"};
1882  static const char *const MIPS64ELAndroidTriples[] = {
1883  "mips64el-linux-android"};
1884 
1885  static const char *const PPCLibDirs[] = {"/lib32", "/lib"};
1886  static const char *const PPCTriples[] = {
1887  "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1888  "powerpc-suse-linux", "powerpc-montavista-linuxspe"};
1889  static const char *const PPC64LibDirs[] = {"/lib64", "/lib"};
1890  static const char *const PPC64Triples[] = {
1891  "powerpc64-linux-gnu", "powerpc64-unknown-linux-gnu",
1892  "powerpc64-suse-linux", "ppc64-redhat-linux"};
1893  static const char *const PPC64LELibDirs[] = {"/lib64", "/lib"};
1894  static const char *const PPC64LETriples[] = {
1895  "powerpc64le-linux-gnu", "powerpc64le-unknown-linux-gnu",
1896  "powerpc64le-suse-linux", "ppc64le-redhat-linux"};
1897 
1898  static const char *const RISCV32LibDirs[] = {"/lib", "/lib32"};
1899  static const char *const RISCVTriples[] = {"riscv32-unknown-linux-gnu",
1900  "riscv64-unknown-linux-gnu"};
1901 
1902  static const char *const SPARCv8LibDirs[] = {"/lib32", "/lib"};
1903  static const char *const SPARCv8Triples[] = {"sparc-linux-gnu",
1904  "sparcv8-linux-gnu"};
1905  static const char *const SPARCv9LibDirs[] = {"/lib64", "/lib"};
1906  static const char *const SPARCv9Triples[] = {"sparc64-linux-gnu",
1907  "sparcv9-linux-gnu"};
1908 
1909  static const char *const SystemZLibDirs[] = {"/lib64", "/lib"};
1910  static const char *const SystemZTriples[] = {
1911  "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1912  "s390x-suse-linux", "s390x-redhat-linux"};
1913 
1914 
1915  using std::begin;
1916  using std::end;
1917 
1918  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
1919  static const char *const SolarisLibDirs[] = {"/lib"};
1920  static const char *const SolarisSparcV8Triples[] = {
1921  "sparc-sun-solaris2.11", "sparc-sun-solaris2.12"};
1922  static const char *const SolarisSparcV9Triples[] = {
1923  "sparcv9-sun-solaris2.11", "sparcv9-sun-solaris2.12"};
1924  static const char *const SolarisX86Triples[] = {"i386-pc-solaris2.11",
1925  "i386-pc-solaris2.12"};
1926  static const char *const SolarisX86_64Triples[] = {"x86_64-pc-solaris2.11",
1927  "x86_64-pc-solaris2.12"};
1928  LibDirs.append(begin(SolarisLibDirs), end(SolarisLibDirs));
1929  BiarchLibDirs.append(begin(SolarisLibDirs), end(SolarisLibDirs));
1930  switch (TargetTriple.getArch()) {
1931  case llvm::Triple::x86:
1932  TripleAliases.append(begin(SolarisX86Triples), end(SolarisX86Triples));
1933  BiarchTripleAliases.append(begin(SolarisX86_64Triples),
1934  end(SolarisX86_64Triples));
1935  break;
1936  case llvm::Triple::x86_64:
1937  TripleAliases.append(begin(SolarisX86_64Triples),
1938  end(SolarisX86_64Triples));
1939  BiarchTripleAliases.append(begin(SolarisX86Triples),
1940  end(SolarisX86Triples));
1941  break;
1942  case llvm::Triple::sparc:
1943  TripleAliases.append(begin(SolarisSparcV8Triples),
1944  end(SolarisSparcV8Triples));
1945  BiarchTripleAliases.append(begin(SolarisSparcV9Triples),
1946  end(SolarisSparcV9Triples));
1947  break;
1948  case llvm::Triple::sparcv9:
1949  TripleAliases.append(begin(SolarisSparcV9Triples),
1950  end(SolarisSparcV9Triples));
1951  BiarchTripleAliases.append(begin(SolarisSparcV8Triples),
1952  end(SolarisSparcV8Triples));
1953  break;
1954  default:
1955  break;
1956  }
1957  return;
1958  }
1959 
1960  switch (TargetTriple.getArch()) {
1961  case llvm::Triple::aarch64:
1962  LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
1963  TripleAliases.append(begin(AArch64Triples), end(AArch64Triples));
1964  BiarchLibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
1965  BiarchTripleAliases.append(begin(AArch64Triples), end(AArch64Triples));
1966  break;
1967  case llvm::Triple::aarch64_be:
1968  LibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs));
1969  TripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples));
1970  BiarchLibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs));
1971  BiarchTripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples));
1972  break;
1973  case llvm::Triple::arm:
1974  case llvm::Triple::thumb:
1975  LibDirs.append(begin(ARMLibDirs), end(ARMLibDirs));
1976  if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1977  TripleAliases.append(begin(ARMHFTriples), end(ARMHFTriples));
1978  } else {
1979  TripleAliases.append(begin(ARMTriples), end(ARMTriples));
1980  }
1981  break;
1982  case llvm::Triple::armeb:
1983  case llvm::Triple::thumbeb:
1984  LibDirs.append(begin(ARMebLibDirs), end(ARMebLibDirs));
1985  if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1986  TripleAliases.append(begin(ARMebHFTriples), end(ARMebHFTriples));
1987  } else {
1988  TripleAliases.append(begin(ARMebTriples), end(ARMebTriples));
1989  }
1990  break;
1991  case llvm::Triple::x86_64:
1992  LibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
1993  TripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
1994  // x32 is always available when x86_64 is available, so adding it as
1995  // secondary arch with x86_64 triples
1996  if (TargetTriple.getEnvironment() == llvm::Triple::GNUX32) {
1997  BiarchLibDirs.append(begin(X32LibDirs), end(X32LibDirs));
1998  BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
1999  } else {
2000  BiarchLibDirs.append(begin(X86LibDirs), end(X86LibDirs));
2001  BiarchTripleAliases.append(begin(X86Triples), end(X86Triples));
2002  }
2003  break;
2004  case llvm::Triple::x86:
2005  LibDirs.append(begin(X86LibDirs), end(X86LibDirs));
2006  // MCU toolchain is 32 bit only and its triple alias is TargetTriple
2007  // itself, which will be appended below.
2008  if (!TargetTriple.isOSIAMCU()) {
2009  TripleAliases.append(begin(X86Triples), end(X86Triples));
2010  BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
2011  BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
2012  }
2013  break;
2014  case llvm::Triple::mips:
2015  LibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs));
2016  TripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2017  BiarchLibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs));
2018  BiarchTripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples));
2019  break;
2020  case llvm::Triple::mipsel:
2021  if (TargetTriple.isAndroid()) {
2022  LibDirs.append(begin(MIPSELAndroidLibDirs), end(MIPSELAndroidLibDirs));
2023  TripleAliases.append(begin(MIPSELAndroidTriples),
2024  end(MIPSELAndroidTriples));
2025  BiarchLibDirs.append(begin(MIPS64ELAndroidLibDirs),
2026  end(MIPS64ELAndroidLibDirs));
2027  BiarchTripleAliases.append(begin(MIPS64ELAndroidTriples),
2028  end(MIPS64ELAndroidTriples));
2029 
2030  } else {
2031  LibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
2032  TripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples));
2033  TripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2034  BiarchLibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
2035  BiarchTripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples));
2036  }
2037  break;
2038  case llvm::Triple::mips64:
2039  LibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs));
2040  TripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples));
2041  BiarchLibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs));
2042  BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2043  break;
2044  case llvm::Triple::mips64el:
2045  if (TargetTriple.isAndroid()) {
2046  LibDirs.append(begin(MIPS64ELAndroidLibDirs),
2047  end(MIPS64ELAndroidLibDirs));
2048  TripleAliases.append(begin(MIPS64ELAndroidTriples),
2049  end(MIPS64ELAndroidTriples));
2050  BiarchLibDirs.append(begin(MIPSELAndroidLibDirs),
2051  end(MIPSELAndroidLibDirs));
2052  BiarchTripleAliases.append(begin(MIPSELAndroidTriples),
2053  end(MIPSELAndroidTriples));
2054 
2055  } else {
2056  LibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
2057  TripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples));
2058  BiarchLibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
2059  BiarchTripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples));
2060  BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
2061  }
2062  break;
2063  case llvm::Triple::ppc:
2064  LibDirs.append(begin(PPCLibDirs), end(PPCLibDirs));
2065  TripleAliases.append(begin(PPCTriples), end(PPCTriples));
2066  BiarchLibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs));
2067  BiarchTripleAliases.append(begin(PPC64Triples), end(PPC64Triples));
2068  break;
2069  case llvm::Triple::ppc64:
2070  LibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs));
2071  TripleAliases.append(begin(PPC64Triples), end(PPC64Triples));
2072  BiarchLibDirs.append(begin(PPCLibDirs), end(PPCLibDirs));
2073  BiarchTripleAliases.append(begin(PPCTriples), end(PPCTriples));
2074  break;
2075  case llvm::Triple::ppc64le:
2076  LibDirs.append(begin(PPC64LELibDirs), end(PPC64LELibDirs));
2077  TripleAliases.append(begin(PPC64LETriples), end(PPC64LETriples));
2078  break;
2079  case llvm::Triple::riscv32:
2080  LibDirs.append(begin(RISCV32LibDirs), end(RISCV32LibDirs));
2081  BiarchLibDirs.append(begin(RISCV32LibDirs), end(RISCV32LibDirs));
2082  TripleAliases.append(begin(RISCVTriples), end(RISCVTriples));
2083  BiarchTripleAliases.append(begin(RISCVTriples), end(RISCVTriples));
2084  break;
2085  case llvm::Triple::sparc:
2086  case llvm::Triple::sparcel:
2087  LibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs));
2088  TripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples));
2089  BiarchLibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs));
2090  BiarchTripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples));
2091  break;
2092  case llvm::Triple::sparcv9:
2093  LibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs));
2094  TripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples));
2095  BiarchLibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs));
2096  BiarchTripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples));
2097  break;
2098  case llvm::Triple::systemz:
2099  LibDirs.append(begin(SystemZLibDirs), end(SystemZLibDirs));
2100  TripleAliases.append(begin(SystemZTriples), end(SystemZTriples));
2101  break;
2102  default:
2103  // By default, just rely on the standard lib directories and the original
2104  // triple.
2105  break;
2106  }
2107 
2108  // Always append the drivers target triple to the end, in case it doesn't
2109  // match any of our aliases.
2110  TripleAliases.push_back(TargetTriple.str());
2111 
2112  // Also include the multiarch variant if it's different.
2113  if (TargetTriple.str() != BiarchTriple.str())
2114  BiarchTripleAliases.push_back(BiarchTriple.str());
2115 }
2116 
2117 bool Generic_GCC::GCCInstallationDetector::ScanGCCForMultilibs(
2118  const llvm::Triple &TargetTriple, const ArgList &Args,
2119  StringRef Path, bool NeedsBiarchSuffix) {
2120  llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
2121  DetectedMultilibs Detected;
2122 
2123  // Android standalone toolchain could have multilibs for ARM and Thumb.
2124  // Debian mips multilibs behave more like the rest of the biarch ones,
2125  // so handle them there
2126  if (isArmOrThumbArch(TargetArch) && TargetTriple.isAndroid()) {
2127  // It should also work without multilibs in a simplified toolchain.
2128  findAndroidArmMultilibs(D, TargetTriple, Path, Args, Detected);
2129  } else if (tools::isMipsArch(TargetArch)) {
2130  if (!findMIPSMultilibs(D, TargetTriple, Path, Args, Detected))
2131  return false;
2132  } else if (isRISCV(TargetArch)) {
2133  findRISCVMultilibs(D, TargetTriple, Path, Args, Detected);
2134  } else if (!findBiarchMultilibs(D, TargetTriple, Path, Args,
2135  NeedsBiarchSuffix, Detected)) {
2136  return false;
2137  }
2138 
2139  Multilibs = Detected.Multilibs;
2140  SelectedMultilib = Detected.SelectedMultilib;
2141  BiarchSibling = Detected.BiarchSibling;
2142 
2143  return true;
2144 }
2145 
2146 void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
2147  const llvm::Triple &TargetTriple, const ArgList &Args,
2148  const std::string &LibDir, StringRef CandidateTriple,
2149  bool NeedsBiarchSuffix) {
2150  llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
2151  // Locations relative to the system lib directory where GCC's triple-specific
2152  // directories might reside.
2153  struct GCCLibSuffix {
2154  // Path from system lib directory to GCC triple-specific directory.
2155  std::string LibSuffix;
2156  // Path from GCC triple-specific directory back to system lib directory.
2157  // This is one '..' component per component in LibSuffix.
2158  StringRef ReversePath;
2159  // Whether this library suffix is relevant for the triple.
2160  bool Active;
2161  } Suffixes[] = {
2162  // This is the normal place.
2163  {"gcc/" + CandidateTriple.str(), "../..", true},
2164 
2165  // Debian puts cross-compilers in gcc-cross.
2166  {"gcc-cross/" + CandidateTriple.str(), "../..",
2167  TargetTriple.getOS() != llvm::Triple::Solaris},
2168 
2169  // The Freescale PPC SDK has the gcc libraries in
2170  // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well. Only do
2171  // this on Freescale triples, though, since some systems put a *lot* of
2172  // files in that location, not just GCC installation data.
2173  {CandidateTriple.str(), "..",
2174  TargetTriple.getVendor() == llvm::Triple::Freescale},
2175 
2176  // Natively multiarch systems sometimes put the GCC triple-specific
2177  // directory within their multiarch lib directory, resulting in the
2178  // triple appearing twice.
2179  {CandidateTriple.str() + "/gcc/" + CandidateTriple.str(), "../../..",
2180  TargetTriple.getOS() != llvm::Triple::Solaris},
2181 
2182  // Deal with cases (on Ubuntu) where the system architecture could be i386
2183  // but the GCC target architecture could be (say) i686.
2184  // FIXME: It may be worthwhile to generalize this and look for a second
2185  // triple.
2186  {"i386-linux-gnu/gcc/" + CandidateTriple.str(), "../../..",
2187  (TargetArch == llvm::Triple::x86 &&
2188  TargetTriple.getOS() != llvm::Triple::Solaris)}};
2189 
2190  for (auto &Suffix : Suffixes) {
2191  if (!Suffix.Active)
2192  continue;
2193 
2194  StringRef LibSuffix = Suffix.LibSuffix;
2195  std::error_code EC;
2197  LI = D.getVFS().dir_begin(LibDir + "/" + LibSuffix, EC),
2198  LE;
2199  !EC && LI != LE; LI = LI.increment(EC)) {
2200  StringRef VersionText = llvm::sys::path::filename(LI->getName());
2201  GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
2202  if (CandidateVersion.Major != -1) // Filter obviously bad entries.
2203  if (!CandidateGCCInstallPaths.insert(LI->getName()).second)
2204  continue; // Saw this path before; no need to look at it again.
2205  if (CandidateVersion.isOlderThan(4, 1, 1))
2206  continue;
2207  if (CandidateVersion <= Version)
2208  continue;
2209 
2210  if (!ScanGCCForMultilibs(TargetTriple, Args, LI->getName(),
2211  NeedsBiarchSuffix))
2212  continue;
2213 
2214  Version = CandidateVersion;
2215  GCCTriple.setTriple(CandidateTriple);
2216  // FIXME: We hack together the directory name here instead of
2217  // using LI to ensure stable path separators across Windows and
2218  // Linux.
2219  GCCInstallPath = (LibDir + "/" + LibSuffix + "/" + VersionText).str();
2220  GCCParentLibPath = (GCCInstallPath + "/../" + Suffix.ReversePath).str();
2221  IsValid = true;
2222  }
2223  }
2224 }
2225 
2226 bool Generic_GCC::GCCInstallationDetector::ScanGentooGccConfig(
2227  const llvm::Triple &TargetTriple, const ArgList &Args,
2228  StringRef CandidateTriple, bool NeedsBiarchSuffix) {
2229  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
2230  D.getVFS().getBufferForFile(D.SysRoot + "/etc/env.d/gcc/config-" +
2231  CandidateTriple.str());
2232  if (File) {
2234  File.get()->getBuffer().split(Lines, "\n");
2235  for (StringRef Line : Lines) {
2236  Line = Line.trim();
2237  // CURRENT=triple-version
2238  if (Line.consume_front("CURRENT=")) {
2239  const std::pair<StringRef, StringRef> ActiveVersion =
2240  Line.rsplit('-');
2241  // Note: Strictly speaking, we should be reading
2242  // /etc/env.d/gcc/${CURRENT} now. However, the file doesn't
2243  // contain anything new or especially useful to us.
2244  const std::string GentooPath = D.SysRoot + "/usr/lib/gcc/" +
2245  ActiveVersion.first.str() + "/" +
2246  ActiveVersion.second.str();
2247  if (D.getVFS().exists(GentooPath + "/crtbegin.o")) {
2248  if (!ScanGCCForMultilibs(TargetTriple, Args, GentooPath,
2249  NeedsBiarchSuffix))
2250  return false;
2251 
2252  Version = GCCVersion::Parse(ActiveVersion.second);
2253  GCCInstallPath = GentooPath;
2254  GCCParentLibPath = GentooPath + "/../../..";
2255  GCCTriple.setTriple(ActiveVersion.first);
2256  IsValid = true;
2257  return true;
2258  }
2259  }
2260  }
2261  }
2262 
2263  return false;
2264 }
2265 
2266 Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple &Triple,
2267  const ArgList &Args)
2268  : ToolChain(D, Triple, Args), GCCInstallation(D),
2269  CudaInstallation(D, Triple, Args) {
2270  getProgramPaths().push_back(getDriver().getInstalledDir());
2271  if (getDriver().getInstalledDir() != getDriver().Dir)
2272  getProgramPaths().push_back(getDriver().Dir);
2273 }
2274 
2276 
2278  switch (AC) {
2280  if (!Preprocess)
2281  Preprocess.reset(new clang::driver::tools::gcc::Preprocessor(*this));
2282  return Preprocess.get();
2284  if (!Compile)
2285  Compile.reset(new tools::gcc::Compiler(*this));
2286  return Compile.get();
2287  default:
2288  return ToolChain::getTool(AC);
2289  }
2290 }
2291 
2293  return new tools::gnutools::Assembler(*this);
2294 }
2295 
2296 Tool *Generic_GCC::buildLinker() const { return new tools::gcc::Linker(*this); }
2297 
2298 void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
2299  // Print the information about how we detected the GCC installation.
2300  GCCInstallation.print(OS);
2301  CudaInstallation.print(OS);
2302 }
2303 
2304 bool Generic_GCC::IsUnwindTablesDefault(const ArgList &Args) const {
2305  return getArch() == llvm::Triple::x86_64;
2306 }
2307 
2309  switch (getArch()) {
2310  case llvm::Triple::x86_64:
2311  return getTriple().isOSWindows();
2312  case llvm::Triple::ppc64:
2313  case llvm::Triple::ppc64le:
2314  return !getTriple().isOSBinFormatMachO() && !getTriple().isMacOSX();
2315  case llvm::Triple::mips64:
2316  case llvm::Triple::mips64el:
2317  return true;
2318  default:
2319  return false;
2320  }
2321 }
2322 
2323 bool Generic_GCC::isPIEDefault() const { return false; }
2324 
2326  return getArch() == llvm::Triple::x86_64 && getTriple().isOSWindows();
2327 }
2328 
2330  switch (getTriple().getArch()) {
2331  case llvm::Triple::x86:
2332  case llvm::Triple::x86_64:
2333  case llvm::Triple::aarch64:
2334  case llvm::Triple::aarch64_be:
2335  case llvm::Triple::arm:
2336  case llvm::Triple::armeb:
2337  case llvm::Triple::avr:
2338  case llvm::Triple::bpfel:
2339  case llvm::Triple::bpfeb:
2340  case llvm::Triple::thumb:
2341  case llvm::Triple::thumbeb:
2342  case llvm::Triple::ppc:
2343  case llvm::Triple::ppc64:
2344  case llvm::Triple::ppc64le:
2345  case llvm::Triple::riscv32:
2346  case llvm::Triple::riscv64:
2347  case llvm::Triple::systemz:
2348  case llvm::Triple::mips:
2349  case llvm::Triple::mipsel:
2350  return true;
2351  case llvm::Triple::mips64:
2352  case llvm::Triple::mips64el:
2353  // Enabled for Debian and Android mips64/mipsel, as they can precisely
2354  // identify the ABI in use (Debian) or only use N64 for MIPS64 (Android).
2355  // Other targets are unable to distinguish N32 from N64.
2356  if (getTriple().getEnvironment() == llvm::Triple::GNUABI64 ||
2357  getTriple().isAndroid())
2358  return true;
2359  return false;
2360  default:
2361  return false;
2362  }
2363 }
2364 
2365 void Generic_GCC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2366  ArgStringList &CC1Args) const {
2367  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2368  DriverArgs.hasArg(options::OPT_nostdincxx))
2369  return;
2370 
2371  switch (GetCXXStdlibType(DriverArgs)) {
2372  case ToolChain::CST_Libcxx: {
2373  std::string Path = findLibCxxIncludePath();
2374  if (!Path.empty())
2375  addSystemInclude(DriverArgs, CC1Args, Path);
2376  break;
2377  }
2378 
2380  addLibStdCxxIncludePaths(DriverArgs, CC1Args);
2381  break;
2382  }
2383 }
2384 
2386  // FIXME: The Linux behavior would probaby be a better approach here.
2387  return getDriver().SysRoot + "/usr/include/c++/v1";
2388 }
2389 
2390 void
2391 Generic_GCC::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
2392  llvm::opt::ArgStringList &CC1Args) const {
2393  // By default, we don't assume we know where libstdc++ might be installed.
2394  // FIXME: If we have a valid GCCInstallation, use it.
2395 }
2396 
2397 /// \brief Helper to add the variant paths of a libstdc++ installation.
2399  Twine Base, Twine Suffix, StringRef GCCTriple, StringRef GCCMultiarchTriple,
2400  StringRef TargetMultiarchTriple, Twine IncludeSuffix,
2401  const ArgList &DriverArgs, ArgStringList &CC1Args) const {
2402  if (!getVFS().exists(Base + Suffix))
2403  return false;
2404 
2405  addSystemInclude(DriverArgs, CC1Args, Base + Suffix);
2406 
2407  // The vanilla GCC layout of libstdc++ headers uses a triple subdirectory. If
2408  // that path exists or we have neither a GCC nor target multiarch triple, use
2409  // this vanilla search path.
2410  if ((GCCMultiarchTriple.empty() && TargetMultiarchTriple.empty()) ||
2411  getVFS().exists(Base + Suffix + "/" + GCCTriple + IncludeSuffix)) {
2412  addSystemInclude(DriverArgs, CC1Args,
2413  Base + Suffix + "/" + GCCTriple + IncludeSuffix);
2414  } else {
2415  // Otherwise try to use multiarch naming schemes which have normalized the
2416  // triples and put the triple before the suffix.
2417  //
2418  // GCC surprisingly uses *both* the GCC triple with a multilib suffix and
2419  // the target triple, so we support that here.
2420  addSystemInclude(DriverArgs, CC1Args,
2421  Base + "/" + GCCMultiarchTriple + Suffix + IncludeSuffix);
2422  addSystemInclude(DriverArgs, CC1Args,
2423  Base + "/" + TargetMultiarchTriple + Suffix);
2424  }
2425 
2426  addSystemInclude(DriverArgs, CC1Args, Base + Suffix + "/backward");
2427  return true;
2428 }
2429 
2430 llvm::opt::DerivedArgList *
2431 Generic_GCC::TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef,
2432  Action::OffloadKind DeviceOffloadKind) const {
2433 
2434  // If this tool chain is used for an OpenMP offloading device we have to make
2435  // sure we always generate a shared library regardless of the commands the
2436  // user passed to the host. This is required because the runtime library
2437  // is required to load the device image dynamically at run time.
2438  if (DeviceOffloadKind == Action::OFK_OpenMP) {
2439  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
2440  const OptTable &Opts = getDriver().getOpts();
2441 
2442  // Request the shared library. Given that these options are decided
2443  // implicitly, they do not refer to any base argument.
2444  DAL->AddFlagArg(/*BaseArg=*/nullptr, Opts.getOption(options::OPT_shared));
2445  DAL->AddFlagArg(/*BaseArg=*/nullptr, Opts.getOption(options::OPT_fPIC));
2446 
2447  // Filter all the arguments we don't care passing to the offloading
2448  // toolchain as they can mess up with the creation of a shared library.
2449  for (auto *A : Args) {
2450  switch ((options::ID)A->getOption().getID()) {
2451  default:
2452  DAL->append(A);
2453  break;
2454  case options::OPT_shared:
2455  case options::OPT_dynamic:
2456  case options::OPT_static:
2457  case options::OPT_fPIC:
2458  case options::OPT_fno_PIC:
2459  case options::OPT_fpic:
2460  case options::OPT_fno_pic:
2461  case options::OPT_fPIE:
2462  case options::OPT_fno_PIE:
2463  case options::OPT_fpie:
2464  case options::OPT_fno_pie:
2465  break;
2466  }
2467  }
2468  return DAL;
2469  }
2470  return nullptr;
2471 }
2472 
2473 void Generic_ELF::anchor() {}
2474 
2475 void Generic_ELF::addClangTargetOptions(const ArgList &DriverArgs,
2476  ArgStringList &CC1Args,
2477  Action::OffloadKind) const {
2479  bool UseInitArrayDefault =
2480  getTriple().getArch() == llvm::Triple::aarch64 ||
2481  getTriple().getArch() == llvm::Triple::aarch64_be ||
2482  (getTriple().getOS() == llvm::Triple::Linux &&
2483  ((!GCCInstallation.isValid() || !V.isOlderThan(4, 7, 0)) ||
2484  getTriple().isAndroid())) ||
2485  getTriple().getOS() == llvm::Triple::NaCl ||
2486  (getTriple().getVendor() == llvm::Triple::MipsTechnologies &&
2487  !getTriple().hasEnvironment()) ||
2488  getTriple().getOS() == llvm::Triple::Solaris;
2489 
2490  if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2491  options::OPT_fno_use_init_array, UseInitArrayDefault))
2492  CC1Args.push_back("-fuse-init-array");
2493 }
const char * getPPCAsmModeForCPU(StringRef Name)
Definition: PPC.cpp:89
MultilibSet & Either(const Multilib &M1, const Multilib &M2)
Add a set of mutually incompatible Multilib segments.
Definition: Multilib.cpp:155
MultilibSet & Maybe(const Multilib &M)
Add an optional Multilib segment.
Definition: Multilib.cpp:145
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: Linux.cpp:841
virtual std::string getDynamicLinker(const llvm::opt::ArgList &Args) const
Definition: Linux.cpp:429
Tool * buildLinker() const override
Definition: Gnu.cpp:2296
const char * getSystemZTargetCPU(const llvm::opt::ArgList &Args)
prefix_list PrefixDirs
Definition: Driver.h:146
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
static bool getPIE(const ArgList &Args, const toolchains::Linux &ToolChain)
Definition: Gnu.cpp:307
bool shouldUseFPXX(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef CPUName, StringRef ABIName, mips::FloatABI FloatABI)
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, bool Shared=false) const
Definition: ToolChain.cpp:365
bool isDefault() const
Check whether the default is selected.
Definition: Multilib.h:95
Tool * getTool(Action::ActionClass AC) const override
Definition: Gnu.cpp:2277
bool getBiarchSibling(Multilib &M) const
Get the biarch sibling multilib (if it exists).
Definition: Gnu.cpp:1763
llvm::Optional< Multilib > BiarchSibling
On Biarch systems, this corresponds to the default multilib when targeting the non-default multilib...
Definition: Gnu.h:30
static llvm::StringRef getGCCToolchainDir(const ArgList &Args)
Definition: Gnu.cpp:1645
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:507
static GCCVersion Parse(StringRef VersionText)
Parse a GCCVersion object out of a string of text.
Definition: Gnu.cpp:1606
static bool findMipsAndroidMultilibs(vfs::FileSystem &VFS, StringRef Path, const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:975
std::string DyldPrefix
Dynamic loader prefix, if present.
Definition: Driver.h:152
bool canTypeBeUserSpecified(ID Id)
canTypeBeUserSpecified - Can this type be specified on the command line (by the type name); this is u...
Definition: Types.cpp:80
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
bool isHostOffloading(OffloadKind OKind) const
Check if this action have any offload kinds.
Definition: Action.h:187
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:510
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:110
Struct to store and manipulate GCC versions.
Definition: Gnu.h:157
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:219
virtual directory_iterator dir_begin(const Twine &Dir, std::error_code &EC)=0
Get a directory_iterator for Dir.
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:408
FloatABI getARMFloatABI(const ToolChain &TC, const llvm::opt::ArgList &Args)
std::string PatchSuffix
Any textual suffix on the patch number.
Definition: Gnu.h:168
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: Gnu.cpp:2323
bool select(const Multilib::flags_list &Flags, Multilib &M) const
Pick the best multilib in the set,.
Definition: Multilib.cpp:247
void print(raw_ostream &OS) const
Print information about the detected CUDA installation.
Definition: Cuda.cpp:269
The virtual file system interface.
Multilib & flag(StringRef F)
Add a flag to the flags list Flag must be a flag accepted by the driver with its leading &#39;-&#39; removed...
Definition: Multilib.h:81
std::string getCPUName(const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
void print(raw_ostream &OS) const
Print information about the detected GCC installation.
Definition: Gnu.cpp:1749
const char * getFilename() const
Definition: InputInfo.h:84
static void linkXRayRuntimeDeps(const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs)
Definition: Gnu.cpp:241
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
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:275
path_list & getProgramPaths()
Definition: ToolChain.h:205
static Multilib makeMultilib(StringRef commonSuffix)
Definition: Gnu.cpp:880
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:61
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
Definition: Multilib.h:41
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
bool isNaN2008(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
static bool isMips64(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:858
directory_iterator & increment(std::error_code &EC)
Equivalent to operator++, with an error code.
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Definition: Multilib.h:51
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
static bool isMipsEL(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:862
MultilibSet & setIncludeDirsCallback(IncludeDirsFunc F)
Definition: Multilib.h:164
vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:93
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:400
types::ID getType() const
Definition: Action.h:132
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, bool IsThinLTO, const Driver &D)
virtual 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:761
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
bool hasMipsAbiArg(const llvm::opt::ArgList &Args, const char *Value)
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:191
bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC, const llvm::opt::ArgList &Args, bool IsOffloadingHost=false, bool GompNeedsRT=false)
Returns true, if an OpenMP runtime has been added.
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:318
void AddOpenMPLinkerScript(const ToolChain &TC, Compilation &C, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:239
static bool isMips16(const ArgList &Args)
Definition: Gnu.cpp:866
StringRef getRISCVABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
CudaInstallationDetector CudaInstallation
Definition: Gnu.h:276
static bool isSoftFloatABI(const ArgList &Args)
Definition: Gnu.cpp:829
static void addMultilibFlag(bool Enabled, const char *const Flag, std::vector< std::string > &Flags)
Flag must be a flag accepted by the driver with its leading &#39;-&#39; removed,
Definition: Gnu.cpp:842
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:2365
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition: Types.cpp:39
const AnnotatedLine * Line
const FunctionProtoType * T
static bool findMipsMuslMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1014
static void normalizeCPUNamesForAssembler(const ArgList &Args, ArgStringList &CmdArgs)
Definition: Gnu.cpp:49
bool isPICDefault() const override
Test whether this toolchain defaults to PIC.
Definition: Gnu.cpp:2308
const char * SplitDebugName(const llvm::opt::ArgList &Args, const InputInfo &Input)
virtual std::string findLibCxxIncludePath() const
Definition: Gnu.cpp:2385
void push_back(const Multilib &M)
Add a completed Multilib to the set.
Definition: Multilib.cpp:235
unsigned size() const
Definition: Multilib.h:159
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:25
static bool findMipsMtiMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1046
Tool * buildAssembler() const override
Definition: Gnu.cpp:2292
std::tuple< llvm::Reloc::Model, unsigned, bool > ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args)
static bool forwardToGCC(const Option &O)
Definition: Gnu.cpp:39
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override
IsUnwindTablesDefault - Does this tool chain use -funwind-tables by default.
Definition: Gnu.cpp:2304
static const char * getLDMOption(const llvm::Triple &T, const ArgList &Args)
Definition: Gnu.cpp:253
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:183
void printVerboseInfo(raw_ostream &OS) const override
Dispatch to the specific toolchain for verbose printing.
Definition: Gnu.cpp:2298
static bool findMipsImgMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:1218
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:749
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:62
mips::FloatABI getMipsFloatABI(const Driver &D, const llvm::opt::ArgList &Args)
vfs::FileSystem & getVFS() const
Definition: Driver.h:301
const Driver & getDriver() const
Definition: ToolChain.h:167
bool isMipsArch(llvm::Triple::ArchType Arch)
Definition: Mips.cpp:23
std::string InstalledDir
The path to the installed clang directory, if any.
Definition: Driver.h:130
bool isUCLibc(const llvm::opt::ArgList &Args)
static bool isMips32(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:854
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
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:673
void appendEBLinkFlags(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const llvm::Triple &Triple)
void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T, const JobAction &JA, const llvm::opt::ArgList &Args, const InputInfo &Output, const char *OutFile)
static bool isArmOrThumbArch(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:850
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:561
bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch, StringRef RHSPatchSuffix=StringRef()) const
Generic_GCC - A tool chain using the &#39;gcc&#39; command to perform all subcommands; this relies on gcc tra...
Definition: Gnu.cpp:1569
static bool findBiarchMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, bool NeedsBiarchSuffix, DetectedMultilibs &Result)
Definition: Gnu.cpp:1460
bool isPICDefaultForced() const override
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
Definition: Gnu.cpp:2325
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:726
Dataflow Directional Tag Classes.
Generic_GCC(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Gnu.cpp:2266
static bool isRISCV(llvm::Triple::ArchType Arch)
Definition: Gnu.cpp:876
const std::string & getCCCGenericGCCName() const
Name to use when invoking gcc/g++.
Definition: Driver.h:293
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
Defines the virtual file system interface vfs::FileSystem.
static bool addXRayRuntime(const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs)
Definition: Gnu.cpp:224
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:2475
static bool findMipsCsMultilibs(const Multilib::flags_list &Flags, FilterNonExistent &NonExistent, DetectedMultilibs &Result)
Definition: Gnu.cpp:884
bool IsIntegratedAssemblerDefault() const override
IsIntegratedAssemblerDefault - Does this tool chain enable -integrated-as by default.
Definition: Gnu.cpp:2329
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
bool exists(const Twine &Path)
Check whether a file exists. Provided for convenience.
std::string MajorStr
The text of the parsed major, and major+minor versions.
Definition: Gnu.h:165
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:2431
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const llvm::Triple & getTriple() const
Definition: ToolChain.h:169
virtual void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:2391
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const Twine &Name, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
This is a convenience method that opens a file, gets its content and then closes the file...
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:276
bool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const llvm::opt::ArgList &Args, DetectedMultilibs &Result)
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:138
bool isNothing() const
Definition: InputInfo.h:75
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
MultilibSet & FilterOut(FilterCallback F)
Filter out some subset of the Multilibs using a user defined callback.
Definition: Multilib.cpp:215
bool isFilename() const
Definition: InputInfo.h:76
const llvm::opt::OptTable & getOpts() const
Definition: Driver.h:297
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:654
static constexpr CPUSuffix Suffixes[]
Definition: Hexagon.cpp:149
bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple, StringRef GCCMultiarchTriple, StringRef TargetMultiarchTriple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Helper to add the variant paths of a libstdc++ installation.
Definition: Gnu.cpp:2398
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:1661
void getMipsCPUAndABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef &CPUName, StringRef &ABIName)
MultilibSet Multilibs
The set of multilibs that the detected installation supports.
Definition: Gnu.h:23
std::vector< std::string > flags_list
Definition: Multilib.h:27
std::vector< std::string > ExtraOpts
Definition: Linux.h:46
static bool isMicroMips(const ArgList &Args)
Definition: Gnu.cpp:871
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:148
int Major
The parsed major, minor, and patch numbers.
Definition: Gnu.h:162
static void findAndroidArmMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, DetectedMultilibs &Result)
Definition: Gnu.cpp:1383
static void findRISCVMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, DetectedMultilibs &Result)
Definition: Gnu.cpp:1425
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:732
Multilib SelectedMultilib
The primary multilib appropriate for the given flags.
Definition: Gnu.h:26
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:73
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:217