Cross-compiling to WASM fails: build script gets wrong `zip` compression feature
- Dominant language
- Rust
- Stars
- 114
- Forks
- 85
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 42
Description
Hi there!
Thank you for the project!!
I was trying the WASM target and was hitting some problems.
After discussing with AI, it seems there is a fix to do in the Cargo.toml.
### Problem
Cross-compiling MathCAT to a WASM target (e.g. `wasm32-wasip1`) with `--features include-zip` panics in `build.rs`:
```
Error: unsupported Zip archive: Unsupported compression
```
### Cause
The `zip` build-dependency is conditionally gated on `target_family`:
```toml
[target.'cfg(target_family = "wasm")'.build-dependencies]
zip = { version = "7.0", default-features = false, features = ["deflate"] }
[target.'cfg(not(target_family = "wasm"))'.build-dependencies]
zip = { version = "7.0", default-features = false, features = ["bzip2"] }
```
However, Cargo evaluates `cfg()` conditions on `build-dependencies` against the **host** platform, not the cross-compilation target ([[Cargo docs](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies)](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies): "the dependency will only be built when the **host platform** matches the specified target"). So when cross-compiling to WASM from a non-WASM host (the only realistic scenario), the build script is compiled with only `bzip2` support. At runtime, `build.rs` correctly detects the target via `CARGO_CFG_TARGET_FAMILY` and selects DEFLATE, which isn't available.
The same pattern applied to `[target.*.dependencies]` works correctly because those are compiled for the target. The issue is specific to `build-dependencies`.
### Fix
Replace the two conditional `build-dependencies` blocks with a single unconditional one:
```toml
[build-dependencies]
bitflags = "2.6"
zip = { version = "7.0", default-features = false, features = ["bzip2", "deflate"] }
```
This ensures the build script has access to both compression methods regardless of host, and the runtime logic in `build.rs` continues to select the appropriate one for the target.
### Steps to reproduce
```bash
git clone https://github.com/daisy/MathCAT
cd MathCAT
cargo clean
rustup target add wasm32-wasip1
cargo check --target wasm32-wasip1 --features include-zip
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.