Skip to content

Cleanup rustc_tool_util and add a convenient macro for build.rs #9927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ path = "src/driver.rs"
[dependencies]
clippy_lints = { path = "clippy_lints" }
semver = "1.0"
rustc_tools_util = "0.2.1"
rustc_tools_util = "0.3.0"
tempfile = { version = "3.2", optional = true }
termize = "0.1"

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

[build-dependencies]
rustc_tools_util = "0.2.1"
rustc_tools_util = "0.3.0"

[features]
deny-warnings = ["clippy_lints/deny-warnings"]
Expand Down
14 changes: 1 addition & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,5 @@ fn main() {
println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap());
// Don't rebuild even if nothing changed
println!("cargo:rerun-if-changed=build.rs");
// forward git repo hashes we build at
println!(
"cargo:rustc-env=GIT_HASH={}",
rustc_tools_util::get_commit_hash().unwrap_or_default()
);
println!(
"cargo:rustc-env=COMMIT_DATE={}",
rustc_tools_util::get_commit_date().unwrap_or_default()
);
println!(
"cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}",
rustc_tools_util::get_channel()
);
rustc_tools_util::setup_version_info!();
}
6 changes: 6 additions & 0 deletions rustc_tools_util/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## Version 0.3.0

* Added `setup_version_info!();` macro for automated scripts.
* `get_version_info!()` no longer requires the user to import `rustc_tools_util::VersionInfo` and `std::env`
2 changes: 1 addition & 1 deletion rustc_tools_util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustc_tools_util"
version = "0.2.1"
version = "0.3.0"
description = "small helper to generate version information for git packages"
repository = "https://github.com/rust-lang/rust-clippy"
readme = "README.md"
Expand Down
38 changes: 17 additions & 21 deletions rustc_tools_util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,39 @@ build = "build.rs"
List rustc_tools_util as regular AND build dependency.
````toml
[dependencies]
rustc_tools_util = "0.2.1"
rustc_tools_util = "0.3.0"

[build-dependencies]
rustc_tools_util = "0.2.1"
rustc_tools_util = "0.3.0"
````

In `build.rs`, generate the data in your `main()`
````rust

```rust
fn main() {
println!(
"cargo:rustc-env=GIT_HASH={}",
rustc_tools_util::get_commit_hash().unwrap_or_default()
);
println!(
"cargo:rustc-env=COMMIT_DATE={}",
rustc_tools_util::get_commit_date().unwrap_or_default()
);
println!(
"cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}",
rustc_tools_util::get_channel().unwrap_or_default()
);
rustc_tools_util::setup_version_info!();
}

````
```

Use the version information in your main.rs
````rust
use rustc_tools_util::*;

```rust
fn show_version() {
let version_info = rustc_tools_util::get_version_info!();
println!("{}", version_info);
}
````
```

This gives the following output in clippy:
`clippy 0.0.212 (a416c5e 2018-12-14)`
`clippy 0.1.66 (a28f3c8 2022-11-20)`

## Repository

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

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

## License

Expand Down
48 changes: 33 additions & 15 deletions rustc_tools_util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#![cfg_attr(feature = "deny-warnings", deny(warnings))]

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,
Expand All @@ -26,6 +26,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,
Expand Down Expand Up @@ -101,7 +119,7 @@ pub fn get_commit_date() -> Option<String> {

#[must_use]
pub fn get_channel() -> String {
match env::var("CFG_RELEASE_CHANNEL") {
match std::env::var("CFG_RELEASE_CHANNEL") {
Ok(channel) => channel,
Err(_) => {
// if that failed, try to ask rustc -V, do some parsing and find out
Expand Down Expand Up @@ -136,8 +154,8 @@ mod test {
fn test_struct_local() {
let vi = get_version_info!();
assert_eq!(vi.major, 0);
assert_eq!(vi.minor, 2);
assert_eq!(vi.patch, 1);
assert_eq!(vi.minor, 3);
assert_eq!(vi.patch, 0);
assert_eq!(vi.crate_name, "rustc_tools_util");
// hard to make positive tests for these since they will always change
assert!(vi.commit_hash.is_none());
Expand All @@ -147,7 +165,7 @@ mod test {
#[test]
fn test_display_local() {
let vi = get_version_info!();
assert_eq!(vi.to_string(), "rustc_tools_util 0.2.1");
assert_eq!(vi.to_string(), "rustc_tools_util 0.3.0");
}

#[test]
Expand All @@ -156,7 +174,7 @@ mod test {
let s = format!("{vi:?}");
assert_eq!(
s,
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 2, patch: 1 }"
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 3, patch: 0 }"
);
}
}
1 change: 0 additions & 1 deletion src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ extern crate rustc_span;
use rustc_interface::interface;
use rustc_session::parse::ParseSess;
use rustc_span::symbol::Symbol;
use rustc_tools_util::VersionInfo;

use std::borrow::Cow;
use std::env;
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// warn on lints, that are included in `rust-lang/rust`s bootstrap
#![warn(rust_2018_idioms, unused_lifetimes)]

use rustc_tools_util::VersionInfo;
use std::env;
use std::path::PathBuf;
use std::process::{self, Command};
Expand Down
1 change: 0 additions & 1 deletion tests/versioncheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(clippy::single_match_else)]

use rustc_tools_util::VersionInfo;
use std::fs;

#[test]
Expand Down