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    // This can return e.g.
28    // -e607be166673a8de9fc07f6f02c60426e556c5f2 src/gcc
29    //  e607be166673a8de9fc07f6f02c60426e556c5f2 src/gcc (master-e607be166673a8de9fc07f6f02c60426e556c5f2.e607be)
30    // +e607be166673a8de9fc07f6f02c60426e556c5f2 src/gcc (master-e607be166673a8de9fc07f6f02c60426e556c5f2.e607be)
31    let git_output = String::from_utf8_lossy(&git_output.stdout)
32        .split_whitespace()
33        .next()
34        .unwrap_or_default()
35        .to_string();
36
37    // The SHA can start with + if the submodule is modified or - if it is not checked out.
38    let gcc_submodule_sha = git_output.trim_start_matches(['+', '-']);
39    if gcc_submodule_sha != cg_gcc_version {
40        *bad = true;
41        eprintln!(
42            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}`).
43Make sure to set the src/gcc submodule to commit {cg_gcc_version}.
44The GCC codegen backend commit is configured at {}."#,
45            cg_gcc_version_path.display(),
46        );
47    }
48}