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

Bry*_*yal 13 unix windows dependency-management rust rust-cargo

我正在编写一个跨平台的库,它具有特定于平台的依赖关系,一个用于类似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 = "*"
Run Code Online (Sandbox Code Playgroud)

但是,这份清单远非详尽无遗.我不关心架构或ABI,只关心OS系列,因此,列表会变得很长,我是否匹配每个类似unix的目标三元组.

是否有任何方法可以使用特定的依赖关系,仅由平台货运的OS系列确定运行?就像是:

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

[target.family.windows.dependencies]
abc-win = "*"
Run Code Online (Sandbox Code Playgroud)

And*_*raw 14

至于我在这里阅读文档,现在应该可以工作:

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

[target.'cfg(windows)'.dependencies]
abc-win = "*"
Run Code Online (Sandbox Code Playgroud)

  • 是的,现在这是真的.我的答案很老,现在不准确. (2认同)