]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/ToolChains/Hurd.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Driver / ToolChains / Hurd.cpp
1 //===--- Hurd.cpp - Hurd ToolChain Implementations --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Hurd.h"
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"
17
18 using namespace clang::driver;
19 using namespace clang::driver::toolchains;
20 using namespace clang;
21 using namespace llvm::opt;
22
23 using tools::addPathIfExists;
24
25 /// Get our best guess at the multiarch triple for a target.
26 ///
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,
33                                       StringRef SysRoot) {
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"))
40       return "i386-gnu";
41   }
42
43   // For most architectures, just use whatever we have rather than trying to be
44   // clever.
45   return TargetTriple.str();
46 }
47
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.
54   //
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.
58
59   if (Triple.getArch() == llvm::Triple::x86)
60     return "lib32";
61
62   return Triple.isArch32Bit() ? "lib" : "lib64";
63 }
64
65 Hurd::Hurd(const Driver &D, const llvm::Triple &Triple,
66            const ArgList &Args)
67     : Generic_ELF(D, Triple, Args) {
68   std::string SysRoot = computeSysRoot();
69   path_list &Paths = getFilePaths();
70
71   const std::string OSLibDir = getOSLibDir(Triple, Args);
72   const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
73
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);
81   }
82
83   addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
84   addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
85
86   addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
87   addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
88
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);
95
96   addPathIfExists(D, SysRoot + "/lib", Paths);
97   addPathIfExists(D, SysRoot + "/usr/lib", Paths);
98 }
99
100 bool Hurd::HasNativeLLVMSupport() const { return true; }
101
102 Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
103
104 Tool *Hurd::buildAssembler() const {
105   return new tools::gnutools::Assembler(*this);
106 }
107
108 std::string Hurd::computeSysRoot() const {
109   if (!getDriver().SysRoot.empty())
110     return getDriver().SysRoot;
111
112   return std::string();
113 }
114
115 std::string Hurd::getDynamicLinker(const ArgList &Args) const {
116   if (getArch() == llvm::Triple::x86)
117     return "/lib/ld.so";
118
119   llvm_unreachable("unsupported architecture");
120 }
121
122 void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
123                                      ArgStringList &CC1Args) const {
124   const Driver &D = getDriver();
125   std::string SysRoot = computeSysRoot();
126
127   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
128     return;
129
130   if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
131     addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
132
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);
137   }
138
139   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
140     return;
141
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) {
148       StringRef Prefix =
149           llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : "";
150       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
151     }
152     return;
153   }
154
155   // Lacking those, try to detect the correct set of system includes for the
156   // target triple.
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);
161   }
162
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");
167
168   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
169 }