Skip to content

Commit f00f267

Browse files
author
Sylvester Hesp
authored
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
1 parent 9e2e667 commit f00f267

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rustc_codegen_spirv/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ use-installed-tools = ["spirv-tools/use-installed-tools"]
2626
# If enabled will compile and link the C++ code for the spirv tools, the compiled
2727
# version is preferred if both this and `use-installed-tools` are enabled
2828
use-compiled-tools = ["spirv-tools/use-compiled-tools"]
29+
# If enabled, this will not check whether the current rustc version is set to the
30+
# appropriate channel. rustc_cogeden_spirv requires a specific nightly version,
31+
# and will likely produce compile errors when built against a different toolchain.
32+
# Enable this feature to be able to experiment with other versions.
33+
skip-toolchain-check = []
34+
2935

3036
[dependencies]
3137
# HACK(eddyb) these only exist to unify features across dependency trees,

crates/rustc_codegen_spirv/build.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//! This custom build script merely checks whether we're compiling with the appropriate Rust toolchain
2+
3+
#![allow(clippy::string_add)]
4+
5+
use std::error::Error;
6+
use std::process::{Command, ExitCode};
7+
8+
/// Current `rust-toolchain` file
9+
/// Unfortunately, directly including the actual workspace `rust-toolchain` doesn't work together with
10+
/// `cargo publish`. We need to figure out a way to do this properly, but let's hardcode it for now :/
11+
//const REQUIRED_RUST_TOOLCHAIN: &str = include_str!("../../rust-toolchain");
12+
const REQUIRED_RUST_TOOLCHAIN: &str = r#"[toolchain]
13+
channel = "nightly-2022-08-29"
14+
components = ["rust-src", "rustc-dev", "llvm-tools-preview"]
15+
# commit_hash = ce36e88256f09078519f8bc6b21e4dc88f88f523"#;
16+
17+
fn get_rustc_commit_hash() -> Result<String, Box<dyn Error>> {
18+
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| String::from("rustc"));
19+
String::from_utf8(Command::new(rustc).arg("-vV").output()?.stdout)?
20+
.lines()
21+
.find_map(|l| l.strip_prefix("commit-hash: "))
22+
.map(|s| s.to_string())
23+
.ok_or_else(|| Box::<dyn Error>::from("`commit-hash` not found in `rustc -vV` output"))
24+
}
25+
26+
fn get_required_commit_hash() -> Result<String, Box<dyn Error>> {
27+
REQUIRED_RUST_TOOLCHAIN
28+
.lines()
29+
.find_map(|l| l.strip_prefix("# commit_hash = "))
30+
.map(|s| s.to_string())
31+
.ok_or_else(|| Box::<dyn Error>::from("`commit_hash` not found in `rust-toolchain`"))
32+
}
33+
34+
fn check_toolchain_version() -> Result<(), Box<dyn Error>> {
35+
if !cfg!(feature = "skip-toolchain-check") {
36+
// gets the commit hash from current rustc
37+
38+
let current_hash = get_rustc_commit_hash()?;
39+
let required_hash = get_required_commit_hash()?;
40+
if current_hash != required_hash {
41+
let stripped_toolchain = REQUIRED_RUST_TOOLCHAIN
42+
.lines()
43+
.filter(|l| !l.trim().is_empty() && !l.starts_with("# "))
44+
.map(|l| l.to_string())
45+
.reduce(|a, b| a + "\n" + &b)
46+
.unwrap_or_default();
47+
48+
return Err(Box::<dyn Error>::from(format!(
49+
r#"
50+
error: wrong toolchain detected (found commit hash `{current_hash}`, expected `{required_hash}`).
51+
Make sure your `rust_toolchain` file contains the following:
52+
-------------
53+
{stripped_toolchain}
54+
-------------"#
55+
)));
56+
}
57+
}
58+
59+
Ok(())
60+
}
61+
62+
fn main() -> ExitCode {
63+
match check_toolchain_version() {
64+
Ok(_) => ExitCode::SUCCESS,
65+
Err(e) => {
66+
eprint!("{}", e);
67+
ExitCode::FAILURE
68+
}
69+
}
70+
}

crates/spirv-builder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ description = "Helper for building shaders with rust-gpu"
1212
default = ["use-compiled-tools"]
1313
use-installed-tools = ["rustc_codegen_spirv/use-installed-tools"]
1414
use-compiled-tools = ["rustc_codegen_spirv/use-compiled-tools"]
15+
skip-toolchain-check = ["rustc_codegen_spirv/skip-toolchain-check"]
1516
watch = ["notify"]
1617

1718
[dependencies]

rust-toolchain

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
[toolchain]
88
channel = "nightly-2022-08-29"
99
components = ["rust-src", "rustc-dev", "llvm-tools-preview"]
10+
# commit_hash = ce36e88256f09078519f8bc6b21e4dc88f88f523
11+
12+
# Whenever changing the nightly channel, update the commit hash above, and make
13+
# sure to change REQUIRED_TOOLCHAIN in crates/rustc_codegen_spirv/src/build.rs also.
14+

0 commit comments

Comments
 (0)