Skip to content

Commit

Permalink
add wasm32-wasi-preview2 target
Browse files Browse the repository at this point in the history
Currently, this is identical to the `wasm32-wasi` in all but name.  See WebAssembly#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 WebAssembly#447 for further details.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Dec 20, 2023
1 parent 4bac52e commit 5447ce5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`,
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 34 additions & 4 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5447ce5

Please sign in to comment.