@@ -28,9 +28,8 @@ macro_rules! get_version_info {
28
28
} } ;
29
29
}
30
30
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`
34
33
#[ macro_export]
35
34
macro_rules! setup_version_info {
36
35
( ) => { {
@@ -43,7 +42,11 @@ macro_rules! setup_version_info {
43
42
"cargo:rustc-env=COMMIT_DATE={}" ,
44
43
$crate:: get_commit_date( ) . unwrap_or_default( )
45
44
) ;
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
+ ) ;
47
50
} } ;
48
51
}
49
52
@@ -87,16 +90,17 @@ impl std::fmt::Debug for VersionInfo {
87
90
"VersionInfo {{ crate_name: \" {}\" , major: {}, minor: {}, patch: {}" ,
88
91
self . crate_name, self . major, self . minor, self . patch,
89
92
) ?;
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( ) ) ?;
99
98
}
99
+ if let Some ( ref host_compiler) = self . host_compiler {
100
+ write ! ( f, ", host_compiler: \" {}\" " , host_compiler. trim( ) ) ?;
101
+ }
102
+
103
+ write ! ( f, " }}" ) ?;
100
104
101
105
Ok ( ( ) )
102
106
}
@@ -152,22 +156,27 @@ pub fn get_commit_date() -> Option<String> {
152
156
}
153
157
154
158
#[ 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 {
156
165
if let Ok ( channel) = std:: env:: var ( "CFG_RELEASE_CHANNEL" ) {
157
166
return channel;
158
167
}
159
168
160
169
// 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 {
162
171
if rustc_output. contains ( "beta" ) {
163
172
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 " ) ;
166
175
}
167
176
}
168
177
169
- // default to nightly
170
- String :: from ( "nightly " )
178
+ // default to stable
179
+ String :: from ( "stable " )
171
180
}
172
181
173
182
#[ cfg( test) ]
@@ -179,17 +188,19 @@ mod test {
179
188
let vi = get_version_info ! ( ) ;
180
189
assert_eq ! ( vi. major, 0 ) ;
181
190
assert_eq ! ( vi. minor, 4 ) ;
182
- assert_eq ! ( vi. patch, 0 ) ;
191
+ assert_eq ! ( vi. patch, 1 ) ;
183
192
assert_eq ! ( vi. crate_name, "rustc_tools_util" ) ;
184
193
// hard to make positive tests for these since they will always change
185
194
assert ! ( vi. commit_hash. is_none( ) ) ;
186
195
assert ! ( vi. commit_date. is_none( ) ) ;
196
+
197
+ assert ! ( vi. host_compiler. is_none( ) ) ;
187
198
}
188
199
189
200
#[ test]
190
201
fn test_display_local ( ) {
191
202
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 " ) ;
193
204
}
194
205
195
206
#[ test]
@@ -198,7 +209,7 @@ mod test {
198
209
let s = format ! ( "{vi:?}" ) ;
199
210
assert_eq ! (
200
211
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 }"
202
213
) ;
203
214
}
204
215
}
0 commit comments