This repository has no description
1diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp
2index 29133f9ee8..b8f18438c7 100644
3--- a/lib/Basic/Targets.cpp
4+++ b/lib/Basic/Targets.cpp
5@@ -683,7 +683,7 @@
6 case llvm::Triple::wasm32:
7 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
8 Triple.getVendor() != llvm::Triple::UnknownVendor ||
9- !Triple.isOSBinFormatWasm())
10+ (!Triple.isOSBinFormatWasm() && os != llvm::Triple::Linux))
11 return nullptr;
12 switch (os) {
13 case llvm::Triple::WASI:
14@@ -692,6 +692,9 @@
15 case llvm::Triple::Emscripten:
16 return std::make_unique<EmscriptenTargetInfo<WebAssembly32TargetInfo>>(
17 Triple, Opts);
18+ case llvm::Triple::Linux:
19+ return std::make_unique<LinuxTargetInfo<WebAssembly32TargetInfo>>(Triple,
20+ Opts);
21 case llvm::Triple::UnknownOS:
22 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly32TargetInfo>>(
23 Triple, Opts);
24@@ -701,7 +704,7 @@
25 case llvm::Triple::wasm64:
26 if (Triple.getSubArch() != llvm::Triple::NoSubArch ||
27 Triple.getVendor() != llvm::Triple::UnknownVendor ||
28- !Triple.isOSBinFormatWasm())
29+ (!Triple.isOSBinFormatWasm() && os != llvm::Triple::Linux))
30 return nullptr;
31 switch (os) {
32 case llvm::Triple::WASI:
33@@ -710,6 +713,9 @@
34 case llvm::Triple::Emscripten:
35 return std::make_unique<EmscriptenTargetInfo<WebAssembly64TargetInfo>>(
36 Triple, Opts);
37+ case llvm::Triple::Linux:
38+ return std::make_unique<LinuxTargetInfo<WebAssembly64TargetInfo>>(Triple,
39+ Opts);
40 case llvm::Triple::UnknownOS:
41 return std::make_unique<WebAssemblyOSTargetInfo<WebAssembly64TargetInfo>>(
42 Triple, Opts);
43diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
44index f9dc8ab24f..2a06ff148b 100644
45--- a/lib/Driver/Driver.cpp
46+++ b/lib/Driver/Driver.cpp
47@@ -6383,7 +6383,11 @@
48 break;
49 case llvm::Triple::Linux:
50 case llvm::Triple::ELFIAMCU:
51- if (Target.getArch() == llvm::Triple::hexagon)
52+ if ((Target.getArch() == llvm::Triple::wasm32 ||
53+ Target.getArch() == llvm::Triple::wasm64) &&
54+ Target.isOSBinFormatWasm())
55+ TC = std::make_unique<toolchains::WebAssembly>(*this, Target, Args);
56+ else if (Target.getArch() == llvm::Triple::hexagon)
57 TC = std::make_unique<toolchains::HexagonToolChain>(*this, Target,
58 Args);
59 else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) &&
60diff --git a/lib/Driver/ToolChains/WebAssembly.cpp b/lib/Driver/ToolChains/WebAssembly.cpp
61index 60bd97e0ee..1eb59d1d3b 100644
62--- a/lib/Driver/ToolChains/WebAssembly.cpp
63+++ b/lib/Driver/ToolChains/WebAssembly.cpp
64@@ -19,6 +19,7 @@
65 #include "llvm/Support/FileSystem.h"
66 #include "llvm/Support/Path.h"
67 #include "llvm/Support/VirtualFileSystem.h"
68+#include <utility>
69
70 using namespace clang::driver;
71 using namespace clang::driver::tools;
72@@ -98,38 +99,42 @@
73 const char *Crt1;
74 const char *Entry = nullptr;
75
76- // When -shared is specified, use the reactor exec model unless
77- // specified otherwise.
78- if (Args.hasArg(options::OPT_shared))
79- IsCommand = false;
80-
81- if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
82- StringRef CM = A->getValue();
83- if (CM == "command") {
84- IsCommand = true;
85- } else if (CM == "reactor") {
86+ if (ToolChain.getTriple().isOSLinux()) {
87+ Crt1 = "crt1.o";
88+ } else {
89+ // When -shared is specified, use the reactor exec model unless
90+ // specified otherwise.
91+ if (Args.hasArg(options::OPT_shared))
92 IsCommand = false;
93+
94+ if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
95+ StringRef CM = A->getValue();
96+ if (CM == "command") {
97+ IsCommand = true;
98+ } else if (CM == "reactor") {
99+ IsCommand = false;
100+ } else {
101+ ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
102+ << CM << A->getOption().getName();
103+ }
104+ }
105+
106+ if (IsCommand) {
107+ // If crt1-command.o exists, it supports new-style commands, so use it.
108+ // Otherwise, use the old crt1.o. This is a temporary transition measure.
109+ // Once WASI libc no longer needs to support LLVM versions which lack
110+ // support for new-style command, it can make crt1.o the same as
111+ // crt1-command.o. And once LLVM no longer needs to support WASI libc
112+ // versions before that, it can switch to using crt1-command.o.
113+ Crt1 = "crt1.o";
114+ if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
115+ Crt1 = "crt1-command.o";
116 } else {
117- ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
118- << CM << A->getOption().getName();
119+ Crt1 = "crt1-reactor.o";
120+ Entry = "_initialize";
121 }
122 }
123
124- if (IsCommand) {
125- // If crt1-command.o exists, it supports new-style commands, so use it.
126- // Otherwise, use the old crt1.o. This is a temporary transition measure.
127- // Once WASI libc no longer needs to support LLVM versions which lack
128- // support for new-style command, it can make crt1.o the same as
129- // crt1-command.o. And once LLVM no longer needs to support WASI libc
130- // versions before that, it can switch to using crt1-command.o.
131- Crt1 = "crt1.o";
132- if (ToolChain.GetFilePath("crt1-command.o") != "crt1-command.o")
133- Crt1 = "crt1-command.o";
134- } else {
135- Crt1 = "crt1-reactor.o";
136- Entry = "_initialize";
137- }
138-
139 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
140 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
141 if (Entry) {
142@@ -235,8 +240,17 @@
143 // bitcode format is not stable.
144 auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
145 getFilePaths().push_back(Dir);
146+ if (getTriple().isOSLinux()) {
147+ auto UsrDir = AppendLTOLibDir(SysRoot + "/usr/lib/" + MultiarchTriple);
148+ getFilePaths().push_back(UsrDir);
149+ }
150 }
151 getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
152+ if (getTriple().isOSLinux()) {
153+ getFilePaths().push_back(SysRoot + "/usr/lib/" + MultiarchTriple);
154+ getFilePaths().push_back(SysRoot + "/usr/lib");
155+ }
156+ getFilePaths().push_back(SysRoot + "/lib");
157 }
158 }
159
160@@ -464,6 +478,8 @@
161 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
162 << A->getAsString(Args);
163 }
164+ if (getTriple().isOSLinux())
165+ return ToolChain::CST_Libstdcxx;
166 return ToolChain::CST_Libcxx;
167 }
168
169@@ -474,10 +490,15 @@
170
171 const Driver &D = getDriver();
172
173- if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
174- SmallString<128> P(D.ResourceDir);
175- llvm::sys::path::append(P, "include");
176- addSystemInclude(DriverArgs, CC1Args, P);
177+ const bool UseBuiltinIncludes =
178+ !DriverArgs.hasArg(options::OPT_nobuiltininc);
179+
180+ SmallString<128> ResourceDirInclude(D.ResourceDir);
181+ llvm::sys::path::append(ResourceDirInclude, "include");
182+
183+ if (UseBuiltinIncludes &&
184+ (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc))) {
185+ addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
186 }
187
188 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
189@@ -496,12 +517,24 @@
190 return;
191 }
192
193- if (getTriple().getOS() != llvm::Triple::UnknownOS) {
194- const std::string MultiarchTriple =
195- getMultiarchTriple(D, getTriple(), D.SysRoot);
196- addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
197- }
198+ const bool IsKnownOs = getTriple().getOS() != llvm::Triple::UnknownOS;
199+ std::string MultiarchTriple;
200+ if (IsKnownOs)
201+ MultiarchTriple = getMultiarchTriple(D, getTriple(), D.SysRoot);
202+
203+ if (IsKnownOs)
204+ addSystemInclude(DriverArgs, CC1Args,
205+ D.SysRoot + "/include/" + MultiarchTriple);
206+ if (getTriple().isOSLinux() && IsKnownOs)
207+ addSystemInclude(DriverArgs, CC1Args,
208+ D.SysRoot + "/usr/include/" + MultiarchTriple);
209+
210 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
211+ if (getTriple().isOSLinux())
212+ addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
213+
214+ if (UseBuiltinIncludes && getTriple().isMusl())
215+ addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
216 }
217
218 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
219@@ -557,12 +590,23 @@
220 llvm::opt::ArgStringList &CC1Args) const {
221 const Driver &D = getDriver();
222 std::string SysRoot = computeSysRoot();
223- std::string LibPath = SysRoot + "/include";
224+ auto MakeIncludeRoot = [&]() -> std::pair<std::string, std::string> {
225+ std::string Primary = SysRoot + "/include";
226+ if (getTriple().isOSLinux())
227+ return {SysRoot + "/usr/include", Primary};
228+ return {Primary, Primary};
229+ };
230+ auto [PrimaryInclude, SecondaryInclude] = MakeIncludeRoot();
231+ std::string LibPath = PrimaryInclude;
232 const std::string MultiarchTriple =
233 getMultiarchTriple(D, getTriple(), SysRoot);
234 bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
235
236 std::string Version = detectLibcxxVersion(LibPath);
237+ if (Version.empty() && PrimaryInclude != SecondaryInclude) {
238+ LibPath = SecondaryInclude;
239+ Version = detectLibcxxVersion(LibPath);
240+ }
241 if (Version.empty())
242 return;
243
244@@ -574,6 +618,11 @@
245
246 // Second add the generic one.
247 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
248+ if (PrimaryInclude != SecondaryInclude && LibPath != PrimaryInclude) {
249+ addSystemInclude(DriverArgs, CC1Args,
250+ PrimaryInclude + "/" + MultiarchTriple + "/c++/" + Version);
251+ addSystemInclude(DriverArgs, CC1Args, PrimaryInclude + "/c++/" + Version);
252+ }
253 }
254
255 void WebAssembly::addLibStdCXXIncludePaths(
256@@ -585,7 +634,14 @@
257 // to how we do it for libc++.
258 const Driver &D = getDriver();
259 std::string SysRoot = computeSysRoot();
260- std::string LibPath = SysRoot + "/include";
261+ auto MakeIncludeRoot = [&]() -> std::pair<std::string, std::string> {
262+ std::string Primary = SysRoot + "/include";
263+ if (getTriple().isOSLinux())
264+ return {SysRoot + "/usr/include", Primary};
265+ return {Primary, Primary};
266+ };
267+ auto [PrimaryInclude, SecondaryInclude] = MakeIncludeRoot();
268+ std::string LibPath = PrimaryInclude;
269 const std::string MultiarchTriple =
270 getMultiarchTriple(D, getTriple(), SysRoot);
271 bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
272@@ -611,6 +667,29 @@
273 Version = MaxVersion.Text;
274 }
275
276+ if (Version.empty() && PrimaryInclude != SecondaryInclude) {
277+ LibPath = SecondaryInclude;
278+ // repeat the search on the fallback location
279+ {
280+ std::error_code EC;
281+ Generic_GCC::GCCVersion MaxVersion =
282+ Generic_GCC::GCCVersion::Parse("0.0.0");
283+ SmallString<128> Path(LibPath);
284+ llvm::sys::path::append(Path, "c++");
285+ for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE;
286+ !EC && LI != LE; LI = LI.increment(EC)) {
287+ StringRef VersionText = llvm::sys::path::filename(LI->path());
288+ if (VersionText[0] != 'v') {
289+ auto ParsedVersion = Generic_GCC::GCCVersion::Parse(VersionText);
290+ if (ParsedVersion > MaxVersion)
291+ MaxVersion = ParsedVersion;
292+ }
293+ }
294+ if (MaxVersion.Major > 0)
295+ Version = MaxVersion.Text;
296+ }
297+ }
298+
299 if (Version.empty())
300 return;
301
302@@ -624,4 +703,15 @@
303 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
304 // Third the backward one.
305 addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
306+
307+ if (PrimaryInclude != SecondaryInclude && LibPath != PrimaryInclude) {
308+ if (IsKnownOs) {
309+ addSystemInclude(DriverArgs, CC1Args,
310+ PrimaryInclude + "/c++/" + Version + "/" + MultiarchTriple);
311+ }
312+ addSystemInclude(DriverArgs, CC1Args,
313+ PrimaryInclude + "/c++/" + Version);
314+ addSystemInclude(DriverArgs, CC1Args,
315+ PrimaryInclude + "/c++/" + Version + "/backward");
316+ }
317 }