HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrustCritical

How can I specify binary-only dependencies?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howspecifydependenciesonlycanbinary

Problem

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

There are several ways to simulate what you want:

1) Turn the binaries to examples

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

# Cargo.toml

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

[dev-dependencies] #

To run the binary, use:

cargo run --example ucitest


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.

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

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

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


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

`# ucitest/Cargo.toml

[dependencies]
conformal = { version = "0.1", path = "../" } #

Code Snippets

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

Context

Stack Overflow Q#35711044, score: 120

Revisions (0)

No revisions yet.