Skip to content

Commit 5447ce5

Browse files
committed
add wasm32-wasi-preview2 target
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]>
1 parent 4bac52e commit 5447ce5

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ jobs:
8484

8585
- name: Build libc
8686
shell: bash
87-
run: make -j4
87+
run: |
88+
make -j4
89+
WASI_SNAPSHOT=preview2 make -j4
8890
8991
- name: Test
9092
shell: bash
@@ -98,6 +100,8 @@ jobs:
98100
mkdir -p $WASI_DIR
99101
cp download/lib/wasi/libclang_rt.builtins-wasm32.a $WASI_DIR
100102
make test
103+
rm -r build
104+
WASI_SNAPSHOT=preview2 make test
101105
# The older version of Clang does not provide the expected symbol for the
102106
# test entrypoints: `undefined symbol: __main_argc_argv`.
103107
# The older (<15.0.7) version of wasm-ld does not provide `__heap_end`,

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ SYSROOT ?= $(CURDIR)/sysroot
1515
INSTALL_DIR ?= /usr/local
1616
# single or posix; note that pthread support is still a work-in-progress.
1717
THREAD_MODEL ?= single
18+
# preview1 or preview2; the latter is not (yet) compatible with multithreading
19+
WASI_SNAPSHOT ?= preview1
1820
# dlmalloc or none
1921
MALLOC_IMPL ?= dlmalloc
2022
# yes or no
@@ -41,6 +43,10 @@ ifeq ($(THREAD_MODEL), posix)
4143
TARGET_TRIPLE = wasm32-wasi-threads
4244
endif
4345

46+
ifeq ($(WASI_SNAPSHOT), preview2)
47+
TARGET_TRIPLE = wasm32-wasi-preview2
48+
endif
49+
4450
BUILTINS_LIB ?= $(shell ${CC} --print-libgcc-file-name)
4551

4652
# 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))
386392
# These variables describe the locations of various files and
387393
# directories in the generated sysroot tree.
388394
SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE)
389-
SYSROOT_INC = $(SYSROOT)/include
395+
SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE)
390396
SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE)
391397

392398
# Files from musl's include directory that we don't want to install in the
@@ -511,7 +517,7 @@ PIC_OBJS = \
511517
# to CC. This is a workaround for a Windows command line size limitation. See
512518
# the `%.a` rule below for details.
513519
$(SYSROOT_LIB)/%.so: $(OBJDIR)/%.so.a $(BUILTINS_LIB)
514-
$(CC) -nodefaultlibs -shared --sysroot=$(SYSROOT) \
520+
$(CC) --target=$(TARGET_TRIPLE) -nodefaultlibs -shared --sysroot=$(SYSROOT) \
515521
-o $@ -Wl,--whole-archive $< -Wl,--no-whole-archive $(BUILTINS_LIB)
516522

517523
$(OBJDIR)/libc.so.a: $(LIBC_SO_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_SO_OBJS)

test/Makefile

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,28 @@ test: run
1616
OBJDIR ?= $(CURDIR)/build
1717
DOWNDIR ?= $(CURDIR)/download
1818

19+
# preview1 or preview2
20+
WASI_SNAPSHOT ?= preview1
21+
1922
##### DOWNLOAD #################################################################
2023

2124
LIBC_TEST_URL ?= https://github.com/bytecodealliance/libc-test
2225
LIBC_TEST = $(DOWNDIR)/libc-test
2326
LIBRT_URL ?= https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-16/libclang_rt.builtins-wasm32-wasi-16.0.tar.gz
2427
LIBRT = $(DOWNDIR)/lib/wasi/libclang_rt.builtins-wasm32.a
25-
WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v3.0.0/wasmtime-v3.0.0-x86_64-linux.tar.xz
28+
WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v16.0.0/wasmtime-v16.0.0-x86_64-linux.tar.xz
2629
WASMTIME = $(DOWNDIR)/$(shell basename $(WASMTIME_URL) .tar.xz)/wasmtime
30+
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
31+
WASM_TOOLS = $(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL) .tar.gz)/wasm-tools
32+
ADAPTER_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v16.0.0/wasi_snapshot_preview1.command.wasm
33+
ADAPTER = $(DOWNDIR)/wasi_snapshot_preview1.command.wasm
34+
35+
TO_DOWNLOAD = $(LIBC_TEST) $(LIBRT) $(WASMTIME)
36+
ifeq ($(WASI_SNAPSHOT), preview2)
37+
TO_DOWNLOAD += $(ADAPTER) $(WASM_TOOLS)
38+
endif
2739

28-
download: $(LIBC_TEST) $(LIBRT) $(WASMTIME)
40+
download: $(TO_DOWNLOAD)
2941

3042
$(DOWNDIR):
3143
mkdir -p download
@@ -42,6 +54,13 @@ $(WASMTIME): | $(DOWNDIR)
4254
wget --no-clobber --directory-prefix=$(DOWNDIR) $(WASMTIME_URL)
4355
tar --extract --file=$(DOWNDIR)/$(shell basename $(WASMTIME_URL)) --directory=$(DOWNDIR)/
4456

57+
$(WASM_TOOLS): | $(DOWNDIR)
58+
wget --no-clobber --directory-prefix=$(DOWNDIR) $(WASM_TOOLS_URL)
59+
tar --extract --file=$(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL)) --directory=$(DOWNDIR)/
60+
61+
$(ADAPTER): | $(DOWNDIR)
62+
wget --no-clobber --directory-prefix=$(DOWNDIR) $(ADAPTER_URL)
63+
4564
clean::
4665
rm -rf download
4766

@@ -110,13 +129,18 @@ WASM_OBJS += $(INFRA_WASM_OBJS)
110129
DIRS := $(patsubst $(OBJDIR)/%/,%,$(sort $(dir $(WASM_OBJS))))
111130
OBJDIRS := $(DIRS:%=$(OBJDIR)/%)
112131

132+
TARGET_TRIPLE = wasm32-wasi
133+
ifeq ($(WASI_SNAPSHOT), preview2)
134+
TARGET_TRIPLE = wasm32-wasi-preview2
135+
endif
136+
113137
# Allow $(CC) to be set from the command line; ?= doesn't work for CC because
114138
# make has a default value for it.
115139
ifeq ($(origin CC), default)
116140
CC := clang
117141
endif
118142
LDFLAGS ?=
119-
CFLAGS ?= --target=wasm32-wasi --sysroot=../sysroot
143+
CFLAGS ?= --target=$(TARGET_TRIPLE) --sysroot=../sysroot
120144
# Always include the `libc-test` infrastructure headers.
121145
CFLAGS += -I$(LIBC_TEST)/src/common
122146

@@ -129,7 +153,10 @@ build: download $(WASMS)
129153

130154
$(WASMS): | $(OBJDIRS)
131155
$(OBJDIR)/%.wasm: $(OBJDIR)/%.wasm.o $(INFRA_WASM_OBJS)
132-
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
156+
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
157+
ifeq ($(WASI_SNAPSHOT), preview2)
158+
$(WASM_TOOLS) component new --adapt $(ADAPTER) $@ -o $@
159+
endif
133160

134161
$(WASM_OBJS): $(LIBC_TEST)/src/common/test.h | $(OBJDIRS)
135162
$(OBJDIR)/%.wasm.o: $(LIBC_TEST)/src/%.c
@@ -144,6 +171,9 @@ clean::
144171
##### RUN ######################################################################
145172

146173
ENGINE ?= $(WASMTIME) run
174+
ifeq ($(WASI_SNAPSHOT), preview2)
175+
ENGINE += --wasm component-model
176+
endif
147177
ERRS:=$(WASMS:%.wasm=%.wasm.err)
148178

149179
# Use the provided Wasm engine to execute each test, emitting its output into

0 commit comments

Comments
 (0)