llvm-toolchain/debian/patches/wasm/wasm-sysroot-usr.diff
Faidon Liambotis b91115bd58 Fix wasm include paths, unbreaking compiler-rt builds on !x86
compiler-rt WebAssembly builds were failing, and as a workaround commit
ca9dbba introduced COMPILER_RT_WASM_ENABLE, disabling its compilation on
several architectures (arm64 armel mips64el mipsel ppc64el armhf).

This was ultimately caused by the WebAssembly driver including paths in
bare /usr/include, when compiling for wasnNN-unknown-unknown targets.
This in turn resulted in this chain of include paths when building
compiler-rt, as one example out of many:
  1. compiler-rt/lib/builtins/divtf3.c:15                  #include "fp_lib.h"
  2. compiler-rt/lib/builtins/fp_lib.h:23                  #include "int_lib.h"
  3. compiler-rt/lib/builtins/int_lib.h:93                 #include <limits.h>
  4. /usr/lib/llvm-14/lib/clang/14.0.6/include/limits.h:20 #if __has_include_next(<limits.h>) #include_next <limits.h>
  5. /usr/include/limits.h:26                              #include <bits/libc-header-start.h>
  6. fatal error: 'bits/libc-header-start.h' file not found

/usr/include/stdint.h is the host's glibc header, and
bits/libc-header-start.h doesn't exist for the wasm targets. This is the
case with or without wasi-libc, as wasi-libc is musl-based and doesn't
have these paths at all.

The only reason builds worked on amd64 is accident:lly the B-D chain
brings in libc6-dev-i386 (through g++-multilib), which creates this
symlink:
  /usr/include/bits -> x86_64-linux-gnu/bits
This effectively meant that on amd64 builds, compiler-rt for wasm
targets was compiled with glibc x86_64 headers.

Ultimately this was rooted on the sysroot-based assumptions that the
upstream driver makes and that we are patching (evidently incompletely)
with our wasm-sysroot-usr.diff patch.

Update our patch to explicitly NOT include the bare /usr/include path on
non-OS targets (wasm32-unknown-unknown etc.), while keeping existing
behavior for backwards compatibility when --sysroot is passed.

Given this should (fingers crossed) address this invariance between
amd64 and other architectures, and unbreak those builds, revert commit
ca9dbba and the COMPILER_RT_WASM_ENABLE flag.
2022-11-17 21:00:23 +02:00

83 lines
3.3 KiB
Diff

Index: llvm-toolchain-14-14.0.6/clang/lib/Driver/ToolChains/WebAssembly.cpp
===================================================================
--- llvm-toolchain-14-14.0.6.orig/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ llvm-toolchain-14-14.0.6/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -16,6 +16,7 @@
#include "clang/Driver/Options.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Option/ArgList.h"
using namespace clang::driver;
@@ -172,7 +173,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
@@ -396,6 +397,7 @@ void WebAssembly::AddClangSystemIncludeA
return;
const Driver &D = getDriver();
+ std::string SysRoot = computeSysRoot();
if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
SmallString<128> P(D.ResourceDir);
@@ -419,12 +421,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,
@@ -467,3 +477,15 @@ SanitizerMask WebAssembly::getSupportedS
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();
+}
+
Index: llvm-toolchain-14-14.0.6/clang/lib/Driver/ToolChains/WebAssembly.h
===================================================================
--- llvm-toolchain-14-14.0.6.orig/clang/lib/Driver/ToolChains/WebAssembly.h
+++ llvm-toolchain-14-14.0.6/clang/lib/Driver/ToolChains/WebAssembly.h
@@ -80,6 +80,8 @@ private:
std::string getMultiarchTriple(const Driver &D,
const llvm::Triple &TargetTriple,
StringRef SysRoot) const override;
+
+ std::string computeSysRoot() const override;
};
} // end namespace toolchains