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

h7appiyu  于 2023-10-20  发布在  其他
关注(0)|答案(3)|浏览(139)

我有一个包含二进制文件和库的板条箱。这个库在依赖性方面非常轻,而二进制文件需要更多的依赖性,例如加载文件或做作用域并行的事情。
目前,我的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"

库需要的唯一依赖项是kdtreerand。然而,看起来即使您只构建了库,它也会构建仅二进制的依赖项。我试过使用features和其他技巧,如[[bin].dependencies][ucitest-dependencies](或在[[bin]]下添加一个dependencies= []行),我认为这可能使它们只为二进制文件构建,但我找不到方法。
这些依赖性还不足以使这成为一个问题,但它困扰着我。有没有一种方法可以缩小依赖关系,使它们只为特定的二进制文件构建?

ntjbwcob

ntjbwcob1#

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

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库的crate就不需要下载它们。
从Rust 1.17开始,二进制文件可以声明它们需要打开某些可选功能,有效地使这些库“仅用于二进制文件”。

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

7kqas0il2#

目前,这可能是最好的解决方法,使用[1,2 ]。
目录结构如下:

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

yourlibraryCargo.toml文件:

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

[dependencies]

Cargo.lock文件以及target目录位于项目根目录下,并由工作区中的所有组件共享。工作区组件是从具有locak路径的依赖项自动推断的,但也可以手动指定。
每个组件及其Cargo.toml文件仍然可以在crates.io上单独发布

相关问题