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