1 //===--- Hurd.cpp - Hurd ToolChain Implementations --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "CommonArgs.h"
12 #include "clang/Config/config.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/VirtualFileSystem.h"
18 using namespace clang::driver;
19 using namespace clang::driver::toolchains;
20 using namespace clang;
21 using namespace llvm::opt;
23 using tools::addPathIfExists;
25 /// Get our best guess at the multiarch triple for a target.
27 /// Debian-based systems are starting to use a multiarch setup where they use
28 /// a target-triple directory in the library and header search paths.
29 /// Unfortunately, this triple does not align with the vanilla target triple,
30 /// so we provide a rough mapping here.
31 static std::string getMultiarchTriple(const Driver &D,
32 const llvm::Triple &TargetTriple,
34 if (TargetTriple.getArch() == llvm::Triple::x86) {
35 // We use the existence of '/lib/<triple>' as a directory to detect some
36 // common hurd triples that don't quite match the Clang triple for both
37 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
38 // regardless of what the actual target triple is.
39 if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
43 // For most architectures, just use whatever we have rather than trying to be
45 return TargetTriple.str();
48 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
49 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
50 // using that variant while targeting other architectures causes problems
51 // because the libraries are laid out in shared system roots that can't cope
52 // with a 'lib32' library search path being considered. So we only enable
53 // them when we know we may need it.
55 // FIXME: This is a bit of a hack. We should really unify this code for
56 // reasoning about oslibdir spellings with the lib dir spellings in the
57 // GCCInstallationDetector, but that is a more significant refactoring.
59 if (Triple.getArch() == llvm::Triple::x86)
62 return Triple.isArch32Bit() ? "lib" : "lib64";
65 Hurd::Hurd(const Driver &D, const llvm::Triple &Triple,
67 : Generic_ELF(D, Triple, Args) {
68 std::string SysRoot = computeSysRoot();
69 path_list &Paths = getFilePaths();
71 const std::string OSLibDir = getOSLibDir(Triple, Args);
72 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
74 // If we are currently running Clang inside of the requested system root, add
75 // its parent library paths to those searched.
76 // FIXME: It's not clear whether we should use the driver's installed
77 // directory ('Dir' below) or the ResourceDir.
78 if (StringRef(D.Dir).startswith(SysRoot)) {
79 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
80 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
83 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
84 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
86 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
87 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
89 // If we are currently running Clang inside of the requested system root, add
90 // its parent library path to those searched.
91 // FIXME: It's not clear whether we should use the driver's installed
92 // directory ('Dir' below) or the ResourceDir.
93 if (StringRef(D.Dir).startswith(SysRoot))
94 addPathIfExists(D, D.Dir + "/../lib", Paths);
96 addPathIfExists(D, SysRoot + "/lib", Paths);
97 addPathIfExists(D, SysRoot + "/usr/lib", Paths);
100 bool Hurd::HasNativeLLVMSupport() const { return true; }
102 Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
104 Tool *Hurd::buildAssembler() const {
105 return new tools::gnutools::Assembler(*this);
108 std::string Hurd::computeSysRoot() const {
109 if (!getDriver().SysRoot.empty())
110 return getDriver().SysRoot;
112 return std::string();
115 std::string Hurd::getDynamicLinker(const ArgList &Args) const {
116 if (getArch() == llvm::Triple::x86)
119 llvm_unreachable("unsupported architecture");
122 void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
123 ArgStringList &CC1Args) const {
124 const Driver &D = getDriver();
125 std::string SysRoot = computeSysRoot();
127 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
130 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
131 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
133 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
134 SmallString<128> P(D.ResourceDir);
135 llvm::sys::path::append(P, "include");
136 addSystemInclude(DriverArgs, CC1Args, P);
139 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
142 // Check for configure-time C include directories.
143 StringRef CIncludeDirs(C_INCLUDE_DIRS);
144 if (CIncludeDirs != "") {
145 SmallVector<StringRef, 5> Dirs;
146 CIncludeDirs.split(Dirs, ":");
147 for (StringRef Dir : Dirs) {
149 llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : "";
150 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
155 // Lacking those, try to detect the correct set of system includes for the
157 if (getTriple().getArch() == llvm::Triple::x86) {
158 std::string Path = SysRoot + "/usr/include/i386-gnu";
159 if (D.getVFS().exists(Path))
160 addExternCSystemInclude(DriverArgs, CC1Args, Path);
163 // Add an include of '/include' directly. This isn't provided by default by
164 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
165 // add even when Clang is acting as-if it were a system compiler.
166 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
168 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");