问题描述
我正在编写一个跨平台的库,该库具有特定于平台的依赖性,一个用于类Unix平台,一个用于Windows。这些箱子只能在特定平台上编译,因此我不能只将它们全部正常地添加到依赖项下。
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.
在实际的rust代码中,我使用 cfg
属性,例如#[cfg(unix)]
属性,可为某些平台编译某些代码,我想在Cargo.toml中做类似的事情,或在构建脚本中获取相关性。目前,我正在使用这样的目标三元组:
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 = "*"
但是,此列表并不详尽。我不在乎架构或ABI,只在乎OS系列,因此,如果我要为每个类似unix的目标三元组匹配,列表将会很长。
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.
是否有任何方法可以使用仅由运行平台货物的OS系列确定的特定依赖项?
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 = "*"
这篇关于如何根据OS系列具有不同的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!