【问题标题】:How to have different dependencies depending on OS family如何根据操作系统系列具有不同的依赖关系
【发布时间】:2015-07-03 11:54:19
【问题描述】:

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

在实际的 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 = "*"

【问题讨论】:

    标签: windows unix rust dependency-management rust-cargo


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      # 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 = "*"
      
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 这个答案现在已经过时了。请参阅 @Andrew Straw 的回答。
        猜你喜欢
        • 1970-01-01
        • 2019-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-09
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多