Cykooz / libheif-rs

Safe wrapper to libheif-sys for parsing heif/heic files
MIT License
34 stars 11 forks source link

unable to compile to wasm32-wasi target #22

Open storybehind opened 4 months ago

storybehind commented 4 months ago

When compiling my project that uses this library as a dependency using command cargo wasi build --release, the below error occurs:

libheif-rs-wasi-compile-err

When looking at the source code in src/utils.rs, it can compile only to unix and windows target platform. I'm new to rust and my question is why can't the function just return CString::new(path.as_os_str().as_bytes()).unwrap_or_default(). Can you alter this function to make it work in other target platforms?

Cykooz commented 4 months ago

why can't the function just return CString::new(path.as_os_str().as_bytes()).unwrap_or_default()

Because the structure OsStr returned by method as_os_str() does not implement method as_bytes(). This method is described in the trait OsStrExt. Declaration and implementation of this trait depends on the particular OS.

Cykooz commented 4 months ago

I have added this code into path_to_cstring function:

    #[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
    {
        use std::os::wasi::ffi::OsStrExt;
        CString::new(path.as_os_str().as_bytes()).unwrap_or_default()
    }

You can try to compile libheif-rs from the dev branch of this git-repo.

storybehind commented 4 months ago

When building dev branch by command cargo wasi build --release, many errors occur like below. Any idea?

libheif-rs-dev-wasi-build-err
Cykooz commented 4 months ago

I don't know. Could you look at bindings.rs file somewhere inside target/wasm32-wasi/release/build/libheif-sys-*/out/ directory? This file generates by bindgen crate from headers-file of original libheif library. May be it contains these function with another name.

Cykooz commented 4 months ago

Have you already found a solution how to build the original libheif library (https://github.com/strukturag/libheif) for wasm32-wasi and statically link them to the rust project? I think this is the most painful task in your case.