From b91115bd58c13f7c246cd0f2e9cec01ae439a3c7 Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Sun, 13 Nov 2022 08:45:30 +0200 Subject: [PATCH 1/7] 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 4. /usr/lib/llvm-14/lib/clang/14.0.6/include/limits.h:20 #if __has_include_next() #include_next 5. /usr/include/limits.h:26 #include 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. --- debian/patches/wasm/wasm-sysroot-usr.diff | 16 +++++++++++++--- debian/rules | 13 +------------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/debian/patches/wasm/wasm-sysroot-usr.diff b/debian/patches/wasm/wasm-sysroot-usr.diff index f24ec1fc..165de408 100644 --- a/debian/patches/wasm/wasm-sysroot-usr.diff +++ b/debian/patches/wasm/wasm-sysroot-usr.diff @@ -27,21 +27,31 @@ Index: llvm-toolchain-14-14.0.6/clang/lib/Driver/ToolChains/WebAssembly.cpp if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { SmallString<128> P(D.ResourceDir); -@@ -421,10 +423,10 @@ void WebAssembly::AddClangSystemIncludeA +@@ -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"); -+ addSystemInclude(DriverArgs, CC1Args, 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 +469,15 @@ SanitizerMask WebAssembly::getSupportedS +@@ -467,3 +477,15 @@ SanitizerMask WebAssembly::getSupportedS Tool *WebAssembly::buildLinker() const { return new tools::wasm::Linker(*this); } diff --git a/debian/rules b/debian/rules index f55a3caa..04ff3481 100755 --- a/debian/rules +++ b/debian/rules @@ -302,12 +302,6 @@ ifeq ($(LIBUNWIND_ENABLE),yes) endif endif -COMPILER_RT_WASM_ENABLE=yes - -ifneq (,$(filter $(DEB_HOST_ARCH), arm64 armel mips64el mipsel ppc64el armhf)) - COMPILER_RT_WASM_ENABLE=no -endif - # Do not install objects STAGE_ALL_CMAKE_EXTRA += -DMLIR_INSTALL_AGGREGATE_OBJECTS=OFF @@ -716,7 +710,6 @@ debian-libclc-build: touch $@ debian-rtlib-wasm-build: -ifeq (${COMPILER_RT_WASM_ENABLE},yes) echo "Using cmake: $(CMAKE_BIN)" for build in wasm32 wasm64; do \ mkdir -p build-compiler-rt-$$build; \ @@ -746,9 +739,6 @@ ifeq (${COMPILER_RT_WASM_ENABLE},yes) -DCOMPILER_RT_OS_DIR=wasi; \ ninja -C build-compiler-rt-$$build $(NJOBS) $(VERBOSE); \ done -else - echo "Skip on this arch" -endif touch $@ @@ -901,10 +891,9 @@ endif $(DEB_INST)/usr/lib/llvm-$(LLVM_VERSION)/build/utils/lit/lit/*/__pycache__/ DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C libclc/build install -ifeq (${COMPILER_RT_WASM_ENABLE},yes) + DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-compiler-rt-wasm32 install DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-compiler-rt-wasm64 install -endif # Rename binaries mkdir -p $(DEB_INST)/usr/bin/ From ecbebd0a8fd344a73c54963f287387a950b99213 Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Sun, 13 Nov 2022 09:51:30 +0200 Subject: [PATCH 2/7] Patch the WebAssembly include paths for C++ as well In wasm-sysroot-usr.diff we have changes to support compilation without a sysroot and with system paths. These so far have applied to the C include paths (among other things), i.e. /usr/include/. This was mainly because that's what I had at hand to test and wanted to keep things limited as a first iteration. Now that we're iterated on it and cleared out some issues, make the exact same changes for the C++ include paths as well, i.e. /usr/include//c++/v1. Nothing installs anything on those paths there yet, so this is mostly preparatory for subsequent changes. --- debian/patches/wasm/wasm-sysroot-usr.diff | 27 +++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/debian/patches/wasm/wasm-sysroot-usr.diff b/debian/patches/wasm/wasm-sysroot-usr.diff index 165de408..0d884214 100644 --- a/debian/patches/wasm/wasm-sysroot-usr.diff +++ b/debian/patches/wasm/wasm-sysroot-usr.diff @@ -27,7 +27,7 @@ Index: llvm-toolchain-14-14.0.6/clang/lib/Driver/ToolChains/WebAssembly.cpp if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { SmallString<128> P(D.ResourceDir); -@@ -419,12 +421,20 @@ void WebAssembly::AddClangSystemIncludeA +@@ -419,27 +421,39 @@ void WebAssembly::AddClangSystemIncludeA return; } @@ -51,7 +51,30 @@ Index: llvm-toolchain-14-14.0.6/clang/lib/Driver/ToolChains/WebAssembly.cpp } void WebAssembly::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, -@@ -467,3 +477,15 @@ SanitizerMask WebAssembly::getSupportedS + ArgStringList &CC1Args) const { ++ std::string SysRoot = computeSysRoot(); ++ + if (!DriverArgs.hasArg(options::OPT_nostdlibinc) && + !DriverArgs.hasArg(options::OPT_nostdincxx)) { + if (getTriple().getOS() != llvm::Triple::UnknownOS) { + const std::string MultiarchTriple = +- getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); ++ getMultiarchTriple(getDriver(), getTriple(), SysRoot); + addSystemInclude(DriverArgs, CC1Args, +- getDriver().SysRoot + "/include/" + MultiarchTriple + ++ SysRoot + "/include/" + MultiarchTriple + + "/c++/v1"); + } +- addSystemInclude(DriverArgs, CC1Args, +- getDriver().SysRoot + "/include/c++/v1"); ++ ++ // don't include the host architecture's headers in the search path ++ if (!getDriver().SysRoot.empty()) ++ addSystemInclude(DriverArgs, CC1Args, SysRoot + "/include/c++/v1"); + } + } + +@@ -467,3 +481,15 @@ SanitizerMask WebAssembly::getSupportedS Tool *WebAssembly::buildLinker() const { return new tools::wasm::Linker(*this); } From 6923a432bf072385e0b849ae191dba9a2487e891 Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Sun, 13 Nov 2022 10:53:35 +0200 Subject: [PATCH 3/7] d/rules: remove targets from .PHONY We actually touch $@ at the end of these targets, so they are not phony. --- debian/rules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/rules b/debian/rules index 04ff3481..48ad0b52 100755 --- a/debian/rules +++ b/debian/rules @@ -1201,4 +1201,4 @@ override_dh_auto_clean: : # remove extra stamps rm -f debian-*-build -.PHONY: override_dh_strip preconfigure debian-full-build debian-libfuzzer-build debian-libclc-build debian-rtlib-wasm-build +.PHONY: override_dh_strip preconfigure From bbfc6cca0704cce55d2bac33e3db865352539640 Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Sun, 13 Nov 2022 11:12:52 +0200 Subject: [PATCH 4/7] d/rules: split override_dh_auto_build into multiple lines --- debian/rules | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/debian/rules b/debian/rules index 48ad0b52..6b8a13b0 100755 --- a/debian/rules +++ b/debian/rules @@ -752,7 +752,12 @@ sccache-stats: fi touch $@ -override_dh_auto_build: debian-full-build debian-libfuzzer-build debian-libclc-build debian-rtlib-wasm-build sccache-stats +override_dh_auto_build: \ + debian-full-build \ + debian-libfuzzer-build \ + debian-libclc-build \ + debian-rtlib-wasm-build \ + sccache-stats override_dh_prep: build_doc dh_prep From 9459395c21478caf0b551970222d239ec250e85e Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Sun, 13 Nov 2022 11:20:01 +0200 Subject: [PATCH 5/7] d/rules: Makeify compiler-rt wasm target Iterate on wasm32/wasm64 using make targets rather than shell, and place build artifacts under a common "build-wasm" directory. --- debian/rules | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/debian/rules b/debian/rules index 6b8a13b0..cb05e356 100755 --- a/debian/rules +++ b/debian/rules @@ -709,16 +709,17 @@ debian-libclc-build: ninja $(NJOBS) $(VERBOSE) touch $@ -debian-rtlib-wasm-build: - echo "Using cmake: $(CMAKE_BIN)" - for build in wasm32 wasm64; do \ - mkdir -p build-compiler-rt-$$build; \ - $(CMAKE_BIN) -B build-compiler-rt-$$build -S compiler-rt/lib/builtins/ \ +build-wasm/compiler-rt-%: cpu = $(@:build-wasm/compiler-rt-%=%) +build-wasm/compiler-rt-%: + @echo "Building compiler-rt for $(cpu)" + @echo "Using cmake: $(CMAKE_BIN)" + mkdir -p "$@" + $(CMAKE_BIN) -B "$@" -S compiler-rt/lib/builtins/ \ -G Ninja \ $(SCCACHE_CMAKE) \ - -DCMAKE_C_COMPILER_TARGET=$$build-unknown-unknown \ - -DCMAKE_CXX_COMPILER_TARGET=$$build-unknown-unknown \ - -DCMAKE_ASM_COMPILER_TARGET=$$build-unknown-unknown \ + -DCMAKE_C_COMPILER_TARGET=$(cpu)-unknown-unknown \ + -DCMAKE_CXX_COMPILER_TARGET=$(cpu)-unknown-unknown \ + -DCMAKE_ASM_COMPILER_TARGET=$(cpu)-unknown-unknown \ -DCMAKE_C_COMPILER=$(STAGE_2_BIN_DIR)/clang \ -DCMAKE_CXX_COMPILER=$(STAGE_2_BIN_DIR)/clang++ \ -DCMAKE_C_FLAGS="$(opt_flags) $(STAGE_2_CFLAGS)" \ @@ -735,12 +736,14 @@ debian-rtlib-wasm-build: -DCOMPILER_RT_INCLUDE_TESTS=OFF \ -DCOMPILER_RT_USE_LIBCXX=OFF \ -DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON \ - -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE=$$build-unknown-unknown \ - -DCOMPILER_RT_OS_DIR=wasi; \ - ninja -C build-compiler-rt-$$build $(NJOBS) $(VERBOSE); \ - done - touch $@ + -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE=$(cpu)-unknown-unknown \ + -DCOMPILER_RT_OS_DIR=wasi + ninja -C "$@" $(NJOBS) $(VERBOSE) +debian-wasm-build: \ + build-wasm/compiler-rt-wasm32 \ + build-wasm/compiler-rt-wasm64 + touch $@ sccache-stats: # If we used sccache, shows stats @@ -756,7 +759,7 @@ override_dh_auto_build: \ debian-full-build \ debian-libfuzzer-build \ debian-libclc-build \ - debian-rtlib-wasm-build \ + debian-wasm-build \ sccache-stats override_dh_prep: build_doc @@ -897,8 +900,8 @@ endif DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C libclc/build install - DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-compiler-rt-wasm32 install - DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-compiler-rt-wasm64 install + DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-wasm/compiler-rt-wasm32 install + DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-wasm/compiler-rt-wasm64 install # Rename binaries mkdir -p $(DEB_INST)/usr/bin/ @@ -1190,7 +1193,7 @@ override_dh_auto_clean: rm -f $(CURDIR)/clang/tools/clang-format/clang-format-diff-$(LLVM_VERSION) rm -f $(CURDIR)/clang/tools/clang-format/clang-format-$(LLVM_VERSION).py rm -rf libclc/build - rm -rf build-compiler-rt-wasm32 build-compiler-rt-wasm64 + rm -rf build-wasm if test -f lld/docs/ld.lld-$(LLVM_VERSION).1; then \ mv lld/docs/ld.lld-$(LLVM_VERSION).1 lld/docs/ld.lld.1; \ fi From 97d60707ed17c4ab13f225129e3f22faaa144274 Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Sun, 13 Nov 2022 12:14:06 +0200 Subject: [PATCH 6/7] Build libc++/libc++abi for wasm32-wasi Do a standalone build of libcxx/libcxxabi, for wasm32-wasi. libcxx requires a libc, and thus this requires a new build dependency on wasi-libc. wasi-libc is not currently available for wasm64, so limit the build to wasm32. This introduces two new binary packages: - libc++-${LLVM_VERSION}-dev-wasm32 - libc++abi-${LLVM_VERSION}-dev-wasm32 There isn't currently a naming convention in Debian for WebAssembly. Use a "-dev-wasm32" suffix, which makes it consistent with packages such as "libc6-dev-i386" and "libstd-rust-dev-wasm32". Given these builds are specific to WASI, it can be argued that the names should be something like "-dev-wasm32-wasi", or simplified to "-dev-wasi" (not very future proof) or "-dev-wasi32" (not something upstream uses). Something to think through later on. WebAssembly only supports shared linking, hence why we -dev packages are provided, shipping development headers and static archives. --- debian/control | 43 ++++++++++ debian/libc++-X.Y-dev-wasm32.install.in | 3 + debian/libc++-X.Y-dev-wasm32.links.in | 2 + debian/libc++abi-X.Y-dev-wasm32.install.in | 1 + debian/libc++abi-X.Y-dev-wasm32.links.in | 1 + debian/qualify-clang.sh | 26 +++++-- debian/rules | 91 +++++++++++++++++++++- 7 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 debian/libc++-X.Y-dev-wasm32.install.in create mode 100644 debian/libc++-X.Y-dev-wasm32.links.in create mode 100644 debian/libc++abi-X.Y-dev-wasm32.install.in create mode 100644 debian/libc++abi-X.Y-dev-wasm32.links.in diff --git a/debian/control b/debian/control index 58f91da2..9883345b 100644 --- a/debian/control +++ b/debian/control @@ -24,6 +24,7 @@ Build-Depends: debhelper (>= 10.0), cmake, ninja-build, libpfm4-dev [linux-any], python3-setuptools, libz3-dev, llvm-spirv-14 [ amd64 arm64 armel armhf mips64el mipsel ppc64el s390x ] | hello [!i386], spirv-tools [ linux-any ] | hello [ !i386], + wasi-libc, libcurl4-dev, libgrpc++-dev [amd64 arm64 armel armhf mips64el mipsel ppc64 ppc64el powerpc riscv64 s390x], protobuf-compiler-grpc [amd64 arm64 armel armhf mips64el mipsel ppc64 ppc64el powerpc riscv64 s390x], @@ -648,6 +649,29 @@ Description: LLVM C++ Standard library (development files) as exception objects, rtti and memory allocation. * Extensive unit tests. +Package: libc++-14-dev-wasm32 +Section: libdevel +Architecture: all +Multi-Arch: foreign +Depends: wasi-libc, libc++abi-14-dev-wasm32, ${misc:Depends} +Provides: libc++-x.y-dev-wasm32 +Conflicts: libc++-x.y-dev-wasm32 +Replaces: libc++-x.y-dev-wasm32 +Description: LLVM C++ Standard library (WASI) + libc++ is another implementation of the C++ standard library + . + Features and Goals + . + * Correctness as defined by the C++ standards. + * Fast execution. + * Minimal memory use. + * Fast compile times. + * ABI compatibility with gcc's libstdc++ for some low-level features such + as exception objects, rtti and memory allocation. + * Extensive unit tests. + . + This package provides a version for the 32-bit WebAssembly System Interface. + # ------------- libcxxabi ------------- Package: libc++abi1-14 @@ -687,6 +711,25 @@ Description: LLVM low level support for a standard C++ library (development file * Correctness as defined by the C++ standards. * Provide a portable sublayer to ease the porting of libc++ +Package: libc++abi-14-dev-wasm32 +Section: libdevel +Architecture: all +Multi-Arch: foreign +Depends: wasi-libc, ${misc:Depends} +Provides: libc++abi-x.y-dev-wasm32 +Conflicts: libc++abi-x.y-dev-wasm32 +Replaces: libc++abi-x.y-dev-wasm32 +Description: LLVM low level support for a standard C++ library (WASI) + libc++abi is another implementation of low level support for a standard C++ + library. + . + Features and Goals + . + * Correctness as defined by the C++ standards. + * Provide a portable sublayer to ease the porting of libc++ + . + This package provides a version for the 32-bit WebAssembly System Interface. + # ------------- libclc ------------- Package: libclc-14 diff --git a/debian/libc++-X.Y-dev-wasm32.install.in b/debian/libc++-X.Y-dev-wasm32.install.in new file mode 100644 index 00000000..13c31f09 --- /dev/null +++ b/debian/libc++-X.Y-dev-wasm32.install.in @@ -0,0 +1,3 @@ +usr/lib/llvm-@LLVM_VERSION@/lib/wasm32-wasi/libc++.a +usr/lib/llvm-@LLVM_VERSION@/include/wasm32-wasi/c++/ +usr/lib/llvm-@LLVM_VERSION@/include/wasm32-wasi/c++/v1/ diff --git a/debian/libc++-X.Y-dev-wasm32.links.in b/debian/libc++-X.Y-dev-wasm32.links.in new file mode 100644 index 00000000..465536e9 --- /dev/null +++ b/debian/libc++-X.Y-dev-wasm32.links.in @@ -0,0 +1,2 @@ +usr/lib/llvm-@LLVM_VERSION@/lib/wasm32-wasi/libc++.a /usr/lib/wasm32-wasi/libc++.a +usr/lib/llvm-@LLVM_VERSION@/include/wasm32-wasi/c++/v1 /usr/include/wasm32-wasi/c++/v1 diff --git a/debian/libc++abi-X.Y-dev-wasm32.install.in b/debian/libc++abi-X.Y-dev-wasm32.install.in new file mode 100644 index 00000000..1d6e8500 --- /dev/null +++ b/debian/libc++abi-X.Y-dev-wasm32.install.in @@ -0,0 +1 @@ +usr/lib/llvm-@LLVM_VERSION@/lib/wasm32-wasi/libc++abi.a diff --git a/debian/libc++abi-X.Y-dev-wasm32.links.in b/debian/libc++abi-X.Y-dev-wasm32.links.in new file mode 100644 index 00000000..c1655fbf --- /dev/null +++ b/debian/libc++abi-X.Y-dev-wasm32.links.in @@ -0,0 +1 @@ +usr/lib/llvm-@LLVM_VERSION@/lib/wasm32-wasi/libc++abi.a /usr/lib/wasm32-wasi/libc++abi.a diff --git a/debian/qualify-clang.sh b/debian/qualify-clang.sh index dfda6e44..385cf5a7 100755 --- a/debian/qualify-clang.sh +++ b/debian/qualify-clang.sh @@ -1361,22 +1361,34 @@ fi if dpkg -l|grep -q wasi-libc; then cat < printf.c #include - int main(int argc, char *argv[]) - { - printf("%s\n", "Hello world!"); + int main(int argc, char *argv[]) { + printf("%s\n", "Hello World!"); } EOF - clang-$VERSION -target wasm32-unknown-wasi -o printf printf.c + # wasi-libc supports only wasm32 right now + clang-$VERSION -target wasm32-wasi -o printf printf.c file printf &> foo.log if ! grep -q "WebAssembly" foo.log; then echo "the generated file isn't a WebAssembly file?" exit 1 fi rm -f printf.c printf + + cat < cout.cpp + #include + int main() { + std::cout << "Hello World!" << std::endl; + } +EOF + # libcxx requires wasi-libc, which only exists for wasm32 right now + clang++-$VERSION --target=wasm32-wasi -o cout cout.cpp + file cout &> foo.log + if ! grep -q "WebAssembly" foo.log; then + echo "the generated file isn't a WebAssembly file?" + exit 1 + fi + rm -f cout.cpp cout fi -echo '#include ' > foo.cpp -# Fails for now -clang++-$VERSION --target=wasm32-wasi -o foo.o -c foo.cpp||true echo ' #include diff --git a/debian/rules b/debian/rules index cb05e356..184de30a 100755 --- a/debian/rules +++ b/debian/rules @@ -740,8 +740,89 @@ build-wasm/compiler-rt-%: -DCOMPILER_RT_OS_DIR=wasi ninja -C "$@" $(NJOBS) $(VERBOSE) +build-wasm/libcxx-%-wasi: cpu = $(@:build-wasm/libcxx-%-wasi=%) +build-wasm/libcxx-%-wasi: build-wasm/compiler-rt-% + @echo "Building libcxx for $(cpu)" + @echo "Using cmake: $(CMAKE_BIN)" + + # We need a functioning clang, which in turn requires a linker. We + # patch clang to use a versioned wasm-ld (cf. wasm-ld-path.diff), so + # create wasm-ld-$(LLVM_VERSION) in the stage2 bin dir manually. + cp $(STAGE_2_BIN_DIR)/wasm-ld $(STAGE_2_BIN_DIR)/wasm-ld-$(LLVM_VERSION) + + # We need a wasm compiler-rt. Depend on the make target that builds it, + # and manually copy it to the stage2 lib dir from there + mkdir -p \ + $(STAGE_2_LIB_DIR)/clang/$(LLVM_VERSION_FULL)/lib/wasi/ + cp build-wasm/compiler-rt-$(cpu)/lib/wasi/libclang_rt.builtins-$(cpu).a \ + $(STAGE_2_LIB_DIR)/clang/$(LLVM_VERSION_FULL)/lib/wasi/ + + # Notes: + # - Uses $(LDFLAGS) instead of $(STAGE_2_LDFLAGS), because wasm-ld does not + # support --build-id yet. Upstream is working on it, cf. D107662. + # - Pass -fno-stack-protector to disable -fstack-protector-strong that is + # passed by default, as this is not supported yet in WebAssembly, cf. + # https://github.com/WebAssembly/wasi-libc/issues/157 + # - Use llvm-ar and llvm-ranlib, as binutils does not currently support + # WebAssembly and creates invalid indexes. + # - Use LLVM_LIBDIR_SUFFIX to install to /usr/lib/wasm32-wasi. To be + # replaced by CMAKE_INSTALL_LIBDIR=lib/$(cpu)-wasi when D130586 + # ships. + mkdir -p "$@" + $(CMAKE_BIN) -B "$@" -S runtimes \ + -G $(GENERATOR) \ + $(SCCACHE_CMAKE) \ + -DCMAKE_C_COMPILER_WORKS=ON \ + -DCMAKE_CXX_COMPILER_WORKS=ON \ + -DLLVM_COMPILER_CHECKED=ON \ + -DCMAKE_C_COMPILER_TARGET=$(cpu)-unknown-wasi \ + -DCMAKE_CXX_COMPILER_TARGET=$(cpu)-unknown-wasi \ + -DCMAKE_ASM_COMPILER_TARGET=$(cpu)-unknown-wasi \ + -DCMAKE_C_COMPILER=$(STAGE_2_BIN_DIR)/clang \ + -DCMAKE_CXX_COMPILER=$(STAGE_2_BIN_DIR)/clang++ \ + -DCMAKE_AR=$(STAGE_2_BIN_DIR)/llvm-ar \ + -DCMAKE_RANLIB=$(STAGE_2_BIN_DIR)/llvm-ranlib \ + -DCMAKE_C_FLAGS="$(opt_flags) $(STAGE_2_CFLAGS) -fno-stack-protector" \ + -DCMAKE_CXX_FLAGS="$(opt_flags) $(STAGE_2_CXXFLAGS) -fno-stack-protector" \ + -DCMAKE_SHARED_LINKER_FLAGS="$(LDFLAGS) -L$(STAGE_2_LIB_DIR)" \ + -DCMAKE_MODULE_LINKER_FLAGS="$(LDFLAGS) -L$(STAGE_2_LIB_DIR)" \ + -DCMAKE_EXE_LINKER_FLAGS="$(LDFLAGS) -L$(STAGE_2_LIB_DIR)" \ + -DCMAKE_INSTALL_PREFIX=/usr/lib/llvm-$(LLVM_VERSION) \ + -DCMAKE_INSTALL_INCLUDEDIR=include/$(cpu)-wasi \ + -DLLVM_LIBDIR_SUFFIX=/$(cpu)-wasi \ + -DLLVM_CONFIG=$(STAGE_2_BIN_DIR)/llvm-config \ + -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ + -DLIBCXX_USE_COMPILER_RT=ON \ + -DLIBCXXABI_USE_COMPILER_RT=ON \ + -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \ + -DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON \ + -DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=OFF \ + -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ + -DLIBCXX_CXX_ABI=libcxxabi \ + -DLIBCXX_ABI_VERSION=2 \ + -DLIBCXX_HAS_MUSL_LIBC:BOOL=ON \ + -DLIBCXX_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL=OFF \ + -DLIBCXX_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXX_ENABLE_FILESYSTEM:BOOL=OFF \ + -DLIBCXX_ENABLE_THREADS:BOOL=OFF \ + -DLIBCXX_HAS_PTHREAD_API:BOOL=OFF \ + -DLIBCXX_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ + -DLIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF \ + -DLIBCXXABI_ENABLE_SHARED:BOOL=OFF \ + -DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=OFF \ + -DLIBCXXABI_SILENT_TERMINATE:BOOL=ON \ + -DLIBCXXABI_ENABLE_THREADS:BOOL=OFF \ + -DLIBCXXABI_HAS_PTHREAD_API:BOOL=OFF \ + -DLIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL=OFF \ + -DLIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL=OFF + ninja -C "$@" $(NJOBS) $(VERBOSE) + +# Build compiler-rt for wasm32 and wasm64. Build libcxx only for wasm32, as +# libcxx requires wasi-libc, which only exists for wasm32 right now. debian-wasm-build: \ build-wasm/compiler-rt-wasm32 \ + build-wasm/libcxx-wasm32-wasi \ build-wasm/compiler-rt-wasm64 touch $@ @@ -902,6 +983,7 @@ endif DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-wasm/compiler-rt-wasm32 install DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-wasm/compiler-rt-wasm64 install + DESTDIR=$(DEB_INST) ninja $(VERBOSE) -C build-wasm/libcxx-wasm32-wasi install # Rename binaries mkdir -p $(DEB_INST)/usr/bin/ @@ -1055,7 +1137,7 @@ ifeq ($(shell dpkg --compare-versions $(shell dpkg-query -W -f '$${Version}' bin else # GNU strip doesn't recognize WebAssembly binaries, and actually corrupts them. # llvm-strip (as of 15.0.2) fails with --strip-debug (but works with --strip-all) - dh_strip -a -v -Xlibclang_rt.builtins-wasm32.a -Xlibclang_rt.builtins-wasm64.a + dh_strip -a -v -Xlibclang_rt.builtins-wasm32.a -Xlibclang_rt.builtins-wasm64.a -Xusr/lib/wasm32-wasi endif override_dh_install: @@ -1085,6 +1167,13 @@ override_dh_installdeb: # Remove some libc++ abi files in the libc++ file. See bug #969274 rm -f $(CURDIR)/debian/libc++-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/include/c++/__cxxabi_config.h $(CURDIR)/debian/libc++-$(LLVM_VERSION)-dev/usr/lib/llvm-$(LLVM_VERSION)/include/c++/cxxabi.h + + mkdir -p $(CURDIR)/debian/libc++abi-$(LLVM_VERSION)-dev-wasm32/usr/lib/llvm-$(LLVM_VERSION)/include/wasm32-wasi/c++/v1 + mv $(CURDIR)/debian/libc++-$(LLVM_VERSION)-dev-wasm32/usr/lib/llvm-$(LLVM_VERSION)/include/wasm32-wasi/c++/v1/__cxxabi_config.h \ + $(CURDIR)/debian/libc++abi-$(LLVM_VERSION)-dev-wasm32/usr/lib/llvm-$(LLVM_VERSION)/include/wasm32-wasi/c++/v1/__cxxabi_config.h + mv $(CURDIR)/debian/libc++-$(LLVM_VERSION)-dev-wasm32/usr/lib/llvm-$(LLVM_VERSION)/include/wasm32-wasi/c++/v1/cxxabi.h \ + $(CURDIR)/debian/libc++abi-$(LLVM_VERSION)-dev-wasm32/usr/lib/llvm-$(LLVM_VERSION)/include/wasm32-wasi/c++/v1/cxxabi.h + # Remove auto generated python pyc find $(CURDIR)/debian/llvm-$(LLVM_VERSION)-tools/usr/lib/llvm-$(LLVM_VERSION)/ -name '*.pyc' | xargs -r rm -f From c0beef7a9cc2cd5cc2808094ed54e0b4b6803d0c Mon Sep 17 00:00:00 2001 From: Faidon Liambotis Date: Fri, 18 Nov 2022 08:44:52 +0200 Subject: [PATCH 7/7] Add changelog entries for recent WebAssembly changes * More WebAssembly (wasm32/wasm64) work: - Update patch wasm-sysroot-usr to stop including /usr/include when building for the non-OS (unknown) wasm targets, to avoid inadvertently including the host's (glibc) headers. - Re-enable builds on all architectures, previously disabled for arm64 armel mips64el mipsel ppc64el armhf in 1:14.0.6-4, due to an FTBFS related to the /usr/include issue above. - Update patch wasm-sysroot-usr to adjust system include paths for C++ headers as well. - Build libc++ and libc++abi for wasm32-wasi, generating two new binary packages, and introducing a build dependency on wasi-libc. - Misc code organization fixes to debian/rules. --- debian/changelog | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/debian/changelog b/debian/changelog index 413272da..23dc3286 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,20 @@ +llvm-toolchain-14 (1:14.0.6-10) UNRELEASED; urgency=medium + + * More WebAssembly (wasm32/wasm64) work: + - Update patch wasm-sysroot-usr to stop including /usr/include when + building for the non-OS (unknown) wasm targets, to avoid inadvertently + including the host's (glibc) headers. + - Re-enable builds on all architectures, previously disabled for + arm64 armel mips64el mipsel ppc64el armhf in 1:14.0.6-4, due to an FTBFS + related to the /usr/include issue above. + - Update patch wasm-sysroot-usr to adjust system include paths for C++ + headers as well. + - Build libc++ and libc++abi for wasm32-wasi, generating two new binary + packages, and introducing a build dependency on wasi-libc. + - Misc code organization fixes to debian/rules. + + -- Faidon Liambotis Fri, 18 Nov 2022 08:21:02 +0200 + llvm-toolchain-14 (1:14.0.6-9) unstable; urgency=medium * Disable the MLIR testsuite run because of a freeze