How to remove windows dependencies from Linux-only project

I have a fairly simple Cargo.toml for a project:

tokio = { version = "1", features = ["full"] }
warp = { version = "0.3", features = ["tls"] }
simplelog = "0.12.1"
log = "0.4.17"
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
confy = "0.5.1"
clap = { version = "4.2.7", features = ["derive", "cargo"] }
lazy_static = "1.4.0"
bytes = "1.4.0"
sysinfo = "0.29.0"
prost = "0.11"
# Only necessary if using Protobuf well-known types:
prost-types = "0.11"
serde_qs = "0.12.0"
anyhow = "1.0.71"
futures = "0.3.28"
derive-new = "0.5.9"
nix = { version = "0.26.2", default-features = false, features = ["signal"] }

I had to run cargo bitbake for something, and it printed many windows related dependencies:

    crate://crates.io/winapi-i686-pc-windows-gnu/0.4.0 \
    crate://crates.io/winapi-util/0.1.5 \
    crate://crates.io/winapi-x86_64-pc-windows-gnu/0.4.0 \
    crate://crates.io/winapi/0.3.9 \
    crate://crates.io/windows-sys/0.48.0 \
    crate://crates.io/windows-targets/0.48.0 \
    crate://crates.io/windows_aarch64_gnullvm/0.48.0 \
    crate://crates.io/windows_aarch64_msvc/0.48.0 \
    crate://crates.io/windows_i686_gnu/0.48.0 \
    crate://crates.io/windows_i686_msvc/0.48.0 \
    crate://crates.io/windows_x86_64_gnu/0.48.0 \
    crate://crates.io/windows_x86_64_gnullvm/0.48.0 \
    crate://crates.io/windows_x86_64_msvc/0.48.0 \

Does anyone know if there is a way to not include/remove these. They seem quite unnecessary (potentially increasing the binary size), if the project is only ever intended to be used on Linux.

If you are compiling on Linux, then Windows-specific code won't be included in the executable, for the simple reason that it can't be compiled on Linux. So if such a project successfully builds, then that means windows-only code has been #[cfg(…)]'d out.

6 Likes

False positive? Is the tool accurate for assessing such things?

Does "i686" appear in your Cargo.lock file? (It's a rhetorical question. @H2CO3 provided the answer to move forward.)

Such dependencies can and should be in a [target.'cfg(windows)'.dependencies] section which removes then from reverb being considered in a build, but so far as I can tell they are still fetched, most likely so lock file can be consistent across platforms.

3 Likes

Dependencies for other platforms are fetched when using cargo fetch or cargo vendor, but not when doing a regular build. The registry contains enough information to produce a valid lock file mentioning these dependencies without having to download the package tarballs.

2 Likes

This seems to be a bug in cargo bitcake which makes it include dependencies that would otherwise be disabled by cfg filters.

1 Like