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