xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Hurd.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric //===--- Hurd.cpp - Hurd ToolChain Implementations --------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "Hurd.h"
100b57cec5SDimitry Andric #include "CommonArgs.h"
110b57cec5SDimitry Andric #include "clang/Config/config.h"
120b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
130b57cec5SDimitry Andric #include "clang/Driver/Options.h"
140b57cec5SDimitry Andric #include "llvm/Support/Path.h"
150b57cec5SDimitry Andric #include "llvm/Support/VirtualFileSystem.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric using namespace clang::driver;
180b57cec5SDimitry Andric using namespace clang::driver::toolchains;
190b57cec5SDimitry Andric using namespace clang;
200b57cec5SDimitry Andric using namespace llvm::opt;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using tools::addPathIfExists;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric /// Get our best guess at the multiarch triple for a target.
250b57cec5SDimitry Andric ///
260b57cec5SDimitry Andric /// Debian-based systems are starting to use a multiarch setup where they use
270b57cec5SDimitry Andric /// a target-triple directory in the library and header search paths.
280b57cec5SDimitry Andric /// Unfortunately, this triple does not align with the vanilla target triple,
290b57cec5SDimitry Andric /// so we provide a rough mapping here.
30480093f4SDimitry Andric std::string Hurd::getMultiarchTriple(const Driver &D,
310b57cec5SDimitry Andric                                      const llvm::Triple &TargetTriple,
32480093f4SDimitry Andric                                      StringRef SysRoot) const {
33*7a6dacacSDimitry Andric   switch (TargetTriple.getArch()) {
34*7a6dacacSDimitry Andric   default:
35*7a6dacacSDimitry Andric     break;
36*7a6dacacSDimitry Andric 
37*7a6dacacSDimitry Andric   case llvm::Triple::x86:
380b57cec5SDimitry Andric     // We use the existence of '/lib/<triple>' as a directory to detect some
390b57cec5SDimitry Andric     // common hurd triples that don't quite match the Clang triple for both
400b57cec5SDimitry Andric     // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
410b57cec5SDimitry Andric     // regardless of what the actual target triple is.
420b57cec5SDimitry Andric     if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
430b57cec5SDimitry Andric       return "i386-gnu";
44*7a6dacacSDimitry Andric     break;
45*7a6dacacSDimitry Andric 
46*7a6dacacSDimitry Andric   case llvm::Triple::x86_64:
47*7a6dacacSDimitry Andric     return "x86_64-gnu";
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   // For most architectures, just use whatever we have rather than trying to be
510b57cec5SDimitry Andric   // clever.
520b57cec5SDimitry Andric   return TargetTriple.str();
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
560b57cec5SDimitry Andric   // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
570b57cec5SDimitry Andric   // using that variant while targeting other architectures causes problems
580b57cec5SDimitry Andric   // because the libraries are laid out in shared system roots that can't cope
590b57cec5SDimitry Andric   // with a 'lib32' library search path being considered. So we only enable
600b57cec5SDimitry Andric   // them when we know we may need it.
610b57cec5SDimitry Andric   //
620b57cec5SDimitry Andric   // FIXME: This is a bit of a hack. We should really unify this code for
630b57cec5SDimitry Andric   // reasoning about oslibdir spellings with the lib dir spellings in the
640b57cec5SDimitry Andric   // GCCInstallationDetector, but that is a more significant refactoring.
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   if (Triple.getArch() == llvm::Triple::x86)
670b57cec5SDimitry Andric     return "lib32";
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   return Triple.isArch32Bit() ? "lib" : "lib64";
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
72d65cd7a5SDimitry Andric Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
730b57cec5SDimitry Andric     : Generic_ELF(D, Triple, Args) {
745ffd83dbSDimitry Andric   GCCInstallation.init(Triple, Args);
755ffd83dbSDimitry Andric   Multilibs = GCCInstallation.getMultilibs();
7606c3fb27SDimitry Andric   SelectedMultilibs.assign({GCCInstallation.getMultilib()});
770b57cec5SDimitry Andric   std::string SysRoot = computeSysRoot();
785ffd83dbSDimitry Andric   ToolChain::path_list &PPaths = getProgramPaths();
795ffd83dbSDimitry Andric 
805ffd83dbSDimitry Andric   Generic_GCC::PushPPaths(PPaths);
815ffd83dbSDimitry Andric 
825ffd83dbSDimitry Andric   // The selection of paths to try here is designed to match the patterns which
835ffd83dbSDimitry Andric   // the GCC driver itself uses, as this is part of the GCC-compatible driver.
845ffd83dbSDimitry Andric   // This was determined by running GCC in a fake filesystem, creating all
855ffd83dbSDimitry Andric   // possible permutations of these directories, and seeing which ones it added
865ffd83dbSDimitry Andric   // to the link paths.
870b57cec5SDimitry Andric   path_list &Paths = getFilePaths();
880b57cec5SDimitry Andric 
895ffd83dbSDimitry Andric   const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
900b57cec5SDimitry Andric   const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
910b57cec5SDimitry Andric 
92480093f4SDimitry Andric #ifdef ENABLE_LINKER_BUILD_ID
93480093f4SDimitry Andric   ExtraOpts.push_back("--build-id");
94480093f4SDimitry Andric #endif
95480093f4SDimitry Andric 
965ffd83dbSDimitry Andric   Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
975ffd83dbSDimitry Andric 
985ffd83dbSDimitry Andric   // Similar to the logic for GCC above, if we currently running Clang inside
995ffd83dbSDimitry Andric   // of the requested system root, add its parent library paths to
1005ffd83dbSDimitry Andric   // those searched.
1010b57cec5SDimitry Andric   // FIXME: It's not clear whether we should use the driver's installed
1020b57cec5SDimitry Andric   // directory ('Dir' below) or the ResourceDir.
1035f757f3fSDimitry Andric   if (StringRef(D.Dir).starts_with(SysRoot)) {
1040b57cec5SDimitry Andric     addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
1050b57cec5SDimitry Andric     addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
1090b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
1120b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
1130b57cec5SDimitry Andric 
1145ffd83dbSDimitry Andric   Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
1155ffd83dbSDimitry Andric 
1165ffd83dbSDimitry Andric   // Similar to the logic for GCC above, if we are currently running Clang
1175ffd83dbSDimitry Andric   // inside of the requested system root, add its parent library path to those
1185ffd83dbSDimitry Andric   // searched.
1190b57cec5SDimitry Andric   // FIXME: It's not clear whether we should use the driver's installed
1200b57cec5SDimitry Andric   // directory ('Dir' below) or the ResourceDir.
1215f757f3fSDimitry Andric   if (StringRef(D.Dir).starts_with(SysRoot))
1220b57cec5SDimitry Andric     addPathIfExists(D, D.Dir + "/../lib", Paths);
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/lib", Paths);
1250b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/usr/lib", Paths);
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric bool Hurd::HasNativeLLVMSupport() const { return true; }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric Tool *Hurd::buildAssembler() const {
1330b57cec5SDimitry Andric   return new tools::gnutools::Assembler(*this);
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric std::string Hurd::getDynamicLinker(const ArgList &Args) const {
137*7a6dacacSDimitry Andric   switch (getArch()) {
138*7a6dacacSDimitry Andric   case llvm::Triple::x86:
1390b57cec5SDimitry Andric     return "/lib/ld.so";
140*7a6dacacSDimitry Andric   case llvm::Triple::x86_64:
141*7a6dacacSDimitry Andric     return "/lib/ld-x86-64.so.1";
142*7a6dacacSDimitry Andric   default:
143*7a6dacacSDimitry Andric     break;
144*7a6dacacSDimitry Andric   }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   llvm_unreachable("unsupported architecture");
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1500b57cec5SDimitry Andric                                      ArgStringList &CC1Args) const {
1510b57cec5SDimitry Andric   const Driver &D = getDriver();
1520b57cec5SDimitry Andric   std::string SysRoot = computeSysRoot();
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
1550b57cec5SDimitry Andric     return;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
1580b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
1610b57cec5SDimitry Andric     SmallString<128> P(D.ResourceDir);
1620b57cec5SDimitry Andric     llvm::sys::path::append(P, "include");
1630b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
1640b57cec5SDimitry Andric   }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
1670b57cec5SDimitry Andric     return;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // Check for configure-time C include directories.
1700b57cec5SDimitry Andric   StringRef CIncludeDirs(C_INCLUDE_DIRS);
1710b57cec5SDimitry Andric   if (CIncludeDirs != "") {
1720b57cec5SDimitry Andric     SmallVector<StringRef, 5> Dirs;
1730b57cec5SDimitry Andric     CIncludeDirs.split(Dirs, ":");
1740b57cec5SDimitry Andric     for (StringRef Dir : Dirs) {
1750b57cec5SDimitry Andric       StringRef Prefix =
1765ffd83dbSDimitry Andric           llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
1770b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
1780b57cec5SDimitry Andric     }
1790b57cec5SDimitry Andric     return;
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   // Lacking those, try to detect the correct set of system includes for the
1830b57cec5SDimitry Andric   // target triple.
1845ffd83dbSDimitry Andric 
1855ffd83dbSDimitry Andric   AddMultilibIncludeArgs(DriverArgs, CC1Args);
1865ffd83dbSDimitry Andric 
187fe6060f1SDimitry Andric   // On systems using multiarch, add /usr/include/$triple before
188fe6060f1SDimitry Andric   // /usr/include.
189fe6060f1SDimitry Andric   std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
190fe6060f1SDimitry Andric   if (!MultiarchIncludeDir.empty() &&
191fe6060f1SDimitry Andric       D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
192fe6060f1SDimitry Andric     addExternCSystemInclude(DriverArgs, CC1Args,
193fe6060f1SDimitry Andric                             SysRoot + "/usr/include/" + MultiarchIncludeDir);
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   // Add an include of '/include' directly. This isn't provided by default by
1960b57cec5SDimitry Andric   // system GCCs, but is often used with cross-compiling GCCs, and harmless to
1970b57cec5SDimitry Andric   // add even when Clang is acting as-if it were a system compiler.
1980b57cec5SDimitry Andric   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
2010b57cec5SDimitry Andric }
202d65cd7a5SDimitry Andric 
203fe6060f1SDimitry Andric void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
204fe6060f1SDimitry Andric                                     llvm::opt::ArgStringList &CC1Args) const {
205fe6060f1SDimitry Andric   // We need a detected GCC installation on Linux to provide libstdc++'s
206fe6060f1SDimitry Andric   // headers in odd Linuxish places.
207fe6060f1SDimitry Andric   if (!GCCInstallation.isValid())
208fe6060f1SDimitry Andric     return;
209fe6060f1SDimitry Andric 
210fe6060f1SDimitry Andric   StringRef TripleStr = GCCInstallation.getTriple().str();
211fe6060f1SDimitry Andric   StringRef DebianMultiarch =
212fe6060f1SDimitry Andric       GCCInstallation.getTriple().getArch() == llvm::Triple::x86 ? "i386-gnu"
213fe6060f1SDimitry Andric                                                                  : TripleStr;
214fe6060f1SDimitry Andric 
215fe6060f1SDimitry Andric   addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, DebianMultiarch);
216fe6060f1SDimitry Andric }
217fe6060f1SDimitry Andric 
218d65cd7a5SDimitry Andric void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
219d65cd7a5SDimitry Andric   for (const auto &Opt : ExtraOpts)
220d65cd7a5SDimitry Andric     CmdArgs.push_back(Opt.c_str());
221d65cd7a5SDimitry Andric }
222