Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: failed to run custom build command for libdeflate-sys v1.20.0 #624

Closed
xujizhong opened this issue Jun 18, 2024 · 6 comments
Closed

Comments

@xujizhong
Copy link

xujizhong commented Jun 18, 2024

Hello, I have a problem compiling oxipng to wasm. I have set features = ["freestanding"], but I still get an error.

this is my cargo.toml:

[package]
name = "image-compress"
version = "0.1.0"
edition = "2021"

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-O", "--no-validation"]

[lib]
crate-type = ["cdylib"]

[dependencies]
image = "0.24.5"
wasm-bindgen = "0.2.84"
lodepng = "= 3.7.2"
imagequant = { version = "4.0", default-features = false }
oxipng = { version = "9.1.1", default-features = false, features = ["freestanding"] }

[profile.release]
lto = true
opt-level = "s"

This is an error message

$ wasm-pack build --release --target web                   
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
   Compiling libdeflate-sys v1.20.0
The following warnings were emitted during compilation:

warning: [email protected]: error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
warning: [email protected]: 1 error generated.

error: failed to run custom build command for `libdeflate-sys v1.20.0`

Caused by:
  process didn't exit successfully: `/Users/admin/xjz/target/release/build/libdeflate-sys-70493e8f187140e2/build-script-build` (exit status: 1)
  --- stdout
  OPT_LEVEL = Some("s")
  TARGET = Some("wasm32-unknown-unknown")
  HOST = Some("aarch64-apple-darwin")
  cargo:rerun-if-env-changed=CC_wasm32-unknown-unknown
  CC_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CC_wasm32_unknown_unknown
  CC_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = Some("clang")
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  cargo:rerun-if-env-changed=CFLAGS_wasm32-unknown-unknown
  CFLAGS_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CFLAGS_wasm32_unknown_unknown
  CFLAGS_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:warning=error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
  cargo:warning=1 error generated.

  --- stderr


  error occurred: Command "clang" "-Os" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "libdeflate" "-ffreestanding" "-nostdlib" "-DFREESTANDING" "-o" "/Users/admin/xjz/target/wasm32-unknown-unknown/release/build/libdeflate-sys-5a8b58051cea9a88/out/lib/d76cd4538b6494af-cpu_features.o" "-c" "libdeflate/lib/arm/cpu_features.c" with args clang did not execute successfully (status code exit status: 1).


Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/Users/admin/xjz" && "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"

@AlexTMjugador
Copy link
Collaborator

AlexTMjugador commented Jun 18, 2024

Your build issue appears to be caused by using an outdated or crippled version of clang that doesn't support WebAssembly targets. Other people facing this problem have resolved it by installing clang via Homebrew: rust-lang/libz-sys#103. In any case, it stems from the libdeflate-sys build script, so there is not much we can do about it on OxiPNG.

Interestingly, I couldn't replicate this build error when attempting to compile to WASM on a Linux host. Instead, I encountered a missing module error for the errno crate. errno is a transitive dependency required only for certain targets, and WASM happens to be one of them.

So, currently, I don't consider WebAssembly a supported target for OxiPNG. While OxiPNG itself may not prevent WebAssembly builds, its dependencies are another story. After resolving your clang issue, you might encounter the errno error I mentioned, or other problems. However, if you have the time to experiment, please feel free to try and report back! We might find a way to make OxiPNG build for WebAssembly 😉

@andrews05
Copy link
Collaborator

andrews05 commented Jun 18, 2024

Just for the record, oxipng 9.0 did build successfully in Squoosh, using wasm-pack.
We could certainly add errno as dependency for wasm though.

@xujizhong
Copy link
Author

Your build issue appears to be caused by using an outdated or crippled version of clang that doesn't support WebAssembly targets. Other people facing this problem have resolved it by installing clang via Homebrew: rust-lang/libz-sys#103. In any case, it stems from the libdeflate-sys build script, so there is not much we can do about it on OxiPNG.

Interestingly, I couldn't replicate this build error when attempting to compile to WASM on a Linux host. Instead, I encountered a missing module error for the errno crate. errno is a transitive dependency required only for certain targets, and WASM happens to be one of them.

So, currently, I don't consider WebAssembly a supported target for OxiPNG. While OxiPNG itself may not prevent WebAssembly builds, its dependencies are another story. After resolving your clang issue, you might encounter the errno error I mentioned, or other problems. However, if you have the time to experiment, please feel free to try and report back! We might find a way to make OxiPNG build for WebAssembly 😉

Thank you for your reply. I also tried to compile wasm after seeing the successful case in Squoosh I will try other solutions. If there is a convenient method, I will reply back.

@xujizhong xujizhong reopened this Jun 19, 2024
@AlexTMjugador
Copy link
Collaborator

[...] since this is a current deficiency of the latest oxipng release

Not necessarily though? Frankly, targeting WebAssembly is fragile, and OxiPNG hasn't seen much love for that target since around 2020. Even if back then it worked fine, and we didn't do anything in OxiPNG proper to purposefully break that compatibility, dependencies have also been updated and changed, and as far as I know they weren't audited to guarantee WebAssembly support. This arguably is a deficiency somewhere that should be looked into, but we don't have enough information to confirm that the deficiency indeed belongs to OxiPNG. So far, the facts I've seen point to causes outside of OxiPNG's control.

@andrews05
Copy link
Collaborator

andrews05 commented Jun 19, 2024

I just tried this on a linux machine and it worked fine, without errno. I think it's only needed for the binary feature, which I didn't include.

@AlexTMjugador
Copy link
Collaborator

[...] I think it's only needed for the binary feature, which I didn't include.

Ah, yes, good catch! cargo build --release --target wasm32-unknown-unknown --no-default-features --features freestanding,filetime,parallel,zopfli works fine on my box too after making sure clang is available. I think this is enough to make OxiPNG usable as a library in WASM environments; it makes sense that CLI-specific stuff related to clap that's gated behind the binary feature does not build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants