Skip to content

Commit aa66212

Browse files
committed
Cleanup rustc_tool_util and add a convenient macro for build.rs
1 parent 73efce9 commit aa66212

File tree

7 files changed

+43
-45
lines changed

7 files changed

+43
-45
lines changed

Cargo.toml

+2-2
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 = {version = "0.2.1", path = "./rustc_tools_util"}
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 = {version = "0.2.1", path = "./rustc_tools_util"}
6060

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

build.rs

+1-13
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/README.md

+12-19
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,29 @@ rustc_tools_util = "0.2.1"
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
5243

44+
This project is part of the rust-lang/rust-clippy repository. The source code
45+
can be found under `./rustc_tools_util/`.
5346

5447
## License
5548

rustc_tools_util/src/lib.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
use std::env;
44

5+
/// This macro creates the version string during compilation from the
6+
/// current environment
57
#[macro_export]
68
macro_rules! get_version_info {
79
() => {{
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"));
10+
let major = std::env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
11+
let minor = std::env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
12+
let patch = std::env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
13+
let crate_name = String::from(std::env!("CARGO_PKG_NAME"));
1214

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);
15+
let host_compiler = std::option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string);
16+
let commit_hash = std::option_env!("GIT_HASH").map(str::to_string);
17+
let commit_date = std::option_env!("COMMIT_DATE").map(str::to_string);
1618

17-
VersionInfo {
19+
$crate::VersionInfo {
1820
major,
1921
minor,
2022
patch,
@@ -26,6 +28,24 @@ macro_rules! get_version_info {
2628
}};
2729
}
2830

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`
34+
#[macro_export]
35+
macro_rules! setup_version_info {
36+
() => {{
37+
println!(
38+
"cargo:rustc-env=GIT_HASH={}",
39+
$crate::get_commit_hash().unwrap_or_default()
40+
);
41+
println!(
42+
"cargo:rustc-env=COMMIT_DATE={}",
43+
$crate::get_commit_date().unwrap_or_default()
44+
);
45+
println!("cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", $crate::get_channel());
46+
}};
47+
}
48+
2949
// some code taken and adapted from RLS and cargo
3050
pub struct VersionInfo {
3151
pub major: u8,

src/driver.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extern crate rustc_span;
1818
use rustc_interface::interface;
1919
use rustc_session::parse::ParseSess;
2020
use rustc_span::symbol::Symbol;
21-
use rustc_tools_util::VersionInfo;
2221

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

src/main.rs

-1
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

-1
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)