22
33use std:: env;
44
5+ /// This macro creates the version string during compilation from the
6+ /// current environment
57#[ macro_export]
68macro_rules! get_version_info {
79 ( ) => { {
8- let major = env!( "CARGO_PKG_VERSION_MAJOR" ) . parse:: <u8 >( ) . unwrap( ) ;
9- let minor = env!( "CARGO_PKG_VERSION_MINOR" ) . parse:: <u8 >( ) . unwrap( ) ;
10- let patch = env!( "CARGO_PKG_VERSION_PATCH" ) . parse:: <u16 >( ) . unwrap( ) ;
11- let crate_name = String :: from( env!( "CARGO_PKG_NAME" ) ) ;
10+ let major = std :: env!( "CARGO_PKG_VERSION_MAJOR" ) . parse:: <u8 >( ) . unwrap( ) ;
11+ let minor = std :: env!( "CARGO_PKG_VERSION_MINOR" ) . parse:: <u8 >( ) . unwrap( ) ;
12+ let patch = std :: env!( "CARGO_PKG_VERSION_PATCH" ) . parse:: <u16 >( ) . unwrap( ) ;
13+ let crate_name = String :: from( std :: env!( "CARGO_PKG_NAME" ) ) ;
1214
13- let host_compiler = option_env!( "RUSTC_RELEASE_CHANNEL" ) . map( str :: to_string) ;
14- let commit_hash = option_env!( "GIT_HASH" ) . map( str :: to_string) ;
15- let commit_date = option_env!( "COMMIT_DATE" ) . map( str :: to_string) ;
15+ let host_compiler = std :: option_env!( "RUSTC_RELEASE_CHANNEL" ) . map( str :: to_string) ;
16+ let commit_hash = std :: option_env!( "GIT_HASH" ) . map( str :: to_string) ;
17+ let commit_date = std :: option_env!( "COMMIT_DATE" ) . map( str :: to_string) ;
1618
17- VersionInfo {
19+ $crate :: VersionInfo {
1820 major,
1921 minor,
2022 patch,
@@ -26,6 +28,24 @@ macro_rules! get_version_info {
2628 } } ;
2729}
2830
31+ /// This macro can be used in `build.rs` to automatically set the needed
32+ /// environment values, namely `GIT_HASH`, `COMMIT_DATE` and
33+ /// `RUSTC_RELEASE_CHANNEL`
34+ #[ macro_export]
35+ macro_rules! setup_version_info {
36+ ( ) => { {
37+ println!(
38+ "cargo:rustc-env=GIT_HASH={}" ,
39+ $crate:: get_commit_hash( ) . unwrap_or_default( )
40+ ) ;
41+ println!(
42+ "cargo:rustc-env=COMMIT_DATE={}" ,
43+ $crate:: get_commit_date( ) . unwrap_or_default( )
44+ ) ;
45+ println!( "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}" , $crate:: get_channel( ) ) ;
46+ } } ;
47+ }
48+
2949// some code taken and adapted from RLS and cargo
3050pub struct VersionInfo {
3151 pub major : u8 ,
0 commit comments