1 | |
2 | |
3 | |
4 | |
5 | |
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 | |
22 | using tools::addPathIfExists; |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | static std::string getMultiarchTriple(const Driver &D, |
31 | const llvm::Triple &TargetTriple, |
32 | StringRef SysRoot) { |
33 | if (TargetTriple.getArch() == llvm::Triple::x86) { |
34 | |
35 | |
36 | |
37 | |
38 | if (D.getVFS().exists(SysRoot + "/lib/i386-gnu")) |
39 | return "i386-gnu"; |
40 | } |
41 | |
42 | |
43 | |
44 | return TargetTriple.str(); |
45 | } |
46 | |
47 | static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
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 | |
74 | |
75 | |
76 | |
77 | if (StringRef(D.Dir).startswith(SysRoot)) { |
78 | addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); |
79 | addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); |
80 | } |
81 | |
82 | addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); |
83 | addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); |
84 | |
85 | addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); |
86 | addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths); |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | if (StringRef(D.Dir).startswith(SysRoot)) |
93 | addPathIfExists(D, D.Dir + "/../lib", Paths); |
94 | |
95 | addPathIfExists(D, SysRoot + "/lib", Paths); |
96 | addPathIfExists(D, SysRoot + "/usr/lib", Paths); |
97 | } |
98 | |
99 | bool Hurd::HasNativeLLVMSupport() const { return true; } |
100 | |
101 | Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); } |
102 | |
103 | Tool *Hurd::buildAssembler() const { |
104 | return new tools::gnutools::Assembler(*this); |
105 | } |
106 | |
107 | std::string Hurd::computeSysRoot() const { |
108 | if (!getDriver().SysRoot.empty()) |
109 | return getDriver().SysRoot; |
110 | |
111 | return std::string(); |
112 | } |
113 | |
114 | std::string Hurd::getDynamicLinker(const ArgList &Args) const { |
115 | if (getArch() == llvm::Triple::x86) |
116 | return "/lib/ld.so"; |
117 | |
118 | llvm_unreachable("unsupported architecture"); |
119 | } |
120 | |
121 | void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
122 | ArgStringList &CC1Args) const { |
123 | const Driver &D = getDriver(); |
124 | std::string SysRoot = computeSysRoot(); |
125 | |
126 | if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) |
127 | return; |
128 | |
129 | if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) |
130 | addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); |
131 | |
132 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
133 | SmallString<128> P(D.ResourceDir); |
134 | llvm::sys::path::append(P, "include"); |
135 | addSystemInclude(DriverArgs, CC1Args, P); |
136 | } |
137 | |
138 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
139 | return; |
140 | |
141 | |
142 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
143 | if (CIncludeDirs != "") { |
144 | SmallVector<StringRef, 5> Dirs; |
145 | CIncludeDirs.split(Dirs, ":"); |
146 | for (StringRef Dir : Dirs) { |
147 | StringRef Prefix = |
148 | llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : ""; |
149 | addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir); |
150 | } |
151 | return; |
152 | } |
153 | |
154 | |
155 | |
156 | if (getTriple().getArch() == llvm::Triple::x86) { |
157 | std::string Path = SysRoot + "/usr/include/i386-gnu"; |
158 | if (D.getVFS().exists(Path)) |
159 | addExternCSystemInclude(DriverArgs, CC1Args, Path); |
160 | } |
161 | |
162 | |
163 | |
164 | |
165 | addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); |
166 | |
167 | addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); |
168 | } |
169 | |