我有一个包含二进制文件和库的箱子.该库对依赖性非常轻,而二进制文件需要更多,例如,加载文件或做范围并行的事情.
目前,我的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"
Run Code Online (Sandbox Code Playgroud)
库需要的唯一依赖项是kdtree和rand.但是,即使您只构建库,它似乎仍然构建了仅二进制依赖项.我已经尝试使用features各种花样像[[bin].dependencies]或[ucitest-dependencies](或添加dependencies= []线下[[bin]]),我认为可能使他们只建立二进制,但我不能找到一种方法.
这些不足以使这成为一个问题,但它困扰着我.有没有办法缩小依赖关系,以便它们只为特定的二进制文件构建?
ken*_*ytm 25
有几种方法可以模拟您想要的内容:
使用构建示例和测试dev-dependencies,因此您可以将这些依赖项移动到此部分中.图书馆不会依赖它们.
# File structure
conformal/
Cargo.toml
src/
lib.rs
examples/ # <-- the `ucitest` is
ucitest.rs # <-- moved to here
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)
要运行二进制文件,请使用:
cargo run --example ucitest
Run Code Online (Sandbox Code Playgroud)
依赖关系可以是可选的,因此依赖于您的conformal库的其他包装箱不需要下载它们.
从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"] # <--
Run Code Online (Sandbox Code Playgroud)
请注意,--features build-binary在构建二进制文件时需要手动传递:
cargo run --features build-binary --bin ucitest
Run Code Online (Sandbox Code Playgroud)
当库和二进制文件是独立的包时,您可以执行任何依赖项管理.
# File structure
conformal/
Cargo.toml
src/
lib.rs
ucitest/ # <-- move ucitest
Cargo.toml # <-- into its own
src/ # <-- package.
main.rs
Run Code Online (Sandbox Code Playgroud)
# 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"
Run Code Online (Sandbox Code Playgroud)
目录结构如下:
project-root
??? Cargo.lock
??? Cargo.toml
??? yourlibary
? ??? Cargo.toml
? ??? src
? ??? lib.rs
??? src
? ??? main.rs
??? target
Run Code Online (Sandbox Code Playgroud)
顶级Cargo.toml文件:
[package]
name = "yourprogram"
version = "0.1.0"
authors = ["You <you@example.com>"]
[workspace]
[dependencies]
yourlibrary = { path = "yourlibrary" }
Run Code Online (Sandbox Code Playgroud)
yourlibrary Cargo.toml 文件:
[package]
name = "yourlibrary"
version = "0.1.0"
authors = ["You <you@example.com>"]
[dependencies]
Run Code Online (Sandbox Code Playgroud)
该Cargo.lock文件和target目录位于项目根目录中,并由工作空间中的所有组件共享.工作区组件是从具有locak路径的依赖项自动推断的,但也可以手动指定.
带有Cargo.toml文件的每个组件仍然可以在crates.io上单独发布
| 归档时间: |
|
| 查看次数: |
3439 次 |
| 最近记录: |