clang 22.0.0git
Hurd.cpp
Go to the documentation of this file.
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"
12#include "clang/Driver/Driver.h"
14#include "llvm/Support/Path.h"
15#include "llvm/Support/VirtualFileSystem.h"
16
17using namespace clang::driver;
18using namespace clang::driver::toolchains;
19using namespace clang;
20using namespace llvm::opt;
21
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.
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(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
55static 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
72Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
73 : Generic_ELF(D, Triple, Args) {
74 GCCInstallation.TripleToDebianMultiarch = [](const llvm::Triple &T) {
75 StringRef TripleStr = T.str();
76 StringRef DebianMultiarch =
77 T.getArch() == llvm::Triple::x86 ? "i386-gnu" : TripleStr;
78 return DebianMultiarch;
79 };
80
81 GCCInstallation.init(Triple, Args);
84 std::string SysRoot = computeSysRoot();
86
88
89 // The selection of paths to try here is designed to match the patterns which
90 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
91 // This was determined by running GCC in a fake filesystem, creating all
92 // possible permutations of these directories, and seeing which ones it added
93 // to the link paths.
94 path_list &Paths = getFilePaths();
95
96 const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
97 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
98
99#ifdef ENABLE_LINKER_BUILD_ID
100 ExtraOpts.push_back("--build-id");
101#endif
102
103 Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
104
105 // Similar to the logic for GCC above, if we currently running Clang inside
106 // of the requested system root, add its parent library paths to
107 // those searched.
108 // FIXME: It's not clear whether we should use the driver's installed
109 // directory ('Dir' below) or the ResourceDir.
110 if (StringRef(D.Dir).starts_with(SysRoot)) {
111 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
112 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
113 }
114
115 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
116 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
117
118 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
119 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
120
121 Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
122
123 // Similar to the logic for GCC above, if we are currently running Clang
124 // inside of the requested system root, add its parent library path to those
125 // searched.
126 // FIXME: It's not clear whether we should use the driver's installed
127 // directory ('Dir' below) or the ResourceDir.
128 if (StringRef(D.Dir).starts_with(SysRoot))
129 addPathIfExists(D, D.Dir + "/../lib", Paths);
130
131 addPathIfExists(D, SysRoot + "/lib", Paths);
132 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
133}
134
135bool Hurd::HasNativeLLVMSupport() const { return true; }
136
137Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
138
140 return new tools::gnutools::Assembler(*this);
141}
142
143std::string Hurd::getDynamicLinker(const ArgList &Args) const {
144 switch (getArch()) {
145 case llvm::Triple::x86:
146 return "/lib/ld.so";
147 case llvm::Triple::x86_64:
148 return "/lib/ld-x86-64.so.1";
149 default:
150 break;
151 }
152
153 llvm_unreachable("unsupported architecture");
154}
155
156void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
157 ArgStringList &CC1Args) const {
158 const Driver &D = getDriver();
159 std::string SysRoot = computeSysRoot();
160
161 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
162 return;
163
164 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
165 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
166
167 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
168 SmallString<128> P(D.ResourceDir);
169 llvm::sys::path::append(P, "include");
170 addSystemInclude(DriverArgs, CC1Args, P);
171 }
172
173 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
174 return;
175
176 // Check for configure-time C include directories.
177 StringRef CIncludeDirs(C_INCLUDE_DIRS);
178 if (CIncludeDirs != "") {
180 CIncludeDirs.split(Dirs, ":");
181 for (StringRef Dir : Dirs) {
182 StringRef Prefix =
183 llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
184 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
185 }
186 return;
187 }
188
189 // Lacking those, try to detect the correct set of system includes for the
190 // target triple.
191
192 AddMultilibIncludeArgs(DriverArgs, CC1Args);
193
194 // On systems using multiarch, add /usr/include/$triple before
195 // /usr/include.
196 std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
197 if (!MultiarchIncludeDir.empty() &&
198 D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
199 addExternCSystemInclude(DriverArgs, CC1Args,
200 SysRoot + "/usr/include/" + MultiarchIncludeDir);
201
202 // Add an include of '/include' directly. This isn't provided by default by
203 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
204 // add even when Clang is acting as-if it were a system compiler.
205 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
206
207 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
208}
209
210void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
211 llvm::opt::ArgStringList &CC1Args) const {
212 // We need a detected GCC installation on Linux to provide libstdc++'s
213 // headers in odd Linuxish places.
215 return;
216
217 addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args);
218}
219
220void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
221 for (const auto &Opt : ExtraOpts)
222 CmdArgs.push_back(Opt.c_str());
223}
StringRef P
const Decl * D
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args)
Definition: Hurd.cpp:55
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:99
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:1436
virtual std::string computeSysRoot() const
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: ToolChain.cpp:1292
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments.
Definition: ToolChain.cpp:1421
path_list & getFilePaths()
Definition: ToolChain.h:295
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:269
const Driver & getDriver() const
Definition: ToolChain.h:253
path_list & getProgramPaths()
Definition: ToolChain.h:298
const llvm::Triple & getTriple() const
Definition: ToolChain.h:255
llvm::SmallVector< Multilib > SelectedMultilibs
Definition: ToolChain.h:200
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:289
std::function< StringRef(const llvm::Triple &)> TripleToDebianMultiarch
Function for converting a triple to a Debian multiarch.
Definition: Gnu.h:251
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:267
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:2083
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:294
void AddMultilibPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, const std::string &MultiarchTriple, path_list &Paths)
Definition: Gnu.cpp:3087
void PushPPaths(ToolChain::path_list &PPaths)
Definition: Gnu.cpp:3072
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:357
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:3168
void AddMultiarchPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, path_list &Paths)
Definition: Gnu.cpp:3153
bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC) const
Definition: Gnu.cpp:3388
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Hurd.cpp:156
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Hurd.cpp:135
std::string getMultiarchTriple(const Driver &D, const llvm::Triple &TargetTriple, StringRef SysRoot) const override
Get our best guess at the multiarch triple for a target.
Definition: Hurd.cpp:30
std::vector< std::string > ExtraOpts
Definition: Hurd.h:37
Tool * buildAssembler() const override
Definition: Hurd.cpp:139
std::string getDynamicLinker(const llvm::opt::ArgList &Args) const override
Definition: Hurd.cpp:143
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Hurd.cpp:210
void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const override
Definition: Hurd.cpp:220
Hurd(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Hurd.cpp:72
Tool * buildLinker() const override
Definition: Hurd.cpp:137
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:354
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T