clang 20.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 "CommonArgs.h"
11#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.init(Triple, Args);
77 std::string SysRoot = computeSysRoot();
79
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, 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(SysRoot)) {
104 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
105 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
106 }
107
108 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
109 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
110
111 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
112 addPathIfExists(D, 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(SysRoot))
122 addPathIfExists(D, D.Dir + "/../lib", Paths);
123
124 addPathIfExists(D, SysRoot + "/lib", Paths);
125 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
126}
127
128bool Hurd::HasNativeLLVMSupport() const { return true; }
129
130Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
131
133 return new tools::gnutools::Assembler(*this);
134}
135
136std::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
149void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
150 ArgStringList &CC1Args) const {
151 const Driver &D = getDriver();
152 std::string SysRoot = computeSysRoot();
153
154 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
155 return;
156
157 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
158 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
159
160 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
161 SmallString<128> P(D.ResourceDir);
162 llvm::sys::path::append(P, "include");
163 addSystemInclude(DriverArgs, CC1Args, P);
164 }
165
166 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
167 return;
168
169 // Check for configure-time C include directories.
170 StringRef CIncludeDirs(C_INCLUDE_DIRS);
171 if (CIncludeDirs != "") {
173 CIncludeDirs.split(Dirs, ":");
174 for (StringRef Dir : Dirs) {
175 StringRef Prefix =
176 llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
177 addExternCSystemInclude(DriverArgs, CC1Args, 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, getTriple(), SysRoot);
190 if (!MultiarchIncludeDir.empty() &&
191 D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
192 addExternCSystemInclude(DriverArgs, CC1Args,
193 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, SysRoot + "/include");
199
200 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
201}
202
203void 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.
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
218void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
219 for (const auto &Opt : ExtraOpts)
220 CmdArgs.push_back(Opt.c_str());
221}
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:77
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:1209
virtual std::string computeSysRoot() const
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: ToolChain.cpp:1095
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:1224
path_list & getFilePaths()
Definition: ToolChain.h:294
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:268
const Driver & getDriver() const
Definition: ToolChain.h:252
path_list & getProgramPaths()
Definition: ToolChain.h:297
const llvm::Triple & getTriple() const
Definition: ToolChain.h:254
llvm::SmallVector< Multilib > SelectedMultilibs
Definition: ToolChain.h:201
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:227
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:2219
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:239
void AddMultilibPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, const std::string &MultiarchTriple, path_list &Paths)
Definition: Gnu.cpp:3153
bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, StringRef DebianMultiarch) const
Definition: Gnu.cpp:3359
void PushPPaths(ToolChain::path_list &PPaths)
Definition: Gnu.cpp:3138
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:3234
void AddMultiarchPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, path_list &Paths)
Definition: Gnu.cpp:3219
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:149
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Hurd.cpp:128
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:132
std::string getDynamicLinker(const llvm::opt::ArgList &Args) const override
Definition: Hurd.cpp:136
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Hurd.cpp:203
void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const override
Definition: Hurd.cpp:218
Hurd(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Hurd.cpp:72
Tool * buildLinker() const override
Definition: Hurd.cpp:130
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:341
The JSON file list parser is used to communicate input to InstallAPI.