Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have different dependencies depending on OS family

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 = "*"
like image 261
Bryal Avatar asked Apr 24 '15 18:04

Bryal


2 Answers

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 = "*"
like image 190
Andrew Straw Avatar answered Sep 29 '22 15:09

Andrew Straw


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

like image 31
Дмитрий Васильев Avatar answered Sep 29 '22 14:09

Дмитрий Васильев