Skip to content

Commit f43e4f3

Browse files
committed
Auto merge of #9927 - xFrednet:0000-rustc-tool-macro-update, r=matthiaskrgr
Cleanup `rustc_tool_util` and add a convenient macro for `build.rs` changelog: none <!-- changelog_checked --> If possible, I'd like to have a new release for this, maybe `v0.3.0` to use the changes in another project. Then we can also remove the `path = "./rustc_tools_util"` from `Cargo.toml`. I'd be happy to help with the release on crates.io if you'd like the help :) r? `@matthiaskrgr`
2 parents be15e60 + 004b885 commit f43e4f3

File tree

9 files changed

+60
-55
lines changed

9 files changed

+60
-55
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ path = "src/driver.rs"
2323
[dependencies]
2424
clippy_lints = { path = "clippy_lints" }
2525
semver = "1.0"
26-
rustc_tools_util = "0.2.1"
26+
rustc_tools_util = "0.3.0"
2727
tempfile = { version = "3.2", optional = true }
2828
termize = "0.1"
2929

@@ -56,7 +56,7 @@ tokio = { version = "1", features = ["io-util"] }
5656
rustc-semver = "1.1"
5757

5858
[build-dependencies]
59-
rustc_tools_util = "0.2.1"
59+
rustc_tools_util = "0.3.0"
6060

6161
[features]
6262
deny-warnings = ["clippy_lints/deny-warnings"]

build.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,5 @@ fn main() {
33
println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap());
44
// Don't rebuild even if nothing changed
55
println!("cargo:rerun-if-changed=build.rs");
6-
// forward git repo hashes we build at
7-
println!(
8-
"cargo:rustc-env=GIT_HASH={}",
9-
rustc_tools_util::get_commit_hash().unwrap_or_default()
10-
);
11-
println!(
12-
"cargo:rustc-env=COMMIT_DATE={}",
13-
rustc_tools_util::get_commit_date().unwrap_or_default()
14-
);
15-
println!(
16-
"cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}",
17-
rustc_tools_util::get_channel()
18-
);
6+
rustc_tools_util::setup_version_info!();
197
}

rustc_tools_util/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
## Version 0.3.0
4+
5+
* Added `setup_version_info!();` macro for automated scripts.
6+
* `get_version_info!()` no longer requires the user to import `rustc_tools_util::VersionInfo` and `std::env`

rustc_tools_util/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustc_tools_util"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
description = "small helper to generate version information for git packages"
55
repository = "https://github.com/rust-lang/rust-clippy"
66
readme = "README.md"

rustc_tools_util/README.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,39 @@ build = "build.rs"
1313
List rustc_tools_util as regular AND build dependency.
1414
````toml
1515
[dependencies]
16-
rustc_tools_util = "0.2.1"
16+
rustc_tools_util = "0.3.0"
1717

1818
[build-dependencies]
19-
rustc_tools_util = "0.2.1"
19+
rustc_tools_util = "0.3.0"
2020
````
2121

2222
In `build.rs`, generate the data in your `main()`
23-
````rust
23+
24+
```rust
2425
fn main() {
25-
println!(
26-
"cargo:rustc-env=GIT_HASH={}",
27-
rustc_tools_util::get_commit_hash().unwrap_or_default()
28-
);
29-
println!(
30-
"cargo:rustc-env=COMMIT_DATE={}",
31-
rustc_tools_util::get_commit_date().unwrap_or_default()
32-
);
33-
println!(
34-
"cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}",
35-
rustc_tools_util::get_channel().unwrap_or_default()
36-
);
26+
rustc_tools_util::setup_version_info!();
3727
}
38-
39-
````
28+
```
4029

4130
Use the version information in your main.rs
42-
````rust
43-
use rustc_tools_util::*;
4431

32+
```rust
4533
fn show_version() {
4634
let version_info = rustc_tools_util::get_version_info!();
4735
println!("{}", version_info);
4836
}
49-
````
37+
```
38+
5039
This gives the following output in clippy:
51-
`clippy 0.0.212 (a416c5e 2018-12-14)`
40+
`clippy 0.1.66 (a28f3c8 2022-11-20)`
41+
42+
## Repository
43+
44+
This project is part of the rust-lang/rust-clippy repository. The source code
45+
can be found under `./rustc_tools_util/`.
5246

47+
The changelog for `rustc_tools_util` is available under:
48+
[`rustc_tools_util/CHANGELOG.md`](https://github.com/rust-lang/rust-clippy/blob/master/rustc_tools_util/CHANGELOG.md)
5349

5450
## License
5551

rustc_tools_util/src/lib.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22

3-
use std::env;
4-
3+
/// This macro creates the version string during compilation from the
4+
/// current environment
55
#[macro_export]
66
macro_rules! get_version_info {
77
() => {{
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"));
1212

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);
1616

17-
VersionInfo {
17+
$crate::VersionInfo {
1818
major,
1919
minor,
2020
patch,
@@ -26,6 +26,24 @@ macro_rules! get_version_info {
2626
}};
2727
}
2828

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+
2947
// some code taken and adapted from RLS and cargo
3048
pub struct VersionInfo {
3149
pub major: u8,
@@ -101,7 +119,7 @@ pub fn get_commit_date() -> Option<String> {
101119

102120
#[must_use]
103121
pub fn get_channel() -> String {
104-
match env::var("CFG_RELEASE_CHANNEL") {
122+
match std::env::var("CFG_RELEASE_CHANNEL") {
105123
Ok(channel) => channel,
106124
Err(_) => {
107125
// if that failed, try to ask rustc -V, do some parsing and find out
@@ -136,8 +154,8 @@ mod test {
136154
fn test_struct_local() {
137155
let vi = get_version_info!();
138156
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);
141159
assert_eq!(vi.crate_name, "rustc_tools_util");
142160
// hard to make positive tests for these since they will always change
143161
assert!(vi.commit_hash.is_none());
@@ -147,7 +165,7 @@ mod test {
147165
#[test]
148166
fn test_display_local() {
149167
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");
151169
}
152170

153171
#[test]
@@ -156,7 +174,7 @@ mod test {
156174
let s = format!("{vi:?}");
157175
assert_eq!(
158176
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 }"
160178
);
161179
}
162180
}

src/driver.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ extern crate rustc_span;
1919
use rustc_interface::interface;
2020
use rustc_session::parse::ParseSess;
2121
use rustc_span::symbol::Symbol;
22-
use rustc_tools_util::VersionInfo;
2322

2423
use std::borrow::Cow;
2524
use std::env;

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// warn on lints, that are included in `rust-lang/rust`s bootstrap
33
#![warn(rust_2018_idioms, unused_lifetimes)]
44

5-
use rustc_tools_util::VersionInfo;
65
use std::env;
76
use std::path::PathBuf;
87
use std::process::{self, Command};

tests/versioncheck.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![warn(rust_2018_idioms, unused_lifetimes)]
33
#![allow(clippy::single_match_else)]
44

5-
use rustc_tools_util::VersionInfo;
65
use std::fs;
76

87
#[test]

0 commit comments

Comments
 (0)