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