Sub topic 8
Dev, build and target-specific dependencies
This chapter assumes you can edit Cargo.toml, run cargo add, and understand version requirements and crate features from earlier chapters. Not every crate your project needs belongs in the final program. Cargo lets you declare which dependencies are needed for which job.
Three dependency tables
Cargo keeps separate tables for dependencies that serve different purposes.
| Table | Used for | Compiled into your library or binary? |
|---|---|---|
| `[dependencies]` | Your normal code in `src/` | Yes |
| `[dev-dependencies]` | Tests, examples and benchmarks | No |
| `[build-dependencies]` | The build script, `build.rs` | No |
Keeping these apart gives you smaller builds for people who depend on your crate. Anyone who uses your library downloads and compiles its [dependencies], but not its dev-dependencies.
[dependencies]
tokio = { version = "1", default-features = false, features = ["rt", "macros"] }
[dev-dependencies]
pretty_assertions = "1"
[build-dependencies]
cc = "1"The tokio line also shows the features syntax from the previous chapter. It is a normal dependency with default features turned off.
Dev-dependencies
A dev-dependency is available to unit tests, integration tests in tests/, examples in examples/ and benchmarks. Your code in src/ can only use it inside #[cfg(test)] modules.
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
#[test]
fn adds() {
assert_eq!(2 + 2, 4);
}
}If you use a dev-dependency in ordinary code, the build fails with an "unresolved import" error. That error is a useful signal that you put the crate in the wrong table, or that the code belongs in a test.
Build-dependencies
A build script is a Rust file named build.rs at the package root. Cargo compiles and runs it before compiling the rest of your package. Typical jobs are compiling C code or generating source files. The script can only use [build-dependencies], and your main code cannot use them.
// build.rs
fn main() {
cc::Build::new().file("src/helper.c").compile("helper");
}This example uses the cc crate from the table above to compile a C file.
Adding each kind from the command line
cargo add takes a flag for each table.
cargo add --dev pretty_assertions
cargo add --build ccWithout a flag, cargo add writes to [dependencies].
Target-specific dependencies
Some crates only make sense on one platform. A [target.<condition>.dependencies] table tells Cargo to fetch and build the crate only when the condition matches.
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winuser"] }
[target.'cfg(unix)'.dependencies]
libc = "0.2"
[target.'cfg(target_os = "linux")'.dev-dependencies]
tempfile = "3"The condition is either a cfg(...) expression or a full target triple, such as x86_64-pc-windows-gnu. Quote the key with single quotes, because it contains parentheses and other characters that TOML does not allow in a bare key. The dev-dependencies and build-dependencies variants work the same way.
In your code, guard the matching use and function with the same condition, or the build on other platforms will fail.
#[cfg(unix)]
fn process_id() -> u32 {
unsafe { libc::getpid() as u32 }
}
#[cfg(windows)]
fn process_id() -> u32 {
std::process::id()
}
fn main() {
println!("{}", process_id());
}Normal dependencies also reach tests and examples, which is why the diagram has an arrow from dependencies to that group.
Common errors
- A crate is missing in tests. You put it in
[dependencies]of a different package, or acfgtarget table does not match your platform. - A crate works in tests but not in
src/. It is in[dev-dependencies]. Move it to[dependencies]if the real code needs it. build.rscannot find a crate. It is in[dependencies]. Build scripts need[build-dependencies].- A target table is ignored. The
cfgexpression is not what you expected. Runrustc --print cfgto see the values for your current target.
Exercise
You are writing a library that normally depends on serde. You want to use pretty_assertions in tests and cc in build.rs. Which cargo add commands do you run, and which tables are changed?
Show answer
Run cargo add serde --features derive (adds to [dependencies]), cargo add --dev pretty_assertions (adds to [dev-dependencies]) and cargo add --build cc (adds to [build-dependencies]).