Skip to main content

reth_node_version_build/
lib.rs

1//! Stamps reth's version metadata into a binary from its build script.
2//!
3//! Version metadata changes with every commit, and the build timestamp with every build. A
4//! library that stamped it would rebuild with every crate that depends on it, so binaries stamp
5//! it in their own build script and read it with `reth_node_core::reth_version_metadata!`.
6//!
7//! ```ignore
8//! // build.rs
9//! fn main() -> Result<(), Box<dyn std::error::Error>> {
10//!     reth_node_version_build::emit_version_metadata()
11//! }
12//! ```
13#![doc(
14    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
15    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
16    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
17)]
18#![cfg_attr(not(test), warn(unused_crate_dependencies))]
19#![cfg_attr(docsrs, feature(doc_cfg))]
20
21use smolgen::{Build, Cargo, Emitter, Gitcl};
22use std::{env, error::Error};
23
24/// Emits the version metadata of the binary being built as `cargo:rustc-env` instructions.
25///
26/// Call it from the binary's build script. The package version, features and profile are the
27/// binary's own.
28pub fn emit_version_metadata() -> Result<(), Box<dyn Error>> {
29    let mut emitter = Emitter::default();
30
31    emitter.add_instructions(&Build::builder().build_timestamp(true).build())?;
32    emitter.add_instructions(&Cargo::builder().features(true).target_triple(true).build())?;
33
34    // Tracking the git HEAD reruns the build script on every commit and checkout. Debug builds
35    // skip it and keep the SHA from the script's last run. Release profiles keep tracking HEAD, so
36    // their version always matches the checkout.
37    let git_builder = Gitcl::builder()
38        .describe(false, true, None)
39        .dirty(true)
40        .sha(false)
41        .rerun_on_head_change(env::var("PROFILE")? != "debug")
42        .build();
43    emitter.add_instructions(&git_builder)?;
44
45    emitter.emit_and_set()?;
46    let sha = env::var("VERGEN_GIT_SHA")?;
47    let sha_short = &sha[0..7];
48
49    let is_dirty = env::var("VERGEN_GIT_DIRTY")? == "true";
50    // > git describe --always --tags
51    // if not on a tag: v0.2.0-beta.3-82-g1939939b
52    // if on a tag: v0.2.0-beta.3
53    // git picks the abbreviated hash length from the repository size, so match any SHA prefix.
54    let not_on_tag = env::var("VERGEN_GIT_DESCRIBE")?
55        .rsplit_once("-g")
56        .is_some_and(|(_, hash)| sha.starts_with(hash));
57    let version_suffix = if is_dirty || not_on_tag { "-dev" } else { "" };
58    println!("cargo:rustc-env=RETH_VERSION_SUFFIX={version_suffix}");
59
60    // Set short SHA
61    println!("cargo:rustc-env=VERGEN_GIT_SHA_SHORT={}", &sha[..8]);
62
63    // Set the build profile
64    let out_dir = env::var("OUT_DIR")?;
65    let profile = out_dir.rsplit(std::path::MAIN_SEPARATOR).nth(3).ok_or("unexpected OUT_DIR")?;
66    println!("cargo:rustc-env=RETH_BUILD_PROFILE={profile}");
67
68    // The binary's version from its Cargo.toml.
69    let pkg_version = env::var("CARGO_PKG_VERSION")?;
70
71    // The short version information for reth.
72    // - The latest version from Cargo.toml
73    // - The short SHA of the latest commit.
74    // Example: 0.1.0 (defa64b2)
75    println!("cargo:rustc-env=RETH_SHORT_VERSION={pkg_version}{version_suffix} ({sha_short})");
76
77    // LONG_VERSION
78    // The long version information for reth, one line per variable since a variable can't hold a
79    // newline.
80    //
81    // Example:
82    //
83    // ```text
84    // Version: 0.1.0
85    // Commit SHA: defa64b2
86    // Build Timestamp: 2023-05-19T01:47:19.815651705Z
87    // Build Features: jemalloc
88    // Build Profile: maxperf
89    // ```
90    println!("cargo:rustc-env=RETH_LONG_VERSION_0=Version: {pkg_version}{version_suffix}");
91    println!("cargo:rustc-env=RETH_LONG_VERSION_1=Commit SHA: {sha}");
92    println!(
93        "cargo:rustc-env=RETH_LONG_VERSION_2=Build Timestamp: {}",
94        env::var("VERGEN_BUILD_TIMESTAMP")?
95    );
96    println!(
97        "cargo:rustc-env=RETH_LONG_VERSION_3=Build Features: {}",
98        env::var("VERGEN_CARGO_FEATURES")?
99    );
100    println!("cargo:rustc-env=RETH_LONG_VERSION_4=Build Profile: {profile}");
101
102    // The version information for reth formatted for P2P (devp2p).
103    // - The latest version from Cargo.toml
104    // - The target triple
105    //
106    // Example: reth/v0.1.0-alpha.1-428a6dc2f/aarch64-apple-darwin
107    println!(
108        "cargo:rustc-env=RETH_P2P_CLIENT_VERSION={}",
109        format_args!("reth/v{pkg_version}-{sha_short}/{}", env::var("VERGEN_CARGO_TARGET_TRIPLE")?)
110    );
111
112    Ok(())
113}