Skip to content

Commit ecb3f23

Browse files
authored
pythongh-136976: Emscripten: Add _decimal and libmpdec (python#136997)
Adds tooling to build mpdec (and thus _decimal) as part of an Emscripten build.
1 parent dc27218 commit ecb3f23

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

Tools/wasm/emscripten/__main__.py

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import subprocess
99
import sys
1010
import sysconfig
11+
import hashlib
1112
import tempfile
1213
from urllib.request import urlopen
1314
from pathlib import Path
@@ -164,20 +165,60 @@ def make_build_python(context, working_dir):
164165
print(f"🎉 {binary} {version}")
165166

166167

167-
@subdir(HOST_BUILD_DIR, clean_ok=True)
168-
def make_emscripten_libffi(context, working_dir):
169-
shutil.rmtree(working_dir / "libffi-3.4.6", ignore_errors=True)
168+
def check_shasum(file: str, expected_shasum: str):
169+
with open(file, "rb") as f:
170+
digest = hashlib.file_digest(f, "sha256")
171+
if digest.hexdigest() != expected_shasum:
172+
raise RuntimeError(f"Unexpected shasum for {file}")
173+
174+
175+
def download_and_unpack(working_dir: Path, url: str, expected_shasum: str):
170176
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete_on_close=False) as tmp_file:
171-
with urlopen(
172-
"https://github.com/libffi/libffi/releases/download/v3.4.6/libffi-3.4.6.tar.gz"
173-
) as response:
177+
with urlopen(url) as response:
174178
shutil.copyfileobj(response, tmp_file)
175179
tmp_file.close()
180+
check_shasum(tmp_file.name, expected_shasum)
176181
shutil.unpack_archive(tmp_file.name, working_dir)
182+
183+
184+
@subdir(HOST_BUILD_DIR, clean_ok=True)
185+
def make_emscripten_libffi(context, working_dir):
186+
ver = "3.4.6"
187+
libffi_dir = working_dir / f"libffi-{ver}"
188+
shutil.rmtree(libffi_dir, ignore_errors=True)
189+
download_and_unpack(working_dir, f"https://github.com/libffi/libffi/releases/download/v{ver}/libffi-{ver}.tar.gz", "b0dea9df23c863a7a50e825440f3ebffabd65df1497108e5d437747843895a4e")
177190
call(
178191
[EMSCRIPTEN_DIR / "make_libffi.sh"],
179192
env=updated_env({"PREFIX": PREFIX_DIR}),
180-
cwd=working_dir / "libffi-3.4.6",
193+
cwd=libffi_dir,
194+
quiet=context.quiet,
195+
)
196+
197+
198+
@subdir(HOST_BUILD_DIR, clean_ok=True)
199+
def make_mpdec(context, working_dir):
200+
ver = "4.0.1"
201+
mpdec_dir = working_dir / f"mpdecimal-{ver}"
202+
shutil.rmtree(mpdec_dir, ignore_errors=True)
203+
download_and_unpack(working_dir, f"https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-{ver}.tar.gz", "96d33abb4bb0070c7be0fed4246cd38416188325f820468214471938545b1ac8")
204+
call(
205+
[
206+
"emconfigure",
207+
mpdec_dir / "configure",
208+
"CFLAGS=-fPIC",
209+
"--prefix",
210+
PREFIX_DIR,
211+
"--disable-shared",
212+
],
213+
cwd=mpdec_dir,
214+
quiet=context.quiet,
215+
)
216+
call(
217+
[
218+
"make",
219+
"install"
220+
],
221+
cwd=mpdec_dir,
181222
quiet=context.quiet,
182223
)
183224

@@ -315,6 +356,7 @@ def build_all(context):
315356
configure_build_python,
316357
make_build_python,
317358
make_emscripten_libffi,
359+
make_mpdec,
318360
configure_emscripten_python,
319361
make_emscripten_python,
320362
]
@@ -343,6 +385,9 @@ def main():
343385
configure_build = subcommands.add_parser(
344386
"configure-build-python", help="Run `configure` for the " "build Python"
345387
)
388+
make_mpdec_cmd = subcommands.add_parser(
389+
"make-mpdec", help="Clone mpdec repo, configure and build it for emscripten"
390+
)
346391
make_libffi_cmd = subcommands.add_parser(
347392
"make-libffi", help="Clone libffi repo, configure and build it for emscripten"
348393
)
@@ -363,6 +408,7 @@ def main():
363408
build,
364409
configure_build,
365410
make_libffi_cmd,
411+
make_mpdec_cmd,
366412
make_build,
367413
configure_host,
368414
make_host,
@@ -400,6 +446,7 @@ def main():
400446

401447
dispatch = {
402448
"make-libffi": make_emscripten_libffi,
449+
"make-mpdec": make_mpdec,
403450
"configure-build-python": configure_build_python,
404451
"make-build-python": make_build_python,
405452
"configure-host": configure_emscripten_python,

0 commit comments

Comments
 (0)