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

How to have different dependencies depending on OS family

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

在实际的 rust 代码中我使用 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 平台 cargo 的家族决定 运行?类似于:

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

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

目前没有办法做到这一点。肯定会很好。

据我阅读文档 here,这现在应该可以工作了:

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

[target.'cfg(windows)'.dependencies]
abc-win = "*"
# 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 = "*"