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. 30*480093f4SDimitry Andric std::string Hurd::getMultiarchTriple(const Driver &D, 310b57cec5SDimitry Andric const llvm::Triple &TargetTriple, 32*480093f4SDimitry 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 640b57cec5SDimitry Andric Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, 650b57cec5SDimitry Andric const ArgList &Args) 660b57cec5SDimitry Andric : Generic_ELF(D, Triple, Args) { 670b57cec5SDimitry Andric std::string SysRoot = computeSysRoot(); 680b57cec5SDimitry Andric path_list &Paths = getFilePaths(); 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric const std::string OSLibDir = getOSLibDir(Triple, Args); 710b57cec5SDimitry Andric const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); 720b57cec5SDimitry Andric 73*480093f4SDimitry Andric #ifdef ENABLE_LINKER_BUILD_ID 74*480093f4SDimitry Andric ExtraOpts.push_back("--build-id"); 75*480093f4SDimitry Andric #endif 76*480093f4SDimitry Andric 770b57cec5SDimitry Andric // If we are currently running Clang inside of the requested system root, add 780b57cec5SDimitry Andric // its parent library paths to those searched. 790b57cec5SDimitry Andric // FIXME: It's not clear whether we should use the driver's installed 800b57cec5SDimitry Andric // directory ('Dir' below) or the ResourceDir. 810b57cec5SDimitry Andric if (StringRef(D.Dir).startswith(SysRoot)) { 820b57cec5SDimitry Andric addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); 830b57cec5SDimitry Andric addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); 870b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); 900b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric // If we are currently running Clang inside of the requested system root, add 930b57cec5SDimitry Andric // its parent library path to those searched. 940b57cec5SDimitry Andric // FIXME: It's not clear whether we should use the driver's installed 950b57cec5SDimitry Andric // directory ('Dir' below) or the ResourceDir. 960b57cec5SDimitry Andric if (StringRef(D.Dir).startswith(SysRoot)) 970b57cec5SDimitry Andric addPathIfExists(D, D.Dir + "/../lib", Paths); 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/lib", Paths); 1000b57cec5SDimitry Andric addPathIfExists(D, SysRoot + "/usr/lib", Paths); 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric bool Hurd::HasNativeLLVMSupport() const { return true; } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric Tool *Hurd::buildAssembler() const { 1080b57cec5SDimitry Andric return new tools::gnutools::Assembler(*this); 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric std::string Hurd::computeSysRoot() const { 1120b57cec5SDimitry Andric if (!getDriver().SysRoot.empty()) 1130b57cec5SDimitry Andric return getDriver().SysRoot; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric return std::string(); 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric std::string Hurd::getDynamicLinker(const ArgList &Args) const { 1190b57cec5SDimitry Andric if (getArch() == llvm::Triple::x86) 1200b57cec5SDimitry Andric return "/lib/ld.so"; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric llvm_unreachable("unsupported architecture"); 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 1260b57cec5SDimitry Andric ArgStringList &CC1Args) const { 1270b57cec5SDimitry Andric const Driver &D = getDriver(); 1280b57cec5SDimitry Andric std::string SysRoot = computeSysRoot(); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 1310b57cec5SDimitry Andric return; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 1340b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 1370b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 1380b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 1390b57cec5SDimitry Andric addSystemInclude(DriverArgs, CC1Args, P); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 1430b57cec5SDimitry Andric return; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric // Check for configure-time C include directories. 1460b57cec5SDimitry Andric StringRef CIncludeDirs(C_INCLUDE_DIRS); 1470b57cec5SDimitry Andric if (CIncludeDirs != "") { 1480b57cec5SDimitry Andric SmallVector<StringRef, 5> Dirs; 1490b57cec5SDimitry Andric CIncludeDirs.split(Dirs, ":"); 1500b57cec5SDimitry Andric for (StringRef Dir : Dirs) { 1510b57cec5SDimitry Andric StringRef Prefix = 1520b57cec5SDimitry Andric llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : ""; 1530b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric return; 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric // Lacking those, try to detect the correct set of system includes for the 1590b57cec5SDimitry Andric // target triple. 1600b57cec5SDimitry Andric if (getTriple().getArch() == llvm::Triple::x86) { 1610b57cec5SDimitry Andric std::string Path = SysRoot + "/usr/include/i386-gnu"; 1620b57cec5SDimitry Andric if (D.getVFS().exists(Path)) 1630b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, Path); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric // Add an include of '/include' directly. This isn't provided by default by 1670b57cec5SDimitry Andric // system GCCs, but is often used with cross-compiling GCCs, and harmless to 1680b57cec5SDimitry Andric // add even when Clang is acting as-if it were a system compiler. 1690b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 1720b57cec5SDimitry Andric } 173