From 5447ce54a821ec721b4671c94b3f556d443ee552 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 20 Dec 2023 14:03:26 -0700 Subject: [PATCH] add `wasm32-wasi-preview2` target Currently, this is identical to the `wasm32-wasi` in all but name. See #449 for the next step, which is to incrementally add Preview 2 features, e.g. `wasi-sockets`. Per the discussion in that PR, I've split the `wasi-sysroot/include` directory into per-target directories. Eventually, we'll want to build a separate sysroot for each target, but there's currently uncertainty about how to configure the default sysroot for e.g. clang, so we're not tackling that yet. See also #447 for further details. Signed-off-by: Joel Dice --- .github/workflows/main.yml | 6 +++++- Makefile | 10 ++++++++-- test/Makefile | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eb9b59f83..186ae0797 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -84,7 +84,9 @@ jobs: - name: Build libc shell: bash - run: make -j4 + run: | + make -j4 + WASI_SNAPSHOT=preview2 make -j4 - name: Test shell: bash @@ -98,6 +100,8 @@ jobs: mkdir -p $WASI_DIR cp download/lib/wasi/libclang_rt.builtins-wasm32.a $WASI_DIR make test + rm -r build + WASI_SNAPSHOT=preview2 make test # The older version of Clang does not provide the expected symbol for the # test entrypoints: `undefined symbol: __main_argc_argv`. # The older (<15.0.7) version of wasm-ld does not provide `__heap_end`, diff --git a/Makefile b/Makefile index 16d7ed600..891b33cf7 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,8 @@ SYSROOT ?= $(CURDIR)/sysroot INSTALL_DIR ?= /usr/local # single or posix; note that pthread support is still a work-in-progress. THREAD_MODEL ?= single +# preview1 or preview2; the latter is not (yet) compatible with multithreading +WASI_SNAPSHOT ?= preview1 # dlmalloc or none MALLOC_IMPL ?= dlmalloc # yes or no @@ -41,6 +43,10 @@ ifeq ($(THREAD_MODEL), posix) TARGET_TRIPLE = wasm32-wasi-threads endif +ifeq ($(WASI_SNAPSHOT), preview2) +TARGET_TRIPLE = wasm32-wasi-preview2 +endif + BUILTINS_LIB ?= $(shell ${CC} --print-libgcc-file-name) # These variables describe the locations of various files and directories in @@ -386,7 +392,7 @@ LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES)) # These variables describe the locations of various files and # directories in the generated sysroot tree. SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE) -SYSROOT_INC = $(SYSROOT)/include +SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE) SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE) # Files from musl's include directory that we don't want to install in the @@ -511,7 +517,7 @@ PIC_OBJS = \ # to CC. This is a workaround for a Windows command line size limitation. See # the `%.a` rule below for details. $(SYSROOT_LIB)/%.so: $(OBJDIR)/%.so.a $(BUILTINS_LIB) - $(CC) -nodefaultlibs -shared --sysroot=$(SYSROOT) \ + $(CC) --target=$(TARGET_TRIPLE) -nodefaultlibs -shared --sysroot=$(SYSROOT) \ -o $@ -Wl,--whole-archive $< -Wl,--no-whole-archive $(BUILTINS_LIB) $(OBJDIR)/libc.so.a: $(LIBC_SO_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_SO_OBJS) diff --git a/test/Makefile b/test/Makefile index 61e1756bc..584e81480 100644 --- a/test/Makefile +++ b/test/Makefile @@ -16,16 +16,28 @@ test: run OBJDIR ?= $(CURDIR)/build DOWNDIR ?= $(CURDIR)/download +# preview1 or preview2 +WASI_SNAPSHOT ?= preview1 + ##### DOWNLOAD ################################################################# LIBC_TEST_URL ?= https://github.com/bytecodealliance/libc-test LIBC_TEST = $(DOWNDIR)/libc-test LIBRT_URL ?= https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-16/libclang_rt.builtins-wasm32-wasi-16.0.tar.gz LIBRT = $(DOWNDIR)/lib/wasi/libclang_rt.builtins-wasm32.a -WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v3.0.0/wasmtime-v3.0.0-x86_64-linux.tar.xz +WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v16.0.0/wasmtime-v16.0.0-x86_64-linux.tar.xz WASMTIME = $(DOWNDIR)/$(shell basename $(WASMTIME_URL) .tar.xz)/wasmtime +WASM_TOOLS_URL ?= https://github.com/bytecodealliance/wasm-tools/releases/download/wasm-tools-1.0.54/wasm-tools-1.0.54-x86_64-linux.tar.gz +WASM_TOOLS = $(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL) .tar.gz)/wasm-tools +ADAPTER_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v16.0.0/wasi_snapshot_preview1.command.wasm +ADAPTER = $(DOWNDIR)/wasi_snapshot_preview1.command.wasm + +TO_DOWNLOAD = $(LIBC_TEST) $(LIBRT) $(WASMTIME) +ifeq ($(WASI_SNAPSHOT), preview2) +TO_DOWNLOAD += $(ADAPTER) $(WASM_TOOLS) +endif -download: $(LIBC_TEST) $(LIBRT) $(WASMTIME) +download: $(TO_DOWNLOAD) $(DOWNDIR): mkdir -p download @@ -42,6 +54,13 @@ $(WASMTIME): | $(DOWNDIR) wget --no-clobber --directory-prefix=$(DOWNDIR) $(WASMTIME_URL) tar --extract --file=$(DOWNDIR)/$(shell basename $(WASMTIME_URL)) --directory=$(DOWNDIR)/ +$(WASM_TOOLS): | $(DOWNDIR) + wget --no-clobber --directory-prefix=$(DOWNDIR) $(WASM_TOOLS_URL) + tar --extract --file=$(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL)) --directory=$(DOWNDIR)/ + +$(ADAPTER): | $(DOWNDIR) + wget --no-clobber --directory-prefix=$(DOWNDIR) $(ADAPTER_URL) + clean:: rm -rf download @@ -110,13 +129,18 @@ WASM_OBJS += $(INFRA_WASM_OBJS) DIRS := $(patsubst $(OBJDIR)/%/,%,$(sort $(dir $(WASM_OBJS)))) OBJDIRS := $(DIRS:%=$(OBJDIR)/%) +TARGET_TRIPLE = wasm32-wasi +ifeq ($(WASI_SNAPSHOT), preview2) +TARGET_TRIPLE = wasm32-wasi-preview2 +endif + # Allow $(CC) to be set from the command line; ?= doesn't work for CC because # make has a default value for it. ifeq ($(origin CC), default) CC := clang endif LDFLAGS ?= -CFLAGS ?= --target=wasm32-wasi --sysroot=../sysroot +CFLAGS ?= --target=$(TARGET_TRIPLE) --sysroot=../sysroot # Always include the `libc-test` infrastructure headers. CFLAGS += -I$(LIBC_TEST)/src/common @@ -129,7 +153,10 @@ build: download $(WASMS) $(WASMS): | $(OBJDIRS) $(OBJDIR)/%.wasm: $(OBJDIR)/%.wasm.o $(INFRA_WASM_OBJS) - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ +ifeq ($(WASI_SNAPSHOT), preview2) + $(WASM_TOOLS) component new --adapt $(ADAPTER) $@ -o $@ +endif $(WASM_OBJS): $(LIBC_TEST)/src/common/test.h | $(OBJDIRS) $(OBJDIR)/%.wasm.o: $(LIBC_TEST)/src/%.c @@ -144,6 +171,9 @@ clean:: ##### RUN ###################################################################### ENGINE ?= $(WASMTIME) run +ifeq ($(WASI_SNAPSHOT), preview2) +ENGINE += --wasm component-model +endif ERRS:=$(WASMS:%.wasm=%.wasm.err) # Use the provided Wasm engine to execute each test, emitting its output into