Build 获取活动依赖项及其版本的列表;货物建造“;

Build 获取活动依赖项及其版本的列表;货物建造“;,build,dependencies,rust,rust-cargo,Build,Dependencies,Rust,Rust Cargo,有些板条箱提供pubconst&str-版本字符串,有些则不提供。为了有一个通用的解决方案,我需要一个所有依赖项及其版本的列表,这些依赖项和版本是编译期间cargo build所知道和使用的,这样我就可以构建我自己的const&str,这是我自己的版本,所有版本我都是用调试输出编译的 是否可以在build.rs中获取所有依赖项及其版本的列表 Cargo.lock似乎是一个很好的来源。在build.rs中解析Cargo.lock是否确实合理?是否保证已更新到Cargo实际使用的内容并写入磁盘 我自

有些板条箱提供pub
const&str
-版本字符串,有些则不提供。为了有一个通用的解决方案,我需要一个所有依赖项及其版本的列表,这些依赖项和版本是编译期间
cargo build
所知道和使用的,这样我就可以构建我自己的
const&str
,这是我自己的版本,所有版本我都是用调试输出编译的

是否可以在
build.rs
中获取所有依赖项及其版本的列表


Cargo.lock
似乎是一个很好的来源。在
build.rs
中解析
Cargo.lock
是否确实合理?是否保证已更新到Cargo实际使用的内容并写入磁盘

我自己的答案,通过解析
Cargo.lock
解决。下面给了我们一个依赖项和依赖项的依赖项列表-每个最终以某种方式链接的板条箱(lto除外)

Cargo.toml
中:

[package]
name = "mycrate"
version = "0.1.0"
authors = ["John Doe"]
build = "build.rs"

[build-dependencies]
toml = "0.2"
build.rs
中:

extern crate toml;

use std::env;
use std::fs;
use std::path;
use std::io::{Read, Write};

fn main() {
    // Read Cargo.lock and de-toml it
    let mut lock_buf = String::new();
    fs::File::open("Cargo.lock").unwrap().read_to_string(&mut lock_buf).unwrap();
    let lock_toml = toml::Parser::new(&lock_buf).parse().unwrap();

    // Get the table of [[package]]s. This is the deep list of dependencies and dependencies of
    // dependencies.
    let mut packages = Vec::new();
    for package in lock_toml.get("package").unwrap().as_slice().unwrap() {
        let package = package.as_table().unwrap();
        packages.push((package.get("name").unwrap().as_str().unwrap(),
                       package.get("version").unwrap().as_str().unwrap()));
    }
    packages.sort();

    // Write out the file to be included in the module stub
    let out_dir = env::var("OUT_DIR").unwrap();
    let mut versions_file = fs::File::create(&path::Path::new(&out_dir).join("versions.include")).unwrap();
    versions_file.write(format!("pub const BUILD_DEPS: [(&'static str, &'static str); {}] = [", packages.len()).as_ref()).unwrap();
    for package in packages {
        versions_file.write(format!("(\"{}\", \"{}\"),\n", package.0, package.1).as_ref()).unwrap();
    }
    versions_file.write("];".as_ref()).unwrap();
}
//! Information about the build-environment

// More info from env!() and friends go here

include!(concat!(env!("OUT_DIR"), "/versions.include"));
src/versions.rs
中:

extern crate toml;

use std::env;
use std::fs;
use std::path;
use std::io::{Read, Write};

fn main() {
    // Read Cargo.lock and de-toml it
    let mut lock_buf = String::new();
    fs::File::open("Cargo.lock").unwrap().read_to_string(&mut lock_buf).unwrap();
    let lock_toml = toml::Parser::new(&lock_buf).parse().unwrap();

    // Get the table of [[package]]s. This is the deep list of dependencies and dependencies of
    // dependencies.
    let mut packages = Vec::new();
    for package in lock_toml.get("package").unwrap().as_slice().unwrap() {
        let package = package.as_table().unwrap();
        packages.push((package.get("name").unwrap().as_str().unwrap(),
                       package.get("version").unwrap().as_str().unwrap()));
    }
    packages.sort();

    // Write out the file to be included in the module stub
    let out_dir = env::var("OUT_DIR").unwrap();
    let mut versions_file = fs::File::create(&path::Path::new(&out_dir).join("versions.include")).unwrap();
    versions_file.write(format!("pub const BUILD_DEPS: [(&'static str, &'static str); {}] = [", packages.len()).as_ref()).unwrap();
    for package in packages {
        versions_file.write(format!("(\"{}\", \"{}\"),\n", package.0, package.1).as_ref()).unwrap();
    }
    versions_file.write("];".as_ref()).unwrap();
}
//! Information about the build-environment

// More info from env!() and friends go here

include!(concat!(env!("OUT_DIR"), "/versions.include"));
src/main.rs中或任何需要的地方:

mod versions;

fn main() {
    println!("I was built using {}", versions::BUILD_DEPS.iter().map(|&(ref pkg, ref ver)| format!("{} {}", pkg, ver)).collect::<Vec<_>>().join(", "));
}
mod版本;
fn main(){
println!(“我是使用{}”,versions::BUILD_DEPS.iter().map(|&(ref-pkg,ref-ver)| format!({}{},pkg,ver)).collect::().join(“,”);
}
然后输出类似于

我是用安卓胶水0.2.1,bitflags 0.3.3,bitflags 0.4.0构建的, 位标志0.6.0、位标志0.7.0、块0.1.6、字节顺序0.5.3、字节 0.3.0,cfg if 0.1.0,cgl 0.1.5,cgmath 0.7.0,clippy 0.0.104

当然,我们可以在编译时构建一个类似字符串的表示形式,但是,将其整齐地分开可以在运行时解析这些信息。
就我所知,如果没有
Cargo.lock
的话,这种方法是有效的:Cargo总是在运行
build.rs
之前生成它。

也许
Cargo过时的
可能有用?Cargo lock文件包含所有这些信息,但我不确定它是在构建过程中的哪一点生成的。
Cargo.lock
将是一个好方法源代码,但在
build.rs
中解析
Cargo.lock
是否确实合理?在运行
build.rs
之前,它是否保证是最新的并写入磁盘?这是我不确定也不想依赖的:)