-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
bpo-40280: Add Tools/wasm with helpers for cross building (GH-29984) #29984
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
Changes from 3 commits
1db84bd
30db875
f933b91
6c46ab4
d0782ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A new directory ``Tools/wasm`` contains WebAssembly-related helpers like ``config.site`` override for wasm32-emscripten, wasm assets generator to bundle the stdlib, and a README. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||
# Python WebAssembly (WASM) build | ||||||
|
||||||
This directory contains configuration and helpers to facilitate cross | ||||||
compile CPython to WebAssembly (WASM). | ||||||
|
||||||
## wasm32-emscripten build | ||||||
|
||||||
Cross compiling to wasm32-emscripten platform needs [Emscripten](https://emscripten.org/) | ||||||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
tool chain and a build Python interpreter. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is correct. In lack of a better term I introduced the term "build Python"/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My brain just keeps reading "build" as a verb instead of an adjective. 😄 |
||||||
|
||||||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
### Compile a build Python interpreter | ||||||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```shell | ||||||
mkdir -p builddir/build | ||||||
pushd builddir/build | ||||||
../../configure -C | ||||||
make -j$(nproc) | ||||||
popd | ||||||
``` | ||||||
|
||||||
### Fetch and build additional emscripten ports | ||||||
|
||||||
```shell | ||||||
embuilder build zlib | ||||||
``` | ||||||
|
||||||
### Cross compile to wasm32-emscripten | ||||||
|
||||||
```shell | ||||||
mkdir -p builddir/emscripten | ||||||
pushd builddir/emscripten | ||||||
|
||||||
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-emscripten \ | ||||||
emconfigure ../../configure -C \ | ||||||
--host=wasm32-unknown-emscripten \ | ||||||
--build=$(../../config.guess) \ | ||||||
--with-build-python=$(pwd)/../build/python | ||||||
|
||||||
emmake make -j$(nproc) python.html | ||||||
``` | ||||||
|
||||||
### Test in browser | ||||||
|
||||||
Serve ``python.html`` with a local webserver and open the file in a browser. | ||||||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```shell | ||||||
emrun python.html | ||||||
``` | ||||||
|
||||||
or | ||||||
|
||||||
```shell | ||||||
python3 -m http.server | ||||||
``` |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,70 @@ | ||||||
# config.site override for cross compiling to wasm32-emscripten platform | ||||||
# | ||||||
# CONFIG_SITE=Tools/wasm/config.site-wasm32-emscripten \ | ||||||
# emconfigure ./configure --host=wasm32-unknown-emscripten --build=... | ||||||
# | ||||||
# Written by Christian Heimes <[email protected]> | ||||||
# Partly based on pyodide's pyconfig.undefs.h file. | ||||||
# | ||||||
|
||||||
# cannot be detected in cross builds | ||||||
ac_cv_buggy_getaddrinfo=no | ||||||
|
||||||
# Emscripten has no /dev/pt* | ||||||
ac_cv_file__dev_ptmx=no | ||||||
ac_cv_file__dev_ptc=no | ||||||
|
||||||
# dummy readelf, Emscripten build does not need readelf. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
ac_cv_prog_ac_ct_READELF=true | ||||||
|
||||||
# new undefined symbols / unsupported features | ||||||
ac_cv_func_posix_spawn=no | ||||||
ac_cv_func_posix_spawnp=no | ||||||
ac_cv_func_eventfd=no | ||||||
ac_cv_func_memfd_create=no | ||||||
ac_cv_func_prlimit=no | ||||||
|
||||||
# unsupported syscall, https://github.com/emscripten-core/emscripten/issues/13393 | ||||||
ac_cv_func_shutdown=no | ||||||
|
||||||
# breaks build, see https://github.com/ethanhs/python-wasm/issues/16 | ||||||
ac_cv_lib_bz2_BZ2_bzCompress=no | ||||||
|
||||||
# The rest is based on pyodide | ||||||
# https://github.com/pyodide/pyodide/blob/main/cpython/pyconfig.undefs.h | ||||||
|
||||||
ac_cv_func_epoll=no | ||||||
ac_cv_func_epoll_create1=no | ||||||
ac_cv_header_linux_vm_sockets_h=no | ||||||
ac_cv_func_socketpair=no | ||||||
ac_cv_func_utimensat=no | ||||||
ac_cv_func_sigaction=no | ||||||
|
||||||
# Untested syscalls in emscripten | ||||||
ac_cv_func_openat=no | ||||||
ac_cv_func_mkdirat=no | ||||||
ac_cv_func_fchownat=no | ||||||
ac_cv_func_renameat=no | ||||||
ac_cv_func_linkat=no | ||||||
ac_cv_func_symlinkat=no | ||||||
ac_cv_func_readlinkat=no | ||||||
ac_cv_func_fchmodat=no | ||||||
ac_cv_func_dup3=no | ||||||
|
||||||
# Syscalls not implemented in emscripten | ||||||
ac_cv_func_preadv2=no | ||||||
ac_cv_func_preadv=no | ||||||
ac_cv_func_pwritev2=no | ||||||
ac_cv_func_pwritev=no | ||||||
ac_cv_func_pipe2=no | ||||||
ac_cv_func_nice=no | ||||||
|
||||||
# Syscalls that resulted in a segfault | ||||||
ac_cv_func_utimensat=no | ||||||
ac_cv_header_sys_ioctl_h=no | ||||||
|
||||||
# sockets are supported, but only in non-blocking mode | ||||||
# ac_cv_header_sys_socket_h=no | ||||||
|
||||||
# Unsupported functionality | ||||||
#undef HAVE_PTHREAD_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
#!/usr/bin/env python | ||
"""Create a WASM asset bundle directory structure | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The WASM asset bundles are pre-loaded by the final WASM build. The bundle | ||
contains: | ||
|
||
- a stripped down, pyc-only stdlib zip file, e.g. {PREFIX}/lib/python311.zip | ||
- os.py as marker module {PREFIX}/lib/python3.11/os.py | ||
- empty lib-dynload directory, to make sure it is copied into the bundle {PREFIX}/lib/python3.11/lib-dynload/.empty | ||
""" | ||
|
||
import argparse | ||
import pathlib | ||
import shutil | ||
import sys | ||
import zipfile | ||
|
||
# source directory | ||
SRCDIR = pathlib.Path(__file__).parent.parent.parent.absolute() | ||
SRCDIR_LIB = SRCDIR / "Lib" | ||
|
||
# sysconfig data relative to build dir | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SYSCONFIGDATA_GLOB = "build/lib.*/_sysconfigdata_*.py" | ||
|
||
# library directory relative to $(prefix) | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
WASM_LIB = pathlib.PurePath("lib") | ||
WASM_STDLIB_ZIP = ( | ||
WASM_LIB / f"python{sys.version_info.major}{sys.version_info.minor}.zip" | ||
) | ||
WASM_STDLIB = ( | ||
WASM_LIB / f"python{sys.version_info.major}.{sys.version_info.minor}" | ||
) | ||
WASM_DYNLOAD = WASM_STDLIB / "lib-dynload" | ||
|
||
|
||
# don't ship large files / packages that are not particular useful at | ||
# the moment. | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
OMIT_FILES = ( | ||
# regression tests | ||
"test/", | ||
# user interfaces: TK, curses | ||
"curses/", | ||
"idlelib/", | ||
"tkinter/", | ||
"turtle.py", | ||
"turtledemo/", | ||
# venv / pip | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"ensurepip/", | ||
"venv/", | ||
# build system | ||
"distutils/", | ||
"lib2to3/", | ||
# concurrency | ||
"concurrent/", | ||
"multiprocessing/", | ||
# deprecated | ||
"asyncore.py", | ||
"asynchat.py", | ||
# synchronous network I/O and protocols are not supported, for example | ||
# socket.create_connection() raises exception | ||
# "BlockingIOError: [Errno 26] Operation in progress". | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"cgi.py", | ||
"cgitb.py", | ||
"email/", | ||
"ftplib.py", | ||
"http/", | ||
"imaplib.py", | ||
"nntplib.py", | ||
"poplib.py", | ||
"smtpd.py", | ||
"smtplib.py", | ||
"socketserver.py", | ||
"telnetlib.py", | ||
"urllib/", | ||
"wsgiref/", | ||
"xmlrpc/", | ||
# dbm / gdbm | ||
"dbm/", | ||
# other platforms | ||
"_aix_support.py", | ||
"_bootsubprocess.py", | ||
"_osx_support.py", | ||
# webbrowser | ||
"antigravity.py", | ||
"webbrowser.py", | ||
# ctypes | ||
"ctypes/", | ||
# pure Python implementations of C extensions | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"_pydecimal.py", | ||
"_pyio.py", | ||
# misc unused or large files | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"pydoc_data/", | ||
"msilib/", | ||
# only pyc encoding files | ||
# "encoding/*.py", | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
# regression test sub directories | ||
OMIT_SUBDIRS = ( | ||
"ctypes/test/", | ||
"tkinter/test/", | ||
"unittest/test/", | ||
) | ||
|
||
|
||
OMIT_ABSOLUTE = {SRCDIR_LIB / name for name in OMIT_FILES} | ||
OMIT_SUBDIRS_ABSOLUTE = tuple(str(SRCDIR_LIB / name) for name in OMIT_SUBDIRS) | ||
|
||
|
||
def filterfunc(name: str) -> bool: | ||
return not name.startswith(OMIT_SUBDIRS_ABSOLUTE) | ||
|
||
|
||
def create_stdlib_zip( | ||
args: argparse.Namespace, compression: int = zipfile.ZIP_DEFLATED, optimize: int = 0 | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sysconfig_data = list(args.builddir.glob(SYSCONFIGDATA_GLOB)) | ||
if not sysconfig_data: | ||
raise ValueError("No sysconfigdata file found") | ||
|
||
with zipfile.PyZipFile( | ||
args.wasm_stdlib_zip, mode="w", compression=compression, optimize=0 | ||
) as pzf: | ||
for entry in sorted(args.srcdir_lib.iterdir()): | ||
if entry.name == "__pycache__": | ||
continue | ||
if entry in OMIT_ABSOLUTE: | ||
continue | ||
if entry.name.endswith(".py") or entry.is_dir(): | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# writepy() writes .pyc files (bytecode) | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pzf.writepy(entry, filterfunc=filterfunc) | ||
for entry in sysconfig_data: | ||
pzf.writepy(entry) | ||
|
||
|
||
def path(val: str) -> pathlib.Path: | ||
return pathlib.Path(val).absolute() | ||
|
||
|
||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--builddir", | ||
help="absolute build directory", | ||
default=pathlib.Path(".").absolute(), | ||
type=path, | ||
) | ||
parser.add_argument( | ||
"--prefix", help="install prefix", default=pathlib.Path("/usr/local"), type=path | ||
) | ||
|
||
|
||
def main(): | ||
args = parser.parse_args() | ||
|
||
relative_prefix = args.prefix.relative_to(pathlib.Path("/")) | ||
args.srcdir = SRCDIR | ||
args.srcdir_lib = SRCDIR_LIB | ||
args.wasm_root = args.builddir / relative_prefix | ||
args.wasm_stdlib_zip = args.wasm_root / WASM_STDLIB_ZIP | ||
args.wasm_stdlib = args.wasm_root / WASM_STDLIB | ||
args.wasm_dynload = args.wasm_root / WASM_DYNLOAD | ||
|
||
# empty, unused directory for dynamic libs, but required for site initialization. | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
args.wasm_dynload.mkdir(parents=True, exist_ok=True) | ||
marker = args.wasm_dynload / ".empty" | ||
marker.touch() | ||
# os.py is a marker for finding the correct lib directory | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
shutil.copy(args.srcdir_lib / "os.py", args.wasm_stdlib) | ||
# useful rest of stdlib | ||
tiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
create_stdlib_zip(args) | ||
size = round(args.wasm_stdlib_zip.stat().st_size / 1024 ** 2, 2) | ||
parser.exit(0, f"Created {args.wasm_stdlib_zip} ({size} MiB)\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Uh oh!
There was an error while loading. Please reload this page.