2025-05-07 12:10:53 -07:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
import contextlib
|
|
|
|
|
|
import functools
|
|
|
|
|
|
import os
|
2025-10-07 19:25:06 -07:00
|
|
|
|
|
2025-05-07 12:10:53 -07:00
|
|
|
|
try:
|
|
|
|
|
|
from os import process_cpu_count as cpu_count
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
from os import cpu_count
|
|
|
|
|
|
import pathlib
|
|
|
|
|
|
import shutil
|
|
|
|
|
|
import subprocess
|
|
|
|
|
|
import sys
|
|
|
|
|
|
import sysconfig
|
|
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
CHECKOUT = pathlib.Path(__file__).parent.parent.parent.parent
|
2025-10-07 19:25:06 -07:00
|
|
|
|
assert (CHECKOUT / "configure").is_file(), (
|
|
|
|
|
|
"Please update the location of the file"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
CROSS_BUILD_DIR = CHECKOUT / "cross-build"
|
2025-08-06 12:28:54 -07:00
|
|
|
|
# Build platform can also be found via `config.guess`.
|
|
|
|
|
|
BUILD_DIR = CROSS_BUILD_DIR / sysconfig.get_config_var("BUILD_GNU_TYPE")
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
LOCAL_SETUP = CHECKOUT / "Modules" / "Setup.local"
|
2025-10-07 19:25:06 -07:00
|
|
|
|
LOCAL_SETUP_MARKER = (
|
2025-10-09 01:13:27 +03:00
|
|
|
|
b"# Generated by Tools/wasm/wasi .\n"
|
|
|
|
|
|
b"# Required to statically build extension modules."
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
2025-10-22 14:34:37 -07:00
|
|
|
|
WASI_SDK_VERSION = 25
|
2025-07-30 11:46:24 -07:00
|
|
|
|
|
2025-05-07 12:10:53 -07:00
|
|
|
|
WASMTIME_VAR_NAME = "WASMTIME"
|
|
|
|
|
|
WASMTIME_HOST_RUNNER_VAR = f"{{{WASMTIME_VAR_NAME}}}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def updated_env(updates={}):
|
|
|
|
|
|
"""Create a new dict representing the environment to use.
|
|
|
|
|
|
|
|
|
|
|
|
The changes made to the execution environment are printed out.
|
|
|
|
|
|
"""
|
|
|
|
|
|
env_defaults = {}
|
|
|
|
|
|
# https://reproducible-builds.org/docs/source-date-epoch/
|
|
|
|
|
|
git_epoch_cmd = ["git", "log", "-1", "--pretty=%ct"]
|
|
|
|
|
|
try:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
epoch = subprocess.check_output(
|
|
|
|
|
|
git_epoch_cmd, encoding="utf-8"
|
|
|
|
|
|
).strip()
|
2025-05-07 12:10:53 -07:00
|
|
|
|
env_defaults["SOURCE_DATE_EPOCH"] = epoch
|
|
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
|
|
pass # Might be building from a tarball.
|
|
|
|
|
|
# This layering lets SOURCE_DATE_EPOCH from os.environ takes precedence.
|
|
|
|
|
|
environment = env_defaults | os.environ | updates
|
|
|
|
|
|
|
|
|
|
|
|
env_diff = {}
|
|
|
|
|
|
for key, value in environment.items():
|
|
|
|
|
|
if os.environ.get(key) != value:
|
|
|
|
|
|
env_diff[key] = value
|
|
|
|
|
|
|
|
|
|
|
|
print("🌎 Environment changes:")
|
|
|
|
|
|
for key in sorted(env_diff.keys()):
|
|
|
|
|
|
print(f" {key}={env_diff[key]}")
|
|
|
|
|
|
|
|
|
|
|
|
return environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def subdir(working_dir, *, clean_ok=False):
|
|
|
|
|
|
"""Decorator to change to a working directory."""
|
2025-10-07 19:25:06 -07:00
|
|
|
|
|
2025-05-07 12:10:53 -07:00
|
|
|
|
def decorator(func):
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
|
|
def wrapper(context):
|
|
|
|
|
|
nonlocal working_dir
|
|
|
|
|
|
|
|
|
|
|
|
if callable(working_dir):
|
|
|
|
|
|
working_dir = working_dir(context)
|
|
|
|
|
|
try:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
tput_output = subprocess.check_output(
|
|
|
|
|
|
["tput", "cols"], encoding="utf-8"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
|
|
terminal_width = 80
|
|
|
|
|
|
else:
|
|
|
|
|
|
terminal_width = int(tput_output.strip())
|
|
|
|
|
|
print("⎯" * terminal_width)
|
|
|
|
|
|
print("📁", working_dir)
|
2025-10-07 19:25:06 -07:00
|
|
|
|
if (
|
|
|
|
|
|
clean_ok
|
|
|
|
|
|
and getattr(context, "clean", False)
|
|
|
|
|
|
and working_dir.exists()
|
|
|
|
|
|
):
|
2025-08-06 12:28:54 -07:00
|
|
|
|
print("🚮 Deleting directory (--clean)...")
|
2025-05-07 12:10:53 -07:00
|
|
|
|
shutil.rmtree(working_dir)
|
|
|
|
|
|
|
|
|
|
|
|
working_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
with contextlib.chdir(working_dir):
|
|
|
|
|
|
return func(context, working_dir)
|
|
|
|
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-30 12:13:37 -07:00
|
|
|
|
def call(command, *, context=None, quiet=False, logdir=None, **kwargs):
|
2025-05-07 12:10:53 -07:00
|
|
|
|
"""Execute a command.
|
|
|
|
|
|
|
|
|
|
|
|
If 'quiet' is true, then redirect stdout and stderr to a temporary file.
|
|
|
|
|
|
"""
|
2025-07-30 12:13:37 -07:00
|
|
|
|
if context is not None:
|
|
|
|
|
|
quiet = context.quiet
|
|
|
|
|
|
logdir = context.logdir
|
|
|
|
|
|
elif quiet and logdir is None:
|
|
|
|
|
|
raise ValueError("When quiet is True, logdir must be specified")
|
|
|
|
|
|
|
2025-05-07 12:10:53 -07:00
|
|
|
|
print("❯", " ".join(map(str, command)))
|
|
|
|
|
|
if not quiet:
|
|
|
|
|
|
stdout = None
|
|
|
|
|
|
stderr = None
|
|
|
|
|
|
else:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
stdout = tempfile.NamedTemporaryFile(
|
|
|
|
|
|
"w",
|
|
|
|
|
|
encoding="utf-8",
|
|
|
|
|
|
delete=False,
|
|
|
|
|
|
dir=logdir,
|
|
|
|
|
|
prefix="cpython-wasi-",
|
|
|
|
|
|
suffix=".log",
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
stderr = subprocess.STDOUT
|
|
|
|
|
|
print(f"📝 Logging output to {stdout.name} (--quiet)...")
|
|
|
|
|
|
|
|
|
|
|
|
subprocess.check_call(command, **kwargs, stdout=stdout, stderr=stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_python_path():
|
|
|
|
|
|
"""The path to the build Python binary."""
|
|
|
|
|
|
binary = BUILD_DIR / "python"
|
|
|
|
|
|
if not binary.is_file():
|
|
|
|
|
|
binary = binary.with_suffix(".exe")
|
|
|
|
|
|
if not binary.is_file():
|
2025-10-07 19:25:06 -07:00
|
|
|
|
raise FileNotFoundError(
|
|
|
|
|
|
f"Unable to find `python(.exe)` in {BUILD_DIR}"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
return binary
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-22 14:41:50 -07:00
|
|
|
|
def build_python_is_pydebug():
|
|
|
|
|
|
"""Find out if the build Python is a pydebug build."""
|
|
|
|
|
|
test = "import sys, test.support; sys.exit(test.support.Py_DEBUG)"
|
2025-10-07 19:25:06 -07:00
|
|
|
|
result = subprocess.run(
|
|
|
|
|
|
[build_python_path(), "-c", test],
|
2025-10-09 01:13:27 +03:00
|
|
|
|
capture_output=True,
|
2025-10-07 19:25:06 -07:00
|
|
|
|
)
|
2025-05-22 14:41:50 -07:00
|
|
|
|
return bool(result.returncode)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-07 12:10:53 -07:00
|
|
|
|
@subdir(BUILD_DIR, clean_ok=True)
|
|
|
|
|
|
def configure_build_python(context, working_dir):
|
|
|
|
|
|
"""Configure the build/host Python."""
|
|
|
|
|
|
if LOCAL_SETUP.exists():
|
2025-07-23 11:50:15 -07:00
|
|
|
|
if LOCAL_SETUP.read_bytes() == LOCAL_SETUP_MARKER:
|
|
|
|
|
|
print(f"👍 {LOCAL_SETUP} exists ...")
|
|
|
|
|
|
else:
|
|
|
|
|
|
print(f"⚠️ {LOCAL_SETUP} exists, but has unexpected contents")
|
2025-05-07 12:10:53 -07:00
|
|
|
|
else:
|
2025-07-23 11:50:15 -07:00
|
|
|
|
print(f"📝 Creating {LOCAL_SETUP} ...")
|
2025-05-07 12:10:53 -07:00
|
|
|
|
LOCAL_SETUP.write_bytes(LOCAL_SETUP_MARKER)
|
|
|
|
|
|
|
2025-10-07 19:25:06 -07:00
|
|
|
|
configure = [os.path.relpath(CHECKOUT / "configure", working_dir)]
|
2025-05-07 12:10:53 -07:00
|
|
|
|
if context.args:
|
|
|
|
|
|
configure.extend(context.args)
|
|
|
|
|
|
|
2025-07-30 12:13:37 -07:00
|
|
|
|
call(configure, context=context)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@subdir(BUILD_DIR)
|
|
|
|
|
|
def make_build_python(context, working_dir):
|
|
|
|
|
|
"""Make/build the build Python."""
|
2025-10-07 19:25:06 -07:00
|
|
|
|
call(["make", "--jobs", str(cpu_count()), "all"], context=context)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
binary = build_python_path()
|
2025-10-07 19:25:06 -07:00
|
|
|
|
cmd = [
|
|
|
|
|
|
binary,
|
|
|
|
|
|
"-c",
|
|
|
|
|
|
"import sys; "
|
|
|
|
|
|
"print(f'{sys.version_info.major}.{sys.version_info.minor}')",
|
|
|
|
|
|
]
|
2025-05-07 12:10:53 -07:00
|
|
|
|
version = subprocess.check_output(cmd, encoding="utf-8").strip()
|
|
|
|
|
|
|
|
|
|
|
|
print(f"🎉 {binary} {version}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_wasi_sdk():
|
2025-07-30 11:46:24 -07:00
|
|
|
|
"""Find the path to the WASI SDK."""
|
2025-05-07 12:10:53 -07:00
|
|
|
|
if wasi_sdk_path := os.environ.get("WASI_SDK_PATH"):
|
|
|
|
|
|
return pathlib.Path(wasi_sdk_path)
|
2025-07-30 11:46:24 -07:00
|
|
|
|
|
|
|
|
|
|
opt_path = pathlib.Path("/opt")
|
|
|
|
|
|
# WASI SDK versions have a ``.0`` suffix, but it's a constant; the WASI SDK team
|
|
|
|
|
|
# has said they don't plan to ever do a point release and all of their Git tags
|
|
|
|
|
|
# lack the ``.0`` suffix.
|
|
|
|
|
|
# Starting with WASI SDK 23, the tarballs went from containing a directory named
|
|
|
|
|
|
# ``wasi-sdk-{WASI_SDK_VERSION}.0`` to e.g.
|
|
|
|
|
|
# ``wasi-sdk-{WASI_SDK_VERSION}.0-x86_64-linux``.
|
2025-10-07 19:25:06 -07:00
|
|
|
|
potential_sdks = [
|
|
|
|
|
|
path
|
|
|
|
|
|
for path in opt_path.glob(f"wasi-sdk-{WASI_SDK_VERSION}.0*")
|
|
|
|
|
|
if path.is_dir()
|
|
|
|
|
|
]
|
2025-07-30 11:46:24 -07:00
|
|
|
|
if len(potential_sdks) == 1:
|
|
|
|
|
|
return potential_sdks[0]
|
|
|
|
|
|
elif (default_path := opt_path / "wasi-sdk").is_dir():
|
2025-05-07 12:10:53 -07:00
|
|
|
|
return default_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wasi_sdk_env(context):
|
|
|
|
|
|
"""Calculate environment variables for building with wasi-sdk."""
|
|
|
|
|
|
wasi_sdk_path = context.wasi_sdk_path
|
|
|
|
|
|
sysroot = wasi_sdk_path / "share" / "wasi-sysroot"
|
2025-10-07 19:25:06 -07:00
|
|
|
|
env = {
|
|
|
|
|
|
"CC": "clang",
|
|
|
|
|
|
"CPP": "clang-cpp",
|
|
|
|
|
|
"CXX": "clang++",
|
|
|
|
|
|
"AR": "llvm-ar",
|
|
|
|
|
|
"RANLIB": "ranlib",
|
|
|
|
|
|
}
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
for env_var, binary_name in list(env.items()):
|
|
|
|
|
|
env[env_var] = os.fsdecode(wasi_sdk_path / "bin" / binary_name)
|
|
|
|
|
|
|
|
|
|
|
|
if wasi_sdk_path != pathlib.Path("/opt/wasi-sdk"):
|
|
|
|
|
|
for compiler in ["CC", "CPP", "CXX"]:
|
|
|
|
|
|
env[compiler] += f" --sysroot={sysroot}"
|
|
|
|
|
|
|
|
|
|
|
|
env["PKG_CONFIG_PATH"] = ""
|
|
|
|
|
|
env["PKG_CONFIG_LIBDIR"] = os.pathsep.join(
|
2025-10-07 19:25:06 -07:00
|
|
|
|
map(
|
|
|
|
|
|
os.fsdecode,
|
|
|
|
|
|
[sysroot / "lib" / "pkgconfig", sysroot / "share" / "pkgconfig"],
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
env["PKG_CONFIG_SYSROOT_DIR"] = os.fsdecode(sysroot)
|
|
|
|
|
|
|
|
|
|
|
|
env["WASI_SDK_PATH"] = os.fsdecode(wasi_sdk_path)
|
|
|
|
|
|
env["WASI_SYSROOT"] = os.fsdecode(sysroot)
|
|
|
|
|
|
|
2025-10-07 19:25:06 -07:00
|
|
|
|
env["PATH"] = os.pathsep.join([
|
|
|
|
|
|
os.fsdecode(wasi_sdk_path / "bin"),
|
|
|
|
|
|
os.environ["PATH"],
|
|
|
|
|
|
])
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
return env
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@subdir(lambda context: CROSS_BUILD_DIR / context.host_triple, clean_ok=True)
|
|
|
|
|
|
def configure_wasi_python(context, working_dir):
|
|
|
|
|
|
"""Configure the WASI/host build."""
|
|
|
|
|
|
if not context.wasi_sdk_path or not context.wasi_sdk_path.exists():
|
2025-10-07 19:25:06 -07:00
|
|
|
|
raise ValueError(
|
|
|
|
|
|
"WASI-SDK not found; "
|
|
|
|
|
|
"download from "
|
|
|
|
|
|
"https://github.com/WebAssembly/wasi-sdk and/or "
|
|
|
|
|
|
"specify via $WASI_SDK_PATH or --wasi-sdk"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
config_site = os.fsdecode(
|
|
|
|
|
|
CHECKOUT / "Tools" / "wasm" / "wasi" / "config.site-wasm32-wasi"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
wasi_build_dir = working_dir.relative_to(CHECKOUT)
|
|
|
|
|
|
|
|
|
|
|
|
python_build_dir = BUILD_DIR / "build"
|
|
|
|
|
|
lib_dirs = list(python_build_dir.glob("lib.*"))
|
2025-10-07 19:25:06 -07:00
|
|
|
|
assert len(lib_dirs) == 1, (
|
|
|
|
|
|
f"Expected a single lib.* directory in {python_build_dir}"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
lib_dir = os.fsdecode(lib_dirs[0])
|
2025-05-22 14:41:50 -07:00
|
|
|
|
python_version = lib_dir.rpartition("-")[-1]
|
2025-10-07 19:25:06 -07:00
|
|
|
|
sysconfig_data_dir = (
|
|
|
|
|
|
f"{wasi_build_dir}/build/lib.wasi-wasm32-{python_version}"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
# Use PYTHONPATH to include sysconfig data which must be anchored to the
|
|
|
|
|
|
# WASI guest's `/` directory.
|
2025-10-07 19:25:06 -07:00
|
|
|
|
args = {
|
|
|
|
|
|
"GUEST_DIR": "/",
|
|
|
|
|
|
"HOST_DIR": CHECKOUT,
|
|
|
|
|
|
"ENV_VAR_NAME": "PYTHONPATH",
|
|
|
|
|
|
"ENV_VAR_VALUE": f"/{sysconfig_data_dir}",
|
|
|
|
|
|
"PYTHON_WASM": working_dir / "python.wasm",
|
|
|
|
|
|
}
|
2025-05-07 12:10:53 -07:00
|
|
|
|
# Check dynamically for wasmtime in case it was specified manually via
|
|
|
|
|
|
# `--host-runner`.
|
|
|
|
|
|
if WASMTIME_HOST_RUNNER_VAR in context.host_runner:
|
|
|
|
|
|
if wasmtime := shutil.which("wasmtime"):
|
|
|
|
|
|
args[WASMTIME_VAR_NAME] = wasmtime
|
|
|
|
|
|
else:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
raise FileNotFoundError(
|
|
|
|
|
|
"wasmtime not found; download from "
|
|
|
|
|
|
"https://github.com/bytecodealliance/wasmtime"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
host_runner = context.host_runner.format_map(args)
|
|
|
|
|
|
env_additions = {"CONFIG_SITE": config_site, "HOSTRUNNER": host_runner}
|
|
|
|
|
|
build_python = os.fsdecode(build_python_path())
|
|
|
|
|
|
# The path to `configure` MUST be relative, else `python.wasm` is unable
|
|
|
|
|
|
# to find the stdlib due to Python not recognizing that it's being
|
|
|
|
|
|
# executed from within a checkout.
|
2025-10-07 19:25:06 -07:00
|
|
|
|
configure = [
|
|
|
|
|
|
os.path.relpath(CHECKOUT / "configure", working_dir),
|
|
|
|
|
|
f"--host={context.host_triple}",
|
|
|
|
|
|
f"--build={BUILD_DIR.name}",
|
|
|
|
|
|
f"--with-build-python={build_python}",
|
|
|
|
|
|
]
|
2025-05-22 14:41:50 -07:00
|
|
|
|
if build_python_is_pydebug():
|
2025-05-07 12:10:53 -07:00
|
|
|
|
configure.append("--with-pydebug")
|
|
|
|
|
|
if context.args:
|
|
|
|
|
|
configure.extend(context.args)
|
2025-10-07 19:25:06 -07:00
|
|
|
|
call(
|
|
|
|
|
|
configure,
|
|
|
|
|
|
env=updated_env(env_additions | wasi_sdk_env(context)),
|
|
|
|
|
|
context=context,
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
python_wasm = working_dir / "python.wasm"
|
|
|
|
|
|
exec_script = working_dir / "python.sh"
|
|
|
|
|
|
with exec_script.open("w", encoding="utf-8") as file:
|
|
|
|
|
|
file.write(f'#!/bin/sh\nexec {host_runner} {python_wasm} "$@"\n')
|
|
|
|
|
|
exec_script.chmod(0o755)
|
2025-05-08 09:54:46 -07:00
|
|
|
|
print(f"🏃♀️ Created {exec_script} (--host-runner)... ")
|
2025-05-07 12:10:53 -07:00
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@subdir(lambda context: CROSS_BUILD_DIR / context.host_triple)
|
|
|
|
|
|
def make_wasi_python(context, working_dir):
|
|
|
|
|
|
"""Run `make` for the WASI/host build."""
|
2025-10-07 19:25:06 -07:00
|
|
|
|
call(
|
|
|
|
|
|
["make", "--jobs", str(cpu_count()), "all"],
|
|
|
|
|
|
env=updated_env(),
|
|
|
|
|
|
context=context,
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
exec_script = working_dir / "python.sh"
|
2025-05-08 09:54:46 -07:00
|
|
|
|
call([exec_script, "--version"], quiet=False)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
print(
|
2025-05-08 09:54:46 -07:00
|
|
|
|
f"🎉 Use `{exec_script.relative_to(context.init_dir)}` "
|
|
|
|
|
|
"to run CPython w/ the WASI host specified by --host-runner"
|
2025-05-07 12:10:53 -07:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_all(context):
|
|
|
|
|
|
"""Build everything."""
|
2025-10-07 19:25:06 -07:00
|
|
|
|
steps = [
|
|
|
|
|
|
configure_build_python,
|
|
|
|
|
|
make_build_python,
|
|
|
|
|
|
configure_wasi_python,
|
|
|
|
|
|
make_wasi_python,
|
|
|
|
|
|
]
|
2025-05-07 12:10:53 -07:00
|
|
|
|
for step in steps:
|
|
|
|
|
|
step(context)
|
|
|
|
|
|
|
2025-10-07 19:25:06 -07:00
|
|
|
|
|
2025-05-07 12:10:53 -07:00
|
|
|
|
def clean_contents(context):
|
|
|
|
|
|
"""Delete all files created by this script."""
|
|
|
|
|
|
if CROSS_BUILD_DIR.exists():
|
|
|
|
|
|
print(f"🧹 Deleting {CROSS_BUILD_DIR} ...")
|
|
|
|
|
|
shutil.rmtree(CROSS_BUILD_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
if LOCAL_SETUP.exists():
|
2025-07-23 11:50:15 -07:00
|
|
|
|
if LOCAL_SETUP.read_bytes() == LOCAL_SETUP_MARKER:
|
|
|
|
|
|
print(f"🧹 Deleting generated {LOCAL_SETUP} ...")
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2025-07-30 11:46:24 -07:00
|
|
|
|
default_host_triple = "wasm32-wasip1"
|
|
|
|
|
|
default_wasi_sdk = find_wasi_sdk()
|
2025-10-07 19:25:06 -07:00
|
|
|
|
default_host_runner = (
|
|
|
|
|
|
f"{WASMTIME_HOST_RUNNER_VAR} run "
|
|
|
|
|
|
# Make sure the stack size will work for a pydebug
|
|
|
|
|
|
# build.
|
|
|
|
|
|
# Use 16 MiB stack.
|
|
|
|
|
|
"--wasm max-wasm-stack=16777216 "
|
|
|
|
|
|
# Enable thread support; causes use of preview1.
|
|
|
|
|
|
# "--wasm threads=y --wasi threads=y "
|
|
|
|
|
|
# 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}"
|
|
|
|
|
|
)
|
2025-07-30 12:13:37 -07:00
|
|
|
|
default_logdir = pathlib.Path(tempfile.gettempdir())
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
subcommands = parser.add_subparsers(dest="subcommand")
|
|
|
|
|
|
build = subcommands.add_parser("build", help="Build everything")
|
2025-10-07 19:25:06 -07:00
|
|
|
|
configure_build = subcommands.add_parser(
|
|
|
|
|
|
"configure-build-python", help="Run `configure` for the build Python"
|
|
|
|
|
|
)
|
|
|
|
|
|
make_build = subcommands.add_parser(
|
|
|
|
|
|
"make-build-python", help="Run `make` for the build Python"
|
|
|
|
|
|
)
|
|
|
|
|
|
configure_host = subcommands.add_parser(
|
|
|
|
|
|
"configure-host",
|
|
|
|
|
|
help="Run `configure` for the "
|
|
|
|
|
|
"host/WASI (pydebug builds "
|
|
|
|
|
|
"are inferred from the build "
|
|
|
|
|
|
"Python)",
|
|
|
|
|
|
)
|
|
|
|
|
|
make_host = subcommands.add_parser(
|
|
|
|
|
|
"make-host", help="Run `make` for the host/WASI"
|
|
|
|
|
|
)
|
|
|
|
|
|
subcommands.add_parser(
|
|
|
|
|
|
"clean", help="Delete files and directories created by this script"
|
|
|
|
|
|
)
|
|
|
|
|
|
for subcommand in (
|
|
|
|
|
|
build,
|
|
|
|
|
|
configure_build,
|
|
|
|
|
|
make_build,
|
|
|
|
|
|
configure_host,
|
|
|
|
|
|
make_host,
|
|
|
|
|
|
):
|
|
|
|
|
|
subcommand.add_argument(
|
|
|
|
|
|
"--quiet",
|
|
|
|
|
|
action="store_true",
|
|
|
|
|
|
default=False,
|
|
|
|
|
|
dest="quiet",
|
|
|
|
|
|
help="Redirect output from subprocesses to a log file",
|
|
|
|
|
|
)
|
|
|
|
|
|
subcommand.add_argument(
|
|
|
|
|
|
"--logdir",
|
|
|
|
|
|
type=pathlib.Path,
|
|
|
|
|
|
default=default_logdir,
|
|
|
|
|
|
help=f"Directory to store log files; defaults to {default_logdir}",
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
for subcommand in configure_build, configure_host:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
subcommand.add_argument(
|
|
|
|
|
|
"--clean",
|
|
|
|
|
|
action="store_true",
|
|
|
|
|
|
default=False,
|
|
|
|
|
|
dest="clean",
|
|
|
|
|
|
help="Delete any relevant directories before building",
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
for subcommand in build, configure_build, configure_host:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
subcommand.add_argument(
|
|
|
|
|
|
"args", nargs="*", help="Extra arguments to pass to `configure`"
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
for subcommand in build, configure_host:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
subcommand.add_argument(
|
|
|
|
|
|
"--wasi-sdk",
|
|
|
|
|
|
type=pathlib.Path,
|
|
|
|
|
|
dest="wasi_sdk_path",
|
|
|
|
|
|
default=default_wasi_sdk,
|
|
|
|
|
|
help=f"Path to the WASI SDK; defaults to {default_wasi_sdk}",
|
|
|
|
|
|
)
|
|
|
|
|
|
subcommand.add_argument(
|
|
|
|
|
|
"--host-runner",
|
|
|
|
|
|
action="store",
|
|
|
|
|
|
default=default_host_runner,
|
|
|
|
|
|
dest="host_runner",
|
|
|
|
|
|
help="Command template for running the WASI host; defaults to "
|
|
|
|
|
|
f"`{default_host_runner}`",
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
for subcommand in build, configure_host, make_host:
|
2025-10-07 19:25:06 -07:00
|
|
|
|
subcommand.add_argument(
|
|
|
|
|
|
"--host-triple",
|
|
|
|
|
|
action="store",
|
|
|
|
|
|
default=default_host_triple,
|
|
|
|
|
|
help="The target triple for the WASI host build; "
|
|
|
|
|
|
f"defaults to {default_host_triple}",
|
|
|
|
|
|
)
|
2025-05-07 12:10:53 -07:00
|
|
|
|
|
|
|
|
|
|
context = parser.parse_args()
|
|
|
|
|
|
context.init_dir = pathlib.Path().absolute()
|
|
|
|
|
|
|
2025-10-07 19:25:06 -07:00
|
|
|
|
dispatch = {
|
|
|
|
|
|
"configure-build-python": configure_build_python,
|
|
|
|
|
|
"make-build-python": make_build_python,
|
|
|
|
|
|
"configure-host": configure_wasi_python,
|
|
|
|
|
|
"make-host": make_wasi_python,
|
|
|
|
|
|
"build": build_all,
|
|
|
|
|
|
"clean": clean_contents,
|
|
|
|
|
|
}
|
2025-05-07 12:10:53 -07:00
|
|
|
|
dispatch[context.subcommand](context)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-07 19:25:06 -07:00
|
|
|
|
if __name__ == "__main__":
|
2025-05-07 12:10:53 -07:00
|
|
|
|
main()
|