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
Get everything working with a pydebug build
  • Loading branch information
brettcannon committed Oct 6, 2023
commit 8717974ad5b32f6f3213171822c05adc3298abff
39 changes: 32 additions & 7 deletions Tools/wasm/wasi.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def build_platform():
return sysconfig.get_config_var("BUILD_GNU_TYPE")


def prep_checkout():
"""Prepare the source checkout for cross-compiling."""
# Without `Setup.local`, in-place execution fails to realize it's in a
# build tree/checkout (the dreaded "No module named 'encodings'" error).
section(CHECKOUT)

local_setup = CHECKOUT / "Modules" / "Setup.local"
if local_setup.exists():
print("Modules/Setup.local already exists ...")
else:
print("Touching Modules/Setup.local ...")
local_setup.touch()


def compile_host_python(context):
"""Compile the build/host Python.

Expand All @@ -58,7 +72,7 @@ def compile_host_python(context):

section(build_dir)

configure = [CHECKOUT / "configure", "-C"]
configure = [os.path.relpath(CHECKOUT / 'configure', build_dir), "-C"]
if context.debug:
configure.append("--with-pydebug")

Expand All @@ -67,15 +81,16 @@ def compile_host_python(context):
else:
with contextlib.chdir(build_dir):
call(configure, quiet=context.quiet)
call(["make", "--jobs", str(cpu_count()), "all"], quiet=context.quiet)
call(["make", "--jobs", str(cpu_count()), "all"],
quiet=context.quiet)

# XXX `python.exe` on Mac
binary = build_dir / "python"
cmd = [binary, "-c",
"import sys; "
"print(f'{sys.version_info.major}.{sys.version_info.minor}')"]
version = subprocess.check_output(cmd, encoding="utf-8").strip()

# XXX Check if the binary is named `python` on macOS
return binary, version


Expand Down Expand Up @@ -125,12 +140,20 @@ def compile_wasi_python(context, build_python, version):
section(build_dir)

config_site = os.fsdecode(CHECKOUT / "Tools" / "wasm" / "config.site-wasm32-wasi")
# Map the checkout to / to load the stdlib from /Lib. Also means paths for
# PYTHONPATH to include sysconfig data must be anchored to the WASI
# runtime's / directory.
# 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=/{CROSS_BUILD_DIR.name}/wasi/build/lib.wasi-wasm32-{version} "
f"--env PYTHONPATH=/{sysconfig_data} "
f"{build_dir / 'python.wasm'} --")
env_additions = {"CONFIG_SITE": config_site, "HOSTRUNNER": host_runner,
# Python's first commit:
Expand Down Expand Up @@ -184,6 +207,8 @@ def main():
raise ValueError("wasi-sdk not found or specified; "
"see https://github.com/WebAssembly/wasi-sdk")

prep_checkout()
print()
build_python, version = compile_host_python(context)
print()
compile_wasi_python(context, build_python, version)
Expand Down