Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-103065, GH-106704, GH-105253: Provide a Tools/wasm/wasi.py script to simplify doing a WASI build #112473

Merged
merged 51 commits into from
Nov 30, 2023
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
a4e6f9b
Set up minimal arg parsing
brettcannon Sep 27, 2023
f1be30a
Get the WASI build working
brettcannon Sep 28, 2023
7031bce
Create a `python.sh` command
brettcannon Oct 3, 2023
3db2388
Switch to `os.process_cpu_count()`
brettcannon Oct 3, 2023
238eed1
Tweak some formatting
brettcannon Oct 4, 2023
01319de
Add a `--skip-build-python` flag
brettcannon Oct 4, 2023
0120446
Add a `--quiet` flag
brettcannon Oct 4, 2023
4642afe
Add `SOURCE_DATE_EPOCH`
brettcannon Oct 5, 2023
705f746
Add `--debug`
brettcannon Oct 5, 2023
134e6b4
Fix a bug related to passing a `pathlib.Path` to `section()`
brettcannon Oct 6, 2023
7851a4d
Merge branch 'main' of https://github.com/python/cpython into wasi.py
brettcannon Oct 6, 2023
95029ee
Rename `--debug` to `--with-pydebug`
brettcannon Oct 6, 2023
8717974
Get everything working with a pydebug build
brettcannon Oct 6, 2023
d66fd8b
Support macOS
brettcannon Oct 17, 2023
b21d59a
Add a verification check for the `python.sh` file
brettcannon Oct 18, 2023
a14348e
Allow for `HOST_RUNNER` to be specified as a CLI option
brettcannon Oct 27, 2023
57f56fa
Turn on thread support for the default host runner
brettcannon Nov 3, 2023
94851f6
Add threaded build support
brettcannon Nov 3, 2023
0eb8435
Merge branch 'main' into wasi.py
brettcannon Nov 7, 2023
7060516
Check if the expected sysconfig directory was created
brettcannon Nov 7, 2023
347d5c0
Do a better job of deleting build directories when run with `--clean`
brettcannon Nov 7, 2023
0aa5599
Make the check for the sysconfig build directory use an absolute path
brettcannon Nov 8, 2023
97ff44a
Merge branch 'main' of https://github.com/python/cpython into wasi.py
brettcannon Nov 16, 2023
082e976
Merge branch 'main' of https://github.com/python/cpython into wasi.py
brettcannon Nov 16, 2023
cfefcf0
Merge branch 'main' of https://github.com/python/cpython into wasi.py
brettcannon Nov 16, 2023
b1268ce
Switch to `--platform` from `--skip-build-python`
brettcannon Nov 16, 2023
31cc023
Merge branch 'main' of https://github.com/python/cpython into wasi.py
brettcannon Nov 16, 2023
c3539c1
Add the `configure-build-python` and `make-build-python` subcommands
brettcannon Nov 22, 2023
2a6c541
Create a decorator to handle some boilerplate
brettcannon Nov 22, 2023
c7dcc95
Don't accidentally delete the checkout
brettcannon Nov 22, 2023
19cbc6e
Add back in the `section()` call to `prep_checkout()`
brettcannon Nov 22, 2023
12a7886
Make automatic cleaning an opt-in experience
brettcannon Nov 22, 2023
57b1ea0
Merge remote-tracking branch 'upstream/main' into wasi.py
brettcannon Nov 22, 2023
eb90cbc
Merge branch 'main' of https://github.com/python/cpython into wasi.py
brettcannon Nov 22, 2023
0f7494c
Merge branch 'main' of https://github.com/python/cpython into wasi.py
brettcannon Nov 22, 2023
62b2da8
Clobber `cross-build`
brettcannon Nov 22, 2023
7893385
Add `configure-host`
brettcannon Nov 23, 2023
9994841
Re-implement `build` in terms of all the other subcommands
brettcannon Nov 23, 2023
d975c47
Touch up formatting
brettcannon Nov 24, 2023
27702ca
Update README
brettcannon Nov 24, 2023
c8cbd7a
Set `SOURCE_DATE_EPOCH` based on `git log -1 --pretty=%ct`
brettcannon Nov 27, 2023
e6d947a
Flesh out the README
brettcannon Nov 27, 2023
e160baf
Some more README flourishes
brettcannon Nov 27, 2023
37619db
Add a news entry
brettcannon Nov 27, 2023
3505077
Merge branch 'main' into wasi.py
brettcannon Nov 27, 2023
5b01cbe
Skip mypy checks
brettcannon Nov 27, 2023
ad0a83c
Merge branch 'wasi.py' of https://github.com/brettcannon/cpython into…
brettcannon Nov 27, 2023
71946e7
Fix an over-indentation
brettcannon Nov 27, 2023
bd488b2
Only set `SOURCE_DATE_EPOCH` if it has not already been set
brettcannon Nov 27, 2023
547ba0f
Simplify some code
brettcannon Nov 28, 2023
dea525e
Bump the version of wasmtime installed into the dev container
brettcannon Nov 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add threaded build support
  • Loading branch information
brettcannon committed Nov 3, 2023
commit 94851f69289adb6c5250e4873e6113461c8e1e9a
48 changes: 29 additions & 19 deletions Tools/wasm/wasi.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def call(command, *, quiet, **kwargs):
prefix="cpython-wasi-",
suffix=".log")
stderr = subprocess.STDOUT
print(f"Logging output to {stdout.name} ...")
print(f"Logging output to {stdout.name} (--quiet)...")

subprocess.check_call(command, **kwargs, stdout=stdout, stderr=stderr)

Expand Down Expand Up @@ -78,8 +78,12 @@ def compile_host_python(context):
configure.append("--with-pydebug")

if not context.build_python:
print("Skipped via --skip-build-python ...")
print("Skipping build (--skip-build-python)...")
else:
if context.clean:
print(f"Deleting {build_dir} (--clean)...")
shutil.rmtree(build_dir, ignore_errors=True)

with contextlib.chdir(build_dir):
call(configure, quiet=context.quiet)
call(["make", "--jobs", str(cpu_count()), "all"],
Expand Down Expand Up @@ -141,26 +145,23 @@ def wasi_sdk_env(context):
def compile_wasi_python(context, build_python, version):
"""Compile the wasm32-wasi Python."""
build_dir = CROSS_BUILD_DIR / HOST_TRIPLE
build_dir.mkdir(exist_ok=True)

section(build_dir)

if context.clean:
print(f"Deleting {build_dir} (--clean)...")
shutil.rmtree(build_dir, ignore_errors=True)

build_dir.mkdir(exist_ok=True)

config_site = os.fsdecode(CHECKOUT / "Tools" / "wasm" / "config.site-wasm32-wasi")
# Use PYTHONPATH to include sysconfig data (which must be anchored to the
# WASI guest's / directory.
guest_build_dir = build_dir.relative_to(CHECKOUT)
sysconfig_data = f"{guest_build_dir}/build/lib.wasi-wasm32-{version}"
if context.debug:
sysconfig_data += "-pydebug"
# host_runner = (f"{shutil.which('wasmtime')} run "
# # Make sure the stack size will work in a pydebug build.
# # The value comes from `ulimit -s` under Linux which is
# # 8291 KiB.
# "--max-wasm-stack 8388608 "
# # Map the checkout to / to load the stdlib from /Lib.
# f"--mapdir /::{CHECKOUT} "
# f"--env PYTHONPATH=/{sysconfig_data} "
# f"{build_dir / 'python.wasm'} --")

host_runner = context.host_runner.format(GUEST_DIR="/",
HOST_DIR=CHECKOUT,
ENV_VAR_NAME="PYTHONPATH",
Expand All @@ -183,6 +184,8 @@ def compile_wasi_python(context, build_python, version):
f"--with-build-python={build_python}"]
if context.debug:
configure.append("--with-pydebug")
if context.threads:
configure.append("--with-wasm-pthreads")
configure_env = os.environ | env_additions | wasi_sdk_env(context)
call(configure, env=configure_env, quiet=context.quiet)
call(["make", "--jobs", str(cpu_count()), "all"],
Expand All @@ -202,19 +205,22 @@ def main():
parser = argparse.ArgumentParser()
subcommands = parser.add_subparsers(dest="subcommand")
build = subcommands.add_parser("build")
build.add_argument("--wasi-sdk", type=pathlib.Path, dest="wasi_sdk_path",
default=find_wasi_sdk(),
help="Path to wasi-sdk; defaults to "
"$WASI_SDK_PATH or /opt/wasi-sdk")
build.add_argument("--skip-build-python", action="store_false",
dest="build_python", default=True,
help="Skip building the build/host Python")
build.add_argument("--quiet", action="store_true", default=False,
dest="quiet",
help="Redirect output from subprocesses to a log file")
build.add_argument("--clean", action="store_true", default=False,
dest="clean",
help="Delete any build directories before building")
build.add_argument("--with-pydebug", action="store_true", default=False,
dest="debug",
help="Debug build (i.e., pydebug)")
build.add_argument("--skip-build-python", action="store_false",
dest="build_python", default=True,
help="Skip building the build/host Python")
build.add_argument("--wasi-sdk", type=pathlib.Path, dest="wasi_sdk_path",
default=find_wasi_sdk(),
help="Path to wasi-sdk; defaults to "
"$WASI_SDK_PATH or /opt/wasi-sdk")
default_host_runner = (f"{shutil.which('wasmtime')} run "
# Make sure the stack size will work for a pydebug build.
# The 8388608 value comes from `ulimit -s` under Linux which
Expand All @@ -233,6 +239,10 @@ def main():
help="Command template for running the WebAssembly code "
"(default for wasmtime 14 or newer: "
f"`{default_host_runner}`)")
build.add_argument("--threads", action="store_true", default=False,
dest="threads",
help="Compile with threads support (off by default as "
"thread support is experimental in WASI)")

context = parser.parse_args()
if not context.wasi_sdk_path or not context.wasi_sdk_path.exists():
Expand Down