clang  10.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.
30 std::string Hurd::getMultiarchTriple(const Driver &D,
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,
65  const ArgList &Args)
66  : Generic_ELF(D, Triple, Args) {
67  std::string SysRoot = computeSysRoot();
68  path_list &Paths = getFilePaths();
69 
70  const std::string OSLibDir = getOSLibDir(Triple, Args);
71  const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
72 
73 #ifdef ENABLE_LINKER_BUILD_ID
74  ExtraOpts.push_back("--build-id");
75 #endif
76 
77  // If we are currently running Clang inside of the requested system root, add
78  // its parent library paths to those searched.
79  // FIXME: It's not clear whether we should use the driver's installed
80  // directory ('Dir' below) or the ResourceDir.
81  if (StringRef(D.Dir).startswith(SysRoot)) {
82  addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
83  addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
84  }
85 
86  addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
87  addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
88 
89  addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
90  addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
91 
92  // If we are currently running Clang inside of the requested system root, add
93  // its parent library path to those searched.
94  // FIXME: It's not clear whether we should use the driver's installed
95  // directory ('Dir' below) or the ResourceDir.
96  if (StringRef(D.Dir).startswith(SysRoot))
97  addPathIfExists(D, D.Dir + "/../lib", Paths);
98 
99  addPathIfExists(D, SysRoot + "/lib", Paths);
100  addPathIfExists(D, SysRoot + "/usr/lib", Paths);
101 }
102 
103 bool Hurd::HasNativeLLVMSupport() const { return true; }
104 
105 Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
106 
108  return new tools::gnutools::Assembler(*this);
109 }
110 
111 std::string Hurd::computeSysRoot() const {
112  if (!getDriver().SysRoot.empty())
113  return getDriver().SysRoot;
114 
115  return std::string();
116 }
117 
118 std::string Hurd::getDynamicLinker(const ArgList &Args) const {
119  if (getArch() == llvm::Triple::x86)
120  return "/lib/ld.so";
121 
122  llvm_unreachable("unsupported architecture");
123 }
124 
125 void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
126  ArgStringList &CC1Args) const {
127  const Driver &D = getDriver();
128  std::string SysRoot = computeSysRoot();
129 
130  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
131  return;
132 
133  if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
134  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
135 
136  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
138  llvm::sys::path::append(P, "include");
139  addSystemInclude(DriverArgs, CC1Args, P);
140  }
141 
142  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
143  return;
144 
145  // Check for configure-time C include directories.
146  StringRef CIncludeDirs(C_INCLUDE_DIRS);
147  if (CIncludeDirs != "") {
149  CIncludeDirs.split(Dirs, ":");
150  for (StringRef Dir : Dirs) {
151  StringRef Prefix =
152  llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : "";
153  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
154  }
155  return;
156  }
157 
158  // Lacking those, try to detect the correct set of system includes for the
159  // target triple.
160  if (getTriple().getArch() == llvm::Triple::x86) {
161  std::string Path = SysRoot + "/usr/include/i386-gnu";
162  if (D.getVFS().exists(Path))
163  addExternCSystemInclude(DriverArgs, CC1Args, Path);
164  }
165 
166  // Add an include of '/include' directly. This isn't provided by default by
167  // system GCCs, but is often used with cross-compiling GCCs, and harmless to
168  // add even when Clang is acting as-if it were a system compiler.
169  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
170 
171  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
172 }
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:835
Tool * buildAssembler() const override
Definition: Hurd.cpp:107
std::vector< std::string > ExtraOpts
Definition: Hurd.h:34
StringRef P
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Hurd.cpp:103
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::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:120
virtual std::string computeSysRoot() const
Definition: Hurd.cpp:111
virtual std::string getDynamicLinker(const llvm::opt::ArgList &Args) const
Definition: Hurd.cpp:118
path_list & getFilePaths()
Definition: ToolChain.h:237
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:319
Hurd(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Hurd.cpp:64
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:215
Tool * buildLinker() const override
Definition: Hurd.cpp:105
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:125
const Driver & getDriver() const
Definition: ToolChain.h:199
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:820
Dataflow Directional Tag Classes.
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
Tool - Information on a specific compilation tool.
Definition: Tool.h:33
const llvm::Triple & getTriple() const
Definition: ToolChain.h:201
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:61
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args)
Definition: Hurd.cpp:47
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:132