From 5f305c766d3559b058f6b1068af90c5e1d1e32ab Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 19 Nov 2025 12:36:49 -0800 Subject: [PATCH 1/2] Simplify setting CLI options for WASI builds This introduces a Wasmtime configuration file to get some CLI options out of the code for easier manipulation. It also allows for easier tweaking after the Makefile is generated. As well, cut back on the flexibility of specifying HOSTRUNNER for simpler code. The flexibility was never used and so it didn't make sense to keep it around. --- Tools/wasm/wasi/__main__.py | 31 ++++++++++++++++--------------- Tools/wasm/wasi/wasmtime.toml | 5 +++++ 2 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 Tools/wasm/wasi/wasmtime.toml diff --git a/Tools/wasm/wasi/__main__.py b/Tools/wasm/wasi/__main__.py index 59ffd436258185..6f2b7db0623573 100644 --- a/Tools/wasm/wasi/__main__.py +++ b/Tools/wasm/wasi/__main__.py @@ -16,7 +16,9 @@ import sysconfig import tempfile -CHECKOUT = pathlib.Path(__file__).parent.parent.parent.parent +HERE = pathlib.Path(__file__).parent + +CHECKOUT = HERE.parent.parent.parent assert (CHECKOUT / "configure").is_file(), ( "Please update the location of the file" ) @@ -305,7 +307,7 @@ def configure_wasi_python(context, working_dir): ) config_site = os.fsdecode( - CHECKOUT / "Tools" / "wasm" / "wasi" / "config.site-wasm32-wasi" + HERE / "config.site-wasm32-wasi" ) wasi_build_dir = working_dir.relative_to(CHECKOUT) @@ -324,10 +326,7 @@ def configure_wasi_python(context, working_dir): # Use PYTHONPATH to include sysconfig data which must be anchored to the # WASI guest's `/` directory. args = { - "GUEST_DIR": "/", - "HOST_DIR": CHECKOUT, - "ENV_VAR_NAME": "PYTHONPATH", - "ENV_VAR_VALUE": f"/{sysconfig_data_dir}", + "PYTHONPATH": f"/{sysconfig_data_dir}", "PYTHON_WASM": working_dir / "python.wasm", } # Check dynamically for wasmtime in case it was specified manually via @@ -417,16 +416,18 @@ def main(): default_wasi_sdk = find_wasi_sdk() default_host_runner = ( f"{WASMTIME_HOST_RUNNER_VAR} run " - # Make sure the stack size will work for a pydebug - # build. - # Use 32 MiB stack. - "--wasm max-wasm-stack=33554432 " - # Enable thread support; causes use of preview1. - # "--wasm threads=y --wasi threads=y " + # For setting PYTHONPATH to the sysconfig data directory. + "--env PYTHONPATH={PYTHONPATH} " # Map the checkout to / to load the stdlib from /Lib. - "--dir {HOST_DIR}::{GUEST_DIR} " - # Set PYTHONPATH to the sysconfig data. - "--env {ENV_VAR_NAME}={ENV_VAR_VALUE}" + f"--dir {os.fsdecode(CHECKOUT)}::/ " + # Flags involving --optimize, --codegen, --debug, --wasm, and --wasi can be kept + # in a config file. + # We are using such a file to act as defaults in case a user wants to override + # only some of the settings themselves, make it easy to modify settings + # post-build so that they immediately apply to the Makefile instead of having to + # regenerate it, and allow for easy copying of the settings for anyone else who + # may want to use them. + f"--config {os.fsdecode(HERE / 'wasmtime.toml')}" ) default_logdir = pathlib.Path(tempfile.gettempdir()) diff --git a/Tools/wasm/wasi/wasmtime.toml b/Tools/wasm/wasi/wasmtime.toml new file mode 100644 index 00000000000000..5a45e8c3db94a6 --- /dev/null +++ b/Tools/wasm/wasi/wasmtime.toml @@ -0,0 +1,5 @@ +# https://docs.wasmtime.dev/cli-options.html#cli-options-using-toml-file + +[wasm] +# 32 MiB; big enough for the test suite to pass under a debug build. +max-wasm-stack = 33_554_432 From 6adfa7b4ae14ebe9757aa62617987edb741acf24 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 19 Nov 2025 12:47:37 -0800 Subject: [PATCH 2/2] Fix formatting --- Tools/wasm/wasi/__main__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Tools/wasm/wasi/__main__.py b/Tools/wasm/wasi/__main__.py index 6f2b7db0623573..06903fd25abe44 100644 --- a/Tools/wasm/wasi/__main__.py +++ b/Tools/wasm/wasi/__main__.py @@ -306,9 +306,7 @@ def configure_wasi_python(context, working_dir): "specify via $WASI_SDK_PATH or --wasi-sdk" ) - config_site = os.fsdecode( - HERE / "config.site-wasm32-wasi" - ) + config_site = os.fsdecode(HERE / "config.site-wasm32-wasi") wasi_build_dir = working_dir.relative_to(CHECKOUT)