Skip to content

Commit

Permalink
Verify that rustc is at least the version from rust-toolchain.toml
Browse files Browse the repository at this point in the history
Issue: #5452
  • Loading branch information
mina86 committed Nov 29, 2021
1 parent b0be2f7 commit 0ba0511
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions neard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ near-primitives = { path = "../core/primitives" }
near-performance-metrics = { path = "../utils/near-performance-metrics" }
near-state-viewer = { path = "../test-utils/state-viewer", package = "state-viewer" }

[build-dependencies]
version_check = "0.9"
toml = "0.5"

[features]
default = ["json_rpc", "jemalloc"]

Expand Down
50 changes: 50 additions & 0 deletions neard/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const FILENAME: &'static str = "../rust-toolchain.toml";
const KEY_PATH: &'static str = "toolchain.channel";

fn get_expected_version() -> Result<version_check::Version, String> {
let toml = std::fs::read_to_string(FILENAME)
.map_err(|err| format!("{}: {}", FILENAME, err))?
.parse::<toml::Value>()
.map_err(|err| format!("{}: {}", FILENAME, err))?;
let channel = toml
.get("toolchain")
.and_then(|value| value.get("channel"))
.ok_or_else(|| format!("{}: {}: missing key", FILENAME, KEY_PATH))?;
let channel = match channel {
toml::Value::String(string) => string,
value => {
return Err(format!(
"{}: {}: expected string but got {}",
FILENAME,
KEY_PATH,
value.type_str()
))
}
};
version_check::Version::parse(&channel).ok_or_else(|| {
format!("{}: {}: expected valid version string got ‘{}’", FILENAME, KEY_PATH, channel)
})
}

fn get_current_version() -> Result<version_check::Version, String> {
version_check::Version::read().ok_or_else(|| String::from("rustc: unable to determine version"))
}

fn run() -> Result<(), String> {
let expected = get_expected_version()?;
let current = get_current_version()?;

if current >= expected {
Ok(())
} else {
Err(format!("rust: expected at least version {} but got {}", expected, current))
}
}

fn main() {
println!("cargo:rerun-if-changed=../rust-toolchain.toml");
if let Err(msg) = run() {
eprintln!("{}", msg);
std::process::exit(1);
}
}

0 comments on commit 0ba0511

Please sign in to comment.