Hurd.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. std::string Hurd::getMultiarchTriple(const Driver &D,
  27. const llvm::Triple &TargetTriple,
  28. StringRef SysRoot) const {
  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, const ArgList &Args)
  56. : Generic_ELF(D, Triple, Args) {
  57. GCCInstallation.init(Triple, Args);
  58. Multilibs = GCCInstallation.getMultilibs();
  59. SelectedMultilib = GCCInstallation.getMultilib();
  60. std::string SysRoot = computeSysRoot();
  61. ToolChain::path_list &PPaths = getProgramPaths();
  62. Generic_GCC::PushPPaths(PPaths);
  63. // The selection of paths to try here is designed to match the patterns which
  64. // the GCC driver itself uses, as this is part of the GCC-compatible driver.
  65. // This was determined by running GCC in a fake filesystem, creating all
  66. // possible permutations of these directories, and seeing which ones it added
  67. // to the link paths.
  68. path_list &Paths = getFilePaths();
  69. const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
  70. const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
  71. #ifdef ENABLE_LINKER_BUILD_ID
  72. ExtraOpts.push_back("--build-id");
  73. #endif
  74. Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
  75. // Similar to the logic for GCC above, if we currently running Clang inside
  76. // of the requested system root, add its parent library paths to
  77. // those searched.
  78. // FIXME: It's not clear whether we should use the driver's installed
  79. // directory ('Dir' below) or the ResourceDir.
  80. if (StringRef(D.Dir).startswith(SysRoot)) {
  81. addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
  82. addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
  83. }
  84. addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
  85. addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
  86. addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
  87. addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
  88. Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
  89. // Similar to the logic for GCC above, if we are currently running Clang
  90. // inside of the requested system root, add its parent library path to those
  91. // searched.
  92. // FIXME: It's not clear whether we should use the driver's installed
  93. // directory ('Dir' below) or the ResourceDir.
  94. if (StringRef(D.Dir).startswith(SysRoot))
  95. addPathIfExists(D, D.Dir + "/../lib", Paths);
  96. addPathIfExists(D, SysRoot + "/lib", Paths);
  97. addPathIfExists(D, SysRoot + "/usr/lib", Paths);
  98. }
  99. bool Hurd::HasNativeLLVMSupport() const { return true; }
  100. Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
  101. Tool *Hurd::buildAssembler() const {
  102. return new tools::gnutools::Assembler(*this);
  103. }
  104. std::string Hurd::getDynamicLinker(const ArgList &Args) const {
  105. if (getArch() == llvm::Triple::x86)
  106. return "/lib/ld.so";
  107. llvm_unreachable("unsupported architecture");
  108. }
  109. void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  110. ArgStringList &CC1Args) const {
  111. const Driver &D = getDriver();
  112. std::string SysRoot = computeSysRoot();
  113. if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
  114. return;
  115. if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
  116. addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
  117. if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
  118. SmallString<128> P(D.ResourceDir);
  119. llvm::sys::path::append(P, "include");
  120. addSystemInclude(DriverArgs, CC1Args, P);
  121. }
  122. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  123. return;
  124. // Check for configure-time C include directories.
  125. StringRef CIncludeDirs(C_INCLUDE_DIRS);
  126. if (CIncludeDirs != "") {
  127. SmallVector<StringRef, 5> Dirs;
  128. CIncludeDirs.split(Dirs, ":");
  129. for (StringRef Dir : Dirs) {
  130. StringRef Prefix =
  131. llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
  132. addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
  133. }
  134. return;
  135. }
  136. // Lacking those, try to detect the correct set of system includes for the
  137. // target triple.
  138. AddMultilibIncludeArgs(DriverArgs, CC1Args);
  139. // On systems using multiarch, add /usr/include/$triple before
  140. // /usr/include.
  141. std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
  142. if (!MultiarchIncludeDir.empty() &&
  143. D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
  144. addExternCSystemInclude(DriverArgs, CC1Args,
  145. SysRoot + "/usr/include/" + MultiarchIncludeDir);
  146. // Add an include of '/include' directly. This isn't provided by default by
  147. // system GCCs, but is often used with cross-compiling GCCs, and harmless to
  148. // add even when Clang is acting as-if it were a system compiler.
  149. addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
  150. addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
  151. }
  152. void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
  153. llvm::opt::ArgStringList &CC1Args) const {
  154. // We need a detected GCC installation on Linux to provide libstdc++'s
  155. // headers in odd Linuxish places.
  156. if (!GCCInstallation.isValid())
  157. return;
  158. StringRef TripleStr = GCCInstallation.getTriple().str();
  159. StringRef DebianMultiarch =
  160. GCCInstallation.getTriple().getArch() == llvm::Triple::x86 ? "i386-gnu"
  161. : TripleStr;
  162. addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, DebianMultiarch);
  163. }
  164. void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
  165. for (const auto &Opt : ExtraOpts)
  166. CmdArgs.push_back(Opt.c_str());
  167. }