tidy/gcc_submodule.rs
1//! Tidy check to ensure that the commit SHA of the `src/gcc` submodule is the same as the
2//! required GCC version of the GCC codegen backend.
3
4use std::path::Path;
5use std::process::Command;
6
7pub fn check(root_path: &Path, compiler_path: &Path, bad: &mut bool) {
8 let cg_gcc_version_path = compiler_path.join("rustc_codegen_gcc/libgccjit.version");
9 let cg_gcc_version = std::fs::read_to_string(&cg_gcc_version_path)
10 .unwrap_or_else(|_| {
11 panic!("Cannot read GCC version from {}", cg_gcc_version_path.display())
12 })
13 .trim()
14 .to_string();
15
16 let git_output = Command::new("git")
17 .current_dir(root_path)
18 .arg("submodule")
19 .arg("status")
20 // --cached asks for the version that is actually committed in the repository, not the one
21 // that is currently checked out.
22 .arg("--cached")
23 .arg("src/gcc")
24 .output()
25 .expect("Cannot determine git SHA of the src/gcc checkout");
26
27 // Git is not available or we are in a tarball
28 if !git_output.status.success() {
29 eprintln!("Cannot figure out the SHA of the GCC submodule");
30 return;
31 }
32
33 // This can return e.g.
34 // -e607be166673a8de9fc07f6f02c60426e556c5f2 src/gcc
35 // e607be166673a8de9fc07f6f02c60426e556c5f2 src/gcc (master-e607be166673a8de9fc07f6f02c60426e556c5f2.e607be)
36 // +e607be166673a8de9fc07f6f02c60426e556c5f2 src/gcc (master-e607be166673a8de9fc07f6f02c60426e556c5f2.e607be)
37 let git_output = String::from_utf8_lossy(&git_output.stdout)
38 .split_whitespace()
39 .next()
40 .unwrap_or_default()
41 .to_string();
42
43 // The SHA can start with + if the submodule is modified or - if it is not checked out.
44 let gcc_submodule_sha = git_output.trim_start_matches(['+', '-']);
45 if gcc_submodule_sha != cg_gcc_version {
46 *bad = true;
47 eprintln!(
48 r#"Commit SHA of the src/gcc submodule (`{gcc_submodule_sha}`) does not match the required GCC version of the GCC codegen backend (`{cg_gcc_version}`).
49Make sure to set the src/gcc submodule to commit {cg_gcc_version}.
50The GCC codegen backend commit is configured at {}."#,
51 cg_gcc_version_path.display(),
52 );
53 }
54}