Skip to content

Commit 6087f49

Browse files
authored
gh-95853: WASM: better version and asset handling in scripts (GH-96045)
- support EMSDK tot-upstream and git releases - allow WASM assents for wasm64-emscripten and WASI. This makes single file distributions on WASI easier. - decouple WASM assets from browser builds
1 parent 757c383 commit 6087f49

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

Makefile.pre.in

+3-2
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,11 @@ $(DLLLIBRARY) libpython$(LDVERSION).dll.a: $(LIBRARY_OBJS)
803803
# wasm assets directory is relative to current build dir, e.g. "./usr/local".
804804
# --preload-file turns a relative asset path into an absolute path.
805805

806+
.PHONY: wasm_stdlib
807+
wasm_stdlib: $(WASM_STDLIB)
806808
$(WASM_STDLIB): $(srcdir)/Lib/*.py $(srcdir)/Lib/*/*.py \
807809
$(srcdir)/Tools/wasm/wasm_assets.py \
808-
Makefile pybuilddir.txt Modules/Setup.local \
809-
python.html python.worker.js
810+
Makefile pybuilddir.txt Modules/Setup.local
810811
$(PYTHON_FOR_BUILD) $(srcdir)/Tools/wasm/wasm_assets.py \
811812
--buildroot . --prefix $(prefix)
812813

Tools/wasm/wasm_assets.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@
108108
"_zoneinfo": ["zoneinfo/"],
109109
}
110110

111+
SYSCONFIG_NAMES = (
112+
"_sysconfigdata__emscripten_wasm32-emscripten",
113+
"_sysconfigdata__emscripten_wasm32-emscripten",
114+
"_sysconfigdata__wasi_wasm32-wasi",
115+
"_sysconfigdata__wasi_wasm64-wasi",
116+
)
117+
118+
111119
def get_builddir(args: argparse.Namespace) -> pathlib.Path:
112120
"""Get builddir path from pybuilddir.txt
113121
"""
@@ -120,7 +128,11 @@ def get_sysconfigdata(args: argparse.Namespace) -> pathlib.Path:
120128
"""Get path to sysconfigdata relative to build root
121129
"""
122130
data_name = sysconfig._get_sysconfigdata_name()
123-
assert "emscripten_wasm32" in data_name
131+
if not data_name.startswith(SYSCONFIG_NAMES):
132+
raise ValueError(
133+
f"Invalid sysconfig data name '{data_name}'.",
134+
SYSCONFIG_NAMES
135+
)
124136
filename = data_name + ".py"
125137
return args.builddir / filename
126138

Tools/wasm/wasm_build.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import dataclasses
2121
import os
2222
import pathlib
23+
import re
2324
import shlex
2425
import shutil
2526
import subprocess
@@ -99,6 +100,24 @@ def get_emscripten_root(emconfig: pathlib.Path = EM_CONFIG) -> pathlib.PurePath:
99100
EMSCRIPTEN_ROOT = get_emscripten_root()
100101

101102

103+
def read_python_version(configure: pathlib.Path = CONFIGURE) -> str:
104+
"""Read PACKAGE_VERSION from configure script
105+
106+
configure and configure.ac are the canonical source for major and
107+
minor version number.
108+
"""
109+
version_re = re.compile("^PACKAGE_VERSION='(\d\.\d+)'")
110+
with configure.open(encoding="utf-8") as f:
111+
for line in f:
112+
mo = version_re.match(line)
113+
if mo:
114+
return mo.group(1)
115+
raise ValueError(f"PACKAGE_VERSION not found in {configure}")
116+
117+
118+
PYTHON_VERSION = read_python_version()
119+
120+
102121
class ConditionError(ValueError):
103122
def __init__(self, info: str, text: str):
104123
self.info = info
@@ -174,6 +193,9 @@ def _check_emscripten():
174193
raise MissingDependency(os.fspath(version_txt), INSTALL_EMSDK)
175194
with open(version_txt) as f:
176195
version = f.read().strip().strip('"')
196+
if version.endswith("-git"):
197+
# git / upstream / tot-upstream installation
198+
version = version[:-4]
177199
version_tuple = tuple(int(v) for v in version.split("."))
178200
if version_tuple < EMSDK_MIN_VERSION:
179201
raise MissingDependency(
@@ -221,7 +243,7 @@ def _check_wasi():
221243
# workaround for https://github.com/python/cpython/issues/95952
222244
"HOSTRUNNER": (
223245
"wasmtime run "
224-
"--env PYTHONPATH=/{relbuilddir}/build/lib.wasi-wasm32-$(VERSION):/Lib "
246+
"--env PYTHONPATH=/{relbuilddir}/build/lib.wasi-wasm32-{version}:/Lib "
225247
"--mapdir /::{srcdir} --"
226248
),
227249
},
@@ -362,6 +384,7 @@ def getenv(self) -> dict:
362384
env[key] = value.format(
363385
relbuilddir=self.builddir.relative_to(SRCDIR),
364386
srcdir=SRCDIR,
387+
version=PYTHON_VERSION,
365388
)
366389
else:
367390
env[key] = value

configure

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ AC_MSG_RESULT($LDLIBRARY)
15811581

15821582
# LIBRARY_DEPS, LINK_PYTHON_OBJS and LINK_PYTHON_DEPS variable
15831583
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
1584-
[Emscripten/browser*], [LIBRARY_DEPS='$(PY3LIBRARY) $(WASM_STDLIB)'],
1584+
[Emscripten/browser*], [LIBRARY_DEPS='$(PY3LIBRARY) $(WASM_STDLIB) python.html python.worker.js'],
15851585
[LIBRARY_DEPS='$(PY3LIBRARY) $(EXPORTSYMS)']
15861586
)
15871587
LINK_PYTHON_DEPS='$(LIBRARY_DEPS)'

0 commit comments

Comments
 (0)