File: wasm-sysroot-usr.diff

package info (click to toggle)
llvm-toolchain-18 1%3A18.1.8-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,908,340 kB
  • sloc: cpp: 6,667,937; ansic: 1,440,452; asm: 883,619; python: 230,549; objc: 76,880; f90: 74,238; lisp: 35,989; pascal: 16,571; sh: 10,229; perl: 7,459; ml: 5,047; awk: 3,523; makefile: 2,987; javascript: 2,149; xml: 892; fortran: 649; cs: 573
file content (122 lines) | stat: -rw-r--r-- 5,102 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Index: llvm-toolchain-18_18.1.2/clang/lib/Driver/ToolChains/WebAssembly.cpp
===================================================================
--- llvm-toolchain-18_18.1.2.orig/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ llvm-toolchain-18_18.1.2/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -222,7 +222,7 @@ WebAssembly::WebAssembly(const Driver &D
 
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
-  auto SysRoot = getDriver().SysRoot;
+  std::string SysRoot = computeSysRoot();
   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
     // Theoretically an "unknown" OS should mean no standard libraries, however
     // it could also mean that a custom set of libraries is in use, so just add
@@ -454,6 +454,7 @@ void WebAssembly::AddClangSystemIncludeA
     return;
 
   const Driver &D = getDriver();
+  std::string SysRoot = computeSysRoot();
 
   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
     SmallString<128> P(D.ResourceDir);
@@ -477,12 +478,20 @@ void WebAssembly::AddClangSystemIncludeA
     return;
   }
 
+  // add the multiarch path on e.g. wasm32-wasi
   if (getTriple().getOS() != llvm::Triple::UnknownOS) {
     const std::string MultiarchTriple =
-        getMultiarchTriple(D, getTriple(), D.SysRoot);
-    addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include/" + MultiarchTriple);
+        getMultiarchTriple(D, getTriple(), SysRoot);
+    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/local/include/" + MultiarchTriple);
+    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/local/include");
+    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/include/" + MultiarchTriple);
   }
-  addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
+
+  // also add the non-multiarch path, only on a known OS (as above), or when
+  // a sysroot is given, for backwards compatibility with the original driver
+  if (getTriple().getOS() != llvm::Triple::UnknownOS ||
+      !getDriver().SysRoot.empty())
+    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
 }
 
 void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
@@ -533,6 +542,17 @@ Tool *WebAssembly::buildLinker() const {
   return new tools::wasm::Linker(*this);
 }
 
+std::string WebAssembly::computeSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+    return getDriver().SysRoot;
+
+  std::string Path = "/usr";
+  if (getVFS().exists(Path))
+    return Path;
+
+  return std::string();
+}
+
 void WebAssembly::addLibCxxIncludePaths(
     const llvm::opt::ArgList &DriverArgs,
     llvm::opt::ArgStringList &CC1Args) const {
@@ -543,18 +563,22 @@ void WebAssembly::addLibCxxIncludePaths(
       getMultiarchTriple(D, getTriple(), SysRoot);
   bool IsKnownOs = (getTriple().getOS() != llvm::Triple::UnknownOS);
 
-  std::string Version = detectLibcxxVersion(LibPath);
-  if (Version.empty())
-    return;
-
   // First add the per-target include path if the OS is known.
   if (IsKnownOs) {
-    std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
-    addSystemInclude(DriverArgs, CC1Args, TargetDir);
+    std::string Version = detectLibcxxVersion(LibPath + "/" + MultiarchTriple);
+    if (!Version.empty()) {
+      std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
+      addSystemInclude(DriverArgs, CC1Args, TargetDir);
+    }
   }
 
   // Second add the generic one.
-  addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
+  // don't include the host architecture's headers in the search path
+  if (!getDriver().SysRoot.empty()) {
+    std::string Version = detectLibcxxVersion(LibPath);
+    if (!Version.empty())
+      addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
+  }
 }
 
 void WebAssembly::addLibStdCXXIncludePaths(
@@ -601,8 +625,11 @@ void WebAssembly::addLibStdCXXIncludePat
     addSystemInclude(DriverArgs, CC1Args, TargetDir);
   }
 
-  // Second add the generic one.
-  addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
-  // Third the backward one.
-  addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
+  // don't include the host architecture's headers in the search path
+  if (!getDriver().SysRoot.empty()) {
+    // Second add the generic one.
+    addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
+    // Third the backward one.
+    addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version + "/backward");
+  }
 }
Index: llvm-toolchain-18_18.1.2/clang/lib/Driver/ToolChains/WebAssembly.h
===================================================================
--- llvm-toolchain-18_18.1.2.orig/clang/lib/Driver/ToolChains/WebAssembly.h
+++ llvm-toolchain-18_18.1.2/clang/lib/Driver/ToolChains/WebAssembly.h
@@ -84,6 +84,8 @@ private:
                              llvm::opt::ArgStringList &CC1Args) const;
   void addLibStdCXXIncludePaths(const llvm::opt::ArgList &DriverArgs,
                                 llvm::opt::ArgStringList &CC1Args) const;
+
+  std::string computeSysRoot() const override;
 };
 
 } // end namespace toolchains