88import subprocess
99import sys
1010import sysconfig
11+ import hashlib
1112import tempfile
1213from urllib .request import urlopen
1314from pathlib import Path
@@ -164,20 +165,59 @@ 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+ "--prefix" ,
209+ PREFIX_DIR ,
210+ "--disable-shared" ,
211+ ],
212+ cwd = mpdec_dir ,
213+ quiet = context .quiet ,
214+ )
215+ call (
216+ [
217+ "make" ,
218+ "install"
219+ ],
220+ cwd = mpdec_dir ,
181221 quiet = context .quiet ,
182222 )
183223
@@ -315,6 +355,7 @@ def build_all(context):
315355 configure_build_python ,
316356 make_build_python ,
317357 make_emscripten_libffi ,
358+ make_mpdec ,
318359 configure_emscripten_python ,
319360 make_emscripten_python ,
320361 ]
@@ -343,6 +384,9 @@ def main():
343384 configure_build = subcommands .add_parser (
344385 "configure-build-python" , help = "Run `configure` for the " "build Python"
345386 )
387+ make_mpdec_cmd = subcommands .add_parser (
388+ "make-mpdec" , help = "Clone libffi repo, configure and build it for emscripten"
389+ )
346390 make_libffi_cmd = subcommands .add_parser (
347391 "make-libffi" , help = "Clone libffi repo, configure and build it for emscripten"
348392 )
@@ -363,6 +407,7 @@ def main():
363407 build ,
364408 configure_build ,
365409 make_libffi_cmd ,
410+ make_mpdec_cmd ,
366411 make_build ,
367412 configure_host ,
368413 make_host ,
@@ -400,6 +445,7 @@ def main():
400445
401446 dispatch = {
402447 "make-libffi" : make_emscripten_libffi ,
448+ "make-mpdec" : make_mpdec ,
403449 "configure-build-python" : configure_build_python ,
404450 "make-build-python" : make_build_python ,
405451 "configure-host" : configure_emscripten_python ,
0 commit comments