xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Driver/ToolChains/Hurd.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- Hurd.cpp - Hurd ToolChain Implementations --------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "Hurd.h"
107330f729Sjoerg #include "CommonArgs.h"
117330f729Sjoerg #include "clang/Config/config.h"
127330f729Sjoerg #include "clang/Driver/Driver.h"
137330f729Sjoerg #include "clang/Driver/Options.h"
147330f729Sjoerg #include "llvm/Support/Path.h"
157330f729Sjoerg #include "llvm/Support/VirtualFileSystem.h"
167330f729Sjoerg 
177330f729Sjoerg using namespace clang::driver;
187330f729Sjoerg using namespace clang::driver::toolchains;
197330f729Sjoerg using namespace clang;
207330f729Sjoerg using namespace llvm::opt;
217330f729Sjoerg 
227330f729Sjoerg using tools::addPathIfExists;
237330f729Sjoerg 
247330f729Sjoerg /// Get our best guess at the multiarch triple for a target.
257330f729Sjoerg ///
267330f729Sjoerg /// Debian-based systems are starting to use a multiarch setup where they use
277330f729Sjoerg /// a target-triple directory in the library and header search paths.
287330f729Sjoerg /// Unfortunately, this triple does not align with the vanilla target triple,
297330f729Sjoerg /// so we provide a rough mapping here.
getMultiarchTriple(const Driver & D,const llvm::Triple & TargetTriple,StringRef SysRoot) const30*e038c9c4Sjoerg std::string Hurd::getMultiarchTriple(const Driver &D,
317330f729Sjoerg                                      const llvm::Triple &TargetTriple,
32*e038c9c4Sjoerg                                      StringRef SysRoot) const {
337330f729Sjoerg   if (TargetTriple.getArch() == llvm::Triple::x86) {
347330f729Sjoerg     // We use the existence of '/lib/<triple>' as a directory to detect some
357330f729Sjoerg     // common hurd triples that don't quite match the Clang triple for both
367330f729Sjoerg     // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
377330f729Sjoerg     // regardless of what the actual target triple is.
387330f729Sjoerg     if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
397330f729Sjoerg       return "i386-gnu";
407330f729Sjoerg   }
417330f729Sjoerg 
427330f729Sjoerg   // For most architectures, just use whatever we have rather than trying to be
437330f729Sjoerg   // clever.
447330f729Sjoerg   return TargetTriple.str();
457330f729Sjoerg }
467330f729Sjoerg 
getOSLibDir(const llvm::Triple & Triple,const ArgList & Args)477330f729Sjoerg static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
487330f729Sjoerg   // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
497330f729Sjoerg   // using that variant while targeting other architectures causes problems
507330f729Sjoerg   // because the libraries are laid out in shared system roots that can't cope
517330f729Sjoerg   // with a 'lib32' library search path being considered. So we only enable
527330f729Sjoerg   // them when we know we may need it.
537330f729Sjoerg   //
547330f729Sjoerg   // FIXME: This is a bit of a hack. We should really unify this code for
557330f729Sjoerg   // reasoning about oslibdir spellings with the lib dir spellings in the
567330f729Sjoerg   // GCCInstallationDetector, but that is a more significant refactoring.
577330f729Sjoerg 
587330f729Sjoerg   if (Triple.getArch() == llvm::Triple::x86)
597330f729Sjoerg     return "lib32";
607330f729Sjoerg 
617330f729Sjoerg   return Triple.isArch32Bit() ? "lib" : "lib64";
627330f729Sjoerg }
637330f729Sjoerg 
Hurd(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)64*e038c9c4Sjoerg Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
657330f729Sjoerg     : Generic_ELF(D, Triple, Args) {
66*e038c9c4Sjoerg   GCCInstallation.init(Triple, Args);
67*e038c9c4Sjoerg   Multilibs = GCCInstallation.getMultilibs();
68*e038c9c4Sjoerg   SelectedMultilib = GCCInstallation.getMultilib();
697330f729Sjoerg   std::string SysRoot = computeSysRoot();
70*e038c9c4Sjoerg   ToolChain::path_list &PPaths = getProgramPaths();
71*e038c9c4Sjoerg 
72*e038c9c4Sjoerg   Generic_GCC::PushPPaths(PPaths);
73*e038c9c4Sjoerg 
74*e038c9c4Sjoerg   // The selection of paths to try here is designed to match the patterns which
75*e038c9c4Sjoerg   // the GCC driver itself uses, as this is part of the GCC-compatible driver.
76*e038c9c4Sjoerg   // This was determined by running GCC in a fake filesystem, creating all
77*e038c9c4Sjoerg   // possible permutations of these directories, and seeing which ones it added
78*e038c9c4Sjoerg   // to the link paths.
797330f729Sjoerg   path_list &Paths = getFilePaths();
807330f729Sjoerg 
81*e038c9c4Sjoerg   const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
827330f729Sjoerg   const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
837330f729Sjoerg 
84*e038c9c4Sjoerg #ifdef ENABLE_LINKER_BUILD_ID
85*e038c9c4Sjoerg   ExtraOpts.push_back("--build-id");
86*e038c9c4Sjoerg #endif
87*e038c9c4Sjoerg 
88*e038c9c4Sjoerg   Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
89*e038c9c4Sjoerg 
90*e038c9c4Sjoerg   // Similar to the logic for GCC above, if we currently running Clang inside
91*e038c9c4Sjoerg   // of the requested system root, add its parent library paths to
92*e038c9c4Sjoerg   // those searched.
937330f729Sjoerg   // FIXME: It's not clear whether we should use the driver's installed
947330f729Sjoerg   // directory ('Dir' below) or the ResourceDir.
957330f729Sjoerg   if (StringRef(D.Dir).startswith(SysRoot)) {
967330f729Sjoerg     addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
977330f729Sjoerg     addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
987330f729Sjoerg   }
997330f729Sjoerg 
1007330f729Sjoerg   addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
1017330f729Sjoerg   addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
1027330f729Sjoerg 
1037330f729Sjoerg   addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
1047330f729Sjoerg   addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
1057330f729Sjoerg 
106*e038c9c4Sjoerg   Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
107*e038c9c4Sjoerg 
108*e038c9c4Sjoerg   // Similar to the logic for GCC above, if we are currently running Clang
109*e038c9c4Sjoerg   // inside of the requested system root, add its parent library path to those
110*e038c9c4Sjoerg   // searched.
1117330f729Sjoerg   // FIXME: It's not clear whether we should use the driver's installed
1127330f729Sjoerg   // directory ('Dir' below) or the ResourceDir.
1137330f729Sjoerg   if (StringRef(D.Dir).startswith(SysRoot))
1147330f729Sjoerg     addPathIfExists(D, D.Dir + "/../lib", Paths);
1157330f729Sjoerg 
1167330f729Sjoerg   addPathIfExists(D, SysRoot + "/lib", Paths);
1177330f729Sjoerg   addPathIfExists(D, SysRoot + "/usr/lib", Paths);
1187330f729Sjoerg }
1197330f729Sjoerg 
HasNativeLLVMSupport() const1207330f729Sjoerg bool Hurd::HasNativeLLVMSupport() const { return true; }
1217330f729Sjoerg 
buildLinker() const1227330f729Sjoerg Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
1237330f729Sjoerg 
buildAssembler() const1247330f729Sjoerg Tool *Hurd::buildAssembler() const {
1257330f729Sjoerg   return new tools::gnutools::Assembler(*this);
1267330f729Sjoerg }
1277330f729Sjoerg 
getDynamicLinker(const ArgList & Args) const1287330f729Sjoerg std::string Hurd::getDynamicLinker(const ArgList &Args) const {
1297330f729Sjoerg   if (getArch() == llvm::Triple::x86)
1307330f729Sjoerg     return "/lib/ld.so";
1317330f729Sjoerg 
1327330f729Sjoerg   llvm_unreachable("unsupported architecture");
1337330f729Sjoerg }
1347330f729Sjoerg 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const1357330f729Sjoerg void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1367330f729Sjoerg                                      ArgStringList &CC1Args) const {
1377330f729Sjoerg   const Driver &D = getDriver();
1387330f729Sjoerg   std::string SysRoot = computeSysRoot();
1397330f729Sjoerg 
1407330f729Sjoerg   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
1417330f729Sjoerg     return;
1427330f729Sjoerg 
1437330f729Sjoerg   if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
1447330f729Sjoerg     addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
1457330f729Sjoerg 
1467330f729Sjoerg   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
1477330f729Sjoerg     SmallString<128> P(D.ResourceDir);
1487330f729Sjoerg     llvm::sys::path::append(P, "include");
1497330f729Sjoerg     addSystemInclude(DriverArgs, CC1Args, P);
1507330f729Sjoerg   }
1517330f729Sjoerg 
1527330f729Sjoerg   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
1537330f729Sjoerg     return;
1547330f729Sjoerg 
1557330f729Sjoerg   // Check for configure-time C include directories.
1567330f729Sjoerg   StringRef CIncludeDirs(C_INCLUDE_DIRS);
1577330f729Sjoerg   if (CIncludeDirs != "") {
1587330f729Sjoerg     SmallVector<StringRef, 5> Dirs;
1597330f729Sjoerg     CIncludeDirs.split(Dirs, ":");
1607330f729Sjoerg     for (StringRef Dir : Dirs) {
1617330f729Sjoerg       StringRef Prefix =
162*e038c9c4Sjoerg           llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
1637330f729Sjoerg       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
1647330f729Sjoerg     }
1657330f729Sjoerg     return;
1667330f729Sjoerg   }
1677330f729Sjoerg 
1687330f729Sjoerg   // Lacking those, try to detect the correct set of system includes for the
1697330f729Sjoerg   // target triple.
170*e038c9c4Sjoerg 
171*e038c9c4Sjoerg   AddMultilibIncludeArgs(DriverArgs, CC1Args);
172*e038c9c4Sjoerg 
173*e038c9c4Sjoerg   // On systems using multiarch, add /usr/include/$triple before
174*e038c9c4Sjoerg   // /usr/include.
175*e038c9c4Sjoerg   std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
176*e038c9c4Sjoerg   if (!MultiarchIncludeDir.empty() &&
177*e038c9c4Sjoerg       D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
178*e038c9c4Sjoerg     addExternCSystemInclude(DriverArgs, CC1Args,
179*e038c9c4Sjoerg                             SysRoot + "/usr/include/" + MultiarchIncludeDir);
1807330f729Sjoerg 
1817330f729Sjoerg   // Add an include of '/include' directly. This isn't provided by default by
1827330f729Sjoerg   // system GCCs, but is often used with cross-compiling GCCs, and harmless to
1837330f729Sjoerg   // add even when Clang is acting as-if it were a system compiler.
1847330f729Sjoerg   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
1857330f729Sjoerg 
1867330f729Sjoerg   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
1877330f729Sjoerg }
188*e038c9c4Sjoerg 
addLibStdCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const189*e038c9c4Sjoerg void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
190*e038c9c4Sjoerg                                     llvm::opt::ArgStringList &CC1Args) const {
191*e038c9c4Sjoerg   // We need a detected GCC installation on Linux to provide libstdc++'s
192*e038c9c4Sjoerg   // headers in odd Linuxish places.
193*e038c9c4Sjoerg   if (!GCCInstallation.isValid())
194*e038c9c4Sjoerg     return;
195*e038c9c4Sjoerg 
196*e038c9c4Sjoerg   StringRef TripleStr = GCCInstallation.getTriple().str();
197*e038c9c4Sjoerg   StringRef DebianMultiarch =
198*e038c9c4Sjoerg       GCCInstallation.getTriple().getArch() == llvm::Triple::x86 ? "i386-gnu"
199*e038c9c4Sjoerg                                                                  : TripleStr;
200*e038c9c4Sjoerg 
201*e038c9c4Sjoerg   addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, DebianMultiarch);
202*e038c9c4Sjoerg }
203*e038c9c4Sjoerg 
addExtraOpts(llvm::opt::ArgStringList & CmdArgs) const204*e038c9c4Sjoerg void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
205*e038c9c4Sjoerg   for (const auto &Opt : ExtraOpts)
206*e038c9c4Sjoerg     CmdArgs.push_back(Opt.c_str());
207*e038c9c4Sjoerg }
208