clang  14.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"
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 
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  if (TargetTriple.getArch() == llvm::Triple::x86) {
34  // We use the existence of '/lib/<triple>' as a directory to detect some
35  // common hurd triples that don't quite match the Clang triple for both
36  // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
37  // regardless of what the actual target triple is.
38  if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
39  return "i386-gnu";
40  }
41 
42  // For most architectures, just use whatever we have rather than trying to be
43  // clever.
44  return TargetTriple.str();
45 }
46 
47 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
48  // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
49  // using that variant while targeting other architectures causes problems
50  // because the libraries are laid out in shared system roots that can't cope
51  // with a 'lib32' library search path being considered. So we only enable
52  // them when we know we may need it.
53  //
54  // FIXME: This is a bit of a hack. We should really unify this code for
55  // reasoning about oslibdir spellings with the lib dir spellings in the
56  // GCCInstallationDetector, but that is a more significant refactoring.
57 
58  if (Triple.getArch() == llvm::Triple::x86)
59  return "lib32";
60 
61  return Triple.isArch32Bit() ? "lib" : "lib64";
62 }
63 
64 Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
65  : Generic_ELF(D, Triple, Args) {
66  GCCInstallation.init(Triple, Args);
69  std::string SysRoot = computeSysRoot();
71 
73 
74  // The selection of paths to try here is designed to match the patterns which
75  // the GCC driver itself uses, as this is part of the GCC-compatible driver.
76  // This was determined by running GCC in a fake filesystem, creating all
77  // possible permutations of these directories, and seeing which ones it added
78  // to the link paths.
79  path_list &Paths = getFilePaths();
80 
81  const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
82  const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
83 
84 #ifdef ENABLE_LINKER_BUILD_ID
85  ExtraOpts.push_back("--build-id");
86 #endif
87 
88  Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
89 
90  // Similar to the logic for GCC above, if we currently running Clang inside
91  // of the requested system root, add its parent library paths to
92  // those searched.
93  // FIXME: It's not clear whether we should use the driver's installed
94  // directory ('Dir' below) or the ResourceDir.
95  if (StringRef(D.Dir).startswith(SysRoot)) {
96  addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
97  addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
98  }
99 
100  addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
101  addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
102 
103  addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
104  addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
105 
106  Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
107 
108  // Similar to the logic for GCC above, if we are currently running Clang
109  // inside of the requested system root, add its parent library path to those
110  // searched.
111  // FIXME: It's not clear whether we should use the driver's installed
112  // directory ('Dir' below) or the ResourceDir.
113  if (StringRef(D.Dir).startswith(SysRoot))
114  addPathIfExists(D, D.Dir + "/../lib", Paths);
115 
116  addPathIfExists(D, SysRoot + "/lib", Paths);
117  addPathIfExists(D, SysRoot + "/usr/lib", Paths);
118 }
119 
120 bool Hurd::HasNativeLLVMSupport() const { return true; }
121 
122 Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
123 
125  return new tools::gnutools::Assembler(*this);
126 }
127 
128 std::string Hurd::getDynamicLinker(const ArgList &Args) const {
129  if (getArch() == llvm::Triple::x86)
130  return "/lib/ld.so";
131 
132  llvm_unreachable("unsupported architecture");
133 }
134 
135 void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
136  ArgStringList &CC1Args) const {
137  const Driver &D = getDriver();
138  std::string SysRoot = computeSysRoot();
139 
140  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
141  return;
142 
143  if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
144  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
145 
146  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
148  llvm::sys::path::append(P, "include");
149  addSystemInclude(DriverArgs, CC1Args, P);
150  }
151 
152  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
153  return;
154 
155  // Check for configure-time C include directories.
156  StringRef CIncludeDirs(C_INCLUDE_DIRS);
157  if (CIncludeDirs != "") {
159  CIncludeDirs.split(Dirs, ":");
160  for (StringRef Dir : Dirs) {
161  StringRef Prefix =
162  llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
163  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
164  }
165  return;
166  }
167 
168  // Lacking those, try to detect the correct set of system includes for the
169  // target triple.
170 
171  AddMultilibIncludeArgs(DriverArgs, CC1Args);
172 
173  // On systems using multiarch, add /usr/include/$triple before
174  // /usr/include.
175  std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
176  if (!MultiarchIncludeDir.empty() &&
177  D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
178  addExternCSystemInclude(DriverArgs, CC1Args,
179  SysRoot + "/usr/include/" + MultiarchIncludeDir);
180 
181  // Add an include of '/include' directly. This isn't provided by default by
182  // system GCCs, but is often used with cross-compiling GCCs, and harmless to
183  // add even when Clang is acting as-if it were a system compiler.
184  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
185 
186  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
187 }
188 
189 void Hurd::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
190  llvm::opt::ArgStringList &CC1Args) const {
191  // We need a detected GCC installation on Linux to provide libstdc++'s
192  // headers in odd Linuxish places.
193  if (!GCCInstallation.isValid())
194  return;
195 
196  StringRef TripleStr = GCCInstallation.getTriple().str();
197  StringRef DebianMultiarch =
198  GCCInstallation.getTriple().getArch() == llvm::Triple::x86 ? "i386-gnu"
199  : TripleStr;
200 
201  addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, DebianMultiarch);
202 }
203 
204 void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
205  for (const auto &Opt : ExtraOpts)
206  CmdArgs.push_back(Opt.c_str());
207 }
clang::driver::toolchains
Definition: AIX.h:55
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:262
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::Driver::Dir
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:123
llvm::SmallVector< std::string, 16 >
clang::driver::toolchains::Generic_GCC::GCCInstallation
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
clang::driver::toolchains::Generic_GCC::AddMultiarchPaths
void AddMultiarchPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, path_list &Paths)
Definition: Gnu.cpp:2844
clang::driver::toolchains::Hurd::AddClangSystemIncludeArgs
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:135
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:221
clang::driver::toolchains::Hurd::addLibStdCxxIncludePaths
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Hurd.cpp:189
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::isValid
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
clang::driver::toolchains::Generic_GCC::AddMultilibPaths
void AddMultilibPaths(const Driver &D, const std::string &SysRoot, const std::string &OSLibDir, const std::string &MultiarchTriple, path_list &Paths)
Definition: Gnu.cpp:2785
Hurd.h
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::init
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, ArrayRef< std::string > ExtraTripleAliases=None)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:1891
clang::driver::toolchains::Generic_GCC::PushPPaths
void PushPPaths(ToolChain::path_list &PPaths)
Definition: Gnu.cpp:2770
clang::driver::toolchains::Generic_GCC::AddMultilibIncludeArgs
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:2859
clang::driver::toolchains::Hurd::buildLinker
Tool * buildLinker() const override
Definition: Hurd.cpp:122
clang::driver::ToolChain::getFilePaths
path_list & getFilePaths()
Definition: ToolChain.h:259
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::toolchains::Generic_ELF
Definition: Gnu.h:362
Options.h
getOSLibDir
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args)
Definition: Hurd.cpp:47
clang::driver::tools::gnutools::Linker
Definition: Gnu.h:53
llvm::opt
Definition: DiagnosticOptions.h:19
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getTriple
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:227
clang::driver::tools::addPathIfExists
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:122
llvm::SmallString< 128 >
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:223
clang::driver::ToolChain::SelectedMultilib
Multilib SelectedMultilib
Definition: ToolChain.h:182
clang::driver::Driver::ResourceDir
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:135
clang::driver::toolchains::Generic_GCC::addGCCLibStdCxxIncludePaths
bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, StringRef DebianMultiarch) const
Definition: Gnu.cpp:2965
clang::driver::ToolChain::getArch
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:237
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::driver::toolchains::Hurd::HasNativeLLVMSupport
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Hurd.cpp:120
clang::driver::ToolChain::computeSysRoot
virtual std::string computeSysRoot() const
Return the sysroot, possibly searching for a default sysroot using target-specific logic.
Definition: ToolChain.cpp:734
clang::driver::tools::gnutools::Assembler
Definition: Gnu.h:41
clang::driver::toolchains::Hurd::getMultiarchTriple
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
clang::driver::toolchains::Hurd::buildAssembler
Tool * buildAssembler() const override
Definition: Hurd.cpp:124
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
clang::driver::toolchains::Hurd::getDynamicLinker
std::string getDynamicLinker(const llvm::opt::ArgList &Args) const override
Definition: Hurd.cpp:128
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getMultilib
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:236
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getMultilibs
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:239
clang::driver::ToolChain::Multilibs
MultilibSet Multilibs
Definition: ToolChain.h:181
clang::driver::Driver::getVFS
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:329
clang::driver::toolchains::Hurd::addExtraOpts
void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const override
Definition: Hurd.cpp:204
clang::driver::ToolChain::addExternCSystemInclude
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:860
clang::driver
Definition: Action.h:31
clang::driver::ToolChain::addSystemInclude
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:845
clang::driver::toolchains::Hurd::ExtraOpts
std::vector< std::string > ExtraOpts
Definition: Hurd.h:37
clang::driver::toolchains::Hurd::Hurd
Hurd(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Hurd.cpp:64