brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.3 KiB · 53ee4d4 Raw
234 lines · cpp
1//===--- Hurd.cpp - Hurd ToolChain Implementations --------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "Hurd.h"10#include "clang/Config/config.h"11#include "clang/Driver/CommonArgs.h"12#include "clang/Driver/Driver.h"13#include "clang/Options/Options.h"14#include "llvm/Support/Path.h"15#include "llvm/Support/VirtualFileSystem.h"16 17using namespace clang::driver;18using namespace clang::driver::toolchains;19using namespace clang;20using namespace llvm::opt;21 22using tools::addPathIfExists;23 24/// Get our best guess at the multiarch triple for a target.25///26/// Debian-based systems are starting to use a multiarch setup where they use27/// a target-triple directory in the library and header search paths.28/// Unfortunately, this triple does not align with the vanilla target triple,29/// so we provide a rough mapping here.30std::string Hurd::getMultiarchTriple(const Driver &D,31                                     const llvm::Triple &TargetTriple,32                                     StringRef SysRoot) const {33  switch (TargetTriple.getArch()) {34  default:35    break;36 37  case llvm::Triple::aarch64:38    return "aarch64-gnu";39 40  case llvm::Triple::riscv64:41    return "riscv64-gnu";42 43  case llvm::Triple::x86:44    // We use the existence of '/lib/<triple>' as a directory to detect some45    // common hurd triples that don't quite match the Clang triple for both46    // 32-bit and 64-bit targets. Multiarch fixes its install triples to these47    // regardless of what the actual target triple is.48    if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))49      return "i386-gnu";50    break;51 52  case llvm::Triple::x86_64:53    return "x86_64-gnu";54  }55 56  // For most architectures, just use whatever we have rather than trying to be57  // clever.58  return TargetTriple.str();59}60 61static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {62  // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and63  // using that variant while targeting other architectures causes problems64  // because the libraries are laid out in shared system roots that can't cope65  // with a 'lib32' library search path being considered. So we only enable66  // them when we know we may need it.67  //68  // FIXME: This is a bit of a hack. We should really unify this code for69  // reasoning about oslibdir spellings with the lib dir spellings in the70  // GCCInstallationDetector, but that is a more significant refactoring.71 72  if (Triple.getArch() == llvm::Triple::x86)73    return "lib32";74 75  return Triple.isArch32Bit() ? "lib" : "lib64";76}77 78Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)79    : Generic_ELF(D, Triple, Args) {80  GCCInstallation.TripleToDebianMultiarch = [](const llvm::Triple &T) {81    StringRef TripleStr = T.str();82    StringRef DebianMultiarch =83        T.getArch() == llvm::Triple::x86 ? "i386-gnu" : TripleStr;84    return DebianMultiarch;85  };86 87  GCCInstallation.init(Triple, Args);88  Multilibs = GCCInstallation.getMultilibs();89  SelectedMultilibs.assign({GCCInstallation.getMultilib()});90  std::string SysRoot = computeSysRoot();91  ToolChain::path_list &PPaths = getProgramPaths();92 93  Generic_GCC::PushPPaths(PPaths);94 95  // The selection of paths to try here is designed to match the patterns which96  // the GCC driver itself uses, as this is part of the GCC-compatible driver.97  // This was determined by running GCC in a fake filesystem, creating all98  // possible permutations of these directories, and seeing which ones it added99  // to the link paths.100  path_list &Paths = getFilePaths();101 102  const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));103  const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);104 105#ifdef ENABLE_LINKER_BUILD_ID106  ExtraOpts.push_back("--build-id");107#endif108 109  Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);110 111  // Similar to the logic for GCC above, if we currently running Clang inside112  // of the requested system root, add its parent library paths to113  // those searched.114  // FIXME: It's not clear whether we should use the driver's installed115  // directory ('Dir' below) or the ResourceDir.116  if (StringRef(D.Dir).starts_with(SysRoot)) {117    addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);118    addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);119  }120 121  addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);122  addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);123 124  addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);125  addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);126 127  Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);128 129  // Similar to the logic for GCC above, if we are currently running Clang130  // inside of the requested system root, add its parent library path to those131  // searched.132  // FIXME: It's not clear whether we should use the driver's installed133  // directory ('Dir' below) or the ResourceDir.134  if (StringRef(D.Dir).starts_with(SysRoot))135    addPathIfExists(D, D.Dir + "/../lib", Paths);136 137  addPathIfExists(D, SysRoot + "/lib", Paths);138  addPathIfExists(D, SysRoot + "/usr/lib", Paths);139}140 141bool Hurd::HasNativeLLVMSupport() const { return true; }142 143Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }144 145Tool *Hurd::buildAssembler() const {146  return new tools::gnutools::Assembler(*this);147}148 149std::string Hurd::getDynamicLinker(const ArgList &Args) const {150  switch (getArch()) {151  case llvm::Triple::aarch64:152    return "/lib/ld-aarch64.so.1";153  case llvm::Triple::riscv64:154    return "/lib/ld-riscv64-lp64.so.1";155  case llvm::Triple::x86:156    return "/lib/ld.so";157  case llvm::Triple::x86_64:158    return "/lib/ld-x86-64.so.1";159  default:160    break;161  }162 163  llvm_unreachable("unsupported architecture");164}165 166void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,167                                     ArgStringList &CC1Args) const {168  const Driver &D = getDriver();169  std::string SysRoot = computeSysRoot();170 171  if (DriverArgs.hasArg(options::OPT_nostdinc))172    return;173 174  if (!DriverArgs.hasArg(options::OPT_nostdlibinc))175    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");176 177  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {178    SmallString<128> P(D.ResourceDir);179    llvm::sys::path::append(P, "include");180    addSystemInclude(DriverArgs, CC1Args, P);181  }182 183  if (DriverArgs.hasArg(options::OPT_nostdlibinc))184    return;185 186  // Check for configure-time C include directories.187  StringRef CIncludeDirs(C_INCLUDE_DIRS);188  if (CIncludeDirs != "") {189    SmallVector<StringRef, 5> Dirs;190    CIncludeDirs.split(Dirs, ":");191    for (StringRef Dir : Dirs) {192      StringRef Prefix =193          llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);194      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);195    }196    return;197  }198 199  // Lacking those, try to detect the correct set of system includes for the200  // target triple.201 202  AddMultilibIncludeArgs(DriverArgs, CC1Args);203 204  // On systems using multiarch, add /usr/include/$triple before205  // /usr/include.206  std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);207  if (!MultiarchIncludeDir.empty() &&208      D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))209    addExternCSystemInclude(DriverArgs, CC1Args,210                            SysRoot + "/usr/include/" + MultiarchIncludeDir);211 212  // Add an include of '/include' directly. This isn't provided by default by213  // system GCCs, but is often used with cross-compiling GCCs, and harmless to214  // add even when Clang is acting as-if it were a system compiler.215  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");216 217  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");218}219 220void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,221                                    llvm::opt::ArgStringList &CC1Args) const {222  // We need a detected GCC installation on Linux to provide libstdc++'s223  // headers in odd Linuxish places.224  if (!GCCInstallation.isValid())225    return;226 227  addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args);228}229 230void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {231  for (const auto &Opt : ExtraOpts)232    CmdArgs.push_back(Opt.c_str());233}234