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 { 330b57cec5SDimitry Andric if (TargetTriple.getArch() == llvm::Triple::x86) { 340b57cec5SDimitry Andric // We use the existence of '/lib/<triple>' as a directory to detect some 350b57cec5SDimitry Andric // common hurd triples that don't quite match the Clang triple for both 360b57cec5SDimitry Andric // 32-bit and 64-bit targets. Multiarch fixes its install triples to these 370b57cec5SDimitry Andric // regardless of what the actual target triple is. 380b57cec5SDimitry Andric if (D.getVFS().exists(SysRoot + "/lib/i386-gnu")) 390b57cec5SDimitry Andric return "i386-gnu"; 400b57cec5SDimitry Andric } 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric // For most architectures, just use whatever we have rather than trying to be 430b57cec5SDimitry Andric // clever. 440b57cec5SDimitry Andric return TargetTriple.str(); 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { 480b57cec5SDimitry Andric // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and 490b57cec5SDimitry Andric // using that variant while targeting other architectures causes problems 500b57cec5SDimitry Andric // because the libraries are laid out in shared system roots that can't cope 510b57cec5SDimitry Andric // with a 'lib32' library search path being considered. So we only enable 520b57cec5SDimitry Andric // them when we know we may need it. 530b57cec5SDimitry Andric // 540b57cec5SDimitry Andric // FIXME: This is a bit of a hack. We should really unify this code for 550b57cec5SDimitry Andric // reasoning about oslibdir spellings with the lib dir spellings in the 560b57cec5SDimitry Andric // GCCInstallationDetector, but that is a more significant refactoring. 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric if (Triple.getArch() == llvm::Triple::x86) 590b57cec5SDimitry Andric return "lib32"; 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric return Triple.isArch32Bit() ? "lib" : "lib64"; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 64d65cd7a5SDimitry Andric Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 650b57cec5SDimitry Andric : Generic_ELF(D, Triple, Args) { 665ffd83dbSDimitry Andric GCCInstallation.init(Triple, Args); 675ffd83dbSDimitry Andric Multilibs = GCCInstallation.getMultilibs(); 685ffd83dbSDimitry Andric SelectedMultilib = GCCInstallation.getMultilib(); 690b57cec5SDimitry Andric std::string SysRoot = computeSysRoot(); 705ffd83dbSDimitry Andric ToolChain::path_list &PPaths = getProgramPaths(); 715ffd83dbSDimitry Andric 725ffd83dbSDimitry Andric Generic_GCC::PushPPaths(PPaths); 735ffd83dbSDimitry Andric 745ffd83dbSDimitry Andric // The selection of paths to try here is designed to match the patterns which 755ffd83dbSDimitry Andric // the GCC driver itself uses, as this is part of the GCC-compatible driver. 765ffd83dbSDimitry Andric // This was determined by running GCC in a fake filesystem, creating all 775ffd83dbSDimitry Andric // possible permutations of these directories, and seeing which ones it added 785ffd83dbSDimitry Andric // to the link paths. 790b57cec5SDimitry Andric path_list &Paths = getFilePaths(); 800b57cec5SDimitry Andric 815ffd83dbSDimitry Andric const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); 820b57cec5SDimitry Andric const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); 830b57cec5SDimitry Andric 84480093f4SDimitry Andric #ifdef ENABLE_LINKER_BUILD_ID 85480093f4SDimitry Andric ExtraOpts.push_back("--build-id"); 86480093f4SDimitry Andric #endif 87480093f4SDimitry Andric 885ffd83dbSDimitry Andric Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths); 895ffd83dbSDimitry Andric 905ffd83dbSDimitry Andric // Similar to the logic for GCC above, if we currently running Clang inside 915ffd83dbSDimitry Andric // of the requested system root, add its parent library paths to 925ffd83dbSDimitry Andric // those searched. 930b57cec5SDimitry Andric // FIXME: It's not clear whether we should use the driver's installed 940b57cec5SDimitry Andric // directory ('Dir' below) or the ResourceDir. 950b57cec5SDimitry Andric if (StringRef(D.Dir).startswith(SysRoot)) { 960b57cec5SDimitry Andric addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); 970b57cec5SDimitry Andric addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); 1010b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); 1040b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths); 1050b57cec5SDimitry Andric 1065ffd83dbSDimitry Andric Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); 1075ffd83dbSDimitry Andric 1085ffd83dbSDimitry Andric // Similar to the logic for GCC above, if we are currently running Clang 1095ffd83dbSDimitry Andric // inside of the requested system root, add its parent library path to those 1105ffd83dbSDimitry Andric // searched. 1110b57cec5SDimitry Andric // FIXME: It's not clear whether we should use the driver's installed 1120b57cec5SDimitry Andric // directory ('Dir' below) or the ResourceDir. 1130b57cec5SDimitry Andric if (StringRef(D.Dir).startswith(SysRoot)) 1140b57cec5SDimitry Andric addPathIfExists(D, D.Dir + "/../lib", Paths); 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/lib", Paths); 1170b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/usr/lib", Paths); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric bool Hurd::HasNativeLLVMSupport() const { return true; } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric Tool *Hurd::buildAssembler() const { 1250b57cec5SDimitry Andric return new tools::gnutools::Assembler(*this); 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric std::string Hurd::getDynamicLinker(const ArgList &Args) const { 1290b57cec5SDimitry Andric if (getArch() == llvm::Triple::x86) 1300b57cec5SDimitry Andric return "/lib/ld.so"; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric llvm_unreachable("unsupported architecture"); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 1360b57cec5SDimitry Andric ArgStringList &CC1Args) const { 1370b57cec5SDimitry Andric const Driver &D = getDriver(); 1380b57cec5SDimitry Andric std::string SysRoot = computeSysRoot(); 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 1410b57cec5SDimitry Andric return; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 1440b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 1470b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 1480b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 1490b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, P); 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 1530b57cec5SDimitry Andric return; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric // Check for configure-time C include directories. 1560b57cec5SDimitry Andric StringRef CIncludeDirs(C_INCLUDE_DIRS); 1570b57cec5SDimitry Andric if (CIncludeDirs != "") { 1580b57cec5SDimitry Andric SmallVector<StringRef, 5> Dirs; 1590b57cec5SDimitry Andric CIncludeDirs.split(Dirs, ":"); 1600b57cec5SDimitry Andric for (StringRef Dir : Dirs) { 1610b57cec5SDimitry Andric StringRef Prefix = 1625ffd83dbSDimitry Andric llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot); 1630b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric return; 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric // Lacking those, try to detect the correct set of system includes for the 1690b57cec5SDimitry Andric // target triple. 1705ffd83dbSDimitry Andric 1715ffd83dbSDimitry Andric AddMultilibIncludeArgs(DriverArgs, CC1Args); 1725ffd83dbSDimitry Andric 173*fe6060f1SDimitry Andric // On systems using multiarch, add /usr/include/$triple before 174*fe6060f1SDimitry Andric // /usr/include. 175*fe6060f1SDimitry Andric std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot); 176*fe6060f1SDimitry Andric if (!MultiarchIncludeDir.empty() && 177*fe6060f1SDimitry Andric D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir)) 178*fe6060f1SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, 179*fe6060f1SDimitry Andric SysRoot + "/usr/include/" + MultiarchIncludeDir); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric // Add an include of '/include' directly. This isn't provided by default by 1820b57cec5SDimitry Andric // system GCCs, but is often used with cross-compiling GCCs, and harmless to 1830b57cec5SDimitry Andric // add even when Clang is acting as-if it were a system compiler. 1840b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 1870b57cec5SDimitry Andric } 188d65cd7a5SDimitry Andric 189*fe6060f1SDimitry Andric void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 190*fe6060f1SDimitry Andric llvm::opt::ArgStringList &CC1Args) const { 191*fe6060f1SDimitry Andric // We need a detected GCC installation on Linux to provide libstdc++'s 192*fe6060f1SDimitry Andric // headers in odd Linuxish places. 193*fe6060f1SDimitry Andric if (!GCCInstallation.isValid()) 194*fe6060f1SDimitry Andric return; 195*fe6060f1SDimitry Andric 196*fe6060f1SDimitry Andric StringRef TripleStr = GCCInstallation.getTriple().str(); 197*fe6060f1SDimitry Andric StringRef DebianMultiarch = 198*fe6060f1SDimitry Andric GCCInstallation.getTriple().getArch() == llvm::Triple::x86 ? "i386-gnu" 199*fe6060f1SDimitry Andric : TripleStr; 200*fe6060f1SDimitry Andric 201*fe6060f1SDimitry Andric addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, DebianMultiarch); 202*fe6060f1SDimitry Andric } 203*fe6060f1SDimitry Andric 204d65cd7a5SDimitry Andric void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { 205d65cd7a5SDimitry Andric for (const auto &Opt : ExtraOpts) 206d65cd7a5SDimitry Andric CmdArgs.push_back(Opt.c_str()); 207d65cd7a5SDimitry Andric } 208