miden_node_utils/version/
mod.rs1#[cfg(feature = "vergen")]
2pub use vergen::vergen;
3
4pub struct LongVersion {
15 pub version: &'static str,
16 pub sha: &'static str,
17 pub branch: &'static str,
18 pub dirty: &'static str,
19 pub features: &'static str,
20 pub rust_version: &'static str,
21 pub host: &'static str,
22 pub target: &'static str,
23 pub opt_level: &'static str,
24 pub debug: &'static str,
25}
26
27impl std::fmt::Display for LongVersion {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 let &Self {
30 version,
31 mut sha,
32 mut branch,
33 dirty,
34 features,
35 rust_version,
36 host,
37 target,
38 opt_level,
39 debug,
40 } = self;
41
42 let dirty = match dirty {
43 "true" => "-dirty",
44 _ => "",
45 };
46
47 if branch == "VERGEN_IDEMPOTENT_OUTPUT" {
52 branch = "";
53 }
54 if sha == "VERGEN_IDEMPOTENT_OUTPUT" {
55 sha = "";
56 }
57
58 f.write_fmt(format_args!(
59 "{version}
60
61SHA: {sha}{dirty}
62branch: {branch}
63features: {features}
64rust version: {rust_version}
65target arch: {target}
66host arch: {host}
67opt-level: {opt_level}
68debug: {debug}
69"
70 ))
71 }
72}
73
74#[cfg(feature = "vergen")]
75mod vergen {
76 use std::path::PathBuf;
77
78 use anyhow::{Context, Result};
79
80 pub fn vergen() -> Result<()> {
94 if let Some(sha) = published_git_sha().context("Checking for published vcs info")? {
95 println!("cargo::rustc-env=VERGEN_GIT_SHA={sha}");
97 println!("cargo::rustc-env=VERGEN_GIT_BRANCH=NA (published)");
98 println!("cargo::rustc-env=VERGEN_GIT_DIRTY=");
99
100 vergen_gitcl::Emitter::new()
101 } else {
102 let mut emitter = vergen_gitcl::Emitter::new();
104 emitter
105 .add_instructions(&git_instructions()?)
106 .context("Adding git instructions")?;
107
108 emitter
109 }
110 .add_instructions(&cargo_instructions()?)
111 .context("Adding cargo instructions")?
112 .add_instructions(&rustc_instructions()?)
113 .context("Adding rustc instructions")?
114 .emit()
115 }
116
117 fn published_git_sha() -> Result<Option<String>> {
123 let cargo_vcs_info = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(".cargo_vcs_info.json");
124 if cargo_vcs_info.exists() {
125 let contents = std::fs::read_to_string(cargo_vcs_info).context("Reading vcs info")?;
127
128 let offset = contents.find(r#""sha1""#).context("Searching for sha1 property")?
136 + r#""sha1""#.len();
137
138 let sha1 = contents[offset + 1..]
139 .chars()
140 .skip_while(|&c| c != '"')
142 .skip(1)
143 .take_while(|&c| c != '"')
145 .take(7)
147 .collect();
148
149 Ok(Some(sha1))
150 } else {
151 Ok(None)
152 }
153 }
154
155 fn git_instructions() -> Result<vergen_gitcl::Gitcl> {
156 const INCLUDE_UNTRACKED: bool = true;
157 const SHORT_SHA: bool = true;
158
159 vergen_gitcl::GitclBuilder::default()
160 .branch(true)
161 .dirty(INCLUDE_UNTRACKED)
162 .sha(SHORT_SHA)
163 .build()
164 .context("Building git instructions")
165 }
166
167 fn cargo_instructions() -> Result<vergen::Cargo> {
168 vergen_gitcl::CargoBuilder::default()
169 .debug(true)
170 .features(true)
171 .target_triple(true)
172 .opt_level(true)
173 .build()
174 .context("Building git instructions")
175 }
176
177 fn rustc_instructions() -> Result<vergen::Rustc> {
178 vergen_gitcl::RustcBuilder::default()
179 .semver(true)
180 .host_triple(true)
181 .build()
182 .context("Building rustc instructions")
183 }
184}