Hurd.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "Hurd.h"
  9. #include "CommonArgs.h"
  10. #include "clang/Config/config.h"
  11. #include "clang/Driver/Driver.h"
  12. #include "clang/Driver/Options.h"
  13. #include "llvm/Support/Path.h"
  14. #include "llvm/Support/VirtualFileSystem.h"
  15. using namespace clang::driver;
  16. using namespace clang::driver::toolchains;
  17. using namespace clang;
  18. using namespace llvm::opt;
  19. using tools::addPathIfExists;
  20. /// Get our best guess at the multiarch triple for a target.
  21. ///
  22. /// Debian-based systems are starting to use a multiarch setup where they use
  23. /// a target-triple directory in the library and header search paths.
  24. /// Unfortunately, this triple does not align with the vanilla target triple,
  25. /// so we provide a rough mapping here.
  26. static std::string getMultiarchTriple(const Driver &D,
  27. const llvm::Triple &TargetTriple,
  28. StringRef SysRoot) {
  29. if (TargetTriple.getArch() == llvm::Triple::x86) {
  30. // We use the existence of '/lib/<triple>' as a directory to detect some
  31. // common hurd triples that don't quite match the Clang triple for both
  32. // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
  33. // regardless of what the actual target triple is.
  34. if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
  35. return "i386-gnu";
  36. }
  37. // For most architectures, just use whatever we have rather than trying to be
  38. // clever.
  39. return TargetTriple.str();
  40. }
  41. static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
  42. // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
  43. // using that variant while targeting other architectures causes problems
  44. // because the libraries are laid out in shared system roots that can't cope
  45. // with a 'lib32' library search path being considered. So we only enable
  46. // them when we know we may need it.
  47. //
  48. // FIXME: This is a bit of a hack. We should really unify this code for
  49. // reasoning about oslibdir spellings with the lib dir spellings in the
  50. // GCCInstallationDetector, but that is a more significant refactoring.
  51. if (Triple.getArch() == llvm::Triple::x86)
  52. return "lib32";
  53. return Triple.isArch32Bit() ? "lib" : "lib64";
  54. }
  55. Hurd::Hurd(const Driver &D, const llvm::Triple &Triple,
  56. const ArgList &Args)
  57. : Generic_ELF(D, Triple, Args) {
  58. std::string SysRoot = computeSysRoot();
  59. path_list &Paths = getFilePaths();
  60. const std::string OSLibDir = getOSLibDir(Triple, Args);
  61. const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
  62. // If we are currently running Clang inside of the requested system root, add
  63. // its parent library paths to those searched.
  64. // FIXME: It's not clear whether we should use the driver's installed
  65. // directory ('Dir' below) or the ResourceDir.
  66. if (StringRef(D.Dir).startswith(SysRoot)) {
  67. addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
  68. addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
  69. }
  70. addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
  71. addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
  72. addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
  73. addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
  74. // If we are currently running Clang inside of the requested system root, add
  75. // its parent library path to those searched.
  76. // FIXME: It's not clear whether we should use the driver's installed
  77. // directory ('Dir' below) or the ResourceDir.
  78. if (StringRef(D.Dir).startswith(SysRoot))
  79. addPathIfExists(D, D.Dir + "/../lib", Paths);
  80. addPathIfExists(D, SysRoot + "/lib", Paths);
  81. addPathIfExists(D, SysRoot + "/usr/lib", Paths);
  82. }
  83. bool Hurd::HasNativeLLVMSupport() const { return true; }
  84. Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
  85. Tool *Hurd::buildAssembler() const {
  86. return new tools::gnutools::Assembler(*this);
  87. }
  88. std::string Hurd::computeSysRoot() const {
  89. if (!getDriver().SysRoot.empty())
  90. return getDriver().SysRoot;
  91. return std::string();
  92. }
  93. std::string Hurd::getDynamicLinker(const ArgList &Args) const {
  94. if (getArch() == llvm::Triple::x86)
  95. return "/lib/ld.so";
  96. llvm_unreachable("unsupported architecture");
  97. }
  98. void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  99. ArgStringList &CC1Args) const {
  100. const Driver &D = getDriver();
  101. std::string SysRoot = computeSysRoot();
  102. if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
  103. return;
  104. if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
  105. addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
  106. if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
  107. SmallString<128> P(D.ResourceDir);
  108. llvm::sys::path::append(P, "include");
  109. addSystemInclude(DriverArgs, CC1Args, P);
  110. }
  111. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  112. return;
  113. // Check for configure-time C include directories.
  114. StringRef CIncludeDirs(C_INCLUDE_DIRS);
  115. if (CIncludeDirs != "") {
  116. SmallVector<StringRef, 5> Dirs;
  117. CIncludeDirs.split(Dirs, ":");
  118. for (StringRef Dir : Dirs) {
  119. StringRef Prefix =
  120. llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : "";
  121. addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
  122. }
  123. return;
  124. }
  125. // Lacking those, try to detect the correct set of system includes for the
  126. // target triple.
  127. if (getTriple().getArch() == llvm::Triple::x86) {
  128. std::string Path = SysRoot + "/usr/include/i386-gnu";
  129. if (D.getVFS().exists(Path))
  130. addExternCSystemInclude(DriverArgs, CC1Args, Path);
  131. }
  132. // Add an include of '/include' directly. This isn't provided by default by
  133. // system GCCs, but is often used with cross-compiling GCCs, and harmless to
  134. // add even when Clang is acting as-if it were a system compiler.
  135. addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
  136. addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
  137. }