Is it possible to make the list of default features platform dependent in your Cargo.toml? I'd like to use features to select platform-dependent dependencies.
I'd imagine something like this:
[features]
# on Unix
default = ["feature-a"]
# not on Unix
default = ["feature-b"]
feature-a = ["dep-a"]
feature-b = ["dep-b"]
[dependencies]
dep-a = { version = "*", optional = true }
dep-b = { version = "*", optional = true }
I've tried:
Using [target.'cfg(unix)'.features] does not work, it is ignored:
[target.'cfg(unix)'.features]
default = ["feature-a"]
# -- snip --
Using a build.rs script to enable features based on cfg conditions only partially works. Dependency resolution is done before running build.rs, so this won't import optional dependencies for features enabled in it. This example won't import dep-a:
fn main() {
#[cfg(unix)]
println!("cargo:rustc-cfg=feature=\"feature-a\"");
// -- snip --
}
Can this be achieved within Rust itself, without external scripts?
winit's workaround for this is to just have the features do nothing on the platforms where they're not supported. e.g. the x11 and wayland features do nothing on the platforms where they're not supported.
In order for this to be a bit cleaner, they use the cfg_aliases crate to make new cfg flags: https://docs.rs/cfg_aliases/latest/cfg_aliases/
How winit uses it for x11/wayland: https://github.com/rust-windowing/winit/blob/fbea75d31f4025825e9bf88b99d7c92e4eb79273/build.rs#L47
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With