1
1
#![ cfg_attr( feature = "deny-warnings" , deny( warnings) ) ]
2
2
3
- use std :: env ;
4
-
3
+ /// This macro creates the version string during compilation from the
4
+ /// current environment
5
5
#[ macro_export]
6
6
macro_rules! get_version_info {
7
7
( ) => { {
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" ) ) ;
12
12
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) ;
16
16
17
- VersionInfo {
17
+ $crate :: VersionInfo {
18
18
major,
19
19
minor,
20
20
patch,
@@ -26,6 +26,24 @@ macro_rules! get_version_info {
26
26
} } ;
27
27
}
28
28
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
+
29
47
// some code taken and adapted from RLS and cargo
30
48
pub struct VersionInfo {
31
49
pub major : u8 ,
@@ -101,7 +119,7 @@ pub fn get_commit_date() -> Option<String> {
101
119
102
120
#[ must_use]
103
121
pub fn get_channel ( ) -> String {
104
- match env:: var ( "CFG_RELEASE_CHANNEL" ) {
122
+ match std :: env:: var ( "CFG_RELEASE_CHANNEL" ) {
105
123
Ok ( channel) => channel,
106
124
Err ( _) => {
107
125
// if that failed, try to ask rustc -V, do some parsing and find out
@@ -136,8 +154,8 @@ mod test {
136
154
fn test_struct_local ( ) {
137
155
let vi = get_version_info ! ( ) ;
138
156
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 ) ;
141
159
assert_eq ! ( vi. crate_name, "rustc_tools_util" ) ;
142
160
// hard to make positive tests for these since they will always change
143
161
assert ! ( vi. commit_hash. is_none( ) ) ;
@@ -147,7 +165,7 @@ mod test {
147
165
#[ test]
148
166
fn test_display_local ( ) {
149
167
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 " ) ;
151
169
}
152
170
153
171
#[ test]
@@ -156,7 +174,7 @@ mod test {
156
174
let s = format ! ( "{vi:?}" ) ;
157
175
assert_eq ! (
158
176
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 }"
160
178
) ;
161
179
}
162
180
}
0 commit comments