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