'How can I specify binary-only dependencies?

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.

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"

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?



Solution 1:[1]

These days this is probably best solved with workspaces [1, 2].

The directory structure is as follows:

project-root
??? Cargo.lock
??? Cargo.toml
??? yourlibary
?   ??? Cargo.toml
?   ??? src
?       ??? lib.rs
??? src
?   ??? main.rs
??? target

The top-level Cargo.toml file:

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

[workspace]

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

yourlibrary Cargo.toml file:

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

[dependencies]

The Cargo.lock file as well as the target directory is at the project root directory and is shared by all the components in the workspace. Workspace components are inferred automatically from dependencies with locak path, but can be specified manually as well.

Each component with its Cargo.toml file can still be published separately on crates.io

Solution 2:[2]

This is not implemented yet in Cargo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shepmaster
Solution 2 Shepmaster