如何指定仅二进制依赖项?

问题描述 投票:0回答:3

我有一个包含二进制文件和库的板条箱。该库的依赖性非常小,而二进制文件则需要更多的东西,例如加载文件或执行范围内的并行操作。

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

[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"

库需要的唯一依赖项是

kdtree
rand
。然而,即使您只构建库,它也会构建仅二进制依赖项。我尝试过使用
features
和其他技巧,如
[[bin].dependencies]
[ucitest-dependencies]
(或在
dependencies= []
下添加
[[bin]]
行),我认为这些技巧可能会使它们只为二进制文件构建,但我找不到一种方式。

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

rust rust-cargo
3个回答
100
投票

有几种方法可以模拟你想要的:


1)将二进制文件转换为示例

示例和测试是使用

dev-dependencies
构建的,因此您可以将这些依赖项移至本部分。图书馆不会依赖他们。

# 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

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

cargo run --example ucitest

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

可以将依赖项设置为可选,因此依赖于您的

conformal
库的其他包将不需要下载它们。

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

# 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

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

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

当库和二进制文件是单独的包时,您可以执行任何您喜欢的依赖项管理。

# 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"

31
投票

如今,这可能最好通过工作区来解决[12]。

目录结构如下:

project-root
├── Cargo.lock
├── Cargo.toml
├── yourlibary
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── src
│   └── main.rs
└── target

顶级

Cargo.toml
文件:

[package]
name = "yourprogram"
version = "0.1.0"
authors = ["You <[email protected]>"]

[workspace]

[dependencies]
yourlibrary = { path = "yourlibrary" }

yourlibrary
Cargo.toml
文件:

[package]
name = "yourlibrary"
version = "0.1.0"
authors = ["You <[email protected]>"]

[dependencies]

Cargo.lock
文件以及
target
目录位于项目根目录,为工作区中的所有组件共享。工作区组件是从具有锁定路径的依赖项自动推断的,但也可以手动指定。

每个组件及其

Cargo.toml
文件仍然可以在 crates.io 上单独发布


13
投票
© www.soinside.com 2019 - 2024. All rights reserved.