NOTE: Version 2.x.x is compatible with Version 1.x.x, but introduces a completely new way to use the
constants without having to use the include!
macro.
NOTE: Version 1.x.x is a breaking change from the 0.1.0 series. This crate no longer generates functions to display the build time information, but rather generates constants. See below for more detail.
vergen
, when used in conjunction with the [Build Scripts] support in
cargo, can either
env!
macro. See the
documentation for VergenKey
for the environment variables names.OUT_DIR
(defined by cargo) with up to 8 build time
constants. This file can then be used with the include!
macro to pull the
constants into your source for use.```toml [package]
build = "build.rs"
[dependencies]
[build-dependencies] vergen = "2" ```
build.rs
```rust extern crate vergen;
use vergen::{ConstantsFlags, Result, Vergen};
fn main() { gen_constants().expect("Unable to generate vergen constants!"); }
fn gen_constants() -> Result<()> { let vergen = Vergen::new(ConstantsFlags::all())?;
for (k, v) in vergen.build_info() {
println!("cargo:rustc-env={}={}", k.name(), v);
}
Ok(())
} ```
rust
fn my_fn() {
println!("Build Timestamp: {}", env!("VERGEN_BUILD_TIMESTAMP"));
}
build.rs
```rust extern crate vergen;
use vergen::{ConstantsFlags, Result, vergen};
fn main() { let mut flags = ConstantsFlags::all(); flags.toggle(ConstantsFlags::BUILD_TIMESTAMP); vergen(flags).expect("Unable to generate constants!"); } ```
version.rs
```rust /// Compile Time (UTC) pub const VERGENBUILDTIMESTAMP: &str = "2018-08-09T15:15:57.282334589+00:00";
/// Compile Time - Short (UTC) pub const VERGENBUILDDATE: &str = "2018-08-09";
/// Commit SHA pub const VERGEN_SHA: &str = "75b390dc6c05a6a4aa2791cc7b3934591803bc22";
/// Commit SHA - Short pub const VERGENSHASHORT: &str = "75b390d";
/// Commit Date pub const VERGENCOMMITDATE: &str = "'2018-08-08'";
/// Target Triple pub const VERGENTARGETTRIPLE: &str = "x86_64-unknown-linux-gnu";
/// Semver pub const VERGEN_SEMVER: &str = "v0.1.0-pre.0";
/// Semver (Lightweight) pub const VERGENSEMVERLIGHTWEIGHT: &str = "v0.1.0-pre.0"; ```
```rust include!(concat!(env!("OUT_DIR"), "/version.rs"));
format!("{} {} blah {}", VERGENCOMMITDATE, VERGENSHA, VERGENSEMVER) ```
Licensed under either of * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.