19

我正在编写一个跨平台库,它具有特定于平台的依赖项,一个用于类 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,只关心操作系统系列,因此,如果我要匹配每个类似 unix 的目标三元组,那么列表会变得很长。

有没有办法使用特定的依赖项,仅由运行平台货物的操作系统系列决定?就像是:

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

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

3 回答 3

23

就我在这里阅读的文档而言,这现在应该可以工作了:

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

[target.'cfg(windows)'.dependencies]
abc-win = "*"
于 2016-06-11T07:50:02.193 回答
1
# 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 = "*"

于 2019-09-19T08:48:47.383 回答
-1

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

于 2015-05-07T15:21:36.760 回答