问题 如何根据OS系列具有不同的依赖关系


我正在编写一个具有特定于平台的依赖项的跨平台库,一个用于类似unix的平台,另一个用于windows。这些包只能在特定的平台上编译,因此我不能只在正常的依赖项下添加它们。

在我使用的实际生锈代码中 cfg 属性,比如 #[cfg(unix)] 为某些平台编译某些代码,我想在Cargo.toml或构建脚本中为依赖项做类似的事情。目前,我正在使用这样的目标三元组:

[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的目标三元组。

有没有办法使用特定的依赖关系,只能由运行的平台的OS系列运行?就像是:

[target.family.unix.dependencies]
abc-sys = "*"
def = "*"

[target.family.windows.dependencies]
abc-win = "*"

3600
2018-04-24 18:28


起源



答案:


据我读到的文档 这里,现在应该工作:

[target.'cfg(unix)'.dependencies]
abc-sys = "*"
def = "*"

[target.'cfg(windows)'.dependencies]
abc-win = "*"

10
2018-06-11 07:50



是的,现在这是真的。我的答案很老,现在不准确。 - Steve Klabnik


答案:


据我读到的文档 这里,现在应该工作:

[target.'cfg(unix)'.dependencies]
abc-sys = "*"
def = "*"

[target.'cfg(windows)'.dependencies]
abc-win = "*"

10
2018-06-11 07:50



是的,现在这是真的。我的答案很老,现在不准确。 - Steve Klabnik


目前无法做到这一点。肯定会很好。


0
2018-05-07 15:21



这个答案现在已经过时了。请参见 @Andrew Straw的答案。 - Toothbrush