From 36499ca81e4e80240033949f268fbcedec8b261c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 2 Mar 2024 10:09:04 -0800 Subject: [PATCH] Start renaming preview1 to p1 and preview2 to p2 This commit is a reflection of WebAssembly/wasi-libc#478 into this repository where a few changes are happening: * A new `wasm32-wasip1` sysroot is prepared matching `wasm32-wasi` * A new `wasm32-wasip1-threads` sysroot is prepared matching `wasm32-wasi-threads` * The `wasm32-wasi-preview2` target is renamed `wasm32-wasip2` I've done a bit of makefile refactoring to deduplicate things a bit now that there's a number of targets being built. The long-term goal would be to remove the `wasm32-wasi` and `wasm32-wasip1-threads` targets, but that's not proposed just yet at this time. --- .gitmodules | 2 +- Makefile | 103 ++++++++++++++++++++-------------------------- src/wasi-libc | 2 +- tests/run.sh | 2 +- tests/testcase.sh | 2 +- 5 files changed, 49 insertions(+), 62 deletions(-) diff --git a/.gitmodules b/.gitmodules index 0691635ce..5329d735b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,7 +3,7 @@ url = https://github.com/llvm/llvm-project [submodule "src/wasi-libc"] path = src/wasi-libc - url = https://github.com/WebAssembly/wasi-libc + url = https://github.com/alexcrichton/wasi-libc [submodule "src/config"] path = src/config url = https://git.savannah.gnu.org/git/config.git diff --git a/Makefile b/Makefile index 6afaa734f..e54dc6662 100644 --- a/Makefile +++ b/Makefile @@ -107,26 +107,22 @@ build/llvm.BUILT: llvm-config touch build/llvm.BUILT +# Flags for running `make` in wasi-libc +# $(1): the target that's being built +WASI_LIBC_MAKEFLAGS = \ + -C $(ROOT_DIR)/src/wasi-libc \ + CC=$(BUILD_PREFIX)/bin/clang \ + AR=$(BUILD_PREFIX)/bin/llvm-ar \ + NM=$(BUILD_PREFIX)/bin/llvm-nm \ + SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ + TARGET_TRIPLE=$(1) + build/wasi-libc.BUILT: build/compiler-rt.BUILT - $(MAKE) -C $(ROOT_DIR)/src/wasi-libc \ - CC=$(BUILD_PREFIX)/bin/clang \ - AR=$(BUILD_PREFIX)/bin/llvm-ar \ - NM=$(BUILD_PREFIX)/bin/llvm-nm \ - SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ - WASI_SNAPSHOT=preview2 \ - default libc_so - $(MAKE) -C $(ROOT_DIR)/src/wasi-libc \ - CC=$(BUILD_PREFIX)/bin/clang \ - AR=$(BUILD_PREFIX)/bin/llvm-ar \ - NM=$(BUILD_PREFIX)/bin/llvm-nm \ - SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ - default libc_so - $(MAKE) -C $(ROOT_DIR)/src/wasi-libc \ - CC=$(BUILD_PREFIX)/bin/clang \ - AR=$(BUILD_PREFIX)/bin/llvm-ar \ - NM=$(BUILD_PREFIX)/bin/llvm-nm \ - SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ - THREAD_MODEL=posix + $(MAKE) $(call WASI_LIBC_MAKEFLAGS,wasm32-wasi) default libc_so + $(MAKE) $(call WASI_LIBC_MAKEFLAGS,wasm32-wasip1) default libc_so + $(MAKE) $(call WASI_LIBC_MAKEFLAGS,wasm32-wasip2) WASI_SNAPSHOT=p2 default libc_so + $(MAKE) $(call WASI_LIBC_MAKEFLAGS,wasm32-wasi-threads) THREAD_MODEL=posix + $(MAKE) $(call WASI_LIBC_MAKEFLAGS,wasm32-wasip1-threads) THREAD_MODEL=posix touch build/wasi-libc.BUILT build/compiler-rt.BUILT: build/llvm.BUILT @@ -156,11 +152,15 @@ build/compiler-rt.BUILT: build/llvm.BUILT DESTDIR=$(DESTDIR) ninja $(NINJA_FLAGS) -C build/compiler-rt install # Install clang-provided headers. cp -R $(ROOT_DIR)/build/llvm/lib/clang $(BUILD_PREFIX)/lib/ + cp -R $(BUILD_PREFIX)/lib/clang/17/lib/wasi $(BUILD_PREFIX)/lib/clang/17/lib/wasip1 + cp -R $(BUILD_PREFIX)/lib/clang/17/lib/wasi $(BUILD_PREFIX)/lib/clang/17/lib/wasip2 touch build/compiler-rt.BUILT # Flags for libcxx and libcxxabi. # $(1): pthreads ON or OFF # $(2): shared libraries ON or OFF +# $(3): the name of the target being built for +# $(4): extra compiler flags to pass LIBCXX_CMAKE_FLAGS = \ -DCMAKE_C_COMPILER_WORKS=ON \ -DCMAKE_CXX_COMPILER_WORKS=ON \ @@ -199,47 +199,34 @@ LIBCXX_CMAKE_FLAGS = \ -DLIBCXXABI_ENABLE_PIC:BOOL=$(2) \ -DWASI_SDK_PREFIX=$(BUILD_PREFIX) \ -DUNIX:BOOL=ON \ - --debug-trycompile + --debug-trycompile \ + -DCMAKE_SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ + -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CFLAGS) $(4) --target=$(3)" \ + -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CXXFLAGS) $(4) --target=$(3)" \ + -DLIBCXX_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/$(3) \ + -DLIBCXXABI_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/$(3) \ + -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ + $(LLVM_PROJ_DIR)/runtimes + +# Rules to build libcxx, factored out here to deduplicate the below +# $(1): pthreads ON or OFF +# $(2): shared libraries ON or OFF +# $(3): the name of the target being built for +define BUILD_LIBCXX + mkdir -p build/libcxx-$(3) + cd build/libcxx-$(3) && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,$(1),$(2),$(3),$(4)) + ninja $(NINJA_FLAGS) -C build/libcxx-$(3) + DESTDIR=$(DESTDIR) ninja $(NINJA_FLAGS) -C build/libcxx-$(3) install + rm -rf $(BUILD_PREFIX)/share/wasi-sysroot/include/$(3)/c++ + mv $(BUILD_PREFIX)/share/wasi-sysroot/include/c++ $(BUILD_PREFIX)/share/wasi-sysroot/include/$(3)/ +endef build/libcxx.BUILT: build/llvm.BUILT build/wasi-libc.BUILT - # Do the build. - mkdir -p build/libcxx-preview2 - cd build/libcxx-preview2 && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,OFF,ON) \ - -DCMAKE_SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ - -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CFLAGS) --target=wasm32-wasi-preview2" \ - -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CXXFLAGS) --target=wasm32-wasi-preview2" \ - -DLIBCXX_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/wasm32-wasi-preview2 \ - -DLIBCXXABI_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/wasm32-wasi-preview2 \ - -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ - $(LLVM_PROJ_DIR)/runtimes - ninja $(NINJA_FLAGS) -C build/libcxx-preview2 - mkdir -p build/libcxx - cd build/libcxx && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,OFF,ON) \ - -DCMAKE_SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ - -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CFLAGS) --target=wasm32-wasi" \ - -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP) $(EXTRA_CXXFLAGS) --target=wasm32-wasi" \ - -DLIBCXX_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/wasm32-wasi \ - -DLIBCXXABI_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/wasm32-wasi \ - -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ - $(LLVM_PROJ_DIR)/runtimes - ninja $(NINJA_FLAGS) -C build/libcxx - mkdir -p build/libcxx-threads - cd build/libcxx-threads && cmake -G Ninja $(call LIBCXX_CMAKE_FLAGS,ON,OFF) \ - -DCMAKE_SYSROOT=$(BUILD_PREFIX)/share/wasi-sysroot \ - -DCMAKE_C_FLAGS="$(DEBUG_PREFIX_MAP) -pthread $(EXTRA_CFLAGS) --target=wasm32-wasi-threads" \ - -DCMAKE_CXX_FLAGS="$(DEBUG_PREFIX_MAP) -pthread $(EXTRA_CXXFLAGS) --target=wasm32-wasi-threads" \ - -DLIBCXX_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/wasm32-wasi-threads \ - -DLIBCXXABI_LIBDIR_SUFFIX=$(ESCAPE_SLASH)/wasm32-wasi-threads \ - -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ - $(LLVM_PROJ_DIR)/runtimes - ninja $(NINJA_FLAGS) -C build/libcxx-threads - # Do the install. - DESTDIR=$(DESTDIR) ninja $(NINJA_FLAGS) -C build/libcxx-preview2 install - mv $(BUILD_PREFIX)/share/wasi-sysroot/include/c++ $(BUILD_PREFIX)/share/wasi-sysroot/include/wasm32-wasi-preview2/ - DESTDIR=$(DESTDIR) ninja $(NINJA_FLAGS) -C build/libcxx install - mv $(BUILD_PREFIX)/share/wasi-sysroot/include/c++ $(BUILD_PREFIX)/share/wasi-sysroot/include/wasm32-wasi/ - DESTDIR=$(DESTDIR) ninja $(NINJA_FLAGS) -C build/libcxx-threads install - mv $(BUILD_PREFIX)/share/wasi-sysroot/include/c++ $(BUILD_PREFIX)/share/wasi-sysroot/include/wasm32-wasi-threads/ + $(call BUILD_LIBCXX,OFF,ON,wasm32-wasi) + $(call BUILD_LIBCXX,OFF,ON,wasm32-wasip1) + $(call BUILD_LIBCXX,OFF,ON,wasm32-wasip2) + $(call BUILD_LIBCXX,ON,OFF,wasm32-wasi-threads,-pthread) + $(call BUILD_LIBCXX,ON,OFF,wasm32-wasip1-threads,-pthread) # As of this writing, `clang++` will ignore the above include dirs unless this one also exists: mkdir -p $(BUILD_PREFIX)/share/wasi-sysroot/include/c++/v1 touch build/libcxx.BUILT diff --git a/src/wasi-libc b/src/wasi-libc index cc62fa82c..0ff94954d 160000 --- a/src/wasi-libc +++ b/src/wasi-libc @@ -1 +1 @@ -Subproject commit cc62fa82c29bfced82d280cbccc231667ee80822 +Subproject commit 0ff94954d2825b697e161630167dc8bb79858cd9 diff --git a/tests/run.sh b/tests/run.sh index 62d05e9ce..1d41ff1b5 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -44,7 +44,7 @@ echo $CC echo $CXX echo "SDK: $wasi_sdk" -for target in wasm32-wasi wasm32-wasi-threads wasm32-wasi-preview2; do +for target in wasm32-wasi wasm32-wasip1 wasm32-wasi-threads wasm32-wasip1-threads wasm32-wasip2; do echo "===== Testing target $target =====" cd $testdir/compile-only for options in -O0 -O2 "-O2 -flto"; do diff --git a/tests/testcase.sh b/tests/testcase.sh index d7368e09c..285424ff0 100755 --- a/tests/testcase.sh +++ b/tests/testcase.sh @@ -28,7 +28,7 @@ else file_options= fi -if [ "$target" == "wasm32-wasi-threads" ]; then +if echo "$target" | grep -q -- '-threads$'; then pthread_options="-pthread" else pthread_options=