What cfg expressions are expected to work in Cargo.toml quoted targets?

https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies describes how to add arbitrary cfg(...) expressions to express complex targets in Cargo.toml. This works as expected with target_os, target_arch, etc, but fails when I use expressions like feature = "some_enabled_feature" and build with cargo build --features some_enabled_feature. The fact that Cargo accepts this without any warning and the documentation does not specify any subset of allowed cfg expressions leaves me wondering which part of the process is incorrect. Who can tell me whether this should be filed as missing documentation or a bug in Cargo?

Based on Cargo’s source code, Cargo extracts the list of cfg attributes by running rustc. It passes the target, but not its own generated cfg attributes (including the features, which is why features don’t work there).

So I think the answer is “these ones”:

bash-on-ubuntu-on-windows$ rustc --print=cfg
debug_assertions
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="linux"
target_pointer_width="64"
target_vendor="unknown"
unix
powershell> rustc --print=cfg
debug_assertions
target_arch="x86_64"
target_endian="little"
target_env="msvc"
target_family="windows"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="windows"
target_pointer_width="64"
target_vendor="pc"
windows
3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.