From 24c7bb7d46473b025c8f35a0cd990c89b7d7ab0f Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 12 Dec 2023 11:34:22 -0700 Subject: [PATCH] use separate include dirs for each target This allows each target to have a different set of header files under the same sysroot. Signed-off-by: Joel Dice --- .github/workflows/main.yml | 6 ++- Makefile | 6 ++- .../libc/sys/wasi_preview2/descriptor_table.c | 4 +- .../headers/public/__header_sys_socket.h | 1 + .../headers/public/__wasi_snapshot.h | 5 +++ libc-top-half/musl/include/sys/socket.h | 3 ++ test/Makefile | 39 +++++++++++++++++-- 7 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 libc-bottom-half/headers/public/__wasi_snapshot.h 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 0167f4a43..4abba786d 100644 --- a/Makefile +++ b/Makefile @@ -406,7 +406,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 @@ -681,6 +681,10 @@ include_dirs: # Remove selected header files. $(RM) $(patsubst %,$(SYSROOT_INC)/%,$(MUSL_OMIT_HEADERS)) +ifeq ($(WASI_SNAPSHOT), preview2) + printf '#ifndef __wasilibc_use_preview2\n#define __wasilibc_use_preview2\n#endif\n' \ + > "$(SYSROOT_INC)/__wasi_snapshot.h" +endif startup_files: include_dirs $(LIBC_BOTTOM_HALF_CRT_OBJS) # diff --git a/libc-bottom-half/cloudlibc/src/libc/sys/wasi_preview2/descriptor_table.c b/libc-bottom-half/cloudlibc/src/libc/sys/wasi_preview2/descriptor_table.c index f11da1ff7..da4a0d004 100644 --- a/libc-bottom-half/cloudlibc/src/libc/sys/wasi_preview2/descriptor_table.c +++ b/libc-bottom-half/cloudlibc/src/libc/sys/wasi_preview2/descriptor_table.c @@ -29,14 +29,14 @@ __attribute__((__import_module__("wasi_snapshot_preview1"), __import_name__("ada static bool wasi_preview1_adapter_open_badfd(int* fd) { - return __wasi_preview1_adapter_open_badfd((int32_t)fd) != 0; + return __wasi_preview1_adapter_open_badfd((int32_t)fd) == 0; } __attribute__((__import_module__("wasi_snapshot_preview1"), __import_name__("adapter_close_badfd"))) extern int32_t __wasi_preview1_adapter_close_badfd(int32_t); static bool wasi_preview1_adapter_close_badfd(int fd) { - return __wasi_preview1_adapter_close_badfd(fd) != 0; + return __wasi_preview1_adapter_close_badfd(fd) == 0; } /* diff --git a/libc-bottom-half/headers/public/__header_sys_socket.h b/libc-bottom-half/headers/public/__header_sys_socket.h index fb3c585ef..f39577468 100644 --- a/libc-bottom-half/headers/public/__header_sys_socket.h +++ b/libc-bottom-half/headers/public/__header_sys_socket.h @@ -1,6 +1,7 @@ #ifndef __wasilibc___header_sys_socket_h #define __wasilibc___header_sys_socket_h +#include <__wasi_snapshot.h> #include <__struct_msghdr.h> #include <__struct_sockaddr.h> #include <__struct_sockaddr_storage.h> diff --git a/libc-bottom-half/headers/public/__wasi_snapshot.h b/libc-bottom-half/headers/public/__wasi_snapshot.h new file mode 100644 index 000000000..26aa6b824 --- /dev/null +++ b/libc-bottom-half/headers/public/__wasi_snapshot.h @@ -0,0 +1,5 @@ +/* This is file is (practically) empty by default. The Makefile will replace it + with a non-empty version that defines `__wasilibc_use_preview2` if targeting + `wasm32-wasi-preview2`. + */ + diff --git a/libc-top-half/musl/include/sys/socket.h b/libc-top-half/musl/include/sys/socket.h index 2a478c248..67dee2588 100644 --- a/libc-top-half/musl/include/sys/socket.h +++ b/libc-top-half/musl/include/sys/socket.h @@ -1,5 +1,8 @@ #ifndef _SYS_SOCKET_H #define _SYS_SOCKET_H + +#include <__wasi_snapshot.h> + #ifdef __wasilibc_unmodified_upstream /* Use alternate WASI libc headers */ #else #include <__header_sys_socket.h> diff --git a/test/Makefile b/test/Makefile index 61e1756bc..9db4cf974 100644 --- a/test/Makefile +++ b/test/Makefile @@ -16,16 +16,29 @@ 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 +# TODO: switch to Wasmtime 16 once it's released +WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/dev/wasmtime-dev-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/dev/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 +55,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 +130,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 +154,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 +172,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