Added toolchain check to rustc_codegen_spirv (#919)

* Added toolchain check to rustc_codegen_spirv

* Removed unused dependency

* Reworked the toolchain check

* Removed some debug code
This commit is contained in:
Sylvester Hesp 2022-09-23 11:35:23 +02:00 committed by GitHub
parent 9e2e66729d
commit f00f267160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 2 deletions

4
Cargo.lock generated
View File

@ -2506,9 +2506,9 @@ checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1"
[[package]]
name = "version_check"
version = "0.9.3"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"

View File

@ -26,6 +26,12 @@ use-installed-tools = ["spirv-tools/use-installed-tools"]
# If enabled will compile and link the C++ code for the spirv tools, the compiled
# version is preferred if both this and `use-installed-tools` are enabled
use-compiled-tools = ["spirv-tools/use-compiled-tools"]
# If enabled, this will not check whether the current rustc version is set to the
# appropriate channel. rustc_cogeden_spirv requires a specific nightly version,
# and will likely produce compile errors when built against a different toolchain.
# Enable this feature to be able to experiment with other versions.
skip-toolchain-check = []
[dependencies]
# HACK(eddyb) these only exist to unify features across dependency trees,

View File

@ -0,0 +1,70 @@
//! This custom build script merely checks whether we're compiling with the appropriate Rust toolchain
#![allow(clippy::string_add)]
use std::error::Error;
use std::process::{Command, ExitCode};
/// Current `rust-toolchain` file
/// Unfortunately, directly including the actual workspace `rust-toolchain` doesn't work together with
/// `cargo publish`. We need to figure out a way to do this properly, but let's hardcode it for now :/
//const REQUIRED_RUST_TOOLCHAIN: &str = include_str!("../../rust-toolchain");
const REQUIRED_RUST_TOOLCHAIN: &str = r#"[toolchain]
channel = "nightly-2022-08-29"
components = ["rust-src", "rustc-dev", "llvm-tools-preview"]
# commit_hash = ce36e88256f09078519f8bc6b21e4dc88f88f523"#;
fn get_rustc_commit_hash() -> Result<String, Box<dyn Error>> {
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| String::from("rustc"));
String::from_utf8(Command::new(rustc).arg("-vV").output()?.stdout)?
.lines()
.find_map(|l| l.strip_prefix("commit-hash: "))
.map(|s| s.to_string())
.ok_or_else(|| Box::<dyn Error>::from("`commit-hash` not found in `rustc -vV` output"))
}
fn get_required_commit_hash() -> Result<String, Box<dyn Error>> {
REQUIRED_RUST_TOOLCHAIN
.lines()
.find_map(|l| l.strip_prefix("# commit_hash = "))
.map(|s| s.to_string())
.ok_or_else(|| Box::<dyn Error>::from("`commit_hash` not found in `rust-toolchain`"))
}
fn check_toolchain_version() -> Result<(), Box<dyn Error>> {
if !cfg!(feature = "skip-toolchain-check") {
// gets the commit hash from current rustc
let current_hash = get_rustc_commit_hash()?;
let required_hash = get_required_commit_hash()?;
if current_hash != required_hash {
let stripped_toolchain = REQUIRED_RUST_TOOLCHAIN
.lines()
.filter(|l| !l.trim().is_empty() && !l.starts_with("# "))
.map(|l| l.to_string())
.reduce(|a, b| a + "\n" + &b)
.unwrap_or_default();
return Err(Box::<dyn Error>::from(format!(
r#"
error: wrong toolchain detected (found commit hash `{current_hash}`, expected `{required_hash}`).
Make sure your `rust_toolchain` file contains the following:
-------------
{stripped_toolchain}
-------------"#
)));
}
}
Ok(())
}
fn main() -> ExitCode {
match check_toolchain_version() {
Ok(_) => ExitCode::SUCCESS,
Err(e) => {
eprint!("{}", e);
ExitCode::FAILURE
}
}
}

View File

@ -12,6 +12,7 @@ description = "Helper for building shaders with rust-gpu"
default = ["use-compiled-tools"]
use-installed-tools = ["rustc_codegen_spirv/use-installed-tools"]
use-compiled-tools = ["rustc_codegen_spirv/use-compiled-tools"]
skip-toolchain-check = ["rustc_codegen_spirv/skip-toolchain-check"]
watch = ["notify"]
[dependencies]

View File

@ -7,3 +7,8 @@
[toolchain]
channel = "nightly-2022-08-29"
components = ["rust-src", "rustc-dev", "llvm-tools-preview"]
# commit_hash = ce36e88256f09078519f8bc6b21e4dc88f88f523
# Whenever changing the nightly channel, update the commit hash above, and make
# sure to change REQUIRED_TOOLCHAIN in crates/rustc_codegen_spirv/src/build.rs also.