Skip to content

Commit

Permalink
add WASI Preview 2 bindings
Browse files Browse the repository at this point in the history
This adds C bindings generated from the `wasi:cli/[email protected]`
world, plus a makefile target to regenerate them from the WIT source files.

We'll use these bindings to call Preview 2 host functions when building for the
`wasm32-wasi-preview2` target.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Jan 4, 2024
1 parent 925ad6d commit ca0a98f
Show file tree
Hide file tree
Showing 8 changed files with 7,520 additions and 3 deletions.
77 changes: 75 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ MALLOC_IMPL ?= dlmalloc
BUILD_LIBC_TOP_HALF ?= yes
# The directory where we will store intermediate artifacts.
OBJDIR ?= build/$(TARGET_TRIPLE)
# The directory where we store files and tools for generating WASI Preview 2 bindings
BINDING_WORK_DIR ?= build/bindings
# URL from which to retrieve the WIT files used to generate the WASI Preview 2 bindings
WASI_CLI_URL ?= https://github.com/WebAssembly/wasi-cli/archive/refs/tags/v0.2.0-rc-2023-12-05.tar.gz
# URL from which to retrieve the `wit-bindgen` command used to generate the WASI Preview 2 bindings
WIT_BINDGEN_URL ?= https://github.com/bytecodealliance/wit-bindgen/releases/download/wit-bindgen-cli-0.16.0/wit-bindgen-v0.16.0-x86_64-linux.tar.gz

# When the length is no larger than this threshold, we consider the
# overhead of bulk memory opcodes to outweigh the performance benefit,
Expand Down Expand Up @@ -68,6 +74,19 @@ LIBC_BOTTOM_HALF_ALL_SOURCES = \
$(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) -name \*.c) \
$(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c))

ifeq ($(WASI_SNAPSHOT), preview1)
# Omit source files not relevant to WASI Preview 1. As we introduce files
# supporting `wasi-sockets` for `wasm32-wasi-preview2`, we'll add those files to
# this list.
LIBC_BOTTOM_HALF_OMIT_SOURCES := \
$(addprefix $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/libc/sys/, \
wasi/preview2.c \
)
LIBC_BOTTOM_HALF_ALL_SOURCES := $(filter-out $(LIBC_BOTTOM_HALF_OMIT_SOURCES),$(LIBC_BOTTOM_HALF_ALL_SOURCES))
# Omit preview2.h from include-all.c test.
INCLUDE_ALL_CLAUSES := -not -name preview2.h
endif

# FIXME(https://reviews.llvm.org/D85567) - due to a bug in LLD the weak
# references to a function defined in `chdir.c` only work if `chdir.c` is at the
# end of the archive, but once that LLD review lands and propagates into LLVM
Expand Down Expand Up @@ -741,7 +760,7 @@ check-symbols: startup_files libc
# Generate a test file that includes all public C header files.
#
cd "$(SYSROOT_INC)" && \
for header in $$(find . -type f -not -name mman.h -not -name signal.h -not -name times.h -not -name resource.h |grep -v /bits/ |grep -v /c++/); do \
for header in $$(find . -type f -not -name mman.h -not -name signal.h -not -name times.h -not -name resource.h $(INCLUDE_ALL_CLAUSES) |grep -v /bits/ |grep -v /c++/); do \
echo '#include <'$$header'>' | sed 's/\.\///' ; \
done |LC_ALL=C sort >$(SYSROOT_SHARE)/include-all.c ; \
cd - >/dev/null
Expand Down Expand Up @@ -816,8 +835,62 @@ install: finish
mkdir -p "$(INSTALL_DIR)"
cp -r "$(SYSROOT)/lib" "$(SYSROOT)/share" "$(SYSROOT)/include" "$(INSTALL_DIR)"

$(BINDING_WORK_DIR)/wasi-cli:
mkdir -p "$(BINDING_WORK_DIR)"
cd "$(BINDING_WORK_DIR)" && \
curl -L "$(WASI_CLI_URL)" -o wasi-cli.tar.gz && \
tar xf wasi-cli.tar.gz && \
mv wasi-cli-* wasi-cli

$(BINDING_WORK_DIR)/wit-bindgen:
mkdir -p "$(BINDING_WORK_DIR)"
cd "$(BINDING_WORK_DIR)" && \
curl -L "$(WIT_BINDGEN_URL)" -o wit-bindgen.tar.gz && \
tar xf wit-bindgen.tar.gz && \
mv wit-bindgen-* wit-bindgen

bindings: $(BINDING_WORK_DIR)/wasi-cli $(BINDING_WORK_DIR)/wit-bindgen
cd "$(BINDING_WORK_DIR)" && \
./wit-bindgen/wit-bindgen c --world wasi:cli/[email protected] \
--rename wasi:clocks/[email protected]=monotonic_clock \
--rename wasi:clocks/[email protected]=wall_clock \
--rename wasi:filesystem/[email protected]=filesystem_preopens \
--rename wasi:filesystem/[email protected]=filesystem \
--rename wasi:io/[email protected]=io_error \
--rename wasi:io/[email protected]=poll \
--rename wasi:io/[email protected]=streams \
--rename wasi:random/[email protected]=random_insecure_seed \
--rename wasi:random/[email protected]=random_insecure \
--rename wasi:random/[email protected]=random \
--rename wasi:sockets/[email protected]=instance_network \
--rename wasi:sockets/[email protected]=ip_name_lookup \
--rename wasi:sockets/[email protected]=network \
--rename wasi:sockets/[email protected]=tcp_create_socket \
--rename wasi:sockets/[email protected]=tcp \
--rename wasi:sockets/[email protected]=udp_create_socket \
--rename wasi:sockets/[email protected]=udp \
--rename wasi:cli/[email protected]=environment \
--rename wasi:cli/[email protected]=exit \
--rename wasi:cli/[email protected]=stdin \
--rename wasi:cli/[email protected]=stdout \
--rename wasi:cli/[email protected]=stderr \
--rename wasi:cli/[email protected]=terminal_input \
--rename wasi:cli/[email protected]=terminal_output \
--rename wasi:cli/[email protected]=terminal_stdin \
--rename wasi:cli/[email protected]=terminal_stdout \
--rename wasi:cli/[email protected]=terminal_stderr \
./wasi-cli/wit && \
mv imports.h ../../libc-bottom-half/headers/public/wasi/preview2.h && \
sed 's_#include "imports.h"_#include "wasi/preview2.h"_' \
< imports.c \
> ../../libc-bottom-half/cloudlibc/src/libc/sys/wasi/preview2.c && \
rm imports.c && \
mv imports_component_type.o \
../../libc-bottom-half/cloudlibc/src/libc/sys/wasi/preview2_component_type.o

clean:
$(RM) -r "$(BINDING_WORK_DIR)"
$(RM) -r "$(OBJDIR)"
$(RM) -r "$(SYSROOT)"

.PHONY: default startup_files libc libc_so finish install include_dirs clean check-symbols
.PHONY: default startup_files libc libc_so finish install include_dirs clean check-symbols bindings
Loading

0 comments on commit ca0a98f

Please sign in to comment.