From 2b45ae2a4a2067c16a43446f4aebcc8f388a1ca7 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 13 Feb 2024 16:30:50 +0900 Subject: [PATCH 01/27] ports/unix/Makefile: Don't assume Linux by default. Motivation: allow to override this with "make UNAME_S=wasi" Signed-off-by: YAMAMOTO Takashi --- ports/unix/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/Makefile b/ports/unix/Makefile index f02e6c6355ab6..0f1d255812f51 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -103,7 +103,7 @@ CC = clang endif # Use clang syntax for map file LDFLAGS_ARCH = -Wl,-map,$@.map -Wl,-dead_strip -else +else ifeq ($(UNAME_S),Linux) # Use gcc syntax for map file LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections endif From 194e015e2aab6992265818cd873d1f28a6d1a03e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 13 Feb 2024 16:35:28 +0900 Subject: [PATCH 02/27] ports/unix/unix_mphal.c: Disable signal mask stuff for WASI. WASI dosen't have signals. Signed-off-by: YAMAMOTO Takashi --- ports/unix/unix_mphal.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index 5f5abc2e056f6..3ea3107d54614 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -43,7 +43,13 @@ #endif #endif -#ifndef _WIN32 +#if defined(_WIN32) || defined(__wasi__) +#define HAS_SIGNAL 0 +#else +#define HAS_SIGNAL 1 +#endif + +#if HAS_SIGNAL != 0 #include static void sighandler(int signum) { @@ -75,7 +81,7 @@ static void sighandler(int signum) { void mp_hal_set_interrupt_char(char c) { // configure terminal settings to (not) let ctrl-C through if (c == CHAR_CTRL_C) { - #ifndef _WIN32 + #if HAS_SIGNAL != 0 // enable signal handler struct sigaction sa; sa.sa_flags = 0; @@ -84,7 +90,7 @@ void mp_hal_set_interrupt_char(char c) { sigaction(SIGINT, &sa, NULL); #endif } else { - #ifndef _WIN32 + #if HAS_SIGNAL != 0 // disable signal handler struct sigaction sa; sa.sa_flags = 0; From d26e552497b7daf46f60b1cfe3504c7514866d52 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 15 Feb 2024 16:09:44 +0900 Subject: [PATCH 03/27] ports/unix: Add build-wasi.sh script. For now, mainly for documentation purpose. As you can see, this involves a lot of experimental things. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 ports/unix/build-wasi.sh diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh new file mode 100755 index 0000000000000..1f7acb8d9a184 --- /dev/null +++ b/ports/unix/build-wasi.sh @@ -0,0 +1,70 @@ +#! /bin/sh + +# prerequisites +# +# https://github.com/WebAssembly/wasi-libc/pull/467 +# https://github.com/WebAssembly/wasi-libc/pull/473 +# https://github.com/WebAssembly/binaryen/pull/6294 +# https://github.com/WebAssembly/binaryen/pull/6259 +# +# WASI_SDK: wasi-sdk 21.0 +# WASM_OPT: binaryen wasm-opt built with the above patches +# WASI_SYSROOT: wasi-libc built with the above patches +# ../../../micropython-ulab: check out https://github.com/v923z/micropython-ulab + +WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-21.0} +WASI_SYSROOT=${WASI_SYSROOT:-${HOME}/git/wasi-libc/sysroot} +BINARYEN_BIN=${BINARYEN_BIN:-${HOME}/git/wasm/binaryen/b/bin} +WASM_OPT=${WASM_OPT:-${BINARYEN_BIN}/wasm-opt} + +# MICROPY_PY_THREAD: GC uses signals, which is not available on wasi. + +# the following things seem building well. i disabled them just because +# i was not intererested in them. +# MICROPY_VFS_FAT +# MICROPY_VFS_LFS1 +# MICROPY_VFS_LFS2 + +# micropython-ulab was added just as an example of user C module. +# you can remove it if you don't use it. + +CFLAGS="-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -mllvm -wasm-enable-sjlj" \ +LDFLAGS="-lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman" \ +make \ +CC="${WASI_SDK}/bin/clang --sysroot=${WASI_SYSROOT}" \ +LD="${WASI_SDK}/bin/wasm-ld" \ +STRIP="${WASI_SDK}/bin/strip" \ +SIZE="${WASI_SDK}/bin/size" \ +UNAME_S=wasi \ +MICROPY_PY_FFI=0 \ +MICROPY_PY_THREAD=0 \ +MICROPY_PY_SOCKET=0 \ +MICROPY_PY_SSL=0 \ +MICROPY_PY_BTREE=0 \ +MICROPY_PY_TERMIOS=0 \ +MICROPY_USE_READLINE=0 \ +MICROPY_VFS_FAT=0 \ +MICROPY_VFS_LFS1=0 \ +MICROPY_VFS_LFS2=0 \ +USER_C_MODULES=../../../micropython-ulab \ +"$@" + +PROG=build-standard/micropython + +# We doesn't have a way to scan GC roots like WASM locals. +# Hopefully --spill-pointers can workaround it. +${WASM_OPT} \ +--spill-pointers \ +-o ${PROG}.spilled ${PROG} + +# LLVM still uses the older version of EH proposal. +# Convert to the latest version of EH proposal. +${WASM_OPT} \ +--translate-to-new-eh --enable-exception-handling \ +-o ${PROG}.spilled.neweh ${PROG}.spilled + +# now you can run it with EH-enabled runtimes. +# eg. (using the latest EH) +# toywasm --wasi build-standard/micropython.spilled.neweh +# eg. (using the old EH) +# iwasm build-standard/micropython.spilled From 3ff3f3d4848521f4aa9d4bc88ce6ffa7b9c7a021 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 17:34:10 +0900 Subject: [PATCH 04/27] ports/unix: Add wasi VARIANT. Start from a copy of standard. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/wasi/manifest.py | 3 ++ ports/unix/variants/wasi/mpconfigvariant.h | 31 +++++++++++++++++++++ ports/unix/variants/wasi/mpconfigvariant.mk | 3 ++ 3 files changed, 37 insertions(+) create mode 100644 ports/unix/variants/wasi/manifest.py create mode 100644 ports/unix/variants/wasi/mpconfigvariant.h create mode 100644 ports/unix/variants/wasi/mpconfigvariant.mk diff --git a/ports/unix/variants/wasi/manifest.py b/ports/unix/variants/wasi/manifest.py new file mode 100644 index 0000000000000..d27bf3c47fe92 --- /dev/null +++ b/ports/unix/variants/wasi/manifest.py @@ -0,0 +1,3 @@ +include("$(PORT_DIR)/variants/manifest.py") + +include("$(MPY_DIR)/extmod/asyncio") diff --git a/ports/unix/variants/wasi/mpconfigvariant.h b/ports/unix/variants/wasi/mpconfigvariant.h new file mode 100644 index 0000000000000..75201e9abc8d6 --- /dev/null +++ b/ports/unix/variants/wasi/mpconfigvariant.h @@ -0,0 +1,31 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Set base feature level. +#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES) + +// Enable extra Unix features. +#include "../mpconfigvariant_common.h" diff --git a/ports/unix/variants/wasi/mpconfigvariant.mk b/ports/unix/variants/wasi/mpconfigvariant.mk new file mode 100644 index 0000000000000..c91db1aa10ff9 --- /dev/null +++ b/ports/unix/variants/wasi/mpconfigvariant.mk @@ -0,0 +1,3 @@ +# This is the default variant when you `make` the Unix port. + +FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py From 355f987d1a68eff62d7190b179ee9f90388b4fcc Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 17:35:39 +0900 Subject: [PATCH 05/27] ports/unix: Make MICROPY_PY_SELECT_POSIX_OPTIMISATIONS overridable. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/mpconfigvariant_common.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/unix/variants/mpconfigvariant_common.h b/ports/unix/variants/mpconfigvariant_common.h index cea0397414325..717260ac0470a 100644 --- a/ports/unix/variants/mpconfigvariant_common.h +++ b/ports/unix/variants/mpconfigvariant_common.h @@ -111,7 +111,9 @@ #endif // The "select" module is enabled by default, but disable select.select(). +#ifndef MICROPY_PY_SELECT_POSIX_OPTIMISATIONS #define MICROPY_PY_SELECT_POSIX_OPTIMISATIONS (1) +#endif #define MICROPY_PY_SELECT_SELECT (0) // Enable the "websocket" module. From 6f0f91dcb3891ca110a8adce865376ebe409bc10 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 17:38:31 +0900 Subject: [PATCH 06/27] ports/unix: Make MICROPY_PY_OS_SYSTEM overridable. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/mpconfigvariant_common.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/unix/variants/mpconfigvariant_common.h b/ports/unix/variants/mpconfigvariant_common.h index 717260ac0470a..10c20630ae003 100644 --- a/ports/unix/variants/mpconfigvariant_common.h +++ b/ports/unix/variants/mpconfigvariant_common.h @@ -95,7 +95,9 @@ #define MICROPY_PY_OS_INCLUDEFILE "ports/unix/modos.c" #define MICROPY_PY_OS_ERRNO (1) #define MICROPY_PY_OS_GETENV_PUTENV_UNSETENV (1) +#ifndef MICROPY_PY_OS_SYSTEM #define MICROPY_PY_OS_SYSTEM (1) +#endif #define MICROPY_PY_OS_URANDOM (1) // Enable the unix-specific "time" module. From 9822893c3789c274e7981185e22fbc0febbfe1e0 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 17:39:53 +0900 Subject: [PATCH 07/27] ports/unix/variants/wasi: Disable MICROPY_PY_SELECT_POSIX_OPTIMISATIONS. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/wasi/mpconfigvariant.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/unix/variants/wasi/mpconfigvariant.h b/ports/unix/variants/wasi/mpconfigvariant.h index 75201e9abc8d6..e54ba42cf3395 100644 --- a/ports/unix/variants/wasi/mpconfigvariant.h +++ b/ports/unix/variants/wasi/mpconfigvariant.h @@ -27,5 +27,8 @@ // Set base feature level. #define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES) +// WASI has different values for POLL constants. +#define MICROPY_PY_SELECT_POSIX_OPTIMISATIONS (0) + // Enable extra Unix features. #include "../mpconfigvariant_common.h" From cb1127215ec8b454eabbfe107ad83d8fb1e464f9 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 17:41:37 +0900 Subject: [PATCH 08/27] ports/unix/variants/wasi: Disable MICROPY_PY_OS_SYSTEM. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/wasi/mpconfigvariant.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/unix/variants/wasi/mpconfigvariant.h b/ports/unix/variants/wasi/mpconfigvariant.h index e54ba42cf3395..e09b3f8b587c5 100644 --- a/ports/unix/variants/wasi/mpconfigvariant.h +++ b/ports/unix/variants/wasi/mpconfigvariant.h @@ -30,5 +30,8 @@ // WASI has different values for POLL constants. #define MICROPY_PY_SELECT_POSIX_OPTIMISATIONS (0) +// WASI doesn't have executable or process. +#define MICROPY_PY_OS_SYSTEM (0) + // Enable extra Unix features. #include "../mpconfigvariant_common.h" From f50795dbc8c252de4bb75d42e5b35e2bb89e5891 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 17:42:36 +0900 Subject: [PATCH 09/27] ports/unix/variants/wasi: Disable MICROPY_PY_SYS_EXECUTABLE. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/wasi/mpconfigvariant.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/unix/variants/wasi/mpconfigvariant.h b/ports/unix/variants/wasi/mpconfigvariant.h index e09b3f8b587c5..95d0a47fe89ab 100644 --- a/ports/unix/variants/wasi/mpconfigvariant.h +++ b/ports/unix/variants/wasi/mpconfigvariant.h @@ -32,6 +32,7 @@ // WASI doesn't have executable or process. #define MICROPY_PY_OS_SYSTEM (0) +#define MICROPY_PY_SYS_EXECUTABLE (0) // Enable extra Unix features. #include "../mpconfigvariant_common.h" From 7a88583ad41d080e3d1bf4ad19c79f9c1d2c0eec Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 17:46:05 +0900 Subject: [PATCH 10/27] ports/unix: Make MICROPY_PY_SYS_EXECUTABLE overridable. Signed-off-by: YAMAMOTO Takashi --- ports/unix/mpconfigport.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 21ce75a351e98..44b3f74c8c5ee 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -155,7 +155,9 @@ typedef long mp_off_t; #define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0) // Enable sys.executable. +#ifndef MICROPY_PY_SYS_EXECUTABLE #define MICROPY_PY_SYS_EXECUTABLE (1) +#endif #define MICROPY_PY_SOCKET_LISTEN_BACKLOG_DEFAULT (SOMAXCONN < 128 ? SOMAXCONN : 128) From 7374363039ea900f3b3add7f867720c3cdc3136f Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 18:15:10 +0900 Subject: [PATCH 11/27] ports/unix/variants/wasi/mpconfigvariant.mk: Disable several things. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/wasi/mpconfigvariant.mk | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ports/unix/variants/wasi/mpconfigvariant.mk b/ports/unix/variants/wasi/mpconfigvariant.mk index c91db1aa10ff9..b70151b9c41fa 100644 --- a/ports/unix/variants/wasi/mpconfigvariant.mk +++ b/ports/unix/variants/wasi/mpconfigvariant.mk @@ -1,3 +1,28 @@ # This is the default variant when you `make` the Unix port. FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py + +# WASI doesn't have FFI +MICROPY_PY_FFI=0 + +# When threading is enabled, micropython GC uses signals, which is +# not available on WASI. +MICROPY_PY_THREAD=0 + +# Disable for now because network support is limited in WASI. +MICROPY_PY_SOCKET=0 +MICROPY_PY_SSL=0 + +# ../../lib/berkeley-db-1.xx/PORT/include/db.h:40:10: +# fatal error: 'sys/cdefs.h' file not found +MICROPY_PY_BTREE=0 + +# WASI doesn't have termios +MICROPY_PY_TERMIOS=0 +MICROPY_USE_READLINE=0 + +# The following things might just work as they are. +# Disabled for now because I'm not interested in them. +MICROPY_VFS_FAT=0 +MICROPY_VFS_LFS1=0 +MICROPY_VFS_LFS2=0 From ab170342e4a5b2dd3c9369fd00294e0a348bd87d Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 18:15:51 +0900 Subject: [PATCH 12/27] ports/unix/build-wasi.sh: Switch to use VARIANT. VARIANT=wasi. Also remove hardcoded ulab. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 1f7acb8d9a184..056846b39424e 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -10,24 +10,12 @@ # WASI_SDK: wasi-sdk 21.0 # WASM_OPT: binaryen wasm-opt built with the above patches # WASI_SYSROOT: wasi-libc built with the above patches -# ../../../micropython-ulab: check out https://github.com/v923z/micropython-ulab WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-21.0} WASI_SYSROOT=${WASI_SYSROOT:-${HOME}/git/wasi-libc/sysroot} BINARYEN_BIN=${BINARYEN_BIN:-${HOME}/git/wasm/binaryen/b/bin} WASM_OPT=${WASM_OPT:-${BINARYEN_BIN}/wasm-opt} -# MICROPY_PY_THREAD: GC uses signals, which is not available on wasi. - -# the following things seem building well. i disabled them just because -# i was not intererested in them. -# MICROPY_VFS_FAT -# MICROPY_VFS_LFS1 -# MICROPY_VFS_LFS2 - -# micropython-ulab was added just as an example of user C module. -# you can remove it if you don't use it. - CFLAGS="-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -mllvm -wasm-enable-sjlj" \ LDFLAGS="-lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman" \ make \ @@ -36,20 +24,10 @@ LD="${WASI_SDK}/bin/wasm-ld" \ STRIP="${WASI_SDK}/bin/strip" \ SIZE="${WASI_SDK}/bin/size" \ UNAME_S=wasi \ -MICROPY_PY_FFI=0 \ -MICROPY_PY_THREAD=0 \ -MICROPY_PY_SOCKET=0 \ -MICROPY_PY_SSL=0 \ -MICROPY_PY_BTREE=0 \ -MICROPY_PY_TERMIOS=0 \ -MICROPY_USE_READLINE=0 \ -MICROPY_VFS_FAT=0 \ -MICROPY_VFS_LFS1=0 \ -MICROPY_VFS_LFS2=0 \ -USER_C_MODULES=../../../micropython-ulab \ +VARIANT=wasi \ "$@" -PROG=build-standard/micropython +PROG=build-wasi/micropython # We doesn't have a way to scan GC roots like WASM locals. # Hopefully --spill-pointers can workaround it. @@ -65,6 +43,6 @@ ${WASM_OPT} \ # now you can run it with EH-enabled runtimes. # eg. (using the latest EH) -# toywasm --wasi build-standard/micropython.spilled.neweh +# toywasm --wasi build-wasi/micropython.spilled.neweh # eg. (using the old EH) -# iwasm build-standard/micropython.spilled +# iwasm build-wasi/micropython.spilled From 70bf5238da8a9c5febf55cbcddf840c4e4b05b14 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 16 Feb 2024 18:25:34 +0900 Subject: [PATCH 13/27] ports/unix/variants/wasi/mpconfigvariant.mk: Update a comment. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/wasi/mpconfigvariant.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/unix/variants/wasi/mpconfigvariant.mk b/ports/unix/variants/wasi/mpconfigvariant.mk index b70151b9c41fa..c78d9cc15b2ef 100644 --- a/ports/unix/variants/wasi/mpconfigvariant.mk +++ b/ports/unix/variants/wasi/mpconfigvariant.mk @@ -1,4 +1,4 @@ -# This is the default variant when you `make` the Unix port. +# Build for WASI FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py From 9b549c3bcd383f49a16683328945e9d9ae19615d Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 2 Apr 2024 15:29:16 +0900 Subject: [PATCH 14/27] ports/unix/build-wasi.sh: Update toolchain patches. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 056846b39424e..9a99fca3c93d1 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -2,24 +2,30 @@ # prerequisites # -# https://github.com/WebAssembly/wasi-libc/pull/467 -# https://github.com/WebAssembly/wasi-libc/pull/473 -# https://github.com/WebAssembly/binaryen/pull/6294 -# https://github.com/WebAssembly/binaryen/pull/6259 +# https://github.com/WebAssembly/wasi-libc/pull/483 (wasi-sdk 22.0?) +# https://github.com/WebAssembly/wasi-libc/pull/473 (wasi-sdk 22.0?) +# https://github.com/WebAssembly/binaryen/pull/6294 (version_117) +# https://github.com/WebAssembly/binaryen/pull/6259 (version_117) +# https://github.com/llvm/llvm-project/pull/84137 (LLVM 19?) # # WASI_SDK: wasi-sdk 21.0 # WASM_OPT: binaryen wasm-opt built with the above patches # WASI_SYSROOT: wasi-libc built with the above patches +# CLANG: clang built with the above patches WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-21.0} WASI_SYSROOT=${WASI_SYSROOT:-${HOME}/git/wasi-libc/sysroot} BINARYEN_BIN=${BINARYEN_BIN:-${HOME}/git/wasm/binaryen/b/bin} WASM_OPT=${WASM_OPT:-${BINARYEN_BIN}/wasm-opt} +RESOURCE_DIR=$(${WASI_SDK}/bin/clang --print-resource-dir) +LLVM_BUILD_DIR=${LLVM_BUILD_DIR:-/Volumes/PortableSSD/llvm/build} +CLANG=${CLANG:-${LLVM_BUILD_DIR}/bin/clang} +CC="${CLANG} --sysroot ${WASI_SYSROOT} -resource-dir ${RESOURCE_DIR}" CFLAGS="-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -mllvm -wasm-enable-sjlj" \ -LDFLAGS="-lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman" \ +LDFLAGS="-lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman -lsetjmp" \ make \ -CC="${WASI_SDK}/bin/clang --sysroot=${WASI_SYSROOT}" \ +CC="${CC}" \ LD="${WASI_SDK}/bin/wasm-ld" \ STRIP="${WASI_SDK}/bin/strip" \ SIZE="${WASI_SDK}/bin/size" \ From e2274d719743c655e4350ceb0dfe8e91c88a93d2 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 2 May 2024 15:31:11 +0900 Subject: [PATCH 15/27] ports/unix/build-wasi.sh: Update WASI-SDK. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 9a99fca3c93d1..3846a4dabbf7d 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -2,18 +2,18 @@ # prerequisites # -# https://github.com/WebAssembly/wasi-libc/pull/483 (wasi-sdk 22.0?) -# https://github.com/WebAssembly/wasi-libc/pull/473 (wasi-sdk 22.0?) +# https://github.com/WebAssembly/wasi-libc/pull/483 (wasi-sdk 22.0) +# https://github.com/WebAssembly/wasi-libc/pull/473 (wasi-sdk 22.0) # https://github.com/WebAssembly/binaryen/pull/6294 (version_117) # https://github.com/WebAssembly/binaryen/pull/6259 (version_117) # https://github.com/llvm/llvm-project/pull/84137 (LLVM 19?) # -# WASI_SDK: wasi-sdk 21.0 +# WASI_SDK: wasi-sdk 22.0 # WASM_OPT: binaryen wasm-opt built with the above patches # WASI_SYSROOT: wasi-libc built with the above patches # CLANG: clang built with the above patches -WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-21.0} +WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-22.0} WASI_SYSROOT=${WASI_SYSROOT:-${HOME}/git/wasi-libc/sysroot} BINARYEN_BIN=${BINARYEN_BIN:-${HOME}/git/wasm/binaryen/b/bin} WASM_OPT=${WASM_OPT:-${BINARYEN_BIN}/wasm-opt} From 8509cba13d54e6dfaae674b1ae5903ceadd07350 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 2 May 2024 15:59:53 +0900 Subject: [PATCH 16/27] ports/unix/build-wasi.sh: Use wasi-libc from wasi-sdk. Also, update comments. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 3846a4dabbf7d..36f3f399c8226 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -8,13 +8,13 @@ # https://github.com/WebAssembly/binaryen/pull/6259 (version_117) # https://github.com/llvm/llvm-project/pull/84137 (LLVM 19?) # -# WASI_SDK: wasi-sdk 22.0 -# WASM_OPT: binaryen wasm-opt built with the above patches -# WASI_SYSROOT: wasi-libc built with the above patches -# CLANG: clang built with the above patches +# WASI_SDK: wasi-sdk 22.0 or later +# WASM_OPT: binaryen wasm-opt version_117 or later +# WASI_SYSROOT: sysroot from wasi-sdk 22.0 or later +# CLANG: clang from LLVM 19 or later, which contains the above mentioned PR WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-22.0} -WASI_SYSROOT=${WASI_SYSROOT:-${HOME}/git/wasi-libc/sysroot} +WASI_SYSROOT=${WASI_SYSROOT:-${WASI_SDK}/share/wasi-sysroot} BINARYEN_BIN=${BINARYEN_BIN:-${HOME}/git/wasm/binaryen/b/bin} WASM_OPT=${WASM_OPT:-${BINARYEN_BIN}/wasm-opt} RESOURCE_DIR=$(${WASI_SDK}/bin/clang --print-resource-dir) From 7a7f6ecae0a78dbaf3b75501f82721c780a4dbc8 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 10 Jun 2024 21:01:48 +0900 Subject: [PATCH 17/27] ports/unix/build-wasi.sh: Update a wasm-opt option. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 36f3f399c8226..b876b1b2e8086 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -41,10 +41,10 @@ ${WASM_OPT} \ --spill-pointers \ -o ${PROG}.spilled ${PROG} -# LLVM still uses the older version of EH proposal. -# Convert to the latest version of EH proposal. +# LLVM still uses the older version of EH proposal. ("phase 3") +# Convert to the latest version of EH proposal with exnref. ${WASM_OPT} \ ---translate-to-new-eh --enable-exception-handling \ +--translate-to-exnref --enable-exception-handling \ -o ${PROG}.spilled.neweh ${PROG}.spilled # now you can run it with EH-enabled runtimes. From 5ab604dc98f08d8e3b8504e6676f86931e7607e4 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 5 Jul 2024 18:06:08 +0900 Subject: [PATCH 18/27] ports/unix/build-wasi.sh: Fix comment grammer. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index b876b1b2e8086..0abab0dd7a5ba 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -35,8 +35,8 @@ VARIANT=wasi \ PROG=build-wasi/micropython -# We doesn't have a way to scan GC roots like WASM locals. -# Hopefully --spill-pointers can workaround it. +# Unlike emscripten, WASI doesn't provide a way to scan GC roots +# like WASM locals. Hopefully --spill-pointers can workaround it. ${WASM_OPT} \ --spill-pointers \ -o ${PROG}.spilled ${PROG} From 437a392db62eb93e941fa735f7ce92f22034d6a5 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 5 Jul 2024 18:07:22 +0900 Subject: [PATCH 19/27] ports/unix/build-wasi.sh: Rename the neweh binary. To match the new name of the corresponding wasm-opt option. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 0abab0dd7a5ba..83dff1299f952 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -45,10 +45,10 @@ ${WASM_OPT} \ # Convert to the latest version of EH proposal with exnref. ${WASM_OPT} \ --translate-to-exnref --enable-exception-handling \ --o ${PROG}.spilled.neweh ${PROG}.spilled +-o ${PROG}.spilled.exnref ${PROG}.spilled # now you can run it with EH-enabled runtimes. # eg. (using the latest EH) -# toywasm --wasi build-wasi/micropython.spilled.neweh +# toywasm --wasi build-wasi/micropython.spilled.exnref # eg. (using the old EH) # iwasm build-wasi/micropython.spilled From 7015fc57119a991ca0e67031462ddcb82f633b3b Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 29 Jul 2024 14:56:22 +0900 Subject: [PATCH 20/27] ports/unix/build-wasi.sh: Use wasi-sdk version of linker. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 83dff1299f952..975e31b6419ce 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -23,10 +23,9 @@ CLANG=${CLANG:-${LLVM_BUILD_DIR}/bin/clang} CC="${CLANG} --sysroot ${WASI_SYSROOT} -resource-dir ${RESOURCE_DIR}" CFLAGS="-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -mllvm -wasm-enable-sjlj" \ -LDFLAGS="-lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman -lsetjmp" \ +LDFLAGS="-lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman -lsetjmp -B ${WASI_SDK}/bin/" \ make \ CC="${CC}" \ -LD="${WASI_SDK}/bin/wasm-ld" \ STRIP="${WASI_SDK}/bin/strip" \ SIZE="${WASI_SDK}/bin/size" \ UNAME_S=wasi \ From 9df64d0b40a93488cea2cc5c42c147de4eb30efc Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 29 Jul 2024 15:00:03 +0900 Subject: [PATCH 21/27] ports/unix/build-wasi.sh: Specify the target explicitly. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 975e31b6419ce..bbc61fc001b21 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -22,8 +22,8 @@ LLVM_BUILD_DIR=${LLVM_BUILD_DIR:-/Volumes/PortableSSD/llvm/build} CLANG=${CLANG:-${LLVM_BUILD_DIR}/bin/clang} CC="${CLANG} --sysroot ${WASI_SYSROOT} -resource-dir ${RESOURCE_DIR}" -CFLAGS="-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -mllvm -wasm-enable-sjlj" \ -LDFLAGS="-lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman -lsetjmp -B ${WASI_SDK}/bin/" \ +CFLAGS="--target=wasm32-wasi -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -mllvm -wasm-enable-sjlj" \ +LDFLAGS="--target=wasm32-wasi -lwasi-emulated-process-clocks -lwasi-emulated-signal -lwasi-emulated-mman -lsetjmp -B ${WASI_SDK}/bin/" \ make \ CC="${CC}" \ STRIP="${WASI_SDK}/bin/strip" \ From 5f409b20795ad163e9cb0d813fbb8ce2a4cfe4ab Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 29 Jul 2024 15:05:09 +0900 Subject: [PATCH 22/27] ports/unix/build-wasi.sh: Add a comment. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index bbc61fc001b21..72eda3c63f6e6 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -13,6 +13,12 @@ # WASI_SYSROOT: sysroot from wasi-sdk 22.0 or later # CLANG: clang from LLVM 19 or later, which contains the above mentioned PR +# REVISIT: +# we specify the target "--target=wasm32-wasi" explicitly below for the case +# where $CLANG is built with a configuration different from wasi-sdk. +# it can be simplified once LLVM 19 becomes a part of wasi-sdk. +# ditto for "-B ${WASI_SDK}/bin/". + WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-22.0} WASI_SYSROOT=${WASI_SYSROOT:-${WASI_SDK}/share/wasi-sysroot} BINARYEN_BIN=${BINARYEN_BIN:-${HOME}/git/wasm/binaryen/b/bin} From 9e05cf3c400f15eb52d5ab8ab4324aa924022b32 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 24 Oct 2024 15:15:35 +0900 Subject: [PATCH 23/27] ports/unix: Make MICROPY_PY_WEBSOCKET overridable. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/mpconfigvariant_common.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/unix/variants/mpconfigvariant_common.h b/ports/unix/variants/mpconfigvariant_common.h index 10c20630ae003..9b9102261a5f7 100644 --- a/ports/unix/variants/mpconfigvariant_common.h +++ b/ports/unix/variants/mpconfigvariant_common.h @@ -119,7 +119,9 @@ #define MICROPY_PY_SELECT_SELECT (0) // Enable the "websocket" module. +#ifndef MICROPY_PY_WEBSOCKET #define MICROPY_PY_WEBSOCKET (1) +#endif // Enable the "machine" module, mostly for machine.mem*. #define MICROPY_PY_MACHINE (1) From fad3b135dae80f63ea4da121f05f5df222d98f28 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 24 Oct 2024 15:16:47 +0900 Subject: [PATCH 24/27] ports/unix: Disable MICROPY_PY_WEBSOCKET. Because it isn't too useful without network. This might be a bit controversial because actually you can use socket even with WASIp1 if you use one of non-standard runtime-specific mechanisms to create/pass an open socket. Anyway, it's easy to re-enable it if you want to use it. Signed-off-by: YAMAMOTO Takashi --- ports/unix/variants/wasi/manifest.py | 4 ++-- ports/unix/variants/wasi/mpconfigvariant.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ports/unix/variants/wasi/manifest.py b/ports/unix/variants/wasi/manifest.py index d27bf3c47fe92..c4a336ca41dff 100644 --- a/ports/unix/variants/wasi/manifest.py +++ b/ports/unix/variants/wasi/manifest.py @@ -1,3 +1,3 @@ -include("$(PORT_DIR)/variants/manifest.py") +# include("$(PORT_DIR)/variants/manifest.py") -include("$(MPY_DIR)/extmod/asyncio") +# include("$(MPY_DIR)/extmod/asyncio") diff --git a/ports/unix/variants/wasi/mpconfigvariant.h b/ports/unix/variants/wasi/mpconfigvariant.h index 95d0a47fe89ab..d637161d7d343 100644 --- a/ports/unix/variants/wasi/mpconfigvariant.h +++ b/ports/unix/variants/wasi/mpconfigvariant.h @@ -34,5 +34,7 @@ #define MICROPY_PY_OS_SYSTEM (0) #define MICROPY_PY_SYS_EXECUTABLE (0) +#define MICROPY_PY_WEBSOCKET (0) + // Enable extra Unix features. #include "../mpconfigvariant_common.h" From a9845245adb148276dfbb75a56487d197a60e746 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 12 Dec 2024 12:34:34 +0900 Subject: [PATCH 25/27] ports/unix/build-wasi.sh: Simplify requirements. Now WASI-SDK 25.0, which contains LLVM 19, has been released. Simplify the requirements by using it. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 72eda3c63f6e6..525997c0a277d 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -2,30 +2,20 @@ # prerequisites # -# https://github.com/WebAssembly/wasi-libc/pull/483 (wasi-sdk 22.0) -# https://github.com/WebAssembly/wasi-libc/pull/473 (wasi-sdk 22.0) -# https://github.com/WebAssembly/binaryen/pull/6294 (version_117) -# https://github.com/WebAssembly/binaryen/pull/6259 (version_117) -# https://github.com/llvm/llvm-project/pull/84137 (LLVM 19?) -# -# WASI_SDK: wasi-sdk 22.0 or later +# WASI_SDK: wasi-sdk 25.0 or later # WASM_OPT: binaryen wasm-opt version_117 or later -# WASI_SYSROOT: sysroot from wasi-sdk 22.0 or later -# CLANG: clang from LLVM 19 or later, which contains the above mentioned PR -# REVISIT: +# Note: # we specify the target "--target=wasm32-wasi" explicitly below for the case # where $CLANG is built with a configuration different from wasi-sdk. -# it can be simplified once LLVM 19 becomes a part of wasi-sdk. # ditto for "-B ${WASI_SDK}/bin/". -WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-22.0} +WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-25.0} WASI_SYSROOT=${WASI_SYSROOT:-${WASI_SDK}/share/wasi-sysroot} -BINARYEN_BIN=${BINARYEN_BIN:-${HOME}/git/wasm/binaryen/b/bin} -WASM_OPT=${WASM_OPT:-${BINARYEN_BIN}/wasm-opt} +WASM_OPT=${WASM_OPT:-wasm-opt} RESOURCE_DIR=$(${WASI_SDK}/bin/clang --print-resource-dir) -LLVM_BUILD_DIR=${LLVM_BUILD_DIR:-/Volumes/PortableSSD/llvm/build} -CLANG=${CLANG:-${LLVM_BUILD_DIR}/bin/clang} +LLVM_DIR=${LLVM_DIR:-${WASI_SDK}} +CLANG=${CLANG:-${LLVM_DIR}/bin/clang} CC="${CLANG} --sysroot ${WASI_SYSROOT} -resource-dir ${RESOURCE_DIR}" CFLAGS="--target=wasm32-wasi -D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -mllvm -wasm-enable-sjlj" \ From 8e3e536253c2d6d7627090e3c1bfbe6960b803cb Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 12 Dec 2024 12:35:57 +0900 Subject: [PATCH 26/27] ports/unix/build-wasi.sh: Use "set -e". Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 525997c0a277d..1a247718b1c14 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -10,6 +10,8 @@ # where $CLANG is built with a configuration different from wasi-sdk. # ditto for "-B ${WASI_SDK}/bin/". +set -e + WASI_SDK=${WASI_SDK:-/opt/wasi-sdk-25.0} WASI_SYSROOT=${WASI_SYSROOT:-${WASI_SDK}/share/wasi-sysroot} WASM_OPT=${WASM_OPT:-wasm-opt} From 5098523c6bdf959ddc98f2e321b9c688ddbc142b Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 12 Dec 2024 12:39:10 +0900 Subject: [PATCH 27/27] ports/unix/build-wasi.sh: Print a bit friendly message. Signed-off-by: YAMAMOTO Takashi --- ports/unix/build-wasi.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ports/unix/build-wasi.sh b/ports/unix/build-wasi.sh index 1a247718b1c14..c72575a44f347 100755 --- a/ports/unix/build-wasi.sh +++ b/ports/unix/build-wasi.sh @@ -44,8 +44,12 @@ ${WASM_OPT} \ --translate-to-exnref --enable-exception-handling \ -o ${PROG}.spilled.exnref ${PROG}.spilled -# now you can run it with EH-enabled runtimes. -# eg. (using the latest EH) -# toywasm --wasi build-wasi/micropython.spilled.exnref -# eg. (using the old EH) -# iwasm build-wasi/micropython.spilled +cat <