I'm writing a cross-platform library that has platform specific dependencies, one for unix-like platforms, and one for windows. These crates only compile on specific platforms, wherefore I can't just add them all under dependencies normally.
In the actual rust code I use cfg
attributes, like #[cfg(unix)]
to compile certain code for certain platforms, and I want to do something similar in the Cargo.toml, or in a build script, for the dependencies. Currently, I'm using target triplets like these:
[target.i686-unknown-linux-gnu.dependencies.crate1]
git = repo1
[target.x86-unknown-linux-gnu.dependencies.crate1]
git = repo1
[target.x86_64-unknown-linux-gnu.dependencies.crate1]
git = repo1
[target.i686-pc-windows-gnu.dependencies]
crate2 = "*"
[target.x86-pc-windows-gnu.dependencies]
crate2 = "*"
[target.x86_64-pc-windows-gnu.dependencies]
crate2 = "*"
However, this list is far from exhaustive. I don't care about architecture or ABI, only OS family, and as such, the list would get very long, were I to match for every single unix-like target triple.
Is there any way to use specific dependencies, determined only by the OS family of the platform cargo is run on? Something like:
[target.family.unix.dependencies]
abc-sys = "*"
def = "*"
[target.family.windows.dependencies]
abc-win = "*"
As far as I read the docs here, this should now work:
[target.'cfg(unix)'.dependencies]
abc-sys = "*"
def = "*"
[target.'cfg(windows)'.dependencies]
abc-win = "*"
# macos dependencies
[target.'cfg(target_os = "macos")'.dependencies]
dep1 = "*"
dep2 = "*"
# windows dependencies
[target.'cfg(target_os = "windows")'.dependencies]
dep3 = "*"
dep4 = "*"
# regular dependencies
[dependencies]
dep5 = "*"
dep6 = "*"
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