@@ -28,9 +28,8 @@ macro_rules! get_version_info {
2828 } } ;
2929}
3030
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`
31+ /// This macro can be used in `build.rs` to automatically set the needed environment values, namely
32+ /// `GIT_HASH`, `COMMIT_DATE` and `RUSTC_RELEASE_CHANNEL`
3433#[ macro_export]
3534macro_rules! setup_version_info {
3635 ( ) => { {
@@ -43,7 +42,11 @@ macro_rules! setup_version_info {
4342 "cargo:rustc-env=COMMIT_DATE={}" ,
4443 $crate:: get_commit_date( ) . unwrap_or_default( )
4544 ) ;
46- println!( "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}" , $crate:: get_channel( ) ) ;
45+ let compiler_version = $crate:: get_compiler_version( ) ;
46+ println!(
47+ "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}" ,
48+ $crate:: get_channel( compiler_version)
49+ ) ;
4750 } } ;
4851}
4952
@@ -87,16 +90,17 @@ impl std::fmt::Debug for VersionInfo {
8790 "VersionInfo {{ crate_name: \" {}\" , major: {}, minor: {}, patch: {}" ,
8891 self . crate_name, self . major, self . minor, self . patch,
8992 ) ?;
90- if self . commit_hash . is_some ( ) {
91- write ! (
92- f,
93- ", commit_hash: \" {}\" , commit_date: \" {}\" }}" ,
94- self . commit_hash. clone( ) . unwrap_or_default( ) . trim( ) ,
95- self . commit_date. clone( ) . unwrap_or_default( ) . trim( )
96- ) ?;
97- } else {
98- write ! ( f, " }}" ) ?;
93+ if let Some ( ref commit_hash) = self . commit_hash {
94+ write ! ( f, ", commit_hash: \" {}\" " , commit_hash. trim( ) , ) ?;
95+ }
96+ if let Some ( ref commit_date) = self . commit_date {
97+ write ! ( f, ", commit_date: \" {}\" " , commit_date. trim( ) ) ?;
9998 }
99+ if let Some ( ref host_compiler) = self . host_compiler {
100+ write ! ( f, ", host_compiler: \" {}\" " , host_compiler. trim( ) ) ?;
101+ }
102+
103+ write ! ( f, " }}" ) ?;
100104
101105 Ok ( ( ) )
102106 }
@@ -152,22 +156,27 @@ pub fn get_commit_date() -> Option<String> {
152156}
153157
154158#[ must_use]
155- pub fn get_channel ( ) -> String {
159+ pub fn get_compiler_version ( ) -> Option < String > {
160+ get_output ( "rustc" , & [ "-V" ] )
161+ }
162+
163+ #[ must_use]
164+ pub fn get_channel ( compiler_version : Option < String > ) -> String {
156165 if let Ok ( channel) = std:: env:: var ( "CFG_RELEASE_CHANNEL" ) {
157166 return channel;
158167 }
159168
160169 // if that failed, try to ask rustc -V, do some parsing and find out
161- if let Some ( rustc_output) = get_output ( "rustc" , & [ "-V" ] ) {
170+ if let Some ( rustc_output) = compiler_version {
162171 if rustc_output. contains ( "beta" ) {
163172 return String :: from ( "beta" ) ;
164- } else if rustc_output. contains ( "stable " ) {
165- return String :: from ( "stable " ) ;
173+ } else if rustc_output. contains ( "nightly " ) {
174+ return String :: from ( "nightly " ) ;
166175 }
167176 }
168177
169- // default to nightly
170- String :: from ( "nightly " )
178+ // default to stable
179+ String :: from ( "stable " )
171180}
172181
173182#[ cfg( test) ]
@@ -179,17 +188,19 @@ mod test {
179188 let vi = get_version_info ! ( ) ;
180189 assert_eq ! ( vi. major, 0 ) ;
181190 assert_eq ! ( vi. minor, 4 ) ;
182- assert_eq ! ( vi. patch, 0 ) ;
191+ assert_eq ! ( vi. patch, 1 ) ;
183192 assert_eq ! ( vi. crate_name, "rustc_tools_util" ) ;
184193 // hard to make positive tests for these since they will always change
185194 assert ! ( vi. commit_hash. is_none( ) ) ;
186195 assert ! ( vi. commit_date. is_none( ) ) ;
196+
197+ assert ! ( vi. host_compiler. is_none( ) ) ;
187198 }
188199
189200 #[ test]
190201 fn test_display_local ( ) {
191202 let vi = get_version_info ! ( ) ;
192- assert_eq ! ( vi. to_string( ) , "rustc_tools_util 0.4.0 " ) ;
203+ assert_eq ! ( vi. to_string( ) , "rustc_tools_util 0.4.1 " ) ;
193204 }
194205
195206 #[ test]
@@ -198,7 +209,7 @@ mod test {
198209 let s = format ! ( "{vi:?}" ) ;
199210 assert_eq ! (
200211 s,
201- "VersionInfo { crate_name: \" rustc_tools_util\" , major: 0, minor: 4, patch: 0 }"
212+ "VersionInfo { crate_name: \" rustc_tools_util\" , major: 0, minor: 4, patch: 1 }"
202213 ) ;
203214 }
204215}
0 commit comments