@@ -268,11 +268,11 @@ fn add(builder: &mut Builder<impl Write>, root: &Path, path: &Path) -> Result<()
268268fn maybe_make_cpython ( repo_dir : & Path , wasi_sdk : & Path ) -> Result < ( ) > {
269269 let cpython_wasi_dir = repo_dir. join ( "cpython/builddir/wasi" ) ;
270270 if !cpython_wasi_dir. join ( "libpython3.14.so" ) . exists ( ) {
271+ fs:: create_dir_all ( & cpython_wasi_dir) ?;
271272 if !cpython_wasi_dir. join ( "libpython3.14.a" ) . exists ( ) {
272273 let cpython_native_dir = repo_dir. join ( "cpython/builddir/build" ) ;
273274 if !cpython_native_dir. join ( PYTHON_EXECUTABLE ) . exists ( ) {
274275 fs:: create_dir_all ( & cpython_native_dir) ?;
275- fs:: create_dir_all ( & cpython_wasi_dir) ?;
276276
277277 run ( Command :: new ( "../../configure" )
278278 . current_dir ( & cpython_native_dir)
@@ -284,6 +284,9 @@ fn maybe_make_cpython(repo_dir: &Path, wasi_sdk: &Path) -> Result<()> {
284284 run ( Command :: new ( "make" ) . current_dir ( cpython_native_dir) ) ?;
285285 }
286286
287+ let lib_install_dir = cpython_wasi_dir. join ( "deps" ) ;
288+ build_zlib ( wasi_sdk, & lib_install_dir) ?;
289+
287290 let config_guess =
288291 run ( Command :: new ( "../../config.guess" ) . current_dir ( & cpython_wasi_dir) ) ?;
289292
@@ -292,7 +295,14 @@ fn maybe_make_cpython(repo_dir: &Path, wasi_sdk: &Path) -> Result<()> {
292295 "CONFIG_SITE" ,
293296 "../../Tools/wasm/wasi/config.site-wasm32-wasi" ,
294297 )
295- . env ( "CFLAGS" , "-fPIC" )
298+ . env (
299+ "CFLAGS" ,
300+ format ! ( "-fPIC -I{}/deps/include" , cpython_wasi_dir. display( ) ) ,
301+ )
302+ . env (
303+ "LDFLAGS" ,
304+ format ! ( "-L{}/deps/lib" , cpython_wasi_dir. display( ) ) ,
305+ )
296306 . current_dir ( & cpython_wasi_dir)
297307 . args ( [
298308 "../../configure" ,
@@ -329,6 +339,7 @@ fn maybe_make_cpython(repo_dir: &Path, wasi_sdk: &Path) -> Result<()> {
329339 . arg ( cpython_wasi_dir. join ( "Modules/_hacl/libHacl_Hash_SHA3.a" ) )
330340 . arg ( cpython_wasi_dir. join ( "Modules/_decimal/libmpdec/libmpdec.a" ) )
331341 . arg ( cpython_wasi_dir. join ( "Modules/expat/libexpat.a" ) )
342+ . arg ( cpython_wasi_dir. join ( "deps/lib/libz.a" ) )
332343 . arg ( "-lwasi-emulated-signal" )
333344 . arg ( "-lwasi-emulated-getpid" )
334345 . arg ( "-lwasi-emulated-process-clocks" )
@@ -388,3 +399,54 @@ fn make_pyo3_config(repo_dir: &Path) -> Result<()> {
388399
389400 Ok ( ( ) )
390401}
402+
403+ fn fetch_extract ( url : & str , out_dir : & Path ) -> Result < ( ) > {
404+ let response = reqwest:: blocking:: get ( url) ?;
405+ let decoder = flate2:: read:: GzDecoder :: new ( response) ;
406+ let mut archive = tar:: Archive :: new ( decoder) ;
407+ archive. unpack ( out_dir) ?;
408+ Ok ( ( ) )
409+ }
410+
411+ fn add_compile_envs ( wasi_sdk : & Path , command : & mut Command ) {
412+ let sysroot = wasi_sdk. join ( "share/wasi-sysroot" ) ;
413+ let sysroot = sysroot. to_string_lossy ( ) ;
414+ command
415+ . env ( "AR" , wasi_sdk. join ( "bin/ar" ) )
416+ . env ( "CC" , wasi_sdk. join ( "bin/clang" ) )
417+ . env ( "RANLIB" , wasi_sdk. join ( "bin/ranlib" ) )
418+ . env (
419+ "CFLAGS" ,
420+ format ! ( "--target=wasm32-wasi --sysroot={sysroot} -I{sysroot}/include/wasm32-wasip1 -D_WASI_EMULATED_SIGNAL -fPIC" ) ,
421+ )
422+ . env (
423+ "LDFLAGS" ,
424+ format ! ( "--target=wasm32-wasip2 --sysroot={sysroot} -L{sysroot}/lib -lwasi-emulated-signal" )
425+ ) ;
426+ }
427+
428+ fn build_zlib ( wasi_sdk : & Path , install_dir : & Path ) -> Result < ( ) > {
429+ let out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) ?) ;
430+ fetch_extract (
431+ "https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz" ,
432+ & out_dir,
433+ ) ?;
434+ let src_dir = out_dir. join ( "zlib-1.3.1" ) ;
435+ let mut configure = Command :: new ( "./configure" ) ;
436+ add_compile_envs ( wasi_sdk, & mut configure) ;
437+ configure
438+ . current_dir ( & src_dir)
439+ . arg ( "--static" )
440+ . arg ( format ! ( "--prefix={}" , install_dir. display( ) ) ) ;
441+ run ( & mut configure) ?;
442+ let mut make = Command :: new ( "make" ) ;
443+ add_compile_envs ( wasi_sdk, & mut make) ;
444+ make. current_dir ( src_dir)
445+ . arg ( format ! ( "AR={}" , wasi_sdk. join( "bin/ar" ) . display( ) ) )
446+ . arg ( "ARFLAGS=rcs" )
447+ . arg ( format ! ( "CC={}" , wasi_sdk. join( "bin/clang" ) . display( ) ) )
448+ . arg ( "static" )
449+ . arg ( "install" ) ;
450+ run ( & mut make) ?;
451+ Ok ( ( ) )
452+ }
0 commit comments