11#![ cfg_attr( feature = "deny-warnings" , deny( warnings) ) ]
22
3- use std :: env ;
4-
3+ /// This macro creates the version string during compilation from the
4+ /// current environment
55#[ macro_export]
66macro_rules! get_version_info {
77 ( ) => { {
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" ) ) ;
8+ let major = std :: env!( "CARGO_PKG_VERSION_MAJOR" ) . parse:: <u8 >( ) . unwrap( ) ;
9+ let minor = std :: env!( "CARGO_PKG_VERSION_MINOR" ) . parse:: <u8 >( ) . unwrap( ) ;
10+ let patch = std :: env!( "CARGO_PKG_VERSION_PATCH" ) . parse:: <u16 >( ) . unwrap( ) ;
11+ let crate_name = String :: from( std :: env!( "CARGO_PKG_NAME" ) ) ;
1212
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) ;
13+ let host_compiler = std :: option_env!( "RUSTC_RELEASE_CHANNEL" ) . map( str :: to_string) ;
14+ let commit_hash = std :: option_env!( "GIT_HASH" ) . map( str :: to_string) ;
15+ let commit_date = std :: option_env!( "COMMIT_DATE" ) . map( str :: to_string) ;
1616
17- VersionInfo {
17+ $crate :: VersionInfo {
1818 major,
1919 minor,
2020 patch,
@@ -26,6 +26,24 @@ macro_rules! get_version_info {
2626 } } ;
2727}
2828
29+ /// This macro can be used in `build.rs` to automatically set the needed
30+ /// environment values, namely `GIT_HASH`, `COMMIT_DATE` and
31+ /// `RUSTC_RELEASE_CHANNEL`
32+ #[ macro_export]
33+ macro_rules! setup_version_info {
34+ ( ) => { {
35+ println!(
36+ "cargo:rustc-env=GIT_HASH={}" ,
37+ $crate:: get_commit_hash( ) . unwrap_or_default( )
38+ ) ;
39+ println!(
40+ "cargo:rustc-env=COMMIT_DATE={}" ,
41+ $crate:: get_commit_date( ) . unwrap_or_default( )
42+ ) ;
43+ println!( "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}" , $crate:: get_channel( ) ) ;
44+ } } ;
45+ }
46+
2947// some code taken and adapted from RLS and cargo
3048pub struct VersionInfo {
3149 pub major : u8 ,
@@ -101,7 +119,7 @@ pub fn get_commit_date() -> Option<String> {
101119
102120#[ must_use]
103121pub fn get_channel ( ) -> String {
104- match env:: var ( "CFG_RELEASE_CHANNEL" ) {
122+ match std :: env:: var ( "CFG_RELEASE_CHANNEL" ) {
105123 Ok ( channel) => channel,
106124 Err ( _) => {
107125 // if that failed, try to ask rustc -V, do some parsing and find out
@@ -136,8 +154,8 @@ mod test {
136154 fn test_struct_local ( ) {
137155 let vi = get_version_info ! ( ) ;
138156 assert_eq ! ( vi. major, 0 ) ;
139- assert_eq ! ( vi. minor, 2 ) ;
140- assert_eq ! ( vi. patch, 1 ) ;
157+ assert_eq ! ( vi. minor, 3 ) ;
158+ assert_eq ! ( vi. patch, 0 ) ;
141159 assert_eq ! ( vi. crate_name, "rustc_tools_util" ) ;
142160 // hard to make positive tests for these since they will always change
143161 assert ! ( vi. commit_hash. is_none( ) ) ;
@@ -147,7 +165,7 @@ mod test {
147165 #[ test]
148166 fn test_display_local ( ) {
149167 let vi = get_version_info ! ( ) ;
150- assert_eq ! ( vi. to_string( ) , "rustc_tools_util 0.2.1 " ) ;
168+ assert_eq ! ( vi. to_string( ) , "rustc_tools_util 0.3.0 " ) ;
151169 }
152170
153171 #[ test]
@@ -156,7 +174,7 @@ mod test {
156174 let s = format ! ( "{vi:?}" ) ;
157175 assert_eq ! (
158176 s,
159- "VersionInfo { crate_name: \" rustc_tools_util\" , major: 0, minor: 2 , patch: 1 }"
177+ "VersionInfo { crate_name: \" rustc_tools_util\" , major: 0, minor: 3 , patch: 0 }"
160178 ) ;
161179 }
162180}
0 commit comments