There's a crate for this: build-info (lib.rs, docs.rs).
It doesn't seem to be able to generate a 'static str
including dependency info on its own, but it does automatically parse Cargo.lock
and include the relevant info into the final binary, with a similar effect.
Sample code (build-info 0.0.32
)
This code is a little bit more complicated than you may need it to be, if you don't want to include recursive dependencies.
build.rs
fn main() {
build_info_build::build_script().collect_dependencies(true);
}
main.rs
use std::collections::BTreeSet;
build_info::build_info!(fn build_info);
/// Collect all of the dependencies of this workspace into a single set.
fn get_dependencies() -> BTreeSet<(&'static str, &'static build_info::semver::Version)> {
// called recursively on each of the dependencies in the tree
fn visit(
info: &'static build_info::CrateInfo,
set: &mut BTreeSet<(&'static str, &'static build_info::semver::Version)>,
) {
set.insert((&info.name, &info.version));
for dep in &info.dependencies {
visit(dep, set);
}
}
let mut set = BTreeSet::new();
let root_info = &build_info().crate_info;
visit(root_info, &mut set);
set
}
fn main() {
for (name, version) in get_dependencies() {
println!("{} {}", name, version);
}
}
Alternative Solutions
Check out cargo-audit and cargo-auditable for a security-oriented solution to the same problem, the former even works to some extent on programs that don't natively include this feature. Unfortunately, while cargo audit bin
can be used to check for known security vulnerabilities in compiled Rust programs, I don't see any flags that print the list of libraries it recovers.
cargo outdated
might be usefull? – NaziCargo.lock
would be a good source but is it actually sound to parseCargo.lock
inbuild.rs
? Is it guaranteed to be up to date and written to disk beforebuild.rs
is run? – Outbid