From b0f7145f9c7290ab129ecef4b71a5f2e39e21f07 Mon Sep 17 00:00:00 2001 From: Simon Rainerson Date: Fri, 14 May 2021 14:01:35 +0200 Subject: [PATCH] Add support for cross compilation for wasm/wasi When using rust after this commit https://github.com/rust-lang/rust/commit/1a491e2304387c2a50c5a0ae219e8d5a0ce1bc89 the target family for wasm targets is now set to "wasm". Also fix the assumption that OsStrExt lives inside std::os::unix for all non-windows targets. --- build.rs | 2 ++ src/conversions/osstr.rs | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/build.rs b/build.rs index c67e92bc..01731aed 100644 --- a/build.rs +++ b/build.rs @@ -536,6 +536,8 @@ fn load_cross_compile_info(cross_compile_config: CrossCompileConfig) -> Result load_cross_compile_from_sysconfigdata(cross_compile_config), // Use hardcoded interpreter config when targeting Windows Some(os) if os == "windows" => windows_hardcoded_cross_compile(cross_compile_config), + // sysconfigdata works fine on wasm/wasi + Some(os) if os == "wasm" => load_cross_compile_from_sysconfigdata(cross_compile_config), // Waiting for users to tell us what they expect on their target platform Some(os) => bail!( "Unsupported target OS family for cross-compilation: {:?}", diff --git a/src/conversions/osstr.rs b/src/conversions/osstr.rs index be6f405b..466a4417 100644 --- a/src/conversions/osstr.rs +++ b/src/conversions/osstr.rs @@ -22,6 +22,9 @@ impl ToPyObject for OsStr { // https://doc.rust-lang.org/src/std/sys_common/mod.rs.html#59 #[cfg(not(windows))] { + #[cfg(target_os = "wasi")] + let bytes = std::os::wasi::ffi::OsStrExt::as_bytes(self); + #[cfg(not(target_os = "wasi"))] let bytes = std::os::unix::ffi::OsStrExt::as_bytes(self); let ptr = bytes.as_ptr() as *const c_char; @@ -68,6 +71,11 @@ impl FromPyObject<'_> for OsString { }; // Create an OsStr view into the raw bytes from Python + #[cfg(target_os = "wasi")] + let os_str: &OsStr = std::os::wasi::ffi::OsStrExt::from_bytes( + fs_encoded_bytes.as_ref(ob.py()).as_bytes(), + ); + #[cfg(not(target_os = "wasi"))] let os_str: &OsStr = std::os::unix::ffi::OsStrExt::from_bytes( fs_encoded_bytes.as_ref(ob.py()).as_bytes(), );