diff options
| author | xFrednet <xFrednet@gmail.com> | 2022-11-21 22:55:14 +0100 |
|---|---|---|
| committer | xFrednet <xFrednet@gmail.com> | 2022-11-21 23:28:26 +0100 |
| commit | aa66212e291cf6d726d58d139b7ab19631431bd5 (patch) | |
| tree | 38eae3b8f3d17d8423a09c1524ada6ed78399fef /rustc_tools_util/src | |
| parent | 73efce9ee6e971f45b20976c1efd96888faf4a1d (diff) | |
Cleanup `rustc_tool_util` and add a convenient macro for `build.rs`
Diffstat (limited to 'rustc_tools_util/src')
| -rw-r--r-- | rustc_tools_util/src/lib.rs | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs index 01d25c53126..5e856319c88 100644 --- a/rustc_tools_util/src/lib.rs +++ b/rustc_tools_util/src/lib.rs @@ -2,19 +2,21 @@ use std::env; +/// This macro creates the version string during compilation from the +/// current environment #[macro_export] macro_rules! get_version_info { () => {{ - let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap(); - let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap(); - let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap(); - let crate_name = String::from(env!("CARGO_PKG_NAME")); + let major = std::env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap(); + let minor = std::env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap(); + let patch = std::env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap(); + let crate_name = String::from(std::env!("CARGO_PKG_NAME")); - let host_compiler = option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string); - let commit_hash = option_env!("GIT_HASH").map(str::to_string); - let commit_date = option_env!("COMMIT_DATE").map(str::to_string); + let host_compiler = std::option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string); + let commit_hash = std::option_env!("GIT_HASH").map(str::to_string); + let commit_date = std::option_env!("COMMIT_DATE").map(str::to_string); - VersionInfo { + $crate::VersionInfo { major, minor, patch, @@ -26,6 +28,24 @@ macro_rules! get_version_info { }}; } +/// This macro can be used in `build.rs` to automatically set the needed +/// environment values, namely `GIT_HASH`, `COMMIT_DATE` and +/// `RUSTC_RELEASE_CHANNEL` +#[macro_export] +macro_rules! setup_version_info { + () => {{ + println!( + "cargo:rustc-env=GIT_HASH={}", + $crate::get_commit_hash().unwrap_or_default() + ); + println!( + "cargo:rustc-env=COMMIT_DATE={}", + $crate::get_commit_date().unwrap_or_default() + ); + println!("cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", $crate::get_channel()); + }}; +} + // some code taken and adapted from RLS and cargo pub struct VersionInfo { pub major: u8, |
