use*_*968 5 dependencies build rust rust-cargo
有些板条箱提供了pub const &str-version字符串,有些则没有.要有一个通用的解决方案,我需要一个列表,列出cargo build编译过程中已知和使用的所有依赖项及其版本,这样我就可以构建自己const &str的版本.这是我自己的版本以及我用 -Debug输出编译的所有版本.
是否有可能获得所有依赖项及其版本的列表build.rs?
Cargo.lock似乎是一个很好的来源.它实际上是声音解析Cargo.lock的build.rs?是否可以保证更新到Cargo实际使用并写入磁盘的内容?
小智 0
有一个用于此目的的板条箱:build-info(lib.rs,docs.rs)。
它似乎无法'static str自行生成包含依赖项信息,但它确实会自动解析Cargo.lock相关信息并将其包含到最终的二进制文件中,具有类似的效果。
build-info 0.0.32)如果您不想包含递归依赖项,则此代码比您可能需要的要复杂一些。
fn main() {
build_info_build::build_script().collect_dependencies(true);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
查看cargo-audit和cargo-auditable以获得针对同一问题的面向安全的解决方案,前者甚至在某种程度上适用于本身不包含此功能的程序。不幸的是,虽然cargo audit bin可以用来检查已编译的 Rust 程序中的已知安全漏洞,但我没有看到任何打印它恢复的库列表的标志。