I'm working on an embedded crate and would like to use a feature flag to select between hardware variants 833 and 840.
This flag should affect not just my code but also which crates I depend on.
Here's a snippet from my Cargo.toml:
[dependencies]
nrf52840-hal = { version = "0.11", optional = true }
nrf52833-hal = { version = "0.11", optional = true }
[features]
nrf52840 = ["nrf52840-hal"]
nrf52833 = ["nrf52833-hal"]
default = ["nrf52840"]
where my crate uses features to select the correct optional dependency.
Unfortunately, it seems that even if a dependency is optional, Cargo still considers its features when compiling transitive dependencies.
In my case both hal crates depend on nrf-hal-common and use feature flags themselves to select hardware.
But Cargo considers both features set, which causes a compilation error in the nrf-hal-common crate.
Basically, nrf52833-hal/Cargo.toml has:
[dependencies.nrf-hal-common]
version = "0.11.0"
features = ["52833"]
default-features = false
The nrf52840-hal has:
[dependencies.nrf-hal-common]
version = "0.11.0"
features = ["52840"]
default-features = false
and so nrf-hal-common is being compiled with features = ["52833" "52840"]
and failing.
The only option that (I think) might work is to have a wrapper crate for each hardware variant that depends on the appropriate hal crate.
But since I need to use hardware features anyway (for my application code), also having to have separate crates would make me sad.
Is there a tidier way to resolve this?