#version-control #tags #git #mercurial #numbers #helper #build

build vcs_version

Helper functions to get version information from VCS

1 unstable release

0.1.0 Mar 7, 2024

#236 in Build Utils

Download history 120/week @ 2024-03-03 16/week @ 2024-03-10 5/week @ 2024-03-17 19/week @ 2024-03-31 1/week @ 2024-04-07 140/week @ 2024-04-14

161 downloads per month

MIT license

12KB
161 lines

README

Docs

Helper functions that can be used from build.rs to extract a version number from Git or Mercurial tags.

Version numbers mostly follow the Python PEP440 conventions. If the current folder corresponds to a version tag, then return that tag. Otherwise, identify the closest tag and return a string of the form tag.dev_N_+_hash_ where N is the number of revisions between the tagged and current revisions. In both cases, if the current folder has been modified, then add the current date as YYYYMMDD to the local version label.

If the current folder isn't a Git or Mercurial checkout, this will try to get the tag, distance and hash from a .hg_archival.txt file as generated by hg archive, then fallback to the version defined in Cargo.toml. Since there is no way to check if the code has been modified, vcs_version will always add a date suffix to the version from .hg_archival.txt or Cargo.toml.

Example usage

Put this in your build.rs:

use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn main()
{
   let out_dir = env::var ("OUT_DIR").unwrap();
   let mut f = File::create (Path::new (&out_dir).join ("build_info.rs")).unwrap();
   let version = vcs_version::get_version();
   writeln!(f, "pub const PKG_NAME: &'static str = \"{} {} {}\";",
            env::var ("CARGO_PKG_NAME").unwrap(),
            version,
            env::var ("PROFILE").unwrap_or ("".into()))
      .unwrap();
   writeln!(f, "pub const PKG_VERSION: &'static str = \"{}\";",
            version)
      .unwrap();
}

Then in your main.rs or lib.rs:

include!(concat!(env!("OUT_DIR"), "/build_info.rs"));

This will define two public string constants:

  • PKG_NAME contains the full package name, including the version number,
  • PKG_VERSION contains only the version.

Dependencies

~4–16MB
~189K SLoC