@@ -12,6 +12,7 @@ macro_rules! get_version_info {
12
12
let patch = std:: env!( "CARGO_PKG_VERSION_PATCH" ) . parse:: <u16 >( ) . unwrap( ) ;
13
13
let crate_name = String :: from( std:: env!( "CARGO_PKG_NAME" ) ) ;
14
14
15
+ let host_compiler_version = std:: option_env!( "RUSTC_VERSION" ) . map( str :: to_string) ;
15
16
let host_compiler = std:: option_env!( "RUSTC_RELEASE_CHANNEL" ) . map( str :: to_string) ;
16
17
let commit_hash = std:: option_env!( "GIT_HASH" ) . map( str :: to_string) ;
17
18
let commit_date = std:: option_env!( "COMMIT_DATE" ) . map( str :: to_string) ;
@@ -20,6 +21,7 @@ macro_rules! get_version_info {
20
21
major,
21
22
minor,
22
23
patch,
24
+ host_compiler_version,
23
25
host_compiler,
24
26
commit_hash,
25
27
commit_date,
@@ -29,7 +31,7 @@ macro_rules! get_version_info {
29
31
}
30
32
31
33
/// This macro can be used in `build.rs` to automatically set the needed
32
- /// environment values, namely `GIT_HASH`, `COMMIT_DATE` and
34
+ /// environment values, namely `GIT_HASH`, `COMMIT_DATE`, `RUSTC_VERSION` and
33
35
/// `RUSTC_RELEASE_CHANNEL`
34
36
#[ macro_export]
35
37
macro_rules! setup_version_info {
@@ -43,7 +45,15 @@ macro_rules! setup_version_info {
43
45
"cargo:rustc-env=COMMIT_DATE={}" ,
44
46
$crate:: get_commit_date( ) . unwrap_or_default( )
45
47
) ;
46
- println!( "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}" , $crate:: get_channel( ) ) ;
48
+ let compiler_version = $crate:: get_compiler_version( ) ;
49
+ println!(
50
+ "cargo:rustc-env=RUSTC_VERSION={}" ,
51
+ compiler_version. as_deref( ) . unwrap_or_default( )
52
+ ) ;
53
+ println!(
54
+ "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}" ,
55
+ $crate:: get_channel( compiler_version)
56
+ ) ;
47
57
} } ;
48
58
}
49
59
@@ -52,6 +62,7 @@ pub struct VersionInfo {
52
62
pub major : u8 ,
53
63
pub minor : u8 ,
54
64
pub patch : u16 ,
65
+ pub host_compiler_version : Option < String > ,
55
66
pub host_compiler : Option < String > ,
56
67
pub commit_hash : Option < String > ,
57
68
pub commit_date : Option < String > ,
@@ -87,16 +98,20 @@ impl std::fmt::Debug for VersionInfo {
87
98
"VersionInfo {{ crate_name: \" {}\" , major: {}, minor: {}, patch: {}" ,
88
99
self . crate_name, self . major, self . minor, self . patch,
89
100
) ?;
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, " }}" ) ?;
101
+ if let Some ( ref commit_hash) = self . commit_hash {
102
+ write ! ( f, ", commit_hash: \" {}\" " , commit_hash. trim( ) , ) ?;
99
103
}
104
+ if let Some ( ref commit_date) = self . commit_date {
105
+ write ! ( f, ", commit_date: \" {}\" " , commit_date. trim( ) ) ?;
106
+ }
107
+ if let Some ( ref version) = self . host_compiler_version {
108
+ write ! ( f, ", host_compiler_version: \" {}\" " , version. trim( ) ) ?;
109
+ }
110
+ if let Some ( ref host_compiler) = self . host_compiler {
111
+ write ! ( f, ", host_compiler: \" {}\" " , host_compiler. trim( ) ) ?;
112
+ }
113
+
114
+ write ! ( f, " }}" ) ?;
100
115
101
116
Ok ( ( ) )
102
117
}
@@ -152,13 +167,18 @@ pub fn get_commit_date() -> Option<String> {
152
167
}
153
168
154
169
#[ must_use]
155
- pub fn get_channel ( ) -> String {
170
+ pub fn get_compiler_version ( ) -> Option < String > {
171
+ get_output ( "rustc" , & [ "-V" ] )
172
+ }
173
+
174
+ #[ must_use]
175
+ pub fn get_channel ( compiler_version : Option < String > ) -> String {
156
176
if let Ok ( channel) = std:: env:: var ( "CFG_RELEASE_CHANNEL" ) {
157
177
return channel;
158
178
}
159
179
160
180
// if that failed, try to ask rustc -V, do some parsing and find out
161
- if let Some ( rustc_output) = get_output ( "rustc" , & [ "-V" ] ) {
181
+ if let Some ( rustc_output) = compiler_version {
162
182
if rustc_output. contains ( "beta" ) {
163
183
return String :: from ( "beta" ) ;
164
184
} else if rustc_output. contains ( "nightly" ) {
@@ -184,6 +204,8 @@ mod test {
184
204
// hard to make positive tests for these since they will always change
185
205
assert ! ( vi. commit_hash. is_none( ) ) ;
186
206
assert ! ( vi. commit_date. is_none( ) ) ;
207
+
208
+ assert ! ( vi. host_compiler. is_none( ) ) ;
187
209
}
188
210
189
211
#[ test]
0 commit comments