| 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 | |
| 9 | #include "Hurd.h" |
| 10 | #include "clang/Config/config.h" |
| 11 | #include "clang/Driver/CommonArgs.h" |
| 12 | #include "clang/Driver/Driver.h" |
| 13 | #include "clang/Options/Options.h" |
| 14 | #include "llvm/Support/Path.h" |
| 15 | #include "llvm/Support/VirtualFileSystem.h" |
| 16 | |
| 17 | using namespace clang::driver; |
| 18 | using namespace clang::driver::toolchains; |
| 19 | using namespace clang; |
| 20 | using namespace llvm::opt; |
| 21 | |
| 22 | using tools::addPathIfExists; |
| 23 | |
| 24 | /// Get our best guess at the multiarch triple for a target. |
| 25 | /// |
| 26 | /// Debian-based systems are starting to use a multiarch setup where they use |
| 27 | /// a target-triple directory in the library and header search paths. |
| 28 | /// Unfortunately, this triple does not align with the vanilla target triple, |
| 29 | /// so we provide a rough mapping here. |
| 30 | std::string Hurd::getMultiarchTriple(const Driver &D, |
| 31 | const llvm::Triple &TargetTriple, |
| 32 | StringRef SysRoot) const { |
| 33 | switch (TargetTriple.getArch()) { |
| 34 | default: |
| 35 | break; |
| 36 | |
| 37 | case llvm::Triple::aarch64: |
| 38 | return "aarch64-gnu" ; |
| 39 | |
| 40 | case llvm::Triple::riscv64: |
| 41 | return "riscv64-gnu" ; |
| 42 | |
| 43 | case llvm::Triple::x86: |
| 44 | // We use the existence of '/lib/<triple>' as a directory to detect some |
| 45 | // common hurd triples that don't quite match the Clang triple for both |
| 46 | // 32-bit and 64-bit targets. Multiarch fixes its install triples to these |
| 47 | // regardless of what the actual target triple is. |
| 48 | if (D.getVFS().exists(Path: SysRoot + "/lib/i386-gnu" )) |
| 49 | return "i386-gnu" ; |
| 50 | break; |
| 51 | |
| 52 | case llvm::Triple::x86_64: |
| 53 | return "x86_64-gnu" ; |
| 54 | } |
| 55 | |
| 56 | // For most architectures, just use whatever we have rather than trying to be |
| 57 | // clever. |
| 58 | return TargetTriple.str(); |
| 59 | } |
| 60 | |
| 61 | static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { |
| 62 | // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and |
| 63 | // using that variant while targeting other architectures causes problems |
| 64 | // because the libraries are laid out in shared system roots that can't cope |
| 65 | // with a 'lib32' library search path being considered. So we only enable |
| 66 | // them when we know we may need it. |
| 67 | // |
| 68 | // FIXME: This is a bit of a hack. We should really unify this code for |
| 69 | // reasoning about oslibdir spellings with the lib dir spellings in the |
| 70 | // GCCInstallationDetector, but that is a more significant refactoring. |
| 71 | |
| 72 | if (Triple.getArch() == llvm::Triple::x86) |
| 73 | return "lib32" ; |
| 74 | |
| 75 | return Triple.isArch32Bit() ? "lib" : "lib64" ; |
| 76 | } |
| 77 | |
| 78 | Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
| 79 | : Generic_ELF(D, Triple, Args) { |
| 80 | GCCInstallation.TripleToDebianMultiarch = [](const llvm::Triple &T) { |
| 81 | StringRef TripleStr = T.str(); |
| 82 | StringRef DebianMultiarch = |
| 83 | T.getArch() == llvm::Triple::x86 ? "i386-gnu" : TripleStr; |
| 84 | return DebianMultiarch; |
| 85 | }; |
| 86 | |
| 87 | GCCInstallation.init(TargetTriple: Triple, Args); |
| 88 | Multilibs = GCCInstallation.getMultilibs(); |
| 89 | SelectedMultilibs.assign(IL: {GCCInstallation.getMultilib()}); |
| 90 | std::string SysRoot = computeSysRoot(); |
| 91 | ToolChain::path_list &PPaths = getProgramPaths(); |
| 92 | |
| 93 | Generic_GCC::PushPPaths(PPaths); |
| 94 | |
| 95 | // The selection of paths to try here is designed to match the patterns which |
| 96 | // the GCC driver itself uses, as this is part of the GCC-compatible driver. |
| 97 | // This was determined by running GCC in a fake filesystem, creating all |
| 98 | // possible permutations of these directories, and seeing which ones it added |
| 99 | // to the link paths. |
| 100 | path_list &Paths = getFilePaths(); |
| 101 | |
| 102 | const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); |
| 103 | const std::string MultiarchTriple = getMultiarchTriple(D, TargetTriple: Triple, SysRoot); |
| 104 | |
| 105 | #ifdef ENABLE_LINKER_BUILD_ID |
| 106 | ExtraOpts.push_back("--build-id" ); |
| 107 | #endif |
| 108 | |
| 109 | Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths); |
| 110 | |
| 111 | // Similar to the logic for GCC above, if we currently running Clang inside |
| 112 | // of the requested system root, add its parent library paths to |
| 113 | // those searched. |
| 114 | // FIXME: It's not clear whether we should use the driver's installed |
| 115 | // directory ('Dir' below) or the ResourceDir. |
| 116 | if (StringRef(D.Dir).starts_with(Prefix: SysRoot)) { |
| 117 | addPathIfExists(D, Path: D.Dir + "/../lib/" + MultiarchTriple, Paths); |
| 118 | addPathIfExists(D, Path: D.Dir + "/../" + OSLibDir, Paths); |
| 119 | } |
| 120 | |
| 121 | addPathIfExists(D, Path: SysRoot + "/lib/" + MultiarchTriple, Paths); |
| 122 | addPathIfExists(D, Path: SysRoot + "/lib/../" + OSLibDir, Paths); |
| 123 | |
| 124 | addPathIfExists(D, Path: SysRoot + "/usr/lib/" + MultiarchTriple, Paths); |
| 125 | addPathIfExists(D, Path: SysRoot + "/usr/lib/../" + OSLibDir, Paths); |
| 126 | |
| 127 | Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); |
| 128 | |
| 129 | // Similar to the logic for GCC above, if we are currently running Clang |
| 130 | // inside of the requested system root, add its parent library path to those |
| 131 | // searched. |
| 132 | // FIXME: It's not clear whether we should use the driver's installed |
| 133 | // directory ('Dir' below) or the ResourceDir. |
| 134 | if (StringRef(D.Dir).starts_with(Prefix: SysRoot)) |
| 135 | addPathIfExists(D, Path: D.Dir + "/../lib" , Paths); |
| 136 | |
| 137 | addPathIfExists(D, Path: SysRoot + "/lib" , Paths); |
| 138 | addPathIfExists(D, Path: SysRoot + "/usr/lib" , Paths); |
| 139 | } |
| 140 | |
| 141 | bool Hurd::HasNativeLLVMSupport() const { return true; } |
| 142 | |
| 143 | Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); } |
| 144 | |
| 145 | Tool *Hurd::buildAssembler() const { |
| 146 | return new tools::gnutools::Assembler(*this); |
| 147 | } |
| 148 | |
| 149 | std::string Hurd::getDynamicLinker(const ArgList &Args) const { |
| 150 | switch (getArch()) { |
| 151 | case llvm::Triple::aarch64: |
| 152 | return "/lib/ld-aarch64.so.1" ; |
| 153 | case llvm::Triple::riscv64: |
| 154 | return "/lib/ld-riscv64-lp64.so.1" ; |
| 155 | case llvm::Triple::x86: |
| 156 | return "/lib/ld.so" ; |
| 157 | case llvm::Triple::x86_64: |
| 158 | return "/lib/ld-x86-64.so.1" ; |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | llvm_unreachable("unsupported architecture" ); |
| 164 | } |
| 165 | |
| 166 | void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 167 | ArgStringList &CC1Args) const { |
| 168 | const Driver &D = getDriver(); |
| 169 | std::string SysRoot = computeSysRoot(); |
| 170 | |
| 171 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc)) |
| 172 | return; |
| 173 | |
| 174 | if (!DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) |
| 175 | addSystemInclude(DriverArgs, CC1Args, Path: SysRoot + "/usr/local/include" ); |
| 176 | |
| 177 | if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) { |
| 178 | SmallString<128> P(D.ResourceDir); |
| 179 | llvm::sys::path::append(path&: P, a: "include" ); |
| 180 | addSystemInclude(DriverArgs, CC1Args, Path: P); |
| 181 | } |
| 182 | |
| 183 | if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) |
| 184 | return; |
| 185 | |
| 186 | // Check for configure-time C include directories. |
| 187 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 188 | if (CIncludeDirs != "" ) { |
| 189 | SmallVector<StringRef, 5> Dirs; |
| 190 | CIncludeDirs.split(A&: Dirs, Separator: ":" ); |
| 191 | for (StringRef Dir : Dirs) { |
| 192 | StringRef Prefix = |
| 193 | llvm::sys::path::is_absolute(path: Dir) ? "" : StringRef(SysRoot); |
| 194 | addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + Dir); |
| 195 | } |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | // Lacking those, try to detect the correct set of system includes for the |
| 200 | // target triple. |
| 201 | |
| 202 | AddMultilibIncludeArgs(DriverArgs, CC1Args); |
| 203 | |
| 204 | // On systems using multiarch, add /usr/include/$triple before |
| 205 | // /usr/include. |
| 206 | std::string MultiarchIncludeDir = getMultiarchTriple(D, TargetTriple: getTriple(), SysRoot); |
| 207 | if (!MultiarchIncludeDir.empty() && |
| 208 | D.getVFS().exists(Path: SysRoot + "/usr/include/" + MultiarchIncludeDir)) |
| 209 | addExternCSystemInclude(DriverArgs, CC1Args, |
| 210 | Path: SysRoot + "/usr/include/" + MultiarchIncludeDir); |
| 211 | |
| 212 | // Add an include of '/include' directly. This isn't provided by default by |
| 213 | // system GCCs, but is often used with cross-compiling GCCs, and harmless to |
| 214 | // add even when Clang is acting as-if it were a system compiler. |
| 215 | addExternCSystemInclude(DriverArgs, CC1Args, Path: SysRoot + "/include" ); |
| 216 | |
| 217 | addExternCSystemInclude(DriverArgs, CC1Args, Path: SysRoot + "/usr/include" ); |
| 218 | } |
| 219 | |
| 220 | void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, |
| 221 | llvm::opt::ArgStringList &CC1Args) const { |
| 222 | // We need a detected GCC installation on Linux to provide libstdc++'s |
| 223 | // headers in odd Linuxish places. |
| 224 | if (!GCCInstallation.isValid()) |
| 225 | return; |
| 226 | |
| 227 | addGCCLibStdCxxIncludePaths(DriverArgs, CC&: CC1Args); |
| 228 | } |
| 229 | |
| 230 | void Hurd::(llvm::opt::ArgStringList &CmdArgs) const { |
| 231 | for (const auto &Opt : ExtraOpts) |
| 232 | CmdArgs.push_back(Elt: Opt.c_str()); |
| 233 | } |
| 234 | |