19
votes

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 = "*"
3

3 Answers

23
votes

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 = "*"
1
votes
# 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 = "*"

-1
votes

There currently is no way to do this. It would be nice, for sure.