Skip to content

Cross-compiling to WASM fails: build script gets wrong zip compression feature #519

@saona-raimundo

Description

@saona-raimundo

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:

[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): "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:

[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

git clone https://github.com/daisy/MathCAT
cd MathCAT
cargo clean
rustup target add wasm32-wasip1
cargo check --target wasm32-wasip1 --features include-zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions