@@ -23,6 +23,17 @@ MALLOC_IMPL ?= dlmalloc
2323BUILD_LIBC_TOP_HALF ?= yes
2424# The directory where we will store intermediate artifacts.
2525OBJDIR ?= build/$(TARGET_TRIPLE )
26+ # The directory where we store files and tools for generating WASI Preview 2 bindings
27+ BINDING_WORK_DIR ?= build/bindings
28+ # URL from which to retrieve the WIT files used to generate the WASI Preview 2 bindings
29+ WASI_CLI_URL ?= https://github.com/WebAssembly/wasi-cli/archive/refs/tags/v0.2.0-rc-2023-12-05.tar.gz
30+ # URL from which to retrieve the `wit-bindgen` command used to generate the WASI
31+ # Preview 2 bindings.
32+ #
33+ # TODO: Switch to bytecodealliance/wit-bindgen 0.17.0 once it's released (which
34+ # will include https://github.com/bytecodealliance/wit-bindgen/pull/804 and
35+ # https://github.com/bytecodealliance/wit-bindgen/pull/805, which we rely on)
36+ WIT_BINDGEN_URL ?= https://github.com/dicej/wit-bindgen/releases/download/wit-bindgen-cli-0.17.0-dicej-pre0/wit-bindgen-v0.17.0-dicej-pre0-x86_64-linux.tar.gz
2637
2738# When the length is no larger than this threshold, we consider the
2839# overhead of bulk memory opcodes to outweigh the performance benefit,
@@ -37,7 +48,7 @@ BULK_MEMORY_THRESHOLD ?= 32
3748# Set the default WASI target triple.
3849TARGET_TRIPLE = wasm32-wasi
3950
40- # Threaded version necessitates a different traget , as objects from different
51+ # Threaded version necessitates a different target , as objects from different
4152# targets can't be mixed together while linking.
4253ifeq ($(THREAD_MODEL ) , posix)
4354TARGET_TRIPLE = wasm32-wasi-threads
@@ -68,6 +79,16 @@ LIBC_BOTTOM_HALF_ALL_SOURCES = \
6879 $(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC ) -name \* .c) \
6980 $(shell find $(LIBC_BOTTOM_HALF_SOURCES ) -name \* .c) )
7081
82+ ifeq ($(WASI_SNAPSHOT ) , preview1)
83+ # Omit source files not relevant to WASI Preview 1. As we introduce files
84+ # supporting `wasi-sockets` for `wasm32-wasi-preview2`, we'll add those files to
85+ # this list.
86+ LIBC_BOTTOM_HALF_OMIT_SOURCES := $(LIBC_BOTTOM_HALF_SOURCES ) /preview2.c
87+ LIBC_BOTTOM_HALF_ALL_SOURCES := $(filter-out $(LIBC_BOTTOM_HALF_OMIT_SOURCES ) ,$(LIBC_BOTTOM_HALF_ALL_SOURCES ) )
88+ # Omit preview2.h from include-all.c test.
89+ INCLUDE_ALL_CLAUSES := -not -name preview2.h
90+ endif
91+
7192# FIXME(https://reviews.llvm.org/D85567) - due to a bug in LLD the weak
7293# references to a function defined in `chdir.c` only work if `chdir.c` is at the
7394# end of the archive, but once that LLD review lands and propagates into LLVM
@@ -741,7 +762,7 @@ check-symbols: startup_files libc
741762 # Generate a test file that includes all public C header files.
742763 #
743764 cd "$(SYSROOT_INC)" && \
744- 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 \
765+ 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 \
745766 echo '# include <'$$header'>' | sed 's/\.\///' ; \
746767 done |LC_ALL=C sort >$(SYSROOT_SHARE)/include-all.c ; \
747768 cd - >/dev/null
@@ -816,8 +837,65 @@ install: finish
816837 mkdir -p " $( INSTALL_DIR) "
817838 cp -r " $( SYSROOT) /lib" " $( SYSROOT) /share" " $( SYSROOT) /include" " $( INSTALL_DIR) "
818839
840+ $(BINDING_WORK_DIR ) /wasi-cli :
841+ mkdir -p " $( BINDING_WORK_DIR) "
842+ cd " $( BINDING_WORK_DIR) " && \
843+ curl -L " $( WASI_CLI_URL) " -o wasi-cli.tar.gz && \
844+ tar xf wasi-cli.tar.gz && \
845+ mv wasi-cli-* wasi-cli
846+
847+ $(BINDING_WORK_DIR ) /wit-bindgen :
848+ mkdir -p " $( BINDING_WORK_DIR) "
849+ cd " $( BINDING_WORK_DIR) " && \
850+ curl -L " $( WIT_BINDGEN_URL) " -o wit-bindgen.tar.gz && \
851+ tar xf wit-bindgen.tar.gz && \
852+ mv wit-bindgen-* wit-bindgen
853+
854+ bindings : $(BINDING_WORK_DIR ) /wasi-cli $(BINDING_WORK_DIR ) /wit-bindgen
855+ cd " $( BINDING_WORK_DIR) " && \
856+ ./wit-bindgen/wit-bindgen c \
857+ --rename-world preview2 \
858+ --type-section-suffix __wasi_libc \
859+ --world wasi:cli/imports@0.2.0-rc-2023-12-05 \
860+ --rename wasi:clocks/monotonic-clock@0.2.0-rc-2023-11-10=monotonic_clock \
861+ --rename wasi:clocks/wall-clock@0.2.0-rc-2023-11-10=wall_clock \
862+ --rename wasi:filesystem/preopens@0.2.0-rc-2023-11-10=filesystem_preopens \
863+ --rename wasi:filesystem/types@0.2.0-rc-2023-11-10=filesystem \
864+ --rename wasi:io/error@0.2.0-rc-2023-11-10=io_error \
865+ --rename wasi:io/poll@0.2.0-rc-2023-11-10=poll \
866+ --rename wasi:io/streams@0.2.0-rc-2023-11-10=streams \
867+ --rename wasi:random/insecure-seed@0.2.0-rc-2023-11-10=random_insecure_seed \
868+ --rename wasi:random/insecure@0.2.0-rc-2023-11-10=random_insecure \
869+ --rename wasi:random/random@0.2.0-rc-2023-11-10=random \
870+ --rename wasi:sockets/instance-network@0.2.0-rc-2023-11-10=instance_network \
871+ --rename wasi:sockets/ip-name-lookup@0.2.0-rc-2023-11-10=ip_name_lookup \
872+ --rename wasi:sockets/network@0.2.0-rc-2023-11-10=network \
873+ --rename wasi:sockets/tcp-create-socket@0.2.0-rc-2023-11-10=tcp_create_socket \
874+ --rename wasi:sockets/tcp@0.2.0-rc-2023-11-10=tcp \
875+ --rename wasi:sockets/udp-create-socket@0.2.0-rc-2023-11-10=udp_create_socket \
876+ --rename wasi:sockets/udp@0.2.0-rc-2023-11-10=udp \
877+ --rename wasi:cli/environment@0.2.0-rc-2023-12-05=environment \
878+ --rename wasi:cli/exit@0.2.0-rc-2023-12-05=exit \
879+ --rename wasi:cli/stdin@0.2.0-rc-2023-12-05=stdin \
880+ --rename wasi:cli/stdout@0.2.0-rc-2023-12-05=stdout \
881+ --rename wasi:cli/stderr@0.2.0-rc-2023-12-05=stderr \
882+ --rename wasi:cli/terminal-input@0.2.0-rc-2023-12-05=terminal_input \
883+ --rename wasi:cli/terminal-output@0.2.0-rc-2023-12-05=terminal_output \
884+ --rename wasi:cli/terminal-stdin@0.2.0-rc-2023-12-05=terminal_stdin \
885+ --rename wasi:cli/terminal-stdout@0.2.0-rc-2023-12-05=terminal_stdout \
886+ --rename wasi:cli/terminal-stderr@0.2.0-rc-2023-12-05=terminal_stderr \
887+ ./wasi-cli/wit && \
888+ mv preview2.h ../../libc-bottom-half/headers/public/wasi/ && \
889+ mv preview2_component_type.o ../../libc-bottom-half/sources && \
890+ sed ' s_#include "preview2.h"_#include "wasi/preview2.h"_' \
891+ < preview2.c \
892+ > ../../libc-bottom-half/sources/preview2.c && \
893+ rm preview2.c
894+
895+
819896clean :
897+ $(RM ) -r " $( BINDING_WORK_DIR) "
820898 $(RM ) -r " $( OBJDIR) "
821899 $(RM ) -r " $( SYSROOT) "
822900
823- .PHONY : default startup_files libc libc_so finish install include_dirs clean check-symbols
901+ .PHONY : default startup_files libc libc_so finish install include_dirs clean check-symbols bindings
0 commit comments