Conditionalize an entire crate by OS?

How can I tell cargo not to build an entire crate except on certain operating systems? The usual answer "just don't do that" doesn't apply, because this crate is part of a workspace and the rest of the workspace should be built on all operating systems. I want to use commands like cargo build --all in CI, rather than running cargo build separately for each crate. Ideally, I'd like to put something like this in the workspace's Cargo.toml file:

[target.'cfg(unix)'.workspace]
members = ['foo', 'bar', 'baz']
[target.'cfg(not(unix))'.workspace]
members = ['foo', 'bar']

Has anybody else encountered this problem before? How did you fix it?

You can put a #![cfg(unix)] on top of the src/lib.rs file for the baz crate.
That way it's effectively empty for everything but unix platforms.

This is what winapi does. See here. Just compile an empty crate on other platforms.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.