How to have different dependencies depending on OS family

£可爱£侵袭症+ 提交于 2019-12-05 13:34:56

问题


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

回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/29854912/how-to-have-different-dependencies-depending-on-os-family

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!