wasm-sysroot-usr
authorLLVM Packaging Team <pkg-llvm-team@lists.alioth.debian.org>
Thu, 11 Jan 2024 13:15:09 +0000 (14:15 +0100)
committerSylvestre Ledru <sylvestre@debian.org>
Thu, 11 Jan 2024 13:15:09 +0000 (14:15 +0100)
===================================================================

Gbp-Pq: Topic wasm
Gbp-Pq: Name wasm-sysroot-usr.diff

clang/lib/Driver/ToolChains/WebAssembly.cpp
clang/lib/Driver/ToolChains/WebAssembly.h

index 4328d5e5627e0907917671e1b2c8318acfcb2484..08c52bf6a200858ebef19a80d25cb445e2549132 100644 (file)
@@ -189,7 +189,7 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
 
   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
@@ -415,6 +415,7 @@ void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
     return;
 
   const Driver &D = getDriver();
+  std::string SysRoot = computeSysRoot();
 
   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
     SmallString<128> P(D.ResourceDir);
@@ -438,12 +439,20 @@ void WebAssembly::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
     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,
@@ -494,6 +503,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 {
@@ -504,18 +524,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(
@@ -562,8 +586,11 @@ void WebAssembly::addLibStdCXXIncludePaths(
     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 479889be20f0e87b3f4287004be3d46aa71d356d..eca08c22e02081b30375011dddd8affe6bbbc669 100644 (file)
@@ -88,6 +88,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