Coverage Report

Created: 2019-07-24 05:18

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