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.
This commit is contained in:
Faidon Liambotis 2022-11-13 12:14:06 +02:00
parent 9459395c21
commit 97d60707ed
7 changed files with 159 additions and 8 deletions

43
debian/control vendored
View File

@ -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 ] <!stage1> | 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

View File

@ -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/

2
debian/libc++-X.Y-dev-wasm32.links.in vendored Normal file
View File

@ -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

View File

@ -0,0 +1 @@
usr/lib/llvm-@LLVM_VERSION@/lib/wasm32-wasi/libc++abi.a

View File

@ -0,0 +1 @@
usr/lib/llvm-@LLVM_VERSION@/lib/wasm32-wasi/libc++abi.a /usr/lib/wasm32-wasi/libc++abi.a

View File

@ -1361,22 +1361,34 @@ fi
if dpkg -l|grep -q wasi-libc; then
cat <<EOF > printf.c
#include <stdio.h>
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 <<EOF > cout.cpp
#include <iostream>
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 <algorithm>' > foo.cpp
# Fails for now
clang++-$VERSION --target=wasm32-wasi -o foo.o -c foo.cpp||true
echo '
#include <vector>

91
debian/rules vendored
View File

@ -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