如何指定仅二进制依赖项? [英] How can I specify binary-only dependencies?

查看:57
本文介绍了如何指定仅二进制依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有二进制文件和库的板条箱.该库的依赖项非常少,而二进制文件需要更多的东西,例如加载文件或执行范围并行的事情.

I have a crate with both a binary and a library. The library is extremely light on dependencies, while the binary requires quite a bit more to, e.g., load files or do scoped parallel things.

目前,我的 Cargo.toml 设置如下:

Currently, I have my Cargo.toml set up like this:

[dependencies.kdtree]
path = "../kdtree"

[dependencies]
rand="0.3.0"
rustc-serialize = "0.3"
csv = {git = "https://github.com/BurntSushi/rust-csv.git"}
crossbeam = "0.2"
num_cpus = "0.2"

[lib]
name = "conformal"
path = "src/lib.rs"

[[bin]]
name = "ucitest"
path = "src/bin/main.rs"

库所需的唯一依赖项是 kdtreerand.但是,似乎即使您只构建库,它也会继续构建仅二进制依赖项.我试过使用 features 和其他技巧,如 [[bin].dependencies][ucitest-dependencies] (或添加一个 [[bin]] 下的 >dependencies= [] 行),我认为可能会使它们只为二进制文件构建,但我找不到方法.

The only dependencies the library needs are the kdtree and rand. However, it seems like even if you only build the library, it goes and builds the binary-only dependencies anyway. I've tried using features and other tricks like [[bin].dependencies] or [ucitest-dependencies] (or adding a dependencies= [] line under [[bin]]) that I thought might make them only build for the binary, but I can't find a way.

这些依赖项不足以使这成为问题,但它困扰着我.有没有办法缩小依赖关系,以便它们只为特定的二进制文件构建?

These aren't enough dependencies to make this a problem, but it's bothering me. Is there a way to narrow down dependencies so they only build for specific binaries?

推荐答案

有几种方法可以模拟您想要的内容:

There are several ways to simulate what you want:

示例 和测试是使用 dev-dependencies 构建的,因此您可以将这些依赖项移至此部分.图书馆不会依赖它们.

Examples and tests are built with dev-dependencies, so you could move those dependencies into this section. The library won't depend on them.

# File structure
conformal/
    Cargo.toml
    src/
        lib.rs
    examples/        # <-- the `ucitest` is
        ucitest.rs   # <-- moved to here

# Cargo.toml

[dependencies]
kdtree = { path = "../kdtree" }
rand = "0.3"

[dev-dependencies]  # <-- move the examples-only dependencies here
serde = "1"
csv = "0.15"
crossbeam = "0.3"
num_cpus = "1"

[lib]
name = "conformal"

[[example]]         # <--- declare the executable
name = "ucitest"    # <--- as an example

要运行二进制文件,请使用:

To run the binary, use:

cargo run --example ucitest

<小时>

2) 具有所需功能的可选依赖项

依赖项可以可选,所以其他依赖于您的 conformal 库不需要下载它们.


2) Optional dependencies with required features

Dependencies can be made optional, so other crates that depend on your conformal library won't need to download them.

从 Rust 1.17 开始,二进制文件可以声明它们require 要打开某些可选功能,有效地使这些库只需要二进制文件".

Starting from Rust 1.17, binaries can declare they require certain optional features to be turned on, effectively making those libraries "needed only for binaries".

# Cargo.toml

[dependencies]
kdtree = { path = "../kdtree" }
rand = "0.3"

serde = { version = "1", optional = true }          # <-- make 
csv = { version = "0.15", optional = true }         # <-- all of
crossbeam = { version = "0.3", optional = true }    # <-- them
num_cpus = { version = "1", optional = true }       # <-- optional

[lib]
name = "conformal"

[features]
build-binary = ["serde", "csv", "crossbeam", "num_cpus"]

[[bin]]         
name = "ucitest"    
required-features = ["build-binary"]     # <--

请注意,在构建二进制文件时,您需要手动传递--features build-binary:

Note that you need to manually pass --features build-binary when building the binaries:

cargo run --features build-binary --bin ucitest

<小时>

3) 将二进制文件作为自己的包

当库和二进制文件是单独的包时,你可以做任何你喜欢的依赖管理.


3) Make the binaries as its own package

You could do whatever dependency management you like when the library and the binary are separate packages.

# File structure
conformal/
    Cargo.toml
    src/
        lib.rs
    ucitest/          # <-- move ucitest
        Cargo.toml    # <-- into its own
        src/          # <-- package.
            main.rs 

# ucitest/Cargo.toml 

[dependencies]
conformal = { version = "0.1", path = "../" }   # <-- explicitly depend on the library
serde = "1"
csv = "0.15"
crossbeam = "0.3"
num_cpus = "1"

这篇关于如何指定仅二进制依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆